org.el 891 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2013 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Bastien Guerry <bzg at gnu 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. (load "org-loaddefs.el" t t t)
  73. (require 'org-macs)
  74. (require 'org-compat)
  75. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  76. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  77. ;;
  78. ;; In Org buffers, the value of `outline-regexp' is that of
  79. ;; `org-outline-regexp'. The only function still directly relying on
  80. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  81. ;; job when `orgstruct-mode' is active.
  82. (defvar org-outline-regexp "\\*+ "
  83. "Regexp to match Org headlines.")
  84. (defvar org-outline-regexp-bol "^\\*+ "
  85. "Regexp to match Org headlines.
  86. This is similar to `org-outline-regexp' but additionally makes
  87. sure that we are at the beginning of the line.")
  88. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  89. "Matches a headline, putting stars and text into groups.
  90. Stars are put in group 1 and the trimmed body in group 2.")
  91. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  92. (unless (boundp 'calendar-view-holidays-initially-flag)
  93. (org-defvaralias 'calendar-view-holidays-initially-flag
  94. 'view-calendar-holidays-initially))
  95. (unless (boundp 'calendar-view-diary-initially-flag)
  96. (org-defvaralias 'calendar-view-diary-initially-flag
  97. 'view-diary-entries-initially))
  98. (unless (boundp 'diary-fancy-buffer)
  99. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  100. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  101. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  102. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  103. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  104. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  105. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  106. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  107. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  108. (declare-function orgtbl-mode "org-table" (&optional arg))
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-beamer-mode "ox-beamer" ())
  111. (declare-function org-table-edit-field "org-table" (arg))
  112. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  113. (declare-function org-table-set-constants "org-table" ())
  114. (declare-function org-id-get-create "org-id" (&optional force))
  115. (declare-function org-id-find-id-file "org-id" (id))
  116. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  117. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  118. (declare-function org-agenda-redo "org-agenda" (&optional all))
  119. (declare-function org-table-align "org-table" ())
  120. (declare-function org-table-paste-rectangle "org-table" ())
  121. (declare-function org-table-maybe-eval-formula "org-table" ())
  122. (declare-function org-table-maybe-recalculate-line "org-table" ())
  123. (declare-function org-element--parse-objects "org-element"
  124. (beg end acc restriction))
  125. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  126. (declare-function org-element-contents "org-element" (element))
  127. (declare-function org-element-context "org-element" (&optional element))
  128. (declare-function org-element-interpret-data "org-element"
  129. (data &optional parent))
  130. (declare-function org-element-map "org-element"
  131. (data types fun &optional info first-match no-recursion))
  132. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  133. (declare-function org-element-parse-buffer "org-element"
  134. (&optional granularity visible-only))
  135. (declare-function org-element-property "org-element" (property element))
  136. (declare-function org-element-put-property "org-element"
  137. (element property value))
  138. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  139. (declare-function org-element--parse-objects "org-element"
  140. (beg end acc restriction))
  141. (declare-function org-element-parse-buffer "org-element"
  142. (&optional granularity visible-only))
  143. (declare-function org-element-restriction "org-element" (element))
  144. (declare-function org-element-type "org-element" (element))
  145. ;; load languages based on value of `org-babel-load-languages'
  146. (defvar org-babel-load-languages)
  147. ;;;###autoload
  148. (defun org-babel-do-load-languages (sym value)
  149. "Load the languages defined in `org-babel-load-languages'."
  150. (set-default sym value)
  151. (mapc (lambda (pair)
  152. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  153. (if active
  154. (progn
  155. (require (intern (concat "ob-" lang))))
  156. (progn
  157. (funcall 'fmakunbound
  158. (intern (concat "org-babel-execute:" lang)))
  159. (funcall 'fmakunbound
  160. (intern (concat "org-babel-expand-body:" lang)))))))
  161. org-babel-load-languages))
  162. ;;;###autoload
  163. (defun org-babel-load-file (file &optional compile)
  164. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  165. This function exports the source code using `org-babel-tangle'
  166. and then loads the resulting file using `load-file'. With prefix
  167. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  168. file to byte-code before it is loaded."
  169. (interactive "fFile to load: \nP")
  170. (require 'ob-core)
  171. (let* ((age (lambda (file)
  172. (float-time
  173. (time-subtract (current-time)
  174. (nth 5 (or (file-attributes (file-truename file))
  175. (file-attributes file)))))))
  176. (base-name (file-name-sans-extension file))
  177. (exported-file (concat base-name ".el")))
  178. ;; tangle if the org-mode file is newer than the elisp file
  179. (unless (and (file-exists-p exported-file)
  180. (> (funcall age file) (funcall age exported-file)))
  181. (org-babel-tangle-file file exported-file "emacs-lisp"))
  182. (message "%s %s"
  183. (if compile
  184. (progn (byte-compile-file exported-file 'load)
  185. "Compiled and loaded")
  186. (progn (load-file exported-file) "Loaded"))
  187. exported-file)))
  188. (defcustom org-babel-load-languages '((emacs-lisp . t))
  189. "Languages which can be evaluated in Org-mode buffers.
  190. This list can be used to load support for any of the languages
  191. below, note that each language will depend on a different set of
  192. system executables and/or Emacs modes. When a language is
  193. \"loaded\", then code blocks in that language can be evaluated
  194. with `org-babel-execute-src-block' bound by default to C-c
  195. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  196. be set to remove code block evaluation from the C-c C-c
  197. keybinding. By default only Emacs Lisp (which has no
  198. requirements) is loaded."
  199. :group 'org-babel
  200. :set 'org-babel-do-load-languages
  201. :version "24.1"
  202. :type '(alist :tag "Babel Languages"
  203. :key-type
  204. (choice
  205. (const :tag "Awk" awk)
  206. (const :tag "C" C)
  207. (const :tag "R" R)
  208. (const :tag "Asymptote" asymptote)
  209. (const :tag "Calc" calc)
  210. (const :tag "Clojure" clojure)
  211. (const :tag "CSS" css)
  212. (const :tag "Ditaa" ditaa)
  213. (const :tag "Dot" dot)
  214. (const :tag "Emacs Lisp" emacs-lisp)
  215. (const :tag "Fortran" fortran)
  216. (const :tag "Gnuplot" gnuplot)
  217. (const :tag "Haskell" haskell)
  218. (const :tag "IO" io)
  219. (const :tag "Java" java)
  220. (const :tag "Javascript" js)
  221. (const :tag "LaTeX" latex)
  222. (const :tag "Ledger" ledger)
  223. (const :tag "Lilypond" lilypond)
  224. (const :tag "Lisp" lisp)
  225. (const :tag "Makefile" makefile)
  226. (const :tag "Maxima" maxima)
  227. (const :tag "Matlab" matlab)
  228. (const :tag "Mscgen" mscgen)
  229. (const :tag "Ocaml" ocaml)
  230. (const :tag "Octave" octave)
  231. (const :tag "Org" org)
  232. (const :tag "Perl" perl)
  233. (const :tag "Pico Lisp" picolisp)
  234. (const :tag "PlantUML" plantuml)
  235. (const :tag "Python" python)
  236. (const :tag "Ruby" ruby)
  237. (const :tag "Sass" sass)
  238. (const :tag "Scala" scala)
  239. (const :tag "Scheme" scheme)
  240. (const :tag "Screen" screen)
  241. (const :tag "Shell Script" sh)
  242. (const :tag "Shen" shen)
  243. (const :tag "Sql" sql)
  244. (const :tag "Sqlite" sqlite))
  245. :value-type (boolean :tag "Activate" :value t)))
  246. ;;;; Customization variables
  247. (defcustom org-clone-delete-id nil
  248. "Remove ID property of clones of a subtree.
  249. When non-nil, clones of a subtree don't inherit the ID property.
  250. Otherwise they inherit the ID property with a new unique
  251. identifier."
  252. :type 'boolean
  253. :version "24.1"
  254. :group 'org-id)
  255. ;;; Version
  256. (org-check-version)
  257. ;;;###autoload
  258. (defun org-version (&optional here full message)
  259. "Show the org-mode version in the echo area.
  260. With prefix argument HERE, insert it at point.
  261. When FULL is non-nil, use a verbose version string.
  262. When MESSAGE is non-nil, display a message with the version."
  263. (interactive "P")
  264. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  265. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  266. (load-suffixes (list ".el"))
  267. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  268. (org-trash (or
  269. (and (fboundp 'org-release) (fboundp 'org-git-version))
  270. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  271. (load-suffixes save-load-suffixes)
  272. (org-version (org-release))
  273. (git-version (org-git-version))
  274. (version (format "Org-mode version %s (%s @ %s)"
  275. org-version
  276. git-version
  277. (if org-install-dir
  278. (if (string= org-dir org-install-dir)
  279. org-install-dir
  280. (concat "mixed installation! " org-install-dir " and " org-dir))
  281. "org-loaddefs.el can not be found!")))
  282. (_version (if full version org-version)))
  283. (if (org-called-interactively-p 'interactive)
  284. (if here
  285. (insert version)
  286. (message version))
  287. (if message (message _version))
  288. _version)))
  289. (defconst org-version (org-version))
  290. ;;; Compatibility constants
  291. ;;; The custom variables
  292. (defgroup org nil
  293. "Outline-based notes management and organizer."
  294. :tag "Org"
  295. :group 'outlines
  296. :group 'calendar)
  297. (defcustom org-mode-hook nil
  298. "Mode hook for Org-mode, run after the mode was turned on."
  299. :group 'org
  300. :type 'hook)
  301. (defcustom org-load-hook nil
  302. "Hook that is run after org.el has been loaded."
  303. :group 'org
  304. :type 'hook)
  305. (defcustom org-log-buffer-setup-hook nil
  306. "Hook that is run after an Org log buffer is created."
  307. :group 'org
  308. :version "24.1"
  309. :type 'hook)
  310. (defvar org-modules) ; defined below
  311. (defvar org-modules-loaded nil
  312. "Have the modules been loaded already?")
  313. (defun org-load-modules-maybe (&optional force)
  314. "Load all extensions listed in `org-modules'."
  315. (when (or force (not org-modules-loaded))
  316. (mapc (lambda (ext)
  317. (condition-case nil (require ext)
  318. (error (message "Problems while trying to load feature `%s'" ext))))
  319. org-modules)
  320. (setq org-modules-loaded t)))
  321. (defun org-set-modules (var value)
  322. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  323. (set var value)
  324. (when (featurep 'org)
  325. (org-load-modules-maybe 'force)))
  326. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  327. "Modules that should always be loaded together with org.el.
  328. If a description starts with <C>, the file is not part of Emacs
  329. and loading it will require that you have downloaded and properly
  330. installed the Org mode distribution.
  331. You can also use this system to load external packages (i.e. neither Org
  332. core modules, nor modules from the CONTRIB directory). Just add symbols
  333. to the end of the list. If the package is called org-xyz.el, then you need
  334. to add the symbol `xyz', and the package must have a call to:
  335. \(provide 'org-xyz)
  336. For export specific modules, see also `org-export-backends'."
  337. :group 'org
  338. :set 'org-set-modules
  339. :version "24.4"
  340. :package-version '(Org . "8.0")
  341. :type
  342. '(set :greedy t
  343. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  344. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  345. (const :tag " crypt: Encryption of subtrees" org-crypt)
  346. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  347. (const :tag " docview: Links to doc-view buffers" org-docview)
  348. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  349. (const :tag " habit: Track your consistency with habits" org-habit)
  350. (const :tag " id: Global IDs for identifying entries" org-id)
  351. (const :tag " info: Links to Info nodes" org-info)
  352. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  353. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  354. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  355. (const :tag " mouse: Additional mouse support" org-mouse)
  356. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  357. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  358. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  359. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  360. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  361. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  362. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  363. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  364. (const :tag "C collector: Collect properties into tables" org-collector)
  365. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  366. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  367. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  368. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  369. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  370. (const :tag "C eval: Include command output as text" org-eval)
  371. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  372. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  373. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  374. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  375. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  376. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  377. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  378. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  379. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  380. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  381. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  382. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  383. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  384. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  385. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  386. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  387. (const :tag "C registry: A registry for Org-mode links" org-registry)
  388. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  389. (const :tag "C secretary: Team management with org-mode" org-secretary)
  390. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  391. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  392. (const :tag "C track: Keep up with Org-mode development" org-track)
  393. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  394. (const :tag "C vm: Links to VM folders/messages" org-vm)
  395. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  396. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  397. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  398. (defvar org-export-registered-backends) ; From ox.el
  399. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  400. (defcustom org-export-backends '(ascii html icalendar latex)
  401. "List of export back-ends that should be always available.
  402. If a description starts with <C>, the file is not part of Emacs
  403. and loading it will require that you have downloaded and properly
  404. installed the Org mode distribution.
  405. Unlike to `org-modules', libraries in this list will not be
  406. loaded along with Org, but only once the export framework is
  407. needed.
  408. This variable needs to be set before org.el is loaded. If you
  409. need to make a change while Emacs is running, use the customize
  410. interface or run the following code, where VALUE stands for the
  411. new value of the variable, after updating it:
  412. \(progn
  413. \(setq org-export-registered-backends
  414. \(org-remove-if-not
  415. \(lambda (backend)
  416. \(or (memq backend val)
  417. \(catch 'parentp
  418. \(mapc
  419. \(lambda (b)
  420. \(and (org-export-derived-backend-p b (car backend))
  421. \(throw 'parentp t)))
  422. val)
  423. nil)))
  424. org-export-registered-backends))
  425. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  426. \(dolist (backend val)
  427. \(cond
  428. \((not (load (format \"ox-%s\" backend) t t))
  429. \(message \"Problems while trying to load export back-end `%s'\"
  430. backend))
  431. \((not (memq backend new-list)) (push backend new-list))))
  432. \(set-default var new-list)))
  433. Adding a back-end to this list will also pull the back-end it
  434. depends on, if any."
  435. :group 'org
  436. :group 'org-export
  437. :version "24.4"
  438. :package-version '(Org . "8.0")
  439. :initialize 'custom-initialize-set
  440. :set (lambda (var val)
  441. (if (not (featurep 'ox)) (set-default var val)
  442. ;; Any back-end not required anymore (not present in VAL and not
  443. ;; a parent of any back-end in the new value) is removed from the
  444. ;; list of registered back-ends.
  445. (setq org-export-registered-backends
  446. (org-remove-if-not
  447. (lambda (backend)
  448. (or (memq backend val)
  449. (catch 'parentp
  450. (mapc
  451. (lambda (b)
  452. (and (org-export-derived-backend-p b (car backend))
  453. (throw 'parentp t)))
  454. val)
  455. nil)))
  456. org-export-registered-backends))
  457. ;; Now build NEW-LIST of both new back-ends and required
  458. ;; parents.
  459. (let ((new-list (mapcar 'car org-export-registered-backends)))
  460. (dolist (backend val)
  461. (cond
  462. ((not (load (format "ox-%s" backend) t t))
  463. (message "Problems while trying to load export back-end `%s'"
  464. backend))
  465. ((not (memq backend new-list)) (push backend new-list))))
  466. ;; Set VAR to that list with fixed dependencies.
  467. (set-default var new-list))))
  468. :type '(set :greedy t
  469. (const :tag " ascii Export buffer to ASCII format" ascii)
  470. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  471. (const :tag " html Export buffer to HTML format" html)
  472. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  473. (const :tag " latex Export buffer to LaTeX format" latex)
  474. (const :tag " man Export buffer to MAN format" man)
  475. (const :tag " md Export buffer to Markdown format" md)
  476. (const :tag " odt Export buffer to ODT format" odt)
  477. (const :tag " org Export buffer to Org format" org)
  478. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  479. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  480. (const :tag "C deck Export buffer to deck.js presentations" deck)
  481. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  482. (const :tag "C groff Export buffer to Groff format" groff)
  483. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  484. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  485. (const :tag "C s5 Export buffer to s5 presentations" s5)
  486. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  487. (eval-after-load 'ox
  488. '(mapc
  489. (lambda (backend)
  490. (condition-case nil (require (intern (format "ox-%s" backend)))
  491. (error (message "Problems while trying to load export back-end `%s'"
  492. backend))))
  493. org-export-backends))
  494. (defcustom org-support-shift-select nil
  495. "Non-nil means make shift-cursor commands select text when possible.
  496. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  497. start selecting a region, or enlarge regions started in this way.
  498. In Org-mode, in special contexts, these same keys are used for
  499. other purposes, important enough to compete with shift selection.
  500. Org tries to balance these needs by supporting `shift-select-mode'
  501. outside these special contexts, under control of this variable.
  502. The default of this variable is nil, to avoid confusing behavior. Shifted
  503. cursor keys will then execute Org commands in the following contexts:
  504. - on a headline, changing TODO state (left/right) and priority (up/down)
  505. - on a time stamp, changing the time
  506. - in a plain list item, changing the bullet type
  507. - in a property definition line, switching between allowed values
  508. - in the BEGIN line of a clock table (changing the time block).
  509. Outside these contexts, the commands will throw an error.
  510. When this variable is t and the cursor is not in a special
  511. context, Org-mode will support shift-selection for making and
  512. enlarging regions. To make this more effective, the bullet
  513. cycling will no longer happen anywhere in an item line, but only
  514. if the cursor is exactly on the bullet.
  515. If you set this variable to the symbol `always', then the keys
  516. will not be special in headlines, property lines, and item lines,
  517. to make shift selection work there as well. If this is what you
  518. want, you can use the following alternative commands: `C-c C-t'
  519. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  520. can be used to switch TODO sets, `C-c -' to cycle item bullet
  521. types, and properties can be edited by hand or in column view.
  522. However, when the cursor is on a timestamp, shift-cursor commands
  523. will still edit the time stamp - this is just too good to give up.
  524. XEmacs user should have this variable set to nil, because
  525. `shift-select-mode' is in Emacs 23 or later only."
  526. :group 'org
  527. :type '(choice
  528. (const :tag "Never" nil)
  529. (const :tag "When outside special context" t)
  530. (const :tag "Everywhere except timestamps" always)))
  531. (defcustom org-loop-over-headlines-in-active-region nil
  532. "Shall some commands act upon headlines in the active region?
  533. When set to `t', some commands will be performed in all headlines
  534. within the active region.
  535. When set to `start-level', some commands will be performed in all
  536. headlines within the active region, provided that these headlines
  537. are of the same level than the first one.
  538. When set to a string, those commands will be performed on the
  539. matching headlines within the active region. Such string must be
  540. a tags/property/todo match as it is used in the agenda tags view.
  541. The list of commands is: `org-schedule', `org-deadline',
  542. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  543. `org-archive-to-archive-sibling'. The archiving commands skip
  544. already archived entries."
  545. :type '(choice (const :tag "Don't loop" nil)
  546. (const :tag "All headlines in active region" t)
  547. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  548. (string :tag "Tags/Property/Todo matcher"))
  549. :version "24.1"
  550. :group 'org-todo
  551. :group 'org-archive)
  552. (defgroup org-startup nil
  553. "Options concerning startup of Org-mode."
  554. :tag "Org Startup"
  555. :group 'org)
  556. (defcustom org-startup-folded t
  557. "Non-nil means entering Org-mode will switch to OVERVIEW.
  558. This can also be configured on a per-file basis by adding one of
  559. the following lines anywhere in the buffer:
  560. #+STARTUP: fold (or `overview', this is equivalent)
  561. #+STARTUP: nofold (or `showall', this is equivalent)
  562. #+STARTUP: content
  563. #+STARTUP: showeverything
  564. By default, this option is ignored when Org opens agenda files
  565. for the first time. If you want the agenda to honor the startup
  566. option, set `org-agenda-inhibit-startup' to nil."
  567. :group 'org-startup
  568. :type '(choice
  569. (const :tag "nofold: show all" nil)
  570. (const :tag "fold: overview" t)
  571. (const :tag "content: all headlines" content)
  572. (const :tag "show everything, even drawers" showeverything)))
  573. (defcustom org-startup-truncated t
  574. "Non-nil means entering Org-mode will set `truncate-lines'.
  575. This is useful since some lines containing links can be very long and
  576. uninteresting. Also tables look terrible when wrapped."
  577. :group 'org-startup
  578. :type 'boolean)
  579. (defcustom org-startup-indented nil
  580. "Non-nil means turn on `org-indent-mode' on startup.
  581. This can also be configured on a per-file basis by adding one of
  582. the following lines anywhere in the buffer:
  583. #+STARTUP: indent
  584. #+STARTUP: noindent"
  585. :group 'org-structure
  586. :type '(choice
  587. (const :tag "Not" nil)
  588. (const :tag "Globally (slow on startup in large files)" t)))
  589. (defcustom org-use-sub-superscripts t
  590. "Non-nil means interpret \"_\" and \"^\" for display.
  591. When this option is turned on, you can use TeX-like syntax for sub- and
  592. superscripts. Several characters after \"_\" or \"^\" will be
  593. considered as a single item - so grouping with {} is normally not
  594. needed. For example, the following things will be parsed as single
  595. sub- or superscripts.
  596. 10^24 or 10^tau several digits will be considered 1 item.
  597. 10^-12 or 10^-tau a leading sign with digits or a word
  598. x^2-y^3 will be read as x^2 - y^3, because items are
  599. terminated by almost any nonword/nondigit char.
  600. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  601. Still, ambiguity is possible - so when in doubt use {} to enclose
  602. the sub/superscript. If you set this variable to the symbol
  603. `{}', the braces are *required* in order to trigger
  604. interpretations as sub/superscript. This can be helpful in
  605. documents that need \"_\" frequently in plain text."
  606. :group 'org-startup
  607. :version "24.1"
  608. :type '(choice
  609. (const :tag "Always interpret" t)
  610. (const :tag "Only with braces" {})
  611. (const :tag "Never interpret" nil)))
  612. (defcustom org-startup-with-beamer-mode nil
  613. "Non-nil means turn on `org-beamer-mode' on startup.
  614. This can also be configured on a per-file basis by adding one of
  615. the following lines anywhere in the buffer:
  616. #+STARTUP: beamer"
  617. :group 'org-startup
  618. :version "24.1"
  619. :type 'boolean)
  620. (defcustom org-startup-align-all-tables nil
  621. "Non-nil means align all tables when visiting a file.
  622. This is useful when the column width in tables is forced with <N> cookies
  623. in table fields. Such tables will look correct only after the first re-align.
  624. This can also be configured on a per-file basis by adding one of
  625. the following lines anywhere in the buffer:
  626. #+STARTUP: align
  627. #+STARTUP: noalign"
  628. :group 'org-startup
  629. :type 'boolean)
  630. (defcustom org-startup-with-inline-images nil
  631. "Non-nil means show inline images when loading a new Org file.
  632. This can also be configured on a per-file basis by adding one of
  633. the following lines anywhere in the buffer:
  634. #+STARTUP: inlineimages
  635. #+STARTUP: noinlineimages"
  636. :group 'org-startup
  637. :version "24.1"
  638. :type 'boolean)
  639. (defcustom org-startup-with-latex-preview nil
  640. "Non-nil means preview LaTeX fragments when loading a new Org file.
  641. This can also be configured on a per-file basis by adding one of
  642. the followinglines anywhere in the buffer:
  643. #+STARTUP: latexpreview
  644. #+STARTUP: nolatexpreview"
  645. :group 'org-startup
  646. :version "24.4"
  647. :package-version '(Org . "8.0")
  648. :type 'boolean)
  649. (defcustom org-insert-mode-line-in-empty-file nil
  650. "Non-nil means insert the first line setting Org-mode in empty files.
  651. When the function `org-mode' is called interactively in an empty file, this
  652. normally means that the file name does not automatically trigger Org-mode.
  653. To ensure that the file will always be in Org-mode in the future, a
  654. line enforcing Org-mode will be inserted into the buffer, if this option
  655. has been set."
  656. :group 'org-startup
  657. :type 'boolean)
  658. (defcustom org-replace-disputed-keys nil
  659. "Non-nil means use alternative key bindings for some keys.
  660. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  661. These keys are also used by other packages like shift-selection-mode'
  662. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  663. If you want to use Org-mode together with one of these other modes,
  664. or more generally if you would like to move some Org-mode commands to
  665. other keys, set this variable and configure the keys with the variable
  666. `org-disputed-keys'.
  667. This option is only relevant at load-time of Org-mode, and must be set
  668. *before* org.el is loaded. Changing it requires a restart of Emacs to
  669. become effective."
  670. :group 'org-startup
  671. :type 'boolean)
  672. (defcustom org-use-extra-keys nil
  673. "Non-nil means use extra key sequence definitions for certain commands.
  674. This happens automatically if you run XEmacs or if `window-system'
  675. is nil. This variable lets you do the same manually. You must
  676. set it before loading org.
  677. Example: on Carbon Emacs 22 running graphically, with an external
  678. keyboard on a Powerbook, the default way of setting M-left might
  679. not work for either Alt or ESC. Setting this variable will make
  680. it work for ESC."
  681. :group 'org-startup
  682. :type 'boolean)
  683. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  684. (defcustom org-disputed-keys
  685. '(([(shift up)] . [(meta p)])
  686. ([(shift down)] . [(meta n)])
  687. ([(shift left)] . [(meta -)])
  688. ([(shift right)] . [(meta +)])
  689. ([(control shift right)] . [(meta shift +)])
  690. ([(control shift left)] . [(meta shift -)]))
  691. "Keys for which Org-mode and other modes compete.
  692. This is an alist, cars are the default keys, second element specifies
  693. the alternative to use when `org-replace-disputed-keys' is t.
  694. Keys can be specified in any syntax supported by `define-key'.
  695. The value of this option takes effect only at Org-mode's startup,
  696. therefore you'll have to restart Emacs to apply it after changing."
  697. :group 'org-startup
  698. :type 'alist)
  699. (defun org-key (key)
  700. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  701. Or return the original if not disputed.
  702. Also apply the translations defined in `org-xemacs-key-equivalents'."
  703. (when org-replace-disputed-keys
  704. (let* ((nkey (key-description key))
  705. (x (org-find-if (lambda (x)
  706. (equal (key-description (car x)) nkey))
  707. org-disputed-keys)))
  708. (setq key (if x (cdr x) key))))
  709. (when (featurep 'xemacs)
  710. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  711. key)
  712. (defun org-find-if (predicate seq)
  713. (catch 'exit
  714. (while seq
  715. (if (funcall predicate (car seq))
  716. (throw 'exit (car seq))
  717. (pop seq)))))
  718. (defun org-defkey (keymap key def)
  719. "Define a key, possibly translated, as returned by `org-key'."
  720. (define-key keymap (org-key key) def))
  721. (defcustom org-ellipsis nil
  722. "The ellipsis to use in the Org-mode outline.
  723. When nil, just use the standard three dots. When a string, use that instead,
  724. When a face, use the standard 3 dots, but with the specified face.
  725. The change affects only Org-mode (which will then use its own display table).
  726. Changing this requires executing `M-x org-mode' in a buffer to become
  727. effective."
  728. :group 'org-startup
  729. :type '(choice (const :tag "Default" nil)
  730. (face :tag "Face" :value org-warning)
  731. (string :tag "String" :value "...#")))
  732. (defvar org-display-table nil
  733. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  734. (defgroup org-keywords nil
  735. "Keywords in Org-mode."
  736. :tag "Org Keywords"
  737. :group 'org)
  738. (defcustom org-deadline-string "DEADLINE:"
  739. "String to mark deadline entries.
  740. A deadline is this string, followed by a time stamp. Should be a word,
  741. terminated by a colon. You can insert a schedule keyword and
  742. a timestamp with \\[org-deadline].
  743. Changes become only effective after restarting Emacs."
  744. :group 'org-keywords
  745. :type 'string)
  746. (defcustom org-scheduled-string "SCHEDULED:"
  747. "String to mark scheduled TODO entries.
  748. A schedule is this string, followed by a time stamp. Should be a word,
  749. terminated by a colon. You can insert a schedule keyword and
  750. a timestamp with \\[org-schedule].
  751. Changes become only effective after restarting Emacs."
  752. :group 'org-keywords
  753. :type 'string)
  754. (defcustom org-closed-string "CLOSED:"
  755. "String used as the prefix for timestamps logging closing a TODO entry."
  756. :group 'org-keywords
  757. :type 'string)
  758. (defcustom org-clock-string "CLOCK:"
  759. "String used as prefix for timestamps clocking work hours on an item."
  760. :group 'org-keywords
  761. :type 'string)
  762. (defcustom org-closed-keep-when-no-todo nil
  763. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  764. :group 'org-todo
  765. :group 'org-keywords
  766. :version "24.4"
  767. :package-version '(Org . "8.0")
  768. :type 'boolean)
  769. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  770. org-scheduled-string "\\|"
  771. org-deadline-string "\\|"
  772. org-closed-string "\\|"
  773. org-clock-string "\\)")
  774. "Matches a line with planning or clock info.")
  775. (defcustom org-comment-string "COMMENT"
  776. "Entries starting with this keyword will never be exported.
  777. An entry can be toggled between COMMENT and normal with
  778. \\[org-toggle-comment].
  779. Changes become only effective after restarting Emacs."
  780. :group 'org-keywords
  781. :type 'string)
  782. (defcustom org-quote-string "QUOTE"
  783. "Entries starting with this keyword will be exported in fixed-width font.
  784. Quoting applies only to the text in the entry following the headline, and does
  785. not extend beyond the next headline, even if that is lower level.
  786. An entry can be toggled between QUOTE and normal with
  787. \\[org-toggle-fixed-width-section]."
  788. :group 'org-keywords
  789. :type 'string)
  790. (defconst org-repeat-re
  791. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  792. "Regular expression for specifying repeated events.
  793. After a match, group 1 contains the repeat expression.")
  794. (defgroup org-structure nil
  795. "Options concerning the general structure of Org-mode files."
  796. :tag "Org Structure"
  797. :group 'org)
  798. (defgroup org-reveal-location nil
  799. "Options about how to make context of a location visible."
  800. :tag "Org Reveal Location"
  801. :group 'org-structure)
  802. (defconst org-context-choice
  803. '(choice
  804. (const :tag "Always" t)
  805. (const :tag "Never" nil)
  806. (repeat :greedy t :tag "Individual contexts"
  807. (cons
  808. (choice :tag "Context"
  809. (const agenda)
  810. (const org-goto)
  811. (const occur-tree)
  812. (const tags-tree)
  813. (const link-search)
  814. (const mark-goto)
  815. (const bookmark-jump)
  816. (const isearch)
  817. (const default))
  818. (boolean))))
  819. "Contexts for the reveal options.")
  820. (defcustom org-show-hierarchy-above '((default . t))
  821. "Non-nil means show full hierarchy when revealing a location.
  822. Org-mode often shows locations in an org-mode file which might have
  823. been invisible before. When this is set, the hierarchy of headings
  824. above the exposed location is shown.
  825. Turning this off for example for sparse trees makes them very compact.
  826. Instead of t, this can also be an alist specifying this option for different
  827. contexts. Valid contexts are
  828. agenda when exposing an entry from the agenda
  829. org-goto when using the command `org-goto' on key C-c C-j
  830. occur-tree when using the command `org-occur' on key C-c /
  831. tags-tree when constructing a sparse tree based on tags matches
  832. link-search when exposing search matches associated with a link
  833. mark-goto when exposing the jump goal of a mark
  834. bookmark-jump when exposing a bookmark location
  835. isearch when exiting from an incremental search
  836. default default for all contexts not set explicitly"
  837. :group 'org-reveal-location
  838. :type org-context-choice)
  839. (defcustom org-show-following-heading '((default . nil))
  840. "Non-nil means show following heading when revealing a location.
  841. Org-mode often shows locations in an org-mode file which might have
  842. been invisible before. When this is set, the heading following the
  843. match is shown.
  844. Turning this off for example for sparse trees makes them very compact,
  845. but makes it harder to edit the location of the match. In such a case,
  846. use the command \\[org-reveal] to show more context.
  847. Instead of t, this can also be an alist specifying this option for different
  848. contexts. See `org-show-hierarchy-above' for valid contexts."
  849. :group 'org-reveal-location
  850. :type org-context-choice)
  851. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  852. "Non-nil means show all sibling heading when revealing a location.
  853. Org-mode often shows locations in an org-mode file which might have
  854. been invisible before. When this is set, the sibling of the current entry
  855. heading are all made visible. If `org-show-hierarchy-above' is t,
  856. the same happens on each level of the hierarchy above the current entry.
  857. By default this is on for the isearch context, off for all other contexts.
  858. Turning this off for example for sparse trees makes them very compact,
  859. but makes it harder to edit the location of the match. In such a case,
  860. use the command \\[org-reveal] to show more context.
  861. Instead of t, this can also be an alist specifying this option for different
  862. contexts. See `org-show-hierarchy-above' for valid contexts."
  863. :group 'org-reveal-location
  864. :type org-context-choice
  865. :version "24.4"
  866. :package-version '(Org . "8.0"))
  867. (defcustom org-show-entry-below '((default . nil))
  868. "Non-nil means show the entry below a headline when revealing a location.
  869. Org-mode often shows locations in an org-mode file which might have
  870. been invisible before. When this is set, the text below the headline that is
  871. exposed is also shown.
  872. By default this is off for all contexts.
  873. Instead of t, this can also be an alist specifying this option for different
  874. contexts. See `org-show-hierarchy-above' for valid contexts."
  875. :group 'org-reveal-location
  876. :type org-context-choice)
  877. (defcustom org-indirect-buffer-display 'other-window
  878. "How should indirect tree buffers be displayed?
  879. This applies to indirect buffers created with the commands
  880. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  881. Valid values are:
  882. current-window Display in the current window
  883. other-window Just display in another window.
  884. dedicated-frame Create one new frame, and re-use it each time.
  885. new-frame Make a new frame each time. Note that in this case
  886. previously-made indirect buffers are kept, and you need to
  887. kill these buffers yourself."
  888. :group 'org-structure
  889. :group 'org-agenda-windows
  890. :type '(choice
  891. (const :tag "In current window" current-window)
  892. (const :tag "In current frame, other window" other-window)
  893. (const :tag "Each time a new frame" new-frame)
  894. (const :tag "One dedicated frame" dedicated-frame)))
  895. (defcustom org-use-speed-commands nil
  896. "Non-nil means activate single letter commands at beginning of a headline.
  897. This may also be a function to test for appropriate locations where speed
  898. commands should be active."
  899. :group 'org-structure
  900. :type '(choice
  901. (const :tag "Never" nil)
  902. (const :tag "At beginning of headline stars" t)
  903. (function)))
  904. (defcustom org-speed-commands-user nil
  905. "Alist of additional speed commands.
  906. This list will be checked before `org-speed-commands-default'
  907. when the variable `org-use-speed-commands' is non-nil
  908. and when the cursor is at the beginning of a headline.
  909. The car if each entry is a string with a single letter, which must
  910. be assigned to `self-insert-command' in the global map.
  911. The cdr is either a command to be called interactively, a function
  912. to be called, or a form to be evaluated.
  913. An entry that is just a list with a single string will be interpreted
  914. as a descriptive headline that will be added when listing the speed
  915. commands in the Help buffer using the `?' speed command."
  916. :group 'org-structure
  917. :type '(repeat :value ("k" . ignore)
  918. (choice :value ("k" . ignore)
  919. (list :tag "Descriptive Headline" (string :tag "Headline"))
  920. (cons :tag "Letter and Command"
  921. (string :tag "Command letter")
  922. (choice
  923. (function)
  924. (sexp))))))
  925. (defgroup org-cycle nil
  926. "Options concerning visibility cycling in Org-mode."
  927. :tag "Org Cycle"
  928. :group 'org-structure)
  929. (defcustom org-cycle-skip-children-state-if-no-children t
  930. "Non-nil means skip CHILDREN state in entries that don't have any."
  931. :group 'org-cycle
  932. :type 'boolean)
  933. (defcustom org-cycle-max-level nil
  934. "Maximum level which should still be subject to visibility cycling.
  935. Levels higher than this will, for cycling, be treated as text, not a headline.
  936. When `org-odd-levels-only' is set, a value of N in this variable actually
  937. means 2N-1 stars as the limiting headline.
  938. When nil, cycle all levels.
  939. Note that the limiting level of cycling is also influenced by
  940. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  941. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  942. than its value."
  943. :group 'org-cycle
  944. :type '(choice
  945. (const :tag "No limit" nil)
  946. (integer :tag "Maximum level")))
  947. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  948. "Names of drawers. Drawers are not opened by cycling on the headline above.
  949. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  950. this:
  951. :DRAWERNAME:
  952. .....
  953. :END:
  954. The drawer \"PROPERTIES\" is special for capturing properties through
  955. the property API.
  956. Drawers can be defined on the per-file basis with a line like:
  957. #+DRAWERS: HIDDEN STATE PROPERTIES"
  958. :group 'org-structure
  959. :group 'org-cycle
  960. :type '(repeat (string :tag "Drawer Name")))
  961. (defcustom org-hide-block-startup nil
  962. "Non-nil means entering Org-mode will fold all blocks.
  963. This can also be set in on a per-file basis with
  964. #+STARTUP: hideblocks
  965. #+STARTUP: showblocks"
  966. :group 'org-startup
  967. :group 'org-cycle
  968. :type 'boolean)
  969. (defcustom org-cycle-global-at-bob nil
  970. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  971. This makes it possible to do global cycling without having to use S-TAB or
  972. \\[universal-argument] TAB. For this special case to work, the first line
  973. of the buffer must not be a headline -- it may be empty or some other text.
  974. When used in this way, `org-cycle-hook' is disabled temporarily to make
  975. sure the cursor stays at the beginning of the buffer. When this option is
  976. nil, don't do anything special at the beginning of the buffer."
  977. :group 'org-cycle
  978. :type 'boolean)
  979. (defcustom org-cycle-level-after-item/entry-creation t
  980. "Non-nil means cycle entry level or item indentation in new empty entries.
  981. When the cursor is at the end of an empty headline, i.e., with only stars
  982. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  983. and then all possible ancestor states, before returning to the original state.
  984. This makes data entry extremely fast: M-RET to create a new headline,
  985. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  986. When the cursor is at the end of an empty plain list item, one TAB will
  987. make it a subitem, two or more tabs will back up to make this an item
  988. higher up in the item hierarchy."
  989. :group 'org-cycle
  990. :type 'boolean)
  991. (defcustom org-cycle-emulate-tab t
  992. "Where should `org-cycle' emulate TAB.
  993. nil Never
  994. white Only in completely white lines
  995. whitestart Only at the beginning of lines, before the first non-white char
  996. t Everywhere except in headlines
  997. exc-hl-bol Everywhere except at the start of a headline
  998. If TAB is used in a place where it does not emulate TAB, the current subtree
  999. visibility is cycled."
  1000. :group 'org-cycle
  1001. :type '(choice (const :tag "Never" nil)
  1002. (const :tag "Only in completely white lines" white)
  1003. (const :tag "Before first char in a line" whitestart)
  1004. (const :tag "Everywhere except in headlines" t)
  1005. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1006. (defcustom org-cycle-separator-lines 2
  1007. "Number of empty lines needed to keep an empty line between collapsed trees.
  1008. If you leave an empty line between the end of a subtree and the following
  1009. headline, this empty line is hidden when the subtree is folded.
  1010. Org-mode will leave (exactly) one empty line visible if the number of
  1011. empty lines is equal or larger to the number given in this variable.
  1012. So the default 2 means at least 2 empty lines after the end of a subtree
  1013. are needed to produce free space between a collapsed subtree and the
  1014. following headline.
  1015. If the number is negative, and the number of empty lines is at least -N,
  1016. all empty lines are shown.
  1017. Special case: when 0, never leave empty lines in collapsed view."
  1018. :group 'org-cycle
  1019. :type 'integer)
  1020. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1021. (defcustom org-pre-cycle-hook nil
  1022. "Hook that is run before visibility cycling is happening.
  1023. The function(s) in this hook must accept a single argument which indicates
  1024. the new state that will be set right after running this hook. The
  1025. argument is a symbol. Before a global state change, it can have the values
  1026. `overview', `content', or `all'. Before a local state change, it can have
  1027. the values `folded', `children', or `subtree'."
  1028. :group 'org-cycle
  1029. :type 'hook)
  1030. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1031. org-cycle-hide-drawers
  1032. org-cycle-hide-inline-tasks
  1033. org-cycle-show-empty-lines
  1034. org-optimize-window-after-visibility-change)
  1035. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1036. The function(s) in this hook must accept a single argument which indicates
  1037. the new state that was set by the most recent `org-cycle' command. The
  1038. argument is a symbol. After a global state change, it can have the values
  1039. `overview', `contents', or `all'. After a local state change, it can have
  1040. the values `folded', `children', or `subtree'."
  1041. :group 'org-cycle
  1042. :type 'hook)
  1043. (defgroup org-edit-structure nil
  1044. "Options concerning structure editing in Org-mode."
  1045. :tag "Org Edit Structure"
  1046. :group 'org-structure)
  1047. (defcustom org-odd-levels-only nil
  1048. "Non-nil means skip even levels and only use odd levels for the outline.
  1049. This has the effect that two stars are being added/taken away in
  1050. promotion/demotion commands. It also influences how levels are
  1051. handled by the exporters.
  1052. Changing it requires restart of `font-lock-mode' to become effective
  1053. for fontification also in regions already fontified.
  1054. You may also set this on a per-file basis by adding one of the following
  1055. lines to the buffer:
  1056. #+STARTUP: odd
  1057. #+STARTUP: oddeven"
  1058. :group 'org-edit-structure
  1059. :group 'org-appearance
  1060. :type 'boolean)
  1061. (defcustom org-adapt-indentation t
  1062. "Non-nil means adapt indentation to outline node level.
  1063. When this variable is set, Org assumes that you write outlines by
  1064. indenting text in each node to align with the headline (after the stars).
  1065. The following issues are influenced by this variable:
  1066. - When this is set and the *entire* text in an entry is indented, the
  1067. indentation is increased by one space in a demotion command, and
  1068. decreased by one in a promotion command. If any line in the entry
  1069. body starts with text at column 0, indentation is not changed at all.
  1070. - Property drawers and planning information is inserted indented when
  1071. this variable s set. When nil, they will not be indented.
  1072. - TAB indents a line relative to context. The lines below a headline
  1073. will be indented when this variable is set.
  1074. Note that this is all about true indentation, by adding and removing
  1075. space characters. See also `org-indent.el' which does level-dependent
  1076. indentation in a virtual way, i.e. at display time in Emacs."
  1077. :group 'org-edit-structure
  1078. :type 'boolean)
  1079. (defcustom org-special-ctrl-a/e nil
  1080. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1081. When t, `C-a' will bring back the cursor to the beginning of the
  1082. headline text, i.e. after the stars and after a possible TODO
  1083. keyword. In an item, this will be the position after bullet and
  1084. check-box, if any. When the cursor is already at that position,
  1085. another `C-a' will bring it to the beginning of the line.
  1086. `C-e' will jump to the end of the headline, ignoring the presence
  1087. of tags in the headline. A second `C-e' will then jump to the
  1088. true end of the line, after any tags. This also means that, when
  1089. this variable is non-nil, `C-e' also will never jump beyond the
  1090. end of the heading of a folded section, i.e. not after the
  1091. ellipses.
  1092. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1093. normally, going to the true line boundary first. Only a directly
  1094. following, identical keypress will bring the cursor to the
  1095. special positions.
  1096. This may also be a cons cell where the behavior for `C-a' and
  1097. `C-e' is set separately."
  1098. :group 'org-edit-structure
  1099. :type '(choice
  1100. (const :tag "off" nil)
  1101. (const :tag "on: after stars/bullet and before tags first" t)
  1102. (const :tag "reversed: true line boundary first" reversed)
  1103. (cons :tag "Set C-a and C-e separately"
  1104. (choice :tag "Special C-a"
  1105. (const :tag "off" nil)
  1106. (const :tag "on: after stars/bullet first" t)
  1107. (const :tag "reversed: before stars/bullet first" reversed))
  1108. (choice :tag "Special C-e"
  1109. (const :tag "off" nil)
  1110. (const :tag "on: before tags first" t)
  1111. (const :tag "reversed: after tags first" reversed)))))
  1112. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1113. (defcustom org-special-ctrl-k nil
  1114. "Non-nil means `C-k' will behave specially in headlines.
  1115. When nil, `C-k' will call the default `kill-line' command.
  1116. When t, the following will happen while the cursor is in the headline:
  1117. - When the cursor is at the beginning of a headline, kill the entire
  1118. line and possible the folded subtree below the line.
  1119. - When in the middle of the headline text, kill the headline up to the tags.
  1120. - When after the headline text, kill the tags."
  1121. :group 'org-edit-structure
  1122. :type 'boolean)
  1123. (defcustom org-ctrl-k-protect-subtree nil
  1124. "Non-nil means, do not delete a hidden subtree with C-k.
  1125. When set to the symbol `error', simply throw an error when C-k is
  1126. used to kill (part-of) a headline that has hidden text behind it.
  1127. Any other non-nil value will result in a query to the user, if it is
  1128. OK to kill that hidden subtree. When nil, kill without remorse."
  1129. :group 'org-edit-structure
  1130. :version "24.1"
  1131. :type '(choice
  1132. (const :tag "Do not protect hidden subtrees" nil)
  1133. (const :tag "Protect hidden subtrees with a security query" t)
  1134. (const :tag "Never kill a hidden subtree with C-k" error)))
  1135. (defcustom org-catch-invisible-edits nil
  1136. "Check if in invisible region before inserting or deleting a character.
  1137. Valid values are:
  1138. nil Do not check, so just do invisible edits.
  1139. error Throw an error and do nothing.
  1140. show Make point visible, and do the requested edit.
  1141. show-and-error Make point visible, then throw an error and abort the edit.
  1142. smart Make point visible, and do insertion/deletion if it is
  1143. adjacent to visible text and the change feels predictable.
  1144. Never delete a previously invisible character or add in the
  1145. middle or right after an invisible region. Basically, this
  1146. allows insertion and backward-delete right before ellipses.
  1147. FIXME: maybe in this case we should not even show?"
  1148. :group 'org-edit-structure
  1149. :version "24.1"
  1150. :type '(choice
  1151. (const :tag "Do not check" nil)
  1152. (const :tag "Throw error when trying to edit" error)
  1153. (const :tag "Unhide, but do not do the edit" show-and-error)
  1154. (const :tag "Show invisible part and do the edit" show)
  1155. (const :tag "Be smart and do the right thing" smart)))
  1156. (defcustom org-yank-folded-subtrees t
  1157. "Non-nil means when yanking subtrees, fold them.
  1158. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1159. it starts with a heading and all other headings in it are either children
  1160. or siblings, then fold all the subtrees. However, do this only if no
  1161. text after the yank would be swallowed into a folded tree by this action."
  1162. :group 'org-edit-structure
  1163. :type 'boolean)
  1164. (defcustom org-yank-adjusted-subtrees nil
  1165. "Non-nil means when yanking subtrees, adjust the level.
  1166. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1167. this function for details."
  1168. :group 'org-edit-structure
  1169. :type 'boolean)
  1170. (defcustom org-M-RET-may-split-line '((default . t))
  1171. "Non-nil means M-RET will split the line at the cursor position.
  1172. When nil, it will go to the end of the line before making a
  1173. new line.
  1174. You may also set this option in a different way for different
  1175. contexts. Valid contexts are:
  1176. headline when creating a new headline
  1177. item when creating a new item
  1178. table in a table field
  1179. default the value to be used for all contexts not explicitly
  1180. customized"
  1181. :group 'org-structure
  1182. :group 'org-table
  1183. :type '(choice
  1184. (const :tag "Always" t)
  1185. (const :tag "Never" nil)
  1186. (repeat :greedy t :tag "Individual contexts"
  1187. (cons
  1188. (choice :tag "Context"
  1189. (const headline)
  1190. (const item)
  1191. (const table)
  1192. (const default))
  1193. (boolean)))))
  1194. (defcustom org-insert-heading-respect-content nil
  1195. "Non-nil means insert new headings after the current subtree.
  1196. When nil, the new heading is created directly after the current line.
  1197. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1198. this variable on for the duration of the command."
  1199. :group 'org-structure
  1200. :type 'boolean)
  1201. (defcustom org-blank-before-new-entry '((heading . auto)
  1202. (plain-list-item . auto))
  1203. "Should `org-insert-heading' leave a blank line before new heading/item?
  1204. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1205. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1206. which case Org will look at the surrounding headings/items and try to
  1207. make an intelligent decision whether to insert a blank line or not.
  1208. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1209. the setting here is ignored and no empty line is inserted to avoid breaking
  1210. the list structure."
  1211. :group 'org-edit-structure
  1212. :type '(list
  1213. (cons (const heading)
  1214. (choice (const :tag "Never" nil)
  1215. (const :tag "Always" t)
  1216. (const :tag "Auto" auto)))
  1217. (cons (const plain-list-item)
  1218. (choice (const :tag "Never" nil)
  1219. (const :tag "Always" t)
  1220. (const :tag "Auto" auto)))))
  1221. (defcustom org-insert-heading-hook nil
  1222. "Hook being run after inserting a new heading."
  1223. :group 'org-edit-structure
  1224. :type 'hook)
  1225. (defcustom org-enable-fixed-width-editor t
  1226. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1227. This currently only means they are never auto-wrapped.
  1228. When nil, such lines will be treated like ordinary lines.
  1229. See also the QUOTE keyword."
  1230. :group 'org-edit-structure
  1231. :type 'boolean)
  1232. (defcustom org-goto-auto-isearch t
  1233. "Non-nil means typing characters in `org-goto' starts incremental search.
  1234. When nil, you can use these keybindings to navigate the buffer:
  1235. q Quit the org-goto interface
  1236. n Go to the next visible heading
  1237. p Go to the previous visible heading
  1238. f Go one heading forward on same level
  1239. b Go one heading backward on same level
  1240. u Go one heading up"
  1241. :group 'org-edit-structure
  1242. :type 'boolean)
  1243. (defgroup org-sparse-trees nil
  1244. "Options concerning sparse trees in Org-mode."
  1245. :tag "Org Sparse Trees"
  1246. :group 'org-structure)
  1247. (defcustom org-highlight-sparse-tree-matches t
  1248. "Non-nil means highlight all matches that define a sparse tree.
  1249. The highlights will automatically disappear the next time the buffer is
  1250. changed by an edit command."
  1251. :group 'org-sparse-trees
  1252. :type 'boolean)
  1253. (defcustom org-remove-highlights-with-change t
  1254. "Non-nil means any change to the buffer will remove temporary highlights.
  1255. Such highlights are created by `org-occur' and `org-clock-display'.
  1256. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1257. The highlights created by `org-preview-latex-fragment' always need
  1258. `C-c C-c' to be removed."
  1259. :group 'org-sparse-trees
  1260. :group 'org-time
  1261. :type 'boolean)
  1262. (defcustom org-occur-hook '(org-first-headline-recenter)
  1263. "Hook that is run after `org-occur' has constructed a sparse tree.
  1264. This can be used to recenter the window to show as much of the structure
  1265. as possible."
  1266. :group 'org-sparse-trees
  1267. :type 'hook)
  1268. (defgroup org-imenu-and-speedbar nil
  1269. "Options concerning imenu and speedbar in Org-mode."
  1270. :tag "Org Imenu and Speedbar"
  1271. :group 'org-structure)
  1272. (defcustom org-imenu-depth 2
  1273. "The maximum level for Imenu access to Org-mode headlines.
  1274. This also applied for speedbar access."
  1275. :group 'org-imenu-and-speedbar
  1276. :type 'integer)
  1277. (defgroup org-table nil
  1278. "Options concerning tables in Org-mode."
  1279. :tag "Org Table"
  1280. :group 'org)
  1281. (defcustom org-enable-table-editor 'optimized
  1282. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1283. When nil, such lines will be treated like ordinary lines.
  1284. When equal to the symbol `optimized', the table editor will be optimized to
  1285. do the following:
  1286. - Automatic overwrite mode in front of whitespace in table fields.
  1287. This makes the structure of the table stay in tact as long as the edited
  1288. field does not exceed the column width.
  1289. - Minimize the number of realigns. Normally, the table is aligned each time
  1290. TAB or RET are pressed to move to another field. With optimization this
  1291. happens only if changes to a field might have changed the column width.
  1292. Optimization requires replacing the functions `self-insert-command',
  1293. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1294. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1295. very good at guessing when a re-align will be necessary, but you can always
  1296. force one with \\[org-ctrl-c-ctrl-c].
  1297. If you would like to use the optimized version in Org-mode, but the
  1298. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1299. This variable can be used to turn on and off the table editor during a session,
  1300. but in order to toggle optimization, a restart is required.
  1301. See also the variable `org-table-auto-blank-field'."
  1302. :group 'org-table
  1303. :type '(choice
  1304. (const :tag "off" nil)
  1305. (const :tag "on" t)
  1306. (const :tag "on, optimized" optimized)))
  1307. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1308. (version<= emacs-version "24.1"))
  1309. "Non-nil means cluster self-insert commands for undo when possible.
  1310. If this is set, then, like in the Emacs command loop, 20 consecutive
  1311. characters will be undone together.
  1312. This is configurable, because there is some impact on typing performance."
  1313. :group 'org-table
  1314. :type 'boolean)
  1315. (defcustom org-table-tab-recognizes-table.el t
  1316. "Non-nil means TAB will automatically notice a table.el table.
  1317. When it sees such a table, it moves point into it and - if necessary -
  1318. calls `table-recognize-table'."
  1319. :group 'org-table-editing
  1320. :type 'boolean)
  1321. (defgroup org-link nil
  1322. "Options concerning links in Org-mode."
  1323. :tag "Org Link"
  1324. :group 'org)
  1325. (defvar org-link-abbrev-alist-local nil
  1326. "Buffer-local version of `org-link-abbrev-alist', which see.
  1327. The value of this is taken from the #+LINK lines.")
  1328. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1329. (defcustom org-link-abbrev-alist nil
  1330. "Alist of link abbreviations.
  1331. The car of each element is a string, to be replaced at the start of a link.
  1332. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1333. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1334. [[linkkey:tag][description]]
  1335. The 'linkkey' must be a word word, starting with a letter, followed
  1336. by letters, numbers, '-' or '_'.
  1337. If REPLACE is a string, the tag will simply be appended to create the link.
  1338. If the string contains \"%s\", the tag will be inserted there. If the string
  1339. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1340. at that point (see the function `url-hexify-string'). If the string contains
  1341. the specifier \"%(my-function)\", then the custom function `my-function' will
  1342. be invoked: this function takes the tag as its only argument and must return
  1343. a string.
  1344. REPLACE may also be a function that will be called with the tag as the
  1345. only argument to create the link, which should be returned as a string.
  1346. See the manual for examples."
  1347. :group 'org-link
  1348. :type '(repeat
  1349. (cons
  1350. (string :tag "Protocol")
  1351. (choice
  1352. (string :tag "Format")
  1353. (function)))))
  1354. (defcustom org-descriptive-links t
  1355. "Non-nil means Org will display descriptive links.
  1356. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1357. \"Org Website\", hiding the link itself and just displaying its
  1358. description. When set to `nil', Org will display the full links
  1359. literally.
  1360. You can interactively set the value of this variable by calling
  1361. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1362. :group 'org-link
  1363. :type 'boolean)
  1364. (defcustom org-link-file-path-type 'adaptive
  1365. "How the path name in file links should be stored.
  1366. Valid values are:
  1367. relative Relative to the current directory, i.e. the directory of the file
  1368. into which the link is being inserted.
  1369. absolute Absolute path, if possible with ~ for home directory.
  1370. noabbrev Absolute path, no abbreviation of home directory.
  1371. adaptive Use relative path for files in the current directory and sub-
  1372. directories of it. For other files, use an absolute path."
  1373. :group 'org-link
  1374. :type '(choice
  1375. (const relative)
  1376. (const absolute)
  1377. (const noabbrev)
  1378. (const adaptive)))
  1379. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1380. "Types of links that should be activated in Org-mode files.
  1381. This is a list of symbols, each leading to the activation of a certain link
  1382. type. In principle, it does not hurt to turn on most link types - there may
  1383. be a small gain when turning off unused link types. The types are:
  1384. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1385. angle Links in angular brackets that may contain whitespace like
  1386. <bbdb:Carsten Dominik>.
  1387. plain Plain links in normal text, no whitespace, like http://google.com.
  1388. radio Text that is matched by a radio target, see manual for details.
  1389. tag Tag settings in a headline (link to tag search).
  1390. date Time stamps (link to calendar).
  1391. footnote Footnote labels.
  1392. Changing this variable requires a restart of Emacs to become effective."
  1393. :group 'org-link
  1394. :type '(set :greedy t
  1395. (const :tag "Double bracket links" bracket)
  1396. (const :tag "Angular bracket links" angle)
  1397. (const :tag "Plain text links" plain)
  1398. (const :tag "Radio target matches" radio)
  1399. (const :tag "Tags" tag)
  1400. (const :tag "Timestamps" date)
  1401. (const :tag "Footnotes" footnote)))
  1402. (defcustom org-make-link-description-function nil
  1403. "Function to use for generating link descriptions from links.
  1404. When nil, the link location will be used. This function must take
  1405. two parameters: the first one is the link, the second one is the
  1406. description generated by `org-insert-link'. The function should
  1407. return the description to use."
  1408. :group 'org-link
  1409. :type 'function)
  1410. (defgroup org-link-store nil
  1411. "Options concerning storing links in Org-mode."
  1412. :tag "Org Store Link"
  1413. :group 'org-link)
  1414. (defcustom org-url-hexify-p t
  1415. "When non-nil, hexify URL when creating a link."
  1416. :type 'boolean
  1417. :version "24.3"
  1418. :group 'org-link-store)
  1419. (defcustom org-email-link-description-format "Email %c: %.30s"
  1420. "Format of the description part of a link to an email or usenet message.
  1421. The following %-escapes will be replaced by corresponding information:
  1422. %F full \"From\" field
  1423. %f name, taken from \"From\" field, address if no name
  1424. %T full \"To\" field
  1425. %t first name in \"To\" field, address if no name
  1426. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1427. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1428. %s subject
  1429. %d date
  1430. %m message-id.
  1431. You may use normal field width specification between the % and the letter.
  1432. This is for example useful to limit the length of the subject.
  1433. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1434. :group 'org-link-store
  1435. :type 'string)
  1436. (defcustom org-from-is-user-regexp
  1437. (let (r1 r2)
  1438. (when (and user-mail-address (not (string= user-mail-address "")))
  1439. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1440. (when (and user-full-name (not (string= user-full-name "")))
  1441. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1442. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1443. "Regexp matched against the \"From:\" header of an email or usenet message.
  1444. It should match if the message is from the user him/herself."
  1445. :group 'org-link-store
  1446. :type 'regexp)
  1447. (defcustom org-context-in-file-links t
  1448. "Non-nil means file links from `org-store-link' contain context.
  1449. A search string will be added to the file name with :: as separator and
  1450. used to find the context when the link is activated by the command
  1451. `org-open-at-point'. When this option is t, the entire active region
  1452. will be placed in the search string of the file link. If set to a
  1453. positive integer, only the first n lines of context will be stored.
  1454. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1455. negates this setting for the duration of the command."
  1456. :group 'org-link-store
  1457. :type '(choice boolean integer))
  1458. (defcustom org-keep-stored-link-after-insertion nil
  1459. "Non-nil means keep link in list for entire session.
  1460. The command `org-store-link' adds a link pointing to the current
  1461. location to an internal list. These links accumulate during a session.
  1462. The command `org-insert-link' can be used to insert links into any
  1463. Org-mode file (offering completion for all stored links). When this
  1464. option is nil, every link which has been inserted once using \\[org-insert-link]
  1465. will be removed from the list, to make completing the unused links
  1466. more efficient."
  1467. :group 'org-link-store
  1468. :type 'boolean)
  1469. (defgroup org-link-follow nil
  1470. "Options concerning following links in Org-mode."
  1471. :tag "Org Follow Link"
  1472. :group 'org-link)
  1473. (defcustom org-link-translation-function nil
  1474. "Function to translate links with different syntax to Org syntax.
  1475. This can be used to translate links created for example by the Planner
  1476. or emacs-wiki packages to Org syntax.
  1477. The function must accept two parameters, a TYPE containing the link
  1478. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1479. which is everything after the link protocol. It should return a cons
  1480. with possibly modified values of type and path.
  1481. Org contains a function for this, so if you set this variable to
  1482. `org-translate-link-from-planner', you should be able follow many
  1483. links created by planner."
  1484. :group 'org-link-follow
  1485. :type 'function)
  1486. (defcustom org-follow-link-hook nil
  1487. "Hook that is run after a link has been followed."
  1488. :group 'org-link-follow
  1489. :type 'hook)
  1490. (defcustom org-tab-follows-link nil
  1491. "Non-nil means on links TAB will follow the link.
  1492. Needs to be set before org.el is loaded.
  1493. This really should not be used, it does not make sense, and the
  1494. implementation is bad."
  1495. :group 'org-link-follow
  1496. :type 'boolean)
  1497. (defcustom org-return-follows-link nil
  1498. "Non-nil means on links RET will follow the link.
  1499. In tables, the special behavior of RET has precedence."
  1500. :group 'org-link-follow
  1501. :type 'boolean)
  1502. (defcustom org-mouse-1-follows-link
  1503. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1504. "Non-nil means mouse-1 on a link will follow the link.
  1505. A longer mouse click will still set point. Does not work on XEmacs.
  1506. Needs to be set before org.el is loaded."
  1507. :group 'org-link-follow
  1508. :type 'boolean)
  1509. (defcustom org-mark-ring-length 4
  1510. "Number of different positions to be recorded in the ring.
  1511. Changing this requires a restart of Emacs to work correctly."
  1512. :group 'org-link-follow
  1513. :type 'integer)
  1514. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1515. "Non-nil means internal links in Org files must exactly match a headline.
  1516. When nil, the link search tries to match a phrase with all words
  1517. in the search text."
  1518. :group 'org-link-follow
  1519. :version "24.1"
  1520. :type '(choice
  1521. (const :tag "Use fuzzy text search" nil)
  1522. (const :tag "Match only exact headline" t)
  1523. (const :tag "Match exact headline or query to create it"
  1524. query-to-create)))
  1525. (defcustom org-link-frame-setup
  1526. '((vm . vm-visit-folder-other-frame)
  1527. (vm-imap . vm-visit-imap-folder-other-frame)
  1528. (gnus . org-gnus-no-new-news)
  1529. (file . find-file-other-window)
  1530. (wl . wl-other-frame))
  1531. "Setup the frame configuration for following links.
  1532. When following a link with Emacs, it may often be useful to display
  1533. this link in another window or frame. This variable can be used to
  1534. set this up for the different types of links.
  1535. For VM, use any of
  1536. `vm-visit-folder'
  1537. `vm-visit-folder-other-window'
  1538. `vm-visit-folder-other-frame'
  1539. For Gnus, use any of
  1540. `gnus'
  1541. `gnus-other-frame'
  1542. `org-gnus-no-new-news'
  1543. For FILE, use any of
  1544. `find-file'
  1545. `find-file-other-window'
  1546. `find-file-other-frame'
  1547. For Wanderlust use any of
  1548. `wl'
  1549. `wl-other-frame'
  1550. For the calendar, use the variable `calendar-setup'.
  1551. For BBDB, it is currently only possible to display the matches in
  1552. another window."
  1553. :group 'org-link-follow
  1554. :type '(list
  1555. (cons (const vm)
  1556. (choice
  1557. (const vm-visit-folder)
  1558. (const vm-visit-folder-other-window)
  1559. (const vm-visit-folder-other-frame)))
  1560. (cons (const gnus)
  1561. (choice
  1562. (const gnus)
  1563. (const gnus-other-frame)
  1564. (const org-gnus-no-new-news)))
  1565. (cons (const file)
  1566. (choice
  1567. (const find-file)
  1568. (const find-file-other-window)
  1569. (const find-file-other-frame)))
  1570. (cons (const wl)
  1571. (choice
  1572. (const wl)
  1573. (const wl-other-frame)))))
  1574. (defcustom org-display-internal-link-with-indirect-buffer nil
  1575. "Non-nil means use indirect buffer to display infile links.
  1576. Activating internal links (from one location in a file to another location
  1577. in the same file) normally just jumps to the location. When the link is
  1578. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1579. is displayed in
  1580. another window. When this option is set, the other window actually displays
  1581. an indirect buffer clone of the current buffer, to avoid any visibility
  1582. changes to the current buffer."
  1583. :group 'org-link-follow
  1584. :type 'boolean)
  1585. (defcustom org-open-non-existing-files nil
  1586. "Non-nil means `org-open-file' will open non-existing files.
  1587. When nil, an error will be generated.
  1588. This variable applies only to external applications because they
  1589. might choke on non-existing files. If the link is to a file that
  1590. will be opened in Emacs, the variable is ignored."
  1591. :group 'org-link-follow
  1592. :type 'boolean)
  1593. (defcustom org-open-directory-means-index-dot-org nil
  1594. "Non-nil means a link to a directory really means to index.org.
  1595. When nil, following a directory link will run dired or open a finder/explorer
  1596. window on that directory."
  1597. :group 'org-link-follow
  1598. :type 'boolean)
  1599. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1600. "Function and arguments to call for following mailto links.
  1601. This is a list with the first element being a Lisp function, and the
  1602. remaining elements being arguments to the function. In string arguments,
  1603. %a will be replaced by the address, and %s will be replaced by the subject
  1604. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1605. :group 'org-link-follow
  1606. :type '(choice
  1607. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1608. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1609. (const :tag "message-mail" (message-mail "%a" "%s"))
  1610. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1611. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1612. "Non-nil means ask for confirmation before executing shell links.
  1613. Shell links can be dangerous: just think about a link
  1614. [[shell:rm -rf ~/*][Google Search]]
  1615. This link would show up in your Org-mode document as \"Google Search\",
  1616. but really it would remove your entire home directory.
  1617. Therefore we advise against setting this variable to nil.
  1618. Just change it to `y-or-n-p' if you want to confirm with a
  1619. single keystroke rather than having to type \"yes\"."
  1620. :group 'org-link-follow
  1621. :type '(choice
  1622. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1623. (const :tag "with y-or-n (faster)" y-or-n-p)
  1624. (const :tag "no confirmation (dangerous)" nil)))
  1625. (put 'org-confirm-shell-link-function
  1626. 'safe-local-variable
  1627. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1628. (defcustom org-confirm-shell-link-not-regexp ""
  1629. "A regexp to skip confirmation for shell links."
  1630. :group 'org-link-follow
  1631. :version "24.1"
  1632. :type 'regexp)
  1633. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1634. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1635. Elisp links can be dangerous: just think about a link
  1636. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1637. This link would show up in your Org-mode document as \"Google Search\",
  1638. but really it would remove your entire home directory.
  1639. Therefore we advise against setting this variable to nil.
  1640. Just change it to `y-or-n-p' if you want to confirm with a
  1641. single keystroke rather than having to type \"yes\"."
  1642. :group 'org-link-follow
  1643. :type '(choice
  1644. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1645. (const :tag "with y-or-n (faster)" y-or-n-p)
  1646. (const :tag "no confirmation (dangerous)" nil)))
  1647. (put 'org-confirm-shell-link-function
  1648. 'safe-local-variable
  1649. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1650. (defcustom org-confirm-elisp-link-not-regexp ""
  1651. "A regexp to skip confirmation for Elisp links."
  1652. :group 'org-link-follow
  1653. :version "24.1"
  1654. :type 'regexp)
  1655. (defconst org-file-apps-defaults-gnu
  1656. '((remote . emacs)
  1657. (system . mailcap)
  1658. (t . mailcap))
  1659. "Default file applications on a UNIX or GNU/Linux system.
  1660. See `org-file-apps'.")
  1661. (defconst org-file-apps-defaults-macosx
  1662. '((remote . emacs)
  1663. (t . "open %s")
  1664. (system . "open %s")
  1665. ("ps.gz" . "gv %s")
  1666. ("eps.gz" . "gv %s")
  1667. ("dvi" . "xdvi %s")
  1668. ("fig" . "xfig %s"))
  1669. "Default file applications on a MacOS X system.
  1670. The system \"open\" is known as a default, but we use X11 applications
  1671. for some files for which the OS does not have a good default.
  1672. See `org-file-apps'.")
  1673. (defconst org-file-apps-defaults-windowsnt
  1674. (list
  1675. '(remote . emacs)
  1676. (cons t
  1677. (list (if (featurep 'xemacs)
  1678. 'mswindows-shell-execute
  1679. 'w32-shell-execute)
  1680. "open" 'file))
  1681. (cons 'system
  1682. (list (if (featurep 'xemacs)
  1683. 'mswindows-shell-execute
  1684. 'w32-shell-execute)
  1685. "open" 'file)))
  1686. "Default file applications on a Windows NT system.
  1687. The system \"open\" is used for most files.
  1688. See `org-file-apps'.")
  1689. (defcustom org-file-apps
  1690. '((auto-mode . emacs)
  1691. ("\\.mm\\'" . default)
  1692. ("\\.x?html?\\'" . default)
  1693. ("\\.pdf\\'" . default))
  1694. "External applications for opening `file:path' items in a document.
  1695. Org-mode uses system defaults for different file types, but
  1696. you can use this variable to set the application for a given file
  1697. extension. The entries in this list are cons cells where the car identifies
  1698. files and the cdr the corresponding command. Possible values for the
  1699. file identifier are
  1700. \"string\" A string as a file identifier can be interpreted in different
  1701. ways, depending on its contents:
  1702. - Alphanumeric characters only:
  1703. Match links with this file extension.
  1704. Example: (\"pdf\" . \"evince %s\")
  1705. to open PDFs with evince.
  1706. - Regular expression: Match links where the
  1707. filename matches the regexp. If you want to
  1708. use groups here, use shy groups.
  1709. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1710. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1711. to open *.html and *.xhtml with firefox.
  1712. - Regular expression which contains (non-shy) groups:
  1713. Match links where the whole link, including \"::\", and
  1714. anything after that, matches the regexp.
  1715. In a custom command string, %1, %2, etc. are replaced with
  1716. the parts of the link that were matched by the groups.
  1717. For backwards compatibility, if a command string is given
  1718. that does not use any of the group matches, this case is
  1719. handled identically to the second one (i.e. match against
  1720. file name only).
  1721. In a custom lisp form, you can access the group matches with
  1722. (match-string n link).
  1723. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1724. to open [[file:document.pdf::5]] with evince at page 5.
  1725. `directory' Matches a directory
  1726. `remote' Matches a remote file, accessible through tramp or efs.
  1727. Remote files most likely should be visited through Emacs
  1728. because external applications cannot handle such paths.
  1729. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1730. so all files Emacs knows how to handle. Using this with
  1731. command `emacs' will open most files in Emacs. Beware that this
  1732. will also open html files inside Emacs, unless you add
  1733. (\"html\" . default) to the list as well.
  1734. t Default for files not matched by any of the other options.
  1735. `system' The system command to open files, like `open' on Windows
  1736. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1737. that will be selected if you call `C-c C-o' with a double
  1738. \\[universal-argument] \\[universal-argument] prefix.
  1739. Possible values for the command are:
  1740. `emacs' The file will be visited by the current Emacs process.
  1741. `default' Use the default application for this file type, which is the
  1742. association for t in the list, most likely in the system-specific
  1743. part.
  1744. This can be used to overrule an unwanted setting in the
  1745. system-specific variable.
  1746. `system' Use the system command for opening files, like \"open\".
  1747. This command is specified by the entry whose car is `system'.
  1748. Most likely, the system-specific version of this variable
  1749. does define this command, but you can overrule/replace it
  1750. here.
  1751. string A command to be executed by a shell; %s will be replaced
  1752. by the path to the file.
  1753. sexp A Lisp form which will be evaluated. The file path will
  1754. be available in the Lisp variable `file'.
  1755. For more examples, see the system specific constants
  1756. `org-file-apps-defaults-macosx'
  1757. `org-file-apps-defaults-windowsnt'
  1758. `org-file-apps-defaults-gnu'."
  1759. :group 'org-link-follow
  1760. :type '(repeat
  1761. (cons (choice :value ""
  1762. (string :tag "Extension")
  1763. (const :tag "System command to open files" system)
  1764. (const :tag "Default for unrecognized files" t)
  1765. (const :tag "Remote file" remote)
  1766. (const :tag "Links to a directory" directory)
  1767. (const :tag "Any files that have Emacs modes"
  1768. auto-mode))
  1769. (choice :value ""
  1770. (const :tag "Visit with Emacs" emacs)
  1771. (const :tag "Use default" default)
  1772. (const :tag "Use the system command" system)
  1773. (string :tag "Command")
  1774. (sexp :tag "Lisp form")))))
  1775. (defcustom org-doi-server-url "http://dx.doi.org/"
  1776. "The URL of the DOI server."
  1777. :type 'string
  1778. :version "24.3"
  1779. :group 'org-link-follow)
  1780. (defgroup org-refile nil
  1781. "Options concerning refiling entries in Org-mode."
  1782. :tag "Org Refile"
  1783. :group 'org)
  1784. (defcustom org-directory "~/org"
  1785. "Directory with org files.
  1786. This is just a default location to look for Org files. There is no need
  1787. at all to put your files into this directory. It is only used in the
  1788. following situations:
  1789. 1. When a capture template specifies a target file that is not an
  1790. absolute path. The path will then be interpreted relative to
  1791. `org-directory'
  1792. 2. When a capture note is filed away in an interactive way (when exiting the
  1793. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1794. with `org-directory' as the default path."
  1795. :group 'org-refile
  1796. :group 'org-remember
  1797. :group 'org-capture
  1798. :type 'directory)
  1799. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1800. "Default target for storing notes.
  1801. Used as a fall back file for org-remember.el and org-capture.el, for
  1802. templates that do not specify a target file."
  1803. :group 'org-refile
  1804. :group 'org-remember
  1805. :group 'org-capture
  1806. :type '(choice
  1807. (const :tag "Default from remember-data-file" nil)
  1808. file))
  1809. (defcustom org-goto-interface 'outline
  1810. "The default interface to be used for `org-goto'.
  1811. Allowed values are:
  1812. outline The interface shows an outline of the relevant file
  1813. and the correct heading is found by moving through
  1814. the outline or by searching with incremental search.
  1815. outline-path-completion Headlines in the current buffer are offered via
  1816. completion. This is the interface also used by
  1817. the refile command."
  1818. :group 'org-refile
  1819. :type '(choice
  1820. (const :tag "Outline" outline)
  1821. (const :tag "Outline-path-completion" outline-path-completion)))
  1822. (defcustom org-goto-max-level 5
  1823. "Maximum target level when running `org-goto' with refile interface."
  1824. :group 'org-refile
  1825. :type 'integer)
  1826. (defcustom org-reverse-note-order nil
  1827. "Non-nil means store new notes at the beginning of a file or entry.
  1828. When nil, new notes will be filed to the end of a file or entry.
  1829. This can also be a list with cons cells of regular expressions that
  1830. are matched against file names, and values."
  1831. :group 'org-remember
  1832. :group 'org-capture
  1833. :group 'org-refile
  1834. :type '(choice
  1835. (const :tag "Reverse always" t)
  1836. (const :tag "Reverse never" nil)
  1837. (repeat :tag "By file name regexp"
  1838. (cons regexp boolean))))
  1839. (defcustom org-log-refile nil
  1840. "Information to record when a task is refiled.
  1841. Possible values are:
  1842. nil Don't add anything
  1843. time Add a time stamp to the task
  1844. note Prompt for a note and add it with template `org-log-note-headings'
  1845. This option can also be set with on a per-file-basis with
  1846. #+STARTUP: nologrefile
  1847. #+STARTUP: logrefile
  1848. #+STARTUP: lognoterefile
  1849. You can have local logging settings for a subtree by setting the LOGGING
  1850. property to one or more of these keywords.
  1851. When bulk-refiling from the agenda, the value `note' is forbidden and
  1852. will temporarily be changed to `time'."
  1853. :group 'org-refile
  1854. :group 'org-progress
  1855. :version "24.1"
  1856. :type '(choice
  1857. (const :tag "No logging" nil)
  1858. (const :tag "Record timestamp" time)
  1859. (const :tag "Record timestamp with note." note)))
  1860. (defcustom org-refile-targets nil
  1861. "Targets for refiling entries with \\[org-refile].
  1862. This is a list of cons cells. Each cell contains:
  1863. - a specification of the files to be considered, either a list of files,
  1864. or a symbol whose function or variable value will be used to retrieve
  1865. a file name or a list of file names. If you use `org-agenda-files' for
  1866. that, all agenda files will be scanned for targets. Nil means consider
  1867. headings in the current buffer.
  1868. - A specification of how to find candidate refile targets. This may be
  1869. any of:
  1870. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1871. This tag has to be present in all target headlines, inheritance will
  1872. not be considered.
  1873. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1874. todo keyword.
  1875. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1876. headlines that are refiling targets.
  1877. - a cons cell (:level . N). Any headline of level N is considered a target.
  1878. Note that, when `org-odd-levels-only' is set, level corresponds to
  1879. order in hierarchy, not to the number of stars.
  1880. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1881. Note that, when `org-odd-levels-only' is set, level corresponds to
  1882. order in hierarchy, not to the number of stars.
  1883. Each element of this list generates a set of possible targets.
  1884. The union of these sets is presented (with completion) to
  1885. the user by `org-refile'.
  1886. You can set the variable `org-refile-target-verify-function' to a function
  1887. to verify each headline found by the simple criteria above.
  1888. When this variable is nil, all top-level headlines in the current buffer
  1889. are used, equivalent to the value `((nil . (:level . 1))'."
  1890. :group 'org-refile
  1891. :type '(repeat
  1892. (cons
  1893. (choice :value org-agenda-files
  1894. (const :tag "All agenda files" org-agenda-files)
  1895. (const :tag "Current buffer" nil)
  1896. (function) (variable) (file))
  1897. (choice :tag "Identify target headline by"
  1898. (cons :tag "Specific tag" (const :value :tag) (string))
  1899. (cons :tag "TODO keyword" (const :value :todo) (string))
  1900. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1901. (cons :tag "Level number" (const :value :level) (integer))
  1902. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1903. (defcustom org-refile-target-verify-function nil
  1904. "Function to verify if the headline at point should be a refile target.
  1905. The function will be called without arguments, with point at the
  1906. beginning of the headline. It should return t and leave point
  1907. where it is if the headline is a valid target for refiling.
  1908. If the target should not be selected, the function must return nil.
  1909. In addition to this, it may move point to a place from where the search
  1910. should be continued. For example, the function may decide that the entire
  1911. subtree of the current entry should be excluded and move point to the end
  1912. of the subtree."
  1913. :group 'org-refile
  1914. :type 'function)
  1915. (defcustom org-refile-use-cache nil
  1916. "Non-nil means cache refile targets to speed up the process.
  1917. The cache for a particular file will be updated automatically when
  1918. the buffer has been killed, or when any of the marker used for flagging
  1919. refile targets no longer points at a live buffer.
  1920. If you have added new entries to a buffer that might themselves be targets,
  1921. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1922. find that easier, `C-u C-u C-u C-c C-w'."
  1923. :group 'org-refile
  1924. :version "24.1"
  1925. :type 'boolean)
  1926. (defcustom org-refile-use-outline-path nil
  1927. "Non-nil means provide refile targets as paths.
  1928. So a level 3 headline will be available as level1/level2/level3.
  1929. When the value is `file', also include the file name (without directory)
  1930. into the path. In this case, you can also stop the completion after
  1931. the file name, to get entries inserted as top level in the file.
  1932. When `full-file-path', include the full file path."
  1933. :group 'org-refile
  1934. :type '(choice
  1935. (const :tag "Not" nil)
  1936. (const :tag "Yes" t)
  1937. (const :tag "Start with file name" file)
  1938. (const :tag "Start with full file path" full-file-path)))
  1939. (defcustom org-outline-path-complete-in-steps t
  1940. "Non-nil means complete the outline path in hierarchical steps.
  1941. When Org-mode uses the refile interface to select an outline path
  1942. \(see variable `org-refile-use-outline-path'), the completion of
  1943. the path can be done is a single go, or if can be done in steps down
  1944. the headline hierarchy. Going in steps is probably the best if you
  1945. do not use a special completion package like `ido' or `icicles'.
  1946. However, when using these packages, going in one step can be very
  1947. fast, while still showing the whole path to the entry."
  1948. :group 'org-refile
  1949. :type 'boolean)
  1950. (defcustom org-refile-allow-creating-parent-nodes nil
  1951. "Non-nil means allow to create new nodes as refile targets.
  1952. New nodes are then created by adding \"/new node name\" to the completion
  1953. of an existing node. When the value of this variable is `confirm',
  1954. new node creation must be confirmed by the user (recommended)
  1955. When nil, the completion must match an existing entry.
  1956. Note that, if the new heading is not seen by the criteria
  1957. listed in `org-refile-targets', multiple instances of the same
  1958. heading would be created by trying again to file under the new
  1959. heading."
  1960. :group 'org-refile
  1961. :type '(choice
  1962. (const :tag "Never" nil)
  1963. (const :tag "Always" t)
  1964. (const :tag "Prompt for confirmation" confirm)))
  1965. (defcustom org-refile-active-region-within-subtree nil
  1966. "Non-nil means also refile active region within a subtree.
  1967. By default `org-refile' doesn't allow refiling regions if they
  1968. don't contain a set of subtrees, but it might be convenient to
  1969. do so sometimes: in that case, the first line of the region is
  1970. converted to a headline before refiling."
  1971. :group 'org-refile
  1972. :version "24.1"
  1973. :type 'boolean)
  1974. (defgroup org-todo nil
  1975. "Options concerning TODO items in Org-mode."
  1976. :tag "Org TODO"
  1977. :group 'org)
  1978. (defgroup org-progress nil
  1979. "Options concerning Progress logging in Org-mode."
  1980. :tag "Org Progress"
  1981. :group 'org-time)
  1982. (defvar org-todo-interpretation-widgets
  1983. '((:tag "Sequence (cycling hits every state)" sequence)
  1984. (:tag "Type (cycling directly to DONE)" type))
  1985. "The available interpretation symbols for customizing `org-todo-keywords'.
  1986. Interested libraries should add to this list.")
  1987. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1988. "List of TODO entry keyword sequences and their interpretation.
  1989. \\<org-mode-map>This is a list of sequences.
  1990. Each sequence starts with a symbol, either `sequence' or `type',
  1991. indicating if the keywords should be interpreted as a sequence of
  1992. action steps, or as different types of TODO items. The first
  1993. keywords are states requiring action - these states will select a headline
  1994. for inclusion into the global TODO list Org-mode produces. If one of
  1995. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1996. signify that no further action is necessary. If \"|\" is not found,
  1997. the last keyword is treated as the only DONE state of the sequence.
  1998. The command \\[org-todo] cycles an entry through these states, and one
  1999. additional state where no keyword is present. For details about this
  2000. cycling, see the manual.
  2001. TODO keywords and interpretation can also be set on a per-file basis with
  2002. the special #+SEQ_TODO and #+TYP_TODO lines.
  2003. Each keyword can optionally specify a character for fast state selection
  2004. \(in combination with the variable `org-use-fast-todo-selection')
  2005. and specifiers for state change logging, using the same syntax that
  2006. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2007. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2008. indicates to record a time stamp each time this state is selected.
  2009. Each keyword may also specify if a timestamp or a note should be
  2010. recorded when entering or leaving the state, by adding additional
  2011. characters in the parenthesis after the keyword. This looks like this:
  2012. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2013. record only the time of the state change. With X and Y being either
  2014. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2015. Y when leaving the state if and only if the *target* state does not
  2016. define X. You may omit any of the fast-selection key or X or /Y,
  2017. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2018. For backward compatibility, this variable may also be just a list
  2019. of keywords. In this case the interpretation (sequence or type) will be
  2020. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2021. :group 'org-todo
  2022. :group 'org-keywords
  2023. :type '(choice
  2024. (repeat :tag "Old syntax, just keywords"
  2025. (string :tag "Keyword"))
  2026. (repeat :tag "New syntax"
  2027. (cons
  2028. (choice
  2029. :tag "Interpretation"
  2030. ;;Quick and dirty way to see
  2031. ;;`org-todo-interpretations'. This takes the
  2032. ;;place of item arguments
  2033. :convert-widget
  2034. (lambda (widget)
  2035. (widget-put widget
  2036. :args (mapcar
  2037. #'(lambda (x)
  2038. (widget-convert
  2039. (cons 'const x)))
  2040. org-todo-interpretation-widgets))
  2041. widget))
  2042. (repeat
  2043. (string :tag "Keyword"))))))
  2044. (defvar org-todo-keywords-1 nil
  2045. "All TODO and DONE keywords active in a buffer.")
  2046. (make-variable-buffer-local 'org-todo-keywords-1)
  2047. (defvar org-todo-keywords-for-agenda nil)
  2048. (defvar org-done-keywords-for-agenda nil)
  2049. (defvar org-drawers-for-agenda nil)
  2050. (defvar org-todo-keyword-alist-for-agenda nil)
  2051. (defvar org-tag-alist-for-agenda nil
  2052. "Alist of all tags from all agenda files.")
  2053. (defvar org-tag-groups-alist-for-agenda nil
  2054. "Alist of all groups tags from all current agenda files.")
  2055. (defvar org-tag-groups-alist nil)
  2056. (make-variable-buffer-local 'org-tag-groups-alist)
  2057. (defvar org-agenda-contributing-files nil)
  2058. (defvar org-not-done-keywords nil)
  2059. (make-variable-buffer-local 'org-not-done-keywords)
  2060. (defvar org-done-keywords nil)
  2061. (make-variable-buffer-local 'org-done-keywords)
  2062. (defvar org-todo-heads nil)
  2063. (make-variable-buffer-local 'org-todo-heads)
  2064. (defvar org-todo-sets nil)
  2065. (make-variable-buffer-local 'org-todo-sets)
  2066. (defvar org-todo-log-states nil)
  2067. (make-variable-buffer-local 'org-todo-log-states)
  2068. (defvar org-todo-kwd-alist nil)
  2069. (make-variable-buffer-local 'org-todo-kwd-alist)
  2070. (defvar org-todo-key-alist nil)
  2071. (make-variable-buffer-local 'org-todo-key-alist)
  2072. (defvar org-todo-key-trigger nil)
  2073. (make-variable-buffer-local 'org-todo-key-trigger)
  2074. (defcustom org-todo-interpretation 'sequence
  2075. "Controls how TODO keywords are interpreted.
  2076. This variable is in principle obsolete and is only used for
  2077. backward compatibility, if the interpretation of todo keywords is
  2078. not given already in `org-todo-keywords'. See that variable for
  2079. more information."
  2080. :group 'org-todo
  2081. :group 'org-keywords
  2082. :type '(choice (const sequence)
  2083. (const type)))
  2084. (defcustom org-use-fast-todo-selection t
  2085. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2086. This variable describes if and under what circumstances the cycling
  2087. mechanism for TODO keywords will be replaced by a single-key, direct
  2088. selection scheme.
  2089. When nil, fast selection is never used.
  2090. When the symbol `prefix', it will be used when `org-todo' is called
  2091. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2092. `C-u t' in an agenda buffer.
  2093. When t, fast selection is used by default. In this case, the prefix
  2094. argument forces cycling instead.
  2095. In all cases, the special interface is only used if access keys have
  2096. actually been assigned by the user, i.e. if keywords in the configuration
  2097. are followed by a letter in parenthesis, like TODO(t)."
  2098. :group 'org-todo
  2099. :type '(choice
  2100. (const :tag "Never" nil)
  2101. (const :tag "By default" t)
  2102. (const :tag "Only with C-u C-c C-t" prefix)))
  2103. (defcustom org-provide-todo-statistics t
  2104. "Non-nil means update todo statistics after insert and toggle.
  2105. ALL-HEADLINES means update todo statistics by including headlines
  2106. with no TODO keyword as well, counting them as not done.
  2107. A list of TODO keywords means the same, but skip keywords that are
  2108. not in this list.
  2109. When this is set, todo statistics is updated in the parent of the
  2110. current entry each time a todo state is changed."
  2111. :group 'org-todo
  2112. :type '(choice
  2113. (const :tag "Yes, only for TODO entries" t)
  2114. (const :tag "Yes, including all entries" 'all-headlines)
  2115. (repeat :tag "Yes, for TODOs in this list"
  2116. (string :tag "TODO keyword"))
  2117. (other :tag "No TODO statistics" nil)))
  2118. (defcustom org-hierarchical-todo-statistics t
  2119. "Non-nil means TODO statistics covers just direct children.
  2120. When nil, all entries in the subtree are considered.
  2121. This has only an effect if `org-provide-todo-statistics' is set.
  2122. To set this to nil for only a single subtree, use a COOKIE_DATA
  2123. property and include the word \"recursive\" into the value."
  2124. :group 'org-todo
  2125. :type 'boolean)
  2126. (defcustom org-after-todo-state-change-hook nil
  2127. "Hook which is run after the state of a TODO item was changed.
  2128. The new state (a string with a TODO keyword, or nil) is available in the
  2129. Lisp variable `org-state'."
  2130. :group 'org-todo
  2131. :type 'hook)
  2132. (defvar org-blocker-hook nil
  2133. "Hook for functions that are allowed to block a state change.
  2134. Functions in this hook should not modify the buffer.
  2135. Each function gets as its single argument a property list,
  2136. see `org-trigger-hook' for more information about this list.
  2137. If any of the functions in this hook returns nil, the state change
  2138. is blocked.")
  2139. (defvar org-trigger-hook nil
  2140. "Hook for functions that are triggered by a state change.
  2141. Each function gets as its single argument a property list with at
  2142. least the following elements:
  2143. (:type type-of-change :position pos-at-entry-start
  2144. :from old-state :to new-state)
  2145. Depending on the type, more properties may be present.
  2146. This mechanism is currently implemented for:
  2147. TODO state changes
  2148. ------------------
  2149. :type todo-state-change
  2150. :from previous state (keyword as a string), or nil, or a symbol
  2151. 'todo' or 'done', to indicate the general type of state.
  2152. :to new state, like in :from")
  2153. (defcustom org-enforce-todo-dependencies nil
  2154. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2155. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2156. be blocked if any prior sibling is not yet done.
  2157. Finally, if the parent is blocked because of ordered siblings of its own,
  2158. the child will also be blocked."
  2159. :set (lambda (var val)
  2160. (set var val)
  2161. (if val
  2162. (add-hook 'org-blocker-hook
  2163. 'org-block-todo-from-children-or-siblings-or-parent)
  2164. (remove-hook 'org-blocker-hook
  2165. 'org-block-todo-from-children-or-siblings-or-parent)))
  2166. :group 'org-todo
  2167. :type 'boolean)
  2168. (defcustom org-enforce-todo-checkbox-dependencies nil
  2169. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2170. When this is nil, checkboxes have no influence on switching TODO states.
  2171. When non-nil, you first need to check off all check boxes before the TODO
  2172. entry can be switched to DONE.
  2173. This variable needs to be set before org.el is loaded, and you need to
  2174. restart Emacs after a change to make the change effective. The only way
  2175. to change is while Emacs is running is through the customize interface."
  2176. :set (lambda (var val)
  2177. (set var val)
  2178. (if val
  2179. (add-hook 'org-blocker-hook
  2180. 'org-block-todo-from-checkboxes)
  2181. (remove-hook 'org-blocker-hook
  2182. 'org-block-todo-from-checkboxes)))
  2183. :group 'org-todo
  2184. :type 'boolean)
  2185. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2186. "Non-nil means inserting a TODO heading is treated as state change.
  2187. So when the command \\[org-insert-todo-heading] is used, state change
  2188. logging will apply if appropriate. When nil, the new TODO item will
  2189. be inserted directly, and no logging will take place."
  2190. :group 'org-todo
  2191. :type 'boolean)
  2192. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2193. "Non-nil means switching TODO states with S-cursor counts as state change.
  2194. This is the default behavior. However, setting this to nil allows a
  2195. convenient way to select a TODO state and bypass any logging associated
  2196. with that."
  2197. :group 'org-todo
  2198. :type 'boolean)
  2199. (defcustom org-todo-state-tags-triggers nil
  2200. "Tag changes that should be triggered by TODO state changes.
  2201. This is a list. Each entry is
  2202. (state-change (tag . flag) .......)
  2203. State-change can be a string with a state, and empty string to indicate the
  2204. state that has no TODO keyword, or it can be one of the symbols `todo'
  2205. or `done', meaning any not-done or done state, respectively."
  2206. :group 'org-todo
  2207. :group 'org-tags
  2208. :type '(repeat
  2209. (cons (choice :tag "When changing to"
  2210. (const :tag "Not-done state" todo)
  2211. (const :tag "Done state" done)
  2212. (string :tag "State"))
  2213. (repeat
  2214. (cons :tag "Tag action"
  2215. (string :tag "Tag")
  2216. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2217. (defcustom org-log-done nil
  2218. "Information to record when a task moves to the DONE state.
  2219. Possible values are:
  2220. nil Don't add anything, just change the keyword
  2221. time Add a time stamp to the task
  2222. note Prompt for a note and add it with template `org-log-note-headings'
  2223. This option can also be set with on a per-file-basis with
  2224. #+STARTUP: nologdone
  2225. #+STARTUP: logdone
  2226. #+STARTUP: lognotedone
  2227. You can have local logging settings for a subtree by setting the LOGGING
  2228. property to one or more of these keywords."
  2229. :group 'org-todo
  2230. :group 'org-progress
  2231. :type '(choice
  2232. (const :tag "No logging" nil)
  2233. (const :tag "Record CLOSED timestamp" time)
  2234. (const :tag "Record CLOSED timestamp with note." note)))
  2235. ;; Normalize old uses of org-log-done.
  2236. (cond
  2237. ((eq org-log-done t) (setq org-log-done 'time))
  2238. ((and (listp org-log-done) (memq 'done org-log-done))
  2239. (setq org-log-done 'note)))
  2240. (defcustom org-log-reschedule nil
  2241. "Information to record when the scheduling date of a tasks is modified.
  2242. Possible values are:
  2243. nil Don't add anything, just change the date
  2244. time Add a time stamp to the task
  2245. note Prompt for a note and add it with template `org-log-note-headings'
  2246. This option can also be set with on a per-file-basis with
  2247. #+STARTUP: nologreschedule
  2248. #+STARTUP: logreschedule
  2249. #+STARTUP: lognotereschedule"
  2250. :group 'org-todo
  2251. :group 'org-progress
  2252. :type '(choice
  2253. (const :tag "No logging" nil)
  2254. (const :tag "Record timestamp" time)
  2255. (const :tag "Record timestamp with note." note)))
  2256. (defcustom org-log-redeadline nil
  2257. "Information to record when the deadline date of a tasks is modified.
  2258. Possible values are:
  2259. nil Don't add anything, just change the date
  2260. time Add a time stamp to the task
  2261. note Prompt for a note and add it with template `org-log-note-headings'
  2262. This option can also be set with on a per-file-basis with
  2263. #+STARTUP: nologredeadline
  2264. #+STARTUP: logredeadline
  2265. #+STARTUP: lognoteredeadline
  2266. You can have local logging settings for a subtree by setting the LOGGING
  2267. property to one or more of these keywords."
  2268. :group 'org-todo
  2269. :group 'org-progress
  2270. :type '(choice
  2271. (const :tag "No logging" nil)
  2272. (const :tag "Record timestamp" time)
  2273. (const :tag "Record timestamp with note." note)))
  2274. (defcustom org-log-note-clock-out nil
  2275. "Non-nil means record a note when clocking out of an item.
  2276. This can also be configured on a per-file basis by adding one of
  2277. the following lines anywhere in the buffer:
  2278. #+STARTUP: lognoteclock-out
  2279. #+STARTUP: nolognoteclock-out"
  2280. :group 'org-todo
  2281. :group 'org-progress
  2282. :type 'boolean)
  2283. (defcustom org-log-done-with-time t
  2284. "Non-nil means the CLOSED time stamp will contain date and time.
  2285. When nil, only the date will be recorded."
  2286. :group 'org-progress
  2287. :type 'boolean)
  2288. (defcustom org-log-note-headings
  2289. '((done . "CLOSING NOTE %t")
  2290. (state . "State %-12s from %-12S %t")
  2291. (note . "Note taken on %t")
  2292. (reschedule . "Rescheduled from %S on %t")
  2293. (delschedule . "Not scheduled, was %S on %t")
  2294. (redeadline . "New deadline from %S on %t")
  2295. (deldeadline . "Removed deadline, was %S on %t")
  2296. (refile . "Refiled on %t")
  2297. (clock-out . ""))
  2298. "Headings for notes added to entries.
  2299. The value is an alist, with the car being a symbol indicating the note
  2300. context, and the cdr is the heading to be used. The heading may also be the
  2301. empty string.
  2302. %t in the heading will be replaced by a time stamp.
  2303. %T will be an active time stamp instead the default inactive one
  2304. %d will be replaced by a short-format time stamp.
  2305. %D will be replaced by an active short-format time stamp.
  2306. %s will be replaced by the new TODO state, in double quotes.
  2307. %S will be replaced by the old TODO state, in double quotes.
  2308. %u will be replaced by the user name.
  2309. %U will be replaced by the full user name.
  2310. In fact, it is not a good idea to change the `state' entry, because
  2311. agenda log mode depends on the format of these entries."
  2312. :group 'org-todo
  2313. :group 'org-progress
  2314. :type '(list :greedy t
  2315. (cons (const :tag "Heading when closing an item" done) string)
  2316. (cons (const :tag
  2317. "Heading when changing todo state (todo sequence only)"
  2318. state) string)
  2319. (cons (const :tag "Heading when just taking a note" note) string)
  2320. (cons (const :tag "Heading when clocking out" clock-out) string)
  2321. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2322. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2323. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2324. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2325. (cons (const :tag "Heading when refiling" refile) string)))
  2326. (unless (assq 'note org-log-note-headings)
  2327. (push '(note . "%t") org-log-note-headings))
  2328. (defcustom org-log-into-drawer nil
  2329. "Non-nil means insert state change notes and time stamps into a drawer.
  2330. When nil, state changes notes will be inserted after the headline and
  2331. any scheduling and clock lines, but not inside a drawer.
  2332. The value of this variable should be the name of the drawer to use.
  2333. LOGBOOK is proposed as the default drawer for this purpose, you can
  2334. also set this to a string to define the drawer of your choice.
  2335. A value of t is also allowed, representing \"LOGBOOK\".
  2336. A value of t or nil can also be set with on a per-file-basis with
  2337. #+STARTUP: logdrawer
  2338. #+STARTUP: nologdrawer
  2339. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2340. will be ignored.
  2341. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2342. a subtree."
  2343. :group 'org-todo
  2344. :group 'org-progress
  2345. :type '(choice
  2346. (const :tag "Not into a drawer" nil)
  2347. (const :tag "LOGBOOK" t)
  2348. (string :tag "Other")))
  2349. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2350. (defun org-log-into-drawer ()
  2351. "Return the value of `org-log-into-drawer', but let properties overrule.
  2352. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2353. used instead of the default value."
  2354. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2355. (cond
  2356. ((not p) org-log-into-drawer)
  2357. ((equal p "nil") nil)
  2358. ((equal p "t") "LOGBOOK")
  2359. (t p))))
  2360. (defcustom org-log-state-notes-insert-after-drawers nil
  2361. "Non-nil means insert state change notes after any drawers in entry.
  2362. Only the drawers that *immediately* follow the headline and the
  2363. deadline/scheduled line are skipped.
  2364. When nil, insert notes right after the heading and perhaps the line
  2365. with deadline/scheduling if present.
  2366. This variable will have no effect if `org-log-into-drawer' is
  2367. set."
  2368. :group 'org-todo
  2369. :group 'org-progress
  2370. :type 'boolean)
  2371. (defcustom org-log-states-order-reversed t
  2372. "Non-nil means the latest state note will be directly after heading.
  2373. When nil, the state change notes will be ordered according to time.
  2374. This option can also be set with on a per-file-basis with
  2375. #+STARTUP: logstatesreversed
  2376. #+STARTUP: nologstatesreversed"
  2377. :group 'org-todo
  2378. :group 'org-progress
  2379. :type 'boolean)
  2380. (defcustom org-todo-repeat-to-state nil
  2381. "The TODO state to which a repeater should return the repeating task.
  2382. By default this is the first task in a TODO sequence, or the previous state
  2383. in a TODO_TYP set. But you can specify another task here.
  2384. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2385. :group 'org-todo
  2386. :version "24.1"
  2387. :type '(choice (const :tag "Head of sequence" nil)
  2388. (string :tag "Specific state")))
  2389. (defcustom org-log-repeat 'time
  2390. "Non-nil means record moving through the DONE state when triggering repeat.
  2391. An auto-repeating task is immediately switched back to TODO when
  2392. marked DONE. If you are not logging state changes (by adding \"@\"
  2393. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2394. record a closing note, there will be no record of the task moving
  2395. through DONE. This variable forces taking a note anyway.
  2396. nil Don't force a record
  2397. time Record a time stamp
  2398. note Prompt for a note and add it with template `org-log-note-headings'
  2399. This option can also be set with on a per-file-basis with
  2400. #+STARTUP: nologrepeat
  2401. #+STARTUP: logrepeat
  2402. #+STARTUP: lognoterepeat
  2403. You can have local logging settings for a subtree by setting the LOGGING
  2404. property to one or more of these keywords."
  2405. :group 'org-todo
  2406. :group 'org-progress
  2407. :type '(choice
  2408. (const :tag "Don't force a record" nil)
  2409. (const :tag "Force recording the DONE state" time)
  2410. (const :tag "Force recording a note with the DONE state" note)))
  2411. (defgroup org-priorities nil
  2412. "Priorities in Org-mode."
  2413. :tag "Org Priorities"
  2414. :group 'org-todo)
  2415. (defcustom org-enable-priority-commands t
  2416. "Non-nil means priority commands are active.
  2417. When nil, these commands will be disabled, so that you never accidentally
  2418. set a priority."
  2419. :group 'org-priorities
  2420. :type 'boolean)
  2421. (defcustom org-highest-priority ?A
  2422. "The highest priority of TODO items. A character like ?A, ?B etc.
  2423. Must have a smaller ASCII number than `org-lowest-priority'."
  2424. :group 'org-priorities
  2425. :type 'character)
  2426. (defcustom org-lowest-priority ?C
  2427. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2428. Must have a larger ASCII number than `org-highest-priority'."
  2429. :group 'org-priorities
  2430. :type 'character)
  2431. (defcustom org-default-priority ?B
  2432. "The default priority of TODO items.
  2433. This is the priority an item gets if no explicit priority is given.
  2434. When starting to cycle on an empty priority the first step in the cycle
  2435. depends on `org-priority-start-cycle-with-default'. The resulting first
  2436. step priority must not exceed the range from `org-highest-priority' to
  2437. `org-lowest-priority' which means that `org-default-priority' has to be
  2438. in this range exclusive or inclusive the range boundaries. Else the
  2439. first step refuses to set the default and the second will fall back
  2440. to (depending on the command used) the highest or lowest priority."
  2441. :group 'org-priorities
  2442. :type 'character)
  2443. (defcustom org-priority-start-cycle-with-default t
  2444. "Non-nil means start with default priority when starting to cycle.
  2445. When this is nil, the first step in the cycle will be (depending on the
  2446. command used) one higher or lower than the default priority.
  2447. See also `org-default-priority'."
  2448. :group 'org-priorities
  2449. :type 'boolean)
  2450. (defcustom org-get-priority-function nil
  2451. "Function to extract the priority from a string.
  2452. The string is normally the headline. If this is nil Org computes the
  2453. priority from the priority cookie like [#A] in the headline. It returns
  2454. an integer, increasing by 1000 for each priority level.
  2455. The user can set a different function here, which should take a string
  2456. as an argument and return the numeric priority."
  2457. :group 'org-priorities
  2458. :version "24.1"
  2459. :type 'function)
  2460. (defgroup org-time nil
  2461. "Options concerning time stamps and deadlines in Org-mode."
  2462. :tag "Org Time"
  2463. :group 'org)
  2464. (defcustom org-insert-labeled-timestamps-at-point nil
  2465. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2466. When nil, these labeled time stamps are forces into the second line of an
  2467. entry, just after the headline. When scheduling from the global TODO list,
  2468. the time stamp will always be forced into the second line."
  2469. :group 'org-time
  2470. :type 'boolean)
  2471. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2472. "Formats for `format-time-string' which are used for time stamps.
  2473. It is not recommended to change this constant.")
  2474. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2475. "Number of minutes to round time stamps to.
  2476. These are two values, the first applies when first creating a time stamp.
  2477. The second applies when changing it with the commands `S-up' and `S-down'.
  2478. When changing the time stamp, this means that it will change in steps
  2479. of N minutes, as given by the second value.
  2480. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2481. numbers should be factors of 60, so for example 5, 10, 15.
  2482. When this is larger than 1, you can still force an exact time stamp by using
  2483. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2484. and by using a prefix arg to `S-up/down' to specify the exact number
  2485. of minutes to shift."
  2486. :group 'org-time
  2487. :get #'(lambda (var) ; Make sure both elements are there
  2488. (if (integerp (default-value var))
  2489. (list (default-value var) 5)
  2490. (default-value var)))
  2491. :type '(list
  2492. (integer :tag "when inserting times")
  2493. (integer :tag "when modifying times")))
  2494. ;; Normalize old customizations of this variable.
  2495. (when (integerp org-time-stamp-rounding-minutes)
  2496. (setq org-time-stamp-rounding-minutes
  2497. (list org-time-stamp-rounding-minutes
  2498. org-time-stamp-rounding-minutes)))
  2499. (defcustom org-display-custom-times nil
  2500. "Non-nil means overlay custom formats over all time stamps.
  2501. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2502. To turn this on on a per-file basis, insert anywhere in the file:
  2503. #+STARTUP: customtime"
  2504. :group 'org-time
  2505. :set 'set-default
  2506. :type 'sexp)
  2507. (make-variable-buffer-local 'org-display-custom-times)
  2508. (defcustom org-time-stamp-custom-formats
  2509. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2510. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2511. These are overlaid over the default ISO format if the variable
  2512. `org-display-custom-times' is set. Time like %H:%M should be at the
  2513. end of the second format. The custom formats are also honored by export
  2514. commands, if custom time display is turned on at the time of export."
  2515. :group 'org-time
  2516. :type 'sexp)
  2517. (defun org-time-stamp-format (&optional long inactive)
  2518. "Get the right format for a time string."
  2519. (let ((f (if long (cdr org-time-stamp-formats)
  2520. (car org-time-stamp-formats))))
  2521. (if inactive
  2522. (concat "[" (substring f 1 -1) "]")
  2523. f)))
  2524. (defcustom org-time-clocksum-format
  2525. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2526. "The format string used when creating CLOCKSUM lines.
  2527. This is also used when Org mode generates a time duration.
  2528. The value can be a single format string containing two
  2529. %-sequences, which will be filled with the number of hours and
  2530. minutes in that order.
  2531. Alternatively, the value can be a plist associating any of the
  2532. keys :years, :months, :weeks, :days, :hours or :minutes with
  2533. format strings. The time duration is formatted using only the
  2534. time components that are needed and concatenating the results.
  2535. If a time unit in absent, it falls back to the next smallest
  2536. unit.
  2537. The keys :require-years, :require-months, :require-days,
  2538. :require-weeks, :require-hours, :require-minutes are also
  2539. meaningful. A non-nil value for these keys indicates that the
  2540. corresponding time component should always be included, even if
  2541. its value is 0.
  2542. For example,
  2543. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2544. :require-minutes t)
  2545. means durations longer than a day will be expressed in days,
  2546. hours and minutes, and durations less than a day will always be
  2547. expressed in hours and minutes (even for durations less than an
  2548. hour).
  2549. The value
  2550. \(:days \"%dd\" :minutes \"%dm\")
  2551. means durations longer than a day will be expressed in days and
  2552. minutes, and durations less than a day will be expressed entirely
  2553. in minutes (even for durations longer than an hour)."
  2554. :group 'org-time
  2555. :group 'org-clock
  2556. :version "24.4"
  2557. :package-version '(Org . "8.0")
  2558. :type '(choice (string :tag "Format string")
  2559. (set :tag "Plist"
  2560. (group :inline t (const :tag "Years" :years)
  2561. (string :tag "Format string"))
  2562. (group :inline t
  2563. (const :tag "Always show years" :require-years)
  2564. (const t))
  2565. (group :inline t (const :tag "Months" :months)
  2566. (string :tag "Format string"))
  2567. (group :inline t
  2568. (const :tag "Always show months" :require-months)
  2569. (const t))
  2570. (group :inline t (const :tag "Weeks" :weeks)
  2571. (string :tag "Format string"))
  2572. (group :inline t
  2573. (const :tag "Always show weeks" :require-weeks)
  2574. (const t))
  2575. (group :inline t (const :tag "Days" :days)
  2576. (string :tag "Format string"))
  2577. (group :inline t
  2578. (const :tag "Always show days" :require-days)
  2579. (const t))
  2580. (group :inline t (const :tag "Hours" :hours)
  2581. (string :tag "Format string"))
  2582. (group :inline t
  2583. (const :tag "Always show hours" :require-hours)
  2584. (const t))
  2585. (group :inline t (const :tag "Minutes" :minutes)
  2586. (string :tag "Format string"))
  2587. (group :inline t
  2588. (const :tag "Always show minutes" :require-minutes)
  2589. (const t)))))
  2590. (defcustom org-time-clocksum-use-fractional nil
  2591. "When non-nil, \\[org-clock-display] uses fractional times.
  2592. See `org-time-clocksum-format' for more on time clock formats."
  2593. :group 'org-time
  2594. :group 'org-clock
  2595. :version "24.3"
  2596. :type 'boolean)
  2597. (defcustom org-time-clocksum-use-effort-durations nil
  2598. "When non-nil, \\[org-clock-display] uses effort durations.
  2599. E.g. by default, one day is considered to be a 8 hours effort,
  2600. so a task that has been clocked for 16 hours will be displayed
  2601. as during 2 days in the clock display or in the clocktable.
  2602. See `org-effort-durations' on how to set effort durations
  2603. and `org-time-clocksum-format' for more on time clock formats."
  2604. :group 'org-time
  2605. :group 'org-clock
  2606. :version "24.4"
  2607. :package-version '(Org . "8.0")
  2608. :type 'boolean)
  2609. (defcustom org-time-clocksum-fractional-format "%.2f"
  2610. "The format string used when creating CLOCKSUM lines,
  2611. or when Org mode generates a time duration, if
  2612. `org-time-clocksum-use-fractional' is enabled.
  2613. The value can be a single format string containing one
  2614. %-sequence, which will be filled with the number of hours as
  2615. a float.
  2616. Alternatively, the value can be a plist associating any of the
  2617. keys :years, :months, :weeks, :days, :hours or :minutes with
  2618. a format string. The time duration is formatted using the
  2619. largest time unit which gives a non-zero integer part. If all
  2620. specified formats have zero integer part, the smallest time unit
  2621. is used."
  2622. :group 'org-time
  2623. :type '(choice (string :tag "Format string")
  2624. (set (group :inline t (const :tag "Years" :years)
  2625. (string :tag "Format string"))
  2626. (group :inline t (const :tag "Months" :months)
  2627. (string :tag "Format string"))
  2628. (group :inline t (const :tag "Weeks" :weeks)
  2629. (string :tag "Format string"))
  2630. (group :inline t (const :tag "Days" :days)
  2631. (string :tag "Format string"))
  2632. (group :inline t (const :tag "Hours" :hours)
  2633. (string :tag "Format string"))
  2634. (group :inline t (const :tag "Minutes" :minutes)
  2635. (string :tag "Format string")))))
  2636. (defcustom org-deadline-warning-days 14
  2637. "Number of days before expiration during which a deadline becomes active.
  2638. This variable governs the display in sparse trees and in the agenda.
  2639. When 0 or negative, it means use this number (the absolute value of it)
  2640. even if a deadline has a different individual lead time specified.
  2641. Custom commands can set this variable in the options section."
  2642. :group 'org-time
  2643. :group 'org-agenda-daily/weekly
  2644. :type 'integer)
  2645. (defcustom org-scheduled-delay-days 0
  2646. "Number of days before a scheduled item becomes active.
  2647. This variable governs the display in sparse trees and in the agenda.
  2648. The default value (i.e. 0) means: don't delay scheduled item.
  2649. When negative, it means use this number (the absolute value of it)
  2650. even if a scheduled item has a different individual delay time
  2651. specified.
  2652. Custom commands can set this variable in the options section."
  2653. :group 'org-time
  2654. :group 'org-agenda-daily/weekly
  2655. :version "24.4"
  2656. :package-version '(Org . "8.0")
  2657. :type 'integer)
  2658. (defcustom org-read-date-prefer-future t
  2659. "Non-nil means assume future for incomplete date input from user.
  2660. This affects the following situations:
  2661. 1. The user gives a month but not a year.
  2662. For example, if it is April and you enter \"feb 2\", this will be read
  2663. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2664. 2. The user gives a day, but no month.
  2665. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2666. read this as the third of *next* month. However, if you enter \"17\",
  2667. it will be considered as *this* month.
  2668. If you set this variable to the symbol `time', then also the following
  2669. will work:
  2670. 3. If the user gives a time.
  2671. If the time is before now, it will be interpreted as tomorrow.
  2672. Currently none of this works for ISO week specifications.
  2673. When this option is nil, the current day, month and year will always be
  2674. used as defaults.
  2675. See also `org-agenda-jump-prefer-future'."
  2676. :group 'org-time
  2677. :type '(choice
  2678. (const :tag "Never" nil)
  2679. (const :tag "Check month and day" t)
  2680. (const :tag "Check month, day, and time" time)))
  2681. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2682. "Should the agenda jump command prefer the future for incomplete dates?
  2683. The default is to do the same as configured in `org-read-date-prefer-future'.
  2684. But you can also set a deviating value here.
  2685. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2686. :group 'org-agenda
  2687. :group 'org-time
  2688. :version "24.1"
  2689. :type '(choice
  2690. (const :tag "Use org-read-date-prefer-future"
  2691. org-read-date-prefer-future)
  2692. (const :tag "Never" nil)
  2693. (const :tag "Always" t)))
  2694. (defcustom org-read-date-force-compatible-dates t
  2695. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2696. Depending on the system Emacs is running on, certain dates cannot
  2697. be represented with the type used internally to represent time.
  2698. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2699. correctly. Some systems allow for earlier dates, some for later,
  2700. some for both. One way to find out it to insert any date into an
  2701. Org buffer, putting the cursor on the year and hitting S-up and
  2702. S-down to test the range.
  2703. When this variable is set to t, the date/time prompt will not let
  2704. you specify dates outside the 1970-2037 range, so it is certain that
  2705. these dates will work in whatever version of Emacs you are
  2706. running, and also that you can move a file from one Emacs implementation
  2707. to another. WHenever Org is forcing the year for you, it will display
  2708. a message and beep.
  2709. When this variable is nil, Org will check if the date is
  2710. representable in the specific Emacs implementation you are using.
  2711. If not, it will force a year, usually the current year, and beep
  2712. to remind you. Currently this setting is not recommended because
  2713. the likelihood that you will open your Org files in an Emacs that
  2714. has limited date range is not negligible.
  2715. A workaround for this problem is to use diary sexp dates for time
  2716. stamps outside of this range."
  2717. :group 'org-time
  2718. :version "24.1"
  2719. :type 'boolean)
  2720. (defcustom org-read-date-display-live t
  2721. "Non-nil means display current interpretation of date prompt live.
  2722. This display will be in an overlay, in the minibuffer."
  2723. :group 'org-time
  2724. :type 'boolean)
  2725. (defcustom org-read-date-popup-calendar t
  2726. "Non-nil means pop up a calendar when prompting for a date.
  2727. In the calendar, the date can be selected with mouse-1. However, the
  2728. minibuffer will also be active, and you can simply enter the date as well.
  2729. When nil, only the minibuffer will be available."
  2730. :group 'org-time
  2731. :type 'boolean)
  2732. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2733. 'org-read-date-popup-calendar)
  2734. (make-obsolete-variable
  2735. 'org-read-date-minibuffer-setup-hook
  2736. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2737. (defcustom org-read-date-minibuffer-setup-hook nil
  2738. "Hook to be used to set up keys for the date/time interface.
  2739. Add key definitions to `minibuffer-local-map', which will be a
  2740. temporary copy.
  2741. WARNING: This option is obsolete, you should use
  2742. `org-read-date-minibuffer-local-map' to set up keys."
  2743. :group 'org-time
  2744. :type 'hook)
  2745. (defcustom org-extend-today-until 0
  2746. "The hour when your day really ends. Must be an integer.
  2747. This has influence for the following applications:
  2748. - When switching the agenda to \"today\". It it is still earlier than
  2749. the time given here, the day recognized as TODAY is actually yesterday.
  2750. - When a date is read from the user and it is still before the time given
  2751. here, the current date and time will be assumed to be yesterday, 23:59.
  2752. Also, timestamps inserted in capture templates follow this rule.
  2753. IMPORTANT: This is a feature whose implementation is and likely will
  2754. remain incomplete. Really, it is only here because past midnight seems to
  2755. be the favorite working time of John Wiegley :-)"
  2756. :group 'org-time
  2757. :type 'integer)
  2758. (defcustom org-use-effective-time nil
  2759. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2760. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2761. \"effective time\" of any timestamps between midnight and 8am will be
  2762. 23:59 of the previous day."
  2763. :group 'org-time
  2764. :version "24.1"
  2765. :type 'boolean)
  2766. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2767. "When non-nil, use the last clock out time for `org-todo'.
  2768. Note that this option has precedence over the combined use of
  2769. `org-use-effective-time' and `org-extend-today-until'."
  2770. :group 'org-time
  2771. :version "24.4"
  2772. :package-version '(Org . "8.0")
  2773. :type 'boolean)
  2774. (defcustom org-edit-timestamp-down-means-later nil
  2775. "Non-nil means S-down will increase the time in a time stamp.
  2776. When nil, S-up will increase."
  2777. :group 'org-time
  2778. :type 'boolean)
  2779. (defcustom org-calendar-follow-timestamp-change t
  2780. "Non-nil means make the calendar window follow timestamp changes.
  2781. When a timestamp is modified and the calendar window is visible, it will be
  2782. moved to the new date."
  2783. :group 'org-time
  2784. :type 'boolean)
  2785. (defgroup org-tags nil
  2786. "Options concerning tags in Org-mode."
  2787. :tag "Org Tags"
  2788. :group 'org)
  2789. (defcustom org-tag-alist nil
  2790. "List of tags allowed in Org-mode files.
  2791. When this list is nil, Org-mode will base TAG input on what is already in the
  2792. buffer.
  2793. The value of this variable is an alist, the car of each entry must be a
  2794. keyword as a string, the cdr may be a character that is used to select
  2795. that tag through the fast-tag-selection interface.
  2796. See the manual for details."
  2797. :group 'org-tags
  2798. :type '(repeat
  2799. (choice
  2800. (cons (string :tag "Tag name")
  2801. (character :tag "Access char"))
  2802. (list :tag "Start radio group"
  2803. (const :startgroup)
  2804. (option (string :tag "Group description")))
  2805. (list :tag "Group tags delimiter"
  2806. (const :grouptags))
  2807. (list :tag "End radio group"
  2808. (const :endgroup)
  2809. (option (string :tag "Group description")))
  2810. (const :tag "New line" (:newline)))))
  2811. (defcustom org-tag-persistent-alist nil
  2812. "List of tags that will always appear in all Org-mode files.
  2813. This is in addition to any in buffer settings or customizations
  2814. of `org-tag-alist'.
  2815. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2816. The value of this variable is an alist, the car of each entry must be a
  2817. keyword as a string, the cdr may be a character that is used to select
  2818. that tag through the fast-tag-selection interface.
  2819. See the manual for details.
  2820. To disable these tags on a per-file basis, insert anywhere in the file:
  2821. #+STARTUP: noptag"
  2822. :group 'org-tags
  2823. :type '(repeat
  2824. (choice
  2825. (cons (string :tag "Tag name")
  2826. (character :tag "Access char"))
  2827. (const :tag "Start radio group" (:startgroup))
  2828. (const :tag "Group tags delimiter" (:grouptags))
  2829. (const :tag "End radio group" (:endgroup))
  2830. (const :tag "New line" (:newline)))))
  2831. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2832. "If non-nil, always offer completion for all tags of all agenda files.
  2833. Instead of customizing this variable directly, you might want to
  2834. set it locally for capture buffers, because there no list of
  2835. tags in that file can be created dynamically (there are none).
  2836. (add-hook 'org-capture-mode-hook
  2837. (lambda ()
  2838. (set (make-local-variable
  2839. 'org-complete-tags-always-offer-all-agenda-tags)
  2840. t)))"
  2841. :group 'org-tags
  2842. :version "24.1"
  2843. :type 'boolean)
  2844. (defvar org-file-tags nil
  2845. "List of tags that can be inherited by all entries in the file.
  2846. The tags will be inherited if the variable `org-use-tag-inheritance'
  2847. says they should be.
  2848. This variable is populated from #+FILETAGS lines.")
  2849. (defcustom org-use-fast-tag-selection 'auto
  2850. "Non-nil means use fast tag selection scheme.
  2851. This is a special interface to select and deselect tags with single keys.
  2852. When nil, fast selection is never used.
  2853. When the symbol `auto', fast selection is used if and only if selection
  2854. characters for tags have been configured, either through the variable
  2855. `org-tag-alist' or through a #+TAGS line in the buffer.
  2856. When t, fast selection is always used and selection keys are assigned
  2857. automatically if necessary."
  2858. :group 'org-tags
  2859. :type '(choice
  2860. (const :tag "Always" t)
  2861. (const :tag "Never" nil)
  2862. (const :tag "When selection characters are configured" 'auto)))
  2863. (defcustom org-fast-tag-selection-single-key nil
  2864. "Non-nil means fast tag selection exits after first change.
  2865. When nil, you have to press RET to exit it.
  2866. During fast tag selection, you can toggle this flag with `C-c'.
  2867. This variable can also have the value `expert'. In this case, the window
  2868. displaying the tags menu is not even shown, until you press C-c again."
  2869. :group 'org-tags
  2870. :type '(choice
  2871. (const :tag "No" nil)
  2872. (const :tag "Yes" t)
  2873. (const :tag "Expert" expert)))
  2874. (defvar org-fast-tag-selection-include-todo nil
  2875. "Non-nil means fast tags selection interface will also offer TODO states.
  2876. This is an undocumented feature, you should not rely on it.")
  2877. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2878. "The column to which tags should be indented in a headline.
  2879. If this number is positive, it specifies the column. If it is negative,
  2880. it means that the tags should be flushright to that column. For example,
  2881. -80 works well for a normal 80 character screen.
  2882. When 0, place tags directly after headline text, with only one space in
  2883. between."
  2884. :group 'org-tags
  2885. :type 'integer)
  2886. (defcustom org-auto-align-tags t
  2887. "Non-nil keeps tags aligned when modifying headlines.
  2888. Some operations (i.e. demoting) change the length of a headline and
  2889. therefore shift the tags around. With this option turned on, after
  2890. each such operation the tags are again aligned to `org-tags-column'."
  2891. :group 'org-tags
  2892. :type 'boolean)
  2893. (defcustom org-use-tag-inheritance t
  2894. "Non-nil means tags in levels apply also for sublevels.
  2895. When nil, only the tags directly given in a specific line apply there.
  2896. This may also be a list of tags that should be inherited, or a regexp that
  2897. matches tags that should be inherited. Additional control is possible
  2898. with the variable `org-tags-exclude-from-inheritance' which gives an
  2899. explicit list of tags to be excluded from inheritance, even if the value of
  2900. `org-use-tag-inheritance' would select it for inheritance.
  2901. If this option is t, a match early-on in a tree can lead to a large
  2902. number of matches in the subtree when constructing the agenda or creating
  2903. a sparse tree. If you only want to see the first match in a tree during
  2904. a search, check out the variable `org-tags-match-list-sublevels'."
  2905. :group 'org-tags
  2906. :type '(choice
  2907. (const :tag "Not" nil)
  2908. (const :tag "Always" t)
  2909. (repeat :tag "Specific tags" (string :tag "Tag"))
  2910. (regexp :tag "Tags matched by regexp")))
  2911. (defcustom org-tags-exclude-from-inheritance nil
  2912. "List of tags that should never be inherited.
  2913. This is a way to exclude a few tags from inheritance. For way to do
  2914. the opposite, to actively allow inheritance for selected tags,
  2915. see the variable `org-use-tag-inheritance'."
  2916. :group 'org-tags
  2917. :type '(repeat (string :tag "Tag")))
  2918. (defun org-tag-inherit-p (tag)
  2919. "Check if TAG is one that should be inherited."
  2920. (cond
  2921. ((member tag org-tags-exclude-from-inheritance) nil)
  2922. ((eq org-use-tag-inheritance t) t)
  2923. ((not org-use-tag-inheritance) nil)
  2924. ((stringp org-use-tag-inheritance)
  2925. (string-match org-use-tag-inheritance tag))
  2926. ((listp org-use-tag-inheritance)
  2927. (member tag org-use-tag-inheritance))
  2928. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2929. (defcustom org-tags-match-list-sublevels t
  2930. "Non-nil means list also sublevels of headlines matching a search.
  2931. This variable applies to tags/property searches, and also to stuck
  2932. projects because this search is based on a tags match as well.
  2933. When set to the symbol `indented', sublevels are indented with
  2934. leading dots.
  2935. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2936. the sublevels of a headline matching a tag search often also match
  2937. the same search. Listing all of them can create very long lists.
  2938. Setting this variable to nil causes subtrees of a match to be skipped.
  2939. This variable is semi-obsolete and probably should always be true. It
  2940. is better to limit inheritance to certain tags using the variables
  2941. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2942. :group 'org-tags
  2943. :type '(choice
  2944. (const :tag "No, don't list them" nil)
  2945. (const :tag "Yes, do list them" t)
  2946. (const :tag "List them, indented with leading dots" indented)))
  2947. (defcustom org-tags-sort-function nil
  2948. "When set, tags are sorted using this function as a comparator."
  2949. :group 'org-tags
  2950. :type '(choice
  2951. (const :tag "No sorting" nil)
  2952. (const :tag "Alphabetical" string<)
  2953. (const :tag "Reverse alphabetical" string>)
  2954. (function :tag "Custom function" nil)))
  2955. (defvar org-tags-history nil
  2956. "History of minibuffer reads for tags.")
  2957. (defvar org-last-tags-completion-table nil
  2958. "The last used completion table for tags.")
  2959. (defvar org-after-tags-change-hook nil
  2960. "Hook that is run after the tags in a line have changed.")
  2961. (defgroup org-properties nil
  2962. "Options concerning properties in Org-mode."
  2963. :tag "Org Properties"
  2964. :group 'org)
  2965. (defcustom org-property-format "%-10s %s"
  2966. "How property key/value pairs should be formatted by `indent-line'.
  2967. When `indent-line' hits a property definition, it will format the line
  2968. according to this format, mainly to make sure that the values are
  2969. lined-up with respect to each other."
  2970. :group 'org-properties
  2971. :type 'string)
  2972. (defcustom org-properties-postprocess-alist nil
  2973. "Alist of properties and functions to adjust inserted values.
  2974. Elements of this alist must be of the form
  2975. ([string] [function])
  2976. where [string] must be a property name and [function] must be a
  2977. lambda expression: this lambda expression must take one argument,
  2978. the value to adjust, and return the new value as a string.
  2979. For example, this element will allow the property \"Remaining\"
  2980. to be updated wrt the relation between the \"Effort\" property
  2981. and the clock summary:
  2982. ((\"Remaining\" (lambda(value)
  2983. (let ((clocksum (org-clock-sum-current-item))
  2984. (effort (org-duration-string-to-minutes
  2985. (org-entry-get (point) \"Effort\"))))
  2986. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2987. :group 'org-properties
  2988. :version "24.1"
  2989. :type '(alist :key-type (string :tag "Property")
  2990. :value-type (function :tag "Function")))
  2991. (defcustom org-use-property-inheritance nil
  2992. "Non-nil means properties apply also for sublevels.
  2993. This setting is chiefly used during property searches. Turning it on can
  2994. cause significant overhead when doing a search, which is why it is not
  2995. on by default.
  2996. When nil, only the properties directly given in the current entry count.
  2997. When t, every property is inherited. The value may also be a list of
  2998. properties that should have inheritance, or a regular expression matching
  2999. properties that should be inherited.
  3000. However, note that some special properties use inheritance under special
  3001. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3002. and the properties ending in \"_ALL\" when they are used as descriptor
  3003. for valid values of a property.
  3004. Note for programmers:
  3005. When querying an entry with `org-entry-get', you can control if inheritance
  3006. should be used. By default, `org-entry-get' looks only at the local
  3007. properties. You can request inheritance by setting the inherit argument
  3008. to t (to force inheritance) or to `selective' (to respect the setting
  3009. in this variable)."
  3010. :group 'org-properties
  3011. :type '(choice
  3012. (const :tag "Not" nil)
  3013. (const :tag "Always" t)
  3014. (repeat :tag "Specific properties" (string :tag "Property"))
  3015. (regexp :tag "Properties matched by regexp")))
  3016. (defun org-property-inherit-p (property)
  3017. "Check if PROPERTY is one that should be inherited."
  3018. (cond
  3019. ((eq org-use-property-inheritance t) t)
  3020. ((not org-use-property-inheritance) nil)
  3021. ((stringp org-use-property-inheritance)
  3022. (string-match org-use-property-inheritance property))
  3023. ((listp org-use-property-inheritance)
  3024. (member property org-use-property-inheritance))
  3025. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3026. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3027. "The default column format, if no other format has been defined.
  3028. This variable can be set on the per-file basis by inserting a line
  3029. #+COLUMNS: %25ITEM ....."
  3030. :group 'org-properties
  3031. :type 'string)
  3032. (defcustom org-columns-ellipses ".."
  3033. "The ellipses to be used when a field in column view is truncated.
  3034. When this is the empty string, as many characters as possible are shown,
  3035. but then there will be no visual indication that the field has been truncated.
  3036. When this is a string of length N, the last N characters of a truncated
  3037. field are replaced by this string. If the column is narrower than the
  3038. ellipses string, only part of the ellipses string will be shown."
  3039. :group 'org-properties
  3040. :type 'string)
  3041. (defcustom org-columns-modify-value-for-display-function nil
  3042. "Function that modifies values for display in column view.
  3043. For example, it can be used to cut out a certain part from a time stamp.
  3044. The function must take 2 arguments:
  3045. column-title The title of the column (*not* the property name)
  3046. value The value that should be modified.
  3047. The function should return the value that should be displayed,
  3048. or nil if the normal value should be used."
  3049. :group 'org-properties
  3050. :type 'function)
  3051. (defcustom org-effort-property "Effort"
  3052. "The property that is being used to keep track of effort estimates.
  3053. Effort estimates given in this property need to have the format H:MM."
  3054. :group 'org-properties
  3055. :group 'org-progress
  3056. :type '(string :tag "Property"))
  3057. (defconst org-global-properties-fixed
  3058. '(("VISIBILITY_ALL" . "folded children content all")
  3059. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3060. "List of property/value pairs that can be inherited by any entry.
  3061. These are fixed values, for the preset properties. The user variable
  3062. that can be used to add to this list is `org-global-properties'.
  3063. The entries in this list are cons cells where the car is a property
  3064. name and cdr is a string with the value. If the value represents
  3065. multiple items like an \"_ALL\" property, separate the items by
  3066. spaces.")
  3067. (defcustom org-global-properties nil
  3068. "List of property/value pairs that can be inherited by any entry.
  3069. This list will be combined with the constant `org-global-properties-fixed'.
  3070. The entries in this list are cons cells where the car is a property
  3071. name and cdr is a string with the value.
  3072. You can set buffer-local values for the same purpose in the variable
  3073. `org-file-properties' this by adding lines like
  3074. #+PROPERTY: NAME VALUE"
  3075. :group 'org-properties
  3076. :type '(repeat
  3077. (cons (string :tag "Property")
  3078. (string :tag "Value"))))
  3079. (defvar org-file-properties nil
  3080. "List of property/value pairs that can be inherited by any entry.
  3081. Valid for the current buffer.
  3082. This variable is populated from #+PROPERTY lines.")
  3083. (make-variable-buffer-local 'org-file-properties)
  3084. (defgroup org-agenda nil
  3085. "Options concerning agenda views in Org-mode."
  3086. :tag "Org Agenda"
  3087. :group 'org)
  3088. (defvar org-category nil
  3089. "Variable used by org files to set a category for agenda display.
  3090. Such files should use a file variable to set it, for example
  3091. # -*- mode: org; org-category: \"ELisp\"
  3092. or contain a special line
  3093. #+CATEGORY: ELisp
  3094. If the file does not specify a category, then file's base name
  3095. is used instead.")
  3096. (make-variable-buffer-local 'org-category)
  3097. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3098. (defcustom org-agenda-files nil
  3099. "The files to be used for agenda display.
  3100. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3101. \\[org-remove-file]. You can also use customize to edit the list.
  3102. If an entry is a directory, all files in that directory that are matched by
  3103. `org-agenda-file-regexp' will be part of the file list.
  3104. If the value of the variable is not a list but a single file name, then
  3105. the list of agenda files is actually stored and maintained in that file, one
  3106. agenda file per line. In this file paths can be given relative to
  3107. `org-directory'. Tilde expansion and environment variable substitution
  3108. are also made."
  3109. :group 'org-agenda
  3110. :type '(choice
  3111. (repeat :tag "List of files and directories" file)
  3112. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3113. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3114. "Regular expression to match files for `org-agenda-files'.
  3115. If any element in the list in that variable contains a directory instead
  3116. of a normal file, all files in that directory that are matched by this
  3117. regular expression will be included."
  3118. :group 'org-agenda
  3119. :type 'regexp)
  3120. (defcustom org-agenda-text-search-extra-files nil
  3121. "List of extra files to be searched by text search commands.
  3122. These files will be search in addition to the agenda files by the
  3123. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3124. Note that these files will only be searched for text search commands,
  3125. not for the other agenda views like todo lists, tag searches or the weekly
  3126. agenda. This variable is intended to list notes and possibly archive files
  3127. that should also be searched by these two commands.
  3128. In fact, if the first element in the list is the symbol `agenda-archives',
  3129. than all archive files of all agenda files will be added to the search
  3130. scope."
  3131. :group 'org-agenda
  3132. :type '(set :greedy t
  3133. (const :tag "Agenda Archives" agenda-archives)
  3134. (repeat :inline t (file))))
  3135. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3136. 'org-agenda-text-search-extra-files)
  3137. (defcustom org-agenda-skip-unavailable-files nil
  3138. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3139. A nil value means to remove them, after a query, from the list."
  3140. :group 'org-agenda
  3141. :type 'boolean)
  3142. (defcustom org-calendar-to-agenda-key [?c]
  3143. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3144. The command `org-calendar-goto-agenda' will be bound to this key. The
  3145. default is the character `c' because then `c' can be used to switch back and
  3146. forth between agenda and calendar."
  3147. :group 'org-agenda
  3148. :type 'sexp)
  3149. (defcustom org-calendar-insert-diary-entry-key [?i]
  3150. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3151. This option is irrelevant until `org-agenda-diary-file' has been configured
  3152. to point to an Org-mode file. When that is the case, the command
  3153. `org-agenda-diary-entry' will be bound to the key given here, by default
  3154. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3155. if you want to continue doing this, you need to change this to a different
  3156. key."
  3157. :group 'org-agenda
  3158. :type 'sexp)
  3159. (defcustom org-agenda-diary-file 'diary-file
  3160. "File to which to add new entries with the `i' key in agenda and calendar.
  3161. When this is the symbol `diary-file', the functionality in the Emacs
  3162. calendar will be used to add entries to the `diary-file'. But when this
  3163. points to a file, `org-agenda-diary-entry' will be used instead."
  3164. :group 'org-agenda
  3165. :type '(choice
  3166. (const :tag "The standard Emacs diary file" diary-file)
  3167. (file :tag "Special Org file diary entries")))
  3168. (eval-after-load "calendar"
  3169. '(progn
  3170. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3171. 'org-calendar-goto-agenda)
  3172. (add-hook 'calendar-mode-hook
  3173. (lambda ()
  3174. (unless (eq org-agenda-diary-file 'diary-file)
  3175. (define-key calendar-mode-map
  3176. org-calendar-insert-diary-entry-key
  3177. 'org-agenda-diary-entry))))))
  3178. (defgroup org-latex nil
  3179. "Options for embedding LaTeX code into Org-mode."
  3180. :tag "Org LaTeX"
  3181. :group 'org)
  3182. (defcustom org-format-latex-options
  3183. '(:foreground default :background default :scale 1.0
  3184. :html-foreground "Black" :html-background "Transparent"
  3185. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3186. "Options for creating images from LaTeX fragments.
  3187. This is a property list with the following properties:
  3188. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3189. `default' means use the foreground of the default face.
  3190. `auto' means use the foreground from the text face.
  3191. :background the background color, or \"Transparent\".
  3192. `default' means use the background of the default face.
  3193. `auto' means use the background from the text face.
  3194. :scale a scaling factor for the size of the images, to get more pixels
  3195. :html-foreground, :html-background, :html-scale
  3196. the same numbers for HTML export.
  3197. :matchers a list indicating which matchers should be used to
  3198. find LaTeX fragments. Valid members of this list are:
  3199. \"begin\" find environments
  3200. \"$1\" find single characters surrounded by $.$
  3201. \"$\" find math expressions surrounded by $...$
  3202. \"$$\" find math expressions surrounded by $$....$$
  3203. \"\\(\" find math expressions surrounded by \\(...\\)
  3204. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3205. :group 'org-latex
  3206. :type 'plist)
  3207. (defcustom org-format-latex-signal-error t
  3208. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3209. When nil, just push out a message."
  3210. :group 'org-latex
  3211. :version "24.1"
  3212. :type 'boolean)
  3213. (defcustom org-latex-to-mathml-jar-file nil
  3214. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3215. Use this to specify additional executable file say a jar file.
  3216. When using MathToWeb as the converter, specify the full-path to
  3217. your mathtoweb.jar file."
  3218. :group 'org-latex
  3219. :version "24.1"
  3220. :type '(choice
  3221. (const :tag "None" nil)
  3222. (file :tag "JAR file" :must-match t)))
  3223. (defcustom org-latex-to-mathml-convert-command nil
  3224. "Command to convert LaTeX fragments to MathML.
  3225. Replace format-specifiers in the command as noted below and use
  3226. `shell-command' to convert LaTeX to MathML.
  3227. %j: Executable file in fully expanded form as specified by
  3228. `org-latex-to-mathml-jar-file'.
  3229. %I: Input LaTeX file in fully expanded form
  3230. %o: Output MathML file
  3231. This command is used by `org-create-math-formula'.
  3232. When using MathToWeb as the converter, set this to
  3233. \"java -jar %j -unicode -force -df %o %I\"."
  3234. :group 'org-latex
  3235. :version "24.1"
  3236. :type '(choice
  3237. (const :tag "None" nil)
  3238. (string :tag "\nShell command")))
  3239. (defcustom org-latex-create-formula-image-program 'dvipng
  3240. "Program to convert LaTeX fragments with.
  3241. dvipng Process the LaTeX fragments to dvi file, then convert
  3242. dvi files to png files using dvipng.
  3243. This will also include processing of non-math environments.
  3244. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3245. to convert pdf files to png files"
  3246. :group 'org-latex
  3247. :version "24.1"
  3248. :type '(choice
  3249. (const :tag "dvipng" dvipng)
  3250. (const :tag "imagemagick" imagemagick)))
  3251. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3252. "Path to store latex preview images.
  3253. A relative path here creates many directories relative to the
  3254. processed org files paths. An absolute path puts all preview
  3255. images at the same place."
  3256. :group 'org-latex
  3257. :version "24.3"
  3258. :type 'string)
  3259. (defun org-format-latex-mathml-available-p ()
  3260. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3261. (save-match-data
  3262. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3263. org-latex-to-mathml-convert-command)
  3264. (let ((executable (car (split-string
  3265. org-latex-to-mathml-convert-command))))
  3266. (when (executable-find executable)
  3267. (if (string-match
  3268. "%j" org-latex-to-mathml-convert-command)
  3269. (file-readable-p org-latex-to-mathml-jar-file)
  3270. t))))))
  3271. (defcustom org-format-latex-header "\\documentclass{article}
  3272. \\usepackage[usenames]{color}
  3273. \\usepackage{amsmath}
  3274. \\usepackage[mathscr]{eucal}
  3275. \\pagestyle{empty} % do not remove
  3276. \[PACKAGES]
  3277. \[DEFAULT-PACKAGES]
  3278. % The settings below are copied from fullpage.sty
  3279. \\setlength{\\textwidth}{\\paperwidth}
  3280. \\addtolength{\\textwidth}{-3cm}
  3281. \\setlength{\\oddsidemargin}{1.5cm}
  3282. \\addtolength{\\oddsidemargin}{-2.54cm}
  3283. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3284. \\setlength{\\textheight}{\\paperheight}
  3285. \\addtolength{\\textheight}{-\\headheight}
  3286. \\addtolength{\\textheight}{-\\headsep}
  3287. \\addtolength{\\textheight}{-\\footskip}
  3288. \\addtolength{\\textheight}{-3cm}
  3289. \\setlength{\\topmargin}{1.5cm}
  3290. \\addtolength{\\topmargin}{-2.54cm}"
  3291. "The document header used for processing LaTeX fragments.
  3292. It is imperative that this header make sure that no page number
  3293. appears on the page. The package defined in the variables
  3294. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3295. will either replace the placeholder \"[PACKAGES]\" in this
  3296. header, or they will be appended."
  3297. :group 'org-latex
  3298. :type 'string)
  3299. (defun org-set-packages-alist (var val)
  3300. "Set the packages alist and make sure it has 3 elements per entry."
  3301. (set var (mapcar (lambda (x)
  3302. (if (and (consp x) (= (length x) 2))
  3303. (list (car x) (nth 1 x) t)
  3304. x))
  3305. val)))
  3306. (defun org-get-packages-alist (var)
  3307. "Get the packages alist and make sure it has 3 elements per entry."
  3308. (mapcar (lambda (x)
  3309. (if (and (consp x) (= (length x) 2))
  3310. (list (car x) (nth 1 x) t)
  3311. x))
  3312. (default-value var)))
  3313. (defcustom org-latex-default-packages-alist
  3314. '(("AUTO" "inputenc" t)
  3315. ("T1" "fontenc" t)
  3316. ("" "fixltx2e" nil)
  3317. ("" "graphicx" t)
  3318. ("" "longtable" nil)
  3319. ("" "float" nil)
  3320. ("" "wrapfig" nil)
  3321. ("" "soul" t)
  3322. ("" "textcomp" t)
  3323. ("" "marvosym" t)
  3324. ("" "wasysym" t)
  3325. ("" "latexsym" t)
  3326. ("" "amssymb" t)
  3327. ("" "amstext" nil)
  3328. ("" "hyperref" nil)
  3329. "\\tolerance=1000")
  3330. "Alist of default packages to be inserted in the header.
  3331. Change this only if one of the packages here causes an
  3332. incompatibility with another package you are using.
  3333. The packages in this list are needed by one part or another of
  3334. Org mode to function properly:
  3335. - inputenc, fontenc: for basic font and character selection
  3336. - amstext: for subscript and superscript
  3337. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3338. symbols used for interpreting the entities in `org-entities'.
  3339. You can skip some of these packages if you don't use any of the
  3340. symbols in it.
  3341. - graphicx: for including images
  3342. - float, wrapfig: for figure placement
  3343. - longtable: for long tables
  3344. - hyperref: for cross references
  3345. Therefore you should not modify this variable unless you know
  3346. what you are doing. The one reason to change it anyway is that
  3347. you might be loading some other package that conflicts with one
  3348. of the default packages. Each cell is of the format
  3349. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3350. the package also needs to be included when compiling LaTeX
  3351. snippets into images for inclusion into non-LaTeX output."
  3352. :group 'org-latex
  3353. :group 'org-export-latex
  3354. :set 'org-set-packages-alist
  3355. :get 'org-get-packages-alist
  3356. :version "24.1"
  3357. :type '(repeat
  3358. (choice
  3359. (list :tag "options/package pair"
  3360. (string :tag "options")
  3361. (string :tag "package")
  3362. (boolean :tag "Snippet"))
  3363. (string :tag "A line of LaTeX"))))
  3364. (defcustom org-latex-packages-alist nil
  3365. "Alist of packages to be inserted in every LaTeX header.
  3366. These will be inserted after `org-latex-default-packages-alist'.
  3367. Each cell is of the format:
  3368. \(\"options\" \"package\" snippet-flag)
  3369. SNIPPET-FLAG, when t, indicates that this package is also needed
  3370. when turning LaTeX snippets into images for inclusion into
  3371. non-LaTeX output.
  3372. Make sure that you only list packages here which:
  3373. - you want in every file
  3374. - do not conflict with the setup in `org-format-latex-header'.
  3375. - do not conflict with the default packages in
  3376. `org-latex-default-packages-alist'."
  3377. :group 'org-latex
  3378. :group 'org-export-latex
  3379. :set 'org-set-packages-alist
  3380. :get 'org-get-packages-alist
  3381. :type '(repeat
  3382. (choice
  3383. (list :tag "options/package pair"
  3384. (string :tag "options")
  3385. (string :tag "package")
  3386. (boolean :tag "Snippet"))
  3387. (string :tag "A line of LaTeX"))))
  3388. (defgroup org-appearance nil
  3389. "Settings for Org-mode appearance."
  3390. :tag "Org Appearance"
  3391. :group 'org)
  3392. (defcustom org-level-color-stars-only nil
  3393. "Non-nil means fontify only the stars in each headline.
  3394. When nil, the entire headline is fontified.
  3395. Changing it requires restart of `font-lock-mode' to become effective
  3396. also in regions already fontified."
  3397. :group 'org-appearance
  3398. :type 'boolean)
  3399. (defcustom org-hide-leading-stars nil
  3400. "Non-nil means hide the first N-1 stars in a headline.
  3401. This works by using the face `org-hide' for these stars. This
  3402. face is white for a light background, and black for a dark
  3403. background. You may have to customize the face `org-hide' to
  3404. make this work.
  3405. Changing it requires restart of `font-lock-mode' to become effective
  3406. also in regions already fontified.
  3407. You may also set this on a per-file basis by adding one of the following
  3408. lines to the buffer:
  3409. #+STARTUP: hidestars
  3410. #+STARTUP: showstars"
  3411. :group 'org-appearance
  3412. :type 'boolean)
  3413. (defcustom org-hidden-keywords nil
  3414. "List of symbols corresponding to keywords to be hidden the org buffer.
  3415. For example, a value '(title) for this list will make the document's title
  3416. appear in the buffer without the initial #+TITLE: keyword."
  3417. :group 'org-appearance
  3418. :version "24.1"
  3419. :type '(set (const :tag "#+AUTHOR" author)
  3420. (const :tag "#+DATE" date)
  3421. (const :tag "#+EMAIL" email)
  3422. (const :tag "#+TITLE" title)))
  3423. (defcustom org-custom-properties nil
  3424. "List of properties (as strings) with a special meaning.
  3425. The default use of these custom properties is to let the user
  3426. hide them with `org-toggle-custom-properties-visibility'."
  3427. :group 'org-properties
  3428. :group 'org-appearance
  3429. :version "24.3"
  3430. :type '(repeat (string :tag "Property Name")))
  3431. (defcustom org-fontify-done-headline nil
  3432. "Non-nil means change the face of a headline if it is marked DONE.
  3433. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3434. When this is non-nil, the headline after the keyword is set to the
  3435. `org-headline-done' as an additional indication."
  3436. :group 'org-appearance
  3437. :type 'boolean)
  3438. (defcustom org-fontify-emphasized-text t
  3439. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3440. Changing this variable requires a restart of Emacs to take effect."
  3441. :group 'org-appearance
  3442. :type 'boolean)
  3443. (defcustom org-fontify-whole-heading-line nil
  3444. "Non-nil means fontify the whole line for headings.
  3445. This is useful when setting a background color for the
  3446. org-level-* faces."
  3447. :group 'org-appearance
  3448. :type 'boolean)
  3449. (defcustom org-highlight-latex-and-related nil
  3450. "Non-nil means highlight LaTeX related syntax in the buffer.
  3451. When non nil, the value should be a list containing any of the
  3452. following symbols:
  3453. `latex' Highlight LaTeX snippets and environments.
  3454. `script' Highlight subscript and superscript.
  3455. `entities' Highlight entities."
  3456. :group 'org-appearance
  3457. :version "24.4"
  3458. :package-version '(Org . "8.0")
  3459. :type '(choice
  3460. (const :tag "No highlighting" nil)
  3461. (set :greedy t :tag "Highlight"
  3462. (const :tag "LaTeX snippets and environments" latex)
  3463. (const :tag "Subscript and superscript" script)
  3464. (const :tag "Entities" entities))))
  3465. (defcustom org-hide-emphasis-markers nil
  3466. "Non-nil mean font-lock should hide the emphasis marker characters."
  3467. :group 'org-appearance
  3468. :type 'boolean)
  3469. (defcustom org-pretty-entities nil
  3470. "Non-nil means show entities as UTF8 characters.
  3471. When nil, the \\name form remains in the buffer."
  3472. :group 'org-appearance
  3473. :version "24.1"
  3474. :type 'boolean)
  3475. (defcustom org-pretty-entities-include-sub-superscripts t
  3476. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3477. :group 'org-appearance
  3478. :version "24.1"
  3479. :type 'boolean)
  3480. (defvar org-emph-re nil
  3481. "Regular expression for matching emphasis.
  3482. After a match, the match groups contain these elements:
  3483. 0 The match of the full regular expression, including the characters
  3484. before and after the proper match
  3485. 1 The character before the proper match, or empty at beginning of line
  3486. 2 The proper match, including the leading and trailing markers
  3487. 3 The leading marker like * or /, indicating the type of highlighting
  3488. 4 The text between the emphasis markers, not including the markers
  3489. 5 The character after the match, empty at the end of a line")
  3490. (defvar org-verbatim-re nil
  3491. "Regular expression for matching verbatim text.")
  3492. (defvar org-emphasis-regexp-components) ; defined just below
  3493. (defvar org-emphasis-alist) ; defined just below
  3494. (defun org-set-emph-re (var val)
  3495. "Set variable and compute the emphasis regular expression."
  3496. (set var val)
  3497. (when (and (boundp 'org-emphasis-alist)
  3498. (boundp 'org-emphasis-regexp-components)
  3499. org-emphasis-alist org-emphasis-regexp-components)
  3500. (let* ((e org-emphasis-regexp-components)
  3501. (pre (car e))
  3502. (post (nth 1 e))
  3503. (border (nth 2 e))
  3504. (body (nth 3 e))
  3505. (nl (nth 4 e))
  3506. (body1 (concat body "*?"))
  3507. (markers (mapconcat 'car org-emphasis-alist ""))
  3508. (vmarkers (mapconcat
  3509. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3510. org-emphasis-alist "")))
  3511. ;; make sure special characters appear at the right position in the class
  3512. (if (string-match "\\^" markers)
  3513. (setq markers (concat (replace-match "" t t markers) "^")))
  3514. (if (string-match "-" markers)
  3515. (setq markers (concat (replace-match "" t t markers) "-")))
  3516. (if (string-match "\\^" vmarkers)
  3517. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3518. (if (string-match "-" vmarkers)
  3519. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3520. (if (> nl 0)
  3521. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3522. (int-to-string nl) "\\}")))
  3523. ;; Make the regexp
  3524. (setq org-emph-re
  3525. (concat "\\([" pre "]\\|^\\)"
  3526. "\\("
  3527. "\\([" markers "]\\)"
  3528. "\\("
  3529. "[^" border "]\\|"
  3530. "[^" border "]"
  3531. body1
  3532. "[^" border "]"
  3533. "\\)"
  3534. "\\3\\)"
  3535. "\\([" post "]\\|$\\)"))
  3536. (setq org-verbatim-re
  3537. (concat "\\([" pre "]\\|^\\)"
  3538. "\\("
  3539. "\\([" vmarkers "]\\)"
  3540. "\\("
  3541. "[^" border "]\\|"
  3542. "[^" border "]"
  3543. body1
  3544. "[^" border "]"
  3545. "\\)"
  3546. "\\3\\)"
  3547. "\\([" post "]\\|$\\)")))))
  3548. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3549. ;; set this option proved cumbersome. See this message/thread:
  3550. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3551. (defvar org-emphasis-regexp-components
  3552. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3553. "Components used to build the regular expression for emphasis.
  3554. This is a list with five entries. Terminology: In an emphasis string
  3555. like \" *strong word* \", we call the initial space PREMATCH, the final
  3556. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3557. and \"trong wor\" is the body. The different components in this variable
  3558. specify what is allowed/forbidden in each part:
  3559. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3560. post Chars allowed as postmatch. End of line will be allowed too.
  3561. border The chars *forbidden* as border characters.
  3562. body-regexp A regexp like \".\" to match a body character. Don't use
  3563. non-shy groups here, and don't allow newline here.
  3564. newline The maximum number of newlines allowed in an emphasis exp.
  3565. You need to reload Org or to restart Emacs after customizing this.")
  3566. (defcustom org-emphasis-alist
  3567. `(("*" bold)
  3568. ("/" italic)
  3569. ("_" underline)
  3570. ("=" org-code verbatim)
  3571. ("~" org-verbatim verbatim)
  3572. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3573. "Alist of characters and faces to emphasize text.
  3574. Text starting and ending with a special character will be emphasized,
  3575. for example *bold*, _underlined_ and /italic/. This variable sets the
  3576. marker characters and the face to be used by font-lock for highlighting
  3577. in Org-mode Emacs buffers.
  3578. You need to reload Org or to restart Emacs after customizing this."
  3579. :group 'org-appearance
  3580. :set 'org-set-emph-re
  3581. :version "24.4"
  3582. :package-version '(Org . "8.0")
  3583. :type '(repeat
  3584. (list
  3585. (string :tag "Marker character")
  3586. (choice
  3587. (face :tag "Font-lock-face")
  3588. (plist :tag "Face property list"))
  3589. (option (const verbatim)))))
  3590. (defvar org-protecting-blocks
  3591. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3592. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3593. This is needed for font-lock setup.")
  3594. ;;; Miscellaneous options
  3595. (defgroup org-completion nil
  3596. "Completion in Org-mode."
  3597. :tag "Org Completion"
  3598. :group 'org)
  3599. (defcustom org-completion-use-ido nil
  3600. "Non-nil means use ido completion wherever possible.
  3601. Note that `ido-mode' must be active for this variable to be relevant.
  3602. If you decide to turn this variable on, you might well want to turn off
  3603. `org-outline-path-complete-in-steps'.
  3604. See also `org-completion-use-iswitchb'."
  3605. :group 'org-completion
  3606. :type 'boolean)
  3607. (defcustom org-completion-use-iswitchb nil
  3608. "Non-nil means use iswitchb completion wherever possible.
  3609. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3610. If you decide to turn this variable on, you might well want to turn off
  3611. `org-outline-path-complete-in-steps'.
  3612. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3613. :group 'org-completion
  3614. :type 'boolean)
  3615. (defcustom org-completion-fallback-command 'hippie-expand
  3616. "The expansion command called by \\[pcomplete] in normal context.
  3617. Normal means, no org-mode-specific context."
  3618. :group 'org-completion
  3619. :type 'function)
  3620. ;;; Functions and variables from their packages
  3621. ;; Declared here to avoid compiler warnings
  3622. ;; XEmacs only
  3623. (defvar outline-mode-menu-heading)
  3624. (defvar outline-mode-menu-show)
  3625. (defvar outline-mode-menu-hide)
  3626. (defvar zmacs-regions) ; XEmacs regions
  3627. ;; Emacs only
  3628. (defvar mark-active)
  3629. ;; Various packages
  3630. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3631. (declare-function calendar-forward-day "cal-move" (arg))
  3632. (declare-function calendar-goto-date "cal-move" (date))
  3633. (declare-function calendar-goto-today "cal-move" ())
  3634. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3635. (defvar calc-embedded-close-formula)
  3636. (defvar calc-embedded-open-formula)
  3637. (declare-function cdlatex-tab "ext:cdlatex" ())
  3638. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3639. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3640. (defvar font-lock-unfontify-region-function)
  3641. (declare-function iswitchb-read-buffer "iswitchb"
  3642. (prompt &optional default require-match start matches-set))
  3643. (defvar iswitchb-temp-buflist)
  3644. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3645. (defvar org-agenda-tags-todo-honor-ignore-options)
  3646. (declare-function org-agenda-skip "org-agenda" ())
  3647. (declare-function
  3648. org-agenda-format-item "org-agenda"
  3649. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3650. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3651. (declare-function org-agenda-change-all-lines "org-agenda"
  3652. (newhead hdmarker &optional fixface just-this))
  3653. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3654. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3655. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3656. (beg end))
  3657. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3658. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3659. "org-agenda" (&optional end))
  3660. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3661. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3662. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3663. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3664. (declare-function org-indent-mode "org-indent" (&optional arg))
  3665. (declare-function parse-time-string "parse-time" (string))
  3666. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3667. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3668. (defvar remember-data-file)
  3669. (defvar texmathp-why)
  3670. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3671. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3672. (defvar org-latex-regexps)
  3673. ;;; Autoload and prepare some org modules
  3674. ;; Some table stuff that needs to be defined here, because it is used
  3675. ;; by the functions setting up org-mode or checking for table context.
  3676. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3677. "Detect an org-type or table-type table.")
  3678. (defconst org-table-line-regexp "^[ \t]*|"
  3679. "Detect an org-type table line.")
  3680. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3681. "Detect an org-type table line.")
  3682. (defconst org-table-hline-regexp "^[ \t]*|-"
  3683. "Detect an org-type table hline.")
  3684. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3685. "Detect a table-type table hline.")
  3686. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3687. "Detect the first line outside a table when searching from within it.
  3688. This works for both table types.")
  3689. ;; Autoload the functions in org-table.el that are needed by functions here.
  3690. (eval-and-compile
  3691. (org-autoload "org-table"
  3692. '(org-table-begin org-table-blank-field org-table-end)))
  3693. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3694. "Detect a #+TBLFM line.")
  3695. ;;;###autoload
  3696. (defun turn-on-orgtbl ()
  3697. "Unconditionally turn on `orgtbl-mode'."
  3698. (require 'org-table)
  3699. (orgtbl-mode 1))
  3700. (defun org-at-table-p (&optional table-type)
  3701. "Return t if the cursor is inside an org-type table.
  3702. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3703. (if org-enable-table-editor
  3704. (save-excursion
  3705. (beginning-of-line 1)
  3706. (looking-at (if table-type org-table-any-line-regexp
  3707. org-table-line-regexp)))
  3708. nil))
  3709. (defsubst org-table-p () (org-at-table-p))
  3710. (defun org-at-table.el-p ()
  3711. "Return t if and only if we are at a table.el table."
  3712. (and (org-at-table-p 'any)
  3713. (save-excursion
  3714. (goto-char (org-table-begin 'any))
  3715. (looking-at org-table1-hline-regexp))))
  3716. (defun org-table-recognize-table.el ()
  3717. "If there is a table.el table nearby, recognize it and move into it."
  3718. (if org-table-tab-recognizes-table.el
  3719. (if (org-at-table.el-p)
  3720. (progn
  3721. (beginning-of-line 1)
  3722. (if (looking-at org-table-dataline-regexp)
  3723. nil
  3724. (if (looking-at org-table1-hline-regexp)
  3725. (progn
  3726. (beginning-of-line 2)
  3727. (if (looking-at org-table-any-border-regexp)
  3728. (beginning-of-line -1)))))
  3729. (if (re-search-forward "|" (org-table-end t) t)
  3730. (progn
  3731. (require 'table)
  3732. (if (table--at-cell-p (point))
  3733. t
  3734. (message "recognizing table.el table...")
  3735. (table-recognize-table)
  3736. (message "recognizing table.el table...done")))
  3737. (error "This should not happen"))
  3738. t)
  3739. nil)
  3740. nil))
  3741. (defun org-at-table-hline-p ()
  3742. "Return t if the cursor is inside a hline in a table."
  3743. (if org-enable-table-editor
  3744. (save-excursion
  3745. (beginning-of-line 1)
  3746. (looking-at org-table-hline-regexp))
  3747. nil))
  3748. (defvar org-table-clean-did-remove-column nil)
  3749. (defun org-table-map-tables (function &optional quietly)
  3750. "Apply FUNCTION to the start of all tables in the buffer."
  3751. (save-excursion
  3752. (save-restriction
  3753. (widen)
  3754. (goto-char (point-min))
  3755. (while (re-search-forward org-table-any-line-regexp nil t)
  3756. (unless quietly
  3757. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3758. (beginning-of-line 1)
  3759. (when (and (looking-at org-table-line-regexp)
  3760. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3761. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3762. (save-excursion (funcall function))
  3763. (or (looking-at org-table-line-regexp)
  3764. (forward-char 1)))
  3765. (re-search-forward org-table-any-border-regexp nil 1))))
  3766. (unless quietly (message "Mapping tables: done")))
  3767. ;; Declare and autoload functions from ox.el and al.
  3768. (declare-function org-export-get-environment "ox"
  3769. (&optional backend subtreep ext-plist))
  3770. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3771. ;; Declare and autoload functions from org-agenda.el
  3772. (eval-and-compile
  3773. (org-autoload "org-agenda"
  3774. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3775. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3776. (declare-function org-clock-update-mode-line "org-clock" ())
  3777. (declare-function org-resolve-clocks "org-clock"
  3778. (&optional also-non-dangling-p prompt last-valid))
  3779. (defun org-at-TBLFM-p (&optional pos)
  3780. "Return t when point (or POS) is in #+TBLFM line."
  3781. (save-excursion
  3782. (let ((pos pos)))
  3783. (goto-char (or pos (point)))
  3784. (beginning-of-line 1)
  3785. (looking-at org-TBLFM-regexp)))
  3786. (defvar org-clock-start-time)
  3787. (defvar org-clock-marker (make-marker)
  3788. "Marker recording the last clock-in.")
  3789. (defvar org-clock-hd-marker (make-marker)
  3790. "Marker recording the last clock-in, but the headline position.")
  3791. (defvar org-clock-heading ""
  3792. "The heading of the current clock entry.")
  3793. (defun org-clock-is-active ()
  3794. "Return non-nil if clock is currently running.
  3795. The return value is actually the clock marker."
  3796. (marker-buffer org-clock-marker))
  3797. (eval-and-compile
  3798. (org-autoload "org-clock" '(org-clock-remove-overlays
  3799. org-clock-update-time-maybe
  3800. org-clocktable-shift)))
  3801. (defun org-check-running-clock ()
  3802. "Check if the current buffer contains the running clock.
  3803. If yes, offer to stop it and to save the buffer with the changes."
  3804. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3805. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3806. (buffer-name))))
  3807. (org-clock-out)
  3808. (when (y-or-n-p "Save changed buffer?")
  3809. (save-buffer))))
  3810. (defun org-clocktable-try-shift (dir n)
  3811. "Check if this line starts a clock table, if yes, shift the time block."
  3812. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3813. (org-clocktable-shift dir n)))
  3814. ;;;###autoload
  3815. (defun org-clock-persistence-insinuate ()
  3816. "Set up hooks for clock persistence."
  3817. (require 'org-clock)
  3818. (add-hook 'org-mode-hook 'org-clock-load)
  3819. (add-hook 'kill-emacs-hook 'org-clock-save))
  3820. ;; Define the variable already here, to make sure we have it.
  3821. (defvar org-indent-mode nil
  3822. "Non-nil if Org-Indent mode is enabled.
  3823. Use the command `org-indent-mode' to change this variable.")
  3824. ;; Autoload archiving code
  3825. ;; The stuff that is needed for cycling and tags has to be defined here.
  3826. (defgroup org-archive nil
  3827. "Options concerning archiving in Org-mode."
  3828. :tag "Org Archive"
  3829. :group 'org-structure)
  3830. (defcustom org-archive-location "%s_archive::"
  3831. "The location where subtrees should be archived.
  3832. The value of this variable is a string, consisting of two parts,
  3833. separated by a double-colon. The first part is a filename and
  3834. the second part is a headline.
  3835. When the filename is omitted, archiving happens in the same file.
  3836. %s in the filename will be replaced by the current file
  3837. name (without the directory part). Archiving to a different file
  3838. is useful to keep archived entries from contributing to the
  3839. Org-mode Agenda.
  3840. The archived entries will be filed as subtrees of the specified
  3841. headline. When the headline is omitted, the subtrees are simply
  3842. filed away at the end of the file, as top-level entries. Also in
  3843. the heading you can use %s to represent the file name, this can be
  3844. useful when using the same archive for a number of different files.
  3845. Here are a few examples:
  3846. \"%s_archive::\"
  3847. If the current file is Projects.org, archive in file
  3848. Projects.org_archive, as top-level trees. This is the default.
  3849. \"::* Archived Tasks\"
  3850. Archive in the current file, under the top-level headline
  3851. \"* Archived Tasks\".
  3852. \"~/org/archive.org::\"
  3853. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3854. \"~/org/archive.org::* From %s\"
  3855. Archive in file ~/org/archive.org (absolute path), under headlines
  3856. \"From FILENAME\" where file name is the current file name.
  3857. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3858. The \"datetree/\" string is special, signifying to archive
  3859. items to the datetree. Items are placed in either the CLOSED
  3860. date of the item, or the current date if there is no CLOSED date.
  3861. The heading will be a subentry to the current date. There doesn't
  3862. need to be a heading, but there always needs to be a slash after
  3863. datetree. For example, to store archived items directly in the
  3864. datetree, use \"~/org/datetree.org::datetree/\".
  3865. \"basement::** Finished Tasks\"
  3866. Archive in file ./basement (relative path), as level 3 trees
  3867. below the level 2 heading \"** Finished Tasks\".
  3868. You may set this option on a per-file basis by adding to the buffer a
  3869. line like
  3870. #+ARCHIVE: basement::** Finished Tasks
  3871. You may also define it locally for a subtree by setting an ARCHIVE property
  3872. in the entry. If such a property is found in an entry, or anywhere up
  3873. the hierarchy, it will be used."
  3874. :group 'org-archive
  3875. :type 'string)
  3876. (defcustom org-archive-tag "ARCHIVE"
  3877. "The tag that marks a subtree as archived.
  3878. An archived subtree does not open during visibility cycling, and does
  3879. not contribute to the agenda listings.
  3880. After changing this, font-lock must be restarted in the relevant buffers to
  3881. get the proper fontification."
  3882. :group 'org-archive
  3883. :group 'org-keywords
  3884. :type 'string)
  3885. (defcustom org-agenda-skip-archived-trees t
  3886. "Non-nil means the agenda will skip any items located in archived trees.
  3887. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3888. variable is no longer recommended, you should leave it at the value t.
  3889. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3890. :group 'org-archive
  3891. :group 'org-agenda-skip
  3892. :type 'boolean)
  3893. (defcustom org-columns-skip-archived-trees t
  3894. "Non-nil means ignore archived trees when creating column view."
  3895. :group 'org-archive
  3896. :group 'org-properties
  3897. :type 'boolean)
  3898. (defcustom org-cycle-open-archived-trees nil
  3899. "Non-nil means `org-cycle' will open archived trees.
  3900. An archived tree is a tree marked with the tag ARCHIVE.
  3901. When nil, archived trees will stay folded. You can still open them with
  3902. normal outline commands like `show-all', but not with the cycling commands."
  3903. :group 'org-archive
  3904. :group 'org-cycle
  3905. :type 'boolean)
  3906. (defcustom org-sparse-tree-open-archived-trees nil
  3907. "Non-nil means sparse tree construction shows matches in archived trees.
  3908. When nil, matches in these trees are highlighted, but the trees are kept in
  3909. collapsed state."
  3910. :group 'org-archive
  3911. :group 'org-sparse-trees
  3912. :type 'boolean)
  3913. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3914. "The default date type when building a sparse tree.
  3915. When this is nil, a date is a scheduled or a deadline timestamp.
  3916. Otherwise, these types are allowed:
  3917. all: all timestamps
  3918. active: only active timestamps (<...>)
  3919. inactive: only inactive timestamps (<...)
  3920. scheduled: only scheduled timestamps
  3921. deadline: only deadline timestamps"
  3922. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3923. (const :tag "All timestamps" all)
  3924. (const :tag "Only active timestamps" active)
  3925. (const :tag "Only inactive timestamps" inactive)
  3926. (const :tag "Only scheduled timestamps" scheduled)
  3927. (const :tag "Only deadline timestamps" deadline)
  3928. (const :tag "Only closed timestamps" closed))
  3929. :version "24.3"
  3930. :group 'org-sparse-trees)
  3931. (defun org-cycle-hide-archived-subtrees (state)
  3932. "Re-hide all archived subtrees after a visibility state change."
  3933. (when (and (not org-cycle-open-archived-trees)
  3934. (not (memq state '(overview folded))))
  3935. (save-excursion
  3936. (let* ((globalp (memq state '(contents all)))
  3937. (beg (if globalp (point-min) (point)))
  3938. (end (if globalp (point-max) (org-end-of-subtree t))))
  3939. (org-hide-archived-subtrees beg end)
  3940. (goto-char beg)
  3941. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3942. (message "%s" (substitute-command-keys
  3943. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3944. (defun org-force-cycle-archived ()
  3945. "Cycle subtree even if it is archived."
  3946. (interactive)
  3947. (setq this-command 'org-cycle)
  3948. (let ((org-cycle-open-archived-trees t))
  3949. (call-interactively 'org-cycle)))
  3950. (defun org-hide-archived-subtrees (beg end)
  3951. "Re-hide all archived subtrees after a visibility state change."
  3952. (save-excursion
  3953. (let* ((re (concat ":" org-archive-tag ":")))
  3954. (goto-char beg)
  3955. (while (re-search-forward re end t)
  3956. (when (org-at-heading-p)
  3957. (org-flag-subtree t)
  3958. (org-end-of-subtree t))))))
  3959. (declare-function outline-end-of-heading "outline" ())
  3960. (declare-function outline-flag-region "outline" (from to flag))
  3961. (defun org-flag-subtree (flag)
  3962. (save-excursion
  3963. (org-back-to-heading t)
  3964. (outline-end-of-heading)
  3965. (outline-flag-region (point)
  3966. (progn (org-end-of-subtree t) (point))
  3967. flag)))
  3968. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3969. (eval-and-compile
  3970. (org-autoload "org-archive"
  3971. '(org-add-archive-files)))
  3972. ;; Autoload Column View Code
  3973. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3974. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3975. (declare-function org-columns-compute "org-colview" (property))
  3976. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3977. '(org-columns-number-to-string
  3978. org-columns-get-format-and-top-level
  3979. org-columns-compute
  3980. org-columns-remove-overlays))
  3981. ;; Autoload ID code
  3982. (declare-function org-id-store-link "org-id")
  3983. (declare-function org-id-locations-load "org-id")
  3984. (declare-function org-id-locations-save "org-id")
  3985. (defvar org-id-track-globally)
  3986. (org-autoload "org-id"
  3987. '(org-id-new
  3988. org-id-copy
  3989. org-id-get-with-outline-path-completion
  3990. org-id-get-with-outline-drilling))
  3991. ;;; Variables for pre-computed regular expressions, all buffer local
  3992. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3993. "Matches first line of a hidden block.")
  3994. (make-variable-buffer-local 'org-drawer-regexp)
  3995. (defvar org-todo-regexp nil
  3996. "Matches any of the TODO state keywords.")
  3997. (make-variable-buffer-local 'org-todo-regexp)
  3998. (defvar org-not-done-regexp nil
  3999. "Matches any of the TODO state keywords except the last one.")
  4000. (make-variable-buffer-local 'org-not-done-regexp)
  4001. (defvar org-not-done-heading-regexp nil
  4002. "Matches a TODO headline that is not done.")
  4003. (make-variable-buffer-local 'org-not-done-regexp)
  4004. (defvar org-todo-line-regexp nil
  4005. "Matches a headline and puts TODO state into group 2 if present.")
  4006. (make-variable-buffer-local 'org-todo-line-regexp)
  4007. (defvar org-complex-heading-regexp nil
  4008. "Matches a headline and puts everything into groups:
  4009. group 1: the stars
  4010. group 2: The todo keyword, maybe
  4011. group 3: Priority cookie
  4012. group 4: True headline
  4013. group 5: Tags")
  4014. (make-variable-buffer-local 'org-complex-heading-regexp)
  4015. (defvar org-complex-heading-regexp-format nil
  4016. "Printf format to make regexp to match an exact headline.
  4017. This regexp will match the headline of any node which has the
  4018. exact headline text that is put into the format, but may have any
  4019. TODO state, priority and tags.")
  4020. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4021. (defvar org-todo-line-tags-regexp nil
  4022. "Matches a headline and puts TODO state into group 2 if present.
  4023. Also put tags into group 4 if tags are present.")
  4024. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4025. (defvar org-ds-keyword-length 12
  4026. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4027. (make-variable-buffer-local 'org-ds-keyword-length)
  4028. (defvar org-deadline-regexp nil
  4029. "Matches the DEADLINE keyword.")
  4030. (make-variable-buffer-local 'org-deadline-regexp)
  4031. (defvar org-deadline-time-regexp nil
  4032. "Matches the DEADLINE keyword together with a time stamp.")
  4033. (make-variable-buffer-local 'org-deadline-time-regexp)
  4034. (defvar org-deadline-time-hour-regexp nil
  4035. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4036. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4037. (defvar org-deadline-line-regexp nil
  4038. "Matches the DEADLINE keyword and the rest of the line.")
  4039. (make-variable-buffer-local 'org-deadline-line-regexp)
  4040. (defvar org-scheduled-regexp nil
  4041. "Matches the SCHEDULED keyword.")
  4042. (make-variable-buffer-local 'org-scheduled-regexp)
  4043. (defvar org-scheduled-time-regexp nil
  4044. "Matches the SCHEDULED keyword together with a time stamp.")
  4045. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4046. (defvar org-scheduled-time-hour-regexp nil
  4047. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4048. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4049. (defvar org-closed-time-regexp nil
  4050. "Matches the CLOSED keyword together with a time stamp.")
  4051. (make-variable-buffer-local 'org-closed-time-regexp)
  4052. (defvar org-keyword-time-regexp nil
  4053. "Matches any of the 4 keywords, together with the time stamp.")
  4054. (make-variable-buffer-local 'org-keyword-time-regexp)
  4055. (defvar org-keyword-time-not-clock-regexp nil
  4056. "Matches any of the 3 keywords, together with the time stamp.")
  4057. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4058. (defvar org-maybe-keyword-time-regexp nil
  4059. "Matches a timestamp, possibly preceded by a keyword.")
  4060. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4061. (defvar org-all-time-keywords nil
  4062. "List of time keywords.")
  4063. (make-variable-buffer-local 'org-all-time-keywords)
  4064. (defconst org-plain-time-of-day-regexp
  4065. (concat
  4066. "\\(\\<[012]?[0-9]"
  4067. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4068. "\\(--?"
  4069. "\\(\\<[012]?[0-9]"
  4070. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4071. "\\)?")
  4072. "Regular expression to match a plain time or time range.
  4073. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4074. groups carry important information:
  4075. 0 the full match
  4076. 1 the first time, range or not
  4077. 8 the second time, if it is a range.")
  4078. (defconst org-plain-time-extension-regexp
  4079. (concat
  4080. "\\(\\<[012]?[0-9]"
  4081. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4082. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4083. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4084. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4085. groups carry important information:
  4086. 0 the full match
  4087. 7 hours of duration
  4088. 9 minutes of duration")
  4089. (defconst org-stamp-time-of-day-regexp
  4090. (concat
  4091. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4092. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4093. "\\(--?"
  4094. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4095. "Regular expression to match a timestamp time or time range.
  4096. After a match, the following groups carry important information:
  4097. 0 the full match
  4098. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4099. 2 the first time, range or not
  4100. 4 the second time, if it is a range.")
  4101. (defconst org-startup-options
  4102. '(("fold" org-startup-folded t)
  4103. ("overview" org-startup-folded t)
  4104. ("nofold" org-startup-folded nil)
  4105. ("showall" org-startup-folded nil)
  4106. ("showeverything" org-startup-folded showeverything)
  4107. ("content" org-startup-folded content)
  4108. ("indent" org-startup-indented t)
  4109. ("noindent" org-startup-indented nil)
  4110. ("hidestars" org-hide-leading-stars t)
  4111. ("showstars" org-hide-leading-stars nil)
  4112. ("odd" org-odd-levels-only t)
  4113. ("oddeven" org-odd-levels-only nil)
  4114. ("align" org-startup-align-all-tables t)
  4115. ("noalign" org-startup-align-all-tables nil)
  4116. ("inlineimages" org-startup-with-inline-images t)
  4117. ("noinlineimages" org-startup-with-inline-images nil)
  4118. ("latexpreview" org-startup-with-latex-preview t)
  4119. ("nolatexpreview" org-startup-with-latex-preview nil)
  4120. ("customtime" org-display-custom-times t)
  4121. ("logdone" org-log-done time)
  4122. ("lognotedone" org-log-done note)
  4123. ("nologdone" org-log-done nil)
  4124. ("lognoteclock-out" org-log-note-clock-out t)
  4125. ("nolognoteclock-out" org-log-note-clock-out nil)
  4126. ("logrepeat" org-log-repeat state)
  4127. ("lognoterepeat" org-log-repeat note)
  4128. ("logdrawer" org-log-into-drawer t)
  4129. ("nologdrawer" org-log-into-drawer nil)
  4130. ("logstatesreversed" org-log-states-order-reversed t)
  4131. ("nologstatesreversed" org-log-states-order-reversed nil)
  4132. ("nologrepeat" org-log-repeat nil)
  4133. ("logreschedule" org-log-reschedule time)
  4134. ("lognotereschedule" org-log-reschedule note)
  4135. ("nologreschedule" org-log-reschedule nil)
  4136. ("logredeadline" org-log-redeadline time)
  4137. ("lognoteredeadline" org-log-redeadline note)
  4138. ("nologredeadline" org-log-redeadline nil)
  4139. ("logrefile" org-log-refile time)
  4140. ("lognoterefile" org-log-refile note)
  4141. ("nologrefile" org-log-refile nil)
  4142. ("fninline" org-footnote-define-inline t)
  4143. ("nofninline" org-footnote-define-inline nil)
  4144. ("fnlocal" org-footnote-section nil)
  4145. ("fnauto" org-footnote-auto-label t)
  4146. ("fnprompt" org-footnote-auto-label nil)
  4147. ("fnconfirm" org-footnote-auto-label confirm)
  4148. ("fnplain" org-footnote-auto-label plain)
  4149. ("fnadjust" org-footnote-auto-adjust t)
  4150. ("nofnadjust" org-footnote-auto-adjust nil)
  4151. ("constcgs" constants-unit-system cgs)
  4152. ("constSI" constants-unit-system SI)
  4153. ("noptag" org-tag-persistent-alist nil)
  4154. ("hideblocks" org-hide-block-startup t)
  4155. ("nohideblocks" org-hide-block-startup nil)
  4156. ("beamer" org-startup-with-beamer-mode t)
  4157. ("entitiespretty" org-pretty-entities t)
  4158. ("entitiesplain" org-pretty-entities nil))
  4159. "Variable associated with STARTUP options for org-mode.
  4160. Each element is a list of three items: the startup options (as written
  4161. in the #+STARTUP line), the corresponding variable, and the value to set
  4162. this variable to if the option is found. An optional forth element PUSH
  4163. means to push this value onto the list in the variable.")
  4164. (defun org-update-property-plist (key val props)
  4165. "Update PROPS with KEY and VAL."
  4166. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4167. (key (if appending (substring key 0 (- (length key) 1)) key))
  4168. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4169. (previous (cdr (assoc key props))))
  4170. (if appending
  4171. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4172. (cons (cons key val) remainder))))
  4173. (defconst org-block-regexp
  4174. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4175. "Regular expression for hiding blocks.")
  4176. (defconst org-heading-keyword-regexp-format
  4177. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4178. "Printf format for a regexp matching a headline with some keyword.
  4179. This regexp will match the headline of any node which has the
  4180. exact keyword that is put into the format. The keyword isn't in
  4181. any group by default, but the stars and the body are.")
  4182. (defconst org-heading-keyword-maybe-regexp-format
  4183. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4184. "Printf format for a regexp matching a headline, possibly with some keyword.
  4185. This regexp can match any headline with the specified keyword, or
  4186. without a keyword. The keyword isn't in any group by default,
  4187. but the stars and the body are.")
  4188. (defcustom org-group-tags t
  4189. "When non-nil (the default), use group tags.
  4190. This can be turned on/off through `org-toggle-tags-groups'."
  4191. :group 'org-tags
  4192. :group 'org-startup
  4193. :type 'boolean)
  4194. (defun org-toggle-tags-groups ()
  4195. "Toggle support for group tags.
  4196. Support for group tags is controlled by the option
  4197. `org-group-tags', which is non-nil by default."
  4198. (interactive)
  4199. (setq org-group-tags (not org-group-tags))
  4200. (cond ((and (derived-mode-p 'org-agenda-mode)
  4201. org-group-tags)
  4202. (org-agenda-redo))
  4203. ((derived-mode-p 'org-mode)
  4204. (let ((org-inhibit-startup t)) (org-mode))))
  4205. (message "Groups tags support has been turned %s"
  4206. (if org-group-tags "on" "off")))
  4207. (defun org-set-regexps-and-options-for-tags ()
  4208. "Precompute regular expressions used for tags in the current buffer."
  4209. (when (derived-mode-p 'org-mode)
  4210. (org-set-local 'org-file-tags nil)
  4211. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4212. (splitre "[ \t]+")
  4213. (start 0)
  4214. tags ftags key value)
  4215. (save-excursion
  4216. (save-restriction
  4217. (widen)
  4218. (goto-char (point-min))
  4219. (while (re-search-forward re nil t)
  4220. (setq key (upcase (org-match-string-no-properties 1))
  4221. value (org-match-string-no-properties 2))
  4222. (if (stringp value) (setq value (org-trim value)))
  4223. (cond
  4224. ((equal key "TAGS")
  4225. (setq tags (append tags (if tags '("\\n") nil)
  4226. (org-split-string value splitre))))
  4227. ((equal key "FILETAGS")
  4228. (when (string-match "\\S-" value)
  4229. (setq ftags
  4230. (append
  4231. ftags
  4232. (apply 'append
  4233. (mapcar (lambda (x) (org-split-string x ":"))
  4234. (org-split-string value)))))))))))
  4235. ;; Process the file tags.
  4236. (and ftags (org-set-local 'org-file-tags
  4237. (mapcar 'org-add-prop-inherited ftags)))
  4238. (org-set-local 'org-tag-groups-alist nil)
  4239. ;; Process the tags.
  4240. ;; FIXME
  4241. (when tags
  4242. (let (e tgs g)
  4243. (while (setq e (pop tags))
  4244. (cond
  4245. ((equal e "{")
  4246. (progn (push '(:startgroup) tgs)
  4247. (when (equal (nth 1 tags) ":")
  4248. (push (list (replace-regexp-in-string
  4249. "(.+)$" "" (nth 0 tags)))
  4250. org-tag-groups-alist)
  4251. (setq g 0))))
  4252. ((equal e ":") (push '(:grouptags) tgs))
  4253. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4254. ((equal e "\\n") (push '(:newline) tgs))
  4255. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4256. (push (cons (match-string 1 e)
  4257. (string-to-char (match-string 2 e))) tgs)
  4258. (if (and g (> g 0))
  4259. (setcar org-tag-groups-alist
  4260. (append (car org-tag-groups-alist)
  4261. (list (match-string 1 e)))))
  4262. (if g (setq g (1+ g))))
  4263. (t (push (list e) tgs)
  4264. (if (and g (> g 0))
  4265. (setcar org-tag-groups-alist
  4266. (append (car org-tag-groups-alist) (list e))))
  4267. (if g (setq g (1+ g))))))
  4268. (org-set-local 'org-tag-alist nil)
  4269. (while (setq e (pop tgs))
  4270. (or (and (stringp (car e))
  4271. (assoc (car e) org-tag-alist))
  4272. (push e org-tag-alist))))))))
  4273. (defun org-set-regexps-and-options ()
  4274. "Precompute regular expressions used in the current buffer."
  4275. (when (derived-mode-p 'org-mode)
  4276. (org-set-local 'org-todo-kwd-alist nil)
  4277. (org-set-local 'org-todo-key-alist nil)
  4278. (org-set-local 'org-todo-key-trigger nil)
  4279. (org-set-local 'org-todo-keywords-1 nil)
  4280. (org-set-local 'org-done-keywords nil)
  4281. (org-set-local 'org-todo-heads nil)
  4282. (org-set-local 'org-todo-sets nil)
  4283. (org-set-local 'org-todo-log-states nil)
  4284. (org-set-local 'org-file-properties nil)
  4285. (let ((re (org-make-options-regexp
  4286. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4287. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4288. "SETUPFILE" "OPTIONS")
  4289. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4290. (splitre "[ \t]+")
  4291. (scripts org-use-sub-superscripts)
  4292. kwds kws0 kwsa key log value cat arch const links hw dws
  4293. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4294. (start 0))
  4295. (save-excursion
  4296. (save-restriction
  4297. (widen)
  4298. (goto-char (point-min))
  4299. (while (or (and ext-setup-or-nil
  4300. (let (ret)
  4301. (with-temp-buffer
  4302. (insert ext-setup-or-nil)
  4303. (let ((major-mode 'org-mode))
  4304. (org-set-regexps-and-options-for-tags)
  4305. (setq ret (list org-file-tags org-tag-alist
  4306. org-tag-groups-alist))))
  4307. (setq org-file-tags (nth 0 ret)
  4308. org-tag-alist (nth 1 ret)
  4309. org-tag-groups-alist (nth 2 ret))))
  4310. (and ext-setup-or-nil
  4311. (string-match re ext-setup-or-nil start)
  4312. (setq start (match-end 0)))
  4313. (and (setq ext-setup-or-nil nil start 0)
  4314. (re-search-forward re nil t)))
  4315. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4316. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4317. (if (stringp value) (setq value (org-trim value)))
  4318. (cond
  4319. ((equal key "CATEGORY")
  4320. (setq cat value))
  4321. ((member key '("SEQ_TODO" "TODO"))
  4322. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4323. ((equal key "TYP_TODO")
  4324. (push (cons 'type (org-split-string value splitre)) kwds))
  4325. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4326. ;; general TODO-like setup
  4327. (push (cons (intern (downcase (match-string 1 key)))
  4328. (org-split-string value splitre)) kwds))
  4329. ((equal key "COLUMNS")
  4330. (org-set-local 'org-columns-default-format value))
  4331. ((equal key "LINK")
  4332. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4333. (push (cons (match-string 1 value)
  4334. (org-trim (match-string 2 value)))
  4335. links)))
  4336. ((equal key "PRIORITIES")
  4337. (setq prio (org-split-string value " +")))
  4338. ((equal key "PROPERTY")
  4339. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4340. (setq props (org-update-property-plist (match-string 1 value)
  4341. (match-string 2 value)
  4342. props))))
  4343. ((equal key "DRAWERS")
  4344. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4345. ((equal key "CONSTANTS")
  4346. (org-table-set-constants))
  4347. ((equal key "STARTUP")
  4348. (let ((opts (org-split-string value splitre))
  4349. l var val)
  4350. (while (setq l (pop opts))
  4351. (when (setq l (assoc l org-startup-options))
  4352. (setq var (nth 1 l) val (nth 2 l))
  4353. (if (not (nth 3 l))
  4354. (set (make-local-variable var) val)
  4355. (if (not (listp (symbol-value var)))
  4356. (set (make-local-variable var) nil))
  4357. (set (make-local-variable var) (symbol-value var))
  4358. (add-to-list var val))))))
  4359. ((equal key "ARCHIVE")
  4360. (setq arch value)
  4361. (remove-text-properties 0 (length arch)
  4362. '(face t fontified t) arch))
  4363. ((equal key "OPTIONS")
  4364. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4365. (setq scripts (read (match-string 2 value)))))
  4366. ((and (equal key "SETUPFILE")
  4367. ;; Prevent checking in Gnus messages
  4368. (not buffer-read-only))
  4369. (setq setup-contents (org-file-contents
  4370. (expand-file-name
  4371. (org-remove-double-quotes value))
  4372. 'noerror))
  4373. (if (not ext-setup-or-nil)
  4374. (setq ext-setup-or-nil setup-contents start 0)
  4375. (setq ext-setup-or-nil
  4376. (concat (substring ext-setup-or-nil 0 start)
  4377. "\n" setup-contents "\n"
  4378. (substring ext-setup-or-nil start)))))))
  4379. ;; search for property blocks
  4380. (goto-char (point-min))
  4381. (while (re-search-forward org-block-regexp nil t)
  4382. (when (equal "PROPERTY" (upcase (match-string 1)))
  4383. (setq value (replace-regexp-in-string
  4384. "[\n\r]" " " (match-string 4)))
  4385. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4386. (setq props (org-update-property-plist (match-string 1 value)
  4387. (match-string 2 value)
  4388. props)))))))
  4389. (org-set-local 'org-use-sub-superscripts scripts)
  4390. (when cat
  4391. (org-set-local 'org-category (intern cat))
  4392. (push (cons "CATEGORY" cat) props))
  4393. (when prio
  4394. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4395. (setq prio (mapcar 'string-to-char prio))
  4396. (org-set-local 'org-highest-priority (nth 0 prio))
  4397. (org-set-local 'org-lowest-priority (nth 1 prio))
  4398. (org-set-local 'org-default-priority (nth 2 prio)))
  4399. (and props (org-set-local 'org-file-properties (nreverse props)))
  4400. (and drawers (org-set-local 'org-drawers drawers))
  4401. (and arch (org-set-local 'org-archive-location arch))
  4402. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4403. ;; Process the TODO keywords
  4404. (unless kwds
  4405. ;; Use the global values as if they had been given locally.
  4406. (setq kwds (default-value 'org-todo-keywords))
  4407. (if (stringp (car kwds))
  4408. (setq kwds (list (cons org-todo-interpretation
  4409. (default-value 'org-todo-keywords)))))
  4410. (setq kwds (reverse kwds)))
  4411. (setq kwds (nreverse kwds))
  4412. (let (inter kws kw)
  4413. (while (setq kws (pop kwds))
  4414. (let ((kws (or
  4415. (run-hook-with-args-until-success
  4416. 'org-todo-setup-filter-hook kws)
  4417. kws)))
  4418. (setq inter (pop kws) sep (member "|" kws)
  4419. kws0 (delete "|" (copy-sequence kws))
  4420. kwsa nil
  4421. kws1 (mapcar
  4422. (lambda (x)
  4423. ;; 1 2
  4424. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4425. (progn
  4426. (setq kw (match-string 1 x)
  4427. key (and (match-end 2) (match-string 2 x))
  4428. log (org-extract-log-state-settings x))
  4429. (push (cons kw (and key (string-to-char key))) kwsa)
  4430. (and log (push log org-todo-log-states))
  4431. kw)
  4432. (error "Invalid TODO keyword %s" x)))
  4433. kws0)
  4434. kwsa (if kwsa (append '((:startgroup))
  4435. (nreverse kwsa)
  4436. '((:endgroup))))
  4437. hw (car kws1)
  4438. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4439. tail (list inter hw (car dws) (org-last dws))))
  4440. (add-to-list 'org-todo-heads hw 'append)
  4441. (push kws1 org-todo-sets)
  4442. (setq org-done-keywords (append org-done-keywords dws nil))
  4443. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4444. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4445. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4446. (setq org-todo-sets (nreverse org-todo-sets)
  4447. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4448. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4449. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4450. ;; Compute the regular expressions and other local variables.
  4451. ;; Using `org-outline-regexp-bol' would complicate them much,
  4452. ;; because of the fixed white space at the end of that string.
  4453. (if (not org-done-keywords)
  4454. (setq org-done-keywords (and org-todo-keywords-1
  4455. (list (org-last org-todo-keywords-1)))))
  4456. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4457. (length org-scheduled-string)
  4458. (length org-clock-string)
  4459. (length org-closed-string)))
  4460. org-drawer-regexp
  4461. (concat "^[ \t]*:\\("
  4462. (mapconcat 'regexp-quote org-drawers "\\|")
  4463. "\\):[ \t]*$")
  4464. org-not-done-keywords
  4465. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4466. org-todo-regexp
  4467. (concat "\\("
  4468. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4469. "\\)")
  4470. org-not-done-regexp
  4471. (concat "\\("
  4472. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4473. "\\)")
  4474. org-not-done-heading-regexp
  4475. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4476. org-todo-line-regexp
  4477. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4478. org-complex-heading-regexp
  4479. (concat "^\\(\\*+\\)"
  4480. "\\(?: +" org-todo-regexp "\\)?"
  4481. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4482. "\\(?: +\\(.*?\\)\\)??"
  4483. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4484. "[ \t]*$")
  4485. org-complex-heading-regexp-format
  4486. (concat "^\\(\\*+\\)"
  4487. "\\(?: +" org-todo-regexp "\\)?"
  4488. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4489. "\\(?: +"
  4490. ;; Stats cookies can be stuck to body.
  4491. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4492. "\\(%s\\)"
  4493. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4494. "\\)"
  4495. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4496. "[ \t]*$")
  4497. org-todo-line-tags-regexp
  4498. (concat "^\\(\\*+\\)"
  4499. "\\(?: +" org-todo-regexp "\\)?"
  4500. "\\(?: +\\(.*?\\)\\)??"
  4501. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4502. "[ \t]*$")
  4503. org-deadline-regexp (concat "\\<" org-deadline-string)
  4504. org-deadline-time-regexp
  4505. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4506. org-deadline-time-hour-regexp
  4507. (concat "\\<" org-deadline-string
  4508. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4509. org-deadline-line-regexp
  4510. (concat "\\<\\(" org-deadline-string "\\).*")
  4511. org-scheduled-regexp
  4512. (concat "\\<" org-scheduled-string)
  4513. org-scheduled-time-regexp
  4514. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4515. org-scheduled-time-hour-regexp
  4516. (concat "\\<" org-scheduled-string
  4517. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4518. org-closed-time-regexp
  4519. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4520. org-keyword-time-regexp
  4521. (concat "\\<\\(" org-scheduled-string
  4522. "\\|" org-deadline-string
  4523. "\\|" org-closed-string
  4524. "\\|" org-clock-string "\\)"
  4525. " *[[<]\\([^]>]+\\)[]>]")
  4526. org-keyword-time-not-clock-regexp
  4527. (concat "\\<\\(" org-scheduled-string
  4528. "\\|" org-deadline-string
  4529. "\\|" org-closed-string
  4530. "\\)"
  4531. " *[[<]\\([^]>]+\\)[]>]")
  4532. org-maybe-keyword-time-regexp
  4533. (concat "\\(\\<\\(" org-scheduled-string
  4534. "\\|" org-deadline-string
  4535. "\\|" org-closed-string
  4536. "\\|" org-clock-string "\\)\\)?"
  4537. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4538. org-all-time-keywords
  4539. (mapcar (lambda (w) (substring w 0 -1))
  4540. (list org-scheduled-string org-deadline-string
  4541. org-clock-string org-closed-string)))
  4542. (org-compute-latex-and-related-regexp)
  4543. (org-set-font-lock-defaults))))
  4544. (defun org-file-contents (file &optional noerror)
  4545. "Return the contents of FILE, as a string."
  4546. (if (or (not file)
  4547. (not (file-readable-p file)))
  4548. (if noerror
  4549. (message "Cannot read file \"%s\"" file)
  4550. (error "Cannot read file \"%s\"" file))
  4551. (with-temp-buffer
  4552. (insert-file-contents file)
  4553. (buffer-string))))
  4554. (defun org-extract-log-state-settings (x)
  4555. "Extract the log state setting from a TODO keyword string.
  4556. This will extract info from a string like \"WAIT(w@/!)\"."
  4557. (let (kw key log1 log2)
  4558. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4559. (setq kw (match-string 1 x)
  4560. key (and (match-end 2) (match-string 2 x))
  4561. log1 (and (match-end 3) (match-string 3 x))
  4562. log2 (and (match-end 4) (match-string 4 x)))
  4563. (and (or log1 log2)
  4564. (list kw
  4565. (and log1 (if (equal log1 "!") 'time 'note))
  4566. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4567. (defun org-remove-keyword-keys (list)
  4568. "Remove a pair of parenthesis at the end of each string in LIST."
  4569. (mapcar (lambda (x)
  4570. (if (string-match "(.*)$" x)
  4571. (substring x 0 (match-beginning 0))
  4572. x))
  4573. list))
  4574. (defun org-assign-fast-keys (alist)
  4575. "Assign fast keys to a keyword-key alist.
  4576. Respect keys that are already there."
  4577. (let (new e (alt ?0))
  4578. (while (setq e (pop alist))
  4579. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4580. (cdr e)) ;; Key already assigned.
  4581. (push e new)
  4582. (let ((clist (string-to-list (downcase (car e))))
  4583. (used (append new alist)))
  4584. (when (= (car clist) ?@)
  4585. (pop clist))
  4586. (while (and clist (rassoc (car clist) used))
  4587. (pop clist))
  4588. (unless clist
  4589. (while (rassoc alt used)
  4590. (incf alt)))
  4591. (push (cons (car e) (or (car clist) alt)) new))))
  4592. (nreverse new)))
  4593. ;;; Some variables used in various places
  4594. (defvar org-window-configuration nil
  4595. "Used in various places to store a window configuration.")
  4596. (defvar org-selected-window nil
  4597. "Used in various places to store a window configuration.")
  4598. (defvar org-finish-function nil
  4599. "Function to be called when `C-c C-c' is used.
  4600. This is for getting out of special buffers like capture.")
  4601. ;; FIXME: Occasionally check by commenting these, to make sure
  4602. ;; no other functions uses these, forgetting to let-bind them.
  4603. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4604. (defvar org-last-state)
  4605. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4606. ;; Defined somewhere in this file, but used before definition.
  4607. (defvar org-entities) ;; defined in org-entities.el
  4608. (defvar org-struct-menu)
  4609. (defvar org-org-menu)
  4610. (defvar org-tbl-menu)
  4611. ;;;; Define the Org-mode
  4612. ;; We use a before-change function to check if a table might need
  4613. ;; an update.
  4614. (defvar org-table-may-need-update t
  4615. "Indicates that a table might need an update.
  4616. This variable is set by `org-before-change-function'.
  4617. `org-table-align' sets it back to nil.")
  4618. (defun org-before-change-function (beg end)
  4619. "Every change indicates that a table might need an update."
  4620. (setq org-table-may-need-update t))
  4621. (defvar org-mode-map)
  4622. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4623. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4624. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4625. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4626. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4627. (defvar org-table-buffer-is-an nil)
  4628. (defvar bidi-paragraph-direction)
  4629. (defvar buffer-face-mode-face)
  4630. (require 'outline)
  4631. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4632. (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"))
  4633. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4634. ;; Other stuff we need.
  4635. (require 'time-date)
  4636. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4637. (require 'easymenu)
  4638. (require 'overlay)
  4639. ;; (require 'org-macs) moved higher up in the file before it is first used
  4640. (require 'org-entities)
  4641. ;; (require 'org-compat) moved higher up in the file before it is first used
  4642. (require 'org-faces)
  4643. (require 'org-list)
  4644. (require 'org-pcomplete)
  4645. (require 'org-src)
  4646. (require 'org-footnote)
  4647. (require 'org-macro)
  4648. ;; babel
  4649. (require 'ob)
  4650. ;;;###autoload
  4651. (define-derived-mode org-mode outline-mode "Org"
  4652. "Outline-based notes management and organizer, alias
  4653. \"Carsten's outline-mode for keeping track of everything.\"
  4654. Org-mode develops organizational tasks around a NOTES file which
  4655. contains information about projects as plain text. Org-mode is
  4656. implemented on top of outline-mode, which is ideal to keep the content
  4657. of large files well structured. It supports ToDo items, deadlines and
  4658. time stamps, which magically appear in the diary listing of the Emacs
  4659. calendar. Tables are easily created with a built-in table editor.
  4660. Plain text URL-like links connect to websites, emails (VM), Usenet
  4661. messages (Gnus), BBDB entries, and any files related to the project.
  4662. For printing and sharing of notes, an Org-mode file (or a part of it)
  4663. can be exported as a structured ASCII or HTML file.
  4664. The following commands are available:
  4665. \\{org-mode-map}"
  4666. ;; Get rid of Outline menus, they are not needed
  4667. ;; Need to do this here because define-derived-mode sets up
  4668. ;; the keymap so late. Still, it is a waste to call this each time
  4669. ;; we switch another buffer into org-mode.
  4670. (if (featurep 'xemacs)
  4671. (when (boundp 'outline-mode-menu-heading)
  4672. ;; Assume this is Greg's port, it uses easymenu
  4673. (easy-menu-remove outline-mode-menu-heading)
  4674. (easy-menu-remove outline-mode-menu-show)
  4675. (easy-menu-remove outline-mode-menu-hide))
  4676. (define-key org-mode-map [menu-bar headings] 'undefined)
  4677. (define-key org-mode-map [menu-bar hide] 'undefined)
  4678. (define-key org-mode-map [menu-bar show] 'undefined))
  4679. (org-load-modules-maybe)
  4680. (easy-menu-add org-org-menu)
  4681. (easy-menu-add org-tbl-menu)
  4682. (org-install-agenda-files-menu)
  4683. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4684. (add-to-invisibility-spec '(org-cwidth))
  4685. (add-to-invisibility-spec '(org-hide-block . t))
  4686. (when (featurep 'xemacs)
  4687. (org-set-local 'line-move-ignore-invisible t))
  4688. (org-set-local 'outline-regexp org-outline-regexp)
  4689. (org-set-local 'outline-level 'org-outline-level)
  4690. (setq bidi-paragraph-direction 'left-to-right)
  4691. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4692. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4693. (when (and org-ellipsis
  4694. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4695. (fboundp 'make-glyph-code))
  4696. (unless org-display-table
  4697. (setq org-display-table (make-display-table)))
  4698. (set-display-table-slot
  4699. org-display-table 4
  4700. (vconcat (mapcar
  4701. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4702. org-ellipsis)))
  4703. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4704. (setq buffer-display-table org-display-table))
  4705. (org-set-regexps-and-options-for-tags)
  4706. (org-set-regexps-and-options)
  4707. (when (and org-tag-faces (not org-tags-special-faces-re))
  4708. ;; tag faces set outside customize.... force initialization.
  4709. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4710. ;; Calc embedded
  4711. (org-set-local 'calc-embedded-open-mode "# ")
  4712. ;; Modify a few syntax entries
  4713. (modify-syntax-entry ?@ "w")
  4714. (modify-syntax-entry ?\" "\"")
  4715. (if org-startup-truncated (setq truncate-lines t))
  4716. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4717. (org-set-local 'font-lock-unfontify-region-function
  4718. 'org-unfontify-region)
  4719. ;; Activate before-change-function
  4720. (org-set-local 'org-table-may-need-update t)
  4721. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4722. 'local)
  4723. ;; Check for running clock before killing a buffer
  4724. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4725. ;; Initialize macros templates.
  4726. (org-macro-initialize-templates)
  4727. ;; Initialize radio targets.
  4728. (org-update-radio-target-regexp)
  4729. ;; Indentation.
  4730. (org-set-local 'indent-line-function 'org-indent-line)
  4731. (org-set-local 'indent-region-function 'org-indent-region)
  4732. ;; Filling and auto-filling.
  4733. (org-setup-filling)
  4734. ;; Comments.
  4735. (org-setup-comments-handling)
  4736. ;; Beginning/end of defun
  4737. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4738. (org-set-local 'end-of-defun-function 'org-forward-element)
  4739. ;; Next error for sparse trees
  4740. (org-set-local 'next-error-function 'org-occur-next-match)
  4741. ;; Make sure dependence stuff works reliably, even for users who set it
  4742. ;; too late :-(
  4743. (if org-enforce-todo-dependencies
  4744. (add-hook 'org-blocker-hook
  4745. 'org-block-todo-from-children-or-siblings-or-parent)
  4746. (remove-hook 'org-blocker-hook
  4747. 'org-block-todo-from-children-or-siblings-or-parent))
  4748. (if org-enforce-todo-checkbox-dependencies
  4749. (add-hook 'org-blocker-hook
  4750. 'org-block-todo-from-checkboxes)
  4751. (remove-hook 'org-blocker-hook
  4752. 'org-block-todo-from-checkboxes))
  4753. ;; Align options lines
  4754. (org-set-local
  4755. 'align-mode-rules-list
  4756. '((org-in-buffer-settings
  4757. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4758. (modes . '(org-mode)))))
  4759. ;; Imenu
  4760. (org-set-local 'imenu-create-index-function
  4761. 'org-imenu-get-tree)
  4762. ;; Make isearch reveal context
  4763. (if (or (featurep 'xemacs)
  4764. (not (boundp 'outline-isearch-open-invisible-function)))
  4765. ;; Emacs 21 and XEmacs make use of the hook
  4766. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4767. ;; Emacs 22 deals with this through a special variable
  4768. (org-set-local 'outline-isearch-open-invisible-function
  4769. (lambda (&rest ignore) (org-show-context 'isearch)))
  4770. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4771. ;; Setup the pcomplete hooks
  4772. (set (make-local-variable 'pcomplete-command-completion-function)
  4773. 'org-pcomplete-initial)
  4774. (set (make-local-variable 'pcomplete-command-name-function)
  4775. 'org-command-at-point)
  4776. (set (make-local-variable 'pcomplete-default-completion-function)
  4777. 'ignore)
  4778. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4779. 'org-parse-arguments)
  4780. (set (make-local-variable 'pcomplete-termination-string) "")
  4781. (when (>= emacs-major-version 23)
  4782. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4783. ;; If empty file that did not turn on org-mode automatically, make it to.
  4784. (if (and org-insert-mode-line-in-empty-file
  4785. (org-called-interactively-p 'any)
  4786. (= (point-min) (point-max)))
  4787. (insert "# -*- mode: org -*-\n\n"))
  4788. (unless org-inhibit-startup
  4789. (org-unmodified
  4790. (and org-startup-with-beamer-mode (org-beamer-mode))
  4791. (when org-startup-align-all-tables
  4792. (org-table-map-tables 'org-table-align 'quietly))
  4793. (when org-startup-with-inline-images
  4794. (org-display-inline-images))
  4795. (when org-startup-with-latex-preview
  4796. (org-preview-latex-fragment))
  4797. (unless org-inhibit-startup-visibility-stuff
  4798. (org-set-startup-visibility))))
  4799. ;; Try to set org-hide correctly
  4800. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4801. ;; Update `customize-package-emacs-version-alist'
  4802. (add-to-list 'customize-package-emacs-version-alist
  4803. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4804. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4805. ("8.0" . "24.4")))
  4806. (defvar org-mode-transpose-word-syntax-table
  4807. (let ((st (make-syntax-table)))
  4808. (mapc (lambda(c) (modify-syntax-entry
  4809. (string-to-char (car c)) "w p" st))
  4810. org-emphasis-alist)
  4811. st))
  4812. (when (fboundp 'abbrev-table-put)
  4813. (abbrev-table-put org-mode-abbrev-table
  4814. :parents (list text-mode-abbrev-table)))
  4815. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4816. (defsubst org-fix-ellipsis-at-bol ()
  4817. (save-excursion (goto-char (window-start)) (recenter 0)))
  4818. (defun org-find-invisible-foreground ()
  4819. (let ((candidates (remove
  4820. "unspecified-bg"
  4821. (nconc
  4822. (list (face-background 'default)
  4823. (face-background 'org-default))
  4824. (mapcar
  4825. (lambda (alist)
  4826. (when (boundp alist)
  4827. (cdr (assoc 'background-color (symbol-value alist)))))
  4828. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4829. (list (face-foreground 'org-hide))))))
  4830. (car (remove nil candidates))))
  4831. (defun org-current-time (&optional rounding-minutes past)
  4832. "Current time, possibly rounded to ROUNDING-MINUTES.
  4833. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4834. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4835. the rounding returns a past time."
  4836. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4837. (car org-time-stamp-rounding-minutes)))
  4838. (time (decode-time)) res)
  4839. (if (< r 1)
  4840. (current-time)
  4841. (setq res
  4842. (apply 'encode-time
  4843. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4844. (nthcdr 2 time))))
  4845. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4846. (seconds-to-time (- (org-float-time res) (* r 60)))
  4847. res))))
  4848. (defun org-today ()
  4849. "Return today date, considering `org-extend-today-until'."
  4850. (time-to-days
  4851. (time-subtract (current-time)
  4852. (list 0 (* 3600 org-extend-today-until) 0))))
  4853. ;;;; Font-Lock stuff, including the activators
  4854. (defvar org-mouse-map (make-sparse-keymap))
  4855. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4856. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4857. (when org-mouse-1-follows-link
  4858. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4859. (when org-tab-follows-link
  4860. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4861. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4862. (require 'font-lock)
  4863. (defconst org-non-link-chars "]\t\n\r<>")
  4864. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4865. "shell" "elisp" "doi" "message"))
  4866. (defvar org-link-types-re nil
  4867. "Matches a link that has a url-like prefix like \"http:\"")
  4868. (defvar org-link-re-with-space nil
  4869. "Matches a link with spaces, optional angular brackets around it.")
  4870. (defvar org-link-re-with-space2 nil
  4871. "Matches a link with spaces, optional angular brackets around it.")
  4872. (defvar org-link-re-with-space3 nil
  4873. "Matches a link with spaces, only for internal part in bracket links.")
  4874. (defvar org-angle-link-re nil
  4875. "Matches link with angular brackets, spaces are allowed.")
  4876. (defvar org-plain-link-re nil
  4877. "Matches plain link, without spaces.")
  4878. (defvar org-bracket-link-regexp nil
  4879. "Matches a link in double brackets.")
  4880. (defvar org-bracket-link-analytic-regexp nil
  4881. "Regular expression used to analyze links.
  4882. Here is what the match groups contain after a match:
  4883. 1: http:
  4884. 2: http
  4885. 3: path
  4886. 4: [desc]
  4887. 5: desc")
  4888. (defvar org-bracket-link-analytic-regexp++ nil
  4889. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4890. (defvar org-any-link-re nil
  4891. "Regular expression matching any link.")
  4892. (defconst org-match-sexp-depth 3
  4893. "Number of stacked braces for sub/superscript matching.")
  4894. (defun org-create-multibrace-regexp (left right n)
  4895. "Create a regular expression which will match a balanced sexp.
  4896. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4897. as single character strings.
  4898. The regexp returned will match the entire expression including the
  4899. delimiters. It will also define a single group which contains the
  4900. match except for the outermost delimiters. The maximum depth of
  4901. stacked delimiters is N. Escaping delimiters is not possible."
  4902. (let* ((nothing (concat "[^" left right "]*?"))
  4903. (or "\\|")
  4904. (re nothing)
  4905. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4906. (while (> n 1)
  4907. (setq n (1- n)
  4908. re (concat re or next)
  4909. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4910. (concat left "\\(" re "\\)" right)))
  4911. (defvar org-match-substring-regexp
  4912. (concat
  4913. "\\(\\S-\\)\\([_^]\\)\\("
  4914. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4915. "\\|"
  4916. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4917. "\\|"
  4918. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4919. "The regular expression matching a sub- or superscript.")
  4920. (defvar org-match-substring-with-braces-regexp
  4921. (concat
  4922. "\\(\\S-\\)\\([_^]\\)\\("
  4923. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4924. "\\)")
  4925. "The regular expression matching a sub- or superscript, forcing braces.")
  4926. (defun org-make-link-regexps ()
  4927. "Update the link regular expressions.
  4928. This should be called after the variable `org-link-types' has changed."
  4929. (setq org-link-types-re
  4930. (concat
  4931. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4932. org-link-re-with-space
  4933. (concat
  4934. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4935. "\\([^" org-non-link-chars " ]"
  4936. "[^" org-non-link-chars "]*"
  4937. "[^" org-non-link-chars " ]\\)>?")
  4938. org-link-re-with-space2
  4939. (concat
  4940. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4941. "\\([^" org-non-link-chars " ]"
  4942. "[^\t\n\r]*"
  4943. "[^" org-non-link-chars " ]\\)>?")
  4944. org-link-re-with-space3
  4945. (concat
  4946. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4947. "\\([^" org-non-link-chars " ]"
  4948. "[^\t\n\r]*\\)")
  4949. org-angle-link-re
  4950. (concat
  4951. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4952. "\\([^" org-non-link-chars " ]"
  4953. "[^" org-non-link-chars "]*"
  4954. "\\)>")
  4955. org-plain-link-re
  4956. (concat
  4957. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4958. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4959. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4960. org-bracket-link-regexp
  4961. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4962. org-bracket-link-analytic-regexp
  4963. (concat
  4964. "\\[\\["
  4965. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4966. "\\([^]]+\\)"
  4967. "\\]"
  4968. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4969. "\\]")
  4970. org-bracket-link-analytic-regexp++
  4971. (concat
  4972. "\\[\\["
  4973. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4974. "\\([^]]+\\)"
  4975. "\\]"
  4976. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4977. "\\]")
  4978. org-any-link-re
  4979. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4980. org-angle-link-re "\\)\\|\\("
  4981. org-plain-link-re "\\)")))
  4982. (org-make-link-regexps)
  4983. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4984. "Regular expression for fast time stamp matching.")
  4985. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4986. "Regular expression for fast time stamp matching.")
  4987. (defconst org-ts-regexp0
  4988. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4989. "Regular expression matching time strings for analysis.
  4990. This one does not require the space after the date, so it can be used
  4991. on a string that terminates immediately after the date.")
  4992. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4993. "Regular expression matching time strings for analysis.")
  4994. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4995. "Regular expression matching time stamps, with groups.")
  4996. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4997. "Regular expression matching time stamps (also [..]), with groups.")
  4998. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4999. "Regular expression matching a time stamp range.")
  5000. (defconst org-tr-regexp-both
  5001. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  5002. "Regular expression matching a time stamp range.")
  5003. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  5004. org-ts-regexp "\\)?")
  5005. "Regular expression matching a time stamp or time stamp range.")
  5006. (defconst org-tsr-regexp-both
  5007. (concat org-ts-regexp-both "\\(--?-?"
  5008. org-ts-regexp-both "\\)?")
  5009. "Regular expression matching a time stamp or time stamp range.
  5010. The time stamps may be either active or inactive.")
  5011. (defvar org-emph-face nil)
  5012. (defun org-do-emphasis-faces (limit)
  5013. "Run through the buffer and add overlays to emphasized strings."
  5014. (let (rtn a)
  5015. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5016. (if (not (= (char-after (match-beginning 3))
  5017. (char-after (match-beginning 4))))
  5018. (progn
  5019. (setq rtn t)
  5020. (setq a (assoc (match-string 3) org-emphasis-alist))
  5021. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5022. 'face
  5023. (nth 1 a))
  5024. (and (nth 4 a)
  5025. (org-remove-flyspell-overlays-in
  5026. (match-beginning 0) (match-end 0)))
  5027. (add-text-properties (match-beginning 2) (match-end 2)
  5028. '(font-lock-multiline t org-emphasis t))
  5029. (when org-hide-emphasis-markers
  5030. (add-text-properties (match-end 4) (match-beginning 5)
  5031. '(invisible org-link))
  5032. (add-text-properties (match-beginning 3) (match-end 3)
  5033. '(invisible org-link)))))
  5034. (backward-char 1))
  5035. rtn))
  5036. (defun org-emphasize (&optional char)
  5037. "Insert or change an emphasis, i.e. a font like bold or italic.
  5038. If there is an active region, change that region to a new emphasis.
  5039. If there is no region, just insert the marker characters and position
  5040. the cursor between them.
  5041. CHAR should be the marker character. If it is a space, it means to
  5042. remove the emphasis of the selected region.
  5043. If CHAR is not given (for example in an interactive call) it will be
  5044. prompted for."
  5045. (interactive)
  5046. (let ((erc org-emphasis-regexp-components)
  5047. (prompt "")
  5048. (string "") beg end move c s)
  5049. (if (org-region-active-p)
  5050. (setq beg (region-beginning) end (region-end)
  5051. string (buffer-substring beg end))
  5052. (setq move t))
  5053. (unless char
  5054. (message "Emphasis marker or tag: [%s]"
  5055. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5056. (setq char (read-char-exclusive)))
  5057. (if (equal char ?\ )
  5058. (setq s "" move nil)
  5059. (unless (assoc (char-to-string char) org-emphasis-alist)
  5060. (user-error "No such emphasis marker: \"%c\"" char))
  5061. (setq s (char-to-string char)))
  5062. (while (and (> (length string) 1)
  5063. (equal (substring string 0 1) (substring string -1))
  5064. (assoc (substring string 0 1) org-emphasis-alist))
  5065. (setq string (substring string 1 -1)))
  5066. (setq string (concat s string s))
  5067. (if beg (delete-region beg end))
  5068. (unless (or (bolp)
  5069. (string-match (concat "[" (nth 0 erc) "\n]")
  5070. (char-to-string (char-before (point)))))
  5071. (insert " "))
  5072. (unless (or (eobp)
  5073. (string-match (concat "[" (nth 1 erc) "\n]")
  5074. (char-to-string (char-after (point)))))
  5075. (insert " ") (backward-char 1))
  5076. (insert string)
  5077. (and move (backward-char 1))))
  5078. (defconst org-nonsticky-props
  5079. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5080. (defsubst org-rear-nonsticky-at (pos)
  5081. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5082. (defun org-activate-plain-links (limit)
  5083. "Run through the buffer and add overlays to links."
  5084. (let (f hl)
  5085. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5086. (not (org-in-src-block-p)))
  5087. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5088. (setq f (get-text-property (match-beginning 0) 'face))
  5089. (setq hl (org-match-string-no-properties 0))
  5090. (if (or (eq f 'org-tag)
  5091. (and (listp f) (memq 'org-tag f)))
  5092. nil
  5093. (add-text-properties (match-beginning 0) (match-end 0)
  5094. (list 'mouse-face 'highlight
  5095. 'face 'org-link
  5096. 'htmlize-link `(:uri ,hl)
  5097. 'keymap org-mouse-map))
  5098. (org-rear-nonsticky-at (match-end 0)))
  5099. t)))
  5100. (defun org-activate-code (limit)
  5101. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5102. (progn
  5103. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5104. (remove-text-properties (match-beginning 0) (match-end 0)
  5105. '(display t invisible t intangible t))
  5106. t)))
  5107. (defcustom org-src-fontify-natively nil
  5108. "When non-nil, fontify code in code blocks."
  5109. :type 'boolean
  5110. :version "24.1"
  5111. :group 'org-appearance
  5112. :group 'org-babel)
  5113. (defcustom org-allow-promoting-top-level-subtree nil
  5114. "When non-nil, allow promoting a top level subtree.
  5115. The leading star of the top level headline will be replaced
  5116. by a #."
  5117. :type 'boolean
  5118. :version "24.1"
  5119. :group 'org-appearance)
  5120. (defun org-fontify-meta-lines-and-blocks (limit)
  5121. (condition-case nil
  5122. (org-fontify-meta-lines-and-blocks-1 limit)
  5123. (error (message "org-mode fontification error"))))
  5124. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5125. "Fontify #+ lines and blocks."
  5126. (let ((case-fold-search t))
  5127. (if (re-search-forward
  5128. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5129. limit t)
  5130. (let ((beg (match-beginning 0))
  5131. (block-start (match-end 0))
  5132. (block-end nil)
  5133. (lang (match-string 7))
  5134. (beg1 (line-beginning-position 2))
  5135. (dc1 (downcase (match-string 2)))
  5136. (dc3 (downcase (match-string 3)))
  5137. end end1 quoting block-type ovl)
  5138. (cond
  5139. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5140. ;; a single line of backend-specific content
  5141. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5142. (remove-text-properties (match-beginning 0) (match-end 0)
  5143. '(display t invisible t intangible t))
  5144. (add-text-properties (match-beginning 1) (match-end 3)
  5145. '(font-lock-fontified t face org-meta-line))
  5146. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5147. '(font-lock-fontified t face org-block))
  5148. ; for backend-specific code
  5149. t)
  5150. ((and (match-end 4) (equal dc3 "+begin"))
  5151. ;; Truly a block
  5152. (setq block-type (downcase (match-string 5))
  5153. quoting (member block-type org-protecting-blocks))
  5154. (when (re-search-forward
  5155. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5156. nil t) ;; on purpose, we look further than LIMIT
  5157. (setq end (min (point-max) (match-end 0))
  5158. end1 (min (point-max) (1- (match-beginning 0))))
  5159. (setq block-end (match-beginning 0))
  5160. (when quoting
  5161. (remove-text-properties beg end
  5162. '(display t invisible t intangible t)))
  5163. (add-text-properties
  5164. beg end
  5165. '(font-lock-fontified t font-lock-multiline t))
  5166. (add-text-properties beg beg1 '(face org-meta-line))
  5167. (add-text-properties end1 (min (point-max) (1+ end))
  5168. '(face org-meta-line)) ; for end_src
  5169. (cond
  5170. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5171. (org-src-font-lock-fontify-block lang block-start block-end)
  5172. ;; remove old background overlays
  5173. (mapc (lambda (ov)
  5174. (if (eq (overlay-get ov 'face) 'org-block-background)
  5175. (delete-overlay ov)))
  5176. (overlays-at (/ (+ beg1 block-end) 2)))
  5177. ;; add a background overlay
  5178. (setq ovl (make-overlay beg1 block-end))
  5179. (overlay-put ovl 'face 'org-block-background)
  5180. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5181. (quoting
  5182. (add-text-properties beg1 (min (point-max) (1+ end1))
  5183. '(face org-block))) ; end of source block
  5184. ((not org-fontify-quote-and-verse-blocks))
  5185. ((string= block-type "quote")
  5186. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5187. ((string= block-type "verse")
  5188. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5189. (add-text-properties beg beg1 '(face org-block-begin-line))
  5190. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5191. '(face org-block-end-line))
  5192. t))
  5193. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5194. (add-text-properties
  5195. beg (match-end 3)
  5196. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5197. '(font-lock-fontified t invisible t)
  5198. '(font-lock-fontified t face org-document-info-keyword)))
  5199. (add-text-properties
  5200. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5201. (if (string-equal dc1 "+title:")
  5202. '(font-lock-fontified t face org-document-title)
  5203. '(font-lock-fontified t face org-document-info))))
  5204. ((or (equal dc1 "+results")
  5205. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5206. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5207. "+call:" "+header:" "+headers:" "+name:"))
  5208. (and (match-end 4) (equal dc3 "+attr")))
  5209. (add-text-properties
  5210. beg (match-end 0)
  5211. '(font-lock-fontified t face org-meta-line))
  5212. t)
  5213. ((member dc3 '(" " ""))
  5214. (add-text-properties
  5215. beg (match-end 0)
  5216. '(font-lock-fontified t face font-lock-comment-face)))
  5217. ((not (member (char-after beg) '(?\ ?\t)))
  5218. ;; just any other in-buffer setting, but not indented
  5219. (add-text-properties
  5220. beg (match-end 0)
  5221. '(font-lock-fontified t face org-meta-line))
  5222. t)
  5223. (t nil))))))
  5224. (defun org-activate-angle-links (limit)
  5225. "Run through the buffer and add overlays to links."
  5226. (if (and (re-search-forward org-angle-link-re limit t)
  5227. (not (org-in-src-block-p)))
  5228. (progn
  5229. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5230. (add-text-properties (match-beginning 0) (match-end 0)
  5231. (list 'mouse-face 'highlight
  5232. 'keymap org-mouse-map))
  5233. (org-rear-nonsticky-at (match-end 0))
  5234. t)))
  5235. (defun org-activate-footnote-links (limit)
  5236. "Run through the buffer and add overlays to footnotes."
  5237. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5238. (when fn
  5239. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5240. (org-remove-flyspell-overlays-in beg end)
  5241. (add-text-properties beg end
  5242. (list 'mouse-face 'highlight
  5243. 'keymap org-mouse-map
  5244. 'help-echo
  5245. (if (= (point-at-bol) beg)
  5246. "Footnote definition"
  5247. "Footnote reference")
  5248. 'font-lock-fontified t
  5249. 'font-lock-multiline t
  5250. 'face 'org-footnote))))))
  5251. (defun org-activate-bracket-links (limit)
  5252. "Run through the buffer and add overlays to bracketed links."
  5253. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5254. (not (org-in-src-block-p)))
  5255. (let* ((hl (org-match-string-no-properties 1))
  5256. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5257. (ip (org-maybe-intangible
  5258. (list 'invisible 'org-link
  5259. 'keymap org-mouse-map 'mouse-face 'highlight
  5260. 'font-lock-multiline t 'help-echo help
  5261. 'htmlize-link `(:uri ,hl))))
  5262. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5263. 'font-lock-multiline t 'help-echo help
  5264. 'htmlize-link `(:uri ,hl))))
  5265. ;; We need to remove the invisible property here. Table narrowing
  5266. ;; may have made some of this invisible.
  5267. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5268. (remove-text-properties (match-beginning 0) (match-end 0)
  5269. '(invisible nil))
  5270. (if (match-end 3)
  5271. (progn
  5272. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5273. (org-rear-nonsticky-at (match-beginning 3))
  5274. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5275. (org-rear-nonsticky-at (match-end 3))
  5276. (add-text-properties (match-end 3) (match-end 0) ip)
  5277. (org-rear-nonsticky-at (match-end 0)))
  5278. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5279. (org-rear-nonsticky-at (match-beginning 1))
  5280. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5281. (org-rear-nonsticky-at (match-end 1))
  5282. (add-text-properties (match-end 1) (match-end 0) ip)
  5283. (org-rear-nonsticky-at (match-end 0)))
  5284. t)))
  5285. (defun org-activate-dates (limit)
  5286. "Run through the buffer and add overlays to dates."
  5287. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5288. (not (equal (char-before (match-beginning 0)) 91)))
  5289. (progn
  5290. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5291. (add-text-properties (match-beginning 0) (match-end 0)
  5292. (list 'mouse-face 'highlight
  5293. 'keymap org-mouse-map))
  5294. (org-rear-nonsticky-at (match-end 0))
  5295. (when org-display-custom-times
  5296. (if (match-end 3)
  5297. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5298. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5299. t)))
  5300. (defvar org-target-link-regexp nil
  5301. "Regular expression matching radio targets in plain text.")
  5302. (make-variable-buffer-local 'org-target-link-regexp)
  5303. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5304. "Regular expression matching a link target.")
  5305. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5306. "Regular expression matching a radio target.")
  5307. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5308. "Regular expression matching any target.")
  5309. (defun org-activate-target-links (limit)
  5310. "Run through the buffer and add overlays to target matches."
  5311. (when org-target-link-regexp
  5312. (let ((case-fold-search t))
  5313. (if (re-search-forward org-target-link-regexp limit t)
  5314. (progn
  5315. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5316. (add-text-properties (match-beginning 0) (match-end 0)
  5317. (list 'mouse-face 'highlight
  5318. 'keymap org-mouse-map
  5319. 'help-echo "Radio target link"
  5320. 'org-linked-text t))
  5321. (org-rear-nonsticky-at (match-end 0))
  5322. t)))))
  5323. (defun org-update-radio-target-regexp ()
  5324. "Find all radio targets in this file and update the regular expression."
  5325. (interactive)
  5326. (when (memq 'radio org-activate-links)
  5327. (setq org-target-link-regexp
  5328. (org-make-target-link-regexp (org-all-targets 'radio)))
  5329. (org-restart-font-lock)))
  5330. (defun org-hide-wide-columns (limit)
  5331. (let (s e)
  5332. (setq s (text-property-any (point) (or limit (point-max))
  5333. 'org-cwidth t))
  5334. (when s
  5335. (setq e (next-single-property-change s 'org-cwidth))
  5336. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5337. (goto-char e)
  5338. t)))
  5339. (defvar org-latex-and-related-regexp nil
  5340. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5341. (defvar org-match-substring-regexp)
  5342. (defvar org-match-substring-with-braces-regexp)
  5343. (defun org-compute-latex-and-related-regexp ()
  5344. "Compute regular expression for LaTeX, entities and sub/superscript.
  5345. Result depends on variable `org-highlight-latex-and-related'."
  5346. (org-set-local
  5347. 'org-latex-and-related-regexp
  5348. (let* ((re-sub
  5349. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5350. ((eq org-use-sub-superscripts '{})
  5351. (list org-match-substring-with-braces-regexp))
  5352. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5353. (re-latex
  5354. (when (memq 'latex org-highlight-latex-and-related)
  5355. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5356. (delq nil
  5357. (mapcar (lambda (x)
  5358. (and (member (car x) matchers) (nth 1 x)))
  5359. org-latex-regexps)))))
  5360. (re-entities
  5361. (when (memq 'entities org-highlight-latex-and-related)
  5362. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5363. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5364. (defun org-do-latex-and-related (limit)
  5365. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5366. LIMIT bounds the search for syntax to highlight. Stop at first
  5367. highlighted object, if any. Return t if some highlighting was
  5368. done, nil otherwise."
  5369. (when (org-string-nw-p org-latex-and-related-regexp)
  5370. (catch 'found
  5371. (while (re-search-forward org-latex-and-related-regexp limit t)
  5372. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5373. 'face))
  5374. '(org-code org-verbatim underline))
  5375. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5376. '(?_ ?^))
  5377. 1
  5378. 0)))
  5379. (font-lock-prepend-text-property
  5380. (+ offset (match-beginning 0)) (match-end 0)
  5381. 'face 'org-latex-and-related)
  5382. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5383. '(font-lock-multiline t)))
  5384. (throw 'found t)))
  5385. nil)))
  5386. (defun org-restart-font-lock ()
  5387. "Restart `font-lock-mode', to force refontification."
  5388. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5389. (font-lock-mode -1)
  5390. (font-lock-mode 1)))
  5391. (defun org-all-targets (&optional radio)
  5392. "Return a list of all targets in this file.
  5393. When optional argument RADIO is non-nil, only find radio
  5394. targets."
  5395. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5396. (save-excursion
  5397. (goto-char (point-min))
  5398. (while (re-search-forward re nil t)
  5399. ;; Make sure point is really within the object.
  5400. (backward-char)
  5401. (let ((obj (org-element-context)))
  5402. (when (memq (org-element-type obj) '(radio-target target))
  5403. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5404. rtn)))
  5405. (defun org-make-target-link-regexp (targets)
  5406. "Make regular expression matching all strings in TARGETS.
  5407. The regular expression finds the targets also if there is a line break
  5408. between words."
  5409. (and targets
  5410. (concat
  5411. "\\<\\("
  5412. (mapconcat
  5413. (lambda (x)
  5414. (setq x (regexp-quote x))
  5415. (while (string-match " +" x)
  5416. (setq x (replace-match "\\s-+" t t x)))
  5417. x)
  5418. targets
  5419. "\\|")
  5420. "\\)\\>")))
  5421. (defun org-activate-tags (limit)
  5422. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5423. (progn
  5424. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5425. (add-text-properties (match-beginning 1) (match-end 1)
  5426. (list 'mouse-face 'highlight
  5427. 'keymap org-mouse-map))
  5428. (org-rear-nonsticky-at (match-end 1))
  5429. t)))
  5430. (defun org-outline-level ()
  5431. "Compute the outline level of the heading at point.
  5432. If this is called at a normal headline, the level is the number of stars.
  5433. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5434. (save-excursion
  5435. (if (not (condition-case nil
  5436. (org-back-to-heading t)
  5437. (error nil)))
  5438. 0
  5439. (looking-at org-outline-regexp)
  5440. (1- (- (match-end 0) (match-beginning 0))))))
  5441. (defvar org-font-lock-keywords nil)
  5442. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5443. "Regular expression matching a property line.")
  5444. (defvar org-font-lock-hook nil
  5445. "Functions to be called for special font lock stuff.")
  5446. (defvar org-font-lock-set-keywords-hook nil
  5447. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5448. This is called after `org-font-lock-extra-keywords' is defined, but before
  5449. it is installed to be used by font lock. This can be useful if something
  5450. needs to be inserted at a specific position in the font-lock sequence.")
  5451. (defun org-font-lock-hook (limit)
  5452. "Run `org-font-lock-hook' within LIMIT."
  5453. (run-hook-with-args 'org-font-lock-hook limit))
  5454. (defun org-set-font-lock-defaults ()
  5455. "Set font lock defaults for the current buffer."
  5456. (let* ((em org-fontify-emphasized-text)
  5457. (lk org-activate-links)
  5458. (org-font-lock-extra-keywords
  5459. (list
  5460. ;; Call the hook
  5461. '(org-font-lock-hook)
  5462. ;; Headlines
  5463. `(,(if org-fontify-whole-heading-line
  5464. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5465. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5466. (1 (org-get-level-face 1))
  5467. (2 (org-get-level-face 2))
  5468. (3 (org-get-level-face 3)))
  5469. ;; Table lines
  5470. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5471. (1 'org-table t))
  5472. ;; Table internals
  5473. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5474. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5475. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5476. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5477. ;; Drawers
  5478. (list org-drawer-regexp '(0 'org-special-keyword t))
  5479. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5480. ;; Properties
  5481. (list org-property-re
  5482. '(1 'org-special-keyword t)
  5483. '(3 'org-property-value t))
  5484. ;; Links
  5485. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5486. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5487. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5488. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5489. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5490. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5491. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5492. ;; Targets.
  5493. (list org-any-target-regexp '(0 'org-target t))
  5494. ;; Diary sexps.
  5495. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5496. ;; Macro
  5497. '("{{{.+}}}" (0 'org-macro t))
  5498. '(org-hide-wide-columns (0 nil append))
  5499. ;; TODO keyword
  5500. (list (format org-heading-keyword-regexp-format
  5501. org-todo-regexp)
  5502. '(2 (org-get-todo-face 2) t))
  5503. ;; DONE
  5504. (if org-fontify-done-headline
  5505. (list (format org-heading-keyword-regexp-format
  5506. (concat
  5507. "\\(?:"
  5508. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5509. "\\)"))
  5510. '(2 'org-headline-done t))
  5511. nil)
  5512. ;; Priorities
  5513. '(org-font-lock-add-priority-faces)
  5514. ;; Tags
  5515. '(org-font-lock-add-tag-faces)
  5516. ;; Tags groups
  5517. (if (and org-group-tags org-tag-groups-alist)
  5518. (list (concat org-outline-regexp-bol ".+\\(:"
  5519. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5520. ":\\).*$")
  5521. '(1 'org-tag-group prepend)))
  5522. ;; Special keywords
  5523. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5524. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5525. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5526. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5527. ;; Emphasis
  5528. (if em
  5529. (if (featurep 'xemacs)
  5530. '(org-do-emphasis-faces (0 nil append))
  5531. '(org-do-emphasis-faces)))
  5532. ;; Checkboxes
  5533. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5534. 1 'org-checkbox prepend)
  5535. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5536. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5537. (0 (org-get-checkbox-statistics-face) t)))
  5538. ;; Description list items
  5539. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5540. 1 'org-list-dt prepend)
  5541. ;; ARCHIVEd headings
  5542. (list (concat
  5543. org-outline-regexp-bol
  5544. "\\(.*:" org-archive-tag ":.*\\)")
  5545. '(1 'org-archived prepend))
  5546. ;; Specials
  5547. '(org-do-latex-and-related)
  5548. '(org-fontify-entities)
  5549. '(org-raise-scripts)
  5550. ;; Code
  5551. '(org-activate-code (1 'org-code t))
  5552. ;; COMMENT
  5553. (list (format org-heading-keyword-regexp-format
  5554. (concat "\\("
  5555. org-comment-string "\\|" org-quote-string
  5556. "\\)"))
  5557. '(2 'org-special-keyword t))
  5558. ;; Blocks and meta lines
  5559. '(org-fontify-meta-lines-and-blocks))))
  5560. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5561. (run-hooks 'org-font-lock-set-keywords-hook)
  5562. ;; Now set the full font-lock-keywords
  5563. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5564. (org-set-local 'font-lock-defaults
  5565. '(org-font-lock-keywords t nil nil backward-paragraph))
  5566. (kill-local-variable 'font-lock-keywords) nil))
  5567. (defun org-toggle-pretty-entities ()
  5568. "Toggle the composition display of entities as UTF8 characters."
  5569. (interactive)
  5570. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5571. (org-restart-font-lock)
  5572. (if org-pretty-entities
  5573. (message "Entities are now displayed as UTF8 characters")
  5574. (save-restriction
  5575. (widen)
  5576. (org-decompose-region (point-min) (point-max))
  5577. (message "Entities are now displayed as plain text"))))
  5578. (defvar org-custom-properties-overlays nil
  5579. "List of overlays used for custom properties.")
  5580. (make-variable-buffer-local 'org-custom-properties-overlays)
  5581. (defun org-toggle-custom-properties-visibility ()
  5582. "Display or hide properties in `org-custom-properties'."
  5583. (interactive)
  5584. (if org-custom-properties-overlays
  5585. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5586. (setq org-custom-properties-overlays nil))
  5587. (unless (not org-custom-properties)
  5588. (save-excursion
  5589. (save-restriction
  5590. (widen)
  5591. (goto-char (point-min))
  5592. (while (re-search-forward org-property-re nil t)
  5593. (mapc (lambda(p)
  5594. (when (equal p (substring (match-string 1) 1 -1))
  5595. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5596. (overlay-put o 'invisible t)
  5597. (overlay-put o 'org-custom-property t)
  5598. (push o org-custom-properties-overlays))))
  5599. org-custom-properties)))))))
  5600. (defun org-fontify-entities (limit)
  5601. "Find an entity to fontify."
  5602. (let (ee)
  5603. (when org-pretty-entities
  5604. (catch 'match
  5605. (while (re-search-forward
  5606. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5607. limit t)
  5608. (if (and (not (org-in-indented-comment-line))
  5609. (setq ee (org-entity-get (match-string 1)))
  5610. (= (length (nth 6 ee)) 1))
  5611. (let*
  5612. ((end (if (equal (match-string 2) "{}")
  5613. (match-end 2)
  5614. (match-end 1))))
  5615. (add-text-properties
  5616. (match-beginning 0) end
  5617. (list 'font-lock-fontified t))
  5618. (compose-region (match-beginning 0) end
  5619. (nth 6 ee) nil)
  5620. (backward-char 1)
  5621. (throw 'match t))))
  5622. nil))))
  5623. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5624. "Fontify string S like in Org-mode."
  5625. (with-temp-buffer
  5626. (insert s)
  5627. (let ((org-odd-levels-only odd-levels))
  5628. (org-mode)
  5629. (font-lock-fontify-buffer)
  5630. (buffer-string))))
  5631. (defvar org-m nil)
  5632. (defvar org-l nil)
  5633. (defvar org-f nil)
  5634. (defun org-get-level-face (n)
  5635. "Get the right face for match N in font-lock matching of headlines."
  5636. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5637. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5638. (if org-cycle-level-faces
  5639. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5640. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5641. (cond
  5642. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5643. ((eq n 2) org-f)
  5644. (t (if org-level-color-stars-only nil org-f))))
  5645. (defun org-get-todo-face (kwd)
  5646. "Get the right face for a TODO keyword KWD.
  5647. If KWD is a number, get the corresponding match group."
  5648. (if (numberp kwd) (setq kwd (match-string kwd)))
  5649. (or (org-face-from-face-or-color
  5650. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5651. (and (member kwd org-done-keywords) 'org-done)
  5652. 'org-todo))
  5653. (defun org-face-from-face-or-color (context inherit face-or-color)
  5654. "Create a face list that inherits INHERIT, but sets the foreground color.
  5655. When FACE-OR-COLOR is not a string, just return it."
  5656. (if (stringp face-or-color)
  5657. (list :inherit inherit
  5658. (cdr (assoc context org-faces-easy-properties))
  5659. face-or-color)
  5660. face-or-color))
  5661. (defun org-font-lock-add-tag-faces (limit)
  5662. "Add the special tag faces."
  5663. (when (and org-tag-faces org-tags-special-faces-re)
  5664. (while (re-search-forward org-tags-special-faces-re limit t)
  5665. (add-text-properties (match-beginning 1) (match-end 1)
  5666. (list 'face (org-get-tag-face 1)
  5667. 'font-lock-fontified t))
  5668. (backward-char 1))))
  5669. (defun org-font-lock-add-priority-faces (limit)
  5670. "Add the special priority faces."
  5671. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5672. (when (save-match-data (org-at-heading-p))
  5673. (add-text-properties
  5674. (match-beginning 0) (match-end 0)
  5675. (list 'face (or (org-face-from-face-or-color
  5676. 'priority 'org-priority
  5677. (cdr (assoc (char-after (match-beginning 1))
  5678. org-priority-faces)))
  5679. 'org-priority)
  5680. 'font-lock-fontified t)))))
  5681. (defun org-get-tag-face (kwd)
  5682. "Get the right face for a TODO keyword KWD.
  5683. If KWD is a number, get the corresponding match group."
  5684. (if (numberp kwd) (setq kwd (match-string kwd)))
  5685. (or (org-face-from-face-or-color
  5686. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5687. 'org-tag))
  5688. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5689. "Remove fontification and activation overlays from links."
  5690. (font-lock-default-unfontify-region beg end)
  5691. (let* ((buffer-undo-list t)
  5692. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5693. (inhibit-modification-hooks t)
  5694. deactivate-mark buffer-file-name buffer-file-truename)
  5695. (org-decompose-region beg end)
  5696. (remove-text-properties beg end
  5697. '(mouse-face t keymap t org-linked-text t
  5698. invisible t intangible t
  5699. org-no-flyspell t org-emphasis t))
  5700. (org-remove-font-lock-display-properties beg end)))
  5701. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5702. ((raise 0.3) (height 0.7))
  5703. ((raise -0.5))
  5704. ((raise 0.5)))
  5705. "Display properties for showing superscripts and subscripts.")
  5706. (defun org-remove-font-lock-display-properties (beg end)
  5707. "Remove specific display properties that have been added by font lock.
  5708. The will remove the raise properties that are used to show superscripts
  5709. and subscripts."
  5710. (let (next prop)
  5711. (while (< beg end)
  5712. (setq next (next-single-property-change beg 'display nil end)
  5713. prop (get-text-property beg 'display))
  5714. (if (member prop org-script-display)
  5715. (put-text-property beg next 'display nil))
  5716. (setq beg next))))
  5717. (defun org-raise-scripts (limit)
  5718. "Add raise properties to sub/superscripts."
  5719. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5720. (if (re-search-forward
  5721. (if (eq org-use-sub-superscripts t)
  5722. org-match-substring-regexp
  5723. org-match-substring-with-braces-regexp)
  5724. limit t)
  5725. (let* ((pos (point)) table-p comment-p
  5726. (mpos (match-beginning 3))
  5727. (emph-p (get-text-property mpos 'org-emphasis))
  5728. (link-p (get-text-property mpos 'mouse-face))
  5729. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5730. (goto-char (point-at-bol))
  5731. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5732. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5733. (goto-char pos)
  5734. ;; Handle a_b^c
  5735. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5736. (if (or comment-p emph-p link-p keyw-p)
  5737. t
  5738. (put-text-property (match-beginning 3) (match-end 0)
  5739. 'display
  5740. (if (equal (char-after (match-beginning 2)) ?^)
  5741. (nth (if table-p 3 1) org-script-display)
  5742. (nth (if table-p 2 0) org-script-display)))
  5743. (add-text-properties (match-beginning 2) (match-end 2)
  5744. (list 'invisible t
  5745. 'org-dwidth t 'org-dwidth-n 1))
  5746. (if (and (eq (char-after (match-beginning 3)) ?{)
  5747. (eq (char-before (match-end 3)) ?}))
  5748. (progn
  5749. (add-text-properties
  5750. (match-beginning 3) (1+ (match-beginning 3))
  5751. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5752. (add-text-properties
  5753. (1- (match-end 3)) (match-end 3)
  5754. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5755. t)))))
  5756. ;;;; Visibility cycling, including org-goto and indirect buffer
  5757. ;;; Cycling
  5758. (defvar org-cycle-global-status nil)
  5759. (make-variable-buffer-local 'org-cycle-global-status)
  5760. (put 'org-cycle-global-status 'org-state t)
  5761. (defvar org-cycle-subtree-status nil)
  5762. (make-variable-buffer-local 'org-cycle-subtree-status)
  5763. (put 'org-cycle-subtree-status 'org-state t)
  5764. (defvar org-inlinetask-min-level)
  5765. ;;;###autoload
  5766. (defun org-cycle (&optional arg)
  5767. "TAB-action and visibility cycling for Org-mode.
  5768. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5769. is outline visibility cycling, but it also invokes other actions
  5770. in special contexts.
  5771. - When this function is called with a prefix argument, rotate the entire
  5772. buffer through 3 states (global cycling)
  5773. 1. OVERVIEW: Show only top-level headlines.
  5774. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5775. 3. SHOW ALL: Show everything.
  5776. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5777. determined by the variable `org-startup-folded', and by any VISIBILITY
  5778. properties in the buffer.
  5779. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5780. including any drawers.
  5781. - When inside a table, re-align the table and move to the next field.
  5782. - When point is at the beginning of a headline, rotate the subtree started
  5783. by this line through 3 different states (local cycling)
  5784. 1. FOLDED: Only the main headline is shown.
  5785. 2. CHILDREN: The main headline and the direct children are shown.
  5786. From this state, you can move to one of the children
  5787. and zoom in further.
  5788. 3. SUBTREE: Show the entire subtree, including body text.
  5789. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5790. - When point is at the beginning of an empty headline and the variable
  5791. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5792. of the headline by demoting and promoting it to likely levels. This
  5793. speeds up creation document structure by pressing TAB once or several
  5794. times right after creating a new headline.
  5795. - When there is a numeric prefix, go up to a heading with level ARG, do
  5796. a `show-subtree' and return to the previous cursor position. If ARG
  5797. is negative, go up that many levels.
  5798. - When point is not at the beginning of a headline, execute the global
  5799. binding for TAB, which is re-indenting the line. See the option
  5800. `org-cycle-emulate-tab' for details.
  5801. - Special case: if point is at the beginning of the buffer and there is
  5802. no headline in line 1, this function will act as if called with prefix arg
  5803. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5804. But only if also the variable `org-cycle-global-at-bob' is t."
  5805. (interactive "P")
  5806. (org-load-modules-maybe)
  5807. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5808. (and org-cycle-level-after-item/entry-creation
  5809. (or (org-cycle-level)
  5810. (org-cycle-item-indentation))))
  5811. (let* (message-log-max ; Don't populate the *Messages* buffer
  5812. (limit-level
  5813. (or org-cycle-max-level
  5814. (and (boundp 'org-inlinetask-min-level)
  5815. org-inlinetask-min-level
  5816. (1- org-inlinetask-min-level))))
  5817. (nstars (and limit-level
  5818. (if org-odd-levels-only
  5819. (and limit-level (1- (* limit-level 2)))
  5820. limit-level)))
  5821. (org-outline-regexp
  5822. (if (not (derived-mode-p 'org-mode))
  5823. outline-regexp
  5824. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5825. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5826. (not (looking-at org-outline-regexp))))
  5827. (org-cycle-hook
  5828. (if bob-special
  5829. (delq 'org-optimize-window-after-visibility-change
  5830. (copy-sequence org-cycle-hook))
  5831. org-cycle-hook))
  5832. (pos (point)))
  5833. (if (or bob-special (equal arg '(4)))
  5834. ;; special case: use global cycling
  5835. (setq arg t))
  5836. (cond
  5837. ((equal arg '(16))
  5838. (setq last-command 'dummy)
  5839. (org-set-startup-visibility)
  5840. (message "Startup visibility, plus VISIBILITY properties"))
  5841. ((equal arg '(64))
  5842. (show-all)
  5843. (message "Entire buffer visible, including drawers"))
  5844. ;; Table: enter it or move to the next field.
  5845. ((org-at-table-p 'any)
  5846. (if (org-at-table.el-p)
  5847. (message "Use C-c ' to edit table.el tables")
  5848. (if arg (org-table-edit-field t)
  5849. (org-table-justify-field-maybe)
  5850. (call-interactively 'org-table-next-field))))
  5851. ((run-hook-with-args-until-success
  5852. 'org-tab-after-check-for-table-hook))
  5853. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5854. ((eq arg t) (org-cycle-internal-global))
  5855. ;; Drawers: delegate to `org-flag-drawer'.
  5856. ((and org-drawers org-drawer-regexp
  5857. (save-excursion
  5858. (beginning-of-line 1)
  5859. (looking-at org-drawer-regexp)))
  5860. (org-flag-drawer ; toggle block visibility
  5861. (not (get-char-property (match-end 0) 'invisible))))
  5862. ;; Show-subtree, ARG levels up from here.
  5863. ((integerp arg)
  5864. (save-excursion
  5865. (org-back-to-heading)
  5866. (outline-up-heading (if (< arg 0) (- arg)
  5867. (- (funcall outline-level) arg)))
  5868. (org-show-subtree)))
  5869. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5870. ((and (featurep 'org-inlinetask)
  5871. (org-inlinetask-at-task-p)
  5872. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5873. (org-inlinetask-toggle-visibility))
  5874. ((org-try-cdlatex-tab))
  5875. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5876. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5877. (save-excursion (beginning-of-line 1)
  5878. (looking-at org-outline-regexp)))
  5879. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5880. (org-cycle-internal-local))
  5881. ;; From there: TAB emulation and template completion.
  5882. (buffer-read-only (org-back-to-heading))
  5883. ((run-hook-with-args-until-success
  5884. 'org-tab-after-check-for-cycling-hook))
  5885. ((org-try-structure-completion))
  5886. ((run-hook-with-args-until-success
  5887. 'org-tab-before-tab-emulation-hook))
  5888. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5889. (or (not (bolp))
  5890. (not (looking-at org-outline-regexp))))
  5891. (call-interactively (global-key-binding "\t")))
  5892. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5893. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5894. (or (and (eq org-cycle-emulate-tab 'white)
  5895. (= (match-end 0) (point-at-eol)))
  5896. (and (eq org-cycle-emulate-tab 'whitestart)
  5897. (>= (match-end 0) pos))))
  5898. t
  5899. (eq org-cycle-emulate-tab t))
  5900. (call-interactively (global-key-binding "\t")))
  5901. (t (save-excursion
  5902. (org-back-to-heading)
  5903. (org-cycle)))))))
  5904. (defun org-cycle-internal-global ()
  5905. "Do the global cycling action."
  5906. ;; Hack to avoid display of messages for .org attachments in Gnus
  5907. (let (message-log-max ; Don't populate the *Messages* buffer
  5908. (ga (string-match "\\*fontification" (buffer-name))))
  5909. (cond
  5910. ((and (eq last-command this-command)
  5911. (eq org-cycle-global-status 'overview))
  5912. ;; We just created the overview - now do table of contents
  5913. ;; This can be slow in very large buffers, so indicate action
  5914. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5915. (unless ga (message "CONTENTS..."))
  5916. (org-content)
  5917. (unless ga (message "CONTENTS...done"))
  5918. (setq org-cycle-global-status 'contents)
  5919. (run-hook-with-args 'org-cycle-hook 'contents))
  5920. ((and (eq last-command this-command)
  5921. (eq org-cycle-global-status 'contents))
  5922. ;; We just showed the table of contents - now show everything
  5923. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5924. (show-all)
  5925. (unless ga (message "SHOW ALL"))
  5926. (setq org-cycle-global-status 'all)
  5927. (run-hook-with-args 'org-cycle-hook 'all))
  5928. (t
  5929. ;; Default action: go to overview
  5930. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5931. (org-overview)
  5932. (unless ga (message "OVERVIEW"))
  5933. (setq org-cycle-global-status 'overview)
  5934. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5935. (defun org-cycle-internal-local ()
  5936. "Do the local cycling action."
  5937. (let (message-log-max ; Don't populate the *Messages* buffer
  5938. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5939. ;; First, determine end of headline (EOH), end of subtree or item
  5940. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5941. (save-excursion
  5942. (if (org-at-item-p)
  5943. (progn
  5944. (beginning-of-line)
  5945. (setq struct (org-list-struct))
  5946. (setq eoh (point-at-eol))
  5947. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5948. (setq has-children (org-list-has-child-p (point) struct)))
  5949. (org-back-to-heading)
  5950. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5951. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5952. (setq has-children
  5953. (or (save-excursion
  5954. (let ((level (funcall outline-level)))
  5955. (outline-next-heading)
  5956. (and (org-at-heading-p t)
  5957. (> (funcall outline-level) level))))
  5958. (save-excursion
  5959. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5960. ;; Determine end invisible part of buffer (EOL)
  5961. (beginning-of-line 2)
  5962. ;; XEmacs doesn't have `next-single-char-property-change'
  5963. (if (featurep 'xemacs)
  5964. (while (and (not (eobp)) ;; this is like `next-line'
  5965. (get-char-property (1- (point)) 'invisible))
  5966. (beginning-of-line 2))
  5967. (while (and (not (eobp)) ;; this is like `next-line'
  5968. (get-char-property (1- (point)) 'invisible))
  5969. (goto-char (next-single-char-property-change (point) 'invisible))
  5970. (and (eolp) (beginning-of-line 2))))
  5971. (setq eol (point)))
  5972. ;; Find out what to do next and set `this-command'
  5973. (cond
  5974. ((= eos eoh)
  5975. ;; Nothing is hidden behind this heading
  5976. (unless (org-before-first-heading-p)
  5977. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5978. (message "EMPTY ENTRY")
  5979. (setq org-cycle-subtree-status nil)
  5980. (save-excursion
  5981. (goto-char eos)
  5982. (outline-next-heading)
  5983. (if (outline-invisible-p) (org-flag-heading nil))))
  5984. ((and (or (>= eol eos)
  5985. (not (string-match "\\S-" (buffer-substring eol eos))))
  5986. (or has-children
  5987. (not (setq children-skipped
  5988. org-cycle-skip-children-state-if-no-children))))
  5989. ;; Entire subtree is hidden in one line: children view
  5990. (unless (org-before-first-heading-p)
  5991. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5992. (if (org-at-item-p)
  5993. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5994. (org-show-entry)
  5995. (org-with-limited-levels (show-children))
  5996. ;; FIXME: This slows down the func way too much.
  5997. ;; How keep drawers hidden in subtree anyway?
  5998. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5999. ;; (org-cycle-hide-drawers 'subtree))
  6000. ;; Fold every list in subtree to top-level items.
  6001. (when (eq org-cycle-include-plain-lists 'integrate)
  6002. (save-excursion
  6003. (org-back-to-heading)
  6004. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6005. (beginning-of-line 1)
  6006. (let* ((struct (org-list-struct))
  6007. (prevs (org-list-prevs-alist struct))
  6008. (end (org-list-get-bottom-point struct)))
  6009. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6010. (org-list-get-all-items (point) struct prevs))
  6011. (goto-char (if (< end eos) end eos)))))))
  6012. (message "CHILDREN")
  6013. (save-excursion
  6014. (goto-char eos)
  6015. (outline-next-heading)
  6016. (if (outline-invisible-p) (org-flag-heading nil)))
  6017. (setq org-cycle-subtree-status 'children)
  6018. (unless (org-before-first-heading-p)
  6019. (run-hook-with-args 'org-cycle-hook 'children)))
  6020. ((or children-skipped
  6021. (and (eq last-command this-command)
  6022. (eq org-cycle-subtree-status 'children)))
  6023. ;; We just showed the children, or no children are there,
  6024. ;; now show everything.
  6025. (unless (org-before-first-heading-p)
  6026. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6027. (outline-flag-region eoh eos nil)
  6028. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6029. (setq org-cycle-subtree-status 'subtree)
  6030. (unless (org-before-first-heading-p)
  6031. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6032. (t
  6033. ;; Default action: hide the subtree.
  6034. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6035. (outline-flag-region eoh eos t)
  6036. (message "FOLDED")
  6037. (setq org-cycle-subtree-status 'folded)
  6038. (unless (org-before-first-heading-p)
  6039. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6040. ;;;###autoload
  6041. (defun org-global-cycle (&optional arg)
  6042. "Cycle the global visibility. For details see `org-cycle'.
  6043. With \\[universal-argument] prefix arg, switch to startup visibility.
  6044. With a numeric prefix, show all headlines up to that level."
  6045. (interactive "P")
  6046. (let ((org-cycle-include-plain-lists
  6047. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6048. (cond
  6049. ((integerp arg)
  6050. (show-all)
  6051. (hide-sublevels arg)
  6052. (setq org-cycle-global-status 'contents))
  6053. ((equal arg '(4))
  6054. (org-set-startup-visibility)
  6055. (message "Startup visibility, plus VISIBILITY properties."))
  6056. (t
  6057. (org-cycle '(4))))))
  6058. (defun org-set-startup-visibility ()
  6059. "Set the visibility required by startup options and properties."
  6060. (cond
  6061. ((eq org-startup-folded t)
  6062. (org-cycle '(4)))
  6063. ((eq org-startup-folded 'content)
  6064. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6065. (org-cycle '(4)) (org-cycle '(4)))))
  6066. (unless (eq org-startup-folded 'showeverything)
  6067. (if org-hide-block-startup (org-hide-block-all))
  6068. (org-set-visibility-according-to-property 'no-cleanup)
  6069. (org-cycle-hide-archived-subtrees 'all)
  6070. (org-cycle-hide-drawers 'all)
  6071. (org-cycle-show-empty-lines t)))
  6072. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6073. "Switch subtree visibilities according to :VISIBILITY: property."
  6074. (interactive)
  6075. (let (org-show-entry-below state)
  6076. (save-excursion
  6077. (goto-char (point-min))
  6078. (while (re-search-forward
  6079. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6080. nil t)
  6081. (setq state (match-string 1))
  6082. (save-excursion
  6083. (org-back-to-heading t)
  6084. (hide-subtree)
  6085. (org-reveal)
  6086. (cond
  6087. ((equal state '("fold" "folded"))
  6088. (hide-subtree))
  6089. ((equal state "children")
  6090. (org-show-hidden-entry)
  6091. (show-children))
  6092. ((equal state "content")
  6093. (save-excursion
  6094. (save-restriction
  6095. (org-narrow-to-subtree)
  6096. (org-content))))
  6097. ((member state '("all" "showall"))
  6098. (show-subtree)))))
  6099. (unless no-cleanup
  6100. (org-cycle-hide-archived-subtrees 'all)
  6101. (org-cycle-hide-drawers 'all)
  6102. (org-cycle-show-empty-lines 'all)))))
  6103. ;; This function uses outline-regexp instead of the more fundamental
  6104. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6105. ;; buffers, where outline-regexp is needed.
  6106. (defun org-overview ()
  6107. "Switch to overview mode, showing only top-level headlines.
  6108. Really, this shows all headlines with level equal or greater than the level
  6109. of the first headline in the buffer. This is important, because if the
  6110. first headline is not level one, then (hide-sublevels 1) gives confusing
  6111. results."
  6112. (interactive)
  6113. (let ((l (org-current-line))
  6114. (level (save-excursion
  6115. (goto-char (point-min))
  6116. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6117. (progn
  6118. (goto-char (match-beginning 0))
  6119. (funcall outline-level))))))
  6120. (and level (hide-sublevels level))
  6121. (recenter '(4))
  6122. (org-goto-line l)))
  6123. (defun org-content (&optional arg)
  6124. "Show all headlines in the buffer, like a table of contents.
  6125. With numerical argument N, show content up to level N."
  6126. (interactive "P")
  6127. (save-excursion
  6128. ;; Visit all headings and show their offspring
  6129. (and (integerp arg) (org-overview))
  6130. (goto-char (point-max))
  6131. (catch 'exit
  6132. (while (and (progn (condition-case nil
  6133. (outline-previous-visible-heading 1)
  6134. (error (goto-char (point-min))))
  6135. t)
  6136. (looking-at org-outline-regexp))
  6137. (if (integerp arg)
  6138. (show-children (1- arg))
  6139. (show-branches))
  6140. (if (bobp) (throw 'exit nil))))))
  6141. (defun org-optimize-window-after-visibility-change (state)
  6142. "Adjust the window after a change in outline visibility.
  6143. This function is the default value of the hook `org-cycle-hook'."
  6144. (when (get-buffer-window (current-buffer))
  6145. (cond
  6146. ((eq state 'content) nil)
  6147. ((eq state 'all) nil)
  6148. ((eq state 'folded) nil)
  6149. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6150. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6151. (defun org-remove-empty-overlays-at (pos)
  6152. "Remove outline overlays that do not contain non-white stuff."
  6153. (mapc
  6154. (lambda (o)
  6155. (and (eq 'outline (overlay-get o 'invisible))
  6156. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6157. (overlay-end o))))
  6158. (delete-overlay o)))
  6159. (overlays-at pos)))
  6160. (defun org-clean-visibility-after-subtree-move ()
  6161. "Fix visibility issues after moving a subtree."
  6162. ;; First, find a reasonable region to look at:
  6163. ;; Start two siblings above, end three below
  6164. (let* ((beg (save-excursion
  6165. (and (org-get-last-sibling)
  6166. (org-get-last-sibling))
  6167. (point)))
  6168. (end (save-excursion
  6169. (and (org-get-next-sibling)
  6170. (org-get-next-sibling)
  6171. (org-get-next-sibling))
  6172. (if (org-at-heading-p)
  6173. (point-at-eol)
  6174. (point))))
  6175. (level (looking-at "\\*+"))
  6176. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6177. (save-excursion
  6178. (save-restriction
  6179. (narrow-to-region beg end)
  6180. (when re
  6181. ;; Properly fold already folded siblings
  6182. (goto-char (point-min))
  6183. (while (re-search-forward re nil t)
  6184. (if (and (not (outline-invisible-p))
  6185. (save-excursion
  6186. (goto-char (point-at-eol)) (outline-invisible-p)))
  6187. (hide-entry))))
  6188. (org-cycle-show-empty-lines 'overview)
  6189. (org-cycle-hide-drawers 'overview)))))
  6190. (defun org-cycle-show-empty-lines (state)
  6191. "Show empty lines above all visible headlines.
  6192. The region to be covered depends on STATE when called through
  6193. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6194. entire buffer covered. Note that an empty line is only shown if there
  6195. are at least `org-cycle-separator-lines' empty lines before the headline."
  6196. (when (not (= org-cycle-separator-lines 0))
  6197. (save-excursion
  6198. (let* ((n (abs org-cycle-separator-lines))
  6199. (re (cond
  6200. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6201. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6202. (t (let ((ns (number-to-string (- n 2))))
  6203. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6204. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6205. beg end b e)
  6206. (cond
  6207. ((memq state '(overview contents t))
  6208. (setq beg (point-min) end (point-max)))
  6209. ((memq state '(children folded))
  6210. (setq beg (point) end (progn (org-end-of-subtree t t)
  6211. (beginning-of-line 2)
  6212. (point)))))
  6213. (when beg
  6214. (goto-char beg)
  6215. (while (re-search-forward re end t)
  6216. (unless (get-char-property (match-end 1) 'invisible)
  6217. (setq e (match-end 1))
  6218. (if (< org-cycle-separator-lines 0)
  6219. (setq b (save-excursion
  6220. (goto-char (match-beginning 0))
  6221. (org-back-over-empty-lines)
  6222. (if (save-excursion
  6223. (goto-char (max (point-min) (1- (point))))
  6224. (org-at-heading-p))
  6225. (1- (point))
  6226. (point))))
  6227. (setq b (match-beginning 1)))
  6228. (outline-flag-region b e nil)))))))
  6229. ;; Never hide empty lines at the end of the file.
  6230. (save-excursion
  6231. (goto-char (point-max))
  6232. (outline-previous-heading)
  6233. (outline-end-of-heading)
  6234. (if (and (looking-at "[ \t\n]+")
  6235. (= (match-end 0) (point-max)))
  6236. (outline-flag-region (point) (match-end 0) nil))))
  6237. (defun org-show-empty-lines-in-parent ()
  6238. "Move to the parent and re-show empty lines before visible headlines."
  6239. (save-excursion
  6240. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6241. (org-cycle-show-empty-lines context))))
  6242. (defun org-files-list ()
  6243. "Return `org-agenda-files' list, plus all open org-mode files.
  6244. This is useful for operations that need to scan all of a user's
  6245. open and agenda-wise Org files."
  6246. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6247. (dolist (buf (buffer-list))
  6248. (with-current-buffer buf
  6249. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6250. (let ((file (expand-file-name (buffer-file-name))))
  6251. (unless (member file files)
  6252. (push file files))))))
  6253. files))
  6254. (defsubst org-entry-beginning-position ()
  6255. "Return the beginning position of the current entry."
  6256. (save-excursion (outline-back-to-heading t) (point)))
  6257. (defsubst org-entry-end-position ()
  6258. "Return the end position of the current entry."
  6259. (save-excursion (outline-next-heading) (point)))
  6260. (defun org-cycle-hide-drawers (state)
  6261. "Re-hide all drawers after a visibility state change."
  6262. (when (and (derived-mode-p 'org-mode)
  6263. (not (memq state '(overview folded contents))))
  6264. (save-excursion
  6265. (let* ((globalp (memq state '(contents all)))
  6266. (beg (if globalp (point-min) (point)))
  6267. (end (if globalp (point-max)
  6268. (if (eq state 'children)
  6269. (save-excursion (outline-next-heading) (point))
  6270. (org-end-of-subtree t)))))
  6271. (goto-char beg)
  6272. (while (re-search-forward org-drawer-regexp end t)
  6273. (org-flag-drawer t))))))
  6274. (defun org-cycle-hide-inline-tasks (state)
  6275. "Re-hide inline task when switching to 'contents visibility state."
  6276. (when (and (eq state 'contents)
  6277. (boundp 'org-inlinetask-min-level)
  6278. org-inlinetask-min-level)
  6279. (hide-sublevels (1- org-inlinetask-min-level))))
  6280. (defun org-flag-drawer (flag)
  6281. "When FLAG is non-nil, hide the drawer we are within.
  6282. Otherwise make it visible."
  6283. (save-excursion
  6284. (beginning-of-line 1)
  6285. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6286. (let ((b (match-end 0)))
  6287. (if (re-search-forward
  6288. "^[ \t]*:END:"
  6289. (save-excursion (outline-next-heading) (point)) t)
  6290. (outline-flag-region b (point-at-eol) flag)
  6291. (user-error ":END: line missing at position %s" b))))))
  6292. (defun org-subtree-end-visible-p ()
  6293. "Is the end of the current subtree visible?"
  6294. (pos-visible-in-window-p
  6295. (save-excursion (org-end-of-subtree t) (point))))
  6296. (defun org-first-headline-recenter (&optional N)
  6297. "Move cursor to the first headline and recenter the headline.
  6298. Optional argument N means put the headline into the Nth line of the window."
  6299. (goto-char (point-min))
  6300. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6301. (beginning-of-line)
  6302. (recenter (prefix-numeric-value N))))
  6303. ;;; Saving and restoring visibility
  6304. (defun org-outline-overlay-data (&optional use-markers)
  6305. "Return a list of the locations of all outline overlays.
  6306. These are overlays with the `invisible' property value `outline'.
  6307. The return value is a list of cons cells, with start and stop
  6308. positions for each overlay.
  6309. If USE-MARKERS is set, return the positions as markers."
  6310. (let (beg end)
  6311. (save-excursion
  6312. (save-restriction
  6313. (widen)
  6314. (delq nil
  6315. (mapcar (lambda (o)
  6316. (when (eq (overlay-get o 'invisible) 'outline)
  6317. (setq beg (overlay-start o)
  6318. end (overlay-end o))
  6319. (and beg end (> end beg)
  6320. (if use-markers
  6321. (cons (move-marker (make-marker) beg)
  6322. (move-marker (make-marker) end))
  6323. (cons beg end)))))
  6324. (overlays-in (point-min) (point-max))))))))
  6325. (defun org-set-outline-overlay-data (data)
  6326. "Create visibility overlays for all positions in DATA.
  6327. DATA should have been made by `org-outline-overlay-data'."
  6328. (let (o)
  6329. (save-excursion
  6330. (save-restriction
  6331. (widen)
  6332. (show-all)
  6333. (mapc (lambda (c)
  6334. (outline-flag-region (car c) (cdr c) t))
  6335. data)))))
  6336. ;;; Folding of blocks
  6337. (defvar org-hide-block-overlays nil
  6338. "Overlays hiding blocks.")
  6339. (make-variable-buffer-local 'org-hide-block-overlays)
  6340. (defun org-block-map (function &optional start end)
  6341. "Call FUNCTION at the head of all source blocks in the current buffer.
  6342. Optional arguments START and END can be used to limit the range."
  6343. (let ((start (or start (point-min)))
  6344. (end (or end (point-max))))
  6345. (save-excursion
  6346. (goto-char start)
  6347. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6348. (save-excursion
  6349. (save-match-data
  6350. (goto-char (match-beginning 0))
  6351. (funcall function)))))))
  6352. (defun org-hide-block-toggle-all ()
  6353. "Toggle the visibility of all blocks in the current buffer."
  6354. (org-block-map #'org-hide-block-toggle))
  6355. (defun org-hide-block-all ()
  6356. "Fold all blocks in the current buffer."
  6357. (interactive)
  6358. (org-show-block-all)
  6359. (org-block-map #'org-hide-block-toggle-maybe))
  6360. (defun org-show-block-all ()
  6361. "Unfold all blocks in the current buffer."
  6362. (interactive)
  6363. (mapc 'delete-overlay org-hide-block-overlays)
  6364. (setq org-hide-block-overlays nil))
  6365. (defun org-hide-block-toggle-maybe ()
  6366. "Toggle visibility of block at point."
  6367. (interactive)
  6368. (let ((case-fold-search t))
  6369. (if (save-excursion
  6370. (beginning-of-line 1)
  6371. (looking-at org-block-regexp))
  6372. (progn (org-hide-block-toggle)
  6373. t) ;; to signal that we took action
  6374. nil))) ;; to signal that we did not
  6375. (defun org-hide-block-toggle (&optional force)
  6376. "Toggle the visibility of the current block."
  6377. (interactive)
  6378. (save-excursion
  6379. (beginning-of-line)
  6380. (if (re-search-forward org-block-regexp nil t)
  6381. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6382. (end (match-end 0)) ;; end of entire body
  6383. ov)
  6384. (if (memq t (mapcar (lambda (overlay)
  6385. (eq (overlay-get overlay 'invisible)
  6386. 'org-hide-block))
  6387. (overlays-at start)))
  6388. (if (or (not force) (eq force 'off))
  6389. (mapc (lambda (ov)
  6390. (when (member ov org-hide-block-overlays)
  6391. (setq org-hide-block-overlays
  6392. (delq ov org-hide-block-overlays)))
  6393. (when (eq (overlay-get ov 'invisible)
  6394. 'org-hide-block)
  6395. (delete-overlay ov)))
  6396. (overlays-at start)))
  6397. (setq ov (make-overlay start end))
  6398. (overlay-put ov 'invisible 'org-hide-block)
  6399. ;; make the block accessible to isearch
  6400. (overlay-put
  6401. ov 'isearch-open-invisible
  6402. (lambda (ov)
  6403. (when (member ov org-hide-block-overlays)
  6404. (setq org-hide-block-overlays
  6405. (delq ov org-hide-block-overlays)))
  6406. (when (eq (overlay-get ov 'invisible)
  6407. 'org-hide-block)
  6408. (delete-overlay ov))))
  6409. (push ov org-hide-block-overlays)))
  6410. (user-error "Not looking at a source block"))))
  6411. ;; org-tab-after-check-for-cycling-hook
  6412. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6413. ;; Remove overlays when changing major mode
  6414. (add-hook 'org-mode-hook
  6415. (lambda () (org-add-hook 'change-major-mode-hook
  6416. 'org-show-block-all 'append 'local)))
  6417. ;;; Org-goto
  6418. (defvar org-goto-window-configuration nil)
  6419. (defvar org-goto-marker nil)
  6420. (defvar org-goto-map)
  6421. (defun org-goto-map ()
  6422. "Set the keymap `org-goto'."
  6423. (setq org-goto-map
  6424. (let ((map (make-sparse-keymap)))
  6425. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6426. mouse-drag-region universal-argument org-occur))
  6427. cmd)
  6428. (while (setq cmd (pop cmds))
  6429. (substitute-key-definition cmd cmd map global-map)))
  6430. (suppress-keymap map)
  6431. (org-defkey map "\C-m" 'org-goto-ret)
  6432. (org-defkey map [(return)] 'org-goto-ret)
  6433. (org-defkey map [(left)] 'org-goto-left)
  6434. (org-defkey map [(right)] 'org-goto-right)
  6435. (org-defkey map [(control ?g)] 'org-goto-quit)
  6436. (org-defkey map "\C-i" 'org-cycle)
  6437. (org-defkey map [(tab)] 'org-cycle)
  6438. (org-defkey map [(down)] 'outline-next-visible-heading)
  6439. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6440. (if org-goto-auto-isearch
  6441. (if (fboundp 'define-key-after)
  6442. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6443. nil)
  6444. (org-defkey map "q" 'org-goto-quit)
  6445. (org-defkey map "n" 'outline-next-visible-heading)
  6446. (org-defkey map "p" 'outline-previous-visible-heading)
  6447. (org-defkey map "f" 'outline-forward-same-level)
  6448. (org-defkey map "b" 'outline-backward-same-level)
  6449. (org-defkey map "u" 'outline-up-heading))
  6450. (org-defkey map "/" 'org-occur)
  6451. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6452. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6453. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6454. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6455. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6456. map)))
  6457. (defconst org-goto-help
  6458. "Browse buffer copy, to find location or copy text.%s
  6459. RET=jump to location C-g=quit and return to previous location
  6460. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6461. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6462. (defun org-goto (&optional alternative-interface)
  6463. "Look up a different location in the current file, keeping current visibility.
  6464. When you want look-up or go to a different location in a
  6465. document, the fastest way is often to fold the entire buffer and
  6466. then dive into the tree. This method has the disadvantage, that
  6467. the previous location will be folded, which may not be what you
  6468. want.
  6469. This command works around this by showing a copy of the current
  6470. buffer in an indirect buffer, in overview mode. You can dive
  6471. into the tree in that copy, use org-occur and incremental search
  6472. to find a location. When pressing RET or `Q', the command
  6473. returns to the original buffer in which the visibility is still
  6474. unchanged. After RET it will also jump to the location selected
  6475. in the indirect buffer and expose the headline hierarchy above.
  6476. With a prefix argument, use the alternative interface: e.g. if
  6477. `org-goto-interface' is 'outline use 'outline-path-completion."
  6478. (interactive "P")
  6479. (org-goto-map)
  6480. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6481. (org-refile-use-outline-path t)
  6482. (org-refile-target-verify-function nil)
  6483. (interface
  6484. (if (not alternative-interface)
  6485. org-goto-interface
  6486. (if (eq org-goto-interface 'outline)
  6487. 'outline-path-completion
  6488. 'outline)))
  6489. (org-goto-start-pos (point))
  6490. (selected-point
  6491. (if (eq interface 'outline)
  6492. (car (org-get-location (current-buffer) org-goto-help))
  6493. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6494. (org-refile-check-position pa)
  6495. (nth 3 pa)))))
  6496. (if selected-point
  6497. (progn
  6498. (org-mark-ring-push org-goto-start-pos)
  6499. (goto-char selected-point)
  6500. (if (or (outline-invisible-p) (org-invisible-p2))
  6501. (org-show-context 'org-goto)))
  6502. (message "Quit"))))
  6503. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6504. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6505. (defvar org-goto-local-auto-isearch-map) ; defined below
  6506. (defun org-get-location (buf help)
  6507. "Let the user select a location in the Org-mode buffer BUF.
  6508. This function uses a recursive edit. It returns the selected position
  6509. or nil."
  6510. (org-no-popups
  6511. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6512. (isearch-hide-immediately nil)
  6513. (isearch-search-fun-function
  6514. (lambda () 'org-goto-local-search-headings))
  6515. (org-goto-selected-point org-goto-exit-command))
  6516. (save-excursion
  6517. (save-window-excursion
  6518. (delete-other-windows)
  6519. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6520. (org-pop-to-buffer-same-window
  6521. (condition-case nil
  6522. (make-indirect-buffer (current-buffer) "*org-goto*")
  6523. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6524. (with-output-to-temp-buffer "*Org Help*"
  6525. (princ (format help (if org-goto-auto-isearch
  6526. " Just type for auto-isearch."
  6527. " n/p/f/b/u to navigate, q to quit."))))
  6528. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6529. (setq buffer-read-only nil)
  6530. (let ((org-startup-truncated t)
  6531. (org-startup-folded nil)
  6532. (org-startup-align-all-tables nil))
  6533. (org-mode)
  6534. (org-overview))
  6535. (setq buffer-read-only t)
  6536. (if (and (boundp 'org-goto-start-pos)
  6537. (integer-or-marker-p org-goto-start-pos))
  6538. (let ((org-show-hierarchy-above t)
  6539. (org-show-siblings t)
  6540. (org-show-following-heading t))
  6541. (goto-char org-goto-start-pos)
  6542. (and (outline-invisible-p) (org-show-context)))
  6543. (goto-char (point-min)))
  6544. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6545. (message "Select location and press RET")
  6546. (use-local-map org-goto-map)
  6547. (recursive-edit)))
  6548. (kill-buffer "*org-goto*")
  6549. (cons org-goto-selected-point org-goto-exit-command))))
  6550. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6551. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6552. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6553. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6554. (defun org-goto-local-search-headings (string bound noerror)
  6555. "Search and make sure that any matches are in headlines."
  6556. (catch 'return
  6557. (while (if isearch-forward
  6558. (search-forward string bound noerror)
  6559. (search-backward string bound noerror))
  6560. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6561. (and (member :headline context)
  6562. (not (member :tags context))))
  6563. (throw 'return (point))))))
  6564. (defun org-goto-local-auto-isearch ()
  6565. "Start isearch."
  6566. (interactive)
  6567. (goto-char (point-min))
  6568. (let ((keys (this-command-keys)))
  6569. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6570. (isearch-mode t)
  6571. (isearch-process-search-char (string-to-char keys)))))
  6572. (defun org-goto-ret (&optional arg)
  6573. "Finish `org-goto' by going to the new location."
  6574. (interactive "P")
  6575. (setq org-goto-selected-point (point)
  6576. org-goto-exit-command 'return)
  6577. (throw 'exit nil))
  6578. (defun org-goto-left ()
  6579. "Finish `org-goto' by going to the new location."
  6580. (interactive)
  6581. (if (org-at-heading-p)
  6582. (progn
  6583. (beginning-of-line 1)
  6584. (setq org-goto-selected-point (point)
  6585. org-goto-exit-command 'left)
  6586. (throw 'exit nil))
  6587. (user-error "Not on a heading")))
  6588. (defun org-goto-right ()
  6589. "Finish `org-goto' by going to the new location."
  6590. (interactive)
  6591. (if (org-at-heading-p)
  6592. (progn
  6593. (setq org-goto-selected-point (point)
  6594. org-goto-exit-command 'right)
  6595. (throw 'exit nil))
  6596. (user-error "Not on a heading")))
  6597. (defun org-goto-quit ()
  6598. "Finish `org-goto' without cursor motion."
  6599. (interactive)
  6600. (setq org-goto-selected-point nil)
  6601. (setq org-goto-exit-command 'quit)
  6602. (throw 'exit nil))
  6603. ;;; Indirect buffer display of subtrees
  6604. (defvar org-indirect-dedicated-frame nil
  6605. "This is the frame being used for indirect tree display.")
  6606. (defvar org-last-indirect-buffer nil)
  6607. (defun org-tree-to-indirect-buffer (&optional arg)
  6608. "Create indirect buffer and narrow it to current subtree.
  6609. With a numerical prefix ARG, go up to this level and then take that tree.
  6610. If ARG is negative, go up that many levels.
  6611. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6612. indirect buffer previously made with this command, to avoid proliferation of
  6613. indirect buffers. However, when you call the command with a \
  6614. \\[universal-argument] prefix, or
  6615. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6616. is kept so that you can work with several indirect buffers at the same time.
  6617. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6618. \\[universal-argument] prefix also
  6619. requests that a new frame be made for the new buffer, so that the dedicated
  6620. frame is not changed."
  6621. (interactive "P")
  6622. (let ((cbuf (current-buffer))
  6623. (cwin (selected-window))
  6624. (pos (point))
  6625. beg end level heading ibuf)
  6626. (save-excursion
  6627. (org-back-to-heading t)
  6628. (when (numberp arg)
  6629. (setq level (org-outline-level))
  6630. (if (< arg 0) (setq arg (+ level arg)))
  6631. (while (> (setq level (org-outline-level)) arg)
  6632. (org-up-heading-safe)))
  6633. (setq beg (point)
  6634. heading (org-get-heading))
  6635. (org-end-of-subtree t t)
  6636. (if (org-at-heading-p) (backward-char 1))
  6637. (setq end (point)))
  6638. (if (and (buffer-live-p org-last-indirect-buffer)
  6639. (not (eq org-indirect-buffer-display 'new-frame))
  6640. (not arg))
  6641. (kill-buffer org-last-indirect-buffer))
  6642. (setq ibuf (org-get-indirect-buffer cbuf)
  6643. org-last-indirect-buffer ibuf)
  6644. (cond
  6645. ((or (eq org-indirect-buffer-display 'new-frame)
  6646. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6647. (select-frame (make-frame))
  6648. (delete-other-windows)
  6649. (org-pop-to-buffer-same-window ibuf)
  6650. (org-set-frame-title heading))
  6651. ((eq org-indirect-buffer-display 'dedicated-frame)
  6652. (raise-frame
  6653. (select-frame (or (and org-indirect-dedicated-frame
  6654. (frame-live-p org-indirect-dedicated-frame)
  6655. org-indirect-dedicated-frame)
  6656. (setq org-indirect-dedicated-frame (make-frame)))))
  6657. (delete-other-windows)
  6658. (org-pop-to-buffer-same-window ibuf)
  6659. (org-set-frame-title (concat "Indirect: " heading)))
  6660. ((eq org-indirect-buffer-display 'current-window)
  6661. (org-pop-to-buffer-same-window ibuf))
  6662. ((eq org-indirect-buffer-display 'other-window)
  6663. (pop-to-buffer ibuf))
  6664. (t (error "Invalid value")))
  6665. (if (featurep 'xemacs)
  6666. (save-excursion (org-mode) (turn-on-font-lock)))
  6667. (narrow-to-region beg end)
  6668. (show-all)
  6669. (goto-char pos)
  6670. (run-hook-with-args 'org-cycle-hook 'all)
  6671. (and (window-live-p cwin) (select-window cwin))))
  6672. (defun org-get-indirect-buffer (&optional buffer)
  6673. (setq buffer (or buffer (current-buffer)))
  6674. (let ((n 1) (base (buffer-name buffer)) bname)
  6675. (while (buffer-live-p
  6676. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6677. (setq n (1+ n)))
  6678. (condition-case nil
  6679. (make-indirect-buffer buffer bname 'clone)
  6680. (error (make-indirect-buffer buffer bname)))))
  6681. (defun org-set-frame-title (title)
  6682. "Set the title of the current frame to the string TITLE."
  6683. ;; FIXME: how to name a single frame in XEmacs???
  6684. (unless (featurep 'xemacs)
  6685. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6686. ;;;; Structure editing
  6687. ;;; Inserting headlines
  6688. (defun org-previous-line-empty-p (&optional next)
  6689. "Is the previous line a blank line?
  6690. When NEXT is non-nil, check the next line instead."
  6691. (save-excursion
  6692. (and (not (bobp))
  6693. (or (beginning-of-line (if next 2 0)) t)
  6694. (save-match-data
  6695. (looking-at "[ \t]*$")))))
  6696. (defun org-insert-heading (&optional arg invisible-ok)
  6697. "Insert a new heading or item with same depth at point.
  6698. If point is in a plain list and ARG is nil, create a new list item.
  6699. With one universal prefix argument, insert a heading even in lists.
  6700. With two universal prefix arguments, insert the heading at the end
  6701. of the parent subtree.
  6702. If point is at the beginning of a headline, insert a sibling before
  6703. the current headline. If point is not at the beginning, split the line
  6704. and create a new headline with the text in the current line after point
  6705. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6706. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6707. This is important for non-interactive uses of the command."
  6708. (interactive "P")
  6709. (cond
  6710. ((or (= (buffer-size) 0)
  6711. (and (not (save-excursion
  6712. (and (ignore-errors (org-back-to-heading invisible-ok))
  6713. (org-at-heading-p))))
  6714. (or arg (not (org-in-item-p)))))
  6715. (insert
  6716. (if (org-previous-line-empty-p) "" "\n")
  6717. (if (org-in-src-block-p) ",* " "* "))
  6718. (run-hooks 'org-insert-heading-hook))
  6719. ((or arg
  6720. (and (not (org-in-item-p)) org-insert-heading-respect-content)
  6721. (not (org-insert-item
  6722. (save-excursion
  6723. (beginning-of-line)
  6724. (looking-at org-list-full-item-re)
  6725. (match-string 3)))))
  6726. (let (begn endn)
  6727. (when (org-buffer-narrowed-p)
  6728. (setq begn (point-min) endn (point-max))
  6729. (widen))
  6730. (let* ((empty-line-p nil)
  6731. (eops (equal arg '(16))) ; insert at end of parent subtree
  6732. (org-insert-heading-respect-content
  6733. (or (not (null arg)) org-insert-heading-respect-content))
  6734. (level nil)
  6735. (on-heading (org-at-heading-p))
  6736. (on-empty-line
  6737. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6738. (head (save-excursion
  6739. (condition-case nil
  6740. (progn
  6741. (org-back-to-heading invisible-ok)
  6742. (when (and (not on-heading)
  6743. (featurep 'org-inlinetask)
  6744. (integerp org-inlinetask-min-level)
  6745. (>= (length (match-string 0))
  6746. org-inlinetask-min-level))
  6747. ;; Find a heading level before the inline task
  6748. (while (and (setq level (org-up-heading-safe))
  6749. (>= level org-inlinetask-min-level)))
  6750. (if (org-at-heading-p)
  6751. (org-back-to-heading invisible-ok)
  6752. (error "This should not happen")))
  6753. (unless (and (save-excursion
  6754. (save-match-data
  6755. (org-backward-heading-same-level 1 invisible-ok))
  6756. (= (point) (match-beginning 0)))
  6757. (not (org-previous-line-empty-p t)))
  6758. (setq empty-line-p (org-previous-line-empty-p)))
  6759. (match-string 0))
  6760. (error "* "))))
  6761. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6762. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6763. pos hide-previous previous-pos)
  6764. (if ;; At the beginning of a heading, open a new line for insertiong
  6765. (and (bolp) (org-at-heading-p)
  6766. (not eops)
  6767. (or (bobp)
  6768. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6769. (open-line (if blank 2 1))
  6770. (save-excursion
  6771. (setq previous-pos (point-at-bol))
  6772. (end-of-line)
  6773. (setq hide-previous (outline-invisible-p)))
  6774. (and org-insert-heading-respect-content
  6775. (save-excursion
  6776. (while (outline-invisible-p)
  6777. (org-show-subtree)
  6778. (org-up-heading-safe))))
  6779. (let ((split
  6780. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6781. (save-excursion
  6782. (let ((p (point)))
  6783. (goto-char (point-at-bol))
  6784. (and (looking-at org-complex-heading-regexp)
  6785. (match-beginning 4)
  6786. (> p (match-beginning 4)))))))
  6787. tags pos)
  6788. (cond
  6789. ;; Insert a new line, possibly at end of parent subtree
  6790. ((and (not arg) (not on-heading) (not on-empty-line)
  6791. (not (save-excursion
  6792. (beginning-of-line 1)
  6793. (looking-at org-list-full-item-re))))
  6794. (beginning-of-line 1))
  6795. (org-insert-heading-respect-content
  6796. (if (not eops)
  6797. (progn
  6798. (org-end-of-subtree nil t)
  6799. (and (looking-at "^\\*") (backward-char 1))
  6800. (while (and (not (bobp))
  6801. ;; Don't delete spaces in empty headlines
  6802. (not (looking-back org-outline-regexp))
  6803. (member (char-before) '(?\ ?\t ?\n)))
  6804. (backward-delete-char 1)))
  6805. (let ((p (point)))
  6806. (org-up-heading-safe)
  6807. (if (= p (point))
  6808. (goto-char (point-max))
  6809. (org-end-of-subtree nil t))))
  6810. (when (featurep 'org-inlinetask)
  6811. (while (and (not (eobp))
  6812. (looking-at "\\(\\*+\\)[ \t]+")
  6813. (>= (length (match-string 1))
  6814. org-inlinetask-min-level))
  6815. (org-end-of-subtree nil t)))
  6816. (or (bolp) (newline))
  6817. (or (org-previous-line-empty-p)
  6818. (and blank (newline)))
  6819. (if (or empty-line-p eops) (open-line 1)))
  6820. ;; Insert a headling containing text after point
  6821. ((org-at-heading-p)
  6822. (when hide-previous
  6823. (show-children)
  6824. (org-show-entry))
  6825. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6826. (setq tags (and (match-end 2) (match-string 2)))
  6827. (and (match-end 1)
  6828. (delete-region (match-beginning 1) (match-end 1)))
  6829. (setq pos (point-at-bol))
  6830. (or split (end-of-line 1))
  6831. (delete-horizontal-space)
  6832. (if (string-match "\\`\\*+\\'"
  6833. (buffer-substring (point-at-bol) (point)))
  6834. (insert " "))
  6835. (newline (if blank 2 1))
  6836. (when tags
  6837. (save-excursion
  6838. (goto-char pos)
  6839. (end-of-line 1)
  6840. (insert " " tags)
  6841. (org-set-tags nil 'align))))
  6842. (t
  6843. (or split (end-of-line 1))
  6844. (newline (cond ((and blank (not on-empty-line)) 2)
  6845. (blank 1)
  6846. (on-empty-line 0) (t 1)))))))
  6847. (insert head) (just-one-space)
  6848. (setq pos (point))
  6849. (end-of-line 1)
  6850. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6851. (when (and org-insert-heading-respect-content hide-previous)
  6852. (save-excursion
  6853. (goto-char previous-pos)
  6854. (hide-subtree)))
  6855. (when (and begn endn)
  6856. (narrow-to-region (min (point) begn) (max (point) endn)))
  6857. (run-hooks 'org-insert-heading-hook))))))
  6858. (defun org-get-heading (&optional no-tags no-todo)
  6859. "Return the heading of the current entry, without the stars.
  6860. When NO-TAGS is non-nil, don't include tags.
  6861. When NO-TODO is non-nil, don't include TODO keywords."
  6862. (save-excursion
  6863. (org-back-to-heading t)
  6864. (cond
  6865. ((and no-tags no-todo)
  6866. (looking-at org-complex-heading-regexp)
  6867. (match-string 4))
  6868. (no-tags
  6869. (looking-at (concat org-outline-regexp
  6870. "\\(.*?\\)"
  6871. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6872. (match-string 1))
  6873. (no-todo
  6874. (looking-at org-todo-line-regexp)
  6875. (match-string 3))
  6876. (t (looking-at org-heading-regexp)
  6877. (match-string 2)))))
  6878. (defvar orgstruct-mode) ; defined below
  6879. (defun org-heading-components ()
  6880. "Return the components of the current heading.
  6881. This is a list with the following elements:
  6882. - the level as an integer
  6883. - the reduced level, different if `org-odd-levels-only' is set.
  6884. - the TODO keyword, or nil
  6885. - the priority character, like ?A, or nil if no priority is given
  6886. - the headline text itself, or the tags string if no headline text
  6887. - the tags string, or nil."
  6888. (save-excursion
  6889. (org-back-to-heading t)
  6890. (if (let (case-fold-search)
  6891. (looking-at
  6892. (if orgstruct-mode
  6893. org-heading-regexp
  6894. org-complex-heading-regexp)))
  6895. (if orgstruct-mode
  6896. (list (length (match-string 1))
  6897. (org-reduced-level (length (match-string 1)))
  6898. nil
  6899. nil
  6900. (match-string 2)
  6901. nil)
  6902. (list (length (match-string 1))
  6903. (org-reduced-level (length (match-string 1)))
  6904. (org-match-string-no-properties 2)
  6905. (and (match-end 3) (aref (match-string 3) 2))
  6906. (org-match-string-no-properties 4)
  6907. (org-match-string-no-properties 5))))))
  6908. (defun org-get-entry ()
  6909. "Get the entry text, after heading, entire subtree."
  6910. (save-excursion
  6911. (org-back-to-heading t)
  6912. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6913. (defun org-insert-heading-after-current ()
  6914. "Insert a new heading with same level as current, after current subtree."
  6915. (interactive)
  6916. (org-back-to-heading)
  6917. (org-insert-heading)
  6918. (org-move-subtree-down)
  6919. (end-of-line 1))
  6920. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6921. "Insert heading with `org-insert-heading-respect-content' set to t."
  6922. (interactive "P")
  6923. (let ((org-insert-heading-respect-content t))
  6924. (org-insert-heading arg invisible-ok)))
  6925. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6926. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6927. (interactive "P")
  6928. (let ((org-insert-heading-respect-content t))
  6929. (org-insert-todo-heading force-state t)))
  6930. (defun org-insert-todo-heading (arg &optional force-heading)
  6931. "Insert a new heading with the same level and TODO state as current heading.
  6932. If the heading has no TODO state, or if the state is DONE, use the first
  6933. state (TODO by default). Also one prefix arg, force first state. With two
  6934. prefix args, force inserting at the end of the parent subtree."
  6935. (interactive "P")
  6936. (when (or force-heading (not (org-insert-item 'checkbox)))
  6937. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6938. force-heading))
  6939. (save-excursion
  6940. (org-back-to-heading)
  6941. (outline-previous-heading)
  6942. (looking-at org-todo-line-regexp))
  6943. (let*
  6944. ((new-mark-x
  6945. (if (or arg
  6946. (not (match-beginning 2))
  6947. (member (match-string 2) org-done-keywords))
  6948. (car org-todo-keywords-1)
  6949. (match-string 2)))
  6950. (new-mark
  6951. (or
  6952. (run-hook-with-args-until-success
  6953. 'org-todo-get-default-hook new-mark-x nil)
  6954. new-mark-x)))
  6955. (beginning-of-line 1)
  6956. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6957. (if org-treat-insert-todo-heading-as-state-change
  6958. (org-todo new-mark)
  6959. (insert new-mark " "))))
  6960. (when org-provide-todo-statistics
  6961. (org-update-parent-todo-statistics))))
  6962. (defun org-insert-subheading (arg)
  6963. "Insert a new subheading and demote it.
  6964. Works for outline headings and for plain lists alike."
  6965. (interactive "P")
  6966. (org-insert-heading arg)
  6967. (cond
  6968. ((org-at-heading-p) (org-do-demote))
  6969. ((org-at-item-p) (org-indent-item))))
  6970. (defun org-insert-todo-subheading (arg)
  6971. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6972. Works for outline headings and for plain lists alike."
  6973. (interactive "P")
  6974. (org-insert-todo-heading arg)
  6975. (cond
  6976. ((org-at-heading-p) (org-do-demote))
  6977. ((org-at-item-p) (org-indent-item))))
  6978. ;;; Promotion and Demotion
  6979. (defvar org-after-demote-entry-hook nil
  6980. "Hook run after an entry has been demoted.
  6981. The cursor will be at the beginning of the entry.
  6982. When a subtree is being demoted, the hook will be called for each node.")
  6983. (defvar org-after-promote-entry-hook nil
  6984. "Hook run after an entry has been promoted.
  6985. The cursor will be at the beginning of the entry.
  6986. When a subtree is being promoted, the hook will be called for each node.")
  6987. (defun org-promote-subtree ()
  6988. "Promote the entire subtree.
  6989. See also `org-promote'."
  6990. (interactive)
  6991. (save-excursion
  6992. (org-with-limited-levels (org-map-tree 'org-promote)))
  6993. (org-fix-position-after-promote))
  6994. (defun org-demote-subtree ()
  6995. "Demote the entire subtree. See `org-demote'.
  6996. See also `org-promote'."
  6997. (interactive)
  6998. (save-excursion
  6999. (org-with-limited-levels (org-map-tree 'org-demote)))
  7000. (org-fix-position-after-promote))
  7001. (defun org-do-promote ()
  7002. "Promote the current heading higher up the tree.
  7003. If the region is active in `transient-mark-mode', promote all headings
  7004. in the region."
  7005. (interactive)
  7006. (save-excursion
  7007. (if (org-region-active-p)
  7008. (org-map-region 'org-promote (region-beginning) (region-end))
  7009. (org-promote)))
  7010. (org-fix-position-after-promote))
  7011. (defun org-do-demote ()
  7012. "Demote the current heading lower down the tree.
  7013. If the region is active in `transient-mark-mode', demote all headings
  7014. in the region."
  7015. (interactive)
  7016. (save-excursion
  7017. (if (org-region-active-p)
  7018. (org-map-region 'org-demote (region-beginning) (region-end))
  7019. (org-demote)))
  7020. (org-fix-position-after-promote))
  7021. (defun org-fix-position-after-promote ()
  7022. "Make sure that after pro/demotion cursor position is right."
  7023. (let ((pos (point)))
  7024. (when (save-excursion
  7025. (beginning-of-line 1)
  7026. (looking-at org-todo-line-regexp)
  7027. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7028. (cond ((eobp) (insert " "))
  7029. ((eolp) (insert " "))
  7030. ((equal (char-after) ?\ ) (forward-char 1))))))
  7031. (defun org-current-level ()
  7032. "Return the level of the current entry, or nil if before the first headline.
  7033. The level is the number of stars at the beginning of the headline."
  7034. (save-excursion
  7035. (org-with-limited-levels
  7036. (if (ignore-errors (org-back-to-heading t))
  7037. (funcall outline-level)))))
  7038. (defun org-get-previous-line-level ()
  7039. "Return the outline depth of the last headline before the current line.
  7040. Returns 0 for the first headline in the buffer, and nil if before the
  7041. first headline."
  7042. (let ((current-level (org-current-level))
  7043. (prev-level (when (> (line-number-at-pos) 1)
  7044. (save-excursion
  7045. (beginning-of-line 0)
  7046. (org-current-level)))))
  7047. (cond ((null current-level) nil) ; Before first headline
  7048. ((null prev-level) 0) ; At first headline
  7049. (prev-level))))
  7050. (defun org-reduced-level (l)
  7051. "Compute the effective level of a heading.
  7052. This takes into account the setting of `org-odd-levels-only'."
  7053. (cond
  7054. ((zerop l) 0)
  7055. (org-odd-levels-only (1+ (floor (/ l 2))))
  7056. (t l)))
  7057. (defun org-level-increment ()
  7058. "Return the number of stars that will be added or removed at a
  7059. time to headlines when structure editing, based on the value of
  7060. `org-odd-levels-only'."
  7061. (if org-odd-levels-only 2 1))
  7062. (defun org-get-valid-level (level &optional change)
  7063. "Rectify a level change under the influence of `org-odd-levels-only'
  7064. LEVEL is a current level, CHANGE is by how much the level should be
  7065. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7066. even level numbers will become the next higher odd number."
  7067. (if org-odd-levels-only
  7068. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7069. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7070. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7071. (max 1 (+ level (or change 0)))))
  7072. (if (boundp 'define-obsolete-function-alias)
  7073. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7074. (define-obsolete-function-alias 'org-get-legal-level
  7075. 'org-get-valid-level)
  7076. (define-obsolete-function-alias 'org-get-legal-level
  7077. 'org-get-valid-level "23.1")))
  7078. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7079. ;; ̀org-with-limited-levels'
  7080. (defun org-promote ()
  7081. "Promote the current heading higher up the tree.
  7082. If the region is active in `transient-mark-mode', promote all headings
  7083. in the region."
  7084. (org-back-to-heading t)
  7085. (let* ((level (save-match-data (funcall outline-level)))
  7086. (after-change-functions (remove 'flyspell-after-change-function
  7087. after-change-functions))
  7088. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7089. (diff (abs (- level (length up-head) -1))))
  7090. (cond ((and (= level 1) org-called-with-limited-levels
  7091. org-allow-promoting-top-level-subtree)
  7092. (replace-match "# " nil t))
  7093. ((= level 1)
  7094. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7095. (t (replace-match up-head nil t)))
  7096. ;; Fixup tag positioning
  7097. (unless (= level 1)
  7098. (and org-auto-align-tags (org-set-tags nil t))
  7099. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7100. (run-hooks 'org-after-promote-entry-hook)))
  7101. (defun org-demote ()
  7102. "Demote the current heading lower down the tree.
  7103. If the region is active in `transient-mark-mode', demote all headings
  7104. in the region."
  7105. (org-back-to-heading t)
  7106. (let* ((level (save-match-data (funcall outline-level)))
  7107. (after-change-functions (remove 'flyspell-after-change-function
  7108. after-change-functions))
  7109. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7110. (diff (abs (- level (length down-head) -1))))
  7111. (replace-match down-head nil t)
  7112. ;; Fixup tag positioning
  7113. (and org-auto-align-tags (org-set-tags nil t))
  7114. (if org-adapt-indentation (org-fixup-indentation diff))
  7115. (run-hooks 'org-after-demote-entry-hook)))
  7116. (defun org-cycle-level ()
  7117. "Cycle the level of an empty headline through possible states.
  7118. This goes first to child, then to parent, level, then up the hierarchy.
  7119. After top level, it switches back to sibling level."
  7120. (interactive)
  7121. (let ((org-adapt-indentation nil))
  7122. (when (org-point-at-end-of-empty-headline)
  7123. (setq this-command 'org-cycle-level) ; Only needed for caching
  7124. (let ((cur-level (org-current-level))
  7125. (prev-level (org-get-previous-line-level)))
  7126. (cond
  7127. ;; If first headline in file, promote to top-level.
  7128. ((= prev-level 0)
  7129. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7130. do (org-do-promote)))
  7131. ;; If same level as prev, demote one.
  7132. ((= prev-level cur-level)
  7133. (org-do-demote))
  7134. ;; If parent is top-level, promote to top level if not already.
  7135. ((= prev-level 1)
  7136. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7137. do (org-do-promote)))
  7138. ;; If top-level, return to prev-level.
  7139. ((= cur-level 1)
  7140. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7141. do (org-do-demote)))
  7142. ;; If less than prev-level, promote one.
  7143. ((< cur-level prev-level)
  7144. (org-do-promote))
  7145. ;; If deeper than prev-level, promote until higher than
  7146. ;; prev-level.
  7147. ((> cur-level prev-level)
  7148. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7149. do (org-do-promote))))
  7150. t))))
  7151. (defun org-map-tree (fun)
  7152. "Call FUN for every heading underneath the current one."
  7153. (org-back-to-heading)
  7154. (let ((level (funcall outline-level)))
  7155. (save-excursion
  7156. (funcall fun)
  7157. (while (and (progn
  7158. (outline-next-heading)
  7159. (> (funcall outline-level) level))
  7160. (not (eobp)))
  7161. (funcall fun)))))
  7162. (defun org-map-region (fun beg end)
  7163. "Call FUN for every heading between BEG and END."
  7164. (let ((org-ignore-region t))
  7165. (save-excursion
  7166. (setq end (copy-marker end))
  7167. (goto-char beg)
  7168. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7169. (< (point) end))
  7170. (funcall fun))
  7171. (while (and (progn
  7172. (outline-next-heading)
  7173. (< (point) end))
  7174. (not (eobp)))
  7175. (funcall fun)))))
  7176. (defvar org-property-end-re) ; silence byte-compiler
  7177. (defun org-fixup-indentation (diff)
  7178. "Change the indentation in the current entry by DIFF.
  7179. However, if any line in the current entry has no indentation, or if it
  7180. would end up with no indentation after the change, nothing at all is done."
  7181. (save-excursion
  7182. (let ((end (save-excursion (outline-next-heading)
  7183. (point-marker)))
  7184. (prohibit (if (> diff 0)
  7185. "^\\S-"
  7186. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7187. col)
  7188. (unless (save-excursion (end-of-line 1)
  7189. (re-search-forward prohibit end t))
  7190. (while (and (< (point) end)
  7191. (re-search-forward "^[ \t]+" end t))
  7192. (goto-char (match-end 0))
  7193. (setq col (current-column))
  7194. (if (< diff 0) (replace-match ""))
  7195. (org-indent-to-column (+ diff col))))
  7196. (move-marker end nil))))
  7197. (defun org-convert-to-odd-levels ()
  7198. "Convert an org-mode file with all levels allowed to one with odd levels.
  7199. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7200. level 5 etc."
  7201. (interactive)
  7202. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7203. (let ((outline-level 'org-outline-level)
  7204. (org-odd-levels-only nil) n)
  7205. (save-excursion
  7206. (goto-char (point-min))
  7207. (while (re-search-forward "^\\*\\*+ " nil t)
  7208. (setq n (- (length (match-string 0)) 2))
  7209. (while (>= (setq n (1- n)) 0)
  7210. (org-demote))
  7211. (end-of-line 1))))))
  7212. (defun org-convert-to-oddeven-levels ()
  7213. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7214. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7215. file contains a section with an even level, conversion would
  7216. destroy the structure of the file. An error is signaled in this
  7217. case."
  7218. (interactive)
  7219. (goto-char (point-min))
  7220. ;; First check if there are no even levels
  7221. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7222. (org-show-context t)
  7223. (error "Not all levels are odd in this file. Conversion not possible"))
  7224. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7225. (let ((outline-regexp org-outline-regexp)
  7226. (outline-level 'org-outline-level)
  7227. (org-odd-levels-only nil) n)
  7228. (save-excursion
  7229. (goto-char (point-min))
  7230. (while (re-search-forward "^\\*\\*+ " nil t)
  7231. (setq n (/ (1- (length (match-string 0))) 2))
  7232. (while (>= (setq n (1- n)) 0)
  7233. (org-promote))
  7234. (end-of-line 1))))))
  7235. (defun org-tr-level (n)
  7236. "Make N odd if required."
  7237. (if org-odd-levels-only (1+ (/ n 2)) n))
  7238. ;;; Vertical tree motion, cutting and pasting of subtrees
  7239. (defun org-move-subtree-up (&optional arg)
  7240. "Move the current subtree up past ARG headlines of the same level."
  7241. (interactive "p")
  7242. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7243. (defun org-move-subtree-down (&optional arg)
  7244. "Move the current subtree down past ARG headlines of the same level."
  7245. (interactive "p")
  7246. (setq arg (prefix-numeric-value arg))
  7247. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7248. 'org-get-last-sibling))
  7249. (ins-point (make-marker))
  7250. (cnt (abs arg))
  7251. (col (current-column))
  7252. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7253. ;; Select the tree
  7254. (org-back-to-heading)
  7255. (setq beg0 (point))
  7256. (save-excursion
  7257. (setq ne-beg (org-back-over-empty-lines))
  7258. (setq beg (point)))
  7259. (save-match-data
  7260. (save-excursion (outline-end-of-heading)
  7261. (setq folded (outline-invisible-p)))
  7262. (outline-end-of-subtree))
  7263. (outline-next-heading)
  7264. (setq ne-end (org-back-over-empty-lines))
  7265. (setq end (point))
  7266. (goto-char beg0)
  7267. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7268. ;; include less whitespace
  7269. (save-excursion
  7270. (goto-char beg)
  7271. (forward-line (- ne-beg ne-end))
  7272. (setq beg (point))))
  7273. ;; Find insertion point, with error handling
  7274. (while (> cnt 0)
  7275. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7276. (progn (goto-char beg0)
  7277. (user-error "Cannot move past superior level or buffer limit")))
  7278. (setq cnt (1- cnt)))
  7279. (if (> arg 0)
  7280. ;; Moving forward - still need to move over subtree
  7281. (progn (org-end-of-subtree t t)
  7282. (save-excursion
  7283. (org-back-over-empty-lines)
  7284. (or (bolp) (newline)))))
  7285. (setq ne-ins (org-back-over-empty-lines))
  7286. (move-marker ins-point (point))
  7287. (setq txt (buffer-substring beg end))
  7288. (org-save-markers-in-region beg end)
  7289. (delete-region beg end)
  7290. (org-remove-empty-overlays-at beg)
  7291. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7292. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7293. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7294. (let ((bbb (point)))
  7295. (insert-before-markers txt)
  7296. (org-reinstall-markers-in-region bbb)
  7297. (move-marker ins-point bbb))
  7298. (or (bolp) (insert "\n"))
  7299. (setq ins-end (point))
  7300. (goto-char ins-point)
  7301. (org-skip-whitespace)
  7302. (when (and (< arg 0)
  7303. (org-first-sibling-p)
  7304. (> ne-ins ne-beg))
  7305. ;; Move whitespace back to beginning
  7306. (save-excursion
  7307. (goto-char ins-end)
  7308. (let ((kill-whole-line t))
  7309. (kill-line (- ne-ins ne-beg)) (point)))
  7310. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7311. (move-marker ins-point nil)
  7312. (if folded
  7313. (hide-subtree)
  7314. (org-show-entry)
  7315. (show-children)
  7316. (org-cycle-hide-drawers 'children))
  7317. (org-clean-visibility-after-subtree-move)
  7318. ;; move back to the initial column we were at
  7319. (move-to-column col)))
  7320. (defvar org-subtree-clip ""
  7321. "Clipboard for cut and paste of subtrees.
  7322. This is actually only a copy of the kill, because we use the normal kill
  7323. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7324. (defvar org-subtree-clip-folded nil
  7325. "Was the last copied subtree folded?
  7326. This is used to fold the tree back after pasting.")
  7327. (defun org-cut-subtree (&optional n)
  7328. "Cut the current subtree into the clipboard.
  7329. With prefix arg N, cut this many sequential subtrees.
  7330. This is a short-hand for marking the subtree and then cutting it."
  7331. (interactive "p")
  7332. (org-copy-subtree n 'cut))
  7333. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7334. "Cut the current subtree into the clipboard.
  7335. With prefix arg N, cut this many sequential subtrees.
  7336. This is a short-hand for marking the subtree and then copying it.
  7337. If CUT is non-nil, actually cut the subtree.
  7338. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7339. of some markers in the region, even if CUT is non-nil. This is
  7340. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7341. (interactive "p")
  7342. (let (beg end folded (beg0 (point)))
  7343. (if (org-called-interactively-p 'any)
  7344. (org-back-to-heading nil) ; take what looks like a subtree
  7345. (org-back-to-heading t)) ; take what is really there
  7346. (setq beg (point))
  7347. (skip-chars-forward " \t\r\n")
  7348. (save-match-data
  7349. (if nosubtrees
  7350. (outline-next-heading)
  7351. (save-excursion (outline-end-of-heading)
  7352. (setq folded (outline-invisible-p)))
  7353. (condition-case nil
  7354. (org-forward-heading-same-level (1- n) t)
  7355. (error nil))
  7356. (org-end-of-subtree t t)))
  7357. (setq end (point))
  7358. (goto-char beg0)
  7359. (when (> end beg)
  7360. (setq org-subtree-clip-folded folded)
  7361. (when (or cut force-store-markers)
  7362. (org-save-markers-in-region beg end))
  7363. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7364. (setq org-subtree-clip (current-kill 0))
  7365. (message "%s: Subtree(s) with %d characters"
  7366. (if cut "Cut" "Copied")
  7367. (length org-subtree-clip)))))
  7368. (defun org-paste-subtree (&optional level tree for-yank)
  7369. "Paste the clipboard as a subtree, with modification of headline level.
  7370. The entire subtree is promoted or demoted in order to match a new headline
  7371. level.
  7372. If the cursor is at the beginning of a headline, the same level as
  7373. that headline is used to paste the tree.
  7374. If not, the new level is derived from the *visible* headings
  7375. before and after the insertion point, and taken to be the inferior headline
  7376. level of the two. So if the previous visible heading is level 3 and the
  7377. next is level 4 (or vice versa), level 4 will be used for insertion.
  7378. This makes sure that the subtree remains an independent subtree and does
  7379. not swallow low level entries.
  7380. You can also force a different level, either by using a numeric prefix
  7381. argument, or by inserting the heading marker by hand. For example, if the
  7382. cursor is after \"*****\", then the tree will be shifted to level 5.
  7383. If optional TREE is given, use this text instead of the kill ring.
  7384. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7385. move back over whitespace before inserting, and move point to the end of
  7386. the inserted text when done."
  7387. (interactive "P")
  7388. (setq tree (or tree (and kill-ring (current-kill 0))))
  7389. (unless (org-kill-is-subtree-p tree)
  7390. (user-error "%s"
  7391. (substitute-command-keys
  7392. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7393. (org-with-limited-levels
  7394. (let* ((visp (not (outline-invisible-p)))
  7395. (txt tree)
  7396. (^re_ "\\(\\*+\\)[ \t]*")
  7397. (old-level (if (string-match org-outline-regexp-bol txt)
  7398. (- (match-end 0) (match-beginning 0) 1)
  7399. -1))
  7400. (force-level (cond (level (prefix-numeric-value level))
  7401. ((and (looking-at "[ \t]*$")
  7402. (string-match
  7403. "^\\*+$" (buffer-substring
  7404. (point-at-bol) (point))))
  7405. (- (match-end 1) (match-beginning 1)))
  7406. ((and (bolp)
  7407. (looking-at org-outline-regexp))
  7408. (- (match-end 0) (point) 1))))
  7409. (previous-level (save-excursion
  7410. (condition-case nil
  7411. (progn
  7412. (outline-previous-visible-heading 1)
  7413. (if (looking-at ^re_)
  7414. (- (match-end 0) (match-beginning 0) 1)
  7415. 1))
  7416. (error 1))))
  7417. (next-level (save-excursion
  7418. (condition-case nil
  7419. (progn
  7420. (or (looking-at org-outline-regexp)
  7421. (outline-next-visible-heading 1))
  7422. (if (looking-at ^re_)
  7423. (- (match-end 0) (match-beginning 0) 1)
  7424. 1))
  7425. (error 1))))
  7426. (new-level (or force-level (max previous-level next-level)))
  7427. (shift (if (or (= old-level -1)
  7428. (= new-level -1)
  7429. (= old-level new-level))
  7430. 0
  7431. (- new-level old-level)))
  7432. (delta (if (> shift 0) -1 1))
  7433. (func (if (> shift 0) 'org-demote 'org-promote))
  7434. (org-odd-levels-only nil)
  7435. beg end newend)
  7436. ;; Remove the forced level indicator
  7437. (if force-level
  7438. (delete-region (point-at-bol) (point)))
  7439. ;; Paste
  7440. (beginning-of-line (if (bolp) 1 2))
  7441. (setq beg (point))
  7442. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7443. (insert-before-markers txt)
  7444. (unless (string-match "\n\\'" txt) (insert "\n"))
  7445. (setq newend (point))
  7446. (org-reinstall-markers-in-region beg)
  7447. (setq end (point))
  7448. (goto-char beg)
  7449. (skip-chars-forward " \t\n\r")
  7450. (setq beg (point))
  7451. (if (and (outline-invisible-p) visp)
  7452. (save-excursion (outline-show-heading)))
  7453. ;; Shift if necessary
  7454. (unless (= shift 0)
  7455. (save-restriction
  7456. (narrow-to-region beg end)
  7457. (while (not (= shift 0))
  7458. (org-map-region func (point-min) (point-max))
  7459. (setq shift (+ delta shift)))
  7460. (goto-char (point-min))
  7461. (setq newend (point-max))))
  7462. (when (or (org-called-interactively-p 'interactive) for-yank)
  7463. (message "Clipboard pasted as level %d subtree" new-level))
  7464. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7465. kill-ring
  7466. (eq org-subtree-clip (current-kill 0))
  7467. org-subtree-clip-folded)
  7468. ;; The tree was folded before it was killed/copied
  7469. (hide-subtree))
  7470. (and for-yank (goto-char newend)))))
  7471. (defun org-kill-is-subtree-p (&optional txt)
  7472. "Check if the current kill is an outline subtree, or a set of trees.
  7473. Returns nil if kill does not start with a headline, or if the first
  7474. headline level is not the largest headline level in the tree.
  7475. So this will actually accept several entries of equal levels as well,
  7476. which is OK for `org-paste-subtree'.
  7477. If optional TXT is given, check this string instead of the current kill."
  7478. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7479. (re (org-get-limited-outline-regexp))
  7480. (^re (concat "^" re))
  7481. (start-level (and kill
  7482. (string-match
  7483. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7484. kill)
  7485. (- (match-end 2) (match-beginning 2) 1)))
  7486. (start (1+ (or (match-beginning 2) -1))))
  7487. (if (not start-level)
  7488. (progn
  7489. nil) ;; does not even start with a heading
  7490. (catch 'exit
  7491. (while (setq start (string-match ^re kill (1+ start)))
  7492. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7493. (throw 'exit nil)))
  7494. t))))
  7495. (defvar org-markers-to-move nil
  7496. "Markers that should be moved with a cut-and-paste operation.
  7497. Those markers are stored together with their positions relative to
  7498. the start of the region.")
  7499. (defun org-save-markers-in-region (beg end)
  7500. "Check markers in region.
  7501. If these markers are between BEG and END, record their position relative
  7502. to BEG, so that after moving the block of text, we can put the markers back
  7503. into place.
  7504. This function gets called just before an entry or tree gets cut from the
  7505. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7506. called immediately, to move the markers with the entries."
  7507. (setq org-markers-to-move nil)
  7508. (when (featurep 'org-clock)
  7509. (org-clock-save-markers-for-cut-and-paste beg end))
  7510. (when (featurep 'org-agenda)
  7511. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7512. (defun org-check-and-save-marker (marker beg end)
  7513. "Check if MARKER is between BEG and END.
  7514. If yes, remember the marker and the distance to BEG."
  7515. (when (and (marker-buffer marker)
  7516. (equal (marker-buffer marker) (current-buffer)))
  7517. (if (and (>= marker beg) (< marker end))
  7518. (push (cons marker (- marker beg)) org-markers-to-move))))
  7519. (defun org-reinstall-markers-in-region (beg)
  7520. "Move all remembered markers to their position relative to BEG."
  7521. (mapc (lambda (x)
  7522. (move-marker (car x) (+ beg (cdr x))))
  7523. org-markers-to-move)
  7524. (setq org-markers-to-move nil))
  7525. (defun org-narrow-to-subtree ()
  7526. "Narrow buffer to the current subtree."
  7527. (interactive)
  7528. (save-excursion
  7529. (save-match-data
  7530. (org-with-limited-levels
  7531. (narrow-to-region
  7532. (progn (org-back-to-heading t) (point))
  7533. (progn (org-end-of-subtree t t)
  7534. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7535. (point)))))))
  7536. (defun org-narrow-to-block ()
  7537. "Narrow buffer to the current block."
  7538. (interactive)
  7539. (let* ((case-fold-search t)
  7540. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7541. "^[ \t]*#\\+end_.*")))
  7542. (if blockp
  7543. (narrow-to-region (car blockp) (cdr blockp))
  7544. (user-error "Not in a block"))))
  7545. (eval-when-compile
  7546. (defvar org-property-drawer-re))
  7547. (defvar org-property-start-re) ;; defined below
  7548. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7549. "Clone the task (subtree) at point N times.
  7550. The clones will be inserted as siblings.
  7551. In interactive use, the user will be prompted for the number of
  7552. clones to be produced. When called with a universal prefix argument
  7553. and if the entry has a timestamp, the user will also be prompted for
  7554. a time shift, which may be a repeater as used in time stamps, for
  7555. example `+3d'.
  7556. When a valid repeater is given and the entry contains any time
  7557. stamps, the clones will become a sequence in time, with time
  7558. stamps in the subtree shifted for each clone produced. If SHIFT
  7559. is nil or the empty string, time stamps will be left alone. The
  7560. ID property of the original subtree is removed.
  7561. If the original subtree did contain time stamps with a repeater,
  7562. the following will happen:
  7563. - the repeater will be removed in each clone
  7564. - an additional clone will be produced, with the current, unshifted
  7565. date(s) in the entry.
  7566. - the original entry will be placed *after* all the clones, with
  7567. repeater intact.
  7568. - the start days in the repeater in the original entry will be shifted
  7569. to past the last clone.
  7570. In this way you can spell out a number of instances of a repeating task,
  7571. and still retain the repeater to cover future instances of the task."
  7572. (interactive "nNumber of clones to produce: ")
  7573. (let ((shift
  7574. (or shift
  7575. (if (and (equal current-prefix-arg '(4))
  7576. (save-excursion
  7577. (re-search-forward org-ts-regexp-both
  7578. (save-excursion
  7579. (org-end-of-subtree t)
  7580. (point)) t)))
  7581. (read-from-minibuffer
  7582. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7583. ""))) ;; No time shift
  7584. (n-no-remove -1)
  7585. (drawer-re org-drawer-regexp)
  7586. beg end template task idprop
  7587. shift-n shift-what doshift nmin nmax)
  7588. (if (not (and (integerp n) (> n 0)))
  7589. (error "Invalid number of replications %s" n))
  7590. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7591. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7592. shift)))
  7593. (error "Invalid shift specification %s" shift))
  7594. (when doshift
  7595. (setq shift-n (string-to-number (match-string 1 shift))
  7596. shift-what (cdr (assoc (match-string 2 shift)
  7597. '(("d" . day) ("w" . week)
  7598. ("m" . month) ("y" . year))))))
  7599. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7600. (setq nmin 1 nmax n)
  7601. (org-back-to-heading t)
  7602. (setq beg (point))
  7603. (setq idprop (org-entry-get nil "ID"))
  7604. (org-end-of-subtree t t)
  7605. (or (bolp) (insert "\n"))
  7606. (setq end (point))
  7607. (setq template (buffer-substring beg end))
  7608. (when (and doshift
  7609. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7610. (delete-region beg end)
  7611. (setq end beg)
  7612. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7613. (goto-char end)
  7614. (loop for n from nmin to nmax do
  7615. ;; prepare clone
  7616. (with-temp-buffer
  7617. (insert template)
  7618. (org-mode)
  7619. (goto-char (point-min))
  7620. (org-show-subtree)
  7621. (and idprop (if org-clone-delete-id
  7622. (org-entry-delete nil "ID")
  7623. (org-id-get-create t)))
  7624. (unless (= n 0)
  7625. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7626. (kill-whole-line))
  7627. (goto-char (point-min))
  7628. (while (re-search-forward drawer-re nil t)
  7629. (mapc (lambda (d)
  7630. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7631. (goto-char (point-min))
  7632. (when doshift
  7633. (while (re-search-forward org-ts-regexp-both nil t)
  7634. (org-timestamp-change (* n shift-n) shift-what))
  7635. (unless (= n n-no-remove)
  7636. (goto-char (point-min))
  7637. (while (re-search-forward org-ts-regexp nil t)
  7638. (save-excursion
  7639. (goto-char (match-beginning 0))
  7640. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7641. (delete-region (match-beginning 1) (match-end 1)))))))
  7642. (setq task (buffer-string)))
  7643. (insert task))
  7644. (goto-char beg)))
  7645. ;;; Outline Sorting
  7646. (defun org-sort (with-case)
  7647. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7648. Optional argument WITH-CASE means sort case-sensitively."
  7649. (interactive "P")
  7650. (cond
  7651. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7652. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7653. (t
  7654. (org-call-with-arg 'org-sort-entries with-case))))
  7655. (defun org-sort-remove-invisible (s)
  7656. "Remove invisible links from string S."
  7657. (remove-text-properties 0 (length s) org-rm-props s)
  7658. (while (string-match org-bracket-link-regexp s)
  7659. (setq s (replace-match (if (match-end 2)
  7660. (match-string 3 s)
  7661. (match-string 1 s)) t t s)))
  7662. (let ((st (format " %s " s)))
  7663. (while (string-match org-emph-re st)
  7664. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7665. (setq s (substring st 1 -1)))
  7666. s)
  7667. (defvar org-priority-regexp) ; defined later in the file
  7668. (defvar org-after-sorting-entries-or-items-hook nil
  7669. "Hook that is run after a bunch of entries or items have been sorted.
  7670. When children are sorted, the cursor is in the parent line when this
  7671. hook gets called. When a region or a plain list is sorted, the cursor
  7672. will be in the first entry of the sorted region/list.")
  7673. (defun org-sort-entries
  7674. (&optional with-case sorting-type getkey-func compare-func property)
  7675. "Sort entries on a certain level of an outline tree.
  7676. If there is an active region, the entries in the region are sorted.
  7677. Else, if the cursor is before the first entry, sort the top-level items.
  7678. Else, the children of the entry at point are sorted.
  7679. Sorting can be alphabetically, numerically, by date/time as given by
  7680. a time stamp, by a property, by priority order, or by a custom function.
  7681. The command prompts for the sorting type unless it has been given to the
  7682. function through the SORTING-TYPE argument, which needs to be a character,
  7683. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7684. precise meaning of each character:
  7685. n Numerically, by converting the beginning of the entry/item to a number.
  7686. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7687. o By order of TODO keywords.
  7688. t By date/time, either the first active time stamp in the entry, or, if
  7689. none exist, by the first inactive one.
  7690. s By the scheduled date/time.
  7691. d By deadline date/time.
  7692. c By creation time, which is assumed to be the first inactive time stamp
  7693. at the beginning of a line.
  7694. p By priority according to the cookie.
  7695. r By the value of a property.
  7696. Capital letters will reverse the sort order.
  7697. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7698. called with point at the beginning of the record. It must return either
  7699. a string or a number that should serve as the sorting key for that record.
  7700. Comparing entries ignores case by default. However, with an optional argument
  7701. WITH-CASE, the sorting considers case as well.
  7702. Sorting is done against the visible part of the headlines, it ignores hidden
  7703. links."
  7704. (interactive "P")
  7705. (let ((case-func (if with-case 'identity 'downcase))
  7706. (cmstr
  7707. ;; The clock marker is lost when using `sort-subr', let's
  7708. ;; store the clocking string.
  7709. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7710. (save-excursion
  7711. (goto-char org-clock-marker)
  7712. (looking-back "^.*") (match-string-no-properties 0))))
  7713. start beg end stars re re2
  7714. txt what tmp)
  7715. ;; Find beginning and end of region to sort
  7716. (cond
  7717. ((org-region-active-p)
  7718. ;; we will sort the region
  7719. (setq end (region-end)
  7720. what "region")
  7721. (goto-char (region-beginning))
  7722. (if (not (org-at-heading-p)) (outline-next-heading))
  7723. (setq start (point)))
  7724. ((or (org-at-heading-p)
  7725. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7726. ;; we will sort the children of the current headline
  7727. (org-back-to-heading)
  7728. (setq start (point)
  7729. end (progn (org-end-of-subtree t t)
  7730. (or (bolp) (insert "\n"))
  7731. (org-back-over-empty-lines)
  7732. (point))
  7733. what "children")
  7734. (goto-char start)
  7735. (show-subtree)
  7736. (outline-next-heading))
  7737. (t
  7738. ;; we will sort the top-level entries in this file
  7739. (goto-char (point-min))
  7740. (or (org-at-heading-p) (outline-next-heading))
  7741. (setq start (point))
  7742. (goto-char (point-max))
  7743. (beginning-of-line 1)
  7744. (when (looking-at ".*?\\S-")
  7745. ;; File ends in a non-white line
  7746. (end-of-line 1)
  7747. (insert "\n"))
  7748. (setq end (point-max))
  7749. (setq what "top-level")
  7750. (goto-char start)
  7751. (show-all)))
  7752. (setq beg (point))
  7753. (if (>= beg end) (user-error "Nothing to sort"))
  7754. (looking-at "\\(\\*+\\)")
  7755. (setq stars (match-string 1)
  7756. re (concat "^" (regexp-quote stars) " +")
  7757. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7758. txt (buffer-substring beg end))
  7759. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7760. (if (and (not (equal stars "*")) (string-match re2 txt))
  7761. (user-error "Region to sort contains a level above the first entry"))
  7762. (unless sorting-type
  7763. (message
  7764. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7765. [t]ime [s]cheduled [d]eadline [c]reated
  7766. A/N/P/R/O/F/T/S/D/C means reversed:"
  7767. what)
  7768. (setq sorting-type (read-char-exclusive))
  7769. (unless getkey-func
  7770. (and (= (downcase sorting-type) ?f)
  7771. (setq getkey-func
  7772. (org-icompleting-read "Sort using function: "
  7773. obarray 'fboundp t nil nil))
  7774. (setq getkey-func (intern getkey-func))))
  7775. (and (= (downcase sorting-type) ?r)
  7776. (not property)
  7777. (setq property
  7778. (org-icompleting-read "Property: "
  7779. (mapcar 'list (org-buffer-property-keys t))
  7780. nil t))))
  7781. (message "Sorting entries...")
  7782. (save-restriction
  7783. (narrow-to-region start end)
  7784. (let ((dcst (downcase sorting-type))
  7785. (case-fold-search nil)
  7786. (now (current-time)))
  7787. (sort-subr
  7788. (/= dcst sorting-type)
  7789. ;; This function moves to the beginning character of the "record" to
  7790. ;; be sorted.
  7791. (lambda nil
  7792. (if (re-search-forward re nil t)
  7793. (goto-char (match-beginning 0))
  7794. (goto-char (point-max))))
  7795. ;; This function moves to the last character of the "record" being
  7796. ;; sorted.
  7797. (lambda nil
  7798. (save-match-data
  7799. (condition-case nil
  7800. (outline-forward-same-level 1)
  7801. (error
  7802. (goto-char (point-max))))))
  7803. ;; This function returns the value that gets sorted against.
  7804. (lambda nil
  7805. (cond
  7806. ((= dcst ?n)
  7807. (if (looking-at org-complex-heading-regexp)
  7808. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7809. nil))
  7810. ((= dcst ?a)
  7811. (if (looking-at org-complex-heading-regexp)
  7812. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7813. nil))
  7814. ((= dcst ?t)
  7815. (let ((end (save-excursion (outline-next-heading) (point))))
  7816. (if (or (re-search-forward org-ts-regexp end t)
  7817. (re-search-forward org-ts-regexp-both end t))
  7818. (org-time-string-to-seconds (match-string 0))
  7819. (org-float-time now))))
  7820. ((= dcst ?c)
  7821. (let ((end (save-excursion (outline-next-heading) (point))))
  7822. (if (re-search-forward
  7823. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7824. end t)
  7825. (org-time-string-to-seconds (match-string 0))
  7826. (org-float-time now))))
  7827. ((= dcst ?s)
  7828. (let ((end (save-excursion (outline-next-heading) (point))))
  7829. (if (re-search-forward org-scheduled-time-regexp end t)
  7830. (org-time-string-to-seconds (match-string 1))
  7831. (org-float-time now))))
  7832. ((= dcst ?d)
  7833. (let ((end (save-excursion (outline-next-heading) (point))))
  7834. (if (re-search-forward org-deadline-time-regexp end t)
  7835. (org-time-string-to-seconds (match-string 1))
  7836. (org-float-time now))))
  7837. ((= dcst ?p)
  7838. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7839. (string-to-char (match-string 2))
  7840. org-default-priority))
  7841. ((= dcst ?r)
  7842. (or (org-entry-get nil property) ""))
  7843. ((= dcst ?o)
  7844. (if (looking-at org-complex-heading-regexp)
  7845. (- 9999 (length (member (match-string 2)
  7846. org-todo-keywords-1)))))
  7847. ((= dcst ?f)
  7848. (if getkey-func
  7849. (progn
  7850. (setq tmp (funcall getkey-func))
  7851. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7852. tmp)
  7853. (error "Invalid key function `%s'" getkey-func)))
  7854. (t (error "Invalid sorting type `%c'" sorting-type))))
  7855. nil
  7856. (cond
  7857. ((= dcst ?a) 'string<)
  7858. ((= dcst ?f) compare-func)
  7859. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7860. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7861. ;; Reset the clock marker if needed
  7862. (when cmstr
  7863. (save-excursion
  7864. (goto-char start)
  7865. (search-forward cmstr nil t)
  7866. (move-marker org-clock-marker (point))))
  7867. (message "Sorting entries...done")))
  7868. (defun org-do-sort (table what &optional with-case sorting-type)
  7869. "Sort TABLE of WHAT according to SORTING-TYPE.
  7870. The user will be prompted for the SORTING-TYPE if the call to this
  7871. function does not specify it. WHAT is only for the prompt, to indicate
  7872. what is being sorted. The sorting key will be extracted from
  7873. the car of the elements of the table.
  7874. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7875. (unless sorting-type
  7876. (message
  7877. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7878. what)
  7879. (setq sorting-type (read-char-exclusive)))
  7880. (let ((dcst (downcase sorting-type))
  7881. extractfun comparefun)
  7882. ;; Define the appropriate functions
  7883. (cond
  7884. ((= dcst ?n)
  7885. (setq extractfun 'string-to-number
  7886. comparefun (if (= dcst sorting-type) '< '>)))
  7887. ((= dcst ?a)
  7888. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7889. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7890. comparefun (if (= dcst sorting-type)
  7891. 'string<
  7892. (lambda (a b) (and (not (string< a b))
  7893. (not (string= a b)))))))
  7894. ((= dcst ?t)
  7895. (setq extractfun
  7896. (lambda (x)
  7897. (if (or (string-match org-ts-regexp x)
  7898. (string-match org-ts-regexp-both x))
  7899. (org-float-time
  7900. (org-time-string-to-time (match-string 0 x)))
  7901. 0))
  7902. comparefun (if (= dcst sorting-type) '< '>)))
  7903. (t (error "Invalid sorting type `%c'" sorting-type)))
  7904. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7905. table)
  7906. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7907. ;;; The orgstruct minor mode
  7908. ;; Define a minor mode which can be used in other modes in order to
  7909. ;; integrate the org-mode structure editing commands.
  7910. ;; This is really a hack, because the org-mode structure commands use
  7911. ;; keys which normally belong to the major mode. Here is how it
  7912. ;; works: The minor mode defines all the keys necessary to operate the
  7913. ;; structure commands, but wraps the commands into a function which
  7914. ;; tests if the cursor is currently at a headline or a plain list
  7915. ;; item. If that is the case, the structure command is used,
  7916. ;; temporarily setting many Org-mode variables like regular
  7917. ;; expressions for filling etc. However, when any of those keys is
  7918. ;; used at a different location, function uses `key-binding' to look
  7919. ;; up if the key has an associated command in another currently active
  7920. ;; keymap (minor modes, major mode, global), and executes that
  7921. ;; command. There might be problems if any of the keys is otherwise
  7922. ;; used as a prefix key.
  7923. (defcustom orgstruct-heading-prefix-regexp nil
  7924. "Regexp that matches the custom prefix of Org headlines in
  7925. orgstruct(++)-mode."
  7926. :group 'org
  7927. :version "24.4"
  7928. :package-version '(Org . "8.0")
  7929. :type 'string)
  7930. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7931. (defcustom orgstruct-setup-hook nil
  7932. "Hook run after orgstruct-mode-map is filled."
  7933. :group 'org
  7934. :version "24.4"
  7935. :package-version '(Org . "8.0")
  7936. :type 'hook)
  7937. (defvar orgstruct-initialized nil)
  7938. (defvar org-local-vars nil
  7939. "List of local variables, for use by `orgstruct-mode'.")
  7940. ;;;###autoload
  7941. (define-minor-mode orgstruct-mode
  7942. "Toggle the minor mode `orgstruct-mode'.
  7943. This mode is for using Org-mode structure commands in other
  7944. modes. The following keys behave as if Org-mode were active, if
  7945. the cursor is on a headline, or on a plain list item (both as
  7946. defined by Org-mode)."
  7947. nil " OrgStruct" (make-sparse-keymap)
  7948. (funcall (if orgstruct-mode
  7949. 'add-to-invisibility-spec
  7950. 'remove-from-invisibility-spec)
  7951. '(outline . t))
  7952. (when orgstruct-mode
  7953. (org-load-modules-maybe)
  7954. (unless orgstruct-initialized
  7955. (orgstruct-setup)
  7956. (setq orgstruct-initialized t))))
  7957. ;;;###autoload
  7958. (defun turn-on-orgstruct ()
  7959. "Unconditionally turn on `orgstruct-mode'."
  7960. (orgstruct-mode 1))
  7961. (defvar org-fb-vars nil)
  7962. (make-variable-buffer-local 'org-fb-vars)
  7963. (defun orgstruct++-mode (&optional arg)
  7964. "Toggle `orgstruct-mode', the enhanced version of it.
  7965. In addition to setting orgstruct-mode, this also exports all
  7966. indentation and autofilling variables from org-mode into the
  7967. buffer. It will also recognize item context in multiline items."
  7968. (interactive "P")
  7969. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7970. (if (< arg 1)
  7971. (progn (orgstruct-mode -1)
  7972. (mapc (lambda(v)
  7973. (org-set-local (car v)
  7974. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7975. org-fb-vars))
  7976. (orgstruct-mode 1)
  7977. (setq org-fb-vars nil)
  7978. (let (var val)
  7979. (mapc
  7980. (lambda (x)
  7981. (when (string-match
  7982. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7983. (symbol-name (car x)))
  7984. (setq var (car x) val (nth 1 x))
  7985. (push (list var `(quote ,(eval var))) org-fb-vars)
  7986. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7987. org-local-vars)
  7988. (org-set-local 'orgstruct-is-++ t))))
  7989. (defvar orgstruct-is-++ nil
  7990. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7991. (make-variable-buffer-local 'orgstruct-is-++)
  7992. ;;;###autoload
  7993. (defun turn-on-orgstruct++ ()
  7994. "Unconditionally turn on `orgstruct++-mode'."
  7995. (orgstruct++-mode 1))
  7996. (defun orgstruct-error ()
  7997. "Error when there is no default binding for a structure key."
  7998. (interactive)
  7999. (funcall (if (fboundp 'user-error)
  8000. 'user-error
  8001. 'error)
  8002. "This key has no function outside structure elements"))
  8003. (defun orgstruct-setup ()
  8004. "Setup orgstruct keymap."
  8005. (dolist (cell '((org-demote . t)
  8006. (org-metaleft . t)
  8007. (org-metaright . t)
  8008. (org-promote . t)
  8009. (org-shiftmetaleft . t)
  8010. (org-shiftmetaright . t)
  8011. org-backward-element
  8012. org-backward-heading-same-level
  8013. org-ctrl-c-ret
  8014. org-ctrl-c-minus
  8015. org-ctrl-c-star
  8016. org-cycle
  8017. org-forward-heading-same-level
  8018. org-insert-heading
  8019. org-insert-heading-respect-content
  8020. org-kill-note-or-show-branches
  8021. org-mark-subtree
  8022. org-meta-return
  8023. org-metadown
  8024. org-metaup
  8025. org-narrow-to-subtree
  8026. org-promote-subtree
  8027. org-reveal
  8028. org-shiftdown
  8029. org-shiftleft
  8030. org-shiftmetadown
  8031. org-shiftmetaup
  8032. org-shiftright
  8033. org-shifttab
  8034. org-shifttab
  8035. org-shiftup
  8036. org-show-subtree
  8037. org-sort
  8038. org-up-element
  8039. outline-demote
  8040. outline-next-visible-heading
  8041. outline-previous-visible-heading
  8042. outline-promote
  8043. outline-up-heading
  8044. show-children))
  8045. (let ((f (or (car-safe cell) cell))
  8046. (disable-when-heading-prefix (cdr-safe cell)))
  8047. (when (fboundp f)
  8048. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8049. (where-is-internal f outline-mode-map)))
  8050. ;; TODO use local-function-key-map
  8051. (dolist (rep '(("<tab>" . "TAB")
  8052. ("<return>" . "RET")
  8053. ("<escape>" . "ESC")
  8054. ("<delete>" . "DEL")))
  8055. (setq binding (read-kbd-macro
  8056. (let ((case-fold-search))
  8057. (replace-regexp-in-string
  8058. (regexp-quote (cdr rep))
  8059. (car rep)
  8060. (key-description binding))))))
  8061. (let ((key (lookup-key orgstruct-mode-map binding)))
  8062. (when (or (not key) (numberp key))
  8063. (condition-case nil
  8064. (org-defkey orgstruct-mode-map
  8065. binding
  8066. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8067. (error nil))))))))
  8068. (run-hooks 'orgstruct-setup-hook))
  8069. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8070. "Create a function for binding in the structure minor mode.
  8071. FUN is the command to call inside a table. KEY is the key that
  8072. should be checked in for a command to execute outside of tables.
  8073. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8074. if `orgstruct-heading-prefix-regexp' is non-nil."
  8075. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8076. (let ((nname name)
  8077. (i 0))
  8078. (while (fboundp (intern nname))
  8079. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8080. (setq name (intern nname)))
  8081. (eval
  8082. (let ((bindings '((org-heading-regexp
  8083. (concat "^"
  8084. orgstruct-heading-prefix-regexp
  8085. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8086. (org-outline-regexp
  8087. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8088. (org-outline-regexp-bol
  8089. (concat "^" org-outline-regexp))
  8090. (outline-regexp org-outline-regexp)
  8091. (outline-heading-end-regexp "\n")
  8092. (outline-level 'org-outline-level)
  8093. (outline-heading-alist))))
  8094. `(defun ,name (arg)
  8095. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8096. "Outside of structure, run the binding of `"
  8097. (key-description key) "'."
  8098. (when disable-when-heading-prefix
  8099. (concat
  8100. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8101. "back to the default binding due to limitations of Org's implementation of\n"
  8102. "`" (symbol-name fun) "'.")))
  8103. (interactive "p")
  8104. (let* ((disable
  8105. ,(when disable-when-heading-prefix
  8106. '(and orgstruct-heading-prefix-regexp
  8107. (not (string= orgstruct-heading-prefix-regexp "")))))
  8108. (fallback
  8109. (or disable
  8110. (not
  8111. (let* ,bindings
  8112. (org-context-p 'headline 'item
  8113. ,(when (memq fun '(org-insert-heading))
  8114. '(when orgstruct-is-++
  8115. 'item-body))))))))
  8116. (if fallback
  8117. (let* ((orgstruct-mode)
  8118. (binding
  8119. (loop with key = ,key
  8120. for rep in
  8121. '(nil
  8122. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8123. ("<\\([^>]*\\)return>" . "\\1RET")
  8124. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8125. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8126. do
  8127. (when rep
  8128. (setq key (read-kbd-macro
  8129. (let ((case-fold-search))
  8130. (replace-regexp-in-string
  8131. (car rep)
  8132. (cdr rep)
  8133. (key-description key))))))
  8134. thereis (key-binding key))))
  8135. (if (keymapp binding)
  8136. (set-temporary-overlay-map binding)
  8137. (let ((func (or binding
  8138. (unless disable
  8139. 'orgstruct-error))))
  8140. (when func
  8141. (call-interactively func)))))
  8142. (org-run-like-in-org-mode
  8143. (lambda ()
  8144. (interactive)
  8145. (let* ,bindings
  8146. (call-interactively ',fun)))))))))
  8147. name))
  8148. (defun org-contextualize-keys (alist contexts)
  8149. "Return valid elements in ALIST depending on CONTEXTS.
  8150. `org-agenda-custom-commands' or `org-capture-templates' are the
  8151. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8152. or `org-capture-templates-contexts' are the associated contexts
  8153. definitions."
  8154. (let ((contexts
  8155. ;; normalize contexts
  8156. (mapcar
  8157. (lambda(c) (cond ((listp (cadr c))
  8158. (list (car c) (car c) (cadr c)))
  8159. ((string= "" (cadr c))
  8160. (list (car c) (car c) (caddr c)))
  8161. (t c))) contexts))
  8162. (a alist) c r s)
  8163. ;; loop over all commands or templates
  8164. (while (setq c (pop a))
  8165. (let (vrules repl)
  8166. (cond
  8167. ((not (assoc (car c) contexts))
  8168. (push c r))
  8169. ((and (assoc (car c) contexts)
  8170. (setq vrules (org-contextualize-validate-key
  8171. (car c) contexts)))
  8172. (mapc (lambda (vr)
  8173. (when (not (equal (car vr) (cadr vr)))
  8174. (setq repl vr))) vrules)
  8175. (if (not repl) (push c r)
  8176. (push (cadr repl) s)
  8177. (push
  8178. (cons (car c)
  8179. (cdr (or (assoc (cadr repl) alist)
  8180. (error "Undefined key `%s' as contextual replacement for `%s'"
  8181. (cadr repl) (car c)))))
  8182. r))))))
  8183. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8184. (delq
  8185. nil
  8186. (delete-dups
  8187. (mapcar (lambda (x)
  8188. (let ((tpl (car x)))
  8189. (when (not (delq
  8190. nil
  8191. (mapcar (lambda(y)
  8192. (equal y tpl)) s))) x)))
  8193. (reverse r))))))
  8194. (defun org-contextualize-validate-key (key contexts)
  8195. "Check CONTEXTS for agenda or capture KEY."
  8196. (let (r rr res)
  8197. (while (setq r (pop contexts))
  8198. (mapc
  8199. (lambda (rr)
  8200. (when
  8201. (and (equal key (car r))
  8202. (if (functionp rr) (funcall rr)
  8203. (or (and (eq (car rr) 'in-file)
  8204. (buffer-file-name)
  8205. (string-match (cdr rr) (buffer-file-name)))
  8206. (and (eq (car rr) 'in-mode)
  8207. (string-match (cdr rr) (symbol-name major-mode)))
  8208. (and (eq (car rr) 'in-buffer)
  8209. (string-match (cdr rr) (buffer-name)))
  8210. (when (and (eq (car rr) 'not-in-file)
  8211. (buffer-file-name))
  8212. (not (string-match (cdr rr) (buffer-file-name))))
  8213. (when (eq (car rr) 'not-in-mode)
  8214. (not (string-match (cdr rr) (symbol-name major-mode))))
  8215. (when (eq (car rr) 'not-in-buffer)
  8216. (not (string-match (cdr rr) (buffer-name)))))))
  8217. (push r res)))
  8218. (car (last r))))
  8219. (delete-dups (delq nil res))))
  8220. (defun org-context-p (&rest contexts)
  8221. "Check if local context is any of CONTEXTS.
  8222. Possible values in the list of contexts are `table', `headline', and `item'."
  8223. (let ((pos (point)))
  8224. (goto-char (point-at-bol))
  8225. (prog1 (or (and (memq 'table contexts)
  8226. (looking-at "[ \t]*|"))
  8227. (and (memq 'headline contexts)
  8228. (looking-at org-outline-regexp))
  8229. (and (memq 'item contexts)
  8230. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8231. (and (memq 'item-body contexts)
  8232. (org-in-item-p)))
  8233. (goto-char pos))))
  8234. (defun org-get-local-variables ()
  8235. "Return a list of all local variables in an Org mode buffer."
  8236. (let (varlist)
  8237. (with-current-buffer (get-buffer-create "*Org tmp*")
  8238. (erase-buffer)
  8239. (org-mode)
  8240. (setq varlist (buffer-local-variables)))
  8241. (kill-buffer "*Org tmp*")
  8242. (delq nil
  8243. (mapcar
  8244. (lambda (x)
  8245. (setq x
  8246. (if (symbolp x)
  8247. (list x)
  8248. (list (car x) (cdr x))))
  8249. (if (and (not (get (car x) 'org-state))
  8250. (string-match
  8251. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8252. (symbol-name (car x))))
  8253. x nil))
  8254. varlist))))
  8255. (defun org-clone-local-variables (from-buffer &optional regexp)
  8256. "Clone local variables from FROM-BUFFER.
  8257. Optional argument REGEXP selects variables to clone."
  8258. (mapc
  8259. (lambda (pair)
  8260. (and (symbolp (car pair))
  8261. (or (null regexp)
  8262. (string-match regexp (symbol-name (car pair))))
  8263. (set (make-local-variable (car pair))
  8264. (cdr pair))))
  8265. (buffer-local-variables from-buffer)))
  8266. ;;;###autoload
  8267. (defun org-run-like-in-org-mode (cmd)
  8268. "Run a command, pretending that the current buffer is in Org-mode.
  8269. This will temporarily bind local variables that are typically bound in
  8270. Org-mode to the values they have in Org-mode, and then interactively
  8271. call CMD."
  8272. (org-load-modules-maybe)
  8273. (unless org-local-vars
  8274. (setq org-local-vars (org-get-local-variables)))
  8275. (let (binds)
  8276. (dolist (var org-local-vars)
  8277. (when (or (not (boundp (car var)))
  8278. (eq (symbol-value (car var))
  8279. (default-value (car var))))
  8280. (push (list (car var) `(quote ,(cadr var))) binds)))
  8281. (eval `(let ,binds
  8282. (call-interactively (quote ,cmd))))))
  8283. ;;;; Archiving
  8284. (defun org-get-category (&optional pos force-refresh)
  8285. "Get the category applying to position POS."
  8286. (save-match-data
  8287. (if force-refresh (org-refresh-category-properties))
  8288. (let ((pos (or pos (point))))
  8289. (or (get-text-property pos 'org-category)
  8290. (progn (org-refresh-category-properties)
  8291. (get-text-property pos 'org-category))))))
  8292. (defun org-refresh-category-properties ()
  8293. "Refresh category text properties in the buffer."
  8294. (let ((case-fold-search t)
  8295. (inhibit-read-only t)
  8296. (def-cat (cond
  8297. ((null org-category)
  8298. (if buffer-file-name
  8299. (file-name-sans-extension
  8300. (file-name-nondirectory buffer-file-name))
  8301. "???"))
  8302. ((symbolp org-category) (symbol-name org-category))
  8303. (t org-category)))
  8304. beg end cat pos optionp)
  8305. (org-with-silent-modifications
  8306. (save-excursion
  8307. (save-restriction
  8308. (widen)
  8309. (goto-char (point-min))
  8310. (put-text-property (point) (point-max) 'org-category def-cat)
  8311. (while (re-search-forward
  8312. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8313. (setq pos (match-end 0)
  8314. optionp (equal (char-after (match-beginning 0)) ?#)
  8315. cat (org-trim (match-string 2)))
  8316. (if optionp
  8317. (setq beg (point-at-bol) end (point-max))
  8318. (org-back-to-heading t)
  8319. (setq beg (point) end (org-end-of-subtree t t)))
  8320. (put-text-property beg end 'org-category cat)
  8321. (put-text-property beg end 'org-category-position beg)
  8322. (goto-char pos)))))))
  8323. (defun org-refresh-properties (dprop tprop)
  8324. "Refresh buffer text properties.
  8325. DPROP is the drawer property and TPROP is the corresponding text
  8326. property to set."
  8327. (let ((case-fold-search t)
  8328. (inhibit-read-only t) p)
  8329. (org-with-silent-modifications
  8330. (save-excursion
  8331. (save-restriction
  8332. (widen)
  8333. (goto-char (point-min))
  8334. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8335. (setq p (org-match-string-no-properties 1))
  8336. (save-excursion
  8337. (org-back-to-heading t)
  8338. (put-text-property
  8339. (point-at-bol) (point-at-eol) tprop p))))))))
  8340. ;;;; Link Stuff
  8341. ;;; Link abbreviations
  8342. (defun org-link-expand-abbrev (link)
  8343. "Apply replacements as defined in `org-link-abbrev-alist'."
  8344. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8345. (let* ((key (match-string 1 link))
  8346. (as (or (assoc key org-link-abbrev-alist-local)
  8347. (assoc key org-link-abbrev-alist)))
  8348. (tag (and (match-end 2) (match-string 3 link)))
  8349. rpl)
  8350. (if (not as)
  8351. link
  8352. (setq rpl (cdr as))
  8353. (cond
  8354. ((symbolp rpl) (funcall rpl tag))
  8355. ((string-match "%(\\([^)]+\\))" rpl)
  8356. (replace-match
  8357. (save-match-data
  8358. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8359. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8360. ((string-match "%h" rpl)
  8361. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8362. (t (concat rpl tag)))))
  8363. link))
  8364. ;;; Storing and inserting links
  8365. (defvar org-insert-link-history nil
  8366. "Minibuffer history for links inserted with `org-insert-link'.")
  8367. (defvar org-stored-links nil
  8368. "Contains the links stored with `org-store-link'.")
  8369. (defvar org-store-link-plist nil
  8370. "Plist with info about the most recently link created with `org-store-link'.")
  8371. (defvar org-link-protocols nil
  8372. "Link protocols added to Org-mode using `org-add-link-type'.")
  8373. (defvar org-store-link-functions nil
  8374. "List of functions that are called to create and store a link.
  8375. Each function will be called in turn until one returns a non-nil
  8376. value. Each function should check if it is responsible for creating
  8377. this link (for example by looking at the major mode).
  8378. If not, it must exit and return nil.
  8379. If yes, it should return a non-nil value after a calling
  8380. `org-store-link-props' with a list of properties and values.
  8381. Special properties are:
  8382. :type The link prefix, like \"http\". This must be given.
  8383. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8384. This is obligatory as well.
  8385. :description Optional default description for the second pair
  8386. of brackets in an Org-mode link. The user can still change
  8387. this when inserting this link into an Org-mode buffer.
  8388. In addition to these, any additional properties can be specified
  8389. and then used in capture templates.")
  8390. (defun org-add-link-type (type &optional follow export)
  8391. "Add TYPE to the list of `org-link-types'.
  8392. Re-compute all regular expressions depending on `org-link-types'
  8393. FOLLOW and EXPORT are two functions.
  8394. FOLLOW should take the link path as the single argument and do whatever
  8395. is necessary to follow the link, for example find a file or display
  8396. a mail message.
  8397. EXPORT should format the link path for export to one of the export formats.
  8398. It should be a function accepting three arguments:
  8399. path the path of the link, the text after the prefix (like \"http:\")
  8400. desc the description of the link, if any, or a description added by
  8401. org-export-normalize-links if there is none
  8402. format the export format, a symbol like `html' or `latex' or `ascii'..
  8403. The function may use the FORMAT information to return different values
  8404. depending on the format. The return value will be put literally into
  8405. the exported file. If the return value is nil, this means Org should
  8406. do what it normally does with links which do not have EXPORT defined.
  8407. Org-mode has a built-in default for exporting links. If you are happy with
  8408. this default, there is no need to define an export function for the link
  8409. type. For a simple example of an export function, see `org-bbdb.el'."
  8410. (add-to-list 'org-link-types type t)
  8411. (org-make-link-regexps)
  8412. (if (assoc type org-link-protocols)
  8413. (setcdr (assoc type org-link-protocols) (list follow export))
  8414. (push (list type follow export) org-link-protocols)))
  8415. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8416. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8417. ;;;###autoload
  8418. (defun org-store-link (arg)
  8419. "\\<org-mode-map>Store an org-link to the current location.
  8420. This link is added to `org-stored-links' and can later be inserted
  8421. into an org-buffer with \\[org-insert-link].
  8422. For some link types, a prefix arg is interpreted.
  8423. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8424. For file links, arg negates `org-context-in-file-links'.
  8425. A double prefix arg force skipping storing functions that are not
  8426. part of Org's core.
  8427. A triple prefix arg force storing a link for each line in the
  8428. active region."
  8429. (interactive "P")
  8430. (org-load-modules-maybe)
  8431. (if (and (equal arg '(64)) (org-region-active-p))
  8432. (save-excursion
  8433. (let ((end (region-end)))
  8434. (goto-char (region-beginning))
  8435. (set-mark (point))
  8436. (while (< (point-at-eol) end)
  8437. (move-end-of-line 1) (activate-mark)
  8438. (let (current-prefix-arg)
  8439. (call-interactively 'org-store-link))
  8440. (move-beginning-of-line 2)
  8441. (set-mark (point)))))
  8442. (org-with-limited-levels
  8443. (setq org-store-link-plist nil)
  8444. (let (link cpltxt desc description search
  8445. txt custom-id agenda-link sfuns sfunsn)
  8446. (cond
  8447. ;; Store a link using an external link type
  8448. ((and (not (equal arg '(16)))
  8449. (setq sfuns
  8450. (delq
  8451. nil (mapcar (lambda (f)
  8452. (let (fs) (if (funcall f) (push f fs))))
  8453. org-store-link-functions))
  8454. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8455. (or (and (cdr sfuns)
  8456. (funcall (intern
  8457. (completing-read
  8458. "Which function for creating the link? "
  8459. sfunsn t (car sfunsn)))))
  8460. (funcall (caar sfuns)))
  8461. (setq link (plist-get org-store-link-plist :link)
  8462. desc (or (plist-get org-store-link-plist
  8463. :description) link))))
  8464. ;; Store a link from a source code buffer
  8465. ((org-src-edit-buffer-p)
  8466. (let (label gc)
  8467. (while (or (not label)
  8468. (save-excursion
  8469. (save-restriction
  8470. (widen)
  8471. (goto-char (point-min))
  8472. (re-search-forward
  8473. (regexp-quote (format org-coderef-label-format label))
  8474. nil t))))
  8475. (when label (message "Label exists already") (sit-for 2))
  8476. (setq label (read-string "Code line label: " label)))
  8477. (end-of-line 1)
  8478. (setq link (format org-coderef-label-format label))
  8479. (setq gc (- 79 (length link)))
  8480. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8481. (insert link)
  8482. (setq link (concat "(" label ")") desc nil)))
  8483. ;; We are in the agenda, link to referenced location
  8484. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8485. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8486. (get-text-property (point) 'org-marker))))
  8487. (when m
  8488. (org-with-point-at m
  8489. (setq agenda-link
  8490. (if (org-called-interactively-p 'any)
  8491. (call-interactively 'org-store-link)
  8492. (org-store-link nil)))))))
  8493. ((eq major-mode 'calendar-mode)
  8494. (let ((cd (calendar-cursor-to-date)))
  8495. (setq link
  8496. (format-time-string
  8497. (car org-time-stamp-formats)
  8498. (apply 'encode-time
  8499. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8500. nil nil nil))))
  8501. (org-store-link-props :type "calendar" :date cd)))
  8502. ((eq major-mode 'help-mode)
  8503. (setq link (concat "help:" (save-excursion
  8504. (goto-char (point-min))
  8505. (looking-at "^[^ ]+")
  8506. (match-string 0))))
  8507. (org-store-link-props :type "help"))
  8508. ((eq major-mode 'w3-mode)
  8509. (setq cpltxt (if (and (buffer-name)
  8510. (not (string-match "Untitled" (buffer-name))))
  8511. (buffer-name)
  8512. (url-view-url t))
  8513. link (url-view-url t))
  8514. (org-store-link-props :type "w3" :url (url-view-url t)))
  8515. ((eq major-mode 'image-mode)
  8516. (setq cpltxt (concat "file:"
  8517. (abbreviate-file-name buffer-file-name))
  8518. link cpltxt)
  8519. (org-store-link-props :type "image" :file buffer-file-name))
  8520. ;; In dired, store a link to the file of the current line
  8521. ((eq major-mode 'dired-mode)
  8522. (let ((file (dired-get-filename nil t)))
  8523. (setq file (if file
  8524. (abbreviate-file-name
  8525. (expand-file-name (dired-get-filename nil t)))
  8526. ;; otherwise, no file so use current directory.
  8527. default-directory))
  8528. (setq cpltxt (concat "file:" file)
  8529. link cpltxt)))
  8530. ((setq search (run-hook-with-args-until-success
  8531. 'org-create-file-search-functions))
  8532. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8533. "::" search))
  8534. (setq cpltxt (or description link)))
  8535. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8536. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8537. (cond
  8538. ;; Store a link using the radio target at point
  8539. ((org-in-regexp "<<\\(.*?\\)>>")
  8540. (setq cpltxt
  8541. (concat "file:"
  8542. (abbreviate-file-name
  8543. (buffer-file-name (buffer-base-buffer)))
  8544. "::" (match-string 1))
  8545. link cpltxt))
  8546. ((and (featurep 'org-id)
  8547. (or (eq org-id-link-to-org-use-id t)
  8548. (and (org-called-interactively-p 'any)
  8549. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8550. (and (eq org-id-link-to-org-use-id
  8551. 'create-if-interactive-and-no-custom-id)
  8552. (not custom-id))))
  8553. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8554. ;; Store a link using the ID at point
  8555. (setq link (condition-case nil
  8556. (prog1 (org-id-store-link)
  8557. (setq desc (plist-get org-store-link-plist
  8558. :description)))
  8559. (error
  8560. ;; Probably before first headline, link only to file
  8561. (concat "file:"
  8562. (abbreviate-file-name
  8563. (buffer-file-name (buffer-base-buffer))))))))
  8564. (t
  8565. ;; Just link to current headline
  8566. (setq cpltxt (concat "file:"
  8567. (abbreviate-file-name
  8568. (buffer-file-name (buffer-base-buffer)))))
  8569. ;; Add a context search string
  8570. (when (org-xor org-context-in-file-links arg)
  8571. (let* ((ee (org-element-at-point))
  8572. (et (org-element-type ee))
  8573. (ev (plist-get (cadr ee) :value))
  8574. (ek (plist-get (cadr ee) :key))
  8575. (eok (and (stringp ek) (string-match "name" ek))))
  8576. (setq txt (cond
  8577. ((org-at-heading-p) nil)
  8578. ((and (eq et 'keyword) eok) ev)
  8579. ((org-region-active-p)
  8580. (buffer-substring (region-beginning) (region-end)))))
  8581. (when (or (null txt) (string-match "\\S-" txt))
  8582. (setq cpltxt
  8583. (concat cpltxt "::"
  8584. (condition-case nil
  8585. (org-make-org-heading-search-string txt)
  8586. (error "")))
  8587. desc (or (and (eq et 'keyword) eok ev)
  8588. (nth 4 (ignore-errors (org-heading-components)))
  8589. "NONE")))))
  8590. (if (string-match "::\\'" cpltxt)
  8591. (setq cpltxt (substring cpltxt 0 -2)))
  8592. (setq link cpltxt))))
  8593. ((buffer-file-name (buffer-base-buffer))
  8594. ;; Just link to this file here.
  8595. (setq cpltxt (concat "file:"
  8596. (abbreviate-file-name
  8597. (buffer-file-name (buffer-base-buffer)))))
  8598. ;; Add a context string.
  8599. (when (org-xor org-context-in-file-links arg)
  8600. (setq txt (if (org-region-active-p)
  8601. (buffer-substring (region-beginning) (region-end))
  8602. (buffer-substring (point-at-bol) (point-at-eol))))
  8603. ;; Only use search option if there is some text.
  8604. (when (string-match "\\S-" txt)
  8605. (setq cpltxt
  8606. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8607. desc "NONE")))
  8608. (setq link cpltxt))
  8609. ((org-called-interactively-p 'interactive)
  8610. (user-error "No method for storing a link from this buffer"))
  8611. (t (setq link nil)))
  8612. ;; We're done setting link and desc, clean up
  8613. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8614. (setq link (or link cpltxt)
  8615. desc (or desc cpltxt))
  8616. (cond ((equal desc "NONE") (setq desc nil))
  8617. ((string-match org-bracket-link-analytic-regexp desc)
  8618. (let ((d0 (match-string 3 desc))
  8619. (p0 (match-string 5 desc)))
  8620. (setq desc
  8621. (replace-regexp-in-string
  8622. org-bracket-link-regexp
  8623. (concat (or p0 d0)
  8624. (if (equal (length (match-string 0 desc))
  8625. (length desc)) "*" "")) desc)))))
  8626. ;; Return the link
  8627. (if (not (and (or (org-called-interactively-p 'any)
  8628. executing-kbd-macro) link))
  8629. (or agenda-link (and link (org-make-link-string link desc)))
  8630. (push (list link desc) org-stored-links)
  8631. (message "Stored: %s" (or desc link))
  8632. (when custom-id
  8633. (setq link (concat "file:" (abbreviate-file-name
  8634. (buffer-file-name)) "::#" custom-id))
  8635. (push (list link desc) org-stored-links)))))))
  8636. (defun org-store-link-props (&rest plist)
  8637. "Store link properties, extract names and addresses."
  8638. (let (x adr)
  8639. (when (setq x (plist-get plist :from))
  8640. (setq adr (mail-extract-address-components x))
  8641. (setq plist (plist-put plist :fromname (car adr)))
  8642. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8643. (when (setq x (plist-get plist :to))
  8644. (setq adr (mail-extract-address-components x))
  8645. (setq plist (plist-put plist :toname (car adr)))
  8646. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8647. (let ((from (plist-get plist :from))
  8648. (to (plist-get plist :to)))
  8649. (when (and from to org-from-is-user-regexp)
  8650. (setq plist
  8651. (plist-put plist :fromto
  8652. (if (string-match org-from-is-user-regexp from)
  8653. (concat "to %t")
  8654. (concat "from %f"))))))
  8655. (setq org-store-link-plist plist))
  8656. (defun org-add-link-props (&rest plist)
  8657. "Add these properties to the link property list."
  8658. (let (key value)
  8659. (while plist
  8660. (setq key (pop plist) value (pop plist))
  8661. (setq org-store-link-plist
  8662. (plist-put org-store-link-plist key value)))))
  8663. (defun org-email-link-description (&optional fmt)
  8664. "Return the description part of an email link.
  8665. This takes information from `org-store-link-plist' and formats it
  8666. according to FMT (default from `org-email-link-description-format')."
  8667. (setq fmt (or fmt org-email-link-description-format))
  8668. (let* ((p org-store-link-plist)
  8669. (to (plist-get p :toaddress))
  8670. (from (plist-get p :fromaddress))
  8671. (table
  8672. (list
  8673. (cons "%c" (plist-get p :fromto))
  8674. (cons "%F" (plist-get p :from))
  8675. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8676. (cons "%T" (plist-get p :to))
  8677. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8678. (cons "%s" (plist-get p :subject))
  8679. (cons "%d" (plist-get p :date))
  8680. (cons "%m" (plist-get p :message-id)))))
  8681. (when (string-match "%c" fmt)
  8682. ;; Check if the user wrote this message
  8683. (if (and org-from-is-user-regexp from to
  8684. (save-match-data (string-match org-from-is-user-regexp from)))
  8685. (setq fmt (replace-match "to %t" t t fmt))
  8686. (setq fmt (replace-match "from %f" t t fmt))))
  8687. (org-replace-escapes fmt table)))
  8688. (defun org-make-org-heading-search-string (&optional string)
  8689. "Make search string for the current headline or STRING."
  8690. (let ((s (or string
  8691. (and (derived-mode-p 'org-mode)
  8692. (save-excursion
  8693. (org-back-to-heading t)
  8694. (org-element-property :raw-value (org-element-at-point))))))
  8695. (lines org-context-in-file-links))
  8696. (or string (setq s (concat "*" s))) ; Add * for headlines
  8697. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8698. (when (and string (integerp lines) (> lines 0))
  8699. (let ((slines (org-split-string s "\n")))
  8700. (when (< lines (length slines))
  8701. (setq s (mapconcat
  8702. 'identity
  8703. (reverse (nthcdr (- (length slines) lines)
  8704. (reverse slines))) "\n")))))
  8705. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8706. (defun org-make-link-string (link &optional description)
  8707. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8708. (unless (string-match "\\S-" link)
  8709. (error "Empty link"))
  8710. (when (and description
  8711. (stringp description)
  8712. (not (string-match "\\S-" description)))
  8713. (setq description nil))
  8714. (when (stringp description)
  8715. ;; Remove brackets from the description, they are fatal.
  8716. (while (string-match "\\[" description)
  8717. (setq description (replace-match "{" t t description)))
  8718. (while (string-match "\\]" description)
  8719. (setq description (replace-match "}" t t description))))
  8720. (when (equal link description)
  8721. ;; No description needed, it is identical
  8722. (setq description nil))
  8723. (when (and (not description)
  8724. (not (string-match (org-image-file-name-regexp) link))
  8725. (not (equal link (org-link-escape link))))
  8726. (setq description (org-extract-attributes link)))
  8727. (setq link
  8728. (cond ((string-match (org-image-file-name-regexp) link) link)
  8729. ((string-match org-link-types-re link)
  8730. (concat (match-string 1 link)
  8731. (org-link-escape (substring link (match-end 1)))))
  8732. (t (org-link-escape link))))
  8733. (concat "[[" link "]"
  8734. (if description (concat "[" description "]") "")
  8735. "]"))
  8736. (defconst org-link-escape-chars
  8737. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8738. "List of characters that should be escaped in link.
  8739. This is the list that is used for internal purposes.")
  8740. (defconst org-link-escape-chars-browser
  8741. '(?\ )
  8742. "List of escapes for characters that are problematic in links.
  8743. This is the list that is used before handing over to the browser.")
  8744. (defun org-link-escape (text &optional table merge)
  8745. "Return percent escaped representation of TEXT.
  8746. TEXT is a string with the text to escape.
  8747. Optional argument TABLE is a list with characters that should be
  8748. escaped. When nil, `org-link-escape-chars' is used.
  8749. If optional argument MERGE is set, merge TABLE into
  8750. `org-link-escape-chars'."
  8751. (cond
  8752. ((and table merge)
  8753. (mapc (lambda (defchr)
  8754. (unless (member defchr table)
  8755. (setq table (cons defchr table)))) org-link-escape-chars))
  8756. ((null table)
  8757. (setq table org-link-escape-chars)))
  8758. (mapconcat
  8759. (lambda (char)
  8760. (if (or (member char table)
  8761. (and (or (< char 32) (= char 37) (> char 126))
  8762. org-url-hexify-p))
  8763. (mapconcat (lambda (sequence-element)
  8764. (format "%%%.2X" sequence-element))
  8765. (or (encode-coding-char char 'utf-8)
  8766. (error "Unable to percent escape character: %s"
  8767. (char-to-string char))) "")
  8768. (char-to-string char))) text ""))
  8769. (defun org-link-unescape (str)
  8770. "Unhex hexified Unicode strings as returned from the JavaScript function
  8771. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8772. (unless (and (null str) (string= "" str))
  8773. (let ((pos 0) (case-fold-search t) unhexed)
  8774. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8775. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8776. (setq str (replace-match unhexed t t str))
  8777. (setq pos (+ pos (length unhexed))))))
  8778. str)
  8779. (defun org-link-unescape-compound (hex)
  8780. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8781. Note: this function also decodes single byte encodings like
  8782. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8783. (save-match-data
  8784. (let* ((bytes (cdr (split-string hex "%")))
  8785. (ret "")
  8786. (eat 0)
  8787. (sum 0))
  8788. (while bytes
  8789. (let* ((val (string-to-number (pop bytes) 16))
  8790. (shift-xor
  8791. (if (= 0 eat)
  8792. (cond
  8793. ((>= val 252) (cons 6 252))
  8794. ((>= val 248) (cons 5 248))
  8795. ((>= val 240) (cons 4 240))
  8796. ((>= val 224) (cons 3 224))
  8797. ((>= val 192) (cons 2 192))
  8798. (t (cons 0 0)))
  8799. (cons 6 128))))
  8800. (if (>= val 192) (setq eat (car shift-xor)))
  8801. (setq val (logxor val (cdr shift-xor)))
  8802. (setq sum (+ (lsh sum (car shift-xor)) val))
  8803. (if (> eat 0) (setq eat (- eat 1)))
  8804. (cond
  8805. ((= 0 eat) ;multi byte
  8806. (setq ret (concat ret (org-char-to-string sum)))
  8807. (setq sum 0))
  8808. ((not bytes) ; single byte(s)
  8809. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8810. )) ;; end (while bytes
  8811. ret )))
  8812. (defun org-link-unescape-single-byte-sequence (hex)
  8813. "Unhexify hex-encoded single byte character sequences."
  8814. (mapconcat (lambda (byte)
  8815. (char-to-string (string-to-number byte 16)))
  8816. (cdr (split-string hex "%")) ""))
  8817. (defun org-xor (a b)
  8818. "Exclusive or."
  8819. (if a (not b) b))
  8820. (defun org-fixup-message-id-for-http (s)
  8821. "Replace special characters in a message id, so it can be used in an http query."
  8822. (when (string-match "%" s)
  8823. (setq s (mapconcat (lambda (c)
  8824. (if (eq c ?%)
  8825. "%25"
  8826. (char-to-string c)))
  8827. s "")))
  8828. (while (string-match "<" s)
  8829. (setq s (replace-match "%3C" t t s)))
  8830. (while (string-match ">" s)
  8831. (setq s (replace-match "%3E" t t s)))
  8832. (while (string-match "@" s)
  8833. (setq s (replace-match "%40" t t s)))
  8834. s)
  8835. (defun org-link-prettify (link)
  8836. "Return a human-readable representation of LINK.
  8837. The car of LINK must be a raw link the cdr of LINK must be either
  8838. a link description or nil."
  8839. (let ((desc (or (cadr link) "<no description>")))
  8840. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8841. "<" (car link) ">")))
  8842. ;;;###autoload
  8843. (defun org-insert-link-global ()
  8844. "Insert a link like Org-mode does.
  8845. This command can be called in any mode to insert a link in Org-mode syntax."
  8846. (interactive)
  8847. (org-load-modules-maybe)
  8848. (org-run-like-in-org-mode 'org-insert-link))
  8849. (defun org-insert-all-links (&optional keep)
  8850. "Insert all links in `org-stored-links'."
  8851. (interactive "P")
  8852. (let ((links (copy-sequence org-stored-links)) l)
  8853. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8854. (insert "- ")
  8855. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8856. (insert "\n"))))
  8857. (defun org-link-fontify-links-to-this-file ()
  8858. "Fontify links to the current file in `org-stored-links'."
  8859. (let ((f (buffer-file-name)) a b)
  8860. (setq a (mapcar (lambda(l)
  8861. (let ((ll (car l)))
  8862. (when (and (string-match "^file:\\(.+\\)::" ll)
  8863. (equal f (expand-file-name (match-string 1 ll))))
  8864. ll)))
  8865. org-stored-links))
  8866. (when (featurep 'org-id)
  8867. (setq b (mapcar (lambda(l)
  8868. (let ((ll (car l)))
  8869. (when (and (string-match "^id:\\(.+\\)$" ll)
  8870. (equal f (expand-file-name
  8871. (or (org-id-find-id-file
  8872. (match-string 1 ll)) ""))))
  8873. ll)))
  8874. org-stored-links)))
  8875. (mapcar (lambda(l)
  8876. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8877. (delq nil (append a b)))))
  8878. (defvar org-link-links-in-this-file nil)
  8879. (defun org-insert-link (&optional complete-file link-location default-description)
  8880. "Insert a link. At the prompt, enter the link.
  8881. Completion can be used to insert any of the link protocol prefixes like
  8882. http or ftp in use.
  8883. The history can be used to select a link previously stored with
  8884. `org-store-link'. When the empty string is entered (i.e. if you just
  8885. press RET at the prompt), the link defaults to the most recently
  8886. stored link. As SPC triggers completion in the minibuffer, you need to
  8887. use M-SPC or C-q SPC to force the insertion of a space character.
  8888. You will also be prompted for a description, and if one is given, it will
  8889. be displayed in the buffer instead of the link.
  8890. If there is already a link at point, this command will allow you to edit link
  8891. and description parts.
  8892. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8893. be selected using completion. The path to the file will be relative to the
  8894. current directory if the file is in the current directory or a subdirectory.
  8895. Otherwise, the link will be the absolute path as completed in the minibuffer
  8896. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8897. option `org-link-file-path-type'.
  8898. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8899. the current directory or below.
  8900. With three \\[universal-argument] prefixes, negate the meaning of
  8901. `org-keep-stored-link-after-insertion'.
  8902. If `org-make-link-description-function' is non-nil, this function will be
  8903. called with the link target, and the result will be the default
  8904. link description.
  8905. If the LINK-LOCATION parameter is non-nil, this value will be
  8906. used as the link location instead of reading one interactively.
  8907. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8908. be used as the default description."
  8909. (interactive "P")
  8910. (let* ((wcf (current-window-configuration))
  8911. (origbuf (current-buffer))
  8912. (region (if (org-region-active-p)
  8913. (buffer-substring (region-beginning) (region-end))))
  8914. (remove (and region (list (region-beginning) (region-end))))
  8915. (desc region)
  8916. tmphist ; byte-compile incorrectly complains about this
  8917. (link link-location)
  8918. (abbrevs org-link-abbrev-alist-local)
  8919. entry file all-prefixes auto-desc)
  8920. (cond
  8921. (link-location) ; specified by arg, just use it.
  8922. ((org-in-regexp org-bracket-link-regexp 1)
  8923. ;; We do have a link at point, and we are going to edit it.
  8924. (setq remove (list (match-beginning 0) (match-end 0)))
  8925. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8926. (setq link (read-string "Link: "
  8927. (org-link-unescape
  8928. (org-match-string-no-properties 1)))))
  8929. ((or (org-in-regexp org-angle-link-re)
  8930. (org-in-regexp org-plain-link-re))
  8931. ;; Convert to bracket link
  8932. (setq remove (list (match-beginning 0) (match-end 0))
  8933. link (read-string "Link: "
  8934. (org-remove-angle-brackets (match-string 0)))))
  8935. ((member complete-file '((4) (16)))
  8936. ;; Completing read for file names.
  8937. (setq link (org-file-complete-link complete-file)))
  8938. (t
  8939. ;; Read link, with completion for stored links.
  8940. (org-link-fontify-links-to-this-file)
  8941. (org-switch-to-buffer-other-window "*Org Links*")
  8942. (with-current-buffer "*Org Links*"
  8943. (erase-buffer)
  8944. (insert "Insert a link.
  8945. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8946. (when org-stored-links
  8947. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8948. (insert (mapconcat 'org-link-prettify
  8949. (reverse org-stored-links) "\n")))
  8950. (goto-char (point-min)))
  8951. (let ((cw (selected-window)))
  8952. (select-window (get-buffer-window "*Org Links*" 'visible))
  8953. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8954. (unless (pos-visible-in-window-p (point-max))
  8955. (org-fit-window-to-buffer))
  8956. (and (window-live-p cw) (select-window cw)))
  8957. ;; Fake a link history, containing the stored links.
  8958. (setq tmphist (append (mapcar 'car org-stored-links)
  8959. org-insert-link-history))
  8960. (setq all-prefixes (append (mapcar 'car abbrevs)
  8961. (mapcar 'car org-link-abbrev-alist)
  8962. org-link-types))
  8963. (unwind-protect
  8964. (progn
  8965. (setq link
  8966. (org-completing-read
  8967. "Link: "
  8968. (append
  8969. (mapcar (lambda (x) (concat x ":"))
  8970. all-prefixes)
  8971. (mapcar 'car org-stored-links))
  8972. nil nil nil
  8973. 'tmphist
  8974. (caar org-stored-links)))
  8975. (if (not (string-match "\\S-" link))
  8976. (user-error "No link selected"))
  8977. (mapc (lambda(l)
  8978. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8979. org-stored-links)
  8980. (if (or (member link all-prefixes)
  8981. (and (equal ":" (substring link -1))
  8982. (member (substring link 0 -1) all-prefixes)
  8983. (setq link (substring link 0 -1))))
  8984. (setq link (with-current-buffer origbuf
  8985. (org-link-try-special-completion link)))))
  8986. (set-window-configuration wcf)
  8987. (kill-buffer "*Org Links*"))
  8988. (setq entry (assoc link org-stored-links))
  8989. (or entry (push link org-insert-link-history))
  8990. (setq desc (or desc (nth 1 entry)))))
  8991. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8992. (not org-keep-stored-link-after-insertion))
  8993. (setq org-stored-links (delq (assoc link org-stored-links)
  8994. org-stored-links)))
  8995. (if (string-match org-plain-link-re link)
  8996. ;; URL-like link, normalize the use of angular brackets.
  8997. (setq link (org-remove-angle-brackets link)))
  8998. ;; Check if we are linking to the current file with a search
  8999. ;; option If yes, simplify the link by using only the search
  9000. ;; option.
  9001. (when (and buffer-file-name
  9002. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9003. (let* ((path (match-string 1 link))
  9004. (case-fold-search nil)
  9005. (search (match-string 2 link)))
  9006. (save-match-data
  9007. (if (equal (file-truename buffer-file-name) (file-truename path))
  9008. ;; We are linking to this same file, with a search option
  9009. (setq link search)))))
  9010. ;; Check if we can/should use a relative path. If yes, simplify the link
  9011. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9012. (let* ((type (match-string 1 link))
  9013. (path (match-string 2 link))
  9014. (origpath path)
  9015. (case-fold-search nil))
  9016. (cond
  9017. ((or (eq org-link-file-path-type 'absolute)
  9018. (equal complete-file '(16)))
  9019. (setq path (abbreviate-file-name (expand-file-name path))))
  9020. ((eq org-link-file-path-type 'noabbrev)
  9021. (setq path (expand-file-name path)))
  9022. ((eq org-link-file-path-type 'relative)
  9023. (setq path (file-relative-name path)))
  9024. (t
  9025. (save-match-data
  9026. (if (string-match (concat "^" (regexp-quote
  9027. (expand-file-name
  9028. (file-name-as-directory
  9029. default-directory))))
  9030. (expand-file-name path))
  9031. ;; We are linking a file with relative path name.
  9032. (setq path (substring (expand-file-name path)
  9033. (match-end 0)))
  9034. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9035. (setq link (concat type path))
  9036. (if (equal desc origpath)
  9037. (setq desc path))))
  9038. (if org-make-link-description-function
  9039. (setq desc
  9040. (or (condition-case nil
  9041. (funcall org-make-link-description-function link desc)
  9042. (error (progn (message "Can't get link description from `%s'"
  9043. (symbol-name org-make-link-description-function))
  9044. (sit-for 2) nil)))
  9045. (read-string "Description: " default-description)))
  9046. (if default-description (setq desc default-description)
  9047. (setq desc (or (and auto-desc desc)
  9048. (read-string "Description: " desc)))))
  9049. (unless (string-match "\\S-" desc) (setq desc nil))
  9050. (if remove (apply 'delete-region remove))
  9051. (insert (org-make-link-string link desc))))
  9052. (defun org-link-try-special-completion (type)
  9053. "If there is completion support for link type TYPE, offer it."
  9054. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9055. (if (functionp fun)
  9056. (funcall fun)
  9057. (read-string "Link (no completion support): " (concat type ":")))))
  9058. (defun org-file-complete-link (&optional arg)
  9059. "Create a file link using completion."
  9060. (let (file link)
  9061. (setq file (org-iread-file-name "File: "))
  9062. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9063. (pwd1 (file-name-as-directory (abbreviate-file-name
  9064. (expand-file-name ".")))))
  9065. (cond
  9066. ((equal arg '(16))
  9067. (setq link (concat
  9068. "file:"
  9069. (abbreviate-file-name (expand-file-name file)))))
  9070. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9071. (setq link (concat "file:" (match-string 1 file))))
  9072. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9073. (expand-file-name file))
  9074. (setq link (concat
  9075. "file:" (match-string 1 (expand-file-name file)))))
  9076. (t (setq link (concat "file:" file)))))
  9077. link))
  9078. (defun org-iread-file-name (&rest args)
  9079. "Read-file-name using `ido-mode' speedup if available.
  9080. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9081. See `read-file-name' for a description of parameters."
  9082. (org-without-partial-completion
  9083. (if (and org-completion-use-ido
  9084. (fboundp 'ido-read-file-name)
  9085. (boundp 'ido-mode) ido-mode
  9086. (listp (second args)))
  9087. (let ((ido-enter-matching-directory nil))
  9088. (apply 'ido-read-file-name args))
  9089. (apply 'read-file-name args))))
  9090. (defun org-completing-read (&rest args)
  9091. "Completing-read with SPACE being a normal character."
  9092. (let ((enable-recursive-minibuffers t)
  9093. (minibuffer-local-completion-map
  9094. (copy-keymap minibuffer-local-completion-map)))
  9095. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9096. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9097. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9098. (apply 'org-icompleting-read args)))
  9099. (defun org-completing-read-no-i (&rest args)
  9100. (let (org-completion-use-ido org-completion-use-iswitchb)
  9101. (apply 'org-completing-read args)))
  9102. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9103. "Use iswitch as a completing-read replacement to choose from choices.
  9104. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9105. from."
  9106. (let* ((iswitchb-use-virtual-buffers nil)
  9107. (iswitchb-make-buflist-hook
  9108. (lambda ()
  9109. (setq iswitchb-temp-buflist choices))))
  9110. (iswitchb-read-buffer prompt)))
  9111. (defun org-icompleting-read (&rest args)
  9112. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9113. (org-without-partial-completion
  9114. (if (and org-completion-use-ido
  9115. (fboundp 'ido-completing-read)
  9116. (boundp 'ido-mode) ido-mode
  9117. (listp (second args)))
  9118. (let ((ido-enter-matching-directory nil))
  9119. (apply 'ido-completing-read (concat (car args))
  9120. (if (consp (car (nth 1 args)))
  9121. (mapcar 'car (nth 1 args))
  9122. (nth 1 args))
  9123. (cddr args)))
  9124. (if (and org-completion-use-iswitchb
  9125. (boundp 'iswitchb-mode) iswitchb-mode
  9126. (listp (second args)))
  9127. (apply 'org-iswitchb-completing-read (concat (car args))
  9128. (if (consp (car (nth 1 args)))
  9129. (mapcar 'car (nth 1 args))
  9130. (nth 1 args))
  9131. (cddr args))
  9132. (apply 'completing-read args)))))
  9133. (defun org-extract-attributes (s)
  9134. "Extract the attributes cookie from a string and set as text property."
  9135. (let (a attr (start 0) key value)
  9136. (save-match-data
  9137. (when (string-match "{{\\([^}]+\\)}}$" s)
  9138. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9139. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9140. (setq key (match-string 1 a) value (match-string 2 a)
  9141. start (match-end 0)
  9142. attr (plist-put attr (intern key) value))))
  9143. (org-add-props s nil 'org-attr attr))
  9144. s))
  9145. ;;; Opening/following a link
  9146. (defvar org-link-search-failed nil)
  9147. (defvar org-open-link-functions nil
  9148. "Hook for functions finding a plain text link.
  9149. These functions must take a single argument, the link content.
  9150. They will be called for links that look like [[link text][description]]
  9151. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9152. like a filename (e.g. \"./blue.png\").
  9153. These functions will be called *before* Org attempts to resolve the
  9154. link by doing text searches in the current buffer - so if you want a
  9155. link \"[[target]]\" to still find \"<<target>>\", your function should
  9156. handle this as a special case.
  9157. When the function does handle the link, it must return a non-nil value.
  9158. If it decides that it is not responsible for this link, it must return
  9159. nil to indicate that that Org-mode can continue with other options
  9160. like exact and fuzzy text search.")
  9161. (defun org-next-link (&optional search-backward)
  9162. "Move forward to the next link.
  9163. If the link is in hidden text, expose it."
  9164. (interactive "P")
  9165. (when (and org-link-search-failed (eq this-command last-command))
  9166. (goto-char (point-min))
  9167. (message "Link search wrapped back to beginning of buffer"))
  9168. (setq org-link-search-failed nil)
  9169. (let* ((pos (point))
  9170. (ct (org-context))
  9171. (a (assoc :link ct))
  9172. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9173. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9174. ((looking-at org-any-link-re)
  9175. ;; Don't stay stuck at link without an org-link face
  9176. (forward-char (if search-backward -1 1))))
  9177. (if (funcall srch-fun org-any-link-re nil t)
  9178. (progn
  9179. (goto-char (match-beginning 0))
  9180. (if (outline-invisible-p) (org-show-context)))
  9181. (goto-char pos)
  9182. (setq org-link-search-failed t)
  9183. (message "No further link found"))))
  9184. (defun org-previous-link ()
  9185. "Move backward to the previous link.
  9186. If the link is in hidden text, expose it."
  9187. (interactive)
  9188. (funcall 'org-next-link t))
  9189. (defun org-translate-link (s)
  9190. "Translate a link string if a translation function has been defined."
  9191. (if (and org-link-translation-function
  9192. (fboundp org-link-translation-function)
  9193. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9194. (progn
  9195. (setq s (funcall org-link-translation-function
  9196. (match-string 1 s) (match-string 2 s)))
  9197. (concat (car s) ":" (cdr s)))
  9198. s))
  9199. (defun org-translate-link-from-planner (type path)
  9200. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9201. This is still an experimental function, your mileage may vary."
  9202. (cond
  9203. ((member type '("http" "https" "news" "ftp"))
  9204. ;; standard Internet links are the same.
  9205. nil)
  9206. ((and (equal type "irc") (string-match "^//" path))
  9207. ;; Planner has two / at the beginning of an irc link, we have 1.
  9208. ;; We should have zero, actually....
  9209. (setq path (substring path 1)))
  9210. ((and (equal type "lisp") (string-match "^/" path))
  9211. ;; Planner has a slash, we do not.
  9212. (setq type "elisp" path (substring path 1)))
  9213. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9214. ;; A typical message link. Planner has the id after the final slash,
  9215. ;; we separate it with a hash mark
  9216. (setq path (concat (match-string 1 path) "#"
  9217. (org-remove-angle-brackets (match-string 2 path))))))
  9218. (cons type path))
  9219. (defun org-find-file-at-mouse (ev)
  9220. "Open file link or URL at mouse."
  9221. (interactive "e")
  9222. (mouse-set-point ev)
  9223. (org-open-at-point 'in-emacs))
  9224. (defun org-open-at-mouse (ev)
  9225. "Open file link or URL at mouse.
  9226. See the docstring of `org-open-file' for details."
  9227. (interactive "e")
  9228. (mouse-set-point ev)
  9229. (if (eq major-mode 'org-agenda-mode)
  9230. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9231. (org-open-at-point))
  9232. (defvar org-window-config-before-follow-link nil
  9233. "The window configuration before following a link.
  9234. This is saved in case the need arises to restore it.")
  9235. (defvar org-open-link-marker (make-marker)
  9236. "Marker pointing to the location where `org-open-at-point; was called.")
  9237. ;;;###autoload
  9238. (defun org-open-at-point-global ()
  9239. "Follow a link like Org-mode does.
  9240. This command can be called in any mode to follow a link that has
  9241. Org-mode syntax."
  9242. (interactive)
  9243. (org-run-like-in-org-mode 'org-open-at-point))
  9244. ;;;###autoload
  9245. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9246. "Open a link in the string S, as if it was in Org-mode."
  9247. (interactive "sLink: \nP")
  9248. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9249. (with-temp-buffer
  9250. (let ((org-inhibit-startup (not reference-buffer)))
  9251. (org-mode)
  9252. (insert s)
  9253. (goto-char (point-min))
  9254. (when reference-buffer
  9255. (setq org-link-abbrev-alist-local
  9256. (with-current-buffer reference-buffer
  9257. org-link-abbrev-alist-local)))
  9258. (org-open-at-point arg reference-buffer)))))
  9259. (defvar org-open-at-point-functions nil
  9260. "Hook that is run when following a link at point.
  9261. Functions in this hook must return t if they identify and follow
  9262. a link at point. If they don't find anything interesting at point,
  9263. they must return nil.")
  9264. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9265. (defun org-open-at-point (&optional arg reference-buffer)
  9266. "Open link at or after point.
  9267. If there is no link at point, this function will search forward up to
  9268. the end of the current line.
  9269. Normally, files will be opened by an appropriate application. If the
  9270. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9271. With a double prefix argument, try to open outside of Emacs, in the
  9272. application the system uses for this file type."
  9273. (interactive "P")
  9274. ;; if in a code block, then open the block's results
  9275. (unless (call-interactively #'org-babel-open-src-block-result)
  9276. (org-load-modules-maybe)
  9277. (move-marker org-open-link-marker (point))
  9278. (setq org-window-config-before-follow-link (current-window-configuration))
  9279. (org-remove-occur-highlights nil nil t)
  9280. (cond
  9281. ((and (org-at-heading-p)
  9282. (not (org-at-timestamp-p t))
  9283. (not (org-in-regexp
  9284. (concat org-plain-link-re "\\|"
  9285. org-bracket-link-regexp "\\|"
  9286. org-angle-link-re "\\|"
  9287. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9288. (not (get-text-property (point) 'org-linked-text)))
  9289. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9290. (lk0 (car lkall))
  9291. (lk (if (stringp lk0) (list lk0) lk0))
  9292. (lkend (cdr lkall)))
  9293. (mapcar (lambda(l)
  9294. (search-forward l nil lkend)
  9295. (goto-char (match-beginning 0))
  9296. (org-open-at-point))
  9297. lk))
  9298. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9299. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9300. ((and (org-at-timestamp-p t)
  9301. (not (org-in-regexp org-bracket-link-regexp)))
  9302. (org-follow-timestamp-link))
  9303. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9304. (not (org-in-regexp org-any-link-re)))
  9305. (org-footnote-action))
  9306. (t
  9307. (let (type path link line search (pos (point)))
  9308. (catch 'match
  9309. (save-excursion
  9310. (skip-chars-forward "^]\n\r")
  9311. (when (org-in-regexp org-bracket-link-regexp 1)
  9312. (setq link (org-extract-attributes
  9313. (org-link-unescape (org-match-string-no-properties 1))))
  9314. (while (string-match " *\n *" link)
  9315. (setq link (replace-match " " t t link)))
  9316. (setq link (org-link-expand-abbrev link))
  9317. (cond
  9318. ((or (file-name-absolute-p link)
  9319. (string-match "^\\.\\.?/" link))
  9320. (setq type "file" path link))
  9321. ((string-match org-link-re-with-space3 link)
  9322. (setq type (match-string 1 link) path (match-string 2 link)))
  9323. ((string-match "^help:+\\(.+\\)" link)
  9324. (setq type "help" path (match-string 1 link)))
  9325. (t (setq type "thisfile" path link)))
  9326. (throw 'match t)))
  9327. (when (get-text-property (point) 'org-linked-text)
  9328. (setq type "thisfile"
  9329. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9330. (1+ (point)) (point))
  9331. path (buffer-substring
  9332. (or (previous-single-property-change pos 'org-linked-text)
  9333. (point-min))
  9334. (or (next-single-property-change pos 'org-linked-text)
  9335. (point-max))))
  9336. (throw 'match t))
  9337. (save-excursion
  9338. (when (or (org-in-regexp org-angle-link-re)
  9339. (let ((match (org-in-regexp org-plain-link-re)))
  9340. ;; Check a plain link is not within a bracket link
  9341. (and match
  9342. (save-excursion
  9343. (progn
  9344. (goto-char (car match))
  9345. (not (org-in-regexp org-bracket-link-regexp))))))
  9346. (let ((line_ending (save-excursion (end-of-line) (point))))
  9347. ;; We are in a line before a plain or bracket link
  9348. (or (re-search-forward org-plain-link-re line_ending t)
  9349. (re-search-forward org-bracket-link-regexp line_ending t))))
  9350. (setq type (match-string 1)
  9351. path (org-link-unescape (match-string 2)))
  9352. (throw 'match t)))
  9353. (save-excursion
  9354. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9355. (setq type "tags"
  9356. path (match-string 1))
  9357. (while (string-match ":" path)
  9358. (setq path (replace-match "+" t t path)))
  9359. (throw 'match t)))
  9360. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9361. (setq type "tree-match"
  9362. path (match-string 1))
  9363. (throw 'match t)))
  9364. (unless path
  9365. (user-error "No link found"))
  9366. ;; switch back to reference buffer
  9367. ;; needed when if called in a temporary buffer through
  9368. ;; org-open-link-from-string
  9369. (with-current-buffer (or reference-buffer (current-buffer))
  9370. ;; Remove any trailing spaces in path
  9371. (if (string-match " +\\'" path)
  9372. (setq path (replace-match "" t t path)))
  9373. (if (and org-link-translation-function
  9374. (fboundp org-link-translation-function))
  9375. ;; Check if we need to translate the link
  9376. (let ((tmp (funcall org-link-translation-function type path)))
  9377. (setq type (car tmp) path (cdr tmp))))
  9378. (cond
  9379. ((assoc type org-link-protocols)
  9380. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9381. ((equal type "help")
  9382. (let ((f-or-v (intern path)))
  9383. (cond ((fboundp f-or-v)
  9384. (describe-function f-or-v))
  9385. ((boundp f-or-v)
  9386. (describe-variable f-or-v))
  9387. (t (error "Not a known function or variable")))))
  9388. ((equal type "mailto")
  9389. (let ((cmd (car org-link-mailto-program))
  9390. (args (cdr org-link-mailto-program)) args1
  9391. (address path) (subject "") a)
  9392. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9393. (setq address (match-string 1 path)
  9394. subject (org-link-escape (match-string 2 path))))
  9395. (while args
  9396. (cond
  9397. ((not (stringp (car args))) (push (pop args) args1))
  9398. (t (setq a (pop args))
  9399. (if (string-match "%a" a)
  9400. (setq a (replace-match address t t a)))
  9401. (if (string-match "%s" a)
  9402. (setq a (replace-match subject t t a)))
  9403. (push a args1))))
  9404. (apply cmd (nreverse args1))))
  9405. ((member type '("http" "https" "ftp" "news"))
  9406. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9407. (org-link-escape
  9408. path org-link-escape-chars-browser)
  9409. path))))
  9410. ((string= type "doi")
  9411. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9412. (org-link-escape
  9413. path org-link-escape-chars-browser)
  9414. path))))
  9415. ((member type '("message"))
  9416. (browse-url (concat type ":" path)))
  9417. ((string= type "tags")
  9418. (org-tags-view arg path))
  9419. ((string= type "tree-match")
  9420. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9421. ((string= type "file")
  9422. (if (string-match "::\\([0-9]+\\)\\'" path)
  9423. (setq line (string-to-number (match-string 1 path))
  9424. path (substring path 0 (match-beginning 0)))
  9425. (if (string-match "::\\(.+\\)\\'" path)
  9426. (setq search (match-string 1 path)
  9427. path (substring path 0 (match-beginning 0)))))
  9428. (if (string-match "[*?{]" (file-name-nondirectory path))
  9429. (dired path)
  9430. (org-open-file path arg line search)))
  9431. ((string= type "shell")
  9432. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9433. (cmd path))
  9434. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9435. (string-match org-confirm-shell-link-not-regexp cmd))
  9436. (not org-confirm-shell-link-function)
  9437. (funcall org-confirm-shell-link-function
  9438. (format "Execute \"%s\" in shell? "
  9439. (org-add-props cmd nil
  9440. 'face 'org-warning))))
  9441. (progn
  9442. (message "Executing %s" cmd)
  9443. (shell-command cmd buf)
  9444. (if (featurep 'midnight)
  9445. (setq clean-buffer-list-kill-buffer-names
  9446. (cons buf clean-buffer-list-kill-buffer-names))))
  9447. (error "Abort"))))
  9448. ((string= type "elisp")
  9449. (let ((cmd path))
  9450. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9451. (string-match org-confirm-elisp-link-not-regexp cmd))
  9452. (not org-confirm-elisp-link-function)
  9453. (funcall org-confirm-elisp-link-function
  9454. (format "Execute \"%s\" as elisp? "
  9455. (org-add-props cmd nil
  9456. 'face 'org-warning))))
  9457. (message "%s => %s" cmd
  9458. (if (equal (string-to-char cmd) ?\()
  9459. (eval (read cmd))
  9460. (call-interactively (read cmd))))
  9461. (error "Abort"))))
  9462. ((and (string= type "thisfile")
  9463. (run-hook-with-args-until-success
  9464. 'org-open-link-functions path)))
  9465. ((string= type "thisfile")
  9466. (if arg
  9467. (switch-to-buffer-other-window
  9468. (org-get-buffer-for-internal-link (current-buffer)))
  9469. (org-mark-ring-push))
  9470. (let ((cmd `(org-link-search
  9471. ,path
  9472. ,(cond ((equal arg '(4)) ''occur)
  9473. ((equal arg '(16)) ''org-occur))
  9474. ,pos)))
  9475. (condition-case nil (let ((org-link-search-inhibit-query t))
  9476. (eval cmd))
  9477. (error (progn (widen) (eval cmd))))))
  9478. (t (browse-url-at-point)))))))
  9479. (move-marker org-open-link-marker nil)
  9480. (run-hook-with-args 'org-follow-link-hook)))
  9481. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9482. "Offer links in the current entry and return the selected link.
  9483. If there is only one link, return it.
  9484. If NTH is an integer, return the NTH link found.
  9485. If ZERO is a string, check also this string for a link, and if
  9486. there is one, return it."
  9487. (with-current-buffer buffer
  9488. (save-excursion
  9489. (save-restriction
  9490. (widen)
  9491. (goto-char marker)
  9492. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9493. "\\(" org-angle-link-re "\\)\\|"
  9494. "\\(" org-plain-link-re "\\)"))
  9495. (cnt ?0)
  9496. (in-emacs (if (integerp nth) nil nth))
  9497. have-zero end links link c)
  9498. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9499. (push (match-string 0 zero) links)
  9500. (setq cnt (1- cnt) have-zero t))
  9501. (save-excursion
  9502. (org-back-to-heading t)
  9503. (setq end (save-excursion (outline-next-heading) (point)))
  9504. (while (re-search-forward re end t)
  9505. (push (match-string 0) links))
  9506. (setq links (org-uniquify (reverse links))))
  9507. (cond
  9508. ((null links)
  9509. (message "No links"))
  9510. ((equal (length links) 1)
  9511. (setq link (car links)))
  9512. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9513. (setq link (nth (if have-zero nth (1- nth)) links)))
  9514. (t ; we have to select a link
  9515. (save-excursion
  9516. (save-window-excursion
  9517. (delete-other-windows)
  9518. (with-output-to-temp-buffer "*Select Link*"
  9519. (mapc (lambda (l)
  9520. (if (not (string-match org-bracket-link-regexp l))
  9521. (princ (format "[%c] %s\n" (incf cnt)
  9522. (org-remove-angle-brackets l)))
  9523. (if (match-end 3)
  9524. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9525. (match-string 3 l) (match-string 1 l)))
  9526. (princ (format "[%c] %s\n" (incf cnt)
  9527. (match-string 1 l))))))
  9528. links))
  9529. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9530. (message "Select link to open, RET to open all:")
  9531. (setq c (read-char-exclusive))
  9532. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9533. (when (equal c ?q) (error "Abort"))
  9534. (if (equal c ?\C-m)
  9535. (setq link links)
  9536. (setq nth (- c ?0))
  9537. (if have-zero (setq nth (1+ nth)))
  9538. (unless (and (integerp nth) (>= (length links) nth))
  9539. (user-error "Invalid link selection"))
  9540. (setq link (nth (1- nth) links)))))
  9541. (cons link end))))))
  9542. ;; Add special file links that specify the way of opening
  9543. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9544. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9545. (defun org-open-file-with-system (path)
  9546. "Open file at PATH using the system way of opening it."
  9547. (org-open-file path 'system))
  9548. (defun org-open-file-with-emacs (path)
  9549. "Open file at PATH in Emacs."
  9550. (org-open-file path 'emacs))
  9551. ;;; File search
  9552. (defvar org-create-file-search-functions nil
  9553. "List of functions to construct the right search string for a file link.
  9554. These functions are called in turn with point at the location to
  9555. which the link should point.
  9556. A function in the hook should first test if it would like to
  9557. handle this file type, for example by checking the `major-mode'
  9558. or the file extension. If it decides not to handle this file, it
  9559. should just return nil to give other functions a chance. If it
  9560. does handle the file, it must return the search string to be used
  9561. when following the link. The search string will be part of the
  9562. file link, given after a double colon, and `org-open-at-point'
  9563. will automatically search for it. If special measures must be
  9564. taken to make the search successful, another function should be
  9565. added to the companion hook `org-execute-file-search-functions',
  9566. which see.
  9567. A function in this hook may also use `setq' to set the variable
  9568. `description' to provide a suggestion for the descriptive text to
  9569. be used for this link when it gets inserted into an Org-mode
  9570. buffer with \\[org-insert-link].")
  9571. (defvar org-execute-file-search-functions nil
  9572. "List of functions to execute a file search triggered by a link.
  9573. Functions added to this hook must accept a single argument, the
  9574. search string that was part of the file link, the part after the
  9575. double colon. The function must first check if it would like to
  9576. handle this search, for example by checking the `major-mode' or
  9577. the file extension. If it decides not to handle this search, it
  9578. should just return nil to give other functions a chance. If it
  9579. does handle the search, it must return a non-nil value to keep
  9580. other functions from trying.
  9581. Each function can access the current prefix argument through the
  9582. variable `current-prefix-arg'. Note that a single prefix is used
  9583. to force opening a link in Emacs, so it may be good to only use a
  9584. numeric or double prefix to guide the search function.
  9585. In case this is needed, a function in this hook can also restore
  9586. the window configuration before `org-open-at-point' was called using:
  9587. (set-window-configuration org-window-config-before-follow-link)")
  9588. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9589. (defun org-link-search (s &optional type avoid-pos stealth)
  9590. "Search for a link search option.
  9591. If S is surrounded by forward slashes, it is interpreted as a
  9592. regular expression. In org-mode files, this will create an `org-occur'
  9593. sparse tree. In ordinary files, `occur' will be used to list matches.
  9594. If the current buffer is in `dired-mode', grep will be used to search
  9595. in all files. If AVOID-POS is given, ignore matches near that position.
  9596. When optional argument STEALTH is non-nil, do not modify
  9597. visibility around point, thus ignoring
  9598. `org-show-hierarchy-above', `org-show-following-heading' and
  9599. `org-show-siblings' variables."
  9600. (let ((case-fold-search t)
  9601. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9602. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9603. (append '(("") (" ") ("\t") ("\n"))
  9604. org-emphasis-alist)
  9605. "\\|") "\\)"))
  9606. (pos (point))
  9607. (pre nil) (post nil)
  9608. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9609. (cond
  9610. ;; First check if there are any special search functions
  9611. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9612. ;; Now try the builtin stuff
  9613. ((and (equal (string-to-char s0) ?#)
  9614. (> (length s0) 1)
  9615. (save-excursion
  9616. (goto-char (point-min))
  9617. (and
  9618. (re-search-forward
  9619. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9620. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9621. (setq type 'dedicated
  9622. pos (match-beginning 0))))
  9623. ;; There is an exact target for this
  9624. (goto-char pos)
  9625. (org-back-to-heading t)))
  9626. ((save-excursion
  9627. (goto-char (point-min))
  9628. (and
  9629. (re-search-forward
  9630. (concat "<<" (regexp-quote s0) ">>") nil t)
  9631. (setq type 'dedicated
  9632. pos (match-beginning 0))))
  9633. ;; There is an exact target for this
  9634. (goto-char pos))
  9635. ((save-excursion
  9636. (goto-char (point-min))
  9637. (and
  9638. (re-search-forward
  9639. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9640. (setq type 'dedicated pos (match-beginning 0))))
  9641. ;; Found an element with a matching #+name affiliated keyword.
  9642. (goto-char pos))
  9643. ((and (string-match "^(\\(.*\\))$" s0)
  9644. (save-excursion
  9645. (goto-char (point-min))
  9646. (and
  9647. (re-search-forward
  9648. (concat "[^[]" (regexp-quote
  9649. (format org-coderef-label-format
  9650. (match-string 1 s0))))
  9651. nil t)
  9652. (setq type 'dedicated
  9653. pos (1+ (match-beginning 0))))))
  9654. ;; There is a coderef target for this
  9655. (goto-char pos))
  9656. ((string-match "^/\\(.*\\)/$" s)
  9657. ;; A regular expression
  9658. (cond
  9659. ((derived-mode-p 'org-mode)
  9660. (org-occur (match-string 1 s)))
  9661. (t (org-do-occur (match-string 1 s)))))
  9662. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9663. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9664. (goto-char (point-min))
  9665. (cond
  9666. ((let (case-fold-search)
  9667. (re-search-forward (format org-complex-heading-regexp-format
  9668. (regexp-quote s))
  9669. nil t))
  9670. ;; OK, found a match
  9671. (setq type 'dedicated)
  9672. (goto-char (match-beginning 0)))
  9673. ((and (not org-link-search-inhibit-query)
  9674. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9675. (y-or-n-p "No match - create this as a new heading? "))
  9676. (goto-char (point-max))
  9677. (or (bolp) (newline))
  9678. (insert "* " s "\n")
  9679. (beginning-of-line 0))
  9680. (t
  9681. (goto-char pos)
  9682. (error "No match"))))
  9683. (t
  9684. ;; A normal search string
  9685. (when (equal (string-to-char s) ?*)
  9686. ;; Anchor on headlines, post may include tags.
  9687. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9688. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9689. s (substring s 1)))
  9690. (remove-text-properties
  9691. 0 (length s)
  9692. '(face nil mouse-face nil keymap nil fontified nil) s)
  9693. ;; Make a series of regular expressions to find a match
  9694. (setq words (org-split-string s "[ \n\r\t]+")
  9695. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9696. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9697. "\\)" markers)
  9698. re2a_ (concat "\\(" (mapconcat 'downcase words
  9699. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9700. re2a (concat "[ \t\r\n]" re2a_)
  9701. re4_ (concat "\\(" (mapconcat 'downcase words
  9702. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9703. re4 (concat "[^a-zA-Z_]" re4_)
  9704. re1 (concat pre re2 post)
  9705. re3 (concat pre (if pre re4_ re4) post)
  9706. re5 (concat pre ".*" re4)
  9707. re2 (concat pre re2)
  9708. re2a (concat pre (if pre re2a_ re2a))
  9709. re4 (concat pre (if pre re4_ re4))
  9710. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9711. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9712. re5 "\\)"))
  9713. (cond
  9714. ((eq type 'org-occur) (org-occur reall))
  9715. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9716. (t (goto-char (point-min))
  9717. (setq type 'fuzzy)
  9718. (if (or (and (org-search-not-self 1 re0 nil t)
  9719. (setq type 'dedicated))
  9720. (org-search-not-self 1 re1 nil t)
  9721. (org-search-not-self 1 re2 nil t)
  9722. (org-search-not-self 1 re2a nil t)
  9723. (org-search-not-self 1 re3 nil t)
  9724. (org-search-not-self 1 re4 nil t)
  9725. (org-search-not-self 1 re5 nil t))
  9726. (goto-char (match-beginning 1))
  9727. (goto-char pos)
  9728. (error "No match"))))))
  9729. (and (derived-mode-p 'org-mode)
  9730. (not stealth)
  9731. (org-show-context 'link-search))
  9732. type))
  9733. (defun org-search-not-self (group &rest args)
  9734. "Execute `re-search-forward', but only accept matches that do not
  9735. enclose the position of `org-open-link-marker'."
  9736. (let ((m org-open-link-marker))
  9737. (catch 'exit
  9738. (while (apply 're-search-forward args)
  9739. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9740. (goto-char (match-end group))
  9741. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9742. (> (match-beginning 0) (marker-position m))
  9743. (< (match-end 0) (marker-position m)))
  9744. (save-match-data
  9745. (or (not (org-in-regexp
  9746. org-bracket-link-analytic-regexp 1))
  9747. (not (match-end 4)) ; no description
  9748. (and (<= (match-beginning 4) (point))
  9749. (>= (match-end 4) (point))))))
  9750. (throw 'exit (point))))))))
  9751. (defun org-get-buffer-for-internal-link (buffer)
  9752. "Return a buffer to be used for displaying the link target of internal links."
  9753. (cond
  9754. ((not org-display-internal-link-with-indirect-buffer)
  9755. buffer)
  9756. ((string-match "(Clone)$" (buffer-name buffer))
  9757. (message "Buffer is already a clone, not making another one")
  9758. ;; we also do not modify visibility in this case
  9759. buffer)
  9760. (t ; make a new indirect buffer for displaying the link
  9761. (let* ((bn (buffer-name buffer))
  9762. (ibn (concat bn "(Clone)"))
  9763. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9764. (with-current-buffer ib (org-overview))
  9765. ib))))
  9766. (defun org-do-occur (regexp &optional cleanup)
  9767. "Call the Emacs command `occur'.
  9768. If CLEANUP is non-nil, remove the printout of the regular expression
  9769. in the *Occur* buffer. This is useful if the regex is long and not useful
  9770. to read."
  9771. (occur regexp)
  9772. (when cleanup
  9773. (let ((cwin (selected-window)) win beg end)
  9774. (when (setq win (get-buffer-window "*Occur*"))
  9775. (select-window win))
  9776. (goto-char (point-min))
  9777. (when (re-search-forward "match[a-z]+" nil t)
  9778. (setq beg (match-end 0))
  9779. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9780. (setq end (1- (match-beginning 0)))))
  9781. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9782. (goto-char (point-min))
  9783. (select-window cwin))))
  9784. ;;; The mark ring for links jumps
  9785. (defvar org-mark-ring nil
  9786. "Mark ring for positions before jumps in Org-mode.")
  9787. (defvar org-mark-ring-last-goto nil
  9788. "Last position in the mark ring used to go back.")
  9789. ;; Fill and close the ring
  9790. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9791. (loop for i from 1 to org-mark-ring-length do
  9792. (push (make-marker) org-mark-ring))
  9793. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9794. org-mark-ring)
  9795. (defun org-mark-ring-push (&optional pos buffer)
  9796. "Put the current position or POS into the mark ring and rotate it."
  9797. (interactive)
  9798. (setq pos (or pos (point)))
  9799. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9800. (move-marker (car org-mark-ring)
  9801. (or pos (point))
  9802. (or buffer (current-buffer)))
  9803. (message "%s"
  9804. (substitute-command-keys
  9805. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9806. (defun org-mark-ring-goto (&optional n)
  9807. "Jump to the previous position in the mark ring.
  9808. With prefix arg N, jump back that many stored positions. When
  9809. called several times in succession, walk through the entire ring.
  9810. Org-mode commands jumping to a different position in the current file,
  9811. or to another Org-mode file, automatically push the old position
  9812. onto the ring."
  9813. (interactive "p")
  9814. (let (p m)
  9815. (if (eq last-command this-command)
  9816. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9817. (setq p org-mark-ring))
  9818. (setq org-mark-ring-last-goto p)
  9819. (setq m (car p))
  9820. (org-pop-to-buffer-same-window (marker-buffer m))
  9821. (goto-char m)
  9822. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9823. (defun org-remove-angle-brackets (s)
  9824. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9825. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9826. s)
  9827. (defun org-add-angle-brackets (s)
  9828. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9829. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9830. s)
  9831. (defun org-remove-double-quotes (s)
  9832. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9833. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9834. s)
  9835. ;;; Following specific links
  9836. (defun org-follow-timestamp-link ()
  9837. "Open an agenda view for the time-stamp date/range at point."
  9838. (cond
  9839. ((org-at-date-range-p t)
  9840. (let ((org-agenda-start-on-weekday)
  9841. (t1 (match-string 1))
  9842. (t2 (match-string 2)) tt1 tt2)
  9843. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9844. tt2 (time-to-days (org-time-string-to-time t2)))
  9845. (let ((org-agenda-buffer-tmp-name
  9846. (format "*Org Agenda(a:%s)"
  9847. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9848. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9849. ((org-at-timestamp-p t)
  9850. (let ((org-agenda-buffer-tmp-name
  9851. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9852. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9853. (substring (match-string 1) 0 10)))
  9854. 1)))
  9855. (t (error "This should not happen"))))
  9856. ;;; Following file links
  9857. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9858. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9859. (declare-function mailcap-mime-info
  9860. "mailcap" (string &optional request no-decode))
  9861. (defvar org-wait nil)
  9862. (defun org-open-file (path &optional in-emacs line search)
  9863. "Open the file at PATH.
  9864. First, this expands any special file name abbreviations. Then the
  9865. configuration variable `org-file-apps' is checked if it contains an
  9866. entry for this file type, and if yes, the corresponding command is launched.
  9867. If no application is found, Emacs simply visits the file.
  9868. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9869. With a double \\[universal-argument] \\[universal-argument] \
  9870. prefix arg, Org tries to avoid opening in Emacs
  9871. and to use an external application to visit the file.
  9872. Optional LINE specifies a line to go to, optional SEARCH a string
  9873. to search for. If LINE or SEARCH is given, the file will be
  9874. opened in Emacs, unless an entry from org-file-apps that makes
  9875. use of groups in a regexp matches.
  9876. If you want to change the way frames are used when following a
  9877. link, please customize `org-link-frame-setup'.
  9878. If the file does not exist, an error is thrown."
  9879. (let* ((file (if (equal path "")
  9880. buffer-file-name
  9881. (substitute-in-file-name (expand-file-name path))))
  9882. (file-apps (append org-file-apps (org-default-apps)))
  9883. (apps (org-remove-if
  9884. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9885. (apps-dlink (org-remove-if-not
  9886. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9887. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9888. (dirp (if remp nil (file-directory-p file)))
  9889. (file (if (and dirp org-open-directory-means-index-dot-org)
  9890. (concat (file-name-as-directory file) "index.org")
  9891. file))
  9892. (a-m-a-p (assq 'auto-mode apps))
  9893. (dfile (downcase file))
  9894. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9895. (link (cond ((and (eq line nil)
  9896. (eq search nil))
  9897. file)
  9898. (line
  9899. (concat file "::" (number-to-string line)))
  9900. (search
  9901. (concat file "::" search))))
  9902. (dlink (downcase link))
  9903. (old-buffer (current-buffer))
  9904. (old-pos (point))
  9905. (old-mode major-mode)
  9906. ext cmd link-match-data)
  9907. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9908. (setq ext (match-string 1 dfile))
  9909. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9910. (setq ext (match-string 1 dfile))))
  9911. (cond
  9912. ((member in-emacs '((16) system))
  9913. (setq cmd (cdr (assoc 'system apps))))
  9914. (in-emacs (setq cmd 'emacs))
  9915. (t
  9916. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9917. (and dirp (cdr (assoc 'directory apps)))
  9918. ; first, try matching against apps-dlink
  9919. ; if we get a match here, store the match data for later
  9920. (let ((match (assoc-default dlink apps-dlink
  9921. 'string-match)))
  9922. (if match
  9923. (progn (setq link-match-data (match-data))
  9924. match)
  9925. (progn (setq in-emacs (or in-emacs line search))
  9926. nil))) ; if we have no match in apps-dlink,
  9927. ; always open the file in emacs if line or search
  9928. ; is given (for backwards compatibility)
  9929. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9930. 'string-match)
  9931. (cdr (assoc ext apps))
  9932. (cdr (assoc t apps))))))
  9933. (when (eq cmd 'system)
  9934. (setq cmd (cdr (assoc 'system apps))))
  9935. (when (eq cmd 'default)
  9936. (setq cmd (cdr (assoc t apps))))
  9937. (when (eq cmd 'mailcap)
  9938. (require 'mailcap)
  9939. (mailcap-parse-mailcaps)
  9940. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9941. (command (mailcap-mime-info mime-type)))
  9942. (if (stringp command)
  9943. (setq cmd command)
  9944. (setq cmd 'emacs))))
  9945. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9946. (not (file-exists-p file))
  9947. (not org-open-non-existing-files))
  9948. (user-error "No such file: %s" file))
  9949. (cond
  9950. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9951. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9952. (while (string-match "['\"]%s['\"]" cmd)
  9953. (setq cmd (replace-match "%s" t t cmd)))
  9954. (while (string-match "%s" cmd)
  9955. (setq cmd (replace-match
  9956. (save-match-data
  9957. (shell-quote-argument
  9958. (convert-standard-filename file)))
  9959. t t cmd)))
  9960. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9961. (save-match-data
  9962. (let ((match-index 1)
  9963. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9964. (set-match-data link-match-data)
  9965. (while (<= match-index number-of-groups)
  9966. (let ((regex (concat "%" (number-to-string match-index)))
  9967. (replace-with (match-string match-index dlink)))
  9968. (while (string-match regex cmd)
  9969. (setq cmd (replace-match replace-with t t cmd))))
  9970. (setq match-index (+ match-index 1)))))
  9971. (save-window-excursion
  9972. (message "Running %s...done" cmd)
  9973. (start-process-shell-command cmd nil cmd)
  9974. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9975. ((or (stringp cmd)
  9976. (eq cmd 'emacs))
  9977. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9978. (widen)
  9979. (if line (org-goto-line line)
  9980. (if search (org-link-search search))))
  9981. ((consp cmd)
  9982. (let ((file (convert-standard-filename file)))
  9983. (save-match-data
  9984. (set-match-data link-match-data)
  9985. (eval cmd))))
  9986. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9987. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9988. (or (not (equal old-buffer (current-buffer)))
  9989. (not (equal old-pos (point))))
  9990. (org-mark-ring-push old-pos old-buffer))))
  9991. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9992. "This function returns non-nil if `entry' uses a regular
  9993. expression which should be matched against the whole link by
  9994. org-open-file.
  9995. It assumes that is the case when the entry uses a regular
  9996. expression which has at least one grouping construct and the
  9997. action is either a lisp form or a command string containing
  9998. '%1', i.e. using at least one subexpression match as a
  9999. parameter."
  10000. (let ((selector (car entry))
  10001. (action (cdr entry)))
  10002. (if (stringp selector)
  10003. (and (> (regexp-opt-depth selector) 0)
  10004. (or (and (stringp action)
  10005. (string-match "%[0-9]" action))
  10006. (consp action)))
  10007. nil)))
  10008. (defun org-default-apps ()
  10009. "Return the default applications for this operating system."
  10010. (cond
  10011. ((eq system-type 'darwin)
  10012. org-file-apps-defaults-macosx)
  10013. ((eq system-type 'windows-nt)
  10014. org-file-apps-defaults-windowsnt)
  10015. (t org-file-apps-defaults-gnu)))
  10016. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10017. "Convert extensions to regular expressions in the cars of LIST.
  10018. Also, weed out any non-string entries, because the return value is used
  10019. only for regexp matching.
  10020. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10021. point to the symbol `emacs', indicating that the file should
  10022. be opened in Emacs."
  10023. (append
  10024. (delq nil
  10025. (mapcar (lambda (x)
  10026. (if (not (stringp (car x)))
  10027. nil
  10028. (if (string-match "\\W" (car x))
  10029. x
  10030. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10031. list))
  10032. (if add-auto-mode
  10033. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10034. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10035. (defun org-file-remote-p (file)
  10036. "Test whether FILE specifies a location on a remote system.
  10037. Return non-nil if the location is indeed remote.
  10038. For example, the filename \"/user@host:/foo\" specifies a location
  10039. on the system \"/user@host:\"."
  10040. (cond ((fboundp 'file-remote-p)
  10041. (file-remote-p file))
  10042. ((fboundp 'tramp-handle-file-remote-p)
  10043. (tramp-handle-file-remote-p file))
  10044. ((and (boundp 'ange-ftp-name-format)
  10045. (string-match (car ange-ftp-name-format) file))
  10046. t)))
  10047. ;;;; Refiling
  10048. (defun org-get-org-file ()
  10049. "Read a filename, with default directory `org-directory'."
  10050. (let ((default (or org-default-notes-file remember-data-file)))
  10051. (read-file-name (format "File name [%s]: " default)
  10052. (file-name-as-directory org-directory)
  10053. default)))
  10054. (defun org-notes-order-reversed-p ()
  10055. "Check if the current file should receive notes in reversed order."
  10056. (cond
  10057. ((not org-reverse-note-order) nil)
  10058. ((eq t org-reverse-note-order) t)
  10059. ((not (listp org-reverse-note-order)) nil)
  10060. (t (catch 'exit
  10061. (let ((all org-reverse-note-order)
  10062. entry)
  10063. (while (setq entry (pop all))
  10064. (if (string-match (car entry) buffer-file-name)
  10065. (throw 'exit (cdr entry))))
  10066. nil)))))
  10067. (defvar org-refile-target-table nil
  10068. "The list of refile targets, created by `org-refile'.")
  10069. (defvar org-agenda-new-buffers nil
  10070. "Buffers created to visit agenda files.")
  10071. (defvar org-refile-cache nil
  10072. "Cache for refile targets.")
  10073. (defvar org-refile-markers nil
  10074. "All the markers used for caching refile locations.")
  10075. (defun org-refile-marker (pos)
  10076. "Get a new refile marker, but only if caching is in use."
  10077. (if (not org-refile-use-cache)
  10078. pos
  10079. (let ((m (make-marker)))
  10080. (move-marker m pos)
  10081. (push m org-refile-markers)
  10082. m)))
  10083. (defun org-refile-cache-clear ()
  10084. "Clear the refile cache and disable all the markers."
  10085. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10086. (setq org-refile-markers nil)
  10087. (setq org-refile-cache nil)
  10088. (message "Refile cache has been cleared"))
  10089. (defun org-refile-cache-check-set (set)
  10090. "Check if all the markers in the cache still have live buffers."
  10091. (let (marker)
  10092. (catch 'exit
  10093. (while (and set (setq marker (nth 3 (pop set))))
  10094. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10095. (when (and marker (null (marker-buffer marker)))
  10096. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10097. (sit-for 3)
  10098. (throw 'exit nil)))
  10099. t)))
  10100. (defun org-refile-cache-put (set &rest identifiers)
  10101. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10102. (let* ((key (sha1 (prin1-to-string identifiers)))
  10103. (entry (assoc key org-refile-cache)))
  10104. (if entry
  10105. (setcdr entry set)
  10106. (push (cons key set) org-refile-cache))))
  10107. (defun org-refile-cache-get (&rest identifiers)
  10108. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10109. (cond
  10110. ((not org-refile-cache) nil)
  10111. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10112. (t
  10113. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10114. org-refile-cache))))
  10115. (and set (org-refile-cache-check-set set) set)))))
  10116. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10117. "Produce a table with refile targets."
  10118. (let ((case-fold-search nil)
  10119. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10120. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10121. targets tgs txt re files f desc descre fast-path-p level pos0)
  10122. (message "Getting targets...")
  10123. (with-current-buffer (or default-buffer (current-buffer))
  10124. (while (setq entry (pop entries))
  10125. (setq files (car entry) desc (cdr entry))
  10126. (setq fast-path-p nil)
  10127. (cond
  10128. ((null files) (setq files (list (current-buffer))))
  10129. ((eq files 'org-agenda-files)
  10130. (setq files (org-agenda-files 'unrestricted)))
  10131. ((and (symbolp files) (fboundp files))
  10132. (setq files (funcall files)))
  10133. ((and (symbolp files) (boundp files))
  10134. (setq files (symbol-value files))))
  10135. (if (stringp files) (setq files (list files)))
  10136. (cond
  10137. ((eq (car desc) :tag)
  10138. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10139. ((eq (car desc) :todo)
  10140. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10141. ((eq (car desc) :regexp)
  10142. (setq descre (cdr desc)))
  10143. ((eq (car desc) :level)
  10144. (setq descre (concat "^\\*\\{" (number-to-string
  10145. (if org-odd-levels-only
  10146. (1- (* 2 (cdr desc)))
  10147. (cdr desc)))
  10148. "\\}[ \t]")))
  10149. ((eq (car desc) :maxlevel)
  10150. (setq fast-path-p t)
  10151. (setq descre (concat "^\\*\\{1," (number-to-string
  10152. (if org-odd-levels-only
  10153. (1- (* 2 (cdr desc)))
  10154. (cdr desc)))
  10155. "\\}[ \t]")))
  10156. (t (error "Bad refiling target description %s" desc)))
  10157. (while (setq f (pop files))
  10158. (with-current-buffer
  10159. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10160. (or
  10161. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10162. (progn
  10163. (if (bufferp f) (setq f (buffer-file-name
  10164. (buffer-base-buffer f))))
  10165. (setq f (and f (expand-file-name f)))
  10166. (if (eq org-refile-use-outline-path 'file)
  10167. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10168. (save-excursion
  10169. (save-restriction
  10170. (widen)
  10171. (goto-char (point-min))
  10172. (while (re-search-forward descre nil t)
  10173. (goto-char (setq pos0 (point-at-bol)))
  10174. (catch 'next
  10175. (when org-refile-target-verify-function
  10176. (save-match-data
  10177. (or (funcall org-refile-target-verify-function)
  10178. (throw 'next t))))
  10179. (when (and (looking-at org-complex-heading-regexp)
  10180. (not (member (match-string 4) excluded-entries))
  10181. (match-string 4))
  10182. (setq level (org-reduced-level
  10183. (- (match-end 1) (match-beginning 1)))
  10184. txt (org-link-display-format (match-string 4))
  10185. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10186. re (format org-complex-heading-regexp-format
  10187. (regexp-quote (match-string 4))))
  10188. (when org-refile-use-outline-path
  10189. (setq txt (mapconcat
  10190. 'org-protect-slash
  10191. (append
  10192. (if (eq org-refile-use-outline-path
  10193. 'file)
  10194. (list (file-name-nondirectory
  10195. (buffer-file-name
  10196. (buffer-base-buffer))))
  10197. (if (eq org-refile-use-outline-path
  10198. 'full-file-path)
  10199. (list (buffer-file-name
  10200. (buffer-base-buffer)))))
  10201. (org-get-outline-path fast-path-p
  10202. level txt)
  10203. (list txt))
  10204. "/")))
  10205. (push (list txt f re (org-refile-marker (point)))
  10206. tgs)))
  10207. (when (= (point) pos0)
  10208. ;; verification function has not moved point
  10209. (goto-char (point-at-eol))))))))
  10210. (when org-refile-use-cache
  10211. (org-refile-cache-put tgs (buffer-file-name) descre))
  10212. (setq targets (append tgs targets))))))
  10213. (message "Getting targets...done")
  10214. (nreverse targets)))
  10215. (defun org-protect-slash (s)
  10216. (while (string-match "/" s)
  10217. (setq s (replace-match "\\" t t s)))
  10218. s)
  10219. (defvar org-olpa (make-vector 20 nil))
  10220. (defun org-get-outline-path (&optional fastp level heading)
  10221. "Return the outline path to the current entry, as a list.
  10222. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10223. routine which makes outline path derivations for an entire file,
  10224. avoiding backtracing. Refile target collection makes use of that."
  10225. (if fastp
  10226. (progn
  10227. (if (> level 19)
  10228. (error "Outline path failure, more than 19 levels"))
  10229. (loop for i from level upto 19 do
  10230. (aset org-olpa i nil))
  10231. (prog1
  10232. (delq nil (append org-olpa nil))
  10233. (aset org-olpa level heading)))
  10234. (let (rtn case-fold-search)
  10235. (save-excursion
  10236. (save-restriction
  10237. (widen)
  10238. (while (org-up-heading-safe)
  10239. (when (looking-at org-complex-heading-regexp)
  10240. (push (org-match-string-no-properties 4) rtn)))
  10241. rtn)))))
  10242. (defun org-format-outline-path (path &optional width prefix separator)
  10243. "Format the outline path PATH for display.
  10244. WIDTH is the maximum number of characters that is available.
  10245. PREFIX is a prefix to be included in the returned string,
  10246. such as the file name.
  10247. SEPARATOR is inserted between the different parts of the path,
  10248. the default is \"/\"."
  10249. (setq width (or width 79))
  10250. (if prefix (setq width (- width (length prefix))))
  10251. (if (not path)
  10252. (or prefix "")
  10253. (let* ((nsteps (length path))
  10254. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10255. (maxwidth (if (<= total-width width)
  10256. 10000 ;; everything fits
  10257. ;; we need to shorten the level headings
  10258. (/ (- width nsteps) nsteps)))
  10259. (org-odd-levels-only nil)
  10260. (n 0)
  10261. (total (1+ (length prefix))))
  10262. (setq maxwidth (max maxwidth 10))
  10263. (concat prefix
  10264. (if prefix (or separator "/"))
  10265. (mapconcat
  10266. (lambda (h)
  10267. (setq n (1+ n))
  10268. (if (and (= n nsteps) (< maxwidth 10000))
  10269. (setq maxwidth (- total-width total)))
  10270. (if (< (length h) maxwidth)
  10271. (progn (setq total (+ total (length h) 1)) h)
  10272. (setq h (substring h 0 (- maxwidth 2))
  10273. total (+ total maxwidth 1))
  10274. (if (string-match "[ \t]+\\'" h)
  10275. (setq h (substring h 0 (match-beginning 0))))
  10276. (setq h (concat h "..")))
  10277. (org-add-props h nil 'face
  10278. (nth (% (1- n) org-n-level-faces)
  10279. org-level-faces))
  10280. h)
  10281. path (or separator "/"))))))
  10282. (defun org-display-outline-path (&optional file current separator just-return-string)
  10283. "Display the current outline path in the echo area.
  10284. If FILE is non-nil, prepend the output with the file name.
  10285. If CURRENT is non-nil, append the current heading to the output.
  10286. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10287. the different parts of the path and defaults to \"/\".
  10288. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10289. (interactive "P")
  10290. (let* (case-fold-search
  10291. message-log-max ; Don't populate the *Messages* buffer
  10292. (bfn (buffer-file-name (buffer-base-buffer)))
  10293. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10294. res)
  10295. (if current (setq path (append path
  10296. (save-excursion
  10297. (org-back-to-heading t)
  10298. (if (looking-at org-complex-heading-regexp)
  10299. (list (match-string 4)))))))
  10300. (setq res
  10301. (org-format-outline-path
  10302. path
  10303. (1- (frame-width))
  10304. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10305. separator))
  10306. (if just-return-string
  10307. (org-no-properties res)
  10308. (message "%s" res))))
  10309. (defvar org-refile-history nil
  10310. "History for refiling operations.")
  10311. (defvar org-after-refile-insert-hook nil
  10312. "Hook run after `org-refile' has inserted its stuff at the new location.
  10313. Note that this is still *before* the stuff will be removed from
  10314. the *old* location.")
  10315. (defvar org-capture-last-stored-marker)
  10316. (defvar org-refile-keep nil
  10317. "Non-nil means `org-refile' will copy instead of refile.")
  10318. (defun org-copy ()
  10319. "Like `org-refile', but copy."
  10320. (interactive)
  10321. (let ((org-refile-keep t))
  10322. (funcall 'org-refile nil nil nil "Copy")))
  10323. (defun org-refile (&optional goto default-buffer rfloc msg)
  10324. "Move the entry or entries at point to another heading.
  10325. The list of target headings is compiled using the information in
  10326. `org-refile-targets', which see.
  10327. At the target location, the entry is filed as a subitem of the target
  10328. heading. Depending on `org-reverse-note-order', the new subitem will
  10329. either be the first or the last subitem.
  10330. If there is an active region, all entries in that region will be moved.
  10331. However, the region must fulfill the requirement that the first heading
  10332. is the first one sets the top-level of the moved text - at most siblings
  10333. below it are allowed.
  10334. With prefix arg GOTO, the command will only visit the target location
  10335. and not actually move anything.
  10336. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10337. go to the location where the last refiling operation has put the subtree.
  10338. With a prefix argument of `2', refile to the running clock.
  10339. RFLOC can be a refile location obtained in a different way.
  10340. MSG is a string to replace \"Refile\" in the default prompt with
  10341. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10342. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10343. If you are using target caching (see `org-refile-use-cache'),
  10344. you have to clear the target cache in order to find new targets.
  10345. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10346. prefix argument (`C-u C-u C-u C-c C-w')."
  10347. (interactive "P")
  10348. (if (member goto '(0 (64)))
  10349. (org-refile-cache-clear)
  10350. (let* ((actionmsg (or msg "Refile"))
  10351. (cbuf (current-buffer))
  10352. (regionp (org-region-active-p))
  10353. (region-start (and regionp (region-beginning)))
  10354. (region-end (and regionp (region-end)))
  10355. (region-length (and regionp (- region-end region-start)))
  10356. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10357. pos it nbuf file re level reversed)
  10358. (setq last-command nil)
  10359. (when regionp
  10360. (goto-char region-start)
  10361. (or (bolp) (goto-char (point-at-bol)))
  10362. (setq region-start (point))
  10363. (unless (or (org-kill-is-subtree-p
  10364. (buffer-substring region-start region-end))
  10365. (prog1 org-refile-active-region-within-subtree
  10366. (org-toggle-heading)))
  10367. (user-error "The region is not a (sequence of) subtree(s)")))
  10368. (if (equal goto '(16))
  10369. (org-refile-goto-last-stored)
  10370. (when (or
  10371. (and (equal goto 2)
  10372. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10373. (prog1
  10374. (setq it (list (or org-clock-heading "running clock")
  10375. (buffer-file-name
  10376. (marker-buffer org-clock-hd-marker))
  10377. ""
  10378. (marker-position org-clock-hd-marker)))
  10379. (setq goto nil)))
  10380. (setq it (or rfloc
  10381. (let (heading-text)
  10382. (save-excursion
  10383. (unless goto
  10384. (org-back-to-heading t)
  10385. (setq heading-text
  10386. (nth 4 (org-heading-components))))
  10387. (org-refile-get-location
  10388. (cond (goto "Goto")
  10389. (regionp (concat actionmsg " region to"))
  10390. (t (concat actionmsg " subtree \""
  10391. heading-text "\" to")))
  10392. default-buffer
  10393. (and (not (equal '(4) goto))
  10394. org-refile-allow-creating-parent-nodes)
  10395. goto))))))
  10396. (setq file (nth 1 it)
  10397. re (nth 2 it)
  10398. pos (nth 3 it))
  10399. (if (and (not goto)
  10400. pos
  10401. (equal (buffer-file-name) file)
  10402. (if regionp
  10403. (and (>= pos region-start)
  10404. (<= pos region-end))
  10405. (and (>= pos (point))
  10406. (< pos (save-excursion
  10407. (org-end-of-subtree t t))))))
  10408. (error "Cannot refile to position inside the tree or region"))
  10409. (setq nbuf (or (find-buffer-visiting file)
  10410. (find-file-noselect file)))
  10411. (if goto
  10412. (progn
  10413. (org-pop-to-buffer-same-window nbuf)
  10414. (goto-char pos)
  10415. (org-show-context 'org-goto))
  10416. (if regionp
  10417. (progn
  10418. (org-kill-new (buffer-substring region-start region-end))
  10419. (org-save-markers-in-region region-start region-end))
  10420. (org-copy-subtree 1 nil t))
  10421. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10422. (find-file-noselect file)))
  10423. (setq reversed (org-notes-order-reversed-p))
  10424. (save-excursion
  10425. (save-restriction
  10426. (widen)
  10427. (if pos
  10428. (progn
  10429. (goto-char pos)
  10430. (looking-at org-outline-regexp)
  10431. (setq level (org-get-valid-level (funcall outline-level) 1))
  10432. (goto-char
  10433. (if reversed
  10434. (or (outline-next-heading) (point-max))
  10435. (or (save-excursion (org-get-next-sibling))
  10436. (org-end-of-subtree t t)
  10437. (point-max)))))
  10438. (setq level 1)
  10439. (if (not reversed)
  10440. (goto-char (point-max))
  10441. (goto-char (point-min))
  10442. (or (outline-next-heading) (goto-char (point-max)))))
  10443. (if (not (bolp)) (newline))
  10444. (org-paste-subtree level)
  10445. (when org-log-refile
  10446. (org-add-log-setup 'refile nil nil 'findpos
  10447. org-log-refile)
  10448. (unless (eq org-log-refile 'note)
  10449. (save-excursion (org-add-log-note))))
  10450. (and org-auto-align-tags
  10451. (let ((org-loop-over-headlines-in-active-region nil))
  10452. (org-set-tags nil t)))
  10453. (with-demoted-errors
  10454. (bookmark-set "org-refile-last-stored"))
  10455. ;; If we are refiling for capture, make sure that the
  10456. ;; last-capture pointers point here
  10457. (when (org-bound-and-true-p org-refile-for-capture)
  10458. (with-demoted-errors
  10459. (bookmark-set "org-capture-last-stored-marker"))
  10460. (move-marker org-capture-last-stored-marker (point)))
  10461. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10462. (run-hooks 'org-after-refile-insert-hook))))
  10463. (unless org-refile-keep
  10464. (if regionp
  10465. (delete-region (point) (+ (point) region-length))
  10466. (org-cut-subtree)))
  10467. (when (featurep 'org-inlinetask)
  10468. (org-inlinetask-remove-END-maybe))
  10469. (setq org-markers-to-move nil)
  10470. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10471. (defun org-refile-goto-last-stored ()
  10472. "Go to the location where the last refile was stored."
  10473. (interactive)
  10474. (bookmark-jump "org-refile-last-stored")
  10475. (message "This is the location of the last refile"))
  10476. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10477. no-exclude)
  10478. "Prompt the user for a refile location, using PROMPT.
  10479. PROMPT should not be suffixed with a colon and a space, because
  10480. this function appends the default value from
  10481. `org-refile-history' automatically, if that is not empty.
  10482. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10483. this is used for the GOTO interface."
  10484. (let ((org-refile-targets org-refile-targets)
  10485. (org-refile-use-outline-path org-refile-use-outline-path)
  10486. excluded-entries)
  10487. (when (and (derived-mode-p 'org-mode)
  10488. (not org-refile-use-cache)
  10489. (not no-exclude))
  10490. (org-map-tree
  10491. (lambda()
  10492. (setq excluded-entries
  10493. (append excluded-entries (list (org-get-heading t t)))))))
  10494. (setq org-refile-target-table
  10495. (org-refile-get-targets default-buffer excluded-entries)))
  10496. (unless org-refile-target-table
  10497. (user-error "No refile targets"))
  10498. (let* ((cbuf (current-buffer))
  10499. (partial-completion-mode nil)
  10500. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10501. (cfunc (if (and org-refile-use-outline-path
  10502. org-outline-path-complete-in-steps)
  10503. 'org-olpath-completing-read
  10504. 'org-icompleting-read))
  10505. (extra (if org-refile-use-outline-path "/" ""))
  10506. (cbnex (concat (buffer-name) extra))
  10507. (filename (and cfn (expand-file-name cfn)))
  10508. (tbl (mapcar
  10509. (lambda (x)
  10510. (if (and (not (member org-refile-use-outline-path
  10511. '(file full-file-path)))
  10512. (not (equal filename (nth 1 x))))
  10513. (cons (concat (car x) extra " ("
  10514. (file-name-nondirectory (nth 1 x)) ")")
  10515. (cdr x))
  10516. (cons (concat (car x) extra) (cdr x))))
  10517. org-refile-target-table))
  10518. (completion-ignore-case t)
  10519. cdef
  10520. (prompt (concat prompt
  10521. (or (and (car org-refile-history)
  10522. (concat " (default " (car org-refile-history) ")"))
  10523. (and (assoc cbnex tbl) (setq cdef cbnex)
  10524. (concat " (default " cbnex ")"))) ": "))
  10525. pa answ parent-target child parent old-hist)
  10526. (setq old-hist org-refile-history)
  10527. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10528. nil 'org-refile-history (or cdef (car org-refile-history))))
  10529. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10530. (org-refile-check-position pa)
  10531. (if pa
  10532. (progn
  10533. (when (or (not org-refile-history)
  10534. (not (eq old-hist org-refile-history))
  10535. (not (equal (car pa) (car org-refile-history))))
  10536. (setq org-refile-history
  10537. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10538. org-refile-history
  10539. (cdr org-refile-history))))
  10540. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10541. (pop org-refile-history)))
  10542. pa)
  10543. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10544. (progn
  10545. (setq parent (match-string 1 answ)
  10546. child (match-string 2 answ))
  10547. (setq parent-target (or (assoc parent tbl)
  10548. (assoc (concat parent "/") tbl)))
  10549. (when (and parent-target
  10550. (or (eq new-nodes t)
  10551. (and (eq new-nodes 'confirm)
  10552. (y-or-n-p (format "Create new node \"%s\"? "
  10553. child)))))
  10554. (org-refile-new-child parent-target child)))
  10555. (user-error "Invalid target location")))))
  10556. (declare-function org-string-nw-p "org-macs" (s))
  10557. (defun org-refile-check-position (refile-pointer)
  10558. "Check if the refile pointer matches the headline to which it points."
  10559. (let* ((file (nth 1 refile-pointer))
  10560. (re (nth 2 refile-pointer))
  10561. (pos (nth 3 refile-pointer))
  10562. buffer)
  10563. (if (and (not (markerp pos)) (not file))
  10564. (user-error "Please save the buffer to a file before refiling")
  10565. (when (org-string-nw-p re)
  10566. (setq buffer (if (markerp pos)
  10567. (marker-buffer pos)
  10568. (or (find-buffer-visiting file)
  10569. (find-file-noselect file))))
  10570. (with-current-buffer buffer
  10571. (save-excursion
  10572. (save-restriction
  10573. (widen)
  10574. (goto-char pos)
  10575. (beginning-of-line 1)
  10576. (unless (org-looking-at-p re)
  10577. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10578. (defun org-refile-new-child (parent-target child)
  10579. "Use refile target PARENT-TARGET to add new CHILD below it."
  10580. (unless parent-target
  10581. (error "Cannot find parent for new node"))
  10582. (let ((file (nth 1 parent-target))
  10583. (pos (nth 3 parent-target))
  10584. level)
  10585. (with-current-buffer (or (find-buffer-visiting file)
  10586. (find-file-noselect file))
  10587. (save-excursion
  10588. (save-restriction
  10589. (widen)
  10590. (if pos
  10591. (goto-char pos)
  10592. (goto-char (point-max))
  10593. (if (not (bolp)) (newline)))
  10594. (when (looking-at org-outline-regexp)
  10595. (setq level (funcall outline-level))
  10596. (org-end-of-subtree t t))
  10597. (org-back-over-empty-lines)
  10598. (insert "\n" (make-string
  10599. (if pos (org-get-valid-level level 1) 1) ?*)
  10600. " " child "\n")
  10601. (beginning-of-line 0)
  10602. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10603. (defun org-olpath-completing-read (prompt collection &rest args)
  10604. "Read an outline path like a file name."
  10605. (let ((thetable collection)
  10606. (org-completion-use-ido nil) ; does not work with ido.
  10607. (org-completion-use-iswitchb nil)) ; or iswitchb
  10608. (apply
  10609. 'org-icompleting-read prompt
  10610. (lambda (string predicate &optional flag)
  10611. (let (rtn r f (l (length string)))
  10612. (cond
  10613. ((eq flag nil)
  10614. ;; try completion
  10615. (try-completion string thetable))
  10616. ((eq flag t)
  10617. ;; all-completions
  10618. (setq rtn (all-completions string thetable predicate))
  10619. (mapcar
  10620. (lambda (x)
  10621. (setq r (substring x l))
  10622. (if (string-match " ([^)]*)$" x)
  10623. (setq f (match-string 0 x))
  10624. (setq f ""))
  10625. (if (string-match "/" r)
  10626. (concat string (substring r 0 (match-end 0)) f)
  10627. x))
  10628. rtn))
  10629. ((eq flag 'lambda)
  10630. ;; exact match?
  10631. (assoc string thetable)))))
  10632. args)))
  10633. ;;;; Dynamic blocks
  10634. (defun org-find-dblock (name)
  10635. "Find the first dynamic block with name NAME in the buffer.
  10636. If not found, stay at current position and return nil."
  10637. (let ((case-fold-search t) pos)
  10638. (save-excursion
  10639. (goto-char (point-min))
  10640. (setq pos (and (re-search-forward
  10641. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10642. (match-beginning 0))))
  10643. (if pos (goto-char pos))
  10644. pos))
  10645. (defconst org-dblock-start-re
  10646. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10647. "Matches the start line of a dynamic block, with parameters.")
  10648. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10649. "Matches the end of a dynamic block.")
  10650. (defun org-create-dblock (plist)
  10651. "Create a dynamic block section, with parameters taken from PLIST.
  10652. PLIST must contain a :name entry which is used as name of the block."
  10653. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10654. (end-of-line 1)
  10655. (newline))
  10656. (let ((col (current-column))
  10657. (name (plist-get plist :name)))
  10658. (insert "#+BEGIN: " name)
  10659. (while plist
  10660. (if (eq (car plist) :name)
  10661. (setq plist (cddr plist))
  10662. (insert " " (prin1-to-string (pop plist)))))
  10663. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10664. (beginning-of-line -2)))
  10665. (defun org-prepare-dblock ()
  10666. "Prepare dynamic block for refresh.
  10667. This empties the block, puts the cursor at the insert position and returns
  10668. the property list including an extra property :name with the block name."
  10669. (unless (looking-at org-dblock-start-re)
  10670. (user-error "Not at a dynamic block"))
  10671. (let* ((begdel (1+ (match-end 0)))
  10672. (name (org-no-properties (match-string 1)))
  10673. (params (append (list :name name)
  10674. (read (concat "(" (match-string 3) ")")))))
  10675. (save-excursion
  10676. (beginning-of-line 1)
  10677. (skip-chars-forward " \t")
  10678. (setq params (plist-put params :indentation-column (current-column))))
  10679. (unless (re-search-forward org-dblock-end-re nil t)
  10680. (error "Dynamic block not terminated"))
  10681. (setq params
  10682. (append params
  10683. (list :content (buffer-substring
  10684. begdel (match-beginning 0)))))
  10685. (delete-region begdel (match-beginning 0))
  10686. (goto-char begdel)
  10687. (open-line 1)
  10688. params))
  10689. (defun org-map-dblocks (&optional command)
  10690. "Apply COMMAND to all dynamic blocks in the current buffer.
  10691. If COMMAND is not given, use `org-update-dblock'."
  10692. (let ((cmd (or command 'org-update-dblock)))
  10693. (save-excursion
  10694. (goto-char (point-min))
  10695. (while (re-search-forward org-dblock-start-re nil t)
  10696. (goto-char (match-beginning 0))
  10697. (save-excursion
  10698. (condition-case nil
  10699. (funcall cmd)
  10700. (error (message "Error during update of dynamic block"))))
  10701. (unless (re-search-forward org-dblock-end-re nil t)
  10702. (error "Dynamic block not terminated"))))))
  10703. (defun org-dblock-update (&optional arg)
  10704. "User command for updating dynamic blocks.
  10705. Update the dynamic block at point. With prefix ARG, update all dynamic
  10706. blocks in the buffer."
  10707. (interactive "P")
  10708. (if arg
  10709. (org-update-all-dblocks)
  10710. (or (looking-at org-dblock-start-re)
  10711. (org-beginning-of-dblock))
  10712. (org-update-dblock)))
  10713. (defun org-update-dblock ()
  10714. "Update the dynamic block at point.
  10715. This means to empty the block, parse for parameters and then call
  10716. the correct writing function."
  10717. (interactive)
  10718. (save-window-excursion
  10719. (let* ((pos (point))
  10720. (line (org-current-line))
  10721. (params (org-prepare-dblock))
  10722. (name (plist-get params :name))
  10723. (indent (plist-get params :indentation-column))
  10724. (cmd (intern (concat "org-dblock-write:" name))))
  10725. (message "Updating dynamic block `%s' at line %d..." name line)
  10726. (funcall cmd params)
  10727. (message "Updating dynamic block `%s' at line %d...done" name line)
  10728. (goto-char pos)
  10729. (when (and indent (> indent 0))
  10730. (setq indent (make-string indent ?\ ))
  10731. (save-excursion
  10732. (org-beginning-of-dblock)
  10733. (forward-line 1)
  10734. (while (not (looking-at org-dblock-end-re))
  10735. (insert indent)
  10736. (beginning-of-line 2))
  10737. (when (looking-at org-dblock-end-re)
  10738. (and (looking-at "[ \t]+")
  10739. (replace-match ""))
  10740. (insert indent)))))))
  10741. (defun org-beginning-of-dblock ()
  10742. "Find the beginning of the dynamic block at point.
  10743. Error if there is no such block at point."
  10744. (let ((pos (point))
  10745. beg)
  10746. (end-of-line 1)
  10747. (if (and (re-search-backward org-dblock-start-re nil t)
  10748. (setq beg (match-beginning 0))
  10749. (re-search-forward org-dblock-end-re nil t)
  10750. (> (match-end 0) pos))
  10751. (goto-char beg)
  10752. (goto-char pos)
  10753. (error "Not in a dynamic block"))))
  10754. (defun org-update-all-dblocks ()
  10755. "Update all dynamic blocks in the buffer.
  10756. This function can be used in a hook."
  10757. (interactive)
  10758. (when (derived-mode-p 'org-mode)
  10759. (org-map-dblocks 'org-update-dblock)))
  10760. ;;;; Completion
  10761. (defun org-get-export-keywords ()
  10762. "Return a list of all currently understood export keywords.
  10763. Export keywords include options, block names, attributes and
  10764. keywords relative to each registered export back-end."
  10765. (delq nil
  10766. (let (keywords)
  10767. (mapc
  10768. (lambda (back-end)
  10769. (let ((props (cdr back-end)))
  10770. ;; Back-end name (for keywords, like #+LATEX:)
  10771. (push (upcase (symbol-name (car back-end))) keywords)
  10772. ;; Back-end options.
  10773. (mapc (lambda (option) (push (cadr option) keywords))
  10774. (plist-get (cdr back-end) :options-alist))))
  10775. (org-bound-and-true-p org-export-registered-backends))
  10776. keywords)))
  10777. (defconst org-options-keywords
  10778. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10779. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10780. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10781. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10782. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10783. (defcustom org-structure-template-alist
  10784. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10785. "<src lang=\"?\">\n\n</src>")
  10786. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10787. "<example>\n?\n</example>")
  10788. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10789. "<quote>\n?\n</quote>")
  10790. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10791. "<verse>\n?\n</verse>")
  10792. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10793. "<verbatim>\n?\n</verbatim>")
  10794. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10795. "<center>\n?\n</center>")
  10796. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10797. "<literal style=\"latex\">\n?\n</literal>")
  10798. ("L" "#+LaTeX: "
  10799. "<literal style=\"latex\">?</literal>")
  10800. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10801. "<literal style=\"html\">\n?\n</literal>")
  10802. ("H" "#+HTML: "
  10803. "<literal style=\"html\">?</literal>")
  10804. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10805. ("A" "#+ASCII: ")
  10806. ("i" "#+INDEX: ?"
  10807. "#+INDEX: ?")
  10808. ("I" "#+INCLUDE: %file ?"
  10809. "<include file=%file markup=\"?\">"))
  10810. "Structure completion elements.
  10811. This is a list of abbreviation keys and values. The value gets inserted
  10812. if you type `<' followed by the key and then press the completion key,
  10813. usually `M-TAB'. %file will be replaced by a file name after prompting
  10814. for the file using completion. The cursor will be placed at the position
  10815. of the `?` in the template.
  10816. There are two templates for each key, the first uses the original Org syntax,
  10817. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10818. the default when the /org-mtags.el/ module has been loaded. See also the
  10819. variable `org-mtags-prefer-muse-templates'."
  10820. :group 'org-completion
  10821. :type '(repeat
  10822. (string :tag "Key")
  10823. (string :tag "Template")
  10824. (string :tag "Muse Template")))
  10825. (defun org-try-structure-completion ()
  10826. "Try to complete a structure template before point.
  10827. This looks for strings like \"<e\" on an otherwise empty line and
  10828. expands them."
  10829. (let ((l (buffer-substring (point-at-bol) (point)))
  10830. a)
  10831. (when (and (looking-at "[ \t]*$")
  10832. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10833. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10834. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10835. (match-beginning 1)) a)
  10836. t)))
  10837. (defun org-complete-expand-structure-template (start cell)
  10838. "Expand a structure template."
  10839. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10840. (rpl (nth (if musep 2 1) cell))
  10841. (ind ""))
  10842. (delete-region start (point))
  10843. (when (string-match "\\`#\\+" rpl)
  10844. (cond
  10845. ((bolp))
  10846. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10847. (setq ind (buffer-substring (point-at-bol) (point))))
  10848. (t (newline))))
  10849. (setq start (point))
  10850. (if (string-match "%file" rpl)
  10851. (setq rpl (replace-match
  10852. (concat
  10853. "\""
  10854. (save-match-data
  10855. (abbreviate-file-name (read-file-name "Include file: ")))
  10856. "\"")
  10857. t t rpl)))
  10858. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10859. (concat "\n" ind)))
  10860. (insert rpl)
  10861. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10862. ;;;; TODO, DEADLINE, Comments
  10863. (defun org-toggle-comment ()
  10864. "Change the COMMENT state of an entry."
  10865. (interactive)
  10866. (save-excursion
  10867. (org-back-to-heading)
  10868. (let (case-fold-search)
  10869. (cond
  10870. ((looking-at (format org-heading-keyword-regexp-format
  10871. org-comment-string))
  10872. (goto-char (match-end 1))
  10873. (looking-at (concat " +" org-comment-string))
  10874. (replace-match "" t t)
  10875. (when (eolp) (insert " ")))
  10876. ((looking-at org-outline-regexp)
  10877. (goto-char (match-end 0))
  10878. (insert org-comment-string " "))))))
  10879. (defvar org-last-todo-state-is-todo nil
  10880. "This is non-nil when the last TODO state change led to a TODO state.
  10881. If the last change removed the TODO tag or switched to DONE, then
  10882. this is nil.")
  10883. (defvar org-setting-tags nil) ; dynamically skipped
  10884. (defvar org-todo-setup-filter-hook nil
  10885. "Hook for functions that pre-filter todo specs.
  10886. Each function takes a todo spec and returns either nil or the spec
  10887. transformed into canonical form." )
  10888. (defvar org-todo-get-default-hook nil
  10889. "Hook for functions that get a default item for todo.
  10890. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10891. nil or a string to be used for the todo mark." )
  10892. (defvar org-agenda-headline-snapshot-before-repeat)
  10893. (defun org-current-effective-time ()
  10894. "Return current time adjusted for `org-extend-today-until' variable."
  10895. (let* ((ct (org-current-time))
  10896. (dct (decode-time ct))
  10897. (ct1
  10898. (cond
  10899. (org-use-last-clock-out-time-as-effective-time
  10900. (or (org-clock-get-last-clock-out-time) ct))
  10901. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10902. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10903. (t ct))))
  10904. ct1))
  10905. (defun org-todo-yesterday (&optional arg)
  10906. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10907. (interactive "P")
  10908. (if (eq major-mode 'org-agenda-mode)
  10909. (apply 'org-agenda-todo-yesterday arg)
  10910. (let* ((hour (third (decode-time
  10911. (org-current-time))))
  10912. (org-extend-today-until (1+ hour)))
  10913. (org-todo arg))))
  10914. (defvar org-block-entry-blocking ""
  10915. "First entry preventing the TODO state change.")
  10916. (defun org-todo (&optional arg)
  10917. "Change the TODO state of an item.
  10918. The state of an item is given by a keyword at the start of the heading,
  10919. like
  10920. *** TODO Write paper
  10921. *** DONE Call mom
  10922. The different keywords are specified in the variable `org-todo-keywords'.
  10923. By default the available states are \"TODO\" and \"DONE\".
  10924. So for this example: when the item starts with TODO, it is changed to DONE.
  10925. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10926. DONE are present, add TODO at the beginning of the heading.
  10927. With \\[universal-argument] prefix arg, use completion to determine the new \
  10928. state.
  10929. With numeric prefix arg, switch to that state.
  10930. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10931. keywords (nextset).
  10932. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10933. With a numeric prefix arg of 0, inhibit note taking for the change.
  10934. For calling through lisp, arg is also interpreted in the following way:
  10935. 'none -> empty state
  10936. \"\"(empty string) -> switch to empty state
  10937. 'done -> switch to DONE
  10938. 'nextset -> switch to the next set of keywords
  10939. 'previousset -> switch to the previous set of keywords
  10940. \"WAITING\" -> switch to the specified keyword, but only if it
  10941. really is a member of `org-todo-keywords'."
  10942. (interactive "P")
  10943. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10944. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10945. 'region-start-level 'region))
  10946. org-loop-over-headlines-in-active-region)
  10947. (org-map-entries
  10948. `(org-todo ,arg)
  10949. org-loop-over-headlines-in-active-region
  10950. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10951. (if (equal arg '(16)) (setq arg 'nextset))
  10952. (let ((org-blocker-hook org-blocker-hook)
  10953. commentp
  10954. case-fold-search)
  10955. (when (equal arg '(64))
  10956. (setq arg nil org-blocker-hook nil))
  10957. (when (and org-blocker-hook
  10958. (or org-inhibit-blocking
  10959. (org-entry-get nil "NOBLOCKING")))
  10960. (setq org-blocker-hook nil))
  10961. (save-excursion
  10962. (catch 'exit
  10963. (org-back-to-heading t)
  10964. (when (looking-at (concat "^\\*+ " org-comment-string))
  10965. (org-toggle-comment)
  10966. (setq commentp t))
  10967. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10968. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10969. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10970. (let* ((match-data (match-data))
  10971. (startpos (point-at-bol))
  10972. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10973. (org-log-done org-log-done)
  10974. (org-log-repeat org-log-repeat)
  10975. (org-todo-log-states org-todo-log-states)
  10976. (org-inhibit-logging
  10977. (if (equal arg 0)
  10978. (progn (setq arg nil) 'note) org-inhibit-logging))
  10979. (this (match-string 1))
  10980. (hl-pos (match-beginning 0))
  10981. (head (org-get-todo-sequence-head this))
  10982. (ass (assoc head org-todo-kwd-alist))
  10983. (interpret (nth 1 ass))
  10984. (done-word (nth 3 ass))
  10985. (final-done-word (nth 4 ass))
  10986. (org-last-state (or this ""))
  10987. (completion-ignore-case t)
  10988. (member (member this org-todo-keywords-1))
  10989. (tail (cdr member))
  10990. (org-state (cond
  10991. ((and org-todo-key-trigger
  10992. (or (and (equal arg '(4))
  10993. (eq org-use-fast-todo-selection 'prefix))
  10994. (and (not arg) org-use-fast-todo-selection
  10995. (not (eq org-use-fast-todo-selection
  10996. 'prefix)))))
  10997. ;; Use fast selection
  10998. (org-fast-todo-selection))
  10999. ((and (equal arg '(4))
  11000. (or (not org-use-fast-todo-selection)
  11001. (not org-todo-key-trigger)))
  11002. ;; Read a state with completion
  11003. (org-icompleting-read
  11004. "State: " (mapcar 'list org-todo-keywords-1)
  11005. nil t))
  11006. ((eq arg 'right)
  11007. (if this
  11008. (if tail (car tail) nil)
  11009. (car org-todo-keywords-1)))
  11010. ((eq arg 'left)
  11011. (if (equal member org-todo-keywords-1)
  11012. nil
  11013. (if this
  11014. (nth (- (length org-todo-keywords-1)
  11015. (length tail) 2)
  11016. org-todo-keywords-1)
  11017. (org-last org-todo-keywords-1))))
  11018. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11019. (setq arg nil))) ; hack to fall back to cycling
  11020. (arg
  11021. ;; user or caller requests a specific state
  11022. (cond
  11023. ((equal arg "") nil)
  11024. ((eq arg 'none) nil)
  11025. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11026. ((eq arg 'nextset)
  11027. (or (car (cdr (member head org-todo-heads)))
  11028. (car org-todo-heads)))
  11029. ((eq arg 'previousset)
  11030. (let ((org-todo-heads (reverse org-todo-heads)))
  11031. (or (car (cdr (member head org-todo-heads)))
  11032. (car org-todo-heads))))
  11033. ((car (member arg org-todo-keywords-1)))
  11034. ((stringp arg)
  11035. (user-error "State `%s' not valid in this file" arg))
  11036. ((nth (1- (prefix-numeric-value arg))
  11037. org-todo-keywords-1))))
  11038. ((null member) (or head (car org-todo-keywords-1)))
  11039. ((equal this final-done-word) nil) ;; -> make empty
  11040. ((null tail) nil) ;; -> first entry
  11041. ((memq interpret '(type priority))
  11042. (if (eq this-command last-command)
  11043. (car tail)
  11044. (if (> (length tail) 0)
  11045. (or done-word (car org-done-keywords))
  11046. nil)))
  11047. (t
  11048. (car tail))))
  11049. (org-state (or
  11050. (run-hook-with-args-until-success
  11051. 'org-todo-get-default-hook org-state org-last-state)
  11052. org-state))
  11053. (next (if org-state (concat " " org-state " ") " "))
  11054. (change-plist (list :type 'todo-state-change :from this :to org-state
  11055. :position startpos))
  11056. dolog now-done-p)
  11057. (when org-blocker-hook
  11058. (setq org-last-todo-state-is-todo
  11059. (not (member this org-done-keywords)))
  11060. (unless (save-excursion
  11061. (save-match-data
  11062. (org-with-wide-buffer
  11063. (run-hook-with-args-until-failure
  11064. 'org-blocker-hook change-plist))))
  11065. (if (org-called-interactively-p 'interactive)
  11066. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11067. this org-state org-block-entry-blocking)
  11068. ;; fail silently
  11069. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11070. this org-state org-block-entry-blocking)
  11071. (throw 'exit nil))))
  11072. (store-match-data match-data)
  11073. (replace-match next t t)
  11074. (unless (pos-visible-in-window-p hl-pos)
  11075. (message "TODO state changed to %s" (org-trim next)))
  11076. (unless head
  11077. (setq head (org-get-todo-sequence-head org-state)
  11078. ass (assoc head org-todo-kwd-alist)
  11079. interpret (nth 1 ass)
  11080. done-word (nth 3 ass)
  11081. final-done-word (nth 4 ass)))
  11082. (when (memq arg '(nextset previousset))
  11083. (message "Keyword-Set %d/%d: %s"
  11084. (- (length org-todo-sets) -1
  11085. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11086. (length org-todo-sets)
  11087. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11088. (setq org-last-todo-state-is-todo
  11089. (not (member org-state org-done-keywords)))
  11090. (setq now-done-p (and (member org-state org-done-keywords)
  11091. (not (member this org-done-keywords))))
  11092. (and logging (org-local-logging logging))
  11093. (when (and (or org-todo-log-states org-log-done)
  11094. (not (eq org-inhibit-logging t))
  11095. (not (memq arg '(nextset previousset))))
  11096. ;; we need to look at recording a time and note
  11097. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11098. (nth 2 (assoc this org-todo-log-states))))
  11099. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11100. (setq dolog 'time))
  11101. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11102. (and org-state
  11103. (member org-state org-not-done-keywords)
  11104. (not (member this org-not-done-keywords))))
  11105. ;; This is now a todo state and was not one before
  11106. ;; If there was a CLOSED time stamp, get rid of it.
  11107. (org-add-planning-info nil nil 'closed))
  11108. (when (and now-done-p org-log-done)
  11109. ;; It is now done, and it was not done before
  11110. (org-add-planning-info 'closed (org-current-effective-time))
  11111. (if (and (not dolog) (eq 'note org-log-done))
  11112. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11113. (when (and org-state dolog)
  11114. ;; This is a non-nil state, and we need to log it
  11115. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11116. ;; Fixup tag positioning
  11117. (org-todo-trigger-tag-changes org-state)
  11118. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11119. (when org-provide-todo-statistics
  11120. (org-update-parent-todo-statistics))
  11121. (run-hooks 'org-after-todo-state-change-hook)
  11122. (if (and arg (not (member org-state org-done-keywords)))
  11123. (setq head (org-get-todo-sequence-head org-state)))
  11124. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11125. ;; Do we need to trigger a repeat?
  11126. (when now-done-p
  11127. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11128. ;; This is for the agenda, take a snapshot of the headline.
  11129. (save-match-data
  11130. (setq org-agenda-headline-snapshot-before-repeat
  11131. (org-get-heading))))
  11132. (org-auto-repeat-maybe org-state))
  11133. ;; Fixup cursor location if close to the keyword
  11134. (if (and (outline-on-heading-p)
  11135. (not (bolp))
  11136. (save-excursion (beginning-of-line 1)
  11137. (looking-at org-todo-line-regexp))
  11138. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11139. (progn
  11140. (goto-char (or (match-end 2) (match-end 1)))
  11141. (and (looking-at " ") (just-one-space))))
  11142. (when org-trigger-hook
  11143. (save-excursion
  11144. (run-hook-with-args 'org-trigger-hook change-plist)))
  11145. (when commentp (org-toggle-comment))))))))
  11146. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11147. "Block turning an entry into a TODO, using the hierarchy.
  11148. This checks whether the current task should be blocked from state
  11149. changes. Such blocking occurs when:
  11150. 1. The task has children which are not all in a completed state.
  11151. 2. A task has a parent with the property :ORDERED:, and there
  11152. are siblings prior to the current task with incomplete
  11153. status.
  11154. 3. The parent of the task is blocked because it has siblings that should
  11155. be done first, or is child of a block grandparent TODO entry."
  11156. (if (not org-enforce-todo-dependencies)
  11157. t ; if locally turned off don't block
  11158. (catch 'dont-block
  11159. ;; If this is not a todo state change, or if this entry is already DONE,
  11160. ;; do not block
  11161. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11162. (member (plist-get change-plist :from)
  11163. (cons 'done org-done-keywords))
  11164. (member (plist-get change-plist :to)
  11165. (cons 'todo org-not-done-keywords))
  11166. (not (plist-get change-plist :to)))
  11167. (throw 'dont-block t))
  11168. ;; If this task has children, and any are undone, it's blocked
  11169. (save-excursion
  11170. (org-back-to-heading t)
  11171. (let ((this-level (funcall outline-level)))
  11172. (outline-next-heading)
  11173. (let ((child-level (funcall outline-level)))
  11174. (while (and (not (eobp))
  11175. (> child-level this-level))
  11176. ;; this todo has children, check whether they are all
  11177. ;; completed
  11178. (if (and (not (org-entry-is-done-p))
  11179. (org-entry-is-todo-p))
  11180. (progn (setq org-block-entry-blocking (org-get-heading))
  11181. (throw 'dont-block nil)))
  11182. (outline-next-heading)
  11183. (setq child-level (funcall outline-level))))))
  11184. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11185. ;; any previous siblings are undone, it's blocked
  11186. (save-excursion
  11187. (org-back-to-heading t)
  11188. (let* ((pos (point))
  11189. (parent-pos (and (org-up-heading-safe) (point))))
  11190. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11191. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11192. (forward-line 1)
  11193. (re-search-forward org-not-done-heading-regexp pos t))
  11194. (setq org-block-entry-blocking (match-string 0))
  11195. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11196. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11197. (while t
  11198. (goto-char parent-pos)
  11199. (if (not (looking-at org-not-done-heading-regexp))
  11200. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11201. (setq pos (point))
  11202. (setq parent-pos (and (org-up-heading-safe) (point)))
  11203. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11204. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11205. (forward-line 1)
  11206. (re-search-forward org-not-done-heading-regexp pos t)
  11207. (setq org-block-entry-blocking (org-get-heading)))
  11208. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11209. (defcustom org-track-ordered-property-with-tag nil
  11210. "Should the ORDERED property also be shown as a tag?
  11211. The ORDERED property decides if an entry should require subtasks to be
  11212. completed in sequence. Since a property is not very visible, setting
  11213. this option means that toggling the ORDERED property with the command
  11214. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11215. not relevant for the behavior, but it makes things more visible.
  11216. Note that toggling the tag with tags commands will not change the property
  11217. and therefore not influence behavior!
  11218. This can be t, meaning the tag ORDERED should be used, It can also be a
  11219. string to select a different tag for this task."
  11220. :group 'org-todo
  11221. :type '(choice
  11222. (const :tag "No tracking" nil)
  11223. (const :tag "Track with ORDERED tag" t)
  11224. (string :tag "Use other tag")))
  11225. (defun org-toggle-ordered-property ()
  11226. "Toggle the ORDERED property of the current entry.
  11227. For better visibility, you can track the value of this property with a tag.
  11228. See variable `org-track-ordered-property-with-tag'."
  11229. (interactive)
  11230. (let* ((t1 org-track-ordered-property-with-tag)
  11231. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11232. (save-excursion
  11233. (org-back-to-heading)
  11234. (if (org-entry-get nil "ORDERED")
  11235. (progn
  11236. (org-delete-property "ORDERED" "PROPERTIES")
  11237. (and tag (org-toggle-tag tag 'off))
  11238. (message "Subtasks can be completed in arbitrary order"))
  11239. (org-entry-put nil "ORDERED" "t")
  11240. (and tag (org-toggle-tag tag 'on))
  11241. (message "Subtasks must be completed in sequence")))))
  11242. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11243. (defun org-block-todo-from-checkboxes (change-plist)
  11244. "Block turning an entry into a TODO, using checkboxes.
  11245. This checks whether the current task should be blocked from state
  11246. changes because there are unchecked boxes in this entry."
  11247. (if (not org-enforce-todo-checkbox-dependencies)
  11248. t ; if locally turned off don't block
  11249. (catch 'dont-block
  11250. ;; If this is not a todo state change, or if this entry is already DONE,
  11251. ;; do not block
  11252. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11253. (member (plist-get change-plist :from)
  11254. (cons 'done org-done-keywords))
  11255. (member (plist-get change-plist :to)
  11256. (cons 'todo org-not-done-keywords))
  11257. (not (plist-get change-plist :to)))
  11258. (throw 'dont-block t))
  11259. ;; If this task has checkboxes that are not checked, it's blocked
  11260. (save-excursion
  11261. (org-back-to-heading t)
  11262. (let ((beg (point)) end)
  11263. (outline-next-heading)
  11264. (setq end (point))
  11265. (goto-char beg)
  11266. (if (org-list-search-forward
  11267. (concat (org-item-beginning-re)
  11268. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11269. "\\[[- ]\\]")
  11270. end t)
  11271. (progn
  11272. (if (boundp 'org-blocked-by-checkboxes)
  11273. (setq org-blocked-by-checkboxes t))
  11274. (throw 'dont-block nil)))))
  11275. t))) ; do not block
  11276. (defun org-entry-blocked-p ()
  11277. "Is the current entry blocked?"
  11278. (org-with-silent-modifications
  11279. (if (org-entry-get nil "NOBLOCKING")
  11280. nil ;; Never block this entry
  11281. (not (run-hook-with-args-until-failure
  11282. 'org-blocker-hook
  11283. (list :type 'todo-state-change
  11284. :position (point)
  11285. :from 'todo
  11286. :to 'done))))))
  11287. (defun org-update-statistics-cookies (all)
  11288. "Update the statistics cookie, either from TODO or from checkboxes.
  11289. This should be called with the cursor in a line with a statistics cookie."
  11290. (interactive "P")
  11291. (if all
  11292. (progn
  11293. (org-update-checkbox-count 'all)
  11294. (org-map-entries 'org-update-parent-todo-statistics))
  11295. (if (not (org-at-heading-p))
  11296. (org-update-checkbox-count)
  11297. (let ((pos (point-marker))
  11298. end l1 l2)
  11299. (ignore-errors (org-back-to-heading t))
  11300. (if (not (org-at-heading-p))
  11301. (org-update-checkbox-count)
  11302. (setq l1 (org-outline-level))
  11303. (setq end (save-excursion
  11304. (outline-next-heading)
  11305. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11306. (point)))
  11307. (if (and (save-excursion
  11308. (re-search-forward
  11309. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11310. (not (save-excursion (re-search-forward
  11311. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11312. (org-update-checkbox-count)
  11313. (if (and l2 (> l2 l1))
  11314. (progn
  11315. (goto-char end)
  11316. (org-update-parent-todo-statistics))
  11317. (goto-char pos)
  11318. (beginning-of-line 1)
  11319. (while (re-search-forward
  11320. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11321. (point-at-eol) t)
  11322. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11323. (goto-char pos)
  11324. (move-marker pos nil)))))
  11325. (defvar org-entry-property-inherited-from) ;; defined below
  11326. (defun org-update-parent-todo-statistics ()
  11327. "Update any statistics cookie in the parent of the current headline.
  11328. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11329. the entire subtree and this will travel up the hierarchy and update
  11330. statistics everywhere."
  11331. (let* ((prop (save-excursion (org-up-heading-safe)
  11332. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11333. (recursive (or (not org-hierarchical-todo-statistics)
  11334. (and prop (string-match "\\<recursive\\>" prop))))
  11335. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11336. 0))
  11337. (first t)
  11338. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11339. level ltoggle l1 new ndel
  11340. (cnt-all 0) (cnt-done 0) is-percent kwd
  11341. checkbox-beg ov ovs ove cookie-present)
  11342. (catch 'exit
  11343. (save-excursion
  11344. (beginning-of-line 1)
  11345. (setq ltoggle (funcall outline-level))
  11346. ;; Three situations are to consider:
  11347. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11348. ;; to the top-level ancestor on the headline;
  11349. ;; 2. If parent has "recursive" property, repeat up to the
  11350. ;; headline setting that property, taking inheritance into
  11351. ;; account;
  11352. ;; 3. Else, move up to direct parent and proceed only once.
  11353. (while (and (setq level (org-up-heading-safe))
  11354. (or recursive first)
  11355. (>= (point) lim))
  11356. (setq first nil cookie-present nil)
  11357. (unless (and level
  11358. (not (string-match
  11359. "\\<checkbox\\>"
  11360. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11361. "")))))
  11362. (throw 'exit nil))
  11363. (while (re-search-forward box-re (point-at-eol) t)
  11364. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11365. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11366. (save-match-data
  11367. (unless (outline-next-heading) (throw 'exit nil))
  11368. (while (and (looking-at org-complex-heading-regexp)
  11369. (> (setq l1 (length (match-string 1))) level))
  11370. (setq kwd (and (or recursive (= l1 ltoggle))
  11371. (match-string 2)))
  11372. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11373. (and (listp org-provide-todo-statistics)
  11374. (or (member kwd org-provide-todo-statistics)
  11375. (member kwd org-done-keywords))))
  11376. (setq cnt-all (1+ cnt-all))
  11377. (if (eq org-provide-todo-statistics t)
  11378. (and kwd (setq cnt-all (1+ cnt-all)))))
  11379. (and (member kwd org-done-keywords)
  11380. (setq cnt-done (1+ cnt-done)))
  11381. (outline-next-heading)))
  11382. (setq new
  11383. (if is-percent
  11384. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11385. (format "[%d/%d]" cnt-done cnt-all))
  11386. ndel (- (match-end 0) checkbox-beg))
  11387. ;; handle overlays when updating cookie from column view
  11388. (when (setq ov (car (overlays-at checkbox-beg)))
  11389. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11390. (delete-overlay ov))
  11391. (goto-char checkbox-beg)
  11392. (insert new)
  11393. (delete-region (point) (+ (point) ndel))
  11394. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11395. (when ov (move-overlay ov ovs ove)))
  11396. (when cookie-present
  11397. (run-hook-with-args 'org-after-todo-statistics-hook
  11398. cnt-done (- cnt-all cnt-done))))))
  11399. (run-hooks 'org-todo-statistics-hook)))
  11400. (defvar org-after-todo-statistics-hook nil
  11401. "Hook that is called after a TODO statistics cookie has been updated.
  11402. Each function is called with two arguments: the number of not-done entries
  11403. and the number of done entries.
  11404. For example, the following function, when added to this hook, will switch
  11405. an entry to DONE when all children are done, and back to TODO when new
  11406. entries are set to a TODO status. Note that this hook is only called
  11407. when there is a statistics cookie in the headline!
  11408. (defun org-summary-todo (n-done n-not-done)
  11409. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11410. (let (org-log-done org-log-states) ; turn off logging
  11411. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11412. ")
  11413. (defvar org-todo-statistics-hook nil
  11414. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11415. This hook runs even if there is no statistics cookie present, in which case
  11416. `org-after-todo-statistics-hook' would not run.")
  11417. (defun org-todo-trigger-tag-changes (state)
  11418. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11419. (let ((l org-todo-state-tags-triggers)
  11420. changes)
  11421. (when (or (not state) (equal state ""))
  11422. (setq changes (append changes (cdr (assoc "" l)))))
  11423. (when (and (stringp state) (> (length state) 0))
  11424. (setq changes (append changes (cdr (assoc state l)))))
  11425. (when (member state org-not-done-keywords)
  11426. (setq changes (append changes (cdr (assoc 'todo l)))))
  11427. (when (member state org-done-keywords)
  11428. (setq changes (append changes (cdr (assoc 'done l)))))
  11429. (dolist (c changes)
  11430. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11431. (defun org-local-logging (value)
  11432. "Get logging settings from a property VALUE."
  11433. (let* (words w a)
  11434. ;; directly set the variables, they are already local.
  11435. (setq org-log-done nil
  11436. org-log-repeat nil
  11437. org-todo-log-states nil)
  11438. (setq words (org-split-string value))
  11439. (while (setq w (pop words))
  11440. (cond
  11441. ((setq a (assoc w org-startup-options))
  11442. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11443. (set (nth 1 a) (nth 2 a))))
  11444. ((setq a (org-extract-log-state-settings w))
  11445. (and (member (car a) org-todo-keywords-1)
  11446. (push a org-todo-log-states)))))))
  11447. (defun org-get-todo-sequence-head (kwd)
  11448. "Return the head of the TODO sequence to which KWD belongs.
  11449. If KWD is not set, check if there is a text property remembering the
  11450. right sequence."
  11451. (let (p)
  11452. (cond
  11453. ((not kwd)
  11454. (or (get-text-property (point-at-bol) 'org-todo-head)
  11455. (progn
  11456. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11457. nil (point-at-eol)))
  11458. (get-text-property p 'org-todo-head))))
  11459. ((not (member kwd org-todo-keywords-1))
  11460. (car org-todo-keywords-1))
  11461. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11462. (defun org-fast-todo-selection ()
  11463. "Fast TODO keyword selection with single keys.
  11464. Returns the new TODO keyword, or nil if no state change should occur."
  11465. (let* ((fulltable org-todo-key-alist)
  11466. (done-keywords org-done-keywords) ;; needed for the faces.
  11467. (maxlen (apply 'max (mapcar
  11468. (lambda (x)
  11469. (if (stringp (car x)) (string-width (car x)) 0))
  11470. fulltable)))
  11471. (expert nil)
  11472. (fwidth (+ maxlen 3 1 3))
  11473. (ncol (/ (- (window-width) 4) fwidth))
  11474. tg cnt e c tbl
  11475. groups ingroup)
  11476. (save-excursion
  11477. (save-window-excursion
  11478. (if expert
  11479. (set-buffer (get-buffer-create " *Org todo*"))
  11480. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11481. (erase-buffer)
  11482. (org-set-local 'org-done-keywords done-keywords)
  11483. (setq tbl fulltable cnt 0)
  11484. (while (setq e (pop tbl))
  11485. (cond
  11486. ((equal e '(:startgroup))
  11487. (push '() groups) (setq ingroup t)
  11488. (when (not (= cnt 0))
  11489. (setq cnt 0)
  11490. (insert "\n"))
  11491. (insert "{ "))
  11492. ((equal e '(:endgroup))
  11493. (setq ingroup nil cnt 0)
  11494. (insert "}\n"))
  11495. ((equal e '(:newline))
  11496. (when (not (= cnt 0))
  11497. (setq cnt 0)
  11498. (insert "\n")
  11499. (setq e (car tbl))
  11500. (while (equal (car tbl) '(:newline))
  11501. (insert "\n")
  11502. (setq tbl (cdr tbl)))))
  11503. (t
  11504. (setq tg (car e) c (cdr e))
  11505. (if ingroup (push tg (car groups)))
  11506. (setq tg (org-add-props tg nil 'face
  11507. (org-get-todo-face tg)))
  11508. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11509. (insert "[" c "] " tg (make-string
  11510. (- fwidth 4 (length tg)) ?\ ))
  11511. (when (= (setq cnt (1+ cnt)) ncol)
  11512. (insert "\n")
  11513. (if ingroup (insert " "))
  11514. (setq cnt 0)))))
  11515. (insert "\n")
  11516. (goto-char (point-min))
  11517. (if (not expert) (org-fit-window-to-buffer))
  11518. (message "[a-z..]:Set [SPC]:clear")
  11519. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11520. (cond
  11521. ((or (= c ?\C-g)
  11522. (and (= c ?q) (not (rassoc c fulltable))))
  11523. (setq quit-flag t))
  11524. ((= c ?\ ) nil)
  11525. ((setq e (rassoc c fulltable) tg (car e))
  11526. tg)
  11527. (t (setq quit-flag t)))))))
  11528. (defun org-entry-is-todo-p ()
  11529. (member (org-get-todo-state) org-not-done-keywords))
  11530. (defun org-entry-is-done-p ()
  11531. (member (org-get-todo-state) org-done-keywords))
  11532. (defun org-get-todo-state ()
  11533. "Return the TODO keyword of the current subtree."
  11534. (save-excursion
  11535. (org-back-to-heading t)
  11536. (and (looking-at org-todo-line-regexp)
  11537. (match-end 2)
  11538. (match-string 2))))
  11539. (defun org-at-date-range-p (&optional inactive-ok)
  11540. "Is the cursor inside a date range?"
  11541. (interactive)
  11542. (save-excursion
  11543. (catch 'exit
  11544. (let ((pos (point)))
  11545. (skip-chars-backward "^[<\r\n")
  11546. (skip-chars-backward "<[")
  11547. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11548. (>= (match-end 0) pos)
  11549. (throw 'exit t))
  11550. (skip-chars-backward "^<[\r\n")
  11551. (skip-chars-backward "<[")
  11552. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11553. (>= (match-end 0) pos)
  11554. (throw 'exit t)))
  11555. nil)))
  11556. (defun org-get-repeat (&optional tagline)
  11557. "Check if there is a deadline/schedule with repeater in this entry."
  11558. (save-match-data
  11559. (save-excursion
  11560. (org-back-to-heading t)
  11561. (and (re-search-forward (if tagline
  11562. (concat tagline "\\s-*" org-repeat-re)
  11563. org-repeat-re)
  11564. (org-entry-end-position) t)
  11565. (match-string-no-properties 1)))))
  11566. (defvar org-last-changed-timestamp)
  11567. (defvar org-last-inserted-timestamp)
  11568. (defvar org-log-post-message)
  11569. (defvar org-log-note-purpose)
  11570. (defvar org-log-note-how)
  11571. (defvar org-log-note-extra)
  11572. (defun org-auto-repeat-maybe (done-word)
  11573. "Check if the current headline contains a repeated deadline/schedule.
  11574. If yes, set TODO state back to what it was and change the base date
  11575. of repeating deadline/scheduled time stamps to new date.
  11576. This function is run automatically after each state change to a DONE state."
  11577. ;; last-state is dynamically scoped into this function
  11578. (let* ((repeat (org-get-repeat))
  11579. (aa (assoc org-last-state org-todo-kwd-alist))
  11580. (interpret (nth 1 aa))
  11581. (head (nth 2 aa))
  11582. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11583. (msg "Entry repeats: ")
  11584. (org-log-done nil)
  11585. (org-todo-log-states nil)
  11586. re type n what ts time to-state)
  11587. (when repeat
  11588. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11589. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11590. org-todo-repeat-to-state))
  11591. (unless (and to-state (member to-state org-todo-keywords-1))
  11592. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11593. (org-todo to-state)
  11594. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11595. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11596. (org-time-stamp-format t t))))
  11597. (when org-log-repeat
  11598. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11599. (memq 'org-add-log-note post-command-hook))
  11600. ;; OK, we are already setup for some record
  11601. (if (eq org-log-repeat 'note)
  11602. ;; make sure we take a note, not only a time stamp
  11603. (setq org-log-note-how 'note))
  11604. ;; Set up for taking a record
  11605. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11606. org-last-state
  11607. 'findpos org-log-repeat)))
  11608. (org-back-to-heading t)
  11609. (org-add-planning-info nil nil 'closed)
  11610. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11611. org-deadline-time-regexp "\\)\\|\\("
  11612. org-ts-regexp "\\)"))
  11613. (while (re-search-forward
  11614. re (save-excursion (outline-next-heading) (point)) t)
  11615. (setq type (if (match-end 1) org-scheduled-string
  11616. (if (match-end 3) org-deadline-string "Plain:"))
  11617. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11618. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11619. (setq n (string-to-number (match-string 2 ts))
  11620. what (match-string 3 ts))
  11621. (if (equal what "w") (setq n (* n 7) what "d"))
  11622. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11623. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11624. ;; Preparation, see if we need to modify the start date for the change
  11625. (when (match-end 1)
  11626. (setq time (save-match-data (org-time-string-to-time ts)))
  11627. (cond
  11628. ((equal (match-string 1 ts) ".")
  11629. ;; Shift starting date to today
  11630. (org-timestamp-change
  11631. (- (org-today) (time-to-days time))
  11632. 'day))
  11633. ((equal (match-string 1 ts) "+")
  11634. (let ((nshiftmax 10) (nshift 0))
  11635. (while (or (= nshift 0)
  11636. (<= (time-to-days time)
  11637. (time-to-days (current-time))))
  11638. (when (= (incf nshift) nshiftmax)
  11639. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11640. (error "Abort")))
  11641. (org-timestamp-change n (cdr (assoc what whata)))
  11642. (org-at-timestamp-p t)
  11643. (setq ts (match-string 1))
  11644. (setq time (save-match-data (org-time-string-to-time ts)))))
  11645. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11646. ;; rematch, so that we have everything in place for the real shift
  11647. (org-at-timestamp-p t)
  11648. (setq ts (match-string 1))
  11649. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11650. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11651. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11652. (setq org-log-post-message msg)
  11653. (message "%s" msg))))
  11654. (defun org-show-todo-tree (arg)
  11655. "Make a compact tree which shows all headlines marked with TODO.
  11656. The tree will show the lines where the regexp matches, and all higher
  11657. headlines above the match.
  11658. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11659. With a numeric prefix N, construct a sparse tree for the Nth element
  11660. of `org-todo-keywords-1'."
  11661. (interactive "P")
  11662. (let ((case-fold-search nil)
  11663. (kwd-re
  11664. (cond ((null arg) org-not-done-regexp)
  11665. ((equal arg '(4))
  11666. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11667. (mapcar 'list org-todo-keywords-1))))
  11668. (concat "\\("
  11669. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11670. "\\)\\>")))
  11671. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11672. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11673. org-todo-keywords-1)))
  11674. (t (user-error "Invalid prefix argument: %s" arg)))))
  11675. (message "%d TODO entries found"
  11676. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11677. (defun org-deadline (arg &optional time)
  11678. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11679. With one universal prefix argument, remove any deadline from the item.
  11680. With two universal prefix arguments, prompt for a warning delay.
  11681. With argument TIME, set the deadline at the corresponding date. TIME
  11682. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11683. (interactive "P")
  11684. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11685. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11686. 'region-start-level 'region))
  11687. org-loop-over-headlines-in-active-region)
  11688. (org-map-entries
  11689. `(org-deadline ',arg ,time)
  11690. org-loop-over-headlines-in-active-region
  11691. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11692. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11693. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11694. (repeater (and old-date
  11695. (string-match
  11696. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11697. old-date)
  11698. (match-string 1 old-date))))
  11699. (cond
  11700. ((equal arg '(4))
  11701. (when (and old-date org-log-redeadline)
  11702. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11703. org-log-redeadline))
  11704. (org-remove-timestamp-with-keyword org-deadline-string)
  11705. (message "Item no longer has a deadline."))
  11706. ((equal arg '(16))
  11707. (save-excursion
  11708. (if (re-search-forward
  11709. org-deadline-time-regexp
  11710. (save-excursion (outline-next-heading) (point)) t)
  11711. (let* ((rpl0 (match-string 1))
  11712. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11713. (replace-match
  11714. (concat org-deadline-string
  11715. " <" rpl
  11716. (format " -%dd"
  11717. (abs
  11718. (- (time-to-days
  11719. (save-match-data
  11720. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11721. (time-to-days old-date-time))))
  11722. ">") t t))
  11723. (user-error "No deadline information to update"))))
  11724. (t
  11725. (org-add-planning-info 'deadline time 'closed)
  11726. (when (and old-date org-log-redeadline
  11727. (not (equal old-date
  11728. (substring org-last-inserted-timestamp 1 -1))))
  11729. (org-add-log-setup 'redeadline nil old-date 'findpos
  11730. org-log-redeadline))
  11731. (when repeater
  11732. (save-excursion
  11733. (org-back-to-heading t)
  11734. (when (re-search-forward (concat org-deadline-string " "
  11735. org-last-inserted-timestamp)
  11736. (save-excursion
  11737. (outline-next-heading) (point)) t)
  11738. (goto-char (1- (match-end 0)))
  11739. (insert " " repeater)
  11740. (setq org-last-inserted-timestamp
  11741. (concat (substring org-last-inserted-timestamp 0 -1)
  11742. " " repeater
  11743. (substring org-last-inserted-timestamp -1))))))
  11744. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11745. (defun org-schedule (arg &optional time)
  11746. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11747. With one universal prefix argument, remove any scheduling date from the item.
  11748. With two universal prefix arguments, prompt for a delay cookie.
  11749. With argument TIME, scheduled at the corresponding date. TIME can
  11750. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11751. (interactive "P")
  11752. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11753. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11754. 'region-start-level 'region))
  11755. org-loop-over-headlines-in-active-region)
  11756. (org-map-entries
  11757. `(org-schedule ',arg ,time)
  11758. org-loop-over-headlines-in-active-region
  11759. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11760. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11761. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11762. (repeater (and old-date
  11763. (string-match
  11764. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11765. old-date)
  11766. (match-string 1 old-date))))
  11767. (cond
  11768. ((equal arg '(4))
  11769. (progn
  11770. (when (and old-date org-log-reschedule)
  11771. (org-add-log-setup 'delschedule nil old-date 'findpos
  11772. org-log-reschedule))
  11773. (org-remove-timestamp-with-keyword org-scheduled-string)
  11774. (message "Item is no longer scheduled.")))
  11775. ((equal arg '(16))
  11776. (save-excursion
  11777. (if (re-search-forward
  11778. org-scheduled-time-regexp
  11779. (save-excursion (outline-next-heading) (point)) t)
  11780. (let* ((rpl0 (match-string 1))
  11781. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11782. (replace-match
  11783. (concat org-scheduled-string
  11784. " <" rpl
  11785. (format " -%dd"
  11786. (abs
  11787. (- (time-to-days
  11788. (save-match-data
  11789. (org-read-date nil t nil "Delay until" old-date-time)))
  11790. (time-to-days old-date-time))))
  11791. ">") t t))
  11792. (user-error "No scheduled information to update"))))
  11793. (t
  11794. (org-add-planning-info 'scheduled time 'closed)
  11795. (when (and old-date org-log-reschedule
  11796. (not (equal old-date
  11797. (substring org-last-inserted-timestamp 1 -1))))
  11798. (org-add-log-setup 'reschedule nil old-date 'findpos
  11799. org-log-reschedule))
  11800. (when repeater
  11801. (save-excursion
  11802. (org-back-to-heading t)
  11803. (when (re-search-forward (concat org-scheduled-string " "
  11804. org-last-inserted-timestamp)
  11805. (save-excursion
  11806. (outline-next-heading) (point)) t)
  11807. (goto-char (1- (match-end 0)))
  11808. (insert " " repeater)
  11809. (setq org-last-inserted-timestamp
  11810. (concat (substring org-last-inserted-timestamp 0 -1)
  11811. " " repeater
  11812. (substring org-last-inserted-timestamp -1))))))
  11813. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11814. (defun org-get-scheduled-time (pom &optional inherit)
  11815. "Get the scheduled time as a time tuple, of a format suitable
  11816. for calling org-schedule with, or if there is no scheduling,
  11817. returns nil."
  11818. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11819. (when time
  11820. (apply 'encode-time (org-parse-time-string time)))))
  11821. (defun org-get-deadline-time (pom &optional inherit)
  11822. "Get the deadline as a time tuple, of a format suitable for
  11823. calling org-deadline with, or if there is no scheduling, returns
  11824. nil."
  11825. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11826. (when time
  11827. (apply 'encode-time (org-parse-time-string time)))))
  11828. (defun org-remove-timestamp-with-keyword (keyword)
  11829. "Remove all time stamps with KEYWORD in the current entry."
  11830. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11831. beg)
  11832. (save-excursion
  11833. (org-back-to-heading t)
  11834. (setq beg (point))
  11835. (outline-next-heading)
  11836. (while (re-search-backward re beg t)
  11837. (replace-match "")
  11838. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11839. (equal (char-before) ?\ ))
  11840. (backward-delete-char 1)
  11841. (if (string-match "^[ \t]*$" (buffer-substring
  11842. (point-at-bol) (point-at-eol)))
  11843. (delete-region (point-at-bol)
  11844. (min (point-max) (1+ (point-at-eol))))))))))
  11845. (defun org-add-planning-info (what &optional time &rest remove)
  11846. "Insert new timestamp with keyword in the line directly after the headline.
  11847. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11848. If non is given, the user is prompted for a date.
  11849. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11850. be removed."
  11851. (interactive)
  11852. (let (org-time-was-given org-end-time-was-given ts
  11853. end default-time default-input)
  11854. (catch 'exit
  11855. (when (and (memq what '(scheduled deadline))
  11856. (or (not time)
  11857. (and (stringp time)
  11858. (string-match "^[-+]+[0-9]" time))))
  11859. ;; Try to get a default date/time from existing timestamp
  11860. (save-excursion
  11861. (org-back-to-heading t)
  11862. (setq end (save-excursion (outline-next-heading) (point)))
  11863. (when (re-search-forward (if (eq what 'scheduled)
  11864. org-scheduled-time-regexp
  11865. org-deadline-time-regexp)
  11866. end t)
  11867. (setq ts (match-string 1)
  11868. default-time
  11869. (apply 'encode-time (org-parse-time-string ts))
  11870. default-input (and ts (org-get-compact-tod ts))))))
  11871. (when what
  11872. (setq time
  11873. (if (stringp time)
  11874. ;; This is a string (relative or absolute), set proper date
  11875. (apply 'encode-time
  11876. (org-read-date-analyze
  11877. time default-time (decode-time default-time)))
  11878. ;; If necessary, get the time from the user
  11879. (or time (org-read-date nil 'to-time nil nil
  11880. default-time default-input)))))
  11881. (when (and org-insert-labeled-timestamps-at-point
  11882. (member what '(scheduled deadline)))
  11883. (insert
  11884. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11885. (org-insert-time-stamp time org-time-was-given
  11886. nil nil nil (list org-end-time-was-given))
  11887. (setq what nil))
  11888. (save-excursion
  11889. (save-restriction
  11890. (let (col list elt ts buffer-invisibility-spec)
  11891. (org-back-to-heading t)
  11892. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11893. (goto-char (match-end 1))
  11894. (setq col (current-column))
  11895. (goto-char (match-end 0))
  11896. (if (eobp) (insert "\n") (forward-char 1))
  11897. (when (and (not what)
  11898. (not (looking-at
  11899. (concat "[ \t]*"
  11900. org-keyword-time-not-clock-regexp))))
  11901. ;; Nothing to add, nothing to remove...... :-)
  11902. (throw 'exit nil))
  11903. (if (and (not (looking-at org-outline-regexp))
  11904. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11905. "[^\r\n]*"))
  11906. (not (equal (match-string 1) org-clock-string)))
  11907. (narrow-to-region (match-beginning 0) (match-end 0))
  11908. (insert-before-markers "\n")
  11909. (backward-char 1)
  11910. (narrow-to-region (point) (point))
  11911. (and org-adapt-indentation (org-indent-to-column col)))
  11912. ;; Check if we have to remove something.
  11913. (setq list (cons what remove))
  11914. (while list
  11915. (setq elt (pop list))
  11916. (when (or (and (eq elt 'scheduled)
  11917. (re-search-forward org-scheduled-time-regexp nil t))
  11918. (and (eq elt 'deadline)
  11919. (re-search-forward org-deadline-time-regexp nil t))
  11920. (and (eq elt 'closed)
  11921. (re-search-forward org-closed-time-regexp nil t)))
  11922. (replace-match "")
  11923. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11924. (and (looking-at "[ \t]+") (replace-match ""))
  11925. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11926. (when what
  11927. (insert
  11928. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11929. (cond ((eq what 'scheduled) org-scheduled-string)
  11930. ((eq what 'deadline) org-deadline-string)
  11931. ((eq what 'closed) org-closed-string))
  11932. " ")
  11933. (setq ts (org-insert-time-stamp
  11934. time
  11935. (or org-time-was-given
  11936. (and (eq what 'closed) org-log-done-with-time))
  11937. (eq what 'closed)
  11938. nil nil (list org-end-time-was-given)))
  11939. (insert
  11940. (if (not (or (bolp) (eq (char-before) ?\ )
  11941. (memq (char-after) '(32 10))
  11942. (eobp))) " " ""))
  11943. (end-of-line 1))
  11944. (goto-char (point-min))
  11945. (widen)
  11946. (if (and (looking-at "[ \t]*\n")
  11947. (equal (char-before) ?\n))
  11948. (delete-region (1- (point)) (point-at-eol)))
  11949. ts))))))
  11950. (defvar org-log-note-marker (make-marker))
  11951. (defvar org-log-note-purpose nil)
  11952. (defvar org-log-note-state nil)
  11953. (defvar org-log-note-previous-state nil)
  11954. (defvar org-log-note-how nil)
  11955. (defvar org-log-note-extra nil)
  11956. (defvar org-log-note-window-configuration nil)
  11957. (defvar org-log-note-return-to (make-marker))
  11958. (defvar org-log-note-effective-time nil
  11959. "Remembered current time so that dynamically scoped
  11960. `org-extend-today-until' affects tha timestamps in state change
  11961. log")
  11962. (defvar org-log-post-message nil
  11963. "Message to be displayed after a log note has been stored.
  11964. The auto-repeater uses this.")
  11965. (defun org-add-note ()
  11966. "Add a note to the current entry.
  11967. This is done in the same way as adding a state change note."
  11968. (interactive)
  11969. (org-add-log-setup 'note nil nil 'findpos nil))
  11970. (defvar org-property-end-re)
  11971. (defun org-add-log-setup (&optional purpose state prev-state
  11972. findpos how extra)
  11973. "Set up the post command hook to take a note.
  11974. If this is about to TODO state change, the new state is expected in STATE.
  11975. When FINDPOS is non-nil, find the correct position for the note in
  11976. the current entry. If not, assume that it can be inserted at point.
  11977. HOW is an indicator what kind of note should be created.
  11978. EXTRA is additional text that will be inserted into the notes buffer."
  11979. (let* ((org-log-into-drawer (org-log-into-drawer))
  11980. (drawer (cond ((stringp org-log-into-drawer)
  11981. org-log-into-drawer)
  11982. (org-log-into-drawer "LOGBOOK"))))
  11983. (save-restriction
  11984. (save-excursion
  11985. (when findpos
  11986. (org-back-to-heading t)
  11987. (narrow-to-region (point) (save-excursion
  11988. (outline-next-heading) (point)))
  11989. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11990. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11991. "[^\r\n]*\\)?"))
  11992. (goto-char (match-end 0))
  11993. (cond
  11994. (drawer
  11995. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11996. nil t)
  11997. (progn
  11998. (goto-char (match-end 0))
  11999. (or org-log-states-order-reversed
  12000. (and (re-search-forward org-property-end-re nil t)
  12001. (goto-char (1- (match-beginning 0))))))
  12002. (insert "\n:" drawer ":\n:END:")
  12003. (beginning-of-line 0)
  12004. (org-indent-line)
  12005. (beginning-of-line 2)
  12006. (org-indent-line)
  12007. (end-of-line 0)))
  12008. ((and org-log-state-notes-insert-after-drawers
  12009. (save-excursion
  12010. (forward-line) (looking-at org-drawer-regexp)))
  12011. (forward-line)
  12012. (while (looking-at org-drawer-regexp)
  12013. (goto-char (match-end 0))
  12014. (re-search-forward org-property-end-re (point-max) t)
  12015. (forward-line))
  12016. (forward-line -1)))
  12017. (unless org-log-states-order-reversed
  12018. (and (= (char-after) ?\n) (forward-char 1))
  12019. (org-skip-over-state-notes)
  12020. (skip-chars-backward " \t\n\r")))
  12021. (move-marker org-log-note-marker (point))
  12022. (setq org-log-note-purpose purpose
  12023. org-log-note-state state
  12024. org-log-note-previous-state prev-state
  12025. org-log-note-how how
  12026. org-log-note-extra extra
  12027. org-log-note-effective-time (org-current-effective-time))
  12028. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12029. (defun org-skip-over-state-notes ()
  12030. "Skip past the list of State notes in an entry."
  12031. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12032. (when (ignore-errors (goto-char (org-in-item-p)))
  12033. (let* ((struct (org-list-struct))
  12034. (prevs (org-list-prevs-alist struct)))
  12035. (while (looking-at "[ \t]*- State")
  12036. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12037. (org-list-get-item-end (point) struct)))))))
  12038. (defun org-add-log-note (&optional purpose)
  12039. "Pop up a window for taking a note, and add this note later at point."
  12040. (remove-hook 'post-command-hook 'org-add-log-note)
  12041. (setq org-log-note-window-configuration (current-window-configuration))
  12042. (delete-other-windows)
  12043. (move-marker org-log-note-return-to (point))
  12044. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12045. (goto-char org-log-note-marker)
  12046. (org-switch-to-buffer-other-window "*Org Note*")
  12047. (erase-buffer)
  12048. (if (memq org-log-note-how '(time state))
  12049. (let (current-prefix-arg) (org-store-log-note))
  12050. (let ((org-inhibit-startup t)) (org-mode))
  12051. (insert (format "# Insert note for %s.
  12052. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12053. (cond
  12054. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12055. ((eq org-log-note-purpose 'done) "closed todo item")
  12056. ((eq org-log-note-purpose 'state)
  12057. (format "state change from \"%s\" to \"%s\""
  12058. (or org-log-note-previous-state "")
  12059. (or org-log-note-state "")))
  12060. ((eq org-log-note-purpose 'reschedule)
  12061. "rescheduling")
  12062. ((eq org-log-note-purpose 'delschedule)
  12063. "no longer scheduled")
  12064. ((eq org-log-note-purpose 'redeadline)
  12065. "changing deadline")
  12066. ((eq org-log-note-purpose 'deldeadline)
  12067. "removing deadline")
  12068. ((eq org-log-note-purpose 'refile)
  12069. "refiling")
  12070. ((eq org-log-note-purpose 'note)
  12071. "this entry")
  12072. (t (error "This should not happen")))))
  12073. (if org-log-note-extra (insert org-log-note-extra))
  12074. (org-set-local 'org-finish-function 'org-store-log-note)
  12075. (run-hooks 'org-log-buffer-setup-hook)))
  12076. (defvar org-note-abort nil) ; dynamically scoped
  12077. (defun org-store-log-note ()
  12078. "Finish taking a log note, and insert it to where it belongs."
  12079. (let ((txt (buffer-string)))
  12080. (kill-buffer (current-buffer))
  12081. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12082. lines ind bul)
  12083. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12084. (setq txt (replace-match "" t t txt)))
  12085. (if (string-match "\\s-+\\'" txt)
  12086. (setq txt (replace-match "" t t txt)))
  12087. (setq lines (org-split-string txt "\n"))
  12088. (when (and note (string-match "\\S-" note))
  12089. (setq note
  12090. (org-replace-escapes
  12091. note
  12092. (list (cons "%u" (user-login-name))
  12093. (cons "%U" user-full-name)
  12094. (cons "%t" (format-time-string
  12095. (org-time-stamp-format 'long 'inactive)
  12096. org-log-note-effective-time))
  12097. (cons "%T" (format-time-string
  12098. (org-time-stamp-format 'long nil)
  12099. org-log-note-effective-time))
  12100. (cons "%d" (format-time-string
  12101. (org-time-stamp-format nil 'inactive)
  12102. org-log-note-effective-time))
  12103. (cons "%D" (format-time-string
  12104. (org-time-stamp-format nil nil)
  12105. org-log-note-effective-time))
  12106. (cons "%s" (if org-log-note-state
  12107. (concat "\"" org-log-note-state "\"")
  12108. ""))
  12109. (cons "%S" (if org-log-note-previous-state
  12110. (concat "\"" org-log-note-previous-state "\"")
  12111. "\"\"")))))
  12112. (if lines (setq note (concat note " \\\\")))
  12113. (push note lines))
  12114. (when (or current-prefix-arg org-note-abort)
  12115. (when org-log-into-drawer
  12116. (org-remove-empty-drawer-at
  12117. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12118. org-log-note-marker))
  12119. (setq lines nil))
  12120. (when lines
  12121. (with-current-buffer (marker-buffer org-log-note-marker)
  12122. (save-excursion
  12123. (goto-char org-log-note-marker)
  12124. (move-marker org-log-note-marker nil)
  12125. (end-of-line 1)
  12126. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12127. (setq ind (save-excursion
  12128. (if (ignore-errors (goto-char (org-in-item-p)))
  12129. (let ((struct (org-list-struct)))
  12130. (org-list-get-ind
  12131. (org-list-get-top-point struct) struct))
  12132. (skip-chars-backward " \r\t\n")
  12133. (cond
  12134. ((and (org-at-heading-p)
  12135. org-adapt-indentation)
  12136. (1+ (org-current-level)))
  12137. ((org-at-heading-p) 0)
  12138. (t (org-get-indentation))))))
  12139. (setq bul (org-list-bullet-string "-"))
  12140. (org-indent-line-to ind)
  12141. (insert bul (pop lines))
  12142. (let ((ind-body (+ (length bul) ind)))
  12143. (while lines
  12144. (insert "\n")
  12145. (org-indent-line-to ind-body)
  12146. (insert (pop lines))))
  12147. (message "Note stored")
  12148. (org-back-to-heading t)
  12149. (org-cycle-hide-drawers 'children))
  12150. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12151. ;; from within `org-add-log-note' because `buffer-undo-list'
  12152. ;; is then modified outside of `org-with-remote-undo'.
  12153. (when (eq this-command 'org-agenda-todo)
  12154. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12155. ;; Don't add undo information when called from `org-agenda-todo'
  12156. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12157. (set-window-configuration org-log-note-window-configuration)
  12158. (with-current-buffer (marker-buffer org-log-note-return-to)
  12159. (goto-char org-log-note-return-to))
  12160. (move-marker org-log-note-return-to nil)
  12161. (and org-log-post-message (message "%s" org-log-post-message))))
  12162. (defun org-remove-empty-drawer-at (drawer pos)
  12163. "Remove an empty drawer DRAWER at position POS.
  12164. POS may also be a marker."
  12165. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12166. (save-excursion
  12167. (save-restriction
  12168. (widen)
  12169. (goto-char pos)
  12170. (if (org-in-regexp
  12171. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12172. (replace-match ""))))))
  12173. (defvar org-ts-type nil)
  12174. (defun org-sparse-tree (&optional arg type)
  12175. "Create a sparse tree, prompt for the details.
  12176. This command can create sparse trees. You first need to select the type
  12177. of match used to create the tree:
  12178. t Show all TODO entries.
  12179. T Show entries with a specific TODO keyword.
  12180. m Show entries selected by a tags/property match.
  12181. p Enter a property name and its value (both with completion on existing
  12182. names/values) and show entries with that property.
  12183. r Show entries matching a regular expression (`/' can be used as well).
  12184. b Show deadlines and scheduled items before a date.
  12185. a Show deadlines and scheduled items after a date.
  12186. d Show deadlines due within `org-deadline-warning-days'.
  12187. D Show deadlines and scheduled items between a date range."
  12188. (interactive "P")
  12189. (let (ans kwd value ts-type)
  12190. (setq type (or type org-sparse-tree-default-date-type))
  12191. (setq org-ts-type type)
  12192. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range\n [c]ycle through date types: %s"
  12193. (cond ((eq type 'all) "all timestamps")
  12194. ((eq type 'scheduled) "only scheduled")
  12195. ((eq type 'deadline) "only deadline")
  12196. ((eq type 'active) "only active timestamps")
  12197. ((eq type 'inactive) "only inactive timestamps")
  12198. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12199. ((eq type 'closed) "with a closed time-stamp")
  12200. (t "scheduled/deadline")))
  12201. (setq ans (read-char-exclusive))
  12202. (cond
  12203. ((equal ans ?c)
  12204. (org-sparse-tree
  12205. arg (cadr (member type '(scheduled-or-deadline
  12206. all scheduled deadline active inactive closed)))))
  12207. ((equal ans ?d)
  12208. (call-interactively 'org-check-deadlines))
  12209. ((equal ans ?b)
  12210. (call-interactively 'org-check-before-date))
  12211. ((equal ans ?a)
  12212. (call-interactively 'org-check-after-date))
  12213. ((equal ans ?D)
  12214. (call-interactively 'org-check-dates-range))
  12215. ((equal ans ?t)
  12216. (call-interactively 'org-show-todo-tree))
  12217. ((equal ans ?T)
  12218. (org-show-todo-tree '(4)))
  12219. ((member ans '(?T ?m))
  12220. (call-interactively 'org-match-sparse-tree))
  12221. ((member ans '(?p ?P))
  12222. (setq kwd (org-icompleting-read "Property: "
  12223. (mapcar 'list (org-buffer-property-keys))))
  12224. (setq value (org-icompleting-read "Value: "
  12225. (mapcar 'list (org-property-values kwd))))
  12226. (unless (string-match "\\`{.*}\\'" value)
  12227. (setq value (concat "\"" value "\"")))
  12228. (org-match-sparse-tree arg (concat kwd "=" value)))
  12229. ((member ans '(?r ?R ?/))
  12230. (call-interactively 'org-occur))
  12231. (t (user-error "No such sparse tree command \"%c\"" ans)))))
  12232. (defvar org-occur-highlights nil
  12233. "List of overlays used for occur matches.")
  12234. (make-variable-buffer-local 'org-occur-highlights)
  12235. (defvar org-occur-parameters nil
  12236. "Parameters of the active org-occur calls.
  12237. This is a list, each call to org-occur pushes as cons cell,
  12238. containing the regular expression and the callback, onto the list.
  12239. The list can contain several entries if `org-occur' has been called
  12240. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12241. will only contain one set of parameters. When the highlights are
  12242. removed (for example with `C-c C-c', or with the next edit (depending
  12243. on `org-remove-highlights-with-change'), this variable is emptied
  12244. as well.")
  12245. (make-variable-buffer-local 'org-occur-parameters)
  12246. (defun org-occur (regexp &optional keep-previous callback)
  12247. "Make a compact tree which shows all matches of REGEXP.
  12248. The tree will show the lines where the regexp matches, and all higher
  12249. headlines above the match. It will also show the heading after the match,
  12250. to make sure editing the matching entry is easy.
  12251. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12252. call to `org-occur' will be kept, to allow stacking of calls to this
  12253. command.
  12254. If CALLBACK is non-nil, it is a function which is called to confirm
  12255. that the match should indeed be shown."
  12256. (interactive "sRegexp: \nP")
  12257. (when (equal regexp "")
  12258. (user-error "Regexp cannot be empty"))
  12259. (unless keep-previous
  12260. (org-remove-occur-highlights nil nil t))
  12261. (push (cons regexp callback) org-occur-parameters)
  12262. (let ((cnt 0))
  12263. (save-excursion
  12264. (goto-char (point-min))
  12265. (if (or (not keep-previous) ; do not want to keep
  12266. (not org-occur-highlights)) ; no previous matches
  12267. ;; hide everything
  12268. (org-overview))
  12269. (while (re-search-forward regexp nil t)
  12270. (when (or (not callback)
  12271. (save-match-data (funcall callback)))
  12272. (setq cnt (1+ cnt))
  12273. (when org-highlight-sparse-tree-matches
  12274. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12275. (org-show-context 'occur-tree))))
  12276. (when org-remove-highlights-with-change
  12277. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12278. nil 'local))
  12279. (unless org-sparse-tree-open-archived-trees
  12280. (org-hide-archived-subtrees (point-min) (point-max)))
  12281. (run-hooks 'org-occur-hook)
  12282. (if (org-called-interactively-p 'interactive)
  12283. (message "%d match(es) for regexp %s" cnt regexp))
  12284. cnt))
  12285. (defun org-occur-next-match (&optional n reset)
  12286. "Function for `next-error-function' to find sparse tree matches.
  12287. N is the number of matches to move, when negative move backwards.
  12288. RESET is entirely ignored - this function always goes back to the
  12289. starting point when no match is found."
  12290. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12291. (search-func (if (< n 0)
  12292. 'previous-single-char-property-change
  12293. 'next-single-char-property-change))
  12294. (n (abs n))
  12295. (pos (point))
  12296. p1)
  12297. (catch 'exit
  12298. (while (setq p1 (funcall search-func (point) 'org-type))
  12299. (when (equal p1 limit)
  12300. (goto-char pos)
  12301. (error "No more matches"))
  12302. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12303. (setq n (1- n))
  12304. (when (= n 0)
  12305. (goto-char p1)
  12306. (throw 'exit (point))))
  12307. (goto-char p1))
  12308. (goto-char p1)
  12309. (error "No more matches"))))
  12310. (defun org-show-context (&optional key)
  12311. "Make sure point and context are visible.
  12312. How much context is shown depends upon the variables
  12313. `org-show-hierarchy-above', `org-show-following-heading',
  12314. `org-show-entry-below' and `org-show-siblings'."
  12315. (let ((heading-p (org-at-heading-p t))
  12316. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12317. (following-p (org-get-alist-option org-show-following-heading key))
  12318. (entry-p (org-get-alist-option org-show-entry-below key))
  12319. (siblings-p (org-get-alist-option org-show-siblings key)))
  12320. ;; Show heading or entry text
  12321. (if (and heading-p (not entry-p))
  12322. (org-flag-heading nil) ; only show the heading
  12323. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12324. (org-show-hidden-entry))) ; show entire entry
  12325. (when following-p
  12326. ;; Show next sibling, or heading below text
  12327. (save-excursion
  12328. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12329. (org-flag-heading nil))))
  12330. (when siblings-p (org-show-siblings))
  12331. (when hierarchy-p
  12332. ;; show all higher headings, possibly with siblings
  12333. (save-excursion
  12334. (while (and (condition-case nil
  12335. (progn (org-up-heading-all 1) t)
  12336. (error nil))
  12337. (not (bobp)))
  12338. (org-flag-heading nil)
  12339. (when siblings-p (org-show-siblings)))))
  12340. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12341. (defvar org-reveal-start-hook nil
  12342. "Hook run before revealing a location.")
  12343. (defun org-reveal (&optional siblings)
  12344. "Show current entry, hierarchy above it, and the following headline.
  12345. This can be used to show a consistent set of context around locations
  12346. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12347. not t for the search context.
  12348. With optional argument SIBLINGS, on each level of the hierarchy all
  12349. siblings are shown. This repairs the tree structure to what it would
  12350. look like when opened with hierarchical calls to `org-cycle'.
  12351. With double optional argument \\[universal-argument] \\[universal-argument], \
  12352. go to the parent and show the
  12353. entire tree."
  12354. (interactive "P")
  12355. (run-hooks 'org-reveal-start-hook)
  12356. (let ((org-show-hierarchy-above t)
  12357. (org-show-following-heading t)
  12358. (org-show-siblings (if siblings t org-show-siblings)))
  12359. (org-show-context nil))
  12360. (when (equal siblings '(16))
  12361. (save-excursion
  12362. (when (org-up-heading-safe)
  12363. (org-show-subtree)
  12364. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12365. (defun org-highlight-new-match (beg end)
  12366. "Highlight from BEG to END and mark the highlight is an occur headline."
  12367. (let ((ov (make-overlay beg end)))
  12368. (overlay-put ov 'face 'secondary-selection)
  12369. (overlay-put ov 'org-type 'org-occur)
  12370. (push ov org-occur-highlights)))
  12371. (defun org-remove-occur-highlights (&optional beg end noremove)
  12372. "Remove the occur highlights from the buffer.
  12373. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12374. from the `before-change-functions' in the current buffer."
  12375. (interactive)
  12376. (unless org-inhibit-highlight-removal
  12377. (mapc 'delete-overlay org-occur-highlights)
  12378. (setq org-occur-highlights nil)
  12379. (setq org-occur-parameters nil)
  12380. (unless noremove
  12381. (remove-hook 'before-change-functions
  12382. 'org-remove-occur-highlights 'local))))
  12383. ;;;; Priorities
  12384. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12385. "Regular expression matching the priority indicator.")
  12386. (defvar org-remove-priority-next-time nil)
  12387. (defun org-priority-up ()
  12388. "Increase the priority of the current item."
  12389. (interactive)
  12390. (org-priority 'up))
  12391. (defun org-priority-down ()
  12392. "Decrease the priority of the current item."
  12393. (interactive)
  12394. (org-priority 'down))
  12395. (defun org-priority (&optional action show)
  12396. "Change the priority of an item.
  12397. ACTION can be `set', `up', `down', or a character."
  12398. (interactive "P")
  12399. (if (equal action '(4))
  12400. (org-show-priority)
  12401. (unless org-enable-priority-commands
  12402. (user-error "Priority commands are disabled"))
  12403. (setq action (or action 'set))
  12404. (let (current new news have remove)
  12405. (save-excursion
  12406. (org-back-to-heading t)
  12407. (if (looking-at org-priority-regexp)
  12408. (setq current (string-to-char (match-string 2))
  12409. have t))
  12410. (cond
  12411. ((eq action 'remove)
  12412. (setq remove t new ?\ ))
  12413. ((or (eq action 'set)
  12414. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12415. (if (not (eq action 'set))
  12416. (setq new action)
  12417. (message "Priority %c-%c, SPC to remove: "
  12418. org-highest-priority org-lowest-priority)
  12419. (save-match-data
  12420. (setq new (read-char-exclusive))))
  12421. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12422. (= (upcase org-lowest-priority) org-lowest-priority))
  12423. (setq new (upcase new)))
  12424. (cond ((equal new ?\ ) (setq remove t))
  12425. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12426. (user-error "Priority must be between `%c' and `%c'"
  12427. org-highest-priority org-lowest-priority))))
  12428. ((eq action 'up)
  12429. (setq new (if have
  12430. (1- current) ; normal cycling
  12431. ;; last priority was empty
  12432. (if (eq last-command this-command)
  12433. org-lowest-priority ; wrap around empty to lowest
  12434. ;; default
  12435. (if org-priority-start-cycle-with-default
  12436. org-default-priority
  12437. (1- org-default-priority))))))
  12438. ((eq action 'down)
  12439. (setq new (if have
  12440. (1+ current) ; normal cycling
  12441. ;; last priority was empty
  12442. (if (eq last-command this-command)
  12443. org-highest-priority ; wrap around empty to highest
  12444. ;; default
  12445. (if org-priority-start-cycle-with-default
  12446. org-default-priority
  12447. (1+ org-default-priority))))))
  12448. (t (user-error "Invalid action")))
  12449. (if (or (< (upcase new) org-highest-priority)
  12450. (> (upcase new) org-lowest-priority))
  12451. (if (and (memq action '(up down))
  12452. (not have) (not (eq last-command this-command)))
  12453. ;; `new' is from default priority
  12454. (error
  12455. "The default can not be set, see `org-default-priority' why")
  12456. ;; normal cycling: `new' is beyond highest/lowest priority
  12457. ;; and is wrapped around to the empty priority
  12458. (setq remove t)))
  12459. (setq news (format "%c" new))
  12460. (if have
  12461. (if remove
  12462. (replace-match "" t t nil 1)
  12463. (replace-match news t t nil 2))
  12464. (if remove
  12465. (user-error "No priority cookie found in line")
  12466. (let ((case-fold-search nil))
  12467. (looking-at org-todo-line-regexp))
  12468. (if (match-end 2)
  12469. (progn
  12470. (goto-char (match-end 2))
  12471. (insert " [#" news "]"))
  12472. (goto-char (match-beginning 3))
  12473. (insert "[#" news "] "))))
  12474. (org-preserve-lc (org-set-tags nil 'align)))
  12475. (if remove
  12476. (message "Priority removed")
  12477. (message "Priority of current item set to %s" news)))))
  12478. (defun org-show-priority ()
  12479. "Show the priority of the current item.
  12480. This priority is composed of the main priority given with the [#A] cookies,
  12481. and by additional input from the age of a schedules or deadline entry."
  12482. (interactive)
  12483. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12484. (org-get-at-bol 'priority)
  12485. (save-excursion
  12486. (save-match-data
  12487. (beginning-of-line)
  12488. (and (looking-at org-heading-regexp)
  12489. (org-get-priority (match-string 0))))))))
  12490. (message "Priority is %d" (if pri pri -1000))))
  12491. (defun org-get-priority (s)
  12492. "Find priority cookie and return priority."
  12493. (save-match-data
  12494. (if (functionp org-get-priority-function)
  12495. (funcall org-get-priority-function)
  12496. (if (not (string-match org-priority-regexp s))
  12497. (* 1000 (- org-lowest-priority org-default-priority))
  12498. (* 1000 (- org-lowest-priority
  12499. (string-to-char (match-string 2 s))))))))
  12500. ;;;; Tags
  12501. (defvar org-agenda-archives-mode)
  12502. (defvar org-map-continue-from nil
  12503. "Position from where mapping should continue.
  12504. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12505. (defvar org-scanner-tags nil
  12506. "The current tag list while the tags scanner is running.")
  12507. (defvar org-trust-scanner-tags nil
  12508. "Should `org-get-tags-at' use the tags for the scanner.
  12509. This is for internal dynamical scoping only.
  12510. When this is non-nil, the function `org-get-tags-at' will return the value
  12511. of `org-scanner-tags' instead of building the list by itself. This
  12512. can lead to large speed-ups when the tags scanner is used in a file with
  12513. many entries, and when the list of tags is retrieved, for example to
  12514. obtain a list of properties. Building the tags list for each entry in such
  12515. a file becomes an N^2 operation - but with this variable set, it scales
  12516. as N.")
  12517. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12518. "Sca headline tags with inheritance and produce output ACTION.
  12519. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12520. or `agenda' to produce an entry list for an agenda view. It can also be
  12521. a Lisp form or a function that should be called at each matched headline, in
  12522. this case the return value is a list of all return values from these calls.
  12523. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12524. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12525. only lines with a not-done TODO keyword are included in the output.
  12526. This should be the same variable that was scoped into
  12527. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12528. START-LEVEL can be a string with asterisks, reducing the scope to
  12529. headlines matching this string."
  12530. (require 'org-agenda)
  12531. (let* ((re (concat "^"
  12532. (if start-level
  12533. ;; Get the correct level to match
  12534. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12535. org-outline-regexp)
  12536. " *\\(\\<\\("
  12537. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12538. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12539. (props (list 'face 'default
  12540. 'done-face 'org-agenda-done
  12541. 'undone-face 'default
  12542. 'mouse-face 'highlight
  12543. 'org-not-done-regexp org-not-done-regexp
  12544. 'org-todo-regexp org-todo-regexp
  12545. 'org-complex-heading-regexp org-complex-heading-regexp
  12546. 'help-echo
  12547. (format "mouse-2 or RET jump to org file %s"
  12548. (abbreviate-file-name
  12549. (or (buffer-file-name (buffer-base-buffer))
  12550. (buffer-name (buffer-base-buffer)))))))
  12551. (case-fold-search nil)
  12552. (org-map-continue-from nil)
  12553. lspos tags tags-list
  12554. (tags-alist (list (cons 0 org-file-tags)))
  12555. (llast 0) rtn rtn1 level category i txt
  12556. todo marker entry priority)
  12557. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12558. (setq action (list 'lambda nil action)))
  12559. (save-excursion
  12560. (goto-char (point-min))
  12561. (when (eq action 'sparse-tree)
  12562. (org-overview)
  12563. (org-remove-occur-highlights))
  12564. (while (re-search-forward re nil t)
  12565. (setq org-map-continue-from nil)
  12566. (catch :skip
  12567. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12568. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12569. (goto-char (setq lspos (match-beginning 0)))
  12570. (setq level (org-reduced-level (org-outline-level))
  12571. category (org-get-category))
  12572. (setq i llast llast level)
  12573. ;; remove tag lists from same and sublevels
  12574. (while (>= i level)
  12575. (when (setq entry (assoc i tags-alist))
  12576. (setq tags-alist (delete entry tags-alist)))
  12577. (setq i (1- i)))
  12578. ;; add the next tags
  12579. (when tags
  12580. (setq tags (org-split-string tags ":")
  12581. tags-alist
  12582. (cons (cons level tags) tags-alist)))
  12583. ;; compile tags for current headline
  12584. (setq tags-list
  12585. (if org-use-tag-inheritance
  12586. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12587. tags)
  12588. org-scanner-tags tags-list)
  12589. (when org-use-tag-inheritance
  12590. (setcdr (car tags-alist)
  12591. (mapcar (lambda (x)
  12592. (setq x (copy-sequence x))
  12593. (org-add-prop-inherited x))
  12594. (cdar tags-alist))))
  12595. (when (and tags org-use-tag-inheritance
  12596. (or (not (eq t org-use-tag-inheritance))
  12597. org-tags-exclude-from-inheritance))
  12598. ;; selective inheritance, remove uninherited ones
  12599. (setcdr (car tags-alist)
  12600. (org-remove-uninherited-tags (cdar tags-alist))))
  12601. (when (and
  12602. ;; eval matcher only when the todo condition is OK
  12603. (and (or (not todo-only) (member todo org-not-done-keywords))
  12604. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12605. (eval matcher)))
  12606. ;; Call the skipper, but return t if it does not skip,
  12607. ;; so that the `and' form continues evaluating
  12608. (progn
  12609. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12610. t)
  12611. ;; Check if timestamps are deselecting this entry
  12612. (or (not todo-only)
  12613. (and (member todo org-not-done-keywords)
  12614. (or (not org-agenda-tags-todo-honor-ignore-options)
  12615. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12616. ;; select this headline
  12617. (cond
  12618. ((eq action 'sparse-tree)
  12619. (and org-highlight-sparse-tree-matches
  12620. (org-get-heading) (match-end 0)
  12621. (org-highlight-new-match
  12622. (match-beginning 1) (match-end 1)))
  12623. (org-show-context 'tags-tree))
  12624. ((eq action 'agenda)
  12625. (setq txt (org-agenda-format-item
  12626. ""
  12627. (concat
  12628. (if (eq org-tags-match-list-sublevels 'indented)
  12629. (make-string (1- level) ?.) "")
  12630. (org-get-heading))
  12631. level category
  12632. tags-list)
  12633. priority (org-get-priority txt))
  12634. (goto-char lspos)
  12635. (setq marker (org-agenda-new-marker))
  12636. (org-add-props txt props
  12637. 'org-marker marker 'org-hd-marker marker 'org-category category
  12638. 'todo-state todo
  12639. 'priority priority 'type "tagsmatch")
  12640. (push txt rtn))
  12641. ((functionp action)
  12642. (setq org-map-continue-from nil)
  12643. (save-excursion
  12644. (setq rtn1 (funcall action))
  12645. (push rtn1 rtn)))
  12646. (t (user-error "Invalid action")))
  12647. ;; if we are to skip sublevels, jump to end of subtree
  12648. (unless org-tags-match-list-sublevels
  12649. (org-end-of-subtree t)
  12650. (backward-char 1))))
  12651. ;; Get the correct position from where to continue
  12652. (if org-map-continue-from
  12653. (goto-char org-map-continue-from)
  12654. (and (= (point) lspos) (end-of-line 1)))))
  12655. (when (and (eq action 'sparse-tree)
  12656. (not org-sparse-tree-open-archived-trees))
  12657. (org-hide-archived-subtrees (point-min) (point-max)))
  12658. (nreverse rtn)))
  12659. (defun org-remove-uninherited-tags (tags)
  12660. "Remove all tags that are not inherited from the list TAGS."
  12661. (cond
  12662. ((eq org-use-tag-inheritance t)
  12663. (if org-tags-exclude-from-inheritance
  12664. (org-delete-all org-tags-exclude-from-inheritance tags)
  12665. tags))
  12666. ((not org-use-tag-inheritance) nil)
  12667. ((stringp org-use-tag-inheritance)
  12668. (delq nil (mapcar
  12669. (lambda (x)
  12670. (if (and (string-match org-use-tag-inheritance x)
  12671. (not (member x org-tags-exclude-from-inheritance)))
  12672. x nil))
  12673. tags)))
  12674. ((listp org-use-tag-inheritance)
  12675. (delq nil (mapcar
  12676. (lambda (x)
  12677. (if (member x org-use-tag-inheritance) x nil))
  12678. tags)))))
  12679. (defun org-match-sparse-tree (&optional todo-only match)
  12680. "Create a sparse tree according to tags string MATCH.
  12681. MATCH can contain positive and negative selection of tags, like
  12682. \"+WORK+URGENT-WITHBOSS\".
  12683. If optional argument TODO-ONLY is non-nil, only select lines that are
  12684. also TODO lines."
  12685. (interactive "P")
  12686. (org-agenda-prepare-buffers (list (current-buffer)))
  12687. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12688. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12689. (defvar org-cached-props nil)
  12690. (defun org-cached-entry-get (pom property)
  12691. (if (or (eq t org-use-property-inheritance)
  12692. (and (stringp org-use-property-inheritance)
  12693. (string-match org-use-property-inheritance property))
  12694. (and (listp org-use-property-inheritance)
  12695. (member property org-use-property-inheritance)))
  12696. ;; Caching is not possible, check it directly
  12697. (org-entry-get pom property 'inherit)
  12698. ;; Get all properties, so that we can do complicated checks easily
  12699. (cdr (assoc property (or org-cached-props
  12700. (setq org-cached-props
  12701. (org-entry-properties pom)))))))
  12702. (defun org-global-tags-completion-table (&optional files)
  12703. "Return the list of all tags in all agenda buffer/files.
  12704. Optional FILES argument is a list of files which can be used
  12705. instead of the agenda files."
  12706. (save-excursion
  12707. (org-uniquify
  12708. (delq nil
  12709. (apply 'append
  12710. (mapcar
  12711. (lambda (file)
  12712. (set-buffer (find-file-noselect file))
  12713. (append (org-get-buffer-tags)
  12714. (mapcar (lambda (x) (if (stringp (car-safe x))
  12715. (list (car-safe x)) nil))
  12716. org-tag-alist)))
  12717. (if (and files (car files))
  12718. files
  12719. (org-agenda-files))))))))
  12720. (defun org-make-tags-matcher (match)
  12721. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12722. The variable `todo-only' is scoped dynamically into this function.
  12723. It will be set to t if the matcher restricts matching to TODO entries,
  12724. otherwise will not be touched.
  12725. Returns a cons of the selection string MATCH and the constructed
  12726. lisp form implementing the matcher. The matcher is to be evaluated
  12727. at an Org entry, with point on the headline, and returns t if the
  12728. entry matches the selection string MATCH. The returned lisp form
  12729. references two variables with information about the entry, which
  12730. must be bound around the form's evaluation: todo, the TODO keyword
  12731. at the entry (or nil of none); and tags-list, the list of all tags
  12732. at the entry including inherited ones. Additionally, the category
  12733. of the entry (if any) must be specified as the text property
  12734. 'org-category on the headline.
  12735. See also `org-scan-tags'.
  12736. "
  12737. (declare (special todo-only))
  12738. (unless (boundp 'todo-only)
  12739. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12740. (unless match
  12741. ;; Get a new match request, with completion against the global
  12742. ;; tags table and the local tags in current buffer
  12743. (let ((org-last-tags-completion-table
  12744. (org-uniquify
  12745. (delq nil (append (org-get-buffer-tags)
  12746. (org-global-tags-completion-table))))))
  12747. (setq match (org-completing-read-no-i
  12748. "Match: " 'org-tags-completion-function nil nil nil
  12749. 'org-tags-history))))
  12750. ;; Parse the string and create a lisp form
  12751. (let ((match0 match)
  12752. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12753. minus tag mm
  12754. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12755. orterms term orlist re-p str-p level-p level-op time-p
  12756. prop-p pn pv po gv rest)
  12757. ;; Expand group tags
  12758. (setq match (org-tags-expand match))
  12759. (if (string-match "/+" match)
  12760. ;; match contains also a todo-matching request
  12761. (progn
  12762. (setq tagsmatch (substring match 0 (match-beginning 0))
  12763. todomatch (substring match (match-end 0)))
  12764. (if (string-match "^!" todomatch)
  12765. (setq todo-only t todomatch (substring todomatch 1)))
  12766. (if (string-match "^\\s-*$" todomatch)
  12767. (setq todomatch nil)))
  12768. ;; only matching tags
  12769. (setq tagsmatch match todomatch nil))
  12770. ;; Make the tags matcher
  12771. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12772. (setq tagsmatcher t)
  12773. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12774. (while (setq term (pop orterms))
  12775. (while (and (equal (substring term -1) "\\") orterms)
  12776. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12777. (while (string-match re term)
  12778. (setq rest (substring term (match-end 0))
  12779. minus (and (match-end 1)
  12780. (equal (match-string 1 term) "-"))
  12781. tag (save-match-data (replace-regexp-in-string
  12782. "\\\\-" "-"
  12783. (match-string 2 term)))
  12784. re-p (equal (string-to-char tag) ?{)
  12785. level-p (match-end 4)
  12786. prop-p (match-end 5)
  12787. mm (cond
  12788. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12789. (level-p
  12790. (setq level-op (org-op-to-function (match-string 3 term)))
  12791. `(,level-op level ,(string-to-number
  12792. (match-string 4 term))))
  12793. (prop-p
  12794. (setq pn (match-string 5 term)
  12795. po (match-string 6 term)
  12796. pv (match-string 7 term)
  12797. re-p (equal (string-to-char pv) ?{)
  12798. str-p (equal (string-to-char pv) ?\")
  12799. time-p (save-match-data
  12800. (string-match "^\"[[<].*[]>]\"$" pv))
  12801. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12802. (if time-p (setq pv (org-matcher-time pv)))
  12803. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12804. (cond
  12805. ((equal pn "CATEGORY")
  12806. (setq gv '(get-text-property (point) 'org-category)))
  12807. ((equal pn "TODO")
  12808. (setq gv 'todo))
  12809. (t
  12810. (setq gv `(org-cached-entry-get nil ,pn))))
  12811. (if re-p
  12812. (if (eq po 'org<>)
  12813. `(not (string-match ,pv (or ,gv "")))
  12814. `(string-match ,pv (or ,gv "")))
  12815. (if str-p
  12816. `(,po (or ,gv "") ,pv)
  12817. `(,po (string-to-number (or ,gv ""))
  12818. ,(string-to-number pv) ))))
  12819. (t `(member ,tag tags-list)))
  12820. mm (if minus (list 'not mm) mm)
  12821. term rest)
  12822. (push mm tagsmatcher))
  12823. (push (if (> (length tagsmatcher) 1)
  12824. (cons 'and tagsmatcher)
  12825. (car tagsmatcher))
  12826. orlist)
  12827. (setq tagsmatcher nil))
  12828. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12829. (setq tagsmatcher
  12830. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12831. ;; Make the todo matcher
  12832. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12833. (setq todomatcher t)
  12834. (setq orterms (org-split-string todomatch "|") orlist nil)
  12835. (while (setq term (pop orterms))
  12836. (while (string-match re term)
  12837. (setq minus (and (match-end 1)
  12838. (equal (match-string 1 term) "-"))
  12839. kwd (match-string 2 term)
  12840. re-p (equal (string-to-char kwd) ?{)
  12841. term (substring term (match-end 0))
  12842. mm (if re-p
  12843. `(string-match ,(substring kwd 1 -1) todo)
  12844. (list 'equal 'todo kwd))
  12845. mm (if minus (list 'not mm) mm))
  12846. (push mm todomatcher))
  12847. (push (if (> (length todomatcher) 1)
  12848. (cons 'and todomatcher)
  12849. (car todomatcher))
  12850. orlist)
  12851. (setq todomatcher nil))
  12852. (setq todomatcher (if (> (length orlist) 1)
  12853. (cons 'or orlist) (car orlist))))
  12854. ;; Return the string and lisp forms of the matcher
  12855. (setq matcher (if todomatcher
  12856. (list 'and tagsmatcher todomatcher)
  12857. tagsmatcher))
  12858. (when todo-only
  12859. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12860. matcher)))
  12861. (cons match0 matcher)))
  12862. (defun org-tags-expand (match &optional single-as-list downcased)
  12863. "Expand group tags in MATCH.
  12864. This replaces every group tag in MATCH with a regexp tag search.
  12865. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12866. will be replaced like this:
  12867. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12868. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12869. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12870. Replacing by a regexp preserves the structure of the match.
  12871. E.g., this expansion
  12872. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12873. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12874. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12875. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12876. assumed to be a single group tag, and the function will return
  12877. the list of tags in this group.
  12878. When DOWNCASE is non-nil, expand downcased TAGS."
  12879. (if org-group-tags
  12880. (let* ((case-fold-search t)
  12881. (stable org-mode-syntax-table)
  12882. (tal (or org-tag-groups-alist-for-agenda
  12883. org-tag-groups-alist))
  12884. (tal (if downcased
  12885. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12886. (tml (mapcar 'car tal))
  12887. (rtnmatch match) rpl)
  12888. ;; @ and _ are allowed as word-components in tags
  12889. (modify-syntax-entry ?@ "w" stable)
  12890. (modify-syntax-entry ?_ "w" stable)
  12891. (while (and tml
  12892. (string-match
  12893. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12894. (regexp-opt tml) "\\>\\)") rtnmatch))
  12895. (let* ((dir (match-string 1 rtnmatch))
  12896. (tag (match-string 2 rtnmatch))
  12897. (tag (if downcased (downcase tag) tag)))
  12898. (setq tml (delete tag tml))
  12899. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12900. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12901. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12902. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12903. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12904. (if single-as-list
  12905. (or (reverse rpl) (list rtnmatch))
  12906. rtnmatch))
  12907. (if single-as-list (list (if downcased (downcase match) match))
  12908. match)))
  12909. (defun org-op-to-function (op &optional stringp)
  12910. "Turn an operator into the appropriate function."
  12911. (setq op
  12912. (cond
  12913. ((equal op "<" ) '(< string< org-time<))
  12914. ((equal op ">" ) '(> org-string> org-time>))
  12915. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12916. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12917. ((member op '("=" "==")) '(= string= org-time=))
  12918. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12919. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12920. (defun org<> (a b) (not (= a b)))
  12921. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12922. (defun org-string>= (a b) (not (string< a b)))
  12923. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12924. (defun org-string<> (a b) (not (string= a b)))
  12925. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12926. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12927. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12928. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12929. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12930. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12931. (defun org-2ft (s)
  12932. "Convert S to a floating point time.
  12933. If S is already a number, just return it. If it is a string, parse
  12934. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12935. (cond
  12936. ((numberp s) s)
  12937. ((stringp s)
  12938. (condition-case nil
  12939. (float-time (apply 'encode-time (org-parse-time-string s)))
  12940. (error 0.)))
  12941. (t 0.)))
  12942. (defun org-time-today ()
  12943. "Time in seconds today at 0:00.
  12944. Returns the float number of seconds since the beginning of the
  12945. epoch to the beginning of today (00:00)."
  12946. (float-time (apply 'encode-time
  12947. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12948. (defun org-matcher-time (s)
  12949. "Interpret a time comparison value."
  12950. (save-match-data
  12951. (cond
  12952. ((string= s "<now>") (float-time))
  12953. ((string= s "<today>") (org-time-today))
  12954. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12955. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12956. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12957. (+ (org-time-today)
  12958. (* (string-to-number (match-string 1 s))
  12959. (cdr (assoc (match-string 2 s)
  12960. '(("d" . 86400.0) ("w" . 604800.0)
  12961. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12962. (t (org-2ft s)))))
  12963. (defun org-match-any-p (re list)
  12964. "Does re match any element of list?"
  12965. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12966. (delq nil list))
  12967. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12968. (defvar org-tags-overlay (make-overlay 1 1))
  12969. (org-detach-overlay org-tags-overlay)
  12970. (defun org-get-local-tags-at (&optional pos)
  12971. "Get a list of tags defined in the current headline."
  12972. (org-get-tags-at pos 'local))
  12973. (defun org-get-local-tags ()
  12974. "Get a list of tags defined in the current headline."
  12975. (org-get-tags-at nil 'local))
  12976. (defun org-get-tags-at (&optional pos local)
  12977. "Get a list of all headline tags applicable at POS.
  12978. POS defaults to point. If tags are inherited, the list contains
  12979. the targets in the same sequence as the headlines appear, i.e.
  12980. the tags of the current headline come last.
  12981. When LOCAL is non-nil, only return tags from the current headline,
  12982. ignore inherited ones."
  12983. (interactive)
  12984. (if (and org-trust-scanner-tags
  12985. (or (not pos) (equal pos (point)))
  12986. (not local))
  12987. org-scanner-tags
  12988. (let (tags ltags lastpos parent)
  12989. (save-excursion
  12990. (save-restriction
  12991. (widen)
  12992. (goto-char (or pos (point)))
  12993. (save-match-data
  12994. (catch 'done
  12995. (condition-case nil
  12996. (progn
  12997. (org-back-to-heading t)
  12998. (while (not (equal lastpos (point)))
  12999. (setq lastpos (point))
  13000. (when (looking-at
  13001. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13002. (setq ltags (org-split-string
  13003. (org-match-string-no-properties 1) ":"))
  13004. (when parent
  13005. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13006. (setq tags (append
  13007. (if parent
  13008. (org-remove-uninherited-tags ltags)
  13009. ltags)
  13010. tags)))
  13011. (or org-use-tag-inheritance (throw 'done t))
  13012. (if local (throw 'done t))
  13013. (or (org-up-heading-safe) (error nil))
  13014. (setq parent t)))
  13015. (error nil)))))
  13016. (if local
  13017. tags
  13018. (reverse (delete-dups
  13019. (reverse (append
  13020. (org-remove-uninherited-tags
  13021. org-file-tags) tags)))))))))
  13022. (defun org-add-prop-inherited (s)
  13023. (add-text-properties 0 (length s) '(inherited t) s)
  13024. s)
  13025. (defun org-toggle-tag (tag &optional onoff)
  13026. "Toggle the tag TAG for the current line.
  13027. If ONOFF is `on' or `off', don't toggle but set to this state."
  13028. (let (res current)
  13029. (save-excursion
  13030. (org-back-to-heading t)
  13031. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13032. (point-at-eol) t)
  13033. (progn
  13034. (setq current (match-string 1))
  13035. (replace-match ""))
  13036. (setq current ""))
  13037. (setq current (nreverse (org-split-string current ":")))
  13038. (cond
  13039. ((eq onoff 'on)
  13040. (setq res t)
  13041. (or (member tag current) (push tag current)))
  13042. ((eq onoff 'off)
  13043. (or (not (member tag current)) (setq current (delete tag current))))
  13044. (t (if (member tag current)
  13045. (setq current (delete tag current))
  13046. (setq res t)
  13047. (push tag current))))
  13048. (end-of-line 1)
  13049. (if current
  13050. (progn
  13051. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13052. (org-set-tags nil t))
  13053. (delete-horizontal-space))
  13054. (run-hooks 'org-after-tags-change-hook))
  13055. res))
  13056. (defun org-align-tags-here (to-col)
  13057. ;; Assumes that this is a headline
  13058. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13059. (beginning-of-line 1)
  13060. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13061. (< pos (match-beginning 2)))
  13062. (progn
  13063. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13064. (goto-char (match-beginning 1))
  13065. (insert " ")
  13066. (delete-region (point) (1+ (match-beginning 2)))
  13067. (setq ncol (max (current-column)
  13068. (1+ col)
  13069. (if (> to-col 0)
  13070. to-col
  13071. (- (abs to-col) tags-l))))
  13072. (setq p (point))
  13073. (insert (make-string (- ncol (current-column)) ?\ ))
  13074. (setq ncol (current-column))
  13075. (when indent-tabs-mode (tabify p (point-at-eol)))
  13076. (org-move-to-column (min ncol col) t))
  13077. (goto-char pos))))
  13078. (defun org-set-tags-command (&optional arg just-align)
  13079. "Call the set-tags command for the current entry."
  13080. (interactive "P")
  13081. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13082. (org-set-tags arg just-align)
  13083. (save-excursion
  13084. (unless (and (org-region-active-p)
  13085. org-loop-over-headlines-in-active-region)
  13086. (org-back-to-heading t))
  13087. (org-set-tags arg just-align))))
  13088. (defun org-set-tags-to (data)
  13089. "Set the tags of the current entry to DATA, replacing the current tags.
  13090. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13091. If DATA is nil or the empty string, any tags will be removed."
  13092. (interactive "sTags: ")
  13093. (setq data
  13094. (cond
  13095. ((eq data nil) "")
  13096. ((equal data "") "")
  13097. ((stringp data)
  13098. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13099. ":"))
  13100. ((listp data)
  13101. (concat ":" (mapconcat 'identity data ":") ":"))))
  13102. (when data
  13103. (save-excursion
  13104. (org-back-to-heading t)
  13105. (when (looking-at org-complex-heading-regexp)
  13106. (if (match-end 5)
  13107. (progn
  13108. (goto-char (match-beginning 5))
  13109. (insert data)
  13110. (delete-region (point) (point-at-eol))
  13111. (org-set-tags nil 'align))
  13112. (goto-char (point-at-eol))
  13113. (insert " " data)
  13114. (org-set-tags nil 'align)))
  13115. (beginning-of-line 1)
  13116. (if (looking-at ".*?\\([ \t]+\\)$")
  13117. (delete-region (match-beginning 1) (match-end 1))))))
  13118. (defun org-align-all-tags ()
  13119. "Align the tags i all headings."
  13120. (interactive)
  13121. (save-excursion
  13122. (or (ignore-errors (org-back-to-heading t))
  13123. (outline-next-heading))
  13124. (if (org-at-heading-p)
  13125. (org-set-tags t)
  13126. (message "No headings"))))
  13127. (defvar org-indent-indentation-per-level)
  13128. (defun org-set-tags (&optional arg just-align)
  13129. "Set the tags for the current headline.
  13130. With prefix ARG, realign all tags in headings in the current buffer."
  13131. (interactive "P")
  13132. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13133. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13134. 'region-start-level 'region))
  13135. org-loop-over-headlines-in-active-region)
  13136. (org-map-entries
  13137. ;; We don't use ARG and JUST-ALIGN here these args are not
  13138. ;; useful when looping over headlines
  13139. `(org-set-tags)
  13140. org-loop-over-headlines-in-active-region
  13141. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13142. (let* ((re org-outline-regexp-bol)
  13143. (current (unless arg (org-get-tags-string)))
  13144. (col (current-column))
  13145. (org-setting-tags t)
  13146. table current-tags inherited-tags ; computed below when needed
  13147. tags p0 c0 c1 rpl di tc level)
  13148. (if arg
  13149. (save-excursion
  13150. (goto-char (point-min))
  13151. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13152. (while (re-search-forward re nil t)
  13153. (org-set-tags nil t)
  13154. (end-of-line 1)))
  13155. (message "All tags realigned to column %d" org-tags-column))
  13156. (if just-align
  13157. (setq tags current)
  13158. ;; Get a new set of tags from the user
  13159. (save-excursion
  13160. (setq table (append org-tag-persistent-alist
  13161. (or org-tag-alist (org-get-buffer-tags))
  13162. (and
  13163. org-complete-tags-always-offer-all-agenda-tags
  13164. (org-global-tags-completion-table
  13165. (org-agenda-files))))
  13166. org-last-tags-completion-table table
  13167. current-tags (org-split-string current ":")
  13168. inherited-tags (nreverse
  13169. (nthcdr (length current-tags)
  13170. (nreverse (org-get-tags-at))))
  13171. tags
  13172. (if (or (eq t org-use-fast-tag-selection)
  13173. (and org-use-fast-tag-selection
  13174. (delq nil (mapcar 'cdr table))))
  13175. (org-fast-tag-selection
  13176. current-tags inherited-tags table
  13177. (if org-fast-tag-selection-include-todo
  13178. org-todo-key-alist))
  13179. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13180. (org-trim
  13181. (org-icompleting-read "Tags: "
  13182. 'org-tags-completion-function
  13183. nil nil current 'org-tags-history))))))
  13184. (while (string-match "[-+&]+" tags)
  13185. ;; No boolean logic, just a list
  13186. (setq tags (replace-match ":" t t tags))))
  13187. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13188. (if org-tags-sort-function
  13189. (setq tags (mapconcat 'identity
  13190. (sort (org-split-string
  13191. tags (org-re "[^[:alnum:]_@#%]+"))
  13192. org-tags-sort-function) ":")))
  13193. (if (string-match "\\`[\t ]*\\'" tags)
  13194. (setq tags "")
  13195. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13196. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13197. ;; Insert new tags at the correct column
  13198. (beginning-of-line 1)
  13199. (setq level (or (and (looking-at org-outline-regexp)
  13200. (- (match-end 0) (point) 1))
  13201. 1))
  13202. (cond
  13203. ((and (equal current "") (equal tags "")))
  13204. ((re-search-forward
  13205. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13206. (point-at-eol) t)
  13207. (if (equal tags "")
  13208. (setq rpl "")
  13209. (goto-char (match-beginning 0))
  13210. (setq c0 (current-column)
  13211. ;; compute offset for the case of org-indent-mode active
  13212. di (if org-indent-mode
  13213. (* (1- org-indent-indentation-per-level) (1- level))
  13214. 0)
  13215. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13216. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13217. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13218. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13219. (replace-match rpl t t)
  13220. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13221. tags)
  13222. (t (error "Tags alignment failed")))
  13223. (org-move-to-column col)
  13224. (unless just-align
  13225. (run-hooks 'org-after-tags-change-hook))))))
  13226. (defun org-change-tag-in-region (beg end tag off)
  13227. "Add or remove TAG for each entry in the region.
  13228. This works in the agenda, and also in an org-mode buffer."
  13229. (interactive
  13230. (list (region-beginning) (region-end)
  13231. (let ((org-last-tags-completion-table
  13232. (if (derived-mode-p 'org-mode)
  13233. (org-uniquify
  13234. (delq nil (append (org-get-buffer-tags)
  13235. (org-global-tags-completion-table))))
  13236. (org-global-tags-completion-table))))
  13237. (org-icompleting-read
  13238. "Tag: " 'org-tags-completion-function nil nil nil
  13239. 'org-tags-history))
  13240. (progn
  13241. (message "[s]et or [r]emove? ")
  13242. (equal (read-char-exclusive) ?r))))
  13243. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13244. (let ((agendap (equal major-mode 'org-agenda-mode))
  13245. l1 l2 m buf pos newhead (cnt 0))
  13246. (goto-char end)
  13247. (setq l2 (1- (org-current-line)))
  13248. (goto-char beg)
  13249. (setq l1 (org-current-line))
  13250. (loop for l from l1 to l2 do
  13251. (org-goto-line l)
  13252. (setq m (get-text-property (point) 'org-hd-marker))
  13253. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13254. (and agendap m))
  13255. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13256. pos (if agendap m (point)))
  13257. (with-current-buffer buf
  13258. (save-excursion
  13259. (save-restriction
  13260. (goto-char pos)
  13261. (setq cnt (1+ cnt))
  13262. (org-toggle-tag tag (if off 'off 'on))
  13263. (setq newhead (org-get-heading)))))
  13264. (and agendap (org-agenda-change-all-lines newhead m))))
  13265. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13266. (defun org-tags-completion-function (string predicate &optional flag)
  13267. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13268. (confirm (lambda (x) (stringp (car x)))))
  13269. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13270. (setq s1 (match-string 1 string)
  13271. s2 (match-string 2 string))
  13272. (setq s1 "" s2 string))
  13273. (cond
  13274. ((eq flag nil)
  13275. ;; try completion
  13276. (setq rtn (try-completion s2 ctable confirm))
  13277. (if (stringp rtn)
  13278. (setq rtn
  13279. (concat s1 s2 (substring rtn (length s2))
  13280. (if (and org-add-colon-after-tag-completion
  13281. (assoc rtn ctable))
  13282. ":" ""))))
  13283. rtn)
  13284. ((eq flag t)
  13285. ;; all-completions
  13286. (all-completions s2 ctable confirm))
  13287. ((eq flag 'lambda)
  13288. ;; exact match?
  13289. (assoc s2 ctable)))))
  13290. (defun org-fast-tag-insert (kwd tags face &optional end)
  13291. "Insert KDW, and the TAGS, the latter with face FACE.
  13292. Also insert END."
  13293. (insert (format "%-12s" (concat kwd ":"))
  13294. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13295. (or end "")))
  13296. (defun org-fast-tag-show-exit (flag)
  13297. (save-excursion
  13298. (org-goto-line 3)
  13299. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13300. (replace-match ""))
  13301. (when flag
  13302. (end-of-line 1)
  13303. (org-move-to-column (- (window-width) 19) t)
  13304. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13305. (defun org-set-current-tags-overlay (current prefix)
  13306. "Add an overlay to CURRENT tag with PREFIX."
  13307. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13308. (if (featurep 'xemacs)
  13309. (org-overlay-display org-tags-overlay (concat prefix s)
  13310. 'secondary-selection)
  13311. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13312. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13313. (defvar org-last-tag-selection-key nil)
  13314. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13315. "Fast tag selection with single keys.
  13316. CURRENT is the current list of tags in the headline, INHERITED is the
  13317. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13318. possibly with grouping information. TODO-TABLE is a similar table with
  13319. TODO keywords, should these have keys assigned to them.
  13320. If the keys are nil, a-z are automatically assigned.
  13321. Returns the new tags string, or nil to not change the current settings."
  13322. (let* ((fulltable (append table todo-table))
  13323. (maxlen (apply 'max (mapcar
  13324. (lambda (x)
  13325. (if (stringp (car x)) (string-width (car x)) 0))
  13326. fulltable)))
  13327. (buf (current-buffer))
  13328. (expert (eq org-fast-tag-selection-single-key 'expert))
  13329. (buffer-tags nil)
  13330. (fwidth (+ maxlen 3 1 3))
  13331. (ncol (/ (- (window-width) 4) fwidth))
  13332. (i-face 'org-done)
  13333. (c-face 'org-todo)
  13334. tg cnt e c char c1 c2 ntable tbl rtn
  13335. ov-start ov-end ov-prefix
  13336. (exit-after-next org-fast-tag-selection-single-key)
  13337. (done-keywords org-done-keywords)
  13338. groups ingroup)
  13339. (save-excursion
  13340. (beginning-of-line 1)
  13341. (if (looking-at
  13342. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13343. (setq ov-start (match-beginning 1)
  13344. ov-end (match-end 1)
  13345. ov-prefix "")
  13346. (setq ov-start (1- (point-at-eol))
  13347. ov-end (1+ ov-start))
  13348. (skip-chars-forward "^\n\r")
  13349. (setq ov-prefix
  13350. (concat
  13351. (buffer-substring (1- (point)) (point))
  13352. (if (> (current-column) org-tags-column)
  13353. " "
  13354. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13355. (move-overlay org-tags-overlay ov-start ov-end)
  13356. (save-window-excursion
  13357. (if expert
  13358. (set-buffer (get-buffer-create " *Org tags*"))
  13359. (delete-other-windows)
  13360. (split-window-vertically)
  13361. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13362. (erase-buffer)
  13363. (org-set-local 'org-done-keywords done-keywords)
  13364. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13365. (org-fast-tag-insert "Current" current c-face "\n\n")
  13366. (org-fast-tag-show-exit exit-after-next)
  13367. (org-set-current-tags-overlay current ov-prefix)
  13368. (setq tbl fulltable char ?a cnt 0)
  13369. (while (setq e (pop tbl))
  13370. (cond
  13371. ((equal (car e) :startgroup)
  13372. (push '() groups) (setq ingroup t)
  13373. (when (not (= cnt 0))
  13374. (setq cnt 0)
  13375. (insert "\n"))
  13376. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13377. ((equal (car e) :endgroup)
  13378. (setq ingroup nil cnt 0)
  13379. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13380. ((equal e '(:newline))
  13381. (when (not (= cnt 0))
  13382. (setq cnt 0)
  13383. (insert "\n")
  13384. (setq e (car tbl))
  13385. (while (equal (car tbl) '(:newline))
  13386. (insert "\n")
  13387. (setq tbl (cdr tbl)))))
  13388. ((equal e '(:grouptags)) nil)
  13389. (t
  13390. (setq tg (copy-sequence (car e)) c2 nil)
  13391. (if (cdr e)
  13392. (setq c (cdr e))
  13393. ;; automatically assign a character.
  13394. (setq c1 (string-to-char
  13395. (downcase (substring
  13396. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13397. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13398. (while (or (rassoc char ntable) (rassoc char table))
  13399. (setq char (1+ char)))
  13400. (setq c2 c1))
  13401. (setq c (or c2 char)))
  13402. (if ingroup (push tg (car groups)))
  13403. (setq tg (org-add-props tg nil 'face
  13404. (cond
  13405. ((not (assoc tg table))
  13406. (org-get-todo-face tg))
  13407. ((member tg current) c-face)
  13408. ((member tg inherited) i-face))))
  13409. (if (equal (caar tbl) :grouptags)
  13410. (org-add-props tg nil 'face 'org-tag-group))
  13411. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13412. (insert "[" c "] " tg (make-string
  13413. (- fwidth 4 (length tg)) ?\ ))
  13414. (push (cons tg c) ntable)
  13415. (when (= (setq cnt (1+ cnt)) ncol)
  13416. (insert "\n")
  13417. (if ingroup (insert " "))
  13418. (setq cnt 0)))))
  13419. (setq ntable (nreverse ntable))
  13420. (insert "\n")
  13421. (goto-char (point-min))
  13422. (if (not expert) (org-fit-window-to-buffer))
  13423. (setq rtn
  13424. (catch 'exit
  13425. (while t
  13426. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13427. (if (not groups) "no " "")
  13428. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13429. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13430. (setq org-last-tag-selection-key c)
  13431. (cond
  13432. ((= c ?\r) (throw 'exit t))
  13433. ((= c ?!)
  13434. (setq groups (not groups))
  13435. (goto-char (point-min))
  13436. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13437. ((= c ?\C-c)
  13438. (if (not expert)
  13439. (org-fast-tag-show-exit
  13440. (setq exit-after-next (not exit-after-next)))
  13441. (setq expert nil)
  13442. (delete-other-windows)
  13443. (set-window-buffer (split-window-vertically) " *Org tags*")
  13444. (org-switch-to-buffer-other-window " *Org tags*")
  13445. (org-fit-window-to-buffer)))
  13446. ((or (= c ?\C-g)
  13447. (and (= c ?q) (not (rassoc c ntable))))
  13448. (org-detach-overlay org-tags-overlay)
  13449. (setq quit-flag t))
  13450. ((= c ?\ )
  13451. (setq current nil)
  13452. (if exit-after-next (setq exit-after-next 'now)))
  13453. ((= c ?\t)
  13454. (condition-case nil
  13455. (setq tg (org-icompleting-read
  13456. "Tag: "
  13457. (or buffer-tags
  13458. (with-current-buffer buf
  13459. (org-get-buffer-tags)))))
  13460. (quit (setq tg "")))
  13461. (when (string-match "\\S-" tg)
  13462. (add-to-list 'buffer-tags (list tg))
  13463. (if (member tg current)
  13464. (setq current (delete tg current))
  13465. (push tg current)))
  13466. (if exit-after-next (setq exit-after-next 'now)))
  13467. ((setq e (rassoc c todo-table) tg (car e))
  13468. (with-current-buffer buf
  13469. (save-excursion (org-todo tg)))
  13470. (if exit-after-next (setq exit-after-next 'now)))
  13471. ((setq e (rassoc c ntable) tg (car e))
  13472. (if (member tg current)
  13473. (setq current (delete tg current))
  13474. (loop for g in groups do
  13475. (if (member tg g)
  13476. (mapc (lambda (x)
  13477. (setq current (delete x current)))
  13478. g)))
  13479. (push tg current))
  13480. (if exit-after-next (setq exit-after-next 'now))))
  13481. ;; Create a sorted list
  13482. (setq current
  13483. (sort current
  13484. (lambda (a b)
  13485. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13486. (if (eq exit-after-next 'now) (throw 'exit t))
  13487. (goto-char (point-min))
  13488. (beginning-of-line 2)
  13489. (delete-region (point) (point-at-eol))
  13490. (org-fast-tag-insert "Current" current c-face)
  13491. (org-set-current-tags-overlay current ov-prefix)
  13492. (while (re-search-forward
  13493. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13494. (setq tg (match-string 1))
  13495. (add-text-properties
  13496. (match-beginning 1) (match-end 1)
  13497. (list 'face
  13498. (cond
  13499. ((member tg current) c-face)
  13500. ((member tg inherited) i-face)
  13501. (t (get-text-property (match-beginning 1) 'face))))))
  13502. (goto-char (point-min)))))
  13503. (org-detach-overlay org-tags-overlay)
  13504. (if rtn
  13505. (mapconcat 'identity current ":")
  13506. nil))))
  13507. (defun org-get-tags-string ()
  13508. "Get the TAGS string in the current headline."
  13509. (unless (org-at-heading-p t)
  13510. (user-error "Not on a heading"))
  13511. (save-excursion
  13512. (beginning-of-line 1)
  13513. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13514. (org-match-string-no-properties 1)
  13515. "")))
  13516. (defun org-get-tags ()
  13517. "Get the list of tags specified in the current headline."
  13518. (org-split-string (org-get-tags-string) ":"))
  13519. (defun org-get-buffer-tags ()
  13520. "Get a table of all tags used in the buffer, for completion."
  13521. (let (tags)
  13522. (save-excursion
  13523. (goto-char (point-min))
  13524. (while (re-search-forward
  13525. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13526. (when (equal (char-after (point-at-bol 0)) ?*)
  13527. (mapc (lambda (x) (add-to-list 'tags x))
  13528. (org-split-string (org-match-string-no-properties 1) ":")))))
  13529. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13530. (mapcar 'list tags)))
  13531. ;;;; The mapping API
  13532. (defun org-map-entries (func &optional match scope &rest skip)
  13533. "Call FUNC at each headline selected by MATCH in SCOPE.
  13534. FUNC is a function or a lisp form. The function will be called without
  13535. arguments, with the cursor positioned at the beginning of the headline.
  13536. The return values of all calls to the function will be collected and
  13537. returned as a list.
  13538. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13539. does not need to preserve point. After evaluation, the cursor will be
  13540. moved to the end of the line (presumably of the headline of the
  13541. processed entry) and search continues from there. Under some
  13542. circumstances, this may not produce the wanted results. For example,
  13543. if you have removed (e.g. archived) the current (sub)tree it could
  13544. mean that the next entry will be skipped entirely. In such cases, you
  13545. can specify the position from where search should continue by making
  13546. FUNC set the variable `org-map-continue-from' to the desired buffer
  13547. position.
  13548. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13549. Only headlines that are matched by this query will be considered during
  13550. the iteration. When MATCH is nil or t, all headlines will be
  13551. visited by the iteration.
  13552. SCOPE determines the scope of this command. It can be any of:
  13553. nil The current buffer, respecting the restriction if any
  13554. tree The subtree started with the entry at point
  13555. region The entries within the active region, if any
  13556. region-start-level
  13557. The entries within the active region, but only those at
  13558. the same level than the first one.
  13559. file The current buffer, without restriction
  13560. file-with-archives
  13561. The current buffer, and any archives associated with it
  13562. agenda All agenda files
  13563. agenda-with-archives
  13564. All agenda files with any archive files associated with them
  13565. \(file1 file2 ...)
  13566. If this is a list, all files in the list will be scanned
  13567. The remaining args are treated as settings for the skipping facilities of
  13568. the scanner. The following items can be given here:
  13569. archive skip trees with the archive tag
  13570. comment skip trees with the COMMENT keyword
  13571. function or Emacs Lisp form:
  13572. will be used as value for `org-agenda-skip-function', so
  13573. whenever the function returns a position, FUNC will not be
  13574. called for that entry and search will continue from the
  13575. position returned
  13576. If your function needs to retrieve the tags including inherited tags
  13577. at the *current* entry, you can use the value of the variable
  13578. `org-scanner-tags' which will be much faster than getting the value
  13579. with `org-get-tags-at'. If your function gets properties with
  13580. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13581. to t around the call to `org-entry-properties' to get the same speedup.
  13582. Note that if your function moves around to retrieve tags and properties at
  13583. a *different* entry, you cannot use these techniques."
  13584. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13585. (not (org-region-active-p)))
  13586. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13587. (org-agenda-skip-archived-trees (memq 'archive skip))
  13588. (org-agenda-skip-comment-trees (memq 'comment skip))
  13589. (org-agenda-skip-function
  13590. (car (org-delete-all '(comment archive) skip)))
  13591. (org-tags-match-list-sublevels t)
  13592. (start-level (eq scope 'region-start-level))
  13593. matcher file res
  13594. org-todo-keywords-for-agenda
  13595. org-done-keywords-for-agenda
  13596. org-todo-keyword-alist-for-agenda
  13597. org-drawers-for-agenda
  13598. org-tag-alist-for-agenda
  13599. todo-only)
  13600. (cond
  13601. ((eq match t) (setq matcher t))
  13602. ((eq match nil) (setq matcher t))
  13603. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13604. (save-window-excursion
  13605. (save-restriction
  13606. (cond ((eq scope 'tree)
  13607. (org-back-to-heading t)
  13608. (org-narrow-to-subtree)
  13609. (setq scope nil))
  13610. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13611. (org-region-active-p))
  13612. ;; If needed, set start-level to a string like "2"
  13613. (when start-level
  13614. (save-excursion
  13615. (goto-char (region-beginning))
  13616. (unless (org-at-heading-p) (outline-next-heading))
  13617. (setq start-level (org-current-level))))
  13618. (narrow-to-region (region-beginning)
  13619. (save-excursion
  13620. (goto-char (region-end))
  13621. (unless (and (bolp) (org-at-heading-p))
  13622. (outline-next-heading))
  13623. (point)))
  13624. (setq scope nil)))
  13625. (if (not scope)
  13626. (progn
  13627. (org-agenda-prepare-buffers
  13628. (list (buffer-file-name (current-buffer))))
  13629. (setq res (org-scan-tags func matcher todo-only start-level)))
  13630. ;; Get the right scope
  13631. (cond
  13632. ((and scope (listp scope) (symbolp (car scope)))
  13633. (setq scope (eval scope)))
  13634. ((eq scope 'agenda)
  13635. (setq scope (org-agenda-files t)))
  13636. ((eq scope 'agenda-with-archives)
  13637. (setq scope (org-agenda-files t))
  13638. (setq scope (org-add-archive-files scope)))
  13639. ((eq scope 'file)
  13640. (setq scope (list (buffer-file-name))))
  13641. ((eq scope 'file-with-archives)
  13642. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13643. (org-agenda-prepare-buffers scope)
  13644. (while (setq file (pop scope))
  13645. (with-current-buffer (org-find-base-buffer-visiting file)
  13646. (save-excursion
  13647. (save-restriction
  13648. (widen)
  13649. (goto-char (point-min))
  13650. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13651. res)))
  13652. ;;;; Properties
  13653. ;;; Setting and retrieving properties
  13654. (defconst org-special-properties
  13655. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13656. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13657. "The special properties valid in Org-mode.
  13658. These are properties that are not defined in the property drawer,
  13659. but in some other way.")
  13660. (defconst org-default-properties
  13661. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13662. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13663. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13664. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13665. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13666. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13667. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13668. "Some properties that are used by Org-mode for various purposes.
  13669. Being in this list makes sure that they are offered for completion.")
  13670. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13671. "Regular expression matching the first line of a property drawer.")
  13672. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13673. "Regular expression matching the last line of a property drawer.")
  13674. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13675. "Regular expression matching the first line of a property drawer.")
  13676. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13677. "Regular expression matching the first line of a property drawer.")
  13678. (defconst org-property-drawer-re
  13679. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13680. org-property-end-re "\\)\n?")
  13681. "Matches an entire property drawer.")
  13682. (defconst org-clock-drawer-re
  13683. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13684. org-property-end-re "\\)\n?")
  13685. "Matches an entire clock drawer.")
  13686. (defsubst org-re-property (property)
  13687. "Return a regexp matching a PROPERTY line.
  13688. Match group 1 will be set to the value."
  13689. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13690. (defsubst org-re-property-keyword (property)
  13691. "Return a regexp matching a PROPERTY line, possibly with no
  13692. value for the property."
  13693. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13694. (defun org-property-action ()
  13695. "Do an action on properties."
  13696. (interactive)
  13697. (let (c)
  13698. (org-at-property-p)
  13699. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13700. (setq c (read-char-exclusive))
  13701. (cond
  13702. ((equal c ?s)
  13703. (call-interactively 'org-set-property))
  13704. ((equal c ?d)
  13705. (call-interactively 'org-delete-property))
  13706. ((equal c ?D)
  13707. (call-interactively 'org-delete-property-globally))
  13708. ((equal c ?c)
  13709. (call-interactively 'org-compute-property-at-point))
  13710. (t (user-error "No such property action %c" c)))))
  13711. (defun org-inc-effort ()
  13712. "Increment the value of the effort property in the current entry."
  13713. (interactive)
  13714. (org-set-effort nil t))
  13715. (defvar org-clock-effort) ;; Defined in org-clock.el
  13716. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13717. (defun org-set-effort (&optional value increment)
  13718. "Set the effort property of the current entry.
  13719. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13720. 10th allowed value.
  13721. When INCREMENT is non-nil, set the property to the next allowed value."
  13722. (interactive "P")
  13723. (if (equal value 0) (setq value 10))
  13724. (let* ((completion-ignore-case t)
  13725. (prop org-effort-property)
  13726. (cur (org-entry-get nil prop))
  13727. (allowed (org-property-get-allowed-values nil prop 'table))
  13728. (existing (mapcar 'list (org-property-values prop)))
  13729. (heading (nth 4 (org-heading-components)))
  13730. rpl
  13731. (val (cond
  13732. ((stringp value) value)
  13733. ((and allowed (integerp value))
  13734. (or (car (nth (1- value) allowed))
  13735. (car (org-last allowed))))
  13736. ((and allowed increment)
  13737. (or (caadr (member (list cur) allowed))
  13738. (user-error "Allowed effort values are not set")))
  13739. (allowed
  13740. (message "Select 1-9,0, [RET%s]: %s"
  13741. (if cur (concat "=" cur) "")
  13742. (mapconcat 'car allowed " "))
  13743. (setq rpl (read-char-exclusive))
  13744. (if (equal rpl ?\r)
  13745. cur
  13746. (setq rpl (- rpl ?0))
  13747. (if (equal rpl 0) (setq rpl 10))
  13748. (if (and (> rpl 0) (<= rpl (length allowed)))
  13749. (car (nth (1- rpl) allowed))
  13750. (org-completing-read "Effort: " allowed nil))))
  13751. (t
  13752. (let (org-completion-use-ido org-completion-use-iswitchb)
  13753. (org-completing-read
  13754. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13755. (concat "[" cur "]") "")
  13756. ": ")
  13757. existing nil nil "" nil cur))))))
  13758. (unless (equal (org-entry-get nil prop) val)
  13759. (org-entry-put nil prop val))
  13760. (save-excursion
  13761. (org-back-to-heading t)
  13762. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13763. (when (string= heading org-clock-current-task)
  13764. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13765. (org-clock-update-mode-line))
  13766. (message "%s is now %s" prop val)))
  13767. (defun org-at-property-p ()
  13768. "Is cursor inside a property drawer?"
  13769. (save-excursion
  13770. (beginning-of-line 1)
  13771. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13772. (save-match-data ;; Used by calling procedures
  13773. (let ((p (point))
  13774. (range (unless (org-before-first-heading-p)
  13775. (org-get-property-block))))
  13776. (and range (<= (car range) p) (< p (cdr range))))))))
  13777. (defun org-get-property-block (&optional beg end force)
  13778. "Return the (beg . end) range of the body of the property drawer.
  13779. BEG and END are the beginning and end of the current subtree, or of
  13780. the part before the first headline. If they are not given, they will
  13781. be found. If the drawer does not exist and FORCE is non-nil, create
  13782. the drawer."
  13783. (catch 'exit
  13784. (save-excursion
  13785. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13786. (progn (org-back-to-heading t) (point))))
  13787. (end (or end (and (not (outline-next-heading)) (point-max))
  13788. (point))))
  13789. (goto-char beg)
  13790. (if (re-search-forward org-property-start-re end t)
  13791. (setq beg (1+ (match-end 0)))
  13792. (if force
  13793. (save-excursion
  13794. (org-insert-property-drawer)
  13795. (setq end (progn (outline-next-heading) (point))))
  13796. (throw 'exit nil))
  13797. (goto-char beg)
  13798. (if (re-search-forward org-property-start-re end t)
  13799. (setq beg (1+ (match-end 0)))))
  13800. (if (re-search-forward org-property-end-re end t)
  13801. (setq end (match-beginning 0))
  13802. (or force (throw 'exit nil))
  13803. (goto-char beg)
  13804. (setq end beg)
  13805. (org-indent-line)
  13806. (insert ":END:\n"))
  13807. (cons beg end)))))
  13808. (defun org-entry-properties (&optional pom which specific)
  13809. "Get all properties of the entry at point-or-marker POM.
  13810. This includes the TODO keyword, the tags, time strings for deadline,
  13811. scheduled, and clocking, and any additional properties defined in the
  13812. entry. The return value is an alist, keys may occur multiple times
  13813. if the property key was used several times.
  13814. POM may also be nil, in which case the current entry is used.
  13815. If WHICH is nil or `all', get all properties. If WHICH is
  13816. `special' or `standard', only get that subclass. If WHICH
  13817. is a string only get exactly this property. SPECIFIC can be a string, the
  13818. specific property we are interested in. Specifying it can speed
  13819. things up because then unnecessary parsing is avoided."
  13820. (setq which (or which 'all))
  13821. (org-with-point-at pom
  13822. (let ((clockstr (substring org-clock-string 0 -1))
  13823. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13824. (case-fold-search nil)
  13825. beg end range props sum-props key key1 value string clocksum clocksumt)
  13826. (save-excursion
  13827. (when (condition-case nil
  13828. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13829. (error nil))
  13830. (setq beg (point))
  13831. (setq sum-props (get-text-property (point) 'org-summaries))
  13832. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13833. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13834. (outline-next-heading)
  13835. (setq end (point))
  13836. (when (memq which '(all special))
  13837. ;; Get the special properties, like TODO and tags
  13838. (goto-char beg)
  13839. (when (and (or (not specific) (string= specific "TODO"))
  13840. (looking-at org-todo-line-regexp) (match-end 2))
  13841. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13842. (when (and (or (not specific) (string= specific "PRIORITY"))
  13843. (looking-at org-priority-regexp))
  13844. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13845. (when (or (not specific) (string= specific "FILE"))
  13846. (push (cons "FILE" buffer-file-name) props))
  13847. (when (and (or (not specific) (string= specific "TAGS"))
  13848. (setq value (org-get-tags-string))
  13849. (string-match "\\S-" value))
  13850. (push (cons "TAGS" value) props))
  13851. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13852. (setq value (org-get-tags-at)))
  13853. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13854. ":"))
  13855. props))
  13856. (when (or (not specific) (string= specific "BLOCKED"))
  13857. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13858. (when (or (not specific)
  13859. (member specific
  13860. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13861. "TIMESTAMP" "TIMESTAMP_IA")))
  13862. (catch 'match
  13863. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13864. (setq key (if (match-end 1)
  13865. (substring (org-match-string-no-properties 1)
  13866. 0 -1))
  13867. string (if (equal key clockstr)
  13868. (org-trim
  13869. (buffer-substring-no-properties
  13870. (match-beginning 3) (goto-char
  13871. (point-at-eol))))
  13872. (substring (org-match-string-no-properties 3)
  13873. 1 -1)))
  13874. ;; Get the correct property name from the key. This is
  13875. ;; necessary if the user has configured time keywords.
  13876. (setq key1 (concat key ":"))
  13877. (cond
  13878. ((not key)
  13879. (setq key
  13880. (if (= (char-after (match-beginning 3)) ?\[)
  13881. "TIMESTAMP_IA" "TIMESTAMP")))
  13882. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13883. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13884. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13885. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13886. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13887. (progn
  13888. (push (cons key string) props)
  13889. ;; no need to search further if match is found
  13890. (throw 'match t))
  13891. (when (or (equal key "CLOCK") (not (assoc key props)))
  13892. (push (cons key string) props)))))))
  13893. (when (memq which '(all standard))
  13894. ;; Get the standard properties, like :PROP: ...
  13895. (setq range (org-get-property-block beg end))
  13896. (when range
  13897. (goto-char (car range))
  13898. (while (re-search-forward
  13899. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13900. (cdr range) t)
  13901. (setq key (org-match-string-no-properties 1)
  13902. value (org-trim (or (org-match-string-no-properties 2) "")))
  13903. (unless (member key excluded)
  13904. (push (cons key (or value "")) props)))))
  13905. (if clocksum
  13906. (push (cons "CLOCKSUM"
  13907. (org-columns-number-to-string (/ (float clocksum) 60.)
  13908. 'add_times))
  13909. props))
  13910. (if clocksumt
  13911. (push (cons "CLOCKSUM_T"
  13912. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13913. 'add_times))
  13914. props))
  13915. (unless (assoc "CATEGORY" props)
  13916. (push (cons "CATEGORY" (org-get-category)) props))
  13917. (append sum-props (nreverse props)))))))
  13918. (defun org-entry-get (pom property &optional inherit literal-nil)
  13919. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13920. If INHERIT is non-nil and the entry does not have the property,
  13921. then also check higher levels of the hierarchy.
  13922. If INHERIT is the symbol `selective', use inheritance only if the setting
  13923. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13924. If the property is present but empty, the return value is the empty string.
  13925. If the property is not present at all, nil is returned.
  13926. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13927. do not interpret it as the list atom nil. This is used for inheritance
  13928. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13929. (org-with-point-at pom
  13930. (if (and inherit (if (eq inherit 'selective)
  13931. (org-property-inherit-p property)
  13932. t))
  13933. (org-entry-get-with-inheritance property literal-nil)
  13934. (if (member property org-special-properties)
  13935. ;; We need a special property. Use `org-entry-properties' to
  13936. ;; retrieve it, but specify the wanted property
  13937. (cdr (assoc property (org-entry-properties nil 'special property)))
  13938. (let ((range (org-get-property-block)))
  13939. (when (and range (not (eq (car range) (cdr range))))
  13940. (let* ((props (list (or (assoc property org-file-properties)
  13941. (assoc property org-global-properties)
  13942. (assoc property org-global-properties-fixed))))
  13943. (ap (lambda (key)
  13944. (when (re-search-forward
  13945. (org-re-property key) (cdr range) t)
  13946. (setq props
  13947. (org-update-property-plist
  13948. key
  13949. (if (match-end 1)
  13950. (org-match-string-no-properties 1) "")
  13951. props)))))
  13952. val)
  13953. (goto-char (car range))
  13954. (funcall ap property)
  13955. (goto-char (car range))
  13956. (while (funcall ap (concat property "+")))
  13957. (setq val (cdr (assoc property props)))
  13958. (when val (if literal-nil val (org-not-nil val))))))))))
  13959. (defun org-property-or-variable-value (var &optional inherit)
  13960. "Check if there is a property fixing the value of VAR.
  13961. If yes, return this value. If not, return the current value of the variable."
  13962. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13963. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13964. (read prop)
  13965. (symbol-value var))))
  13966. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13967. "Delete the property PROPERTY from entry at point-or-marker POM.
  13968. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13969. an empty drawer to delete."
  13970. (org-with-point-at pom
  13971. (if (member property org-special-properties)
  13972. nil ; cannot delete these properties.
  13973. (let ((range (org-get-property-block)))
  13974. (if (and range
  13975. (goto-char (car range))
  13976. (re-search-forward
  13977. (org-re-property property)
  13978. (cdr range) t))
  13979. (progn
  13980. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13981. (and delete-empty-drawer
  13982. (org-remove-empty-drawer-at
  13983. delete-empty-drawer (car range)))
  13984. t)
  13985. nil)))))
  13986. ;; Multi-values properties are properties that contain multiple values
  13987. ;; These values are assumed to be single words, separated by whitespace.
  13988. (defun org-entry-add-to-multivalued-property (pom property value)
  13989. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13990. (let* ((old (org-entry-get pom property))
  13991. (values (and old (org-split-string old "[ \t]"))))
  13992. (setq value (org-entry-protect-space value))
  13993. (unless (member value values)
  13994. (setq values (append values (list value)))
  13995. (org-entry-put pom property
  13996. (mapconcat 'identity values " ")))))
  13997. (defun org-entry-remove-from-multivalued-property (pom property value)
  13998. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13999. (let* ((old (org-entry-get pom property))
  14000. (values (and old (org-split-string old "[ \t]"))))
  14001. (setq value (org-entry-protect-space value))
  14002. (when (member value values)
  14003. (setq values (delete value values))
  14004. (org-entry-put pom property
  14005. (mapconcat 'identity values " ")))))
  14006. (defun org-entry-member-in-multivalued-property (pom property value)
  14007. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14008. (let* ((old (org-entry-get pom property))
  14009. (values (and old (org-split-string old "[ \t]"))))
  14010. (setq value (org-entry-protect-space value))
  14011. (member value values)))
  14012. (defun org-entry-get-multivalued-property (pom property)
  14013. "Return a list of values in a multivalued property."
  14014. (let* ((value (org-entry-get pom property))
  14015. (values (and value (org-split-string value "[ \t]"))))
  14016. (mapcar 'org-entry-restore-space values)))
  14017. (defun org-entry-put-multivalued-property (pom property &rest values)
  14018. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14019. VALUES should be a list of strings. Spaces will be protected."
  14020. (org-entry-put pom property
  14021. (mapconcat 'org-entry-protect-space values " "))
  14022. (let* ((value (org-entry-get pom property))
  14023. (values (and value (org-split-string value "[ \t]"))))
  14024. (mapcar 'org-entry-restore-space values)))
  14025. (defun org-entry-protect-space (s)
  14026. "Protect spaces and newline in string S."
  14027. (while (string-match " " s)
  14028. (setq s (replace-match "%20" t t s)))
  14029. (while (string-match "\n" s)
  14030. (setq s (replace-match "%0A" t t s)))
  14031. s)
  14032. (defun org-entry-restore-space (s)
  14033. "Restore spaces and newline in string S."
  14034. (while (string-match "%20" s)
  14035. (setq s (replace-match " " t t s)))
  14036. (while (string-match "%0A" s)
  14037. (setq s (replace-match "\n" t t s)))
  14038. s)
  14039. (defvar org-entry-property-inherited-from (make-marker)
  14040. "Marker pointing to the entry from where a property was inherited.
  14041. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14042. location of the entry where the inheritance search matched. If there was
  14043. no match, the marker will point nowhere.
  14044. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14045. is set.")
  14046. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14047. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14048. The search will stop at the first ancestor which has the property defined.
  14049. If the value found is \"nil\", return nil to show that the property
  14050. should be considered as undefined (this is the meaning of nil here).
  14051. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14052. (move-marker org-entry-property-inherited-from nil)
  14053. (let (tmp)
  14054. (save-excursion
  14055. (save-restriction
  14056. (widen)
  14057. (catch 'ex
  14058. (while t
  14059. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14060. (or (ignore-errors (org-back-to-heading t))
  14061. (goto-char (point-min)))
  14062. (move-marker org-entry-property-inherited-from (point))
  14063. (throw 'ex tmp))
  14064. (or (ignore-errors (org-up-heading-safe))
  14065. (throw 'ex nil))))))
  14066. (setq tmp (or tmp
  14067. (cdr (assoc property org-file-properties))
  14068. (cdr (assoc property org-global-properties))
  14069. (cdr (assoc property org-global-properties-fixed))))
  14070. (if literal-nil tmp (org-not-nil tmp))))
  14071. (defvar org-property-changed-functions nil
  14072. "Hook called when the value of a property has changed.
  14073. Each hook function should accept two arguments, the name of the property
  14074. and the new value.")
  14075. (defun org-entry-put (pom property value)
  14076. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14077. (org-with-point-at pom
  14078. (org-back-to-heading t)
  14079. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14080. range)
  14081. (cond
  14082. ((equal property "TODO")
  14083. (when (and (stringp value) (string-match "\\S-" value)
  14084. (not (member value org-todo-keywords-1)))
  14085. (user-error "\"%s\" is not a valid TODO state" value))
  14086. (if (or (not value)
  14087. (not (string-match "\\S-" value)))
  14088. (setq value 'none))
  14089. (org-todo value)
  14090. (org-set-tags nil 'align))
  14091. ((equal property "PRIORITY")
  14092. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14093. (string-to-char value) ?\ ))
  14094. (org-set-tags nil 'align))
  14095. ((equal property "CLOCKSUM")
  14096. (if (not (re-search-forward
  14097. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14098. (error "Cannot find a clock log")
  14099. (goto-char (- (match-end 1) 2))
  14100. (cond
  14101. ((eq value 'earlier) (org-timestamp-down))
  14102. ((eq value 'later) (org-timestamp-up)))
  14103. (org-clock-sum-current-item)))
  14104. ((equal property "SCHEDULED")
  14105. (if (re-search-forward org-scheduled-time-regexp end t)
  14106. (cond
  14107. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14108. ((eq value 'later) (org-timestamp-change 1 'day))
  14109. (t (call-interactively 'org-schedule)))
  14110. (call-interactively 'org-schedule)))
  14111. ((equal property "DEADLINE")
  14112. (if (re-search-forward org-deadline-time-regexp end t)
  14113. (cond
  14114. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14115. ((eq value 'later) (org-timestamp-change 1 'day))
  14116. (t (call-interactively 'org-deadline)))
  14117. (call-interactively 'org-deadline)))
  14118. ((member property org-special-properties)
  14119. (error "The %s property can not yet be set with `org-entry-put'"
  14120. property))
  14121. (t ; a non-special property
  14122. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14123. (setq range (org-get-property-block beg end 'force))
  14124. (goto-char (car range))
  14125. (if (re-search-forward
  14126. (org-re-property-keyword property) (cdr range) t)
  14127. (progn
  14128. (delete-region (match-beginning 0) (match-end 0))
  14129. (goto-char (match-beginning 0)))
  14130. (goto-char (cdr range))
  14131. (insert "\n")
  14132. (backward-char 1)
  14133. (org-indent-line))
  14134. (insert ":" property ":")
  14135. (and value (insert " " value))
  14136. (org-indent-line)))))
  14137. (run-hook-with-args 'org-property-changed-functions property value)))
  14138. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14139. "Get all property keys in the current buffer.
  14140. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14141. like tags and TODO state.
  14142. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14143. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14144. and others.
  14145. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14146. formats in the current buffer."
  14147. (let (rtn range cfmt s p)
  14148. (save-excursion
  14149. (save-restriction
  14150. (widen)
  14151. (goto-char (point-min))
  14152. (while (re-search-forward org-property-start-re nil t)
  14153. (setq range (org-get-property-block))
  14154. (goto-char (car range))
  14155. (while (re-search-forward
  14156. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14157. (cdr range) t)
  14158. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14159. (outline-next-heading))))
  14160. (when include-specials
  14161. (setq rtn (append org-special-properties rtn)))
  14162. (when include-defaults
  14163. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14164. (add-to-list 'rtn org-effort-property))
  14165. (when include-columns
  14166. (save-excursion
  14167. (save-restriction
  14168. (widen)
  14169. (goto-char (point-min))
  14170. (while (re-search-forward
  14171. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14172. nil t)
  14173. (setq cfmt (match-string 2) s 0)
  14174. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14175. cfmt s)
  14176. (setq s (match-end 0)
  14177. p (match-string 1 cfmt))
  14178. (unless (or (equal p "ITEM")
  14179. (member p org-special-properties))
  14180. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14181. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14182. (defun org-property-values (key)
  14183. "Return a list of all values of property KEY in the current buffer."
  14184. (save-excursion
  14185. (save-restriction
  14186. (widen)
  14187. (goto-char (point-min))
  14188. (let ((re (org-re-property key))
  14189. values)
  14190. (while (re-search-forward re nil t)
  14191. (add-to-list 'values (org-trim (match-string 1))))
  14192. (delete "" values)))))
  14193. (defun org-insert-property-drawer ()
  14194. "Insert a property drawer into the current entry."
  14195. (org-back-to-heading t)
  14196. (looking-at org-outline-regexp)
  14197. (let ((indent (if org-adapt-indentation
  14198. (- (match-end 0) (match-beginning 0))
  14199. 0))
  14200. (beg (point))
  14201. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14202. end hiddenp)
  14203. (outline-next-heading)
  14204. (setq end (point))
  14205. (goto-char beg)
  14206. (while (re-search-forward re end t))
  14207. (setq hiddenp (outline-invisible-p))
  14208. (end-of-line 1)
  14209. (and (equal (char-after) ?\n) (forward-char 1))
  14210. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14211. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14212. ;; just skip this line
  14213. (beginning-of-line 2)
  14214. ;; Drawer start, find the end
  14215. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14216. (beginning-of-line 1)))
  14217. (org-skip-over-state-notes)
  14218. (skip-chars-backward " \t\n\r")
  14219. (if (eq (char-before) ?*) (forward-char 1))
  14220. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14221. (beginning-of-line 0)
  14222. (org-indent-to-column indent)
  14223. (beginning-of-line 2)
  14224. (org-indent-to-column indent)
  14225. (beginning-of-line 0)
  14226. (if hiddenp
  14227. (save-excursion
  14228. (org-back-to-heading t)
  14229. (hide-entry))
  14230. (org-flag-drawer t))))
  14231. (defun org-insert-drawer (&optional arg drawer)
  14232. "Insert a drawer at point.
  14233. Optional argument DRAWER, when non-nil, is a string representing
  14234. drawer's name. Otherwise, the user is prompted for a name.
  14235. If a region is active, insert the drawer around that region
  14236. instead.
  14237. Point is left between drawer's boundaries."
  14238. (interactive "P")
  14239. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14240. "LOGBOOK"))
  14241. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14242. ;; internally by Org. They are meant to be inserted
  14243. ;; automatically.
  14244. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14245. ;; Remove system drawers from list. Note: For some reason,
  14246. ;; `org-completing-read' ignores the predicate while
  14247. ;; `completing-read' handles it fine.
  14248. (drawer (if arg "PROPERTIES"
  14249. (or drawer
  14250. (completing-read
  14251. "Drawer: " org-drawers
  14252. (lambda (d) (not (member d system-drawers))))))))
  14253. (cond
  14254. ;; With C-u, fall back on `org-insert-property-drawer'
  14255. (arg (org-insert-property-drawer))
  14256. ;; With an active region, insert a drawer at point.
  14257. ((not (org-region-active-p))
  14258. (progn
  14259. (unless (bolp) (insert "\n"))
  14260. (insert (format ":%s:\n\n:END:\n" drawer))
  14261. (forward-line -2)))
  14262. ;; Otherwise, insert the drawer at point
  14263. (t
  14264. (let ((rbeg (region-beginning))
  14265. (rend (copy-marker (region-end))))
  14266. (unwind-protect
  14267. (progn
  14268. (goto-char rbeg)
  14269. (beginning-of-line)
  14270. (when (save-excursion
  14271. (re-search-forward org-outline-regexp-bol rend t))
  14272. (user-error "Drawers cannot contain headlines"))
  14273. ;; Position point at the beginning of the first
  14274. ;; non-blank line in region. Insert drawer's opening
  14275. ;; there, then indent it.
  14276. (org-skip-whitespace)
  14277. (beginning-of-line)
  14278. (insert ":" drawer ":\n")
  14279. (forward-line -1)
  14280. (indent-for-tab-command)
  14281. ;; Move point to the beginning of the first blank line
  14282. ;; after the last non-blank line in region. Insert
  14283. ;; drawer's closing, then indent it.
  14284. (goto-char rend)
  14285. (skip-chars-backward " \r\t\n")
  14286. (insert "\n:END:")
  14287. (deactivate-mark t)
  14288. (indent-for-tab-command)
  14289. (unless (eolp) (insert "\n")))
  14290. ;; Clear marker, whatever the outcome of insertion is.
  14291. (set-marker rend nil)))))))
  14292. (defvar org-property-set-functions-alist nil
  14293. "Property set function alist.
  14294. Each entry should have the following format:
  14295. (PROPERTY . READ-FUNCTION)
  14296. The read function will be called with the same argument as
  14297. `org-completing-read'.")
  14298. (defun org-set-property-function (property)
  14299. "Get the function that should be used to set PROPERTY.
  14300. This is computed according to `org-property-set-functions-alist'."
  14301. (or (cdr (assoc property org-property-set-functions-alist))
  14302. 'org-completing-read))
  14303. (defun org-read-property-value (property)
  14304. "Read PROPERTY value from user."
  14305. (let* ((completion-ignore-case t)
  14306. (allowed (org-property-get-allowed-values nil property 'table))
  14307. (cur (org-entry-get nil property))
  14308. (prompt (concat property " value"
  14309. (if (and cur (string-match "\\S-" cur))
  14310. (concat " [" cur "]") "") ": "))
  14311. (set-function (org-set-property-function property))
  14312. (val (if allowed
  14313. (funcall set-function prompt allowed nil
  14314. (not (get-text-property 0 'org-unrestricted
  14315. (caar allowed))))
  14316. (let (org-completion-use-ido org-completion-use-iswitchb)
  14317. (funcall set-function prompt
  14318. (mapcar 'list (org-property-values property))
  14319. nil nil "" nil cur)))))
  14320. (if (equal val "")
  14321. cur
  14322. val)))
  14323. (defvar org-last-set-property nil)
  14324. (defvar org-last-set-property-value nil)
  14325. (defun org-read-property-name ()
  14326. "Read a property name."
  14327. (let* ((completion-ignore-case t)
  14328. (keys (org-buffer-property-keys nil t t))
  14329. (default-prop (or (save-excursion
  14330. (save-match-data
  14331. (beginning-of-line)
  14332. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14333. (null (string= (match-string 1) "END"))
  14334. (match-string 1))))
  14335. org-last-set-property))
  14336. (property (org-icompleting-read
  14337. (concat "Property"
  14338. (if default-prop (concat " [" default-prop "]") "")
  14339. ": ")
  14340. (mapcar 'list keys)
  14341. nil nil nil nil
  14342. default-prop)))
  14343. (if (member property keys)
  14344. property
  14345. (or (cdr (assoc (downcase property)
  14346. (mapcar (lambda (x) (cons (downcase x) x))
  14347. keys)))
  14348. property))))
  14349. (defun org-set-property-and-value (use-last)
  14350. "Allow to set [PROPERTY]: [value] direction from prompt.
  14351. When use-default, don't even ask, just use the last
  14352. \"[PROPERTY]: [value]\" string from the history."
  14353. (interactive "P")
  14354. (let* ((completion-ignore-case t)
  14355. (pv (or (and use-last org-last-set-property-value)
  14356. (org-completing-read
  14357. "Enter a \"[Property]: [value]\" pair: "
  14358. nil nil nil nil nil
  14359. org-last-set-property-value)))
  14360. prop val)
  14361. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14362. (setq prop (match-string 1 pv)
  14363. val (match-string 2 pv))
  14364. (org-set-property prop val))))
  14365. (defun org-set-property (property value)
  14366. "In the current entry, set PROPERTY to VALUE.
  14367. When called interactively, this will prompt for a property name, offering
  14368. completion on existing and default properties. And then it will prompt
  14369. for a value, offering completion either on allowed values (via an inherited
  14370. xxx_ALL property) or on existing values in other instances of this property
  14371. in the current file."
  14372. (interactive (list nil nil))
  14373. (let* ((property (or property (org-read-property-name)))
  14374. (value (or value (org-read-property-value property)))
  14375. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14376. (setq org-last-set-property property)
  14377. (setq org-last-set-property-value (concat property ": " value))
  14378. ;; Possibly postprocess the inserted value:
  14379. (when fn (setq value (funcall fn value)))
  14380. (unless (equal (org-entry-get nil property) value)
  14381. (org-entry-put nil property value))))
  14382. (defun org-delete-property (property &optional delete-empty-drawer)
  14383. "In the current entry, delete PROPERTY.
  14384. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14385. an empty drawer to delete."
  14386. (interactive
  14387. (let* ((completion-ignore-case t)
  14388. (prop (org-icompleting-read "Property: "
  14389. (org-entry-properties nil 'standard))))
  14390. (list prop)))
  14391. (message "Property %s %s" property
  14392. (if (org-entry-delete nil property delete-empty-drawer)
  14393. "deleted"
  14394. "was not present in the entry")))
  14395. (defun org-delete-property-globally (property)
  14396. "Remove PROPERTY globally, from all entries."
  14397. (interactive
  14398. (let* ((completion-ignore-case t)
  14399. (prop (org-icompleting-read
  14400. "Globally remove property: "
  14401. (mapcar 'list (org-buffer-property-keys)))))
  14402. (list prop)))
  14403. (save-excursion
  14404. (save-restriction
  14405. (widen)
  14406. (goto-char (point-min))
  14407. (let ((cnt 0))
  14408. (while (re-search-forward
  14409. (org-re-property property)
  14410. nil t)
  14411. (setq cnt (1+ cnt))
  14412. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14413. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14414. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14415. (defun org-compute-property-at-point ()
  14416. "Compute the property at point.
  14417. This looks for an enclosing column format, extracts the operator and
  14418. then applies it to the property in the column format's scope."
  14419. (interactive)
  14420. (unless (org-at-property-p)
  14421. (user-error "Not at a property"))
  14422. (let ((prop (org-match-string-no-properties 2)))
  14423. (org-columns-get-format-and-top-level)
  14424. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14425. (user-error "No operator defined for property %s" prop))
  14426. (org-columns-compute prop)))
  14427. (defvar org-property-allowed-value-functions nil
  14428. "Hook for functions supplying allowed values for a specific property.
  14429. The functions must take a single argument, the name of the property, and
  14430. return a flat list of allowed values. If \":ETC\" is one of
  14431. the values, this means that these values are intended as defaults for
  14432. completion, but that other values should be allowed too.
  14433. The functions must return nil if they are not responsible for this
  14434. property.")
  14435. (defun org-property-get-allowed-values (pom property &optional table)
  14436. "Get allowed values for the property PROPERTY.
  14437. When TABLE is non-nil, return an alist that can directly be used for
  14438. completion."
  14439. (let (vals)
  14440. (cond
  14441. ((equal property "TODO")
  14442. (setq vals (org-with-point-at pom
  14443. (append org-todo-keywords-1 '("")))))
  14444. ((equal property "PRIORITY")
  14445. (let ((n org-lowest-priority))
  14446. (while (>= n org-highest-priority)
  14447. (push (char-to-string n) vals)
  14448. (setq n (1- n)))))
  14449. ((member property org-special-properties))
  14450. ((setq vals (run-hook-with-args-until-success
  14451. 'org-property-allowed-value-functions property)))
  14452. (t
  14453. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14454. (when (and vals (string-match "\\S-" vals))
  14455. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14456. (setq vals (mapcar (lambda (x)
  14457. (cond ((stringp x) x)
  14458. ((numberp x) (number-to-string x))
  14459. ((symbolp x) (symbol-name x))
  14460. (t "???")))
  14461. vals)))))
  14462. (when (member ":ETC" vals)
  14463. (setq vals (remove ":ETC" vals))
  14464. (org-add-props (car vals) '(org-unrestricted t)))
  14465. (if table (mapcar 'list vals) vals)))
  14466. (defun org-property-previous-allowed-value (&optional previous)
  14467. "Switch to the next allowed value for this property."
  14468. (interactive)
  14469. (org-property-next-allowed-value t))
  14470. (defun org-property-next-allowed-value (&optional previous)
  14471. "Switch to the next allowed value for this property."
  14472. (interactive)
  14473. (unless (org-at-property-p)
  14474. (user-error "Not at a property"))
  14475. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14476. (key (match-string 2))
  14477. (value (match-string 3))
  14478. (allowed (or (org-property-get-allowed-values (point) key)
  14479. (and (member value '("[ ]" "[-]" "[X]"))
  14480. '("[ ]" "[X]"))))
  14481. (heading (save-match-data (nth 4 (org-heading-components))))
  14482. nval)
  14483. (unless allowed
  14484. (user-error "Allowed values for this property have not been defined"))
  14485. (if previous (setq allowed (reverse allowed)))
  14486. (if (member value allowed)
  14487. (setq nval (car (cdr (member value allowed)))))
  14488. (setq nval (or nval (car allowed)))
  14489. (if (equal nval value)
  14490. (user-error "Only one allowed value for this property"))
  14491. (org-at-property-p)
  14492. (replace-match (concat " :" key ": " nval) t t)
  14493. (org-indent-line)
  14494. (beginning-of-line 1)
  14495. (skip-chars-forward " \t")
  14496. (when (equal prop org-effort-property)
  14497. (save-excursion
  14498. (org-back-to-heading t)
  14499. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14500. (when (string= org-clock-current-task heading)
  14501. (setq org-clock-effort nval)
  14502. (org-clock-update-mode-line)))
  14503. (run-hook-with-args 'org-property-changed-functions key nval)))
  14504. (defun org-find-olp (path &optional this-buffer)
  14505. "Return a marker pointing to the entry at outline path OLP.
  14506. If anything goes wrong, throw an error.
  14507. You can wrap this call to catch the error like this:
  14508. (condition-case msg
  14509. (org-mobile-locate-entry (match-string 4))
  14510. (error (nth 1 msg)))
  14511. The return value will then be either a string with the error message,
  14512. or a marker if everything is OK.
  14513. If THIS-BUFFER is set, the outline path does not contain a file,
  14514. only headings."
  14515. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14516. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14517. (level 1)
  14518. (lmin 1)
  14519. (lmax 1)
  14520. limit re end found pos heading cnt flevel)
  14521. (unless buffer (error "File not found :%s" file))
  14522. (with-current-buffer buffer
  14523. (save-excursion
  14524. (save-restriction
  14525. (widen)
  14526. (setq limit (point-max))
  14527. (goto-char (point-min))
  14528. (while (setq heading (pop path))
  14529. (setq re (format org-complex-heading-regexp-format
  14530. (regexp-quote heading)))
  14531. (setq cnt 0 pos (point))
  14532. (while (re-search-forward re end t)
  14533. (setq level (- (match-end 1) (match-beginning 1)))
  14534. (if (and (>= level lmin) (<= level lmax))
  14535. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14536. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14537. lmax heading))
  14538. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14539. lmax heading))
  14540. (goto-char found)
  14541. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14542. (setq end (save-excursion (org-end-of-subtree t t))))
  14543. (when (org-at-heading-p)
  14544. (point-marker)))))))
  14545. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14546. "Find node HEADING in BUFFER.
  14547. Return a marker to the heading if it was found, or nil if not.
  14548. If POS-ONLY is set, return just the position instead of a marker.
  14549. The heading text must match exact, but it may have a TODO keyword,
  14550. a priority cookie and tags in the standard locations."
  14551. (with-current-buffer (or buffer (current-buffer))
  14552. (save-excursion
  14553. (save-restriction
  14554. (widen)
  14555. (goto-char (point-min))
  14556. (let (case-fold-search)
  14557. (if (re-search-forward
  14558. (format org-complex-heading-regexp-format
  14559. (regexp-quote heading)) nil t)
  14560. (if pos-only
  14561. (match-beginning 0)
  14562. (move-marker (make-marker) (match-beginning 0)))))))))
  14563. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14564. "Find Org node headline HEADING in all .org files in directory DIR.
  14565. When the target headline is found, return a marker to this location."
  14566. (let ((files (directory-files (or dir default-directory)
  14567. nil "\\`[^.#].*\\.org\\'"))
  14568. file visiting m buffer)
  14569. (catch 'found
  14570. (while (setq file (pop files))
  14571. (message "trying %s" file)
  14572. (setq visiting (org-find-base-buffer-visiting file))
  14573. (setq buffer (or visiting (find-file-noselect file)))
  14574. (setq m (org-find-exact-headline-in-buffer
  14575. heading buffer))
  14576. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14577. (and m (throw 'found m))))))
  14578. (defun org-find-entry-with-id (ident)
  14579. "Locate the entry that contains the ID property with exact value IDENT.
  14580. IDENT can be a string, a symbol or a number, this function will search for
  14581. the string representation of it.
  14582. Return the position where this entry starts, or nil if there is no such entry."
  14583. (interactive "sID: ")
  14584. (let ((id (cond
  14585. ((stringp ident) ident)
  14586. ((symbol-name ident) (symbol-name ident))
  14587. ((numberp ident) (number-to-string ident))
  14588. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14589. (case-fold-search nil))
  14590. (save-excursion
  14591. (save-restriction
  14592. (widen)
  14593. (goto-char (point-min))
  14594. (when (re-search-forward
  14595. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14596. nil t)
  14597. (org-back-to-heading t)
  14598. (point))))))
  14599. ;;;; Timestamps
  14600. (defvar org-last-changed-timestamp nil)
  14601. (defvar org-last-inserted-timestamp nil
  14602. "The last time stamp inserted with `org-insert-time-stamp'.")
  14603. (defvar org-time-was-given) ; dynamically scoped parameter
  14604. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14605. (defvar org-ts-what) ; dynamically scoped parameter
  14606. (defun org-time-stamp (arg &optional inactive)
  14607. "Prompt for a date/time and insert a time stamp.
  14608. If the user specifies a time like HH:MM or if this command is
  14609. called with at least one prefix argument, the time stamp contains
  14610. the date and the time. Otherwise, only the date is be included.
  14611. All parts of a date not specified by the user is filled in from
  14612. the current date/time. So if you just press return without
  14613. typing anything, the time stamp will represent the current
  14614. date/time.
  14615. If there is already a timestamp at the cursor, it will be
  14616. modified.
  14617. With two universal prefix arguments, insert an active timestamp
  14618. with the current time without prompting the user."
  14619. (interactive "P")
  14620. (let* ((ts nil)
  14621. (default-time
  14622. ;; Default time is either today, or, when entering a range,
  14623. ;; the range start.
  14624. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14625. (save-excursion
  14626. (re-search-backward
  14627. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14628. (- (point) 20) t)))
  14629. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14630. (current-time)))
  14631. (default-input (and ts (org-get-compact-tod ts)))
  14632. (repeater (save-excursion
  14633. (save-match-data
  14634. (beginning-of-line)
  14635. (when (re-search-forward
  14636. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14637. (save-excursion (progn (end-of-line) (point))) t)
  14638. (match-string 0)))))
  14639. org-time-was-given org-end-time-was-given time)
  14640. (cond
  14641. ((and (org-at-timestamp-p t)
  14642. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14643. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14644. (insert "--")
  14645. (setq time (let ((this-command this-command))
  14646. (org-read-date arg 'totime nil nil
  14647. default-time default-input inactive)))
  14648. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14649. ((org-at-timestamp-p t)
  14650. (setq time (let ((this-command this-command))
  14651. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14652. (when (org-at-timestamp-p t) ; just to get the match data
  14653. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14654. (replace-match "")
  14655. (setq org-last-changed-timestamp
  14656. (org-insert-time-stamp
  14657. time (or org-time-was-given arg)
  14658. inactive nil nil (list org-end-time-was-given)))
  14659. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14660. (setq org-last-changed-timestamp
  14661. (concat (substring org-last-inserted-timestamp 0 -1)
  14662. " " repeater ">"))))
  14663. (message "Timestamp updated"))
  14664. ((equal arg '(16))
  14665. (org-insert-time-stamp (current-time) t))
  14666. (t
  14667. (setq time (let ((this-command this-command))
  14668. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14669. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14670. nil nil (list org-end-time-was-given))))))
  14671. ;; FIXME: can we use this for something else, like computing time differences?
  14672. (defun org-get-compact-tod (s)
  14673. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14674. (let* ((t1 (match-string 1 s))
  14675. (h1 (string-to-number (match-string 2 s)))
  14676. (m1 (string-to-number (match-string 3 s)))
  14677. (t2 (and (match-end 4) (match-string 5 s)))
  14678. (h2 (and t2 (string-to-number (match-string 6 s))))
  14679. (m2 (and t2 (string-to-number (match-string 7 s))))
  14680. dh dm)
  14681. (if (not t2)
  14682. t1
  14683. (setq dh (- h2 h1) dm (- m2 m1))
  14684. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14685. (concat t1 "+" (number-to-string dh)
  14686. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14687. (defun org-time-stamp-inactive (&optional arg)
  14688. "Insert an inactive time stamp.
  14689. An inactive time stamp is enclosed in square brackets instead of angle
  14690. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14691. does not link to the calendar and cannot be changed with the S-cursor keys.
  14692. So these are more for recording a certain time/date."
  14693. (interactive "P")
  14694. (org-time-stamp arg 'inactive))
  14695. (defvar org-date-ovl (make-overlay 1 1))
  14696. (overlay-put org-date-ovl 'face 'org-date-selected)
  14697. (org-detach-overlay org-date-ovl)
  14698. (defvar org-ans1) ; dynamically scoped parameter
  14699. (defvar org-ans2) ; dynamically scoped parameter
  14700. (defvar org-plain-time-of-day-regexp) ; defined below
  14701. (defvar org-overriding-default-time nil) ; dynamically scoped
  14702. (defvar org-read-date-overlay nil)
  14703. (defvar org-dcst nil) ; dynamically scoped
  14704. (defvar org-read-date-history nil)
  14705. (defvar org-read-date-final-answer nil)
  14706. (defvar org-read-date-analyze-futurep nil)
  14707. (defvar org-read-date-analyze-forced-year nil)
  14708. (defvar org-read-date-inactive)
  14709. (defvar org-read-date-minibuffer-local-map
  14710. (let ((map (make-sparse-keymap)))
  14711. (set-keymap-parent map minibuffer-local-map)
  14712. (org-defkey map (kbd ".")
  14713. (lambda () (interactive)
  14714. (org-eval-in-calendar '(calendar-goto-today))))
  14715. (org-defkey map [(meta shift left)]
  14716. (lambda () (interactive)
  14717. (org-eval-in-calendar '(calendar-backward-month 1))))
  14718. (org-defkey map [(meta shift right)]
  14719. (lambda () (interactive)
  14720. (org-eval-in-calendar '(calendar-forward-month 1))))
  14721. (org-defkey map [(meta shift up)]
  14722. (lambda () (interactive)
  14723. (org-eval-in-calendar '(calendar-backward-year 1))))
  14724. (org-defkey map [(meta shift down)]
  14725. (lambda () (interactive)
  14726. (org-eval-in-calendar '(calendar-forward-year 1))))
  14727. (org-defkey map [?\e (shift left)]
  14728. (lambda () (interactive)
  14729. (org-eval-in-calendar '(calendar-backward-month 1))))
  14730. (org-defkey map [?\e (shift right)]
  14731. (lambda () (interactive)
  14732. (org-eval-in-calendar '(calendar-forward-month 1))))
  14733. (org-defkey map [?\e (shift up)]
  14734. (lambda () (interactive)
  14735. (org-eval-in-calendar '(calendar-backward-year 1))))
  14736. (org-defkey map [?\e (shift down)]
  14737. (lambda () (interactive)
  14738. (org-eval-in-calendar '(calendar-forward-year 1))))
  14739. (org-defkey map [(shift up)]
  14740. (lambda () (interactive)
  14741. (org-eval-in-calendar '(calendar-backward-week 1))))
  14742. (org-defkey map [(shift down)]
  14743. (lambda () (interactive)
  14744. (org-eval-in-calendar '(calendar-forward-week 1))))
  14745. (org-defkey map [(shift left)]
  14746. (lambda () (interactive)
  14747. (org-eval-in-calendar '(calendar-backward-day 1))))
  14748. (org-defkey map [(shift right)]
  14749. (lambda () (interactive)
  14750. (org-eval-in-calendar '(calendar-forward-day 1))))
  14751. (org-defkey map "!"
  14752. (lambda () (interactive)
  14753. (org-eval-in-calendar '(diary-view-entries))
  14754. (message "")))
  14755. (org-defkey map ">"
  14756. (lambda () (interactive)
  14757. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14758. (org-defkey map "<"
  14759. (lambda () (interactive)
  14760. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14761. (org-defkey map "\C-v"
  14762. (lambda () (interactive)
  14763. (org-eval-in-calendar
  14764. '(calendar-scroll-left-three-months 1))))
  14765. (org-defkey map "\M-v"
  14766. (lambda () (interactive)
  14767. (org-eval-in-calendar
  14768. '(calendar-scroll-right-three-months 1))))
  14769. map)
  14770. "Keymap for minibuffer commands when using `org-read-date'.")
  14771. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14772. default-time default-input inactive)
  14773. "Read a date, possibly a time, and make things smooth for the user.
  14774. The prompt will suggest to enter an ISO date, but you can also enter anything
  14775. which will at least partially be understood by `parse-time-string'.
  14776. Unrecognized parts of the date will default to the current day, month, year,
  14777. hour and minute. If this command is called to replace a timestamp at point,
  14778. or to enter the second timestamp of a range, the default time is taken
  14779. from the existing stamp. Furthermore, the command prefers the future,
  14780. so if you are giving a date where the year is not given, and the day-month
  14781. combination is already past in the current year, it will assume you
  14782. mean next year. For details, see the manual. A few examples:
  14783. 3-2-5 --> 2003-02-05
  14784. feb 15 --> currentyear-02-15
  14785. 2/15 --> currentyear-02-15
  14786. sep 12 9 --> 2009-09-12
  14787. 12:45 --> today 12:45
  14788. 22 sept 0:34 --> currentyear-09-22 0:34
  14789. 12 --> currentyear-currentmonth-12
  14790. Fri --> nearest Friday after today
  14791. -Tue --> last Tuesday
  14792. etc.
  14793. Furthermore you can specify a relative date by giving, as the *first* thing
  14794. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14795. change in days weeks, months, years.
  14796. With a single plus or minus, the date is relative to today. With a double
  14797. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14798. +4d --> four days from today
  14799. +4 --> same as above
  14800. +2w --> two weeks from today
  14801. ++5 --> five days from default date
  14802. The function understands only English month and weekday abbreviations.
  14803. While prompting, a calendar is popped up - you can also select the
  14804. date with the mouse (button 1). The calendar shows a period of three
  14805. months. To scroll it to other months, use the keys `>' and `<'.
  14806. If you don't like the calendar, turn it off with
  14807. \(setq org-read-date-popup-calendar nil)
  14808. With optional argument TO-TIME, the date will immediately be converted
  14809. to an internal time.
  14810. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14811. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14812. still enter a time, and this function will inform the calling routine
  14813. about this change. The calling routine may then choose to change the
  14814. format used to insert the time stamp into the buffer to include the time.
  14815. With optional argument FROM-STRING, read from this string instead from
  14816. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14817. the time/date that is used for everything that is not specified by the
  14818. user."
  14819. (require 'parse-time)
  14820. (let* ((org-time-stamp-rounding-minutes
  14821. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14822. (org-dcst org-display-custom-times)
  14823. (ct (org-current-time))
  14824. (org-def (or org-overriding-default-time default-time ct))
  14825. (org-defdecode (decode-time org-def))
  14826. (dummy (progn
  14827. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14828. (setcar (nthcdr 2 org-defdecode) -1)
  14829. (setcar (nthcdr 1 org-defdecode) 59)
  14830. (setq org-def (apply 'encode-time org-defdecode)
  14831. org-defdecode (decode-time org-def)))))
  14832. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14833. (calendar-frame-setup nil)
  14834. (calendar-setup nil)
  14835. (calendar-move-hook nil)
  14836. (calendar-view-diary-initially-flag nil)
  14837. (calendar-view-holidays-initially-flag nil)
  14838. (timestr (format-time-string
  14839. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14840. (prompt (concat (if prompt (concat prompt " ") "")
  14841. (format "Date+time [%s]: " timestr)))
  14842. ans (org-ans0 "") org-ans1 org-ans2 final)
  14843. (cond
  14844. (from-string (setq ans from-string))
  14845. (org-read-date-popup-calendar
  14846. (save-excursion
  14847. (save-window-excursion
  14848. (calendar)
  14849. (org-eval-in-calendar '(setq cursor-type nil) t)
  14850. (unwind-protect
  14851. (progn
  14852. (calendar-forward-day (- (time-to-days org-def)
  14853. (calendar-absolute-from-gregorian
  14854. (calendar-current-date))))
  14855. (org-eval-in-calendar nil t)
  14856. (let* ((old-map (current-local-map))
  14857. (map (copy-keymap calendar-mode-map))
  14858. (minibuffer-local-map
  14859. (copy-keymap org-read-date-minibuffer-local-map)))
  14860. (org-defkey map (kbd "RET") 'org-calendar-select)
  14861. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14862. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14863. (unwind-protect
  14864. (progn
  14865. (use-local-map map)
  14866. (setq org-read-date-inactive inactive)
  14867. (add-hook 'post-command-hook 'org-read-date-display)
  14868. (setq org-ans0 (read-string prompt default-input
  14869. 'org-read-date-history nil))
  14870. ;; org-ans0: from prompt
  14871. ;; org-ans1: from mouse click
  14872. ;; org-ans2: from calendar motion
  14873. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14874. (remove-hook 'post-command-hook 'org-read-date-display)
  14875. (use-local-map old-map)
  14876. (when org-read-date-overlay
  14877. (delete-overlay org-read-date-overlay)
  14878. (setq org-read-date-overlay nil)))))
  14879. (bury-buffer "*Calendar*")))))
  14880. (t ; Naked prompt only
  14881. (unwind-protect
  14882. (setq ans (read-string prompt default-input
  14883. 'org-read-date-history timestr))
  14884. (when org-read-date-overlay
  14885. (delete-overlay org-read-date-overlay)
  14886. (setq org-read-date-overlay nil)))))
  14887. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14888. (when org-read-date-analyze-forced-year
  14889. (message "Year was forced into %s"
  14890. (if org-read-date-force-compatible-dates
  14891. "compatible range (1970-2037)"
  14892. "range representable on this machine"))
  14893. (ding))
  14894. ;; One round trip to get rid of 34th of August and stuff like that....
  14895. (setq final (decode-time (apply 'encode-time final)))
  14896. (setq org-read-date-final-answer ans)
  14897. (if to-time
  14898. (apply 'encode-time final)
  14899. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14900. (format "%04d-%02d-%02d %02d:%02d"
  14901. (nth 5 final) (nth 4 final) (nth 3 final)
  14902. (nth 2 final) (nth 1 final))
  14903. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14904. (defvar org-def)
  14905. (defvar org-defdecode)
  14906. (defvar org-with-time)
  14907. (defun org-read-date-display ()
  14908. "Display the current date prompt interpretation in the minibuffer."
  14909. (when org-read-date-display-live
  14910. (when org-read-date-overlay
  14911. (delete-overlay org-read-date-overlay))
  14912. (when (minibufferp (current-buffer))
  14913. (save-excursion
  14914. (end-of-line 1)
  14915. (while (not (equal (buffer-substring
  14916. (max (point-min) (- (point) 4)) (point))
  14917. " "))
  14918. (insert " ")))
  14919. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14920. " " (or org-ans1 org-ans2)))
  14921. (org-end-time-was-given nil)
  14922. (f (org-read-date-analyze ans org-def org-defdecode))
  14923. (fmts (if org-dcst
  14924. org-time-stamp-custom-formats
  14925. org-time-stamp-formats))
  14926. (fmt (if (or org-with-time
  14927. (and (boundp 'org-time-was-given) org-time-was-given))
  14928. (cdr fmts)
  14929. (car fmts)))
  14930. (txt (format-time-string fmt (apply 'encode-time f)))
  14931. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14932. (txt (concat "=> " txt)))
  14933. (when (and org-end-time-was-given
  14934. (string-match org-plain-time-of-day-regexp txt))
  14935. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14936. org-end-time-was-given
  14937. (substring txt (match-end 0)))))
  14938. (when org-read-date-analyze-futurep
  14939. (setq txt (concat txt " (=>F)")))
  14940. (setq org-read-date-overlay
  14941. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14942. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14943. (defun org-read-date-analyze (ans org-def org-defdecode)
  14944. "Analyze the combined answer of the date prompt."
  14945. ;; FIXME: cleanup and comment
  14946. (let ((nowdecode (decode-time (current-time)))
  14947. delta deltan deltaw deltadef year month day
  14948. hour minute second wday pm h2 m2 tl wday1
  14949. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14950. (setq org-read-date-analyze-futurep nil
  14951. org-read-date-analyze-forced-year nil)
  14952. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14953. (setq ans "+0"))
  14954. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14955. (setq ans (replace-match "" t t ans)
  14956. deltan (car delta)
  14957. deltaw (nth 1 delta)
  14958. deltadef (nth 2 delta)))
  14959. ;; Check if there is an iso week date in there. If yes, store the
  14960. ;; info and postpone interpreting it until the rest of the parsing
  14961. ;; is done.
  14962. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14963. (setq iso-year (if (match-end 1)
  14964. (org-small-year-to-year
  14965. (string-to-number (match-string 1 ans))))
  14966. iso-weekday (if (match-end 3)
  14967. (string-to-number (match-string 3 ans)))
  14968. iso-week (string-to-number (match-string 2 ans)))
  14969. (setq ans (replace-match "" t t ans)))
  14970. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14971. (when (string-match
  14972. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14973. (setq year (if (match-end 2)
  14974. (string-to-number (match-string 2 ans))
  14975. (progn (setq kill-year t)
  14976. (string-to-number (format-time-string "%Y"))))
  14977. month (string-to-number (match-string 3 ans))
  14978. day (string-to-number (match-string 4 ans)))
  14979. (if (< year 100) (setq year (+ 2000 year)))
  14980. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14981. t nil ans)))
  14982. ;; Help matching dotted european dates
  14983. (when (string-match
  14984. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14985. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14986. (setq kill-year t)
  14987. (string-to-number (format-time-string "%Y")))
  14988. day (string-to-number (match-string 1 ans))
  14989. month (string-to-number (match-string 2 ans))
  14990. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14991. t nil ans)))
  14992. ;; Help matching american dates, like 5/30 or 5/30/7
  14993. (when (string-match
  14994. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14995. (setq year (if (match-end 4)
  14996. (string-to-number (match-string 4 ans))
  14997. (progn (setq kill-year t)
  14998. (string-to-number (format-time-string "%Y"))))
  14999. month (string-to-number (match-string 1 ans))
  15000. day (string-to-number (match-string 2 ans)))
  15001. (if (< year 100) (setq year (+ 2000 year)))
  15002. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15003. t nil ans)))
  15004. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15005. ;; If there is a time with am/pm, and *no* time without it, we convert
  15006. ;; so that matching will be successful.
  15007. (loop for i from 1 to 2 do ; twice, for end time as well
  15008. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15009. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15010. (setq hour (string-to-number (match-string 1 ans))
  15011. minute (if (match-end 3)
  15012. (string-to-number (match-string 3 ans))
  15013. 0)
  15014. pm (equal ?p
  15015. (string-to-char (downcase (match-string 4 ans)))))
  15016. (if (and (= hour 12) (not pm))
  15017. (setq hour 0)
  15018. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15019. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15020. t t ans))))
  15021. ;; Check if a time range is given as a duration
  15022. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15023. (setq hour (string-to-number (match-string 1 ans))
  15024. h2 (+ hour (string-to-number (match-string 3 ans)))
  15025. minute (string-to-number (match-string 2 ans))
  15026. m2 (+ minute (if (match-end 5) (string-to-number
  15027. (match-string 5 ans))0)))
  15028. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15029. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15030. t t ans)))
  15031. ;; Check if there is a time range
  15032. (when (boundp 'org-end-time-was-given)
  15033. (setq org-time-was-given nil)
  15034. (when (and (string-match org-plain-time-of-day-regexp ans)
  15035. (match-end 8))
  15036. (setq org-end-time-was-given (match-string 8 ans))
  15037. (setq ans (concat (substring ans 0 (match-beginning 7))
  15038. (substring ans (match-end 7))))))
  15039. (setq tl (parse-time-string ans)
  15040. day (or (nth 3 tl) (nth 3 org-defdecode))
  15041. month (or (nth 4 tl)
  15042. (if (and org-read-date-prefer-future
  15043. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15044. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15045. (nth 4 org-defdecode)))
  15046. year (or (and (not kill-year) (nth 5 tl))
  15047. (if (and org-read-date-prefer-future
  15048. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15049. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15050. (nth 5 org-defdecode)))
  15051. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15052. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15053. second (or (nth 0 tl) 0)
  15054. wday (nth 6 tl))
  15055. (when (and (eq org-read-date-prefer-future 'time)
  15056. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15057. (equal day (nth 3 nowdecode))
  15058. (equal month (nth 4 nowdecode))
  15059. (equal year (nth 5 nowdecode))
  15060. (nth 2 tl)
  15061. (or (< (nth 2 tl) (nth 2 nowdecode))
  15062. (and (= (nth 2 tl) (nth 2 nowdecode))
  15063. (nth 1 tl)
  15064. (< (nth 1 tl) (nth 1 nowdecode)))))
  15065. (setq day (1+ day)
  15066. futurep t))
  15067. ;; Special date definitions below
  15068. (cond
  15069. (iso-week
  15070. ;; There was an iso week
  15071. (require 'cal-iso)
  15072. (setq futurep nil)
  15073. (setq year (or iso-year year)
  15074. day (or iso-weekday wday 1)
  15075. wday nil ; to make sure that the trigger below does not match
  15076. iso-date (calendar-gregorian-from-absolute
  15077. (calendar-absolute-from-iso
  15078. (list iso-week day year))))
  15079. ; FIXME: Should we also push ISO weeks into the future?
  15080. ; (when (and org-read-date-prefer-future
  15081. ; (not iso-year)
  15082. ; (< (calendar-absolute-from-gregorian iso-date)
  15083. ; (time-to-days (current-time))))
  15084. ; (setq year (1+ year)
  15085. ; iso-date (calendar-gregorian-from-absolute
  15086. ; (calendar-absolute-from-iso
  15087. ; (list iso-week day year)))))
  15088. (setq month (car iso-date)
  15089. year (nth 2 iso-date)
  15090. day (nth 1 iso-date)))
  15091. (deltan
  15092. (setq futurep nil)
  15093. (unless deltadef
  15094. (let ((now (decode-time (current-time))))
  15095. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15096. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15097. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15098. ((equal deltaw "m") (setq month (+ month deltan)))
  15099. ((equal deltaw "y") (setq year (+ year deltan)))))
  15100. ((and wday (not (nth 3 tl)))
  15101. ;; Weekday was given, but no day, so pick that day in the week
  15102. ;; on or after the derived date.
  15103. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15104. (unless (equal wday wday1)
  15105. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15106. (if (and (boundp 'org-time-was-given)
  15107. (nth 2 tl))
  15108. (setq org-time-was-given t))
  15109. (if (< year 100) (setq year (+ 2000 year)))
  15110. ;; Check of the date is representable
  15111. (if org-read-date-force-compatible-dates
  15112. (progn
  15113. (if (< year 1970)
  15114. (setq year 1970 org-read-date-analyze-forced-year t))
  15115. (if (> year 2037)
  15116. (setq year 2037 org-read-date-analyze-forced-year t)))
  15117. (condition-case nil
  15118. (ignore (encode-time second minute hour day month year))
  15119. (error
  15120. (setq year (nth 5 org-defdecode))
  15121. (setq org-read-date-analyze-forced-year t))))
  15122. (setq org-read-date-analyze-futurep futurep)
  15123. (list second minute hour day month year)))
  15124. (defvar parse-time-weekdays)
  15125. (defun org-read-date-get-relative (s today default)
  15126. "Check string S for special relative date string.
  15127. TODAY and DEFAULT are internal times, for today and for a default.
  15128. Return shift list (N what def-flag)
  15129. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15130. N is the number of WHATs to shift.
  15131. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15132. the DEFAULT date rather than TODAY."
  15133. (require 'parse-time)
  15134. (when (and
  15135. (string-match
  15136. (concat
  15137. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15138. "\\([0-9]+\\)?"
  15139. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15140. "\\([ \t]\\|$\\)") s)
  15141. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15142. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15143. (string-to-char (substring (match-string 1 s) -1))
  15144. ?+))
  15145. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15146. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15147. (what (if (match-end 3) (match-string 3 s) "d"))
  15148. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15149. (date (if rel default today))
  15150. (wday (nth 6 (decode-time date)))
  15151. delta)
  15152. (if wday1
  15153. (progn
  15154. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15155. (if (= delta 0) (setq delta 7))
  15156. (if (= dir ?-)
  15157. (progn
  15158. (setq delta (- delta 7))
  15159. (if (= delta 0) (setq delta -7))))
  15160. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15161. (list delta "d" rel))
  15162. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15163. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15164. "Turn a user-specified date into the internal representation.
  15165. The internal representation needed by the calendar is (month day year).
  15166. This is a wrapper to handle the brain-dead convention in calendar that
  15167. user function argument order change dependent on argument order."
  15168. (if (boundp 'calendar-date-style)
  15169. (cond
  15170. ((eq calendar-date-style 'american)
  15171. (list arg1 arg2 arg3))
  15172. ((eq calendar-date-style 'european)
  15173. (list arg2 arg1 arg3))
  15174. ((eq calendar-date-style 'iso)
  15175. (list arg2 arg3 arg1)))
  15176. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15177. (if (org-bound-and-true-p european-calendar-style)
  15178. (list arg2 arg1 arg3)
  15179. (list arg1 arg2 arg3)))))
  15180. (defun org-eval-in-calendar (form &optional keepdate)
  15181. "Eval FORM in the calendar window and return to current window.
  15182. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15183. otherwise stick to the current value of `org-ans2'."
  15184. (let ((sf (selected-frame))
  15185. (sw (selected-window)))
  15186. (select-window (get-buffer-window "*Calendar*" t))
  15187. (eval form)
  15188. (when (and (not keepdate) (calendar-cursor-to-date))
  15189. (let* ((date (calendar-cursor-to-date))
  15190. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15191. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15192. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15193. (select-window sw)
  15194. (org-select-frame-set-input-focus sf)))
  15195. (defun org-calendar-select ()
  15196. "Return to `org-read-date' with the date currently selected.
  15197. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15198. (interactive)
  15199. (when (calendar-cursor-to-date)
  15200. (let* ((date (calendar-cursor-to-date))
  15201. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15202. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15203. (if (active-minibuffer-window) (exit-minibuffer))))
  15204. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15205. "Insert a date stamp for the date given by the internal TIME.
  15206. WITH-HM means use the stamp format that includes the time of the day.
  15207. INACTIVE means use square brackets instead of angular ones, so that the
  15208. stamp will not contribute to the agenda.
  15209. PRE and POST are optional strings to be inserted before and after the
  15210. stamp.
  15211. The command returns the inserted time stamp."
  15212. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15213. stamp)
  15214. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15215. (insert-before-markers (or pre ""))
  15216. (when (listp extra)
  15217. (setq extra (car extra))
  15218. (if (and (stringp extra)
  15219. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15220. (setq extra (format "-%02d:%02d"
  15221. (string-to-number (match-string 1 extra))
  15222. (string-to-number (match-string 2 extra))))
  15223. (setq extra nil)))
  15224. (when extra
  15225. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15226. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15227. (insert-before-markers (or post ""))
  15228. (setq org-last-inserted-timestamp stamp)))
  15229. (defun org-toggle-time-stamp-overlays ()
  15230. "Toggle the use of custom time stamp formats."
  15231. (interactive)
  15232. (setq org-display-custom-times (not org-display-custom-times))
  15233. (unless org-display-custom-times
  15234. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15235. (while (setq p (next-single-property-change p 'display))
  15236. (if (and (get-text-property p 'display)
  15237. (eq (get-text-property p 'face) 'org-date))
  15238. (remove-text-properties
  15239. p (setq p (next-single-property-change p 'display))
  15240. '(display t))))
  15241. (set-buffer-modified-p bmp)))
  15242. (if (featurep 'xemacs)
  15243. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15244. (org-restart-font-lock)
  15245. (setq org-table-may-need-update t)
  15246. (if org-display-custom-times
  15247. (message "Time stamps are overlaid with custom format")
  15248. (message "Time stamp overlays removed")))
  15249. (defun org-display-custom-time (beg end)
  15250. "Overlay modified time stamp format over timestamp between BEG and END."
  15251. (let* ((ts (buffer-substring beg end))
  15252. t1 w1 with-hm tf time str w2 (off 0))
  15253. (save-match-data
  15254. (setq t1 (org-parse-time-string ts t))
  15255. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15256. (setq off (- (match-end 0) (match-beginning 0)))))
  15257. (setq end (- end off))
  15258. (setq w1 (- end beg)
  15259. with-hm (and (nth 1 t1) (nth 2 t1))
  15260. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15261. time (org-fix-decoded-time t1)
  15262. str (org-add-props
  15263. (format-time-string
  15264. (substring tf 1 -1) (apply 'encode-time time))
  15265. nil 'mouse-face 'highlight)
  15266. w2 (length str))
  15267. (if (not (= w2 w1))
  15268. (add-text-properties (1+ beg) (+ 2 beg)
  15269. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15270. (if (featurep 'xemacs)
  15271. (progn
  15272. (put-text-property beg end 'invisible t)
  15273. (put-text-property beg end 'end-glyph (make-glyph str)))
  15274. (put-text-property beg end 'display str))))
  15275. (defun org-translate-time (string)
  15276. "Translate all timestamps in STRING to custom format.
  15277. But do this only if the variable `org-display-custom-times' is set."
  15278. (when org-display-custom-times
  15279. (save-match-data
  15280. (let* ((start 0)
  15281. (re org-ts-regexp-both)
  15282. t1 with-hm inactive tf time str beg end)
  15283. (while (setq start (string-match re string start))
  15284. (setq beg (match-beginning 0)
  15285. end (match-end 0)
  15286. t1 (save-match-data
  15287. (org-parse-time-string (substring string beg end) t))
  15288. with-hm (and (nth 1 t1) (nth 2 t1))
  15289. inactive (equal (substring string beg (1+ beg)) "[")
  15290. tf (funcall (if with-hm 'cdr 'car)
  15291. org-time-stamp-custom-formats)
  15292. time (org-fix-decoded-time t1)
  15293. str (format-time-string
  15294. (concat
  15295. (if inactive "[" "<") (substring tf 1 -1)
  15296. (if inactive "]" ">"))
  15297. (apply 'encode-time time))
  15298. string (replace-match str t t string)
  15299. start (+ start (length str)))))))
  15300. string)
  15301. (defun org-fix-decoded-time (time)
  15302. "Set 0 instead of nil for the first 6 elements of time.
  15303. Don't touch the rest."
  15304. (let ((n 0))
  15305. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15306. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15307. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15308. "Difference between TIMESTAMP-STRING and now in days.
  15309. If SECONDS is non-nil, return the difference in seconds."
  15310. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15311. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15312. (funcall fdiff (current-time)))))
  15313. (defun org-deadline-close (timestamp-string &optional ndays)
  15314. "Is the time in TIMESTAMP-STRING close to the current date?"
  15315. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15316. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15317. (not (org-entry-is-done-p))))
  15318. (defun org-get-wdays (ts &optional delay zero-delay)
  15319. "Get the deadline lead time appropriate for timestring TS.
  15320. When DELAY is non-nil, get the delay time for scheduled items
  15321. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15322. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15323. don't try to find the delay cookie in the scheduled timestamp."
  15324. (let ((tv (if delay org-scheduled-delay-days
  15325. org-deadline-warning-days)))
  15326. (cond
  15327. ((or (and delay (< tv 0))
  15328. (and delay zero-delay (<= tv 0))
  15329. (and (not delay) (<= tv 0)))
  15330. ;; Enforce this value no matter what
  15331. (- tv))
  15332. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15333. ;; lead time is specified.
  15334. (floor (* (string-to-number (match-string 1 ts))
  15335. (cdr (assoc (match-string 2 ts)
  15336. '(("d" . 1) ("w" . 7)
  15337. ("m" . 30.4) ("y" . 365.25)
  15338. ("h" . 0.041667)))))))
  15339. ;; go for the default.
  15340. (t tv))))
  15341. (defun org-calendar-select-mouse (ev)
  15342. "Return to `org-read-date' with the date currently selected.
  15343. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15344. (interactive "e")
  15345. (mouse-set-point ev)
  15346. (when (calendar-cursor-to-date)
  15347. (let* ((date (calendar-cursor-to-date))
  15348. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15349. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15350. (if (active-minibuffer-window) (exit-minibuffer))))
  15351. (defun org-check-deadlines (ndays)
  15352. "Check if there are any deadlines due or past due.
  15353. A deadline is considered due if it happens within `org-deadline-warning-days'
  15354. days from today's date. If the deadline appears in an entry marked DONE,
  15355. it is not shown. The prefix arg NDAYS can be used to test that many
  15356. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15357. (interactive "P")
  15358. (let* ((org-warn-days
  15359. (cond
  15360. ((equal ndays '(4)) 100000)
  15361. (ndays (prefix-numeric-value ndays))
  15362. (t (abs org-deadline-warning-days))))
  15363. (case-fold-search nil)
  15364. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15365. (callback
  15366. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15367. (message "%d deadlines past-due or due within %d days"
  15368. (org-occur regexp nil callback)
  15369. org-warn-days)))
  15370. (defsubst org-re-timestamp (type)
  15371. "Return a regexp for timestamp TYPE.
  15372. Allowed values for TYPE are:
  15373. all: all timestamps
  15374. active: only active timestamps (<...>)
  15375. inactive: only inactive timestamps ([...])
  15376. scheduled: only scheduled timestamps
  15377. deadline: only deadline timestamps
  15378. closed: only closed time-stamps
  15379. When TYPE is nil, fall back on returning a regexp that matches
  15380. both scheduled and deadline timestamps."
  15381. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15382. ((eq type 'active) org-ts-regexp)
  15383. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15384. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15385. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15386. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15387. ((eq type 'scheduled-or-deadline)
  15388. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15389. (defun org-check-before-date (date)
  15390. "Check if there are deadlines or scheduled entries before DATE."
  15391. (interactive (list (org-read-date)))
  15392. (let ((case-fold-search nil)
  15393. (regexp (org-re-timestamp org-ts-type))
  15394. (callback
  15395. (lambda () (time-less-p
  15396. (org-time-string-to-time (match-string 1))
  15397. (org-time-string-to-time date)))))
  15398. (message "%d entries before %s"
  15399. (org-occur regexp nil callback) date)))
  15400. (defun org-check-after-date (date)
  15401. "Check if there are deadlines or scheduled entries after DATE."
  15402. (interactive (list (org-read-date)))
  15403. (let ((case-fold-search nil)
  15404. (regexp (org-re-timestamp org-ts-type))
  15405. (callback
  15406. (lambda () (not
  15407. (time-less-p
  15408. (org-time-string-to-time (match-string 1))
  15409. (org-time-string-to-time date))))))
  15410. (message "%d entries after %s"
  15411. (org-occur regexp nil callback) date)))
  15412. (defun org-check-dates-range (start-date end-date)
  15413. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15414. (interactive (list (org-read-date nil nil nil "Range starts")
  15415. (org-read-date nil nil nil "Range end")))
  15416. (let ((case-fold-search nil)
  15417. (regexp (org-re-timestamp org-ts-type))
  15418. (callback
  15419. (lambda ()
  15420. (let ((match (match-string 1)))
  15421. (and
  15422. (not (time-less-p
  15423. (org-time-string-to-time match)
  15424. (org-time-string-to-time start-date)))
  15425. (time-less-p
  15426. (org-time-string-to-time match)
  15427. (org-time-string-to-time end-date)))))))
  15428. (message "%d entries between %s and %s"
  15429. (org-occur regexp nil callback) start-date end-date)))
  15430. (defun org-evaluate-time-range (&optional to-buffer)
  15431. "Evaluate a time range by computing the difference between start and end.
  15432. Normally the result is just printed in the echo area, but with prefix arg
  15433. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15434. If the time range is actually in a table, the result is inserted into the
  15435. next column.
  15436. For time difference computation, a year is assumed to be exactly 365
  15437. days in order to avoid rounding problems."
  15438. (interactive "P")
  15439. (or
  15440. (org-clock-update-time-maybe)
  15441. (save-excursion
  15442. (unless (org-at-date-range-p t)
  15443. (goto-char (point-at-bol))
  15444. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15445. (if (not (org-at-date-range-p t))
  15446. (user-error "Not at a time-stamp range, and none found in current line")))
  15447. (let* ((ts1 (match-string 1))
  15448. (ts2 (match-string 2))
  15449. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15450. (match-end (match-end 0))
  15451. (time1 (org-time-string-to-time ts1))
  15452. (time2 (org-time-string-to-time ts2))
  15453. (t1 (org-float-time time1))
  15454. (t2 (org-float-time time2))
  15455. (diff (abs (- t2 t1)))
  15456. (negative (< (- t2 t1) 0))
  15457. ;; (ys (floor (* 365 24 60 60)))
  15458. (ds (* 24 60 60))
  15459. (hs (* 60 60))
  15460. (fy "%dy %dd %02d:%02d")
  15461. (fy1 "%dy %dd")
  15462. (fd "%dd %02d:%02d")
  15463. (fd1 "%dd")
  15464. (fh "%02d:%02d")
  15465. y d h m align)
  15466. (if havetime
  15467. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15468. y 0
  15469. d (floor (/ diff ds)) diff (mod diff ds)
  15470. h (floor (/ diff hs)) diff (mod diff hs)
  15471. m (floor (/ diff 60)))
  15472. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15473. y 0
  15474. d (floor (+ (/ diff ds) 0.5))
  15475. h 0 m 0))
  15476. (if (not to-buffer)
  15477. (message "%s" (org-make-tdiff-string y d h m))
  15478. (if (org-at-table-p)
  15479. (progn
  15480. (goto-char match-end)
  15481. (setq align t)
  15482. (and (looking-at " *|") (goto-char (match-end 0))))
  15483. (goto-char match-end))
  15484. (if (looking-at
  15485. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15486. (replace-match ""))
  15487. (if negative (insert " -"))
  15488. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15489. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15490. (insert " " (format fh h m))))
  15491. (if align (org-table-align))
  15492. (message "Time difference inserted")))))
  15493. (defun org-make-tdiff-string (y d h m)
  15494. (let ((fmt "")
  15495. (l nil))
  15496. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15497. l (push y l)))
  15498. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15499. l (push d l)))
  15500. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15501. l (push h l)))
  15502. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15503. l (push m l)))
  15504. (apply 'format fmt (nreverse l))))
  15505. (defun org-time-string-to-time (s &optional buffer pos)
  15506. "Convert a timestamp string into internal time."
  15507. (condition-case errdata
  15508. (apply 'encode-time (org-parse-time-string s))
  15509. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15510. s (if (not (and buffer pos))
  15511. ""
  15512. (format " at %d in buffer `%s'" pos buffer))
  15513. (cdr errdata)))))
  15514. (defun org-time-string-to-seconds (s)
  15515. "Convert a timestamp string to a number of seconds."
  15516. (org-float-time (org-time-string-to-time s)))
  15517. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15518. "Convert a time stamp to an absolute day number.
  15519. If there is a specifier for a cyclic time stamp, get the closest
  15520. date to DAYNR.
  15521. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15522. The variable `date' is bound by the calendar when this is called."
  15523. (cond
  15524. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15525. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15526. daynr
  15527. (+ daynr 1000)))
  15528. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15529. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15530. (time-to-days (current-time))) (match-string 0 s)
  15531. prefer show-all))
  15532. (t (time-to-days
  15533. (condition-case errdata
  15534. (apply 'encode-time (org-parse-time-string s))
  15535. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15536. s (if (not (and buffer pos))
  15537. ""
  15538. (format " at %d in buffer `%s'" pos buffer))
  15539. (cdr errdata))))))))
  15540. (defun org-days-to-iso-week (days)
  15541. "Return the iso week number."
  15542. (require 'cal-iso)
  15543. (car (calendar-iso-from-absolute days)))
  15544. (defun org-small-year-to-year (year)
  15545. "Convert 2-digit years into 4-digit years.
  15546. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2037.
  15547. The year 2000 cannot be abbreviated. Any year larger than 99
  15548. is returned unchanged."
  15549. (if (< year 38)
  15550. (setq year (+ 2000 year))
  15551. (if (< year 100)
  15552. (setq year (+ 1900 year))))
  15553. year)
  15554. (defun org-time-from-absolute (d)
  15555. "Return the time corresponding to date D.
  15556. D may be an absolute day number, or a calendar-type list (month day year)."
  15557. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15558. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15559. (defun org-calendar-holiday ()
  15560. "List of holidays, for Diary display in Org-mode."
  15561. (require 'holidays)
  15562. (let ((hl (funcall
  15563. (if (fboundp 'calendar-check-holidays)
  15564. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15565. (if hl (mapconcat 'identity hl "; "))))
  15566. (defun org-diary-sexp-entry (sexp entry date)
  15567. "Process a SEXP diary ENTRY for DATE."
  15568. (require 'diary-lib)
  15569. (let ((result (if calendar-debug-sexp
  15570. (let ((stack-trace-on-error t))
  15571. (eval (car (read-from-string sexp))))
  15572. (condition-case nil
  15573. (eval (car (read-from-string sexp)))
  15574. (error
  15575. (beep)
  15576. (message "Bad sexp at line %d in %s: %s"
  15577. (org-current-line)
  15578. (buffer-file-name) sexp)
  15579. (sleep-for 2))))))
  15580. (cond ((stringp result) (split-string result "; "))
  15581. ((and (consp result)
  15582. (not (consp (cdr result)))
  15583. (stringp (cdr result))) (cdr result))
  15584. ((and (consp result)
  15585. (stringp (car result))) result)
  15586. (result entry))))
  15587. (defun org-diary-to-ical-string (frombuf)
  15588. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15589. This uses the icalendar.el library."
  15590. (let* ((tmpdir (if (featurep 'xemacs)
  15591. (temp-directory)
  15592. temporary-file-directory))
  15593. (tmpfile (make-temp-name
  15594. (expand-file-name "orgics" tmpdir)))
  15595. buf rtn b e)
  15596. (with-current-buffer frombuf
  15597. (icalendar-export-region (point-min) (point-max) tmpfile)
  15598. (setq buf (find-buffer-visiting tmpfile))
  15599. (set-buffer buf)
  15600. (goto-char (point-min))
  15601. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15602. (setq b (match-beginning 0)))
  15603. (goto-char (point-max))
  15604. (if (re-search-backward "^END:VEVENT" nil t)
  15605. (setq e (match-end 0)))
  15606. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15607. (kill-buffer buf)
  15608. (delete-file tmpfile)
  15609. rtn))
  15610. (defun org-closest-date (start current change prefer show-all)
  15611. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15612. When PREFER is `past', return a date that is either CURRENT or past.
  15613. When PREFER is `future', return a date that is either CURRENT or future.
  15614. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15615. ;; Make the proper lists from the dates
  15616. (catch 'exit
  15617. (let ((a1 '(("h" . hour)
  15618. ("d" . day)
  15619. ("w" . week)
  15620. ("m" . month)
  15621. ("y" . year)))
  15622. (shour (nth 2 (org-parse-time-string start)))
  15623. dn dw sday cday n1 n2 n0
  15624. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15625. (setq start (org-date-to-gregorian start)
  15626. current (org-date-to-gregorian
  15627. (if show-all
  15628. current
  15629. (time-to-days (current-time))))
  15630. sday (calendar-absolute-from-gregorian start)
  15631. cday (calendar-absolute-from-gregorian current))
  15632. (if (<= cday sday) (throw 'exit sday))
  15633. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15634. (setq dn (string-to-number (match-string 1 change))
  15635. dw (cdr (assoc (match-string 2 change) a1)))
  15636. (user-error "Invalid change specifier: %s" change))
  15637. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15638. (cond
  15639. ((eq dw 'hour)
  15640. (let ((missing-hours
  15641. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15642. dn)))
  15643. (setq n1 (if (zerop missing-hours) cday
  15644. (- cday (1+ (floor (/ missing-hours 24)))))
  15645. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15646. ((eq dw 'day)
  15647. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15648. n2 (+ n1 dn)))
  15649. ((eq dw 'year)
  15650. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15651. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15652. (setq date1 (list m d y1)
  15653. n1 (calendar-absolute-from-gregorian date1)
  15654. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15655. n2 (calendar-absolute-from-gregorian date2)))
  15656. ((eq dw 'month)
  15657. ;; approx number of month between the two dates
  15658. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15659. ;; How often does dn fit in there?
  15660. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15661. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15662. m (+ m nm)
  15663. ny (floor (/ m 12))
  15664. y (+ y ny)
  15665. m (- m (* ny 12)))
  15666. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15667. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15668. (setq m2 (+ m dn) y2 y)
  15669. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15670. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15671. (while (<= n2 cday)
  15672. (setq n1 n2 m m2 y y2)
  15673. (setq m2 (+ m dn) y2 y)
  15674. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15675. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15676. ;; Make sure n1 is the earlier date
  15677. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15678. (if show-all
  15679. (cond
  15680. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15681. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15682. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15683. (cond
  15684. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15685. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15686. (t (if (= cday n1) n1 n2)))))))
  15687. (defun org-date-to-gregorian (date)
  15688. "Turn any specification of DATE into a Gregorian date for the calendar."
  15689. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15690. ((and (listp date) (= (length date) 3)) date)
  15691. ((stringp date)
  15692. (setq date (org-parse-time-string date))
  15693. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15694. ((listp date)
  15695. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15696. (defun org-parse-time-string (s &optional nodefault)
  15697. "Parse the standard Org-mode time string.
  15698. This should be a lot faster than the normal `parse-time-string'.
  15699. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15700. hour and minute fields will be nil if not given."
  15701. (cond ((string-match org-ts-regexp0 s)
  15702. (list 0
  15703. (if (or (match-beginning 8) (not nodefault))
  15704. (string-to-number (or (match-string 8 s) "0")))
  15705. (if (or (match-beginning 7) (not nodefault))
  15706. (string-to-number (or (match-string 7 s) "0")))
  15707. (string-to-number (match-string 4 s))
  15708. (string-to-number (match-string 3 s))
  15709. (string-to-number (match-string 2 s))
  15710. nil nil nil))
  15711. ((string-match "^<[^>]+>$" s)
  15712. (decode-time (seconds-to-time (org-matcher-time s))))
  15713. (t (error "Not a standard Org-mode time string: %s" s))))
  15714. (defun org-timestamp-up (&optional arg)
  15715. "Increase the date item at the cursor by one.
  15716. If the cursor is on the year, change the year. If it is on the month,
  15717. the day or the time, change that.
  15718. With prefix ARG, change by that many units."
  15719. (interactive "p")
  15720. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15721. (defun org-timestamp-down (&optional arg)
  15722. "Decrease the date item at the cursor by one.
  15723. If the cursor is on the year, change the year. If it is on the month,
  15724. the day or the time, change that.
  15725. With prefix ARG, change by that many units."
  15726. (interactive "p")
  15727. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15728. (defun org-timestamp-up-day (&optional arg)
  15729. "Increase the date in the time stamp by one day.
  15730. With prefix ARG, change that many days."
  15731. (interactive "p")
  15732. (if (and (not (org-at-timestamp-p t))
  15733. (org-at-heading-p))
  15734. (org-todo 'up)
  15735. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15736. (defun org-timestamp-down-day (&optional arg)
  15737. "Decrease the date in the time stamp by one day.
  15738. With prefix ARG, change that many days."
  15739. (interactive "p")
  15740. (if (and (not (org-at-timestamp-p t))
  15741. (org-at-heading-p))
  15742. (org-todo 'down)
  15743. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15744. (defun org-at-timestamp-p (&optional inactive-ok)
  15745. "Determine if the cursor is in or at a timestamp."
  15746. (interactive)
  15747. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15748. (pos (point))
  15749. (ans (or (looking-at tsr)
  15750. (save-excursion
  15751. (skip-chars-backward "^[<\n\r\t")
  15752. (if (> (point) (point-min)) (backward-char 1))
  15753. (and (looking-at tsr)
  15754. (> (- (match-end 0) pos) -1))))))
  15755. (and ans
  15756. (boundp 'org-ts-what)
  15757. (setq org-ts-what
  15758. (cond
  15759. ((= pos (match-beginning 0)) 'bracket)
  15760. ;; Point is considered to be "on the bracket" whether
  15761. ;; it's really on it or right after it.
  15762. ((= pos (1- (match-end 0))) 'bracket)
  15763. ((= pos (match-end 0)) 'after)
  15764. ((org-pos-in-match-range pos 2) 'year)
  15765. ((org-pos-in-match-range pos 3) 'month)
  15766. ((org-pos-in-match-range pos 7) 'hour)
  15767. ((org-pos-in-match-range pos 8) 'minute)
  15768. ((or (org-pos-in-match-range pos 4)
  15769. (org-pos-in-match-range pos 5)) 'day)
  15770. ((and (> pos (or (match-end 8) (match-end 5)))
  15771. (< pos (match-end 0)))
  15772. (- pos (or (match-end 8) (match-end 5))))
  15773. (t 'day))))
  15774. ans))
  15775. (defun org-toggle-timestamp-type ()
  15776. "Toggle the type (<active> or [inactive]) of a time stamp."
  15777. (interactive)
  15778. (when (org-at-timestamp-p t)
  15779. (let ((beg (match-beginning 0)) (end (match-end 0))
  15780. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15781. (save-excursion
  15782. (goto-char beg)
  15783. (while (re-search-forward "[][<>]" end t)
  15784. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15785. t t)))
  15786. (message "Timestamp is now %sactive"
  15787. (if (equal (char-after beg) ?<) "" "in")))))
  15788. (defun org-at-clock-log-p nil
  15789. "Is the cursor on the clock log line?"
  15790. (save-excursion
  15791. (move-beginning-of-line 1)
  15792. (looking-at "^[ \t]*CLOCK:")))
  15793. (defvar org-clock-history) ; defined in org-clock.el
  15794. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15795. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15796. "Change the date in the time stamp at point.
  15797. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15798. `year', `minute', `second'. If WHAT is not given, the cursor position
  15799. in the timestamp determines what will be changed.
  15800. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15801. (let ((origin (point)) origin-cat
  15802. with-hm inactive
  15803. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15804. org-ts-what
  15805. extra rem
  15806. ts time time0 fixnext clrgx)
  15807. (if (not (org-at-timestamp-p t))
  15808. (user-error "Not at a timestamp"))
  15809. (if (and (not what) (eq org-ts-what 'bracket))
  15810. (org-toggle-timestamp-type)
  15811. ;; Point isn't on brackets. Remember the part of the time-stamp
  15812. ;; the point was in. Indeed, size of time-stamps may change,
  15813. ;; but point must be kept in the same category nonetheless.
  15814. (setq origin-cat org-ts-what)
  15815. (if (and (not what) (not (eq org-ts-what 'day))
  15816. org-display-custom-times
  15817. (get-text-property (point) 'display)
  15818. (not (get-text-property (1- (point)) 'display)))
  15819. (setq org-ts-what 'day))
  15820. (setq org-ts-what (or what org-ts-what)
  15821. inactive (= (char-after (match-beginning 0)) ?\[)
  15822. ts (match-string 0))
  15823. (replace-match "")
  15824. (when (string-match
  15825. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15826. ts)
  15827. (setq extra (match-string 1 ts))
  15828. (if suppress-tmp-delay
  15829. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15830. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15831. (setq with-hm t))
  15832. (setq time0 (org-parse-time-string ts))
  15833. (when (and updown
  15834. (eq org-ts-what 'minute)
  15835. (not current-prefix-arg))
  15836. ;; This looks like s-up and s-down. Change by one rounding step.
  15837. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15838. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15839. (setcar (cdr time0) (+ (nth 1 time0)
  15840. (if (> n 0) (- rem) (- dm rem))))))
  15841. (setq time
  15842. (encode-time (or (car time0) 0)
  15843. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15844. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15845. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15846. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15847. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15848. (nthcdr 6 time0)))
  15849. (when (and (member org-ts-what '(hour minute))
  15850. extra
  15851. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15852. (setq extra (org-modify-ts-extra
  15853. extra
  15854. (if (eq org-ts-what 'hour) 2 5)
  15855. n dm)))
  15856. (when (integerp org-ts-what)
  15857. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15858. (if (eq what 'calendar)
  15859. (let ((cal-date (org-get-date-from-calendar)))
  15860. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15861. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15862. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15863. (setcar time0 (or (car time0) 0))
  15864. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15865. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15866. (setq time (apply 'encode-time time0))))
  15867. ;; Insert the new time-stamp, and ensure point stays in the same
  15868. ;; category as before (i.e. not after the last position in that
  15869. ;; category).
  15870. (let ((pos (point)))
  15871. ;; Stay before inserted string. `save-excursion' is of no use.
  15872. (setq org-last-changed-timestamp
  15873. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15874. (goto-char pos))
  15875. (save-match-data
  15876. (looking-at org-ts-regexp3)
  15877. (goto-char (cond
  15878. ;; `day' category ends before `hour' if any, or at
  15879. ;; the end of the day name.
  15880. ((eq origin-cat 'day)
  15881. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15882. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15883. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15884. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15885. ;; `year' and `month' have both fixed size: point
  15886. ;; couldn't have moved into another part.
  15887. (t origin))))
  15888. ;; Update clock if on a CLOCK line.
  15889. (org-clock-update-time-maybe)
  15890. ;; Maybe adjust the closest clock in `org-clock-history'
  15891. (when org-clock-adjust-closest
  15892. (if (not (and (org-at-clock-log-p)
  15893. (< 1 (length (delq nil (mapcar 'marker-position
  15894. org-clock-history))))))
  15895. (message "No clock to adjust")
  15896. (cond ((save-excursion ; fix previous clock?
  15897. (re-search-backward org-ts-regexp0 nil t)
  15898. (org-looking-back (concat org-clock-string " \\[")))
  15899. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15900. ((save-excursion ; fix next clock?
  15901. (re-search-backward org-ts-regexp0 nil t)
  15902. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15903. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15904. (save-window-excursion
  15905. ;; Find closest clock to point, adjust the previous/next one in history
  15906. (let* ((p (save-excursion (org-back-to-heading t)))
  15907. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15908. (clfixnth
  15909. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15910. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15911. (if (not clfixpos)
  15912. (message "No clock to adjust")
  15913. (save-excursion
  15914. (org-goto-marker-or-bmk clfixpos)
  15915. (org-show-subtree)
  15916. (when (re-search-forward clrgx nil t)
  15917. (goto-char (match-beginning 1))
  15918. (let (org-clock-adjust-closest)
  15919. (org-timestamp-change n org-ts-what updown))
  15920. (message "Clock adjusted in %s for heading: %s"
  15921. (file-name-nondirectory (buffer-file-name))
  15922. (org-get-heading t t)))))))))
  15923. ;; Try to recenter the calendar window, if any.
  15924. (if (and org-calendar-follow-timestamp-change
  15925. (get-buffer-window "*Calendar*" t)
  15926. (memq org-ts-what '(day month year)))
  15927. (org-recenter-calendar (time-to-days time))))))
  15928. (defun org-modify-ts-extra (s pos n dm)
  15929. "Change the different parts of the lead-time and repeat fields in timestamp."
  15930. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15931. ng h m new rem)
  15932. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15933. (cond
  15934. ((or (org-pos-in-match-range pos 2)
  15935. (org-pos-in-match-range pos 3))
  15936. (setq m (string-to-number (match-string 3 s))
  15937. h (string-to-number (match-string 2 s)))
  15938. (if (org-pos-in-match-range pos 2)
  15939. (setq h (+ h n))
  15940. (setq n (* dm (org-no-warnings (signum n))))
  15941. (when (not (= 0 (setq rem (% m dm))))
  15942. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15943. (setq m (+ m n)))
  15944. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15945. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15946. (setq h (min 24 (max 0 h)))
  15947. (setq ng 1 new (format "-%02d:%02d" h m)))
  15948. ((org-pos-in-match-range pos 6)
  15949. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15950. ((org-pos-in-match-range pos 5)
  15951. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15952. ((org-pos-in-match-range pos 9)
  15953. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15954. ((org-pos-in-match-range pos 8)
  15955. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15956. (when ng
  15957. (setq s (concat
  15958. (substring s 0 (match-beginning ng))
  15959. new
  15960. (substring s (match-end ng))))))
  15961. s))
  15962. (defun org-recenter-calendar (date)
  15963. "If the calendar is visible, recenter it to DATE."
  15964. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15965. (when cwin
  15966. (let ((calendar-move-hook nil))
  15967. (with-selected-window cwin
  15968. (calendar-goto-date (if (listp date) date
  15969. (calendar-gregorian-from-absolute date))))))))
  15970. (defun org-goto-calendar (&optional arg)
  15971. "Go to the Emacs calendar at the current date.
  15972. If there is a time stamp in the current line, go to that date.
  15973. A prefix ARG can be used to force the current date."
  15974. (interactive "P")
  15975. (let ((tsr org-ts-regexp) diff
  15976. (calendar-move-hook nil)
  15977. (calendar-view-holidays-initially-flag nil)
  15978. (calendar-view-diary-initially-flag nil))
  15979. (if (or (org-at-timestamp-p)
  15980. (save-excursion
  15981. (beginning-of-line 1)
  15982. (looking-at (concat ".*" tsr))))
  15983. (let ((d1 (time-to-days (current-time)))
  15984. (d2 (time-to-days
  15985. (org-time-string-to-time (match-string 1)))))
  15986. (setq diff (- d2 d1))))
  15987. (calendar)
  15988. (calendar-goto-today)
  15989. (if (and diff (not arg)) (calendar-forward-day diff))))
  15990. (defun org-get-date-from-calendar ()
  15991. "Return a list (month day year) of date at point in calendar."
  15992. (with-current-buffer "*Calendar*"
  15993. (save-match-data
  15994. (calendar-cursor-to-date))))
  15995. (defun org-date-from-calendar ()
  15996. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15997. If there is already a time stamp at the cursor position, update it."
  15998. (interactive)
  15999. (if (org-at-timestamp-p t)
  16000. (org-timestamp-change 0 'calendar)
  16001. (let ((cal-date (org-get-date-from-calendar)))
  16002. (org-insert-time-stamp
  16003. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16004. (defcustom org-effort-durations
  16005. `(("h" . 60)
  16006. ("d" . ,(* 60 8))
  16007. ("w" . ,(* 60 8 5))
  16008. ("m" . ,(* 60 8 5 4))
  16009. ("y" . ,(* 60 8 5 40)))
  16010. "Conversion factor to minutes for an effort modifier.
  16011. Each entry has the form (MODIFIER . MINUTES).
  16012. In an effort string, a number followed by MODIFIER is multiplied
  16013. by the specified number of MINUTES to obtain an effort in
  16014. minutes.
  16015. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16016. effort string \"2hours\" is equivalent to 120 minutes."
  16017. :group 'org-agenda
  16018. :version "24.1"
  16019. :type '(alist :key-type (string :tag "Modifier")
  16020. :value-type (number :tag "Minutes")))
  16021. (defun org-minutes-to-clocksum-string (m)
  16022. "Format number of minutes as a clocksum string.
  16023. The format is determined by `org-time-clocksum-format',
  16024. `org-time-clocksum-use-fractional' and
  16025. `org-time-clocksum-fractional-format' and
  16026. `org-time-clocksum-use-effort-durations'."
  16027. (let ((clocksum "")
  16028. (m (round m)) ; Don't allow fractions of minutes
  16029. h d w mo y fmt n)
  16030. (setq h (if org-time-clocksum-use-effort-durations
  16031. (cdr (assoc "h" org-effort-durations)) 60)
  16032. d (if org-time-clocksum-use-effort-durations
  16033. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16034. w (if org-time-clocksum-use-effort-durations
  16035. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16036. mo (if org-time-clocksum-use-effort-durations
  16037. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16038. y (if org-time-clocksum-use-effort-durations
  16039. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16040. ;; fractional format
  16041. (if org-time-clocksum-use-fractional
  16042. (cond
  16043. ;; single format string
  16044. ((stringp org-time-clocksum-fractional-format)
  16045. (format org-time-clocksum-fractional-format (/ m (float h))))
  16046. ;; choice of fractional formats for different time units
  16047. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16048. (> (/ (truncate m) (* y d h)) 0))
  16049. (format fmt (/ m (* y d (float h)))))
  16050. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16051. (> (/ (truncate m) (* mo d h)) 0))
  16052. (format fmt (/ m (* mo d (float h)))))
  16053. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16054. (> (/ (truncate m) (* w d h)) 0))
  16055. (format fmt (/ m (* w d (float h)))))
  16056. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16057. (> (/ (truncate m) (* d h)) 0))
  16058. (format fmt (/ m (* d (float h)))))
  16059. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16060. (> (/ (truncate m) h) 0))
  16061. (format fmt (/ m (float h))))
  16062. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16063. (format fmt m))
  16064. ;; fall back to smallest time unit with a format
  16065. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16066. (format fmt (/ m (float h))))
  16067. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16068. (format fmt (/ m (* d (float h)))))
  16069. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16070. (format fmt (/ m (* w d (float h)))))
  16071. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16072. (format fmt (/ m (* mo d (float h)))))
  16073. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16074. (format fmt (/ m (* y d (float h))))))
  16075. ;; standard (non-fractional) format, with single format string
  16076. (if (stringp org-time-clocksum-format)
  16077. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16078. ;; separate formats components
  16079. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16080. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16081. (plist-get org-time-clocksum-format :require-years))
  16082. (setq clocksum (concat clocksum (format fmt n))
  16083. m (- m (* n y d h))))
  16084. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16085. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16086. (plist-get org-time-clocksum-format :require-months))
  16087. (setq clocksum (concat clocksum (format fmt n))
  16088. m (- m (* n mo d h))))
  16089. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16090. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16091. (plist-get org-time-clocksum-format :require-weeks))
  16092. (setq clocksum (concat clocksum (format fmt n))
  16093. m (- m (* n w d h))))
  16094. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16095. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16096. (plist-get org-time-clocksum-format :require-days))
  16097. (setq clocksum (concat clocksum (format fmt n))
  16098. m (- m (* n d h))))
  16099. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16100. (or (> (setq n (/ (truncate m) h)) 0)
  16101. (plist-get org-time-clocksum-format :require-hours))
  16102. (setq clocksum (concat clocksum (format fmt n))
  16103. m (- m (* n h))))
  16104. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16105. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16106. (setq clocksum (concat clocksum (format fmt m))))
  16107. ;; return formatted time duration
  16108. clocksum))))
  16109. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16110. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16111. "Org mode version 8.0")
  16112. (defun org-hours-to-clocksum-string (n)
  16113. (org-minutes-to-clocksum-string (* n 60)))
  16114. (defun org-hh:mm-string-to-minutes (s)
  16115. "Convert a string H:MM to a number of minutes.
  16116. If the string is just a number, interpret it as minutes.
  16117. In fact, the first hh:mm or number in the string will be taken,
  16118. there can be extra stuff in the string.
  16119. If no number is found, the return value is 0."
  16120. (cond
  16121. ((integerp s) s)
  16122. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16123. (+ (* (string-to-number (match-string 1 s)) 60)
  16124. (string-to-number (match-string 2 s))))
  16125. ((string-match "\\([0-9]+\\)" s)
  16126. (string-to-number (match-string 1 s)))
  16127. (t 0)))
  16128. (defcustom org-image-actual-width t
  16129. "Should we use the actual width of images when inlining them?
  16130. When set to `t', always use the image width.
  16131. When set to a number, use imagemagick (when available) to set
  16132. the image's width to this value.
  16133. When set to a number in a list, try to get the width from any
  16134. #+ATTR.* keyword if it matches a width specification like
  16135. #+ATTR_HTML: :width 300px
  16136. and fall back on that number if none is found.
  16137. When set to nil, try to get the width from an #+ATTR.* keyword
  16138. and fall back on the original width if none is found.
  16139. This requires Emacs >= 24.1, build with imagemagick support."
  16140. :group 'org-appearance
  16141. :version "24.4"
  16142. :package-version '(Org . "8.0")
  16143. :type '(choice
  16144. (const :tag "Use the image width" t)
  16145. (integer :tag "Use a number of pixels")
  16146. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16147. (const :tag "Use #+ATTR* or don't resize" nil)))
  16148. (defcustom org-agenda-inhibit-startup nil
  16149. "Inhibit startup when preparing agenda buffers.
  16150. When this variable is `t' (the default), the initialization of
  16151. the Org agenda buffers is inhibited: e.g. the visibility state
  16152. is not set, the tables are not re-aligned, etc."
  16153. :type 'boolean
  16154. :version "24.3"
  16155. :group 'org-agenda)
  16156. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16157. "Convert a duration string S to minutes.
  16158. A bare number is interpreted as minutes, modifiers can be set by
  16159. customizing `org-effort-durations' (which see).
  16160. Entries containing a colon are interpreted as H:MM by
  16161. `org-hh:mm-string-to-minutes'."
  16162. (let ((result 0)
  16163. (re (concat "\\([0-9.]+\\) *\\("
  16164. (regexp-opt (mapcar 'car org-effort-durations))
  16165. "\\)")))
  16166. (while (string-match re s)
  16167. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16168. (string-to-number (match-string 1 s))))
  16169. (setq s (replace-match "" nil t s)))
  16170. (setq result (floor result))
  16171. (incf result (org-hh:mm-string-to-minutes s))
  16172. (if output-to-string (number-to-string result) result)))
  16173. ;;;; Files
  16174. (defun org-save-all-org-buffers ()
  16175. "Save all Org-mode buffers without user confirmation."
  16176. (interactive)
  16177. (message "Saving all Org-mode buffers...")
  16178. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16179. (when (featurep 'org-id) (org-id-locations-save))
  16180. (message "Saving all Org-mode buffers... done"))
  16181. (defun org-revert-all-org-buffers ()
  16182. "Revert all Org-mode buffers.
  16183. Prompt for confirmation when there are unsaved changes.
  16184. Be sure you know what you are doing before letting this function
  16185. overwrite your changes.
  16186. This function is useful in a setup where one tracks org files
  16187. with a version control system, to revert on one machine after pulling
  16188. changes from another. I believe the procedure must be like this:
  16189. 1. M-x org-save-all-org-buffers
  16190. 2. Pull changes from the other machine, resolve conflicts
  16191. 3. M-x org-revert-all-org-buffers"
  16192. (interactive)
  16193. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16194. (user-error "Abort"))
  16195. (save-excursion
  16196. (save-window-excursion
  16197. (mapc
  16198. (lambda (b)
  16199. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16200. (with-current-buffer b buffer-file-name))
  16201. (org-pop-to-buffer-same-window b)
  16202. (revert-buffer t 'no-confirm)))
  16203. (buffer-list))
  16204. (when (and (featurep 'org-id) org-id-track-globally)
  16205. (org-id-locations-load)))))
  16206. ;;;; Agenda files
  16207. ;;;###autoload
  16208. (defun org-switchb (&optional arg)
  16209. "Switch between Org buffers.
  16210. With one prefix argument, restrict available buffers to files.
  16211. With two prefix arguments, restrict available buffers to agenda files.
  16212. Defaults to `iswitchb' for buffer name completion.
  16213. Set `org-completion-use-ido' to make it use ido instead."
  16214. (interactive "P")
  16215. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16216. ((equal arg '(16)) (org-buffer-list 'agenda))
  16217. (t (org-buffer-list))))
  16218. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16219. (org-completion-use-ido org-completion-use-ido))
  16220. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16221. (setq org-completion-use-iswitchb t))
  16222. (org-pop-to-buffer-same-window
  16223. (org-icompleting-read "Org buffer: "
  16224. (mapcar 'list (mapcar 'buffer-name blist))
  16225. nil t))))
  16226. ;;; Define some older names previously used for this functionality
  16227. ;;;###autoload
  16228. (defalias 'org-ido-switchb 'org-switchb)
  16229. ;;;###autoload
  16230. (defalias 'org-iswitchb 'org-switchb)
  16231. (defun org-buffer-list (&optional predicate exclude-tmp)
  16232. "Return a list of Org buffers.
  16233. PREDICATE can be `export', `files' or `agenda'.
  16234. export restrict the list to Export buffers.
  16235. files restrict the list to buffers visiting Org files.
  16236. agenda restrict the list to buffers visiting agenda files.
  16237. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16238. (let* ((bfn nil)
  16239. (agenda-files (and (eq predicate 'agenda)
  16240. (mapcar 'file-truename (org-agenda-files t))))
  16241. (filter
  16242. (cond
  16243. ((eq predicate 'files)
  16244. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16245. ((eq predicate 'export)
  16246. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16247. ((eq predicate 'agenda)
  16248. (lambda (b)
  16249. (with-current-buffer b
  16250. (and (derived-mode-p 'org-mode)
  16251. (setq bfn (buffer-file-name b))
  16252. (member (file-truename bfn) agenda-files)))))
  16253. (t (lambda (b) (with-current-buffer b
  16254. (or (derived-mode-p 'org-mode)
  16255. (string-match "\*Org .*Export"
  16256. (buffer-name b)))))))))
  16257. (delq nil
  16258. (mapcar
  16259. (lambda(b)
  16260. (if (and (funcall filter b)
  16261. (or (not exclude-tmp)
  16262. (not (string-match "tmp" (buffer-name b)))))
  16263. b
  16264. nil))
  16265. (buffer-list)))))
  16266. (defun org-agenda-files (&optional unrestricted archives)
  16267. "Get the list of agenda files.
  16268. Optional UNRESTRICTED means return the full list even if a restriction
  16269. is currently in place.
  16270. When ARCHIVES is t, include all archive files that are really being
  16271. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16272. `org-agenda-archives-mode' is t."
  16273. (let ((files
  16274. (cond
  16275. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16276. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16277. ((listp org-agenda-files) org-agenda-files)
  16278. (t (error "Invalid value of `org-agenda-files'")))))
  16279. (setq files (apply 'append
  16280. (mapcar (lambda (f)
  16281. (if (file-directory-p f)
  16282. (directory-files
  16283. f t org-agenda-file-regexp)
  16284. (list f)))
  16285. files)))
  16286. (when org-agenda-skip-unavailable-files
  16287. (setq files (delq nil
  16288. (mapcar (function
  16289. (lambda (file)
  16290. (and (file-readable-p file) file)))
  16291. files))))
  16292. (when (or (eq archives t)
  16293. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16294. (setq files (org-add-archive-files files)))
  16295. files))
  16296. (defun org-agenda-file-p (&optional file)
  16297. "Return non-nil, if FILE is an agenda file.
  16298. If FILE is omitted, use the file associated with the current
  16299. buffer."
  16300. (member (or file (buffer-file-name))
  16301. (org-agenda-files t)))
  16302. (defun org-edit-agenda-file-list ()
  16303. "Edit the list of agenda files.
  16304. Depending on setup, this either uses customize to edit the variable
  16305. `org-agenda-files', or it visits the file that is holding the list. In the
  16306. latter case, the buffer is set up in a way that saving it automatically kills
  16307. the buffer and restores the previous window configuration."
  16308. (interactive)
  16309. (if (stringp org-agenda-files)
  16310. (let ((cw (current-window-configuration)))
  16311. (find-file org-agenda-files)
  16312. (org-set-local 'org-window-configuration cw)
  16313. (org-add-hook 'after-save-hook
  16314. (lambda ()
  16315. (set-window-configuration
  16316. (prog1 org-window-configuration
  16317. (kill-buffer (current-buffer))))
  16318. (org-install-agenda-files-menu)
  16319. (message "New agenda file list installed"))
  16320. nil 'local)
  16321. (message "%s" (substitute-command-keys
  16322. "Edit list and finish with \\[save-buffer]")))
  16323. (customize-variable 'org-agenda-files)))
  16324. (defun org-store-new-agenda-file-list (list)
  16325. "Set new value for the agenda file list and save it correctly."
  16326. (if (stringp org-agenda-files)
  16327. (let ((fe (org-read-agenda-file-list t)) b u)
  16328. (while (setq b (find-buffer-visiting org-agenda-files))
  16329. (kill-buffer b))
  16330. (with-temp-file org-agenda-files
  16331. (insert
  16332. (mapconcat
  16333. (lambda (f) ;; Keep un-expanded entries.
  16334. (if (setq u (assoc f fe))
  16335. (cdr u)
  16336. f))
  16337. list "\n")
  16338. "\n")))
  16339. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16340. (org-insert-mode-line-in-empty-file nil))
  16341. (setq org-agenda-files list)
  16342. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16343. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16344. "Read the list of agenda files from a file.
  16345. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16346. filenames, used by `org-store-new-agenda-file-list' to write back
  16347. un-expanded file names."
  16348. (when (file-directory-p org-agenda-files)
  16349. (error "`org-agenda-files' cannot be a single directory"))
  16350. (when (stringp org-agenda-files)
  16351. (with-temp-buffer
  16352. (insert-file-contents org-agenda-files)
  16353. (mapcar
  16354. (lambda (f)
  16355. (let ((e (expand-file-name (substitute-in-file-name f)
  16356. org-directory)))
  16357. (if pair-with-expansion
  16358. (cons e f)
  16359. e)))
  16360. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16361. ;;;###autoload
  16362. (defun org-cycle-agenda-files ()
  16363. "Cycle through the files in `org-agenda-files'.
  16364. If the current buffer visits an agenda file, find the next one in the list.
  16365. If the current buffer does not, find the first agenda file."
  16366. (interactive)
  16367. (let* ((fs (org-agenda-files t))
  16368. (files (append fs (list (car fs))))
  16369. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16370. file)
  16371. (unless files (user-error "No agenda files"))
  16372. (catch 'exit
  16373. (while (setq file (pop files))
  16374. (if (equal (file-truename file) tcf)
  16375. (when (car files)
  16376. (find-file (car files))
  16377. (throw 'exit t))))
  16378. (find-file (car fs)))
  16379. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16380. (defun org-agenda-file-to-front (&optional to-end)
  16381. "Move/add the current file to the top of the agenda file list.
  16382. If the file is not present in the list, it is added to the front. If it is
  16383. present, it is moved there. With optional argument TO-END, add/move to the
  16384. end of the list."
  16385. (interactive "P")
  16386. (let ((org-agenda-skip-unavailable-files nil)
  16387. (file-alist (mapcar (lambda (x)
  16388. (cons (file-truename x) x))
  16389. (org-agenda-files t)))
  16390. (ctf (file-truename
  16391. (or buffer-file-name
  16392. (user-error "Please save the current buffer to a file"))))
  16393. x had)
  16394. (setq x (assoc ctf file-alist) had x)
  16395. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16396. (if to-end
  16397. (setq file-alist (append (delq x file-alist) (list x)))
  16398. (setq file-alist (cons x (delq x file-alist))))
  16399. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16400. (org-install-agenda-files-menu)
  16401. (message "File %s to %s of agenda file list"
  16402. (if had "moved" "added") (if to-end "end" "front"))))
  16403. (defun org-remove-file (&optional file)
  16404. "Remove current file from the list of files in variable `org-agenda-files'.
  16405. These are the files which are being checked for agenda entries.
  16406. Optional argument FILE means use this file instead of the current."
  16407. (interactive)
  16408. (let* ((org-agenda-skip-unavailable-files nil)
  16409. (file (or file buffer-file-name
  16410. (user-error "Current buffer does not visit a file")))
  16411. (true-file (file-truename file))
  16412. (afile (abbreviate-file-name file))
  16413. (files (delq nil (mapcar
  16414. (lambda (x)
  16415. (if (equal true-file
  16416. (file-truename x))
  16417. nil x))
  16418. (org-agenda-files t)))))
  16419. (if (not (= (length files) (length (org-agenda-files t))))
  16420. (progn
  16421. (org-store-new-agenda-file-list files)
  16422. (org-install-agenda-files-menu)
  16423. (message "Removed file: %s" afile))
  16424. (message "File was not in list: %s (not removed)" afile))))
  16425. (defun org-file-menu-entry (file)
  16426. (vector file (list 'find-file file) t))
  16427. (defun org-check-agenda-file (file)
  16428. "Make sure FILE exists. If not, ask user what to do."
  16429. (when (not (file-exists-p file))
  16430. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16431. (abbreviate-file-name file))
  16432. (let ((r (downcase (read-char-exclusive))))
  16433. (cond
  16434. ((equal r ?r)
  16435. (org-remove-file file)
  16436. (throw 'nextfile t))
  16437. (t (error "Abort"))))))
  16438. (defun org-get-agenda-file-buffer (file)
  16439. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16440. it to the list of buffers which might be released later."
  16441. (let ((buf (org-find-base-buffer-visiting file)))
  16442. (if buf
  16443. buf ; just return it
  16444. ;; Make a new buffer and remember it
  16445. (setq buf (find-file-noselect file))
  16446. (if buf (push buf org-agenda-new-buffers))
  16447. buf)))
  16448. (defun org-release-buffers (blist)
  16449. "Release all buffers in list, asking the user for confirmation when needed.
  16450. When a buffer is unmodified, it is just killed. When modified, it is saved
  16451. \(if the user agrees) and then killed."
  16452. (let (buf file)
  16453. (while (setq buf (pop blist))
  16454. (setq file (buffer-file-name buf))
  16455. (when (and (buffer-modified-p buf)
  16456. file
  16457. (y-or-n-p (format "Save file %s? " file)))
  16458. (with-current-buffer buf (save-buffer)))
  16459. (kill-buffer buf))))
  16460. (defun org-agenda-prepare-buffers (files)
  16461. "Create buffers for all agenda files, protect archived trees and comments."
  16462. (interactive)
  16463. (let ((pa '(:org-archived t))
  16464. (pc '(:org-comment t))
  16465. (pall '(:org-archived t :org-comment t))
  16466. (inhibit-read-only t)
  16467. (org-inhibit-startup org-agenda-inhibit-startup)
  16468. (rea (concat ":" org-archive-tag ":"))
  16469. file re)
  16470. (setq org-tag-alist-for-agenda nil
  16471. org-tag-groups-alist-for-agenda nil)
  16472. (save-excursion
  16473. (save-restriction
  16474. (while (setq file (pop files))
  16475. (catch 'nextfile
  16476. (if (bufferp file)
  16477. (set-buffer file)
  16478. (org-check-agenda-file file)
  16479. (set-buffer (org-get-agenda-file-buffer file)))
  16480. (widen)
  16481. (org-set-regexps-and-options-for-tags)
  16482. (org-refresh-category-properties)
  16483. (org-refresh-properties org-effort-property 'org-effort)
  16484. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16485. (setq org-todo-keywords-for-agenda
  16486. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16487. (setq org-done-keywords-for-agenda
  16488. (append org-done-keywords-for-agenda org-done-keywords))
  16489. (setq org-todo-keyword-alist-for-agenda
  16490. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16491. (setq org-drawers-for-agenda
  16492. (append org-drawers-for-agenda org-drawers))
  16493. (unless (equal org-tag-alist-for-agenda org-tag-alist)
  16494. (setq org-tag-alist-for-agenda
  16495. (append org-tag-alist-for-agenda org-tag-alist)))
  16496. (if org-group-tags
  16497. (setq org-tag-groups-alist-for-agenda
  16498. (org-uniquify-alist
  16499. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16500. (org-with-silent-modifications
  16501. (save-excursion
  16502. (remove-text-properties (point-min) (point-max) pall)
  16503. (when org-agenda-skip-archived-trees
  16504. (goto-char (point-min))
  16505. (while (re-search-forward rea nil t)
  16506. (if (org-at-heading-p t)
  16507. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16508. (goto-char (point-min))
  16509. (setq re (format org-heading-keyword-regexp-format
  16510. org-comment-string))
  16511. (while (re-search-forward re nil t)
  16512. (add-text-properties
  16513. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16514. (setq org-todo-keywords-for-agenda
  16515. (org-uniquify org-todo-keywords-for-agenda))
  16516. (setq org-todo-keyword-alist-for-agenda
  16517. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16518. ;;;; CDLaTeX minor mode
  16519. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16520. "Keymap for the minor `org-cdlatex-mode'.")
  16521. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16522. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16523. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16524. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16525. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16526. (defvar org-cdlatex-texmathp-advice-is-done nil
  16527. "Flag remembering if we have applied the advice to texmathp already.")
  16528. (define-minor-mode org-cdlatex-mode
  16529. "Toggle the minor `org-cdlatex-mode'.
  16530. This mode supports entering LaTeX environment and math in LaTeX fragments
  16531. in Org-mode.
  16532. \\{org-cdlatex-mode-map}"
  16533. nil " OCDL" nil
  16534. (when org-cdlatex-mode
  16535. (require 'cdlatex)
  16536. (run-hooks 'cdlatex-mode-hook)
  16537. (cdlatex-compute-tables))
  16538. (unless org-cdlatex-texmathp-advice-is-done
  16539. (setq org-cdlatex-texmathp-advice-is-done t)
  16540. (defadvice texmathp (around org-math-always-on activate)
  16541. "Always return t in org-mode buffers.
  16542. This is because we want to insert math symbols without dollars even outside
  16543. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16544. an embedded LaTeX fragment, let texmathp do its job.
  16545. \\[org-cdlatex-mode-map]"
  16546. (interactive)
  16547. (let (p)
  16548. (cond
  16549. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16550. ((eq this-command 'cdlatex-math-symbol)
  16551. (setq ad-return-value t
  16552. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16553. (t
  16554. (let ((p (org-inside-LaTeX-fragment-p)))
  16555. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16556. (setq ad-return-value t
  16557. texmathp-why '("Org-mode embedded math" . 0))
  16558. (if p ad-do-it)))))))))
  16559. (defun turn-on-org-cdlatex ()
  16560. "Unconditionally turn on `org-cdlatex-mode'."
  16561. (org-cdlatex-mode 1))
  16562. (defun org-try-cdlatex-tab ()
  16563. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16564. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16565. - inside a LaTeX fragment, or
  16566. - after the first word in a line, where an abbreviation expansion could
  16567. insert a LaTeX environment."
  16568. (when org-cdlatex-mode
  16569. (cond
  16570. ;; Before any word on the line: No expansion possible.
  16571. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16572. ;; Just after first word on the line: Expand it. Make sure it
  16573. ;; cannot happen on headlines, though.
  16574. ((save-excursion
  16575. (skip-chars-backward "a-zA-Z0-9*")
  16576. (skip-chars-backward " \t")
  16577. (and (bolp) (not (org-at-heading-p))))
  16578. (cdlatex-tab) t)
  16579. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16580. (defun org-cdlatex-underscore-caret (&optional arg)
  16581. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16582. Revert to the normal definition outside of these fragments."
  16583. (interactive "P")
  16584. (if (org-inside-LaTeX-fragment-p)
  16585. (call-interactively 'cdlatex-sub-superscript)
  16586. (let (org-cdlatex-mode)
  16587. (call-interactively (key-binding (vector last-input-event))))))
  16588. (defun org-cdlatex-math-modify (&optional arg)
  16589. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16590. Revert to the normal definition outside of these fragments."
  16591. (interactive "P")
  16592. (if (org-inside-LaTeX-fragment-p)
  16593. (call-interactively 'cdlatex-math-modify)
  16594. (let (org-cdlatex-mode)
  16595. (call-interactively (key-binding (vector last-input-event))))))
  16596. ;;;; LaTeX fragments
  16597. (defvar org-latex-regexps
  16598. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16599. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16600. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16601. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16602. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16603. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16604. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16605. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16606. "Regular expressions for matching embedded LaTeX.")
  16607. (defun org-inside-LaTeX-fragment-p ()
  16608. "Test if point is inside a LaTeX fragment.
  16609. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16610. sequence appearing also before point.
  16611. Even though the matchers for math are configurable, this function assumes
  16612. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16613. delimiters are skipped when they have been removed by customization.
  16614. The return value is nil, or a cons cell with the delimiter and the
  16615. position of this delimiter.
  16616. This function does a reasonably good job, but can locally be fooled by
  16617. for example currency specifications. For example it will assume being in
  16618. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16619. fragments that are properly closed, but during editing, we have to live
  16620. with the uncertainty caused by missing closing delimiters. This function
  16621. looks only before point, not after."
  16622. (catch 'exit
  16623. (let ((pos (point))
  16624. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16625. (lim (progn
  16626. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16627. (point)))
  16628. dd-on str (start 0) m re)
  16629. (goto-char pos)
  16630. (when dodollar
  16631. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16632. re (nth 1 (assoc "$" org-latex-regexps)))
  16633. (while (string-match re str start)
  16634. (cond
  16635. ((= (match-end 0) (length str))
  16636. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16637. ((= (match-end 0) (- (length str) 5))
  16638. (throw 'exit nil))
  16639. (t (setq start (match-end 0))))))
  16640. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16641. (goto-char pos)
  16642. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16643. (and (match-beginning 2) (throw 'exit nil))
  16644. ;; count $$
  16645. (while (re-search-backward "\\$\\$" lim t)
  16646. (setq dd-on (not dd-on)))
  16647. (goto-char pos)
  16648. (if dd-on (cons "$$" m))))))
  16649. (defun org-inside-latex-macro-p ()
  16650. "Is point inside a LaTeX macro or its arguments?"
  16651. (save-match-data
  16652. (org-in-regexp
  16653. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16654. (defvar org-latex-fragment-image-overlays nil
  16655. "List of overlays carrying the images of latex fragments.")
  16656. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16657. (defun org-remove-latex-fragment-image-overlays ()
  16658. "Remove all overlays with LaTeX fragment images in current buffer."
  16659. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16660. (setq org-latex-fragment-image-overlays nil))
  16661. (defun org-preview-latex-fragment (&optional subtree)
  16662. "Preview the LaTeX fragment at point, or all locally or globally.
  16663. If the cursor is in a LaTeX fragment, create the image and overlay
  16664. it over the source code. If there is no fragment at point, display
  16665. all fragments in the current text, from one headline to the next. With
  16666. prefix SUBTREE, display all fragments in the current subtree. With a
  16667. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16668. the cursor is before the first headline,
  16669. display all fragments in the buffer.
  16670. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16671. (interactive "P")
  16672. (unless buffer-file-name
  16673. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  16674. (org-remove-latex-fragment-image-overlays)
  16675. (save-excursion
  16676. (save-restriction
  16677. (let (beg end at msg)
  16678. (cond
  16679. ((or (equal subtree '(16))
  16680. (not (save-excursion
  16681. (re-search-backward org-outline-regexp-bol nil t))))
  16682. (setq beg (point-min) end (point-max)
  16683. msg "Creating images for buffer...%s"))
  16684. ((equal subtree '(4))
  16685. (org-back-to-heading)
  16686. (setq beg (point) end (org-end-of-subtree t)
  16687. msg "Creating images for subtree...%s"))
  16688. (t
  16689. (if (setq at (org-inside-LaTeX-fragment-p))
  16690. (goto-char (max (point-min) (- (cdr at) 2)))
  16691. (org-back-to-heading))
  16692. (setq beg (point) end (progn (outline-next-heading) (point))
  16693. msg (if at "Creating image...%s"
  16694. "Creating images for entry...%s"))))
  16695. (message msg "")
  16696. (narrow-to-region beg end)
  16697. (goto-char beg)
  16698. (org-format-latex
  16699. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16700. (file-name-nondirectory
  16701. buffer-file-name)))
  16702. default-directory 'overlays msg at 'forbuffer
  16703. org-latex-create-formula-image-program)
  16704. (message msg "done. Use `C-c C-c' to remove images.")))))
  16705. (defun org-format-latex (prefix &optional dir overlays msg at
  16706. forbuffer processing-type)
  16707. "Replace LaTeX fragments with links to an image, and produce images.
  16708. Some of the options can be changed using the variable
  16709. `org-format-latex-options'."
  16710. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16711. (let* ((prefixnodir (file-name-nondirectory prefix))
  16712. (absprefix (expand-file-name prefix dir))
  16713. (todir (file-name-directory absprefix))
  16714. (opt org-format-latex-options)
  16715. (optnew org-format-latex-options)
  16716. (matchers (plist-get opt :matchers))
  16717. (re-list org-latex-regexps)
  16718. (cnt 0) txt hash link beg end re e checkdir
  16719. string
  16720. m n block-type block linkfile movefile ov)
  16721. ;; Check the different regular expressions
  16722. (while (setq e (pop re-list))
  16723. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16724. block (if block-type "\n\n" ""))
  16725. (when (member m matchers)
  16726. (goto-char (point-min))
  16727. (while (re-search-forward re nil t)
  16728. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16729. (or (not overlays)
  16730. (not (eq (get-char-property (match-beginning n)
  16731. 'org-overlay-type)
  16732. 'org-latex-overlay))))
  16733. (cond
  16734. ((eq processing-type 'verbatim))
  16735. ((eq processing-type 'mathjax)
  16736. ;; Prepare for MathJax processing.
  16737. (setq string (match-string n))
  16738. (when (member m '("$" "$1"))
  16739. (save-excursion
  16740. (delete-region (match-beginning n) (match-end n))
  16741. (goto-char (match-beginning n))
  16742. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16743. ((or (eq processing-type 'dvipng)
  16744. (eq processing-type 'imagemagick))
  16745. ;; Process to an image.
  16746. (setq txt (match-string n)
  16747. beg (match-beginning n) end (match-end n)
  16748. cnt (1+ cnt))
  16749. (let ((face (face-at-point))
  16750. (fg (plist-get opt :foreground))
  16751. (bg (plist-get opt :background))
  16752. ;; Ensure full list is printed.
  16753. print-length print-level)
  16754. (when forbuffer
  16755. ;; Get the colors from the face at point.
  16756. (goto-char beg)
  16757. (when (eq fg 'auto)
  16758. (setq fg (face-attribute face :foreground nil 'default)))
  16759. (when (eq bg 'auto)
  16760. (setq bg (face-attribute face :background nil 'default)))
  16761. (setq optnew (copy-sequence opt))
  16762. (plist-put optnew :foreground fg)
  16763. (plist-put optnew :background bg))
  16764. (setq hash (sha1 (prin1-to-string
  16765. (list org-format-latex-header
  16766. org-latex-default-packages-alist
  16767. org-latex-packages-alist
  16768. org-format-latex-options
  16769. forbuffer txt fg bg)))
  16770. linkfile (format "%s_%s.png" prefix hash)
  16771. movefile (format "%s_%s.png" absprefix hash)))
  16772. (setq link (concat block "[[file:" linkfile "]]" block))
  16773. (if msg (message msg cnt))
  16774. (goto-char beg)
  16775. (unless checkdir ; Ensure the directory exists.
  16776. (setq checkdir t)
  16777. (or (file-directory-p todir) (make-directory todir t)))
  16778. (unless (file-exists-p movefile)
  16779. (org-create-formula-image
  16780. txt movefile optnew forbuffer processing-type))
  16781. (if overlays
  16782. (progn
  16783. (mapc (lambda (o)
  16784. (if (eq (overlay-get o 'org-overlay-type)
  16785. 'org-latex-overlay)
  16786. (delete-overlay o)))
  16787. (overlays-in beg end))
  16788. (setq ov (make-overlay beg end))
  16789. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16790. (if (featurep 'xemacs)
  16791. (progn
  16792. (overlay-put ov 'invisible t)
  16793. (overlay-put
  16794. ov 'end-glyph
  16795. (make-glyph (vector 'png :file movefile))))
  16796. (overlay-put
  16797. ov 'display
  16798. (list 'image :type 'png :file movefile :ascent 'center)))
  16799. (push ov org-latex-fragment-image-overlays)
  16800. (goto-char end))
  16801. (delete-region beg end)
  16802. (insert (org-add-props link
  16803. (list 'org-latex-src
  16804. (replace-regexp-in-string
  16805. "\"" "" txt)
  16806. 'org-latex-src-embed-type
  16807. (if block-type 'paragraph 'character))))))
  16808. ((eq processing-type 'mathml)
  16809. ;; Process to MathML
  16810. (unless (save-match-data (org-format-latex-mathml-available-p))
  16811. (user-error "LaTeX to MathML converter not configured"))
  16812. (setq txt (match-string n)
  16813. beg (match-beginning n) end (match-end n)
  16814. cnt (1+ cnt))
  16815. (if msg (message msg cnt))
  16816. (goto-char beg)
  16817. (delete-region beg end)
  16818. (insert (org-format-latex-as-mathml
  16819. txt block-type prefix dir)))
  16820. (t
  16821. (error "Unknown conversion type %s for LaTeX fragments"
  16822. processing-type)))))))))
  16823. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16824. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16825. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16826. sucessful, return the portion between \"<math...> </math>\"
  16827. elements otherwise return nil. When MATHML-FILE is specified,
  16828. write the results in to that file. When invoked as an
  16829. interactive command, prompt for LATEX-FRAG, with initial value
  16830. set to the current active region and echo the results for user
  16831. inspection."
  16832. (interactive (list (let ((frag (when (org-region-active-p)
  16833. (buffer-substring-no-properties
  16834. (region-beginning) (region-end)))))
  16835. (read-string "LaTeX Fragment: " frag nil frag))))
  16836. (unless latex-frag (error "Invalid LaTeX fragment"))
  16837. (let* ((tmp-in-file (file-relative-name
  16838. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16839. (ignore (write-region latex-frag nil tmp-in-file))
  16840. (tmp-out-file (file-relative-name
  16841. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16842. (cmd (format-spec
  16843. org-latex-to-mathml-convert-command
  16844. `((?j . ,(shell-quote-argument
  16845. (expand-file-name org-latex-to-mathml-jar-file)))
  16846. (?I . ,(shell-quote-argument tmp-in-file))
  16847. (?o . ,(shell-quote-argument tmp-out-file)))))
  16848. mathml shell-command-output)
  16849. (when (org-called-interactively-p 'any)
  16850. (unless (org-format-latex-mathml-available-p)
  16851. (user-error "LaTeX to MathML converter not configured")))
  16852. (message "Running %s" cmd)
  16853. (setq shell-command-output (shell-command-to-string cmd))
  16854. (setq mathml
  16855. (when (file-readable-p tmp-out-file)
  16856. (with-current-buffer (find-file-noselect tmp-out-file t)
  16857. (goto-char (point-min))
  16858. (when (re-search-forward
  16859. (concat
  16860. (regexp-quote
  16861. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16862. "\\(.\\|\n\\)*"
  16863. (regexp-quote "</math>")) nil t)
  16864. (prog1 (match-string 0) (kill-buffer))))))
  16865. (cond
  16866. (mathml
  16867. (setq mathml
  16868. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16869. (when mathml-file
  16870. (write-region mathml nil mathml-file))
  16871. (when (org-called-interactively-p 'any)
  16872. (message mathml)))
  16873. ((message "LaTeX to MathML conversion failed")
  16874. (message shell-command-output)))
  16875. (delete-file tmp-in-file)
  16876. (when (file-exists-p tmp-out-file)
  16877. (delete-file tmp-out-file))
  16878. mathml))
  16879. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16880. prefix &optional dir)
  16881. "Use `org-create-math-formula' but check local cache first."
  16882. (let* ((absprefix (expand-file-name prefix dir))
  16883. (print-length nil) (print-level nil)
  16884. (formula-id (concat
  16885. "formula-"
  16886. (sha1
  16887. (prin1-to-string
  16888. (list latex-frag
  16889. org-latex-to-mathml-convert-command)))))
  16890. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16891. (formula-cache-dir (file-name-directory formula-cache)))
  16892. (unless (file-directory-p formula-cache-dir)
  16893. (make-directory formula-cache-dir t))
  16894. (unless (file-exists-p formula-cache)
  16895. (org-create-math-formula latex-frag formula-cache))
  16896. (if (file-exists-p formula-cache)
  16897. ;; Successful conversion. Return the link to MathML file.
  16898. (org-add-props
  16899. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16900. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16901. 'org-latex-src-embed-type (if latex-frag-type
  16902. 'paragraph 'character)))
  16903. ;; Failed conversion. Return the LaTeX fragment verbatim
  16904. latex-frag)))
  16905. (defun org-create-formula-image (string tofile options buffer &optional type)
  16906. "Create an image from LaTeX source using dvipng or convert.
  16907. This function calls either `org-create-formula-image-with-dvipng'
  16908. or `org-create-formula-image-with-imagemagick' depending on the
  16909. value of `org-latex-create-formula-image-program' or on the value
  16910. of the optional TYPE variable.
  16911. Note: ultimately these two function should be combined as they
  16912. share a good deal of logic."
  16913. (org-check-external-command
  16914. "latex" "needed to convert LaTeX fragments to images")
  16915. (funcall
  16916. (case (or type org-latex-create-formula-image-program)
  16917. ('dvipng
  16918. (org-check-external-command
  16919. "dvipng" "needed to convert LaTeX fragments to images")
  16920. #'org-create-formula-image-with-dvipng)
  16921. ('imagemagick
  16922. (org-check-external-command
  16923. "convert" "you need to install imagemagick")
  16924. #'org-create-formula-image-with-imagemagick)
  16925. (t (error
  16926. "Invalid value of `org-latex-create-formula-image-program'")))
  16927. string tofile options buffer))
  16928. (declare-function org-export--get-global-options "ox" (&optional backend))
  16929. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16930. (defun org-create-formula--latex-header ()
  16931. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16932. (org-latex-guess-inputenc
  16933. (org-splice-latex-header
  16934. org-format-latex-header
  16935. org-latex-default-packages-alist
  16936. org-latex-packages-alist t
  16937. (plist-get
  16938. (org-combine-plists
  16939. (org-export--get-global-options 'latex)
  16940. (org-export--get-inbuffer-options 'latex))
  16941. :latex-header))))
  16942. ;; This function borrows from Ganesh Swami's latex2png.el
  16943. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16944. "This calls dvipng."
  16945. (require 'ox-latex)
  16946. (let* ((tmpdir (if (featurep 'xemacs)
  16947. (temp-directory)
  16948. temporary-file-directory))
  16949. (texfilebase (make-temp-name
  16950. (expand-file-name "orgtex" tmpdir)))
  16951. (texfile (concat texfilebase ".tex"))
  16952. (dvifile (concat texfilebase ".dvi"))
  16953. (pngfile (concat texfilebase ".png"))
  16954. (fnh (if (featurep 'xemacs)
  16955. (font-height (face-font 'default))
  16956. (face-attribute 'default :height nil)))
  16957. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16958. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16959. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16960. "Black"))
  16961. (bg (or (plist-get options (if buffer :background :html-background))
  16962. "Transparent")))
  16963. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16964. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16965. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16966. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16967. (let ((latex-header (org-create-formula--latex-header)))
  16968. (with-temp-file texfile
  16969. (insert latex-header)
  16970. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16971. (let ((dir default-directory))
  16972. (condition-case nil
  16973. (progn
  16974. (cd tmpdir)
  16975. (call-process "latex" nil nil nil texfile))
  16976. (error nil))
  16977. (cd dir))
  16978. (if (not (file-exists-p dvifile))
  16979. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16980. (condition-case nil
  16981. (if (featurep 'xemacs)
  16982. (call-process "dvipng" nil nil nil
  16983. "-fg" fg "-bg" bg
  16984. "-T" "tight"
  16985. "-o" pngfile
  16986. dvifile)
  16987. (call-process "dvipng" nil nil nil
  16988. "-fg" fg "-bg" bg
  16989. "-D" dpi
  16990. ;;"-x" scale "-y" scale
  16991. "-T" "tight"
  16992. "-o" pngfile
  16993. dvifile))
  16994. (error nil))
  16995. (if (not (file-exists-p pngfile))
  16996. (if org-format-latex-signal-error
  16997. (error "Failed to create png file from %s" texfile)
  16998. (message "Failed to create png file from %s" texfile)
  16999. nil)
  17000. ;; Use the requested file name and clean up
  17001. (copy-file pngfile tofile 'replace)
  17002. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17003. (if (file-exists-p (concat texfilebase e))
  17004. (delete-file (concat texfilebase e))))
  17005. pngfile))))
  17006. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17007. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17008. "This calls convert, which is included into imagemagick."
  17009. (require 'ox-latex)
  17010. (let* ((tmpdir (if (featurep 'xemacs)
  17011. (temp-directory)
  17012. temporary-file-directory))
  17013. (texfilebase (make-temp-name
  17014. (expand-file-name "orgtex" tmpdir)))
  17015. (texfile (concat texfilebase ".tex"))
  17016. (pdffile (concat texfilebase ".pdf"))
  17017. (pngfile (concat texfilebase ".png"))
  17018. (fnh (if (featurep 'xemacs)
  17019. (font-height (face-font 'default))
  17020. (face-attribute 'default :height nil)))
  17021. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17022. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17023. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17024. "black"))
  17025. (bg (or (plist-get options (if buffer :background :html-background))
  17026. "white")))
  17027. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17028. (setq fg (org-latex-color-format fg)))
  17029. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17030. (setq bg (org-latex-color-format
  17031. (if (string= bg "Transparent") "white" bg))))
  17032. (let ((latex-header (org-create-formula--latex-header)))
  17033. (with-temp-file texfile
  17034. (insert latex-header)
  17035. (insert "\n\\begin{document}\n"
  17036. "\\definecolor{fg}{rgb}{" fg "}\n"
  17037. "\\definecolor{bg}{rgb}{" bg "}\n"
  17038. "\n\\pagecolor{bg}\n"
  17039. "\n{\\color{fg}\n"
  17040. string
  17041. "\n}\n"
  17042. "\n\\end{document}\n")))
  17043. (org-latex-compile texfile t)
  17044. (if (not (file-exists-p pdffile))
  17045. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17046. (condition-case nil
  17047. (if (featurep 'xemacs)
  17048. (call-process "convert" nil nil nil
  17049. "-density" "96"
  17050. "-trim"
  17051. "-antialias"
  17052. pdffile
  17053. "-quality" "100"
  17054. ;; "-sharpen" "0x1.0"
  17055. pngfile)
  17056. (call-process "convert" nil nil nil
  17057. "-density" dpi
  17058. "-trim"
  17059. "-antialias"
  17060. pdffile
  17061. "-quality" "100"
  17062. ;; "-sharpen" "0x1.0"
  17063. pngfile))
  17064. (error nil))
  17065. (if (not (file-exists-p pngfile))
  17066. (if org-format-latex-signal-error
  17067. (error "Failed to create png file from %s" texfile)
  17068. (message "Failed to create png file from %s" texfile)
  17069. nil)
  17070. ;; Use the requested file name and clean up
  17071. (copy-file pngfile tofile 'replace)
  17072. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17073. (if (file-exists-p (concat texfilebase e))
  17074. (delete-file (concat texfilebase e))))
  17075. pngfile))))
  17076. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17077. "Fill a LaTeX header template TPL.
  17078. In the template, the following place holders will be recognized:
  17079. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17080. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17081. [PACKAGES] \\usepackage statements for PKG
  17082. [NO-PACKAGES] do not include PKG
  17083. [EXTRA] the string EXTRA
  17084. [NO-EXTRA] do not include EXTRA
  17085. For backward compatibility, if both the positive and the negative place
  17086. holder is missing, the positive one (without the \"NO-\") will be
  17087. assumed to be present at the end of the template.
  17088. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17089. EXTRA is a string.
  17090. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17091. (let (rpl (end ""))
  17092. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17093. (setq rpl (if (or (match-end 1) (not def-pkg))
  17094. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17095. tpl (replace-match rpl t t tpl))
  17096. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17097. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17098. (setq rpl (if (or (match-end 1) (not pkg))
  17099. "" (org-latex-packages-to-string pkg snippets-p t))
  17100. tpl (replace-match rpl t t tpl))
  17101. (if pkg (setq end
  17102. (concat end "\n"
  17103. (org-latex-packages-to-string pkg snippets-p)))))
  17104. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17105. (setq rpl (if (or (match-end 1) (not extra))
  17106. "" (concat extra "\n"))
  17107. tpl (replace-match rpl t t tpl))
  17108. (if (and extra (string-match "\\S-" extra))
  17109. (setq end (concat end "\n" extra))))
  17110. (if (string-match "\\S-" end)
  17111. (concat tpl "\n" end)
  17112. tpl)))
  17113. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17114. "Turn an alist of packages into a string with the \\usepackage macros."
  17115. (setq pkg (mapconcat (lambda(p)
  17116. (cond
  17117. ((stringp p) p)
  17118. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17119. (format "%% Package %s omitted" (cadr p)))
  17120. ((equal "" (car p))
  17121. (format "\\usepackage{%s}" (cadr p)))
  17122. (t
  17123. (format "\\usepackage[%s]{%s}"
  17124. (car p) (cadr p)))))
  17125. pkg
  17126. "\n"))
  17127. (if newline (concat pkg "\n") pkg))
  17128. (defun org-dvipng-color (attr)
  17129. "Return a RGB color specification for dvipng."
  17130. (apply 'format "rgb %s %s %s"
  17131. (mapcar 'org-normalize-color
  17132. (if (featurep 'xemacs)
  17133. (color-rgb-components
  17134. (face-property 'default
  17135. (cond ((eq attr :foreground) 'foreground)
  17136. ((eq attr :background) 'background))))
  17137. (color-values (face-attribute 'default attr nil))))))
  17138. (defun org-dvipng-color-format (color-name)
  17139. "Convert COLOR-NAME to a RGB color value for dvipng."
  17140. (apply 'format "rgb %s %s %s"
  17141. (mapcar 'org-normalize-color
  17142. (color-values color-name))))
  17143. (defun org-latex-color (attr)
  17144. "Return a RGB color for the LaTeX color package."
  17145. (apply 'format "%s,%s,%s"
  17146. (mapcar 'org-normalize-color
  17147. (if (featurep 'xemacs)
  17148. (color-rgb-components
  17149. (face-property 'default
  17150. (cond ((eq attr :foreground) 'foreground)
  17151. ((eq attr :background) 'background))))
  17152. (color-values (face-attribute 'default attr nil))))))
  17153. (defun org-latex-color-format (color-name)
  17154. "Convert COLOR-NAME to a RGB color value."
  17155. (apply 'format "%s,%s,%s"
  17156. (mapcar 'org-normalize-color
  17157. (color-values color-name))))
  17158. (defun org-normalize-color (value)
  17159. "Return string to be used as color value for an RGB component."
  17160. (format "%g" (/ value 65535.0)))
  17161. ;; Image display
  17162. (defvar org-inline-image-overlays nil)
  17163. (make-variable-buffer-local 'org-inline-image-overlays)
  17164. (defun org-toggle-inline-images (&optional include-linked)
  17165. "Toggle the display of inline images.
  17166. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17167. (interactive "P")
  17168. (if org-inline-image-overlays
  17169. (progn
  17170. (org-remove-inline-images)
  17171. (message "Inline image display turned off"))
  17172. (org-display-inline-images include-linked)
  17173. (if (and (org-called-interactively-p)
  17174. org-inline-image-overlays)
  17175. (message "%d images displayed inline"
  17176. (length org-inline-image-overlays))
  17177. (message "No images to display inline"))))
  17178. (defun org-redisplay-inline-images ()
  17179. "Refresh the display of inline images."
  17180. (interactive)
  17181. (if (not org-inline-image-overlays)
  17182. (org-toggle-inline-images)
  17183. (org-toggle-inline-images)
  17184. (org-toggle-inline-images)))
  17185. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17186. "Display inline images.
  17187. Normally only links without a description part are inlined, because this
  17188. is how it will work for export. When INCLUDE-LINKED is set, also links
  17189. with a description part will be inlined. This can be nice for a quick
  17190. look at those images, but it does not reflect what exported files will look
  17191. like.
  17192. When REFRESH is set, refresh existing images between BEG and END.
  17193. This will create new image displays only if necessary.
  17194. BEG and END default to the buffer boundaries."
  17195. (interactive "P")
  17196. (unless refresh
  17197. (org-remove-inline-images)
  17198. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17199. (save-excursion
  17200. (save-restriction
  17201. (widen)
  17202. (setq beg (or beg (point-min)) end (or end (point-max)))
  17203. (goto-char beg)
  17204. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17205. (substring (org-image-file-name-regexp) 0 -2)
  17206. "\\)\\]" (if include-linked "" "\\]")))
  17207. (case-fold-search t)
  17208. old file ov img type attrwidth width)
  17209. (while (re-search-forward re end t)
  17210. (setq old (get-char-property-and-overlay (match-beginning 1)
  17211. 'org-image-overlay)
  17212. file (expand-file-name
  17213. (concat (or (match-string 3) "") (match-string 4))))
  17214. (when (image-type-available-p 'imagemagick)
  17215. (setq attrwidth (if (or (listp org-image-actual-width)
  17216. (null org-image-actual-width))
  17217. (save-excursion
  17218. (save-match-data
  17219. (when (re-search-backward
  17220. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17221. (save-excursion
  17222. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17223. (string-to-number (match-string 1))))))
  17224. width (cond ((eq org-image-actual-width t) nil)
  17225. ((null org-image-actual-width) attrwidth)
  17226. ((numberp org-image-actual-width)
  17227. org-image-actual-width)
  17228. ((listp org-image-actual-width)
  17229. (or attrwidth (car org-image-actual-width))))
  17230. type (if width 'imagemagick)))
  17231. (when (file-exists-p file)
  17232. (if (and (car-safe old) refresh)
  17233. (image-refresh (overlay-get (cdr old) 'display))
  17234. (setq img (save-match-data (create-image file type nil :width width)))
  17235. (when img
  17236. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17237. (overlay-put ov 'display img)
  17238. (overlay-put ov 'face 'default)
  17239. (overlay-put ov 'org-image-overlay t)
  17240. (overlay-put ov 'modification-hooks
  17241. (list 'org-display-inline-remove-overlay))
  17242. (push ov org-inline-image-overlays)))))))))
  17243. (define-obsolete-function-alias
  17244. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17245. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17246. "Remove inline-display overlay if a corresponding region is modified."
  17247. (let ((inhibit-modification-hooks t))
  17248. (when (and ov after)
  17249. (delete ov org-inline-image-overlays)
  17250. (delete-overlay ov))))
  17251. (defun org-remove-inline-images ()
  17252. "Remove inline display of images."
  17253. (interactive)
  17254. (mapc 'delete-overlay org-inline-image-overlays)
  17255. (setq org-inline-image-overlays nil))
  17256. ;;;; Key bindings
  17257. ;; Outline functions from `outline-mode-prefix-map'
  17258. ;; that can be remapped in Org:
  17259. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17260. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17261. (define-key org-mode-map [remap outline-forward-same-level]
  17262. 'org-forward-heading-same-level)
  17263. (define-key org-mode-map [remap outline-backward-same-level]
  17264. 'org-backward-heading-same-level)
  17265. (define-key org-mode-map [remap show-branches]
  17266. 'org-kill-note-or-show-branches)
  17267. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17268. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17269. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17270. ;; Outline functions from `outline-mode-prefix-map' that can not
  17271. ;; be remapped in Org:
  17272. ;;
  17273. ;; - the column "key binding" shows whether the Outline function is still
  17274. ;; available in Org mode on the same key that it has been bound to in
  17275. ;; Outline mode:
  17276. ;; - "overridden": key used for a different functionality in Org mode
  17277. ;; - else: key still bound to the same Outline function in Org mode
  17278. ;;
  17279. ;; | Outline function | key binding | Org replacement |
  17280. ;; |------------------------------------+-------------+-----------------------|
  17281. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17282. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17283. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17284. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17285. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17286. ;; | `show-entry' | overridden | no replacement |
  17287. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17288. ;; | `show-branches' | `C-c C-k' | still same function |
  17289. ;; | `show-subtree' | overridden | visibility cycling |
  17290. ;; | `show-all' | overridden | no replacement |
  17291. ;; | `hide-subtree' | overridden | visibility cycling |
  17292. ;; | `hide-body' | overridden | no replacement |
  17293. ;; | `hide-entry' | overridden | visibility cycling |
  17294. ;; | `hide-leaves' | overridden | no replacement |
  17295. ;; | `hide-sublevels' | overridden | no replacement |
  17296. ;; | `hide-other' | overridden | no replacement |
  17297. ;; Make `C-c C-x' a prefix key
  17298. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17299. ;; TAB key with modifiers
  17300. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17301. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17302. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17303. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17304. ;; The following line is necessary under Suse GNU/Linux
  17305. (unless (featurep 'xemacs)
  17306. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17307. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17308. (define-key org-mode-map [backtab] 'org-shifttab)
  17309. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17310. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17311. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17312. ;; Cursor keys with modifiers
  17313. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17314. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17315. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17316. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17317. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17318. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17319. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17320. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17321. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17322. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17323. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17324. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17325. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17326. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17327. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17328. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17329. ;; Babel keys
  17330. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17331. (mapc (lambda (pair)
  17332. (define-key org-babel-map (car pair) (cdr pair)))
  17333. org-babel-key-bindings)
  17334. ;;; Extra keys for tty access.
  17335. ;; We only set them when really needed because otherwise the
  17336. ;; menus don't show the simple keys
  17337. (when (or org-use-extra-keys
  17338. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17339. (not window-system))
  17340. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17341. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17342. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17343. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17344. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17345. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17346. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17347. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17348. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17349. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17350. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17351. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17352. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17353. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17354. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17355. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17356. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17357. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17358. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17359. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17360. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17361. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17362. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17363. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17364. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17365. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17366. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17367. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17368. ;; All the other keys
  17369. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17370. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17371. (if (boundp 'narrow-map)
  17372. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17373. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17374. (if (boundp 'narrow-map)
  17375. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17376. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17377. (if (boundp 'narrow-map)
  17378. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17379. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17380. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17381. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17382. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17383. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17384. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17385. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17386. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17387. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17388. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17389. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17390. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17391. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17392. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17393. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17394. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17395. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17396. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17397. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17398. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17399. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17400. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17401. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17402. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17403. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17404. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17405. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17406. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17407. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17408. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17409. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17410. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17411. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17412. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17413. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17414. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17415. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17416. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17417. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17418. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17419. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17420. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17421. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17422. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17423. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17424. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17425. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17426. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17427. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17428. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17429. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17430. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17431. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17432. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17433. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17434. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17435. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17436. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17437. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17438. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17439. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  17440. (org-defkey org-mode-map "\C-m" 'org-return)
  17441. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17442. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17443. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17444. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17445. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17446. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17447. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17448. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17449. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17450. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17451. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17452. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17453. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17454. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17455. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17456. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17457. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17458. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17459. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17460. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17461. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17462. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17463. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17464. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17465. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17466. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17467. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17468. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17469. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17470. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17471. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17472. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17473. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17474. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17475. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17476. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17477. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17478. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17479. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17480. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17481. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17482. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17483. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17484. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17485. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17486. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17487. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17488. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17489. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17490. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17491. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17492. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17493. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17494. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17495. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17496. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17497. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17498. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17499. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17500. (when (featurep 'xemacs)
  17501. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17502. (defconst org-speed-commands-default
  17503. '(
  17504. ("Outline Navigation")
  17505. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17506. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17507. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17508. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17509. ("F" . org-next-block)
  17510. ("B" . org-previous-block)
  17511. ("u" . (org-speed-move-safe 'outline-up-heading))
  17512. ("j" . org-goto)
  17513. ("g" . (org-refile t))
  17514. ("Outline Visibility")
  17515. ("c" . org-cycle)
  17516. ("C" . org-shifttab)
  17517. (" " . org-display-outline-path)
  17518. ("s" . org-narrow-to-subtree)
  17519. ("=" . org-columns)
  17520. ("Outline Structure Editing")
  17521. ("U" . org-shiftmetaup)
  17522. ("D" . org-shiftmetadown)
  17523. ("r" . org-metaright)
  17524. ("l" . org-metaleft)
  17525. ("R" . org-shiftmetaright)
  17526. ("L" . org-shiftmetaleft)
  17527. ("i" . (progn (forward-char 1) (call-interactively
  17528. 'org-insert-heading-respect-content)))
  17529. ("^" . org-sort)
  17530. ("w" . org-refile)
  17531. ("a" . org-archive-subtree-default-with-confirmation)
  17532. ("@" . org-mark-subtree)
  17533. ("#" . org-toggle-comment)
  17534. ("Clock Commands")
  17535. ("I" . org-clock-in)
  17536. ("O" . org-clock-out)
  17537. ("Meta Data Editing")
  17538. ("t" . org-todo)
  17539. ("," . (org-priority))
  17540. ("0" . (org-priority ?\ ))
  17541. ("1" . (org-priority ?A))
  17542. ("2" . (org-priority ?B))
  17543. ("3" . (org-priority ?C))
  17544. (":" . org-set-tags-command)
  17545. ("e" . org-set-effort)
  17546. ("E" . org-inc-effort)
  17547. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17548. (org-entry-put (point) "APPT_WARNTIME" m)))
  17549. ("Agenda Views etc")
  17550. ("v" . org-agenda)
  17551. ("/" . org-sparse-tree)
  17552. ("Misc")
  17553. ("o" . org-open-at-point)
  17554. ("?" . org-speed-command-help)
  17555. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17556. (">" . (org-agenda-remove-restriction-lock))
  17557. )
  17558. "The default speed commands.")
  17559. (defun org-print-speed-command (e)
  17560. (if (> (length (car e)) 1)
  17561. (progn
  17562. (princ "\n")
  17563. (princ (car e))
  17564. (princ "\n")
  17565. (princ (make-string (length (car e)) ?-))
  17566. (princ "\n"))
  17567. (princ (car e))
  17568. (princ " ")
  17569. (if (symbolp (cdr e))
  17570. (princ (symbol-name (cdr e)))
  17571. (prin1 (cdr e)))
  17572. (princ "\n")))
  17573. (defun org-speed-command-help ()
  17574. "Show the available speed commands."
  17575. (interactive)
  17576. (if (not org-use-speed-commands)
  17577. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  17578. (with-output-to-temp-buffer "*Help*"
  17579. (princ "User-defined Speed commands\n===========================\n")
  17580. (mapc 'org-print-speed-command org-speed-commands-user)
  17581. (princ "\n")
  17582. (princ "Built-in Speed commands\n=======================\n")
  17583. (mapc 'org-print-speed-command org-speed-commands-default))
  17584. (with-current-buffer "*Help*"
  17585. (setq truncate-lines t))))
  17586. (defun org-speed-move-safe (cmd)
  17587. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17588. If not, return to the original position and throw an error."
  17589. (interactive)
  17590. (let ((pos (point)))
  17591. (call-interactively cmd)
  17592. (unless (and (bolp) (org-at-heading-p))
  17593. (goto-char pos)
  17594. (error "Boundary reached while executing %s" cmd))))
  17595. (defvar org-self-insert-command-undo-counter 0)
  17596. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17597. (defvar org-speed-command nil)
  17598. (define-obsolete-function-alias
  17599. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17600. (defun org-speed-command-activate (keys)
  17601. "Hook for activating single-letter speed commands.
  17602. `org-speed-commands-default' specifies a minimal command set.
  17603. Use `org-speed-commands-user' for further customization."
  17604. (when (or (and (bolp) (looking-at org-outline-regexp))
  17605. (and (functionp org-use-speed-commands)
  17606. (funcall org-use-speed-commands)))
  17607. (cdr (assoc keys (append org-speed-commands-user
  17608. org-speed-commands-default)))))
  17609. (define-obsolete-function-alias
  17610. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17611. (defun org-babel-speed-command-activate (keys)
  17612. "Hook for activating single-letter code block commands."
  17613. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17614. (cdr (assoc keys org-babel-key-bindings))))
  17615. (defcustom org-speed-command-hook
  17616. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17617. "Hook for activating speed commands at strategic locations.
  17618. Hook functions are called in sequence until a valid handler is
  17619. found.
  17620. Each hook takes a single argument, a user-pressed command key
  17621. which is also a `self-insert-command' from the global map.
  17622. Within the hook, examine the cursor position and the command key
  17623. and return nil or a valid handler as appropriate. Handler could
  17624. be one of an interactive command, a function, or a form.
  17625. Set `org-use-speed-commands' to non-nil value to enable this
  17626. hook. The default setting is `org-speed-command-activate'."
  17627. :group 'org-structure
  17628. :version "24.1"
  17629. :type 'hook)
  17630. (defun org-self-insert-command (N)
  17631. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17632. If the cursor is in a table looking at whitespace, the whitespace is
  17633. overwritten, and the table is not marked as requiring realignment."
  17634. (interactive "p")
  17635. (org-check-before-invisible-edit 'insert)
  17636. (cond
  17637. ((and org-use-speed-commands
  17638. (setq org-speed-command
  17639. (run-hook-with-args-until-success
  17640. 'org-speed-command-hook (this-command-keys))))
  17641. (cond
  17642. ((commandp org-speed-command)
  17643. (setq this-command org-speed-command)
  17644. (call-interactively org-speed-command))
  17645. ((functionp org-speed-command)
  17646. (funcall org-speed-command))
  17647. ((and org-speed-command (listp org-speed-command))
  17648. (eval org-speed-command))
  17649. (t (let (org-use-speed-commands)
  17650. (call-interactively 'org-self-insert-command)))))
  17651. ((and
  17652. (org-table-p)
  17653. (progn
  17654. ;; check if we blank the field, and if that triggers align
  17655. (and (featurep 'org-table) org-table-auto-blank-field
  17656. (member last-command
  17657. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17658. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17659. ;; got extra space, this field does not determine column width
  17660. (let (org-table-may-need-update) (org-table-blank-field))
  17661. ;; no extra space, this field may determine column width
  17662. (org-table-blank-field)))
  17663. t)
  17664. (eq N 1)
  17665. (looking-at "[^|\n]* |"))
  17666. (let (org-table-may-need-update)
  17667. (goto-char (1- (match-end 0)))
  17668. (backward-delete-char 1)
  17669. (goto-char (match-beginning 0))
  17670. (self-insert-command N)))
  17671. (t
  17672. (setq org-table-may-need-update t)
  17673. (self-insert-command N)
  17674. (org-fix-tags-on-the-fly)
  17675. (if org-self-insert-cluster-for-undo
  17676. (if (not (eq last-command 'org-self-insert-command))
  17677. (setq org-self-insert-command-undo-counter 1)
  17678. (if (>= org-self-insert-command-undo-counter 20)
  17679. (setq org-self-insert-command-undo-counter 1)
  17680. (and (> org-self-insert-command-undo-counter 0)
  17681. buffer-undo-list (listp buffer-undo-list)
  17682. (not (cadr buffer-undo-list)) ; remove nil entry
  17683. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17684. (setq org-self-insert-command-undo-counter
  17685. (1+ org-self-insert-command-undo-counter))))))))
  17686. (defun org-check-before-invisible-edit (kind)
  17687. "Check is editing if kind KIND would be dangerous with invisible text around.
  17688. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17689. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17690. (if (and org-catch-invisible-edits
  17691. (or (not (boundp 'visible-mode)) (not visible-mode))
  17692. (or (get-char-property (point) 'invisible)
  17693. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17694. ;; OK, we need to take a closer look
  17695. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17696. (invisible-before-point (if (bobp) nil (get-char-property
  17697. (1- (point)) 'invisible)))
  17698. (border-and-ok-direction
  17699. (or
  17700. ;; Check if we are acting predictably before invisible text
  17701. (and invisible-at-point (not invisible-before-point)
  17702. (memq kind '(insert delete-backward)))
  17703. ;; Check if we are acting predictably after invisible text
  17704. ;; This works not well, and I have turned it off. It seems
  17705. ;; better to always show and stop after invisible text.
  17706. ;; (and (not invisible-at-point) invisible-before-point
  17707. ;; (memq kind '(insert delete)))
  17708. )))
  17709. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17710. (memq invisible-before-point '(outline org-hide-block t)))
  17711. (if (eq org-catch-invisible-edits 'error)
  17712. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  17713. (if (and org-custom-properties-overlays
  17714. (y-or-n-p "Display invisible properties in this buffer? "))
  17715. (org-toggle-custom-properties-visibility)
  17716. ;; Make the area visible
  17717. (save-excursion
  17718. (if invisible-before-point
  17719. (goto-char (previous-single-char-property-change
  17720. (point) 'invisible)))
  17721. (org-cycle))
  17722. (cond
  17723. ((eq org-catch-invisible-edits 'show)
  17724. ;; That's it, we do the edit after showing
  17725. (message
  17726. "Unfolding invisible region around point before editing")
  17727. (sit-for 1))
  17728. ((and (eq org-catch-invisible-edits 'smart)
  17729. border-and-ok-direction)
  17730. (message "Unfolding invisible region around point before editing"))
  17731. (t
  17732. ;; Don't do the edit, make the user repeat it in full visibility
  17733. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17734. (defun org-fix-tags-on-the-fly ()
  17735. (when (and (equal (char-after (point-at-bol)) ?*)
  17736. (org-at-heading-p))
  17737. (org-align-tags-here org-tags-column)))
  17738. (defun org-delete-backward-char (N)
  17739. "Like `delete-backward-char', insert whitespace at field end in tables.
  17740. When deleting backwards, in tables this function will insert whitespace in
  17741. front of the next \"|\" separator, to keep the table aligned. The table will
  17742. still be marked for re-alignment if the field did fill the entire column,
  17743. because, in this case the deletion might narrow the column."
  17744. (interactive "p")
  17745. (save-match-data
  17746. (org-check-before-invisible-edit 'delete-backward)
  17747. (if (and (org-table-p)
  17748. (eq N 1)
  17749. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17750. (looking-at ".*?|"))
  17751. (let ((pos (point))
  17752. (noalign (looking-at "[^|\n\r]* |"))
  17753. (c org-table-may-need-update))
  17754. (backward-delete-char N)
  17755. (if (not overwrite-mode)
  17756. (progn
  17757. (skip-chars-forward "^|")
  17758. (insert " ")
  17759. (goto-char (1- pos))))
  17760. ;; noalign: if there were two spaces at the end, this field
  17761. ;; does not determine the width of the column.
  17762. (if noalign (setq org-table-may-need-update c)))
  17763. (backward-delete-char N)
  17764. (org-fix-tags-on-the-fly))))
  17765. (defun org-delete-char (N)
  17766. "Like `delete-char', but insert whitespace at field end in tables.
  17767. When deleting characters, in tables this function will insert whitespace in
  17768. front of the next \"|\" separator, to keep the table aligned. The table will
  17769. still be marked for re-alignment if the field did fill the entire column,
  17770. because, in this case the deletion might narrow the column."
  17771. (interactive "p")
  17772. (save-match-data
  17773. (org-check-before-invisible-edit 'delete)
  17774. (if (and (org-table-p)
  17775. (not (bolp))
  17776. (not (= (char-after) ?|))
  17777. (eq N 1))
  17778. (if (looking-at ".*?|")
  17779. (let ((pos (point))
  17780. (noalign (looking-at "[^|\n\r]* |"))
  17781. (c org-table-may-need-update))
  17782. (replace-match
  17783. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17784. (goto-char pos)
  17785. ;; noalign: if there were two spaces at the end, this field
  17786. ;; does not determine the width of the column.
  17787. (if noalign (setq org-table-may-need-update c)))
  17788. (delete-char N))
  17789. (delete-char N)
  17790. (org-fix-tags-on-the-fly))))
  17791. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17792. (put 'org-self-insert-command 'delete-selection t)
  17793. (put 'orgtbl-self-insert-command 'delete-selection t)
  17794. (put 'org-delete-char 'delete-selection 'supersede)
  17795. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17796. (put 'org-yank 'delete-selection 'yank)
  17797. ;; Make `flyspell-mode' delay after some commands
  17798. (put 'org-self-insert-command 'flyspell-delayed t)
  17799. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17800. (put 'org-delete-char 'flyspell-delayed t)
  17801. (put 'org-delete-backward-char 'flyspell-delayed t)
  17802. ;; Make pabbrev-mode expand after org-mode commands
  17803. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17804. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17805. ;; How to do this: Measure non-white length of current string
  17806. ;; If equal to column width, we should realign.
  17807. (defun org-remap (map &rest commands)
  17808. "In MAP, remap the functions given in COMMANDS.
  17809. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17810. (let (new old)
  17811. (while commands
  17812. (setq old (pop commands) new (pop commands))
  17813. (if (fboundp 'command-remapping)
  17814. (org-defkey map (vector 'remap old) new)
  17815. (substitute-key-definition old new map global-map)))))
  17816. (defun org-transpose-words ()
  17817. "Transpose words for Org.
  17818. This uses the `org-mode-transpose-word-syntax-table' syntax
  17819. table, which interprets characters in `org-emphasis-alist' as
  17820. word constituants."
  17821. (interactive)
  17822. (with-syntax-table org-mode-transpose-word-syntax-table
  17823. (call-interactively 'transpose-words)))
  17824. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17825. (when (eq org-enable-table-editor 'optimized)
  17826. ;; If the user wants maximum table support, we need to hijack
  17827. ;; some standard editing functions
  17828. (org-remap org-mode-map
  17829. 'self-insert-command 'org-self-insert-command
  17830. 'delete-char 'org-delete-char
  17831. 'delete-backward-char 'org-delete-backward-char)
  17832. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17833. (defvar org-ctrl-c-ctrl-c-hook nil
  17834. "Hook for functions attaching themselves to `C-c C-c'.
  17835. This can be used to add additional functionality to the C-c C-c
  17836. key which executes context-dependent commands. This hook is run
  17837. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17838. run after the last test.
  17839. Each function will be called with no arguments. The function
  17840. must check if the context is appropriate for it to act. If yes,
  17841. it should do its thing and then return a non-nil value. If the
  17842. context is wrong, just do nothing and return nil.")
  17843. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17844. "Hook for functions attaching themselves to `C-c C-c'.
  17845. This can be used to add additional functionality to the C-c C-c
  17846. key which executes context-dependent commands. This hook is run
  17847. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17848. before the first test.
  17849. Each function will be called with no arguments. The function
  17850. must check if the context is appropriate for it to act. If yes,
  17851. it should do its thing and then return a non-nil value. If the
  17852. context is wrong, just do nothing and return nil.")
  17853. (defvar org-tab-first-hook nil
  17854. "Hook for functions to attach themselves to TAB.
  17855. See `org-ctrl-c-ctrl-c-hook' for more information.
  17856. This hook runs as the first action when TAB is pressed, even before
  17857. `org-cycle' messes around with the `outline-regexp' to cater for
  17858. inline tasks and plain list item folding.
  17859. If any function in this hook returns t, any other actions that
  17860. would have been caused by TAB (such as table field motion or visibility
  17861. cycling) will not occur.")
  17862. (defvar org-tab-after-check-for-table-hook nil
  17863. "Hook for functions to attach themselves to TAB.
  17864. See `org-ctrl-c-ctrl-c-hook' for more information.
  17865. This hook runs after it has been established that the cursor is not in a
  17866. table, but before checking if the cursor is in a headline or if global cycling
  17867. should be done.
  17868. If any function in this hook returns t, not other actions like visibility
  17869. cycling will be done.")
  17870. (defvar org-tab-after-check-for-cycling-hook nil
  17871. "Hook for functions to attach themselves to TAB.
  17872. See `org-ctrl-c-ctrl-c-hook' for more information.
  17873. This hook runs after it has been established that not table field motion and
  17874. not visibility should be done because of current context. This is probably
  17875. the place where a package like yasnippets can hook in.")
  17876. (defvar org-tab-before-tab-emulation-hook nil
  17877. "Hook for functions to attach themselves to TAB.
  17878. See `org-ctrl-c-ctrl-c-hook' for more information.
  17879. This hook runs after every other options for TAB have been exhausted, but
  17880. before indentation and \t insertion takes place.")
  17881. (defvar org-metaleft-hook nil
  17882. "Hook for functions attaching themselves to `M-left'.
  17883. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17884. (defvar org-metaright-hook nil
  17885. "Hook for functions attaching themselves to `M-right'.
  17886. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17887. (defvar org-metaup-hook nil
  17888. "Hook for functions attaching themselves to `M-up'.
  17889. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17890. (defvar org-metadown-hook nil
  17891. "Hook for functions attaching themselves to `M-down'.
  17892. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17893. (defvar org-shiftmetaleft-hook nil
  17894. "Hook for functions attaching themselves to `M-S-left'.
  17895. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17896. (defvar org-shiftmetaright-hook nil
  17897. "Hook for functions attaching themselves to `M-S-right'.
  17898. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17899. (defvar org-shiftmetaup-hook nil
  17900. "Hook for functions attaching themselves to `M-S-up'.
  17901. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17902. (defvar org-shiftmetadown-hook nil
  17903. "Hook for functions attaching themselves to `M-S-down'.
  17904. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17905. (defvar org-metareturn-hook nil
  17906. "Hook for functions attaching themselves to `M-RET'.
  17907. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17908. (defvar org-shiftup-hook nil
  17909. "Hook for functions attaching themselves to `S-up'.
  17910. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17911. (defvar org-shiftup-final-hook nil
  17912. "Hook for functions attaching themselves to `S-up'.
  17913. This one runs after all other options except shift-select have been excluded.
  17914. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17915. (defvar org-shiftdown-hook nil
  17916. "Hook for functions attaching themselves to `S-down'.
  17917. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17918. (defvar org-shiftdown-final-hook nil
  17919. "Hook for functions attaching themselves to `S-down'.
  17920. This one runs after all other options except shift-select have been excluded.
  17921. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17922. (defvar org-shiftleft-hook nil
  17923. "Hook for functions attaching themselves to `S-left'.
  17924. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17925. (defvar org-shiftleft-final-hook nil
  17926. "Hook for functions attaching themselves to `S-left'.
  17927. This one runs after all other options except shift-select have been excluded.
  17928. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17929. (defvar org-shiftright-hook nil
  17930. "Hook for functions attaching themselves to `S-right'.
  17931. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17932. (defvar org-shiftright-final-hook nil
  17933. "Hook for functions attaching themselves to `S-right'.
  17934. This one runs after all other options except shift-select have been excluded.
  17935. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17936. (defun org-modifier-cursor-error ()
  17937. "Throw an error, a modified cursor command was applied in wrong context."
  17938. (user-error "This command is active in special context like tables, headlines or items"))
  17939. (defun org-shiftselect-error ()
  17940. "Throw an error because Shift-Cursor command was applied in wrong context."
  17941. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17942. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17943. (user-error "This command works only in special context like headlines or timestamps")))
  17944. (defun org-call-for-shift-select (cmd)
  17945. (let ((this-command-keys-shift-translated t))
  17946. (call-interactively cmd)))
  17947. (defun org-shifttab (&optional arg)
  17948. "Global visibility cycling or move to previous table field.
  17949. Call `org-table-previous-field' within a table.
  17950. When ARG is nil, cycle globally through visibility states.
  17951. When ARG is a numeric prefix, show contents of this level."
  17952. (interactive "P")
  17953. (cond
  17954. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17955. ((integerp arg)
  17956. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17957. (message "Content view to level: %d" arg)
  17958. (org-content (prefix-numeric-value arg2))
  17959. (org-cycle-show-empty-lines t)
  17960. (setq org-cycle-global-status 'overview)))
  17961. (t (call-interactively 'org-global-cycle))))
  17962. (defun org-shiftmetaleft ()
  17963. "Promote subtree or delete table column.
  17964. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17965. `org-table-delete-column', depending on context. See the
  17966. individual commands for more information."
  17967. (interactive)
  17968. (cond
  17969. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17970. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17971. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17972. ((if (not (org-region-active-p)) (org-at-item-p)
  17973. (save-excursion (goto-char (region-beginning))
  17974. (org-at-item-p)))
  17975. (call-interactively 'org-outdent-item-tree))
  17976. (t (org-modifier-cursor-error))))
  17977. (defun org-shiftmetaright ()
  17978. "Demote subtree or insert table column.
  17979. Calls `org-demote-subtree', `org-indent-item-tree', or
  17980. `org-table-insert-column', depending on context. See the
  17981. individual commands for more information."
  17982. (interactive)
  17983. (cond
  17984. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17985. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17986. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17987. ((if (not (org-region-active-p)) (org-at-item-p)
  17988. (save-excursion (goto-char (region-beginning))
  17989. (org-at-item-p)))
  17990. (call-interactively 'org-indent-item-tree))
  17991. (t (org-modifier-cursor-error))))
  17992. (defun org-shiftmetaup (&optional arg)
  17993. "Move subtree up or kill table row.
  17994. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17995. `org-move-item-up' or `org-timestamp-up', depending on context.
  17996. See the individual commands for more information."
  17997. (interactive "P")
  17998. (cond
  17999. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18000. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18001. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18002. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18003. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18004. (call-interactively 'org-timestamp-up)))
  18005. (t (call-interactively 'org-drag-line-backward))))
  18006. (defun org-shiftmetadown (&optional arg)
  18007. "Move subtree down or insert table row.
  18008. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18009. `org-move-item-down' or `org-timestamp-up', depending on context.
  18010. See the individual commands for more information."
  18011. (interactive "P")
  18012. (cond
  18013. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18014. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18015. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18016. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18017. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18018. (call-interactively 'org-timestamp-down)))
  18019. (t (call-interactively 'org-drag-line-forward))))
  18020. (defsubst org-hidden-tree-error ()
  18021. (user-error
  18022. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18023. (defun org-metaleft (&optional arg)
  18024. "Promote heading or move table column to left.
  18025. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18026. With no specific context, calls the Emacs default `backward-word'.
  18027. See the individual commands for more information."
  18028. (interactive "P")
  18029. (cond
  18030. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18031. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18032. ((org-with-limited-levels
  18033. (or (org-at-heading-p)
  18034. (and (org-region-active-p)
  18035. (save-excursion
  18036. (goto-char (region-beginning))
  18037. (org-at-heading-p)))))
  18038. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18039. (call-interactively 'org-do-promote))
  18040. ;; At an inline task.
  18041. ((org-at-heading-p)
  18042. (call-interactively 'org-inlinetask-promote))
  18043. ((or (org-at-item-p)
  18044. (and (org-region-active-p)
  18045. (save-excursion
  18046. (goto-char (region-beginning))
  18047. (org-at-item-p))))
  18048. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18049. (call-interactively 'org-outdent-item))
  18050. (t (call-interactively 'backward-word))))
  18051. (defun org-metaright (&optional arg)
  18052. "Demote a subtree, a list item or move table column to right.
  18053. In front of a drawer or a block keyword, indent it correctly.
  18054. With no specific context, calls the Emacs default `forward-word'.
  18055. See the individual commands for more information."
  18056. (interactive "P")
  18057. (cond
  18058. ((run-hook-with-args-until-success 'org-metaright-hook))
  18059. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18060. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18061. ((org-at-block-p) (call-interactively 'org-indent-block))
  18062. ((org-with-limited-levels
  18063. (or (org-at-heading-p)
  18064. (and (org-region-active-p)
  18065. (save-excursion
  18066. (goto-char (region-beginning))
  18067. (org-at-heading-p)))))
  18068. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18069. (call-interactively 'org-do-demote))
  18070. ;; At an inline task.
  18071. ((org-at-heading-p)
  18072. (call-interactively 'org-inlinetask-demote))
  18073. ((or (org-at-item-p)
  18074. (and (org-region-active-p)
  18075. (save-excursion
  18076. (goto-char (region-beginning))
  18077. (org-at-item-p))))
  18078. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18079. (call-interactively 'org-indent-item))
  18080. (t (call-interactively 'forward-word))))
  18081. (defun org-check-for-hidden (what)
  18082. "Check if there are hidden headlines/items in the current visual line.
  18083. WHAT can be either `headlines' or `items'. If the current line is
  18084. an outline or item heading and it has a folded subtree below it,
  18085. this function returns t, nil otherwise."
  18086. (let ((re (cond
  18087. ((eq what 'headlines) org-outline-regexp-bol)
  18088. ((eq what 'items) (org-item-beginning-re))
  18089. (t (error "This should not happen"))))
  18090. beg end)
  18091. (save-excursion
  18092. (catch 'exit
  18093. (unless (org-region-active-p)
  18094. (setq beg (point-at-bol))
  18095. (beginning-of-line 2)
  18096. (while (and (not (eobp)) ;; this is like `next-line'
  18097. (get-char-property (1- (point)) 'invisible))
  18098. (beginning-of-line 2))
  18099. (setq end (point))
  18100. (goto-char beg)
  18101. (goto-char (point-at-eol))
  18102. (setq end (max end (point)))
  18103. (while (re-search-forward re end t)
  18104. (if (get-char-property (match-beginning 0) 'invisible)
  18105. (throw 'exit t))))
  18106. nil))))
  18107. (defun org-metaup (&optional arg)
  18108. "Move subtree up or move table row up.
  18109. Calls `org-move-subtree-up' or `org-table-move-row' or
  18110. `org-move-item-up', depending on context. See the individual commands
  18111. for more information."
  18112. (interactive "P")
  18113. (cond
  18114. ((run-hook-with-args-until-success 'org-metaup-hook))
  18115. ((org-region-active-p)
  18116. (let* ((a (min (region-beginning) (region-end)))
  18117. (b (1- (max (region-beginning) (region-end))))
  18118. (c (save-excursion (goto-char a)
  18119. (move-beginning-of-line 0)))
  18120. (d (save-excursion (goto-char a)
  18121. (move-end-of-line 0) (point))))
  18122. (transpose-regions a b c d)
  18123. (goto-char c)))
  18124. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18125. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18126. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18127. (t (org-drag-element-backward))))
  18128. (defun org-metadown (&optional arg)
  18129. "Move subtree down or move table row down.
  18130. Calls `org-move-subtree-down' or `org-table-move-row' or
  18131. `org-move-item-down', depending on context. See the individual
  18132. commands for more information."
  18133. (interactive "P")
  18134. (cond
  18135. ((run-hook-with-args-until-success 'org-metadown-hook))
  18136. ((org-region-active-p)
  18137. (let* ((a (min (region-beginning) (region-end)))
  18138. (b (max (region-beginning) (region-end)))
  18139. (c (save-excursion (goto-char b)
  18140. (move-beginning-of-line 1)))
  18141. (d (save-excursion (goto-char b)
  18142. (move-end-of-line 1) (1+ (point)))))
  18143. (transpose-regions a b c d)
  18144. (goto-char d)))
  18145. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18146. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18147. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18148. (t (org-drag-element-forward))))
  18149. (defun org-shiftup (&optional arg)
  18150. "Increase item in timestamp or increase priority of current headline.
  18151. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18152. depending on context. See the individual commands for more information."
  18153. (interactive "P")
  18154. (cond
  18155. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18156. ((and org-support-shift-select (org-region-active-p))
  18157. (org-call-for-shift-select 'previous-line))
  18158. ((org-at-timestamp-p t)
  18159. (call-interactively (if org-edit-timestamp-down-means-later
  18160. 'org-timestamp-down 'org-timestamp-up)))
  18161. ((and (not (eq org-support-shift-select 'always))
  18162. org-enable-priority-commands
  18163. (org-at-heading-p))
  18164. (call-interactively 'org-priority-up))
  18165. ((and (not org-support-shift-select) (org-at-item-p))
  18166. (call-interactively 'org-previous-item))
  18167. ((org-clocktable-try-shift 'up arg))
  18168. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18169. (org-support-shift-select
  18170. (org-call-for-shift-select 'previous-line))
  18171. (t (org-shiftselect-error))))
  18172. (defun org-shiftdown (&optional arg)
  18173. "Decrease item in timestamp or decrease priority of current headline.
  18174. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18175. depending on context. See the individual commands for more information."
  18176. (interactive "P")
  18177. (cond
  18178. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18179. ((and org-support-shift-select (org-region-active-p))
  18180. (org-call-for-shift-select 'next-line))
  18181. ((org-at-timestamp-p t)
  18182. (call-interactively (if org-edit-timestamp-down-means-later
  18183. 'org-timestamp-up 'org-timestamp-down)))
  18184. ((and (not (eq org-support-shift-select 'always))
  18185. org-enable-priority-commands
  18186. (org-at-heading-p))
  18187. (call-interactively 'org-priority-down))
  18188. ((and (not org-support-shift-select) (org-at-item-p))
  18189. (call-interactively 'org-next-item))
  18190. ((org-clocktable-try-shift 'down arg))
  18191. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18192. (org-support-shift-select
  18193. (org-call-for-shift-select 'next-line))
  18194. (t (org-shiftselect-error))))
  18195. (defun org-shiftright (&optional arg)
  18196. "Cycle the thing at point or in the current line, depending on context.
  18197. Depending on context, this does one of the following:
  18198. - switch a timestamp at point one day into the future
  18199. - on a headline, switch to the next TODO keyword.
  18200. - on an item, switch entire list to the next bullet type
  18201. - on a property line, switch to the next allowed value
  18202. - on a clocktable definition line, move time block into the future"
  18203. (interactive "P")
  18204. (cond
  18205. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18206. ((and org-support-shift-select (org-region-active-p))
  18207. (org-call-for-shift-select 'forward-char))
  18208. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18209. ((and (not (eq org-support-shift-select 'always))
  18210. (org-at-heading-p))
  18211. (let ((org-inhibit-logging
  18212. (not org-treat-S-cursor-todo-selection-as-state-change))
  18213. (org-inhibit-blocking
  18214. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18215. (org-call-with-arg 'org-todo 'right)))
  18216. ((or (and org-support-shift-select
  18217. (not (eq org-support-shift-select 'always))
  18218. (org-at-item-bullet-p))
  18219. (and (not org-support-shift-select) (org-at-item-p)))
  18220. (org-call-with-arg 'org-cycle-list-bullet nil))
  18221. ((and (not (eq org-support-shift-select 'always))
  18222. (org-at-property-p))
  18223. (call-interactively 'org-property-next-allowed-value))
  18224. ((org-clocktable-try-shift 'right arg))
  18225. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18226. (org-support-shift-select
  18227. (org-call-for-shift-select 'forward-char))
  18228. (t (org-shiftselect-error))))
  18229. (defun org-shiftleft (&optional arg)
  18230. "Cycle the thing at point or in the current line, depending on context.
  18231. Depending on context, this does one of the following:
  18232. - switch a timestamp at point one day into the past
  18233. - on a headline, switch to the previous TODO keyword.
  18234. - on an item, switch entire list to the previous bullet type
  18235. - on a property line, switch to the previous allowed value
  18236. - on a clocktable definition line, move time block into the past"
  18237. (interactive "P")
  18238. (cond
  18239. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18240. ((and org-support-shift-select (org-region-active-p))
  18241. (org-call-for-shift-select 'backward-char))
  18242. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18243. ((and (not (eq org-support-shift-select 'always))
  18244. (org-at-heading-p))
  18245. (let ((org-inhibit-logging
  18246. (not org-treat-S-cursor-todo-selection-as-state-change))
  18247. (org-inhibit-blocking
  18248. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18249. (org-call-with-arg 'org-todo 'left)))
  18250. ((or (and org-support-shift-select
  18251. (not (eq org-support-shift-select 'always))
  18252. (org-at-item-bullet-p))
  18253. (and (not org-support-shift-select) (org-at-item-p)))
  18254. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18255. ((and (not (eq org-support-shift-select 'always))
  18256. (org-at-property-p))
  18257. (call-interactively 'org-property-previous-allowed-value))
  18258. ((org-clocktable-try-shift 'left arg))
  18259. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18260. (org-support-shift-select
  18261. (org-call-for-shift-select 'backward-char))
  18262. (t (org-shiftselect-error))))
  18263. (defun org-shiftcontrolright ()
  18264. "Switch to next TODO set."
  18265. (interactive)
  18266. (cond
  18267. ((and org-support-shift-select (org-region-active-p))
  18268. (org-call-for-shift-select 'forward-word))
  18269. ((and (not (eq org-support-shift-select 'always))
  18270. (org-at-heading-p))
  18271. (org-call-with-arg 'org-todo 'nextset))
  18272. (org-support-shift-select
  18273. (org-call-for-shift-select 'forward-word))
  18274. (t (org-shiftselect-error))))
  18275. (defun org-shiftcontrolleft ()
  18276. "Switch to previous TODO set."
  18277. (interactive)
  18278. (cond
  18279. ((and org-support-shift-select (org-region-active-p))
  18280. (org-call-for-shift-select 'backward-word))
  18281. ((and (not (eq org-support-shift-select 'always))
  18282. (org-at-heading-p))
  18283. (org-call-with-arg 'org-todo 'previousset))
  18284. (org-support-shift-select
  18285. (org-call-for-shift-select 'backward-word))
  18286. (t (org-shiftselect-error))))
  18287. (defun org-shiftcontrolup (&optional n)
  18288. "Change timestamps synchronously up in CLOCK log lines.
  18289. Optional argument N tells to change by that many units."
  18290. (interactive "P")
  18291. (cond ((and (not org-support-shift-select)
  18292. (org-at-clock-log-p)
  18293. (org-at-timestamp-p t))
  18294. (org-clock-timestamps-up n))
  18295. (t (org-shiftselect-error))))
  18296. (defun org-shiftcontroldown (&optional n)
  18297. "Change timestamps synchronously down in CLOCK log lines.
  18298. Optional argument N tells to change by that many units."
  18299. (interactive "P")
  18300. (cond ((and (not org-support-shift-select)
  18301. (org-at-clock-log-p)
  18302. (org-at-timestamp-p t))
  18303. (org-clock-timestamps-down n))
  18304. (t (org-shiftselect-error))))
  18305. (defun org-ctrl-c-ret ()
  18306. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18307. (interactive)
  18308. (cond
  18309. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18310. (t (call-interactively 'org-insert-heading))))
  18311. (defun org-find-visible ()
  18312. (let ((s (point)))
  18313. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18314. (get-char-property s 'invisible)))
  18315. s))
  18316. (defun org-find-invisible ()
  18317. (let ((s (point)))
  18318. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18319. (not (get-char-property s 'invisible))))
  18320. s))
  18321. (defun org-copy-visible (beg end)
  18322. "Copy the visible parts of the region."
  18323. (interactive "r")
  18324. (let (snippets s)
  18325. (save-excursion
  18326. (save-restriction
  18327. (narrow-to-region beg end)
  18328. (setq s (goto-char (point-min)))
  18329. (while (not (= (point) (point-max)))
  18330. (goto-char (org-find-invisible))
  18331. (push (buffer-substring s (point)) snippets)
  18332. (setq s (goto-char (org-find-visible))))))
  18333. (kill-new (apply 'concat (nreverse snippets)))))
  18334. (defun org-copy-special ()
  18335. "Copy region in table or copy current subtree.
  18336. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18337. See the individual commands for more information."
  18338. (interactive)
  18339. (call-interactively
  18340. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18341. (defun org-cut-special ()
  18342. "Cut region in table or cut current subtree.
  18343. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18344. See the individual commands for more information."
  18345. (interactive)
  18346. (call-interactively
  18347. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18348. (defun org-paste-special (arg)
  18349. "Paste rectangular region into table, or past subtree relative to level.
  18350. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18351. See the individual commands for more information."
  18352. (interactive "P")
  18353. (if (org-at-table-p)
  18354. (org-table-paste-rectangle)
  18355. (org-paste-subtree arg)))
  18356. (defsubst org-in-fixed-width-region-p ()
  18357. "Is point in a fixed-width region?"
  18358. (save-match-data
  18359. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18360. (defun org-edit-special (&optional arg)
  18361. "Call a special editor for the element at point.
  18362. When at a table, call the formula editor with `org-table-edit-formulas'.
  18363. When in a source code block, call `org-edit-src-code'.
  18364. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18365. When at an #+INCLUDE keyword, visit the included file.
  18366. On a link, call `ffap' to visit the link at point.
  18367. Otherwise, return a user error."
  18368. (interactive "P")
  18369. (let ((element (org-element-at-point)))
  18370. (assert (not buffer-read-only) nil
  18371. "Buffer is read-only: %s" (buffer-name))
  18372. (case (org-element-type element)
  18373. (src-block
  18374. (if (not arg) (org-edit-src-code)
  18375. (let* ((info (org-babel-get-src-block-info))
  18376. (lang (nth 0 info))
  18377. (params (nth 2 info))
  18378. (session (cdr (assq :session params))))
  18379. (if (not session) (org-edit-src-code)
  18380. ;; At a src-block with a session and function called with
  18381. ;; an ARG: switch to the buffer related to the inferior
  18382. ;; process.
  18383. (switch-to-buffer
  18384. (funcall (intern (concat "org-babel-prep-session:" lang))
  18385. session params))))))
  18386. (keyword
  18387. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18388. (find-file
  18389. (org-remove-double-quotes
  18390. (car (org-split-string (org-element-property :value element)))))
  18391. (user-error "No special environment to edit here")))
  18392. (table
  18393. (if (eq (org-element-property :type element) 'table.el)
  18394. (org-edit-src-code)
  18395. (call-interactively 'org-table-edit-formulas)))
  18396. ;; Only Org tables contain `table-row' type elements.
  18397. (table-row (call-interactively 'org-table-edit-formulas))
  18398. ((example-block export-block) (org-edit-src-code))
  18399. (fixed-width (org-edit-fixed-width-region))
  18400. (otherwise
  18401. ;; No notable element at point. Though, we may be at a link,
  18402. ;; which is an object. Thus, scan deeper.
  18403. (if (eq (org-element-type (org-element-context element)) 'link)
  18404. (call-interactively 'ffap)
  18405. (user-error "No special environment to edit here"))))))
  18406. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18407. (defun org-ctrl-c-ctrl-c (&optional arg)
  18408. "Set tags in headline, or update according to changed information at point.
  18409. This command does many different things, depending on context:
  18410. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18411. this is what we do.
  18412. - If the cursor is on a statistics cookie, update it.
  18413. - If the cursor is in a headline, prompt for tags and insert them
  18414. into the current line, aligned to `org-tags-column'. When called
  18415. with prefix arg, realign all tags in the current buffer.
  18416. - If the cursor is in one of the special #+KEYWORD lines, this
  18417. triggers scanning the buffer for these lines and updating the
  18418. information.
  18419. - If the cursor is inside a table, realign the table. This command
  18420. works even if the automatic table editor has been turned off.
  18421. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18422. the entire table.
  18423. - If the cursor is at a footnote reference or definition, jump to
  18424. the corresponding definition or references, respectively.
  18425. - If the cursor is a the beginning of a dynamic block, update it.
  18426. - If the current buffer is a capture buffer, close note and file it.
  18427. - If the cursor is on a <<<target>>>, update radio targets and
  18428. corresponding links in this buffer.
  18429. - If the cursor is on a numbered item in a plain list, renumber the
  18430. ordered list.
  18431. - If the cursor is on a checkbox, toggle it.
  18432. - If the cursor is on a code block, evaluate it. The variable
  18433. `org-confirm-babel-evaluate' can be used to control prompting
  18434. before code block evaluation, by default every code block
  18435. evaluation requires confirmation. Code block evaluation can be
  18436. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18437. (interactive "P")
  18438. (cond
  18439. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18440. org-occur-highlights
  18441. org-latex-fragment-image-overlays)
  18442. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18443. (org-remove-occur-highlights)
  18444. (org-remove-latex-fragment-image-overlays)
  18445. (message "Temporary highlights/overlays removed from current buffer"))
  18446. ((and (local-variable-p 'org-finish-function (current-buffer))
  18447. (fboundp org-finish-function))
  18448. (funcall org-finish-function))
  18449. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18450. (t
  18451. (let* ((context (org-element-context)) (type (org-element-type context)))
  18452. ;; Test if point is within a blank line.
  18453. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18454. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18455. (user-error "C-c C-c can do nothing useful at this location"))
  18456. ;; For convenience: at the first line of a paragraph on the
  18457. ;; same line as an item, apply function on that item instead.
  18458. (when (eq type 'paragraph)
  18459. (let ((parent (org-element-property :parent context)))
  18460. (when (and (eq (org-element-type parent) 'item)
  18461. (= (point-at-bol) (org-element-property :begin parent)))
  18462. (setq context parent type 'item))))
  18463. ;; Act according to type of element or object at point.
  18464. (case type
  18465. (clock (org-clock-update-time-maybe))
  18466. (dynamic-block
  18467. (save-excursion
  18468. (goto-char (org-element-property :post-affiliated context))
  18469. (org-update-dblock)))
  18470. (footnote-definition
  18471. (goto-char (org-element-property :post-affiliated context))
  18472. (call-interactively 'org-footnote-action))
  18473. (footnote-reference (call-interactively 'org-footnote-action))
  18474. ((headline inlinetask)
  18475. (save-excursion (goto-char (org-element-property :begin context))
  18476. (call-interactively 'org-set-tags)))
  18477. (item
  18478. ;; At an item: a double C-u set checkbox to "[-]"
  18479. ;; unconditionally, whereas a single one will toggle its
  18480. ;; presence. Without an universal argument, if the item
  18481. ;; has a checkbox, toggle it. Otherwise repair the list.
  18482. (let* ((box (org-element-property :checkbox context))
  18483. (struct (org-element-property :structure context))
  18484. (old-struct (copy-tree struct))
  18485. (parents (org-list-parents-alist struct))
  18486. (prevs (org-list-prevs-alist struct))
  18487. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18488. (org-list-set-checkbox
  18489. (org-element-property :begin context) struct
  18490. (cond ((equal arg '(16)) "[-]")
  18491. ((and (not box) (equal arg '(4))) "[ ]")
  18492. ((or (not box) (equal arg '(4))) nil)
  18493. ((eq box 'on) "[ ]")
  18494. (t "[X]")))
  18495. ;; Mimic `org-list-write-struct' but with grabbing
  18496. ;; a return value from `org-list-struct-fix-box'.
  18497. (org-list-struct-fix-ind struct parents 2)
  18498. (org-list-struct-fix-item-end struct)
  18499. (org-list-struct-fix-bul struct prevs)
  18500. (org-list-struct-fix-ind struct parents)
  18501. (let ((block-item
  18502. (org-list-struct-fix-box struct parents prevs orderedp)))
  18503. (if (and box (equal struct old-struct))
  18504. (if (equal arg '(16))
  18505. (message "Checkboxes already reset")
  18506. (user-error "Cannot toggle this checkbox: %s"
  18507. (if (eq box 'on)
  18508. "all subitems checked"
  18509. "unchecked subitems")))
  18510. (org-list-struct-apply-struct struct old-struct)
  18511. (org-update-checkbox-count-maybe))
  18512. (when block-item
  18513. (message "Checkboxes were removed due to empty box at line %d"
  18514. (org-current-line block-item))))))
  18515. (keyword
  18516. (let ((org-inhibit-startup-visibility-stuff t)
  18517. (org-startup-align-all-tables nil))
  18518. (when (boundp 'org-table-coordinate-overlays)
  18519. (mapc 'delete-overlay org-table-coordinate-overlays)
  18520. (setq org-table-coordinate-overlays nil))
  18521. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18522. (message "Local setup has been refreshed"))
  18523. (plain-list
  18524. ;; At a plain list, with a double C-u argument, set
  18525. ;; checkboxes of each item to "[-]", whereas a single one
  18526. ;; will toggle their presence according to the state of the
  18527. ;; first item in the list. Without an argument, repair the
  18528. ;; list.
  18529. (let* ((begin (org-element-property :contents-begin context))
  18530. (beginm (move-marker (make-marker) begin))
  18531. (struct (org-element-property :structure context))
  18532. (old-struct (copy-tree struct))
  18533. (first-box (save-excursion
  18534. (goto-char begin)
  18535. (looking-at org-list-full-item-re)
  18536. (match-string-no-properties 3)))
  18537. (new-box (cond ((equal arg '(16)) "[-]")
  18538. ((equal arg '(4)) (unless first-box "[ ]"))
  18539. ((equal first-box "[X]") "[ ]")
  18540. (t "[X]"))))
  18541. (cond
  18542. (arg
  18543. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18544. (org-list-get-all-items
  18545. begin struct (org-list-prevs-alist struct))))
  18546. ((and first-box (eq (point) begin))
  18547. ;; For convenience, when point is at bol on the first
  18548. ;; item of the list and no argument is provided, simply
  18549. ;; toggle checkbox of that item, if any.
  18550. (org-list-set-checkbox begin struct new-box)))
  18551. (org-list-write-struct
  18552. struct (org-list-parents-alist struct) old-struct)
  18553. (org-update-checkbox-count-maybe)
  18554. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18555. ((property-drawer node-property)
  18556. (call-interactively 'org-property-action))
  18557. ((radio-target target)
  18558. (call-interactively 'org-update-radio-target-regexp))
  18559. (statistics-cookie
  18560. (call-interactively 'org-update-statistics-cookies))
  18561. ((table table-cell table-row)
  18562. ;; At a table, recalculate every field and align it. Also
  18563. ;; send the table if necessary. If the table has
  18564. ;; a `table.el' type, just give up. At a table row or
  18565. ;; cell, maybe recalculate line but always align table.
  18566. (if (eq (org-element-property :type context) 'table.el)
  18567. (message "Use C-c ' to edit table.el tables")
  18568. (let ((org-enable-table-editor t))
  18569. (if (or (eq type 'table)
  18570. ;; Check if point is at a TBLFM line.
  18571. (and (eq type 'table-row)
  18572. (= (point) (org-element-property :end context))))
  18573. (save-excursion
  18574. (if (org-at-TBLFM-p) (org-calc-current-TBLFM)
  18575. (goto-char (org-element-property :contents-begin context))
  18576. (org-call-with-arg 'org-table-recalculate (or arg t))
  18577. (orgtbl-send-table 'maybe)))
  18578. (org-table-maybe-eval-formula)
  18579. (cond (arg (call-interactively 'org-table-recalculate))
  18580. ((org-table-maybe-recalculate-line))
  18581. (t (org-table-align)))))))
  18582. (timestamp (org-timestamp-change 0 'day))
  18583. (otherwise
  18584. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18585. (user-error
  18586. "C-c C-c can do nothing useful at this location")))))))))
  18587. (defun org-mode-restart ()
  18588. "Restart Org-mode, to scan again for special lines.
  18589. Also updates the keyword regular expressions."
  18590. (interactive)
  18591. (org-mode)
  18592. (message "Org-mode restarted"))
  18593. (defun org-kill-note-or-show-branches ()
  18594. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18595. (interactive)
  18596. (if (not org-finish-function)
  18597. (progn
  18598. (hide-subtree)
  18599. (call-interactively 'show-branches))
  18600. (let ((org-note-abort t))
  18601. (funcall org-finish-function))))
  18602. (defun org-open-line (n)
  18603. "Insert a new row in tables, call `open-line' elsewhere."
  18604. (interactive "*p")
  18605. (if (org-at-table-p)
  18606. (org-table-insert-row)
  18607. (open-line n)))
  18608. (defun org-return (&optional indent)
  18609. "Goto next table row or insert a newline.
  18610. Calls `org-table-next-row' or `newline', depending on context.
  18611. See the individual commands for more information."
  18612. (interactive)
  18613. (let (org-ts-what)
  18614. (cond
  18615. ((or (bobp) (org-in-src-block-p))
  18616. (if indent (newline-and-indent) (newline)))
  18617. ((org-at-table-p)
  18618. (org-table-justify-field-maybe)
  18619. (call-interactively 'org-table-next-row))
  18620. ;; when `newline-and-indent' is called within a list, make sure
  18621. ;; text moved stays inside the item.
  18622. ((and (org-in-item-p) indent)
  18623. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18624. (progn
  18625. (save-match-data (newline))
  18626. (org-indent-line-to (length (match-string 0))))
  18627. (let ((ind (org-get-indentation)))
  18628. (newline)
  18629. (if (org-looking-back org-list-end-re)
  18630. (org-indent-line)
  18631. (org-indent-line-to ind)))))
  18632. ((and org-return-follows-link
  18633. (org-at-timestamp-p t)
  18634. (not (eq org-ts-what 'after)))
  18635. (org-follow-timestamp-link))
  18636. ((and org-return-follows-link
  18637. (let ((tprop (get-text-property (point) 'face)))
  18638. (or (eq tprop 'org-link)
  18639. (and (listp tprop) (memq 'org-link tprop)))))
  18640. (call-interactively 'org-open-at-point))
  18641. ((and (org-at-heading-p)
  18642. (looking-at
  18643. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18644. (org-show-entry)
  18645. (end-of-line 1)
  18646. (newline))
  18647. (t (if indent (newline-and-indent) (newline))))))
  18648. (defun org-return-indent ()
  18649. "Goto next table row or insert a newline and indent.
  18650. Calls `org-table-next-row' or `newline-and-indent', depending on
  18651. context. See the individual commands for more information."
  18652. (interactive)
  18653. (org-return t))
  18654. (defun org-ctrl-c-star ()
  18655. "Compute table, or change heading status of lines.
  18656. Calls `org-table-recalculate' or `org-toggle-heading',
  18657. depending on context."
  18658. (interactive)
  18659. (cond
  18660. ((org-at-table-p)
  18661. (call-interactively 'org-table-recalculate))
  18662. (t
  18663. ;; Convert all lines in region to list items
  18664. (call-interactively 'org-toggle-heading))))
  18665. (defun org-ctrl-c-minus ()
  18666. "Insert separator line in table or modify bullet status of line.
  18667. Also turns a plain line or a region of lines into list items.
  18668. Calls `org-table-insert-hline', `org-toggle-item', or
  18669. `org-cycle-list-bullet', depending on context."
  18670. (interactive)
  18671. (cond
  18672. ((org-at-table-p)
  18673. (call-interactively 'org-table-insert-hline))
  18674. ((org-region-active-p)
  18675. (call-interactively 'org-toggle-item))
  18676. ((org-in-item-p)
  18677. (call-interactively 'org-cycle-list-bullet))
  18678. (t
  18679. (call-interactively 'org-toggle-item))))
  18680. (defun org-toggle-item (arg)
  18681. "Convert headings or normal lines to items, items to normal lines.
  18682. If there is no active region, only the current line is considered.
  18683. If the first non blank line in the region is a headline, convert
  18684. all headlines to items, shifting text accordingly.
  18685. If it is an item, convert all items to normal lines.
  18686. If it is normal text, change region into a list of items.
  18687. With a prefix argument ARG, change the region in a single item."
  18688. (interactive "P")
  18689. (let ((shift-text
  18690. (function
  18691. ;; Shift text in current section to IND, from point to END.
  18692. ;; The function leaves point to END line.
  18693. (lambda (ind end)
  18694. (let ((min-i 1000) (end (copy-marker end)))
  18695. ;; First determine the minimum indentation (MIN-I) of
  18696. ;; the text.
  18697. (save-excursion
  18698. (catch 'exit
  18699. (while (< (point) end)
  18700. (let ((i (org-get-indentation)))
  18701. (cond
  18702. ;; Skip blank lines and inline tasks.
  18703. ((looking-at "^[ \t]*$"))
  18704. ((looking-at org-outline-regexp-bol))
  18705. ;; We can't find less than 0 indentation.
  18706. ((zerop i) (throw 'exit (setq min-i 0)))
  18707. ((< i min-i) (setq min-i i))))
  18708. (forward-line))))
  18709. ;; Then indent each line so that a line indented to
  18710. ;; MIN-I becomes indented to IND. Ignore blank lines
  18711. ;; and inline tasks in the process.
  18712. (let ((delta (- ind min-i)))
  18713. (while (< (point) end)
  18714. (unless (or (looking-at "^[ \t]*$")
  18715. (looking-at org-outline-regexp-bol))
  18716. (org-indent-line-to (+ (org-get-indentation) delta)))
  18717. (forward-line)))))))
  18718. (skip-blanks
  18719. (function
  18720. ;; Return beginning of first non-blank line, starting from
  18721. ;; line at POS.
  18722. (lambda (pos)
  18723. (save-excursion
  18724. (goto-char pos)
  18725. (skip-chars-forward " \r\t\n")
  18726. (point-at-bol)))))
  18727. beg end)
  18728. ;; Determine boundaries of changes.
  18729. (if (org-region-active-p)
  18730. (setq beg (funcall skip-blanks (region-beginning))
  18731. end (copy-marker (region-end)))
  18732. (setq beg (funcall skip-blanks (point-at-bol))
  18733. end (copy-marker (point-at-eol))))
  18734. ;; Depending on the starting line, choose an action on the text
  18735. ;; between BEG and END.
  18736. (org-with-limited-levels
  18737. (save-excursion
  18738. (goto-char beg)
  18739. (cond
  18740. ;; Case 1. Start at an item: de-itemize. Note that it only
  18741. ;; happens when a region is active: `org-ctrl-c-minus'
  18742. ;; would call `org-cycle-list-bullet' otherwise.
  18743. ((org-at-item-p)
  18744. (while (< (point) end)
  18745. (when (org-at-item-p)
  18746. (skip-chars-forward " \t")
  18747. (delete-region (point) (match-end 0)))
  18748. (forward-line)))
  18749. ;; Case 2. Start at an heading: convert to items.
  18750. ((org-at-heading-p)
  18751. (let* ((bul (org-list-bullet-string "-"))
  18752. (bul-len (length bul))
  18753. ;; Indentation of the first heading. It should be
  18754. ;; relative to the indentation of its parent, if any.
  18755. (start-ind (save-excursion
  18756. (cond
  18757. ((not org-adapt-indentation) 0)
  18758. ((not (outline-previous-heading)) 0)
  18759. (t (length (match-string 0))))))
  18760. ;; Level of first heading. Further headings will be
  18761. ;; compared to it to determine hierarchy in the list.
  18762. (ref-level (org-reduced-level (org-outline-level))))
  18763. (while (< (point) end)
  18764. (let* ((level (org-reduced-level (org-outline-level)))
  18765. (delta (max 0 (- level ref-level))))
  18766. ;; If current headline is less indented than the first
  18767. ;; one, set it as reference, in order to preserve
  18768. ;; subtrees.
  18769. (when (< level ref-level) (setq ref-level level))
  18770. (replace-match bul t t)
  18771. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18772. ;; Ensure all text down to END (or SECTION-END) belongs
  18773. ;; to the newly created item.
  18774. (let ((section-end (save-excursion
  18775. (or (outline-next-heading) (point)))))
  18776. (forward-line)
  18777. (funcall shift-text
  18778. (+ start-ind (* (1+ delta) bul-len))
  18779. (min end section-end)))))))
  18780. ;; Case 3. Normal line with ARG: make the first line of region
  18781. ;; an item, and shift indentation of others lines to
  18782. ;; set them as item's body.
  18783. (arg (let* ((bul (org-list-bullet-string "-"))
  18784. (bul-len (length bul))
  18785. (ref-ind (org-get-indentation)))
  18786. (skip-chars-forward " \t")
  18787. (insert bul)
  18788. (forward-line)
  18789. (while (< (point) end)
  18790. ;; Ensure that lines less indented than first one
  18791. ;; still get included in item body.
  18792. (funcall shift-text
  18793. (+ ref-ind bul-len)
  18794. (min end (save-excursion (or (outline-next-heading)
  18795. (point)))))
  18796. (forward-line))))
  18797. ;; Case 4. Normal line without ARG: turn each non-item line
  18798. ;; into an item.
  18799. (t
  18800. (while (< (point) end)
  18801. (unless (or (org-at-heading-p) (org-at-item-p))
  18802. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18803. (replace-match
  18804. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18805. (forward-line))))))))
  18806. (defun org-toggle-heading (&optional nstars)
  18807. "Convert headings to normal text, or items or text to headings.
  18808. If there is no active region, only convert the current line.
  18809. With a \\[universal-argument] prefix, convert the whole list at
  18810. point into heading.
  18811. In a region:
  18812. - If the first non blank line is a headline, remove the stars
  18813. from all headlines in the region.
  18814. - If it is a normal line, turn each and every normal line (i.e.,
  18815. not an heading or an item) in the region into headings. If you
  18816. want to convert only the first line of this region, use one
  18817. universal prefix argument.
  18818. - If it is a plain list item, turn all plain list items into headings.
  18819. When converting a line into a heading, the number of stars is chosen
  18820. such that the lines become children of the current entry. However,
  18821. when a numeric prefix argument is given, its value determines the
  18822. number of stars to add."
  18823. (interactive "P")
  18824. (let ((skip-blanks
  18825. (function
  18826. ;; Return beginning of first non-blank line, starting from
  18827. ;; line at POS.
  18828. (lambda (pos)
  18829. (save-excursion
  18830. (goto-char pos)
  18831. (while (org-at-comment-p) (forward-line))
  18832. (skip-chars-forward " \r\t\n")
  18833. (point-at-bol)))))
  18834. beg end toggled)
  18835. ;; Determine boundaries of changes. If a universal prefix has
  18836. ;; been given, put the list in a region. If region ends at a bol,
  18837. ;; do not consider the last line to be in the region.
  18838. (when (and current-prefix-arg (org-at-item-p))
  18839. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18840. (org-mark-element))
  18841. (if (org-region-active-p)
  18842. (setq beg (funcall skip-blanks (region-beginning))
  18843. end (copy-marker (save-excursion
  18844. (goto-char (region-end))
  18845. (if (bolp) (point) (point-at-eol)))))
  18846. (setq beg (funcall skip-blanks (point-at-bol))
  18847. end (copy-marker (point-at-eol))))
  18848. ;; Ensure inline tasks don't count as headings.
  18849. (org-with-limited-levels
  18850. (save-excursion
  18851. (goto-char beg)
  18852. (cond
  18853. ;; Case 1. Started at an heading: de-star headings.
  18854. ((org-at-heading-p)
  18855. (while (< (point) end)
  18856. (when (org-at-heading-p t)
  18857. (looking-at org-outline-regexp) (replace-match "")
  18858. (setq toggled t))
  18859. (forward-line)))
  18860. ;; Case 2. Started at an item: change items into headlines.
  18861. ;; One star will be added by `org-list-to-subtree'.
  18862. ((org-at-item-p)
  18863. (let* ((stars (make-string
  18864. ;; subtract the star that will be added again by
  18865. ;; `org-list-to-subtree'
  18866. (if (numberp nstars) (1- nstars)
  18867. (or (org-current-level) 0))
  18868. ?*))
  18869. (add-stars
  18870. (cond (nstars "") ; stars from prefix only
  18871. ((equal stars "") "") ; before first heading
  18872. (org-odd-levels-only "*") ; inside heading, odd
  18873. (t "")))) ; inside heading, oddeven
  18874. (while (< (point) end)
  18875. (when (org-at-item-p)
  18876. ;; Pay attention to cases when region ends before list.
  18877. (let* ((struct (org-list-struct))
  18878. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18879. (save-restriction
  18880. (narrow-to-region (point) list-end)
  18881. (insert
  18882. (org-list-to-subtree
  18883. (org-list-parse-list t)
  18884. '(:istart (concat stars add-stars (funcall get-stars depth))
  18885. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18886. (setq toggled t))
  18887. (forward-line))))
  18888. ;; Case 3. Started at normal text: make every line an heading,
  18889. ;; skipping headlines and items.
  18890. (t (let* ((stars
  18891. (make-string
  18892. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18893. (add-stars
  18894. (cond (nstars "") ; stars from prefix only
  18895. ((equal stars "") "*") ; before first heading
  18896. (org-odd-levels-only "**") ; inside heading, odd
  18897. (t "*"))) ; inside heading, oddeven
  18898. (rpl (concat stars add-stars " "))
  18899. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18900. (while (< (point) (if (equal nstars '(4)) lend end))
  18901. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18902. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18903. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18904. (forward-line)))))))
  18905. (unless toggled (message "Cannot toggle heading from here"))))
  18906. (defun org-meta-return (&optional arg)
  18907. "Insert a new heading or wrap a region in a table.
  18908. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18909. See the individual commands for more information."
  18910. (interactive "P")
  18911. (cond
  18912. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18913. ((or (org-at-drawer-p) (org-at-property-p))
  18914. (newline-and-indent))
  18915. ((org-at-table-p)
  18916. (call-interactively 'org-table-wrap-region))
  18917. (t (call-interactively 'org-insert-heading))))
  18918. ;;; Menu entries
  18919. (defsubst org-in-subtree-not-table-p ()
  18920. "Are we in a subtree and not in a table?"
  18921. (and (not (org-before-first-heading-p))
  18922. (not (org-at-table-p))))
  18923. ;; Define the Org-mode menus
  18924. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18925. '("Tbl"
  18926. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18927. ["Next Field" org-cycle (org-at-table-p)]
  18928. ["Previous Field" org-shifttab (org-at-table-p)]
  18929. ["Next Row" org-return (org-at-table-p)]
  18930. "--"
  18931. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18932. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18933. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18934. "--"
  18935. ("Column"
  18936. ["Move Column Left" org-metaleft (org-at-table-p)]
  18937. ["Move Column Right" org-metaright (org-at-table-p)]
  18938. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18939. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18940. ("Row"
  18941. ["Move Row Up" org-metaup (org-at-table-p)]
  18942. ["Move Row Down" org-metadown (org-at-table-p)]
  18943. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18944. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18945. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18946. "--"
  18947. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18948. ("Rectangle"
  18949. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18950. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18951. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18952. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18953. "--"
  18954. ("Calculate"
  18955. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18956. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18957. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18958. "--"
  18959. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18960. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18961. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18962. "--"
  18963. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18964. "--"
  18965. ["Sum Column/Rectangle" org-table-sum
  18966. (or (org-at-table-p) (org-region-active-p))]
  18967. ["Which Column?" org-table-current-column (org-at-table-p)])
  18968. ["Debug Formulas"
  18969. org-table-toggle-formula-debugger
  18970. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18971. ["Show Col/Row Numbers"
  18972. org-table-toggle-coordinate-overlays
  18973. :style toggle
  18974. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18975. "--"
  18976. ["Create" org-table-create (and (not (org-at-table-p))
  18977. org-enable-table-editor)]
  18978. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18979. ["Import from File" org-table-import (not (org-at-table-p))]
  18980. ["Export to File" org-table-export (org-at-table-p)]
  18981. "--"
  18982. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18983. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18984. '("Org"
  18985. ("Show/Hide"
  18986. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18987. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18988. ["Sparse Tree..." org-sparse-tree t]
  18989. ["Reveal Context" org-reveal t]
  18990. ["Show All" show-all t]
  18991. "--"
  18992. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18993. "--"
  18994. ["New Heading" org-insert-heading t]
  18995. ("Navigate Headings"
  18996. ["Up" outline-up-heading t]
  18997. ["Next" outline-next-visible-heading t]
  18998. ["Previous" outline-previous-visible-heading t]
  18999. ["Next Same Level" outline-forward-same-level t]
  19000. ["Previous Same Level" outline-backward-same-level t]
  19001. "--"
  19002. ["Jump" org-goto t])
  19003. ("Edit Structure"
  19004. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19005. "--"
  19006. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19007. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19008. "--"
  19009. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19010. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19011. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19012. "--"
  19013. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19014. "--"
  19015. ["Copy visible text" org-copy-visible t]
  19016. "--"
  19017. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19018. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19019. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19020. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19021. "--"
  19022. ["Sort Region/Children" org-sort t]
  19023. "--"
  19024. ["Convert to odd levels" org-convert-to-odd-levels t]
  19025. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19026. ("Editing"
  19027. ["Emphasis..." org-emphasize t]
  19028. ["Edit Source Example" org-edit-special t]
  19029. "--"
  19030. ["Footnote new/jump" org-footnote-action t]
  19031. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19032. ("Archive"
  19033. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19034. "--"
  19035. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19036. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19037. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19038. )
  19039. "--"
  19040. ("Hyperlinks"
  19041. ["Store Link (Global)" org-store-link t]
  19042. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19043. ["Insert Link" org-insert-link t]
  19044. ["Follow Link" org-open-at-point t]
  19045. "--"
  19046. ["Next link" org-next-link t]
  19047. ["Previous link" org-previous-link t]
  19048. "--"
  19049. ["Descriptive Links"
  19050. org-toggle-link-display
  19051. :style radio
  19052. :selected org-descriptive-links
  19053. ]
  19054. ["Literal Links"
  19055. org-toggle-link-display
  19056. :style radio
  19057. :selected (not org-descriptive-links)])
  19058. "--"
  19059. ("TODO Lists"
  19060. ["TODO/DONE/-" org-todo t]
  19061. ("Select keyword"
  19062. ["Next keyword" org-shiftright (org-at-heading-p)]
  19063. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19064. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19065. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19066. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19067. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19068. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19069. "--"
  19070. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19071. :selected org-enforce-todo-dependencies :style toggle :active t]
  19072. "Settings for tree at point"
  19073. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19074. :selected (org-entry-get nil "ORDERED")
  19075. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19076. ["Do Children parallel" org-toggle-ordered-property :style radio
  19077. :selected (not (org-entry-get nil "ORDERED"))
  19078. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19079. "--"
  19080. ["Set Priority" org-priority t]
  19081. ["Priority Up" org-shiftup t]
  19082. ["Priority Down" org-shiftdown t]
  19083. "--"
  19084. ["Get news from all feeds" org-feed-update-all t]
  19085. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19086. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19087. ("TAGS and Properties"
  19088. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19089. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19090. "--"
  19091. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19092. ["Column view of properties" org-columns t]
  19093. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19094. ("Dates and Scheduling"
  19095. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19096. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19097. ("Change Date"
  19098. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19099. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19100. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19101. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19102. ["Compute Time Range" org-evaluate-time-range t]
  19103. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19104. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19105. "--"
  19106. ["Custom time format" org-toggle-time-stamp-overlays
  19107. :style radio :selected org-display-custom-times]
  19108. "--"
  19109. ["Goto Calendar" org-goto-calendar t]
  19110. ["Date from Calendar" org-date-from-calendar t]
  19111. "--"
  19112. ["Start/Restart Timer" org-timer-start t]
  19113. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19114. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19115. ["Insert Timer String" org-timer t]
  19116. ["Insert Timer Item" org-timer-item t])
  19117. ("Logging work"
  19118. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19119. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19120. ["Clock out" org-clock-out t]
  19121. ["Clock cancel" org-clock-cancel t]
  19122. "--"
  19123. ["Mark as default task" org-clock-mark-default-task t]
  19124. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19125. ["Goto running clock" org-clock-goto t]
  19126. "--"
  19127. ["Display times" org-clock-display t]
  19128. ["Create clock table" org-clock-report t]
  19129. "--"
  19130. ["Record DONE time"
  19131. (progn (setq org-log-done (not org-log-done))
  19132. (message "Switching to %s will %s record a timestamp"
  19133. (car org-done-keywords)
  19134. (if org-log-done "automatically" "not")))
  19135. :style toggle :selected org-log-done])
  19136. "--"
  19137. ["Agenda Command..." org-agenda t]
  19138. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19139. ("File List for Agenda")
  19140. ("Special views current file"
  19141. ["TODO Tree" org-show-todo-tree t]
  19142. ["Check Deadlines" org-check-deadlines t]
  19143. ["Timeline" org-timeline t]
  19144. ["Tags/Property tree" org-match-sparse-tree t])
  19145. "--"
  19146. ["Export/Publish..." org-export-dispatch t]
  19147. ("LaTeX"
  19148. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19149. :selected org-cdlatex-mode]
  19150. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19151. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19152. ["Modify math symbol" org-cdlatex-math-modify
  19153. (org-inside-LaTeX-fragment-p)]
  19154. ["Insert citation" org-reftex-citation t]
  19155. "--"
  19156. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19157. "--"
  19158. ("MobileOrg"
  19159. ["Push Files and Views" org-mobile-push t]
  19160. ["Get Captured and Flagged" org-mobile-pull t]
  19161. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19162. "--"
  19163. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19164. "--"
  19165. ("Documentation"
  19166. ["Show Version" org-version t]
  19167. ["Info Documentation" org-info t])
  19168. ("Customize"
  19169. ["Browse Org Group" org-customize t]
  19170. "--"
  19171. ["Expand This Menu" org-create-customize-menu
  19172. (fboundp 'customize-menu-create)])
  19173. ["Send bug report" org-submit-bug-report t]
  19174. "--"
  19175. ("Refresh/Reload"
  19176. ["Refresh setup current buffer" org-mode-restart t]
  19177. ["Reload Org (after update)" org-reload t]
  19178. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19179. ))
  19180. (defun org-info (&optional node)
  19181. "Read documentation for Org-mode in the info system.
  19182. With optional NODE, go directly to that node."
  19183. (interactive)
  19184. (info (format "(org)%s" (or node ""))))
  19185. ;;;###autoload
  19186. (defun org-submit-bug-report ()
  19187. "Submit a bug report on Org-mode via mail.
  19188. Don't hesitate to report any problems or inaccurate documentation.
  19189. If you don't have setup sending mail from (X)Emacs, please copy the
  19190. output buffer into your mail program, as it gives us important
  19191. information about your Org-mode version and configuration."
  19192. (interactive)
  19193. (require 'reporter)
  19194. (org-load-modules-maybe)
  19195. (org-require-autoloaded-modules)
  19196. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19197. (reporter-submit-bug-report
  19198. "emacs-orgmode@gnu.org"
  19199. (org-version nil 'full)
  19200. (let (list)
  19201. (save-window-excursion
  19202. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19203. (delete-other-windows)
  19204. (erase-buffer)
  19205. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19206. We would like to add your full Org-mode and Outline configuration to the
  19207. bug report. This greatly simplifies the work of the maintainer and
  19208. other experts on the mailing list.
  19209. HOWEVER, some variables you have customized may contain private
  19210. information. The names of customers, colleagues, or friends, might
  19211. appear in the form of file names, tags, todo states, or search strings.
  19212. If you answer yes to the prompt, you might want to check and remove
  19213. such private information before sending the email.")
  19214. (add-text-properties (point-min) (point-max) '(face org-warning))
  19215. (when (yes-or-no-p "Include your Org-mode configuration ")
  19216. (mapatoms
  19217. (lambda (v)
  19218. (and (boundp v)
  19219. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19220. (or (and (symbol-value v)
  19221. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19222. (and
  19223. (get v 'custom-type) (get v 'standard-value)
  19224. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19225. (push v list)))))
  19226. (kill-buffer (get-buffer "*Warn about privacy*"))
  19227. list))
  19228. nil nil
  19229. "Remember to cover the basics, that is, what you expected to happen and
  19230. what in fact did happen. You don't know how to make a good report? See
  19231. http://orgmode.org/manual/Feedback.html#Feedback
  19232. Your bug report will be posted to the Org-mode mailing list.
  19233. ------------------------------------------------------------------------")
  19234. (save-excursion
  19235. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19236. (replace-match "\\1Bug: \\3 [\\2]")))))
  19237. (defun org-install-agenda-files-menu ()
  19238. (let ((bl (buffer-list)))
  19239. (save-excursion
  19240. (while bl
  19241. (set-buffer (pop bl))
  19242. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19243. (when (derived-mode-p 'org-mode)
  19244. (easy-menu-change
  19245. '("Org") "File List for Agenda"
  19246. (append
  19247. (list
  19248. ["Edit File List" (org-edit-agenda-file-list) t]
  19249. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19250. ["Remove Current File from List" org-remove-file t]
  19251. ["Cycle through agenda files" org-cycle-agenda-files t]
  19252. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19253. "--")
  19254. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19255. ;;;; Documentation
  19256. (defun org-require-autoloaded-modules ()
  19257. (interactive)
  19258. (mapc 'require
  19259. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19260. org-remember org-table org-timer)))
  19261. ;;;###autoload
  19262. (defun org-reload (&optional uncompiled)
  19263. "Reload all org lisp files.
  19264. With prefix arg UNCOMPILED, load the uncompiled versions."
  19265. (interactive "P")
  19266. (require 'loadhist)
  19267. (let* ((org-dir (org-find-library-dir "org"))
  19268. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19269. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19270. (remove-re (mapconcat 'identity
  19271. (mapcar (lambda (f) (concat "^" f "$"))
  19272. (list (if (featurep 'xemacs)
  19273. "org-colview"
  19274. "org-colview-xemacs")
  19275. "org" "org-loaddefs" "org-version"))
  19276. "\\|"))
  19277. (feats (delete-dups
  19278. (mapcar 'file-name-sans-extension
  19279. (mapcar 'file-name-nondirectory
  19280. (delq nil
  19281. (mapcar 'feature-file
  19282. features))))))
  19283. (lfeat (append
  19284. (sort
  19285. (setq feats
  19286. (delq nil (mapcar
  19287. (lambda (f)
  19288. (if (and (string-match feature-re f)
  19289. (not (string-match remove-re f)))
  19290. f nil))
  19291. feats)))
  19292. 'string-lessp)
  19293. (list "org-version" "org")))
  19294. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19295. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19296. load-uncore load-misses)
  19297. (setq load-misses
  19298. (delq 't
  19299. (mapcar (lambda (f)
  19300. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19301. (and (string= org-dir contrib-dir)
  19302. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19303. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19304. (add-to-list 'load-uncore f 'append)
  19305. 't)
  19306. f))
  19307. lfeat)))
  19308. (if load-uncore
  19309. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19310. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19311. (if load-misses
  19312. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19313. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19314. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19315. ;;;###autoload
  19316. (defun org-customize ()
  19317. "Call the customize function with org as argument."
  19318. (interactive)
  19319. (org-load-modules-maybe)
  19320. (org-require-autoloaded-modules)
  19321. (customize-browse 'org))
  19322. (defun org-create-customize-menu ()
  19323. "Create a full customization menu for Org-mode, insert it into the menu."
  19324. (interactive)
  19325. (org-load-modules-maybe)
  19326. (org-require-autoloaded-modules)
  19327. (if (fboundp 'customize-menu-create)
  19328. (progn
  19329. (easy-menu-change
  19330. '("Org") "Customize"
  19331. `(["Browse Org group" org-customize t]
  19332. "--"
  19333. ,(customize-menu-create 'org)
  19334. ["Set" Custom-set t]
  19335. ["Save" Custom-save t]
  19336. ["Reset to Current" Custom-reset-current t]
  19337. ["Reset to Saved" Custom-reset-saved t]
  19338. ["Reset to Standard Settings" Custom-reset-standard t]))
  19339. (message "\"Org\"-menu now contains full customization menu"))
  19340. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19341. ;;;; Miscellaneous stuff
  19342. ;;; Generally useful functions
  19343. (defun org-get-at-bol (property)
  19344. "Get text property PROPERTY at beginning of line."
  19345. (get-text-property (point-at-bol) property))
  19346. (defun org-find-text-property-in-string (prop s)
  19347. "Return the first non-nil value of property PROP in string S."
  19348. (or (get-text-property 0 prop s)
  19349. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19350. prop s)))
  19351. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19352. "Display the given MESSAGE as a warning."
  19353. (if (fboundp 'display-warning)
  19354. (display-warning 'org message
  19355. (if (featurep 'xemacs) 'warning :warning))
  19356. (let ((buf (get-buffer-create "*Org warnings*")))
  19357. (with-current-buffer buf
  19358. (goto-char (point-max))
  19359. (insert "Warning (Org): " message)
  19360. (unless (bolp)
  19361. (newline)))
  19362. (display-buffer buf)
  19363. (sit-for 0))))
  19364. (defun org-eval (form)
  19365. "Eval FORM and return result."
  19366. (condition-case error
  19367. (eval form)
  19368. (error (format "%%![Error: %s]" error))))
  19369. (defun org-in-clocktable-p ()
  19370. "Check if the cursor is in a clocktable."
  19371. (let ((pos (point)) start)
  19372. (save-excursion
  19373. (end-of-line 1)
  19374. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19375. (setq start (match-beginning 0))
  19376. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19377. (>= (match-end 0) pos)
  19378. start))))
  19379. (defun org-in-commented-line ()
  19380. "Is point in a line starting with `#'?"
  19381. (equal (char-after (point-at-bol)) ?#))
  19382. (defun org-in-indented-comment-line ()
  19383. "Is point in a line starting with `#' after some white space?"
  19384. (save-excursion
  19385. (save-match-data
  19386. (goto-char (point-at-bol))
  19387. (looking-at "[ \t]*#"))))
  19388. (defun org-in-verbatim-emphasis ()
  19389. (save-match-data
  19390. (and (org-in-regexp org-emph-re 2)
  19391. (>= (point) (match-beginning 3))
  19392. (<= (point) (match-end 4))
  19393. (member (match-string 3) '("=" "~")))))
  19394. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19395. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19396. (if (and marker (marker-buffer marker)
  19397. (buffer-live-p (marker-buffer marker)))
  19398. (progn
  19399. (org-pop-to-buffer-same-window (marker-buffer marker))
  19400. (if (or (> marker (point-max)) (< marker (point-min)))
  19401. (widen))
  19402. (goto-char marker)
  19403. (org-show-context 'org-goto))
  19404. (if bookmark
  19405. (bookmark-jump bookmark)
  19406. (error "Cannot find location"))))
  19407. (defun org-quote-csv-field (s)
  19408. "Quote field for inclusion in CSV material."
  19409. (if (string-match "[\",]" s)
  19410. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19411. s))
  19412. (defun org-force-self-insert (N)
  19413. "Needed to enforce self-insert under remapping."
  19414. (interactive "p")
  19415. (self-insert-command N))
  19416. (defun org-string-width (s)
  19417. "Compute width of string, ignoring invisible characters.
  19418. This ignores character with invisibility property `org-link', and also
  19419. characters with property `org-cwidth', because these will become invisible
  19420. upon the next fontification round."
  19421. (let (b l)
  19422. (when (or (eq t buffer-invisibility-spec)
  19423. (assq 'org-link buffer-invisibility-spec))
  19424. (while (setq b (text-property-any 0 (length s)
  19425. 'invisible 'org-link s))
  19426. (setq s (concat (substring s 0 b)
  19427. (substring s (or (next-single-property-change
  19428. b 'invisible s) (length s)))))))
  19429. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19430. (setq s (concat (substring s 0 b)
  19431. (substring s (or (next-single-property-change
  19432. b 'org-cwidth s) (length s))))))
  19433. (setq l (string-width s) b -1)
  19434. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19435. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19436. l))
  19437. (defun org-shorten-string (s maxlength)
  19438. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19439. If the string is shorter or has length MAXLENGTH, just return the
  19440. original string. If it is longer, the functions finds a space in the
  19441. string, breaks this string off at that locations and adds three dots
  19442. as ellipsis. Including the ellipsis, the string will not be longer
  19443. than MAXLENGTH. If finding a good breaking point in the string does
  19444. not work, the string is just chopped off in the middle of a word
  19445. if necessary."
  19446. (if (<= (length s) maxlength)
  19447. s
  19448. (let* ((n (max (- maxlength 4) 1))
  19449. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19450. (if (string-match re s)
  19451. (concat (match-string 1 s) "...")
  19452. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19453. (defun org-get-indentation (&optional line)
  19454. "Get the indentation of the current line, interpreting tabs.
  19455. When LINE is given, assume it represents a line and compute its indentation."
  19456. (if line
  19457. (if (string-match "^ *" (org-remove-tabs line))
  19458. (match-end 0))
  19459. (save-excursion
  19460. (beginning-of-line 1)
  19461. (skip-chars-forward " \t")
  19462. (current-column))))
  19463. (defun org-get-string-indentation (s)
  19464. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19465. (let ((n -1) (i 0) (w tab-width) c)
  19466. (catch 'exit
  19467. (while (< (setq n (1+ n)) (length s))
  19468. (setq c (aref s n))
  19469. (cond ((= c ?\ ) (setq i (1+ i)))
  19470. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19471. (t (throw 'exit t)))))
  19472. i))
  19473. (defun org-remove-tabs (s &optional width)
  19474. "Replace tabulators in S with spaces.
  19475. Assumes that s is a single line, starting in column 0."
  19476. (setq width (or width tab-width))
  19477. (while (string-match "\t" s)
  19478. (setq s (replace-match
  19479. (make-string
  19480. (- (* width (/ (+ (match-beginning 0) width) width))
  19481. (match-beginning 0)) ?\ )
  19482. t t s)))
  19483. s)
  19484. (defun org-fix-indentation (line ind)
  19485. "Fix indentation in LINE.
  19486. IND is a cons cell with target and minimum indentation.
  19487. If the current indentation in LINE is smaller than the minimum,
  19488. leave it alone. If it is larger than ind, set it to the target."
  19489. (let* ((l (org-remove-tabs line))
  19490. (i (org-get-indentation l))
  19491. (i1 (car ind)) (i2 (cdr ind)))
  19492. (if (>= i i2) (setq l (substring line i2)))
  19493. (if (> i1 0)
  19494. (concat (make-string i1 ?\ ) l)
  19495. l)))
  19496. (defun org-remove-indentation (code &optional n)
  19497. "Remove the maximum common indentation from the lines in CODE.
  19498. N may optionally be the number of spaces to remove."
  19499. (with-temp-buffer
  19500. (insert code)
  19501. (org-do-remove-indentation n)
  19502. (buffer-string)))
  19503. (defun org-do-remove-indentation (&optional n)
  19504. "Remove the maximum common indentation from the buffer."
  19505. (untabify (point-min) (point-max))
  19506. (let ((min 10000) re)
  19507. (if n
  19508. (setq min n)
  19509. (goto-char (point-min))
  19510. (while (re-search-forward "^ *[^ \n]" nil t)
  19511. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19512. (unless (or (= min 0) (= min 10000))
  19513. (setq re (format "^ \\{%d\\}" min))
  19514. (goto-char (point-min))
  19515. (while (re-search-forward re nil t)
  19516. (replace-match "")
  19517. (end-of-line 1))
  19518. min)))
  19519. (defun org-fill-template (template alist)
  19520. "Find each %key of ALIST in TEMPLATE and replace it."
  19521. (let ((case-fold-search nil)
  19522. entry key value)
  19523. (setq alist (sort (copy-sequence alist)
  19524. (lambda (a b) (< (length (car a)) (length (car b))))))
  19525. (while (setq entry (pop alist))
  19526. (setq template
  19527. (replace-regexp-in-string
  19528. (concat "%" (regexp-quote (car entry)))
  19529. (or (cdr entry) "") template t t)))
  19530. template))
  19531. (defun org-base-buffer (buffer)
  19532. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19533. (if (not buffer)
  19534. buffer
  19535. (or (buffer-base-buffer buffer)
  19536. buffer)))
  19537. (defun org-trim (s)
  19538. "Remove whitespace at beginning and end of string."
  19539. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19540. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19541. s)
  19542. (defun org-wrap (string &optional width lines)
  19543. "Wrap string to either a number of lines, or a width in characters.
  19544. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19545. that costs. If there is a word longer than WIDTH, the text is actually
  19546. wrapped to the length of that word.
  19547. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19548. many lines, whatever width that takes.
  19549. The return value is a list of lines, without newlines at the end."
  19550. (let* ((words (org-split-string string "[ \t\n]+"))
  19551. (maxword (apply 'max (mapcar 'org-string-width words)))
  19552. w ll)
  19553. (cond (width
  19554. (org-do-wrap words (max maxword width)))
  19555. (lines
  19556. (setq w maxword)
  19557. (setq ll (org-do-wrap words maxword))
  19558. (if (<= (length ll) lines)
  19559. ll
  19560. (setq ll words)
  19561. (while (> (length ll) lines)
  19562. (setq w (1+ w))
  19563. (setq ll (org-do-wrap words w)))
  19564. ll))
  19565. (t (error "Cannot wrap this")))))
  19566. (defun org-do-wrap (words width)
  19567. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19568. (let (lines line)
  19569. (while words
  19570. (setq line (pop words))
  19571. (while (and words (< (+ (length line) (length (car words))) width))
  19572. (setq line (concat line " " (pop words))))
  19573. (setq lines (push line lines)))
  19574. (nreverse lines)))
  19575. (defun org-split-string (string &optional separators)
  19576. "Splits STRING into substrings at SEPARATORS.
  19577. No empty strings are returned if there are matches at the beginning
  19578. and end of string."
  19579. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19580. (start 0)
  19581. notfirst
  19582. (list nil))
  19583. (while (and (string-match rexp string
  19584. (if (and notfirst
  19585. (= start (match-beginning 0))
  19586. (< start (length string)))
  19587. (1+ start) start))
  19588. (< (match-beginning 0) (length string)))
  19589. (setq notfirst t)
  19590. (or (eq (match-beginning 0) 0)
  19591. (and (eq (match-beginning 0) (match-end 0))
  19592. (eq (match-beginning 0) start))
  19593. (setq list
  19594. (cons (substring string start (match-beginning 0))
  19595. list)))
  19596. (setq start (match-end 0)))
  19597. (or (eq start (length string))
  19598. (setq list
  19599. (cons (substring string start)
  19600. list)))
  19601. (nreverse list)))
  19602. (defun org-quote-vert (s)
  19603. "Replace \"|\" with \"\\vert\"."
  19604. (while (string-match "|" s)
  19605. (setq s (replace-match "\\vert" t t s)))
  19606. s)
  19607. (defun org-uuidgen-p (s)
  19608. "Is S an ID created by UUIDGEN?"
  19609. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19610. (defun org-in-src-block-p (&optional inside)
  19611. "Whether point is in a code source block.
  19612. When INSIDE is non-nil, don't consider we are within a src block
  19613. when point is at #+BEGIN_SRC or #+END_SRC."
  19614. (let ((case-fold-search t) ov)
  19615. (or (and (setq ov (overlays-at (point)))
  19616. (memq 'org-block-background
  19617. (overlay-properties (car ov))))
  19618. (and (not inside)
  19619. (save-match-data
  19620. (save-excursion
  19621. (beginning-of-line)
  19622. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19623. (defun org-context ()
  19624. "Return a list of contexts of the current cursor position.
  19625. If several contexts apply, all are returned.
  19626. Each context entry is a list with a symbol naming the context, and
  19627. two positions indicating start and end of the context. Possible
  19628. contexts are:
  19629. :headline anywhere in a headline
  19630. :headline-stars on the leading stars in a headline
  19631. :todo-keyword on a TODO keyword (including DONE) in a headline
  19632. :tags on the TAGS in a headline
  19633. :priority on the priority cookie in a headline
  19634. :item on the first line of a plain list item
  19635. :item-bullet on the bullet/number of a plain list item
  19636. :checkbox on the checkbox in a plain list item
  19637. :table in an org-mode table
  19638. :table-special on a special filed in a table
  19639. :table-table in a table.el table
  19640. :clocktable in a clocktable
  19641. :src-block in a source block
  19642. :link on a hyperlink
  19643. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19644. :target on a <<target>>
  19645. :radio-target on a <<<radio-target>>>
  19646. :latex-fragment on a LaTeX fragment
  19647. :latex-preview on a LaTeX fragment with overlaid preview image
  19648. This function expects the position to be visible because it uses font-lock
  19649. faces as a help to recognize the following contexts: :table-special, :link,
  19650. and :keyword."
  19651. (let* ((f (get-text-property (point) 'face))
  19652. (faces (if (listp f) f (list f)))
  19653. (case-fold-search t)
  19654. (p (point)) clist o)
  19655. ;; First the large context
  19656. (cond
  19657. ((org-at-heading-p t)
  19658. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19659. (when (progn
  19660. (beginning-of-line 1)
  19661. (looking-at org-todo-line-tags-regexp))
  19662. (push (org-point-in-group p 1 :headline-stars) clist)
  19663. (push (org-point-in-group p 2 :todo-keyword) clist)
  19664. (push (org-point-in-group p 4 :tags) clist))
  19665. (goto-char p)
  19666. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19667. (if (looking-at "\\[#[A-Z0-9]\\]")
  19668. (push (org-point-in-group p 0 :priority) clist)))
  19669. ((org-at-item-p)
  19670. (push (org-point-in-group p 2 :item-bullet) clist)
  19671. (push (list :item (point-at-bol)
  19672. (save-excursion (org-end-of-item) (point)))
  19673. clist)
  19674. (and (org-at-item-checkbox-p)
  19675. (push (org-point-in-group p 0 :checkbox) clist)))
  19676. ((org-at-table-p)
  19677. (push (list :table (org-table-begin) (org-table-end)) clist)
  19678. (if (memq 'org-formula faces)
  19679. (push (list :table-special
  19680. (previous-single-property-change p 'face)
  19681. (next-single-property-change p 'face)) clist)))
  19682. ((org-at-table-p 'any)
  19683. (push (list :table-table) clist)))
  19684. (goto-char p)
  19685. (let ((case-fold-search t))
  19686. ;; New the "medium" contexts: clocktables, source blocks
  19687. (cond ((org-in-clocktable-p)
  19688. (push (list :clocktable
  19689. (and (or (looking-at "#\\+BEGIN: clocktable")
  19690. (search-backward "#+BEGIN: clocktable" nil t))
  19691. (match-beginning 0))
  19692. (and (re-search-forward "#\\+END:?" nil t)
  19693. (match-end 0))) clist))
  19694. ((org-in-src-block-p)
  19695. (push (list :src-block
  19696. (and (or (looking-at "#\\+BEGIN_SRC")
  19697. (search-backward "#+BEGIN_SRC" nil t))
  19698. (match-beginning 0))
  19699. (and (search-forward "#+END_SRC" nil t)
  19700. (match-beginning 0))) clist))))
  19701. (goto-char p)
  19702. ;; Now the small context
  19703. (cond
  19704. ((org-at-timestamp-p)
  19705. (push (org-point-in-group p 0 :timestamp) clist))
  19706. ((memq 'org-link faces)
  19707. (push (list :link
  19708. (previous-single-property-change p 'face)
  19709. (next-single-property-change p 'face)) clist))
  19710. ((memq 'org-special-keyword faces)
  19711. (push (list :keyword
  19712. (previous-single-property-change p 'face)
  19713. (next-single-property-change p 'face)) clist))
  19714. ((org-at-target-p)
  19715. (push (org-point-in-group p 0 :target) clist)
  19716. (goto-char (1- (match-beginning 0)))
  19717. (if (looking-at org-radio-target-regexp)
  19718. (push (org-point-in-group p 0 :radio-target) clist))
  19719. (goto-char p))
  19720. ((setq o (car (delq nil
  19721. (mapcar
  19722. (lambda (x)
  19723. (if (memq x org-latex-fragment-image-overlays) x))
  19724. (overlays-at (point))))))
  19725. (push (list :latex-fragment
  19726. (overlay-start o) (overlay-end o)) clist)
  19727. (push (list :latex-preview
  19728. (overlay-start o) (overlay-end o)) clist))
  19729. ((org-inside-LaTeX-fragment-p)
  19730. ;; FIXME: positions wrong.
  19731. (push (list :latex-fragment (point) (point)) clist)))
  19732. (setq clist (nreverse (delq nil clist)))
  19733. clist))
  19734. ;; FIXME: Compare with at-regexp-p Do we need both?
  19735. (defun org-in-regexp (re &optional nlines visually)
  19736. "Check if point is inside a match of regexp.
  19737. Normally only the current line is checked, but you can include NLINES extra
  19738. lines both before and after point into the search.
  19739. If VISUALLY is set, require that the cursor is not after the match but
  19740. really on, so that the block visually is on the match."
  19741. (catch 'exit
  19742. (let ((pos (point))
  19743. (eol (point-at-eol (+ 1 (or nlines 0))))
  19744. (inc (if visually 1 0)))
  19745. (save-excursion
  19746. (beginning-of-line (- 1 (or nlines 0)))
  19747. (while (re-search-forward re eol t)
  19748. (if (and (<= (match-beginning 0) pos)
  19749. (>= (+ inc (match-end 0)) pos))
  19750. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19751. (defun org-at-regexp-p (regexp)
  19752. "Is point inside a match of REGEXP in the current line?"
  19753. (catch 'exit
  19754. (save-excursion
  19755. (let ((pos (point)) (end (point-at-eol)))
  19756. (beginning-of-line 1)
  19757. (while (re-search-forward regexp end t)
  19758. (if (and (<= (match-beginning 0) pos)
  19759. (>= (match-end 0) pos))
  19760. (throw 'exit t)))
  19761. nil))))
  19762. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19763. "Non-nil when point is between matches of START-RE and END-RE.
  19764. Also return a non-nil value when point is on one of the matches.
  19765. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19766. buffer positions. Default values are the positions of headlines
  19767. surrounding the point.
  19768. The functions returns a cons cell whose car (resp. cdr) is the
  19769. position before START-RE (resp. after END-RE)."
  19770. (save-match-data
  19771. (let ((pos (point))
  19772. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19773. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19774. beg end)
  19775. (save-excursion
  19776. ;; Point is on a block when on START-RE or if START-RE can be
  19777. ;; found before it...
  19778. (and (or (org-at-regexp-p start-re)
  19779. (re-search-backward start-re limit-up t))
  19780. (setq beg (match-beginning 0))
  19781. ;; ... and END-RE after it...
  19782. (goto-char (match-end 0))
  19783. (re-search-forward end-re limit-down t)
  19784. (> (setq end (match-end 0)) pos)
  19785. ;; ... without another START-RE in-between.
  19786. (goto-char (match-beginning 0))
  19787. (not (re-search-backward start-re (1+ beg) t))
  19788. ;; Return value.
  19789. (cons beg end))))))
  19790. (defun org-in-block-p (names)
  19791. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19792. NAMES is a list of strings containing names of blocks.
  19793. Return first block name matched, or nil. Beware that in case of
  19794. nested blocks, the returned name may not belong to the closest
  19795. block from point."
  19796. (save-match-data
  19797. (catch 'exit
  19798. (let ((case-fold-search t)
  19799. (lim-up (save-excursion (outline-previous-heading)))
  19800. (lim-down (save-excursion (outline-next-heading))))
  19801. (mapc (lambda (name)
  19802. (let ((n (regexp-quote name)))
  19803. (when (org-between-regexps-p
  19804. (concat "^[ \t]*#\\+begin_" n)
  19805. (concat "^[ \t]*#\\+end_" n)
  19806. lim-up lim-down)
  19807. (throw 'exit n))))
  19808. names))
  19809. nil)))
  19810. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19811. "Call `multi-occur' with buffers for all agenda files."
  19812. (interactive "sOrg-files matching: \np")
  19813. (let* ((files (org-agenda-files))
  19814. (tnames (mapcar 'file-truename files))
  19815. (extra org-agenda-text-search-extra-files)
  19816. f)
  19817. (when (eq (car extra) 'agenda-archives)
  19818. (setq extra (cdr extra))
  19819. (setq files (org-add-archive-files files)))
  19820. (while (setq f (pop extra))
  19821. (unless (member (file-truename f) tnames)
  19822. (add-to-list 'files f 'append)
  19823. (add-to-list 'tnames (file-truename f) 'append)))
  19824. (multi-occur
  19825. (mapcar (lambda (x)
  19826. (with-current-buffer
  19827. (or (get-file-buffer x) (find-file-noselect x))
  19828. (widen)
  19829. (current-buffer)))
  19830. files)
  19831. regexp)))
  19832. (if (boundp 'occur-mode-find-occurrence-hook)
  19833. ;; Emacs 23
  19834. (add-hook 'occur-mode-find-occurrence-hook
  19835. (lambda ()
  19836. (when (derived-mode-p 'org-mode)
  19837. (org-reveal))))
  19838. ;; Emacs 22
  19839. (defadvice occur-mode-goto-occurrence
  19840. (after org-occur-reveal activate)
  19841. (and (derived-mode-p 'org-mode) (org-reveal)))
  19842. (defadvice occur-mode-goto-occurrence-other-window
  19843. (after org-occur-reveal activate)
  19844. (and (derived-mode-p 'org-mode) (org-reveal)))
  19845. (defadvice occur-mode-display-occurrence
  19846. (after org-occur-reveal activate)
  19847. (when (derived-mode-p 'org-mode)
  19848. (let ((pos (occur-mode-find-occurrence)))
  19849. (with-current-buffer (marker-buffer pos)
  19850. (save-excursion
  19851. (goto-char pos)
  19852. (org-reveal)))))))
  19853. (defun org-occur-link-in-agenda-files ()
  19854. "Create a link and search for it in the agendas.
  19855. The link is not stored in `org-stored-links', it is just created
  19856. for the search purpose."
  19857. (interactive)
  19858. (let ((link (condition-case nil
  19859. (org-store-link nil)
  19860. (error "Unable to create a link to here"))))
  19861. (org-occur-in-agenda-files (regexp-quote link))))
  19862. (defun org-reverse-string (string)
  19863. "Return the reverse of STRING."
  19864. (apply 'string (reverse (string-to-list string))))
  19865. (defsubst org-uniquify (list)
  19866. "Non-destructively remove duplicate elements from LIST."
  19867. (let ((res (copy-sequence list))) (delete-dups res)))
  19868. (defun org-uniquify-alist (alist)
  19869. "Merge elements of ALIST with the same key.
  19870. For example, in this alist:
  19871. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19872. => '((a 1 3) (b 2))
  19873. merge (a 1) and (a 3) into (a 1 3).
  19874. The function returns the new ALIST."
  19875. (let (rtn)
  19876. (mapc
  19877. (lambda (e)
  19878. (let (n)
  19879. (if (not (assoc (car e) rtn))
  19880. (push e rtn)
  19881. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19882. (setq rtn (assq-delete-all (car e) rtn))
  19883. (push n rtn))))
  19884. alist)
  19885. rtn))
  19886. (defun org-delete-all (elts list)
  19887. "Remove all elements in ELTS from LIST."
  19888. (while elts
  19889. (setq list (delete (pop elts) list)))
  19890. list)
  19891. (defun org-count (cl-item cl-seq)
  19892. "Count the number of occurrences of ITEM in SEQ.
  19893. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19894. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19895. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19896. (while (< cl-start cl-end)
  19897. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19898. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19899. (setq cl-start (1+ cl-start)))
  19900. cl-count))
  19901. (defun org-remove-if (predicate seq)
  19902. "Remove everything from SEQ that fulfills PREDICATE."
  19903. (let (res e)
  19904. (while seq
  19905. (setq e (pop seq))
  19906. (if (not (funcall predicate e)) (push e res)))
  19907. (nreverse res)))
  19908. (defun org-remove-if-not (predicate seq)
  19909. "Remove everything from SEQ that does not fulfill PREDICATE."
  19910. (let (res e)
  19911. (while seq
  19912. (setq e (pop seq))
  19913. (if (funcall predicate e) (push e res)))
  19914. (nreverse res)))
  19915. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19916. "Reduce two-argument FUNCTION across SEQ.
  19917. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19918. \":initial-value\" removed."
  19919. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19920. (cadr (memq :initial-value cl-keys)))
  19921. (cl-seq (pop cl-seq))
  19922. (t (funcall cl-func)))))
  19923. (while cl-seq
  19924. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19925. cl-accum))
  19926. (defun org-back-over-empty-lines ()
  19927. "Move backwards over whitespace, to the beginning of the first empty line.
  19928. Returns the number of empty lines passed."
  19929. (let ((pos (point)))
  19930. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19931. (skip-chars-backward " \t\n\r")
  19932. (unless (eobp)
  19933. (forward-line -1)))
  19934. (beginning-of-line 2)
  19935. (goto-char (min (point) pos))
  19936. (count-lines (point) pos)))
  19937. (defun org-skip-whitespace ()
  19938. (skip-chars-forward " \t\n\r"))
  19939. (defun org-point-in-group (point group &optional context)
  19940. "Check if POINT is in match-group GROUP.
  19941. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19942. match. If the match group does not exist or point is not inside it,
  19943. return nil."
  19944. (and (match-beginning group)
  19945. (>= point (match-beginning group))
  19946. (<= point (match-end group))
  19947. (if context
  19948. (list context (match-beginning group) (match-end group))
  19949. t)))
  19950. (defun org-switch-to-buffer-other-window (&rest args)
  19951. "Switch to buffer in a second window on the current frame.
  19952. In particular, do not allow pop-up frames.
  19953. Returns the newly created buffer."
  19954. (org-no-popups
  19955. (apply 'switch-to-buffer-other-window args)))
  19956. (defun org-combine-plists (&rest plists)
  19957. "Create a single property list from all plists in PLISTS.
  19958. The process starts by copying the first list, and then setting properties
  19959. from the other lists. Settings in the last list are the most significant
  19960. ones and overrule settings in the other lists."
  19961. (let ((rtn (copy-sequence (pop plists)))
  19962. p v ls)
  19963. (while plists
  19964. (setq ls (pop plists))
  19965. (while ls
  19966. (setq p (pop ls) v (pop ls))
  19967. (setq rtn (plist-put rtn p v))))
  19968. rtn))
  19969. (defun org-replace-escapes (string table)
  19970. "Replace %-escapes in STRING with values in TABLE.
  19971. TABLE is an association list with keys like \"%a\" and string values.
  19972. The sequences in STRING may contain normal field width and padding information,
  19973. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19974. so values can contain further %-escapes if they are define later in TABLE."
  19975. (let ((tbl (copy-alist table))
  19976. (case-fold-search nil)
  19977. (pchg 0)
  19978. e re rpl)
  19979. (while (setq e (pop tbl))
  19980. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19981. (when (and (cdr e) (string-match re (cdr e)))
  19982. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19983. (safe "SREF"))
  19984. (add-text-properties 0 3 (list 'sref sref) safe)
  19985. (setcdr e (replace-match safe t t (cdr e)))))
  19986. (while (string-match re string)
  19987. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19988. (cdr e)))
  19989. (setq string (replace-match rpl t t string))))
  19990. (while (setq pchg (next-property-change pchg string))
  19991. (let ((sref (get-text-property pchg 'sref string)))
  19992. (when (and sref (string-match "SREF" string pchg))
  19993. (setq string (replace-match sref t t string)))))
  19994. string))
  19995. (defun org-sublist (list start end)
  19996. "Return a section of LIST, from START to END.
  19997. Counting starts at 1."
  19998. (let (rtn (c start))
  19999. (setq list (nthcdr (1- start) list))
  20000. (while (and list (<= c end))
  20001. (push (pop list) rtn)
  20002. (setq c (1+ c)))
  20003. (nreverse rtn)))
  20004. (defun org-find-base-buffer-visiting (file)
  20005. "Like `find-buffer-visiting' but always return the base buffer and
  20006. not an indirect buffer."
  20007. (let ((buf (or (get-file-buffer file)
  20008. (find-buffer-visiting file))))
  20009. (if buf
  20010. (or (buffer-base-buffer buf) buf)
  20011. nil)))
  20012. (defun org-image-file-name-regexp (&optional extensions)
  20013. "Return regexp matching the file names of images.
  20014. If EXTENSIONS is given, only match these."
  20015. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20016. (image-file-name-regexp)
  20017. (let ((image-file-name-extensions
  20018. (or extensions
  20019. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20020. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20021. (concat "\\."
  20022. (regexp-opt (nconc (mapcar 'upcase
  20023. image-file-name-extensions)
  20024. image-file-name-extensions)
  20025. t)
  20026. "\\'"))))
  20027. (defun org-file-image-p (file &optional extensions)
  20028. "Return non-nil if FILE is an image."
  20029. (save-match-data
  20030. (string-match (org-image-file-name-regexp extensions) file)))
  20031. (defun org-get-cursor-date (&optional with-time)
  20032. "Return the date at cursor in as a time.
  20033. This works in the calendar and in the agenda, anywhere else it just
  20034. returns the current time.
  20035. If WITH-TIME is non-nil, returns the time of the event at point (in
  20036. the agenda) or the current time of the day."
  20037. (let (date day defd tp tm hod mod)
  20038. (when with-time
  20039. (setq tp (get-text-property (point) 'time))
  20040. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20041. (setq hod (string-to-number (match-string 1 tp))
  20042. mod (string-to-number (match-string 2 tp))))
  20043. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20044. mod (nth 1 (decode-time (current-time))))))
  20045. (cond
  20046. ((eq major-mode 'calendar-mode)
  20047. (setq date (calendar-cursor-to-date)
  20048. defd (encode-time 0 (or mod 0) (or hod 0)
  20049. (nth 1 date) (nth 0 date) (nth 2 date))))
  20050. ((eq major-mode 'org-agenda-mode)
  20051. (setq day (get-text-property (point) 'day))
  20052. (if day
  20053. (setq date (calendar-gregorian-from-absolute day)
  20054. defd (encode-time 0 (or mod 0) (or hod 0)
  20055. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20056. (or defd (current-time))))
  20057. (defun org-mark-subtree (&optional up)
  20058. "Mark the current subtree.
  20059. This puts point at the start of the current subtree, and mark at
  20060. the end. If a numeric prefix UP is given, move up into the
  20061. hierarchy of headlines by UP levels before marking the subtree."
  20062. (interactive "P")
  20063. (org-with-limited-levels
  20064. (cond ((org-at-heading-p) (beginning-of-line))
  20065. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20066. (t (outline-previous-visible-heading 1))))
  20067. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20068. (if (org-called-interactively-p 'any)
  20069. (call-interactively 'org-mark-element)
  20070. (org-mark-element)))
  20071. ;;; Indentation
  20072. (defun org-indent-line ()
  20073. "Indent line depending on context."
  20074. (interactive)
  20075. (let* ((pos (point))
  20076. (itemp (org-at-item-p))
  20077. (case-fold-search t)
  20078. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20079. (inline-task-p (and (featurep 'org-inlinetask)
  20080. (org-inlinetask-in-task-p)))
  20081. (inline-re (and inline-task-p
  20082. (org-inlinetask-outline-regexp)))
  20083. column)
  20084. (if (and orgstruct-is-++ (eq pos (point)))
  20085. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20086. (indent-according-to-mode))
  20087. (beginning-of-line 1)
  20088. (cond
  20089. ;; Headings
  20090. ((looking-at org-outline-regexp) (setq column 0))
  20091. ;; Footnote definition
  20092. ((looking-at org-footnote-definition-re) (setq column 0))
  20093. ;; Literal examples
  20094. ((looking-at "[ \t]*:\\( \\|$\\)")
  20095. (setq column (org-get-indentation))) ; do nothing
  20096. ;; Lists
  20097. ((ignore-errors (goto-char (org-in-item-p)))
  20098. (setq column (if itemp
  20099. (org-get-indentation)
  20100. (org-list-item-body-column (point))))
  20101. (goto-char pos))
  20102. ;; Drawers
  20103. ((and (looking-at "[ \t]*:END:")
  20104. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20105. (save-excursion
  20106. (goto-char (1- (match-beginning 1)))
  20107. (setq column (current-column))))
  20108. ;; Special blocks
  20109. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20110. (save-excursion
  20111. (re-search-backward
  20112. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20113. (setq column (org-get-indentation (match-string 0))))
  20114. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20115. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20116. (save-excursion
  20117. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20118. (setq column
  20119. (cond ((equal (downcase (match-string 1)) "src")
  20120. ;; src blocks: let `org-edit-src-exit' handle them
  20121. (org-get-indentation))
  20122. ((equal (downcase (match-string 1)) "example")
  20123. (max (org-get-indentation)
  20124. (org-get-indentation (match-string 0))))
  20125. (t
  20126. (org-get-indentation (match-string 0))))))
  20127. ;; This line has nothing special, look at the previous relevant
  20128. ;; line to compute indentation
  20129. (t
  20130. (beginning-of-line 0)
  20131. (while (and (not (bobp))
  20132. (not (looking-at org-table-line-regexp))
  20133. (not (looking-at org-drawer-regexp))
  20134. ;; When point started in an inline task, do not move
  20135. ;; above task starting line.
  20136. (not (and inline-task-p (looking-at inline-re)))
  20137. ;; Skip drawers, blocks, empty lines, verbatim,
  20138. ;; comments, tables, footnotes definitions, lists,
  20139. ;; inline tasks.
  20140. (or (and (looking-at "[ \t]*:END:")
  20141. (re-search-backward org-drawer-regexp nil t))
  20142. (and (looking-at "[ \t]*#\\+end_")
  20143. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20144. (looking-at "[ \t]*[\n:#|]")
  20145. (looking-at org-footnote-definition-re)
  20146. (and (not inline-task-p)
  20147. (featurep 'org-inlinetask)
  20148. (org-inlinetask-in-task-p)
  20149. (or (org-inlinetask-goto-beginning) t))))
  20150. (beginning-of-line 0))
  20151. (cond
  20152. ;; There was a list item above.
  20153. ((save-excursion
  20154. (and (ignore-errors (goto-char (org-in-item-p)))
  20155. (goto-char
  20156. (org-list-get-top-point (org-list-struct)))))
  20157. (looking-at org-list-full-item-re)
  20158. (setq column (length (match-string 0))))
  20159. ;; There was an heading above.
  20160. ((looking-at "\\*+[ \t]+")
  20161. (if (not org-adapt-indentation)
  20162. (setq column 0)
  20163. (goto-char (match-end 0))
  20164. (setq column (current-column))))
  20165. ;; A drawer had started and is unfinished
  20166. ((looking-at org-drawer-regexp)
  20167. (goto-char (1- (match-beginning 1)))
  20168. (setq column (current-column)))
  20169. ;; Else, nothing noticeable found: get indentation and go on.
  20170. (t (setq column (org-get-indentation))))))
  20171. ;; Now apply indentation and move cursor accordingly
  20172. (goto-char pos)
  20173. (if (<= (current-column) (current-indentation))
  20174. (org-indent-line-to column)
  20175. (save-excursion (org-indent-line-to column)))
  20176. ;; Special polishing for properties, see `org-property-format'
  20177. (setq column (current-column))
  20178. (beginning-of-line 1)
  20179. (if (looking-at
  20180. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20181. (replace-match (concat (match-string 1)
  20182. (format org-property-format
  20183. (match-string 2) (match-string 3)))
  20184. t t))
  20185. (org-move-to-column column))))
  20186. (defun org-indent-drawer ()
  20187. "Indent the drawer at point."
  20188. (interactive)
  20189. (let ((p (point))
  20190. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20191. (match-end 0)))
  20192. (folded
  20193. (save-excursion
  20194. (end-of-line)
  20195. (when (overlays-at (point))
  20196. (member 'invisible (overlay-properties
  20197. (car (overlays-at (point)))))))))
  20198. (when folded (org-cycle))
  20199. (indent-for-tab-command)
  20200. (while (and (move-beginning-of-line 2) (< (point) e))
  20201. (indent-for-tab-command))
  20202. (goto-char p)
  20203. (when folded (org-cycle)))
  20204. (message "Drawer at point indented"))
  20205. (defun org-indent-block ()
  20206. "Indent the block at point."
  20207. (interactive)
  20208. (let ((p (point))
  20209. (case-fold-search t)
  20210. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20211. (match-end 0)))
  20212. (folded
  20213. (save-excursion
  20214. (end-of-line)
  20215. (when (overlays-at (point))
  20216. (member 'invisible (overlay-properties
  20217. (car (overlays-at (point)))))))))
  20218. (when folded (org-cycle))
  20219. (indent-for-tab-command)
  20220. (while (and (move-beginning-of-line 2) (< (point) e))
  20221. (indent-for-tab-command))
  20222. (goto-char p)
  20223. (when folded (org-cycle)))
  20224. (message "Block at point indented"))
  20225. (defun org-indent-region (start end)
  20226. "Indent region."
  20227. (interactive "r")
  20228. (save-excursion
  20229. (let ((line-end (org-current-line end)))
  20230. (goto-char start)
  20231. (while (< (org-current-line) line-end)
  20232. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20233. (t (call-interactively 'org-indent-line)))
  20234. (move-beginning-of-line 2)))))
  20235. ;;; Filling
  20236. ;; We use our own fill-paragraph and auto-fill functions.
  20237. ;; `org-fill-paragraph' relies on adaptive filling and context
  20238. ;; checking. Appropriate `fill-prefix' is computed with
  20239. ;; `org-adaptive-fill-function'.
  20240. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20241. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20242. ;; `org-adaptive-fill-function' value. Internally,
  20243. ;; `org-comment-line-break-function' breaks the line.
  20244. ;; `org-setup-filling' installs filling and auto-filling related
  20245. ;; variables during `org-mode' initialization.
  20246. (defun org-setup-filling ()
  20247. (interactive)
  20248. ;; Prevent auto-fill from inserting unwanted new items.
  20249. (when (boundp 'fill-nobreak-predicate)
  20250. (org-set-local
  20251. 'fill-nobreak-predicate
  20252. (org-uniquify
  20253. (append fill-nobreak-predicate
  20254. '(org-fill-paragraph-separate-nobreak-p
  20255. org-fill-line-break-nobreak-p
  20256. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20257. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20258. (org-set-local 'auto-fill-inhibit-regexp nil)
  20259. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20260. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20261. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20262. (defvar org-element-paragraph-separate) ; org-element.el
  20263. (defun org-fill-paragraph-separate-nobreak-p ()
  20264. "Non-nil when a line break at point would insert a new item."
  20265. (looking-at (substring org-element-paragraph-separate 1)))
  20266. (defun org-fill-line-break-nobreak-p ()
  20267. "Non-nil when a line break at point would create an Org line break."
  20268. (save-excursion
  20269. (skip-chars-backward "[ \t]")
  20270. (skip-chars-backward "\\\\")
  20271. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20272. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20273. "Non-nil when a line break at point would insert a new item."
  20274. (and (org-at-timestamp-p t)
  20275. (not (looking-at org-ts-regexp-both))))
  20276. (declare-function message-in-body-p "message" ())
  20277. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20278. (defun org-adaptive-fill-function ()
  20279. "Compute a fill prefix for the current line.
  20280. Return fill prefix, as a string, or nil if current line isn't
  20281. meant to be filled."
  20282. (let (prefix)
  20283. (catch 'exit
  20284. (when (derived-mode-p 'message-mode)
  20285. (save-excursion
  20286. (beginning-of-line)
  20287. (cond ((or (not (message-in-body-p))
  20288. (looking-at orgtbl-line-start-regexp))
  20289. (throw 'exit nil))
  20290. ((looking-at message-cite-prefix-regexp)
  20291. (throw 'exit (match-string-no-properties 0)))
  20292. ((looking-at org-outline-regexp)
  20293. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20294. (org-with-wide-buffer
  20295. (let* ((p (line-beginning-position))
  20296. (element (save-excursion
  20297. (beginning-of-line)
  20298. (or (ignore-errors (org-element-at-point))
  20299. (user-error "An element cannot be parsed line %d"
  20300. (line-number-at-pos (point))))))
  20301. (type (org-element-type element))
  20302. (post-affiliated (org-element-property :post-affiliated element)))
  20303. (unless (and post-affiliated (< p post-affiliated))
  20304. (case type
  20305. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20306. (footnote-definition "")
  20307. ((item plain-list)
  20308. (make-string (org-list-item-body-column
  20309. (or post-affiliated
  20310. (org-element-property :begin element)))
  20311. ? ))
  20312. (paragraph
  20313. ;; Fill prefix is usually the same as the current line,
  20314. ;; except if the paragraph is at the beginning of an item.
  20315. (let ((parent (org-element-property :parent element)))
  20316. (cond ((eq (org-element-type parent) 'item)
  20317. (make-string (org-list-item-body-column
  20318. (org-element-property :begin parent))
  20319. ? ))
  20320. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20321. (match-string 0))
  20322. (t ""))))
  20323. (comment-block
  20324. ;; Only fill contents if P is within block boundaries.
  20325. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20326. (forward-line)
  20327. (point)))
  20328. (cend (save-excursion
  20329. (goto-char (org-element-property :end element))
  20330. (skip-chars-backward " \r\t\n")
  20331. (line-beginning-position))))
  20332. (when (and (>= p cbeg) (< p cend))
  20333. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20334. (match-string 0)
  20335. "")))))))))))
  20336. (declare-function message-goto-body "message" ())
  20337. (defvar message-cite-prefix-regexp) ; From message.el
  20338. (defun org-fill-paragraph (&optional justify)
  20339. "Fill element at point, when applicable.
  20340. This function only applies to comment blocks, comments, example
  20341. blocks and paragraphs. Also, as a special case, re-align table
  20342. when point is at one.
  20343. If JUSTIFY is non-nil (interactively, with prefix argument),
  20344. justify as well. If `sentence-end-double-space' is non-nil, then
  20345. period followed by one space does not end a sentence, so don't
  20346. break a line there. The variable `fill-column' controls the
  20347. width for filling.
  20348. For convenience, when point is at a plain list, an item or
  20349. a footnote definition, try to fill the first paragraph within."
  20350. (interactive)
  20351. (if (and (derived-mode-p 'message-mode)
  20352. (or (not (message-in-body-p))
  20353. (save-excursion (move-beginning-of-line 1)
  20354. (looking-at message-cite-prefix-regexp))))
  20355. ;; First ensure filling is correct in message-mode.
  20356. (let ((fill-paragraph-function
  20357. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20358. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20359. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20360. (paragraph-separate
  20361. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20362. (fill-paragraph nil))
  20363. (with-syntax-table org-mode-transpose-word-syntax-table
  20364. ;; Move to end of line in order to get the first paragraph
  20365. ;; within a plain list or a footnote definition.
  20366. (let ((element (save-excursion
  20367. (end-of-line)
  20368. (or (ignore-errors (org-element-at-point))
  20369. (user-error "An element cannot be parsed line %d"
  20370. (line-number-at-pos (point)))))))
  20371. ;; First check if point is in a blank line at the beginning of
  20372. ;; the buffer. In that case, ignore filling.
  20373. (case (org-element-type element)
  20374. ;; Use major mode filling function is src blocks.
  20375. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20376. ;; Align Org tables, leave table.el tables as-is.
  20377. (table-row (org-table-align) t)
  20378. (table
  20379. (when (eq (org-element-property :type element) 'org)
  20380. (org-table-align))
  20381. t)
  20382. (paragraph
  20383. ;; Paragraphs may contain `line-break' type objects.
  20384. (let ((beg (max (point-min)
  20385. (org-element-property :contents-begin element)))
  20386. (end (min (point-max)
  20387. (org-element-property :contents-end element))))
  20388. ;; Do nothing if point is at an affiliated keyword.
  20389. (if (< (line-end-position) beg) t
  20390. (when (derived-mode-p 'message-mode)
  20391. ;; In `message-mode', do not fill following citation
  20392. ;; in current paragraph nor text before message body.
  20393. (let ((body-start (save-excursion (message-goto-body))))
  20394. (when body-start (setq beg (max body-start beg))))
  20395. (when (save-excursion
  20396. (re-search-forward
  20397. (concat "^" message-cite-prefix-regexp) end t))
  20398. (setq end (match-beginning 0))))
  20399. ;; Fill paragraph, taking line breaks into account.
  20400. ;; For that, slice the paragraph using line breaks as
  20401. ;; separators, and fill the parts in reverse order to
  20402. ;; avoid messing with markers.
  20403. (save-excursion
  20404. (goto-char end)
  20405. (mapc
  20406. (lambda (pos)
  20407. (fill-region-as-paragraph pos (point) justify)
  20408. (goto-char pos))
  20409. ;; Find the list of ending positions for line breaks
  20410. ;; in the current paragraph. Add paragraph
  20411. ;; beginning to include first slice.
  20412. (nreverse
  20413. (cons beg
  20414. (org-element-map
  20415. (org-element--parse-objects
  20416. beg end nil (org-element-restriction 'paragraph))
  20417. 'line-break
  20418. (lambda (lb) (org-element-property :end lb)))))))
  20419. t)))
  20420. ;; Contents of `comment-block' type elements should be
  20421. ;; filled as plain text, but only if point is within block
  20422. ;; markers.
  20423. (comment-block
  20424. (let* ((case-fold-search t)
  20425. (beg (save-excursion
  20426. (goto-char (org-element-property :begin element))
  20427. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20428. (forward-line)
  20429. (point)))
  20430. (end (save-excursion
  20431. (goto-char (org-element-property :end element))
  20432. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20433. (line-beginning-position))))
  20434. (when (and (>= (point) beg) (< (point) end))
  20435. (fill-region-as-paragraph
  20436. (save-excursion
  20437. (end-of-line)
  20438. (re-search-backward "^[ \t]*$" beg 'move)
  20439. (line-beginning-position))
  20440. (save-excursion
  20441. (beginning-of-line)
  20442. (re-search-forward "^[ \t]*$" end 'move)
  20443. (line-beginning-position))
  20444. justify)))
  20445. t)
  20446. ;; Fill comments.
  20447. (comment (fill-comment-paragraph justify))
  20448. ;; Ignore every other element.
  20449. (otherwise t))))))
  20450. (defun org-auto-fill-function ()
  20451. "Auto-fill function."
  20452. ;; Check if auto-filling is meaningful.
  20453. (let ((fc (current-fill-column)))
  20454. (when (and fc (> (current-column) fc))
  20455. (let* ((fill-prefix (org-adaptive-fill-function))
  20456. ;; Enforce empty fill prefix, if required. Otherwise, it
  20457. ;; will be computed again.
  20458. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20459. (when fill-prefix (do-auto-fill))))))
  20460. (defun org-comment-line-break-function (&optional soft)
  20461. "Break line at point and indent, continuing comment if within one.
  20462. The inserted newline is marked hard if variable
  20463. `use-hard-newlines' is true, unless optional argument SOFT is
  20464. non-nil."
  20465. (if soft (insert-and-inherit ?\n) (newline 1))
  20466. (save-excursion (forward-char -1) (delete-horizontal-space))
  20467. (delete-horizontal-space)
  20468. (indent-to-left-margin)
  20469. (insert-before-markers-and-inherit fill-prefix))
  20470. ;;; Comments
  20471. ;; Org comments syntax is quite complex. It requires the entire line
  20472. ;; to be just a comment. Also, even with the right syntax at the
  20473. ;; beginning of line, some some elements (i.e. verse-block or
  20474. ;; example-block) don't accept comments. Usual Emacs comment commands
  20475. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20476. ;; Org still relies on `comment-dwim', but cannot trust
  20477. ;; `comment-only-p'. So, `comment-region-function' and
  20478. ;; `uncomment-region-function' both point
  20479. ;; to`org-comment-or-uncomment-region'. Eventually,
  20480. ;; `org-insert-comment' takes care of insertion of comments at the
  20481. ;; beginning of line.
  20482. ;; `org-setup-comments-handling' install comments related variables
  20483. ;; during `org-mode' initialization.
  20484. (defun org-setup-comments-handling ()
  20485. (interactive)
  20486. (org-set-local 'comment-use-syntax nil)
  20487. (org-set-local 'comment-start "# ")
  20488. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20489. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20490. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20491. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20492. (defun org-insert-comment ()
  20493. "Insert an empty comment above current line.
  20494. If the line is empty, insert comment at its beginning."
  20495. (beginning-of-line)
  20496. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20497. (org-indent-line)
  20498. (insert "# "))
  20499. (defvar comment-empty-lines) ; From newcomment.el.
  20500. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20501. "Comment or uncomment each non-blank line in the region.
  20502. Uncomment each non-blank line between BEG and END if it only
  20503. contains commented lines. Otherwise, comment them."
  20504. (save-restriction
  20505. ;; Restrict region
  20506. (narrow-to-region (save-excursion (goto-char beg)
  20507. (skip-chars-forward " \r\t\n" end)
  20508. (line-beginning-position))
  20509. (save-excursion (goto-char end)
  20510. (skip-chars-backward " \r\t\n" beg)
  20511. (line-end-position)))
  20512. (let ((uncommentp
  20513. ;; UNCOMMENTP is non-nil when every non blank line between
  20514. ;; BEG and END is a comment.
  20515. (save-excursion
  20516. (goto-char (point-min))
  20517. (while (and (not (eobp))
  20518. (let ((element (org-element-at-point)))
  20519. (and (eq (org-element-type element) 'comment)
  20520. (goto-char (min (point-max)
  20521. (org-element-property
  20522. :end element)))))))
  20523. (eobp))))
  20524. (if uncommentp
  20525. ;; Only blank lines and comments in region: uncomment it.
  20526. (save-excursion
  20527. (goto-char (point-min))
  20528. (while (not (eobp))
  20529. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20530. (replace-match "" nil nil nil 1))
  20531. (forward-line)))
  20532. ;; Comment each line in region.
  20533. (let ((min-indent (point-max)))
  20534. ;; First find the minimum indentation across all lines.
  20535. (save-excursion
  20536. (goto-char (point-min))
  20537. (while (and (not (eobp)) (not (zerop min-indent)))
  20538. (unless (looking-at "[ \t]*$")
  20539. (setq min-indent (min min-indent (current-indentation))))
  20540. (forward-line)))
  20541. ;; Then loop over all lines.
  20542. (save-excursion
  20543. (goto-char (point-min))
  20544. (while (not (eobp))
  20545. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20546. ;; Don't get fooled by invisible text (e.g. link path)
  20547. ;; when moving to column MIN-INDENT.
  20548. (let ((buffer-invisibility-spec nil))
  20549. (org-move-to-column min-indent t))
  20550. (insert comment-start))
  20551. (forward-line))))))))
  20552. ;;; Planning
  20553. ;; This section contains tools to operate on timestamp objects, as
  20554. ;; returned by, e.g. `org-element-context'.
  20555. (defun org-timestamp-has-time-p (timestamp)
  20556. "Non-nil when TIMESTAMP has a time specified."
  20557. (org-element-property :hour-start timestamp))
  20558. (defun org-timestamp-format (timestamp format &optional end utc)
  20559. "Format a TIMESTAMP element into a string.
  20560. FORMAT is a format specifier to be passed to
  20561. `format-time-string'.
  20562. When optional argument END is non-nil, use end of date-range or
  20563. time-range, if possible.
  20564. When optional argument UTC is non-nil, time will be expressed as
  20565. Universal Time."
  20566. (format-time-string
  20567. format
  20568. (apply 'encode-time
  20569. (cons 0
  20570. (mapcar
  20571. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20572. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20573. '(:minute-start :hour-start :day-start :month-start
  20574. :year-start)))))
  20575. utc))
  20576. (defun org-timestamp-split-range (timestamp &optional end)
  20577. "Extract a timestamp object from a date or time range.
  20578. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20579. the end of the range. Otherwise, extract its start.
  20580. Return a new timestamp object sharing the same parent as
  20581. TIMESTAMP."
  20582. (let ((type (org-element-property :type timestamp)))
  20583. (if (memq type '(active inactive diary)) timestamp
  20584. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20585. ;; Set new type.
  20586. (org-element-put-property
  20587. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20588. ;; Copy start properties over end properties if END is
  20589. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20590. (let ((p-alist '((:minute-start . :minute-end)
  20591. (:hour-start . :hour-end)
  20592. (:day-start . :day-end)
  20593. (:month-start . :month-end)
  20594. (:year-start . :year-end))))
  20595. (dolist (p-cell p-alist)
  20596. (org-element-put-property
  20597. split-ts
  20598. (funcall (if end 'car 'cdr) p-cell)
  20599. (org-element-property
  20600. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20601. ;; Eventually refresh `:raw-value'.
  20602. (org-element-put-property split-ts :raw-value nil)
  20603. (org-element-put-property
  20604. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20605. (defun org-timestamp-translate (timestamp &optional boundary)
  20606. "Apply `org-translate-time' on a TIMESTAMP object.
  20607. When optional argument BOUNDARY is non-nil, it is either the
  20608. symbol `start' or `end'. In this case, only translate the
  20609. starting or ending part of TIMESTAMP if it is a date or time
  20610. range. Otherwise, translate both parts."
  20611. (if (and (not boundary)
  20612. (memq (org-element-property :type timestamp)
  20613. '(active-range inactive-range)))
  20614. (concat
  20615. (org-translate-time
  20616. (org-element-property :raw-value
  20617. (org-timestamp-split-range timestamp)))
  20618. "--"
  20619. (org-translate-time
  20620. (org-element-property :raw-value
  20621. (org-timestamp-split-range timestamp t))))
  20622. (org-translate-time
  20623. (org-element-property
  20624. :raw-value
  20625. (if (not boundary) timestamp
  20626. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20627. ;;; Other stuff.
  20628. (defun org-toggle-fixed-width-section (arg)
  20629. "Toggle the fixed-width export.
  20630. If there is no active region, the QUOTE keyword at the current headline is
  20631. inserted or removed. When present, it causes the text between this headline
  20632. and the next to be exported as fixed-width text, and unmodified.
  20633. If there is an active region, this command adds or removes a colon as the
  20634. first character of this line. If the first character of a line is a colon,
  20635. this line is also exported in fixed-width font."
  20636. (interactive "P")
  20637. (let* ((cc 0)
  20638. (regionp (org-region-active-p))
  20639. (beg (if regionp (region-beginning) (point)))
  20640. (end (if regionp (region-end)))
  20641. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20642. (case-fold-search nil)
  20643. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20644. off)
  20645. (if regionp
  20646. (save-excursion
  20647. (goto-char beg)
  20648. (setq cc (current-column))
  20649. (beginning-of-line 1)
  20650. (setq off (looking-at re))
  20651. (while (> nlines 0)
  20652. (setq nlines (1- nlines))
  20653. (beginning-of-line 1)
  20654. (cond
  20655. (arg
  20656. (org-move-to-column cc t)
  20657. (insert ": \n")
  20658. (forward-line -1))
  20659. ((and off (looking-at re))
  20660. (replace-match "" t t nil 1))
  20661. ((not off) (org-move-to-column cc t) (insert ": ")))
  20662. (forward-line 1)))
  20663. (save-excursion
  20664. (org-back-to-heading)
  20665. (cond
  20666. ((looking-at (format org-heading-keyword-regexp-format
  20667. org-quote-string))
  20668. (goto-char (match-end 1))
  20669. (looking-at (concat " +" org-quote-string))
  20670. (replace-match "" t t)
  20671. (when (eolp) (insert " ")))
  20672. ((looking-at org-outline-regexp)
  20673. (goto-char (match-end 0))
  20674. (insert org-quote-string " ")))))))
  20675. (defun org-reftex-citation ()
  20676. "Use reftex-citation to insert a citation into the buffer.
  20677. This looks for a line like
  20678. #+BIBLIOGRAPHY: foo plain option:-d
  20679. and derives from it that foo.bib is the bibliography file relevant
  20680. for this document. It then installs the necessary environment for RefTeX
  20681. to work in this buffer and calls `reftex-citation' to insert a citation
  20682. into the buffer.
  20683. Export of such citations to both LaTeX and HTML is handled by the contributed
  20684. package org-exp-bibtex by Taru Karttunen."
  20685. (interactive)
  20686. (let ((reftex-docstruct-symbol 'rds)
  20687. (reftex-cite-format "\\cite{%l}")
  20688. rds bib)
  20689. (save-excursion
  20690. (save-restriction
  20691. (widen)
  20692. (let ((case-fold-search t)
  20693. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20694. (if (not (save-excursion
  20695. (or (re-search-forward re nil t)
  20696. (re-search-backward re nil t))))
  20697. (error "No bibliography defined in file")
  20698. (setq bib (concat (match-string 1) ".bib")
  20699. rds (list (list 'bib bib)))))))
  20700. (call-interactively 'reftex-citation)))
  20701. ;;;; Functions extending outline functionality
  20702. (defun org-beginning-of-line (&optional arg)
  20703. "Go to the beginning of the current line. If that is invisible, continue
  20704. to a visible line beginning. This makes the function of C-a more intuitive.
  20705. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20706. first attempt, and only move to after the tags when the cursor is already
  20707. beyond the end of the headline."
  20708. (interactive "P")
  20709. (let ((pos (point))
  20710. (special (if (consp org-special-ctrl-a/e)
  20711. (car org-special-ctrl-a/e)
  20712. org-special-ctrl-a/e))
  20713. refpos)
  20714. (if (org-bound-and-true-p visual-line-mode)
  20715. (beginning-of-visual-line 1)
  20716. (beginning-of-line 1))
  20717. (if (and arg (fboundp 'move-beginning-of-line))
  20718. (call-interactively 'move-beginning-of-line)
  20719. (if (bobp)
  20720. nil
  20721. (backward-char 1)
  20722. (if (org-truely-invisible-p)
  20723. (while (and (not (bobp)) (org-truely-invisible-p))
  20724. (backward-char 1)
  20725. (beginning-of-line 1))
  20726. (forward-char 1))))
  20727. (when special
  20728. (cond
  20729. ((and (looking-at org-complex-heading-regexp)
  20730. (= (char-after (match-end 1)) ?\ ))
  20731. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20732. (point-at-eol)))
  20733. (goto-char
  20734. (if (eq special t)
  20735. (cond ((> pos refpos) refpos)
  20736. ((= pos (point)) refpos)
  20737. (t (point)))
  20738. (cond ((> pos (point)) (point))
  20739. ((not (eq last-command this-command)) (point))
  20740. (t refpos)))))
  20741. ((org-at-item-p)
  20742. ;; Being at an item and not looking at an the item means point
  20743. ;; was previously moved to beginning of a visual line, which
  20744. ;; doesn't contain the item. Therefore, do nothing special,
  20745. ;; just stay here.
  20746. (when (looking-at org-list-full-item-re)
  20747. ;; Set special position at first white space character after
  20748. ;; bullet, and check-box, if any.
  20749. (let ((after-bullet
  20750. (let ((box (match-end 3)))
  20751. (if (not box) (match-end 1)
  20752. (let ((after (char-after box)))
  20753. (if (and after (= after ? )) (1+ box) box))))))
  20754. ;; Special case: Move point to special position when
  20755. ;; currently after it or at beginning of line.
  20756. (if (eq special t)
  20757. (when (or (> pos after-bullet) (= (point) pos))
  20758. (goto-char after-bullet))
  20759. ;; Reversed case: Move point to special position when
  20760. ;; point was already at beginning of line and command is
  20761. ;; repeated.
  20762. (when (and (= (point) pos) (eq last-command this-command))
  20763. (goto-char after-bullet))))))))
  20764. (org-no-warnings
  20765. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20766. (defun org-end-of-line (&optional arg)
  20767. "Go to the end of the line.
  20768. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20769. tags on the first attempt, and only move to after the tags when
  20770. the cursor is already beyond the end of the headline."
  20771. (interactive "P")
  20772. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20773. org-special-ctrl-a/e))
  20774. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20775. 'end-of-visual-line)
  20776. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20777. (t 'end-of-line))))
  20778. (if (or (not special) arg) (call-interactively move-fun)
  20779. (let* ((element (save-excursion (beginning-of-line)
  20780. (org-element-at-point)))
  20781. (type (org-element-type element)))
  20782. (cond
  20783. ((memq type '(headline inlinetask))
  20784. (let ((pos (point)))
  20785. (beginning-of-line 1)
  20786. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20787. (if (eq special t)
  20788. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20789. (goto-char (match-beginning 1))
  20790. (goto-char (match-end 0)))
  20791. (if (or (< pos (match-end 0))
  20792. (not (eq this-command last-command)))
  20793. (goto-char (match-end 0))
  20794. (goto-char (match-beginning 1))))
  20795. (call-interactively move-fun))))
  20796. ((org-element-property :hiddenp element)
  20797. ;; If element is hidden, `move-end-of-line' would put point
  20798. ;; after it. Use `end-of-line' to stay on current line.
  20799. (call-interactively 'end-of-line))
  20800. (t (call-interactively move-fun)))))
  20801. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20802. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20803. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20804. (defun org-backward-sentence (&optional arg)
  20805. "Go to beginning of sentence, or beginning of table field.
  20806. This will call `backward-sentence' or `org-table-beginning-of-field',
  20807. depending on context."
  20808. (interactive "P")
  20809. (cond
  20810. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20811. (t (call-interactively 'backward-sentence))))
  20812. (defun org-forward-sentence (&optional arg)
  20813. "Go to end of sentence, or end of table field.
  20814. This will call `forward-sentence' or `org-table-end-of-field',
  20815. depending on context."
  20816. (interactive "P")
  20817. (cond
  20818. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20819. (t (call-interactively 'forward-sentence))))
  20820. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20821. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20822. (defun org-kill-line (&optional arg)
  20823. "Kill line, to tags or end of line."
  20824. (interactive "P")
  20825. (cond
  20826. ((or (not org-special-ctrl-k)
  20827. (bolp)
  20828. (not (org-at-heading-p)))
  20829. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20830. org-ctrl-k-protect-subtree)
  20831. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20832. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20833. (user-error "C-k aborted as it would kill a hidden subtree")))
  20834. (call-interactively
  20835. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20836. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20837. (kill-region (point) (match-beginning 1))
  20838. (org-set-tags nil t))
  20839. (t (kill-region (point) (point-at-eol)))))
  20840. (define-key org-mode-map "\C-k" 'org-kill-line)
  20841. (defun org-yank (&optional arg)
  20842. "Yank. If the kill is a subtree, treat it specially.
  20843. This command will look at the current kill and check if is a single
  20844. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20845. cursor is at the beginning of a line or after the stars of a currently
  20846. empty headline, then the yank is handled specially. How exactly depends
  20847. on the value of the following variables, both set by default.
  20848. org-yank-folded-subtrees
  20849. When set, the subtree(s) will be folded after insertion, but only
  20850. if doing so would now swallow text after the yanked text.
  20851. org-yank-adjusted-subtrees
  20852. When set, the subtree will be promoted or demoted in order to
  20853. fit into the local outline tree structure, which means that the level
  20854. will be adjusted so that it becomes the smaller one of the two
  20855. *visible* surrounding headings.
  20856. Any prefix to this command will cause `yank' to be called directly with
  20857. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20858. will just
  20859. plainly yank the text as it is.
  20860. \[1] The test checks if the first non-white line is a heading
  20861. and if there are no other headings with fewer stars."
  20862. (interactive "P")
  20863. (org-yank-generic 'yank arg))
  20864. (defun org-yank-generic (command arg)
  20865. "Perform some yank-like command.
  20866. This function implements the behavior described in the `org-yank'
  20867. documentation. However, it has been generalized to work for any
  20868. interactive command with similar behavior."
  20869. ;; pretend to be command COMMAND
  20870. (setq this-command command)
  20871. (if arg
  20872. (call-interactively command)
  20873. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20874. (and (org-kill-is-subtree-p)
  20875. (or (bolp)
  20876. (and (looking-at "[ \t]*$")
  20877. (string-match
  20878. "\\`\\*+\\'"
  20879. (buffer-substring (point-at-bol) (point)))))))
  20880. swallowp)
  20881. (cond
  20882. ((and subtreep org-yank-folded-subtrees)
  20883. (let ((beg (point))
  20884. end)
  20885. (if (and subtreep org-yank-adjusted-subtrees)
  20886. (org-paste-subtree nil nil 'for-yank)
  20887. (call-interactively command))
  20888. (setq end (point))
  20889. (goto-char beg)
  20890. (when (and (bolp) subtreep
  20891. (not (setq swallowp
  20892. (org-yank-folding-would-swallow-text beg end))))
  20893. (org-with-limited-levels
  20894. (or (looking-at org-outline-regexp)
  20895. (re-search-forward org-outline-regexp-bol end t))
  20896. (while (and (< (point) end) (looking-at org-outline-regexp))
  20897. (hide-subtree)
  20898. (org-cycle-show-empty-lines 'folded)
  20899. (condition-case nil
  20900. (outline-forward-same-level 1)
  20901. (error (goto-char end))))))
  20902. (when swallowp
  20903. (message
  20904. "Inserted text not folded because that would swallow text"))
  20905. (goto-char end)
  20906. (skip-chars-forward " \t\n\r")
  20907. (beginning-of-line 1)
  20908. (push-mark beg 'nomsg)))
  20909. ((and subtreep org-yank-adjusted-subtrees)
  20910. (let ((beg (point-at-bol)))
  20911. (org-paste-subtree nil nil 'for-yank)
  20912. (push-mark beg 'nomsg)))
  20913. (t
  20914. (call-interactively command))))))
  20915. (defun org-yank-folding-would-swallow-text (beg end)
  20916. "Would hide-subtree at BEG swallow any text after END?"
  20917. (let (level)
  20918. (org-with-limited-levels
  20919. (save-excursion
  20920. (goto-char beg)
  20921. (when (or (looking-at org-outline-regexp)
  20922. (re-search-forward org-outline-regexp-bol end t))
  20923. (setq level (org-outline-level)))
  20924. (goto-char end)
  20925. (skip-chars-forward " \t\r\n\v\f")
  20926. (if (or (eobp)
  20927. (and (bolp) (looking-at org-outline-regexp)
  20928. (<= (org-outline-level) level)))
  20929. nil ; Nothing would be swallowed
  20930. t))))) ; something would swallow
  20931. (define-key org-mode-map "\C-y" 'org-yank)
  20932. (defun org-truely-invisible-p ()
  20933. "Check if point is at a character currently not visible.
  20934. This version does not only check the character property, but also
  20935. `visible-mode'."
  20936. ;; Early versions of noutline don't have `outline-invisible-p'.
  20937. (if (org-bound-and-true-p visible-mode)
  20938. nil
  20939. (outline-invisible-p)))
  20940. (defun org-invisible-p2 ()
  20941. "Check if point is at a character currently not visible."
  20942. (save-excursion
  20943. (if (and (eolp) (not (bobp))) (backward-char 1))
  20944. ;; Early versions of noutline don't have `outline-invisible-p'.
  20945. (outline-invisible-p)))
  20946. (defun org-back-to-heading (&optional invisible-ok)
  20947. "Call `outline-back-to-heading', but provide a better error message."
  20948. (condition-case nil
  20949. (outline-back-to-heading invisible-ok)
  20950. (error (error "Before first headline at position %d in buffer %s"
  20951. (point) (current-buffer)))))
  20952. (defun org-before-first-heading-p ()
  20953. "Before first heading?"
  20954. (save-excursion
  20955. (end-of-line)
  20956. (null (re-search-backward org-outline-regexp-bol nil t))))
  20957. (defun org-at-heading-p (&optional ignored)
  20958. (outline-on-heading-p t))
  20959. ;; Compatibility alias with Org versions < 7.8.03
  20960. (defalias 'org-on-heading-p 'org-at-heading-p)
  20961. (defun org-at-comment-p nil
  20962. "Is cursor in a line starting with a # character?"
  20963. (save-excursion
  20964. (beginning-of-line)
  20965. (looking-at "^#")))
  20966. (defun org-at-drawer-p nil
  20967. "Is cursor at a drawer keyword?"
  20968. (save-excursion
  20969. (move-beginning-of-line 1)
  20970. (looking-at org-drawer-regexp)))
  20971. (defun org-at-block-p nil
  20972. "Is cursor at a block keyword?"
  20973. (save-excursion
  20974. (move-beginning-of-line 1)
  20975. (looking-at org-block-regexp)))
  20976. (defun org-point-at-end-of-empty-headline ()
  20977. "If point is at the end of an empty headline, return t, else nil.
  20978. If the heading only contains a TODO keyword, it is still still considered
  20979. empty."
  20980. (and (looking-at "[ \t]*$")
  20981. (when org-todo-line-regexp
  20982. (save-excursion
  20983. (beginning-of-line 1)
  20984. (let ((case-fold-search nil))
  20985. (looking-at org-todo-line-regexp)
  20986. (string= (match-string 3) ""))))))
  20987. (defun org-at-heading-or-item-p ()
  20988. (or (org-at-heading-p) (org-at-item-p)))
  20989. (defun org-at-target-p ()
  20990. (or (org-in-regexp org-radio-target-regexp)
  20991. (org-in-regexp org-target-regexp)))
  20992. ;; Compatibility alias with Org versions < 7.8.03
  20993. (defalias 'org-on-target-p 'org-at-target-p)
  20994. (defun org-up-heading-all (arg)
  20995. "Move to the heading line of which the present line is a subheading.
  20996. This function considers both visible and invisible heading lines.
  20997. With argument, move up ARG levels."
  20998. (if (fboundp 'outline-up-heading-all)
  20999. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  21000. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  21001. (defun org-up-heading-safe ()
  21002. "Move to the heading line of which the present line is a subheading.
  21003. This version will not throw an error. It will return the level of the
  21004. headline found, or nil if no higher level is found.
  21005. Also, this function will be a lot faster than `outline-up-heading',
  21006. because it relies on stars being the outline starters. This can really
  21007. make a significant difference in outlines with very many siblings."
  21008. (let (start-level re)
  21009. (org-back-to-heading t)
  21010. (setq start-level (funcall outline-level))
  21011. (if (equal start-level 1)
  21012. nil
  21013. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21014. (if (re-search-backward re nil t)
  21015. (funcall outline-level)))))
  21016. (defun org-first-sibling-p ()
  21017. "Is this heading the first child of its parents?"
  21018. (interactive)
  21019. (let ((re org-outline-regexp-bol)
  21020. level l)
  21021. (unless (org-at-heading-p t)
  21022. (user-error "Not at a heading"))
  21023. (setq level (funcall outline-level))
  21024. (save-excursion
  21025. (if (not (re-search-backward re nil t))
  21026. t
  21027. (setq l (funcall outline-level))
  21028. (< l level)))))
  21029. (defun org-goto-sibling (&optional previous)
  21030. "Goto the next sibling, even if it is invisible.
  21031. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21032. when a sibling was found. When none is found, return nil and don't
  21033. move point."
  21034. (let ((fun (if previous 're-search-backward 're-search-forward))
  21035. (pos (point))
  21036. (re org-outline-regexp-bol)
  21037. level l)
  21038. (when (condition-case nil (org-back-to-heading t) (error nil))
  21039. (setq level (funcall outline-level))
  21040. (catch 'exit
  21041. (or previous (forward-char 1))
  21042. (while (funcall fun re nil t)
  21043. (setq l (funcall outline-level))
  21044. (when (< l level) (goto-char pos) (throw 'exit nil))
  21045. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21046. (goto-char pos)
  21047. nil))))
  21048. (defun org-show-siblings ()
  21049. "Show all siblings of the current headline."
  21050. (save-excursion
  21051. (while (org-goto-sibling) (org-flag-heading nil)))
  21052. (save-excursion
  21053. (while (org-goto-sibling 'previous)
  21054. (org-flag-heading nil))))
  21055. (defun org-goto-first-child ()
  21056. "Goto the first child, even if it is invisible.
  21057. Return t when a child was found. Otherwise don't move point and
  21058. return nil."
  21059. (let (level (pos (point)) (re org-outline-regexp-bol))
  21060. (when (condition-case nil (org-back-to-heading t) (error nil))
  21061. (setq level (outline-level))
  21062. (forward-char 1)
  21063. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21064. (progn (goto-char (match-beginning 0)) t)
  21065. (goto-char pos) nil))))
  21066. (defun org-show-hidden-entry ()
  21067. "Show an entry where even the heading is hidden."
  21068. (save-excursion
  21069. (org-show-entry)))
  21070. (defun org-flag-heading (flag &optional entry)
  21071. "Flag the current heading. FLAG non-nil means make invisible.
  21072. When ENTRY is non-nil, show the entire entry."
  21073. (save-excursion
  21074. (org-back-to-heading t)
  21075. ;; Check if we should show the entire entry
  21076. (if entry
  21077. (progn
  21078. (org-show-entry)
  21079. (save-excursion
  21080. (and (outline-next-heading)
  21081. (org-flag-heading nil))))
  21082. (outline-flag-region (max (point-min) (1- (point)))
  21083. (save-excursion (outline-end-of-heading) (point))
  21084. flag))))
  21085. (defun org-get-next-sibling ()
  21086. "Move to next heading of the same level, and return point.
  21087. If there is no such heading, return nil.
  21088. This is like outline-next-sibling, but invisible headings are ok."
  21089. (let ((level (funcall outline-level)))
  21090. (outline-next-heading)
  21091. (while (and (not (eobp)) (> (funcall outline-level) level))
  21092. (outline-next-heading))
  21093. (if (or (eobp) (< (funcall outline-level) level))
  21094. nil
  21095. (point))))
  21096. (defun org-get-last-sibling ()
  21097. "Move to previous heading of the same level, and return point.
  21098. If there is no such heading, return nil."
  21099. (let ((opoint (point))
  21100. (level (funcall outline-level)))
  21101. (outline-previous-heading)
  21102. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21103. (while (and (> (funcall outline-level) level)
  21104. (not (bobp)))
  21105. (outline-previous-heading))
  21106. (if (< (funcall outline-level) level)
  21107. nil
  21108. (point)))))
  21109. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21110. "Goto to the end of a subtree."
  21111. ;; This contains an exact copy of the original function, but it uses
  21112. ;; `org-back-to-heading', to make it work also in invisible
  21113. ;; trees. And is uses an invisible-ok argument.
  21114. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21115. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21116. ;; with many children and grandchildren etc, this can be much faster
  21117. ;; than the outline version.
  21118. (org-back-to-heading invisible-ok)
  21119. (let ((first t)
  21120. (level (funcall outline-level)))
  21121. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21122. ;; A true heading (not a plain list item), in Org-mode
  21123. ;; This means we can easily find the end by looking
  21124. ;; only for the right number of stars. Using a regexp to do
  21125. ;; this is so much faster than using a Lisp loop.
  21126. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21127. (forward-char 1)
  21128. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21129. ;; something else, do it the slow way
  21130. (while (and (not (eobp))
  21131. (or first (> (funcall outline-level) level)))
  21132. (setq first nil)
  21133. (outline-next-heading)))
  21134. (unless to-heading
  21135. (if (memq (preceding-char) '(?\n ?\^M))
  21136. (progn
  21137. ;; Go to end of line before heading
  21138. (forward-char -1)
  21139. (if (memq (preceding-char) '(?\n ?\^M))
  21140. ;; leave blank line before heading
  21141. (forward-char -1))))))
  21142. (point))
  21143. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21144. "Use Org version in org-mode, for dramatic speed-up."
  21145. (if (derived-mode-p 'org-mode)
  21146. (progn
  21147. (org-end-of-subtree nil t)
  21148. (unless (eobp) (backward-char 1)))
  21149. ad-do-it))
  21150. (defun org-end-of-meta-data-and-drawers ()
  21151. "Jump to the first text after meta data and drawers in the current entry.
  21152. This will move over empty lines, lines with planning time stamps,
  21153. clocking lines, and drawers."
  21154. (org-back-to-heading t)
  21155. (let ((end (save-excursion (outline-next-heading) (point)))
  21156. (re (concat "\\(" org-drawer-regexp "\\)"
  21157. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21158. (forward-line 1)
  21159. (while (re-search-forward re end t)
  21160. (if (not (match-end 1))
  21161. ;; empty or planning line
  21162. (forward-line 1)
  21163. ;; a drawer, find the end
  21164. (re-search-forward "^[ \t]*:END:" end 'move)
  21165. (forward-line 1)))
  21166. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21167. (point)))
  21168. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21169. "Move forward to the ARG'th subheading at same level as this one.
  21170. Stop at the first and last subheadings of a superior heading.
  21171. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21172. non-nil it will also look at invisible ones."
  21173. (interactive "p")
  21174. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21175. (if (and arg (< arg 0))
  21176. (goto-char (point-min))
  21177. (outline-next-heading))
  21178. (org-at-heading-p)
  21179. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21180. (f (if (and arg (< arg 0))
  21181. 're-search-backward
  21182. 're-search-forward))
  21183. (count (if arg (abs arg) 1))
  21184. (result (point)))
  21185. (while (and (prog1 (> count 0)
  21186. (forward-char (if (and arg (< arg 0)) -1 1)))
  21187. (funcall f org-outline-regexp-bol nil 'move))
  21188. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21189. (cond ((< l level) (setq count 0))
  21190. ((and (= l level)
  21191. (or invisible-ok
  21192. (progn
  21193. (goto-char (line-beginning-position))
  21194. (not (outline-invisible-p)))))
  21195. (setq count (1- count))
  21196. (when (eq l level)
  21197. (setq result (point)))))))
  21198. (goto-char result))
  21199. (beginning-of-line 1)))
  21200. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21201. "Move backward to the ARG'th subheading at same level as this one.
  21202. Stop at the first and last subheadings of a superior heading."
  21203. (interactive "p")
  21204. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21205. (defun org-next-block (arg &optional backward block-regexp)
  21206. "Jump to the next block.
  21207. With a prefix argument ARG, jump forward ARG many source blocks.
  21208. When BACKWARD is non-nil, jump to the previous block.
  21209. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21210. (interactive "p")
  21211. (let ((re (or block-regexp org-block-regexp))
  21212. (re-search-fn (or (and backward 're-search-backward)
  21213. 're-search-forward)))
  21214. (if (looking-at re) (forward-char 1))
  21215. (condition-case nil
  21216. (funcall re-search-fn re nil nil arg)
  21217. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21218. (goto-char (match-beginning 0)) (org-show-context)))
  21219. (defun org-previous-block (arg &optional block-regexp)
  21220. "Jump to the previous block.
  21221. With a prefix argument ARG, jump backward ARG many source blocks.
  21222. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21223. (interactive "p")
  21224. (org-next-block arg t block-regexp))
  21225. (defun org-forward-element ()
  21226. "Move forward by one element.
  21227. Move to the next element at the same level, when possible."
  21228. (interactive)
  21229. (cond ((eobp) (user-error "Cannot move further down"))
  21230. ((org-with-limited-levels (org-at-heading-p))
  21231. (let ((origin (point)))
  21232. (goto-char (org-end-of-subtree nil t))
  21233. (unless (org-with-limited-levels (org-at-heading-p))
  21234. (goto-char origin)
  21235. (user-error "Cannot move further down"))))
  21236. (t
  21237. (let* ((elem (org-element-at-point))
  21238. (end (org-element-property :end elem))
  21239. (parent (org-element-property :parent elem)))
  21240. (if (and parent (= (org-element-property :contents-end parent) end))
  21241. (goto-char (org-element-property :end parent))
  21242. (goto-char end))))))
  21243. (defun org-backward-element ()
  21244. "Move backward by one element.
  21245. Move to the previous element at the same level, when possible."
  21246. (interactive)
  21247. (cond ((bobp) (user-error "Cannot move further up"))
  21248. ((org-with-limited-levels (org-at-heading-p))
  21249. ;; At a headline, move to the previous one, if any, or stay
  21250. ;; here.
  21251. (let ((origin (point)))
  21252. (org-with-limited-levels (org-backward-heading-same-level 1))
  21253. ;; When current headline has no sibling above, move to its
  21254. ;; parent.
  21255. (when (= (point) origin)
  21256. (or (org-with-limited-levels (org-up-heading-safe))
  21257. (progn (goto-char origin)
  21258. (user-error "Cannot move further up"))))))
  21259. (t
  21260. (let* ((trail (org-element-at-point 'keep-trail))
  21261. (elem (car trail))
  21262. (prev-elem (nth 1 trail))
  21263. (beg (org-element-property :begin elem)))
  21264. (cond
  21265. ;; Move to beginning of current element if point isn't
  21266. ;; there already.
  21267. ((/= (point) beg) (goto-char beg))
  21268. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21269. ((org-before-first-heading-p) (goto-char (point-min)))
  21270. (t (org-back-to-heading)))))))
  21271. (defun org-up-element ()
  21272. "Move to upper element."
  21273. (interactive)
  21274. (if (org-with-limited-levels (org-at-heading-p))
  21275. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  21276. (let* ((elem (org-element-at-point))
  21277. (parent (org-element-property :parent elem)))
  21278. (if parent (goto-char (org-element-property :begin parent))
  21279. (if (org-with-limited-levels (org-before-first-heading-p))
  21280. (user-error "No surrounding element")
  21281. (org-with-limited-levels (org-back-to-heading)))))))
  21282. (defvar org-element-greater-elements)
  21283. (defun org-down-element ()
  21284. "Move to inner element."
  21285. (interactive)
  21286. (let ((element (org-element-at-point)))
  21287. (cond
  21288. ((memq (org-element-type element) '(plain-list table))
  21289. (goto-char (org-element-property :contents-begin element))
  21290. (forward-char))
  21291. ((memq (org-element-type element) org-element-greater-elements)
  21292. ;; If contents are hidden, first disclose them.
  21293. (when (org-element-property :hiddenp element) (org-cycle))
  21294. (goto-char (or (org-element-property :contents-begin element)
  21295. (user-error "No content for this element"))))
  21296. (t (user-error "No inner element")))))
  21297. (defun org-drag-element-backward ()
  21298. "Move backward element at point."
  21299. (interactive)
  21300. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21301. (let* ((trail (org-element-at-point 'keep-trail))
  21302. (elem (car trail))
  21303. (prev-elem (nth 1 trail)))
  21304. ;; Error out if no previous element or previous element is
  21305. ;; a parent of the current one.
  21306. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21307. (user-error "Cannot drag element backward")
  21308. (let ((pos (point)))
  21309. (org-element-swap-A-B prev-elem elem)
  21310. (goto-char (+ (org-element-property :begin prev-elem)
  21311. (- pos (org-element-property :begin elem)))))))))
  21312. (defun org-drag-element-forward ()
  21313. "Move forward element at point."
  21314. (interactive)
  21315. (let* ((pos (point))
  21316. (elem (org-element-at-point)))
  21317. (when (= (point-max) (org-element-property :end elem))
  21318. (user-error "Cannot drag element forward"))
  21319. (goto-char (org-element-property :end elem))
  21320. (let ((next-elem (org-element-at-point)))
  21321. (when (or (org-element-nested-p elem next-elem)
  21322. (and (eq (org-element-type next-elem) 'headline)
  21323. (not (eq (org-element-type elem) 'headline))))
  21324. (goto-char pos)
  21325. (user-error "Cannot drag element forward"))
  21326. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21327. ;; body's length (without final blanks) and by the length of
  21328. ;; blanks between ELEM and NEXT-ELEM.
  21329. (let ((size-next (- (save-excursion
  21330. (goto-char (org-element-property :end next-elem))
  21331. (skip-chars-backward " \r\t\n")
  21332. (forward-line)
  21333. ;; Small correction if buffer doesn't end
  21334. ;; with a newline character.
  21335. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21336. (org-element-property :begin next-elem)))
  21337. (size-blank (- (org-element-property :end elem)
  21338. (save-excursion
  21339. (goto-char (org-element-property :end elem))
  21340. (skip-chars-backward " \r\t\n")
  21341. (forward-line)
  21342. (point)))))
  21343. (org-element-swap-A-B elem next-elem)
  21344. (goto-char (+ pos size-next size-blank))))))
  21345. (defun org-drag-line-forward (arg)
  21346. "Drag the line at point ARG lines forward."
  21347. (interactive "p")
  21348. (dotimes (n (abs arg))
  21349. (let ((c (current-column)))
  21350. (if (< 0 arg)
  21351. (progn
  21352. (beginning-of-line 2)
  21353. (transpose-lines 1)
  21354. (beginning-of-line 0))
  21355. (transpose-lines 1)
  21356. (beginning-of-line -1))
  21357. (org-move-to-column c))))
  21358. (defun org-drag-line-backward (arg)
  21359. "Drag the line at point ARG lines backward."
  21360. (interactive "p")
  21361. (org-drag-line-forward (- arg)))
  21362. (defun org-mark-element ()
  21363. "Put point at beginning of this element, mark at end.
  21364. Interactively, if this command is repeated or (in Transient Mark
  21365. mode) if the mark is active, it marks the next element after the
  21366. ones already marked."
  21367. (interactive)
  21368. (let (deactivate-mark)
  21369. (if (and (org-called-interactively-p 'any)
  21370. (or (and (eq last-command this-command) (mark t))
  21371. (and transient-mark-mode mark-active)))
  21372. (set-mark
  21373. (save-excursion
  21374. (goto-char (mark))
  21375. (goto-char (org-element-property :end (org-element-at-point)))))
  21376. (let ((element (org-element-at-point)))
  21377. (end-of-line)
  21378. (push-mark (org-element-property :end element) t t)
  21379. (goto-char (org-element-property :begin element))))))
  21380. (defun org-narrow-to-element ()
  21381. "Narrow buffer to current element."
  21382. (interactive)
  21383. (let ((elem (org-element-at-point)))
  21384. (cond
  21385. ((eq (car elem) 'headline)
  21386. (narrow-to-region
  21387. (org-element-property :begin elem)
  21388. (org-element-property :end elem)))
  21389. ((memq (car elem) org-element-greater-elements)
  21390. (narrow-to-region
  21391. (org-element-property :contents-begin elem)
  21392. (org-element-property :contents-end elem)))
  21393. (t
  21394. (narrow-to-region
  21395. (org-element-property :begin elem)
  21396. (org-element-property :end elem))))))
  21397. (defun org-transpose-element ()
  21398. "Transpose current and previous elements, keeping blank lines between.
  21399. Point is moved after both elements."
  21400. (interactive)
  21401. (org-skip-whitespace)
  21402. (let ((end (org-element-property :end (org-element-at-point))))
  21403. (org-drag-element-backward)
  21404. (goto-char end)))
  21405. (defun org-unindent-buffer ()
  21406. "Un-indent the visible part of the buffer.
  21407. Relative indentation (between items, inside blocks, etc.) isn't
  21408. modified."
  21409. (interactive)
  21410. (unless (eq major-mode 'org-mode)
  21411. (user-error "Cannot un-indent a buffer not in Org mode"))
  21412. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21413. unindent-tree ; For byte-compiler.
  21414. (unindent-tree
  21415. (function
  21416. (lambda (contents)
  21417. (mapc
  21418. (lambda (element)
  21419. (if (memq (org-element-type element) '(headline section))
  21420. (funcall unindent-tree (org-element-contents element))
  21421. (save-excursion
  21422. (save-restriction
  21423. (narrow-to-region
  21424. (org-element-property :begin element)
  21425. (org-element-property :end element))
  21426. (org-do-remove-indentation)))))
  21427. (reverse contents))))))
  21428. (funcall unindent-tree (org-element-contents parse-tree))))
  21429. (defun org-show-subtree ()
  21430. "Show everything after this heading at deeper levels."
  21431. (interactive)
  21432. (outline-flag-region
  21433. (point)
  21434. (save-excursion
  21435. (org-end-of-subtree t t))
  21436. nil))
  21437. (defun org-show-entry ()
  21438. "Show the body directly following this heading.
  21439. Show the heading too, if it is currently invisible."
  21440. (interactive)
  21441. (save-excursion
  21442. (condition-case nil
  21443. (progn
  21444. (org-back-to-heading t)
  21445. (outline-flag-region
  21446. (max (point-min) (1- (point)))
  21447. (save-excursion
  21448. (if (re-search-forward
  21449. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21450. (match-beginning 1)
  21451. (point-max)))
  21452. nil)
  21453. (org-cycle-hide-drawers 'children))
  21454. (error nil))))
  21455. (defun org-make-options-regexp (kwds &optional extra)
  21456. "Make a regular expression for keyword lines."
  21457. (concat
  21458. "^#\\+\\("
  21459. (mapconcat 'regexp-quote kwds "\\|")
  21460. (if extra (concat "\\|" extra))
  21461. "\\):[ \t]*\\(.*\\)"))
  21462. ;; Make isearch reveal the necessary context
  21463. (defun org-isearch-end ()
  21464. "Reveal context after isearch exits."
  21465. (when isearch-success ; only if search was successful
  21466. (if (featurep 'xemacs)
  21467. ;; Under XEmacs, the hook is run in the correct place,
  21468. ;; we directly show the context.
  21469. (org-show-context 'isearch)
  21470. ;; In Emacs the hook runs *before* restoring the overlays.
  21471. ;; So we have to use a one-time post-command-hook to do this.
  21472. ;; (Emacs 22 has a special variable, see function `org-mode')
  21473. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21474. isearch-mode-end-hook-quit)
  21475. ;; Only when the isearch was not quitted.
  21476. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21477. 'append 'local)))
  21478. (org-fix-ellipsis-at-bol)))
  21479. (defun org-isearch-post-command ()
  21480. "Remove self from hook, and show context."
  21481. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21482. (org-show-context 'isearch))
  21483. ;;;; Integration with and fixes for other packages
  21484. ;;; Imenu support
  21485. (defvar org-imenu-markers nil
  21486. "All markers currently used by Imenu.")
  21487. (make-variable-buffer-local 'org-imenu-markers)
  21488. (defun org-imenu-new-marker (&optional pos)
  21489. "Return a new marker for use by Imenu, and remember the marker."
  21490. (let ((m (make-marker)))
  21491. (move-marker m (or pos (point)))
  21492. (push m org-imenu-markers)
  21493. m))
  21494. (defun org-imenu-get-tree ()
  21495. "Produce the index for Imenu."
  21496. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21497. (setq org-imenu-markers nil)
  21498. (let* ((n org-imenu-depth)
  21499. (re (concat "^" (org-get-limited-outline-regexp)))
  21500. (subs (make-vector (1+ n) nil))
  21501. (last-level 0)
  21502. m level head0 head)
  21503. (save-excursion
  21504. (save-restriction
  21505. (widen)
  21506. (goto-char (point-max))
  21507. (while (re-search-backward re nil t)
  21508. (setq level (org-reduced-level (funcall outline-level)))
  21509. (when (and (<= level n)
  21510. (looking-at org-complex-heading-regexp)
  21511. (setq head0 (org-match-string-no-properties 4)))
  21512. (setq head (org-link-display-format head0)
  21513. m (org-imenu-new-marker))
  21514. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21515. (if (>= level last-level)
  21516. (push (cons head m) (aref subs level))
  21517. (push (cons head (aref subs (1+ level))) (aref subs level))
  21518. (loop for i from (1+ level) to n do (aset subs i nil)))
  21519. (setq last-level level)))))
  21520. (aref subs 1)))
  21521. (eval-after-load "imenu"
  21522. '(progn
  21523. (add-hook 'imenu-after-jump-hook
  21524. (lambda ()
  21525. (if (derived-mode-p 'org-mode)
  21526. (org-show-context 'org-goto))))))
  21527. (defun org-link-display-format (link)
  21528. "Replace a link with its the description.
  21529. If there is no description, use the link target."
  21530. (save-match-data
  21531. (if (string-match org-bracket-link-analytic-regexp link)
  21532. (replace-match (if (match-end 5)
  21533. (match-string 5 link)
  21534. (concat (match-string 1 link)
  21535. (match-string 3 link)))
  21536. nil t link)
  21537. link)))
  21538. (defun org-toggle-link-display ()
  21539. "Toggle the literal or descriptive display of links."
  21540. (interactive)
  21541. (if org-descriptive-links
  21542. (progn (org-remove-from-invisibility-spec '(org-link))
  21543. (org-restart-font-lock)
  21544. (setq org-descriptive-links nil))
  21545. (progn (add-to-invisibility-spec '(org-link))
  21546. (org-restart-font-lock)
  21547. (setq org-descriptive-links t))))
  21548. ;; Speedbar support
  21549. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21550. "Overlay marking the agenda restriction line in speedbar.")
  21551. (overlay-put org-speedbar-restriction-lock-overlay
  21552. 'face 'org-agenda-restriction-lock)
  21553. (overlay-put org-speedbar-restriction-lock-overlay
  21554. 'help-echo "Agendas are currently limited to this item.")
  21555. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21556. (defun org-speedbar-set-agenda-restriction ()
  21557. "Restrict future agenda commands to the location at point in speedbar.
  21558. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21559. (interactive)
  21560. (require 'org-agenda)
  21561. (let (p m tp np dir txt)
  21562. (cond
  21563. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21564. 'org-imenu t))
  21565. (setq m (get-text-property p 'org-imenu-marker))
  21566. (with-current-buffer (marker-buffer m)
  21567. (goto-char m)
  21568. (org-agenda-set-restriction-lock 'subtree)))
  21569. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21570. 'speedbar-function 'speedbar-find-file))
  21571. (setq tp (previous-single-property-change
  21572. (1+ p) 'speedbar-function)
  21573. np (next-single-property-change
  21574. tp 'speedbar-function)
  21575. dir (speedbar-line-directory)
  21576. txt (buffer-substring-no-properties (or tp (point-min))
  21577. (or np (point-max))))
  21578. (with-current-buffer (find-file-noselect
  21579. (let ((default-directory dir))
  21580. (expand-file-name txt)))
  21581. (unless (derived-mode-p 'org-mode)
  21582. (user-error "Cannot restrict to non-Org-mode file"))
  21583. (org-agenda-set-restriction-lock 'file)))
  21584. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  21585. (move-overlay org-speedbar-restriction-lock-overlay
  21586. (point-at-bol) (point-at-eol))
  21587. (setq current-prefix-arg nil)
  21588. (org-agenda-maybe-redo)))
  21589. (eval-after-load "speedbar"
  21590. '(progn
  21591. (speedbar-add-supported-extension ".org")
  21592. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21593. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21594. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21595. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21596. (add-hook 'speedbar-visiting-tag-hook
  21597. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21598. ;;; Fixes and Hacks for problems with other packages
  21599. ;; Make flyspell not check words in links, to not mess up our keymap
  21600. (defvar org-element-affiliated-keywords) ; From org-element.el
  21601. (defvar org-element-block-name-alist) ; From org-element.el
  21602. (defun org-mode-flyspell-verify ()
  21603. "Don't let flyspell put overlays at active buttons, or on
  21604. {todo,all-time,additional-option-like}-keywords."
  21605. (require 'org-element) ; For `org-element-affiliated-keywords'
  21606. (let ((pos (max (1- (point)) (point-min)))
  21607. (word (thing-at-point 'word)))
  21608. (and (not (get-text-property pos 'keymap))
  21609. (not (get-text-property pos 'org-no-flyspell))
  21610. (not (member word org-todo-keywords-1))
  21611. (not (member word org-all-time-keywords))
  21612. (not (member word org-options-keywords))
  21613. (not (member word (mapcar 'car org-startup-options)))
  21614. (not (member-ignore-case word org-element-affiliated-keywords))
  21615. (not (member-ignore-case word (org-get-export-keywords)))
  21616. (not (member-ignore-case
  21617. word (mapcar 'car org-element-block-name-alist)))
  21618. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21619. (defun org-remove-flyspell-overlays-in (beg end)
  21620. "Remove flyspell overlays in region."
  21621. (and (org-bound-and-true-p flyspell-mode)
  21622. (fboundp 'flyspell-delete-region-overlays)
  21623. (flyspell-delete-region-overlays beg end))
  21624. (add-text-properties beg end '(org-no-flyspell t)))
  21625. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21626. (eval-after-load "bookmark"
  21627. '(if (boundp 'bookmark-after-jump-hook)
  21628. ;; We can use the hook
  21629. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21630. ;; Hook not available, use advice
  21631. (defadvice bookmark-jump (after org-make-visible activate)
  21632. "Make the position visible."
  21633. (org-bookmark-jump-unhide))))
  21634. ;; Make sure saveplace shows the location if it was hidden
  21635. (eval-after-load "saveplace"
  21636. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21637. "Make the position visible."
  21638. (org-bookmark-jump-unhide)))
  21639. ;; Make sure ecb shows the location if it was hidden
  21640. (eval-after-load "ecb"
  21641. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21642. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21643. (if (derived-mode-p 'org-mode)
  21644. (org-show-context))))
  21645. (defun org-bookmark-jump-unhide ()
  21646. "Unhide the current position, to show the bookmark location."
  21647. (and (derived-mode-p 'org-mode)
  21648. (or (outline-invisible-p)
  21649. (save-excursion (goto-char (max (point-min) (1- (point))))
  21650. (outline-invisible-p)))
  21651. (org-show-context 'bookmark-jump)))
  21652. ;; Make session.el ignore our circular variable
  21653. (eval-after-load "session"
  21654. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21655. ;;;; Finish up
  21656. (provide 'org)
  21657. (run-hooks 'org-load-hook)
  21658. ;;; org.el ends here