org.el 890 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550
  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. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum))
  70. (require 'calendar)
  71. (require 'find-func)
  72. (require 'format-spec)
  73. (load "org-loaddefs.el" t t t)
  74. (require 'org-macs)
  75. (require 'org-compat)
  76. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  77. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  78. ;;
  79. ;; In Org buffers, the value of `outline-regexp' is that of
  80. ;; `org-outline-regexp'. The only function still directly relying on
  81. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  82. ;; job when `orgstruct-mode' is active.
  83. (defvar org-outline-regexp "\\*+ "
  84. "Regexp to match Org headlines.")
  85. (defvar org-outline-regexp-bol "^\\*+ "
  86. "Regexp to match Org headlines.
  87. This is similar to `org-outline-regexp' but additionally makes
  88. sure that we are at the beginning of the line.")
  89. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  90. "Matches a headline, putting stars and text into groups.
  91. Stars are put in group 1 and the trimmed body in group 2.")
  92. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  93. (unless (boundp 'calendar-view-holidays-initially-flag)
  94. (org-defvaralias 'calendar-view-holidays-initially-flag
  95. 'view-calendar-holidays-initially))
  96. (unless (boundp 'calendar-view-diary-initially-flag)
  97. (org-defvaralias 'calendar-view-diary-initially-flag
  98. 'view-diary-entries-initially))
  99. (unless (boundp 'diary-fancy-buffer)
  100. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  101. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  102. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  103. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  104. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  105. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  106. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  107. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  108. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  109. (declare-function orgtbl-mode "org-table" (&optional arg))
  110. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  111. (declare-function org-beamer-mode "ox-beamer" ())
  112. (declare-function org-table-edit-field "org-table" (arg))
  113. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  114. (declare-function org-table-set-constants "org-table" ())
  115. (declare-function org-id-get-create "org-id" (&optional force))
  116. (declare-function org-id-find-id-file "org-id" (id))
  117. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  118. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  119. (declare-function org-agenda-redo "org-agenda" (&optional all))
  120. (declare-function org-table-align "org-table" ())
  121. (declare-function org-table-paste-rectangle "org-table" ())
  122. (declare-function org-table-maybe-eval-formula "org-table" ())
  123. (declare-function org-table-maybe-recalculate-line "org-table" ())
  124. (declare-function org-element--parse-objects "org-element"
  125. (beg end acc restriction))
  126. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  127. (declare-function org-element-contents "org-element" (element))
  128. (declare-function org-element-context "org-element" (&optional element))
  129. (declare-function org-element-interpret-data "org-element"
  130. (data &optional parent))
  131. (declare-function org-element-map "org-element"
  132. (data types fun &optional info first-match no-recursion))
  133. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  134. (declare-function org-element-parse-buffer "org-element"
  135. (&optional granularity visible-only))
  136. (declare-function org-element-property "org-element" (property element))
  137. (declare-function org-element-put-property "org-element"
  138. (element property value))
  139. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  140. (declare-function org-element--parse-objects "org-element"
  141. (beg end acc restriction))
  142. (declare-function org-element-parse-buffer "org-element"
  143. (&optional granularity visible-only))
  144. (declare-function org-element-restriction "org-element" (element))
  145. (declare-function org-element-type "org-element" (element))
  146. ;; load languages based on value of `org-babel-load-languages'
  147. (defvar org-babel-load-languages)
  148. ;;;###autoload
  149. (defun org-babel-do-load-languages (sym value)
  150. "Load the languages defined in `org-babel-load-languages'."
  151. (set-default sym value)
  152. (mapc (lambda (pair)
  153. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  154. (if active
  155. (progn
  156. (require (intern (concat "ob-" lang))))
  157. (progn
  158. (funcall 'fmakunbound
  159. (intern (concat "org-babel-execute:" lang)))
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-expand-body:" lang)))))))
  162. org-babel-load-languages))
  163. ;;;###autoload
  164. (defun org-babel-load-file (file &optional compile)
  165. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  166. This function exports the source code using `org-babel-tangle'
  167. and then loads the resulting file using `load-file'. With prefix
  168. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  169. file to byte-code before it is loaded."
  170. (interactive "fFile to load: \nP")
  171. (require 'ob-core)
  172. (let* ((age (lambda (file)
  173. (float-time
  174. (time-subtract (current-time)
  175. (nth 5 (or (file-attributes (file-truename file))
  176. (file-attributes file)))))))
  177. (base-name (file-name-sans-extension file))
  178. (exported-file (concat base-name ".el")))
  179. ;; tangle if the org-mode file is newer than the elisp file
  180. (unless (and (file-exists-p exported-file)
  181. (> (funcall age file) (funcall age exported-file)))
  182. (org-babel-tangle-file file exported-file "emacs-lisp"))
  183. (message "%s %s"
  184. (if compile
  185. (progn (byte-compile-file exported-file 'load)
  186. "Compiled and loaded")
  187. (progn (load-file exported-file) "Loaded"))
  188. exported-file)))
  189. (defcustom org-babel-load-languages '((emacs-lisp . t))
  190. "Languages which can be evaluated in Org-mode buffers.
  191. This list can be used to load support for any of the languages
  192. below, note that each language will depend on a different set of
  193. system executables and/or Emacs modes. When a language is
  194. \"loaded\", then code blocks in that language can be evaluated
  195. with `org-babel-execute-src-block' bound by default to C-c
  196. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  197. be set to remove code block evaluation from the C-c C-c
  198. keybinding. By default only Emacs Lisp (which has no
  199. requirements) is loaded."
  200. :group 'org-babel
  201. :set 'org-babel-do-load-languages
  202. :version "24.1"
  203. :type '(alist :tag "Babel Languages"
  204. :key-type
  205. (choice
  206. (const :tag "Awk" awk)
  207. (const :tag "C" C)
  208. (const :tag "R" R)
  209. (const :tag "Asymptote" asymptote)
  210. (const :tag "Calc" calc)
  211. (const :tag "Clojure" clojure)
  212. (const :tag "CSS" css)
  213. (const :tag "Ditaa" ditaa)
  214. (const :tag "Dot" dot)
  215. (const :tag "Emacs Lisp" emacs-lisp)
  216. (const :tag "Fortran" fortran)
  217. (const :tag "Gnuplot" gnuplot)
  218. (const :tag "Haskell" haskell)
  219. (const :tag "IO" io)
  220. (const :tag "Java" java)
  221. (const :tag "Javascript" js)
  222. (const :tag "LaTeX" latex)
  223. (const :tag "Ledger" ledger)
  224. (const :tag "Lilypond" lilypond)
  225. (const :tag "Lisp" lisp)
  226. (const :tag "Makefile" makefile)
  227. (const :tag "Maxima" maxima)
  228. (const :tag "Matlab" matlab)
  229. (const :tag "Mscgen" mscgen)
  230. (const :tag "Ocaml" ocaml)
  231. (const :tag "Octave" octave)
  232. (const :tag "Org" org)
  233. (const :tag "Perl" perl)
  234. (const :tag "Pico Lisp" picolisp)
  235. (const :tag "PlantUML" plantuml)
  236. (const :tag "Python" python)
  237. (const :tag "Ruby" ruby)
  238. (const :tag "Sass" sass)
  239. (const :tag "Scala" scala)
  240. (const :tag "Scheme" scheme)
  241. (const :tag "Screen" screen)
  242. (const :tag "Shell Script" sh)
  243. (const :tag "Shen" shen)
  244. (const :tag "Sql" sql)
  245. (const :tag "Sqlite" sqlite))
  246. :value-type (boolean :tag "Activate" :value t)))
  247. ;;;; Customization variables
  248. (defcustom org-clone-delete-id nil
  249. "Remove ID property of clones of a subtree.
  250. When non-nil, clones of a subtree don't inherit the ID property.
  251. Otherwise they inherit the ID property with a new unique
  252. identifier."
  253. :type 'boolean
  254. :version "24.1"
  255. :group 'org-id)
  256. ;;; Version
  257. (org-check-version)
  258. ;;;###autoload
  259. (defun org-version (&optional here full message)
  260. "Show the org-mode version in the echo area.
  261. With prefix argument HERE, insert it at point.
  262. When FULL is non-nil, use a verbose version string.
  263. When MESSAGE is non-nil, display a message with the version."
  264. (interactive "P")
  265. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  266. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  267. (load-suffixes (list ".el"))
  268. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  269. (org-trash (or
  270. (and (fboundp 'org-release) (fboundp 'org-git-version))
  271. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  272. (load-suffixes save-load-suffixes)
  273. (org-version (org-release))
  274. (git-version (org-git-version))
  275. (version (format "Org-mode version %s (%s @ %s)"
  276. org-version
  277. git-version
  278. (if org-install-dir
  279. (if (string= org-dir org-install-dir)
  280. org-install-dir
  281. (concat "mixed installation! " org-install-dir " and " org-dir))
  282. "org-loaddefs.el can not be found!")))
  283. (_version (if full version org-version)))
  284. (if (org-called-interactively-p 'interactive)
  285. (if here
  286. (insert version)
  287. (message version))
  288. (if message (message _version))
  289. _version)))
  290. (defconst org-version (org-version))
  291. ;;; Compatibility constants
  292. ;;; The custom variables
  293. (defgroup org nil
  294. "Outline-based notes management and organizer."
  295. :tag "Org"
  296. :group 'outlines
  297. :group 'calendar)
  298. (defcustom org-mode-hook nil
  299. "Mode hook for Org-mode, run after the mode was turned on."
  300. :group 'org
  301. :type 'hook)
  302. (defcustom org-load-hook nil
  303. "Hook that is run after org.el has been loaded."
  304. :group 'org
  305. :type 'hook)
  306. (defcustom org-log-buffer-setup-hook nil
  307. "Hook that is run after an Org log buffer is created."
  308. :group 'org
  309. :version "24.1"
  310. :type 'hook)
  311. (defvar org-modules) ; defined below
  312. (defvar org-modules-loaded nil
  313. "Have the modules been loaded already?")
  314. (defun org-load-modules-maybe (&optional force)
  315. "Load all extensions listed in `org-modules'."
  316. (when (or force (not org-modules-loaded))
  317. (mapc (lambda (ext)
  318. (condition-case nil (require ext)
  319. (error (message "Problems while trying to load feature `%s'" ext))))
  320. org-modules)
  321. (setq org-modules-loaded t)))
  322. (defun org-set-modules (var value)
  323. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  324. (set var value)
  325. (when (featurep 'org)
  326. (org-load-modules-maybe 'force)))
  327. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  328. "Modules that should always be loaded together with org.el.
  329. If a description starts with <C>, the file is not part of Emacs
  330. and loading it will require that you have downloaded and properly
  331. installed the Org mode distribution.
  332. You can also use this system to load external packages (i.e. neither Org
  333. core modules, nor modules from the CONTRIB directory). Just add symbols
  334. to the end of the list. If the package is called org-xyz.el, then you need
  335. to add the symbol `xyz', and the package must have a call to:
  336. \(provide 'org-xyz)
  337. For export specific modules, see also `org-export-backends'."
  338. :group 'org
  339. :set 'org-set-modules
  340. :version "24.4"
  341. :package-version '(Org . "8.0")
  342. :type
  343. '(set :greedy t
  344. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  345. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  346. (const :tag " crypt: Encryption of subtrees" org-crypt)
  347. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  348. (const :tag " docview: Links to doc-view buffers" org-docview)
  349. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  350. (const :tag " habit: Track your consistency with habits" org-habit)
  351. (const :tag " id: Global IDs for identifying entries" org-id)
  352. (const :tag " info: Links to Info nodes" org-info)
  353. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  354. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  355. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  356. (const :tag " mouse: Additional mouse support" org-mouse)
  357. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  358. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  359. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  360. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  361. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  362. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  363. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  364. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  365. (const :tag "C collector: Collect properties into tables" org-collector)
  366. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  367. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  368. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  369. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  370. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  371. (const :tag "C eval: Include command output as text" org-eval)
  372. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  373. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  374. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  375. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  376. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  377. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  378. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  379. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  380. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  381. (const :tag "C mac-message: Links to messages in Apple Mail" org-mac-message)
  382. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  383. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  384. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  385. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  386. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  387. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  388. (const :tag "C registry: A registry for Org-mode links" org-registry)
  389. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  390. (const :tag "C secretary: Team management with org-mode" org-secretary)
  391. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  392. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  393. (const :tag "C track: Keep up with Org-mode development" org-track)
  394. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  395. (const :tag "C vm: Links to VM folders/messages" org-vm)
  396. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  397. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  398. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  399. (defvar org-export-registered-backends) ; From ox.el
  400. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  401. (defcustom org-export-backends '(ascii html icalendar latex)
  402. "List of export back-ends that should be always available.
  403. If a description starts with <C>, the file is not part of Emacs
  404. and loading it will require that you have downloaded and properly
  405. installed the Org mode distribution.
  406. Unlike to `org-modules', libraries in this list will not be
  407. loaded along with Org, but only once the export framework is
  408. needed.
  409. This variable needs to be set before org.el is loaded. If you
  410. need to make a change while Emacs is running, use the customize
  411. interface or run the following code, where VALUE stands for the
  412. new value of the variable, after updating it:
  413. \(progn
  414. \(setq org-export-registered-backends
  415. \(org-remove-if-not
  416. \(lambda (backend)
  417. \(or (memq backend val)
  418. \(catch 'parentp
  419. \(mapc
  420. \(lambda (b)
  421. \(and (org-export-derived-backend-p b (car backend))
  422. \(throw 'parentp t)))
  423. val)
  424. nil)))
  425. org-export-registered-backends))
  426. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  427. \(dolist (backend val)
  428. \(cond
  429. \((not (load (format \"ox-%s\" backend) t t))
  430. \(message \"Problems while trying to load export back-end `%s'\"
  431. backend))
  432. \((not (memq backend new-list)) (push backend new-list))))
  433. \(set-default var new-list)))
  434. Adding a back-end to this list will also pull the back-end it
  435. depends on, if any."
  436. :group 'org
  437. :group 'org-export
  438. :version "24.4"
  439. :package-version '(Org . "8.0")
  440. :initialize 'custom-initialize-set
  441. :set (lambda (var val)
  442. (if (not (featurep 'ox)) (set-default var val)
  443. ;; Any back-end not required anymore (not present in VAL and not
  444. ;; a parent of any back-end in the new value) is removed from the
  445. ;; list of registered back-ends.
  446. (setq org-export-registered-backends
  447. (org-remove-if-not
  448. (lambda (backend)
  449. (or (memq backend val)
  450. (catch 'parentp
  451. (mapc
  452. (lambda (b)
  453. (and (org-export-derived-backend-p b (car backend))
  454. (throw 'parentp t)))
  455. val)
  456. nil)))
  457. org-export-registered-backends))
  458. ;; Now build NEW-LIST of both new back-ends and required
  459. ;; parents.
  460. (let ((new-list (mapcar 'car org-export-registered-backends)))
  461. (dolist (backend val)
  462. (cond
  463. ((not (load (format "ox-%s" backend) t t))
  464. (message "Problems while trying to load export back-end `%s'"
  465. backend))
  466. ((not (memq backend new-list)) (push backend new-list))))
  467. ;; Set VAR to that list with fixed dependencies.
  468. (set-default var new-list))))
  469. :type '(set :greedy t
  470. (const :tag " ascii Export buffer to ASCII format" ascii)
  471. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  472. (const :tag " html Export buffer to HTML format" html)
  473. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  474. (const :tag " latex Export buffer to LaTeX format" latex)
  475. (const :tag " man Export buffer to MAN format" man)
  476. (const :tag " md Export buffer to Markdown format" md)
  477. (const :tag " odt Export buffer to ODT format" odt)
  478. (const :tag " org Export buffer to Org format" org)
  479. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  480. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  481. (const :tag "C deck Export buffer to deck.js presentations" deck)
  482. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  483. (const :tag "C groff Export buffer to Groff format" groff)
  484. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  485. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  486. (const :tag "C s5 Export buffer to s5 presentations" s5)
  487. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  488. (eval-after-load 'ox
  489. '(mapc
  490. (lambda (backend)
  491. (condition-case nil (require (intern (format "ox-%s" backend)))
  492. (error (message "Problems while trying to load export back-end `%s'"
  493. backend))))
  494. org-export-backends))
  495. (defcustom org-support-shift-select nil
  496. "Non-nil means make shift-cursor commands select text when possible.
  497. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  498. start selecting a region, or enlarge regions started in this way.
  499. In Org-mode, in special contexts, these same keys are used for
  500. other purposes, important enough to compete with shift selection.
  501. Org tries to balance these needs by supporting `shift-select-mode'
  502. outside these special contexts, under control of this variable.
  503. The default of this variable is nil, to avoid confusing behavior. Shifted
  504. cursor keys will then execute Org commands in the following contexts:
  505. - on a headline, changing TODO state (left/right) and priority (up/down)
  506. - on a time stamp, changing the time
  507. - in a plain list item, changing the bullet type
  508. - in a property definition line, switching between allowed values
  509. - in the BEGIN line of a clock table (changing the time block).
  510. Outside these contexts, the commands will throw an error.
  511. When this variable is t and the cursor is not in a special
  512. context, Org-mode will support shift-selection for making and
  513. enlarging regions. To make this more effective, the bullet
  514. cycling will no longer happen anywhere in an item line, but only
  515. if the cursor is exactly on the bullet.
  516. If you set this variable to the symbol `always', then the keys
  517. will not be special in headlines, property lines, and item lines,
  518. to make shift selection work there as well. If this is what you
  519. want, you can use the following alternative commands: `C-c C-t'
  520. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  521. can be used to switch TODO sets, `C-c -' to cycle item bullet
  522. types, and properties can be edited by hand or in column view.
  523. However, when the cursor is on a timestamp, shift-cursor commands
  524. will still edit the time stamp - this is just too good to give up.
  525. XEmacs user should have this variable set to nil, because
  526. `shift-select-mode' is in Emacs 23 or later only."
  527. :group 'org
  528. :type '(choice
  529. (const :tag "Never" nil)
  530. (const :tag "When outside special context" t)
  531. (const :tag "Everywhere except timestamps" always)))
  532. (defcustom org-loop-over-headlines-in-active-region nil
  533. "Shall some commands act upon headlines in the active region?
  534. When set to `t', some commands will be performed in all headlines
  535. within the active region.
  536. When set to `start-level', some commands will be performed in all
  537. headlines within the active region, provided that these headlines
  538. are of the same level than the first one.
  539. When set to a string, those commands will be performed on the
  540. matching headlines within the active region. Such string must be
  541. a tags/property/todo match as it is used in the agenda tags view.
  542. The list of commands is: `org-schedule', `org-deadline',
  543. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  544. `org-archive-to-archive-sibling'. The archiving commands skip
  545. already archived entries."
  546. :type '(choice (const :tag "Don't loop" nil)
  547. (const :tag "All headlines in active region" t)
  548. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  549. (string :tag "Tags/Property/Todo matcher"))
  550. :version "24.1"
  551. :group 'org-todo
  552. :group 'org-archive)
  553. (defgroup org-startup nil
  554. "Options concerning startup of Org-mode."
  555. :tag "Org Startup"
  556. :group 'org)
  557. (defcustom org-startup-folded t
  558. "Non-nil means entering Org-mode will switch to OVERVIEW.
  559. This can also be configured on a per-file basis by adding one of
  560. the following lines anywhere in the buffer:
  561. #+STARTUP: fold (or `overview', this is equivalent)
  562. #+STARTUP: nofold (or `showall', this is equivalent)
  563. #+STARTUP: content
  564. #+STARTUP: showeverything
  565. By default, this option is ignored when Org opens agenda files
  566. for the first time. If you want the agenda to honor the startup
  567. option, set `org-agenda-inhibit-startup' to nil."
  568. :group 'org-startup
  569. :type '(choice
  570. (const :tag "nofold: show all" nil)
  571. (const :tag "fold: overview" t)
  572. (const :tag "content: all headlines" content)
  573. (const :tag "show everything, even drawers" showeverything)))
  574. (defcustom org-startup-truncated t
  575. "Non-nil means entering Org-mode will set `truncate-lines'.
  576. This is useful since some lines containing links can be very long and
  577. uninteresting. Also tables look terrible when wrapped."
  578. :group 'org-startup
  579. :type 'boolean)
  580. (defcustom org-startup-indented nil
  581. "Non-nil means turn on `org-indent-mode' on startup.
  582. This can also be configured on a per-file basis by adding one of
  583. the following lines anywhere in the buffer:
  584. #+STARTUP: indent
  585. #+STARTUP: noindent"
  586. :group 'org-structure
  587. :type '(choice
  588. (const :tag "Not" nil)
  589. (const :tag "Globally (slow on startup in large files)" t)))
  590. (defcustom org-use-sub-superscripts t
  591. "Non-nil means interpret \"_\" and \"^\" for display.
  592. When this option is turned on, you can use TeX-like syntax for sub- and
  593. superscripts. Several characters after \"_\" or \"^\" will be
  594. considered as a single item - so grouping with {} is normally not
  595. needed. For example, the following things will be parsed as single
  596. sub- or superscripts.
  597. 10^24 or 10^tau several digits will be considered 1 item.
  598. 10^-12 or 10^-tau a leading sign with digits or a word
  599. x^2-y^3 will be read as x^2 - y^3, because items are
  600. terminated by almost any nonword/nondigit char.
  601. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  602. Still, ambiguity is possible - so when in doubt use {} to enclose
  603. the sub/superscript. If you set this variable to the symbol
  604. `{}', the braces are *required* in order to trigger
  605. interpretations as sub/superscript. This can be helpful in
  606. documents that need \"_\" frequently in plain text."
  607. :group 'org-startup
  608. :version "24.1"
  609. :type '(choice
  610. (const :tag "Always interpret" t)
  611. (const :tag "Only with braces" {})
  612. (const :tag "Never interpret" nil)))
  613. (defcustom org-startup-with-beamer-mode nil
  614. "Non-nil means turn on `org-beamer-mode' on startup.
  615. This can also be configured on a per-file basis by adding one of
  616. the following lines anywhere in the buffer:
  617. #+STARTUP: beamer"
  618. :group 'org-startup
  619. :version "24.1"
  620. :type 'boolean)
  621. (defcustom org-startup-align-all-tables nil
  622. "Non-nil means align all tables when visiting a file.
  623. This is useful when the column width in tables is forced with <N> cookies
  624. in table fields. Such tables will look correct only after the first re-align.
  625. This can also be configured on a per-file basis by adding one of
  626. the following lines anywhere in the buffer:
  627. #+STARTUP: align
  628. #+STARTUP: noalign"
  629. :group 'org-startup
  630. :type 'boolean)
  631. (defcustom org-startup-with-inline-images nil
  632. "Non-nil means show inline images when loading a new Org file.
  633. This can also be configured on a per-file basis by adding one of
  634. the following lines anywhere in the buffer:
  635. #+STARTUP: inlineimages
  636. #+STARTUP: noinlineimages"
  637. :group 'org-startup
  638. :version "24.1"
  639. :type 'boolean)
  640. (defcustom org-startup-with-latex-preview nil
  641. "Non-nil means preview LaTeX fragments when loading a new Org file.
  642. This can also be configured on a per-file basis by adding one of
  643. the followinglines anywhere in the buffer:
  644. #+STARTUP: latexpreview
  645. #+STARTUP: nolatexpreview"
  646. :group 'org-startup
  647. :version "24.4"
  648. :package-version '(Org . "8.0")
  649. :type 'boolean)
  650. (defcustom org-insert-mode-line-in-empty-file nil
  651. "Non-nil means insert the first line setting Org-mode in empty files.
  652. When the function `org-mode' is called interactively in an empty file, this
  653. normally means that the file name does not automatically trigger Org-mode.
  654. To ensure that the file will always be in Org-mode in the future, a
  655. line enforcing Org-mode will be inserted into the buffer, if this option
  656. has been set."
  657. :group 'org-startup
  658. :type 'boolean)
  659. (defcustom org-replace-disputed-keys nil
  660. "Non-nil means use alternative key bindings for some keys.
  661. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  662. These keys are also used by other packages like shift-selection-mode'
  663. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  664. If you want to use Org-mode together with one of these other modes,
  665. or more generally if you would like to move some Org-mode commands to
  666. other keys, set this variable and configure the keys with the variable
  667. `org-disputed-keys'.
  668. This option is only relevant at load-time of Org-mode, and must be set
  669. *before* org.el is loaded. Changing it requires a restart of Emacs to
  670. become effective."
  671. :group 'org-startup
  672. :type 'boolean)
  673. (defcustom org-use-extra-keys nil
  674. "Non-nil means use extra key sequence definitions for certain commands.
  675. This happens automatically if you run XEmacs or if `window-system'
  676. is nil. This variable lets you do the same manually. You must
  677. set it before loading org.
  678. Example: on Carbon Emacs 22 running graphically, with an external
  679. keyboard on a Powerbook, the default way of setting M-left might
  680. not work for either Alt or ESC. Setting this variable will make
  681. it work for ESC."
  682. :group 'org-startup
  683. :type 'boolean)
  684. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  685. (defcustom org-disputed-keys
  686. '(([(shift up)] . [(meta p)])
  687. ([(shift down)] . [(meta n)])
  688. ([(shift left)] . [(meta -)])
  689. ([(shift right)] . [(meta +)])
  690. ([(control shift right)] . [(meta shift +)])
  691. ([(control shift left)] . [(meta shift -)]))
  692. "Keys for which Org-mode and other modes compete.
  693. This is an alist, cars are the default keys, second element specifies
  694. the alternative to use when `org-replace-disputed-keys' is t.
  695. Keys can be specified in any syntax supported by `define-key'.
  696. The value of this option takes effect only at Org-mode's startup,
  697. therefore you'll have to restart Emacs to apply it after changing."
  698. :group 'org-startup
  699. :type 'alist)
  700. (defun org-key (key)
  701. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  702. Or return the original if not disputed.
  703. Also apply the translations defined in `org-xemacs-key-equivalents'."
  704. (when org-replace-disputed-keys
  705. (let* ((nkey (key-description key))
  706. (x (org-find-if (lambda (x)
  707. (equal (key-description (car x)) nkey))
  708. org-disputed-keys)))
  709. (setq key (if x (cdr x) key))))
  710. (when (featurep 'xemacs)
  711. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  712. key)
  713. (defun org-find-if (predicate seq)
  714. (catch 'exit
  715. (while seq
  716. (if (funcall predicate (car seq))
  717. (throw 'exit (car seq))
  718. (pop seq)))))
  719. (defun org-defkey (keymap key def)
  720. "Define a key, possibly translated, as returned by `org-key'."
  721. (define-key keymap (org-key key) def))
  722. (defcustom org-ellipsis nil
  723. "The ellipsis to use in the Org-mode outline.
  724. When nil, just use the standard three dots. When a string, use that instead,
  725. When a face, use the standard 3 dots, but with the specified face.
  726. The change affects only Org-mode (which will then use its own display table).
  727. Changing this requires executing `M-x org-mode' in a buffer to become
  728. effective."
  729. :group 'org-startup
  730. :type '(choice (const :tag "Default" nil)
  731. (face :tag "Face" :value org-warning)
  732. (string :tag "String" :value "...#")))
  733. (defvar org-display-table nil
  734. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  735. (defgroup org-keywords nil
  736. "Keywords in Org-mode."
  737. :tag "Org Keywords"
  738. :group 'org)
  739. (defcustom org-deadline-string "DEADLINE:"
  740. "String to mark deadline entries.
  741. A deadline is this string, followed by a time stamp. Should be a word,
  742. terminated by a colon. You can insert a schedule keyword and
  743. a timestamp with \\[org-deadline].
  744. Changes become only effective after restarting Emacs."
  745. :group 'org-keywords
  746. :type 'string)
  747. (defcustom org-scheduled-string "SCHEDULED:"
  748. "String to mark scheduled TODO entries.
  749. A schedule is this string, followed by a time stamp. Should be a word,
  750. terminated by a colon. You can insert a schedule keyword and
  751. a timestamp with \\[org-schedule].
  752. Changes become only effective after restarting Emacs."
  753. :group 'org-keywords
  754. :type 'string)
  755. (defcustom org-closed-string "CLOSED:"
  756. "String used as the prefix for timestamps logging closing a TODO entry."
  757. :group 'org-keywords
  758. :type 'string)
  759. (defcustom org-clock-string "CLOCK:"
  760. "String used as prefix for timestamps clocking work hours on an item."
  761. :group 'org-keywords
  762. :type 'string)
  763. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  764. org-scheduled-string "\\|"
  765. org-deadline-string "\\|"
  766. org-closed-string "\\|"
  767. org-clock-string "\\)")
  768. "Matches a line with planning or clock info.")
  769. (defcustom org-comment-string "COMMENT"
  770. "Entries starting with this keyword will never be exported.
  771. An entry can be toggled between COMMENT and normal with
  772. \\[org-toggle-comment].
  773. Changes become only effective after restarting Emacs."
  774. :group 'org-keywords
  775. :type 'string)
  776. (defcustom org-quote-string "QUOTE"
  777. "Entries starting with this keyword will be exported in fixed-width font.
  778. Quoting applies only to the text in the entry following the headline, and does
  779. not extend beyond the next headline, even if that is lower level.
  780. An entry can be toggled between QUOTE and normal with
  781. \\[org-toggle-fixed-width-section]."
  782. :group 'org-keywords
  783. :type 'string)
  784. (defconst org-repeat-re
  785. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  786. "Regular expression for specifying repeated events.
  787. After a match, group 1 contains the repeat expression.")
  788. (defgroup org-structure nil
  789. "Options concerning the general structure of Org-mode files."
  790. :tag "Org Structure"
  791. :group 'org)
  792. (defgroup org-reveal-location nil
  793. "Options about how to make context of a location visible."
  794. :tag "Org Reveal Location"
  795. :group 'org-structure)
  796. (defconst org-context-choice
  797. '(choice
  798. (const :tag "Always" t)
  799. (const :tag "Never" nil)
  800. (repeat :greedy t :tag "Individual contexts"
  801. (cons
  802. (choice :tag "Context"
  803. (const agenda)
  804. (const org-goto)
  805. (const occur-tree)
  806. (const tags-tree)
  807. (const link-search)
  808. (const mark-goto)
  809. (const bookmark-jump)
  810. (const isearch)
  811. (const default))
  812. (boolean))))
  813. "Contexts for the reveal options.")
  814. (defcustom org-show-hierarchy-above '((default . t))
  815. "Non-nil means show full hierarchy when revealing a location.
  816. Org-mode often shows locations in an org-mode file which might have
  817. been invisible before. When this is set, the hierarchy of headings
  818. above the exposed location is shown.
  819. Turning this off for example for sparse trees makes them very compact.
  820. Instead of t, this can also be an alist specifying this option for different
  821. contexts. Valid contexts are
  822. agenda when exposing an entry from the agenda
  823. org-goto when using the command `org-goto' on key C-c C-j
  824. occur-tree when using the command `org-occur' on key C-c /
  825. tags-tree when constructing a sparse tree based on tags matches
  826. link-search when exposing search matches associated with a link
  827. mark-goto when exposing the jump goal of a mark
  828. bookmark-jump when exposing a bookmark location
  829. isearch when exiting from an incremental search
  830. default default for all contexts not set explicitly"
  831. :group 'org-reveal-location
  832. :type org-context-choice)
  833. (defcustom org-show-following-heading '((default . nil))
  834. "Non-nil means show following heading when revealing a location.
  835. Org-mode often shows locations in an org-mode file which might have
  836. been invisible before. When this is set, the heading following the
  837. match is shown.
  838. Turning this off for example for sparse trees makes them very compact,
  839. but makes it harder to edit the location of the match. In such a case,
  840. use the command \\[org-reveal] to show more context.
  841. Instead of t, this can also be an alist specifying this option for different
  842. contexts. See `org-show-hierarchy-above' for valid contexts."
  843. :group 'org-reveal-location
  844. :type org-context-choice)
  845. (defcustom org-show-siblings '((default . nil) (isearch t) (bookmark-jump t))
  846. "Non-nil means show all sibling heading when revealing a location.
  847. Org-mode often shows locations in an org-mode file which might have
  848. been invisible before. When this is set, the sibling of the current entry
  849. heading are all made visible. If `org-show-hierarchy-above' is t,
  850. the same happens on each level of the hierarchy above the current entry.
  851. By default this is on for the isearch context, off for all other contexts.
  852. Turning this off for example for sparse trees makes them very compact,
  853. but makes it harder to edit the location of the match. In such a case,
  854. use the command \\[org-reveal] to show more context.
  855. Instead of t, this can also be an alist specifying this option for different
  856. contexts. See `org-show-hierarchy-above' for valid contexts."
  857. :group 'org-reveal-location
  858. :type org-context-choice
  859. :version "24.4"
  860. :package-version '(Org . "8.0"))
  861. (defcustom org-show-entry-below '((default . nil))
  862. "Non-nil means show the entry below a headline when revealing a location.
  863. Org-mode often shows locations in an org-mode file which might have
  864. been invisible before. When this is set, the text below the headline that is
  865. exposed is also shown.
  866. By default this is off for all contexts.
  867. Instead of t, this can also be an alist specifying this option for different
  868. contexts. See `org-show-hierarchy-above' for valid contexts."
  869. :group 'org-reveal-location
  870. :type org-context-choice)
  871. (defcustom org-indirect-buffer-display 'other-window
  872. "How should indirect tree buffers be displayed?
  873. This applies to indirect buffers created with the commands
  874. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  875. Valid values are:
  876. current-window Display in the current window
  877. other-window Just display in another window.
  878. dedicated-frame Create one new frame, and re-use it each time.
  879. new-frame Make a new frame each time. Note that in this case
  880. previously-made indirect buffers are kept, and you need to
  881. kill these buffers yourself."
  882. :group 'org-structure
  883. :group 'org-agenda-windows
  884. :type '(choice
  885. (const :tag "In current window" current-window)
  886. (const :tag "In current frame, other window" other-window)
  887. (const :tag "Each time a new frame" new-frame)
  888. (const :tag "One dedicated frame" dedicated-frame)))
  889. (defcustom org-use-speed-commands nil
  890. "Non-nil means activate single letter commands at beginning of a headline.
  891. This may also be a function to test for appropriate locations where speed
  892. commands should be active."
  893. :group 'org-structure
  894. :type '(choice
  895. (const :tag "Never" nil)
  896. (const :tag "At beginning of headline stars" t)
  897. (function)))
  898. (defcustom org-speed-commands-user nil
  899. "Alist of additional speed commands.
  900. This list will be checked before `org-speed-commands-default'
  901. when the variable `org-use-speed-commands' is non-nil
  902. and when the cursor is at the beginning of a headline.
  903. The car if each entry is a string with a single letter, which must
  904. be assigned to `self-insert-command' in the global map.
  905. The cdr is either a command to be called interactively, a function
  906. to be called, or a form to be evaluated.
  907. An entry that is just a list with a single string will be interpreted
  908. as a descriptive headline that will be added when listing the speed
  909. commands in the Help buffer using the `?' speed command."
  910. :group 'org-structure
  911. :type '(repeat :value ("k" . ignore)
  912. (choice :value ("k" . ignore)
  913. (list :tag "Descriptive Headline" (string :tag "Headline"))
  914. (cons :tag "Letter and Command"
  915. (string :tag "Command letter")
  916. (choice
  917. (function)
  918. (sexp))))))
  919. (defgroup org-cycle nil
  920. "Options concerning visibility cycling in Org-mode."
  921. :tag "Org Cycle"
  922. :group 'org-structure)
  923. (defcustom org-cycle-skip-children-state-if-no-children t
  924. "Non-nil means skip CHILDREN state in entries that don't have any."
  925. :group 'org-cycle
  926. :type 'boolean)
  927. (defcustom org-cycle-max-level nil
  928. "Maximum level which should still be subject to visibility cycling.
  929. Levels higher than this will, for cycling, be treated as text, not a headline.
  930. When `org-odd-levels-only' is set, a value of N in this variable actually
  931. means 2N-1 stars as the limiting headline.
  932. When nil, cycle all levels.
  933. Note that the limiting level of cycling is also influenced by
  934. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  935. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  936. than its value."
  937. :group 'org-cycle
  938. :type '(choice
  939. (const :tag "No limit" nil)
  940. (integer :tag "Maximum level")))
  941. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  942. "Names of drawers. Drawers are not opened by cycling on the headline above.
  943. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  944. this:
  945. :DRAWERNAME:
  946. .....
  947. :END:
  948. The drawer \"PROPERTIES\" is special for capturing properties through
  949. the property API.
  950. Drawers can be defined on the per-file basis with a line like:
  951. #+DRAWERS: HIDDEN STATE PROPERTIES"
  952. :group 'org-structure
  953. :group 'org-cycle
  954. :type '(repeat (string :tag "Drawer Name")))
  955. (defcustom org-hide-block-startup nil
  956. "Non-nil means entering Org-mode will fold all blocks.
  957. This can also be set in on a per-file basis with
  958. #+STARTUP: hideblocks
  959. #+STARTUP: showblocks"
  960. :group 'org-startup
  961. :group 'org-cycle
  962. :type 'boolean)
  963. (defcustom org-cycle-global-at-bob nil
  964. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  965. This makes it possible to do global cycling without having to use S-TAB or
  966. \\[universal-argument] TAB. For this special case to work, the first line
  967. of the buffer must not be a headline -- it may be empty or some other text.
  968. When used in this way, `org-cycle-hook' is disabled temporarily to make
  969. sure the cursor stays at the beginning of the buffer. When this option is
  970. nil, don't do anything special at the beginning of the buffer."
  971. :group 'org-cycle
  972. :type 'boolean)
  973. (defcustom org-cycle-level-after-item/entry-creation t
  974. "Non-nil means cycle entry level or item indentation in new empty entries.
  975. When the cursor is at the end of an empty headline, i.e., with only stars
  976. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  977. and then all possible ancestor states, before returning to the original state.
  978. This makes data entry extremely fast: M-RET to create a new headline,
  979. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  980. When the cursor is at the end of an empty plain list item, one TAB will
  981. make it a subitem, two or more tabs will back up to make this an item
  982. higher up in the item hierarchy."
  983. :group 'org-cycle
  984. :type 'boolean)
  985. (defcustom org-cycle-emulate-tab t
  986. "Where should `org-cycle' emulate TAB.
  987. nil Never
  988. white Only in completely white lines
  989. whitestart Only at the beginning of lines, before the first non-white char
  990. t Everywhere except in headlines
  991. exc-hl-bol Everywhere except at the start of a headline
  992. If TAB is used in a place where it does not emulate TAB, the current subtree
  993. visibility is cycled."
  994. :group 'org-cycle
  995. :type '(choice (const :tag "Never" nil)
  996. (const :tag "Only in completely white lines" white)
  997. (const :tag "Before first char in a line" whitestart)
  998. (const :tag "Everywhere except in headlines" t)
  999. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  1000. ))
  1001. (defcustom org-cycle-separator-lines 2
  1002. "Number of empty lines needed to keep an empty line between collapsed trees.
  1003. If you leave an empty line between the end of a subtree and the following
  1004. headline, this empty line is hidden when the subtree is folded.
  1005. Org-mode will leave (exactly) one empty line visible if the number of
  1006. empty lines is equal or larger to the number given in this variable.
  1007. So the default 2 means at least 2 empty lines after the end of a subtree
  1008. are needed to produce free space between a collapsed subtree and the
  1009. following headline.
  1010. If the number is negative, and the number of empty lines is at least -N,
  1011. all empty lines are shown.
  1012. Special case: when 0, never leave empty lines in collapsed view."
  1013. :group 'org-cycle
  1014. :type 'integer)
  1015. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1016. (defcustom org-pre-cycle-hook nil
  1017. "Hook that is run before visibility cycling is happening.
  1018. The function(s) in this hook must accept a single argument which indicates
  1019. the new state that will be set right after running this hook. The
  1020. argument is a symbol. Before a global state change, it can have the values
  1021. `overview', `content', or `all'. Before a local state change, it can have
  1022. the values `folded', `children', or `subtree'."
  1023. :group 'org-cycle
  1024. :type 'hook)
  1025. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1026. org-cycle-hide-drawers
  1027. org-cycle-hide-inline-tasks
  1028. org-cycle-show-empty-lines
  1029. org-optimize-window-after-visibility-change)
  1030. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1031. The function(s) in this hook must accept a single argument which indicates
  1032. the new state that was set by the most recent `org-cycle' command. The
  1033. argument is a symbol. After a global state change, it can have the values
  1034. `overview', `contents', or `all'. After a local state change, it can have
  1035. the values `folded', `children', or `subtree'."
  1036. :group 'org-cycle
  1037. :type 'hook)
  1038. (defgroup org-edit-structure nil
  1039. "Options concerning structure editing in Org-mode."
  1040. :tag "Org Edit Structure"
  1041. :group 'org-structure)
  1042. (defcustom org-odd-levels-only nil
  1043. "Non-nil means skip even levels and only use odd levels for the outline.
  1044. This has the effect that two stars are being added/taken away in
  1045. promotion/demotion commands. It also influences how levels are
  1046. handled by the exporters.
  1047. Changing it requires restart of `font-lock-mode' to become effective
  1048. for fontification also in regions already fontified.
  1049. You may also set this on a per-file basis by adding one of the following
  1050. lines to the buffer:
  1051. #+STARTUP: odd
  1052. #+STARTUP: oddeven"
  1053. :group 'org-edit-structure
  1054. :group 'org-appearance
  1055. :type 'boolean)
  1056. (defcustom org-adapt-indentation t
  1057. "Non-nil means adapt indentation to outline node level.
  1058. When this variable is set, Org assumes that you write outlines by
  1059. indenting text in each node to align with the headline (after the stars).
  1060. The following issues are influenced by this variable:
  1061. - When this is set and the *entire* text in an entry is indented, the
  1062. indentation is increased by one space in a demotion command, and
  1063. decreased by one in a promotion command. If any line in the entry
  1064. body starts with text at column 0, indentation is not changed at all.
  1065. - Property drawers and planning information is inserted indented when
  1066. this variable s set. When nil, they will not be indented.
  1067. - TAB indents a line relative to context. The lines below a headline
  1068. will be indented when this variable is set.
  1069. Note that this is all about true indentation, by adding and removing
  1070. space characters. See also `org-indent.el' which does level-dependent
  1071. indentation in a virtual way, i.e. at display time in Emacs."
  1072. :group 'org-edit-structure
  1073. :type 'boolean)
  1074. (defcustom org-special-ctrl-a/e nil
  1075. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1076. When t, `C-a' will bring back the cursor to the beginning of the
  1077. headline text, i.e. after the stars and after a possible TODO
  1078. keyword. In an item, this will be the position after bullet and
  1079. check-box, if any. When the cursor is already at that position,
  1080. another `C-a' will bring it to the beginning of the line.
  1081. `C-e' will jump to the end of the headline, ignoring the presence
  1082. of tags in the headline. A second `C-e' will then jump to the
  1083. true end of the line, after any tags. This also means that, when
  1084. this variable is non-nil, `C-e' also will never jump beyond the
  1085. end of the heading of a folded section, i.e. not after the
  1086. ellipses.
  1087. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1088. normally, going to the true line boundary first. Only a directly
  1089. following, identical keypress will bring the cursor to the
  1090. special positions.
  1091. This may also be a cons cell where the behavior for `C-a' and
  1092. `C-e' is set separately."
  1093. :group 'org-edit-structure
  1094. :type '(choice
  1095. (const :tag "off" nil)
  1096. (const :tag "on: after stars/bullet and before tags first" t)
  1097. (const :tag "reversed: true line boundary first" reversed)
  1098. (cons :tag "Set C-a and C-e separately"
  1099. (choice :tag "Special C-a"
  1100. (const :tag "off" nil)
  1101. (const :tag "on: after stars/bullet first" t)
  1102. (const :tag "reversed: before stars/bullet first" reversed))
  1103. (choice :tag "Special C-e"
  1104. (const :tag "off" nil)
  1105. (const :tag "on: before tags first" t)
  1106. (const :tag "reversed: after tags first" reversed)))))
  1107. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1108. (defcustom org-special-ctrl-k nil
  1109. "Non-nil means `C-k' will behave specially in headlines.
  1110. When nil, `C-k' will call the default `kill-line' command.
  1111. When t, the following will happen while the cursor is in the headline:
  1112. - When the cursor is at the beginning of a headline, kill the entire
  1113. line and possible the folded subtree below the line.
  1114. - When in the middle of the headline text, kill the headline up to the tags.
  1115. - When after the headline text, kill the tags."
  1116. :group 'org-edit-structure
  1117. :type 'boolean)
  1118. (defcustom org-ctrl-k-protect-subtree nil
  1119. "Non-nil means, do not delete a hidden subtree with C-k.
  1120. When set to the symbol `error', simply throw an error when C-k is
  1121. used to kill (part-of) a headline that has hidden text behind it.
  1122. Any other non-nil value will result in a query to the user, if it is
  1123. OK to kill that hidden subtree. When nil, kill without remorse."
  1124. :group 'org-edit-structure
  1125. :version "24.1"
  1126. :type '(choice
  1127. (const :tag "Do not protect hidden subtrees" nil)
  1128. (const :tag "Protect hidden subtrees with a security query" t)
  1129. (const :tag "Never kill a hidden subtree with C-k" error)))
  1130. (defcustom org-catch-invisible-edits nil
  1131. "Check if in invisible region before inserting or deleting a character.
  1132. Valid values are:
  1133. nil Do not check, so just do invisible edits.
  1134. error Throw an error and do nothing.
  1135. show Make point visible, and do the requested edit.
  1136. show-and-error Make point visible, then throw an error and abort the edit.
  1137. smart Make point visible, and do insertion/deletion if it is
  1138. adjacent to visible text and the change feels predictable.
  1139. Never delete a previously invisible character or add in the
  1140. middle or right after an invisible region. Basically, this
  1141. allows insertion and backward-delete right before ellipses.
  1142. FIXME: maybe in this case we should not even show?"
  1143. :group 'org-edit-structure
  1144. :version "24.1"
  1145. :type '(choice
  1146. (const :tag "Do not check" nil)
  1147. (const :tag "Throw error when trying to edit" error)
  1148. (const :tag "Unhide, but do not do the edit" show-and-error)
  1149. (const :tag "Show invisible part and do the edit" show)
  1150. (const :tag "Be smart and do the right thing" smart)))
  1151. (defcustom org-yank-folded-subtrees t
  1152. "Non-nil means when yanking subtrees, fold them.
  1153. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1154. it starts with a heading and all other headings in it are either children
  1155. or siblings, then fold all the subtrees. However, do this only if no
  1156. text after the yank would be swallowed into a folded tree by this action."
  1157. :group 'org-edit-structure
  1158. :type 'boolean)
  1159. (defcustom org-yank-adjusted-subtrees nil
  1160. "Non-nil means when yanking subtrees, adjust the level.
  1161. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1162. this function for details."
  1163. :group 'org-edit-structure
  1164. :type 'boolean)
  1165. (defcustom org-M-RET-may-split-line '((default . t))
  1166. "Non-nil means M-RET will split the line at the cursor position.
  1167. When nil, it will go to the end of the line before making a
  1168. new line.
  1169. You may also set this option in a different way for different
  1170. contexts. Valid contexts are:
  1171. headline when creating a new headline
  1172. item when creating a new item
  1173. table in a table field
  1174. default the value to be used for all contexts not explicitly
  1175. customized"
  1176. :group 'org-structure
  1177. :group 'org-table
  1178. :type '(choice
  1179. (const :tag "Always" t)
  1180. (const :tag "Never" nil)
  1181. (repeat :greedy t :tag "Individual contexts"
  1182. (cons
  1183. (choice :tag "Context"
  1184. (const headline)
  1185. (const item)
  1186. (const table)
  1187. (const default))
  1188. (boolean)))))
  1189. (defcustom org-insert-heading-respect-content nil
  1190. "Non-nil means insert new headings after the current subtree.
  1191. When nil, the new heading is created directly after the current line.
  1192. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1193. this variable on for the duration of the command."
  1194. :group 'org-structure
  1195. :type 'boolean)
  1196. (defcustom org-blank-before-new-entry '((heading . auto)
  1197. (plain-list-item . auto))
  1198. "Should `org-insert-heading' leave a blank line before new heading/item?
  1199. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1200. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1201. which case Org will look at the surrounding headings/items and try to
  1202. make an intelligent decision whether to insert a blank line or not.
  1203. For plain lists, if `org-list-empty-line-terminates-plain-lists' is set,
  1204. the setting here is ignored and no empty line is inserted to avoid breaking
  1205. the list structure."
  1206. :group 'org-edit-structure
  1207. :type '(list
  1208. (cons (const heading)
  1209. (choice (const :tag "Never" nil)
  1210. (const :tag "Always" t)
  1211. (const :tag "Auto" auto)))
  1212. (cons (const plain-list-item)
  1213. (choice (const :tag "Never" nil)
  1214. (const :tag "Always" t)
  1215. (const :tag "Auto" auto)))))
  1216. (defcustom org-insert-heading-hook nil
  1217. "Hook being run after inserting a new heading."
  1218. :group 'org-edit-structure
  1219. :type 'hook)
  1220. (defcustom org-enable-fixed-width-editor t
  1221. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1222. This currently only means they are never auto-wrapped.
  1223. When nil, such lines will be treated like ordinary lines.
  1224. See also the QUOTE keyword."
  1225. :group 'org-edit-structure
  1226. :type 'boolean)
  1227. (defcustom org-goto-auto-isearch t
  1228. "Non-nil means typing characters in `org-goto' starts incremental search.
  1229. When nil, you can use these keybindings to navigate the buffer:
  1230. q Quit the org-goto interface
  1231. n Go to the next visible heading
  1232. p Go to the previous visible heading
  1233. f Go one heading forward on same level
  1234. b Go one heading backward on same level
  1235. u Go one heading up"
  1236. :group 'org-edit-structure
  1237. :type 'boolean)
  1238. (defgroup org-sparse-trees nil
  1239. "Options concerning sparse trees in Org-mode."
  1240. :tag "Org Sparse Trees"
  1241. :group 'org-structure)
  1242. (defcustom org-highlight-sparse-tree-matches t
  1243. "Non-nil means highlight all matches that define a sparse tree.
  1244. The highlights will automatically disappear the next time the buffer is
  1245. changed by an edit command."
  1246. :group 'org-sparse-trees
  1247. :type 'boolean)
  1248. (defcustom org-remove-highlights-with-change t
  1249. "Non-nil means any change to the buffer will remove temporary highlights.
  1250. Such highlights are created by `org-occur' and `org-clock-display'.
  1251. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1252. The highlights created by `org-preview-latex-fragment' always need
  1253. `C-c C-c' to be removed."
  1254. :group 'org-sparse-trees
  1255. :group 'org-time
  1256. :type 'boolean)
  1257. (defcustom org-occur-hook '(org-first-headline-recenter)
  1258. "Hook that is run after `org-occur' has constructed a sparse tree.
  1259. This can be used to recenter the window to show as much of the structure
  1260. as possible."
  1261. :group 'org-sparse-trees
  1262. :type 'hook)
  1263. (defgroup org-imenu-and-speedbar nil
  1264. "Options concerning imenu and speedbar in Org-mode."
  1265. :tag "Org Imenu and Speedbar"
  1266. :group 'org-structure)
  1267. (defcustom org-imenu-depth 2
  1268. "The maximum level for Imenu access to Org-mode headlines.
  1269. This also applied for speedbar access."
  1270. :group 'org-imenu-and-speedbar
  1271. :type 'integer)
  1272. (defgroup org-table nil
  1273. "Options concerning tables in Org-mode."
  1274. :tag "Org Table"
  1275. :group 'org)
  1276. (defcustom org-enable-table-editor 'optimized
  1277. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1278. When nil, such lines will be treated like ordinary lines.
  1279. When equal to the symbol `optimized', the table editor will be optimized to
  1280. do the following:
  1281. - Automatic overwrite mode in front of whitespace in table fields.
  1282. This makes the structure of the table stay in tact as long as the edited
  1283. field does not exceed the column width.
  1284. - Minimize the number of realigns. Normally, the table is aligned each time
  1285. TAB or RET are pressed to move to another field. With optimization this
  1286. happens only if changes to a field might have changed the column width.
  1287. Optimization requires replacing the functions `self-insert-command',
  1288. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1289. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1290. very good at guessing when a re-align will be necessary, but you can always
  1291. force one with \\[org-ctrl-c-ctrl-c].
  1292. If you would like to use the optimized version in Org-mode, but the
  1293. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1294. This variable can be used to turn on and off the table editor during a session,
  1295. but in order to toggle optimization, a restart is required.
  1296. See also the variable `org-table-auto-blank-field'."
  1297. :group 'org-table
  1298. :type '(choice
  1299. (const :tag "off" nil)
  1300. (const :tag "on" t)
  1301. (const :tag "on, optimized" optimized)))
  1302. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1303. (version<= emacs-version "24.1"))
  1304. "Non-nil means cluster self-insert commands for undo when possible.
  1305. If this is set, then, like in the Emacs command loop, 20 consecutive
  1306. characters will be undone together.
  1307. This is configurable, because there is some impact on typing performance."
  1308. :group 'org-table
  1309. :type 'boolean)
  1310. (defcustom org-table-tab-recognizes-table.el t
  1311. "Non-nil means TAB will automatically notice a table.el table.
  1312. When it sees such a table, it moves point into it and - if necessary -
  1313. calls `table-recognize-table'."
  1314. :group 'org-table-editing
  1315. :type 'boolean)
  1316. (defgroup org-link nil
  1317. "Options concerning links in Org-mode."
  1318. :tag "Org Link"
  1319. :group 'org)
  1320. (defvar org-link-abbrev-alist-local nil
  1321. "Buffer-local version of `org-link-abbrev-alist', which see.
  1322. The value of this is taken from the #+LINK lines.")
  1323. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1324. (defcustom org-link-abbrev-alist nil
  1325. "Alist of link abbreviations.
  1326. The car of each element is a string, to be replaced at the start of a link.
  1327. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1328. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1329. [[linkkey:tag][description]]
  1330. The 'linkkey' must be a word word, starting with a letter, followed
  1331. by letters, numbers, '-' or '_'.
  1332. If REPLACE is a string, the tag will simply be appended to create the link.
  1333. If the string contains \"%s\", the tag will be inserted there. If the string
  1334. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1335. at that point (see the function `url-hexify-string'). If the string contains
  1336. the specifier \"%(my-function)\", then the custom function `my-function' will
  1337. be invoked: this function takes the tag as its only argument and must return
  1338. a string.
  1339. REPLACE may also be a function that will be called with the tag as the
  1340. only argument to create the link, which should be returned as a string.
  1341. See the manual for examples."
  1342. :group 'org-link
  1343. :type '(repeat
  1344. (cons
  1345. (string :tag "Protocol")
  1346. (choice
  1347. (string :tag "Format")
  1348. (function)))))
  1349. (defcustom org-descriptive-links t
  1350. "Non-nil means Org will display descriptive links.
  1351. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1352. \"Org Website\", hiding the link itself and just displaying its
  1353. description. When set to `nil', Org will display the full links
  1354. literally.
  1355. You can interactively set the value of this variable by calling
  1356. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1357. :group 'org-link
  1358. :type 'boolean)
  1359. (defcustom org-link-file-path-type 'adaptive
  1360. "How the path name in file links should be stored.
  1361. Valid values are:
  1362. relative Relative to the current directory, i.e. the directory of the file
  1363. into which the link is being inserted.
  1364. absolute Absolute path, if possible with ~ for home directory.
  1365. noabbrev Absolute path, no abbreviation of home directory.
  1366. adaptive Use relative path for files in the current directory and sub-
  1367. directories of it. For other files, use an absolute path."
  1368. :group 'org-link
  1369. :type '(choice
  1370. (const relative)
  1371. (const absolute)
  1372. (const noabbrev)
  1373. (const adaptive)))
  1374. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1375. "Types of links that should be activated in Org-mode files.
  1376. This is a list of symbols, each leading to the activation of a certain link
  1377. type. In principle, it does not hurt to turn on most link types - there may
  1378. be a small gain when turning off unused link types. The types are:
  1379. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1380. angle Links in angular brackets that may contain whitespace like
  1381. <bbdb:Carsten Dominik>.
  1382. plain Plain links in normal text, no whitespace, like http://google.com.
  1383. radio Text that is matched by a radio target, see manual for details.
  1384. tag Tag settings in a headline (link to tag search).
  1385. date Time stamps (link to calendar).
  1386. footnote Footnote labels.
  1387. Changing this variable requires a restart of Emacs to become effective."
  1388. :group 'org-link
  1389. :type '(set :greedy t
  1390. (const :tag "Double bracket links" bracket)
  1391. (const :tag "Angular bracket links" angle)
  1392. (const :tag "Plain text links" plain)
  1393. (const :tag "Radio target matches" radio)
  1394. (const :tag "Tags" tag)
  1395. (const :tag "Timestamps" date)
  1396. (const :tag "Footnotes" footnote)))
  1397. (defcustom org-make-link-description-function nil
  1398. "Function to use for generating link descriptions from links.
  1399. When nil, the link location will be used. This function must take
  1400. two parameters: the first one is the link, the second one is the
  1401. description generated by `org-insert-link'. The function should
  1402. return the description to use."
  1403. :group 'org-link
  1404. :type 'function)
  1405. (defgroup org-link-store nil
  1406. "Options concerning storing links in Org-mode."
  1407. :tag "Org Store Link"
  1408. :group 'org-link)
  1409. (defcustom org-url-hexify-p t
  1410. "When non-nil, hexify URL when creating a link."
  1411. :type 'boolean
  1412. :version "24.3"
  1413. :group 'org-link-store)
  1414. (defcustom org-email-link-description-format "Email %c: %.30s"
  1415. "Format of the description part of a link to an email or usenet message.
  1416. The following %-escapes will be replaced by corresponding information:
  1417. %F full \"From\" field
  1418. %f name, taken from \"From\" field, address if no name
  1419. %T full \"To\" field
  1420. %t first name in \"To\" field, address if no name
  1421. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1422. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1423. %s subject
  1424. %d date
  1425. %m message-id.
  1426. You may use normal field width specification between the % and the letter.
  1427. This is for example useful to limit the length of the subject.
  1428. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1429. :group 'org-link-store
  1430. :type 'string)
  1431. (defcustom org-from-is-user-regexp
  1432. (let (r1 r2)
  1433. (when (and user-mail-address (not (string= user-mail-address "")))
  1434. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1435. (when (and user-full-name (not (string= user-full-name "")))
  1436. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1437. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1438. "Regexp matched against the \"From:\" header of an email or usenet message.
  1439. It should match if the message is from the user him/herself."
  1440. :group 'org-link-store
  1441. :type 'regexp)
  1442. (defcustom org-context-in-file-links t
  1443. "Non-nil means file links from `org-store-link' contain context.
  1444. A search string will be added to the file name with :: as separator and
  1445. used to find the context when the link is activated by the command
  1446. `org-open-at-point'. When this option is t, the entire active region
  1447. will be placed in the search string of the file link. If set to a
  1448. positive integer, only the first n lines of context will be stored.
  1449. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1450. negates this setting for the duration of the command."
  1451. :group 'org-link-store
  1452. :type '(choice boolean integer))
  1453. (defcustom org-keep-stored-link-after-insertion nil
  1454. "Non-nil means keep link in list for entire session.
  1455. The command `org-store-link' adds a link pointing to the current
  1456. location to an internal list. These links accumulate during a session.
  1457. The command `org-insert-link' can be used to insert links into any
  1458. Org-mode file (offering completion for all stored links). When this
  1459. option is nil, every link which has been inserted once using \\[org-insert-link]
  1460. will be removed from the list, to make completing the unused links
  1461. more efficient."
  1462. :group 'org-link-store
  1463. :type 'boolean)
  1464. (defgroup org-link-follow nil
  1465. "Options concerning following links in Org-mode."
  1466. :tag "Org Follow Link"
  1467. :group 'org-link)
  1468. (defcustom org-link-translation-function nil
  1469. "Function to translate links with different syntax to Org syntax.
  1470. This can be used to translate links created for example by the Planner
  1471. or emacs-wiki packages to Org syntax.
  1472. The function must accept two parameters, a TYPE containing the link
  1473. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1474. which is everything after the link protocol. It should return a cons
  1475. with possibly modified values of type and path.
  1476. Org contains a function for this, so if you set this variable to
  1477. `org-translate-link-from-planner', you should be able follow many
  1478. links created by planner."
  1479. :group 'org-link-follow
  1480. :type 'function)
  1481. (defcustom org-follow-link-hook nil
  1482. "Hook that is run after a link has been followed."
  1483. :group 'org-link-follow
  1484. :type 'hook)
  1485. (defcustom org-tab-follows-link nil
  1486. "Non-nil means on links TAB will follow the link.
  1487. Needs to be set before org.el is loaded.
  1488. This really should not be used, it does not make sense, and the
  1489. implementation is bad."
  1490. :group 'org-link-follow
  1491. :type 'boolean)
  1492. (defcustom org-return-follows-link nil
  1493. "Non-nil means on links RET will follow the link.
  1494. In tables, the special behavior of RET has precedence."
  1495. :group 'org-link-follow
  1496. :type 'boolean)
  1497. (defcustom org-mouse-1-follows-link
  1498. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1499. "Non-nil means mouse-1 on a link will follow the link.
  1500. A longer mouse click will still set point. Does not work on XEmacs.
  1501. Needs to be set before org.el is loaded."
  1502. :group 'org-link-follow
  1503. :type 'boolean)
  1504. (defcustom org-mark-ring-length 4
  1505. "Number of different positions to be recorded in the ring.
  1506. Changing this requires a restart of Emacs to work correctly."
  1507. :group 'org-link-follow
  1508. :type 'integer)
  1509. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1510. "Non-nil means internal links in Org files must exactly match a headline.
  1511. When nil, the link search tries to match a phrase with all words
  1512. in the search text."
  1513. :group 'org-link-follow
  1514. :version "24.1"
  1515. :type '(choice
  1516. (const :tag "Use fuzzy text search" nil)
  1517. (const :tag "Match only exact headline" t)
  1518. (const :tag "Match exact headline or query to create it"
  1519. query-to-create)))
  1520. (defcustom org-link-frame-setup
  1521. '((vm . vm-visit-folder-other-frame)
  1522. (vm-imap . vm-visit-imap-folder-other-frame)
  1523. (gnus . org-gnus-no-new-news)
  1524. (file . find-file-other-window)
  1525. (wl . wl-other-frame))
  1526. "Setup the frame configuration for following links.
  1527. When following a link with Emacs, it may often be useful to display
  1528. this link in another window or frame. This variable can be used to
  1529. set this up for the different types of links.
  1530. For VM, use any of
  1531. `vm-visit-folder'
  1532. `vm-visit-folder-other-window'
  1533. `vm-visit-folder-other-frame'
  1534. For Gnus, use any of
  1535. `gnus'
  1536. `gnus-other-frame'
  1537. `org-gnus-no-new-news'
  1538. For FILE, use any of
  1539. `find-file'
  1540. `find-file-other-window'
  1541. `find-file-other-frame'
  1542. For Wanderlust use any of
  1543. `wl'
  1544. `wl-other-frame'
  1545. For the calendar, use the variable `calendar-setup'.
  1546. For BBDB, it is currently only possible to display the matches in
  1547. another window."
  1548. :group 'org-link-follow
  1549. :type '(list
  1550. (cons (const vm)
  1551. (choice
  1552. (const vm-visit-folder)
  1553. (const vm-visit-folder-other-window)
  1554. (const vm-visit-folder-other-frame)))
  1555. (cons (const gnus)
  1556. (choice
  1557. (const gnus)
  1558. (const gnus-other-frame)
  1559. (const org-gnus-no-new-news)))
  1560. (cons (const file)
  1561. (choice
  1562. (const find-file)
  1563. (const find-file-other-window)
  1564. (const find-file-other-frame)))
  1565. (cons (const wl)
  1566. (choice
  1567. (const wl)
  1568. (const wl-other-frame)))))
  1569. (defcustom org-display-internal-link-with-indirect-buffer nil
  1570. "Non-nil means use indirect buffer to display infile links.
  1571. Activating internal links (from one location in a file to another location
  1572. in the same file) normally just jumps to the location. When the link is
  1573. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1574. is displayed in
  1575. another window. When this option is set, the other window actually displays
  1576. an indirect buffer clone of the current buffer, to avoid any visibility
  1577. changes to the current buffer."
  1578. :group 'org-link-follow
  1579. :type 'boolean)
  1580. (defcustom org-open-non-existing-files nil
  1581. "Non-nil means `org-open-file' will open non-existing files.
  1582. When nil, an error will be generated.
  1583. This variable applies only to external applications because they
  1584. might choke on non-existing files. If the link is to a file that
  1585. will be opened in Emacs, the variable is ignored."
  1586. :group 'org-link-follow
  1587. :type 'boolean)
  1588. (defcustom org-open-directory-means-index-dot-org nil
  1589. "Non-nil means a link to a directory really means to index.org.
  1590. When nil, following a directory link will run dired or open a finder/explorer
  1591. window on that directory."
  1592. :group 'org-link-follow
  1593. :type 'boolean)
  1594. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1595. "Function and arguments to call for following mailto links.
  1596. This is a list with the first element being a Lisp function, and the
  1597. remaining elements being arguments to the function. In string arguments,
  1598. %a will be replaced by the address, and %s will be replaced by the subject
  1599. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1600. :group 'org-link-follow
  1601. :type '(choice
  1602. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1603. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1604. (const :tag "message-mail" (message-mail "%a" "%s"))
  1605. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1606. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1607. "Non-nil means ask for confirmation before executing shell links.
  1608. Shell links can be dangerous: just think about a link
  1609. [[shell:rm -rf ~/*][Google Search]]
  1610. This link would show up in your Org-mode document as \"Google Search\",
  1611. but really it would remove your entire home directory.
  1612. Therefore we advise against setting this variable to nil.
  1613. Just change it to `y-or-n-p' if you want to confirm with a
  1614. single keystroke rather than having to type \"yes\"."
  1615. :group 'org-link-follow
  1616. :type '(choice
  1617. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1618. (const :tag "with y-or-n (faster)" y-or-n-p)
  1619. (const :tag "no confirmation (dangerous)" nil)))
  1620. (put 'org-confirm-shell-link-function
  1621. 'safe-local-variable
  1622. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1623. (defcustom org-confirm-shell-link-not-regexp ""
  1624. "A regexp to skip confirmation for shell links."
  1625. :group 'org-link-follow
  1626. :version "24.1"
  1627. :type 'regexp)
  1628. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1629. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1630. Elisp links can be dangerous: just think about a link
  1631. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1632. This link would show up in your Org-mode document as \"Google Search\",
  1633. but really it would remove your entire home directory.
  1634. Therefore we advise against setting this variable to nil.
  1635. Just change it to `y-or-n-p' if you want to confirm with a
  1636. single keystroke rather than having to type \"yes\"."
  1637. :group 'org-link-follow
  1638. :type '(choice
  1639. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1640. (const :tag "with y-or-n (faster)" y-or-n-p)
  1641. (const :tag "no confirmation (dangerous)" nil)))
  1642. (put 'org-confirm-shell-link-function
  1643. 'safe-local-variable
  1644. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1645. (defcustom org-confirm-elisp-link-not-regexp ""
  1646. "A regexp to skip confirmation for Elisp links."
  1647. :group 'org-link-follow
  1648. :version "24.1"
  1649. :type 'regexp)
  1650. (defconst org-file-apps-defaults-gnu
  1651. '((remote . emacs)
  1652. (system . mailcap)
  1653. (t . mailcap))
  1654. "Default file applications on a UNIX or GNU/Linux system.
  1655. See `org-file-apps'.")
  1656. (defconst org-file-apps-defaults-macosx
  1657. '((remote . emacs)
  1658. (t . "open %s")
  1659. (system . "open %s")
  1660. ("ps.gz" . "gv %s")
  1661. ("eps.gz" . "gv %s")
  1662. ("dvi" . "xdvi %s")
  1663. ("fig" . "xfig %s"))
  1664. "Default file applications on a MacOS X system.
  1665. The system \"open\" is known as a default, but we use X11 applications
  1666. for some files for which the OS does not have a good default.
  1667. See `org-file-apps'.")
  1668. (defconst org-file-apps-defaults-windowsnt
  1669. (list
  1670. '(remote . emacs)
  1671. (cons t
  1672. (list (if (featurep 'xemacs)
  1673. 'mswindows-shell-execute
  1674. 'w32-shell-execute)
  1675. "open" 'file))
  1676. (cons 'system
  1677. (list (if (featurep 'xemacs)
  1678. 'mswindows-shell-execute
  1679. 'w32-shell-execute)
  1680. "open" 'file)))
  1681. "Default file applications on a Windows NT system.
  1682. The system \"open\" is used for most files.
  1683. See `org-file-apps'.")
  1684. (defcustom org-file-apps
  1685. '(
  1686. (auto-mode . emacs)
  1687. ("\\.mm\\'" . default)
  1688. ("\\.x?html?\\'" . default)
  1689. ("\\.pdf\\'" . default)
  1690. )
  1691. "External applications for opening `file:path' items in a document.
  1692. Org-mode uses system defaults for different file types, but
  1693. you can use this variable to set the application for a given file
  1694. extension. The entries in this list are cons cells where the car identifies
  1695. files and the cdr the corresponding command. Possible values for the
  1696. file identifier are
  1697. \"string\" A string as a file identifier can be interpreted in different
  1698. ways, depending on its contents:
  1699. - Alphanumeric characters only:
  1700. Match links with this file extension.
  1701. Example: (\"pdf\" . \"evince %s\")
  1702. to open PDFs with evince.
  1703. - Regular expression: Match links where the
  1704. filename matches the regexp. If you want to
  1705. use groups here, use shy groups.
  1706. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1707. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1708. to open *.html and *.xhtml with firefox.
  1709. - Regular expression which contains (non-shy) groups:
  1710. Match links where the whole link, including \"::\", and
  1711. anything after that, matches the regexp.
  1712. In a custom command string, %1, %2, etc. are replaced with
  1713. the parts of the link that were matched by the groups.
  1714. For backwards compatibility, if a command string is given
  1715. that does not use any of the group matches, this case is
  1716. handled identically to the second one (i.e. match against
  1717. file name only).
  1718. In a custom lisp form, you can access the group matches with
  1719. (match-string n link).
  1720. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1721. to open [[file:document.pdf::5]] with evince at page 5.
  1722. `directory' Matches a directory
  1723. `remote' Matches a remote file, accessible through tramp or efs.
  1724. Remote files most likely should be visited through Emacs
  1725. because external applications cannot handle such paths.
  1726. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1727. so all files Emacs knows how to handle. Using this with
  1728. command `emacs' will open most files in Emacs. Beware that this
  1729. will also open html files inside Emacs, unless you add
  1730. (\"html\" . default) to the list as well.
  1731. t Default for files not matched by any of the other options.
  1732. `system' The system command to open files, like `open' on Windows
  1733. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1734. that will be selected if you call `C-c C-o' with a double
  1735. \\[universal-argument] \\[universal-argument] prefix.
  1736. Possible values for the command are:
  1737. `emacs' The file will be visited by the current Emacs process.
  1738. `default' Use the default application for this file type, which is the
  1739. association for t in the list, most likely in the system-specific
  1740. part.
  1741. This can be used to overrule an unwanted setting in the
  1742. system-specific variable.
  1743. `system' Use the system command for opening files, like \"open\".
  1744. This command is specified by the entry whose car is `system'.
  1745. Most likely, the system-specific version of this variable
  1746. does define this command, but you can overrule/replace it
  1747. here.
  1748. string A command to be executed by a shell; %s will be replaced
  1749. by the path to the file.
  1750. sexp A Lisp form which will be evaluated. The file path will
  1751. be available in the Lisp variable `file'.
  1752. For more examples, see the system specific constants
  1753. `org-file-apps-defaults-macosx'
  1754. `org-file-apps-defaults-windowsnt'
  1755. `org-file-apps-defaults-gnu'."
  1756. :group 'org-link-follow
  1757. :type '(repeat
  1758. (cons (choice :value ""
  1759. (string :tag "Extension")
  1760. (const :tag "System command to open files" system)
  1761. (const :tag "Default for unrecognized files" t)
  1762. (const :tag "Remote file" remote)
  1763. (const :tag "Links to a directory" directory)
  1764. (const :tag "Any files that have Emacs modes"
  1765. auto-mode))
  1766. (choice :value ""
  1767. (const :tag "Visit with Emacs" emacs)
  1768. (const :tag "Use default" default)
  1769. (const :tag "Use the system command" system)
  1770. (string :tag "Command")
  1771. (sexp :tag "Lisp form")))))
  1772. (defcustom org-doi-server-url "http://dx.doi.org/"
  1773. "The URL of the DOI server."
  1774. :type 'string
  1775. :version "24.3"
  1776. :group 'org-link-follow)
  1777. (defgroup org-refile nil
  1778. "Options concerning refiling entries in Org-mode."
  1779. :tag "Org Refile"
  1780. :group 'org)
  1781. (defcustom org-directory "~/org"
  1782. "Directory with org files.
  1783. This is just a default location to look for Org files. There is no need
  1784. at all to put your files into this directory. It is only used in the
  1785. following situations:
  1786. 1. When a capture template specifies a target file that is not an
  1787. absolute path. The path will then be interpreted relative to
  1788. `org-directory'
  1789. 2. When a capture note is filed away in an interactive way (when exiting the
  1790. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1791. with `org-directory' as the default path."
  1792. :group 'org-refile
  1793. :group 'org-remember
  1794. :group 'org-capture
  1795. :type 'directory)
  1796. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1797. "Default target for storing notes.
  1798. Used as a fall back file for org-remember.el and org-capture.el, for
  1799. templates that do not specify a target file."
  1800. :group 'org-refile
  1801. :group 'org-remember
  1802. :group 'org-capture
  1803. :type '(choice
  1804. (const :tag "Default from remember-data-file" nil)
  1805. file))
  1806. (defcustom org-goto-interface 'outline
  1807. "The default interface to be used for `org-goto'.
  1808. Allowed values are:
  1809. outline The interface shows an outline of the relevant file
  1810. and the correct heading is found by moving through
  1811. the outline or by searching with incremental search.
  1812. outline-path-completion Headlines in the current buffer are offered via
  1813. completion. This is the interface also used by
  1814. the refile command."
  1815. :group 'org-refile
  1816. :type '(choice
  1817. (const :tag "Outline" outline)
  1818. (const :tag "Outline-path-completion" outline-path-completion)))
  1819. (defcustom org-goto-max-level 5
  1820. "Maximum target level when running `org-goto' with refile interface."
  1821. :group 'org-refile
  1822. :type 'integer)
  1823. (defcustom org-reverse-note-order nil
  1824. "Non-nil means store new notes at the beginning of a file or entry.
  1825. When nil, new notes will be filed to the end of a file or entry.
  1826. This can also be a list with cons cells of regular expressions that
  1827. are matched against file names, and values."
  1828. :group 'org-remember
  1829. :group 'org-capture
  1830. :group 'org-refile
  1831. :type '(choice
  1832. (const :tag "Reverse always" t)
  1833. (const :tag "Reverse never" nil)
  1834. (repeat :tag "By file name regexp"
  1835. (cons regexp boolean))))
  1836. (defcustom org-log-refile nil
  1837. "Information to record when a task is refiled.
  1838. Possible values are:
  1839. nil Don't add anything
  1840. time Add a time stamp to the task
  1841. note Prompt for a note and add it with template `org-log-note-headings'
  1842. This option can also be set with on a per-file-basis with
  1843. #+STARTUP: nologrefile
  1844. #+STARTUP: logrefile
  1845. #+STARTUP: lognoterefile
  1846. You can have local logging settings for a subtree by setting the LOGGING
  1847. property to one or more of these keywords.
  1848. When bulk-refiling from the agenda, the value `note' is forbidden and
  1849. will temporarily be changed to `time'."
  1850. :group 'org-refile
  1851. :group 'org-progress
  1852. :version "24.1"
  1853. :type '(choice
  1854. (const :tag "No logging" nil)
  1855. (const :tag "Record timestamp" time)
  1856. (const :tag "Record timestamp with note." note)))
  1857. (defcustom org-refile-targets nil
  1858. "Targets for refiling entries with \\[org-refile].
  1859. This is a list of cons cells. Each cell contains:
  1860. - a specification of the files to be considered, either a list of files,
  1861. or a symbol whose function or variable value will be used to retrieve
  1862. a file name or a list of file names. If you use `org-agenda-files' for
  1863. that, all agenda files will be scanned for targets. Nil means consider
  1864. headings in the current buffer.
  1865. - A specification of how to find candidate refile targets. This may be
  1866. any of:
  1867. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1868. This tag has to be present in all target headlines, inheritance will
  1869. not be considered.
  1870. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1871. todo keyword.
  1872. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1873. headlines that are refiling targets.
  1874. - a cons cell (:level . N). Any headline of level N is considered a target.
  1875. Note that, when `org-odd-levels-only' is set, level corresponds to
  1876. order in hierarchy, not to the number of stars.
  1877. - a cons cell (:maxlevel . N). Any headline with level <= N is 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. Each element of this list generates a set of possible targets.
  1881. The union of these sets is presented (with completion) to
  1882. the user by `org-refile'.
  1883. You can set the variable `org-refile-target-verify-function' to a function
  1884. to verify each headline found by the simple criteria above.
  1885. When this variable is nil, all top-level headlines in the current buffer
  1886. are used, equivalent to the value `((nil . (:level . 1))'."
  1887. :group 'org-refile
  1888. :type '(repeat
  1889. (cons
  1890. (choice :value org-agenda-files
  1891. (const :tag "All agenda files" org-agenda-files)
  1892. (const :tag "Current buffer" nil)
  1893. (function) (variable) (file))
  1894. (choice :tag "Identify target headline by"
  1895. (cons :tag "Specific tag" (const :value :tag) (string))
  1896. (cons :tag "TODO keyword" (const :value :todo) (string))
  1897. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1898. (cons :tag "Level number" (const :value :level) (integer))
  1899. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1900. (defcustom org-refile-target-verify-function nil
  1901. "Function to verify if the headline at point should be a refile target.
  1902. The function will be called without arguments, with point at the
  1903. beginning of the headline. It should return t and leave point
  1904. where it is if the headline is a valid target for refiling.
  1905. If the target should not be selected, the function must return nil.
  1906. In addition to this, it may move point to a place from where the search
  1907. should be continued. For example, the function may decide that the entire
  1908. subtree of the current entry should be excluded and move point to the end
  1909. of the subtree."
  1910. :group 'org-refile
  1911. :type 'function)
  1912. (defcustom org-refile-use-cache nil
  1913. "Non-nil means cache refile targets to speed up the process.
  1914. The cache for a particular file will be updated automatically when
  1915. the buffer has been killed, or when any of the marker used for flagging
  1916. refile targets no longer points at a live buffer.
  1917. If you have added new entries to a buffer that might themselves be targets,
  1918. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1919. find that easier, `C-u C-u C-u C-c C-w'."
  1920. :group 'org-refile
  1921. :version "24.1"
  1922. :type 'boolean)
  1923. (defcustom org-refile-use-outline-path nil
  1924. "Non-nil means provide refile targets as paths.
  1925. So a level 3 headline will be available as level1/level2/level3.
  1926. When the value is `file', also include the file name (without directory)
  1927. into the path. In this case, you can also stop the completion after
  1928. the file name, to get entries inserted as top level in the file.
  1929. When `full-file-path', include the full file path."
  1930. :group 'org-refile
  1931. :type '(choice
  1932. (const :tag "Not" nil)
  1933. (const :tag "Yes" t)
  1934. (const :tag "Start with file name" file)
  1935. (const :tag "Start with full file path" full-file-path)))
  1936. (defcustom org-outline-path-complete-in-steps t
  1937. "Non-nil means complete the outline path in hierarchical steps.
  1938. When Org-mode uses the refile interface to select an outline path
  1939. \(see variable `org-refile-use-outline-path'), the completion of
  1940. the path can be done is a single go, or if can be done in steps down
  1941. the headline hierarchy. Going in steps is probably the best if you
  1942. do not use a special completion package like `ido' or `icicles'.
  1943. However, when using these packages, going in one step can be very
  1944. fast, while still showing the whole path to the entry."
  1945. :group 'org-refile
  1946. :type 'boolean)
  1947. (defcustom org-refile-allow-creating-parent-nodes nil
  1948. "Non-nil means allow to create new nodes as refile targets.
  1949. New nodes are then created by adding \"/new node name\" to the completion
  1950. of an existing node. When the value of this variable is `confirm',
  1951. new node creation must be confirmed by the user (recommended)
  1952. When nil, the completion must match an existing entry.
  1953. Note that, if the new heading is not seen by the criteria
  1954. listed in `org-refile-targets', multiple instances of the same
  1955. heading would be created by trying again to file under the new
  1956. heading."
  1957. :group 'org-refile
  1958. :type '(choice
  1959. (const :tag "Never" nil)
  1960. (const :tag "Always" t)
  1961. (const :tag "Prompt for confirmation" confirm)))
  1962. (defcustom org-refile-active-region-within-subtree nil
  1963. "Non-nil means also refile active region within a subtree.
  1964. By default `org-refile' doesn't allow refiling regions if they
  1965. don't contain a set of subtrees, but it might be convenient to
  1966. do so sometimes: in that case, the first line of the region is
  1967. converted to a headline before refiling."
  1968. :group 'org-refile
  1969. :version "24.1"
  1970. :type 'boolean)
  1971. (defgroup org-todo nil
  1972. "Options concerning TODO items in Org-mode."
  1973. :tag "Org TODO"
  1974. :group 'org)
  1975. (defgroup org-progress nil
  1976. "Options concerning Progress logging in Org-mode."
  1977. :tag "Org Progress"
  1978. :group 'org-time)
  1979. (defvar org-todo-interpretation-widgets
  1980. '((:tag "Sequence (cycling hits every state)" sequence)
  1981. (:tag "Type (cycling directly to DONE)" type))
  1982. "The available interpretation symbols for customizing `org-todo-keywords'.
  1983. Interested libraries should add to this list.")
  1984. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1985. "List of TODO entry keyword sequences and their interpretation.
  1986. \\<org-mode-map>This is a list of sequences.
  1987. Each sequence starts with a symbol, either `sequence' or `type',
  1988. indicating if the keywords should be interpreted as a sequence of
  1989. action steps, or as different types of TODO items. The first
  1990. keywords are states requiring action - these states will select a headline
  1991. for inclusion into the global TODO list Org-mode produces. If one of
  1992. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1993. signify that no further action is necessary. If \"|\" is not found,
  1994. the last keyword is treated as the only DONE state of the sequence.
  1995. The command \\[org-todo] cycles an entry through these states, and one
  1996. additional state where no keyword is present. For details about this
  1997. cycling, see the manual.
  1998. TODO keywords and interpretation can also be set on a per-file basis with
  1999. the special #+SEQ_TODO and #+TYP_TODO lines.
  2000. Each keyword can optionally specify a character for fast state selection
  2001. \(in combination with the variable `org-use-fast-todo-selection')
  2002. and specifiers for state change logging, using the same syntax that
  2003. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2004. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2005. indicates to record a time stamp each time this state is selected.
  2006. Each keyword may also specify if a timestamp or a note should be
  2007. recorded when entering or leaving the state, by adding additional
  2008. characters in the parenthesis after the keyword. This looks like this:
  2009. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2010. record only the time of the state change. With X and Y being either
  2011. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2012. Y when leaving the state if and only if the *target* state does not
  2013. define X. You may omit any of the fast-selection key or X or /Y,
  2014. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2015. For backward compatibility, this variable may also be just a list
  2016. of keywords. In this case the interpretation (sequence or type) will be
  2017. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2018. :group 'org-todo
  2019. :group 'org-keywords
  2020. :type '(choice
  2021. (repeat :tag "Old syntax, just keywords"
  2022. (string :tag "Keyword"))
  2023. (repeat :tag "New syntax"
  2024. (cons
  2025. (choice
  2026. :tag "Interpretation"
  2027. ;;Quick and dirty way to see
  2028. ;;`org-todo-interpretations'. This takes the
  2029. ;;place of item arguments
  2030. :convert-widget
  2031. (lambda (widget)
  2032. (widget-put widget
  2033. :args (mapcar
  2034. #'(lambda (x)
  2035. (widget-convert
  2036. (cons 'const x)))
  2037. org-todo-interpretation-widgets))
  2038. widget))
  2039. (repeat
  2040. (string :tag "Keyword"))))))
  2041. (defvar org-todo-keywords-1 nil
  2042. "All TODO and DONE keywords active in a buffer.")
  2043. (make-variable-buffer-local 'org-todo-keywords-1)
  2044. (defvar org-todo-keywords-for-agenda nil)
  2045. (defvar org-done-keywords-for-agenda nil)
  2046. (defvar org-drawers-for-agenda nil)
  2047. (defvar org-todo-keyword-alist-for-agenda nil)
  2048. (defvar org-tag-alist-for-agenda nil
  2049. "Alist of all tags from all agenda files.")
  2050. (defvar org-tag-groups-alist-for-agenda nil
  2051. "Alist of all groups tags from all current agenda files.")
  2052. (defvar org-tag-groups-alist nil)
  2053. (make-variable-buffer-local 'org-tag-groups-alist)
  2054. (defvar org-agenda-contributing-files nil)
  2055. (defvar org-not-done-keywords nil)
  2056. (make-variable-buffer-local 'org-not-done-keywords)
  2057. (defvar org-done-keywords nil)
  2058. (make-variable-buffer-local 'org-done-keywords)
  2059. (defvar org-todo-heads nil)
  2060. (make-variable-buffer-local 'org-todo-heads)
  2061. (defvar org-todo-sets nil)
  2062. (make-variable-buffer-local 'org-todo-sets)
  2063. (defvar org-todo-log-states nil)
  2064. (make-variable-buffer-local 'org-todo-log-states)
  2065. (defvar org-todo-kwd-alist nil)
  2066. (make-variable-buffer-local 'org-todo-kwd-alist)
  2067. (defvar org-todo-key-alist nil)
  2068. (make-variable-buffer-local 'org-todo-key-alist)
  2069. (defvar org-todo-key-trigger nil)
  2070. (make-variable-buffer-local 'org-todo-key-trigger)
  2071. (defcustom org-todo-interpretation 'sequence
  2072. "Controls how TODO keywords are interpreted.
  2073. This variable is in principle obsolete and is only used for
  2074. backward compatibility, if the interpretation of todo keywords is
  2075. not given already in `org-todo-keywords'. See that variable for
  2076. more information."
  2077. :group 'org-todo
  2078. :group 'org-keywords
  2079. :type '(choice (const sequence)
  2080. (const type)))
  2081. (defcustom org-use-fast-todo-selection t
  2082. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2083. This variable describes if and under what circumstances the cycling
  2084. mechanism for TODO keywords will be replaced by a single-key, direct
  2085. selection scheme.
  2086. When nil, fast selection is never used.
  2087. When the symbol `prefix', it will be used when `org-todo' is called
  2088. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2089. `C-u t' in an agenda buffer.
  2090. When t, fast selection is used by default. In this case, the prefix
  2091. argument forces cycling instead.
  2092. In all cases, the special interface is only used if access keys have
  2093. actually been assigned by the user, i.e. if keywords in the configuration
  2094. are followed by a letter in parenthesis, like TODO(t)."
  2095. :group 'org-todo
  2096. :type '(choice
  2097. (const :tag "Never" nil)
  2098. (const :tag "By default" t)
  2099. (const :tag "Only with C-u C-c C-t" prefix)))
  2100. (defcustom org-provide-todo-statistics t
  2101. "Non-nil means update todo statistics after insert and toggle.
  2102. ALL-HEADLINES means update todo statistics by including headlines
  2103. with no TODO keyword as well, counting them as not done.
  2104. A list of TODO keywords means the same, but skip keywords that are
  2105. not in this list.
  2106. When this is set, todo statistics is updated in the parent of the
  2107. current entry each time a todo state is changed."
  2108. :group 'org-todo
  2109. :type '(choice
  2110. (const :tag "Yes, only for TODO entries" t)
  2111. (const :tag "Yes, including all entries" 'all-headlines)
  2112. (repeat :tag "Yes, for TODOs in this list"
  2113. (string :tag "TODO keyword"))
  2114. (other :tag "No TODO statistics" nil)))
  2115. (defcustom org-hierarchical-todo-statistics t
  2116. "Non-nil means TODO statistics covers just direct children.
  2117. When nil, all entries in the subtree are considered.
  2118. This has only an effect if `org-provide-todo-statistics' is set.
  2119. To set this to nil for only a single subtree, use a COOKIE_DATA
  2120. property and include the word \"recursive\" into the value."
  2121. :group 'org-todo
  2122. :type 'boolean)
  2123. (defcustom org-after-todo-state-change-hook nil
  2124. "Hook which is run after the state of a TODO item was changed.
  2125. The new state (a string with a TODO keyword, or nil) is available in the
  2126. Lisp variable `org-state'."
  2127. :group 'org-todo
  2128. :type 'hook)
  2129. (defvar org-blocker-hook nil
  2130. "Hook for functions that are allowed to block a state change.
  2131. Functions in this hook should not modify the buffer.
  2132. Each function gets as its single argument a property list,
  2133. see `org-trigger-hook' for more information about this list.
  2134. If any of the functions in this hook returns nil, the state change
  2135. is blocked.")
  2136. (defvar org-trigger-hook nil
  2137. "Hook for functions that are triggered by a state change.
  2138. Each function gets as its single argument a property list with at
  2139. least the following elements:
  2140. (:type type-of-change :position pos-at-entry-start
  2141. :from old-state :to new-state)
  2142. Depending on the type, more properties may be present.
  2143. This mechanism is currently implemented for:
  2144. TODO state changes
  2145. ------------------
  2146. :type todo-state-change
  2147. :from previous state (keyword as a string), or nil, or a symbol
  2148. 'todo' or 'done', to indicate the general type of state.
  2149. :to new state, like in :from")
  2150. (defcustom org-enforce-todo-dependencies nil
  2151. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2152. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2153. be blocked if any prior sibling is not yet done.
  2154. Finally, if the parent is blocked because of ordered siblings of its own,
  2155. the child will also be blocked."
  2156. :set (lambda (var val)
  2157. (set var val)
  2158. (if val
  2159. (add-hook 'org-blocker-hook
  2160. 'org-block-todo-from-children-or-siblings-or-parent)
  2161. (remove-hook 'org-blocker-hook
  2162. 'org-block-todo-from-children-or-siblings-or-parent)))
  2163. :group 'org-todo
  2164. :type 'boolean)
  2165. (defcustom org-enforce-todo-checkbox-dependencies nil
  2166. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2167. When this is nil, checkboxes have no influence on switching TODO states.
  2168. When non-nil, you first need to check off all check boxes before the TODO
  2169. entry can be switched to DONE.
  2170. This variable needs to be set before org.el is loaded, and you need to
  2171. restart Emacs after a change to make the change effective. The only way
  2172. to change is while Emacs is running is through the customize interface."
  2173. :set (lambda (var val)
  2174. (set var val)
  2175. (if val
  2176. (add-hook 'org-blocker-hook
  2177. 'org-block-todo-from-checkboxes)
  2178. (remove-hook 'org-blocker-hook
  2179. 'org-block-todo-from-checkboxes)))
  2180. :group 'org-todo
  2181. :type 'boolean)
  2182. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2183. "Non-nil means inserting a TODO heading is treated as state change.
  2184. So when the command \\[org-insert-todo-heading] is used, state change
  2185. logging will apply if appropriate. When nil, the new TODO item will
  2186. be inserted directly, and no logging will take place."
  2187. :group 'org-todo
  2188. :type 'boolean)
  2189. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2190. "Non-nil means switching TODO states with S-cursor counts as state change.
  2191. This is the default behavior. However, setting this to nil allows a
  2192. convenient way to select a TODO state and bypass any logging associated
  2193. with that."
  2194. :group 'org-todo
  2195. :type 'boolean)
  2196. (defcustom org-todo-state-tags-triggers nil
  2197. "Tag changes that should be triggered by TODO state changes.
  2198. This is a list. Each entry is
  2199. (state-change (tag . flag) .......)
  2200. State-change can be a string with a state, and empty string to indicate the
  2201. state that has no TODO keyword, or it can be one of the symbols `todo'
  2202. or `done', meaning any not-done or done state, respectively."
  2203. :group 'org-todo
  2204. :group 'org-tags
  2205. :type '(repeat
  2206. (cons (choice :tag "When changing to"
  2207. (const :tag "Not-done state" todo)
  2208. (const :tag "Done state" done)
  2209. (string :tag "State"))
  2210. (repeat
  2211. (cons :tag "Tag action"
  2212. (string :tag "Tag")
  2213. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2214. (defcustom org-log-done nil
  2215. "Information to record when a task moves to the DONE state.
  2216. Possible values are:
  2217. nil Don't add anything, just change the keyword
  2218. time Add a time stamp to the task
  2219. note Prompt for a note and add it with template `org-log-note-headings'
  2220. This option can also be set with on a per-file-basis with
  2221. #+STARTUP: nologdone
  2222. #+STARTUP: logdone
  2223. #+STARTUP: lognotedone
  2224. You can have local logging settings for a subtree by setting the LOGGING
  2225. property to one or more of these keywords."
  2226. :group 'org-todo
  2227. :group 'org-progress
  2228. :type '(choice
  2229. (const :tag "No logging" nil)
  2230. (const :tag "Record CLOSED timestamp" time)
  2231. (const :tag "Record CLOSED timestamp with note." note)))
  2232. ;; Normalize old uses of org-log-done.
  2233. (cond
  2234. ((eq org-log-done t) (setq org-log-done 'time))
  2235. ((and (listp org-log-done) (memq 'done org-log-done))
  2236. (setq org-log-done 'note)))
  2237. (defcustom org-log-reschedule nil
  2238. "Information to record when the scheduling date of a tasks is modified.
  2239. Possible values are:
  2240. nil Don't add anything, just change the date
  2241. time Add a time stamp to the task
  2242. note Prompt for a note and add it with template `org-log-note-headings'
  2243. This option can also be set with on a per-file-basis with
  2244. #+STARTUP: nologreschedule
  2245. #+STARTUP: logreschedule
  2246. #+STARTUP: lognotereschedule"
  2247. :group 'org-todo
  2248. :group 'org-progress
  2249. :type '(choice
  2250. (const :tag "No logging" nil)
  2251. (const :tag "Record timestamp" time)
  2252. (const :tag "Record timestamp with note." note)))
  2253. (defcustom org-log-redeadline nil
  2254. "Information to record when the deadline date of a tasks is modified.
  2255. Possible values are:
  2256. nil Don't add anything, just change the date
  2257. time Add a time stamp to the task
  2258. note Prompt for a note and add it with template `org-log-note-headings'
  2259. This option can also be set with on a per-file-basis with
  2260. #+STARTUP: nologredeadline
  2261. #+STARTUP: logredeadline
  2262. #+STARTUP: lognoteredeadline
  2263. You can have local logging settings for a subtree by setting the LOGGING
  2264. property to one or more of these keywords."
  2265. :group 'org-todo
  2266. :group 'org-progress
  2267. :type '(choice
  2268. (const :tag "No logging" nil)
  2269. (const :tag "Record timestamp" time)
  2270. (const :tag "Record timestamp with note." note)))
  2271. (defcustom org-log-note-clock-out nil
  2272. "Non-nil means record a note when clocking out of an item.
  2273. This can also be configured on a per-file basis by adding one of
  2274. the following lines anywhere in the buffer:
  2275. #+STARTUP: lognoteclock-out
  2276. #+STARTUP: nolognoteclock-out"
  2277. :group 'org-todo
  2278. :group 'org-progress
  2279. :type 'boolean)
  2280. (defcustom org-log-done-with-time t
  2281. "Non-nil means the CLOSED time stamp will contain date and time.
  2282. When nil, only the date will be recorded."
  2283. :group 'org-progress
  2284. :type 'boolean)
  2285. (defcustom org-log-note-headings
  2286. '((done . "CLOSING NOTE %t")
  2287. (state . "State %-12s from %-12S %t")
  2288. (note . "Note taken on %t")
  2289. (reschedule . "Rescheduled from %S on %t")
  2290. (delschedule . "Not scheduled, was %S on %t")
  2291. (redeadline . "New deadline from %S on %t")
  2292. (deldeadline . "Removed deadline, was %S on %t")
  2293. (refile . "Refiled on %t")
  2294. (clock-out . ""))
  2295. "Headings for notes added to entries.
  2296. The value is an alist, with the car being a symbol indicating the note
  2297. context, and the cdr is the heading to be used. The heading may also be the
  2298. empty string.
  2299. %t in the heading will be replaced by a time stamp.
  2300. %T will be an active time stamp instead the default inactive one
  2301. %d will be replaced by a short-format time stamp.
  2302. %D will be replaced by an active short-format time stamp.
  2303. %s will be replaced by the new TODO state, in double quotes.
  2304. %S will be replaced by the old TODO state, in double quotes.
  2305. %u will be replaced by the user name.
  2306. %U will be replaced by the full user name.
  2307. In fact, it is not a good idea to change the `state' entry, because
  2308. agenda log mode depends on the format of these entries."
  2309. :group 'org-todo
  2310. :group 'org-progress
  2311. :type '(list :greedy t
  2312. (cons (const :tag "Heading when closing an item" done) string)
  2313. (cons (const :tag
  2314. "Heading when changing todo state (todo sequence only)"
  2315. state) string)
  2316. (cons (const :tag "Heading when just taking a note" note) string)
  2317. (cons (const :tag "Heading when clocking out" clock-out) string)
  2318. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2319. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2320. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2321. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2322. (cons (const :tag "Heading when refiling" refile) string)))
  2323. (unless (assq 'note org-log-note-headings)
  2324. (push '(note . "%t") org-log-note-headings))
  2325. (defcustom org-log-into-drawer nil
  2326. "Non-nil means insert state change notes and time stamps into a drawer.
  2327. When nil, state changes notes will be inserted after the headline and
  2328. any scheduling and clock lines, but not inside a drawer.
  2329. The value of this variable should be the name of the drawer to use.
  2330. LOGBOOK is proposed as the default drawer for this purpose, you can
  2331. also set this to a string to define the drawer of your choice.
  2332. A value of t is also allowed, representing \"LOGBOOK\".
  2333. A value of t or nil can also be set with on a per-file-basis with
  2334. #+STARTUP: logdrawer
  2335. #+STARTUP: nologdrawer
  2336. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2337. will be ignored.
  2338. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2339. a subtree."
  2340. :group 'org-todo
  2341. :group 'org-progress
  2342. :type '(choice
  2343. (const :tag "Not into a drawer" nil)
  2344. (const :tag "LOGBOOK" t)
  2345. (string :tag "Other")))
  2346. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2347. (defun org-log-into-drawer ()
  2348. "Return the value of `org-log-into-drawer', but let properties overrule.
  2349. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2350. used instead of the default value."
  2351. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2352. (cond
  2353. ((not p) org-log-into-drawer)
  2354. ((equal p "nil") nil)
  2355. ((equal p "t") "LOGBOOK")
  2356. (t p))))
  2357. (defcustom org-log-state-notes-insert-after-drawers nil
  2358. "Non-nil means insert state change notes after any drawers in entry.
  2359. Only the drawers that *immediately* follow the headline and the
  2360. deadline/scheduled line are skipped.
  2361. When nil, insert notes right after the heading and perhaps the line
  2362. with deadline/scheduling if present.
  2363. This variable will have no effect if `org-log-into-drawer' is
  2364. set."
  2365. :group 'org-todo
  2366. :group 'org-progress
  2367. :type 'boolean)
  2368. (defcustom org-log-states-order-reversed t
  2369. "Non-nil means the latest state note will be directly after heading.
  2370. When nil, the state change notes will be ordered according to time.
  2371. This option can also be set with on a per-file-basis with
  2372. #+STARTUP: logstatesreversed
  2373. #+STARTUP: nologstatesreversed"
  2374. :group 'org-todo
  2375. :group 'org-progress
  2376. :type 'boolean)
  2377. (defcustom org-todo-repeat-to-state nil
  2378. "The TODO state to which a repeater should return the repeating task.
  2379. By default this is the first task in a TODO sequence, or the previous state
  2380. in a TODO_TYP set. But you can specify another task here.
  2381. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2382. :group 'org-todo
  2383. :version "24.1"
  2384. :type '(choice (const :tag "Head of sequence" nil)
  2385. (string :tag "Specific state")))
  2386. (defcustom org-log-repeat 'time
  2387. "Non-nil means record moving through the DONE state when triggering repeat.
  2388. An auto-repeating task is immediately switched back to TODO when
  2389. marked DONE. If you are not logging state changes (by adding \"@\"
  2390. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2391. record a closing note, there will be no record of the task moving
  2392. through DONE. This variable forces taking a note anyway.
  2393. nil Don't force a record
  2394. time Record a time stamp
  2395. note Prompt for a note and add it with template `org-log-note-headings'
  2396. This option can also be set with on a per-file-basis with
  2397. #+STARTUP: nologrepeat
  2398. #+STARTUP: logrepeat
  2399. #+STARTUP: lognoterepeat
  2400. You can have local logging settings for a subtree by setting the LOGGING
  2401. property to one or more of these keywords."
  2402. :group 'org-todo
  2403. :group 'org-progress
  2404. :type '(choice
  2405. (const :tag "Don't force a record" nil)
  2406. (const :tag "Force recording the DONE state" time)
  2407. (const :tag "Force recording a note with the DONE state" note)))
  2408. (defgroup org-priorities nil
  2409. "Priorities in Org-mode."
  2410. :tag "Org Priorities"
  2411. :group 'org-todo)
  2412. (defcustom org-enable-priority-commands t
  2413. "Non-nil means priority commands are active.
  2414. When nil, these commands will be disabled, so that you never accidentally
  2415. set a priority."
  2416. :group 'org-priorities
  2417. :type 'boolean)
  2418. (defcustom org-highest-priority ?A
  2419. "The highest priority of TODO items. A character like ?A, ?B etc.
  2420. Must have a smaller ASCII number than `org-lowest-priority'."
  2421. :group 'org-priorities
  2422. :type 'character)
  2423. (defcustom org-lowest-priority ?C
  2424. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2425. Must have a larger ASCII number than `org-highest-priority'."
  2426. :group 'org-priorities
  2427. :type 'character)
  2428. (defcustom org-default-priority ?B
  2429. "The default priority of TODO items.
  2430. This is the priority an item gets if no explicit priority is given.
  2431. When starting to cycle on an empty priority the first step in the cycle
  2432. depends on `org-priority-start-cycle-with-default'. The resulting first
  2433. step priority must not exceed the range from `org-highest-priority' to
  2434. `org-lowest-priority' which means that `org-default-priority' has to be
  2435. in this range exclusive or inclusive the range boundaries. Else the
  2436. first step refuses to set the default and the second will fall back
  2437. to (depending on the command used) the highest or lowest priority."
  2438. :group 'org-priorities
  2439. :type 'character)
  2440. (defcustom org-priority-start-cycle-with-default t
  2441. "Non-nil means start with default priority when starting to cycle.
  2442. When this is nil, the first step in the cycle will be (depending on the
  2443. command used) one higher or lower than the default priority.
  2444. See also `org-default-priority'."
  2445. :group 'org-priorities
  2446. :type 'boolean)
  2447. (defcustom org-get-priority-function nil
  2448. "Function to extract the priority from a string.
  2449. The string is normally the headline. If this is nil Org computes the
  2450. priority from the priority cookie like [#A] in the headline. It returns
  2451. an integer, increasing by 1000 for each priority level.
  2452. The user can set a different function here, which should take a string
  2453. as an argument and return the numeric priority."
  2454. :group 'org-priorities
  2455. :version "24.1"
  2456. :type 'function)
  2457. (defgroup org-time nil
  2458. "Options concerning time stamps and deadlines in Org-mode."
  2459. :tag "Org Time"
  2460. :group 'org)
  2461. (defcustom org-insert-labeled-timestamps-at-point nil
  2462. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2463. When nil, these labeled time stamps are forces into the second line of an
  2464. entry, just after the headline. When scheduling from the global TODO list,
  2465. the time stamp will always be forced into the second line."
  2466. :group 'org-time
  2467. :type 'boolean)
  2468. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2469. "Formats for `format-time-string' which are used for time stamps.
  2470. It is not recommended to change this constant.")
  2471. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2472. "Number of minutes to round time stamps to.
  2473. These are two values, the first applies when first creating a time stamp.
  2474. The second applies when changing it with the commands `S-up' and `S-down'.
  2475. When changing the time stamp, this means that it will change in steps
  2476. of N minutes, as given by the second value.
  2477. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2478. numbers should be factors of 60, so for example 5, 10, 15.
  2479. When this is larger than 1, you can still force an exact time stamp by using
  2480. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2481. and by using a prefix arg to `S-up/down' to specify the exact number
  2482. of minutes to shift."
  2483. :group 'org-time
  2484. :get #'(lambda (var) ; Make sure both elements are there
  2485. (if (integerp (default-value var))
  2486. (list (default-value var) 5)
  2487. (default-value var)))
  2488. :type '(list
  2489. (integer :tag "when inserting times")
  2490. (integer :tag "when modifying times")))
  2491. ;; Normalize old customizations of this variable.
  2492. (when (integerp org-time-stamp-rounding-minutes)
  2493. (setq org-time-stamp-rounding-minutes
  2494. (list org-time-stamp-rounding-minutes
  2495. org-time-stamp-rounding-minutes)))
  2496. (defcustom org-display-custom-times nil
  2497. "Non-nil means overlay custom formats over all time stamps.
  2498. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2499. To turn this on on a per-file basis, insert anywhere in the file:
  2500. #+STARTUP: customtime"
  2501. :group 'org-time
  2502. :set 'set-default
  2503. :type 'sexp)
  2504. (make-variable-buffer-local 'org-display-custom-times)
  2505. (defcustom org-time-stamp-custom-formats
  2506. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2507. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2508. These are overlaid over the default ISO format if the variable
  2509. `org-display-custom-times' is set. Time like %H:%M should be at the
  2510. end of the second format. The custom formats are also honored by export
  2511. commands, if custom time display is turned on at the time of export."
  2512. :group 'org-time
  2513. :type 'sexp)
  2514. (defun org-time-stamp-format (&optional long inactive)
  2515. "Get the right format for a time string."
  2516. (let ((f (if long (cdr org-time-stamp-formats)
  2517. (car org-time-stamp-formats))))
  2518. (if inactive
  2519. (concat "[" (substring f 1 -1) "]")
  2520. f)))
  2521. (defcustom org-time-clocksum-format
  2522. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2523. "The format string used when creating CLOCKSUM lines.
  2524. This is also used when Org mode generates a time duration.
  2525. The value can be a single format string containing two
  2526. %-sequences, which will be filled with the number of hours and
  2527. minutes in that order.
  2528. Alternatively, the value can be a plist associating any of the
  2529. keys :years, :months, :weeks, :days, :hours or :minutes with
  2530. format strings. The time duration is formatted using only the
  2531. time components that are needed and concatenating the results.
  2532. If a time unit in absent, it falls back to the next smallest
  2533. unit.
  2534. The keys :require-years, :require-months, :require-days,
  2535. :require-weeks, :require-hours, :require-minutes are also
  2536. meaningful. A non-nil value for these keys indicates that the
  2537. corresponding time component should always be included, even if
  2538. its value is 0.
  2539. For example,
  2540. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2541. :require-minutes t)
  2542. means durations longer than a day will be expressed in days,
  2543. hours and minutes, and durations less than a day will always be
  2544. expressed in hours and minutes (even for durations less than an
  2545. hour).
  2546. The value
  2547. \(:days \"%dd\" :minutes \"%dm\")
  2548. means durations longer than a day will be expressed in days and
  2549. minutes, and durations less than a day will be expressed entirely
  2550. in minutes (even for durations longer than an hour)."
  2551. :group 'org-time
  2552. :group 'org-clock
  2553. :version "24.4"
  2554. :package-version '(Org . "8.0")
  2555. :type '(choice (string :tag "Format string")
  2556. (set :tag "Plist"
  2557. (group :inline t (const :tag "Years" :years)
  2558. (string :tag "Format string"))
  2559. (group :inline t
  2560. (const :tag "Always show years" :require-years)
  2561. (const t))
  2562. (group :inline t (const :tag "Months" :months)
  2563. (string :tag "Format string"))
  2564. (group :inline t
  2565. (const :tag "Always show months" :require-months)
  2566. (const t))
  2567. (group :inline t (const :tag "Weeks" :weeks)
  2568. (string :tag "Format string"))
  2569. (group :inline t
  2570. (const :tag "Always show weeks" :require-weeks)
  2571. (const t))
  2572. (group :inline t (const :tag "Days" :days)
  2573. (string :tag "Format string"))
  2574. (group :inline t
  2575. (const :tag "Always show days" :require-days)
  2576. (const t))
  2577. (group :inline t (const :tag "Hours" :hours)
  2578. (string :tag "Format string"))
  2579. (group :inline t
  2580. (const :tag "Always show hours" :require-hours)
  2581. (const t))
  2582. (group :inline t (const :tag "Minutes" :minutes)
  2583. (string :tag "Format string"))
  2584. (group :inline t
  2585. (const :tag "Always show minutes" :require-minutes)
  2586. (const t)))))
  2587. (defcustom org-time-clocksum-use-fractional nil
  2588. "When non-nil, \\[org-clock-display] uses fractional times.
  2589. See `org-time-clocksum-format' for more on time clock formats."
  2590. :group 'org-time
  2591. :group 'org-clock
  2592. :version "24.3"
  2593. :type 'boolean)
  2594. (defcustom org-time-clocksum-use-effort-durations nil
  2595. "When non-nil, \\[org-clock-display] uses effort durations.
  2596. E.g. by default, one day is considered to be a 8 hours effort,
  2597. so a task that has been clocked for 16 hours will be displayed
  2598. as during 2 days in the clock display or in the clocktable.
  2599. See `org-effort-durations' on how to set effort durations
  2600. and `org-time-clocksum-format' for more on time clock formats."
  2601. :group 'org-time
  2602. :group 'org-clock
  2603. :version "24.4"
  2604. :package-version '(Org . "8.0")
  2605. :type 'boolean)
  2606. (defcustom org-time-clocksum-fractional-format "%.2f"
  2607. "The format string used when creating CLOCKSUM lines,
  2608. or when Org mode generates a time duration, if
  2609. `org-time-clocksum-use-fractional' is enabled.
  2610. The value can be a single format string containing one
  2611. %-sequence, which will be filled with the number of hours as
  2612. a float.
  2613. Alternatively, the value can be a plist associating any of the
  2614. keys :years, :months, :weeks, :days, :hours or :minutes with
  2615. a format string. The time duration is formatted using the
  2616. largest time unit which gives a non-zero integer part. If all
  2617. specified formats have zero integer part, the smallest time unit
  2618. is used."
  2619. :group 'org-time
  2620. :type '(choice (string :tag "Format string")
  2621. (set (group :inline t (const :tag "Years" :years)
  2622. (string :tag "Format string"))
  2623. (group :inline t (const :tag "Months" :months)
  2624. (string :tag "Format string"))
  2625. (group :inline t (const :tag "Weeks" :weeks)
  2626. (string :tag "Format string"))
  2627. (group :inline t (const :tag "Days" :days)
  2628. (string :tag "Format string"))
  2629. (group :inline t (const :tag "Hours" :hours)
  2630. (string :tag "Format string"))
  2631. (group :inline t (const :tag "Minutes" :minutes)
  2632. (string :tag "Format string")))))
  2633. (defcustom org-deadline-warning-days 14
  2634. "Number of days before expiration during which a deadline becomes active.
  2635. This variable governs the display in sparse trees and in the agenda.
  2636. When 0 or negative, it means use this number (the absolute value of it)
  2637. even if a deadline has a different individual lead time specified.
  2638. Custom commands can set this variable in the options section."
  2639. :group 'org-time
  2640. :group 'org-agenda-daily/weekly
  2641. :type 'integer)
  2642. (defcustom org-scheduled-delay-days 0
  2643. "Number of days before a scheduled item becomes active.
  2644. This variable governs the display in sparse trees and in the agenda.
  2645. The default value (i.e. 0) means: don't delay scheduled item.
  2646. When negative, it means use this number (the absolute value of it)
  2647. even if a scheduled item has a different individual delay time
  2648. specified.
  2649. Custom commands can set this variable in the options section."
  2650. :group 'org-time
  2651. :group 'org-agenda-daily/weekly
  2652. :version "24.4"
  2653. :package-version '(Org . "8.0")
  2654. :type 'integer)
  2655. (defcustom org-read-date-prefer-future t
  2656. "Non-nil means assume future for incomplete date input from user.
  2657. This affects the following situations:
  2658. 1. The user gives a month but not a year.
  2659. For example, if it is April and you enter \"feb 2\", this will be read
  2660. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2661. 2. The user gives a day, but no month.
  2662. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2663. read this as the third of *next* month. However, if you enter \"17\",
  2664. it will be considered as *this* month.
  2665. If you set this variable to the symbol `time', then also the following
  2666. will work:
  2667. 3. If the user gives a time.
  2668. If the time is before now, it will be interpreted as tomorrow.
  2669. Currently none of this works for ISO week specifications.
  2670. When this option is nil, the current day, month and year will always be
  2671. used as defaults.
  2672. See also `org-agenda-jump-prefer-future'."
  2673. :group 'org-time
  2674. :type '(choice
  2675. (const :tag "Never" nil)
  2676. (const :tag "Check month and day" t)
  2677. (const :tag "Check month, day, and time" time)))
  2678. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2679. "Should the agenda jump command prefer the future for incomplete dates?
  2680. The default is to do the same as configured in `org-read-date-prefer-future'.
  2681. But you can also set a deviating value here.
  2682. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2683. :group 'org-agenda
  2684. :group 'org-time
  2685. :version "24.1"
  2686. :type '(choice
  2687. (const :tag "Use org-read-date-prefer-future"
  2688. org-read-date-prefer-future)
  2689. (const :tag "Never" nil)
  2690. (const :tag "Always" t)))
  2691. (defcustom org-read-date-force-compatible-dates t
  2692. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2693. Depending on the system Emacs is running on, certain dates cannot
  2694. be represented with the type used internally to represent time.
  2695. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2696. correctly. Some systems allow for earlier dates, some for later,
  2697. some for both. One way to find out it to insert any date into an
  2698. Org buffer, putting the cursor on the year and hitting S-up and
  2699. S-down to test the range.
  2700. When this variable is set to t, the date/time prompt will not let
  2701. you specify dates outside the 1970-2037 range, so it is certain that
  2702. these dates will work in whatever version of Emacs you are
  2703. running, and also that you can move a file from one Emacs implementation
  2704. to another. WHenever Org is forcing the year for you, it will display
  2705. a message and beep.
  2706. When this variable is nil, Org will check if the date is
  2707. representable in the specific Emacs implementation you are using.
  2708. If not, it will force a year, usually the current year, and beep
  2709. to remind you. Currently this setting is not recommended because
  2710. the likelihood that you will open your Org files in an Emacs that
  2711. has limited date range is not negligible.
  2712. A workaround for this problem is to use diary sexp dates for time
  2713. stamps outside of this range."
  2714. :group 'org-time
  2715. :version "24.1"
  2716. :type 'boolean)
  2717. (defcustom org-read-date-display-live t
  2718. "Non-nil means display current interpretation of date prompt live.
  2719. This display will be in an overlay, in the minibuffer."
  2720. :group 'org-time
  2721. :type 'boolean)
  2722. (defcustom org-read-date-popup-calendar t
  2723. "Non-nil means pop up a calendar when prompting for a date.
  2724. In the calendar, the date can be selected with mouse-1. However, the
  2725. minibuffer will also be active, and you can simply enter the date as well.
  2726. When nil, only the minibuffer will be available."
  2727. :group 'org-time
  2728. :type 'boolean)
  2729. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2730. 'org-read-date-popup-calendar)
  2731. (make-obsolete-variable
  2732. 'org-read-date-minibuffer-setup-hook
  2733. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2734. (defcustom org-read-date-minibuffer-setup-hook nil
  2735. "Hook to be used to set up keys for the date/time interface.
  2736. Add key definitions to `minibuffer-local-map', which will be a
  2737. temporary copy.
  2738. WARNING: This option is obsolete, you should use
  2739. `org-read-date-minibuffer-local-map' to set up keys."
  2740. :group 'org-time
  2741. :type 'hook)
  2742. (defcustom org-extend-today-until 0
  2743. "The hour when your day really ends. Must be an integer.
  2744. This has influence for the following applications:
  2745. - When switching the agenda to \"today\". It it is still earlier than
  2746. the time given here, the day recognized as TODAY is actually yesterday.
  2747. - When a date is read from the user and it is still before the time given
  2748. here, the current date and time will be assumed to be yesterday, 23:59.
  2749. Also, timestamps inserted in capture templates follow this rule.
  2750. IMPORTANT: This is a feature whose implementation is and likely will
  2751. remain incomplete. Really, it is only here because past midnight seems to
  2752. be the favorite working time of John Wiegley :-)"
  2753. :group 'org-time
  2754. :type 'integer)
  2755. (defcustom org-use-effective-time nil
  2756. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2757. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2758. \"effective time\" of any timestamps between midnight and 8am will be
  2759. 23:59 of the previous day."
  2760. :group 'org-time
  2761. :version "24.1"
  2762. :type 'boolean)
  2763. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2764. "When non-nil, use the last clock out time for `org-todo'.
  2765. Note that this option has precedence over the combined use of
  2766. `org-use-effective-time' and `org-extend-today-until'."
  2767. :group 'org-time
  2768. :version "24.4"
  2769. :package-version '(Org . "8.0")
  2770. :type 'boolean)
  2771. (defcustom org-edit-timestamp-down-means-later nil
  2772. "Non-nil means S-down will increase the time in a time stamp.
  2773. When nil, S-up will increase."
  2774. :group 'org-time
  2775. :type 'boolean)
  2776. (defcustom org-calendar-follow-timestamp-change t
  2777. "Non-nil means make the calendar window follow timestamp changes.
  2778. When a timestamp is modified and the calendar window is visible, it will be
  2779. moved to the new date."
  2780. :group 'org-time
  2781. :type 'boolean)
  2782. (defgroup org-tags nil
  2783. "Options concerning tags in Org-mode."
  2784. :tag "Org Tags"
  2785. :group 'org)
  2786. (defcustom org-tag-alist nil
  2787. "List of tags allowed in Org-mode files.
  2788. When this list is nil, Org-mode will base TAG input on what is already in the
  2789. buffer.
  2790. The value of this variable is an alist, the car of each entry must be a
  2791. keyword as a string, the cdr may be a character that is used to select
  2792. that tag through the fast-tag-selection interface.
  2793. See the manual for details."
  2794. :group 'org-tags
  2795. :type '(repeat
  2796. (choice
  2797. (cons (string :tag "Tag name")
  2798. (character :tag "Access char"))
  2799. (list :tag "Start radio group"
  2800. (const :startgroup)
  2801. (option (string :tag "Group description")))
  2802. (list :tag "Group tags delimiter"
  2803. (const :grouptags))
  2804. (list :tag "End radio group"
  2805. (const :endgroup)
  2806. (option (string :tag "Group description")))
  2807. (const :tag "New line" (:newline)))))
  2808. (defcustom org-tag-persistent-alist nil
  2809. "List of tags that will always appear in all Org-mode files.
  2810. This is in addition to any in buffer settings or customizations
  2811. of `org-tag-alist'.
  2812. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2813. The value of this variable is an alist, the car of each entry must be a
  2814. keyword as a string, the cdr may be a character that is used to select
  2815. that tag through the fast-tag-selection interface.
  2816. See the manual for details.
  2817. To disable these tags on a per-file basis, insert anywhere in the file:
  2818. #+STARTUP: noptag"
  2819. :group 'org-tags
  2820. :type '(repeat
  2821. (choice
  2822. (cons (string :tag "Tag name")
  2823. (character :tag "Access char"))
  2824. (const :tag "Start radio group" (:startgroup))
  2825. (const :tag "Group tags delimiter" (:grouptags))
  2826. (const :tag "End radio group" (:endgroup))
  2827. (const :tag "New line" (:newline)))))
  2828. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2829. "If non-nil, always offer completion for all tags of all agenda files.
  2830. Instead of customizing this variable directly, you might want to
  2831. set it locally for capture buffers, because there no list of
  2832. tags in that file can be created dynamically (there are none).
  2833. (add-hook 'org-capture-mode-hook
  2834. (lambda ()
  2835. (set (make-local-variable
  2836. 'org-complete-tags-always-offer-all-agenda-tags)
  2837. t)))"
  2838. :group 'org-tags
  2839. :version "24.1"
  2840. :type 'boolean)
  2841. (defvar org-file-tags nil
  2842. "List of tags that can be inherited by all entries in the file.
  2843. The tags will be inherited if the variable `org-use-tag-inheritance'
  2844. says they should be.
  2845. This variable is populated from #+FILETAGS lines.")
  2846. (defcustom org-use-fast-tag-selection 'auto
  2847. "Non-nil means use fast tag selection scheme.
  2848. This is a special interface to select and deselect tags with single keys.
  2849. When nil, fast selection is never used.
  2850. When the symbol `auto', fast selection is used if and only if selection
  2851. characters for tags have been configured, either through the variable
  2852. `org-tag-alist' or through a #+TAGS line in the buffer.
  2853. When t, fast selection is always used and selection keys are assigned
  2854. automatically if necessary."
  2855. :group 'org-tags
  2856. :type '(choice
  2857. (const :tag "Always" t)
  2858. (const :tag "Never" nil)
  2859. (const :tag "When selection characters are configured" 'auto)))
  2860. (defcustom org-fast-tag-selection-single-key nil
  2861. "Non-nil means fast tag selection exits after first change.
  2862. When nil, you have to press RET to exit it.
  2863. During fast tag selection, you can toggle this flag with `C-c'.
  2864. This variable can also have the value `expert'. In this case, the window
  2865. displaying the tags menu is not even shown, until you press C-c again."
  2866. :group 'org-tags
  2867. :type '(choice
  2868. (const :tag "No" nil)
  2869. (const :tag "Yes" t)
  2870. (const :tag "Expert" expert)))
  2871. (defvar org-fast-tag-selection-include-todo nil
  2872. "Non-nil means fast tags selection interface will also offer TODO states.
  2873. This is an undocumented feature, you should not rely on it.")
  2874. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2875. "The column to which tags should be indented in a headline.
  2876. If this number is positive, it specifies the column. If it is negative,
  2877. it means that the tags should be flushright to that column. For example,
  2878. -80 works well for a normal 80 character screen.
  2879. When 0, place tags directly after headline text, with only one space in
  2880. between."
  2881. :group 'org-tags
  2882. :type 'integer)
  2883. (defcustom org-auto-align-tags t
  2884. "Non-nil keeps tags aligned when modifying headlines.
  2885. Some operations (i.e. demoting) change the length of a headline and
  2886. therefore shift the tags around. With this option turned on, after
  2887. each such operation the tags are again aligned to `org-tags-column'."
  2888. :group 'org-tags
  2889. :type 'boolean)
  2890. (defcustom org-use-tag-inheritance t
  2891. "Non-nil means tags in levels apply also for sublevels.
  2892. When nil, only the tags directly given in a specific line apply there.
  2893. This may also be a list of tags that should be inherited, or a regexp that
  2894. matches tags that should be inherited. Additional control is possible
  2895. with the variable `org-tags-exclude-from-inheritance' which gives an
  2896. explicit list of tags to be excluded from inheritance, even if the value of
  2897. `org-use-tag-inheritance' would select it for inheritance.
  2898. If this option is t, a match early-on in a tree can lead to a large
  2899. number of matches in the subtree when constructing the agenda or creating
  2900. a sparse tree. If you only want to see the first match in a tree during
  2901. a search, check out the variable `org-tags-match-list-sublevels'."
  2902. :group 'org-tags
  2903. :type '(choice
  2904. (const :tag "Not" nil)
  2905. (const :tag "Always" t)
  2906. (repeat :tag "Specific tags" (string :tag "Tag"))
  2907. (regexp :tag "Tags matched by regexp")))
  2908. (defcustom org-tags-exclude-from-inheritance nil
  2909. "List of tags that should never be inherited.
  2910. This is a way to exclude a few tags from inheritance. For way to do
  2911. the opposite, to actively allow inheritance for selected tags,
  2912. see the variable `org-use-tag-inheritance'."
  2913. :group 'org-tags
  2914. :type '(repeat (string :tag "Tag")))
  2915. (defun org-tag-inherit-p (tag)
  2916. "Check if TAG is one that should be inherited."
  2917. (cond
  2918. ((member tag org-tags-exclude-from-inheritance) nil)
  2919. ((eq org-use-tag-inheritance t) t)
  2920. ((not org-use-tag-inheritance) nil)
  2921. ((stringp org-use-tag-inheritance)
  2922. (string-match org-use-tag-inheritance tag))
  2923. ((listp org-use-tag-inheritance)
  2924. (member tag org-use-tag-inheritance))
  2925. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2926. (defcustom org-tags-match-list-sublevels t
  2927. "Non-nil means list also sublevels of headlines matching a search.
  2928. This variable applies to tags/property searches, and also to stuck
  2929. projects because this search is based on a tags match as well.
  2930. When set to the symbol `indented', sublevels are indented with
  2931. leading dots.
  2932. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2933. the sublevels of a headline matching a tag search often also match
  2934. the same search. Listing all of them can create very long lists.
  2935. Setting this variable to nil causes subtrees of a match to be skipped.
  2936. This variable is semi-obsolete and probably should always be true. It
  2937. is better to limit inheritance to certain tags using the variables
  2938. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2939. :group 'org-tags
  2940. :type '(choice
  2941. (const :tag "No, don't list them" nil)
  2942. (const :tag "Yes, do list them" t)
  2943. (const :tag "List them, indented with leading dots" indented)))
  2944. (defcustom org-tags-sort-function nil
  2945. "When set, tags are sorted using this function as a comparator."
  2946. :group 'org-tags
  2947. :type '(choice
  2948. (const :tag "No sorting" nil)
  2949. (const :tag "Alphabetical" string<)
  2950. (const :tag "Reverse alphabetical" string>)
  2951. (function :tag "Custom function" nil)))
  2952. (defvar org-tags-history nil
  2953. "History of minibuffer reads for tags.")
  2954. (defvar org-last-tags-completion-table nil
  2955. "The last used completion table for tags.")
  2956. (defvar org-after-tags-change-hook nil
  2957. "Hook that is run after the tags in a line have changed.")
  2958. (defgroup org-properties nil
  2959. "Options concerning properties in Org-mode."
  2960. :tag "Org Properties"
  2961. :group 'org)
  2962. (defcustom org-property-format "%-10s %s"
  2963. "How property key/value pairs should be formatted by `indent-line'.
  2964. When `indent-line' hits a property definition, it will format the line
  2965. according to this format, mainly to make sure that the values are
  2966. lined-up with respect to each other."
  2967. :group 'org-properties
  2968. :type 'string)
  2969. (defcustom org-properties-postprocess-alist nil
  2970. "Alist of properties and functions to adjust inserted values.
  2971. Elements of this alist must be of the form
  2972. ([string] [function])
  2973. where [string] must be a property name and [function] must be a
  2974. lambda expression: this lambda expression must take one argument,
  2975. the value to adjust, and return the new value as a string.
  2976. For example, this element will allow the property \"Remaining\"
  2977. to be updated wrt the relation between the \"Effort\" property
  2978. and the clock summary:
  2979. ((\"Remaining\" (lambda(value)
  2980. (let ((clocksum (org-clock-sum-current-item))
  2981. (effort (org-duration-string-to-minutes
  2982. (org-entry-get (point) \"Effort\"))))
  2983. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2984. :group 'org-properties
  2985. :version "24.1"
  2986. :type '(alist :key-type (string :tag "Property")
  2987. :value-type (function :tag "Function")))
  2988. (defcustom org-use-property-inheritance nil
  2989. "Non-nil means properties apply also for sublevels.
  2990. This setting is chiefly used during property searches. Turning it on can
  2991. cause significant overhead when doing a search, which is why it is not
  2992. on by default.
  2993. When nil, only the properties directly given in the current entry count.
  2994. When t, every property is inherited. The value may also be a list of
  2995. properties that should have inheritance, or a regular expression matching
  2996. properties that should be inherited.
  2997. However, note that some special properties use inheritance under special
  2998. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2999. and the properties ending in \"_ALL\" when they are used as descriptor
  3000. for valid values of a property.
  3001. Note for programmers:
  3002. When querying an entry with `org-entry-get', you can control if inheritance
  3003. should be used. By default, `org-entry-get' looks only at the local
  3004. properties. You can request inheritance by setting the inherit argument
  3005. to t (to force inheritance) or to `selective' (to respect the setting
  3006. in this variable)."
  3007. :group 'org-properties
  3008. :type '(choice
  3009. (const :tag "Not" nil)
  3010. (const :tag "Always" t)
  3011. (repeat :tag "Specific properties" (string :tag "Property"))
  3012. (regexp :tag "Properties matched by regexp")))
  3013. (defun org-property-inherit-p (property)
  3014. "Check if PROPERTY is one that should be inherited."
  3015. (cond
  3016. ((eq org-use-property-inheritance t) t)
  3017. ((not org-use-property-inheritance) nil)
  3018. ((stringp org-use-property-inheritance)
  3019. (string-match org-use-property-inheritance property))
  3020. ((listp org-use-property-inheritance)
  3021. (member property org-use-property-inheritance))
  3022. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3023. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3024. "The default column format, if no other format has been defined.
  3025. This variable can be set on the per-file basis by inserting a line
  3026. #+COLUMNS: %25ITEM ....."
  3027. :group 'org-properties
  3028. :type 'string)
  3029. (defcustom org-columns-ellipses ".."
  3030. "The ellipses to be used when a field in column view is truncated.
  3031. When this is the empty string, as many characters as possible are shown,
  3032. but then there will be no visual indication that the field has been truncated.
  3033. When this is a string of length N, the last N characters of a truncated
  3034. field are replaced by this string. If the column is narrower than the
  3035. ellipses string, only part of the ellipses string will be shown."
  3036. :group 'org-properties
  3037. :type 'string)
  3038. (defcustom org-columns-modify-value-for-display-function nil
  3039. "Function that modifies values for display in column view.
  3040. For example, it can be used to cut out a certain part from a time stamp.
  3041. The function must take 2 arguments:
  3042. column-title The title of the column (*not* the property name)
  3043. value The value that should be modified.
  3044. The function should return the value that should be displayed,
  3045. or nil if the normal value should be used."
  3046. :group 'org-properties
  3047. :type 'function)
  3048. (defcustom org-effort-property "Effort"
  3049. "The property that is being used to keep track of effort estimates.
  3050. Effort estimates given in this property need to have the format H:MM."
  3051. :group 'org-properties
  3052. :group 'org-progress
  3053. :type '(string :tag "Property"))
  3054. (defconst org-global-properties-fixed
  3055. '(("VISIBILITY_ALL" . "folded children content all")
  3056. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3057. "List of property/value pairs that can be inherited by any entry.
  3058. These are fixed values, for the preset properties. The user variable
  3059. that can be used to add to this list is `org-global-properties'.
  3060. The entries in this list are cons cells where the car is a property
  3061. name and cdr is a string with the value. If the value represents
  3062. multiple items like an \"_ALL\" property, separate the items by
  3063. spaces.")
  3064. (defcustom org-global-properties nil
  3065. "List of property/value pairs that can be inherited by any entry.
  3066. This list will be combined with the constant `org-global-properties-fixed'.
  3067. The entries in this list are cons cells where the car is a property
  3068. name and cdr is a string with the value.
  3069. You can set buffer-local values for the same purpose in the variable
  3070. `org-file-properties' this by adding lines like
  3071. #+PROPERTY: NAME VALUE"
  3072. :group 'org-properties
  3073. :type '(repeat
  3074. (cons (string :tag "Property")
  3075. (string :tag "Value"))))
  3076. (defvar org-file-properties nil
  3077. "List of property/value pairs that can be inherited by any entry.
  3078. Valid for the current buffer.
  3079. This variable is populated from #+PROPERTY lines.")
  3080. (make-variable-buffer-local 'org-file-properties)
  3081. (defgroup org-agenda nil
  3082. "Options concerning agenda views in Org-mode."
  3083. :tag "Org Agenda"
  3084. :group 'org)
  3085. (defvar org-category nil
  3086. "Variable used by org files to set a category for agenda display.
  3087. Such files should use a file variable to set it, for example
  3088. # -*- mode: org; org-category: \"ELisp\"
  3089. or contain a special line
  3090. #+CATEGORY: ELisp
  3091. If the file does not specify a category, then file's base name
  3092. is used instead.")
  3093. (make-variable-buffer-local 'org-category)
  3094. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3095. (defcustom org-agenda-files nil
  3096. "The files to be used for agenda display.
  3097. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3098. \\[org-remove-file]. You can also use customize to edit the list.
  3099. If an entry is a directory, all files in that directory that are matched by
  3100. `org-agenda-file-regexp' will be part of the file list.
  3101. If the value of the variable is not a list but a single file name, then
  3102. the list of agenda files is actually stored and maintained in that file, one
  3103. agenda file per line. In this file paths can be given relative to
  3104. `org-directory'. Tilde expansion and environment variable substitution
  3105. are also made."
  3106. :group 'org-agenda
  3107. :type '(choice
  3108. (repeat :tag "List of files and directories" file)
  3109. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3110. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3111. "Regular expression to match files for `org-agenda-files'.
  3112. If any element in the list in that variable contains a directory instead
  3113. of a normal file, all files in that directory that are matched by this
  3114. regular expression will be included."
  3115. :group 'org-agenda
  3116. :type 'regexp)
  3117. (defcustom org-agenda-text-search-extra-files nil
  3118. "List of extra files to be searched by text search commands.
  3119. These files will be search in addition to the agenda files by the
  3120. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3121. Note that these files will only be searched for text search commands,
  3122. not for the other agenda views like todo lists, tag searches or the weekly
  3123. agenda. This variable is intended to list notes and possibly archive files
  3124. that should also be searched by these two commands.
  3125. In fact, if the first element in the list is the symbol `agenda-archives',
  3126. than all archive files of all agenda files will be added to the search
  3127. scope."
  3128. :group 'org-agenda
  3129. :type '(set :greedy t
  3130. (const :tag "Agenda Archives" agenda-archives)
  3131. (repeat :inline t (file))))
  3132. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3133. 'org-agenda-text-search-extra-files)
  3134. (defcustom org-agenda-skip-unavailable-files nil
  3135. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3136. A nil value means to remove them, after a query, from the list."
  3137. :group 'org-agenda
  3138. :type 'boolean)
  3139. (defcustom org-calendar-to-agenda-key [?c]
  3140. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3141. The command `org-calendar-goto-agenda' will be bound to this key. The
  3142. default is the character `c' because then `c' can be used to switch back and
  3143. forth between agenda and calendar."
  3144. :group 'org-agenda
  3145. :type 'sexp)
  3146. (defcustom org-calendar-insert-diary-entry-key [?i]
  3147. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3148. This option is irrelevant until `org-agenda-diary-file' has been configured
  3149. to point to an Org-mode file. When that is the case, the command
  3150. `org-agenda-diary-entry' will be bound to the key given here, by default
  3151. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3152. if you want to continue doing this, you need to change this to a different
  3153. key."
  3154. :group 'org-agenda
  3155. :type 'sexp)
  3156. (defcustom org-agenda-diary-file 'diary-file
  3157. "File to which to add new entries with the `i' key in agenda and calendar.
  3158. When this is the symbol `diary-file', the functionality in the Emacs
  3159. calendar will be used to add entries to the `diary-file'. But when this
  3160. points to a file, `org-agenda-diary-entry' will be used instead."
  3161. :group 'org-agenda
  3162. :type '(choice
  3163. (const :tag "The standard Emacs diary file" diary-file)
  3164. (file :tag "Special Org file diary entries")))
  3165. (eval-after-load "calendar"
  3166. '(progn
  3167. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3168. 'org-calendar-goto-agenda)
  3169. (add-hook 'calendar-mode-hook
  3170. (lambda ()
  3171. (unless (eq org-agenda-diary-file 'diary-file)
  3172. (define-key calendar-mode-map
  3173. org-calendar-insert-diary-entry-key
  3174. 'org-agenda-diary-entry))))))
  3175. (defgroup org-latex nil
  3176. "Options for embedding LaTeX code into Org-mode."
  3177. :tag "Org LaTeX"
  3178. :group 'org)
  3179. (defcustom org-format-latex-options
  3180. '(:foreground default :background default :scale 1.0
  3181. :html-foreground "Black" :html-background "Transparent"
  3182. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3183. "Options for creating images from LaTeX fragments.
  3184. This is a property list with the following properties:
  3185. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3186. `default' means use the foreground of the default face.
  3187. `auto' means use the foreground from the text face.
  3188. :background the background color, or \"Transparent\".
  3189. `default' means use the background of the default face.
  3190. `auto' means use the background from the text face.
  3191. :scale a scaling factor for the size of the images, to get more pixels
  3192. :html-foreground, :html-background, :html-scale
  3193. the same numbers for HTML export.
  3194. :matchers a list indicating which matchers should be used to
  3195. find LaTeX fragments. Valid members of this list are:
  3196. \"begin\" find environments
  3197. \"$1\" find single characters surrounded by $.$
  3198. \"$\" find math expressions surrounded by $...$
  3199. \"$$\" find math expressions surrounded by $$....$$
  3200. \"\\(\" find math expressions surrounded by \\(...\\)
  3201. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3202. :group 'org-latex
  3203. :type 'plist)
  3204. (defcustom org-format-latex-signal-error t
  3205. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3206. When nil, just push out a message."
  3207. :group 'org-latex
  3208. :version "24.1"
  3209. :type 'boolean)
  3210. (defcustom org-latex-to-mathml-jar-file nil
  3211. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3212. Use this to specify additional executable file say a jar file.
  3213. When using MathToWeb as the converter, specify the full-path to
  3214. your mathtoweb.jar file."
  3215. :group 'org-latex
  3216. :version "24.1"
  3217. :type '(choice
  3218. (const :tag "None" nil)
  3219. (file :tag "JAR file" :must-match t)))
  3220. (defcustom org-latex-to-mathml-convert-command nil
  3221. "Command to convert LaTeX fragments to MathML.
  3222. Replace format-specifiers in the command as noted below and use
  3223. `shell-command' to convert LaTeX to MathML.
  3224. %j: Executable file in fully expanded form as specified by
  3225. `org-latex-to-mathml-jar-file'.
  3226. %I: Input LaTeX file in fully expanded form
  3227. %o: Output MathML file
  3228. This command is used by `org-create-math-formula'.
  3229. When using MathToWeb as the converter, set this to
  3230. \"java -jar %j -unicode -force -df %o %I\"."
  3231. :group 'org-latex
  3232. :version "24.1"
  3233. :type '(choice
  3234. (const :tag "None" nil)
  3235. (string :tag "\nShell command")))
  3236. (defcustom org-latex-create-formula-image-program 'dvipng
  3237. "Program to convert LaTeX fragments with.
  3238. dvipng Process the LaTeX fragments to dvi file, then convert
  3239. dvi files to png files using dvipng.
  3240. This will also include processing of non-math environments.
  3241. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3242. to convert pdf files to png files"
  3243. :group 'org-latex
  3244. :version "24.1"
  3245. :type '(choice
  3246. (const :tag "dvipng" dvipng)
  3247. (const :tag "imagemagick" imagemagick)))
  3248. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3249. "Path to store latex preview images.
  3250. A relative path here creates many directories relative to the
  3251. processed org files paths. An absolute path puts all preview
  3252. images at the same place."
  3253. :group 'org-latex
  3254. :version "24.3"
  3255. :type 'string)
  3256. (defun org-format-latex-mathml-available-p ()
  3257. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3258. (save-match-data
  3259. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3260. org-latex-to-mathml-convert-command)
  3261. (let ((executable (car (split-string
  3262. org-latex-to-mathml-convert-command))))
  3263. (when (executable-find executable)
  3264. (if (string-match
  3265. "%j" org-latex-to-mathml-convert-command)
  3266. (file-readable-p org-latex-to-mathml-jar-file)
  3267. t))))))
  3268. (defcustom org-format-latex-header "\\documentclass{article}
  3269. \\usepackage[usenames]{color}
  3270. \\usepackage{amsmath}
  3271. \\usepackage[mathscr]{eucal}
  3272. \\pagestyle{empty} % do not remove
  3273. \[PACKAGES]
  3274. \[DEFAULT-PACKAGES]
  3275. % The settings below are copied from fullpage.sty
  3276. \\setlength{\\textwidth}{\\paperwidth}
  3277. \\addtolength{\\textwidth}{-3cm}
  3278. \\setlength{\\oddsidemargin}{1.5cm}
  3279. \\addtolength{\\oddsidemargin}{-2.54cm}
  3280. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3281. \\setlength{\\textheight}{\\paperheight}
  3282. \\addtolength{\\textheight}{-\\headheight}
  3283. \\addtolength{\\textheight}{-\\headsep}
  3284. \\addtolength{\\textheight}{-\\footskip}
  3285. \\addtolength{\\textheight}{-3cm}
  3286. \\setlength{\\topmargin}{1.5cm}
  3287. \\addtolength{\\topmargin}{-2.54cm}"
  3288. "The document header used for processing LaTeX fragments.
  3289. It is imperative that this header make sure that no page number
  3290. appears on the page. The package defined in the variables
  3291. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3292. will either replace the placeholder \"[PACKAGES]\" in this
  3293. header, or they will be appended."
  3294. :group 'org-latex
  3295. :type 'string)
  3296. (defun org-set-packages-alist (var val)
  3297. "Set the packages alist and make sure it has 3 elements per entry."
  3298. (set var (mapcar (lambda (x)
  3299. (if (and (consp x) (= (length x) 2))
  3300. (list (car x) (nth 1 x) t)
  3301. x))
  3302. val)))
  3303. (defun org-get-packages-alist (var)
  3304. "Get the packages alist and make sure it has 3 elements per entry."
  3305. (mapcar (lambda (x)
  3306. (if (and (consp x) (= (length x) 2))
  3307. (list (car x) (nth 1 x) t)
  3308. x))
  3309. (default-value var)))
  3310. (defcustom org-latex-default-packages-alist
  3311. '(("AUTO" "inputenc" t)
  3312. ("T1" "fontenc" t)
  3313. ("" "fixltx2e" nil)
  3314. ("" "graphicx" t)
  3315. ("" "longtable" nil)
  3316. ("" "float" nil)
  3317. ("" "wrapfig" nil)
  3318. ("" "soul" t)
  3319. ("" "textcomp" t)
  3320. ("" "marvosym" t)
  3321. ("" "wasysym" t)
  3322. ("" "latexsym" t)
  3323. ("" "amssymb" t)
  3324. ("" "hyperref" nil)
  3325. "\\tolerance=1000")
  3326. "Alist of default packages to be inserted in the header.
  3327. Change this only if one of the packages here causes an
  3328. incompatibility with another package you are using.
  3329. The packages in this list are needed by one part or another of
  3330. Org mode to function properly:
  3331. - inputenc, fontenc: for basic font and character selection
  3332. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3333. symbols used for interpreting the entities in `org-entities'.
  3334. You can skip some of these packages if you don't use any of the
  3335. symbols in it.
  3336. - graphicx: for including images
  3337. - float, wrapfig: for figure placement
  3338. - longtable: for long tables
  3339. - hyperref: for cross references
  3340. Therefore you should not modify this variable unless you know
  3341. what you are doing. The one reason to change it anyway is that
  3342. you might be loading some other package that conflicts with one
  3343. of the default packages. Each cell is of the format
  3344. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3345. the package also needs to be included when compiling LaTeX
  3346. snippets into images for inclusion into non-LaTeX output."
  3347. :group 'org-latex
  3348. :group 'org-export-latex
  3349. :set 'org-set-packages-alist
  3350. :get 'org-get-packages-alist
  3351. :version "24.1"
  3352. :type '(repeat
  3353. (choice
  3354. (list :tag "options/package pair"
  3355. (string :tag "options")
  3356. (string :tag "package")
  3357. (boolean :tag "Snippet"))
  3358. (string :tag "A line of LaTeX"))))
  3359. (defcustom org-latex-packages-alist nil
  3360. "Alist of packages to be inserted in every LaTeX header.
  3361. These will be inserted after `org-latex-default-packages-alist'.
  3362. Each cell is of the format:
  3363. \(\"options\" \"package\" snippet-flag)
  3364. SNIPPET-FLAG, when t, indicates that this package is also needed
  3365. when turning LaTeX snippets into images for inclusion into
  3366. non-LaTeX output.
  3367. Make sure that you only list packages here which:
  3368. - you want in every file
  3369. - do not conflict with the setup in `org-format-latex-header'.
  3370. - do not conflict with the default packages in
  3371. `org-latex-default-packages-alist'."
  3372. :group 'org-latex
  3373. :group 'org-export-latex
  3374. :set 'org-set-packages-alist
  3375. :get 'org-get-packages-alist
  3376. :type '(repeat
  3377. (choice
  3378. (list :tag "options/package pair"
  3379. (string :tag "options")
  3380. (string :tag "package")
  3381. (boolean :tag "Snippet"))
  3382. (string :tag "A line of LaTeX"))))
  3383. (defgroup org-appearance nil
  3384. "Settings for Org-mode appearance."
  3385. :tag "Org Appearance"
  3386. :group 'org)
  3387. (defcustom org-level-color-stars-only nil
  3388. "Non-nil means fontify only the stars in each headline.
  3389. When nil, the entire headline is fontified.
  3390. Changing it requires restart of `font-lock-mode' to become effective
  3391. also in regions already fontified."
  3392. :group 'org-appearance
  3393. :type 'boolean)
  3394. (defcustom org-hide-leading-stars nil
  3395. "Non-nil means hide the first N-1 stars in a headline.
  3396. This works by using the face `org-hide' for these stars. This
  3397. face is white for a light background, and black for a dark
  3398. background. You may have to customize the face `org-hide' to
  3399. make this work.
  3400. Changing it requires restart of `font-lock-mode' to become effective
  3401. also in regions already fontified.
  3402. You may also set this on a per-file basis by adding one of the following
  3403. lines to the buffer:
  3404. #+STARTUP: hidestars
  3405. #+STARTUP: showstars"
  3406. :group 'org-appearance
  3407. :type 'boolean)
  3408. (defcustom org-hidden-keywords nil
  3409. "List of symbols corresponding to keywords to be hidden the org buffer.
  3410. For example, a value '(title) for this list will make the document's title
  3411. appear in the buffer without the initial #+TITLE: keyword."
  3412. :group 'org-appearance
  3413. :version "24.1"
  3414. :type '(set (const :tag "#+AUTHOR" author)
  3415. (const :tag "#+DATE" date)
  3416. (const :tag "#+EMAIL" email)
  3417. (const :tag "#+TITLE" title)))
  3418. (defcustom org-custom-properties nil
  3419. "List of properties (as strings) with a special meaning.
  3420. The default use of these custom properties is to let the user
  3421. hide them with `org-toggle-custom-properties-visibility'."
  3422. :group 'org-properties
  3423. :group 'org-appearance
  3424. :version "24.3"
  3425. :type '(repeat (string :tag "Property Name")))
  3426. (defcustom org-fontify-done-headline nil
  3427. "Non-nil means change the face of a headline if it is marked DONE.
  3428. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3429. When this is non-nil, the headline after the keyword is set to the
  3430. `org-headline-done' as an additional indication."
  3431. :group 'org-appearance
  3432. :type 'boolean)
  3433. (defcustom org-fontify-emphasized-text t
  3434. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3435. Changing this variable requires a restart of Emacs to take effect."
  3436. :group 'org-appearance
  3437. :type 'boolean)
  3438. (defcustom org-fontify-whole-heading-line nil
  3439. "Non-nil means fontify the whole line for headings.
  3440. This is useful when setting a background color for the
  3441. org-level-* faces."
  3442. :group 'org-appearance
  3443. :type 'boolean)
  3444. (defcustom org-highlight-latex-and-related nil
  3445. "Non-nil means highlight LaTeX related syntax in the buffer.
  3446. When non nil, the value should be a list containing any of the
  3447. following symbols:
  3448. `latex' Highlight LaTeX snippets and environments.
  3449. `script' Highlight subscript and superscript.
  3450. `entities' Highlight entities."
  3451. :group 'org-appearance
  3452. :version "24.4"
  3453. :package-version '(Org . "8.0")
  3454. :type '(choice
  3455. (const :tag "No highlighting" nil)
  3456. (set :greedy t :tag "Highlight"
  3457. (const :tag "LaTeX snippets and environments" latex)
  3458. (const :tag "Subscript and superscript" script)
  3459. (const :tag "Entities" entities))))
  3460. (defcustom org-hide-emphasis-markers nil
  3461. "Non-nil mean font-lock should hide the emphasis marker characters."
  3462. :group 'org-appearance
  3463. :type 'boolean)
  3464. (defcustom org-pretty-entities nil
  3465. "Non-nil means show entities as UTF8 characters.
  3466. When nil, the \\name form remains in the buffer."
  3467. :group 'org-appearance
  3468. :version "24.1"
  3469. :type 'boolean)
  3470. (defcustom org-pretty-entities-include-sub-superscripts t
  3471. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3472. :group 'org-appearance
  3473. :version "24.1"
  3474. :type 'boolean)
  3475. (defvar org-emph-re nil
  3476. "Regular expression for matching emphasis.
  3477. After a match, the match groups contain these elements:
  3478. 0 The match of the full regular expression, including the characters
  3479. before and after the proper match
  3480. 1 The character before the proper match, or empty at beginning of line
  3481. 2 The proper match, including the leading and trailing markers
  3482. 3 The leading marker like * or /, indicating the type of highlighting
  3483. 4 The text between the emphasis markers, not including the markers
  3484. 5 The character after the match, empty at the end of a line")
  3485. (defvar org-verbatim-re nil
  3486. "Regular expression for matching verbatim text.")
  3487. (defvar org-emphasis-regexp-components) ; defined just below
  3488. (defvar org-emphasis-alist) ; defined just below
  3489. (defun org-set-emph-re (var val)
  3490. "Set variable and compute the emphasis regular expression."
  3491. (set var val)
  3492. (when (and (boundp 'org-emphasis-alist)
  3493. (boundp 'org-emphasis-regexp-components)
  3494. org-emphasis-alist org-emphasis-regexp-components)
  3495. (let* ((e org-emphasis-regexp-components)
  3496. (pre (car e))
  3497. (post (nth 1 e))
  3498. (border (nth 2 e))
  3499. (body (nth 3 e))
  3500. (nl (nth 4 e))
  3501. (body1 (concat body "*?"))
  3502. (markers (mapconcat 'car org-emphasis-alist ""))
  3503. (vmarkers (mapconcat
  3504. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3505. org-emphasis-alist "")))
  3506. ;; make sure special characters appear at the right position in the class
  3507. (if (string-match "\\^" markers)
  3508. (setq markers (concat (replace-match "" t t markers) "^")))
  3509. (if (string-match "-" markers)
  3510. (setq markers (concat (replace-match "" t t markers) "-")))
  3511. (if (string-match "\\^" vmarkers)
  3512. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3513. (if (string-match "-" vmarkers)
  3514. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3515. (if (> nl 0)
  3516. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3517. (int-to-string nl) "\\}")))
  3518. ;; Make the regexp
  3519. (setq org-emph-re
  3520. (concat "\\([" pre "]\\|^\\)"
  3521. "\\("
  3522. "\\([" markers "]\\)"
  3523. "\\("
  3524. "[^" border "]\\|"
  3525. "[^" border "]"
  3526. body1
  3527. "[^" border "]"
  3528. "\\)"
  3529. "\\3\\)"
  3530. "\\([" post "]\\|$\\)"))
  3531. (setq org-verbatim-re
  3532. (concat "\\([" pre "]\\|^\\)"
  3533. "\\("
  3534. "\\([" vmarkers "]\\)"
  3535. "\\("
  3536. "[^" border "]\\|"
  3537. "[^" border "]"
  3538. body1
  3539. "[^" border "]"
  3540. "\\)"
  3541. "\\3\\)"
  3542. "\\([" post "]\\|$\\)")))))
  3543. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3544. ;; set this option proved cumbersome. See this message/thread:
  3545. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3546. (defvar org-emphasis-regexp-components
  3547. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3548. "Components used to build the regular expression for emphasis.
  3549. This is a list with five entries. Terminology: In an emphasis string
  3550. like \" *strong word* \", we call the initial space PREMATCH, the final
  3551. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3552. and \"trong wor\" is the body. The different components in this variable
  3553. specify what is allowed/forbidden in each part:
  3554. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3555. post Chars allowed as postmatch. End of line will be allowed too.
  3556. border The chars *forbidden* as border characters.
  3557. body-regexp A regexp like \".\" to match a body character. Don't use
  3558. non-shy groups here, and don't allow newline here.
  3559. newline The maximum number of newlines allowed in an emphasis exp.
  3560. You need to reload Org or to restart Emacs after customizing this.")
  3561. (defcustom org-emphasis-alist
  3562. `(("*" bold)
  3563. ("/" italic)
  3564. ("_" underline)
  3565. ("=" org-code verbatim)
  3566. ("~" org-verbatim verbatim)
  3567. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3568. "Alist of characters and faces to emphasize text.
  3569. Text starting and ending with a special character will be emphasized,
  3570. for example *bold*, _underlined_ and /italic/. This variable sets the
  3571. marker characters and the face to be used by font-lock for highlighting
  3572. in Org-mode Emacs buffers.
  3573. You need to reload Org or to restart Emacs after customizing this."
  3574. :group 'org-appearance
  3575. :set 'org-set-emph-re
  3576. :version "24.4"
  3577. :package-version '(Org . "8.0")
  3578. :type '(repeat
  3579. (list
  3580. (string :tag "Marker character")
  3581. (choice
  3582. (face :tag "Font-lock-face")
  3583. (plist :tag "Face property list"))
  3584. (option (const verbatim)))))
  3585. (defvar org-protecting-blocks
  3586. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3587. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3588. This is needed for font-lock setup.")
  3589. ;;; Miscellaneous options
  3590. (defgroup org-completion nil
  3591. "Completion in Org-mode."
  3592. :tag "Org Completion"
  3593. :group 'org)
  3594. (defcustom org-completion-use-ido nil
  3595. "Non-nil means use ido completion wherever possible.
  3596. Note that `ido-mode' must be active for this variable to be relevant.
  3597. If you decide to turn this variable on, you might well want to turn off
  3598. `org-outline-path-complete-in-steps'.
  3599. See also `org-completion-use-iswitchb'."
  3600. :group 'org-completion
  3601. :type 'boolean)
  3602. (defcustom org-completion-use-iswitchb nil
  3603. "Non-nil means use iswitchb completion wherever possible.
  3604. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3605. If you decide to turn this variable on, you might well want to turn off
  3606. `org-outline-path-complete-in-steps'.
  3607. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3608. :group 'org-completion
  3609. :type 'boolean)
  3610. (defcustom org-completion-fallback-command 'hippie-expand
  3611. "The expansion command called by \\[pcomplete] in normal context.
  3612. Normal means, no org-mode-specific context."
  3613. :group 'org-completion
  3614. :type 'function)
  3615. ;;; Functions and variables from their packages
  3616. ;; Declared here to avoid compiler warnings
  3617. ;; XEmacs only
  3618. (defvar outline-mode-menu-heading)
  3619. (defvar outline-mode-menu-show)
  3620. (defvar outline-mode-menu-hide)
  3621. (defvar zmacs-regions) ; XEmacs regions
  3622. ;; Emacs only
  3623. (defvar mark-active)
  3624. ;; Various packages
  3625. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3626. (declare-function calendar-forward-day "cal-move" (arg))
  3627. (declare-function calendar-goto-date "cal-move" (date))
  3628. (declare-function calendar-goto-today "cal-move" ())
  3629. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3630. (defvar calc-embedded-close-formula)
  3631. (defvar calc-embedded-open-formula)
  3632. (declare-function cdlatex-tab "ext:cdlatex" ())
  3633. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3634. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3635. (defvar font-lock-unfontify-region-function)
  3636. (declare-function iswitchb-read-buffer "iswitchb"
  3637. (prompt &optional default require-match start matches-set))
  3638. (defvar iswitchb-temp-buflist)
  3639. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3640. (defvar org-agenda-tags-todo-honor-ignore-options)
  3641. (declare-function org-agenda-skip "org-agenda" ())
  3642. (declare-function
  3643. org-agenda-format-item "org-agenda"
  3644. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3645. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3646. (declare-function org-agenda-change-all-lines "org-agenda"
  3647. (newhead hdmarker &optional fixface just-this))
  3648. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3649. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3650. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3651. (beg end))
  3652. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3653. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3654. "org-agenda" (&optional end))
  3655. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3656. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3657. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3658. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3659. (declare-function org-indent-mode "org-indent" (&optional arg))
  3660. (declare-function parse-time-string "parse-time" (string))
  3661. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3662. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3663. (defvar remember-data-file)
  3664. (defvar texmathp-why)
  3665. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3666. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3667. (defvar org-latex-regexps)
  3668. ;;; Autoload and prepare some org modules
  3669. ;; Some table stuff that needs to be defined here, because it is used
  3670. ;; by the functions setting up org-mode or checking for table context.
  3671. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3672. "Detect an org-type or table-type table.")
  3673. (defconst org-table-line-regexp "^[ \t]*|"
  3674. "Detect an org-type table line.")
  3675. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3676. "Detect an org-type table line.")
  3677. (defconst org-table-hline-regexp "^[ \t]*|-"
  3678. "Detect an org-type table hline.")
  3679. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3680. "Detect a table-type table hline.")
  3681. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3682. "Detect the first line outside a table when searching from within it.
  3683. This works for both table types.")
  3684. ;; Autoload the functions in org-table.el that are needed by functions here.
  3685. (eval-and-compile
  3686. (org-autoload "org-table"
  3687. '(org-table-begin org-table-blank-field org-table-end)))
  3688. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  3689. "Detect a #+TBLFM line.")
  3690. ;;;###autoload
  3691. (defun turn-on-orgtbl ()
  3692. "Unconditionally turn on `orgtbl-mode'."
  3693. (require 'org-table)
  3694. (orgtbl-mode 1))
  3695. (defun org-at-table-p (&optional table-type)
  3696. "Return t if the cursor is inside an org-type table.
  3697. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3698. (if org-enable-table-editor
  3699. (save-excursion
  3700. (beginning-of-line 1)
  3701. (looking-at (if table-type org-table-any-line-regexp
  3702. org-table-line-regexp)))
  3703. nil))
  3704. (defsubst org-table-p () (org-at-table-p))
  3705. (defun org-at-table.el-p ()
  3706. "Return t if and only if we are at a table.el table."
  3707. (and (org-at-table-p 'any)
  3708. (save-excursion
  3709. (goto-char (org-table-begin 'any))
  3710. (looking-at org-table1-hline-regexp))))
  3711. (defun org-table-recognize-table.el ()
  3712. "If there is a table.el table nearby, recognize it and move into it."
  3713. (if org-table-tab-recognizes-table.el
  3714. (if (org-at-table.el-p)
  3715. (progn
  3716. (beginning-of-line 1)
  3717. (if (looking-at org-table-dataline-regexp)
  3718. nil
  3719. (if (looking-at org-table1-hline-regexp)
  3720. (progn
  3721. (beginning-of-line 2)
  3722. (if (looking-at org-table-any-border-regexp)
  3723. (beginning-of-line -1)))))
  3724. (if (re-search-forward "|" (org-table-end t) t)
  3725. (progn
  3726. (require 'table)
  3727. (if (table--at-cell-p (point))
  3728. t
  3729. (message "recognizing table.el table...")
  3730. (table-recognize-table)
  3731. (message "recognizing table.el table...done")))
  3732. (error "This should not happen"))
  3733. t)
  3734. nil)
  3735. nil))
  3736. (defun org-at-table-hline-p ()
  3737. "Return t if the cursor is inside a hline in a table."
  3738. (if org-enable-table-editor
  3739. (save-excursion
  3740. (beginning-of-line 1)
  3741. (looking-at org-table-hline-regexp))
  3742. nil))
  3743. (defvar org-table-clean-did-remove-column nil)
  3744. (defun org-table-map-tables (function &optional quietly)
  3745. "Apply FUNCTION to the start of all tables in the buffer."
  3746. (save-excursion
  3747. (save-restriction
  3748. (widen)
  3749. (goto-char (point-min))
  3750. (while (re-search-forward org-table-any-line-regexp nil t)
  3751. (unless quietly
  3752. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3753. (beginning-of-line 1)
  3754. (when (and (looking-at org-table-line-regexp)
  3755. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3756. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3757. (save-excursion (funcall function))
  3758. (or (looking-at org-table-line-regexp)
  3759. (forward-char 1)))
  3760. (re-search-forward org-table-any-border-regexp nil 1))))
  3761. (unless quietly (message "Mapping tables: done")))
  3762. ;; Declare and autoload functions from ox.el and al.
  3763. (declare-function org-export-get-environment "ox"
  3764. (&optional backend subtreep ext-plist))
  3765. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3766. ;; Declare and autoload functions from org-agenda.el
  3767. (eval-and-compile
  3768. (org-autoload "org-agenda"
  3769. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3770. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3771. (declare-function org-clock-update-mode-line "org-clock" ())
  3772. (declare-function org-resolve-clocks "org-clock"
  3773. (&optional also-non-dangling-p prompt last-valid))
  3774. (defun org-at-TBLFM-p (&optional pos)
  3775. "Return t when point (or POS) is in #+TBLFM line."
  3776. (save-excursion
  3777. (let ((pos pos)))
  3778. (goto-char (or pos (point)))
  3779. (beginning-of-line 1)
  3780. (looking-at org-TBLFM-regexp)))
  3781. (defvar org-clock-start-time)
  3782. (defvar org-clock-marker (make-marker)
  3783. "Marker recording the last clock-in.")
  3784. (defvar org-clock-hd-marker (make-marker)
  3785. "Marker recording the last clock-in, but the headline position.")
  3786. (defvar org-clock-heading ""
  3787. "The heading of the current clock entry.")
  3788. (defun org-clock-is-active ()
  3789. "Return non-nil if clock is currently running.
  3790. The return value is actually the clock marker."
  3791. (marker-buffer org-clock-marker))
  3792. (eval-and-compile
  3793. (org-autoload "org-clock" '(org-clock-remove-overlays
  3794. org-clock-update-time-maybe
  3795. org-clocktable-shift)))
  3796. (defun org-check-running-clock ()
  3797. "Check if the current buffer contains the running clock.
  3798. If yes, offer to stop it and to save the buffer with the changes."
  3799. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3800. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3801. (buffer-name))))
  3802. (org-clock-out)
  3803. (when (y-or-n-p "Save changed buffer?")
  3804. (save-buffer))))
  3805. (defun org-clocktable-try-shift (dir n)
  3806. "Check if this line starts a clock table, if yes, shift the time block."
  3807. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3808. (org-clocktable-shift dir n)))
  3809. ;;;###autoload
  3810. (defun org-clock-persistence-insinuate ()
  3811. "Set up hooks for clock persistence."
  3812. (require 'org-clock)
  3813. (add-hook 'org-mode-hook 'org-clock-load)
  3814. (add-hook 'kill-emacs-hook 'org-clock-save))
  3815. ;; Define the variable already here, to make sure we have it.
  3816. (defvar org-indent-mode nil
  3817. "Non-nil if Org-Indent mode is enabled.
  3818. Use the command `org-indent-mode' to change this variable.")
  3819. ;; Autoload archiving code
  3820. ;; The stuff that is needed for cycling and tags has to be defined here.
  3821. (defgroup org-archive nil
  3822. "Options concerning archiving in Org-mode."
  3823. :tag "Org Archive"
  3824. :group 'org-structure)
  3825. (defcustom org-archive-location "%s_archive::"
  3826. "The location where subtrees should be archived.
  3827. The value of this variable is a string, consisting of two parts,
  3828. separated by a double-colon. The first part is a filename and
  3829. the second part is a headline.
  3830. When the filename is omitted, archiving happens in the same file.
  3831. %s in the filename will be replaced by the current file
  3832. name (without the directory part). Archiving to a different file
  3833. is useful to keep archived entries from contributing to the
  3834. Org-mode Agenda.
  3835. The archived entries will be filed as subtrees of the specified
  3836. headline. When the headline is omitted, the subtrees are simply
  3837. filed away at the end of the file, as top-level entries. Also in
  3838. the heading you can use %s to represent the file name, this can be
  3839. useful when using the same archive for a number of different files.
  3840. Here are a few examples:
  3841. \"%s_archive::\"
  3842. If the current file is Projects.org, archive in file
  3843. Projects.org_archive, as top-level trees. This is the default.
  3844. \"::* Archived Tasks\"
  3845. Archive in the current file, under the top-level headline
  3846. \"* Archived Tasks\".
  3847. \"~/org/archive.org::\"
  3848. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3849. \"~/org/archive.org::* From %s\"
  3850. Archive in file ~/org/archive.org (absolute path), under headlines
  3851. \"From FILENAME\" where file name is the current file name.
  3852. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3853. The \"datetree/\" string is special, signifying to archive
  3854. items to the datetree. Items are placed in either the CLOSED
  3855. date of the item, or the current date if there is no CLOSED date.
  3856. The heading will be a subentry to the current date. There doesn't
  3857. need to be a heading, but there always needs to be a slash after
  3858. datetree. For example, to store archived items directly in the
  3859. datetree, use \"~/org/datetree.org::datetree/\".
  3860. \"basement::** Finished Tasks\"
  3861. Archive in file ./basement (relative path), as level 3 trees
  3862. below the level 2 heading \"** Finished Tasks\".
  3863. You may set this option on a per-file basis by adding to the buffer a
  3864. line like
  3865. #+ARCHIVE: basement::** Finished Tasks
  3866. You may also define it locally for a subtree by setting an ARCHIVE property
  3867. in the entry. If such a property is found in an entry, or anywhere up
  3868. the hierarchy, it will be used."
  3869. :group 'org-archive
  3870. :type 'string)
  3871. (defcustom org-archive-tag "ARCHIVE"
  3872. "The tag that marks a subtree as archived.
  3873. An archived subtree does not open during visibility cycling, and does
  3874. not contribute to the agenda listings.
  3875. After changing this, font-lock must be restarted in the relevant buffers to
  3876. get the proper fontification."
  3877. :group 'org-archive
  3878. :group 'org-keywords
  3879. :type 'string)
  3880. (defcustom org-agenda-skip-archived-trees t
  3881. "Non-nil means the agenda will skip any items located in archived trees.
  3882. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3883. variable is no longer recommended, you should leave it at the value t.
  3884. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3885. :group 'org-archive
  3886. :group 'org-agenda-skip
  3887. :type 'boolean)
  3888. (defcustom org-columns-skip-archived-trees t
  3889. "Non-nil means ignore archived trees when creating column view."
  3890. :group 'org-archive
  3891. :group 'org-properties
  3892. :type 'boolean)
  3893. (defcustom org-cycle-open-archived-trees nil
  3894. "Non-nil means `org-cycle' will open archived trees.
  3895. An archived tree is a tree marked with the tag ARCHIVE.
  3896. When nil, archived trees will stay folded. You can still open them with
  3897. normal outline commands like `show-all', but not with the cycling commands."
  3898. :group 'org-archive
  3899. :group 'org-cycle
  3900. :type 'boolean)
  3901. (defcustom org-sparse-tree-open-archived-trees nil
  3902. "Non-nil means sparse tree construction shows matches in archived trees.
  3903. When nil, matches in these trees are highlighted, but the trees are kept in
  3904. collapsed state."
  3905. :group 'org-archive
  3906. :group 'org-sparse-trees
  3907. :type 'boolean)
  3908. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3909. "The default date type when building a sparse tree.
  3910. When this is nil, a date is a scheduled or a deadline timestamp.
  3911. Otherwise, these types are allowed:
  3912. all: all timestamps
  3913. active: only active timestamps (<...>)
  3914. inactive: only inactive timestamps (<...)
  3915. scheduled: only scheduled timestamps
  3916. deadline: only deadline timestamps"
  3917. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3918. (const :tag "All timestamps" all)
  3919. (const :tag "Only active timestamps" active)
  3920. (const :tag "Only inactive timestamps" inactive)
  3921. (const :tag "Only scheduled timestamps" scheduled)
  3922. (const :tag "Only deadline timestamps" deadline)
  3923. (const :tag "Only closed timestamps" closed))
  3924. :version "24.3"
  3925. :group 'org-sparse-trees)
  3926. (defun org-cycle-hide-archived-subtrees (state)
  3927. "Re-hide all archived subtrees after a visibility state change."
  3928. (when (and (not org-cycle-open-archived-trees)
  3929. (not (memq state '(overview folded))))
  3930. (save-excursion
  3931. (let* ((globalp (memq state '(contents all)))
  3932. (beg (if globalp (point-min) (point)))
  3933. (end (if globalp (point-max) (org-end-of-subtree t))))
  3934. (org-hide-archived-subtrees beg end)
  3935. (goto-char beg)
  3936. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3937. (message "%s" (substitute-command-keys
  3938. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3939. (defun org-force-cycle-archived ()
  3940. "Cycle subtree even if it is archived."
  3941. (interactive)
  3942. (setq this-command 'org-cycle)
  3943. (let ((org-cycle-open-archived-trees t))
  3944. (call-interactively 'org-cycle)))
  3945. (defun org-hide-archived-subtrees (beg end)
  3946. "Re-hide all archived subtrees after a visibility state change."
  3947. (save-excursion
  3948. (let* ((re (concat ":" org-archive-tag ":")))
  3949. (goto-char beg)
  3950. (while (re-search-forward re end t)
  3951. (when (org-at-heading-p)
  3952. (org-flag-subtree t)
  3953. (org-end-of-subtree t))))))
  3954. (declare-function outline-end-of-heading "outline" ())
  3955. (declare-function outline-flag-region "outline" (from to flag))
  3956. (defun org-flag-subtree (flag)
  3957. (save-excursion
  3958. (org-back-to-heading t)
  3959. (outline-end-of-heading)
  3960. (outline-flag-region (point)
  3961. (progn (org-end-of-subtree t) (point))
  3962. flag)))
  3963. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3964. (eval-and-compile
  3965. (org-autoload "org-archive"
  3966. '(org-add-archive-files)))
  3967. ;; Autoload Column View Code
  3968. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3969. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3970. (declare-function org-columns-compute "org-colview" (property))
  3971. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3972. '(org-columns-number-to-string
  3973. org-columns-get-format-and-top-level
  3974. org-columns-compute
  3975. org-columns-remove-overlays))
  3976. ;; Autoload ID code
  3977. (declare-function org-id-store-link "org-id")
  3978. (declare-function org-id-locations-load "org-id")
  3979. (declare-function org-id-locations-save "org-id")
  3980. (defvar org-id-track-globally)
  3981. (org-autoload "org-id"
  3982. '(org-id-new
  3983. org-id-copy
  3984. org-id-get-with-outline-path-completion
  3985. org-id-get-with-outline-drilling))
  3986. ;;; Variables for pre-computed regular expressions, all buffer local
  3987. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3988. "Matches first line of a hidden block.")
  3989. (make-variable-buffer-local 'org-drawer-regexp)
  3990. (defvar org-todo-regexp nil
  3991. "Matches any of the TODO state keywords.")
  3992. (make-variable-buffer-local 'org-todo-regexp)
  3993. (defvar org-not-done-regexp nil
  3994. "Matches any of the TODO state keywords except the last one.")
  3995. (make-variable-buffer-local 'org-not-done-regexp)
  3996. (defvar org-not-done-heading-regexp nil
  3997. "Matches a TODO headline that is not done.")
  3998. (make-variable-buffer-local 'org-not-done-regexp)
  3999. (defvar org-todo-line-regexp nil
  4000. "Matches a headline and puts TODO state into group 2 if present.")
  4001. (make-variable-buffer-local 'org-todo-line-regexp)
  4002. (defvar org-complex-heading-regexp nil
  4003. "Matches a headline and puts everything into groups:
  4004. group 1: the stars
  4005. group 2: The todo keyword, maybe
  4006. group 3: Priority cookie
  4007. group 4: True headline
  4008. group 5: Tags")
  4009. (make-variable-buffer-local 'org-complex-heading-regexp)
  4010. (defvar org-complex-heading-regexp-format nil
  4011. "Printf format to make regexp to match an exact headline.
  4012. This regexp will match the headline of any node which has the
  4013. exact headline text that is put into the format, but may have any
  4014. TODO state, priority and tags.")
  4015. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4016. (defvar org-todo-line-tags-regexp nil
  4017. "Matches a headline and puts TODO state into group 2 if present.
  4018. Also put tags into group 4 if tags are present.")
  4019. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  4020. (defvar org-ds-keyword-length 12
  4021. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  4022. (make-variable-buffer-local 'org-ds-keyword-length)
  4023. (defvar org-deadline-regexp nil
  4024. "Matches the DEADLINE keyword.")
  4025. (make-variable-buffer-local 'org-deadline-regexp)
  4026. (defvar org-deadline-time-regexp nil
  4027. "Matches the DEADLINE keyword together with a time stamp.")
  4028. (make-variable-buffer-local 'org-deadline-time-regexp)
  4029. (defvar org-deadline-time-hour-regexp nil
  4030. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  4031. (make-variable-buffer-local 'org-deadline-time-hour-regexp)
  4032. (defvar org-deadline-line-regexp nil
  4033. "Matches the DEADLINE keyword and the rest of the line.")
  4034. (make-variable-buffer-local 'org-deadline-line-regexp)
  4035. (defvar org-scheduled-regexp nil
  4036. "Matches the SCHEDULED keyword.")
  4037. (make-variable-buffer-local 'org-scheduled-regexp)
  4038. (defvar org-scheduled-time-regexp nil
  4039. "Matches the SCHEDULED keyword together with a time stamp.")
  4040. (make-variable-buffer-local 'org-scheduled-time-regexp)
  4041. (defvar org-scheduled-time-hour-regexp nil
  4042. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  4043. (make-variable-buffer-local 'org-scheduled-time-hour-regexp)
  4044. (defvar org-closed-time-regexp nil
  4045. "Matches the CLOSED keyword together with a time stamp.")
  4046. (make-variable-buffer-local 'org-closed-time-regexp)
  4047. (defvar org-keyword-time-regexp nil
  4048. "Matches any of the 4 keywords, together with the time stamp.")
  4049. (make-variable-buffer-local 'org-keyword-time-regexp)
  4050. (defvar org-keyword-time-not-clock-regexp nil
  4051. "Matches any of the 3 keywords, together with the time stamp.")
  4052. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  4053. (defvar org-maybe-keyword-time-regexp nil
  4054. "Matches a timestamp, possibly preceded by a keyword.")
  4055. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4056. (defvar org-all-time-keywords nil
  4057. "List of time keywords.")
  4058. (make-variable-buffer-local 'org-all-time-keywords)
  4059. (defconst org-plain-time-of-day-regexp
  4060. (concat
  4061. "\\(\\<[012]?[0-9]"
  4062. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4063. "\\(--?"
  4064. "\\(\\<[012]?[0-9]"
  4065. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4066. "\\)?")
  4067. "Regular expression to match a plain time or time range.
  4068. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4069. groups carry important information:
  4070. 0 the full match
  4071. 1 the first time, range or not
  4072. 8 the second time, if it is a range.")
  4073. (defconst org-plain-time-extension-regexp
  4074. (concat
  4075. "\\(\\<[012]?[0-9]"
  4076. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4077. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4078. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4079. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4080. groups carry important information:
  4081. 0 the full match
  4082. 7 hours of duration
  4083. 9 minutes of duration")
  4084. (defconst org-stamp-time-of-day-regexp
  4085. (concat
  4086. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4087. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4088. "\\(--?"
  4089. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4090. "Regular expression to match a timestamp time or time range.
  4091. After a match, the following groups carry important information:
  4092. 0 the full match
  4093. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4094. 2 the first time, range or not
  4095. 4 the second time, if it is a range.")
  4096. (defconst org-startup-options
  4097. '(("fold" org-startup-folded t)
  4098. ("overview" org-startup-folded t)
  4099. ("nofold" org-startup-folded nil)
  4100. ("showall" org-startup-folded nil)
  4101. ("showeverything" org-startup-folded showeverything)
  4102. ("content" org-startup-folded content)
  4103. ("indent" org-startup-indented t)
  4104. ("noindent" org-startup-indented nil)
  4105. ("hidestars" org-hide-leading-stars t)
  4106. ("showstars" org-hide-leading-stars nil)
  4107. ("odd" org-odd-levels-only t)
  4108. ("oddeven" org-odd-levels-only nil)
  4109. ("align" org-startup-align-all-tables t)
  4110. ("noalign" org-startup-align-all-tables nil)
  4111. ("inlineimages" org-startup-with-inline-images t)
  4112. ("noinlineimages" org-startup-with-inline-images nil)
  4113. ("latexpreview" org-startup-with-latex-preview t)
  4114. ("nolatexpreview" org-startup-with-latex-preview nil)
  4115. ("customtime" org-display-custom-times t)
  4116. ("logdone" org-log-done time)
  4117. ("lognotedone" org-log-done note)
  4118. ("nologdone" org-log-done nil)
  4119. ("lognoteclock-out" org-log-note-clock-out t)
  4120. ("nolognoteclock-out" org-log-note-clock-out nil)
  4121. ("logrepeat" org-log-repeat state)
  4122. ("lognoterepeat" org-log-repeat note)
  4123. ("logdrawer" org-log-into-drawer t)
  4124. ("nologdrawer" org-log-into-drawer nil)
  4125. ("logstatesreversed" org-log-states-order-reversed t)
  4126. ("nologstatesreversed" org-log-states-order-reversed nil)
  4127. ("nologrepeat" org-log-repeat nil)
  4128. ("logreschedule" org-log-reschedule time)
  4129. ("lognotereschedule" org-log-reschedule note)
  4130. ("nologreschedule" org-log-reschedule nil)
  4131. ("logredeadline" org-log-redeadline time)
  4132. ("lognoteredeadline" org-log-redeadline note)
  4133. ("nologredeadline" org-log-redeadline nil)
  4134. ("logrefile" org-log-refile time)
  4135. ("lognoterefile" org-log-refile note)
  4136. ("nologrefile" org-log-refile nil)
  4137. ("fninline" org-footnote-define-inline t)
  4138. ("nofninline" org-footnote-define-inline nil)
  4139. ("fnlocal" org-footnote-section nil)
  4140. ("fnauto" org-footnote-auto-label t)
  4141. ("fnprompt" org-footnote-auto-label nil)
  4142. ("fnconfirm" org-footnote-auto-label confirm)
  4143. ("fnplain" org-footnote-auto-label plain)
  4144. ("fnadjust" org-footnote-auto-adjust t)
  4145. ("nofnadjust" org-footnote-auto-adjust nil)
  4146. ("constcgs" constants-unit-system cgs)
  4147. ("constSI" constants-unit-system SI)
  4148. ("noptag" org-tag-persistent-alist nil)
  4149. ("hideblocks" org-hide-block-startup t)
  4150. ("nohideblocks" org-hide-block-startup nil)
  4151. ("beamer" org-startup-with-beamer-mode t)
  4152. ("entitiespretty" org-pretty-entities t)
  4153. ("entitiesplain" org-pretty-entities nil))
  4154. "Variable associated with STARTUP options for org-mode.
  4155. Each element is a list of three items: the startup options (as written
  4156. in the #+STARTUP line), the corresponding variable, and the value to set
  4157. this variable to if the option is found. An optional forth element PUSH
  4158. means to push this value onto the list in the variable.")
  4159. (defun org-update-property-plist (key val props)
  4160. "Update PROPS with KEY and VAL."
  4161. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4162. (key (if appending (substring key 0 (- (length key) 1)) key))
  4163. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4164. (previous (cdr (assoc key props))))
  4165. (if appending
  4166. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4167. (cons (cons key val) remainder))))
  4168. (defconst org-block-regexp
  4169. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4170. "Regular expression for hiding blocks.")
  4171. (defconst org-heading-keyword-regexp-format
  4172. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4173. "Printf format for a regexp matching a headline with some keyword.
  4174. This regexp will match the headline of any node which has the
  4175. exact keyword that is put into the format. The keyword isn't in
  4176. any group by default, but the stars and the body are.")
  4177. (defconst org-heading-keyword-maybe-regexp-format
  4178. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4179. "Printf format for a regexp matching a headline, possibly with some keyword.
  4180. This regexp can match any headline with the specified keyword, or
  4181. without a keyword. The keyword isn't in any group by default,
  4182. but the stars and the body are.")
  4183. (defcustom org-group-tags t
  4184. "When non-nil (the default), use group tags.
  4185. This can be turned on/off through `org-toggle-tags-groups'."
  4186. :group 'org-tags
  4187. :group 'org-startup
  4188. :type 'boolean)
  4189. (defun org-toggle-tags-groups ()
  4190. "Toggle support for group tags.
  4191. Support for group tags is controlled by the option
  4192. `org-group-tags', which is non-nil by default."
  4193. (interactive)
  4194. (setq org-group-tags (not org-group-tags))
  4195. (cond ((and (derived-mode-p 'org-agenda-mode)
  4196. org-group-tags)
  4197. (org-agenda-redo))
  4198. ((derived-mode-p 'org-mode)
  4199. (let ((org-inhibit-startup t)) (org-mode))))
  4200. (message "Groups tags support has been turned %s"
  4201. (if org-group-tags "on" "off")))
  4202. (defun org-set-regexps-and-options-for-tags ()
  4203. "Precompute regular expressions used for tags in the current buffer."
  4204. (when (derived-mode-p 'org-mode)
  4205. (org-set-local 'org-file-tags nil)
  4206. (let ((re (org-make-options-regexp '("FILETAGS" "TAGS")))
  4207. (splitre "[ \t]+")
  4208. (start 0)
  4209. tags ftags key value)
  4210. (save-excursion
  4211. (save-restriction
  4212. (widen)
  4213. (goto-char (point-min))
  4214. (while (re-search-forward re nil t)
  4215. (setq key (upcase (org-match-string-no-properties 1))
  4216. value (org-match-string-no-properties 2))
  4217. (if (stringp value) (setq value (org-trim value)))
  4218. (cond
  4219. ((equal key "TAGS")
  4220. (setq tags (append tags (if tags '("\\n") nil)
  4221. (org-split-string value splitre))))
  4222. ((equal key "FILETAGS")
  4223. (when (string-match "\\S-" value)
  4224. (setq ftags
  4225. (append
  4226. ftags
  4227. (apply 'append
  4228. (mapcar (lambda (x) (org-split-string x ":"))
  4229. (org-split-string value)))))))))))
  4230. ;; Process the file tags.
  4231. (and ftags (org-set-local 'org-file-tags
  4232. (mapcar 'org-add-prop-inherited ftags)))
  4233. (org-set-local 'org-tag-groups-alist nil)
  4234. ;; Process the tags.
  4235. ;; FIXME
  4236. (when tags
  4237. (let (e tgs g)
  4238. (while (setq e (pop tags))
  4239. (cond
  4240. ((equal e "{")
  4241. (progn (push '(:startgroup) tgs)
  4242. (when (equal (nth 1 tags) ":")
  4243. (push (list (replace-regexp-in-string
  4244. "(.+)$" "" (nth 0 tags)))
  4245. org-tag-groups-alist)
  4246. (setq g 0))))
  4247. ((equal e ":") (push '(:grouptags) tgs))
  4248. ((equal e "}") (push '(:endgroup) tgs) (if g (setq g nil)))
  4249. ((equal e "\\n") (push '(:newline) tgs))
  4250. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4251. (push (cons (match-string 1 e)
  4252. (string-to-char (match-string 2 e))) tgs)
  4253. (if (and g (> g 0))
  4254. (setcar org-tag-groups-alist
  4255. (append (car org-tag-groups-alist)
  4256. (list (match-string 1 e)))))
  4257. (if g (setq g (1+ g))))
  4258. (t (push (list e) tgs)
  4259. (if (and g (> g 0))
  4260. (setcar org-tag-groups-alist
  4261. (append (car org-tag-groups-alist) (list e))))
  4262. (if g (setq g (1+ g))))))
  4263. (org-set-local 'org-tag-alist nil)
  4264. (while (setq e (pop tgs))
  4265. (or (and (stringp (car e))
  4266. (assoc (car e) org-tag-alist))
  4267. (push e org-tag-alist))))))))
  4268. (defun org-set-regexps-and-options ()
  4269. "Precompute regular expressions used in the current buffer."
  4270. (when (derived-mode-p 'org-mode)
  4271. (org-set-local 'org-todo-kwd-alist nil)
  4272. (org-set-local 'org-todo-key-alist nil)
  4273. (org-set-local 'org-todo-key-trigger nil)
  4274. (org-set-local 'org-todo-keywords-1 nil)
  4275. (org-set-local 'org-done-keywords nil)
  4276. (org-set-local 'org-todo-heads nil)
  4277. (org-set-local 'org-todo-sets nil)
  4278. (org-set-local 'org-todo-log-states nil)
  4279. (org-set-local 'org-file-properties nil)
  4280. (let ((re (org-make-options-regexp
  4281. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE"
  4282. "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4283. "SETUPFILE" "OPTIONS")
  4284. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4285. (splitre "[ \t]+")
  4286. (scripts org-use-sub-superscripts)
  4287. kwds kws0 kwsa key log value cat arch const links hw dws
  4288. tail sep kws1 prio props drawers ext-setup-or-nil setup-contents
  4289. (start 0))
  4290. (save-excursion
  4291. (save-restriction
  4292. (widen)
  4293. (goto-char (point-min))
  4294. (while (or (and ext-setup-or-nil
  4295. (let (ret)
  4296. (with-temp-buffer
  4297. (insert ext-setup-or-nil)
  4298. (let ((major-mode 'org-mode))
  4299. (org-set-regexps-and-options-for-tags)
  4300. (setq ret (list org-file-tags org-tag-alist
  4301. org-tag-groups-alist))))
  4302. (setq org-file-tags (nth 0 ret)
  4303. org-tag-alist (nth 1 ret)
  4304. org-tag-groups-alist (nth 2 ret))))
  4305. (and ext-setup-or-nil
  4306. (string-match re ext-setup-or-nil start)
  4307. (setq start (match-end 0)))
  4308. (and (setq ext-setup-or-nil nil start 0)
  4309. (re-search-forward re nil t)))
  4310. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4311. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4312. (if (stringp value) (setq value (org-trim value)))
  4313. (cond
  4314. ((equal key "CATEGORY")
  4315. (setq cat value))
  4316. ((member key '("SEQ_TODO" "TODO"))
  4317. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4318. ((equal key "TYP_TODO")
  4319. (push (cons 'type (org-split-string value splitre)) kwds))
  4320. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4321. ;; general TODO-like setup
  4322. (push (cons (intern (downcase (match-string 1 key)))
  4323. (org-split-string value splitre)) kwds))
  4324. ((equal key "COLUMNS")
  4325. (org-set-local 'org-columns-default-format value))
  4326. ((equal key "LINK")
  4327. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4328. (push (cons (match-string 1 value)
  4329. (org-trim (match-string 2 value)))
  4330. links)))
  4331. ((equal key "PRIORITIES")
  4332. (setq prio (org-split-string value " +")))
  4333. ((equal key "PROPERTY")
  4334. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4335. (setq props (org-update-property-plist (match-string 1 value)
  4336. (match-string 2 value)
  4337. props))))
  4338. ((equal key "DRAWERS")
  4339. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4340. ((equal key "CONSTANTS")
  4341. (org-table-set-constants))
  4342. ((equal key "STARTUP")
  4343. (let ((opts (org-split-string value splitre))
  4344. l var val)
  4345. (while (setq l (pop opts))
  4346. (when (setq l (assoc l org-startup-options))
  4347. (setq var (nth 1 l) val (nth 2 l))
  4348. (if (not (nth 3 l))
  4349. (set (make-local-variable var) val)
  4350. (if (not (listp (symbol-value var)))
  4351. (set (make-local-variable var) nil))
  4352. (set (make-local-variable var) (symbol-value var))
  4353. (add-to-list var val))))))
  4354. ((equal key "ARCHIVE")
  4355. (setq arch value)
  4356. (remove-text-properties 0 (length arch)
  4357. '(face t fontified t) arch))
  4358. ((equal key "OPTIONS")
  4359. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4360. (setq scripts (read (match-string 2 value)))))
  4361. ((and (equal key "SETUPFILE")
  4362. ;; Prevent checking in Gnus messages
  4363. (not buffer-read-only))
  4364. (setq setup-contents (org-file-contents
  4365. (expand-file-name
  4366. (org-remove-double-quotes value))
  4367. 'noerror))
  4368. (if (not ext-setup-or-nil)
  4369. (setq ext-setup-or-nil setup-contents start 0)
  4370. (setq ext-setup-or-nil
  4371. (concat (substring ext-setup-or-nil 0 start)
  4372. "\n" setup-contents "\n"
  4373. (substring ext-setup-or-nil start)))))))
  4374. ;; search for property blocks
  4375. (goto-char (point-min))
  4376. (while (re-search-forward org-block-regexp nil t)
  4377. (when (equal "PROPERTY" (upcase (match-string 1)))
  4378. (setq value (replace-regexp-in-string
  4379. "[\n\r]" " " (match-string 4)))
  4380. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4381. (setq props (org-update-property-plist (match-string 1 value)
  4382. (match-string 2 value)
  4383. props)))))))
  4384. (org-set-local 'org-use-sub-superscripts scripts)
  4385. (when cat
  4386. (org-set-local 'org-category (intern cat))
  4387. (push (cons "CATEGORY" cat) props))
  4388. (when prio
  4389. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4390. (setq prio (mapcar 'string-to-char prio))
  4391. (org-set-local 'org-highest-priority (nth 0 prio))
  4392. (org-set-local 'org-lowest-priority (nth 1 prio))
  4393. (org-set-local 'org-default-priority (nth 2 prio)))
  4394. (and props (org-set-local 'org-file-properties (nreverse props)))
  4395. (and drawers (org-set-local 'org-drawers drawers))
  4396. (and arch (org-set-local 'org-archive-location arch))
  4397. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4398. ;; Process the TODO keywords
  4399. (unless kwds
  4400. ;; Use the global values as if they had been given locally.
  4401. (setq kwds (default-value 'org-todo-keywords))
  4402. (if (stringp (car kwds))
  4403. (setq kwds (list (cons org-todo-interpretation
  4404. (default-value 'org-todo-keywords)))))
  4405. (setq kwds (reverse kwds)))
  4406. (setq kwds (nreverse kwds))
  4407. (let (inter kws kw)
  4408. (while (setq kws (pop kwds))
  4409. (let ((kws (or
  4410. (run-hook-with-args-until-success
  4411. 'org-todo-setup-filter-hook kws)
  4412. kws)))
  4413. (setq inter (pop kws) sep (member "|" kws)
  4414. kws0 (delete "|" (copy-sequence kws))
  4415. kwsa nil
  4416. kws1 (mapcar
  4417. (lambda (x)
  4418. ;; 1 2
  4419. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4420. (progn
  4421. (setq kw (match-string 1 x)
  4422. key (and (match-end 2) (match-string 2 x))
  4423. log (org-extract-log-state-settings x))
  4424. (push (cons kw (and key (string-to-char key))) kwsa)
  4425. (and log (push log org-todo-log-states))
  4426. kw)
  4427. (error "Invalid TODO keyword %s" x)))
  4428. kws0)
  4429. kwsa (if kwsa (append '((:startgroup))
  4430. (nreverse kwsa)
  4431. '((:endgroup))))
  4432. hw (car kws1)
  4433. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4434. tail (list inter hw (car dws) (org-last dws))))
  4435. (add-to-list 'org-todo-heads hw 'append)
  4436. (push kws1 org-todo-sets)
  4437. (setq org-done-keywords (append org-done-keywords dws nil))
  4438. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4439. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4440. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4441. (setq org-todo-sets (nreverse org-todo-sets)
  4442. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4443. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4444. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4445. ;; Compute the regular expressions and other local variables.
  4446. ;; Using `org-outline-regexp-bol' would complicate them much,
  4447. ;; because of the fixed white space at the end of that string.
  4448. (if (not org-done-keywords)
  4449. (setq org-done-keywords (and org-todo-keywords-1
  4450. (list (org-last org-todo-keywords-1)))))
  4451. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4452. (length org-scheduled-string)
  4453. (length org-clock-string)
  4454. (length org-closed-string)))
  4455. org-drawer-regexp
  4456. (concat "^[ \t]*:\\("
  4457. (mapconcat 'regexp-quote org-drawers "\\|")
  4458. "\\):[ \t]*$")
  4459. org-not-done-keywords
  4460. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4461. org-todo-regexp
  4462. (concat "\\("
  4463. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4464. "\\)")
  4465. org-not-done-regexp
  4466. (concat "\\("
  4467. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4468. "\\)")
  4469. org-not-done-heading-regexp
  4470. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4471. org-todo-line-regexp
  4472. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4473. org-complex-heading-regexp
  4474. (concat "^\\(\\*+\\)"
  4475. "\\(?: +" org-todo-regexp "\\)?"
  4476. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4477. "\\(?: +\\(.*?\\)\\)??"
  4478. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4479. "[ \t]*$")
  4480. org-complex-heading-regexp-format
  4481. (concat "^\\(\\*+\\)"
  4482. "\\(?: +" org-todo-regexp "\\)?"
  4483. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4484. "\\(?: +"
  4485. ;; Stats cookies can be stuck to body.
  4486. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4487. "\\(%s\\)"
  4488. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4489. "\\)"
  4490. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4491. "[ \t]*$")
  4492. org-todo-line-tags-regexp
  4493. (concat "^\\(\\*+\\)"
  4494. "\\(?: +" org-todo-regexp "\\)?"
  4495. "\\(?: +\\(.*?\\)\\)??"
  4496. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4497. "[ \t]*$")
  4498. org-deadline-regexp (concat "\\<" org-deadline-string)
  4499. org-deadline-time-regexp
  4500. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4501. org-deadline-time-hour-regexp
  4502. (concat "\\<" org-deadline-string
  4503. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4504. org-deadline-line-regexp
  4505. (concat "\\<\\(" org-deadline-string "\\).*")
  4506. org-scheduled-regexp
  4507. (concat "\\<" org-scheduled-string)
  4508. org-scheduled-time-regexp
  4509. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4510. org-scheduled-time-hour-regexp
  4511. (concat "\\<" org-scheduled-string
  4512. " *<\\(.+[0-9]\\{1,2\\}:[0-9]\\{2\\}[^>]*\\)>")
  4513. org-closed-time-regexp
  4514. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4515. org-keyword-time-regexp
  4516. (concat "\\<\\(" org-scheduled-string
  4517. "\\|" org-deadline-string
  4518. "\\|" org-closed-string
  4519. "\\|" org-clock-string "\\)"
  4520. " *[[<]\\([^]>]+\\)[]>]")
  4521. org-keyword-time-not-clock-regexp
  4522. (concat "\\<\\(" org-scheduled-string
  4523. "\\|" org-deadline-string
  4524. "\\|" org-closed-string
  4525. "\\)"
  4526. " *[[<]\\([^]>]+\\)[]>]")
  4527. org-maybe-keyword-time-regexp
  4528. (concat "\\(\\<\\(" org-scheduled-string
  4529. "\\|" org-deadline-string
  4530. "\\|" org-closed-string
  4531. "\\|" org-clock-string "\\)\\)?"
  4532. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4533. org-all-time-keywords
  4534. (mapcar (lambda (w) (substring w 0 -1))
  4535. (list org-scheduled-string org-deadline-string
  4536. org-clock-string org-closed-string)))
  4537. (org-compute-latex-and-related-regexp)
  4538. (org-set-font-lock-defaults))))
  4539. (defun org-file-contents (file &optional noerror)
  4540. "Return the contents of FILE, as a string."
  4541. (if (or (not file)
  4542. (not (file-readable-p file)))
  4543. (if noerror
  4544. (message "Cannot read file \"%s\"" file)
  4545. (error "Cannot read file \"%s\"" file))
  4546. (with-temp-buffer
  4547. (insert-file-contents file)
  4548. (buffer-string))))
  4549. (defun org-extract-log-state-settings (x)
  4550. "Extract the log state setting from a TODO keyword string.
  4551. This will extract info from a string like \"WAIT(w@/!)\"."
  4552. (let (kw key log1 log2)
  4553. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4554. (setq kw (match-string 1 x)
  4555. key (and (match-end 2) (match-string 2 x))
  4556. log1 (and (match-end 3) (match-string 3 x))
  4557. log2 (and (match-end 4) (match-string 4 x)))
  4558. (and (or log1 log2)
  4559. (list kw
  4560. (and log1 (if (equal log1 "!") 'time 'note))
  4561. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4562. (defun org-remove-keyword-keys (list)
  4563. "Remove a pair of parenthesis at the end of each string in LIST."
  4564. (mapcar (lambda (x)
  4565. (if (string-match "(.*)$" x)
  4566. (substring x 0 (match-beginning 0))
  4567. x))
  4568. list))
  4569. (defun org-assign-fast-keys (alist)
  4570. "Assign fast keys to a keyword-key alist.
  4571. Respect keys that are already there."
  4572. (let (new e (alt ?0))
  4573. (while (setq e (pop alist))
  4574. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4575. (cdr e)) ;; Key already assigned.
  4576. (push e new)
  4577. (let ((clist (string-to-list (downcase (car e))))
  4578. (used (append new alist)))
  4579. (when (= (car clist) ?@)
  4580. (pop clist))
  4581. (while (and clist (rassoc (car clist) used))
  4582. (pop clist))
  4583. (unless clist
  4584. (while (rassoc alt used)
  4585. (incf alt)))
  4586. (push (cons (car e) (or (car clist) alt)) new))))
  4587. (nreverse new)))
  4588. ;;; Some variables used in various places
  4589. (defvar org-window-configuration nil
  4590. "Used in various places to store a window configuration.")
  4591. (defvar org-selected-window nil
  4592. "Used in various places to store a window configuration.")
  4593. (defvar org-finish-function nil
  4594. "Function to be called when `C-c C-c' is used.
  4595. This is for getting out of special buffers like capture.")
  4596. ;; FIXME: Occasionally check by commenting these, to make sure
  4597. ;; no other functions uses these, forgetting to let-bind them.
  4598. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4599. (defvar org-last-state)
  4600. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4601. ;; Defined somewhere in this file, but used before definition.
  4602. (defvar org-entities) ;; defined in org-entities.el
  4603. (defvar org-struct-menu)
  4604. (defvar org-org-menu)
  4605. (defvar org-tbl-menu)
  4606. ;;;; Define the Org-mode
  4607. ;; We use a before-change function to check if a table might need
  4608. ;; an update.
  4609. (defvar org-table-may-need-update t
  4610. "Indicates that a table might need an update.
  4611. This variable is set by `org-before-change-function'.
  4612. `org-table-align' sets it back to nil.")
  4613. (defun org-before-change-function (beg end)
  4614. "Every change indicates that a table might need an update."
  4615. (setq org-table-may-need-update t))
  4616. (defvar org-mode-map)
  4617. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4618. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4619. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4620. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4621. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4622. (defvar org-table-buffer-is-an nil)
  4623. (defvar bidi-paragraph-direction)
  4624. (defvar buffer-face-mode-face)
  4625. (require 'outline)
  4626. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4627. (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"))
  4628. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4629. ;; Other stuff we need.
  4630. (require 'time-date)
  4631. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4632. (require 'easymenu)
  4633. (require 'overlay)
  4634. ;; (require 'org-macs) moved higher up in the file before it is first used
  4635. (require 'org-entities)
  4636. ;; (require 'org-compat) moved higher up in the file before it is first used
  4637. (require 'org-faces)
  4638. (require 'org-list)
  4639. (require 'org-pcomplete)
  4640. (require 'org-src)
  4641. (require 'org-footnote)
  4642. (require 'org-macro)
  4643. ;; babel
  4644. (require 'ob)
  4645. ;;;###autoload
  4646. (define-derived-mode org-mode outline-mode "Org"
  4647. "Outline-based notes management and organizer, alias
  4648. \"Carsten's outline-mode for keeping track of everything.\"
  4649. Org-mode develops organizational tasks around a NOTES file which
  4650. contains information about projects as plain text. Org-mode is
  4651. implemented on top of outline-mode, which is ideal to keep the content
  4652. of large files well structured. It supports ToDo items, deadlines and
  4653. time stamps, which magically appear in the diary listing of the Emacs
  4654. calendar. Tables are easily created with a built-in table editor.
  4655. Plain text URL-like links connect to websites, emails (VM), Usenet
  4656. messages (Gnus), BBDB entries, and any files related to the project.
  4657. For printing and sharing of notes, an Org-mode file (or a part of it)
  4658. can be exported as a structured ASCII or HTML file.
  4659. The following commands are available:
  4660. \\{org-mode-map}"
  4661. ;; Get rid of Outline menus, they are not needed
  4662. ;; Need to do this here because define-derived-mode sets up
  4663. ;; the keymap so late. Still, it is a waste to call this each time
  4664. ;; we switch another buffer into org-mode.
  4665. (if (featurep 'xemacs)
  4666. (when (boundp 'outline-mode-menu-heading)
  4667. ;; Assume this is Greg's port, it uses easymenu
  4668. (easy-menu-remove outline-mode-menu-heading)
  4669. (easy-menu-remove outline-mode-menu-show)
  4670. (easy-menu-remove outline-mode-menu-hide))
  4671. (define-key org-mode-map [menu-bar headings] 'undefined)
  4672. (define-key org-mode-map [menu-bar hide] 'undefined)
  4673. (define-key org-mode-map [menu-bar show] 'undefined))
  4674. (org-load-modules-maybe)
  4675. (easy-menu-add org-org-menu)
  4676. (easy-menu-add org-tbl-menu)
  4677. (org-install-agenda-files-menu)
  4678. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4679. (add-to-invisibility-spec '(org-cwidth))
  4680. (add-to-invisibility-spec '(org-hide-block . t))
  4681. (when (featurep 'xemacs)
  4682. (org-set-local 'line-move-ignore-invisible t))
  4683. (org-set-local 'outline-regexp org-outline-regexp)
  4684. (org-set-local 'outline-level 'org-outline-level)
  4685. (setq bidi-paragraph-direction 'left-to-right)
  4686. ;; FIXME Circumvent a bug in outline.el (Emacs <24.4)
  4687. (set (make-local-variable 'paragraph-start) " \\|[ \t]*$\\|\\*+ ")
  4688. (when (and org-ellipsis
  4689. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4690. (fboundp 'make-glyph-code))
  4691. (unless org-display-table
  4692. (setq org-display-table (make-display-table)))
  4693. (set-display-table-slot
  4694. org-display-table 4
  4695. (vconcat (mapcar
  4696. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4697. org-ellipsis)))
  4698. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4699. (setq buffer-display-table org-display-table))
  4700. (org-set-regexps-and-options-for-tags)
  4701. (org-set-regexps-and-options)
  4702. (when (and org-tag-faces (not org-tags-special-faces-re))
  4703. ;; tag faces set outside customize.... force initialization.
  4704. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4705. ;; Calc embedded
  4706. (org-set-local 'calc-embedded-open-mode "# ")
  4707. ;; Modify a few syntax entries
  4708. (modify-syntax-entry ?@ "w")
  4709. (modify-syntax-entry ?\" "\"")
  4710. (if org-startup-truncated (setq truncate-lines t))
  4711. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4712. (org-set-local 'font-lock-unfontify-region-function
  4713. 'org-unfontify-region)
  4714. ;; Activate before-change-function
  4715. (org-set-local 'org-table-may-need-update t)
  4716. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4717. 'local)
  4718. ;; Check for running clock before killing a buffer
  4719. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4720. ;; Initialize macros templates.
  4721. (org-macro-initialize-templates)
  4722. ;; Initialize radio targets.
  4723. (org-update-radio-target-regexp)
  4724. ;; Indentation.
  4725. (org-set-local 'indent-line-function 'org-indent-line)
  4726. (org-set-local 'indent-region-function 'org-indent-region)
  4727. ;; Filling and auto-filling.
  4728. (org-setup-filling)
  4729. ;; Comments.
  4730. (org-setup-comments-handling)
  4731. ;; Beginning/end of defun
  4732. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4733. (org-set-local 'end-of-defun-function 'org-forward-element)
  4734. ;; Next error for sparse trees
  4735. (org-set-local 'next-error-function 'org-occur-next-match)
  4736. ;; Make sure dependence stuff works reliably, even for users who set it
  4737. ;; too late :-(
  4738. (if org-enforce-todo-dependencies
  4739. (add-hook 'org-blocker-hook
  4740. 'org-block-todo-from-children-or-siblings-or-parent)
  4741. (remove-hook 'org-blocker-hook
  4742. 'org-block-todo-from-children-or-siblings-or-parent))
  4743. (if org-enforce-todo-checkbox-dependencies
  4744. (add-hook 'org-blocker-hook
  4745. 'org-block-todo-from-checkboxes)
  4746. (remove-hook 'org-blocker-hook
  4747. 'org-block-todo-from-checkboxes))
  4748. ;; Align options lines
  4749. (org-set-local
  4750. 'align-mode-rules-list
  4751. '((org-in-buffer-settings
  4752. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4753. (modes . '(org-mode)))))
  4754. ;; Imenu
  4755. (org-set-local 'imenu-create-index-function
  4756. 'org-imenu-get-tree)
  4757. ;; Make isearch reveal context
  4758. (if (or (featurep 'xemacs)
  4759. (not (boundp 'outline-isearch-open-invisible-function)))
  4760. ;; Emacs 21 and XEmacs make use of the hook
  4761. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4762. ;; Emacs 22 deals with this through a special variable
  4763. (org-set-local 'outline-isearch-open-invisible-function
  4764. (lambda (&rest ignore) (org-show-context 'isearch)))
  4765. (org-add-hook 'isearch-mode-end-hook 'org-fix-ellipsis-at-bol 'append 'local))
  4766. ;; Setup the pcomplete hooks
  4767. (set (make-local-variable 'pcomplete-command-completion-function)
  4768. 'org-pcomplete-initial)
  4769. (set (make-local-variable 'pcomplete-command-name-function)
  4770. 'org-command-at-point)
  4771. (set (make-local-variable 'pcomplete-default-completion-function)
  4772. 'ignore)
  4773. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4774. 'org-parse-arguments)
  4775. (set (make-local-variable 'pcomplete-termination-string) "")
  4776. (when (>= emacs-major-version 23)
  4777. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4778. ;; If empty file that did not turn on org-mode automatically, make it to.
  4779. (if (and org-insert-mode-line-in-empty-file
  4780. (org-called-interactively-p 'any)
  4781. (= (point-min) (point-max)))
  4782. (insert "# -*- mode: org -*-\n\n"))
  4783. (unless org-inhibit-startup
  4784. (org-unmodified
  4785. (and org-startup-with-beamer-mode (org-beamer-mode))
  4786. (when org-startup-align-all-tables
  4787. (org-table-map-tables 'org-table-align 'quietly))
  4788. (when org-startup-with-inline-images
  4789. (org-display-inline-images))
  4790. (when org-startup-with-latex-preview
  4791. (org-preview-latex-fragment))
  4792. (unless org-inhibit-startup-visibility-stuff
  4793. (org-set-startup-visibility))))
  4794. ;; Try to set org-hide correctly
  4795. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4796. ;; Update `customize-package-emacs-version-alist'
  4797. (add-to-list 'customize-package-emacs-version-alist
  4798. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4799. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4800. ("8.0" . "24.4")))
  4801. (defvar org-mode-transpose-word-syntax-table
  4802. (let ((st (make-syntax-table)))
  4803. (mapc (lambda(c) (modify-syntax-entry
  4804. (string-to-char (car c)) "w p" st))
  4805. org-emphasis-alist)
  4806. st))
  4807. (when (fboundp 'abbrev-table-put)
  4808. (abbrev-table-put org-mode-abbrev-table
  4809. :parents (list text-mode-abbrev-table)))
  4810. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4811. (defsubst org-fix-ellipsis-at-bol ()
  4812. (save-excursion (goto-char (window-start)) (recenter 0)))
  4813. (defun org-find-invisible-foreground ()
  4814. (let ((candidates (remove
  4815. "unspecified-bg"
  4816. (nconc
  4817. (list (face-background 'default)
  4818. (face-background 'org-default))
  4819. (mapcar
  4820. (lambda (alist)
  4821. (when (boundp alist)
  4822. (cdr (assoc 'background-color (symbol-value alist)))))
  4823. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4824. (list (face-foreground 'org-hide))))))
  4825. (car (remove nil candidates))))
  4826. (defun org-current-time (&optional rounding-minutes past)
  4827. "Current time, possibly rounded to ROUNDING-MINUTES.
  4828. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4829. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4830. the rounding returns a past time."
  4831. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4832. (car org-time-stamp-rounding-minutes)))
  4833. (time (decode-time)) res)
  4834. (if (< r 1)
  4835. (current-time)
  4836. (setq res
  4837. (apply 'encode-time
  4838. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4839. (nthcdr 2 time))))
  4840. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4841. (seconds-to-time (- (org-float-time res) (* r 60)))
  4842. res))))
  4843. (defun org-today ()
  4844. "Return today date, considering `org-extend-today-until'."
  4845. (time-to-days
  4846. (time-subtract (current-time)
  4847. (list 0 (* 3600 org-extend-today-until) 0))))
  4848. ;;;; Font-Lock stuff, including the activators
  4849. (defvar org-mouse-map (make-sparse-keymap))
  4850. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4851. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4852. (when org-mouse-1-follows-link
  4853. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4854. (when org-tab-follows-link
  4855. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4856. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4857. (require 'font-lock)
  4858. (defconst org-non-link-chars "]\t\n\r<>")
  4859. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4860. "shell" "elisp" "doi" "message"))
  4861. (defvar org-link-types-re nil
  4862. "Matches a link that has a url-like prefix like \"http:\"")
  4863. (defvar org-link-re-with-space nil
  4864. "Matches a link with spaces, optional angular brackets around it.")
  4865. (defvar org-link-re-with-space2 nil
  4866. "Matches a link with spaces, optional angular brackets around it.")
  4867. (defvar org-link-re-with-space3 nil
  4868. "Matches a link with spaces, only for internal part in bracket links.")
  4869. (defvar org-angle-link-re nil
  4870. "Matches link with angular brackets, spaces are allowed.")
  4871. (defvar org-plain-link-re nil
  4872. "Matches plain link, without spaces.")
  4873. (defvar org-bracket-link-regexp nil
  4874. "Matches a link in double brackets.")
  4875. (defvar org-bracket-link-analytic-regexp nil
  4876. "Regular expression used to analyze links.
  4877. Here is what the match groups contain after a match:
  4878. 1: http:
  4879. 2: http
  4880. 3: path
  4881. 4: [desc]
  4882. 5: desc")
  4883. (defvar org-bracket-link-analytic-regexp++ nil
  4884. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4885. (defvar org-any-link-re nil
  4886. "Regular expression matching any link.")
  4887. (defconst org-match-sexp-depth 3
  4888. "Number of stacked braces for sub/superscript matching.")
  4889. (defun org-create-multibrace-regexp (left right n)
  4890. "Create a regular expression which will match a balanced sexp.
  4891. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4892. as single character strings.
  4893. The regexp returned will match the entire expression including the
  4894. delimiters. It will also define a single group which contains the
  4895. match except for the outermost delimiters. The maximum depth of
  4896. stacked delimiters is N. Escaping delimiters is not possible."
  4897. (let* ((nothing (concat "[^" left right "]*?"))
  4898. (or "\\|")
  4899. (re nothing)
  4900. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4901. (while (> n 1)
  4902. (setq n (1- n)
  4903. re (concat re or next)
  4904. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4905. (concat left "\\(" re "\\)" right)))
  4906. (defvar org-match-substring-regexp
  4907. (concat
  4908. "\\(\\S-\\)\\([_^]\\)\\("
  4909. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4910. "\\|"
  4911. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4912. "\\|"
  4913. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4914. "The regular expression matching a sub- or superscript.")
  4915. (defvar org-match-substring-with-braces-regexp
  4916. (concat
  4917. "\\(\\S-\\)\\([_^]\\)\\("
  4918. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4919. "\\)")
  4920. "The regular expression matching a sub- or superscript, forcing braces.")
  4921. (defun org-make-link-regexps ()
  4922. "Update the link regular expressions.
  4923. This should be called after the variable `org-link-types' has changed."
  4924. (setq org-link-types-re
  4925. (concat
  4926. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4927. org-link-re-with-space
  4928. (concat
  4929. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4930. "\\([^" org-non-link-chars " ]"
  4931. "[^" org-non-link-chars "]*"
  4932. "[^" org-non-link-chars " ]\\)>?")
  4933. org-link-re-with-space2
  4934. (concat
  4935. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4936. "\\([^" org-non-link-chars " ]"
  4937. "[^\t\n\r]*"
  4938. "[^" org-non-link-chars " ]\\)>?")
  4939. org-link-re-with-space3
  4940. (concat
  4941. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4942. "\\([^" org-non-link-chars " ]"
  4943. "[^\t\n\r]*\\)")
  4944. org-angle-link-re
  4945. (concat
  4946. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4947. "\\([^" org-non-link-chars " ]"
  4948. "[^" org-non-link-chars "]*"
  4949. "\\)>")
  4950. org-plain-link-re
  4951. (concat
  4952. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4953. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4954. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4955. org-bracket-link-regexp
  4956. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4957. org-bracket-link-analytic-regexp
  4958. (concat
  4959. "\\[\\["
  4960. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4961. "\\([^]]+\\)"
  4962. "\\]"
  4963. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4964. "\\]")
  4965. org-bracket-link-analytic-regexp++
  4966. (concat
  4967. "\\[\\["
  4968. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4969. "\\([^]]+\\)"
  4970. "\\]"
  4971. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4972. "\\]")
  4973. org-any-link-re
  4974. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4975. org-angle-link-re "\\)\\|\\("
  4976. org-plain-link-re "\\)")))
  4977. (org-make-link-regexps)
  4978. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4979. "Regular expression for fast time stamp matching.")
  4980. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4981. "Regular expression for fast time stamp matching.")
  4982. (defconst org-ts-regexp0
  4983. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4984. "Regular expression matching time strings for analysis.
  4985. This one does not require the space after the date, so it can be used
  4986. on a string that terminates immediately after the date.")
  4987. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4988. "Regular expression matching time strings for analysis.")
  4989. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4990. "Regular expression matching time stamps, with groups.")
  4991. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4992. "Regular expression matching time stamps (also [..]), with groups.")
  4993. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4994. "Regular expression matching a time stamp range.")
  4995. (defconst org-tr-regexp-both
  4996. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4997. "Regular expression matching a time stamp range.")
  4998. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4999. org-ts-regexp "\\)?")
  5000. "Regular expression matching a time stamp or time stamp range.")
  5001. (defconst org-tsr-regexp-both
  5002. (concat org-ts-regexp-both "\\(--?-?"
  5003. org-ts-regexp-both "\\)?")
  5004. "Regular expression matching a time stamp or time stamp range.
  5005. The time stamps may be either active or inactive.")
  5006. (defvar org-emph-face nil)
  5007. (defun org-do-emphasis-faces (limit)
  5008. "Run through the buffer and add overlays to emphasized strings."
  5009. (let (rtn a)
  5010. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5011. (if (not (= (char-after (match-beginning 3))
  5012. (char-after (match-beginning 4))))
  5013. (progn
  5014. (setq rtn t)
  5015. (setq a (assoc (match-string 3) org-emphasis-alist))
  5016. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5017. 'face
  5018. (nth 1 a))
  5019. (and (nth 4 a)
  5020. (org-remove-flyspell-overlays-in
  5021. (match-beginning 0) (match-end 0)))
  5022. (add-text-properties (match-beginning 2) (match-end 2)
  5023. '(font-lock-multiline t org-emphasis t))
  5024. (when org-hide-emphasis-markers
  5025. (add-text-properties (match-end 4) (match-beginning 5)
  5026. '(invisible org-link))
  5027. (add-text-properties (match-beginning 3) (match-end 3)
  5028. '(invisible org-link)))))
  5029. (backward-char 1))
  5030. rtn))
  5031. (defun org-emphasize (&optional char)
  5032. "Insert or change an emphasis, i.e. a font like bold or italic.
  5033. If there is an active region, change that region to a new emphasis.
  5034. If there is no region, just insert the marker characters and position
  5035. the cursor between them.
  5036. CHAR should be the marker character. If it is a space, it means to
  5037. remove the emphasis of the selected region.
  5038. If CHAR is not given (for example in an interactive call) it will be
  5039. prompted for."
  5040. (interactive)
  5041. (let ((erc org-emphasis-regexp-components)
  5042. (prompt "")
  5043. (string "") beg end move c s)
  5044. (if (org-region-active-p)
  5045. (setq beg (region-beginning) end (region-end)
  5046. string (buffer-substring beg end))
  5047. (setq move t))
  5048. (unless char
  5049. (message "Emphasis marker or tag: [%s]"
  5050. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5051. (setq char (read-char-exclusive)))
  5052. (if (equal char ?\ )
  5053. (setq s "" move nil)
  5054. (unless (assoc (char-to-string char) org-emphasis-alist)
  5055. (user-error "No such emphasis marker: \"%c\"" char))
  5056. (setq s (char-to-string char)))
  5057. (while (and (> (length string) 1)
  5058. (equal (substring string 0 1) (substring string -1))
  5059. (assoc (substring string 0 1) org-emphasis-alist))
  5060. (setq string (substring string 1 -1)))
  5061. (setq string (concat s string s))
  5062. (if beg (delete-region beg end))
  5063. (unless (or (bolp)
  5064. (string-match (concat "[" (nth 0 erc) "\n]")
  5065. (char-to-string (char-before (point)))))
  5066. (insert " "))
  5067. (unless (or (eobp)
  5068. (string-match (concat "[" (nth 1 erc) "\n]")
  5069. (char-to-string (char-after (point)))))
  5070. (insert " ") (backward-char 1))
  5071. (insert string)
  5072. (and move (backward-char 1))))
  5073. (defconst org-nonsticky-props
  5074. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5075. (defsubst org-rear-nonsticky-at (pos)
  5076. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5077. (defun org-activate-plain-links (limit)
  5078. "Run through the buffer and add overlays to links."
  5079. (let (f hl)
  5080. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5081. (not (org-in-src-block-p)))
  5082. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5083. (setq f (get-text-property (match-beginning 0) 'face))
  5084. (setq hl (org-match-string-no-properties 0))
  5085. (if (or (eq f 'org-tag)
  5086. (and (listp f) (memq 'org-tag f)))
  5087. nil
  5088. (add-text-properties (match-beginning 0) (match-end 0)
  5089. (list 'mouse-face 'highlight
  5090. 'face 'org-link
  5091. 'htmlize-link `(:uri ,hl)
  5092. 'keymap org-mouse-map))
  5093. (org-rear-nonsticky-at (match-end 0)))
  5094. t)))
  5095. (defun org-activate-code (limit)
  5096. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5097. (progn
  5098. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5099. (remove-text-properties (match-beginning 0) (match-end 0)
  5100. '(display t invisible t intangible t))
  5101. t)))
  5102. (defcustom org-src-fontify-natively nil
  5103. "When non-nil, fontify code in code blocks."
  5104. :type 'boolean
  5105. :version "24.1"
  5106. :group 'org-appearance
  5107. :group 'org-babel)
  5108. (defcustom org-allow-promoting-top-level-subtree nil
  5109. "When non-nil, allow promoting a top level subtree.
  5110. The leading star of the top level headline will be replaced
  5111. by a #."
  5112. :type 'boolean
  5113. :version "24.1"
  5114. :group 'org-appearance)
  5115. (defun org-fontify-meta-lines-and-blocks (limit)
  5116. (condition-case nil
  5117. (org-fontify-meta-lines-and-blocks-1 limit)
  5118. (error (message "org-mode fontification error"))))
  5119. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5120. "Fontify #+ lines and blocks."
  5121. (let ((case-fold-search t))
  5122. (if (re-search-forward
  5123. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5124. limit t)
  5125. (let ((beg (match-beginning 0))
  5126. (block-start (match-end 0))
  5127. (block-end nil)
  5128. (lang (match-string 7))
  5129. (beg1 (line-beginning-position 2))
  5130. (dc1 (downcase (match-string 2)))
  5131. (dc3 (downcase (match-string 3)))
  5132. end end1 quoting block-type ovl)
  5133. (cond
  5134. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5135. ;; a single line of backend-specific content
  5136. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5137. (remove-text-properties (match-beginning 0) (match-end 0)
  5138. '(display t invisible t intangible t))
  5139. (add-text-properties (match-beginning 1) (match-end 3)
  5140. '(font-lock-fontified t face org-meta-line))
  5141. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5142. '(font-lock-fontified t face org-block))
  5143. ; for backend-specific code
  5144. t)
  5145. ((and (match-end 4) (equal dc3 "+begin"))
  5146. ;; Truly a block
  5147. (setq block-type (downcase (match-string 5))
  5148. quoting (member block-type org-protecting-blocks))
  5149. (when (re-search-forward
  5150. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5151. nil t) ;; on purpose, we look further than LIMIT
  5152. (setq end (min (point-max) (match-end 0))
  5153. end1 (min (point-max) (1- (match-beginning 0))))
  5154. (setq block-end (match-beginning 0))
  5155. (when quoting
  5156. (remove-text-properties beg end
  5157. '(display t invisible t intangible t)))
  5158. (add-text-properties
  5159. beg end
  5160. '(font-lock-fontified t font-lock-multiline t))
  5161. (add-text-properties beg beg1 '(face org-meta-line))
  5162. (add-text-properties end1 (min (point-max) (1+ end))
  5163. '(face org-meta-line)) ; for end_src
  5164. (cond
  5165. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5166. (org-src-font-lock-fontify-block lang block-start block-end)
  5167. ;; remove old background overlays
  5168. (mapc (lambda (ov)
  5169. (if (eq (overlay-get ov 'face) 'org-block-background)
  5170. (delete-overlay ov)))
  5171. (overlays-at (/ (+ beg1 block-end) 2)))
  5172. ;; add a background overlay
  5173. (setq ovl (make-overlay beg1 block-end))
  5174. (overlay-put ovl 'face 'org-block-background)
  5175. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5176. (quoting
  5177. (add-text-properties beg1 (min (point-max) (1+ end1))
  5178. '(face org-block))) ; end of source block
  5179. ((not org-fontify-quote-and-verse-blocks))
  5180. ((string= block-type "quote")
  5181. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5182. ((string= block-type "verse")
  5183. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5184. (add-text-properties beg beg1 '(face org-block-begin-line))
  5185. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5186. '(face org-block-end-line))
  5187. t))
  5188. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5189. (add-text-properties
  5190. beg (match-end 3)
  5191. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5192. '(font-lock-fontified t invisible t)
  5193. '(font-lock-fontified t face org-document-info-keyword)))
  5194. (add-text-properties
  5195. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5196. (if (string-equal dc1 "+title:")
  5197. '(font-lock-fontified t face org-document-title)
  5198. '(font-lock-fontified t face org-document-info))))
  5199. ((or (equal dc1 "+results")
  5200. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5201. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5202. "+call:" "+header:" "+headers:" "+name:"))
  5203. (and (match-end 4) (equal dc3 "+attr")))
  5204. (add-text-properties
  5205. beg (match-end 0)
  5206. '(font-lock-fontified t face org-meta-line))
  5207. t)
  5208. ((member dc3 '(" " ""))
  5209. (add-text-properties
  5210. beg (match-end 0)
  5211. '(font-lock-fontified t face font-lock-comment-face)))
  5212. ((not (member (char-after beg) '(?\ ?\t)))
  5213. ;; just any other in-buffer setting, but not indented
  5214. (add-text-properties
  5215. beg (match-end 0)
  5216. '(font-lock-fontified t face org-meta-line))
  5217. t)
  5218. (t nil))))))
  5219. (defun org-activate-angle-links (limit)
  5220. "Run through the buffer and add overlays to links."
  5221. (if (and (re-search-forward org-angle-link-re limit t)
  5222. (not (org-in-src-block-p)))
  5223. (progn
  5224. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5225. (add-text-properties (match-beginning 0) (match-end 0)
  5226. (list 'mouse-face 'highlight
  5227. 'keymap org-mouse-map))
  5228. (org-rear-nonsticky-at (match-end 0))
  5229. t)))
  5230. (defun org-activate-footnote-links (limit)
  5231. "Run through the buffer and add overlays to footnotes."
  5232. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5233. (when fn
  5234. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5235. (org-remove-flyspell-overlays-in beg end)
  5236. (add-text-properties beg end
  5237. (list 'mouse-face 'highlight
  5238. 'keymap org-mouse-map
  5239. 'help-echo
  5240. (if (= (point-at-bol) beg)
  5241. "Footnote definition"
  5242. "Footnote reference")
  5243. 'font-lock-fontified t
  5244. 'font-lock-multiline t
  5245. 'face 'org-footnote))))))
  5246. (defun org-activate-bracket-links (limit)
  5247. "Run through the buffer and add overlays to bracketed links."
  5248. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5249. (not (org-in-src-block-p)))
  5250. (let* ((hl (org-match-string-no-properties 1))
  5251. (help (concat "LINK: " hl))
  5252. ;; FIXME: Above we should remove the escapes. But that
  5253. ;; requires another match, protecting match data, a lot
  5254. ;; of overhead for font-lock.
  5255. (ip (org-maybe-intangible
  5256. (list 'invisible 'org-link
  5257. 'keymap org-mouse-map 'mouse-face 'highlight
  5258. 'font-lock-multiline t 'help-echo help
  5259. 'htmlize-link `(:uri ,hl))))
  5260. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5261. 'font-lock-multiline t 'help-echo help
  5262. 'htmlize-link `(:uri ,hl))))
  5263. ;; We need to remove the invisible property here. Table narrowing
  5264. ;; may have made some of this invisible.
  5265. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5266. (remove-text-properties (match-beginning 0) (match-end 0)
  5267. '(invisible nil))
  5268. (if (match-end 3)
  5269. (progn
  5270. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5271. (org-rear-nonsticky-at (match-beginning 3))
  5272. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5273. (org-rear-nonsticky-at (match-end 3))
  5274. (add-text-properties (match-end 3) (match-end 0) ip)
  5275. (org-rear-nonsticky-at (match-end 0)))
  5276. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5277. (org-rear-nonsticky-at (match-beginning 1))
  5278. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5279. (org-rear-nonsticky-at (match-end 1))
  5280. (add-text-properties (match-end 1) (match-end 0) ip)
  5281. (org-rear-nonsticky-at (match-end 0)))
  5282. t)))
  5283. (defun org-activate-dates (limit)
  5284. "Run through the buffer and add overlays to dates."
  5285. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5286. (not (equal (char-before (match-beginning 0)) 91)))
  5287. (progn
  5288. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5289. (add-text-properties (match-beginning 0) (match-end 0)
  5290. (list 'mouse-face 'highlight
  5291. 'keymap org-mouse-map))
  5292. (org-rear-nonsticky-at (match-end 0))
  5293. (when org-display-custom-times
  5294. (if (match-end 3)
  5295. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5296. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5297. t)))
  5298. (defvar org-target-link-regexp nil
  5299. "Regular expression matching radio targets in plain text.")
  5300. (make-variable-buffer-local 'org-target-link-regexp)
  5301. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5302. "Regular expression matching a link target.")
  5303. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5304. "Regular expression matching a radio target.")
  5305. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5306. "Regular expression matching any target.")
  5307. (defun org-activate-target-links (limit)
  5308. "Run through the buffer and add overlays to target matches."
  5309. (when org-target-link-regexp
  5310. (let ((case-fold-search t))
  5311. (if (re-search-forward org-target-link-regexp limit t)
  5312. (progn
  5313. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5314. (add-text-properties (match-beginning 0) (match-end 0)
  5315. (list 'mouse-face 'highlight
  5316. 'keymap org-mouse-map
  5317. 'help-echo "Radio target link"
  5318. 'org-linked-text t))
  5319. (org-rear-nonsticky-at (match-end 0))
  5320. t)))))
  5321. (defun org-update-radio-target-regexp ()
  5322. "Find all radio targets in this file and update the regular expression."
  5323. (interactive)
  5324. (when (memq 'radio org-activate-links)
  5325. (setq org-target-link-regexp
  5326. (org-make-target-link-regexp (org-all-targets 'radio)))
  5327. (org-restart-font-lock)))
  5328. (defun org-hide-wide-columns (limit)
  5329. (let (s e)
  5330. (setq s (text-property-any (point) (or limit (point-max))
  5331. 'org-cwidth t))
  5332. (when s
  5333. (setq e (next-single-property-change s 'org-cwidth))
  5334. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5335. (goto-char e)
  5336. t)))
  5337. (defvar org-latex-and-related-regexp nil
  5338. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5339. (defvar org-match-substring-regexp)
  5340. (defvar org-match-substring-with-braces-regexp)
  5341. (defun org-compute-latex-and-related-regexp ()
  5342. "Compute regular expression for LaTeX, entities and sub/superscript.
  5343. Result depends on variable `org-highlight-latex-and-related'."
  5344. (org-set-local
  5345. 'org-latex-and-related-regexp
  5346. (let* ((re-sub
  5347. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5348. ((eq org-use-sub-superscripts '{})
  5349. (list org-match-substring-with-braces-regexp))
  5350. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5351. (re-latex
  5352. (when (memq 'latex org-highlight-latex-and-related)
  5353. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5354. (delq nil
  5355. (mapcar (lambda (x)
  5356. (and (member (car x) matchers) (nth 1 x)))
  5357. org-latex-regexps)))))
  5358. (re-entities
  5359. (when (memq 'entities org-highlight-latex-and-related)
  5360. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5361. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5362. (defun org-do-latex-and-related (limit)
  5363. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5364. LIMIT bounds the search for syntax to highlight. Stop at first
  5365. highlighted object, if any. Return t if some highlighting was
  5366. done, nil otherwise."
  5367. (when (org-string-nw-p org-latex-and-related-regexp)
  5368. (catch 'found
  5369. (while (re-search-forward org-latex-and-related-regexp limit t)
  5370. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5371. 'face))
  5372. '(org-code org-verbatim underline))
  5373. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5374. '(?_ ?^))
  5375. 1
  5376. 0)))
  5377. (font-lock-prepend-text-property
  5378. (+ offset (match-beginning 0)) (match-end 0)
  5379. 'face 'org-latex-and-related)
  5380. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5381. '(font-lock-multiline t)))
  5382. (throw 'found t)))
  5383. nil)))
  5384. (defun org-restart-font-lock ()
  5385. "Restart `font-lock-mode', to force refontification."
  5386. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5387. (font-lock-mode -1)
  5388. (font-lock-mode 1)))
  5389. (defun org-all-targets (&optional radio)
  5390. "Return a list of all targets in this file.
  5391. When optional argument RADIO is non-nil, only find radio
  5392. targets."
  5393. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5394. (save-excursion
  5395. (goto-char (point-min))
  5396. (while (re-search-forward re nil t)
  5397. ;; Make sure point is really within the object.
  5398. (backward-char)
  5399. (let ((obj (org-element-context)))
  5400. (when (memq (org-element-type obj) '(radio-target target))
  5401. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5402. rtn)))
  5403. (defun org-make-target-link-regexp (targets)
  5404. "Make regular expression matching all strings in TARGETS.
  5405. The regular expression finds the targets also if there is a line break
  5406. between words."
  5407. (and targets
  5408. (concat
  5409. "\\<\\("
  5410. (mapconcat
  5411. (lambda (x)
  5412. (setq x (regexp-quote x))
  5413. (while (string-match " +" x)
  5414. (setq x (replace-match "\\s-+" t t x)))
  5415. x)
  5416. targets
  5417. "\\|")
  5418. "\\)\\>")))
  5419. (defun org-activate-tags (limit)
  5420. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5421. (progn
  5422. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5423. (add-text-properties (match-beginning 1) (match-end 1)
  5424. (list 'mouse-face 'highlight
  5425. 'keymap org-mouse-map))
  5426. (org-rear-nonsticky-at (match-end 1))
  5427. t)))
  5428. (defun org-outline-level ()
  5429. "Compute the outline level of the heading at point.
  5430. If this is called at a normal headline, the level is the number of stars.
  5431. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5432. (save-excursion
  5433. (if (not (condition-case nil
  5434. (org-back-to-heading t)
  5435. (error nil)))
  5436. 0
  5437. (looking-at org-outline-regexp)
  5438. (1- (- (match-end 0) (match-beginning 0))))))
  5439. (defvar org-font-lock-keywords nil)
  5440. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5441. "Regular expression matching a property line.")
  5442. (defvar org-font-lock-hook nil
  5443. "Functions to be called for special font lock stuff.")
  5444. (defvar org-font-lock-set-keywords-hook nil
  5445. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5446. This is called after `org-font-lock-extra-keywords' is defined, but before
  5447. it is installed to be used by font lock. This can be useful if something
  5448. needs to be inserted at a specific position in the font-lock sequence.")
  5449. (defun org-font-lock-hook (limit)
  5450. "Run `org-font-lock-hook' within LIMIT."
  5451. (run-hook-with-args 'org-font-lock-hook limit))
  5452. (defun org-set-font-lock-defaults ()
  5453. "Set font lock defaults for the current buffer."
  5454. (let* ((em org-fontify-emphasized-text)
  5455. (lk org-activate-links)
  5456. (org-font-lock-extra-keywords
  5457. (list
  5458. ;; Call the hook
  5459. '(org-font-lock-hook)
  5460. ;; Headlines
  5461. `(,(if org-fontify-whole-heading-line
  5462. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5463. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5464. (1 (org-get-level-face 1))
  5465. (2 (org-get-level-face 2))
  5466. (3 (org-get-level-face 3)))
  5467. ;; Table lines
  5468. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5469. (1 'org-table t))
  5470. ;; Table internals
  5471. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5472. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5473. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5474. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5475. ;; Drawers
  5476. (list org-drawer-regexp '(0 'org-special-keyword t))
  5477. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5478. ;; Properties
  5479. (list org-property-re
  5480. '(1 'org-special-keyword t)
  5481. '(3 'org-property-value t))
  5482. ;; Links
  5483. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5484. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5485. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5486. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5487. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5488. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5489. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5490. ;; Targets.
  5491. (list org-any-target-regexp '(0 'org-target t))
  5492. ;; Diary sexps.
  5493. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5494. ;; Macro
  5495. '("{{{.+}}}" (0 'org-macro t))
  5496. '(org-hide-wide-columns (0 nil append))
  5497. ;; TODO keyword
  5498. (list (format org-heading-keyword-regexp-format
  5499. org-todo-regexp)
  5500. '(2 (org-get-todo-face 2) t))
  5501. ;; DONE
  5502. (if org-fontify-done-headline
  5503. (list (format org-heading-keyword-regexp-format
  5504. (concat
  5505. "\\(?:"
  5506. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5507. "\\)"))
  5508. '(2 'org-headline-done t))
  5509. nil)
  5510. ;; Priorities
  5511. '(org-font-lock-add-priority-faces)
  5512. ;; Tags
  5513. '(org-font-lock-add-tag-faces)
  5514. ;; Tags groups
  5515. (if (and org-group-tags org-tag-groups-alist)
  5516. (list (concat org-outline-regexp-bol ".+\\(:"
  5517. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5518. ":\\).*$")
  5519. '(1 'org-tag-group prepend)))
  5520. ;; Special keywords
  5521. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5522. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5523. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5524. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5525. ;; Emphasis
  5526. (if em
  5527. (if (featurep 'xemacs)
  5528. '(org-do-emphasis-faces (0 nil append))
  5529. '(org-do-emphasis-faces)))
  5530. ;; Checkboxes
  5531. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5532. 1 'org-checkbox prepend)
  5533. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5534. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5535. (0 (org-get-checkbox-statistics-face) t)))
  5536. ;; Description list items
  5537. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5538. 1 'org-list-dt prepend)
  5539. ;; ARCHIVEd headings
  5540. (list (concat
  5541. org-outline-regexp-bol
  5542. "\\(.*:" org-archive-tag ":.*\\)")
  5543. '(1 'org-archived prepend))
  5544. ;; Specials
  5545. '(org-do-latex-and-related)
  5546. '(org-fontify-entities)
  5547. '(org-raise-scripts)
  5548. ;; Code
  5549. '(org-activate-code (1 'org-code t))
  5550. ;; COMMENT
  5551. (list (format org-heading-keyword-regexp-format
  5552. (concat "\\("
  5553. org-comment-string "\\|" org-quote-string
  5554. "\\)"))
  5555. '(2 'org-special-keyword t))
  5556. ;; Blocks and meta lines
  5557. '(org-fontify-meta-lines-and-blocks)
  5558. )))
  5559. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5560. (run-hooks 'org-font-lock-set-keywords-hook)
  5561. ;; Now set the full font-lock-keywords
  5562. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5563. (org-set-local 'font-lock-defaults
  5564. '(org-font-lock-keywords t nil nil backward-paragraph))
  5565. (kill-local-variable 'font-lock-keywords) nil))
  5566. (defun org-toggle-pretty-entities ()
  5567. "Toggle the composition display of entities as UTF8 characters."
  5568. (interactive)
  5569. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5570. (org-restart-font-lock)
  5571. (if org-pretty-entities
  5572. (message "Entities are now displayed as UTF8 characters")
  5573. (save-restriction
  5574. (widen)
  5575. (org-decompose-region (point-min) (point-max))
  5576. (message "Entities are now displayed as plain text"))))
  5577. (defvar org-custom-properties-overlays nil
  5578. "List of overlays used for custom properties.")
  5579. (make-variable-buffer-local 'org-custom-properties-overlays)
  5580. (defun org-toggle-custom-properties-visibility ()
  5581. "Display or hide properties in `org-custom-properties'."
  5582. (interactive)
  5583. (if org-custom-properties-overlays
  5584. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5585. (setq org-custom-properties-overlays nil))
  5586. (unless (not org-custom-properties)
  5587. (save-excursion
  5588. (save-restriction
  5589. (widen)
  5590. (goto-char (point-min))
  5591. (while (re-search-forward org-property-re nil t)
  5592. (mapc (lambda(p)
  5593. (when (equal p (substring (match-string 1) 1 -1))
  5594. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5595. (overlay-put o 'invisible t)
  5596. (overlay-put o 'org-custom-property t)
  5597. (push o org-custom-properties-overlays))))
  5598. org-custom-properties)))))))
  5599. (defun org-fontify-entities (limit)
  5600. "Find an entity to fontify."
  5601. (let (ee)
  5602. (when org-pretty-entities
  5603. (catch 'match
  5604. (while (re-search-forward
  5605. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5606. limit t)
  5607. (if (and (not (org-in-indented-comment-line))
  5608. (setq ee (org-entity-get (match-string 1)))
  5609. (= (length (nth 6 ee)) 1))
  5610. (let*
  5611. ((end (if (equal (match-string 2) "{}")
  5612. (match-end 2)
  5613. (match-end 1))))
  5614. (add-text-properties
  5615. (match-beginning 0) end
  5616. (list 'font-lock-fontified t))
  5617. (compose-region (match-beginning 0) end
  5618. (nth 6 ee) nil)
  5619. (backward-char 1)
  5620. (throw 'match t))))
  5621. nil))))
  5622. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5623. "Fontify string S like in Org-mode."
  5624. (with-temp-buffer
  5625. (insert s)
  5626. (let ((org-odd-levels-only odd-levels))
  5627. (org-mode)
  5628. (font-lock-fontify-buffer)
  5629. (buffer-string))))
  5630. (defvar org-m nil)
  5631. (defvar org-l nil)
  5632. (defvar org-f nil)
  5633. (defun org-get-level-face (n)
  5634. "Get the right face for match N in font-lock matching of headlines."
  5635. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5636. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5637. (if org-cycle-level-faces
  5638. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5639. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5640. (cond
  5641. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5642. ((eq n 2) org-f)
  5643. (t (if org-level-color-stars-only nil org-f))))
  5644. (defun org-get-todo-face (kwd)
  5645. "Get the right face for a TODO keyword KWD.
  5646. If KWD is a number, get the corresponding match group."
  5647. (if (numberp kwd) (setq kwd (match-string kwd)))
  5648. (or (org-face-from-face-or-color
  5649. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5650. (and (member kwd org-done-keywords) 'org-done)
  5651. 'org-todo))
  5652. (defun org-face-from-face-or-color (context inherit face-or-color)
  5653. "Create a face list that inherits INHERIT, but sets the foreground color.
  5654. When FACE-OR-COLOR is not a string, just return it."
  5655. (if (stringp face-or-color)
  5656. (list :inherit inherit
  5657. (cdr (assoc context org-faces-easy-properties))
  5658. face-or-color)
  5659. face-or-color))
  5660. (defun org-font-lock-add-tag-faces (limit)
  5661. "Add the special tag faces."
  5662. (when (and org-tag-faces org-tags-special-faces-re)
  5663. (while (re-search-forward org-tags-special-faces-re limit t)
  5664. (add-text-properties (match-beginning 1) (match-end 1)
  5665. (list 'face (org-get-tag-face 1)
  5666. 'font-lock-fontified t))
  5667. (backward-char 1))))
  5668. (defun org-font-lock-add-priority-faces (limit)
  5669. "Add the special priority faces."
  5670. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5671. (when (save-match-data (org-at-heading-p))
  5672. (add-text-properties
  5673. (match-beginning 0) (match-end 0)
  5674. (list 'face (or (org-face-from-face-or-color
  5675. 'priority 'org-priority
  5676. (cdr (assoc (char-after (match-beginning 1))
  5677. org-priority-faces)))
  5678. 'org-priority)
  5679. 'font-lock-fontified t)))))
  5680. (defun org-get-tag-face (kwd)
  5681. "Get the right face for a TODO keyword KWD.
  5682. If KWD is a number, get the corresponding match group."
  5683. (if (numberp kwd) (setq kwd (match-string kwd)))
  5684. (or (org-face-from-face-or-color
  5685. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5686. 'org-tag))
  5687. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5688. "Remove fontification and activation overlays from links."
  5689. (font-lock-default-unfontify-region beg end)
  5690. (let* ((buffer-undo-list t)
  5691. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5692. (inhibit-modification-hooks t)
  5693. deactivate-mark buffer-file-name buffer-file-truename)
  5694. (org-decompose-region beg end)
  5695. (remove-text-properties beg end
  5696. '(mouse-face t keymap t org-linked-text t
  5697. invisible t intangible t
  5698. org-no-flyspell t org-emphasis t))
  5699. (org-remove-font-lock-display-properties beg end)))
  5700. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5701. ((raise 0.3) (height 0.7))
  5702. ((raise -0.5))
  5703. ((raise 0.5)))
  5704. "Display properties for showing superscripts and subscripts.")
  5705. (defun org-remove-font-lock-display-properties (beg end)
  5706. "Remove specific display properties that have been added by font lock.
  5707. The will remove the raise properties that are used to show superscripts
  5708. and subscripts."
  5709. (let (next prop)
  5710. (while (< beg end)
  5711. (setq next (next-single-property-change beg 'display nil end)
  5712. prop (get-text-property beg 'display))
  5713. (if (member prop org-script-display)
  5714. (put-text-property beg next 'display nil))
  5715. (setq beg next))))
  5716. (defun org-raise-scripts (limit)
  5717. "Add raise properties to sub/superscripts."
  5718. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5719. (if (re-search-forward
  5720. (if (eq org-use-sub-superscripts t)
  5721. org-match-substring-regexp
  5722. org-match-substring-with-braces-regexp)
  5723. limit t)
  5724. (let* ((pos (point)) table-p comment-p
  5725. (mpos (match-beginning 3))
  5726. (emph-p (get-text-property mpos 'org-emphasis))
  5727. (link-p (get-text-property mpos 'mouse-face))
  5728. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5729. (goto-char (point-at-bol))
  5730. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5731. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5732. (goto-char pos)
  5733. ;; Handle a_b^c
  5734. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5735. (if (or comment-p emph-p link-p keyw-p)
  5736. t
  5737. (put-text-property (match-beginning 3) (match-end 0)
  5738. 'display
  5739. (if (equal (char-after (match-beginning 2)) ?^)
  5740. (nth (if table-p 3 1) org-script-display)
  5741. (nth (if table-p 2 0) org-script-display)))
  5742. (add-text-properties (match-beginning 2) (match-end 2)
  5743. (list 'invisible t
  5744. 'org-dwidth t 'org-dwidth-n 1))
  5745. (if (and (eq (char-after (match-beginning 3)) ?{)
  5746. (eq (char-before (match-end 3)) ?}))
  5747. (progn
  5748. (add-text-properties
  5749. (match-beginning 3) (1+ (match-beginning 3))
  5750. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5751. (add-text-properties
  5752. (1- (match-end 3)) (match-end 3)
  5753. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5754. t)))))
  5755. ;;;; Visibility cycling, including org-goto and indirect buffer
  5756. ;;; Cycling
  5757. (defvar org-cycle-global-status nil)
  5758. (make-variable-buffer-local 'org-cycle-global-status)
  5759. (put 'org-cycle-global-status 'org-state t)
  5760. (defvar org-cycle-subtree-status nil)
  5761. (make-variable-buffer-local 'org-cycle-subtree-status)
  5762. (put 'org-cycle-subtree-status 'org-state t)
  5763. (defvar org-inlinetask-min-level)
  5764. ;;;###autoload
  5765. (defun org-cycle (&optional arg)
  5766. "TAB-action and visibility cycling for Org-mode.
  5767. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5768. is outline visibility cycling, but it also invokes other actions
  5769. in special contexts.
  5770. - When this function is called with a prefix argument, rotate the entire
  5771. buffer through 3 states (global cycling)
  5772. 1. OVERVIEW: Show only top-level headlines.
  5773. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5774. 3. SHOW ALL: Show everything.
  5775. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5776. determined by the variable `org-startup-folded', and by any VISIBILITY
  5777. properties in the buffer.
  5778. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5779. including any drawers.
  5780. - When inside a table, re-align the table and move to the next field.
  5781. - When point is at the beginning of a headline, rotate the subtree started
  5782. by this line through 3 different states (local cycling)
  5783. 1. FOLDED: Only the main headline is shown.
  5784. 2. CHILDREN: The main headline and the direct children are shown.
  5785. From this state, you can move to one of the children
  5786. and zoom in further.
  5787. 3. SUBTREE: Show the entire subtree, including body text.
  5788. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5789. - When point is at the beginning of an empty headline and the variable
  5790. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5791. of the headline by demoting and promoting it to likely levels. This
  5792. speeds up creation document structure by pressing TAB once or several
  5793. times right after creating a new headline.
  5794. - When there is a numeric prefix, go up to a heading with level ARG, do
  5795. a `show-subtree' and return to the previous cursor position. If ARG
  5796. is negative, go up that many levels.
  5797. - When point is not at the beginning of a headline, execute the global
  5798. binding for TAB, which is re-indenting the line. See the option
  5799. `org-cycle-emulate-tab' for details.
  5800. - Special case: if point is at the beginning of the buffer and there is
  5801. no headline in line 1, this function will act as if called with prefix arg
  5802. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5803. But only if also the variable `org-cycle-global-at-bob' is t."
  5804. (interactive "P")
  5805. (org-load-modules-maybe)
  5806. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5807. (and org-cycle-level-after-item/entry-creation
  5808. (or (org-cycle-level)
  5809. (org-cycle-item-indentation))))
  5810. (let* (message-log-max ; Don't populate the *Messages* buffer
  5811. (limit-level
  5812. (or org-cycle-max-level
  5813. (and (boundp 'org-inlinetask-min-level)
  5814. org-inlinetask-min-level
  5815. (1- org-inlinetask-min-level))))
  5816. (nstars (and limit-level
  5817. (if org-odd-levels-only
  5818. (and limit-level (1- (* limit-level 2)))
  5819. limit-level)))
  5820. (org-outline-regexp
  5821. (if (not (derived-mode-p 'org-mode))
  5822. outline-regexp
  5823. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5824. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5825. (not (looking-at org-outline-regexp))))
  5826. (org-cycle-hook
  5827. (if bob-special
  5828. (delq 'org-optimize-window-after-visibility-change
  5829. (copy-sequence org-cycle-hook))
  5830. org-cycle-hook))
  5831. (pos (point)))
  5832. (if (or bob-special (equal arg '(4)))
  5833. ;; special case: use global cycling
  5834. (setq arg t))
  5835. (cond
  5836. ((equal arg '(16))
  5837. (setq last-command 'dummy)
  5838. (org-set-startup-visibility)
  5839. (message "Startup visibility, plus VISIBILITY properties"))
  5840. ((equal arg '(64))
  5841. (show-all)
  5842. (message "Entire buffer visible, including drawers"))
  5843. ;; Table: enter it or move to the next field.
  5844. ((org-at-table-p 'any)
  5845. (if (org-at-table.el-p)
  5846. (message "Use C-c ' to edit table.el tables")
  5847. (if arg (org-table-edit-field t)
  5848. (org-table-justify-field-maybe)
  5849. (call-interactively 'org-table-next-field))))
  5850. ((run-hook-with-args-until-success
  5851. 'org-tab-after-check-for-table-hook))
  5852. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5853. ((eq arg t) (org-cycle-internal-global))
  5854. ;; Drawers: delegate to `org-flag-drawer'.
  5855. ((and org-drawers org-drawer-regexp
  5856. (save-excursion
  5857. (beginning-of-line 1)
  5858. (looking-at org-drawer-regexp)))
  5859. (org-flag-drawer ; toggle block visibility
  5860. (not (get-char-property (match-end 0) 'invisible))))
  5861. ;; Show-subtree, ARG levels up from here.
  5862. ((integerp arg)
  5863. (save-excursion
  5864. (org-back-to-heading)
  5865. (outline-up-heading (if (< arg 0) (- arg)
  5866. (- (funcall outline-level) arg)))
  5867. (org-show-subtree)))
  5868. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5869. ((and (featurep 'org-inlinetask)
  5870. (org-inlinetask-at-task-p)
  5871. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5872. (org-inlinetask-toggle-visibility))
  5873. ((org-try-cdlatex-tab))
  5874. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5875. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5876. (save-excursion (beginning-of-line 1)
  5877. (looking-at org-outline-regexp)))
  5878. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5879. (org-cycle-internal-local))
  5880. ;; From there: TAB emulation and template completion.
  5881. (buffer-read-only (org-back-to-heading))
  5882. ((run-hook-with-args-until-success
  5883. 'org-tab-after-check-for-cycling-hook))
  5884. ((org-try-structure-completion))
  5885. ((run-hook-with-args-until-success
  5886. 'org-tab-before-tab-emulation-hook))
  5887. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5888. (or (not (bolp))
  5889. (not (looking-at org-outline-regexp))))
  5890. (call-interactively (global-key-binding "\t")))
  5891. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5892. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5893. (or (and (eq org-cycle-emulate-tab 'white)
  5894. (= (match-end 0) (point-at-eol)))
  5895. (and (eq org-cycle-emulate-tab 'whitestart)
  5896. (>= (match-end 0) pos))))
  5897. t
  5898. (eq org-cycle-emulate-tab t))
  5899. (call-interactively (global-key-binding "\t")))
  5900. (t (save-excursion
  5901. (org-back-to-heading)
  5902. (org-cycle)))))))
  5903. (defun org-cycle-internal-global ()
  5904. "Do the global cycling action."
  5905. ;; Hack to avoid display of messages for .org attachments in Gnus
  5906. (let (message-log-max ; Don't populate the *Messages* buffer
  5907. (ga (string-match "\\*fontification" (buffer-name))))
  5908. (cond
  5909. ((and (eq last-command this-command)
  5910. (eq org-cycle-global-status 'overview))
  5911. ;; We just created the overview - now do table of contents
  5912. ;; This can be slow in very large buffers, so indicate action
  5913. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5914. (unless ga (message "CONTENTS..."))
  5915. (org-content)
  5916. (unless ga (message "CONTENTS...done"))
  5917. (setq org-cycle-global-status 'contents)
  5918. (run-hook-with-args 'org-cycle-hook 'contents))
  5919. ((and (eq last-command this-command)
  5920. (eq org-cycle-global-status 'contents))
  5921. ;; We just showed the table of contents - now show everything
  5922. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5923. (show-all)
  5924. (unless ga (message "SHOW ALL"))
  5925. (setq org-cycle-global-status 'all)
  5926. (run-hook-with-args 'org-cycle-hook 'all))
  5927. (t
  5928. ;; Default action: go to overview
  5929. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5930. (org-overview)
  5931. (unless ga (message "OVERVIEW"))
  5932. (setq org-cycle-global-status 'overview)
  5933. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5934. (defun org-cycle-internal-local ()
  5935. "Do the local cycling action."
  5936. (let (message-log-max ; Don't populate the *Messages* buffer
  5937. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5938. ;; First, determine end of headline (EOH), end of subtree or item
  5939. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5940. (save-excursion
  5941. (if (org-at-item-p)
  5942. (progn
  5943. (beginning-of-line)
  5944. (setq struct (org-list-struct))
  5945. (setq eoh (point-at-eol))
  5946. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5947. (setq has-children (org-list-has-child-p (point) struct)))
  5948. (org-back-to-heading)
  5949. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5950. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5951. (setq has-children
  5952. (or (save-excursion
  5953. (let ((level (funcall outline-level)))
  5954. (outline-next-heading)
  5955. (and (org-at-heading-p t)
  5956. (> (funcall outline-level) level))))
  5957. (save-excursion
  5958. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5959. ;; Determine end invisible part of buffer (EOL)
  5960. (beginning-of-line 2)
  5961. ;; XEmacs doesn't have `next-single-char-property-change'
  5962. (if (featurep 'xemacs)
  5963. (while (and (not (eobp)) ;; this is like `next-line'
  5964. (get-char-property (1- (point)) 'invisible))
  5965. (beginning-of-line 2))
  5966. (while (and (not (eobp)) ;; this is like `next-line'
  5967. (get-char-property (1- (point)) 'invisible))
  5968. (goto-char (next-single-char-property-change (point) 'invisible))
  5969. (and (eolp) (beginning-of-line 2))))
  5970. (setq eol (point)))
  5971. ;; Find out what to do next and set `this-command'
  5972. (cond
  5973. ((= eos eoh)
  5974. ;; Nothing is hidden behind this heading
  5975. (unless (org-before-first-heading-p)
  5976. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5977. (message "EMPTY ENTRY")
  5978. (setq org-cycle-subtree-status nil)
  5979. (save-excursion
  5980. (goto-char eos)
  5981. (outline-next-heading)
  5982. (if (outline-invisible-p) (org-flag-heading nil))))
  5983. ((and (or (>= eol eos)
  5984. (not (string-match "\\S-" (buffer-substring eol eos))))
  5985. (or has-children
  5986. (not (setq children-skipped
  5987. org-cycle-skip-children-state-if-no-children))))
  5988. ;; Entire subtree is hidden in one line: children view
  5989. (unless (org-before-first-heading-p)
  5990. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5991. (if (org-at-item-p)
  5992. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5993. (org-show-entry)
  5994. (org-with-limited-levels (show-children))
  5995. ;; FIXME: This slows down the func way too much.
  5996. ;; How keep drawers hidden in subtree anyway?
  5997. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5998. ;; (org-cycle-hide-drawers 'subtree))
  5999. ;; Fold every list in subtree to top-level items.
  6000. (when (eq org-cycle-include-plain-lists 'integrate)
  6001. (save-excursion
  6002. (org-back-to-heading)
  6003. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6004. (beginning-of-line 1)
  6005. (let* ((struct (org-list-struct))
  6006. (prevs (org-list-prevs-alist struct))
  6007. (end (org-list-get-bottom-point struct)))
  6008. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6009. (org-list-get-all-items (point) struct prevs))
  6010. (goto-char (if (< end eos) end eos)))))))
  6011. (message "CHILDREN")
  6012. (save-excursion
  6013. (goto-char eos)
  6014. (outline-next-heading)
  6015. (if (outline-invisible-p) (org-flag-heading nil)))
  6016. (setq org-cycle-subtree-status 'children)
  6017. (unless (org-before-first-heading-p)
  6018. (run-hook-with-args 'org-cycle-hook 'children)))
  6019. ((or children-skipped
  6020. (and (eq last-command this-command)
  6021. (eq org-cycle-subtree-status 'children)))
  6022. ;; We just showed the children, or no children are there,
  6023. ;; now show everything.
  6024. (unless (org-before-first-heading-p)
  6025. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6026. (outline-flag-region eoh eos nil)
  6027. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6028. (setq org-cycle-subtree-status 'subtree)
  6029. (unless (org-before-first-heading-p)
  6030. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6031. (t
  6032. ;; Default action: hide the subtree.
  6033. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6034. (outline-flag-region eoh eos t)
  6035. (message "FOLDED")
  6036. (setq org-cycle-subtree-status 'folded)
  6037. (unless (org-before-first-heading-p)
  6038. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6039. ;;;###autoload
  6040. (defun org-global-cycle (&optional arg)
  6041. "Cycle the global visibility. For details see `org-cycle'.
  6042. With \\[universal-argument] prefix arg, switch to startup visibility.
  6043. With a numeric prefix, show all headlines up to that level."
  6044. (interactive "P")
  6045. (let ((org-cycle-include-plain-lists
  6046. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6047. (cond
  6048. ((integerp arg)
  6049. (show-all)
  6050. (hide-sublevels arg)
  6051. (setq org-cycle-global-status 'contents))
  6052. ((equal arg '(4))
  6053. (org-set-startup-visibility)
  6054. (message "Startup visibility, plus VISIBILITY properties."))
  6055. (t
  6056. (org-cycle '(4))))))
  6057. (defun org-set-startup-visibility ()
  6058. "Set the visibility required by startup options and properties."
  6059. (cond
  6060. ((eq org-startup-folded t)
  6061. (org-cycle '(4)))
  6062. ((eq org-startup-folded 'content)
  6063. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  6064. (org-cycle '(4)) (org-cycle '(4)))))
  6065. (unless (eq org-startup-folded 'showeverything)
  6066. (if org-hide-block-startup (org-hide-block-all))
  6067. (org-set-visibility-according-to-property 'no-cleanup)
  6068. (org-cycle-hide-archived-subtrees 'all)
  6069. (org-cycle-hide-drawers 'all)
  6070. (org-cycle-show-empty-lines t)))
  6071. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6072. "Switch subtree visibilities according to :VISIBILITY: property."
  6073. (interactive)
  6074. (let (org-show-entry-below state)
  6075. (save-excursion
  6076. (goto-char (point-min))
  6077. (while (re-search-forward
  6078. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  6079. nil t)
  6080. (setq state (match-string 1))
  6081. (save-excursion
  6082. (org-back-to-heading t)
  6083. (hide-subtree)
  6084. (org-reveal)
  6085. (cond
  6086. ((equal state '("fold" "folded"))
  6087. (hide-subtree))
  6088. ((equal state "children")
  6089. (org-show-hidden-entry)
  6090. (show-children))
  6091. ((equal state "content")
  6092. (save-excursion
  6093. (save-restriction
  6094. (org-narrow-to-subtree)
  6095. (org-content))))
  6096. ((member state '("all" "showall"))
  6097. (show-subtree)))))
  6098. (unless no-cleanup
  6099. (org-cycle-hide-archived-subtrees 'all)
  6100. (org-cycle-hide-drawers 'all)
  6101. (org-cycle-show-empty-lines 'all)))))
  6102. ;; This function uses outline-regexp instead of the more fundamental
  6103. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6104. ;; buffers, where outline-regexp is needed.
  6105. (defun org-overview ()
  6106. "Switch to overview mode, showing only top-level headlines.
  6107. Really, this shows all headlines with level equal or greater than the level
  6108. of the first headline in the buffer. This is important, because if the
  6109. first headline is not level one, then (hide-sublevels 1) gives confusing
  6110. results."
  6111. (interactive)
  6112. (let ((l (org-current-line))
  6113. (level (save-excursion
  6114. (goto-char (point-min))
  6115. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6116. (progn
  6117. (goto-char (match-beginning 0))
  6118. (funcall outline-level))))))
  6119. (and level (hide-sublevels level))
  6120. (recenter '(4))
  6121. (org-goto-line l)))
  6122. (defun org-content (&optional arg)
  6123. "Show all headlines in the buffer, like a table of contents.
  6124. With numerical argument N, show content up to level N."
  6125. (interactive "P")
  6126. (save-excursion
  6127. ;; Visit all headings and show their offspring
  6128. (and (integerp arg) (org-overview))
  6129. (goto-char (point-max))
  6130. (catch 'exit
  6131. (while (and (progn (condition-case nil
  6132. (outline-previous-visible-heading 1)
  6133. (error (goto-char (point-min))))
  6134. t)
  6135. (looking-at org-outline-regexp))
  6136. (if (integerp arg)
  6137. (show-children (1- arg))
  6138. (show-branches))
  6139. (if (bobp) (throw 'exit nil))))))
  6140. (defun org-optimize-window-after-visibility-change (state)
  6141. "Adjust the window after a change in outline visibility.
  6142. This function is the default value of the hook `org-cycle-hook'."
  6143. (when (get-buffer-window (current-buffer))
  6144. (cond
  6145. ((eq state 'content) nil)
  6146. ((eq state 'all) nil)
  6147. ((eq state 'folded) nil)
  6148. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6149. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6150. (defun org-remove-empty-overlays-at (pos)
  6151. "Remove outline overlays that do not contain non-white stuff."
  6152. (mapc
  6153. (lambda (o)
  6154. (and (eq 'outline (overlay-get o 'invisible))
  6155. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6156. (overlay-end o))))
  6157. (delete-overlay o)))
  6158. (overlays-at pos)))
  6159. (defun org-clean-visibility-after-subtree-move ()
  6160. "Fix visibility issues after moving a subtree."
  6161. ;; First, find a reasonable region to look at:
  6162. ;; Start two siblings above, end three below
  6163. (let* ((beg (save-excursion
  6164. (and (org-get-last-sibling)
  6165. (org-get-last-sibling))
  6166. (point)))
  6167. (end (save-excursion
  6168. (and (org-get-next-sibling)
  6169. (org-get-next-sibling)
  6170. (org-get-next-sibling))
  6171. (if (org-at-heading-p)
  6172. (point-at-eol)
  6173. (point))))
  6174. (level (looking-at "\\*+"))
  6175. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6176. (save-excursion
  6177. (save-restriction
  6178. (narrow-to-region beg end)
  6179. (when re
  6180. ;; Properly fold already folded siblings
  6181. (goto-char (point-min))
  6182. (while (re-search-forward re nil t)
  6183. (if (and (not (outline-invisible-p))
  6184. (save-excursion
  6185. (goto-char (point-at-eol)) (outline-invisible-p)))
  6186. (hide-entry))))
  6187. (org-cycle-show-empty-lines 'overview)
  6188. (org-cycle-hide-drawers 'overview)))))
  6189. (defun org-cycle-show-empty-lines (state)
  6190. "Show empty lines above all visible headlines.
  6191. The region to be covered depends on STATE when called through
  6192. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6193. entire buffer covered. Note that an empty line is only shown if there
  6194. are at least `org-cycle-separator-lines' empty lines before the headline."
  6195. (when (not (= org-cycle-separator-lines 0))
  6196. (save-excursion
  6197. (let* ((n (abs org-cycle-separator-lines))
  6198. (re (cond
  6199. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6200. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6201. (t (let ((ns (number-to-string (- n 2))))
  6202. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6203. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6204. beg end b e)
  6205. (cond
  6206. ((memq state '(overview contents t))
  6207. (setq beg (point-min) end (point-max)))
  6208. ((memq state '(children folded))
  6209. (setq beg (point) end (progn (org-end-of-subtree t t)
  6210. (beginning-of-line 2)
  6211. (point)))))
  6212. (when beg
  6213. (goto-char beg)
  6214. (while (re-search-forward re end t)
  6215. (unless (get-char-property (match-end 1) 'invisible)
  6216. (setq e (match-end 1))
  6217. (if (< org-cycle-separator-lines 0)
  6218. (setq b (save-excursion
  6219. (goto-char (match-beginning 0))
  6220. (org-back-over-empty-lines)
  6221. (if (save-excursion
  6222. (goto-char (max (point-min) (1- (point))))
  6223. (org-at-heading-p))
  6224. (1- (point))
  6225. (point))))
  6226. (setq b (match-beginning 1)))
  6227. (outline-flag-region b e nil)))))))
  6228. ;; Never hide empty lines at the end of the file.
  6229. (save-excursion
  6230. (goto-char (point-max))
  6231. (outline-previous-heading)
  6232. (outline-end-of-heading)
  6233. (if (and (looking-at "[ \t\n]+")
  6234. (= (match-end 0) (point-max)))
  6235. (outline-flag-region (point) (match-end 0) nil))))
  6236. (defun org-show-empty-lines-in-parent ()
  6237. "Move to the parent and re-show empty lines before visible headlines."
  6238. (save-excursion
  6239. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6240. (org-cycle-show-empty-lines context))))
  6241. (defun org-files-list ()
  6242. "Return `org-agenda-files' list, plus all open org-mode files.
  6243. This is useful for operations that need to scan all of a user's
  6244. open and agenda-wise Org files."
  6245. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6246. (dolist (buf (buffer-list))
  6247. (with-current-buffer buf
  6248. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6249. (let ((file (expand-file-name (buffer-file-name))))
  6250. (unless (member file files)
  6251. (push file files))))))
  6252. files))
  6253. (defsubst org-entry-beginning-position ()
  6254. "Return the beginning position of the current entry."
  6255. (save-excursion (outline-back-to-heading t) (point)))
  6256. (defsubst org-entry-end-position ()
  6257. "Return the end position of the current entry."
  6258. (save-excursion (outline-next-heading) (point)))
  6259. (defun org-cycle-hide-drawers (state)
  6260. "Re-hide all drawers after a visibility state change."
  6261. (when (and (derived-mode-p 'org-mode)
  6262. (not (memq state '(overview folded contents))))
  6263. (save-excursion
  6264. (let* ((globalp (memq state '(contents all)))
  6265. (beg (if globalp (point-min) (point)))
  6266. (end (if globalp (point-max)
  6267. (if (eq state 'children)
  6268. (save-excursion (outline-next-heading) (point))
  6269. (org-end-of-subtree t)))))
  6270. (goto-char beg)
  6271. (while (re-search-forward org-drawer-regexp end t)
  6272. (org-flag-drawer t))))))
  6273. (defun org-cycle-hide-inline-tasks (state)
  6274. "Re-hide inline task when switching to 'contents visibility state."
  6275. (when (and (eq state 'contents)
  6276. (boundp 'org-inlinetask-min-level)
  6277. org-inlinetask-min-level)
  6278. (hide-sublevels (1- org-inlinetask-min-level))))
  6279. (defun org-flag-drawer (flag)
  6280. "When FLAG is non-nil, hide the drawer we are within.
  6281. Otherwise make it visible."
  6282. (save-excursion
  6283. (beginning-of-line 1)
  6284. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6285. (let ((b (match-end 0)))
  6286. (if (re-search-forward
  6287. "^[ \t]*:END:"
  6288. (save-excursion (outline-next-heading) (point)) t)
  6289. (outline-flag-region b (point-at-eol) flag)
  6290. (error ":END: line missing at position %s" b))))))
  6291. (defun org-subtree-end-visible-p ()
  6292. "Is the end of the current subtree visible?"
  6293. (pos-visible-in-window-p
  6294. (save-excursion (org-end-of-subtree t) (point))))
  6295. (defun org-first-headline-recenter (&optional N)
  6296. "Move cursor to the first headline and recenter the headline.
  6297. Optional argument N means put the headline into the Nth line of the window."
  6298. (goto-char (point-min))
  6299. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6300. (beginning-of-line)
  6301. (recenter (prefix-numeric-value N))))
  6302. ;;; Saving and restoring visibility
  6303. (defun org-outline-overlay-data (&optional use-markers)
  6304. "Return a list of the locations of all outline overlays.
  6305. These are overlays with the `invisible' property value `outline'.
  6306. The return value is a list of cons cells, with start and stop
  6307. positions for each overlay.
  6308. If USE-MARKERS is set, return the positions as markers."
  6309. (let (beg end)
  6310. (save-excursion
  6311. (save-restriction
  6312. (widen)
  6313. (delq nil
  6314. (mapcar (lambda (o)
  6315. (when (eq (overlay-get o 'invisible) 'outline)
  6316. (setq beg (overlay-start o)
  6317. end (overlay-end o))
  6318. (and beg end (> end beg)
  6319. (if use-markers
  6320. (cons (move-marker (make-marker) beg)
  6321. (move-marker (make-marker) end))
  6322. (cons beg end)))))
  6323. (overlays-in (point-min) (point-max))))))))
  6324. (defun org-set-outline-overlay-data (data)
  6325. "Create visibility overlays for all positions in DATA.
  6326. DATA should have been made by `org-outline-overlay-data'."
  6327. (let (o)
  6328. (save-excursion
  6329. (save-restriction
  6330. (widen)
  6331. (show-all)
  6332. (mapc (lambda (c)
  6333. (outline-flag-region (car c) (cdr c) t))
  6334. data)))))
  6335. ;;; Folding of blocks
  6336. (defvar org-hide-block-overlays nil
  6337. "Overlays hiding blocks.")
  6338. (make-variable-buffer-local 'org-hide-block-overlays)
  6339. (defun org-block-map (function &optional start end)
  6340. "Call FUNCTION at the head of all source blocks in the current buffer.
  6341. Optional arguments START and END can be used to limit the range."
  6342. (let ((start (or start (point-min)))
  6343. (end (or end (point-max))))
  6344. (save-excursion
  6345. (goto-char start)
  6346. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6347. (save-excursion
  6348. (save-match-data
  6349. (goto-char (match-beginning 0))
  6350. (funcall function)))))))
  6351. (defun org-hide-block-toggle-all ()
  6352. "Toggle the visibility of all blocks in the current buffer."
  6353. (org-block-map #'org-hide-block-toggle))
  6354. (defun org-hide-block-all ()
  6355. "Fold all blocks in the current buffer."
  6356. (interactive)
  6357. (org-show-block-all)
  6358. (org-block-map #'org-hide-block-toggle-maybe))
  6359. (defun org-show-block-all ()
  6360. "Unfold all blocks in the current buffer."
  6361. (interactive)
  6362. (mapc 'delete-overlay org-hide-block-overlays)
  6363. (setq org-hide-block-overlays nil))
  6364. (defun org-hide-block-toggle-maybe ()
  6365. "Toggle visibility of block at point."
  6366. (interactive)
  6367. (let ((case-fold-search t))
  6368. (if (save-excursion
  6369. (beginning-of-line 1)
  6370. (looking-at org-block-regexp))
  6371. (progn (org-hide-block-toggle)
  6372. t) ;; to signal that we took action
  6373. nil))) ;; to signal that we did not
  6374. (defun org-hide-block-toggle (&optional force)
  6375. "Toggle the visibility of the current block."
  6376. (interactive)
  6377. (save-excursion
  6378. (beginning-of-line)
  6379. (if (re-search-forward org-block-regexp nil t)
  6380. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6381. (end (match-end 0)) ;; end of entire body
  6382. ov)
  6383. (if (memq t (mapcar (lambda (overlay)
  6384. (eq (overlay-get overlay 'invisible)
  6385. 'org-hide-block))
  6386. (overlays-at start)))
  6387. (if (or (not force) (eq force 'off))
  6388. (mapc (lambda (ov)
  6389. (when (member ov org-hide-block-overlays)
  6390. (setq org-hide-block-overlays
  6391. (delq ov org-hide-block-overlays)))
  6392. (when (eq (overlay-get ov 'invisible)
  6393. 'org-hide-block)
  6394. (delete-overlay ov)))
  6395. (overlays-at start)))
  6396. (setq ov (make-overlay start end))
  6397. (overlay-put ov 'invisible 'org-hide-block)
  6398. ;; make the block accessible to isearch
  6399. (overlay-put
  6400. ov 'isearch-open-invisible
  6401. (lambda (ov)
  6402. (when (member ov org-hide-block-overlays)
  6403. (setq org-hide-block-overlays
  6404. (delq ov org-hide-block-overlays)))
  6405. (when (eq (overlay-get ov 'invisible)
  6406. 'org-hide-block)
  6407. (delete-overlay ov))))
  6408. (push ov org-hide-block-overlays)))
  6409. (error "Not looking at a source block"))))
  6410. ;; org-tab-after-check-for-cycling-hook
  6411. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6412. ;; Remove overlays when changing major mode
  6413. (add-hook 'org-mode-hook
  6414. (lambda () (org-add-hook 'change-major-mode-hook
  6415. 'org-show-block-all 'append 'local)))
  6416. ;;; Org-goto
  6417. (defvar org-goto-window-configuration nil)
  6418. (defvar org-goto-marker nil)
  6419. (defvar org-goto-map)
  6420. (defun org-goto-map ()
  6421. "Set the keymap `org-goto'."
  6422. (setq org-goto-map
  6423. (let ((map (make-sparse-keymap)))
  6424. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6425. mouse-drag-region universal-argument org-occur))
  6426. cmd)
  6427. (while (setq cmd (pop cmds))
  6428. (substitute-key-definition cmd cmd map global-map)))
  6429. (suppress-keymap map)
  6430. (org-defkey map "\C-m" 'org-goto-ret)
  6431. (org-defkey map [(return)] 'org-goto-ret)
  6432. (org-defkey map [(left)] 'org-goto-left)
  6433. (org-defkey map [(right)] 'org-goto-right)
  6434. (org-defkey map [(control ?g)] 'org-goto-quit)
  6435. (org-defkey map "\C-i" 'org-cycle)
  6436. (org-defkey map [(tab)] 'org-cycle)
  6437. (org-defkey map [(down)] 'outline-next-visible-heading)
  6438. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6439. (if org-goto-auto-isearch
  6440. (if (fboundp 'define-key-after)
  6441. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6442. nil)
  6443. (org-defkey map "q" 'org-goto-quit)
  6444. (org-defkey map "n" 'outline-next-visible-heading)
  6445. (org-defkey map "p" 'outline-previous-visible-heading)
  6446. (org-defkey map "f" 'outline-forward-same-level)
  6447. (org-defkey map "b" 'outline-backward-same-level)
  6448. (org-defkey map "u" 'outline-up-heading))
  6449. (org-defkey map "/" 'org-occur)
  6450. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6451. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6452. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6453. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6454. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6455. map)))
  6456. (defconst org-goto-help
  6457. "Browse buffer copy, to find location or copy text.%s
  6458. RET=jump to location C-g=quit and return to previous location
  6459. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6460. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6461. (defun org-goto (&optional alternative-interface)
  6462. "Look up a different location in the current file, keeping current visibility.
  6463. When you want look-up or go to a different location in a
  6464. document, the fastest way is often to fold the entire buffer and
  6465. then dive into the tree. This method has the disadvantage, that
  6466. the previous location will be folded, which may not be what you
  6467. want.
  6468. This command works around this by showing a copy of the current
  6469. buffer in an indirect buffer, in overview mode. You can dive
  6470. into the tree in that copy, use org-occur and incremental search
  6471. to find a location. When pressing RET or `Q', the command
  6472. returns to the original buffer in which the visibility is still
  6473. unchanged. After RET it will also jump to the location selected
  6474. in the indirect buffer and expose the headline hierarchy above.
  6475. With a prefix argument, use the alternative interface: e.g. if
  6476. `org-goto-interface' is 'outline use 'outline-path-completion."
  6477. (interactive "P")
  6478. (org-goto-map)
  6479. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6480. (org-refile-use-outline-path t)
  6481. (org-refile-target-verify-function nil)
  6482. (interface
  6483. (if (not alternative-interface)
  6484. org-goto-interface
  6485. (if (eq org-goto-interface 'outline)
  6486. 'outline-path-completion
  6487. 'outline)))
  6488. (org-goto-start-pos (point))
  6489. (selected-point
  6490. (if (eq interface 'outline)
  6491. (car (org-get-location (current-buffer) org-goto-help))
  6492. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6493. (org-refile-check-position pa)
  6494. (nth 3 pa)))))
  6495. (if selected-point
  6496. (progn
  6497. (org-mark-ring-push org-goto-start-pos)
  6498. (goto-char selected-point)
  6499. (if (or (outline-invisible-p) (org-invisible-p2))
  6500. (org-show-context 'org-goto)))
  6501. (message "Quit"))))
  6502. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6503. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6504. (defvar org-goto-local-auto-isearch-map) ; defined below
  6505. (defun org-get-location (buf help)
  6506. "Let the user select a location in the Org-mode buffer BUF.
  6507. This function uses a recursive edit. It returns the selected position
  6508. or nil."
  6509. (org-no-popups
  6510. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6511. (isearch-hide-immediately nil)
  6512. (isearch-search-fun-function
  6513. (lambda () 'org-goto-local-search-headings))
  6514. (org-goto-selected-point org-goto-exit-command))
  6515. (save-excursion
  6516. (save-window-excursion
  6517. (delete-other-windows)
  6518. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6519. (org-pop-to-buffer-same-window
  6520. (condition-case nil
  6521. (make-indirect-buffer (current-buffer) "*org-goto*")
  6522. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6523. (with-output-to-temp-buffer "*Org Help*"
  6524. (princ (format help (if org-goto-auto-isearch
  6525. " Just type for auto-isearch."
  6526. " n/p/f/b/u to navigate, q to quit."))))
  6527. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6528. (setq buffer-read-only nil)
  6529. (let ((org-startup-truncated t)
  6530. (org-startup-folded nil)
  6531. (org-startup-align-all-tables nil))
  6532. (org-mode)
  6533. (org-overview))
  6534. (setq buffer-read-only t)
  6535. (if (and (boundp 'org-goto-start-pos)
  6536. (integer-or-marker-p org-goto-start-pos))
  6537. (let ((org-show-hierarchy-above t)
  6538. (org-show-siblings t)
  6539. (org-show-following-heading t))
  6540. (goto-char org-goto-start-pos)
  6541. (and (outline-invisible-p) (org-show-context)))
  6542. (goto-char (point-min)))
  6543. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6544. (message "Select location and press RET")
  6545. (use-local-map org-goto-map)
  6546. (recursive-edit)))
  6547. (kill-buffer "*org-goto*")
  6548. (cons org-goto-selected-point org-goto-exit-command))))
  6549. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6550. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6551. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6552. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6553. (defun org-goto-local-search-headings (string bound noerror)
  6554. "Search and make sure that any matches are in headlines."
  6555. (catch 'return
  6556. (while (if isearch-forward
  6557. (search-forward string bound noerror)
  6558. (search-backward string bound noerror))
  6559. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6560. (and (member :headline context)
  6561. (not (member :tags context))))
  6562. (throw 'return (point))))))
  6563. (defun org-goto-local-auto-isearch ()
  6564. "Start isearch."
  6565. (interactive)
  6566. (goto-char (point-min))
  6567. (let ((keys (this-command-keys)))
  6568. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6569. (isearch-mode t)
  6570. (isearch-process-search-char (string-to-char keys)))))
  6571. (defun org-goto-ret (&optional arg)
  6572. "Finish `org-goto' by going to the new location."
  6573. (interactive "P")
  6574. (setq org-goto-selected-point (point)
  6575. org-goto-exit-command 'return)
  6576. (throw 'exit nil))
  6577. (defun org-goto-left ()
  6578. "Finish `org-goto' by going to the new location."
  6579. (interactive)
  6580. (if (org-at-heading-p)
  6581. (progn
  6582. (beginning-of-line 1)
  6583. (setq org-goto-selected-point (point)
  6584. org-goto-exit-command 'left)
  6585. (throw 'exit nil))
  6586. (error "Not on a heading")))
  6587. (defun org-goto-right ()
  6588. "Finish `org-goto' by going to the new location."
  6589. (interactive)
  6590. (if (org-at-heading-p)
  6591. (progn
  6592. (setq org-goto-selected-point (point)
  6593. org-goto-exit-command 'right)
  6594. (throw 'exit nil))
  6595. (error "Not on a heading")))
  6596. (defun org-goto-quit ()
  6597. "Finish `org-goto' without cursor motion."
  6598. (interactive)
  6599. (setq org-goto-selected-point nil)
  6600. (setq org-goto-exit-command 'quit)
  6601. (throw 'exit nil))
  6602. ;;; Indirect buffer display of subtrees
  6603. (defvar org-indirect-dedicated-frame nil
  6604. "This is the frame being used for indirect tree display.")
  6605. (defvar org-last-indirect-buffer nil)
  6606. (defun org-tree-to-indirect-buffer (&optional arg)
  6607. "Create indirect buffer and narrow it to current subtree.
  6608. With a numerical prefix ARG, go up to this level and then take that tree.
  6609. If ARG is negative, go up that many levels.
  6610. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6611. indirect buffer previously made with this command, to avoid proliferation of
  6612. indirect buffers. However, when you call the command with a \
  6613. \\[universal-argument] prefix, or
  6614. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6615. is kept so that you can work with several indirect buffers at the same time.
  6616. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6617. \\[universal-argument] prefix also
  6618. requests that a new frame be made for the new buffer, so that the dedicated
  6619. frame is not changed."
  6620. (interactive "P")
  6621. (let ((cbuf (current-buffer))
  6622. (cwin (selected-window))
  6623. (pos (point))
  6624. beg end level heading ibuf)
  6625. (save-excursion
  6626. (org-back-to-heading t)
  6627. (when (numberp arg)
  6628. (setq level (org-outline-level))
  6629. (if (< arg 0) (setq arg (+ level arg)))
  6630. (while (> (setq level (org-outline-level)) arg)
  6631. (org-up-heading-safe)))
  6632. (setq beg (point)
  6633. heading (org-get-heading))
  6634. (org-end-of-subtree t t)
  6635. (if (org-at-heading-p) (backward-char 1))
  6636. (setq end (point)))
  6637. (if (and (buffer-live-p org-last-indirect-buffer)
  6638. (not (eq org-indirect-buffer-display 'new-frame))
  6639. (not arg))
  6640. (kill-buffer org-last-indirect-buffer))
  6641. (setq ibuf (org-get-indirect-buffer cbuf)
  6642. org-last-indirect-buffer ibuf)
  6643. (cond
  6644. ((or (eq org-indirect-buffer-display 'new-frame)
  6645. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6646. (select-frame (make-frame))
  6647. (delete-other-windows)
  6648. (org-pop-to-buffer-same-window ibuf)
  6649. (org-set-frame-title heading))
  6650. ((eq org-indirect-buffer-display 'dedicated-frame)
  6651. (raise-frame
  6652. (select-frame (or (and org-indirect-dedicated-frame
  6653. (frame-live-p org-indirect-dedicated-frame)
  6654. org-indirect-dedicated-frame)
  6655. (setq org-indirect-dedicated-frame (make-frame)))))
  6656. (delete-other-windows)
  6657. (org-pop-to-buffer-same-window ibuf)
  6658. (org-set-frame-title (concat "Indirect: " heading)))
  6659. ((eq org-indirect-buffer-display 'current-window)
  6660. (org-pop-to-buffer-same-window ibuf))
  6661. ((eq org-indirect-buffer-display 'other-window)
  6662. (pop-to-buffer ibuf))
  6663. (t (error "Invalid value")))
  6664. (if (featurep 'xemacs)
  6665. (save-excursion (org-mode) (turn-on-font-lock)))
  6666. (narrow-to-region beg end)
  6667. (show-all)
  6668. (goto-char pos)
  6669. (run-hook-with-args 'org-cycle-hook 'all)
  6670. (and (window-live-p cwin) (select-window cwin))))
  6671. (defun org-get-indirect-buffer (&optional buffer)
  6672. (setq buffer (or buffer (current-buffer)))
  6673. (let ((n 1) (base (buffer-name buffer)) bname)
  6674. (while (buffer-live-p
  6675. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6676. (setq n (1+ n)))
  6677. (condition-case nil
  6678. (make-indirect-buffer buffer bname 'clone)
  6679. (error (make-indirect-buffer buffer bname)))))
  6680. (defun org-set-frame-title (title)
  6681. "Set the title of the current frame to the string TITLE."
  6682. ;; FIXME: how to name a single frame in XEmacs???
  6683. (unless (featurep 'xemacs)
  6684. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6685. ;;;; Structure editing
  6686. ;;; Inserting headlines
  6687. (defun org-previous-line-empty-p (&optional next)
  6688. "Is the previous line a blank line?
  6689. When NEXT is non-nil, check the next line instead."
  6690. (save-excursion
  6691. (and (not (bobp))
  6692. (or (beginning-of-line (if next 2 0)) t)
  6693. (save-match-data
  6694. (looking-at "[ \t]*$")))))
  6695. (defun org-insert-heading (&optional arg invisible-ok)
  6696. "Insert a new heading or item with same depth at point.
  6697. If point is in a plain list and ARG is nil, create a new list item.
  6698. With one universal prefix argument, insert a heading even in lists.
  6699. With two universal prefix arguments, insert the heading at the end
  6700. of the parent subtree.
  6701. If point is at the beginning of a headline, insert a sibling before
  6702. the current headline. If point is not at the beginning, split the line
  6703. and create a new headline with the text in the current line after point
  6704. \(see `org-M-RET-may-split-line' on how to modify this behavior).
  6705. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6706. This is important for non-interactive uses of the command."
  6707. (interactive "P")
  6708. (cond
  6709. ((or (= (buffer-size) 0)
  6710. (and (not (save-excursion
  6711. (and (ignore-errors (org-back-to-heading invisible-ok))
  6712. (org-at-heading-p))))
  6713. (or arg (not (org-in-item-p)))))
  6714. (insert
  6715. (if (org-previous-line-empty-p) "" "\n")
  6716. (if (org-in-src-block-p) ",* " "* "))
  6717. (run-hooks 'org-insert-heading-hook))
  6718. ((or arg
  6719. org-insert-heading-respect-content
  6720. (not (org-insert-item
  6721. (save-excursion
  6722. (beginning-of-line)
  6723. (looking-at org-list-full-item-re)
  6724. (match-string 3)))))
  6725. (let (begn endn)
  6726. (when (org-buffer-narrowed-p)
  6727. (setq begn (point-min) endn (point-max))
  6728. (widen))
  6729. (let* ((empty-line-p nil)
  6730. (eops (equal arg '(16))) ; insert at end of parent subtree
  6731. (org-insert-heading-respect-content
  6732. (or (not (null arg)) org-insert-heading-respect-content))
  6733. (level nil)
  6734. (on-heading (org-at-heading-p))
  6735. (on-empty-line
  6736. (save-excursion (beginning-of-line 1) (looking-at "^\\s-*$")))
  6737. (head (save-excursion
  6738. (condition-case nil
  6739. (progn
  6740. (org-back-to-heading invisible-ok)
  6741. (when (and (not on-heading)
  6742. (featurep 'org-inlinetask)
  6743. (integerp org-inlinetask-min-level)
  6744. (>= (length (match-string 0))
  6745. org-inlinetask-min-level))
  6746. ;; Find a heading level before the inline task
  6747. (while (and (setq level (org-up-heading-safe))
  6748. (>= level org-inlinetask-min-level)))
  6749. (if (org-at-heading-p)
  6750. (org-back-to-heading invisible-ok)
  6751. (error "This should not happen")))
  6752. (unless (and (save-excursion
  6753. (save-match-data
  6754. (org-backward-heading-same-level 1 invisible-ok))
  6755. (= (point) (match-beginning 0)))
  6756. (not (org-previous-line-empty-p t)))
  6757. (setq empty-line-p (org-previous-line-empty-p)))
  6758. (match-string 0))
  6759. (error "* "))))
  6760. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6761. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6762. pos hide-previous previous-pos)
  6763. (if ;; At the beginning of a heading, open a new line for insertiong
  6764. (and (bolp) (org-at-heading-p)
  6765. (not eops)
  6766. (or (bobp)
  6767. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6768. (open-line (if blank 2 1))
  6769. (save-excursion
  6770. (setq previous-pos (point-at-bol))
  6771. (end-of-line)
  6772. (setq hide-previous (outline-invisible-p)))
  6773. (and org-insert-heading-respect-content
  6774. (save-excursion
  6775. (while (outline-invisible-p)
  6776. (org-show-subtree)
  6777. (org-up-heading-safe))))
  6778. (let ((split
  6779. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6780. (save-excursion
  6781. (let ((p (point)))
  6782. (goto-char (point-at-bol))
  6783. (and (looking-at org-complex-heading-regexp)
  6784. (match-beginning 4)
  6785. (> p (match-beginning 4)))))))
  6786. tags pos)
  6787. (cond
  6788. ;; Insert a new line, possibly at end of parent subtree
  6789. ((and (not arg) (not on-heading) (not on-empty-line)
  6790. (not (save-excursion
  6791. (beginning-of-line 1)
  6792. (looking-at org-list-full-item-re))))
  6793. (beginning-of-line 1))
  6794. (org-insert-heading-respect-content
  6795. (if (not eops)
  6796. (progn
  6797. (org-end-of-subtree nil t)
  6798. (and (looking-at "^\\*") (backward-char 1))
  6799. (while (and (not (bobp))
  6800. ;; Don't delete spaces in empty headlines
  6801. (not (looking-back org-outline-regexp))
  6802. (member (char-before) '(?\ ?\t ?\n)))
  6803. (backward-delete-char 1)))
  6804. (let ((p (point)))
  6805. (org-up-heading-safe)
  6806. (if (= p (point))
  6807. (goto-char (point-max))
  6808. (org-end-of-subtree nil t))))
  6809. (when (featurep 'org-inlinetask)
  6810. (while (and (not (eobp))
  6811. (looking-at "\\(\\*+\\)[ \t]+")
  6812. (>= (length (match-string 1))
  6813. org-inlinetask-min-level))
  6814. (org-end-of-subtree nil t)))
  6815. (or (bolp) (newline))
  6816. (or (org-previous-line-empty-p)
  6817. (and blank (newline)))
  6818. (if (or empty-line-p eops) (open-line 1)))
  6819. ;; Insert a headling containing text after point
  6820. ((org-at-heading-p)
  6821. (when hide-previous
  6822. (show-children)
  6823. (org-show-entry))
  6824. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6825. (setq tags (and (match-end 2) (match-string 2)))
  6826. (and (match-end 1)
  6827. (delete-region (match-beginning 1) (match-end 1)))
  6828. (setq pos (point-at-bol))
  6829. (or split (end-of-line 1))
  6830. (delete-horizontal-space)
  6831. (if (string-match "\\`\\*+\\'"
  6832. (buffer-substring (point-at-bol) (point)))
  6833. (insert " "))
  6834. (newline (if blank 2 1))
  6835. (when tags
  6836. (save-excursion
  6837. (goto-char pos)
  6838. (end-of-line 1)
  6839. (insert " " tags)
  6840. (org-set-tags nil 'align))))
  6841. (t
  6842. (or split (end-of-line 1))
  6843. (newline (cond ((and blank (not on-empty-line)) 2)
  6844. (blank 1)
  6845. (on-empty-line 0) (t 1)))))))
  6846. (insert head) (just-one-space)
  6847. (setq pos (point))
  6848. (end-of-line 1)
  6849. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6850. (when (and org-insert-heading-respect-content hide-previous)
  6851. (save-excursion
  6852. (goto-char previous-pos)
  6853. (hide-subtree)))
  6854. (when (and begn endn)
  6855. (narrow-to-region (min (point) begn) (max (point) endn)))
  6856. (run-hooks 'org-insert-heading-hook))))))
  6857. (defun org-get-heading (&optional no-tags no-todo)
  6858. "Return the heading of the current entry, without the stars.
  6859. When NO-TAGS is non-nil, don't include tags.
  6860. When NO-TODO is non-nil, don't include TODO keywords."
  6861. (save-excursion
  6862. (org-back-to-heading t)
  6863. (cond
  6864. ((and no-tags no-todo)
  6865. (looking-at org-complex-heading-regexp)
  6866. (match-string 4))
  6867. (no-tags
  6868. (looking-at (concat org-outline-regexp
  6869. "\\(.*?\\)"
  6870. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6871. (match-string 1))
  6872. (no-todo
  6873. (looking-at org-todo-line-regexp)
  6874. (match-string 3))
  6875. (t (looking-at org-heading-regexp)
  6876. (match-string 2)))))
  6877. (defvar orgstruct-mode) ; defined below
  6878. (defun org-heading-components ()
  6879. "Return the components of the current heading.
  6880. This is a list with the following elements:
  6881. - the level as an integer
  6882. - the reduced level, different if `org-odd-levels-only' is set.
  6883. - the TODO keyword, or nil
  6884. - the priority character, like ?A, or nil if no priority is given
  6885. - the headline text itself, or the tags string if no headline text
  6886. - the tags string, or nil."
  6887. (save-excursion
  6888. (org-back-to-heading t)
  6889. (if (let (case-fold-search)
  6890. (looking-at
  6891. (if orgstruct-mode
  6892. org-heading-regexp
  6893. org-complex-heading-regexp)))
  6894. (if orgstruct-mode
  6895. (list (length (match-string 1))
  6896. (org-reduced-level (length (match-string 1)))
  6897. nil
  6898. nil
  6899. (match-string 2)
  6900. nil)
  6901. (list (length (match-string 1))
  6902. (org-reduced-level (length (match-string 1)))
  6903. (org-match-string-no-properties 2)
  6904. (and (match-end 3) (aref (match-string 3) 2))
  6905. (org-match-string-no-properties 4)
  6906. (org-match-string-no-properties 5))))))
  6907. (defun org-get-entry ()
  6908. "Get the entry text, after heading, entire subtree."
  6909. (save-excursion
  6910. (org-back-to-heading t)
  6911. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6912. (defun org-insert-heading-after-current ()
  6913. "Insert a new heading with same level as current, after current subtree."
  6914. (interactive)
  6915. (org-back-to-heading)
  6916. (org-insert-heading)
  6917. (org-move-subtree-down)
  6918. (end-of-line 1))
  6919. (defun org-insert-heading-respect-content (&optional arg invisible-ok)
  6920. "Insert heading with `org-insert-heading-respect-content' set to t."
  6921. (interactive "P")
  6922. (let ((org-insert-heading-respect-content t))
  6923. (org-insert-heading arg invisible-ok)))
  6924. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6925. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6926. (interactive "P")
  6927. (let ((org-insert-heading-respect-content t))
  6928. (org-insert-todo-heading force-state t)))
  6929. (defun org-insert-todo-heading (arg &optional force-heading)
  6930. "Insert a new heading with the same level and TODO state as current heading.
  6931. If the heading has no TODO state, or if the state is DONE, use the first
  6932. state (TODO by default). Also one prefix arg, force first state. With two
  6933. prefix args, force inserting at the end of the parent subtree."
  6934. (interactive "P")
  6935. (when (or force-heading (not (org-insert-item 'checkbox)))
  6936. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6937. force-heading))
  6938. (save-excursion
  6939. (org-back-to-heading)
  6940. (outline-previous-heading)
  6941. (looking-at org-todo-line-regexp))
  6942. (let*
  6943. ((new-mark-x
  6944. (if (or arg
  6945. (not (match-beginning 2))
  6946. (member (match-string 2) org-done-keywords))
  6947. (car org-todo-keywords-1)
  6948. (match-string 2)))
  6949. (new-mark
  6950. (or
  6951. (run-hook-with-args-until-success
  6952. 'org-todo-get-default-hook new-mark-x nil)
  6953. new-mark-x)))
  6954. (beginning-of-line 1)
  6955. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6956. (if org-treat-insert-todo-heading-as-state-change
  6957. (org-todo new-mark)
  6958. (insert new-mark " "))))
  6959. (when org-provide-todo-statistics
  6960. (org-update-parent-todo-statistics))))
  6961. (defun org-insert-subheading (arg)
  6962. "Insert a new subheading and demote it.
  6963. Works for outline headings and for plain lists alike."
  6964. (interactive "P")
  6965. (org-insert-heading arg)
  6966. (cond
  6967. ((org-at-heading-p) (org-do-demote))
  6968. ((org-at-item-p) (org-indent-item))))
  6969. (defun org-insert-todo-subheading (arg)
  6970. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6971. Works for outline headings and for plain lists alike."
  6972. (interactive "P")
  6973. (org-insert-todo-heading arg)
  6974. (cond
  6975. ((org-at-heading-p) (org-do-demote))
  6976. ((org-at-item-p) (org-indent-item))))
  6977. ;;; Promotion and Demotion
  6978. (defvar org-after-demote-entry-hook nil
  6979. "Hook run after an entry has been demoted.
  6980. The cursor will be at the beginning of the entry.
  6981. When a subtree is being demoted, the hook will be called for each node.")
  6982. (defvar org-after-promote-entry-hook nil
  6983. "Hook run after an entry has been promoted.
  6984. The cursor will be at the beginning of the entry.
  6985. When a subtree is being promoted, the hook will be called for each node.")
  6986. (defun org-promote-subtree ()
  6987. "Promote the entire subtree.
  6988. See also `org-promote'."
  6989. (interactive)
  6990. (save-excursion
  6991. (org-with-limited-levels (org-map-tree 'org-promote)))
  6992. (org-fix-position-after-promote))
  6993. (defun org-demote-subtree ()
  6994. "Demote the entire subtree. See `org-demote'.
  6995. See also `org-promote'."
  6996. (interactive)
  6997. (save-excursion
  6998. (org-with-limited-levels (org-map-tree 'org-demote)))
  6999. (org-fix-position-after-promote))
  7000. (defun org-do-promote ()
  7001. "Promote the current heading higher up the tree.
  7002. If the region is active in `transient-mark-mode', promote all headings
  7003. in the region."
  7004. (interactive)
  7005. (save-excursion
  7006. (if (org-region-active-p)
  7007. (org-map-region 'org-promote (region-beginning) (region-end))
  7008. (org-promote)))
  7009. (org-fix-position-after-promote))
  7010. (defun org-do-demote ()
  7011. "Demote the current heading lower down the tree.
  7012. If the region is active in `transient-mark-mode', demote all headings
  7013. in the region."
  7014. (interactive)
  7015. (save-excursion
  7016. (if (org-region-active-p)
  7017. (org-map-region 'org-demote (region-beginning) (region-end))
  7018. (org-demote)))
  7019. (org-fix-position-after-promote))
  7020. (defun org-fix-position-after-promote ()
  7021. "Make sure that after pro/demotion cursor position is right."
  7022. (let ((pos (point)))
  7023. (when (save-excursion
  7024. (beginning-of-line 1)
  7025. (looking-at org-todo-line-regexp)
  7026. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7027. (cond ((eobp) (insert " "))
  7028. ((eolp) (insert " "))
  7029. ((equal (char-after) ?\ ) (forward-char 1))))))
  7030. (defun org-current-level ()
  7031. "Return the level of the current entry, or nil if before the first headline.
  7032. The level is the number of stars at the beginning of the headline."
  7033. (save-excursion
  7034. (org-with-limited-levels
  7035. (if (ignore-errors (org-back-to-heading t))
  7036. (funcall outline-level)))))
  7037. (defun org-get-previous-line-level ()
  7038. "Return the outline depth of the last headline before the current line.
  7039. Returns 0 for the first headline in the buffer, and nil if before the
  7040. first headline."
  7041. (let ((current-level (org-current-level))
  7042. (prev-level (when (> (line-number-at-pos) 1)
  7043. (save-excursion
  7044. (beginning-of-line 0)
  7045. (org-current-level)))))
  7046. (cond ((null current-level) nil) ; Before first headline
  7047. ((null prev-level) 0) ; At first headline
  7048. (prev-level))))
  7049. (defun org-reduced-level (l)
  7050. "Compute the effective level of a heading.
  7051. This takes into account the setting of `org-odd-levels-only'."
  7052. (cond
  7053. ((zerop l) 0)
  7054. (org-odd-levels-only (1+ (floor (/ l 2))))
  7055. (t l)))
  7056. (defun org-level-increment ()
  7057. "Return the number of stars that will be added or removed at a
  7058. time to headlines when structure editing, based on the value of
  7059. `org-odd-levels-only'."
  7060. (if org-odd-levels-only 2 1))
  7061. (defun org-get-valid-level (level &optional change)
  7062. "Rectify a level change under the influence of `org-odd-levels-only'
  7063. LEVEL is a current level, CHANGE is by how much the level should be
  7064. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7065. even level numbers will become the next higher odd number."
  7066. (if org-odd-levels-only
  7067. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7068. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7069. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7070. (max 1 (+ level (or change 0)))))
  7071. (if (boundp 'define-obsolete-function-alias)
  7072. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7073. (define-obsolete-function-alias 'org-get-legal-level
  7074. 'org-get-valid-level)
  7075. (define-obsolete-function-alias 'org-get-legal-level
  7076. 'org-get-valid-level "23.1")))
  7077. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  7078. ;; ̀org-with-limited-levels'
  7079. (defun org-promote ()
  7080. "Promote the current heading higher up the tree.
  7081. If the region is active in `transient-mark-mode', promote all headings
  7082. in the region."
  7083. (org-back-to-heading t)
  7084. (let* ((level (save-match-data (funcall outline-level)))
  7085. (after-change-functions (remove 'flyspell-after-change-function
  7086. after-change-functions))
  7087. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7088. (diff (abs (- level (length up-head) -1))))
  7089. (cond ((and (= level 1) org-called-with-limited-levels
  7090. org-allow-promoting-top-level-subtree)
  7091. (replace-match "# " nil t))
  7092. ((= level 1)
  7093. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  7094. (t (replace-match up-head nil t)))
  7095. ;; Fixup tag positioning
  7096. (unless (= level 1)
  7097. (and org-auto-align-tags (org-set-tags nil t))
  7098. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  7099. (run-hooks 'org-after-promote-entry-hook)))
  7100. (defun org-demote ()
  7101. "Demote the current heading lower down the tree.
  7102. If the region is active in `transient-mark-mode', demote all headings
  7103. in the region."
  7104. (org-back-to-heading t)
  7105. (let* ((level (save-match-data (funcall outline-level)))
  7106. (after-change-functions (remove 'flyspell-after-change-function
  7107. after-change-functions))
  7108. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7109. (diff (abs (- level (length down-head) -1))))
  7110. (replace-match down-head nil t)
  7111. ;; Fixup tag positioning
  7112. (and org-auto-align-tags (org-set-tags nil t))
  7113. (if org-adapt-indentation (org-fixup-indentation diff))
  7114. (run-hooks 'org-after-demote-entry-hook)))
  7115. (defun org-cycle-level ()
  7116. "Cycle the level of an empty headline through possible states.
  7117. This goes first to child, then to parent, level, then up the hierarchy.
  7118. After top level, it switches back to sibling level."
  7119. (interactive)
  7120. (let ((org-adapt-indentation nil))
  7121. (when (org-point-at-end-of-empty-headline)
  7122. (setq this-command 'org-cycle-level) ; Only needed for caching
  7123. (let ((cur-level (org-current-level))
  7124. (prev-level (org-get-previous-line-level)))
  7125. (cond
  7126. ;; If first headline in file, promote to top-level.
  7127. ((= prev-level 0)
  7128. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7129. do (org-do-promote)))
  7130. ;; If same level as prev, demote one.
  7131. ((= prev-level cur-level)
  7132. (org-do-demote))
  7133. ;; If parent is top-level, promote to top level if not already.
  7134. ((= prev-level 1)
  7135. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7136. do (org-do-promote)))
  7137. ;; If top-level, return to prev-level.
  7138. ((= cur-level 1)
  7139. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7140. do (org-do-demote)))
  7141. ;; If less than prev-level, promote one.
  7142. ((< cur-level prev-level)
  7143. (org-do-promote))
  7144. ;; If deeper than prev-level, promote until higher than
  7145. ;; prev-level.
  7146. ((> cur-level prev-level)
  7147. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7148. do (org-do-promote))))
  7149. t))))
  7150. (defun org-map-tree (fun)
  7151. "Call FUN for every heading underneath the current one."
  7152. (org-back-to-heading)
  7153. (let ((level (funcall outline-level)))
  7154. (save-excursion
  7155. (funcall fun)
  7156. (while (and (progn
  7157. (outline-next-heading)
  7158. (> (funcall outline-level) level))
  7159. (not (eobp)))
  7160. (funcall fun)))))
  7161. (defun org-map-region (fun beg end)
  7162. "Call FUN for every heading between BEG and END."
  7163. (let ((org-ignore-region t))
  7164. (save-excursion
  7165. (setq end (copy-marker end))
  7166. (goto-char beg)
  7167. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7168. (< (point) end))
  7169. (funcall fun))
  7170. (while (and (progn
  7171. (outline-next-heading)
  7172. (< (point) end))
  7173. (not (eobp)))
  7174. (funcall fun)))))
  7175. (defvar org-property-end-re) ; silence byte-compiler
  7176. (defun org-fixup-indentation (diff)
  7177. "Change the indentation in the current entry by DIFF.
  7178. However, if any line in the current entry has no indentation, or if it
  7179. would end up with no indentation after the change, nothing at all is done."
  7180. (save-excursion
  7181. (let ((end (save-excursion (outline-next-heading)
  7182. (point-marker)))
  7183. (prohibit (if (> diff 0)
  7184. "^\\S-"
  7185. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7186. col)
  7187. (unless (save-excursion (end-of-line 1)
  7188. (re-search-forward prohibit end t))
  7189. (while (and (< (point) end)
  7190. (re-search-forward "^[ \t]+" end t))
  7191. (goto-char (match-end 0))
  7192. (setq col (current-column))
  7193. (if (< diff 0) (replace-match ""))
  7194. (org-indent-to-column (+ diff col))))
  7195. (move-marker end nil))))
  7196. (defun org-convert-to-odd-levels ()
  7197. "Convert an org-mode file with all levels allowed to one with odd levels.
  7198. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7199. level 5 etc."
  7200. (interactive)
  7201. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7202. (let ((outline-level 'org-outline-level)
  7203. (org-odd-levels-only nil) n)
  7204. (save-excursion
  7205. (goto-char (point-min))
  7206. (while (re-search-forward "^\\*\\*+ " nil t)
  7207. (setq n (- (length (match-string 0)) 2))
  7208. (while (>= (setq n (1- n)) 0)
  7209. (org-demote))
  7210. (end-of-line 1))))))
  7211. (defun org-convert-to-oddeven-levels ()
  7212. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7213. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7214. file contains a section with an even level, conversion would
  7215. destroy the structure of the file. An error is signaled in this
  7216. case."
  7217. (interactive)
  7218. (goto-char (point-min))
  7219. ;; First check if there are no even levels
  7220. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7221. (org-show-context t)
  7222. (error "Not all levels are odd in this file. Conversion not possible"))
  7223. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7224. (let ((outline-regexp org-outline-regexp)
  7225. (outline-level 'org-outline-level)
  7226. (org-odd-levels-only nil) n)
  7227. (save-excursion
  7228. (goto-char (point-min))
  7229. (while (re-search-forward "^\\*\\*+ " nil t)
  7230. (setq n (/ (1- (length (match-string 0))) 2))
  7231. (while (>= (setq n (1- n)) 0)
  7232. (org-promote))
  7233. (end-of-line 1))))))
  7234. (defun org-tr-level (n)
  7235. "Make N odd if required."
  7236. (if org-odd-levels-only (1+ (/ n 2)) n))
  7237. ;;; Vertical tree motion, cutting and pasting of subtrees
  7238. (defun org-move-subtree-up (&optional arg)
  7239. "Move the current subtree up past ARG headlines of the same level."
  7240. (interactive "p")
  7241. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7242. (defun org-move-subtree-down (&optional arg)
  7243. "Move the current subtree down past ARG headlines of the same level."
  7244. (interactive "p")
  7245. (setq arg (prefix-numeric-value arg))
  7246. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7247. 'org-get-last-sibling))
  7248. (ins-point (make-marker))
  7249. (cnt (abs arg))
  7250. (col (current-column))
  7251. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7252. ;; Select the tree
  7253. (org-back-to-heading)
  7254. (setq beg0 (point))
  7255. (save-excursion
  7256. (setq ne-beg (org-back-over-empty-lines))
  7257. (setq beg (point)))
  7258. (save-match-data
  7259. (save-excursion (outline-end-of-heading)
  7260. (setq folded (outline-invisible-p)))
  7261. (outline-end-of-subtree))
  7262. (outline-next-heading)
  7263. (setq ne-end (org-back-over-empty-lines))
  7264. (setq end (point))
  7265. (goto-char beg0)
  7266. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7267. ;; include less whitespace
  7268. (save-excursion
  7269. (goto-char beg)
  7270. (forward-line (- ne-beg ne-end))
  7271. (setq beg (point))))
  7272. ;; Find insertion point, with error handling
  7273. (while (> cnt 0)
  7274. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7275. (progn (goto-char beg0)
  7276. (user-error "Cannot move past superior level or buffer limit")))
  7277. (setq cnt (1- cnt)))
  7278. (if (> arg 0)
  7279. ;; Moving forward - still need to move over subtree
  7280. (progn (org-end-of-subtree t t)
  7281. (save-excursion
  7282. (org-back-over-empty-lines)
  7283. (or (bolp) (newline)))))
  7284. (setq ne-ins (org-back-over-empty-lines))
  7285. (move-marker ins-point (point))
  7286. (setq txt (buffer-substring beg end))
  7287. (org-save-markers-in-region beg end)
  7288. (delete-region beg end)
  7289. (org-remove-empty-overlays-at beg)
  7290. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7291. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7292. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7293. (let ((bbb (point)))
  7294. (insert-before-markers txt)
  7295. (org-reinstall-markers-in-region bbb)
  7296. (move-marker ins-point bbb))
  7297. (or (bolp) (insert "\n"))
  7298. (setq ins-end (point))
  7299. (goto-char ins-point)
  7300. (org-skip-whitespace)
  7301. (when (and (< arg 0)
  7302. (org-first-sibling-p)
  7303. (> ne-ins ne-beg))
  7304. ;; Move whitespace back to beginning
  7305. (save-excursion
  7306. (goto-char ins-end)
  7307. (let ((kill-whole-line t))
  7308. (kill-line (- ne-ins ne-beg)) (point)))
  7309. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7310. (move-marker ins-point nil)
  7311. (if folded
  7312. (hide-subtree)
  7313. (org-show-entry)
  7314. (show-children)
  7315. (org-cycle-hide-drawers 'children))
  7316. (org-clean-visibility-after-subtree-move)
  7317. ;; move back to the initial column we were at
  7318. (move-to-column col)))
  7319. (defvar org-subtree-clip ""
  7320. "Clipboard for cut and paste of subtrees.
  7321. This is actually only a copy of the kill, because we use the normal kill
  7322. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7323. (defvar org-subtree-clip-folded nil
  7324. "Was the last copied subtree folded?
  7325. This is used to fold the tree back after pasting.")
  7326. (defun org-cut-subtree (&optional n)
  7327. "Cut the current subtree into the clipboard.
  7328. With prefix arg N, cut this many sequential subtrees.
  7329. This is a short-hand for marking the subtree and then cutting it."
  7330. (interactive "p")
  7331. (org-copy-subtree n 'cut))
  7332. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7333. "Cut the current subtree into the clipboard.
  7334. With prefix arg N, cut this many sequential subtrees.
  7335. This is a short-hand for marking the subtree and then copying it.
  7336. If CUT is non-nil, actually cut the subtree.
  7337. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7338. of some markers in the region, even if CUT is non-nil. This is
  7339. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7340. (interactive "p")
  7341. (let (beg end folded (beg0 (point)))
  7342. (if (org-called-interactively-p 'any)
  7343. (org-back-to-heading nil) ; take what looks like a subtree
  7344. (org-back-to-heading t)) ; take what is really there
  7345. (setq beg (point))
  7346. (skip-chars-forward " \t\r\n")
  7347. (save-match-data
  7348. (if nosubtrees
  7349. (outline-next-heading)
  7350. (save-excursion (outline-end-of-heading)
  7351. (setq folded (outline-invisible-p)))
  7352. (condition-case nil
  7353. (org-forward-heading-same-level (1- n) t)
  7354. (error nil))
  7355. (org-end-of-subtree t t)))
  7356. (setq end (point))
  7357. (goto-char beg0)
  7358. (when (> end beg)
  7359. (setq org-subtree-clip-folded folded)
  7360. (when (or cut force-store-markers)
  7361. (org-save-markers-in-region beg end))
  7362. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7363. (setq org-subtree-clip (current-kill 0))
  7364. (message "%s: Subtree(s) with %d characters"
  7365. (if cut "Cut" "Copied")
  7366. (length org-subtree-clip)))))
  7367. (defun org-paste-subtree (&optional level tree for-yank)
  7368. "Paste the clipboard as a subtree, with modification of headline level.
  7369. The entire subtree is promoted or demoted in order to match a new headline
  7370. level.
  7371. If the cursor is at the beginning of a headline, the same level as
  7372. that headline is used to paste the tree.
  7373. If not, the new level is derived from the *visible* headings
  7374. before and after the insertion point, and taken to be the inferior headline
  7375. level of the two. So if the previous visible heading is level 3 and the
  7376. next is level 4 (or vice versa), level 4 will be used for insertion.
  7377. This makes sure that the subtree remains an independent subtree and does
  7378. not swallow low level entries.
  7379. You can also force a different level, either by using a numeric prefix
  7380. argument, or by inserting the heading marker by hand. For example, if the
  7381. cursor is after \"*****\", then the tree will be shifted to level 5.
  7382. If optional TREE is given, use this text instead of the kill ring.
  7383. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7384. move back over whitespace before inserting, and move point to the end of
  7385. the inserted text when done."
  7386. (interactive "P")
  7387. (setq tree (or tree (and kill-ring (current-kill 0))))
  7388. (unless (org-kill-is-subtree-p tree)
  7389. (error "%s"
  7390. (substitute-command-keys
  7391. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7392. (org-with-limited-levels
  7393. (let* ((visp (not (outline-invisible-p)))
  7394. (txt tree)
  7395. (^re_ "\\(\\*+\\)[ \t]*")
  7396. (old-level (if (string-match org-outline-regexp-bol txt)
  7397. (- (match-end 0) (match-beginning 0) 1)
  7398. -1))
  7399. (force-level (cond (level (prefix-numeric-value level))
  7400. ((and (looking-at "[ \t]*$")
  7401. (string-match
  7402. "^\\*+$" (buffer-substring
  7403. (point-at-bol) (point))))
  7404. (- (match-end 1) (match-beginning 1)))
  7405. ((and (bolp)
  7406. (looking-at org-outline-regexp))
  7407. (- (match-end 0) (point) 1))))
  7408. (previous-level (save-excursion
  7409. (condition-case nil
  7410. (progn
  7411. (outline-previous-visible-heading 1)
  7412. (if (looking-at ^re_)
  7413. (- (match-end 0) (match-beginning 0) 1)
  7414. 1))
  7415. (error 1))))
  7416. (next-level (save-excursion
  7417. (condition-case nil
  7418. (progn
  7419. (or (looking-at org-outline-regexp)
  7420. (outline-next-visible-heading 1))
  7421. (if (looking-at ^re_)
  7422. (- (match-end 0) (match-beginning 0) 1)
  7423. 1))
  7424. (error 1))))
  7425. (new-level (or force-level (max previous-level next-level)))
  7426. (shift (if (or (= old-level -1)
  7427. (= new-level -1)
  7428. (= old-level new-level))
  7429. 0
  7430. (- new-level old-level)))
  7431. (delta (if (> shift 0) -1 1))
  7432. (func (if (> shift 0) 'org-demote 'org-promote))
  7433. (org-odd-levels-only nil)
  7434. beg end newend)
  7435. ;; Remove the forced level indicator
  7436. (if force-level
  7437. (delete-region (point-at-bol) (point)))
  7438. ;; Paste
  7439. (beginning-of-line (if (bolp) 1 2))
  7440. (setq beg (point))
  7441. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7442. (insert-before-markers txt)
  7443. (unless (string-match "\n\\'" txt) (insert "\n"))
  7444. (setq newend (point))
  7445. (org-reinstall-markers-in-region beg)
  7446. (setq end (point))
  7447. (goto-char beg)
  7448. (skip-chars-forward " \t\n\r")
  7449. (setq beg (point))
  7450. (if (and (outline-invisible-p) visp)
  7451. (save-excursion (outline-show-heading)))
  7452. ;; Shift if necessary
  7453. (unless (= shift 0)
  7454. (save-restriction
  7455. (narrow-to-region beg end)
  7456. (while (not (= shift 0))
  7457. (org-map-region func (point-min) (point-max))
  7458. (setq shift (+ delta shift)))
  7459. (goto-char (point-min))
  7460. (setq newend (point-max))))
  7461. (when (or (org-called-interactively-p 'interactive) for-yank)
  7462. (message "Clipboard pasted as level %d subtree" new-level))
  7463. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7464. kill-ring
  7465. (eq org-subtree-clip (current-kill 0))
  7466. org-subtree-clip-folded)
  7467. ;; The tree was folded before it was killed/copied
  7468. (hide-subtree))
  7469. (and for-yank (goto-char newend)))))
  7470. (defun org-kill-is-subtree-p (&optional txt)
  7471. "Check if the current kill is an outline subtree, or a set of trees.
  7472. Returns nil if kill does not start with a headline, or if the first
  7473. headline level is not the largest headline level in the tree.
  7474. So this will actually accept several entries of equal levels as well,
  7475. which is OK for `org-paste-subtree'.
  7476. If optional TXT is given, check this string instead of the current kill."
  7477. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7478. (re (org-get-limited-outline-regexp))
  7479. (^re (concat "^" re))
  7480. (start-level (and kill
  7481. (string-match
  7482. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7483. kill)
  7484. (- (match-end 2) (match-beginning 2) 1)))
  7485. (start (1+ (or (match-beginning 2) -1))))
  7486. (if (not start-level)
  7487. (progn
  7488. nil) ;; does not even start with a heading
  7489. (catch 'exit
  7490. (while (setq start (string-match ^re kill (1+ start)))
  7491. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7492. (throw 'exit nil)))
  7493. t))))
  7494. (defvar org-markers-to-move nil
  7495. "Markers that should be moved with a cut-and-paste operation.
  7496. Those markers are stored together with their positions relative to
  7497. the start of the region.")
  7498. (defun org-save-markers-in-region (beg end)
  7499. "Check markers in region.
  7500. If these markers are between BEG and END, record their position relative
  7501. to BEG, so that after moving the block of text, we can put the markers back
  7502. into place.
  7503. This function gets called just before an entry or tree gets cut from the
  7504. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7505. called immediately, to move the markers with the entries."
  7506. (setq org-markers-to-move nil)
  7507. (when (featurep 'org-clock)
  7508. (org-clock-save-markers-for-cut-and-paste beg end))
  7509. (when (featurep 'org-agenda)
  7510. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7511. (defun org-check-and-save-marker (marker beg end)
  7512. "Check if MARKER is between BEG and END.
  7513. If yes, remember the marker and the distance to BEG."
  7514. (when (and (marker-buffer marker)
  7515. (equal (marker-buffer marker) (current-buffer)))
  7516. (if (and (>= marker beg) (< marker end))
  7517. (push (cons marker (- marker beg)) org-markers-to-move))))
  7518. (defun org-reinstall-markers-in-region (beg)
  7519. "Move all remembered markers to their position relative to BEG."
  7520. (mapc (lambda (x)
  7521. (move-marker (car x) (+ beg (cdr x))))
  7522. org-markers-to-move)
  7523. (setq org-markers-to-move nil))
  7524. (defun org-narrow-to-subtree ()
  7525. "Narrow buffer to the current subtree."
  7526. (interactive)
  7527. (save-excursion
  7528. (save-match-data
  7529. (org-with-limited-levels
  7530. (narrow-to-region
  7531. (progn (org-back-to-heading t) (point))
  7532. (progn (org-end-of-subtree t t)
  7533. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7534. (point)))))))
  7535. (defun org-narrow-to-block ()
  7536. "Narrow buffer to the current block."
  7537. (interactive)
  7538. (let* ((case-fold-search t)
  7539. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7540. "^[ \t]*#\\+end_.*")))
  7541. (if blockp
  7542. (narrow-to-region (car blockp) (cdr blockp))
  7543. (error "Not in a block"))))
  7544. (eval-when-compile
  7545. (defvar org-property-drawer-re))
  7546. (defvar org-property-start-re) ;; defined below
  7547. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7548. "Clone the task (subtree) at point N times.
  7549. The clones will be inserted as siblings.
  7550. In interactive use, the user will be prompted for the number of
  7551. clones to be produced. When called with a universal prefix argument
  7552. and if the entry has a timestamp, the user will also be prompted for
  7553. a time shift, which may be a repeater as used in time stamps, for
  7554. example `+3d'.
  7555. When a valid repeater is given and the entry contains any time
  7556. stamps, the clones will become a sequence in time, with time
  7557. stamps in the subtree shifted for each clone produced. If SHIFT
  7558. is nil or the empty string, time stamps will be left alone. The
  7559. ID property of the original subtree is removed.
  7560. If the original subtree did contain time stamps with a repeater,
  7561. the following will happen:
  7562. - the repeater will be removed in each clone
  7563. - an additional clone will be produced, with the current, unshifted
  7564. date(s) in the entry.
  7565. - the original entry will be placed *after* all the clones, with
  7566. repeater intact.
  7567. - the start days in the repeater in the original entry will be shifted
  7568. to past the last clone.
  7569. In this way you can spell out a number of instances of a repeating task,
  7570. and still retain the repeater to cover future instances of the task."
  7571. (interactive "nNumber of clones to produce: ")
  7572. (let ((shift
  7573. (or shift
  7574. (if (and (equal current-prefix-arg '(4))
  7575. (save-excursion
  7576. (re-search-forward org-ts-regexp-both
  7577. (save-excursion
  7578. (org-end-of-subtree t)
  7579. (point)) t)))
  7580. (read-from-minibuffer
  7581. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7582. ""))) ;; No time shift
  7583. (n-no-remove -1)
  7584. (drawer-re org-drawer-regexp)
  7585. beg end template task idprop
  7586. shift-n shift-what doshift nmin nmax)
  7587. (if (not (and (integerp n) (> n 0)))
  7588. (error "Invalid number of replications %s" n))
  7589. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7590. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7591. shift)))
  7592. (error "Invalid shift specification %s" shift))
  7593. (when doshift
  7594. (setq shift-n (string-to-number (match-string 1 shift))
  7595. shift-what (cdr (assoc (match-string 2 shift)
  7596. '(("d" . day) ("w" . week)
  7597. ("m" . month) ("y" . year))))))
  7598. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7599. (setq nmin 1 nmax n)
  7600. (org-back-to-heading t)
  7601. (setq beg (point))
  7602. (setq idprop (org-entry-get nil "ID"))
  7603. (org-end-of-subtree t t)
  7604. (or (bolp) (insert "\n"))
  7605. (setq end (point))
  7606. (setq template (buffer-substring beg end))
  7607. (when (and doshift
  7608. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7609. (delete-region beg end)
  7610. (setq end beg)
  7611. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7612. (goto-char end)
  7613. (loop for n from nmin to nmax do
  7614. ;; prepare clone
  7615. (with-temp-buffer
  7616. (insert template)
  7617. (org-mode)
  7618. (goto-char (point-min))
  7619. (org-show-subtree)
  7620. (and idprop (if org-clone-delete-id
  7621. (org-entry-delete nil "ID")
  7622. (org-id-get-create t)))
  7623. (unless (= n 0)
  7624. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7625. (kill-whole-line))
  7626. (goto-char (point-min))
  7627. (while (re-search-forward drawer-re nil t)
  7628. (mapc (lambda (d)
  7629. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7630. (goto-char (point-min))
  7631. (when doshift
  7632. (while (re-search-forward org-ts-regexp-both nil t)
  7633. (org-timestamp-change (* n shift-n) shift-what))
  7634. (unless (= n n-no-remove)
  7635. (goto-char (point-min))
  7636. (while (re-search-forward org-ts-regexp nil t)
  7637. (save-excursion
  7638. (goto-char (match-beginning 0))
  7639. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7640. (delete-region (match-beginning 1) (match-end 1)))))))
  7641. (setq task (buffer-string)))
  7642. (insert task))
  7643. (goto-char beg)))
  7644. ;;; Outline Sorting
  7645. (defun org-sort (with-case)
  7646. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7647. Optional argument WITH-CASE means sort case-sensitively."
  7648. (interactive "P")
  7649. (cond
  7650. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7651. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7652. (t
  7653. (org-call-with-arg 'org-sort-entries with-case))))
  7654. (defun org-sort-remove-invisible (s)
  7655. "Remove invisible links from string S."
  7656. (remove-text-properties 0 (length s) org-rm-props s)
  7657. (while (string-match org-bracket-link-regexp s)
  7658. (setq s (replace-match (if (match-end 2)
  7659. (match-string 3 s)
  7660. (match-string 1 s)) t t s)))
  7661. (let ((st (format " %s " s)))
  7662. (while (string-match org-emph-re st)
  7663. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7664. (setq s (substring st 1 -1)))
  7665. s)
  7666. (defvar org-priority-regexp) ; defined later in the file
  7667. (defvar org-after-sorting-entries-or-items-hook nil
  7668. "Hook that is run after a bunch of entries or items have been sorted.
  7669. When children are sorted, the cursor is in the parent line when this
  7670. hook gets called. When a region or a plain list is sorted, the cursor
  7671. will be in the first entry of the sorted region/list.")
  7672. (defun org-sort-entries
  7673. (&optional with-case sorting-type getkey-func compare-func property)
  7674. "Sort entries on a certain level of an outline tree.
  7675. If there is an active region, the entries in the region are sorted.
  7676. Else, if the cursor is before the first entry, sort the top-level items.
  7677. Else, the children of the entry at point are sorted.
  7678. Sorting can be alphabetically, numerically, by date/time as given by
  7679. a time stamp, by a property, by priority order, or by a custom function.
  7680. The command prompts for the sorting type unless it has been given to the
  7681. function through the SORTING-TYPE argument, which needs to be a character,
  7682. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7683. precise meaning of each character:
  7684. n Numerically, by converting the beginning of the entry/item to a number.
  7685. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7686. o By order of TODO keywords.
  7687. t By date/time, either the first active time stamp in the entry, or, if
  7688. none exist, by the first inactive one.
  7689. s By the scheduled date/time.
  7690. d By deadline date/time.
  7691. c By creation time, which is assumed to be the first inactive time stamp
  7692. at the beginning of a line.
  7693. p By priority according to the cookie.
  7694. r By the value of a property.
  7695. Capital letters will reverse the sort order.
  7696. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7697. called with point at the beginning of the record. It must return either
  7698. a string or a number that should serve as the sorting key for that record.
  7699. Comparing entries ignores case by default. However, with an optional argument
  7700. WITH-CASE, the sorting considers case as well.
  7701. Sorting is done against the visible part of the headlines, it ignores hidden
  7702. links."
  7703. (interactive "P")
  7704. (let ((case-func (if with-case 'identity 'downcase))
  7705. (cmstr
  7706. ;; The clock marker is lost when using `sort-subr', let's
  7707. ;; store the clocking string.
  7708. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7709. (save-excursion
  7710. (goto-char org-clock-marker)
  7711. (looking-back "^.*") (match-string-no-properties 0))))
  7712. start beg end stars re re2
  7713. txt what tmp)
  7714. ;; Find beginning and end of region to sort
  7715. (cond
  7716. ((org-region-active-p)
  7717. ;; we will sort the region
  7718. (setq end (region-end)
  7719. what "region")
  7720. (goto-char (region-beginning))
  7721. (if (not (org-at-heading-p)) (outline-next-heading))
  7722. (setq start (point)))
  7723. ((or (org-at-heading-p)
  7724. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7725. ;; we will sort the children of the current headline
  7726. (org-back-to-heading)
  7727. (setq start (point)
  7728. end (progn (org-end-of-subtree t t)
  7729. (or (bolp) (insert "\n"))
  7730. (org-back-over-empty-lines)
  7731. (point))
  7732. what "children")
  7733. (goto-char start)
  7734. (show-subtree)
  7735. (outline-next-heading))
  7736. (t
  7737. ;; we will sort the top-level entries in this file
  7738. (goto-char (point-min))
  7739. (or (org-at-heading-p) (outline-next-heading))
  7740. (setq start (point))
  7741. (goto-char (point-max))
  7742. (beginning-of-line 1)
  7743. (when (looking-at ".*?\\S-")
  7744. ;; File ends in a non-white line
  7745. (end-of-line 1)
  7746. (insert "\n"))
  7747. (setq end (point-max))
  7748. (setq what "top-level")
  7749. (goto-char start)
  7750. (show-all)))
  7751. (setq beg (point))
  7752. (if (>= beg end) (error "Nothing to sort"))
  7753. (looking-at "\\(\\*+\\)")
  7754. (setq stars (match-string 1)
  7755. re (concat "^" (regexp-quote stars) " +")
  7756. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7757. txt (buffer-substring beg end))
  7758. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7759. (if (and (not (equal stars "*")) (string-match re2 txt))
  7760. (error "Region to sort contains a level above the first entry"))
  7761. (unless sorting-type
  7762. (message
  7763. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7764. [t]ime [s]cheduled [d]eadline [c]reated
  7765. A/N/P/R/O/F/T/S/D/C means reversed:"
  7766. what)
  7767. (setq sorting-type (read-char-exclusive))
  7768. (unless getkey-func
  7769. (and (= (downcase sorting-type) ?f)
  7770. (setq getkey-func
  7771. (org-icompleting-read "Sort using function: "
  7772. obarray 'fboundp t nil nil))
  7773. (setq getkey-func (intern getkey-func))))
  7774. (and (= (downcase sorting-type) ?r)
  7775. (not property)
  7776. (setq property
  7777. (org-icompleting-read "Property: "
  7778. (mapcar 'list (org-buffer-property-keys t))
  7779. nil t))))
  7780. (message "Sorting entries...")
  7781. (save-restriction
  7782. (narrow-to-region start end)
  7783. (let ((dcst (downcase sorting-type))
  7784. (case-fold-search nil)
  7785. (now (current-time)))
  7786. (sort-subr
  7787. (/= dcst sorting-type)
  7788. ;; This function moves to the beginning character of the "record" to
  7789. ;; be sorted.
  7790. (lambda nil
  7791. (if (re-search-forward re nil t)
  7792. (goto-char (match-beginning 0))
  7793. (goto-char (point-max))))
  7794. ;; This function moves to the last character of the "record" being
  7795. ;; sorted.
  7796. (lambda nil
  7797. (save-match-data
  7798. (condition-case nil
  7799. (outline-forward-same-level 1)
  7800. (error
  7801. (goto-char (point-max))))))
  7802. ;; This function returns the value that gets sorted against.
  7803. (lambda nil
  7804. (cond
  7805. ((= dcst ?n)
  7806. (if (looking-at org-complex-heading-regexp)
  7807. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7808. nil))
  7809. ((= dcst ?a)
  7810. (if (looking-at org-complex-heading-regexp)
  7811. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7812. nil))
  7813. ((= dcst ?t)
  7814. (let ((end (save-excursion (outline-next-heading) (point))))
  7815. (if (or (re-search-forward org-ts-regexp end t)
  7816. (re-search-forward org-ts-regexp-both end t))
  7817. (org-time-string-to-seconds (match-string 0))
  7818. (org-float-time now))))
  7819. ((= dcst ?c)
  7820. (let ((end (save-excursion (outline-next-heading) (point))))
  7821. (if (re-search-forward
  7822. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7823. end t)
  7824. (org-time-string-to-seconds (match-string 0))
  7825. (org-float-time now))))
  7826. ((= dcst ?s)
  7827. (let ((end (save-excursion (outline-next-heading) (point))))
  7828. (if (re-search-forward org-scheduled-time-regexp end t)
  7829. (org-time-string-to-seconds (match-string 1))
  7830. (org-float-time now))))
  7831. ((= dcst ?d)
  7832. (let ((end (save-excursion (outline-next-heading) (point))))
  7833. (if (re-search-forward org-deadline-time-regexp end t)
  7834. (org-time-string-to-seconds (match-string 1))
  7835. (org-float-time now))))
  7836. ((= dcst ?p)
  7837. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7838. (string-to-char (match-string 2))
  7839. org-default-priority))
  7840. ((= dcst ?r)
  7841. (or (org-entry-get nil property) ""))
  7842. ((= dcst ?o)
  7843. (if (looking-at org-complex-heading-regexp)
  7844. (- 9999 (length (member (match-string 2)
  7845. org-todo-keywords-1)))))
  7846. ((= dcst ?f)
  7847. (if getkey-func
  7848. (progn
  7849. (setq tmp (funcall getkey-func))
  7850. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7851. tmp)
  7852. (error "Invalid key function `%s'" getkey-func)))
  7853. (t (error "Invalid sorting type `%c'" sorting-type))))
  7854. nil
  7855. (cond
  7856. ((= dcst ?a) 'string<)
  7857. ((= dcst ?f) compare-func)
  7858. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7859. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7860. ;; Reset the clock marker if needed
  7861. (when cmstr
  7862. (save-excursion
  7863. (goto-char start)
  7864. (search-forward cmstr nil t)
  7865. (move-marker org-clock-marker (point))))
  7866. (message "Sorting entries...done")))
  7867. (defun org-do-sort (table what &optional with-case sorting-type)
  7868. "Sort TABLE of WHAT according to SORTING-TYPE.
  7869. The user will be prompted for the SORTING-TYPE if the call to this
  7870. function does not specify it. WHAT is only for the prompt, to indicate
  7871. what is being sorted. The sorting key will be extracted from
  7872. the car of the elements of the table.
  7873. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7874. (unless sorting-type
  7875. (message
  7876. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7877. what)
  7878. (setq sorting-type (read-char-exclusive)))
  7879. (let ((dcst (downcase sorting-type))
  7880. extractfun comparefun)
  7881. ;; Define the appropriate functions
  7882. (cond
  7883. ((= dcst ?n)
  7884. (setq extractfun 'string-to-number
  7885. comparefun (if (= dcst sorting-type) '< '>)))
  7886. ((= dcst ?a)
  7887. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7888. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7889. comparefun (if (= dcst sorting-type)
  7890. 'string<
  7891. (lambda (a b) (and (not (string< a b))
  7892. (not (string= a b)))))))
  7893. ((= dcst ?t)
  7894. (setq extractfun
  7895. (lambda (x)
  7896. (if (or (string-match org-ts-regexp x)
  7897. (string-match org-ts-regexp-both x))
  7898. (org-float-time
  7899. (org-time-string-to-time (match-string 0 x)))
  7900. 0))
  7901. comparefun (if (= dcst sorting-type) '< '>)))
  7902. (t (error "Invalid sorting type `%c'" sorting-type)))
  7903. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7904. table)
  7905. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7906. ;;; The orgstruct minor mode
  7907. ;; Define a minor mode which can be used in other modes in order to
  7908. ;; integrate the org-mode structure editing commands.
  7909. ;; This is really a hack, because the org-mode structure commands use
  7910. ;; keys which normally belong to the major mode. Here is how it
  7911. ;; works: The minor mode defines all the keys necessary to operate the
  7912. ;; structure commands, but wraps the commands into a function which
  7913. ;; tests if the cursor is currently at a headline or a plain list
  7914. ;; item. If that is the case, the structure command is used,
  7915. ;; temporarily setting many Org-mode variables like regular
  7916. ;; expressions for filling etc. However, when any of those keys is
  7917. ;; used at a different location, function uses `key-binding' to look
  7918. ;; up if the key has an associated command in another currently active
  7919. ;; keymap (minor modes, major mode, global), and executes that
  7920. ;; command. There might be problems if any of the keys is otherwise
  7921. ;; used as a prefix key.
  7922. (defcustom orgstruct-heading-prefix-regexp nil
  7923. "Regexp that matches the custom prefix of Org headlines in
  7924. orgstruct(++)-mode."
  7925. :group 'org
  7926. :version "24.4"
  7927. :package-version '(Org . "8.0")
  7928. :type 'string)
  7929. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7930. (defcustom orgstruct-setup-hook nil
  7931. "Hook run after orgstruct-mode-map is filled."
  7932. :group 'org
  7933. :version "24.4"
  7934. :package-version '(Org . "8.0")
  7935. :type 'hook)
  7936. (defvar orgstruct-initialized nil)
  7937. (defvar org-local-vars nil
  7938. "List of local variables, for use by `orgstruct-mode'.")
  7939. ;;;###autoload
  7940. (define-minor-mode orgstruct-mode
  7941. "Toggle the minor mode `orgstruct-mode'.
  7942. This mode is for using Org-mode structure commands in other
  7943. modes. The following keys behave as if Org-mode were active, if
  7944. the cursor is on a headline, or on a plain list item (both as
  7945. defined by Org-mode)."
  7946. nil " OrgStruct" (make-sparse-keymap)
  7947. (funcall (if orgstruct-mode
  7948. 'add-to-invisibility-spec
  7949. 'remove-from-invisibility-spec)
  7950. '(outline . t))
  7951. (when orgstruct-mode
  7952. (org-load-modules-maybe)
  7953. (unless orgstruct-initialized
  7954. (orgstruct-setup)
  7955. (setq orgstruct-initialized t))))
  7956. ;;;###autoload
  7957. (defun turn-on-orgstruct ()
  7958. "Unconditionally turn on `orgstruct-mode'."
  7959. (orgstruct-mode 1))
  7960. (defvar org-fb-vars nil)
  7961. (make-variable-buffer-local 'org-fb-vars)
  7962. (defun orgstruct++-mode (&optional arg)
  7963. "Toggle `orgstruct-mode', the enhanced version of it.
  7964. In addition to setting orgstruct-mode, this also exports all
  7965. indentation and autofilling variables from org-mode into the
  7966. buffer. It will also recognize item context in multiline items."
  7967. (interactive "P")
  7968. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7969. (if (< arg 1)
  7970. (progn (orgstruct-mode -1)
  7971. (mapc (lambda(v)
  7972. (org-set-local (car v)
  7973. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7974. org-fb-vars))
  7975. (orgstruct-mode 1)
  7976. (setq org-fb-vars nil)
  7977. (let (var val)
  7978. (mapc
  7979. (lambda (x)
  7980. (when (string-match
  7981. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7982. (symbol-name (car x)))
  7983. (setq var (car x) val (nth 1 x))
  7984. (push (list var `(quote ,(eval var))) org-fb-vars)
  7985. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7986. org-local-vars)
  7987. (org-set-local 'orgstruct-is-++ t))))
  7988. (defvar orgstruct-is-++ nil
  7989. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7990. (make-variable-buffer-local 'orgstruct-is-++)
  7991. ;;;###autoload
  7992. (defun turn-on-orgstruct++ ()
  7993. "Unconditionally turn on `orgstruct++-mode'."
  7994. (orgstruct++-mode 1))
  7995. (defun orgstruct-error ()
  7996. "Error when there is no default binding for a structure key."
  7997. (interactive)
  7998. (funcall (if (fboundp 'user-error)
  7999. 'user-error
  8000. 'error)
  8001. "This key has no function outside structure elements"))
  8002. (defun orgstruct-setup ()
  8003. "Setup orgstruct keymap."
  8004. (dolist (cell '((org-demote . t)
  8005. (org-metaleft . t)
  8006. (org-metaright . t)
  8007. (org-promote . t)
  8008. (org-shiftmetaleft . t)
  8009. (org-shiftmetaright . t)
  8010. org-backward-element
  8011. org-backward-heading-same-level
  8012. org-ctrl-c-ret
  8013. org-ctrl-c-minus
  8014. org-ctrl-c-star
  8015. org-cycle
  8016. org-forward-heading-same-level
  8017. org-insert-heading
  8018. org-insert-heading-respect-content
  8019. org-kill-note-or-show-branches
  8020. org-mark-subtree
  8021. org-meta-return
  8022. org-metadown
  8023. org-metaup
  8024. org-narrow-to-subtree
  8025. org-promote-subtree
  8026. org-reveal
  8027. org-shiftdown
  8028. org-shiftleft
  8029. org-shiftmetadown
  8030. org-shiftmetaup
  8031. org-shiftright
  8032. org-shifttab
  8033. org-shifttab
  8034. org-shiftup
  8035. org-show-subtree
  8036. org-sort
  8037. org-up-element
  8038. outline-demote
  8039. outline-next-visible-heading
  8040. outline-previous-visible-heading
  8041. outline-promote
  8042. outline-up-heading
  8043. show-children))
  8044. (let ((f (or (car-safe cell) cell))
  8045. (disable-when-heading-prefix (cdr-safe cell)))
  8046. (when (fboundp f)
  8047. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8048. (where-is-internal f outline-mode-map)))
  8049. ;; TODO use local-function-key-map
  8050. (dolist (rep '(("<tab>" . "TAB")
  8051. ("<return>" . "RET")
  8052. ("<escape>" . "ESC")
  8053. ("<delete>" . "DEL")))
  8054. (setq binding (read-kbd-macro
  8055. (let ((case-fold-search))
  8056. (replace-regexp-in-string
  8057. (regexp-quote (cdr rep))
  8058. (car rep)
  8059. (key-description binding))))))
  8060. (let ((key (lookup-key orgstruct-mode-map binding)))
  8061. (when (or (not key) (numberp key))
  8062. (condition-case nil
  8063. (org-defkey orgstruct-mode-map
  8064. binding
  8065. (orgstruct-make-binding f binding disable-when-heading-prefix))
  8066. (error nil))))))))
  8067. (run-hooks 'orgstruct-setup-hook))
  8068. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8069. "Create a function for binding in the structure minor mode.
  8070. FUN is the command to call inside a table. KEY is the key that
  8071. should be checked in for a command to execute outside of tables.
  8072. Non-nil DISABLE-WHEN-HEADING-PREFIX means to disable the command
  8073. if `orgstruct-heading-prefix-regexp' is non-nil."
  8074. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8075. (let ((nname name)
  8076. (i 0))
  8077. (while (fboundp (intern nname))
  8078. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8079. (setq name (intern nname)))
  8080. (eval
  8081. (let ((bindings '((org-heading-regexp
  8082. (concat "^"
  8083. orgstruct-heading-prefix-regexp
  8084. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8085. (org-outline-regexp
  8086. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8087. (org-outline-regexp-bol
  8088. (concat "^" org-outline-regexp))
  8089. (outline-regexp org-outline-regexp)
  8090. (outline-heading-end-regexp "\n")
  8091. (outline-level 'org-outline-level)
  8092. (outline-heading-alist))))
  8093. `(defun ,name (arg)
  8094. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8095. "Outside of structure, run the binding of `"
  8096. (key-description key) "'."
  8097. (when disable-when-heading-prefix
  8098. (concat
  8099. "\nIf `orgstruct-heading-prefix-regexp' is non-nil, this command will always fall\n"
  8100. "back to the default binding due to limitations of Org's implementation of\n"
  8101. "`" (symbol-name fun) "'.")))
  8102. (interactive "p")
  8103. (let* ((disable
  8104. ,(when disable-when-heading-prefix
  8105. '(and orgstruct-heading-prefix-regexp
  8106. (not (string= orgstruct-heading-prefix-regexp "")))))
  8107. (fallback
  8108. (or disable
  8109. (not
  8110. (let* ,bindings
  8111. (org-context-p 'headline 'item
  8112. ,(when (memq fun '(org-insert-heading))
  8113. '(when orgstruct-is-++
  8114. 'item-body))))))))
  8115. (if fallback
  8116. (let* ((orgstruct-mode)
  8117. (binding
  8118. (loop with key = ,key
  8119. for rep in
  8120. '(nil
  8121. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8122. ("<\\([^>]*\\)return>" . "\\1RET")
  8123. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8124. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8125. do
  8126. (when rep
  8127. (setq key (read-kbd-macro
  8128. (let ((case-fold-search))
  8129. (replace-regexp-in-string
  8130. (car rep)
  8131. (cdr rep)
  8132. (key-description key))))))
  8133. thereis (key-binding key))))
  8134. (if (keymapp binding)
  8135. (set-temporary-overlay-map binding)
  8136. (let ((func (or binding
  8137. (unless disable
  8138. 'orgstruct-error))))
  8139. (when func
  8140. (call-interactively func)))))
  8141. (org-run-like-in-org-mode
  8142. (lambda ()
  8143. (interactive)
  8144. (let* ,bindings
  8145. (call-interactively ',fun)))))))))
  8146. name))
  8147. (defun org-contextualize-keys (alist contexts)
  8148. "Return valid elements in ALIST depending on CONTEXTS.
  8149. `org-agenda-custom-commands' or `org-capture-templates' are the
  8150. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8151. or `org-capture-templates-contexts' are the associated contexts
  8152. definitions."
  8153. (let ((contexts
  8154. ;; normalize contexts
  8155. (mapcar
  8156. (lambda(c) (cond ((listp (cadr c))
  8157. (list (car c) (car c) (cadr c)))
  8158. ((string= "" (cadr c))
  8159. (list (car c) (car c) (caddr c)))
  8160. (t c))) contexts))
  8161. (a alist) c r s)
  8162. ;; loop over all commands or templates
  8163. (while (setq c (pop a))
  8164. (let (vrules repl)
  8165. (cond
  8166. ((not (assoc (car c) contexts))
  8167. (push c r))
  8168. ((and (assoc (car c) contexts)
  8169. (setq vrules (org-contextualize-validate-key
  8170. (car c) contexts)))
  8171. (mapc (lambda (vr)
  8172. (when (not (equal (car vr) (cadr vr)))
  8173. (setq repl vr))) vrules)
  8174. (if (not repl) (push c r)
  8175. (push (cadr repl) s)
  8176. (push
  8177. (cons (car c)
  8178. (cdr (or (assoc (cadr repl) alist)
  8179. (error "Undefined key `%s' as contextual replacement for `%s'"
  8180. (cadr repl) (car c)))))
  8181. r))))))
  8182. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8183. (delq
  8184. nil
  8185. (delete-dups
  8186. (mapcar (lambda (x)
  8187. (let ((tpl (car x)))
  8188. (when (not (delq
  8189. nil
  8190. (mapcar (lambda(y)
  8191. (equal y tpl)) s))) x)))
  8192. (reverse r))))))
  8193. (defun org-contextualize-validate-key (key contexts)
  8194. "Check CONTEXTS for agenda or capture KEY."
  8195. (let (r rr res)
  8196. (while (setq r (pop contexts))
  8197. (mapc
  8198. (lambda (rr)
  8199. (when
  8200. (and (equal key (car r))
  8201. (if (functionp rr) (funcall rr)
  8202. (or (and (eq (car rr) 'in-file)
  8203. (buffer-file-name)
  8204. (string-match (cdr rr) (buffer-file-name)))
  8205. (and (eq (car rr) 'in-mode)
  8206. (string-match (cdr rr) (symbol-name major-mode)))
  8207. (and (eq (car rr) 'in-buffer)
  8208. (string-match (cdr rr) (buffer-name)))
  8209. (when (and (eq (car rr) 'not-in-file)
  8210. (buffer-file-name))
  8211. (not (string-match (cdr rr) (buffer-file-name))))
  8212. (when (eq (car rr) 'not-in-mode)
  8213. (not (string-match (cdr rr) (symbol-name major-mode))))
  8214. (when (eq (car rr) 'not-in-buffer)
  8215. (not (string-match (cdr rr) (buffer-name)))))))
  8216. (push r res)))
  8217. (car (last r))))
  8218. (delete-dups (delq nil res))))
  8219. (defun org-context-p (&rest contexts)
  8220. "Check if local context is any of CONTEXTS.
  8221. Possible values in the list of contexts are `table', `headline', and `item'."
  8222. (let ((pos (point)))
  8223. (goto-char (point-at-bol))
  8224. (prog1 (or (and (memq 'table contexts)
  8225. (looking-at "[ \t]*|"))
  8226. (and (memq 'headline contexts)
  8227. (looking-at org-outline-regexp))
  8228. (and (memq 'item contexts)
  8229. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8230. (and (memq 'item-body contexts)
  8231. (org-in-item-p)))
  8232. (goto-char pos))))
  8233. (defun org-get-local-variables ()
  8234. "Return a list of all local variables in an Org mode buffer."
  8235. (let (varlist)
  8236. (with-current-buffer (get-buffer-create "*Org tmp*")
  8237. (erase-buffer)
  8238. (org-mode)
  8239. (setq varlist (buffer-local-variables)))
  8240. (kill-buffer "*Org tmp*")
  8241. (delq nil
  8242. (mapcar
  8243. (lambda (x)
  8244. (setq x
  8245. (if (symbolp x)
  8246. (list x)
  8247. (list (car x) (cdr x))))
  8248. (if (and (not (get (car x) 'org-state))
  8249. (string-match
  8250. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8251. (symbol-name (car x))))
  8252. x nil))
  8253. varlist))))
  8254. (defun org-clone-local-variables (from-buffer &optional regexp)
  8255. "Clone local variables from FROM-BUFFER.
  8256. Optional argument REGEXP selects variables to clone."
  8257. (mapc
  8258. (lambda (pair)
  8259. (and (symbolp (car pair))
  8260. (or (null regexp)
  8261. (string-match regexp (symbol-name (car pair))))
  8262. (set (make-local-variable (car pair))
  8263. (cdr pair))))
  8264. (buffer-local-variables from-buffer)))
  8265. ;;;###autoload
  8266. (defun org-run-like-in-org-mode (cmd)
  8267. "Run a command, pretending that the current buffer is in Org-mode.
  8268. This will temporarily bind local variables that are typically bound in
  8269. Org-mode to the values they have in Org-mode, and then interactively
  8270. call CMD."
  8271. (org-load-modules-maybe)
  8272. (unless org-local-vars
  8273. (setq org-local-vars (org-get-local-variables)))
  8274. (let (binds)
  8275. (dolist (var org-local-vars)
  8276. (when (or (not (boundp (car var)))
  8277. (eq (symbol-value (car var))
  8278. (default-value (car var))))
  8279. (push (list (car var) `(quote ,(cadr var))) binds)))
  8280. (eval `(let ,binds
  8281. (call-interactively (quote ,cmd))))))
  8282. ;;;; Archiving
  8283. (defun org-get-category (&optional pos force-refresh)
  8284. "Get the category applying to position POS."
  8285. (save-match-data
  8286. (if force-refresh (org-refresh-category-properties))
  8287. (let ((pos (or pos (point))))
  8288. (or (get-text-property pos 'org-category)
  8289. (progn (org-refresh-category-properties)
  8290. (get-text-property pos 'org-category))))))
  8291. (defun org-refresh-category-properties ()
  8292. "Refresh category text properties in the buffer."
  8293. (let ((case-fold-search t)
  8294. (inhibit-read-only t)
  8295. (def-cat (cond
  8296. ((null org-category)
  8297. (if buffer-file-name
  8298. (file-name-sans-extension
  8299. (file-name-nondirectory buffer-file-name))
  8300. "???"))
  8301. ((symbolp org-category) (symbol-name org-category))
  8302. (t org-category)))
  8303. beg end cat pos optionp)
  8304. (org-with-silent-modifications
  8305. (save-excursion
  8306. (save-restriction
  8307. (widen)
  8308. (goto-char (point-min))
  8309. (put-text-property (point) (point-max) 'org-category def-cat)
  8310. (while (re-search-forward
  8311. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8312. (setq pos (match-end 0)
  8313. optionp (equal (char-after (match-beginning 0)) ?#)
  8314. cat (org-trim (match-string 2)))
  8315. (if optionp
  8316. (setq beg (point-at-bol) end (point-max))
  8317. (org-back-to-heading t)
  8318. (setq beg (point) end (org-end-of-subtree t t)))
  8319. (put-text-property beg end 'org-category cat)
  8320. (put-text-property beg end 'org-category-position beg)
  8321. (goto-char pos)))))))
  8322. (defun org-refresh-properties (dprop tprop)
  8323. "Refresh buffer text properties.
  8324. DPROP is the drawer property and TPROP is the corresponding text
  8325. property to set."
  8326. (let ((case-fold-search t)
  8327. (inhibit-read-only t) p)
  8328. (org-with-silent-modifications
  8329. (save-excursion
  8330. (save-restriction
  8331. (widen)
  8332. (goto-char (point-min))
  8333. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8334. (setq p (org-match-string-no-properties 1))
  8335. (save-excursion
  8336. (org-back-to-heading t)
  8337. (put-text-property
  8338. (point-at-bol) (point-at-eol) tprop p))))))))
  8339. ;;;; Link Stuff
  8340. ;;; Link abbreviations
  8341. (defun org-link-expand-abbrev (link)
  8342. "Apply replacements as defined in `org-link-abbrev-alist'."
  8343. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8344. (let* ((key (match-string 1 link))
  8345. (as (or (assoc key org-link-abbrev-alist-local)
  8346. (assoc key org-link-abbrev-alist)))
  8347. (tag (and (match-end 2) (match-string 3 link)))
  8348. rpl)
  8349. (if (not as)
  8350. link
  8351. (setq rpl (cdr as))
  8352. (cond
  8353. ((symbolp rpl) (funcall rpl tag))
  8354. ((string-match "%(\\([^)]+\\))" rpl)
  8355. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8356. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8357. ((string-match "%h" rpl)
  8358. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8359. (t (concat rpl tag)))))
  8360. link))
  8361. ;;; Storing and inserting links
  8362. (defvar org-insert-link-history nil
  8363. "Minibuffer history for links inserted with `org-insert-link'.")
  8364. (defvar org-stored-links nil
  8365. "Contains the links stored with `org-store-link'.")
  8366. (defvar org-store-link-plist nil
  8367. "Plist with info about the most recently link created with `org-store-link'.")
  8368. (defvar org-link-protocols nil
  8369. "Link protocols added to Org-mode using `org-add-link-type'.")
  8370. (defvar org-store-link-functions nil
  8371. "List of functions that are called to create and store a link.
  8372. Each function will be called in turn until one returns a non-nil
  8373. value. Each function should check if it is responsible for creating
  8374. this link (for example by looking at the major mode).
  8375. If not, it must exit and return nil.
  8376. If yes, it should return a non-nil value after a calling
  8377. `org-store-link-props' with a list of properties and values.
  8378. Special properties are:
  8379. :type The link prefix, like \"http\". This must be given.
  8380. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8381. This is obligatory as well.
  8382. :description Optional default description for the second pair
  8383. of brackets in an Org-mode link. The user can still change
  8384. this when inserting this link into an Org-mode buffer.
  8385. In addition to these, any additional properties can be specified
  8386. and then used in capture templates.")
  8387. (defun org-add-link-type (type &optional follow export)
  8388. "Add TYPE to the list of `org-link-types'.
  8389. Re-compute all regular expressions depending on `org-link-types'
  8390. FOLLOW and EXPORT are two functions.
  8391. FOLLOW should take the link path as the single argument and do whatever
  8392. is necessary to follow the link, for example find a file or display
  8393. a mail message.
  8394. EXPORT should format the link path for export to one of the export formats.
  8395. It should be a function accepting three arguments:
  8396. path the path of the link, the text after the prefix (like \"http:\")
  8397. desc the description of the link, if any, or a description added by
  8398. org-export-normalize-links if there is none
  8399. format the export format, a symbol like `html' or `latex' or `ascii'..
  8400. The function may use the FORMAT information to return different values
  8401. depending on the format. The return value will be put literally into
  8402. the exported file. If the return value is nil, this means Org should
  8403. do what it normally does with links which do not have EXPORT defined.
  8404. Org-mode has a built-in default for exporting links. If you are happy with
  8405. this default, there is no need to define an export function for the link
  8406. type. For a simple example of an export function, see `org-bbdb.el'."
  8407. (add-to-list 'org-link-types type t)
  8408. (org-make-link-regexps)
  8409. (if (assoc type org-link-protocols)
  8410. (setcdr (assoc type org-link-protocols) (list follow export))
  8411. (push (list type follow export) org-link-protocols)))
  8412. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8413. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8414. ;;;###autoload
  8415. (defun org-store-link (arg)
  8416. "\\<org-mode-map>Store an org-link to the current location.
  8417. This link is added to `org-stored-links' and can later be inserted
  8418. into an org-buffer with \\[org-insert-link].
  8419. For some link types, a prefix arg is interpreted.
  8420. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8421. For file links, arg negates `org-context-in-file-links'.
  8422. A double prefix arg force skipping storing functions that are not
  8423. part of Org's core.
  8424. A triple prefix arg force storing a link for each line in the
  8425. active region."
  8426. (interactive "P")
  8427. (org-load-modules-maybe)
  8428. (if (and (equal arg '(64)) (org-region-active-p))
  8429. (save-excursion
  8430. (let ((end (region-end)))
  8431. (goto-char (region-beginning))
  8432. (set-mark (point))
  8433. (while (< (point-at-eol) end)
  8434. (move-end-of-line 1) (activate-mark)
  8435. (let (current-prefix-arg)
  8436. (call-interactively 'org-store-link))
  8437. (move-beginning-of-line 2)
  8438. (set-mark (point)))))
  8439. (org-with-limited-levels
  8440. (setq org-store-link-plist nil)
  8441. (let (link cpltxt desc description search
  8442. txt custom-id agenda-link sfuns sfunsn)
  8443. (cond
  8444. ;; Store a link using an external link type
  8445. ((and (not (equal arg '(16)))
  8446. (setq sfuns
  8447. (delq
  8448. nil (mapcar (lambda (f)
  8449. (let (fs) (if (funcall f) (push f fs))))
  8450. org-store-link-functions))
  8451. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8452. (or (and (cdr sfuns)
  8453. (funcall (intern
  8454. (completing-read
  8455. "Which function for creating the link? "
  8456. sfunsn t (car sfunsn)))))
  8457. (funcall (caar sfuns)))
  8458. (setq link (plist-get org-store-link-plist :link)
  8459. desc (or (plist-get org-store-link-plist
  8460. :description) link))))
  8461. ;; Store a link from a source code buffer
  8462. ((org-src-edit-buffer-p)
  8463. (let (label gc)
  8464. (while (or (not label)
  8465. (save-excursion
  8466. (save-restriction
  8467. (widen)
  8468. (goto-char (point-min))
  8469. (re-search-forward
  8470. (regexp-quote (format org-coderef-label-format label))
  8471. nil t))))
  8472. (when label (message "Label exists already") (sit-for 2))
  8473. (setq label (read-string "Code line label: " label)))
  8474. (end-of-line 1)
  8475. (setq link (format org-coderef-label-format label))
  8476. (setq gc (- 79 (length link)))
  8477. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8478. (insert link)
  8479. (setq link (concat "(" label ")") desc nil)))
  8480. ;; We are in the agenda, link to referenced location
  8481. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8482. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8483. (get-text-property (point) 'org-marker))))
  8484. (when m
  8485. (org-with-point-at m
  8486. (setq agenda-link
  8487. (if (org-called-interactively-p 'any)
  8488. (call-interactively 'org-store-link)
  8489. (org-store-link nil)))))))
  8490. ((eq major-mode 'calendar-mode)
  8491. (let ((cd (calendar-cursor-to-date)))
  8492. (setq link
  8493. (format-time-string
  8494. (car org-time-stamp-formats)
  8495. (apply 'encode-time
  8496. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8497. nil nil nil))))
  8498. (org-store-link-props :type "calendar" :date cd)))
  8499. ((eq major-mode 'help-mode)
  8500. (setq link (concat "help:" (save-excursion
  8501. (goto-char (point-min))
  8502. (looking-at "^[^ ]+")
  8503. (match-string 0))))
  8504. (org-store-link-props :type "help"))
  8505. ((eq major-mode 'w3-mode)
  8506. (setq cpltxt (if (and (buffer-name)
  8507. (not (string-match "Untitled" (buffer-name))))
  8508. (buffer-name)
  8509. (url-view-url t))
  8510. link (url-view-url t))
  8511. (org-store-link-props :type "w3" :url (url-view-url t)))
  8512. ((eq major-mode 'image-mode)
  8513. (setq cpltxt (concat "file:"
  8514. (abbreviate-file-name buffer-file-name))
  8515. link cpltxt)
  8516. (org-store-link-props :type "image" :file buffer-file-name))
  8517. ;; In dired, store a link to the file of the current line
  8518. ((eq major-mode 'dired-mode)
  8519. (let ((file (dired-get-filename nil t)))
  8520. (setq file (if file
  8521. (abbreviate-file-name
  8522. (expand-file-name (dired-get-filename nil t)))
  8523. ;; otherwise, no file so use current directory.
  8524. default-directory))
  8525. (setq cpltxt (concat "file:" file)
  8526. link cpltxt)))
  8527. ((setq search (run-hook-with-args-until-success
  8528. 'org-create-file-search-functions))
  8529. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8530. "::" search))
  8531. (setq cpltxt (or description link)))
  8532. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8533. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8534. (cond
  8535. ;; Store a link using the radio target at point
  8536. ((org-in-regexp "<<\\(.*?\\)>>")
  8537. (setq cpltxt
  8538. (concat "file:"
  8539. (abbreviate-file-name
  8540. (buffer-file-name (buffer-base-buffer)))
  8541. "::" (match-string 1))
  8542. link cpltxt))
  8543. ((and (featurep 'org-id)
  8544. (or (eq org-id-link-to-org-use-id t)
  8545. (and (org-called-interactively-p 'any)
  8546. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8547. (and (eq org-id-link-to-org-use-id
  8548. 'create-if-interactive-and-no-custom-id)
  8549. (not custom-id))))
  8550. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8551. ;; Store a link using the ID at point
  8552. (setq link (condition-case nil
  8553. (prog1 (org-id-store-link)
  8554. (setq desc (plist-get org-store-link-plist
  8555. :description)))
  8556. (error
  8557. ;; Probably before first headline, link only to file
  8558. (concat "file:"
  8559. (abbreviate-file-name
  8560. (buffer-file-name (buffer-base-buffer))))))))
  8561. (t
  8562. ;; Just link to current headline
  8563. (setq cpltxt (concat "file:"
  8564. (abbreviate-file-name
  8565. (buffer-file-name (buffer-base-buffer)))))
  8566. ;; Add a context search string
  8567. (when (org-xor org-context-in-file-links arg)
  8568. (let* ((ee (org-element-at-point))
  8569. (et (org-element-type ee))
  8570. (ev (plist-get (cadr ee) :value))
  8571. (ek (plist-get (cadr ee) :key))
  8572. (eok (and (stringp ek) (string-match "name" ek))))
  8573. (setq txt (cond
  8574. ((org-at-heading-p) nil)
  8575. ((and (eq et 'keyword) eok) ev)
  8576. ((org-region-active-p)
  8577. (buffer-substring (region-beginning) (region-end)))))
  8578. (when (or (null txt) (string-match "\\S-" txt))
  8579. (setq cpltxt
  8580. (concat cpltxt "::"
  8581. (condition-case nil
  8582. (org-make-org-heading-search-string txt)
  8583. (error "")))
  8584. desc (or (and (eq et 'keyword) eok ev)
  8585. (nth 4 (ignore-errors (org-heading-components)))
  8586. "NONE")))))
  8587. (if (string-match "::\\'" cpltxt)
  8588. (setq cpltxt (substring cpltxt 0 -2)))
  8589. (setq link cpltxt))))
  8590. ((buffer-file-name (buffer-base-buffer))
  8591. ;; Just link to this file here.
  8592. (setq cpltxt (concat "file:"
  8593. (abbreviate-file-name
  8594. (buffer-file-name (buffer-base-buffer)))))
  8595. ;; Add a context string.
  8596. (when (org-xor org-context-in-file-links arg)
  8597. (setq txt (if (org-region-active-p)
  8598. (buffer-substring (region-beginning) (region-end))
  8599. (buffer-substring (point-at-bol) (point-at-eol))))
  8600. ;; Only use search option if there is some text.
  8601. (when (string-match "\\S-" txt)
  8602. (setq cpltxt
  8603. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8604. desc "NONE")))
  8605. (setq link cpltxt))
  8606. ((org-called-interactively-p 'interactive)
  8607. (user-error "No method for storing a link from this buffer"))
  8608. (t (setq link nil)))
  8609. ;; We're done setting link and desc, clean up
  8610. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8611. (setq link (or link cpltxt)
  8612. desc (or desc cpltxt))
  8613. (cond ((equal desc "NONE") (setq desc nil))
  8614. ((string-match org-bracket-link-regexp desc)
  8615. (setq desc
  8616. (replace-regexp-in-string
  8617. org-bracket-link-regexp
  8618. (concat "\\3" (if (equal (length (match-string 0 desc))
  8619. (length desc)) "*" "")) desc))))
  8620. ;; Return the link
  8621. (if (not (and (or (org-called-interactively-p 'any)
  8622. executing-kbd-macro) link))
  8623. (or agenda-link (and link (org-make-link-string link desc)))
  8624. (push (list link desc) org-stored-links)
  8625. (message "Stored: %s" (or desc link))
  8626. (when custom-id
  8627. (setq link (concat "file:" (abbreviate-file-name
  8628. (buffer-file-name)) "::#" custom-id))
  8629. (push (list link desc) org-stored-links)))))))
  8630. (defun org-store-link-props (&rest plist)
  8631. "Store link properties, extract names and addresses."
  8632. (let (x adr)
  8633. (when (setq x (plist-get plist :from))
  8634. (setq adr (mail-extract-address-components x))
  8635. (setq plist (plist-put plist :fromname (car adr)))
  8636. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8637. (when (setq x (plist-get plist :to))
  8638. (setq adr (mail-extract-address-components x))
  8639. (setq plist (plist-put plist :toname (car adr)))
  8640. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8641. (let ((from (plist-get plist :from))
  8642. (to (plist-get plist :to)))
  8643. (when (and from to org-from-is-user-regexp)
  8644. (setq plist
  8645. (plist-put plist :fromto
  8646. (if (string-match org-from-is-user-regexp from)
  8647. (concat "to %t")
  8648. (concat "from %f"))))))
  8649. (setq org-store-link-plist plist))
  8650. (defun org-add-link-props (&rest plist)
  8651. "Add these properties to the link property list."
  8652. (let (key value)
  8653. (while plist
  8654. (setq key (pop plist) value (pop plist))
  8655. (setq org-store-link-plist
  8656. (plist-put org-store-link-plist key value)))))
  8657. (defun org-email-link-description (&optional fmt)
  8658. "Return the description part of an email link.
  8659. This takes information from `org-store-link-plist' and formats it
  8660. according to FMT (default from `org-email-link-description-format')."
  8661. (setq fmt (or fmt org-email-link-description-format))
  8662. (let* ((p org-store-link-plist)
  8663. (to (plist-get p :toaddress))
  8664. (from (plist-get p :fromaddress))
  8665. (table
  8666. (list
  8667. (cons "%c" (plist-get p :fromto))
  8668. (cons "%F" (plist-get p :from))
  8669. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8670. (cons "%T" (plist-get p :to))
  8671. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8672. (cons "%s" (plist-get p :subject))
  8673. (cons "%d" (plist-get p :date))
  8674. (cons "%m" (plist-get p :message-id)))))
  8675. (when (string-match "%c" fmt)
  8676. ;; Check if the user wrote this message
  8677. (if (and org-from-is-user-regexp from to
  8678. (save-match-data (string-match org-from-is-user-regexp from)))
  8679. (setq fmt (replace-match "to %t" t t fmt))
  8680. (setq fmt (replace-match "from %f" t t fmt))))
  8681. (org-replace-escapes fmt table)))
  8682. (defun org-make-org-heading-search-string (&optional string)
  8683. "Make search string for the current headline or STRING."
  8684. (let ((s (or string
  8685. (and (derived-mode-p 'org-mode)
  8686. (save-excursion
  8687. (org-back-to-heading t)
  8688. (org-element-property :raw-value (org-element-at-point))))))
  8689. (lines org-context-in-file-links))
  8690. (or string (setq s (concat "*" s))) ; Add * for headlines
  8691. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8692. (when (and string (integerp lines) (> lines 0))
  8693. (let ((slines (org-split-string s "\n")))
  8694. (when (< lines (length slines))
  8695. (setq s (mapconcat
  8696. 'identity
  8697. (reverse (nthcdr (- (length slines) lines)
  8698. (reverse slines))) "\n")))))
  8699. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8700. (defun org-make-link-string (link &optional description)
  8701. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8702. (unless (string-match "\\S-" link)
  8703. (error "Empty link"))
  8704. (when (and description
  8705. (stringp description)
  8706. (not (string-match "\\S-" description)))
  8707. (setq description nil))
  8708. (when (stringp description)
  8709. ;; Remove brackets from the description, they are fatal.
  8710. (while (string-match "\\[" description)
  8711. (setq description (replace-match "{" t t description)))
  8712. (while (string-match "\\]" description)
  8713. (setq description (replace-match "}" t t description))))
  8714. (when (equal link description)
  8715. ;; No description needed, it is identical
  8716. (setq description nil))
  8717. (when (and (not description)
  8718. (not (string-match (org-image-file-name-regexp) link))
  8719. (not (equal link (org-link-escape link))))
  8720. (setq description (org-extract-attributes link)))
  8721. (setq link
  8722. (cond ((string-match (org-image-file-name-regexp) link) link)
  8723. ((string-match org-link-types-re link)
  8724. (concat (match-string 1 link)
  8725. (org-link-escape (substring link (match-end 1)))))
  8726. (t (org-link-escape link))))
  8727. (concat "[[" link "]"
  8728. (if description (concat "[" description "]") "")
  8729. "]"))
  8730. (defconst org-link-escape-chars
  8731. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8732. "List of characters that should be escaped in link.
  8733. This is the list that is used for internal purposes.")
  8734. (defconst org-link-escape-chars-browser
  8735. '(?\ )
  8736. "List of escapes for characters that are problematic in links.
  8737. This is the list that is used before handing over to the browser.")
  8738. (defun org-link-escape (text &optional table merge)
  8739. "Return percent escaped representation of TEXT.
  8740. TEXT is a string with the text to escape.
  8741. Optional argument TABLE is a list with characters that should be
  8742. escaped. When nil, `org-link-escape-chars' is used.
  8743. If optional argument MERGE is set, merge TABLE into
  8744. `org-link-escape-chars'."
  8745. (cond
  8746. ((and table merge)
  8747. (mapc (lambda (defchr)
  8748. (unless (member defchr table)
  8749. (setq table (cons defchr table)))) org-link-escape-chars))
  8750. ((null table)
  8751. (setq table org-link-escape-chars)))
  8752. (mapconcat
  8753. (lambda (char)
  8754. (if (or (member char table)
  8755. (and (or (< char 32) (= char 37) (> char 126))
  8756. org-url-hexify-p))
  8757. (mapconcat (lambda (sequence-element)
  8758. (format "%%%.2X" sequence-element))
  8759. (or (encode-coding-char char 'utf-8)
  8760. (error "Unable to percent escape character: %s"
  8761. (char-to-string char))) "")
  8762. (char-to-string char))) text ""))
  8763. (defun org-link-unescape (str)
  8764. "Unhex hexified Unicode strings as returned from the JavaScript function
  8765. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8766. (unless (and (null str) (string= "" str))
  8767. (let ((pos 0) (case-fold-search t) unhexed)
  8768. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8769. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8770. (setq str (replace-match unhexed t t str))
  8771. (setq pos (+ pos (length unhexed))))))
  8772. str)
  8773. (defun org-link-unescape-compound (hex)
  8774. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8775. Note: this function also decodes single byte encodings like
  8776. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8777. (save-match-data
  8778. (let* ((bytes (cdr (split-string hex "%")))
  8779. (ret "")
  8780. (eat 0)
  8781. (sum 0))
  8782. (while bytes
  8783. (let* ((val (string-to-number (pop bytes) 16))
  8784. (shift-xor
  8785. (if (= 0 eat)
  8786. (cond
  8787. ((>= val 252) (cons 6 252))
  8788. ((>= val 248) (cons 5 248))
  8789. ((>= val 240) (cons 4 240))
  8790. ((>= val 224) (cons 3 224))
  8791. ((>= val 192) (cons 2 192))
  8792. (t (cons 0 0)))
  8793. (cons 6 128))))
  8794. (if (>= val 192) (setq eat (car shift-xor)))
  8795. (setq val (logxor val (cdr shift-xor)))
  8796. (setq sum (+ (lsh sum (car shift-xor)) val))
  8797. (if (> eat 0) (setq eat (- eat 1)))
  8798. (cond
  8799. ((= 0 eat) ;multi byte
  8800. (setq ret (concat ret (org-char-to-string sum)))
  8801. (setq sum 0))
  8802. ((not bytes) ; single byte(s)
  8803. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8804. )) ;; end (while bytes
  8805. ret )))
  8806. (defun org-link-unescape-single-byte-sequence (hex)
  8807. "Unhexify hex-encoded single byte character sequences."
  8808. (mapconcat (lambda (byte)
  8809. (char-to-string (string-to-number byte 16)))
  8810. (cdr (split-string hex "%")) ""))
  8811. (defun org-xor (a b)
  8812. "Exclusive or."
  8813. (if a (not b) b))
  8814. (defun org-fixup-message-id-for-http (s)
  8815. "Replace special characters in a message id, so it can be used in an http query."
  8816. (when (string-match "%" s)
  8817. (setq s (mapconcat (lambda (c)
  8818. (if (eq c ?%)
  8819. "%25"
  8820. (char-to-string c)))
  8821. s "")))
  8822. (while (string-match "<" s)
  8823. (setq s (replace-match "%3C" t t s)))
  8824. (while (string-match ">" s)
  8825. (setq s (replace-match "%3E" t t s)))
  8826. (while (string-match "@" s)
  8827. (setq s (replace-match "%40" t t s)))
  8828. s)
  8829. (defun org-link-prettify (link)
  8830. "Return a human-readable representation of LINK.
  8831. The car of LINK must be a raw link the cdr of LINK must be either
  8832. a link description or nil."
  8833. (let ((desc (or (cadr link) "<no description>")))
  8834. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8835. "<" (car link) ">")))
  8836. ;;;###autoload
  8837. (defun org-insert-link-global ()
  8838. "Insert a link like Org-mode does.
  8839. This command can be called in any mode to insert a link in Org-mode syntax."
  8840. (interactive)
  8841. (org-load-modules-maybe)
  8842. (org-run-like-in-org-mode 'org-insert-link))
  8843. (defun org-insert-all-links (&optional keep)
  8844. "Insert all links in `org-stored-links'."
  8845. (interactive "P")
  8846. (let ((links (copy-sequence org-stored-links)) l)
  8847. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8848. (insert "- ")
  8849. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  8850. (insert "\n"))))
  8851. (defun org-link-fontify-links-to-this-file ()
  8852. "Fontify links to the current file in `org-stored-links'."
  8853. (let ((f (buffer-file-name)) a b)
  8854. (setq a (mapcar (lambda(l)
  8855. (let ((ll (car l)))
  8856. (when (and (string-match "^file:\\(.+\\)::" ll)
  8857. (equal f (expand-file-name (match-string 1 ll))))
  8858. ll)))
  8859. org-stored-links))
  8860. (when (featurep 'org-id)
  8861. (setq b (mapcar (lambda(l)
  8862. (let ((ll (car l)))
  8863. (when (and (string-match "^id:\\(.+\\)$" ll)
  8864. (equal f (expand-file-name
  8865. (or (org-id-find-id-file
  8866. (match-string 1 ll)) ""))))
  8867. ll)))
  8868. org-stored-links)))
  8869. (mapcar (lambda(l)
  8870. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8871. (delq nil (append a b)))))
  8872. (defvar org-link-links-in-this-file nil)
  8873. (defun org-insert-link (&optional complete-file link-location default-description)
  8874. "Insert a link. At the prompt, enter the link.
  8875. Completion can be used to insert any of the link protocol prefixes like
  8876. http or ftp in use.
  8877. The history can be used to select a link previously stored with
  8878. `org-store-link'. When the empty string is entered (i.e. if you just
  8879. press RET at the prompt), the link defaults to the most recently
  8880. stored link. As SPC triggers completion in the minibuffer, you need to
  8881. use M-SPC or C-q SPC to force the insertion of a space character.
  8882. You will also be prompted for a description, and if one is given, it will
  8883. be displayed in the buffer instead of the link.
  8884. If there is already a link at point, this command will allow you to edit link
  8885. and description parts.
  8886. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8887. be selected using completion. The path to the file will be relative to the
  8888. current directory if the file is in the current directory or a subdirectory.
  8889. Otherwise, the link will be the absolute path as completed in the minibuffer
  8890. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8891. option `org-link-file-path-type'.
  8892. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8893. the current directory or below.
  8894. With three \\[universal-argument] prefixes, negate the meaning of
  8895. `org-keep-stored-link-after-insertion'.
  8896. If `org-make-link-description-function' is non-nil, this function will be
  8897. called with the link target, and the result will be the default
  8898. link description.
  8899. If the LINK-LOCATION parameter is non-nil, this value will be
  8900. used as the link location instead of reading one interactively.
  8901. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8902. be used as the default description."
  8903. (interactive "P")
  8904. (let* ((wcf (current-window-configuration))
  8905. (origbuf (current-buffer))
  8906. (region (if (org-region-active-p)
  8907. (buffer-substring (region-beginning) (region-end))))
  8908. (remove (and region (list (region-beginning) (region-end))))
  8909. (desc region)
  8910. tmphist ; byte-compile incorrectly complains about this
  8911. (link link-location)
  8912. (abbrevs org-link-abbrev-alist-local)
  8913. entry file all-prefixes auto-desc)
  8914. (cond
  8915. (link-location) ; specified by arg, just use it.
  8916. ((org-in-regexp org-bracket-link-regexp 1)
  8917. ;; We do have a link at point, and we are going to edit it.
  8918. (setq remove (list (match-beginning 0) (match-end 0)))
  8919. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8920. (setq link (read-string "Link: "
  8921. (org-link-unescape
  8922. (org-match-string-no-properties 1)))))
  8923. ((or (org-in-regexp org-angle-link-re)
  8924. (org-in-regexp org-plain-link-re))
  8925. ;; Convert to bracket link
  8926. (setq remove (list (match-beginning 0) (match-end 0))
  8927. link (read-string "Link: "
  8928. (org-remove-angle-brackets (match-string 0)))))
  8929. ((member complete-file '((4) (16)))
  8930. ;; Completing read for file names.
  8931. (setq link (org-file-complete-link complete-file)))
  8932. (t
  8933. ;; Read link, with completion for stored links.
  8934. (org-link-fontify-links-to-this-file)
  8935. (org-switch-to-buffer-other-window "*Org Links*")
  8936. (with-current-buffer "*Org Links*"
  8937. (erase-buffer)
  8938. (insert "Insert a link.
  8939. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8940. (when org-stored-links
  8941. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8942. (insert (mapconcat 'org-link-prettify
  8943. (reverse org-stored-links) "\n")))
  8944. (goto-char (point-min)))
  8945. (let ((cw (selected-window)))
  8946. (select-window (get-buffer-window "*Org Links*" 'visible))
  8947. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8948. (unless (pos-visible-in-window-p (point-max))
  8949. (org-fit-window-to-buffer))
  8950. (and (window-live-p cw) (select-window cw)))
  8951. ;; Fake a link history, containing the stored links.
  8952. (setq tmphist (append (mapcar 'car org-stored-links)
  8953. org-insert-link-history))
  8954. (setq all-prefixes (append (mapcar 'car abbrevs)
  8955. (mapcar 'car org-link-abbrev-alist)
  8956. org-link-types))
  8957. (unwind-protect
  8958. (progn
  8959. (setq link
  8960. (org-completing-read
  8961. "Link: "
  8962. (append
  8963. (mapcar (lambda (x) (concat x ":"))
  8964. all-prefixes)
  8965. (mapcar 'car org-stored-links))
  8966. nil nil nil
  8967. 'tmphist
  8968. (caar org-stored-links)))
  8969. (if (not (string-match "\\S-" link))
  8970. (error "No link selected"))
  8971. (mapc (lambda(l)
  8972. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8973. org-stored-links)
  8974. (if (or (member link all-prefixes)
  8975. (and (equal ":" (substring link -1))
  8976. (member (substring link 0 -1) all-prefixes)
  8977. (setq link (substring link 0 -1))))
  8978. (setq link (with-current-buffer origbuf
  8979. (org-link-try-special-completion link)))))
  8980. (set-window-configuration wcf)
  8981. (kill-buffer "*Org Links*"))
  8982. (setq entry (assoc link org-stored-links))
  8983. (or entry (push link org-insert-link-history))
  8984. (setq desc (or desc (nth 1 entry)))))
  8985. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8986. (not org-keep-stored-link-after-insertion))
  8987. (setq org-stored-links (delq (assoc link org-stored-links)
  8988. org-stored-links)))
  8989. (if (string-match org-plain-link-re link)
  8990. ;; URL-like link, normalize the use of angular brackets.
  8991. (setq link (org-remove-angle-brackets link)))
  8992. ;; Check if we are linking to the current file with a search
  8993. ;; option If yes, simplify the link by using only the search
  8994. ;; option.
  8995. (when (and buffer-file-name
  8996. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8997. (let* ((path (match-string 1 link))
  8998. (case-fold-search nil)
  8999. (search (match-string 2 link)))
  9000. (save-match-data
  9001. (if (equal (file-truename buffer-file-name) (file-truename path))
  9002. ;; We are linking to this same file, with a search option
  9003. (setq link search)))))
  9004. ;; Check if we can/should use a relative path. If yes, simplify the link
  9005. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9006. (let* ((type (match-string 1 link))
  9007. (path (match-string 2 link))
  9008. (origpath path)
  9009. (case-fold-search nil))
  9010. (cond
  9011. ((or (eq org-link-file-path-type 'absolute)
  9012. (equal complete-file '(16)))
  9013. (setq path (abbreviate-file-name (expand-file-name path))))
  9014. ((eq org-link-file-path-type 'noabbrev)
  9015. (setq path (expand-file-name path)))
  9016. ((eq org-link-file-path-type 'relative)
  9017. (setq path (file-relative-name path)))
  9018. (t
  9019. (save-match-data
  9020. (if (string-match (concat "^" (regexp-quote
  9021. (expand-file-name
  9022. (file-name-as-directory
  9023. default-directory))))
  9024. (expand-file-name path))
  9025. ;; We are linking a file with relative path name.
  9026. (setq path (substring (expand-file-name path)
  9027. (match-end 0)))
  9028. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9029. (setq link (concat type path))
  9030. (if (equal desc origpath)
  9031. (setq desc path))))
  9032. (if org-make-link-description-function
  9033. (setq desc
  9034. (or (condition-case nil
  9035. (funcall org-make-link-description-function link desc)
  9036. (error (progn (message "Can't get link description from `%s'"
  9037. (symbol-name org-make-link-description-function))
  9038. (sit-for 2) nil)))
  9039. (read-string "Description: " default-description)))
  9040. (if default-description (setq desc default-description)
  9041. (setq desc (or (and auto-desc desc)
  9042. (read-string "Description: " desc)))))
  9043. (unless (string-match "\\S-" desc) (setq desc nil))
  9044. (if remove (apply 'delete-region remove))
  9045. (insert (org-make-link-string link desc))))
  9046. (defun org-link-try-special-completion (type)
  9047. "If there is completion support for link type TYPE, offer it."
  9048. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9049. (if (functionp fun)
  9050. (funcall fun)
  9051. (read-string "Link (no completion support): " (concat type ":")))))
  9052. (defun org-file-complete-link (&optional arg)
  9053. "Create a file link using completion."
  9054. (let (file link)
  9055. (setq file (org-iread-file-name "File: "))
  9056. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  9057. (pwd1 (file-name-as-directory (abbreviate-file-name
  9058. (expand-file-name ".")))))
  9059. (cond
  9060. ((equal arg '(16))
  9061. (setq link (concat
  9062. "file:"
  9063. (abbreviate-file-name (expand-file-name file)))))
  9064. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9065. (setq link (concat "file:" (match-string 1 file))))
  9066. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9067. (expand-file-name file))
  9068. (setq link (concat
  9069. "file:" (match-string 1 (expand-file-name file)))))
  9070. (t (setq link (concat "file:" file)))))
  9071. link))
  9072. (defun org-iread-file-name (&rest args)
  9073. "Read-file-name using `ido-mode' speedup if available.
  9074. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9075. See `read-file-name' for a description of parameters."
  9076. (org-without-partial-completion
  9077. (if (and org-completion-use-ido
  9078. (fboundp 'ido-read-file-name)
  9079. (boundp 'ido-mode) ido-mode
  9080. (listp (second args)))
  9081. (let ((ido-enter-matching-directory nil))
  9082. (apply 'ido-read-file-name args))
  9083. (apply 'read-file-name args))))
  9084. (defun org-completing-read (&rest args)
  9085. "Completing-read with SPACE being a normal character."
  9086. (let ((enable-recursive-minibuffers t)
  9087. (minibuffer-local-completion-map
  9088. (copy-keymap minibuffer-local-completion-map)))
  9089. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9090. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9091. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9092. (apply 'org-icompleting-read args)))
  9093. (defun org-completing-read-no-i (&rest args)
  9094. (let (org-completion-use-ido org-completion-use-iswitchb)
  9095. (apply 'org-completing-read args)))
  9096. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9097. "Use iswitch as a completing-read replacement to choose from choices.
  9098. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9099. from."
  9100. (let* ((iswitchb-use-virtual-buffers nil)
  9101. (iswitchb-make-buflist-hook
  9102. (lambda ()
  9103. (setq iswitchb-temp-buflist choices))))
  9104. (iswitchb-read-buffer prompt)))
  9105. (defun org-icompleting-read (&rest args)
  9106. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  9107. (org-without-partial-completion
  9108. (if (and org-completion-use-ido
  9109. (fboundp 'ido-completing-read)
  9110. (boundp 'ido-mode) ido-mode
  9111. (listp (second args)))
  9112. (let ((ido-enter-matching-directory nil))
  9113. (apply 'ido-completing-read (concat (car args))
  9114. (if (consp (car (nth 1 args)))
  9115. (mapcar 'car (nth 1 args))
  9116. (nth 1 args))
  9117. (cddr args)))
  9118. (if (and org-completion-use-iswitchb
  9119. (boundp 'iswitchb-mode) iswitchb-mode
  9120. (listp (second args)))
  9121. (apply 'org-iswitchb-completing-read (concat (car args))
  9122. (if (consp (car (nth 1 args)))
  9123. (mapcar 'car (nth 1 args))
  9124. (nth 1 args))
  9125. (cddr args))
  9126. (apply 'completing-read args)))))
  9127. (defun org-extract-attributes (s)
  9128. "Extract the attributes cookie from a string and set as text property."
  9129. (let (a attr (start 0) key value)
  9130. (save-match-data
  9131. (when (string-match "{{\\([^}]+\\)}}$" s)
  9132. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  9133. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  9134. (setq key (match-string 1 a) value (match-string 2 a)
  9135. start (match-end 0)
  9136. attr (plist-put attr (intern key) value))))
  9137. (org-add-props s nil 'org-attr attr))
  9138. s))
  9139. ;;; Opening/following a link
  9140. (defvar org-link-search-failed nil)
  9141. (defvar org-open-link-functions nil
  9142. "Hook for functions finding a plain text link.
  9143. These functions must take a single argument, the link content.
  9144. They will be called for links that look like [[link text][description]]
  9145. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9146. like a filename (e.g. \"./blue.png\").
  9147. These functions will be called *before* Org attempts to resolve the
  9148. link by doing text searches in the current buffer - so if you want a
  9149. link \"[[target]]\" to still find \"<<target>>\", your function should
  9150. handle this as a special case.
  9151. When the function does handle the link, it must return a non-nil value.
  9152. If it decides that it is not responsible for this link, it must return
  9153. nil to indicate that that Org-mode can continue with other options
  9154. like exact and fuzzy text search.")
  9155. (defun org-next-link (&optional search-backward)
  9156. "Move forward to the next link.
  9157. If the link is in hidden text, expose it."
  9158. (interactive "P")
  9159. (when (and org-link-search-failed (eq this-command last-command))
  9160. (goto-char (point-min))
  9161. (message "Link search wrapped back to beginning of buffer"))
  9162. (setq org-link-search-failed nil)
  9163. (let* ((pos (point))
  9164. (ct (org-context))
  9165. (a (assoc :link ct))
  9166. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9167. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9168. ((looking-at org-any-link-re)
  9169. ;; Don't stay stuck at link without an org-link face
  9170. (forward-char (if search-backward -1 1))))
  9171. (if (funcall srch-fun org-any-link-re nil t)
  9172. (progn
  9173. (goto-char (match-beginning 0))
  9174. (if (outline-invisible-p) (org-show-context)))
  9175. (goto-char pos)
  9176. (setq org-link-search-failed t)
  9177. (message "No further link found"))))
  9178. (defun org-previous-link ()
  9179. "Move backward to the previous link.
  9180. If the link is in hidden text, expose it."
  9181. (interactive)
  9182. (funcall 'org-next-link t))
  9183. (defun org-translate-link (s)
  9184. "Translate a link string if a translation function has been defined."
  9185. (if (and org-link-translation-function
  9186. (fboundp org-link-translation-function)
  9187. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  9188. (progn
  9189. (setq s (funcall org-link-translation-function
  9190. (match-string 1 s) (match-string 2 s)))
  9191. (concat (car s) ":" (cdr s)))
  9192. s))
  9193. (defun org-translate-link-from-planner (type path)
  9194. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9195. This is still an experimental function, your mileage may vary."
  9196. (cond
  9197. ((member type '("http" "https" "news" "ftp"))
  9198. ;; standard Internet links are the same.
  9199. nil)
  9200. ((and (equal type "irc") (string-match "^//" path))
  9201. ;; Planner has two / at the beginning of an irc link, we have 1.
  9202. ;; We should have zero, actually....
  9203. (setq path (substring path 1)))
  9204. ((and (equal type "lisp") (string-match "^/" path))
  9205. ;; Planner has a slash, we do not.
  9206. (setq type "elisp" path (substring path 1)))
  9207. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9208. ;; A typical message link. Planner has the id after the final slash,
  9209. ;; we separate it with a hash mark
  9210. (setq path (concat (match-string 1 path) "#"
  9211. (org-remove-angle-brackets (match-string 2 path)))))
  9212. )
  9213. (cons type path))
  9214. (defun org-find-file-at-mouse (ev)
  9215. "Open file link or URL at mouse."
  9216. (interactive "e")
  9217. (mouse-set-point ev)
  9218. (org-open-at-point 'in-emacs))
  9219. (defun org-open-at-mouse (ev)
  9220. "Open file link or URL at mouse.
  9221. See the docstring of `org-open-file' for details."
  9222. (interactive "e")
  9223. (mouse-set-point ev)
  9224. (if (eq major-mode 'org-agenda-mode)
  9225. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9226. (org-open-at-point))
  9227. (defvar org-window-config-before-follow-link nil
  9228. "The window configuration before following a link.
  9229. This is saved in case the need arises to restore it.")
  9230. (defvar org-open-link-marker (make-marker)
  9231. "Marker pointing to the location where `org-open-at-point; was called.")
  9232. ;;;###autoload
  9233. (defun org-open-at-point-global ()
  9234. "Follow a link like Org-mode does.
  9235. This command can be called in any mode to follow a link that has
  9236. Org-mode syntax."
  9237. (interactive)
  9238. (org-run-like-in-org-mode 'org-open-at-point))
  9239. ;;;###autoload
  9240. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9241. "Open a link in the string S, as if it was in Org-mode."
  9242. (interactive "sLink: \nP")
  9243. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9244. (with-temp-buffer
  9245. (let ((org-inhibit-startup (not reference-buffer)))
  9246. (org-mode)
  9247. (insert s)
  9248. (goto-char (point-min))
  9249. (when reference-buffer
  9250. (setq org-link-abbrev-alist-local
  9251. (with-current-buffer reference-buffer
  9252. org-link-abbrev-alist-local)))
  9253. (org-open-at-point arg reference-buffer)))))
  9254. (defvar org-open-at-point-functions nil
  9255. "Hook that is run when following a link at point.
  9256. Functions in this hook must return t if they identify and follow
  9257. a link at point. If they don't find anything interesting at point,
  9258. they must return nil.")
  9259. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9260. (defun org-open-at-point (&optional arg reference-buffer)
  9261. "Open link at or after point.
  9262. If there is no link at point, this function will search forward up to
  9263. the end of the current line.
  9264. Normally, files will be opened by an appropriate application. If the
  9265. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9266. With a double prefix argument, try to open outside of Emacs, in the
  9267. application the system uses for this file type."
  9268. (interactive "P")
  9269. ;; if in a code block, then open the block's results
  9270. (unless (call-interactively #'org-babel-open-src-block-result)
  9271. (org-load-modules-maybe)
  9272. (move-marker org-open-link-marker (point))
  9273. (setq org-window-config-before-follow-link (current-window-configuration))
  9274. (org-remove-occur-highlights nil nil t)
  9275. (cond
  9276. ((and (org-at-heading-p)
  9277. (not (org-at-timestamp-p t))
  9278. (not (org-in-regexp
  9279. (concat org-plain-link-re "\\|"
  9280. org-bracket-link-regexp "\\|"
  9281. org-angle-link-re "\\|"
  9282. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9283. (not (get-text-property (point) 'org-linked-text)))
  9284. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9285. (lk0 (car lkall))
  9286. (lk (if (stringp lk0) (list lk0) lk0))
  9287. (lkend (cdr lkall)))
  9288. (mapcar (lambda(l)
  9289. (search-forward l nil lkend)
  9290. (goto-char (match-beginning 0))
  9291. (org-open-at-point))
  9292. lk))
  9293. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9294. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9295. ((and (org-at-timestamp-p t)
  9296. (not (org-in-regexp org-bracket-link-regexp)))
  9297. (org-follow-timestamp-link))
  9298. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9299. (not (org-in-regexp org-any-link-re)))
  9300. (org-footnote-action))
  9301. (t
  9302. (let (type path link line search (pos (point)))
  9303. (catch 'match
  9304. (save-excursion
  9305. (skip-chars-forward "^]\n\r")
  9306. (when (org-in-regexp org-bracket-link-regexp 1)
  9307. (setq link (org-extract-attributes
  9308. (org-link-unescape (org-match-string-no-properties 1))))
  9309. (while (string-match " *\n *" link)
  9310. (setq link (replace-match " " t t link)))
  9311. (setq link (org-link-expand-abbrev link))
  9312. (cond
  9313. ((or (file-name-absolute-p link)
  9314. (string-match "^\\.\\.?/" link))
  9315. (setq type "file" path link))
  9316. ((string-match org-link-re-with-space3 link)
  9317. (setq type (match-string 1 link) path (match-string 2 link)))
  9318. ((string-match "^help:+\\(.+\\)" link)
  9319. (setq type "help" path (match-string 1 link)))
  9320. (t (setq type "thisfile" path link)))
  9321. (throw 'match t)))
  9322. (when (get-text-property (point) 'org-linked-text)
  9323. (setq type "thisfile"
  9324. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9325. (1+ (point)) (point))
  9326. path (buffer-substring
  9327. (or (previous-single-property-change pos 'org-linked-text)
  9328. (point-min))
  9329. (or (next-single-property-change pos 'org-linked-text)
  9330. (point-max))))
  9331. (throw 'match t))
  9332. (save-excursion
  9333. (when (or (org-in-regexp org-angle-link-re)
  9334. (let ((match (org-in-regexp org-plain-link-re)))
  9335. ;; Check a plain link is not within a bracket link
  9336. (and match
  9337. (save-excursion
  9338. (progn
  9339. (goto-char (car match))
  9340. (not (org-in-regexp org-bracket-link-regexp))))))
  9341. (let ((line_ending (save-excursion (end-of-line) (point))))
  9342. ;; We are in a line before a plain or bracket link
  9343. (or (re-search-forward org-plain-link-re line_ending t)
  9344. (re-search-forward org-bracket-link-regexp line_ending t))))
  9345. (setq type (match-string 1)
  9346. path (org-link-unescape (match-string 2)))
  9347. (throw 'match t)))
  9348. (save-excursion
  9349. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9350. (setq type "tags"
  9351. path (match-string 1))
  9352. (while (string-match ":" path)
  9353. (setq path (replace-match "+" t t path)))
  9354. (throw 'match t)))
  9355. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9356. (setq type "tree-match"
  9357. path (match-string 1))
  9358. (throw 'match t)))
  9359. (unless path
  9360. (user-error "No link found"))
  9361. ;; switch back to reference buffer
  9362. ;; needed when if called in a temporary buffer through
  9363. ;; org-open-link-from-string
  9364. (with-current-buffer (or reference-buffer (current-buffer))
  9365. ;; Remove any trailing spaces in path
  9366. (if (string-match " +\\'" path)
  9367. (setq path (replace-match "" t t path)))
  9368. (if (and org-link-translation-function
  9369. (fboundp org-link-translation-function))
  9370. ;; Check if we need to translate the link
  9371. (let ((tmp (funcall org-link-translation-function type path)))
  9372. (setq type (car tmp) path (cdr tmp))))
  9373. (cond
  9374. ((assoc type org-link-protocols)
  9375. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9376. ((equal type "help")
  9377. (let ((f-or-v (intern path)))
  9378. (cond ((fboundp f-or-v)
  9379. (describe-function f-or-v))
  9380. ((boundp f-or-v)
  9381. (describe-variable f-or-v))
  9382. (t (error "Not a known function or variable")))))
  9383. ((equal type "mailto")
  9384. (let ((cmd (car org-link-mailto-program))
  9385. (args (cdr org-link-mailto-program)) args1
  9386. (address path) (subject "") a)
  9387. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9388. (setq address (match-string 1 path)
  9389. subject (org-link-escape (match-string 2 path))))
  9390. (while args
  9391. (cond
  9392. ((not (stringp (car args))) (push (pop args) args1))
  9393. (t (setq a (pop args))
  9394. (if (string-match "%a" a)
  9395. (setq a (replace-match address t t a)))
  9396. (if (string-match "%s" a)
  9397. (setq a (replace-match subject t t a)))
  9398. (push a args1))))
  9399. (apply cmd (nreverse args1))))
  9400. ((member type '("http" "https" "ftp" "news"))
  9401. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9402. (org-link-escape
  9403. path org-link-escape-chars-browser)
  9404. path))))
  9405. ((string= type "doi")
  9406. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9407. (org-link-escape
  9408. path org-link-escape-chars-browser)
  9409. path))))
  9410. ((member type '("message"))
  9411. (browse-url (concat type ":" path)))
  9412. ((string= type "tags")
  9413. (org-tags-view arg path))
  9414. ((string= type "tree-match")
  9415. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9416. ((string= type "file")
  9417. (if (string-match "::\\([0-9]+\\)\\'" path)
  9418. (setq line (string-to-number (match-string 1 path))
  9419. path (substring path 0 (match-beginning 0)))
  9420. (if (string-match "::\\(.+\\)\\'" path)
  9421. (setq search (match-string 1 path)
  9422. path (substring path 0 (match-beginning 0)))))
  9423. (if (string-match "[*?{]" (file-name-nondirectory path))
  9424. (dired path)
  9425. (org-open-file path arg line search)))
  9426. ((string= type "shell")
  9427. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9428. (cmd path))
  9429. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9430. (string-match org-confirm-shell-link-not-regexp cmd))
  9431. (not org-confirm-shell-link-function)
  9432. (funcall org-confirm-shell-link-function
  9433. (format "Execute \"%s\" in shell? "
  9434. (org-add-props cmd nil
  9435. 'face 'org-warning))))
  9436. (progn
  9437. (message "Executing %s" cmd)
  9438. (shell-command cmd buf)
  9439. (if (featurep 'midnight)
  9440. (setq clean-buffer-list-kill-buffer-names
  9441. (cons buf clean-buffer-list-kill-buffer-names))))
  9442. (error "Abort"))))
  9443. ((string= type "elisp")
  9444. (let ((cmd path))
  9445. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9446. (string-match org-confirm-elisp-link-not-regexp cmd))
  9447. (not org-confirm-elisp-link-function)
  9448. (funcall org-confirm-elisp-link-function
  9449. (format "Execute \"%s\" as elisp? "
  9450. (org-add-props cmd nil
  9451. 'face 'org-warning))))
  9452. (message "%s => %s" cmd
  9453. (if (equal (string-to-char cmd) ?\()
  9454. (eval (read cmd))
  9455. (call-interactively (read cmd))))
  9456. (error "Abort"))))
  9457. ((and (string= type "thisfile")
  9458. (run-hook-with-args-until-success
  9459. 'org-open-link-functions path)))
  9460. ((string= type "thisfile")
  9461. (if arg
  9462. (switch-to-buffer-other-window
  9463. (org-get-buffer-for-internal-link (current-buffer)))
  9464. (org-mark-ring-push))
  9465. (let ((cmd `(org-link-search
  9466. ,path
  9467. ,(cond ((equal arg '(4)) ''occur)
  9468. ((equal arg '(16)) ''org-occur))
  9469. ,pos)))
  9470. (condition-case nil (let ((org-link-search-inhibit-query t))
  9471. (eval cmd))
  9472. (error (progn (widen) (eval cmd))))))
  9473. (t (browse-url-at-point)))))))
  9474. (move-marker org-open-link-marker nil)
  9475. (run-hook-with-args 'org-follow-link-hook)))
  9476. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9477. "Offer links in the current entry and return the selected link.
  9478. If there is only one link, return it.
  9479. If NTH is an integer, return the NTH link found.
  9480. If ZERO is a string, check also this string for a link, and if
  9481. there is one, return it."
  9482. (with-current-buffer buffer
  9483. (save-excursion
  9484. (save-restriction
  9485. (widen)
  9486. (goto-char marker)
  9487. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9488. "\\(" org-angle-link-re "\\)\\|"
  9489. "\\(" org-plain-link-re "\\)"))
  9490. (cnt ?0)
  9491. (in-emacs (if (integerp nth) nil nth))
  9492. have-zero end links link c)
  9493. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9494. (push (match-string 0 zero) links)
  9495. (setq cnt (1- cnt) have-zero t))
  9496. (save-excursion
  9497. (org-back-to-heading t)
  9498. (setq end (save-excursion (outline-next-heading) (point)))
  9499. (while (re-search-forward re end t)
  9500. (push (match-string 0) links))
  9501. (setq links (org-uniquify (reverse links))))
  9502. (cond
  9503. ((null links)
  9504. (message "No links"))
  9505. ((equal (length links) 1)
  9506. (setq link (car links)))
  9507. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9508. (setq link (nth (if have-zero nth (1- nth)) links)))
  9509. (t ; we have to select a link
  9510. (save-excursion
  9511. (save-window-excursion
  9512. (delete-other-windows)
  9513. (with-output-to-temp-buffer "*Select Link*"
  9514. (mapc (lambda (l)
  9515. (if (not (string-match org-bracket-link-regexp l))
  9516. (princ (format "[%c] %s\n" (incf cnt)
  9517. (org-remove-angle-brackets l)))
  9518. (if (match-end 3)
  9519. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9520. (match-string 3 l) (match-string 1 l)))
  9521. (princ (format "[%c] %s\n" (incf cnt)
  9522. (match-string 1 l))))))
  9523. links))
  9524. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9525. (message "Select link to open, RET to open all:")
  9526. (setq c (read-char-exclusive))
  9527. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9528. (when (equal c ?q) (error "Abort"))
  9529. (if (equal c ?\C-m)
  9530. (setq link links)
  9531. (setq nth (- c ?0))
  9532. (if have-zero (setq nth (1+ nth)))
  9533. (unless (and (integerp nth) (>= (length links) nth))
  9534. (error "Invalid link selection"))
  9535. (setq link (nth (1- nth) links)))))
  9536. (cons link end))))))
  9537. ;; Add special file links that specify the way of opening
  9538. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9539. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9540. (defun org-open-file-with-system (path)
  9541. "Open file at PATH using the system way of opening it."
  9542. (org-open-file path 'system))
  9543. (defun org-open-file-with-emacs (path)
  9544. "Open file at PATH in Emacs."
  9545. (org-open-file path 'emacs))
  9546. ;;; File search
  9547. (defvar org-create-file-search-functions nil
  9548. "List of functions to construct the right search string for a file link.
  9549. These functions are called in turn with point at the location to
  9550. which the link should point.
  9551. A function in the hook should first test if it would like to
  9552. handle this file type, for example by checking the `major-mode'
  9553. or the file extension. If it decides not to handle this file, it
  9554. should just return nil to give other functions a chance. If it
  9555. does handle the file, it must return the search string to be used
  9556. when following the link. The search string will be part of the
  9557. file link, given after a double colon, and `org-open-at-point'
  9558. will automatically search for it. If special measures must be
  9559. taken to make the search successful, another function should be
  9560. added to the companion hook `org-execute-file-search-functions',
  9561. which see.
  9562. A function in this hook may also use `setq' to set the variable
  9563. `description' to provide a suggestion for the descriptive text to
  9564. be used for this link when it gets inserted into an Org-mode
  9565. buffer with \\[org-insert-link].")
  9566. (defvar org-execute-file-search-functions nil
  9567. "List of functions to execute a file search triggered by a link.
  9568. Functions added to this hook must accept a single argument, the
  9569. search string that was part of the file link, the part after the
  9570. double colon. The function must first check if it would like to
  9571. handle this search, for example by checking the `major-mode' or
  9572. the file extension. If it decides not to handle this search, it
  9573. should just return nil to give other functions a chance. If it
  9574. does handle the search, it must return a non-nil value to keep
  9575. other functions from trying.
  9576. Each function can access the current prefix argument through the
  9577. variable `current-prefix-arg'. Note that a single prefix is used
  9578. to force opening a link in Emacs, so it may be good to only use a
  9579. numeric or double prefix to guide the search function.
  9580. In case this is needed, a function in this hook can also restore
  9581. the window configuration before `org-open-at-point' was called using:
  9582. (set-window-configuration org-window-config-before-follow-link)")
  9583. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9584. (defun org-link-search (s &optional type avoid-pos stealth)
  9585. "Search for a link search option.
  9586. If S is surrounded by forward slashes, it is interpreted as a
  9587. regular expression. In org-mode files, this will create an `org-occur'
  9588. sparse tree. In ordinary files, `occur' will be used to list matches.
  9589. If the current buffer is in `dired-mode', grep will be used to search
  9590. in all files. If AVOID-POS is given, ignore matches near that position.
  9591. When optional argument STEALTH is non-nil, do not modify
  9592. visibility around point, thus ignoring
  9593. `org-show-hierarchy-above', `org-show-following-heading' and
  9594. `org-show-siblings' variables."
  9595. (let ((case-fold-search t)
  9596. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9597. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9598. (append '(("") (" ") ("\t") ("\n"))
  9599. org-emphasis-alist)
  9600. "\\|") "\\)"))
  9601. (pos (point))
  9602. (pre nil) (post nil)
  9603. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9604. (cond
  9605. ;; First check if there are any special search functions
  9606. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9607. ;; Now try the builtin stuff
  9608. ((and (equal (string-to-char s0) ?#)
  9609. (> (length s0) 1)
  9610. (save-excursion
  9611. (goto-char (point-min))
  9612. (and
  9613. (re-search-forward
  9614. (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
  9615. (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9616. (setq type 'dedicated
  9617. pos (match-beginning 0))))
  9618. ;; There is an exact target for this
  9619. (goto-char pos)
  9620. (org-back-to-heading t)))
  9621. ((save-excursion
  9622. (goto-char (point-min))
  9623. (and
  9624. (re-search-forward
  9625. (concat "<<" (regexp-quote s0) ">>") nil t)
  9626. (setq type 'dedicated
  9627. pos (match-beginning 0))))
  9628. ;; There is an exact target for this
  9629. (goto-char pos))
  9630. ((save-excursion
  9631. (goto-char (point-min))
  9632. (and
  9633. (re-search-forward
  9634. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9635. (setq type 'dedicated pos (match-beginning 0))))
  9636. ;; Found an element with a matching #+name affiliated keyword.
  9637. (goto-char pos))
  9638. ((and (string-match "^(\\(.*\\))$" s0)
  9639. (save-excursion
  9640. (goto-char (point-min))
  9641. (and
  9642. (re-search-forward
  9643. (concat "[^[]" (regexp-quote
  9644. (format org-coderef-label-format
  9645. (match-string 1 s0))))
  9646. nil t)
  9647. (setq type 'dedicated
  9648. pos (1+ (match-beginning 0))))))
  9649. ;; There is a coderef target for this
  9650. (goto-char pos))
  9651. ((string-match "^/\\(.*\\)/$" s)
  9652. ;; A regular expression
  9653. (cond
  9654. ((derived-mode-p 'org-mode)
  9655. (org-occur (match-string 1 s)))
  9656. (t (org-do-occur (match-string 1 s)))))
  9657. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9658. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9659. (goto-char (point-min))
  9660. (cond
  9661. ((let (case-fold-search)
  9662. (re-search-forward (format org-complex-heading-regexp-format
  9663. (regexp-quote s))
  9664. nil t))
  9665. ;; OK, found a match
  9666. (setq type 'dedicated)
  9667. (goto-char (match-beginning 0)))
  9668. ((and (not org-link-search-inhibit-query)
  9669. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9670. (y-or-n-p "No match - create this as a new heading? "))
  9671. (goto-char (point-max))
  9672. (or (bolp) (newline))
  9673. (insert "* " s "\n")
  9674. (beginning-of-line 0))
  9675. (t
  9676. (goto-char pos)
  9677. (error "No match"))))
  9678. (t
  9679. ;; A normal search string
  9680. (when (equal (string-to-char s) ?*)
  9681. ;; Anchor on headlines, post may include tags.
  9682. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9683. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9684. s (substring s 1)))
  9685. (remove-text-properties
  9686. 0 (length s)
  9687. '(face nil mouse-face nil keymap nil fontified nil) s)
  9688. ;; Make a series of regular expressions to find a match
  9689. (setq words (org-split-string s "[ \n\r\t]+")
  9690. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9691. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9692. "\\)" markers)
  9693. re2a_ (concat "\\(" (mapconcat 'downcase words
  9694. "[ \t\r\n]+") "\\)[ \t\r\n]")
  9695. re2a (concat "[ \t\r\n]" re2a_)
  9696. re4_ (concat "\\(" (mapconcat 'downcase words
  9697. "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9698. re4 (concat "[^a-zA-Z_]" re4_)
  9699. re1 (concat pre re2 post)
  9700. re3 (concat pre (if pre re4_ re4) post)
  9701. re5 (concat pre ".*" re4)
  9702. re2 (concat pre re2)
  9703. re2a (concat pre (if pre re2a_ re2a))
  9704. re4 (concat pre (if pre re4_ re4))
  9705. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9706. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9707. re5 "\\)"
  9708. ))
  9709. (cond
  9710. ((eq type 'org-occur) (org-occur reall))
  9711. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9712. (t (goto-char (point-min))
  9713. (setq type 'fuzzy)
  9714. (if (or (and (org-search-not-self 1 re0 nil t)
  9715. (setq type 'dedicated))
  9716. (org-search-not-self 1 re1 nil t)
  9717. (org-search-not-self 1 re2 nil t)
  9718. (org-search-not-self 1 re2a nil t)
  9719. (org-search-not-self 1 re3 nil t)
  9720. (org-search-not-self 1 re4 nil t)
  9721. (org-search-not-self 1 re5 nil t))
  9722. (goto-char (match-beginning 1))
  9723. (goto-char pos)
  9724. (error "No match"))))))
  9725. (and (derived-mode-p 'org-mode)
  9726. (not stealth)
  9727. (org-show-context 'link-search))
  9728. type))
  9729. (defun org-search-not-self (group &rest args)
  9730. "Execute `re-search-forward', but only accept matches that do not
  9731. enclose the position of `org-open-link-marker'."
  9732. (let ((m org-open-link-marker))
  9733. (catch 'exit
  9734. (while (apply 're-search-forward args)
  9735. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9736. (goto-char (match-end group))
  9737. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9738. (> (match-beginning 0) (marker-position m))
  9739. (< (match-end 0) (marker-position m)))
  9740. (save-match-data
  9741. (or (not (org-in-regexp
  9742. org-bracket-link-analytic-regexp 1))
  9743. (not (match-end 4)) ; no description
  9744. (and (<= (match-beginning 4) (point))
  9745. (>= (match-end 4) (point))))))
  9746. (throw 'exit (point))))))))
  9747. (defun org-get-buffer-for-internal-link (buffer)
  9748. "Return a buffer to be used for displaying the link target of internal links."
  9749. (cond
  9750. ((not org-display-internal-link-with-indirect-buffer)
  9751. buffer)
  9752. ((string-match "(Clone)$" (buffer-name buffer))
  9753. (message "Buffer is already a clone, not making another one")
  9754. ;; we also do not modify visibility in this case
  9755. buffer)
  9756. (t ; make a new indirect buffer for displaying the link
  9757. (let* ((bn (buffer-name buffer))
  9758. (ibn (concat bn "(Clone)"))
  9759. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9760. (with-current-buffer ib (org-overview))
  9761. ib))))
  9762. (defun org-do-occur (regexp &optional cleanup)
  9763. "Call the Emacs command `occur'.
  9764. If CLEANUP is non-nil, remove the printout of the regular expression
  9765. in the *Occur* buffer. This is useful if the regex is long and not useful
  9766. to read."
  9767. (occur regexp)
  9768. (when cleanup
  9769. (let ((cwin (selected-window)) win beg end)
  9770. (when (setq win (get-buffer-window "*Occur*"))
  9771. (select-window win))
  9772. (goto-char (point-min))
  9773. (when (re-search-forward "match[a-z]+" nil t)
  9774. (setq beg (match-end 0))
  9775. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9776. (setq end (1- (match-beginning 0)))))
  9777. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9778. (goto-char (point-min))
  9779. (select-window cwin))))
  9780. ;;; The mark ring for links jumps
  9781. (defvar org-mark-ring nil
  9782. "Mark ring for positions before jumps in Org-mode.")
  9783. (defvar org-mark-ring-last-goto nil
  9784. "Last position in the mark ring used to go back.")
  9785. ;; Fill and close the ring
  9786. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9787. (loop for i from 1 to org-mark-ring-length do
  9788. (push (make-marker) org-mark-ring))
  9789. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9790. org-mark-ring)
  9791. (defun org-mark-ring-push (&optional pos buffer)
  9792. "Put the current position or POS into the mark ring and rotate it."
  9793. (interactive)
  9794. (setq pos (or pos (point)))
  9795. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9796. (move-marker (car org-mark-ring)
  9797. (or pos (point))
  9798. (or buffer (current-buffer)))
  9799. (message "%s"
  9800. (substitute-command-keys
  9801. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9802. (defun org-mark-ring-goto (&optional n)
  9803. "Jump to the previous position in the mark ring.
  9804. With prefix arg N, jump back that many stored positions. When
  9805. called several times in succession, walk through the entire ring.
  9806. Org-mode commands jumping to a different position in the current file,
  9807. or to another Org-mode file, automatically push the old position
  9808. onto the ring."
  9809. (interactive "p")
  9810. (let (p m)
  9811. (if (eq last-command this-command)
  9812. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9813. (setq p org-mark-ring))
  9814. (setq org-mark-ring-last-goto p)
  9815. (setq m (car p))
  9816. (org-pop-to-buffer-same-window (marker-buffer m))
  9817. (goto-char m)
  9818. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9819. (defun org-remove-angle-brackets (s)
  9820. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9821. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9822. s)
  9823. (defun org-add-angle-brackets (s)
  9824. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9825. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9826. s)
  9827. (defun org-remove-double-quotes (s)
  9828. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9829. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9830. s)
  9831. ;;; Following specific links
  9832. (defun org-follow-timestamp-link ()
  9833. "Open an agenda view for the time-stamp date/range at point."
  9834. (cond
  9835. ((org-at-date-range-p t)
  9836. (let ((org-agenda-start-on-weekday)
  9837. (t1 (match-string 1))
  9838. (t2 (match-string 2)) tt1 tt2)
  9839. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9840. tt2 (time-to-days (org-time-string-to-time t2)))
  9841. (let ((org-agenda-buffer-tmp-name
  9842. (format "*Org Agenda(a:%s)"
  9843. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9844. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9845. ((org-at-timestamp-p t)
  9846. (let ((org-agenda-buffer-tmp-name
  9847. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9848. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9849. (substring (match-string 1) 0 10)))
  9850. 1)))
  9851. (t (error "This should not happen"))))
  9852. ;;; Following file links
  9853. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9854. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9855. (declare-function mailcap-mime-info
  9856. "mailcap" (string &optional request no-decode))
  9857. (defvar org-wait nil)
  9858. (defun org-open-file (path &optional in-emacs line search)
  9859. "Open the file at PATH.
  9860. First, this expands any special file name abbreviations. Then the
  9861. configuration variable `org-file-apps' is checked if it contains an
  9862. entry for this file type, and if yes, the corresponding command is launched.
  9863. If no application is found, Emacs simply visits the file.
  9864. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9865. With a double \\[universal-argument] \\[universal-argument] \
  9866. prefix arg, Org tries to avoid opening in Emacs
  9867. and to use an external application to visit the file.
  9868. Optional LINE specifies a line to go to, optional SEARCH a string
  9869. to search for. If LINE or SEARCH is given, the file will be
  9870. opened in Emacs, unless an entry from org-file-apps that makes
  9871. use of groups in a regexp matches.
  9872. If you want to change the way frames are used when following a
  9873. link, please customize `org-link-frame-setup'.
  9874. If the file does not exist, an error is thrown."
  9875. (let* ((file (if (equal path "")
  9876. buffer-file-name
  9877. (substitute-in-file-name (expand-file-name path))))
  9878. (file-apps (append org-file-apps (org-default-apps)))
  9879. (apps (org-remove-if
  9880. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9881. (apps-dlink (org-remove-if-not
  9882. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9883. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9884. (dirp (if remp nil (file-directory-p file)))
  9885. (file (if (and dirp org-open-directory-means-index-dot-org)
  9886. (concat (file-name-as-directory file) "index.org")
  9887. file))
  9888. (a-m-a-p (assq 'auto-mode apps))
  9889. (dfile (downcase file))
  9890. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9891. (link (cond ((and (eq line nil)
  9892. (eq search nil))
  9893. file)
  9894. (line
  9895. (concat file "::" (number-to-string line)))
  9896. (search
  9897. (concat file "::" search))))
  9898. (dlink (downcase link))
  9899. (old-buffer (current-buffer))
  9900. (old-pos (point))
  9901. (old-mode major-mode)
  9902. ext cmd link-match-data)
  9903. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9904. (setq ext (match-string 1 dfile))
  9905. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9906. (setq ext (match-string 1 dfile))))
  9907. (cond
  9908. ((member in-emacs '((16) system))
  9909. (setq cmd (cdr (assoc 'system apps))))
  9910. (in-emacs (setq cmd 'emacs))
  9911. (t
  9912. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9913. (and dirp (cdr (assoc 'directory apps)))
  9914. ; first, try matching against apps-dlink
  9915. ; if we get a match here, store the match data for later
  9916. (let ((match (assoc-default dlink apps-dlink
  9917. 'string-match)))
  9918. (if match
  9919. (progn (setq link-match-data (match-data))
  9920. match)
  9921. (progn (setq in-emacs (or in-emacs line search))
  9922. nil))) ; if we have no match in apps-dlink,
  9923. ; always open the file in emacs if line or search
  9924. ; is given (for backwards compatibility)
  9925. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9926. 'string-match)
  9927. (cdr (assoc ext apps))
  9928. (cdr (assoc t apps))))))
  9929. (when (eq cmd 'system)
  9930. (setq cmd (cdr (assoc 'system apps))))
  9931. (when (eq cmd 'default)
  9932. (setq cmd (cdr (assoc t apps))))
  9933. (when (eq cmd 'mailcap)
  9934. (require 'mailcap)
  9935. (mailcap-parse-mailcaps)
  9936. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9937. (command (mailcap-mime-info mime-type)))
  9938. (if (stringp command)
  9939. (setq cmd command)
  9940. (setq cmd 'emacs))))
  9941. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9942. (not (file-exists-p file))
  9943. (not org-open-non-existing-files))
  9944. (error "No such file: %s" file))
  9945. (cond
  9946. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9947. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9948. (while (string-match "['\"]%s['\"]" cmd)
  9949. (setq cmd (replace-match "%s" t t cmd)))
  9950. (while (string-match "%s" cmd)
  9951. (setq cmd (replace-match
  9952. (save-match-data
  9953. (shell-quote-argument
  9954. (convert-standard-filename file)))
  9955. t t cmd)))
  9956. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9957. (save-match-data
  9958. (let ((match-index 1)
  9959. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9960. (set-match-data link-match-data)
  9961. (while (<= match-index number-of-groups)
  9962. (let ((regex (concat "%" (number-to-string match-index)))
  9963. (replace-with (match-string match-index dlink)))
  9964. (while (string-match regex cmd)
  9965. (setq cmd (replace-match replace-with t t cmd))))
  9966. (setq match-index (+ match-index 1)))))
  9967. (save-window-excursion
  9968. (message "Running %s...done" cmd)
  9969. (start-process-shell-command cmd nil cmd)
  9970. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9971. ((or (stringp cmd)
  9972. (eq cmd 'emacs))
  9973. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9974. (widen)
  9975. (if line (org-goto-line line)
  9976. (if search (org-link-search search))))
  9977. ((consp cmd)
  9978. (let ((file (convert-standard-filename file)))
  9979. (save-match-data
  9980. (set-match-data link-match-data)
  9981. (eval cmd))))
  9982. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9983. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9984. (or (not (equal old-buffer (current-buffer)))
  9985. (not (equal old-pos (point))))
  9986. (org-mark-ring-push old-pos old-buffer))))
  9987. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9988. "This function returns non-nil if `entry' uses a regular
  9989. expression which should be matched against the whole link by
  9990. org-open-file.
  9991. It assumes that is the case when the entry uses a regular
  9992. expression which has at least one grouping construct and the
  9993. action is either a lisp form or a command string containing
  9994. '%1', i.e. using at least one subexpression match as a
  9995. parameter."
  9996. (let ((selector (car entry))
  9997. (action (cdr entry)))
  9998. (if (stringp selector)
  9999. (and (> (regexp-opt-depth selector) 0)
  10000. (or (and (stringp action)
  10001. (string-match "%[0-9]" action))
  10002. (consp action)))
  10003. nil)))
  10004. (defun org-default-apps ()
  10005. "Return the default applications for this operating system."
  10006. (cond
  10007. ((eq system-type 'darwin)
  10008. org-file-apps-defaults-macosx)
  10009. ((eq system-type 'windows-nt)
  10010. org-file-apps-defaults-windowsnt)
  10011. (t org-file-apps-defaults-gnu)))
  10012. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10013. "Convert extensions to regular expressions in the cars of LIST.
  10014. Also, weed out any non-string entries, because the return value is used
  10015. only for regexp matching.
  10016. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10017. point to the symbol `emacs', indicating that the file should
  10018. be opened in Emacs."
  10019. (append
  10020. (delq nil
  10021. (mapcar (lambda (x)
  10022. (if (not (stringp (car x)))
  10023. nil
  10024. (if (string-match "\\W" (car x))
  10025. x
  10026. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10027. list))
  10028. (if add-auto-mode
  10029. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10030. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10031. (defun org-file-remote-p (file)
  10032. "Test whether FILE specifies a location on a remote system.
  10033. Return non-nil if the location is indeed remote.
  10034. For example, the filename \"/user@host:/foo\" specifies a location
  10035. on the system \"/user@host:\"."
  10036. (cond ((fboundp 'file-remote-p)
  10037. (file-remote-p file))
  10038. ((fboundp 'tramp-handle-file-remote-p)
  10039. (tramp-handle-file-remote-p file))
  10040. ((and (boundp 'ange-ftp-name-format)
  10041. (string-match (car ange-ftp-name-format) file))
  10042. t)))
  10043. ;;;; Refiling
  10044. (defun org-get-org-file ()
  10045. "Read a filename, with default directory `org-directory'."
  10046. (let ((default (or org-default-notes-file remember-data-file)))
  10047. (read-file-name (format "File name [%s]: " default)
  10048. (file-name-as-directory org-directory)
  10049. default)))
  10050. (defun org-notes-order-reversed-p ()
  10051. "Check if the current file should receive notes in reversed order."
  10052. (cond
  10053. ((not org-reverse-note-order) nil)
  10054. ((eq t org-reverse-note-order) t)
  10055. ((not (listp org-reverse-note-order)) nil)
  10056. (t (catch 'exit
  10057. (let ((all org-reverse-note-order)
  10058. entry)
  10059. (while (setq entry (pop all))
  10060. (if (string-match (car entry) buffer-file-name)
  10061. (throw 'exit (cdr entry))))
  10062. nil)))))
  10063. (defvar org-refile-target-table nil
  10064. "The list of refile targets, created by `org-refile'.")
  10065. (defvar org-agenda-new-buffers nil
  10066. "Buffers created to visit agenda files.")
  10067. (defvar org-refile-cache nil
  10068. "Cache for refile targets.")
  10069. (defvar org-refile-markers nil
  10070. "All the markers used for caching refile locations.")
  10071. (defun org-refile-marker (pos)
  10072. "Get a new refile marker, but only if caching is in use."
  10073. (if (not org-refile-use-cache)
  10074. pos
  10075. (let ((m (make-marker)))
  10076. (move-marker m pos)
  10077. (push m org-refile-markers)
  10078. m)))
  10079. (defun org-refile-cache-clear ()
  10080. "Clear the refile cache and disable all the markers."
  10081. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10082. (setq org-refile-markers nil)
  10083. (setq org-refile-cache nil)
  10084. (message "Refile cache has been cleared"))
  10085. (defun org-refile-cache-check-set (set)
  10086. "Check if all the markers in the cache still have live buffers."
  10087. (let (marker)
  10088. (catch 'exit
  10089. (while (and set (setq marker (nth 3 (pop set))))
  10090. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10091. (when (and marker (null (marker-buffer marker)))
  10092. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10093. (sit-for 3)
  10094. (throw 'exit nil)))
  10095. t)))
  10096. (defun org-refile-cache-put (set &rest identifiers)
  10097. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10098. (let* ((key (sha1 (prin1-to-string identifiers)))
  10099. (entry (assoc key org-refile-cache)))
  10100. (if entry
  10101. (setcdr entry set)
  10102. (push (cons key set) org-refile-cache))))
  10103. (defun org-refile-cache-get (&rest identifiers)
  10104. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10105. (cond
  10106. ((not org-refile-cache) nil)
  10107. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10108. (t
  10109. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10110. org-refile-cache))))
  10111. (and set (org-refile-cache-check-set set) set)))))
  10112. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10113. "Produce a table with refile targets."
  10114. (let ((case-fold-search nil)
  10115. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10116. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10117. targets tgs txt re files f desc descre fast-path-p level pos0)
  10118. (message "Getting targets...")
  10119. (with-current-buffer (or default-buffer (current-buffer))
  10120. (while (setq entry (pop entries))
  10121. (setq files (car entry) desc (cdr entry))
  10122. (setq fast-path-p nil)
  10123. (cond
  10124. ((null files) (setq files (list (current-buffer))))
  10125. ((eq files 'org-agenda-files)
  10126. (setq files (org-agenda-files 'unrestricted)))
  10127. ((and (symbolp files) (fboundp files))
  10128. (setq files (funcall files)))
  10129. ((and (symbolp files) (boundp files))
  10130. (setq files (symbol-value files))))
  10131. (if (stringp files) (setq files (list files)))
  10132. (cond
  10133. ((eq (car desc) :tag)
  10134. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10135. ((eq (car desc) :todo)
  10136. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10137. ((eq (car desc) :regexp)
  10138. (setq descre (cdr desc)))
  10139. ((eq (car desc) :level)
  10140. (setq descre (concat "^\\*\\{" (number-to-string
  10141. (if org-odd-levels-only
  10142. (1- (* 2 (cdr desc)))
  10143. (cdr desc)))
  10144. "\\}[ \t]")))
  10145. ((eq (car desc) :maxlevel)
  10146. (setq fast-path-p t)
  10147. (setq descre (concat "^\\*\\{1," (number-to-string
  10148. (if org-odd-levels-only
  10149. (1- (* 2 (cdr desc)))
  10150. (cdr desc)))
  10151. "\\}[ \t]")))
  10152. (t (error "Bad refiling target description %s" desc)))
  10153. (while (setq f (pop files))
  10154. (with-current-buffer
  10155. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10156. (or
  10157. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10158. (progn
  10159. (if (bufferp f) (setq f (buffer-file-name
  10160. (buffer-base-buffer f))))
  10161. (setq f (and f (expand-file-name f)))
  10162. (if (eq org-refile-use-outline-path 'file)
  10163. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10164. (save-excursion
  10165. (save-restriction
  10166. (widen)
  10167. (goto-char (point-min))
  10168. (while (re-search-forward descre nil t)
  10169. (goto-char (setq pos0 (point-at-bol)))
  10170. (catch 'next
  10171. (when org-refile-target-verify-function
  10172. (save-match-data
  10173. (or (funcall org-refile-target-verify-function)
  10174. (throw 'next t))))
  10175. (when (and (looking-at org-complex-heading-regexp)
  10176. (not (member (match-string 4) excluded-entries))
  10177. (match-string 4))
  10178. (setq level (org-reduced-level
  10179. (- (match-end 1) (match-beginning 1)))
  10180. txt (org-link-display-format (match-string 4))
  10181. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10182. re (format org-complex-heading-regexp-format
  10183. (regexp-quote (match-string 4))))
  10184. (when org-refile-use-outline-path
  10185. (setq txt (mapconcat
  10186. 'org-protect-slash
  10187. (append
  10188. (if (eq org-refile-use-outline-path
  10189. 'file)
  10190. (list (file-name-nondirectory
  10191. (buffer-file-name
  10192. (buffer-base-buffer))))
  10193. (if (eq org-refile-use-outline-path
  10194. 'full-file-path)
  10195. (list (buffer-file-name
  10196. (buffer-base-buffer)))))
  10197. (org-get-outline-path fast-path-p
  10198. level txt)
  10199. (list txt))
  10200. "/")))
  10201. (push (list txt f re (org-refile-marker (point)))
  10202. tgs)))
  10203. (when (= (point) pos0)
  10204. ;; verification function has not moved point
  10205. (goto-char (point-at-eol))))))))
  10206. (when org-refile-use-cache
  10207. (org-refile-cache-put tgs (buffer-file-name) descre))
  10208. (setq targets (append tgs targets))
  10209. ))))
  10210. (message "Getting targets...done")
  10211. (nreverse targets)))
  10212. (defun org-protect-slash (s)
  10213. (while (string-match "/" s)
  10214. (setq s (replace-match "\\" t t s)))
  10215. s)
  10216. (defvar org-olpa (make-vector 20 nil))
  10217. (defun org-get-outline-path (&optional fastp level heading)
  10218. "Return the outline path to the current entry, as a list.
  10219. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10220. routine which makes outline path derivations for an entire file,
  10221. avoiding backtracing. Refile target collection makes use of that."
  10222. (if fastp
  10223. (progn
  10224. (if (> level 19)
  10225. (error "Outline path failure, more than 19 levels"))
  10226. (loop for i from level upto 19 do
  10227. (aset org-olpa i nil))
  10228. (prog1
  10229. (delq nil (append org-olpa nil))
  10230. (aset org-olpa level heading)))
  10231. (let (rtn case-fold-search)
  10232. (save-excursion
  10233. (save-restriction
  10234. (widen)
  10235. (while (org-up-heading-safe)
  10236. (when (looking-at org-complex-heading-regexp)
  10237. (push (org-match-string-no-properties 4) rtn)))
  10238. rtn)))))
  10239. (defun org-format-outline-path (path &optional width prefix separator)
  10240. "Format the outline path PATH for display.
  10241. WIDTH is the maximum number of characters that is available.
  10242. PREFIX is a prefix to be included in the returned string,
  10243. such as the file name.
  10244. SEPARATOR is inserted between the different parts of the path,
  10245. the default is \"/\"."
  10246. (setq width (or width 79))
  10247. (if prefix (setq width (- width (length prefix))))
  10248. (if (not path)
  10249. (or prefix "")
  10250. (let* ((nsteps (length path))
  10251. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10252. (maxwidth (if (<= total-width width)
  10253. 10000 ;; everything fits
  10254. ;; we need to shorten the level headings
  10255. (/ (- width nsteps) nsteps)))
  10256. (org-odd-levels-only nil)
  10257. (n 0)
  10258. (total (1+ (length prefix))))
  10259. (setq maxwidth (max maxwidth 10))
  10260. (concat prefix
  10261. (if prefix (or separator "/"))
  10262. (mapconcat
  10263. (lambda (h)
  10264. (setq n (1+ n))
  10265. (if (and (= n nsteps) (< maxwidth 10000))
  10266. (setq maxwidth (- total-width total)))
  10267. (if (< (length h) maxwidth)
  10268. (progn (setq total (+ total (length h) 1)) h)
  10269. (setq h (substring h 0 (- maxwidth 2))
  10270. total (+ total maxwidth 1))
  10271. (if (string-match "[ \t]+\\'" h)
  10272. (setq h (substring h 0 (match-beginning 0))))
  10273. (setq h (concat h "..")))
  10274. (org-add-props h nil 'face
  10275. (nth (% (1- n) org-n-level-faces)
  10276. org-level-faces))
  10277. h)
  10278. path (or separator "/"))))))
  10279. (defun org-display-outline-path (&optional file current separator just-return-string)
  10280. "Display the current outline path in the echo area.
  10281. If FILE is non-nil, prepend the output with the file name.
  10282. If CURRENT is non-nil, append the current heading to the output.
  10283. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10284. the different parts of the path and defaults to \"/\".
  10285. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10286. (interactive "P")
  10287. (let* (case-fold-search
  10288. message-log-max ; Don't populate the *Messages* buffer
  10289. (bfn (buffer-file-name (buffer-base-buffer)))
  10290. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10291. res)
  10292. (if current (setq path (append path
  10293. (save-excursion
  10294. (org-back-to-heading t)
  10295. (if (looking-at org-complex-heading-regexp)
  10296. (list (match-string 4)))))))
  10297. (setq res
  10298. (org-format-outline-path
  10299. path
  10300. (1- (frame-width))
  10301. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10302. separator))
  10303. (if just-return-string
  10304. (org-no-properties res)
  10305. (message "%s" res))))
  10306. (defvar org-refile-history nil
  10307. "History for refiling operations.")
  10308. (defvar org-after-refile-insert-hook nil
  10309. "Hook run after `org-refile' has inserted its stuff at the new location.
  10310. Note that this is still *before* the stuff will be removed from
  10311. the *old* location.")
  10312. (defvar org-capture-last-stored-marker)
  10313. (defvar org-refile-keep nil
  10314. "Non-nil means `org-refile' will copy instead of refile.")
  10315. (defun org-copy ()
  10316. "Like `org-refile', but copy."
  10317. (interactive)
  10318. (let ((org-refile-keep t))
  10319. (funcall 'org-refile nil nil nil "Copy")))
  10320. (defun org-refile (&optional goto default-buffer rfloc msg)
  10321. "Move the entry or entries at point to another heading.
  10322. The list of target headings is compiled using the information in
  10323. `org-refile-targets', which see.
  10324. At the target location, the entry is filed as a subitem of the target
  10325. heading. Depending on `org-reverse-note-order', the new subitem will
  10326. either be the first or the last subitem.
  10327. If there is an active region, all entries in that region will be moved.
  10328. However, the region must fulfill the requirement that the first heading
  10329. is the first one sets the top-level of the moved text - at most siblings
  10330. below it are allowed.
  10331. With prefix arg GOTO, the command will only visit the target location
  10332. and not actually move anything.
  10333. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10334. go to the location where the last refiling operation has put the subtree.
  10335. With a prefix argument of `2', refile to the running clock.
  10336. RFLOC can be a refile location obtained in a different way.
  10337. MSG is a string to replace \"Refile\" in the default prompt with
  10338. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10339. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10340. If you are using target caching (see `org-refile-use-cache'),
  10341. you have to clear the target cache in order to find new targets.
  10342. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10343. prefix argument (`C-u C-u C-u C-c C-w')."
  10344. (interactive "P")
  10345. (if (member goto '(0 (64)))
  10346. (org-refile-cache-clear)
  10347. (let* ((actionmsg (or msg "Refile"))
  10348. (cbuf (current-buffer))
  10349. (regionp (org-region-active-p))
  10350. (region-start (and regionp (region-beginning)))
  10351. (region-end (and regionp (region-end)))
  10352. (region-length (and regionp (- region-end region-start)))
  10353. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10354. pos it nbuf file re level reversed)
  10355. (setq last-command nil)
  10356. (when regionp
  10357. (goto-char region-start)
  10358. (or (bolp) (goto-char (point-at-bol)))
  10359. (setq region-start (point))
  10360. (unless (or (org-kill-is-subtree-p
  10361. (buffer-substring region-start region-end))
  10362. (prog1 org-refile-active-region-within-subtree
  10363. (org-toggle-heading)))
  10364. (error "The region is not a (sequence of) subtree(s)")))
  10365. (if (equal goto '(16))
  10366. (org-refile-goto-last-stored)
  10367. (when (or
  10368. (and (equal goto 2)
  10369. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10370. (prog1
  10371. (setq it (list (or org-clock-heading "running clock")
  10372. (buffer-file-name
  10373. (marker-buffer org-clock-hd-marker))
  10374. ""
  10375. (marker-position org-clock-hd-marker)))
  10376. (setq goto nil)))
  10377. (setq it (or rfloc
  10378. (let (heading-text)
  10379. (save-excursion
  10380. (unless goto
  10381. (org-back-to-heading t)
  10382. (setq heading-text
  10383. (nth 4 (org-heading-components))))
  10384. (org-refile-get-location
  10385. (cond (goto "Goto")
  10386. (regionp (concat actionmsg " region to"))
  10387. (t (concat actionmsg " subtree \""
  10388. heading-text "\" to")))
  10389. default-buffer
  10390. (and (not (equal '(4) goto))
  10391. org-refile-allow-creating-parent-nodes)
  10392. goto))))))
  10393. (setq file (nth 1 it)
  10394. re (nth 2 it)
  10395. pos (nth 3 it))
  10396. (if (and (not goto)
  10397. pos
  10398. (equal (buffer-file-name) file)
  10399. (if regionp
  10400. (and (>= pos region-start)
  10401. (<= pos region-end))
  10402. (and (>= pos (point))
  10403. (< pos (save-excursion
  10404. (org-end-of-subtree t t))))))
  10405. (error "Cannot refile to position inside the tree or region"))
  10406. (setq nbuf (or (find-buffer-visiting file)
  10407. (find-file-noselect file)))
  10408. (if goto
  10409. (progn
  10410. (org-pop-to-buffer-same-window nbuf)
  10411. (goto-char pos)
  10412. (org-show-context 'org-goto))
  10413. (if regionp
  10414. (progn
  10415. (org-kill-new (buffer-substring region-start region-end))
  10416. (org-save-markers-in-region region-start region-end))
  10417. (org-copy-subtree 1 nil t))
  10418. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10419. (find-file-noselect file)))
  10420. (setq reversed (org-notes-order-reversed-p))
  10421. (save-excursion
  10422. (save-restriction
  10423. (widen)
  10424. (if pos
  10425. (progn
  10426. (goto-char pos)
  10427. (looking-at org-outline-regexp)
  10428. (setq level (org-get-valid-level (funcall outline-level) 1))
  10429. (goto-char
  10430. (if reversed
  10431. (or (outline-next-heading) (point-max))
  10432. (or (save-excursion (org-get-next-sibling))
  10433. (org-end-of-subtree t t)
  10434. (point-max)))))
  10435. (setq level 1)
  10436. (if (not reversed)
  10437. (goto-char (point-max))
  10438. (goto-char (point-min))
  10439. (or (outline-next-heading) (goto-char (point-max)))))
  10440. (if (not (bolp)) (newline))
  10441. (org-paste-subtree level)
  10442. (when org-log-refile
  10443. (org-add-log-setup 'refile nil nil 'findpos
  10444. org-log-refile)
  10445. (unless (eq org-log-refile 'note)
  10446. (save-excursion (org-add-log-note))))
  10447. (and org-auto-align-tags
  10448. (let ((org-loop-over-headlines-in-active-region nil))
  10449. (org-set-tags nil t)))
  10450. (with-demoted-errors
  10451. (bookmark-set "org-refile-last-stored"))
  10452. ;; If we are refiling for capture, make sure that the
  10453. ;; last-capture pointers point here
  10454. (when (org-bound-and-true-p org-refile-for-capture)
  10455. (with-demoted-errors
  10456. (bookmark-set "org-capture-last-stored-marker"))
  10457. (move-marker org-capture-last-stored-marker (point)))
  10458. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10459. (run-hooks 'org-after-refile-insert-hook))))
  10460. (unless org-refile-keep
  10461. (if regionp
  10462. (delete-region (point) (+ (point) region-length))
  10463. (org-cut-subtree)))
  10464. (when (featurep 'org-inlinetask)
  10465. (org-inlinetask-remove-END-maybe))
  10466. (setq org-markers-to-move nil)
  10467. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10468. (defun org-refile-goto-last-stored ()
  10469. "Go to the location where the last refile was stored."
  10470. (interactive)
  10471. (bookmark-jump "org-refile-last-stored")
  10472. (message "This is the location of the last refile"))
  10473. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10474. no-exclude)
  10475. "Prompt the user for a refile location, using PROMPT.
  10476. PROMPT should not be suffixed with a colon and a space, because
  10477. this function appends the default value from
  10478. `org-refile-history' automatically, if that is not empty.
  10479. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10480. this is used for the GOTO interface."
  10481. (let ((org-refile-targets org-refile-targets)
  10482. (org-refile-use-outline-path org-refile-use-outline-path)
  10483. excluded-entries)
  10484. (when (and (derived-mode-p 'org-mode)
  10485. (not org-refile-use-cache)
  10486. (not no-exclude))
  10487. (org-map-tree
  10488. (lambda()
  10489. (setq excluded-entries
  10490. (append excluded-entries (list (org-get-heading t t)))))))
  10491. (setq org-refile-target-table
  10492. (org-refile-get-targets default-buffer excluded-entries)))
  10493. (unless org-refile-target-table
  10494. (error "No refile targets"))
  10495. (let* ((cbuf (current-buffer))
  10496. (partial-completion-mode nil)
  10497. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10498. (cfunc (if (and org-refile-use-outline-path
  10499. org-outline-path-complete-in-steps)
  10500. 'org-olpath-completing-read
  10501. 'org-icompleting-read))
  10502. (extra (if org-refile-use-outline-path "/" ""))
  10503. (cbnex (concat (buffer-name) extra))
  10504. (filename (and cfn (expand-file-name cfn)))
  10505. (tbl (mapcar
  10506. (lambda (x)
  10507. (if (and (not (member org-refile-use-outline-path
  10508. '(file full-file-path)))
  10509. (not (equal filename (nth 1 x))))
  10510. (cons (concat (car x) extra " ("
  10511. (file-name-nondirectory (nth 1 x)) ")")
  10512. (cdr x))
  10513. (cons (concat (car x) extra) (cdr x))))
  10514. org-refile-target-table))
  10515. (completion-ignore-case t)
  10516. cdef
  10517. (prompt (concat prompt
  10518. (or (and (car org-refile-history)
  10519. (concat " (default " (car org-refile-history) ")"))
  10520. (and (assoc cbnex tbl) (setq cdef cbnex)
  10521. (concat " (default " cbnex ")"))) ": "))
  10522. pa answ parent-target child parent old-hist)
  10523. (setq old-hist org-refile-history)
  10524. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10525. nil 'org-refile-history (or cdef (car org-refile-history))))
  10526. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10527. (org-refile-check-position pa)
  10528. (if pa
  10529. (progn
  10530. (when (or (not org-refile-history)
  10531. (not (eq old-hist org-refile-history))
  10532. (not (equal (car pa) (car org-refile-history))))
  10533. (setq org-refile-history
  10534. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10535. org-refile-history
  10536. (cdr org-refile-history))))
  10537. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10538. (pop org-refile-history)))
  10539. pa)
  10540. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10541. (progn
  10542. (setq parent (match-string 1 answ)
  10543. child (match-string 2 answ))
  10544. (setq parent-target (or (assoc parent tbl)
  10545. (assoc (concat parent "/") tbl)))
  10546. (when (and parent-target
  10547. (or (eq new-nodes t)
  10548. (and (eq new-nodes 'confirm)
  10549. (y-or-n-p (format "Create new node \"%s\"? "
  10550. child)))))
  10551. (org-refile-new-child parent-target child)))
  10552. (error "Invalid target location")))))
  10553. (declare-function org-string-nw-p "org-macs" (s))
  10554. (defun org-refile-check-position (refile-pointer)
  10555. "Check if the refile pointer matches the headline to which it points."
  10556. (let* ((file (nth 1 refile-pointer))
  10557. (re (nth 2 refile-pointer))
  10558. (pos (nth 3 refile-pointer))
  10559. buffer)
  10560. (if (and (not (markerp pos)) (not file))
  10561. (error "Please save the buffer to a file before refiling")
  10562. (when (org-string-nw-p re)
  10563. (setq buffer (if (markerp pos)
  10564. (marker-buffer pos)
  10565. (or (find-buffer-visiting file)
  10566. (find-file-noselect file))))
  10567. (with-current-buffer buffer
  10568. (save-excursion
  10569. (save-restriction
  10570. (widen)
  10571. (goto-char pos)
  10572. (beginning-of-line 1)
  10573. (unless (org-looking-at-p re)
  10574. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10575. (defun org-refile-new-child (parent-target child)
  10576. "Use refile target PARENT-TARGET to add new CHILD below it."
  10577. (unless parent-target
  10578. (error "Cannot find parent for new node"))
  10579. (let ((file (nth 1 parent-target))
  10580. (pos (nth 3 parent-target))
  10581. level)
  10582. (with-current-buffer (or (find-buffer-visiting file)
  10583. (find-file-noselect file))
  10584. (save-excursion
  10585. (save-restriction
  10586. (widen)
  10587. (if pos
  10588. (goto-char pos)
  10589. (goto-char (point-max))
  10590. (if (not (bolp)) (newline)))
  10591. (when (looking-at org-outline-regexp)
  10592. (setq level (funcall outline-level))
  10593. (org-end-of-subtree t t))
  10594. (org-back-over-empty-lines)
  10595. (insert "\n" (make-string
  10596. (if pos (org-get-valid-level level 1) 1) ?*)
  10597. " " child "\n")
  10598. (beginning-of-line 0)
  10599. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10600. (defun org-olpath-completing-read (prompt collection &rest args)
  10601. "Read an outline path like a file name."
  10602. (let ((thetable collection)
  10603. (org-completion-use-ido nil) ; does not work with ido.
  10604. (org-completion-use-iswitchb nil)) ; or iswitchb
  10605. (apply
  10606. 'org-icompleting-read prompt
  10607. (lambda (string predicate &optional flag)
  10608. (let (rtn r f (l (length string)))
  10609. (cond
  10610. ((eq flag nil)
  10611. ;; try completion
  10612. (try-completion string thetable))
  10613. ((eq flag t)
  10614. ;; all-completions
  10615. (setq rtn (all-completions string thetable predicate))
  10616. (mapcar
  10617. (lambda (x)
  10618. (setq r (substring x l))
  10619. (if (string-match " ([^)]*)$" x)
  10620. (setq f (match-string 0 x))
  10621. (setq f ""))
  10622. (if (string-match "/" r)
  10623. (concat string (substring r 0 (match-end 0)) f)
  10624. x))
  10625. rtn))
  10626. ((eq flag 'lambda)
  10627. ;; exact match?
  10628. (assoc string thetable)))))
  10629. args)))
  10630. ;;;; Dynamic blocks
  10631. (defun org-find-dblock (name)
  10632. "Find the first dynamic block with name NAME in the buffer.
  10633. If not found, stay at current position and return nil."
  10634. (let ((case-fold-search t) pos)
  10635. (save-excursion
  10636. (goto-char (point-min))
  10637. (setq pos (and (re-search-forward
  10638. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10639. (match-beginning 0))))
  10640. (if pos (goto-char pos))
  10641. pos))
  10642. (defconst org-dblock-start-re
  10643. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10644. "Matches the start line of a dynamic block, with parameters.")
  10645. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10646. "Matches the end of a dynamic block.")
  10647. (defun org-create-dblock (plist)
  10648. "Create a dynamic block section, with parameters taken from PLIST.
  10649. PLIST must contain a :name entry which is used as name of the block."
  10650. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10651. (end-of-line 1)
  10652. (newline))
  10653. (let ((col (current-column))
  10654. (name (plist-get plist :name)))
  10655. (insert "#+BEGIN: " name)
  10656. (while plist
  10657. (if (eq (car plist) :name)
  10658. (setq plist (cddr plist))
  10659. (insert " " (prin1-to-string (pop plist)))))
  10660. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10661. (beginning-of-line -2)))
  10662. (defun org-prepare-dblock ()
  10663. "Prepare dynamic block for refresh.
  10664. This empties the block, puts the cursor at the insert position and returns
  10665. the property list including an extra property :name with the block name."
  10666. (unless (looking-at org-dblock-start-re)
  10667. (error "Not at a dynamic block"))
  10668. (let* ((begdel (1+ (match-end 0)))
  10669. (name (org-no-properties (match-string 1)))
  10670. (params (append (list :name name)
  10671. (read (concat "(" (match-string 3) ")")))))
  10672. (save-excursion
  10673. (beginning-of-line 1)
  10674. (skip-chars-forward " \t")
  10675. (setq params (plist-put params :indentation-column (current-column))))
  10676. (unless (re-search-forward org-dblock-end-re nil t)
  10677. (error "Dynamic block not terminated"))
  10678. (setq params
  10679. (append params
  10680. (list :content (buffer-substring
  10681. begdel (match-beginning 0)))))
  10682. (delete-region begdel (match-beginning 0))
  10683. (goto-char begdel)
  10684. (open-line 1)
  10685. params))
  10686. (defun org-map-dblocks (&optional command)
  10687. "Apply COMMAND to all dynamic blocks in the current buffer.
  10688. If COMMAND is not given, use `org-update-dblock'."
  10689. (let ((cmd (or command 'org-update-dblock)))
  10690. (save-excursion
  10691. (goto-char (point-min))
  10692. (while (re-search-forward org-dblock-start-re nil t)
  10693. (goto-char (match-beginning 0))
  10694. (save-excursion
  10695. (condition-case nil
  10696. (funcall cmd)
  10697. (error (message "Error during update of dynamic block"))))
  10698. (unless (re-search-forward org-dblock-end-re nil t)
  10699. (error "Dynamic block not terminated"))))))
  10700. (defun org-dblock-update (&optional arg)
  10701. "User command for updating dynamic blocks.
  10702. Update the dynamic block at point. With prefix ARG, update all dynamic
  10703. blocks in the buffer."
  10704. (interactive "P")
  10705. (if arg
  10706. (org-update-all-dblocks)
  10707. (or (looking-at org-dblock-start-re)
  10708. (org-beginning-of-dblock))
  10709. (org-update-dblock)))
  10710. (defun org-update-dblock ()
  10711. "Update the dynamic block at point.
  10712. This means to empty the block, parse for parameters and then call
  10713. the correct writing function."
  10714. (interactive)
  10715. (save-window-excursion
  10716. (let* ((pos (point))
  10717. (line (org-current-line))
  10718. (params (org-prepare-dblock))
  10719. (name (plist-get params :name))
  10720. (indent (plist-get params :indentation-column))
  10721. (cmd (intern (concat "org-dblock-write:" name))))
  10722. (message "Updating dynamic block `%s' at line %d..." name line)
  10723. (funcall cmd params)
  10724. (message "Updating dynamic block `%s' at line %d...done" name line)
  10725. (goto-char pos)
  10726. (when (and indent (> indent 0))
  10727. (setq indent (make-string indent ?\ ))
  10728. (save-excursion
  10729. (org-beginning-of-dblock)
  10730. (forward-line 1)
  10731. (while (not (looking-at org-dblock-end-re))
  10732. (insert indent)
  10733. (beginning-of-line 2))
  10734. (when (looking-at org-dblock-end-re)
  10735. (and (looking-at "[ \t]+")
  10736. (replace-match ""))
  10737. (insert indent)))))))
  10738. (defun org-beginning-of-dblock ()
  10739. "Find the beginning of the dynamic block at point.
  10740. Error if there is no such block at point."
  10741. (let ((pos (point))
  10742. beg)
  10743. (end-of-line 1)
  10744. (if (and (re-search-backward org-dblock-start-re nil t)
  10745. (setq beg (match-beginning 0))
  10746. (re-search-forward org-dblock-end-re nil t)
  10747. (> (match-end 0) pos))
  10748. (goto-char beg)
  10749. (goto-char pos)
  10750. (error "Not in a dynamic block"))))
  10751. (defun org-update-all-dblocks ()
  10752. "Update all dynamic blocks in the buffer.
  10753. This function can be used in a hook."
  10754. (interactive)
  10755. (when (derived-mode-p 'org-mode)
  10756. (org-map-dblocks 'org-update-dblock)))
  10757. ;;;; Completion
  10758. (defun org-get-export-keywords ()
  10759. "Return a list of all currently understood export keywords.
  10760. Export keywords include options, block names, attributes and
  10761. keywords relative to each registered export back-end."
  10762. (delq nil
  10763. (let (keywords)
  10764. (mapc
  10765. (lambda (back-end)
  10766. (let ((props (cdr back-end)))
  10767. ;; Back-end name (for keywords, like #+LATEX:)
  10768. (push (upcase (symbol-name (car back-end))) keywords)
  10769. ;; Back-end options.
  10770. (mapc (lambda (option) (push (cadr option) keywords))
  10771. (plist-get (cdr back-end) :options-alist))))
  10772. (org-bound-and-true-p org-export-registered-backends))
  10773. keywords)))
  10774. (defconst org-options-keywords
  10775. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10776. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10777. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10778. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10779. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  10780. (defcustom org-structure-template-alist
  10781. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10782. "<src lang=\"?\">\n\n</src>")
  10783. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10784. "<example>\n?\n</example>")
  10785. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10786. "<quote>\n?\n</quote>")
  10787. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10788. "<verse>\n?\n</verse>")
  10789. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10790. "<verbatim>\n?\n</verbatim>")
  10791. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10792. "<center>\n?\n</center>")
  10793. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10794. "<literal style=\"latex\">\n?\n</literal>")
  10795. ("L" "#+LaTeX: "
  10796. "<literal style=\"latex\">?</literal>")
  10797. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10798. "<literal style=\"html\">\n?\n</literal>")
  10799. ("H" "#+HTML: "
  10800. "<literal style=\"html\">?</literal>")
  10801. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10802. ("A" "#+ASCII: ")
  10803. ("i" "#+INDEX: ?"
  10804. "#+INDEX: ?")
  10805. ("I" "#+INCLUDE: %file ?"
  10806. "<include file=%file markup=\"?\">"))
  10807. "Structure completion elements.
  10808. This is a list of abbreviation keys and values. The value gets inserted
  10809. if you type `<' followed by the key and then press the completion key,
  10810. usually `M-TAB'. %file will be replaced by a file name after prompting
  10811. for the file using completion. The cursor will be placed at the position
  10812. of the `?` in the template.
  10813. There are two templates for each key, the first uses the original Org syntax,
  10814. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10815. the default when the /org-mtags.el/ module has been loaded. See also the
  10816. variable `org-mtags-prefer-muse-templates'."
  10817. :group 'org-completion
  10818. :type '(repeat
  10819. (string :tag "Key")
  10820. (string :tag "Template")
  10821. (string :tag "Muse Template")))
  10822. (defun org-try-structure-completion ()
  10823. "Try to complete a structure template before point.
  10824. This looks for strings like \"<e\" on an otherwise empty line and
  10825. expands them."
  10826. (let ((l (buffer-substring (point-at-bol) (point)))
  10827. a)
  10828. (when (and (looking-at "[ \t]*$")
  10829. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10830. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10831. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10832. (match-beginning 1)) a)
  10833. t)))
  10834. (defun org-complete-expand-structure-template (start cell)
  10835. "Expand a structure template."
  10836. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10837. (rpl (nth (if musep 2 1) cell))
  10838. (ind ""))
  10839. (delete-region start (point))
  10840. (when (string-match "\\`#\\+" rpl)
  10841. (cond
  10842. ((bolp))
  10843. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10844. (setq ind (buffer-substring (point-at-bol) (point))))
  10845. (t (newline))))
  10846. (setq start (point))
  10847. (if (string-match "%file" rpl)
  10848. (setq rpl (replace-match
  10849. (concat
  10850. "\""
  10851. (save-match-data
  10852. (abbreviate-file-name (read-file-name "Include file: ")))
  10853. "\"")
  10854. t t rpl)))
  10855. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10856. (concat "\n" ind)))
  10857. (insert rpl)
  10858. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10859. ;;;; TODO, DEADLINE, Comments
  10860. (defun org-toggle-comment ()
  10861. "Change the COMMENT state of an entry."
  10862. (interactive)
  10863. (save-excursion
  10864. (org-back-to-heading)
  10865. (let (case-fold-search)
  10866. (cond
  10867. ((looking-at (format org-heading-keyword-regexp-format
  10868. org-comment-string))
  10869. (goto-char (match-end 1))
  10870. (looking-at (concat " +" org-comment-string))
  10871. (replace-match "" t t)
  10872. (when (eolp) (insert " ")))
  10873. ((looking-at org-outline-regexp)
  10874. (goto-char (match-end 0))
  10875. (insert org-comment-string " "))))))
  10876. (defvar org-last-todo-state-is-todo nil
  10877. "This is non-nil when the last TODO state change led to a TODO state.
  10878. If the last change removed the TODO tag or switched to DONE, then
  10879. this is nil.")
  10880. (defvar org-setting-tags nil) ; dynamically skipped
  10881. (defvar org-todo-setup-filter-hook nil
  10882. "Hook for functions that pre-filter todo specs.
  10883. Each function takes a todo spec and returns either nil or the spec
  10884. transformed into canonical form." )
  10885. (defvar org-todo-get-default-hook nil
  10886. "Hook for functions that get a default item for todo.
  10887. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10888. nil or a string to be used for the todo mark." )
  10889. (defvar org-agenda-headline-snapshot-before-repeat)
  10890. (defun org-current-effective-time ()
  10891. "Return current time adjusted for `org-extend-today-until' variable."
  10892. (let* ((ct (org-current-time))
  10893. (dct (decode-time ct))
  10894. (ct1
  10895. (cond
  10896. (org-use-last-clock-out-time-as-effective-time
  10897. (or (org-clock-get-last-clock-out-time) ct))
  10898. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10899. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10900. (t ct))))
  10901. ct1))
  10902. (defun org-todo-yesterday (&optional arg)
  10903. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10904. (interactive "P")
  10905. (if (eq major-mode 'org-agenda-mode)
  10906. (apply 'org-agenda-todo-yesterday arg)
  10907. (let* ((hour (third (decode-time
  10908. (org-current-time))))
  10909. (org-extend-today-until (1+ hour)))
  10910. (org-todo arg))))
  10911. (defvar org-block-entry-blocking ""
  10912. "First entry preventing the TODO state change.")
  10913. (defun org-todo (&optional arg)
  10914. "Change the TODO state of an item.
  10915. The state of an item is given by a keyword at the start of the heading,
  10916. like
  10917. *** TODO Write paper
  10918. *** DONE Call mom
  10919. The different keywords are specified in the variable `org-todo-keywords'.
  10920. By default the available states are \"TODO\" and \"DONE\".
  10921. So for this example: when the item starts with TODO, it is changed to DONE.
  10922. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10923. DONE are present, add TODO at the beginning of the heading.
  10924. With \\[universal-argument] prefix arg, use completion to determine the new \
  10925. state.
  10926. With numeric prefix arg, switch to that state.
  10927. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10928. keywords (nextset).
  10929. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10930. With a numeric prefix arg of 0, inhibit note taking for the change.
  10931. For calling through lisp, arg is also interpreted in the following way:
  10932. 'none -> empty state
  10933. \"\"(empty string) -> switch to empty state
  10934. 'done -> switch to DONE
  10935. 'nextset -> switch to the next set of keywords
  10936. 'previousset -> switch to the previous set of keywords
  10937. \"WAITING\" -> switch to the specified keyword, but only if it
  10938. really is a member of `org-todo-keywords'."
  10939. (interactive "P")
  10940. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10941. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10942. 'region-start-level 'region))
  10943. org-loop-over-headlines-in-active-region)
  10944. (org-map-entries
  10945. `(org-todo ,arg)
  10946. org-loop-over-headlines-in-active-region
  10947. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10948. (if (equal arg '(16)) (setq arg 'nextset))
  10949. (let ((org-blocker-hook org-blocker-hook)
  10950. commentp
  10951. case-fold-search)
  10952. (when (equal arg '(64))
  10953. (setq arg nil org-blocker-hook nil))
  10954. (when (and org-blocker-hook
  10955. (or org-inhibit-blocking
  10956. (org-entry-get nil "NOBLOCKING")))
  10957. (setq org-blocker-hook nil))
  10958. (save-excursion
  10959. (catch 'exit
  10960. (org-back-to-heading t)
  10961. (when (looking-at (concat "^\\*+ " org-comment-string))
  10962. (org-toggle-comment)
  10963. (setq commentp t))
  10964. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10965. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10966. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10967. (let* ((match-data (match-data))
  10968. (startpos (point-at-bol))
  10969. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10970. (org-log-done org-log-done)
  10971. (org-log-repeat org-log-repeat)
  10972. (org-todo-log-states org-todo-log-states)
  10973. (org-inhibit-logging
  10974. (if (equal arg 0)
  10975. (progn (setq arg nil) 'note) org-inhibit-logging))
  10976. (this (match-string 1))
  10977. (hl-pos (match-beginning 0))
  10978. (head (org-get-todo-sequence-head this))
  10979. (ass (assoc head org-todo-kwd-alist))
  10980. (interpret (nth 1 ass))
  10981. (done-word (nth 3 ass))
  10982. (final-done-word (nth 4 ass))
  10983. (org-last-state (or this ""))
  10984. (completion-ignore-case t)
  10985. (member (member this org-todo-keywords-1))
  10986. (tail (cdr member))
  10987. (org-state (cond
  10988. ((and org-todo-key-trigger
  10989. (or (and (equal arg '(4))
  10990. (eq org-use-fast-todo-selection 'prefix))
  10991. (and (not arg) org-use-fast-todo-selection
  10992. (not (eq org-use-fast-todo-selection
  10993. 'prefix)))))
  10994. ;; Use fast selection
  10995. (org-fast-todo-selection))
  10996. ((and (equal arg '(4))
  10997. (or (not org-use-fast-todo-selection)
  10998. (not org-todo-key-trigger)))
  10999. ;; Read a state with completion
  11000. (org-icompleting-read
  11001. "State: " (mapcar 'list org-todo-keywords-1)
  11002. nil t))
  11003. ((eq arg 'right)
  11004. (if this
  11005. (if tail (car tail) nil)
  11006. (car org-todo-keywords-1)))
  11007. ((eq arg 'left)
  11008. (if (equal member org-todo-keywords-1)
  11009. nil
  11010. (if this
  11011. (nth (- (length org-todo-keywords-1)
  11012. (length tail) 2)
  11013. org-todo-keywords-1)
  11014. (org-last org-todo-keywords-1))))
  11015. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11016. (setq arg nil))) ; hack to fall back to cycling
  11017. (arg
  11018. ;; user or caller requests a specific state
  11019. (cond
  11020. ((equal arg "") nil)
  11021. ((eq arg 'none) nil)
  11022. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11023. ((eq arg 'nextset)
  11024. (or (car (cdr (member head org-todo-heads)))
  11025. (car org-todo-heads)))
  11026. ((eq arg 'previousset)
  11027. (let ((org-todo-heads (reverse org-todo-heads)))
  11028. (or (car (cdr (member head org-todo-heads)))
  11029. (car org-todo-heads))))
  11030. ((car (member arg org-todo-keywords-1)))
  11031. ((stringp arg)
  11032. (error "State `%s' not valid in this file" arg))
  11033. ((nth (1- (prefix-numeric-value arg))
  11034. org-todo-keywords-1))))
  11035. ((null member) (or head (car org-todo-keywords-1)))
  11036. ((equal this final-done-word) nil) ;; -> make empty
  11037. ((null tail) nil) ;; -> first entry
  11038. ((memq interpret '(type priority))
  11039. (if (eq this-command last-command)
  11040. (car tail)
  11041. (if (> (length tail) 0)
  11042. (or done-word (car org-done-keywords))
  11043. nil)))
  11044. (t
  11045. (car tail))))
  11046. (org-state (or
  11047. (run-hook-with-args-until-success
  11048. 'org-todo-get-default-hook org-state org-last-state)
  11049. org-state))
  11050. (next (if org-state (concat " " org-state " ") " "))
  11051. (change-plist (list :type 'todo-state-change :from this :to org-state
  11052. :position startpos))
  11053. dolog now-done-p)
  11054. (when org-blocker-hook
  11055. (setq org-last-todo-state-is-todo
  11056. (not (member this org-done-keywords)))
  11057. (unless (save-excursion
  11058. (save-match-data
  11059. (org-with-wide-buffer
  11060. (run-hook-with-args-until-failure
  11061. 'org-blocker-hook change-plist))))
  11062. (if (org-called-interactively-p 'interactive)
  11063. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11064. this org-state org-block-entry-blocking)
  11065. ;; fail silently
  11066. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11067. this org-state org-block-entry-blocking)
  11068. (throw 'exit nil))))
  11069. (store-match-data match-data)
  11070. (replace-match next t t)
  11071. (unless (pos-visible-in-window-p hl-pos)
  11072. (message "TODO state changed to %s" (org-trim next)))
  11073. (unless head
  11074. (setq head (org-get-todo-sequence-head org-state)
  11075. ass (assoc head org-todo-kwd-alist)
  11076. interpret (nth 1 ass)
  11077. done-word (nth 3 ass)
  11078. final-done-word (nth 4 ass)))
  11079. (when (memq arg '(nextset previousset))
  11080. (message "Keyword-Set %d/%d: %s"
  11081. (- (length org-todo-sets) -1
  11082. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11083. (length org-todo-sets)
  11084. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11085. (setq org-last-todo-state-is-todo
  11086. (not (member org-state org-done-keywords)))
  11087. (setq now-done-p (and (member org-state org-done-keywords)
  11088. (not (member this org-done-keywords))))
  11089. (and logging (org-local-logging logging))
  11090. (when (and (or org-todo-log-states org-log-done)
  11091. (not (eq org-inhibit-logging t))
  11092. (not (memq arg '(nextset previousset))))
  11093. ;; we need to look at recording a time and note
  11094. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11095. (nth 2 (assoc this org-todo-log-states))))
  11096. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11097. (setq dolog 'time))
  11098. (when (or (not org-state)
  11099. (and org-state
  11100. (member org-state org-not-done-keywords)
  11101. (not (member this org-not-done-keywords))))
  11102. ;; This is now a todo state and was not one before
  11103. ;; If there was a CLOSED time stamp, get rid of it.
  11104. (org-add-planning-info nil nil 'closed))
  11105. (when (and now-done-p org-log-done)
  11106. ;; It is now done, and it was not done before
  11107. (org-add-planning-info 'closed (org-current-effective-time))
  11108. (if (and (not dolog) (eq 'note org-log-done))
  11109. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11110. (when (and org-state dolog)
  11111. ;; This is a non-nil state, and we need to log it
  11112. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11113. ;; Fixup tag positioning
  11114. (org-todo-trigger-tag-changes org-state)
  11115. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11116. (when org-provide-todo-statistics
  11117. (org-update-parent-todo-statistics))
  11118. (run-hooks 'org-after-todo-state-change-hook)
  11119. (if (and arg (not (member org-state org-done-keywords)))
  11120. (setq head (org-get-todo-sequence-head org-state)))
  11121. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11122. ;; Do we need to trigger a repeat?
  11123. (when now-done-p
  11124. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11125. ;; This is for the agenda, take a snapshot of the headline.
  11126. (save-match-data
  11127. (setq org-agenda-headline-snapshot-before-repeat
  11128. (org-get-heading))))
  11129. (org-auto-repeat-maybe org-state))
  11130. ;; Fixup cursor location if close to the keyword
  11131. (if (and (outline-on-heading-p)
  11132. (not (bolp))
  11133. (save-excursion (beginning-of-line 1)
  11134. (looking-at org-todo-line-regexp))
  11135. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11136. (progn
  11137. (goto-char (or (match-end 2) (match-end 1)))
  11138. (and (looking-at " ") (just-one-space))))
  11139. (when org-trigger-hook
  11140. (save-excursion
  11141. (run-hook-with-args 'org-trigger-hook change-plist)))
  11142. (when commentp (org-toggle-comment))))))))
  11143. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11144. "Block turning an entry into a TODO, using the hierarchy.
  11145. This checks whether the current task should be blocked from state
  11146. changes. Such blocking occurs when:
  11147. 1. The task has children which are not all in a completed state.
  11148. 2. A task has a parent with the property :ORDERED:, and there
  11149. are siblings prior to the current task with incomplete
  11150. status.
  11151. 3. The parent of the task is blocked because it has siblings that should
  11152. be done first, or is child of a block grandparent TODO entry."
  11153. (if (not org-enforce-todo-dependencies)
  11154. t ; if locally turned off don't block
  11155. (catch 'dont-block
  11156. ;; If this is not a todo state change, or if this entry is already DONE,
  11157. ;; do not block
  11158. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11159. (member (plist-get change-plist :from)
  11160. (cons 'done org-done-keywords))
  11161. (member (plist-get change-plist :to)
  11162. (cons 'todo org-not-done-keywords))
  11163. (not (plist-get change-plist :to)))
  11164. (throw 'dont-block t))
  11165. ;; If this task has children, and any are undone, it's blocked
  11166. (save-excursion
  11167. (org-back-to-heading t)
  11168. (let ((this-level (funcall outline-level)))
  11169. (outline-next-heading)
  11170. (let ((child-level (funcall outline-level)))
  11171. (while (and (not (eobp))
  11172. (> child-level this-level))
  11173. ;; this todo has children, check whether they are all
  11174. ;; completed
  11175. (if (and (not (org-entry-is-done-p))
  11176. (org-entry-is-todo-p))
  11177. (progn (setq org-block-entry-blocking (org-get-heading))
  11178. (throw 'dont-block nil)))
  11179. (outline-next-heading)
  11180. (setq child-level (funcall outline-level))))))
  11181. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11182. ;; any previous siblings are undone, it's blocked
  11183. (save-excursion
  11184. (org-back-to-heading t)
  11185. (let* ((pos (point))
  11186. (parent-pos (and (org-up-heading-safe) (point))))
  11187. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11188. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11189. (forward-line 1)
  11190. (re-search-forward org-not-done-heading-regexp pos t))
  11191. (setq org-block-entry-blocking (match-string 0))
  11192. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11193. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11194. (while t
  11195. (goto-char parent-pos)
  11196. (if (not (looking-at org-not-done-heading-regexp))
  11197. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11198. (setq pos (point))
  11199. (setq parent-pos (and (org-up-heading-safe) (point)))
  11200. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11201. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11202. (forward-line 1)
  11203. (re-search-forward org-not-done-heading-regexp pos t)
  11204. (setq org-block-entry-blocking (org-get-heading)))
  11205. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11206. (defcustom org-track-ordered-property-with-tag nil
  11207. "Should the ORDERED property also be shown as a tag?
  11208. The ORDERED property decides if an entry should require subtasks to be
  11209. completed in sequence. Since a property is not very visible, setting
  11210. this option means that toggling the ORDERED property with the command
  11211. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11212. not relevant for the behavior, but it makes things more visible.
  11213. Note that toggling the tag with tags commands will not change the property
  11214. and therefore not influence behavior!
  11215. This can be t, meaning the tag ORDERED should be used, It can also be a
  11216. string to select a different tag for this task."
  11217. :group 'org-todo
  11218. :type '(choice
  11219. (const :tag "No tracking" nil)
  11220. (const :tag "Track with ORDERED tag" t)
  11221. (string :tag "Use other tag")))
  11222. (defun org-toggle-ordered-property ()
  11223. "Toggle the ORDERED property of the current entry.
  11224. For better visibility, you can track the value of this property with a tag.
  11225. See variable `org-track-ordered-property-with-tag'."
  11226. (interactive)
  11227. (let* ((t1 org-track-ordered-property-with-tag)
  11228. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11229. (save-excursion
  11230. (org-back-to-heading)
  11231. (if (org-entry-get nil "ORDERED")
  11232. (progn
  11233. (org-delete-property "ORDERED" "PROPERTIES")
  11234. (and tag (org-toggle-tag tag 'off))
  11235. (message "Subtasks can be completed in arbitrary order"))
  11236. (org-entry-put nil "ORDERED" "t")
  11237. (and tag (org-toggle-tag tag 'on))
  11238. (message "Subtasks must be completed in sequence")))))
  11239. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11240. (defun org-block-todo-from-checkboxes (change-plist)
  11241. "Block turning an entry into a TODO, using checkboxes.
  11242. This checks whether the current task should be blocked from state
  11243. changes because there are unchecked boxes in this entry."
  11244. (if (not org-enforce-todo-checkbox-dependencies)
  11245. t ; if locally turned off don't block
  11246. (catch 'dont-block
  11247. ;; If this is not a todo state change, or if this entry is already DONE,
  11248. ;; do not block
  11249. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11250. (member (plist-get change-plist :from)
  11251. (cons 'done org-done-keywords))
  11252. (member (plist-get change-plist :to)
  11253. (cons 'todo org-not-done-keywords))
  11254. (not (plist-get change-plist :to)))
  11255. (throw 'dont-block t))
  11256. ;; If this task has checkboxes that are not checked, it's blocked
  11257. (save-excursion
  11258. (org-back-to-heading t)
  11259. (let ((beg (point)) end)
  11260. (outline-next-heading)
  11261. (setq end (point))
  11262. (goto-char beg)
  11263. (if (org-list-search-forward
  11264. (concat (org-item-beginning-re)
  11265. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11266. "\\[[- ]\\]")
  11267. end t)
  11268. (progn
  11269. (if (boundp 'org-blocked-by-checkboxes)
  11270. (setq org-blocked-by-checkboxes t))
  11271. (throw 'dont-block nil)))))
  11272. t))) ; do not block
  11273. (defun org-entry-blocked-p ()
  11274. "Is the current entry blocked?"
  11275. (org-with-silent-modifications
  11276. (if (org-entry-get nil "NOBLOCKING")
  11277. nil ;; Never block this entry
  11278. (not (run-hook-with-args-until-failure
  11279. 'org-blocker-hook
  11280. (list :type 'todo-state-change
  11281. :position (point)
  11282. :from 'todo
  11283. :to 'done))))))
  11284. (defun org-update-statistics-cookies (all)
  11285. "Update the statistics cookie, either from TODO or from checkboxes.
  11286. This should be called with the cursor in a line with a statistics cookie."
  11287. (interactive "P")
  11288. (if all
  11289. (progn
  11290. (org-update-checkbox-count 'all)
  11291. (org-map-entries 'org-update-parent-todo-statistics))
  11292. (if (not (org-at-heading-p))
  11293. (org-update-checkbox-count)
  11294. (let ((pos (point-marker))
  11295. end l1 l2)
  11296. (ignore-errors (org-back-to-heading t))
  11297. (if (not (org-at-heading-p))
  11298. (org-update-checkbox-count)
  11299. (setq l1 (org-outline-level))
  11300. (setq end (save-excursion
  11301. (outline-next-heading)
  11302. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11303. (point)))
  11304. (if (and (save-excursion
  11305. (re-search-forward
  11306. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11307. (not (save-excursion (re-search-forward
  11308. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11309. (org-update-checkbox-count)
  11310. (if (and l2 (> l2 l1))
  11311. (progn
  11312. (goto-char end)
  11313. (org-update-parent-todo-statistics))
  11314. (goto-char pos)
  11315. (beginning-of-line 1)
  11316. (while (re-search-forward
  11317. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11318. (point-at-eol) t)
  11319. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11320. (goto-char pos)
  11321. (move-marker pos nil)))))
  11322. (defvar org-entry-property-inherited-from) ;; defined below
  11323. (defun org-update-parent-todo-statistics ()
  11324. "Update any statistics cookie in the parent of the current headline.
  11325. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11326. the entire subtree and this will travel up the hierarchy and update
  11327. statistics everywhere."
  11328. (let* ((prop (save-excursion (org-up-heading-safe)
  11329. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11330. (recursive (or (not org-hierarchical-todo-statistics)
  11331. (and prop (string-match "\\<recursive\\>" prop))))
  11332. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11333. 0))
  11334. (first t)
  11335. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11336. level ltoggle l1 new ndel
  11337. (cnt-all 0) (cnt-done 0) is-percent kwd
  11338. checkbox-beg ov ovs ove cookie-present)
  11339. (catch 'exit
  11340. (save-excursion
  11341. (beginning-of-line 1)
  11342. (setq ltoggle (funcall outline-level))
  11343. ;; Three situations are to consider:
  11344. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11345. ;; to the top-level ancestor on the headline;
  11346. ;; 2. If parent has "recursive" property, repeat up to the
  11347. ;; headline setting that property, taking inheritance into
  11348. ;; account;
  11349. ;; 3. Else, move up to direct parent and proceed only once.
  11350. (while (and (setq level (org-up-heading-safe))
  11351. (or recursive first)
  11352. (>= (point) lim))
  11353. (setq first nil cookie-present nil)
  11354. (unless (and level
  11355. (not (string-match
  11356. "\\<checkbox\\>"
  11357. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11358. "")))))
  11359. (throw 'exit nil))
  11360. (while (re-search-forward box-re (point-at-eol) t)
  11361. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11362. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11363. (save-match-data
  11364. (unless (outline-next-heading) (throw 'exit nil))
  11365. (while (and (looking-at org-complex-heading-regexp)
  11366. (> (setq l1 (length (match-string 1))) level))
  11367. (setq kwd (and (or recursive (= l1 ltoggle))
  11368. (match-string 2)))
  11369. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11370. (and (listp org-provide-todo-statistics)
  11371. (or (member kwd org-provide-todo-statistics)
  11372. (member kwd org-done-keywords))))
  11373. (setq cnt-all (1+ cnt-all))
  11374. (if (eq org-provide-todo-statistics t)
  11375. (and kwd (setq cnt-all (1+ cnt-all)))))
  11376. (and (member kwd org-done-keywords)
  11377. (setq cnt-done (1+ cnt-done)))
  11378. (outline-next-heading)))
  11379. (setq new
  11380. (if is-percent
  11381. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11382. (format "[%d/%d]" cnt-done cnt-all))
  11383. ndel (- (match-end 0) checkbox-beg))
  11384. ;; handle overlays when updating cookie from column view
  11385. (when (setq ov (car (overlays-at checkbox-beg)))
  11386. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11387. (delete-overlay ov))
  11388. (goto-char checkbox-beg)
  11389. (insert new)
  11390. (delete-region (point) (+ (point) ndel))
  11391. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11392. (when ov (move-overlay ov ovs ove)))
  11393. (when cookie-present
  11394. (run-hook-with-args 'org-after-todo-statistics-hook
  11395. cnt-done (- cnt-all cnt-done))))))
  11396. (run-hooks 'org-todo-statistics-hook)))
  11397. (defvar org-after-todo-statistics-hook nil
  11398. "Hook that is called after a TODO statistics cookie has been updated.
  11399. Each function is called with two arguments: the number of not-done entries
  11400. and the number of done entries.
  11401. For example, the following function, when added to this hook, will switch
  11402. an entry to DONE when all children are done, and back to TODO when new
  11403. entries are set to a TODO status. Note that this hook is only called
  11404. when there is a statistics cookie in the headline!
  11405. (defun org-summary-todo (n-done n-not-done)
  11406. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11407. (let (org-log-done org-log-states) ; turn off logging
  11408. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11409. ")
  11410. (defvar org-todo-statistics-hook nil
  11411. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11412. This hook runs even if there is no statistics cookie present, in which case
  11413. `org-after-todo-statistics-hook' would not run.")
  11414. (defun org-todo-trigger-tag-changes (state)
  11415. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11416. (let ((l org-todo-state-tags-triggers)
  11417. changes)
  11418. (when (or (not state) (equal state ""))
  11419. (setq changes (append changes (cdr (assoc "" l)))))
  11420. (when (and (stringp state) (> (length state) 0))
  11421. (setq changes (append changes (cdr (assoc state l)))))
  11422. (when (member state org-not-done-keywords)
  11423. (setq changes (append changes (cdr (assoc 'todo l)))))
  11424. (when (member state org-done-keywords)
  11425. (setq changes (append changes (cdr (assoc 'done l)))))
  11426. (dolist (c changes)
  11427. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11428. (defun org-local-logging (value)
  11429. "Get logging settings from a property VALUE."
  11430. (let* (words w a)
  11431. ;; directly set the variables, they are already local.
  11432. (setq org-log-done nil
  11433. org-log-repeat nil
  11434. org-todo-log-states nil)
  11435. (setq words (org-split-string value))
  11436. (while (setq w (pop words))
  11437. (cond
  11438. ((setq a (assoc w org-startup-options))
  11439. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11440. (set (nth 1 a) (nth 2 a))))
  11441. ((setq a (org-extract-log-state-settings w))
  11442. (and (member (car a) org-todo-keywords-1)
  11443. (push a org-todo-log-states)))))))
  11444. (defun org-get-todo-sequence-head (kwd)
  11445. "Return the head of the TODO sequence to which KWD belongs.
  11446. If KWD is not set, check if there is a text property remembering the
  11447. right sequence."
  11448. (let (p)
  11449. (cond
  11450. ((not kwd)
  11451. (or (get-text-property (point-at-bol) 'org-todo-head)
  11452. (progn
  11453. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11454. nil (point-at-eol)))
  11455. (get-text-property p 'org-todo-head))))
  11456. ((not (member kwd org-todo-keywords-1))
  11457. (car org-todo-keywords-1))
  11458. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11459. (defun org-fast-todo-selection ()
  11460. "Fast TODO keyword selection with single keys.
  11461. Returns the new TODO keyword, or nil if no state change should occur."
  11462. (let* ((fulltable org-todo-key-alist)
  11463. (done-keywords org-done-keywords) ;; needed for the faces.
  11464. (maxlen (apply 'max (mapcar
  11465. (lambda (x)
  11466. (if (stringp (car x)) (string-width (car x)) 0))
  11467. fulltable)))
  11468. (expert nil)
  11469. (fwidth (+ maxlen 3 1 3))
  11470. (ncol (/ (- (window-width) 4) fwidth))
  11471. tg cnt e c tbl
  11472. groups ingroup)
  11473. (save-excursion
  11474. (save-window-excursion
  11475. (if expert
  11476. (set-buffer (get-buffer-create " *Org todo*"))
  11477. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11478. (erase-buffer)
  11479. (org-set-local 'org-done-keywords done-keywords)
  11480. (setq tbl fulltable cnt 0)
  11481. (while (setq e (pop tbl))
  11482. (cond
  11483. ((equal e '(:startgroup))
  11484. (push '() groups) (setq ingroup t)
  11485. (when (not (= cnt 0))
  11486. (setq cnt 0)
  11487. (insert "\n"))
  11488. (insert "{ "))
  11489. ((equal e '(:endgroup))
  11490. (setq ingroup nil cnt 0)
  11491. (insert "}\n"))
  11492. ((equal e '(:newline))
  11493. (when (not (= cnt 0))
  11494. (setq cnt 0)
  11495. (insert "\n")
  11496. (setq e (car tbl))
  11497. (while (equal (car tbl) '(:newline))
  11498. (insert "\n")
  11499. (setq tbl (cdr tbl)))))
  11500. (t
  11501. (setq tg (car e) c (cdr e))
  11502. (if ingroup (push tg (car groups)))
  11503. (setq tg (org-add-props tg nil 'face
  11504. (org-get-todo-face tg)))
  11505. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11506. (insert "[" c "] " tg (make-string
  11507. (- fwidth 4 (length tg)) ?\ ))
  11508. (when (= (setq cnt (1+ cnt)) ncol)
  11509. (insert "\n")
  11510. (if ingroup (insert " "))
  11511. (setq cnt 0)))))
  11512. (insert "\n")
  11513. (goto-char (point-min))
  11514. (if (not expert) (org-fit-window-to-buffer))
  11515. (message "[a-z..]:Set [SPC]:clear")
  11516. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11517. (cond
  11518. ((or (= c ?\C-g)
  11519. (and (= c ?q) (not (rassoc c fulltable))))
  11520. (setq quit-flag t))
  11521. ((= c ?\ ) nil)
  11522. ((setq e (rassoc c fulltable) tg (car e))
  11523. tg)
  11524. (t (setq quit-flag t)))))))
  11525. (defun org-entry-is-todo-p ()
  11526. (member (org-get-todo-state) org-not-done-keywords))
  11527. (defun org-entry-is-done-p ()
  11528. (member (org-get-todo-state) org-done-keywords))
  11529. (defun org-get-todo-state ()
  11530. "Return the TODO keyword of the current subtree."
  11531. (save-excursion
  11532. (org-back-to-heading t)
  11533. (and (looking-at org-todo-line-regexp)
  11534. (match-end 2)
  11535. (match-string 2))))
  11536. (defun org-at-date-range-p (&optional inactive-ok)
  11537. "Is the cursor inside a date range?"
  11538. (interactive)
  11539. (save-excursion
  11540. (catch 'exit
  11541. (let ((pos (point)))
  11542. (skip-chars-backward "^[<\r\n")
  11543. (skip-chars-backward "<[")
  11544. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11545. (>= (match-end 0) pos)
  11546. (throw 'exit t))
  11547. (skip-chars-backward "^<[\r\n")
  11548. (skip-chars-backward "<[")
  11549. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11550. (>= (match-end 0) pos)
  11551. (throw 'exit t)))
  11552. nil)))
  11553. (defun org-get-repeat (&optional tagline)
  11554. "Check if there is a deadline/schedule with repeater in this entry."
  11555. (save-match-data
  11556. (save-excursion
  11557. (org-back-to-heading t)
  11558. (and (re-search-forward (if tagline
  11559. (concat tagline "\\s-*" org-repeat-re)
  11560. org-repeat-re)
  11561. (org-entry-end-position) t)
  11562. (match-string-no-properties 1)))))
  11563. (defvar org-last-changed-timestamp)
  11564. (defvar org-last-inserted-timestamp)
  11565. (defvar org-log-post-message)
  11566. (defvar org-log-note-purpose)
  11567. (defvar org-log-note-how)
  11568. (defvar org-log-note-extra)
  11569. (defun org-auto-repeat-maybe (done-word)
  11570. "Check if the current headline contains a repeated deadline/schedule.
  11571. If yes, set TODO state back to what it was and change the base date
  11572. of repeating deadline/scheduled time stamps to new date.
  11573. This function is run automatically after each state change to a DONE state."
  11574. ;; last-state is dynamically scoped into this function
  11575. (let* ((repeat (org-get-repeat))
  11576. (aa (assoc org-last-state org-todo-kwd-alist))
  11577. (interpret (nth 1 aa))
  11578. (head (nth 2 aa))
  11579. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11580. (msg "Entry repeats: ")
  11581. (org-log-done nil)
  11582. (org-todo-log-states nil)
  11583. re type n what ts time to-state)
  11584. (when repeat
  11585. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11586. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11587. org-todo-repeat-to-state))
  11588. (unless (and to-state (member to-state org-todo-keywords-1))
  11589. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11590. (org-todo to-state)
  11591. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11592. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11593. (org-time-stamp-format t t))))
  11594. (when org-log-repeat
  11595. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11596. (memq 'org-add-log-note post-command-hook))
  11597. ;; OK, we are already setup for some record
  11598. (if (eq org-log-repeat 'note)
  11599. ;; make sure we take a note, not only a time stamp
  11600. (setq org-log-note-how 'note))
  11601. ;; Set up for taking a record
  11602. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11603. org-last-state
  11604. 'findpos org-log-repeat)))
  11605. (org-back-to-heading t)
  11606. (org-add-planning-info nil nil 'closed)
  11607. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11608. org-deadline-time-regexp "\\)\\|\\("
  11609. org-ts-regexp "\\)"))
  11610. (while (re-search-forward
  11611. re (save-excursion (outline-next-heading) (point)) t)
  11612. (setq type (if (match-end 1) org-scheduled-string
  11613. (if (match-end 3) org-deadline-string "Plain:"))
  11614. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11615. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11616. (setq n (string-to-number (match-string 2 ts))
  11617. what (match-string 3 ts))
  11618. (if (equal what "w") (setq n (* n 7) what "d"))
  11619. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11620. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11621. ;; Preparation, see if we need to modify the start date for the change
  11622. (when (match-end 1)
  11623. (setq time (save-match-data (org-time-string-to-time ts)))
  11624. (cond
  11625. ((equal (match-string 1 ts) ".")
  11626. ;; Shift starting date to today
  11627. (org-timestamp-change
  11628. (- (org-today) (time-to-days time))
  11629. 'day))
  11630. ((equal (match-string 1 ts) "+")
  11631. (let ((nshiftmax 10) (nshift 0))
  11632. (while (or (= nshift 0)
  11633. (<= (time-to-days time)
  11634. (time-to-days (current-time))))
  11635. (when (= (incf nshift) nshiftmax)
  11636. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11637. (error "Abort")))
  11638. (org-timestamp-change n (cdr (assoc what whata)))
  11639. (org-at-timestamp-p t)
  11640. (setq ts (match-string 1))
  11641. (setq time (save-match-data (org-time-string-to-time ts)))))
  11642. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11643. ;; rematch, so that we have everything in place for the real shift
  11644. (org-at-timestamp-p t)
  11645. (setq ts (match-string 1))
  11646. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11647. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11648. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11649. (setq org-log-post-message msg)
  11650. (message "%s" msg))))
  11651. (defun org-show-todo-tree (arg)
  11652. "Make a compact tree which shows all headlines marked with TODO.
  11653. The tree will show the lines where the regexp matches, and all higher
  11654. headlines above the match.
  11655. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11656. With a numeric prefix N, construct a sparse tree for the Nth element
  11657. of `org-todo-keywords-1'."
  11658. (interactive "P")
  11659. (let ((case-fold-search nil)
  11660. (kwd-re
  11661. (cond ((null arg) org-not-done-regexp)
  11662. ((equal arg '(4))
  11663. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11664. (mapcar 'list org-todo-keywords-1))))
  11665. (concat "\\("
  11666. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11667. "\\)\\>")))
  11668. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11669. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11670. org-todo-keywords-1)))
  11671. (t (error "Invalid prefix argument: %s" arg)))))
  11672. (message "%d TODO entries found"
  11673. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11674. (defun org-deadline (arg &optional time)
  11675. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11676. With one universal prefix argument, remove any deadline from the item.
  11677. With two universal prefix arguments, prompt for a warning delay.
  11678. With argument TIME, set the deadline at the corresponding date. TIME
  11679. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11680. (interactive "P")
  11681. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11682. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11683. 'region-start-level 'region))
  11684. org-loop-over-headlines-in-active-region)
  11685. (org-map-entries
  11686. `(org-deadline ',arg ,time)
  11687. org-loop-over-headlines-in-active-region
  11688. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11689. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11690. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11691. (repeater (and old-date
  11692. (string-match
  11693. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11694. old-date)
  11695. (match-string 1 old-date))))
  11696. (cond
  11697. ((equal arg '(4))
  11698. (when (and old-date org-log-redeadline)
  11699. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11700. org-log-redeadline))
  11701. (org-remove-timestamp-with-keyword org-deadline-string)
  11702. (message "Item no longer has a deadline."))
  11703. ((equal arg '(16))
  11704. (save-excursion
  11705. (if (re-search-forward
  11706. org-deadline-time-regexp
  11707. (save-excursion (outline-next-heading) (point)) t)
  11708. (let* ((rpl0 (match-string 1))
  11709. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11710. (replace-match
  11711. (concat org-deadline-string
  11712. " <" rpl
  11713. (format " -%dd"
  11714. (abs
  11715. (- (time-to-days
  11716. (save-match-data
  11717. (org-read-date nil t nil "Warn starting from" old-date-time)))
  11718. (time-to-days old-date-time))))
  11719. ">") t t))
  11720. (user-error "No deadline information to update"))))
  11721. (t
  11722. (org-add-planning-info 'deadline time 'closed)
  11723. (when (and old-date org-log-redeadline
  11724. (not (equal old-date
  11725. (substring org-last-inserted-timestamp 1 -1))))
  11726. (org-add-log-setup 'redeadline nil old-date 'findpos
  11727. org-log-redeadline))
  11728. (when repeater
  11729. (save-excursion
  11730. (org-back-to-heading t)
  11731. (when (re-search-forward (concat org-deadline-string " "
  11732. org-last-inserted-timestamp)
  11733. (save-excursion
  11734. (outline-next-heading) (point)) t)
  11735. (goto-char (1- (match-end 0)))
  11736. (insert " " repeater)
  11737. (setq org-last-inserted-timestamp
  11738. (concat (substring org-last-inserted-timestamp 0 -1)
  11739. " " repeater
  11740. (substring org-last-inserted-timestamp -1))))))
  11741. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11742. (defun org-schedule (arg &optional time)
  11743. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11744. With one universal prefix argument, remove any scheduling date from the item.
  11745. With two universal prefix arguments, prompt for a delay cookie.
  11746. With argument TIME, scheduled at the corresponding date. TIME can
  11747. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11748. (interactive "P")
  11749. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11750. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11751. 'region-start-level 'region))
  11752. org-loop-over-headlines-in-active-region)
  11753. (org-map-entries
  11754. `(org-schedule ',arg ,time)
  11755. org-loop-over-headlines-in-active-region
  11756. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11757. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11758. (old-date-time (if old-date (org-time-string-to-time old-date)))
  11759. (repeater (and old-date
  11760. (string-match
  11761. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11762. old-date)
  11763. (match-string 1 old-date))))
  11764. (cond
  11765. ((equal arg '(4))
  11766. (progn
  11767. (when (and old-date org-log-reschedule)
  11768. (org-add-log-setup 'delschedule nil old-date 'findpos
  11769. org-log-reschedule))
  11770. (org-remove-timestamp-with-keyword org-scheduled-string)
  11771. (message "Item is no longer scheduled.")))
  11772. ((equal arg '(16))
  11773. (save-excursion
  11774. (if (re-search-forward
  11775. org-scheduled-time-regexp
  11776. (save-excursion (outline-next-heading) (point)) t)
  11777. (let* ((rpl0 (match-string 1))
  11778. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11779. (replace-match
  11780. (concat org-scheduled-string
  11781. " <" rpl
  11782. (format " -%dd"
  11783. (abs
  11784. (- (time-to-days
  11785. (save-match-data
  11786. (org-read-date nil t nil "Delay until" old-date-time)))
  11787. (time-to-days old-date-time))))
  11788. ">") t t))
  11789. (user-error "No scheduled information to update"))))
  11790. (t
  11791. (org-add-planning-info 'scheduled time 'closed)
  11792. (when (and old-date org-log-reschedule
  11793. (not (equal old-date
  11794. (substring org-last-inserted-timestamp 1 -1))))
  11795. (org-add-log-setup 'reschedule nil old-date 'findpos
  11796. org-log-reschedule))
  11797. (when repeater
  11798. (save-excursion
  11799. (org-back-to-heading t)
  11800. (when (re-search-forward (concat org-scheduled-string " "
  11801. org-last-inserted-timestamp)
  11802. (save-excursion
  11803. (outline-next-heading) (point)) t)
  11804. (goto-char (1- (match-end 0)))
  11805. (insert " " repeater)
  11806. (setq org-last-inserted-timestamp
  11807. (concat (substring org-last-inserted-timestamp 0 -1)
  11808. " " repeater
  11809. (substring org-last-inserted-timestamp -1))))))
  11810. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11811. (defun org-get-scheduled-time (pom &optional inherit)
  11812. "Get the scheduled time as a time tuple, of a format suitable
  11813. for calling org-schedule with, or if there is no scheduling,
  11814. returns nil."
  11815. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11816. (when time
  11817. (apply 'encode-time (org-parse-time-string time)))))
  11818. (defun org-get-deadline-time (pom &optional inherit)
  11819. "Get the deadline as a time tuple, of a format suitable for
  11820. calling org-deadline with, or if there is no scheduling, returns
  11821. nil."
  11822. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11823. (when time
  11824. (apply 'encode-time (org-parse-time-string time)))))
  11825. (defun org-remove-timestamp-with-keyword (keyword)
  11826. "Remove all time stamps with KEYWORD in the current entry."
  11827. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11828. beg)
  11829. (save-excursion
  11830. (org-back-to-heading t)
  11831. (setq beg (point))
  11832. (outline-next-heading)
  11833. (while (re-search-backward re beg t)
  11834. (replace-match "")
  11835. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11836. (equal (char-before) ?\ ))
  11837. (backward-delete-char 1)
  11838. (if (string-match "^[ \t]*$" (buffer-substring
  11839. (point-at-bol) (point-at-eol)))
  11840. (delete-region (point-at-bol)
  11841. (min (point-max) (1+ (point-at-eol))))))))))
  11842. (defun org-add-planning-info (what &optional time &rest remove)
  11843. "Insert new timestamp with keyword in the line directly after the headline.
  11844. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11845. If non is given, the user is prompted for a date.
  11846. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11847. be removed."
  11848. (interactive)
  11849. (let (org-time-was-given org-end-time-was-given ts
  11850. end default-time default-input)
  11851. (catch 'exit
  11852. (when (and (memq what '(scheduled deadline))
  11853. (or (not time)
  11854. (and (stringp time)
  11855. (string-match "^[-+]+[0-9]" time))))
  11856. ;; Try to get a default date/time from existing timestamp
  11857. (save-excursion
  11858. (org-back-to-heading t)
  11859. (setq end (save-excursion (outline-next-heading) (point)))
  11860. (when (re-search-forward (if (eq what 'scheduled)
  11861. org-scheduled-time-regexp
  11862. org-deadline-time-regexp)
  11863. end t)
  11864. (setq ts (match-string 1)
  11865. default-time
  11866. (apply 'encode-time (org-parse-time-string ts))
  11867. default-input (and ts (org-get-compact-tod ts))))))
  11868. (when what
  11869. (setq time
  11870. (if (stringp time)
  11871. ;; This is a string (relative or absolute), set proper date
  11872. (apply 'encode-time
  11873. (org-read-date-analyze
  11874. time default-time (decode-time default-time)))
  11875. ;; If necessary, get the time from the user
  11876. (or time (org-read-date nil 'to-time nil nil
  11877. default-time default-input)))))
  11878. (when (and org-insert-labeled-timestamps-at-point
  11879. (member what '(scheduled deadline)))
  11880. (insert
  11881. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11882. (org-insert-time-stamp time org-time-was-given
  11883. nil nil nil (list org-end-time-was-given))
  11884. (setq what nil))
  11885. (save-excursion
  11886. (save-restriction
  11887. (let (col list elt ts buffer-invisibility-spec)
  11888. (org-back-to-heading t)
  11889. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11890. (goto-char (match-end 1))
  11891. (setq col (current-column))
  11892. (goto-char (match-end 0))
  11893. (if (eobp) (insert "\n") (forward-char 1))
  11894. (when (and (not what)
  11895. (not (looking-at
  11896. (concat "[ \t]*"
  11897. org-keyword-time-not-clock-regexp))))
  11898. ;; Nothing to add, nothing to remove...... :-)
  11899. (throw 'exit nil))
  11900. (if (and (not (looking-at org-outline-regexp))
  11901. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11902. "[^\r\n]*"))
  11903. (not (equal (match-string 1) org-clock-string)))
  11904. (narrow-to-region (match-beginning 0) (match-end 0))
  11905. (insert-before-markers "\n")
  11906. (backward-char 1)
  11907. (narrow-to-region (point) (point))
  11908. (and org-adapt-indentation (org-indent-to-column col)))
  11909. ;; Check if we have to remove something.
  11910. (setq list (cons what remove))
  11911. (while list
  11912. (setq elt (pop list))
  11913. (when (or (and (eq elt 'scheduled)
  11914. (re-search-forward org-scheduled-time-regexp nil t))
  11915. (and (eq elt 'deadline)
  11916. (re-search-forward org-deadline-time-regexp nil t))
  11917. (and (eq elt 'closed)
  11918. (re-search-forward org-closed-time-regexp nil t)))
  11919. (replace-match "")
  11920. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11921. (and (looking-at "[ \t]+") (replace-match ""))
  11922. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11923. (when what
  11924. (insert
  11925. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11926. (cond ((eq what 'scheduled) org-scheduled-string)
  11927. ((eq what 'deadline) org-deadline-string)
  11928. ((eq what 'closed) org-closed-string))
  11929. " ")
  11930. (setq ts (org-insert-time-stamp
  11931. time
  11932. (or org-time-was-given
  11933. (and (eq what 'closed) org-log-done-with-time))
  11934. (eq what 'closed)
  11935. nil nil (list org-end-time-was-given)))
  11936. (insert
  11937. (if (not (or (bolp) (eq (char-before) ?\ )
  11938. (memq (char-after) '(32 10))
  11939. (eobp))) " " ""))
  11940. (end-of-line 1))
  11941. (goto-char (point-min))
  11942. (widen)
  11943. (if (and (looking-at "[ \t]*\n")
  11944. (equal (char-before) ?\n))
  11945. (delete-region (1- (point)) (point-at-eol)))
  11946. ts))))))
  11947. (defvar org-log-note-marker (make-marker))
  11948. (defvar org-log-note-purpose nil)
  11949. (defvar org-log-note-state nil)
  11950. (defvar org-log-note-previous-state nil)
  11951. (defvar org-log-note-how nil)
  11952. (defvar org-log-note-extra nil)
  11953. (defvar org-log-note-window-configuration nil)
  11954. (defvar org-log-note-return-to (make-marker))
  11955. (defvar org-log-note-effective-time nil
  11956. "Remembered current time so that dynamically scoped
  11957. `org-extend-today-until' affects tha timestamps in state change
  11958. log")
  11959. (defvar org-log-post-message nil
  11960. "Message to be displayed after a log note has been stored.
  11961. The auto-repeater uses this.")
  11962. (defun org-add-note ()
  11963. "Add a note to the current entry.
  11964. This is done in the same way as adding a state change note."
  11965. (interactive)
  11966. (org-add-log-setup 'note nil nil 'findpos nil))
  11967. (defvar org-property-end-re)
  11968. (defun org-add-log-setup (&optional purpose state prev-state
  11969. findpos how extra)
  11970. "Set up the post command hook to take a note.
  11971. If this is about to TODO state change, the new state is expected in STATE.
  11972. When FINDPOS is non-nil, find the correct position for the note in
  11973. the current entry. If not, assume that it can be inserted at point.
  11974. HOW is an indicator what kind of note should be created.
  11975. EXTRA is additional text that will be inserted into the notes buffer."
  11976. (let* ((org-log-into-drawer (org-log-into-drawer))
  11977. (drawer (cond ((stringp org-log-into-drawer)
  11978. org-log-into-drawer)
  11979. (org-log-into-drawer "LOGBOOK"))))
  11980. (save-restriction
  11981. (save-excursion
  11982. (when findpos
  11983. (org-back-to-heading t)
  11984. (narrow-to-region (point) (save-excursion
  11985. (outline-next-heading) (point)))
  11986. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11987. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11988. "[^\r\n]*\\)?"))
  11989. (goto-char (match-end 0))
  11990. (cond
  11991. (drawer
  11992. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11993. nil t)
  11994. (progn
  11995. (goto-char (match-end 0))
  11996. (or org-log-states-order-reversed
  11997. (and (re-search-forward org-property-end-re nil t)
  11998. (goto-char (1- (match-beginning 0))))))
  11999. (insert "\n:" drawer ":\n:END:")
  12000. (beginning-of-line 0)
  12001. (org-indent-line)
  12002. (beginning-of-line 2)
  12003. (org-indent-line)
  12004. (end-of-line 0)))
  12005. ((and org-log-state-notes-insert-after-drawers
  12006. (save-excursion
  12007. (forward-line) (looking-at org-drawer-regexp)))
  12008. (forward-line)
  12009. (while (looking-at org-drawer-regexp)
  12010. (goto-char (match-end 0))
  12011. (re-search-forward org-property-end-re (point-max) t)
  12012. (forward-line))
  12013. (forward-line -1)))
  12014. (unless org-log-states-order-reversed
  12015. (and (= (char-after) ?\n) (forward-char 1))
  12016. (org-skip-over-state-notes)
  12017. (skip-chars-backward " \t\n\r")))
  12018. (move-marker org-log-note-marker (point))
  12019. (setq org-log-note-purpose purpose
  12020. org-log-note-state state
  12021. org-log-note-previous-state prev-state
  12022. org-log-note-how how
  12023. org-log-note-extra extra
  12024. org-log-note-effective-time (org-current-effective-time))
  12025. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  12026. (defun org-skip-over-state-notes ()
  12027. "Skip past the list of State notes in an entry."
  12028. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  12029. (when (ignore-errors (goto-char (org-in-item-p)))
  12030. (let* ((struct (org-list-struct))
  12031. (prevs (org-list-prevs-alist struct)))
  12032. (while (looking-at "[ \t]*- State")
  12033. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12034. (org-list-get-item-end (point) struct)))))))
  12035. (defun org-add-log-note (&optional purpose)
  12036. "Pop up a window for taking a note, and add this note later at point."
  12037. (remove-hook 'post-command-hook 'org-add-log-note)
  12038. (setq org-log-note-window-configuration (current-window-configuration))
  12039. (delete-other-windows)
  12040. (move-marker org-log-note-return-to (point))
  12041. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12042. (goto-char org-log-note-marker)
  12043. (org-switch-to-buffer-other-window "*Org Note*")
  12044. (erase-buffer)
  12045. (if (memq org-log-note-how '(time state))
  12046. (let (current-prefix-arg) (org-store-log-note))
  12047. (let ((org-inhibit-startup t)) (org-mode))
  12048. (insert (format "# Insert note for %s.
  12049. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12050. (cond
  12051. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12052. ((eq org-log-note-purpose 'done) "closed todo item")
  12053. ((eq org-log-note-purpose 'state)
  12054. (format "state change from \"%s\" to \"%s\""
  12055. (or org-log-note-previous-state "")
  12056. (or org-log-note-state "")))
  12057. ((eq org-log-note-purpose 'reschedule)
  12058. "rescheduling")
  12059. ((eq org-log-note-purpose 'delschedule)
  12060. "no longer scheduled")
  12061. ((eq org-log-note-purpose 'redeadline)
  12062. "changing deadline")
  12063. ((eq org-log-note-purpose 'deldeadline)
  12064. "removing deadline")
  12065. ((eq org-log-note-purpose 'refile)
  12066. "refiling")
  12067. ((eq org-log-note-purpose 'note)
  12068. "this entry")
  12069. (t (error "This should not happen")))))
  12070. (if org-log-note-extra (insert org-log-note-extra))
  12071. (org-set-local 'org-finish-function 'org-store-log-note)
  12072. (run-hooks 'org-log-buffer-setup-hook)))
  12073. (defvar org-note-abort nil) ; dynamically scoped
  12074. (defun org-store-log-note ()
  12075. "Finish taking a log note, and insert it to where it belongs."
  12076. (let ((txt (buffer-string)))
  12077. (kill-buffer (current-buffer))
  12078. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12079. lines ind bul)
  12080. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12081. (setq txt (replace-match "" t t txt)))
  12082. (if (string-match "\\s-+\\'" txt)
  12083. (setq txt (replace-match "" t t txt)))
  12084. (setq lines (org-split-string txt "\n"))
  12085. (when (and note (string-match "\\S-" note))
  12086. (setq note
  12087. (org-replace-escapes
  12088. note
  12089. (list (cons "%u" (user-login-name))
  12090. (cons "%U" user-full-name)
  12091. (cons "%t" (format-time-string
  12092. (org-time-stamp-format 'long 'inactive)
  12093. org-log-note-effective-time))
  12094. (cons "%T" (format-time-string
  12095. (org-time-stamp-format 'long nil)
  12096. org-log-note-effective-time))
  12097. (cons "%d" (format-time-string
  12098. (org-time-stamp-format nil 'inactive)
  12099. org-log-note-effective-time))
  12100. (cons "%D" (format-time-string
  12101. (org-time-stamp-format nil nil)
  12102. org-log-note-effective-time))
  12103. (cons "%s" (if org-log-note-state
  12104. (concat "\"" org-log-note-state "\"")
  12105. ""))
  12106. (cons "%S" (if org-log-note-previous-state
  12107. (concat "\"" org-log-note-previous-state "\"")
  12108. "\"\"")))))
  12109. (if lines (setq note (concat note " \\\\")))
  12110. (push note lines))
  12111. (when (or current-prefix-arg org-note-abort)
  12112. (when org-log-into-drawer
  12113. (org-remove-empty-drawer-at
  12114. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  12115. org-log-note-marker))
  12116. (setq lines nil))
  12117. (when lines
  12118. (with-current-buffer (marker-buffer org-log-note-marker)
  12119. (save-excursion
  12120. (goto-char org-log-note-marker)
  12121. (move-marker org-log-note-marker nil)
  12122. (end-of-line 1)
  12123. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12124. (setq ind (save-excursion
  12125. (if (ignore-errors (goto-char (org-in-item-p)))
  12126. (let ((struct (org-list-struct)))
  12127. (org-list-get-ind
  12128. (org-list-get-top-point struct) struct))
  12129. (skip-chars-backward " \r\t\n")
  12130. (cond
  12131. ((and (org-at-heading-p)
  12132. org-adapt-indentation)
  12133. (1+ (org-current-level)))
  12134. ((org-at-heading-p) 0)
  12135. (t (org-get-indentation))))))
  12136. (setq bul (org-list-bullet-string "-"))
  12137. (org-indent-line-to ind)
  12138. (insert bul (pop lines))
  12139. (let ((ind-body (+ (length bul) ind)))
  12140. (while lines
  12141. (insert "\n")
  12142. (org-indent-line-to ind-body)
  12143. (insert (pop lines))))
  12144. (message "Note stored")
  12145. (org-back-to-heading t)
  12146. (org-cycle-hide-drawers 'children))
  12147. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12148. ;; from within `org-add-log-note' because `buffer-undo-list'
  12149. ;; is then modified outside of `org-with-remote-undo'.
  12150. (when (eq this-command 'org-agenda-todo)
  12151. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12152. ;; Don't add undo information when called from `org-agenda-todo'
  12153. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12154. (set-window-configuration org-log-note-window-configuration)
  12155. (with-current-buffer (marker-buffer org-log-note-return-to)
  12156. (goto-char org-log-note-return-to))
  12157. (move-marker org-log-note-return-to nil)
  12158. (and org-log-post-message (message "%s" org-log-post-message))))
  12159. (defun org-remove-empty-drawer-at (drawer pos)
  12160. "Remove an empty drawer DRAWER at position POS.
  12161. POS may also be a marker."
  12162. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12163. (save-excursion
  12164. (save-restriction
  12165. (widen)
  12166. (goto-char pos)
  12167. (if (org-in-regexp
  12168. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  12169. (replace-match ""))))))
  12170. (defvar org-ts-type nil)
  12171. (defun org-sparse-tree (&optional arg type)
  12172. "Create a sparse tree, prompt for the details.
  12173. This command can create sparse trees. You first need to select the type
  12174. of match used to create the tree:
  12175. t Show all TODO entries.
  12176. T Show entries with a specific TODO keyword.
  12177. m Show entries selected by a tags/property match.
  12178. p Enter a property name and its value (both with completion on existing
  12179. names/values) and show entries with that property.
  12180. r Show entries matching a regular expression (`/' can be used as well).
  12181. b Show deadlines and scheduled items before a date.
  12182. a Show deadlines and scheduled items after a date.
  12183. d Show deadlines due within `org-deadline-warning-days'.
  12184. D Show deadlines and scheduled items between a date range."
  12185. (interactive "P")
  12186. (let (ans kwd value ts-type)
  12187. (setq type (or type org-sparse-tree-default-date-type))
  12188. (setq org-ts-type type)
  12189. (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"
  12190. (cond ((eq type 'all) "all timestamps")
  12191. ((eq type 'scheduled) "only scheduled")
  12192. ((eq type 'deadline) "only deadline")
  12193. ((eq type 'active) "only active timestamps")
  12194. ((eq type 'inactive) "only inactive timestamps")
  12195. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  12196. ((eq type 'closed) "with a closed time-stamp")
  12197. (t "scheduled/deadline")))
  12198. (setq ans (read-char-exclusive))
  12199. (cond
  12200. ((equal ans ?c)
  12201. (org-sparse-tree
  12202. arg (cadr (member type '(scheduled-or-deadline
  12203. all scheduled deadline active inactive closed)))))
  12204. ((equal ans ?d)
  12205. (call-interactively 'org-check-deadlines))
  12206. ((equal ans ?b)
  12207. (call-interactively 'org-check-before-date))
  12208. ((equal ans ?a)
  12209. (call-interactively 'org-check-after-date))
  12210. ((equal ans ?D)
  12211. (call-interactively 'org-check-dates-range))
  12212. ((equal ans ?t)
  12213. (call-interactively 'org-show-todo-tree))
  12214. ((equal ans ?T)
  12215. (org-show-todo-tree '(4)))
  12216. ((member ans '(?T ?m))
  12217. (call-interactively 'org-match-sparse-tree))
  12218. ((member ans '(?p ?P))
  12219. (setq kwd (org-icompleting-read "Property: "
  12220. (mapcar 'list (org-buffer-property-keys))))
  12221. (setq value (org-icompleting-read "Value: "
  12222. (mapcar 'list (org-property-values kwd))))
  12223. (unless (string-match "\\`{.*}\\'" value)
  12224. (setq value (concat "\"" value "\"")))
  12225. (org-match-sparse-tree arg (concat kwd "=" value)))
  12226. ((member ans '(?r ?R ?/))
  12227. (call-interactively 'org-occur))
  12228. (t (error "No such sparse tree command \"%c\"" ans)))))
  12229. (defvar org-occur-highlights nil
  12230. "List of overlays used for occur matches.")
  12231. (make-variable-buffer-local 'org-occur-highlights)
  12232. (defvar org-occur-parameters nil
  12233. "Parameters of the active org-occur calls.
  12234. This is a list, each call to org-occur pushes as cons cell,
  12235. containing the regular expression and the callback, onto the list.
  12236. The list can contain several entries if `org-occur' has been called
  12237. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12238. will only contain one set of parameters. When the highlights are
  12239. removed (for example with `C-c C-c', or with the next edit (depending
  12240. on `org-remove-highlights-with-change'), this variable is emptied
  12241. as well.")
  12242. (make-variable-buffer-local 'org-occur-parameters)
  12243. (defun org-occur (regexp &optional keep-previous callback)
  12244. "Make a compact tree which shows all matches of REGEXP.
  12245. The tree will show the lines where the regexp matches, and all higher
  12246. headlines above the match. It will also show the heading after the match,
  12247. to make sure editing the matching entry is easy.
  12248. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12249. call to `org-occur' will be kept, to allow stacking of calls to this
  12250. command.
  12251. If CALLBACK is non-nil, it is a function which is called to confirm
  12252. that the match should indeed be shown."
  12253. (interactive "sRegexp: \nP")
  12254. (when (equal regexp "")
  12255. (error "Regexp cannot be empty"))
  12256. (unless keep-previous
  12257. (org-remove-occur-highlights nil nil t))
  12258. (push (cons regexp callback) org-occur-parameters)
  12259. (let ((cnt 0))
  12260. (save-excursion
  12261. (goto-char (point-min))
  12262. (if (or (not keep-previous) ; do not want to keep
  12263. (not org-occur-highlights)) ; no previous matches
  12264. ;; hide everything
  12265. (org-overview))
  12266. (while (re-search-forward regexp nil t)
  12267. (when (or (not callback)
  12268. (save-match-data (funcall callback)))
  12269. (setq cnt (1+ cnt))
  12270. (when org-highlight-sparse-tree-matches
  12271. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12272. (org-show-context 'occur-tree))))
  12273. (when org-remove-highlights-with-change
  12274. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12275. nil 'local))
  12276. (unless org-sparse-tree-open-archived-trees
  12277. (org-hide-archived-subtrees (point-min) (point-max)))
  12278. (run-hooks 'org-occur-hook)
  12279. (if (org-called-interactively-p 'interactive)
  12280. (message "%d match(es) for regexp %s" cnt regexp))
  12281. cnt))
  12282. (defun org-occur-next-match (&optional n reset)
  12283. "Function for `next-error-function' to find sparse tree matches.
  12284. N is the number of matches to move, when negative move backwards.
  12285. RESET is entirely ignored - this function always goes back to the
  12286. starting point when no match is found."
  12287. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12288. (search-func (if (< n 0)
  12289. 'previous-single-char-property-change
  12290. 'next-single-char-property-change))
  12291. (n (abs n))
  12292. (pos (point))
  12293. p1)
  12294. (catch 'exit
  12295. (while (setq p1 (funcall search-func (point) 'org-type))
  12296. (when (equal p1 limit)
  12297. (goto-char pos)
  12298. (error "No more matches"))
  12299. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12300. (setq n (1- n))
  12301. (when (= n 0)
  12302. (goto-char p1)
  12303. (throw 'exit (point))))
  12304. (goto-char p1))
  12305. (goto-char p1)
  12306. (error "No more matches"))))
  12307. (defun org-show-context (&optional key)
  12308. "Make sure point and context are visible.
  12309. How much context is shown depends upon the variables
  12310. `org-show-hierarchy-above', `org-show-following-heading',
  12311. `org-show-entry-below' and `org-show-siblings'."
  12312. (let ((heading-p (org-at-heading-p t))
  12313. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12314. (following-p (org-get-alist-option org-show-following-heading key))
  12315. (entry-p (org-get-alist-option org-show-entry-below key))
  12316. (siblings-p (org-get-alist-option org-show-siblings key)))
  12317. ;; Show heading or entry text
  12318. (if (and heading-p (not entry-p))
  12319. (org-flag-heading nil) ; only show the heading
  12320. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12321. (org-show-hidden-entry))) ; show entire entry
  12322. (when following-p
  12323. ;; Show next sibling, or heading below text
  12324. (save-excursion
  12325. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12326. (org-flag-heading nil))))
  12327. (when siblings-p (org-show-siblings))
  12328. (when hierarchy-p
  12329. ;; show all higher headings, possibly with siblings
  12330. (save-excursion
  12331. (while (and (condition-case nil
  12332. (progn (org-up-heading-all 1) t)
  12333. (error nil))
  12334. (not (bobp)))
  12335. (org-flag-heading nil)
  12336. (when siblings-p (org-show-siblings)))))
  12337. (unless (eq key 'agenda) (org-fix-ellipsis-at-bol))))
  12338. (defvar org-reveal-start-hook nil
  12339. "Hook run before revealing a location.")
  12340. (defun org-reveal (&optional siblings)
  12341. "Show current entry, hierarchy above it, and the following headline.
  12342. This can be used to show a consistent set of context around locations
  12343. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12344. not t for the search context.
  12345. With optional argument SIBLINGS, on each level of the hierarchy all
  12346. siblings are shown. This repairs the tree structure to what it would
  12347. look like when opened with hierarchical calls to `org-cycle'.
  12348. With double optional argument \\[universal-argument] \\[universal-argument], \
  12349. go to the parent and show the
  12350. entire tree."
  12351. (interactive "P")
  12352. (run-hooks 'org-reveal-start-hook)
  12353. (let ((org-show-hierarchy-above t)
  12354. (org-show-following-heading t)
  12355. (org-show-siblings (if siblings t org-show-siblings)))
  12356. (org-show-context nil))
  12357. (when (equal siblings '(16))
  12358. (save-excursion
  12359. (when (org-up-heading-safe)
  12360. (org-show-subtree)
  12361. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12362. (defun org-highlight-new-match (beg end)
  12363. "Highlight from BEG to END and mark the highlight is an occur headline."
  12364. (let ((ov (make-overlay beg end)))
  12365. (overlay-put ov 'face 'secondary-selection)
  12366. (overlay-put ov 'org-type 'org-occur)
  12367. (push ov org-occur-highlights)))
  12368. (defun org-remove-occur-highlights (&optional beg end noremove)
  12369. "Remove the occur highlights from the buffer.
  12370. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12371. from the `before-change-functions' in the current buffer."
  12372. (interactive)
  12373. (unless org-inhibit-highlight-removal
  12374. (mapc 'delete-overlay org-occur-highlights)
  12375. (setq org-occur-highlights nil)
  12376. (setq org-occur-parameters nil)
  12377. (unless noremove
  12378. (remove-hook 'before-change-functions
  12379. 'org-remove-occur-highlights 'local))))
  12380. ;;;; Priorities
  12381. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12382. "Regular expression matching the priority indicator.")
  12383. (defvar org-remove-priority-next-time nil)
  12384. (defun org-priority-up ()
  12385. "Increase the priority of the current item."
  12386. (interactive)
  12387. (org-priority 'up))
  12388. (defun org-priority-down ()
  12389. "Decrease the priority of the current item."
  12390. (interactive)
  12391. (org-priority 'down))
  12392. (defun org-priority (&optional action show)
  12393. "Change the priority of an item.
  12394. ACTION can be `set', `up', `down', or a character."
  12395. (interactive "P")
  12396. (if (equal action '(4))
  12397. (org-show-priority)
  12398. (unless org-enable-priority-commands
  12399. (error "Priority commands are disabled"))
  12400. (setq action (or action 'set))
  12401. (let (current new news have remove)
  12402. (save-excursion
  12403. (org-back-to-heading t)
  12404. (if (looking-at org-priority-regexp)
  12405. (setq current (string-to-char (match-string 2))
  12406. have t))
  12407. (cond
  12408. ((eq action 'remove)
  12409. (setq remove t new ?\ ))
  12410. ((or (eq action 'set)
  12411. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12412. (if (not (eq action 'set))
  12413. (setq new action)
  12414. (message "Priority %c-%c, SPC to remove: "
  12415. org-highest-priority org-lowest-priority)
  12416. (save-match-data
  12417. (setq new (read-char-exclusive))))
  12418. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12419. (= (upcase org-lowest-priority) org-lowest-priority))
  12420. (setq new (upcase new)))
  12421. (cond ((equal new ?\ ) (setq remove t))
  12422. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12423. (error "Priority must be between `%c' and `%c'"
  12424. org-highest-priority org-lowest-priority))))
  12425. ((eq action 'up)
  12426. (setq new (if have
  12427. (1- current) ; normal cycling
  12428. ;; last priority was empty
  12429. (if (eq last-command this-command)
  12430. org-lowest-priority ; wrap around empty to lowest
  12431. ;; default
  12432. (if org-priority-start-cycle-with-default
  12433. org-default-priority
  12434. (1- org-default-priority))))))
  12435. ((eq action 'down)
  12436. (setq new (if have
  12437. (1+ current) ; normal cycling
  12438. ;; last priority was empty
  12439. (if (eq last-command this-command)
  12440. org-highest-priority ; wrap around empty to highest
  12441. ;; default
  12442. (if org-priority-start-cycle-with-default
  12443. org-default-priority
  12444. (1+ org-default-priority))))))
  12445. (t (error "Invalid action")))
  12446. (if (or (< (upcase new) org-highest-priority)
  12447. (> (upcase new) org-lowest-priority))
  12448. (if (and (memq action '(up down))
  12449. (not have) (not (eq last-command this-command)))
  12450. ;; `new' is from default priority
  12451. (error
  12452. "The default can not be set, see `org-default-priority' why")
  12453. ;; normal cycling: `new' is beyond highest/lowest priority
  12454. ;; and is wrapped around to the empty priority
  12455. (setq remove t)))
  12456. (setq news (format "%c" new))
  12457. (if have
  12458. (if remove
  12459. (replace-match "" t t nil 1)
  12460. (replace-match news t t nil 2))
  12461. (if remove
  12462. (error "No priority cookie found in line")
  12463. (let ((case-fold-search nil))
  12464. (looking-at org-todo-line-regexp))
  12465. (if (match-end 2)
  12466. (progn
  12467. (goto-char (match-end 2))
  12468. (insert " [#" news "]"))
  12469. (goto-char (match-beginning 3))
  12470. (insert "[#" news "] "))))
  12471. (org-preserve-lc (org-set-tags nil 'align)))
  12472. (if remove
  12473. (message "Priority removed")
  12474. (message "Priority of current item set to %s" news)))))
  12475. (defun org-show-priority ()
  12476. "Show the priority of the current item.
  12477. This priority is composed of the main priority given with the [#A] cookies,
  12478. and by additional input from the age of a schedules or deadline entry."
  12479. (interactive)
  12480. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12481. (org-get-at-bol 'priority)
  12482. (save-excursion
  12483. (save-match-data
  12484. (beginning-of-line)
  12485. (and (looking-at org-heading-regexp)
  12486. (org-get-priority (match-string 0))))))))
  12487. (message "Priority is %d" (if pri pri -1000))))
  12488. (defun org-get-priority (s)
  12489. "Find priority cookie and return priority."
  12490. (save-match-data
  12491. (if (functionp org-get-priority-function)
  12492. (funcall org-get-priority-function)
  12493. (if (not (string-match org-priority-regexp s))
  12494. (* 1000 (- org-lowest-priority org-default-priority))
  12495. (* 1000 (- org-lowest-priority
  12496. (string-to-char (match-string 2 s))))))))
  12497. ;;;; Tags
  12498. (defvar org-agenda-archives-mode)
  12499. (defvar org-map-continue-from nil
  12500. "Position from where mapping should continue.
  12501. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12502. (defvar org-scanner-tags nil
  12503. "The current tag list while the tags scanner is running.")
  12504. (defvar org-trust-scanner-tags nil
  12505. "Should `org-get-tags-at' use the tags for the scanner.
  12506. This is for internal dynamical scoping only.
  12507. When this is non-nil, the function `org-get-tags-at' will return the value
  12508. of `org-scanner-tags' instead of building the list by itself. This
  12509. can lead to large speed-ups when the tags scanner is used in a file with
  12510. many entries, and when the list of tags is retrieved, for example to
  12511. obtain a list of properties. Building the tags list for each entry in such
  12512. a file becomes an N^2 operation - but with this variable set, it scales
  12513. as N.")
  12514. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12515. "Sca headline tags with inheritance and produce output ACTION.
  12516. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12517. or `agenda' to produce an entry list for an agenda view. It can also be
  12518. a Lisp form or a function that should be called at each matched headline, in
  12519. this case the return value is a list of all return values from these calls.
  12520. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12521. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12522. only lines with a not-done TODO keyword are included in the output.
  12523. This should be the same variable that was scoped into
  12524. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12525. START-LEVEL can be a string with asterisks, reducing the scope to
  12526. headlines matching this string."
  12527. (require 'org-agenda)
  12528. (let* ((re (concat "^"
  12529. (if start-level
  12530. ;; Get the correct level to match
  12531. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12532. org-outline-regexp)
  12533. " *\\(\\<\\("
  12534. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12535. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12536. (props (list 'face 'default
  12537. 'done-face 'org-agenda-done
  12538. 'undone-face 'default
  12539. 'mouse-face 'highlight
  12540. 'org-not-done-regexp org-not-done-regexp
  12541. 'org-todo-regexp org-todo-regexp
  12542. 'org-complex-heading-regexp org-complex-heading-regexp
  12543. 'help-echo
  12544. (format "mouse-2 or RET jump to org file %s"
  12545. (abbreviate-file-name
  12546. (or (buffer-file-name (buffer-base-buffer))
  12547. (buffer-name (buffer-base-buffer)))))))
  12548. (case-fold-search nil)
  12549. (org-map-continue-from nil)
  12550. lspos tags tags-list
  12551. (tags-alist (list (cons 0 org-file-tags)))
  12552. (llast 0) rtn rtn1 level category i txt
  12553. todo marker entry priority)
  12554. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12555. (setq action (list 'lambda nil action)))
  12556. (save-excursion
  12557. (goto-char (point-min))
  12558. (when (eq action 'sparse-tree)
  12559. (org-overview)
  12560. (org-remove-occur-highlights))
  12561. (while (re-search-forward re nil t)
  12562. (setq org-map-continue-from nil)
  12563. (catch :skip
  12564. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12565. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12566. (goto-char (setq lspos (match-beginning 0)))
  12567. (setq level (org-reduced-level (org-outline-level))
  12568. category (org-get-category))
  12569. (setq i llast llast level)
  12570. ;; remove tag lists from same and sublevels
  12571. (while (>= i level)
  12572. (when (setq entry (assoc i tags-alist))
  12573. (setq tags-alist (delete entry tags-alist)))
  12574. (setq i (1- i)))
  12575. ;; add the next tags
  12576. (when tags
  12577. (setq tags (org-split-string tags ":")
  12578. tags-alist
  12579. (cons (cons level tags) tags-alist)))
  12580. ;; compile tags for current headline
  12581. (setq tags-list
  12582. (if org-use-tag-inheritance
  12583. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12584. tags)
  12585. org-scanner-tags tags-list)
  12586. (when org-use-tag-inheritance
  12587. (setcdr (car tags-alist)
  12588. (mapcar (lambda (x)
  12589. (setq x (copy-sequence x))
  12590. (org-add-prop-inherited x))
  12591. (cdar tags-alist))))
  12592. (when (and tags org-use-tag-inheritance
  12593. (or (not (eq t org-use-tag-inheritance))
  12594. org-tags-exclude-from-inheritance))
  12595. ;; selective inheritance, remove uninherited ones
  12596. (setcdr (car tags-alist)
  12597. (org-remove-uninherited-tags (cdar tags-alist))))
  12598. (when (and
  12599. ;; eval matcher only when the todo condition is OK
  12600. (and (or (not todo-only) (member todo org-not-done-keywords))
  12601. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12602. (eval matcher)))
  12603. ;; Call the skipper, but return t if it does not skip,
  12604. ;; so that the `and' form continues evaluating
  12605. (progn
  12606. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12607. t)
  12608. ;; Check if timestamps are deselecting this entry
  12609. (or (not todo-only)
  12610. (and (member todo org-not-done-keywords)
  12611. (or (not org-agenda-tags-todo-honor-ignore-options)
  12612. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12613. ;; select this headline
  12614. (cond
  12615. ((eq action 'sparse-tree)
  12616. (and org-highlight-sparse-tree-matches
  12617. (org-get-heading) (match-end 0)
  12618. (org-highlight-new-match
  12619. (match-beginning 1) (match-end 1)))
  12620. (org-show-context 'tags-tree))
  12621. ((eq action 'agenda)
  12622. (setq txt (org-agenda-format-item
  12623. ""
  12624. (concat
  12625. (if (eq org-tags-match-list-sublevels 'indented)
  12626. (make-string (1- level) ?.) "")
  12627. (org-get-heading))
  12628. level category
  12629. tags-list)
  12630. priority (org-get-priority txt))
  12631. (goto-char lspos)
  12632. (setq marker (org-agenda-new-marker))
  12633. (org-add-props txt props
  12634. 'org-marker marker 'org-hd-marker marker 'org-category category
  12635. 'todo-state todo
  12636. 'priority priority 'type "tagsmatch")
  12637. (push txt rtn))
  12638. ((functionp action)
  12639. (setq org-map-continue-from nil)
  12640. (save-excursion
  12641. (setq rtn1 (funcall action))
  12642. (push rtn1 rtn)))
  12643. (t (error "Invalid action")))
  12644. ;; if we are to skip sublevels, jump to end of subtree
  12645. (unless org-tags-match-list-sublevels
  12646. (org-end-of-subtree t)
  12647. (backward-char 1))))
  12648. ;; Get the correct position from where to continue
  12649. (if org-map-continue-from
  12650. (goto-char org-map-continue-from)
  12651. (and (= (point) lspos) (end-of-line 1)))))
  12652. (when (and (eq action 'sparse-tree)
  12653. (not org-sparse-tree-open-archived-trees))
  12654. (org-hide-archived-subtrees (point-min) (point-max)))
  12655. (nreverse rtn)))
  12656. (defun org-remove-uninherited-tags (tags)
  12657. "Remove all tags that are not inherited from the list TAGS."
  12658. (cond
  12659. ((eq org-use-tag-inheritance t)
  12660. (if org-tags-exclude-from-inheritance
  12661. (org-delete-all org-tags-exclude-from-inheritance tags)
  12662. tags))
  12663. ((not org-use-tag-inheritance) nil)
  12664. ((stringp org-use-tag-inheritance)
  12665. (delq nil (mapcar
  12666. (lambda (x)
  12667. (if (and (string-match org-use-tag-inheritance x)
  12668. (not (member x org-tags-exclude-from-inheritance)))
  12669. x nil))
  12670. tags)))
  12671. ((listp org-use-tag-inheritance)
  12672. (delq nil (mapcar
  12673. (lambda (x)
  12674. (if (member x org-use-tag-inheritance) x nil))
  12675. tags)))))
  12676. (defun org-match-sparse-tree (&optional todo-only match)
  12677. "Create a sparse tree according to tags string MATCH.
  12678. MATCH can contain positive and negative selection of tags, like
  12679. \"+WORK+URGENT-WITHBOSS\".
  12680. If optional argument TODO-ONLY is non-nil, only select lines that are
  12681. also TODO lines."
  12682. (interactive "P")
  12683. (org-agenda-prepare-buffers (list (current-buffer)))
  12684. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12685. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12686. (defvar org-cached-props nil)
  12687. (defun org-cached-entry-get (pom property)
  12688. (if (or (eq t org-use-property-inheritance)
  12689. (and (stringp org-use-property-inheritance)
  12690. (string-match org-use-property-inheritance property))
  12691. (and (listp org-use-property-inheritance)
  12692. (member property org-use-property-inheritance)))
  12693. ;; Caching is not possible, check it directly
  12694. (org-entry-get pom property 'inherit)
  12695. ;; Get all properties, so that we can do complicated checks easily
  12696. (cdr (assoc property (or org-cached-props
  12697. (setq org-cached-props
  12698. (org-entry-properties pom)))))))
  12699. (defun org-global-tags-completion-table (&optional files)
  12700. "Return the list of all tags in all agenda buffer/files.
  12701. Optional FILES argument is a list of files which can be used
  12702. instead of the agenda files."
  12703. (save-excursion
  12704. (org-uniquify
  12705. (delq nil
  12706. (apply 'append
  12707. (mapcar
  12708. (lambda (file)
  12709. (set-buffer (find-file-noselect file))
  12710. (append (org-get-buffer-tags)
  12711. (mapcar (lambda (x) (if (stringp (car-safe x))
  12712. (list (car-safe x)) nil))
  12713. org-tag-alist)))
  12714. (if (and files (car files))
  12715. files
  12716. (org-agenda-files))))))))
  12717. (defun org-make-tags-matcher (match)
  12718. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12719. The variable `todo-only' is scoped dynamically into this function.
  12720. It will be set to t if the matcher restricts matching to TODO entries,
  12721. otherwise will not be touched.
  12722. Returns a cons of the selection string MATCH and the constructed
  12723. lisp form implementing the matcher. The matcher is to be evaluated
  12724. at an Org entry, with point on the headline, and returns t if the
  12725. entry matches the selection string MATCH. The returned lisp form
  12726. references two variables with information about the entry, which
  12727. must be bound around the form's evaluation: todo, the TODO keyword
  12728. at the entry (or nil of none); and tags-list, the list of all tags
  12729. at the entry including inherited ones. Additionally, the category
  12730. of the entry (if any) must be specified as the text property
  12731. 'org-category on the headline.
  12732. See also `org-scan-tags'.
  12733. "
  12734. (declare (special todo-only))
  12735. (unless (boundp 'todo-only)
  12736. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  12737. (unless match
  12738. ;; Get a new match request, with completion against the global
  12739. ;; tags table and the local tags in current buffer
  12740. (let ((org-last-tags-completion-table
  12741. (org-uniquify
  12742. (delq nil (append (org-get-buffer-tags)
  12743. (org-global-tags-completion-table))))))
  12744. (setq match (org-completing-read-no-i
  12745. "Match: " 'org-tags-completion-function nil nil nil
  12746. 'org-tags-history))))
  12747. ;; Parse the string and create a lisp form
  12748. (let ((match0 match)
  12749. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12750. minus tag mm
  12751. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12752. orterms term orlist re-p str-p level-p level-op time-p
  12753. prop-p pn pv po gv rest)
  12754. ;; Expand group tags
  12755. (setq match (org-tags-expand match))
  12756. (if (string-match "/+" match)
  12757. ;; match contains also a todo-matching request
  12758. (progn
  12759. (setq tagsmatch (substring match 0 (match-beginning 0))
  12760. todomatch (substring match (match-end 0)))
  12761. (if (string-match "^!" todomatch)
  12762. (setq todo-only t todomatch (substring todomatch 1)))
  12763. (if (string-match "^\\s-*$" todomatch)
  12764. (setq todomatch nil)))
  12765. ;; only matching tags
  12766. (setq tagsmatch match todomatch nil))
  12767. ;; Make the tags matcher
  12768. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12769. (setq tagsmatcher t)
  12770. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12771. (while (setq term (pop orterms))
  12772. (while (and (equal (substring term -1) "\\") orterms)
  12773. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12774. (while (string-match re term)
  12775. (setq rest (substring term (match-end 0))
  12776. minus (and (match-end 1)
  12777. (equal (match-string 1 term) "-"))
  12778. tag (save-match-data (replace-regexp-in-string
  12779. "\\\\-" "-"
  12780. (match-string 2 term)))
  12781. re-p (equal (string-to-char tag) ?{)
  12782. level-p (match-end 4)
  12783. prop-p (match-end 5)
  12784. mm (cond
  12785. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12786. (level-p
  12787. (setq level-op (org-op-to-function (match-string 3 term)))
  12788. `(,level-op level ,(string-to-number
  12789. (match-string 4 term))))
  12790. (prop-p
  12791. (setq pn (match-string 5 term)
  12792. po (match-string 6 term)
  12793. pv (match-string 7 term)
  12794. re-p (equal (string-to-char pv) ?{)
  12795. str-p (equal (string-to-char pv) ?\")
  12796. time-p (save-match-data
  12797. (string-match "^\"[[<].*[]>]\"$" pv))
  12798. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12799. (if time-p (setq pv (org-matcher-time pv)))
  12800. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12801. (cond
  12802. ((equal pn "CATEGORY")
  12803. (setq gv '(get-text-property (point) 'org-category)))
  12804. ((equal pn "TODO")
  12805. (setq gv 'todo))
  12806. (t
  12807. (setq gv `(org-cached-entry-get nil ,pn))))
  12808. (if re-p
  12809. (if (eq po 'org<>)
  12810. `(not (string-match ,pv (or ,gv "")))
  12811. `(string-match ,pv (or ,gv "")))
  12812. (if str-p
  12813. `(,po (or ,gv "") ,pv)
  12814. `(,po (string-to-number (or ,gv ""))
  12815. ,(string-to-number pv) ))))
  12816. (t `(member ,tag tags-list)))
  12817. mm (if minus (list 'not mm) mm)
  12818. term rest)
  12819. (push mm tagsmatcher))
  12820. (push (if (> (length tagsmatcher) 1)
  12821. (cons 'and tagsmatcher)
  12822. (car tagsmatcher))
  12823. orlist)
  12824. (setq tagsmatcher nil))
  12825. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12826. (setq tagsmatcher
  12827. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12828. ;; Make the todo matcher
  12829. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12830. (setq todomatcher t)
  12831. (setq orterms (org-split-string todomatch "|") orlist nil)
  12832. (while (setq term (pop orterms))
  12833. (while (string-match re term)
  12834. (setq minus (and (match-end 1)
  12835. (equal (match-string 1 term) "-"))
  12836. kwd (match-string 2 term)
  12837. re-p (equal (string-to-char kwd) ?{)
  12838. term (substring term (match-end 0))
  12839. mm (if re-p
  12840. `(string-match ,(substring kwd 1 -1) todo)
  12841. (list 'equal 'todo kwd))
  12842. mm (if minus (list 'not mm) mm))
  12843. (push mm todomatcher))
  12844. (push (if (> (length todomatcher) 1)
  12845. (cons 'and todomatcher)
  12846. (car todomatcher))
  12847. orlist)
  12848. (setq todomatcher nil))
  12849. (setq todomatcher (if (> (length orlist) 1)
  12850. (cons 'or orlist) (car orlist))))
  12851. ;; Return the string and lisp forms of the matcher
  12852. (setq matcher (if todomatcher
  12853. (list 'and tagsmatcher todomatcher)
  12854. tagsmatcher))
  12855. (when todo-only
  12856. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12857. matcher)))
  12858. (cons match0 matcher)))
  12859. (defun org-tags-expand (match &optional single-as-list downcased)
  12860. "Expand group tags in MATCH.
  12861. This replaces every group tag in MATCH with a regexp tag search.
  12862. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  12863. will be replaced like this:
  12864. Work => {\\(?:Work\\|Lab\\|Conf\\)}
  12865. +Work => +{\\(?:Work\\|Lab\\|Conf\\)}
  12866. -Work => -{\\(?:Work\\|Lab\\|Conf\\)}
  12867. Replacing by a regexp preserves the structure of the match.
  12868. E.g., this expansion
  12869. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  12870. will match anything tagged with \"Lab\" and \"Home\", or tagged
  12871. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  12872. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  12873. assumed to be a single group tag, and the function will return
  12874. the list of tags in this group.
  12875. When DOWNCASE is non-nil, expand downcased TAGS."
  12876. (if org-group-tags
  12877. (let* ((case-fold-search t)
  12878. (stable org-mode-syntax-table)
  12879. (tal (or org-tag-groups-alist-for-agenda
  12880. org-tag-groups-alist))
  12881. (tal (if downcased
  12882. (mapcar (lambda(tg) (mapcar 'downcase tg)) tal) tal))
  12883. (tml (mapcar 'car tal))
  12884. (rtnmatch match) rpl)
  12885. ;; @ and _ are allowed as word-components in tags
  12886. (modify-syntax-entry ?@ "w" stable)
  12887. (modify-syntax-entry ?_ "w" stable)
  12888. (while (and tml
  12889. (string-match
  12890. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  12891. (regexp-opt tml) "\\>\\)") rtnmatch))
  12892. (let* ((dir (match-string 1 rtnmatch))
  12893. (tag (match-string 2 rtnmatch))
  12894. (tag (if downcased (downcase tag) tag)))
  12895. (setq tml (delete tag tml))
  12896. (when (not (get-text-property 0 'grouptag (match-string 2 rtnmatch)))
  12897. (setq rpl (append (org-uniquify rpl) (assoc tag tal)))
  12898. (setq rpl (concat dir "{\\<" (regexp-opt rpl) "\\>}"))
  12899. (if (stringp rpl) (org-add-props rpl '(grouptag t)))
  12900. (setq rtnmatch (replace-match rpl t t rtnmatch)))))
  12901. (if single-as-list
  12902. (or (reverse rpl) (list rtnmatch))
  12903. rtnmatch))
  12904. (if single-as-list (list (if downcased (downcase match) match))
  12905. match)))
  12906. (defun org-op-to-function (op &optional stringp)
  12907. "Turn an operator into the appropriate function."
  12908. (setq op
  12909. (cond
  12910. ((equal op "<" ) '(< string< org-time<))
  12911. ((equal op ">" ) '(> org-string> org-time>))
  12912. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12913. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12914. ((member op '("=" "==")) '(= string= org-time=))
  12915. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12916. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12917. (defun org<> (a b) (not (= a b)))
  12918. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12919. (defun org-string>= (a b) (not (string< a b)))
  12920. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12921. (defun org-string<> (a b) (not (string= a b)))
  12922. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12923. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12924. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= 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) (org<> a b)))
  12928. (defun org-2ft (s)
  12929. "Convert S to a floating point time.
  12930. If S is already a number, just return it. If it is a string, parse
  12931. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12932. (cond
  12933. ((numberp s) s)
  12934. ((stringp s)
  12935. (condition-case nil
  12936. (float-time (apply 'encode-time (org-parse-time-string s)))
  12937. (error 0.)))
  12938. (t 0.)))
  12939. (defun org-time-today ()
  12940. "Time in seconds today at 0:00.
  12941. Returns the float number of seconds since the beginning of the
  12942. epoch to the beginning of today (00:00)."
  12943. (float-time (apply 'encode-time
  12944. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12945. (defun org-matcher-time (s)
  12946. "Interpret a time comparison value."
  12947. (save-match-data
  12948. (cond
  12949. ((string= s "<now>") (float-time))
  12950. ((string= s "<today>") (org-time-today))
  12951. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12952. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12953. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12954. (+ (org-time-today)
  12955. (* (string-to-number (match-string 1 s))
  12956. (cdr (assoc (match-string 2 s)
  12957. '(("d" . 86400.0) ("w" . 604800.0)
  12958. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12959. (t (org-2ft s)))))
  12960. (defun org-match-any-p (re list)
  12961. "Does re match any element of list?"
  12962. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12963. (delq nil list))
  12964. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12965. (defvar org-tags-overlay (make-overlay 1 1))
  12966. (org-detach-overlay org-tags-overlay)
  12967. (defun org-get-local-tags-at (&optional pos)
  12968. "Get a list of tags defined in the current headline."
  12969. (org-get-tags-at pos 'local))
  12970. (defun org-get-local-tags ()
  12971. "Get a list of tags defined in the current headline."
  12972. (org-get-tags-at nil 'local))
  12973. (defun org-get-tags-at (&optional pos local)
  12974. "Get a list of all headline tags applicable at POS.
  12975. POS defaults to point. If tags are inherited, the list contains
  12976. the targets in the same sequence as the headlines appear, i.e.
  12977. the tags of the current headline come last.
  12978. When LOCAL is non-nil, only return tags from the current headline,
  12979. ignore inherited ones."
  12980. (interactive)
  12981. (if (and org-trust-scanner-tags
  12982. (or (not pos) (equal pos (point)))
  12983. (not local))
  12984. org-scanner-tags
  12985. (let (tags ltags lastpos parent)
  12986. (save-excursion
  12987. (save-restriction
  12988. (widen)
  12989. (goto-char (or pos (point)))
  12990. (save-match-data
  12991. (catch 'done
  12992. (condition-case nil
  12993. (progn
  12994. (org-back-to-heading t)
  12995. (while (not (equal lastpos (point)))
  12996. (setq lastpos (point))
  12997. (when (looking-at
  12998. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12999. (setq ltags (org-split-string
  13000. (org-match-string-no-properties 1) ":"))
  13001. (when parent
  13002. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13003. (setq tags (append
  13004. (if parent
  13005. (org-remove-uninherited-tags ltags)
  13006. ltags)
  13007. tags)))
  13008. (or org-use-tag-inheritance (throw 'done t))
  13009. (if local (throw 'done t))
  13010. (or (org-up-heading-safe) (error nil))
  13011. (setq parent t)))
  13012. (error nil)))))
  13013. (if local
  13014. tags
  13015. (reverse (delete-dups
  13016. (reverse (append
  13017. (org-remove-uninherited-tags
  13018. org-file-tags) tags)))))))))
  13019. (defun org-add-prop-inherited (s)
  13020. (add-text-properties 0 (length s) '(inherited t) s)
  13021. s)
  13022. (defun org-toggle-tag (tag &optional onoff)
  13023. "Toggle the tag TAG for the current line.
  13024. If ONOFF is `on' or `off', don't toggle but set to this state."
  13025. (let (res current)
  13026. (save-excursion
  13027. (org-back-to-heading t)
  13028. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13029. (point-at-eol) t)
  13030. (progn
  13031. (setq current (match-string 1))
  13032. (replace-match ""))
  13033. (setq current ""))
  13034. (setq current (nreverse (org-split-string current ":")))
  13035. (cond
  13036. ((eq onoff 'on)
  13037. (setq res t)
  13038. (or (member tag current) (push tag current)))
  13039. ((eq onoff 'off)
  13040. (or (not (member tag current)) (setq current (delete tag current))))
  13041. (t (if (member tag current)
  13042. (setq current (delete tag current))
  13043. (setq res t)
  13044. (push tag current))))
  13045. (end-of-line 1)
  13046. (if current
  13047. (progn
  13048. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13049. (org-set-tags nil t))
  13050. (delete-horizontal-space))
  13051. (run-hooks 'org-after-tags-change-hook))
  13052. res))
  13053. (defun org-align-tags-here (to-col)
  13054. ;; Assumes that this is a headline
  13055. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13056. (beginning-of-line 1)
  13057. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13058. (< pos (match-beginning 2)))
  13059. (progn
  13060. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13061. (goto-char (match-beginning 1))
  13062. (insert " ")
  13063. (delete-region (point) (1+ (match-beginning 2)))
  13064. (setq ncol (max (current-column)
  13065. (1+ col)
  13066. (if (> to-col 0)
  13067. to-col
  13068. (- (abs to-col) tags-l))))
  13069. (setq p (point))
  13070. (insert (make-string (- ncol (current-column)) ?\ ))
  13071. (setq ncol (current-column))
  13072. (when indent-tabs-mode (tabify p (point-at-eol)))
  13073. (org-move-to-column (min ncol col) t))
  13074. (goto-char pos))))
  13075. (defun org-set-tags-command (&optional arg just-align)
  13076. "Call the set-tags command for the current entry."
  13077. (interactive "P")
  13078. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13079. (org-set-tags arg just-align)
  13080. (save-excursion
  13081. (unless (and (org-region-active-p)
  13082. org-loop-over-headlines-in-active-region)
  13083. (org-back-to-heading t))
  13084. (org-set-tags arg just-align))))
  13085. (defun org-set-tags-to (data)
  13086. "Set the tags of the current entry to DATA, replacing the current tags.
  13087. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13088. If DATA is nil or the empty string, any tags will be removed."
  13089. (interactive "sTags: ")
  13090. (setq data
  13091. (cond
  13092. ((eq data nil) "")
  13093. ((equal data "") "")
  13094. ((stringp data)
  13095. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13096. ":"))
  13097. ((listp data)
  13098. (concat ":" (mapconcat 'identity data ":") ":"))))
  13099. (when data
  13100. (save-excursion
  13101. (org-back-to-heading t)
  13102. (when (looking-at org-complex-heading-regexp)
  13103. (if (match-end 5)
  13104. (progn
  13105. (goto-char (match-beginning 5))
  13106. (insert data)
  13107. (delete-region (point) (point-at-eol))
  13108. (org-set-tags nil 'align))
  13109. (goto-char (point-at-eol))
  13110. (insert " " data)
  13111. (org-set-tags nil 'align)))
  13112. (beginning-of-line 1)
  13113. (if (looking-at ".*?\\([ \t]+\\)$")
  13114. (delete-region (match-beginning 1) (match-end 1))))))
  13115. (defun org-align-all-tags ()
  13116. "Align the tags i all headings."
  13117. (interactive)
  13118. (save-excursion
  13119. (or (ignore-errors (org-back-to-heading t))
  13120. (outline-next-heading))
  13121. (if (org-at-heading-p)
  13122. (org-set-tags t)
  13123. (message "No headings"))))
  13124. (defvar org-indent-indentation-per-level)
  13125. (defun org-set-tags (&optional arg just-align)
  13126. "Set the tags for the current headline.
  13127. With prefix ARG, realign all tags in headings in the current buffer."
  13128. (interactive "P")
  13129. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13130. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13131. 'region-start-level 'region))
  13132. org-loop-over-headlines-in-active-region)
  13133. (org-map-entries
  13134. ;; We don't use ARG and JUST-ALIGN here these args are not
  13135. ;; useful when looping over headlines
  13136. `(org-set-tags)
  13137. org-loop-over-headlines-in-active-region
  13138. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  13139. (let* ((re org-outline-regexp-bol)
  13140. (current (unless arg (org-get-tags-string)))
  13141. (col (current-column))
  13142. (org-setting-tags t)
  13143. table current-tags inherited-tags ; computed below when needed
  13144. tags p0 c0 c1 rpl di tc level)
  13145. (if arg
  13146. (save-excursion
  13147. (goto-char (point-min))
  13148. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13149. (while (re-search-forward re nil t)
  13150. (org-set-tags nil t)
  13151. (end-of-line 1)))
  13152. (message "All tags realigned to column %d" org-tags-column))
  13153. (if just-align
  13154. (setq tags current)
  13155. ;; Get a new set of tags from the user
  13156. (save-excursion
  13157. (setq table (append org-tag-persistent-alist
  13158. (or org-tag-alist (org-get-buffer-tags))
  13159. (and
  13160. org-complete-tags-always-offer-all-agenda-tags
  13161. (org-global-tags-completion-table
  13162. (org-agenda-files))))
  13163. org-last-tags-completion-table table
  13164. current-tags (org-split-string current ":")
  13165. inherited-tags (nreverse
  13166. (nthcdr (length current-tags)
  13167. (nreverse (org-get-tags-at))))
  13168. tags
  13169. (if (or (eq t org-use-fast-tag-selection)
  13170. (and org-use-fast-tag-selection
  13171. (delq nil (mapcar 'cdr table))))
  13172. (org-fast-tag-selection
  13173. current-tags inherited-tags table
  13174. (if org-fast-tag-selection-include-todo
  13175. org-todo-key-alist))
  13176. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  13177. (org-trim
  13178. (org-icompleting-read "Tags: "
  13179. 'org-tags-completion-function
  13180. nil nil current 'org-tags-history))))))
  13181. (while (string-match "[-+&]+" tags)
  13182. ;; No boolean logic, just a list
  13183. (setq tags (replace-match ":" t t tags))))
  13184. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  13185. (if org-tags-sort-function
  13186. (setq tags (mapconcat 'identity
  13187. (sort (org-split-string
  13188. tags (org-re "[^[:alnum:]_@#%]+"))
  13189. org-tags-sort-function) ":")))
  13190. (if (string-match "\\`[\t ]*\\'" tags)
  13191. (setq tags "")
  13192. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  13193. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  13194. ;; Insert new tags at the correct column
  13195. (beginning-of-line 1)
  13196. (setq level (or (and (looking-at org-outline-regexp)
  13197. (- (match-end 0) (point) 1))
  13198. 1))
  13199. (cond
  13200. ((and (equal current "") (equal tags "")))
  13201. ((re-search-forward
  13202. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13203. (point-at-eol) t)
  13204. (if (equal tags "")
  13205. (setq rpl "")
  13206. (goto-char (match-beginning 0))
  13207. (setq c0 (current-column)
  13208. ;; compute offset for the case of org-indent-mode active
  13209. di (if org-indent-mode
  13210. (* (1- org-indent-indentation-per-level) (1- level))
  13211. 0)
  13212. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  13213. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  13214. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  13215. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  13216. (replace-match rpl t t)
  13217. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  13218. tags)
  13219. (t (error "Tags alignment failed")))
  13220. (org-move-to-column col)
  13221. (unless just-align
  13222. (run-hooks 'org-after-tags-change-hook))))))
  13223. (defun org-change-tag-in-region (beg end tag off)
  13224. "Add or remove TAG for each entry in the region.
  13225. This works in the agenda, and also in an org-mode buffer."
  13226. (interactive
  13227. (list (region-beginning) (region-end)
  13228. (let ((org-last-tags-completion-table
  13229. (if (derived-mode-p 'org-mode)
  13230. (org-uniquify
  13231. (delq nil (append (org-get-buffer-tags)
  13232. (org-global-tags-completion-table))))
  13233. (org-global-tags-completion-table))))
  13234. (org-icompleting-read
  13235. "Tag: " 'org-tags-completion-function nil nil nil
  13236. 'org-tags-history))
  13237. (progn
  13238. (message "[s]et or [r]emove? ")
  13239. (equal (read-char-exclusive) ?r))))
  13240. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13241. (let ((agendap (equal major-mode 'org-agenda-mode))
  13242. l1 l2 m buf pos newhead (cnt 0))
  13243. (goto-char end)
  13244. (setq l2 (1- (org-current-line)))
  13245. (goto-char beg)
  13246. (setq l1 (org-current-line))
  13247. (loop for l from l1 to l2 do
  13248. (org-goto-line l)
  13249. (setq m (get-text-property (point) 'org-hd-marker))
  13250. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13251. (and agendap m))
  13252. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13253. pos (if agendap m (point)))
  13254. (with-current-buffer buf
  13255. (save-excursion
  13256. (save-restriction
  13257. (goto-char pos)
  13258. (setq cnt (1+ cnt))
  13259. (org-toggle-tag tag (if off 'off 'on))
  13260. (setq newhead (org-get-heading)))))
  13261. (and agendap (org-agenda-change-all-lines newhead m))))
  13262. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13263. (defun org-tags-completion-function (string predicate &optional flag)
  13264. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13265. (confirm (lambda (x) (stringp (car x)))))
  13266. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13267. (setq s1 (match-string 1 string)
  13268. s2 (match-string 2 string))
  13269. (setq s1 "" s2 string))
  13270. (cond
  13271. ((eq flag nil)
  13272. ;; try completion
  13273. (setq rtn (try-completion s2 ctable confirm))
  13274. (if (stringp rtn)
  13275. (setq rtn
  13276. (concat s1 s2 (substring rtn (length s2))
  13277. (if (and org-add-colon-after-tag-completion
  13278. (assoc rtn ctable))
  13279. ":" ""))))
  13280. rtn)
  13281. ((eq flag t)
  13282. ;; all-completions
  13283. (all-completions s2 ctable confirm))
  13284. ((eq flag 'lambda)
  13285. ;; exact match?
  13286. (assoc s2 ctable)))))
  13287. (defun org-fast-tag-insert (kwd tags face &optional end)
  13288. "Insert KDW, and the TAGS, the latter with face FACE.
  13289. Also insert END."
  13290. (insert (format "%-12s" (concat kwd ":"))
  13291. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13292. (or end "")))
  13293. (defun org-fast-tag-show-exit (flag)
  13294. (save-excursion
  13295. (org-goto-line 3)
  13296. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13297. (replace-match ""))
  13298. (when flag
  13299. (end-of-line 1)
  13300. (org-move-to-column (- (window-width) 19) t)
  13301. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13302. (defun org-set-current-tags-overlay (current prefix)
  13303. "Add an overlay to CURRENT tag with PREFIX."
  13304. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13305. (if (featurep 'xemacs)
  13306. (org-overlay-display org-tags-overlay (concat prefix s)
  13307. 'secondary-selection)
  13308. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13309. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13310. (defvar org-last-tag-selection-key nil)
  13311. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13312. "Fast tag selection with single keys.
  13313. CURRENT is the current list of tags in the headline, INHERITED is the
  13314. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13315. possibly with grouping information. TODO-TABLE is a similar table with
  13316. TODO keywords, should these have keys assigned to them.
  13317. If the keys are nil, a-z are automatically assigned.
  13318. Returns the new tags string, or nil to not change the current settings."
  13319. (let* ((fulltable (append table todo-table))
  13320. (maxlen (apply 'max (mapcar
  13321. (lambda (x)
  13322. (if (stringp (car x)) (string-width (car x)) 0))
  13323. fulltable)))
  13324. (buf (current-buffer))
  13325. (expert (eq org-fast-tag-selection-single-key 'expert))
  13326. (buffer-tags nil)
  13327. (fwidth (+ maxlen 3 1 3))
  13328. (ncol (/ (- (window-width) 4) fwidth))
  13329. (i-face 'org-done)
  13330. (c-face 'org-todo)
  13331. tg cnt e c char c1 c2 ntable tbl rtn
  13332. ov-start ov-end ov-prefix
  13333. (exit-after-next org-fast-tag-selection-single-key)
  13334. (done-keywords org-done-keywords)
  13335. groups ingroup)
  13336. (save-excursion
  13337. (beginning-of-line 1)
  13338. (if (looking-at
  13339. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13340. (setq ov-start (match-beginning 1)
  13341. ov-end (match-end 1)
  13342. ov-prefix "")
  13343. (setq ov-start (1- (point-at-eol))
  13344. ov-end (1+ ov-start))
  13345. (skip-chars-forward "^\n\r")
  13346. (setq ov-prefix
  13347. (concat
  13348. (buffer-substring (1- (point)) (point))
  13349. (if (> (current-column) org-tags-column)
  13350. " "
  13351. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13352. (move-overlay org-tags-overlay ov-start ov-end)
  13353. (save-window-excursion
  13354. (if expert
  13355. (set-buffer (get-buffer-create " *Org tags*"))
  13356. (delete-other-windows)
  13357. (split-window-vertically)
  13358. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13359. (erase-buffer)
  13360. (org-set-local 'org-done-keywords done-keywords)
  13361. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13362. (org-fast-tag-insert "Current" current c-face "\n\n")
  13363. (org-fast-tag-show-exit exit-after-next)
  13364. (org-set-current-tags-overlay current ov-prefix)
  13365. (setq tbl fulltable char ?a cnt 0)
  13366. (while (setq e (pop tbl))
  13367. (cond
  13368. ((equal (car e) :startgroup)
  13369. (push '() groups) (setq ingroup t)
  13370. (when (not (= cnt 0))
  13371. (setq cnt 0)
  13372. (insert "\n"))
  13373. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13374. ((equal (car e) :endgroup)
  13375. (setq ingroup nil cnt 0)
  13376. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13377. ((equal e '(:newline))
  13378. (when (not (= cnt 0))
  13379. (setq cnt 0)
  13380. (insert "\n")
  13381. (setq e (car tbl))
  13382. (while (equal (car tbl) '(:newline))
  13383. (insert "\n")
  13384. (setq tbl (cdr tbl)))))
  13385. ((equal e '(:grouptags)) nil)
  13386. (t
  13387. (setq tg (copy-sequence (car e)) c2 nil)
  13388. (if (cdr e)
  13389. (setq c (cdr e))
  13390. ;; automatically assign a character.
  13391. (setq c1 (string-to-char
  13392. (downcase (substring
  13393. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13394. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13395. (while (or (rassoc char ntable) (rassoc char table))
  13396. (setq char (1+ char)))
  13397. (setq c2 c1))
  13398. (setq c (or c2 char)))
  13399. (if ingroup (push tg (car groups)))
  13400. (setq tg (org-add-props tg nil 'face
  13401. (cond
  13402. ((not (assoc tg table))
  13403. (org-get-todo-face tg))
  13404. ((member tg current) c-face)
  13405. ((member tg inherited) i-face))))
  13406. (if (equal (caar tbl) :grouptags)
  13407. (org-add-props tg nil 'face 'org-tag-group))
  13408. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13409. (insert "[" c "] " tg (make-string
  13410. (- fwidth 4 (length tg)) ?\ ))
  13411. (push (cons tg c) ntable)
  13412. (when (= (setq cnt (1+ cnt)) ncol)
  13413. (insert "\n")
  13414. (if ingroup (insert " "))
  13415. (setq cnt 0)))))
  13416. (setq ntable (nreverse ntable))
  13417. (insert "\n")
  13418. (goto-char (point-min))
  13419. (if (not expert) (org-fit-window-to-buffer))
  13420. (setq rtn
  13421. (catch 'exit
  13422. (while t
  13423. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13424. (if (not groups) "no " "")
  13425. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13426. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13427. (setq org-last-tag-selection-key c)
  13428. (cond
  13429. ((= c ?\r) (throw 'exit t))
  13430. ((= c ?!)
  13431. (setq groups (not groups))
  13432. (goto-char (point-min))
  13433. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13434. ((= c ?\C-c)
  13435. (if (not expert)
  13436. (org-fast-tag-show-exit
  13437. (setq exit-after-next (not exit-after-next)))
  13438. (setq expert nil)
  13439. (delete-other-windows)
  13440. (set-window-buffer (split-window-vertically) " *Org tags*")
  13441. (org-switch-to-buffer-other-window " *Org tags*")
  13442. (org-fit-window-to-buffer)))
  13443. ((or (= c ?\C-g)
  13444. (and (= c ?q) (not (rassoc c ntable))))
  13445. (org-detach-overlay org-tags-overlay)
  13446. (setq quit-flag t))
  13447. ((= c ?\ )
  13448. (setq current nil)
  13449. (if exit-after-next (setq exit-after-next 'now)))
  13450. ((= c ?\t)
  13451. (condition-case nil
  13452. (setq tg (org-icompleting-read
  13453. "Tag: "
  13454. (or buffer-tags
  13455. (with-current-buffer buf
  13456. (org-get-buffer-tags)))))
  13457. (quit (setq tg "")))
  13458. (when (string-match "\\S-" tg)
  13459. (add-to-list 'buffer-tags (list tg))
  13460. (if (member tg current)
  13461. (setq current (delete tg current))
  13462. (push tg current)))
  13463. (if exit-after-next (setq exit-after-next 'now)))
  13464. ((setq e (rassoc c todo-table) tg (car e))
  13465. (with-current-buffer buf
  13466. (save-excursion (org-todo tg)))
  13467. (if exit-after-next (setq exit-after-next 'now)))
  13468. ((setq e (rassoc c ntable) tg (car e))
  13469. (if (member tg current)
  13470. (setq current (delete tg current))
  13471. (loop for g in groups do
  13472. (if (member tg g)
  13473. (mapc (lambda (x)
  13474. (setq current (delete x current)))
  13475. g)))
  13476. (push tg current))
  13477. (if exit-after-next (setq exit-after-next 'now))))
  13478. ;; Create a sorted list
  13479. (setq current
  13480. (sort current
  13481. (lambda (a b)
  13482. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13483. (if (eq exit-after-next 'now) (throw 'exit t))
  13484. (goto-char (point-min))
  13485. (beginning-of-line 2)
  13486. (delete-region (point) (point-at-eol))
  13487. (org-fast-tag-insert "Current" current c-face)
  13488. (org-set-current-tags-overlay current ov-prefix)
  13489. (while (re-search-forward
  13490. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13491. (setq tg (match-string 1))
  13492. (add-text-properties
  13493. (match-beginning 1) (match-end 1)
  13494. (list 'face
  13495. (cond
  13496. ((member tg current) c-face)
  13497. ((member tg inherited) i-face)
  13498. (t (get-text-property (match-beginning 1) 'face))))))
  13499. (goto-char (point-min)))))
  13500. (org-detach-overlay org-tags-overlay)
  13501. (if rtn
  13502. (mapconcat 'identity current ":")
  13503. nil))))
  13504. (defun org-get-tags-string ()
  13505. "Get the TAGS string in the current headline."
  13506. (unless (org-at-heading-p t)
  13507. (error "Not on a heading"))
  13508. (save-excursion
  13509. (beginning-of-line 1)
  13510. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13511. (org-match-string-no-properties 1)
  13512. "")))
  13513. (defun org-get-tags ()
  13514. "Get the list of tags specified in the current headline."
  13515. (org-split-string (org-get-tags-string) ":"))
  13516. (defun org-get-buffer-tags ()
  13517. "Get a table of all tags used in the buffer, for completion."
  13518. (let (tags)
  13519. (save-excursion
  13520. (goto-char (point-min))
  13521. (while (re-search-forward
  13522. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13523. (when (equal (char-after (point-at-bol 0)) ?*)
  13524. (mapc (lambda (x) (add-to-list 'tags x))
  13525. (org-split-string (org-match-string-no-properties 1) ":")))))
  13526. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13527. (mapcar 'list tags)))
  13528. ;;;; The mapping API
  13529. (defun org-map-entries (func &optional match scope &rest skip)
  13530. "Call FUNC at each headline selected by MATCH in SCOPE.
  13531. FUNC is a function or a lisp form. The function will be called without
  13532. arguments, with the cursor positioned at the beginning of the headline.
  13533. The return values of all calls to the function will be collected and
  13534. returned as a list.
  13535. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13536. does not need to preserve point. After evaluation, the cursor will be
  13537. moved to the end of the line (presumably of the headline of the
  13538. processed entry) and search continues from there. Under some
  13539. circumstances, this may not produce the wanted results. For example,
  13540. if you have removed (e.g. archived) the current (sub)tree it could
  13541. mean that the next entry will be skipped entirely. In such cases, you
  13542. can specify the position from where search should continue by making
  13543. FUNC set the variable `org-map-continue-from' to the desired buffer
  13544. position.
  13545. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13546. Only headlines that are matched by this query will be considered during
  13547. the iteration. When MATCH is nil or t, all headlines will be
  13548. visited by the iteration.
  13549. SCOPE determines the scope of this command. It can be any of:
  13550. nil The current buffer, respecting the restriction if any
  13551. tree The subtree started with the entry at point
  13552. region The entries within the active region, if any
  13553. region-start-level
  13554. The entries within the active region, but only those at
  13555. the same level than the first one.
  13556. file The current buffer, without restriction
  13557. file-with-archives
  13558. The current buffer, and any archives associated with it
  13559. agenda All agenda files
  13560. agenda-with-archives
  13561. All agenda files with any archive files associated with them
  13562. \(file1 file2 ...)
  13563. If this is a list, all files in the list will be scanned
  13564. The remaining args are treated as settings for the skipping facilities of
  13565. the scanner. The following items can be given here:
  13566. archive skip trees with the archive tag
  13567. comment skip trees with the COMMENT keyword
  13568. function or Emacs Lisp form:
  13569. will be used as value for `org-agenda-skip-function', so
  13570. whenever the function returns a position, FUNC will not be
  13571. called for that entry and search will continue from the
  13572. position returned
  13573. If your function needs to retrieve the tags including inherited tags
  13574. at the *current* entry, you can use the value of the variable
  13575. `org-scanner-tags' which will be much faster than getting the value
  13576. with `org-get-tags-at'. If your function gets properties with
  13577. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13578. to t around the call to `org-entry-properties' to get the same speedup.
  13579. Note that if your function moves around to retrieve tags and properties at
  13580. a *different* entry, you cannot use these techniques."
  13581. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13582. (not (org-region-active-p)))
  13583. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13584. (org-agenda-skip-archived-trees (memq 'archive skip))
  13585. (org-agenda-skip-comment-trees (memq 'comment skip))
  13586. (org-agenda-skip-function
  13587. (car (org-delete-all '(comment archive) skip)))
  13588. (org-tags-match-list-sublevels t)
  13589. (start-level (eq scope 'region-start-level))
  13590. matcher file res
  13591. org-todo-keywords-for-agenda
  13592. org-done-keywords-for-agenda
  13593. org-todo-keyword-alist-for-agenda
  13594. org-drawers-for-agenda
  13595. org-tag-alist-for-agenda
  13596. todo-only)
  13597. (cond
  13598. ((eq match t) (setq matcher t))
  13599. ((eq match nil) (setq matcher t))
  13600. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13601. (save-window-excursion
  13602. (save-restriction
  13603. (cond ((eq scope 'tree)
  13604. (org-back-to-heading t)
  13605. (org-narrow-to-subtree)
  13606. (setq scope nil))
  13607. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13608. (org-region-active-p))
  13609. ;; If needed, set start-level to a string like "2"
  13610. (when start-level
  13611. (save-excursion
  13612. (goto-char (region-beginning))
  13613. (unless (org-at-heading-p) (outline-next-heading))
  13614. (setq start-level (org-current-level))))
  13615. (narrow-to-region (region-beginning)
  13616. (save-excursion
  13617. (goto-char (region-end))
  13618. (unless (and (bolp) (org-at-heading-p))
  13619. (outline-next-heading))
  13620. (point)))
  13621. (setq scope nil)))
  13622. (if (not scope)
  13623. (progn
  13624. (org-agenda-prepare-buffers
  13625. (list (buffer-file-name (current-buffer))))
  13626. (setq res (org-scan-tags func matcher todo-only start-level)))
  13627. ;; Get the right scope
  13628. (cond
  13629. ((and scope (listp scope) (symbolp (car scope)))
  13630. (setq scope (eval scope)))
  13631. ((eq scope 'agenda)
  13632. (setq scope (org-agenda-files t)))
  13633. ((eq scope 'agenda-with-archives)
  13634. (setq scope (org-agenda-files t))
  13635. (setq scope (org-add-archive-files scope)))
  13636. ((eq scope 'file)
  13637. (setq scope (list (buffer-file-name))))
  13638. ((eq scope 'file-with-archives)
  13639. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13640. (org-agenda-prepare-buffers scope)
  13641. (while (setq file (pop scope))
  13642. (with-current-buffer (org-find-base-buffer-visiting file)
  13643. (save-excursion
  13644. (save-restriction
  13645. (widen)
  13646. (goto-char (point-min))
  13647. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13648. res)))
  13649. ;;;; Properties
  13650. ;;; Setting and retrieving properties
  13651. (defconst org-special-properties
  13652. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13653. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13654. "The special properties valid in Org-mode.
  13655. These are properties that are not defined in the property drawer,
  13656. but in some other way.")
  13657. (defconst org-default-properties
  13658. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13659. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13660. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13661. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13662. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13663. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13664. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13665. "Some properties that are used by Org-mode for various purposes.
  13666. Being in this list makes sure that they are offered for completion.")
  13667. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13668. "Regular expression matching the first line of a property drawer.")
  13669. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13670. "Regular expression matching the last line of a property drawer.")
  13671. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13672. "Regular expression matching the first line of a property drawer.")
  13673. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13674. "Regular expression matching the first line of a property drawer.")
  13675. (defconst org-property-drawer-re
  13676. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13677. org-property-end-re "\\)\n?")
  13678. "Matches an entire property drawer.")
  13679. (defconst org-clock-drawer-re
  13680. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13681. org-property-end-re "\\)\n?")
  13682. "Matches an entire clock drawer.")
  13683. (defsubst org-re-property (property)
  13684. "Return a regexp matching a PROPERTY line.
  13685. Match group 1 will be set to the value."
  13686. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13687. (defsubst org-re-property-keyword (property)
  13688. "Return a regexp matching a PROPERTY line, possibly with no
  13689. value for the property."
  13690. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13691. (defun org-property-action ()
  13692. "Do an action on properties."
  13693. (interactive)
  13694. (let (c)
  13695. (org-at-property-p)
  13696. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13697. (setq c (read-char-exclusive))
  13698. (cond
  13699. ((equal c ?s)
  13700. (call-interactively 'org-set-property))
  13701. ((equal c ?d)
  13702. (call-interactively 'org-delete-property))
  13703. ((equal c ?D)
  13704. (call-interactively 'org-delete-property-globally))
  13705. ((equal c ?c)
  13706. (call-interactively 'org-compute-property-at-point))
  13707. (t (error "No such property action %c" c)))))
  13708. (defun org-inc-effort ()
  13709. "Increment the value of the effort property in the current entry."
  13710. (interactive)
  13711. (org-set-effort nil t))
  13712. (defvar org-clock-effort) ;; Defined in org-clock.el
  13713. (defvar org-clock-current-task) ;; Defined in org-clock.el
  13714. (defun org-set-effort (&optional value increment)
  13715. "Set the effort property of the current entry.
  13716. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13717. 10th allowed value.
  13718. When INCREMENT is non-nil, set the property to the next allowed value."
  13719. (interactive "P")
  13720. (if (equal value 0) (setq value 10))
  13721. (let* ((completion-ignore-case t)
  13722. (prop org-effort-property)
  13723. (cur (org-entry-get nil prop))
  13724. (allowed (org-property-get-allowed-values nil prop 'table))
  13725. (existing (mapcar 'list (org-property-values prop)))
  13726. (heading (nth 4 (org-heading-components)))
  13727. rpl
  13728. (val (cond
  13729. ((stringp value) value)
  13730. ((and allowed (integerp value))
  13731. (or (car (nth (1- value) allowed))
  13732. (car (org-last allowed))))
  13733. ((and allowed increment)
  13734. (or (caadr (member (list cur) allowed))
  13735. (error "Allowed effort values are not set")))
  13736. (allowed
  13737. (message "Select 1-9,0, [RET%s]: %s"
  13738. (if cur (concat "=" cur) "")
  13739. (mapconcat 'car allowed " "))
  13740. (setq rpl (read-char-exclusive))
  13741. (if (equal rpl ?\r)
  13742. cur
  13743. (setq rpl (- rpl ?0))
  13744. (if (equal rpl 0) (setq rpl 10))
  13745. (if (and (> rpl 0) (<= rpl (length allowed)))
  13746. (car (nth (1- rpl) allowed))
  13747. (org-completing-read "Effort: " allowed nil))))
  13748. (t
  13749. (let (org-completion-use-ido org-completion-use-iswitchb)
  13750. (org-completing-read
  13751. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13752. (concat "[" cur "]") "")
  13753. ": ")
  13754. existing nil nil "" nil cur))))))
  13755. (unless (equal (org-entry-get nil prop) val)
  13756. (org-entry-put nil prop val))
  13757. (save-excursion
  13758. (org-back-to-heading t)
  13759. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13760. (when (string= heading org-clock-current-task)
  13761. (setq org-clock-effort (get-text-property (point-at-bol) 'org-effort))
  13762. (org-clock-update-mode-line))
  13763. (message "%s is now %s" prop val)))
  13764. (defun org-at-property-p ()
  13765. "Is cursor inside a property drawer?"
  13766. (save-excursion
  13767. (beginning-of-line 1)
  13768. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13769. (save-match-data ;; Used by calling procedures
  13770. (let ((p (point))
  13771. (range (unless (org-before-first-heading-p)
  13772. (org-get-property-block))))
  13773. (and range (<= (car range) p) (< p (cdr range))))))))
  13774. (defun org-get-property-block (&optional beg end force)
  13775. "Return the (beg . end) range of the body of the property drawer.
  13776. BEG and END are the beginning and end of the current subtree, or of
  13777. the part before the first headline. If they are not given, they will
  13778. be found. If the drawer does not exist and FORCE is non-nil, create
  13779. the drawer."
  13780. (catch 'exit
  13781. (save-excursion
  13782. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13783. (progn (org-back-to-heading t) (point))))
  13784. (end (or end (and (not (outline-next-heading)) (point-max))
  13785. (point))))
  13786. (goto-char beg)
  13787. (if (re-search-forward org-property-start-re end t)
  13788. (setq beg (1+ (match-end 0)))
  13789. (if force
  13790. (save-excursion
  13791. (org-insert-property-drawer)
  13792. (setq end (progn (outline-next-heading) (point))))
  13793. (throw 'exit nil))
  13794. (goto-char beg)
  13795. (if (re-search-forward org-property-start-re end t)
  13796. (setq beg (1+ (match-end 0)))))
  13797. (if (re-search-forward org-property-end-re end t)
  13798. (setq end (match-beginning 0))
  13799. (or force (throw 'exit nil))
  13800. (goto-char beg)
  13801. (setq end beg)
  13802. (org-indent-line)
  13803. (insert ":END:\n"))
  13804. (cons beg end)))))
  13805. (defun org-entry-properties (&optional pom which specific)
  13806. "Get all properties of the entry at point-or-marker POM.
  13807. This includes the TODO keyword, the tags, time strings for deadline,
  13808. scheduled, and clocking, and any additional properties defined in the
  13809. entry. The return value is an alist, keys may occur multiple times
  13810. if the property key was used several times.
  13811. POM may also be nil, in which case the current entry is used.
  13812. If WHICH is nil or `all', get all properties. If WHICH is
  13813. `special' or `standard', only get that subclass. If WHICH
  13814. is a string only get exactly this property. SPECIFIC can be a string, the
  13815. specific property we are interested in. Specifying it can speed
  13816. things up because then unnecessary parsing is avoided."
  13817. (setq which (or which 'all))
  13818. (org-with-point-at pom
  13819. (let ((clockstr (substring org-clock-string 0 -1))
  13820. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13821. (case-fold-search nil)
  13822. beg end range props sum-props key key1 value string clocksum clocksumt)
  13823. (save-excursion
  13824. (when (condition-case nil
  13825. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13826. (error nil))
  13827. (setq beg (point))
  13828. (setq sum-props (get-text-property (point) 'org-summaries))
  13829. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13830. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13831. (outline-next-heading)
  13832. (setq end (point))
  13833. (when (memq which '(all special))
  13834. ;; Get the special properties, like TODO and tags
  13835. (goto-char beg)
  13836. (when (and (or (not specific) (string= specific "TODO"))
  13837. (looking-at org-todo-line-regexp) (match-end 2))
  13838. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13839. (when (and (or (not specific) (string= specific "PRIORITY"))
  13840. (looking-at org-priority-regexp))
  13841. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13842. (when (or (not specific) (string= specific "FILE"))
  13843. (push (cons "FILE" buffer-file-name) props))
  13844. (when (and (or (not specific) (string= specific "TAGS"))
  13845. (setq value (org-get-tags-string))
  13846. (string-match "\\S-" value))
  13847. (push (cons "TAGS" value) props))
  13848. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13849. (setq value (org-get-tags-at)))
  13850. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13851. ":"))
  13852. props))
  13853. (when (or (not specific) (string= specific "BLOCKED"))
  13854. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13855. (when (or (not specific)
  13856. (member specific
  13857. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13858. "TIMESTAMP" "TIMESTAMP_IA")))
  13859. (catch 'match
  13860. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13861. (setq key (if (match-end 1)
  13862. (substring (org-match-string-no-properties 1)
  13863. 0 -1))
  13864. string (if (equal key clockstr)
  13865. (org-trim
  13866. (buffer-substring-no-properties
  13867. (match-beginning 3) (goto-char
  13868. (point-at-eol))))
  13869. (substring (org-match-string-no-properties 3)
  13870. 1 -1)))
  13871. ;; Get the correct property name from the key. This is
  13872. ;; necessary if the user has configured time keywords.
  13873. (setq key1 (concat key ":"))
  13874. (cond
  13875. ((not key)
  13876. (setq key
  13877. (if (= (char-after (match-beginning 3)) ?\[)
  13878. "TIMESTAMP_IA" "TIMESTAMP")))
  13879. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13880. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13881. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13882. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13883. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13884. (progn
  13885. (push (cons key string) props)
  13886. ;; no need to search further if match is found
  13887. (throw 'match t))
  13888. (when (or (equal key "CLOCK") (not (assoc key props)))
  13889. (push (cons key string) props)))))))
  13890. (when (memq which '(all standard))
  13891. ;; Get the standard properties, like :PROP: ...
  13892. (setq range (org-get-property-block beg end))
  13893. (when range
  13894. (goto-char (car range))
  13895. (while (re-search-forward
  13896. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13897. (cdr range) t)
  13898. (setq key (org-match-string-no-properties 1)
  13899. value (org-trim (or (org-match-string-no-properties 2) "")))
  13900. (unless (member key excluded)
  13901. (push (cons key (or value "")) props)))))
  13902. (if clocksum
  13903. (push (cons "CLOCKSUM"
  13904. (org-columns-number-to-string (/ (float clocksum) 60.)
  13905. 'add_times))
  13906. props))
  13907. (if clocksumt
  13908. (push (cons "CLOCKSUM_T"
  13909. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13910. 'add_times))
  13911. props))
  13912. (unless (assoc "CATEGORY" props)
  13913. (push (cons "CATEGORY" (org-get-category)) props))
  13914. (append sum-props (nreverse props)))))))
  13915. (defun org-entry-get (pom property &optional inherit literal-nil)
  13916. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13917. If INHERIT is non-nil and the entry does not have the property,
  13918. then also check higher levels of the hierarchy.
  13919. If INHERIT is the symbol `selective', use inheritance only if the setting
  13920. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13921. If the property is present but empty, the return value is the empty string.
  13922. If the property is not present at all, nil is returned.
  13923. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13924. do not interpret it as the list atom nil. This is used for inheritance
  13925. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13926. (org-with-point-at pom
  13927. (if (and inherit (if (eq inherit 'selective)
  13928. (org-property-inherit-p property)
  13929. t))
  13930. (org-entry-get-with-inheritance property literal-nil)
  13931. (if (member property org-special-properties)
  13932. ;; We need a special property. Use `org-entry-properties' to
  13933. ;; retrieve it, but specify the wanted property
  13934. (cdr (assoc property (org-entry-properties nil 'special property)))
  13935. (let ((range (org-get-property-block)))
  13936. (when (and range (not (eq (car range) (cdr range))))
  13937. (let* ((props (list (or (assoc property org-file-properties)
  13938. (assoc property org-global-properties)
  13939. (assoc property org-global-properties-fixed))))
  13940. (ap (lambda (key)
  13941. (when (re-search-forward
  13942. (org-re-property key) (cdr range) t)
  13943. (setq props
  13944. (org-update-property-plist
  13945. key
  13946. (if (match-end 1)
  13947. (org-match-string-no-properties 1) "")
  13948. props)))))
  13949. val)
  13950. (goto-char (car range))
  13951. (funcall ap property)
  13952. (goto-char (car range))
  13953. (while (funcall ap (concat property "+")))
  13954. (setq val (cdr (assoc property props)))
  13955. (when val (if literal-nil val (org-not-nil val))))))))))
  13956. (defun org-property-or-variable-value (var &optional inherit)
  13957. "Check if there is a property fixing the value of VAR.
  13958. If yes, return this value. If not, return the current value of the variable."
  13959. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13960. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13961. (read prop)
  13962. (symbol-value var))))
  13963. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13964. "Delete the property PROPERTY from entry at point-or-marker POM.
  13965. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13966. an empty drawer to delete."
  13967. (org-with-point-at pom
  13968. (if (member property org-special-properties)
  13969. nil ; cannot delete these properties.
  13970. (let ((range (org-get-property-block)))
  13971. (if (and range
  13972. (goto-char (car range))
  13973. (re-search-forward
  13974. (org-re-property property)
  13975. (cdr range) t))
  13976. (progn
  13977. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13978. (and delete-empty-drawer
  13979. (org-remove-empty-drawer-at
  13980. delete-empty-drawer (car range)))
  13981. t)
  13982. nil)))))
  13983. ;; Multi-values properties are properties that contain multiple values
  13984. ;; These values are assumed to be single words, separated by whitespace.
  13985. (defun org-entry-add-to-multivalued-property (pom property value)
  13986. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13987. (let* ((old (org-entry-get pom property))
  13988. (values (and old (org-split-string old "[ \t]"))))
  13989. (setq value (org-entry-protect-space value))
  13990. (unless (member value values)
  13991. (setq values (append values (list value)))
  13992. (org-entry-put pom property
  13993. (mapconcat 'identity values " ")))))
  13994. (defun org-entry-remove-from-multivalued-property (pom property value)
  13995. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13996. (let* ((old (org-entry-get pom property))
  13997. (values (and old (org-split-string old "[ \t]"))))
  13998. (setq value (org-entry-protect-space value))
  13999. (when (member value values)
  14000. (setq values (delete value values))
  14001. (org-entry-put pom property
  14002. (mapconcat 'identity values " ")))))
  14003. (defun org-entry-member-in-multivalued-property (pom property value)
  14004. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14005. (let* ((old (org-entry-get pom property))
  14006. (values (and old (org-split-string old "[ \t]"))))
  14007. (setq value (org-entry-protect-space value))
  14008. (member value values)))
  14009. (defun org-entry-get-multivalued-property (pom property)
  14010. "Return a list of values in a multivalued property."
  14011. (let* ((value (org-entry-get pom property))
  14012. (values (and value (org-split-string value "[ \t]"))))
  14013. (mapcar 'org-entry-restore-space values)))
  14014. (defun org-entry-put-multivalued-property (pom property &rest values)
  14015. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14016. VALUES should be a list of strings. Spaces will be protected."
  14017. (org-entry-put pom property
  14018. (mapconcat 'org-entry-protect-space values " "))
  14019. (let* ((value (org-entry-get pom property))
  14020. (values (and value (org-split-string value "[ \t]"))))
  14021. (mapcar 'org-entry-restore-space values)))
  14022. (defun org-entry-protect-space (s)
  14023. "Protect spaces and newline in string S."
  14024. (while (string-match " " s)
  14025. (setq s (replace-match "%20" t t s)))
  14026. (while (string-match "\n" s)
  14027. (setq s (replace-match "%0A" t t s)))
  14028. s)
  14029. (defun org-entry-restore-space (s)
  14030. "Restore spaces and newline in string S."
  14031. (while (string-match "%20" s)
  14032. (setq s (replace-match " " t t s)))
  14033. (while (string-match "%0A" s)
  14034. (setq s (replace-match "\n" t t s)))
  14035. s)
  14036. (defvar org-entry-property-inherited-from (make-marker)
  14037. "Marker pointing to the entry from where a property was inherited.
  14038. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14039. location of the entry where the inheritance search matched. If there was
  14040. no match, the marker will point nowhere.
  14041. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14042. is set.")
  14043. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14044. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14045. The search will stop at the first ancestor which has the property defined.
  14046. If the value found is \"nil\", return nil to show that the property
  14047. should be considered as undefined (this is the meaning of nil here).
  14048. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14049. (move-marker org-entry-property-inherited-from nil)
  14050. (let (tmp)
  14051. (save-excursion
  14052. (save-restriction
  14053. (widen)
  14054. (catch 'ex
  14055. (while t
  14056. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  14057. (or (ignore-errors (org-back-to-heading t))
  14058. (goto-char (point-min)))
  14059. (move-marker org-entry-property-inherited-from (point))
  14060. (throw 'ex tmp))
  14061. (or (ignore-errors (org-up-heading-safe))
  14062. (throw 'ex nil))))))
  14063. (setq tmp (or tmp
  14064. (cdr (assoc property org-file-properties))
  14065. (cdr (assoc property org-global-properties))
  14066. (cdr (assoc property org-global-properties-fixed))))
  14067. (if literal-nil tmp (org-not-nil tmp))))
  14068. (defvar org-property-changed-functions nil
  14069. "Hook called when the value of a property has changed.
  14070. Each hook function should accept two arguments, the name of the property
  14071. and the new value.")
  14072. (defun org-entry-put (pom property value)
  14073. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  14074. (org-with-point-at pom
  14075. (org-back-to-heading t)
  14076. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  14077. range)
  14078. (cond
  14079. ((equal property "TODO")
  14080. (when (and (stringp value) (string-match "\\S-" value)
  14081. (not (member value org-todo-keywords-1)))
  14082. (error "\"%s\" is not a valid TODO state" value))
  14083. (if (or (not value)
  14084. (not (string-match "\\S-" value)))
  14085. (setq value 'none))
  14086. (org-todo value)
  14087. (org-set-tags nil 'align))
  14088. ((equal property "PRIORITY")
  14089. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  14090. (string-to-char value) ?\ ))
  14091. (org-set-tags nil 'align))
  14092. ((equal property "CLOCKSUM")
  14093. (if (not (re-search-forward
  14094. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  14095. (error "Cannot find a clock log")
  14096. (goto-char (- (match-end 1) 2))
  14097. (cond
  14098. ((eq value 'earlier) (org-timestamp-down))
  14099. ((eq value 'later) (org-timestamp-up)))
  14100. (org-clock-sum-current-item)))
  14101. ((equal property "SCHEDULED")
  14102. (if (re-search-forward org-scheduled-time-regexp end t)
  14103. (cond
  14104. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14105. ((eq value 'later) (org-timestamp-change 1 'day))
  14106. (t (call-interactively 'org-schedule)))
  14107. (call-interactively 'org-schedule)))
  14108. ((equal property "DEADLINE")
  14109. (if (re-search-forward org-deadline-time-regexp end t)
  14110. (cond
  14111. ((eq value 'earlier) (org-timestamp-change -1 'day))
  14112. ((eq value 'later) (org-timestamp-change 1 'day))
  14113. (t (call-interactively 'org-deadline)))
  14114. (call-interactively 'org-deadline)))
  14115. ((member property org-special-properties)
  14116. (error "The %s property can not yet be set with `org-entry-put'"
  14117. property))
  14118. (t ; a non-special property
  14119. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  14120. (setq range (org-get-property-block beg end 'force))
  14121. (goto-char (car range))
  14122. (if (re-search-forward
  14123. (org-re-property-keyword property) (cdr range) t)
  14124. (progn
  14125. (delete-region (match-beginning 0) (match-end 0))
  14126. (goto-char (match-beginning 0)))
  14127. (goto-char (cdr range))
  14128. (insert "\n")
  14129. (backward-char 1)
  14130. (org-indent-line))
  14131. (insert ":" property ":")
  14132. (and value (insert " " value))
  14133. (org-indent-line)))))
  14134. (run-hook-with-args 'org-property-changed-functions property value)))
  14135. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  14136. "Get all property keys in the current buffer.
  14137. With INCLUDE-SPECIALS, also list the special properties that reflect things
  14138. like tags and TODO state.
  14139. With INCLUDE-DEFAULTS, also include properties that has special meaning
  14140. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  14141. and others.
  14142. With INCLUDE-COLUMNS, also include property names given in COLUMN
  14143. formats in the current buffer."
  14144. (let (rtn range cfmt s p)
  14145. (save-excursion
  14146. (save-restriction
  14147. (widen)
  14148. (goto-char (point-min))
  14149. (while (re-search-forward org-property-start-re nil t)
  14150. (setq range (org-get-property-block))
  14151. (goto-char (car range))
  14152. (while (re-search-forward
  14153. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  14154. (cdr range) t)
  14155. (add-to-list 'rtn (org-match-string-no-properties 1)))
  14156. (outline-next-heading))))
  14157. (when include-specials
  14158. (setq rtn (append org-special-properties rtn)))
  14159. (when include-defaults
  14160. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  14161. (add-to-list 'rtn org-effort-property))
  14162. (when include-columns
  14163. (save-excursion
  14164. (save-restriction
  14165. (widen)
  14166. (goto-char (point-min))
  14167. (while (re-search-forward
  14168. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  14169. nil t)
  14170. (setq cfmt (match-string 2) s 0)
  14171. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  14172. cfmt s)
  14173. (setq s (match-end 0)
  14174. p (match-string 1 cfmt))
  14175. (unless (or (equal p "ITEM")
  14176. (member p org-special-properties))
  14177. (add-to-list 'rtn (match-string 1 cfmt))))))))
  14178. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  14179. (defun org-property-values (key)
  14180. "Return a list of all values of property KEY in the current buffer."
  14181. (save-excursion
  14182. (save-restriction
  14183. (widen)
  14184. (goto-char (point-min))
  14185. (let ((re (org-re-property key))
  14186. values)
  14187. (while (re-search-forward re nil t)
  14188. (add-to-list 'values (org-trim (match-string 1))))
  14189. (delete "" values)))))
  14190. (defun org-insert-property-drawer ()
  14191. "Insert a property drawer into the current entry."
  14192. (org-back-to-heading t)
  14193. (looking-at org-outline-regexp)
  14194. (let ((indent (if org-adapt-indentation
  14195. (- (match-end 0) (match-beginning 0))
  14196. 0))
  14197. (beg (point))
  14198. (re (concat "^[ \t]*" org-keyword-time-regexp))
  14199. end hiddenp)
  14200. (outline-next-heading)
  14201. (setq end (point))
  14202. (goto-char beg)
  14203. (while (re-search-forward re end t))
  14204. (setq hiddenp (outline-invisible-p))
  14205. (end-of-line 1)
  14206. (and (equal (char-after) ?\n) (forward-char 1))
  14207. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  14208. (if (member (match-string 1) '("CLOCK:" ":END:"))
  14209. ;; just skip this line
  14210. (beginning-of-line 2)
  14211. ;; Drawer start, find the end
  14212. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  14213. (beginning-of-line 1)))
  14214. (org-skip-over-state-notes)
  14215. (skip-chars-backward " \t\n\r")
  14216. (if (eq (char-before) ?*) (forward-char 1))
  14217. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  14218. (beginning-of-line 0)
  14219. (org-indent-to-column indent)
  14220. (beginning-of-line 2)
  14221. (org-indent-to-column indent)
  14222. (beginning-of-line 0)
  14223. (if hiddenp
  14224. (save-excursion
  14225. (org-back-to-heading t)
  14226. (hide-entry))
  14227. (org-flag-drawer t))))
  14228. (defun org-insert-drawer (&optional arg drawer)
  14229. "Insert a drawer at point.
  14230. Optional argument DRAWER, when non-nil, is a string representing
  14231. drawer's name. Otherwise, the user is prompted for a name.
  14232. If a region is active, insert the drawer around that region
  14233. instead.
  14234. Point is left between drawer's boundaries."
  14235. (interactive "P")
  14236. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  14237. "LOGBOOK"))
  14238. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  14239. ;; internally by Org. They are meant to be inserted
  14240. ;; automatically.
  14241. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  14242. ;; Remove system drawers from list. Note: For some reason,
  14243. ;; `org-completing-read' ignores the predicate while
  14244. ;; `completing-read' handles it fine.
  14245. (drawer (if arg "PROPERTIES"
  14246. (or drawer
  14247. (completing-read
  14248. "Drawer: " org-drawers
  14249. (lambda (d) (not (member d system-drawers))))))))
  14250. (cond
  14251. ;; With C-u, fall back on `org-insert-property-drawer'
  14252. (arg (org-insert-property-drawer))
  14253. ;; With an active region, insert a drawer at point.
  14254. ((not (org-region-active-p))
  14255. (progn
  14256. (unless (bolp) (insert "\n"))
  14257. (insert (format ":%s:\n\n:END:\n" drawer))
  14258. (forward-line -2)))
  14259. ;; Otherwise, insert the drawer at point
  14260. (t
  14261. (let ((rbeg (region-beginning))
  14262. (rend (copy-marker (region-end))))
  14263. (unwind-protect
  14264. (progn
  14265. (goto-char rbeg)
  14266. (beginning-of-line)
  14267. (when (save-excursion
  14268. (re-search-forward org-outline-regexp-bol rend t))
  14269. (error "Drawers cannot contain headlines"))
  14270. ;; Position point at the beginning of the first
  14271. ;; non-blank line in region. Insert drawer's opening
  14272. ;; there, then indent it.
  14273. (org-skip-whitespace)
  14274. (beginning-of-line)
  14275. (insert ":" drawer ":\n")
  14276. (forward-line -1)
  14277. (indent-for-tab-command)
  14278. ;; Move point to the beginning of the first blank line
  14279. ;; after the last non-blank line in region. Insert
  14280. ;; drawer's closing, then indent it.
  14281. (goto-char rend)
  14282. (skip-chars-backward " \r\t\n")
  14283. (insert "\n:END:")
  14284. (deactivate-mark t)
  14285. (indent-for-tab-command)
  14286. (unless (eolp) (insert "\n")))
  14287. ;; Clear marker, whatever the outcome of insertion is.
  14288. (set-marker rend nil)))))))
  14289. (defvar org-property-set-functions-alist nil
  14290. "Property set function alist.
  14291. Each entry should have the following format:
  14292. (PROPERTY . READ-FUNCTION)
  14293. The read function will be called with the same argument as
  14294. `org-completing-read'.")
  14295. (defun org-set-property-function (property)
  14296. "Get the function that should be used to set PROPERTY.
  14297. This is computed according to `org-property-set-functions-alist'."
  14298. (or (cdr (assoc property org-property-set-functions-alist))
  14299. 'org-completing-read))
  14300. (defun org-read-property-value (property)
  14301. "Read PROPERTY value from user."
  14302. (let* ((completion-ignore-case t)
  14303. (allowed (org-property-get-allowed-values nil property 'table))
  14304. (cur (org-entry-get nil property))
  14305. (prompt (concat property " value"
  14306. (if (and cur (string-match "\\S-" cur))
  14307. (concat " [" cur "]") "") ": "))
  14308. (set-function (org-set-property-function property))
  14309. (val (if allowed
  14310. (funcall set-function prompt allowed nil
  14311. (not (get-text-property 0 'org-unrestricted
  14312. (caar allowed))))
  14313. (let (org-completion-use-ido org-completion-use-iswitchb)
  14314. (funcall set-function prompt
  14315. (mapcar 'list (org-property-values property))
  14316. nil nil "" nil cur)))))
  14317. (if (equal val "")
  14318. cur
  14319. val)))
  14320. (defvar org-last-set-property nil)
  14321. (defvar org-last-set-property-value nil)
  14322. (defun org-read-property-name ()
  14323. "Read a property name."
  14324. (let* ((completion-ignore-case t)
  14325. (keys (org-buffer-property-keys nil t t))
  14326. (default-prop (or (save-excursion
  14327. (save-match-data
  14328. (beginning-of-line)
  14329. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  14330. (null (string= (match-string 1) "END"))
  14331. (match-string 1))))
  14332. org-last-set-property))
  14333. (property (org-icompleting-read
  14334. (concat "Property"
  14335. (if default-prop (concat " [" default-prop "]") "")
  14336. ": ")
  14337. (mapcar 'list keys)
  14338. nil nil nil nil
  14339. default-prop)))
  14340. (if (member property keys)
  14341. property
  14342. (or (cdr (assoc (downcase property)
  14343. (mapcar (lambda (x) (cons (downcase x) x))
  14344. keys)))
  14345. property))))
  14346. (defun org-set-property-and-value (use-last)
  14347. "Allow to set [PROPERTY]: [value] direction from prompt.
  14348. When use-default, don't even ask, just use the last
  14349. \"[PROPERTY]: [value]\" string from the history."
  14350. (interactive "P")
  14351. (let* ((completion-ignore-case t)
  14352. (pv (or (and use-last org-last-set-property-value)
  14353. (org-completing-read
  14354. "Enter a \"[Property]: [value]\" pair: "
  14355. nil nil nil nil nil
  14356. org-last-set-property-value)))
  14357. prop val)
  14358. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14359. (setq prop (match-string 1 pv)
  14360. val (match-string 2 pv))
  14361. (org-set-property prop val))))
  14362. (defun org-set-property (property value)
  14363. "In the current entry, set PROPERTY to VALUE.
  14364. When called interactively, this will prompt for a property name, offering
  14365. completion on existing and default properties. And then it will prompt
  14366. for a value, offering completion either on allowed values (via an inherited
  14367. xxx_ALL property) or on existing values in other instances of this property
  14368. in the current file."
  14369. (interactive (list nil nil))
  14370. (let* ((property (or property (org-read-property-name)))
  14371. (value (or value (org-read-property-value property)))
  14372. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14373. (setq org-last-set-property property)
  14374. (setq org-last-set-property-value (concat property ": " value))
  14375. ;; Possibly postprocess the inserted value:
  14376. (when fn (setq value (funcall fn value)))
  14377. (unless (equal (org-entry-get nil property) value)
  14378. (org-entry-put nil property value))))
  14379. (defun org-delete-property (property &optional delete-empty-drawer)
  14380. "In the current entry, delete PROPERTY.
  14381. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14382. an empty drawer to delete."
  14383. (interactive
  14384. (let* ((completion-ignore-case t)
  14385. (prop (org-icompleting-read "Property: "
  14386. (org-entry-properties nil 'standard))))
  14387. (list prop)))
  14388. (message "Property %s %s" property
  14389. (if (org-entry-delete nil property delete-empty-drawer)
  14390. "deleted"
  14391. "was not present in the entry")))
  14392. (defun org-delete-property-globally (property)
  14393. "Remove PROPERTY globally, from all entries."
  14394. (interactive
  14395. (let* ((completion-ignore-case t)
  14396. (prop (org-icompleting-read
  14397. "Globally remove property: "
  14398. (mapcar 'list (org-buffer-property-keys)))))
  14399. (list prop)))
  14400. (save-excursion
  14401. (save-restriction
  14402. (widen)
  14403. (goto-char (point-min))
  14404. (let ((cnt 0))
  14405. (while (re-search-forward
  14406. (org-re-property property)
  14407. nil t)
  14408. (setq cnt (1+ cnt))
  14409. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14410. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14411. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14412. (defun org-compute-property-at-point ()
  14413. "Compute the property at point.
  14414. This looks for an enclosing column format, extracts the operator and
  14415. then applies it to the property in the column format's scope."
  14416. (interactive)
  14417. (unless (org-at-property-p)
  14418. (error "Not at a property"))
  14419. (let ((prop (org-match-string-no-properties 2)))
  14420. (org-columns-get-format-and-top-level)
  14421. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14422. (error "No operator defined for property %s" prop))
  14423. (org-columns-compute prop)))
  14424. (defvar org-property-allowed-value-functions nil
  14425. "Hook for functions supplying allowed values for a specific property.
  14426. The functions must take a single argument, the name of the property, and
  14427. return a flat list of allowed values. If \":ETC\" is one of
  14428. the values, this means that these values are intended as defaults for
  14429. completion, but that other values should be allowed too.
  14430. The functions must return nil if they are not responsible for this
  14431. property.")
  14432. (defun org-property-get-allowed-values (pom property &optional table)
  14433. "Get allowed values for the property PROPERTY.
  14434. When TABLE is non-nil, return an alist that can directly be used for
  14435. completion."
  14436. (let (vals)
  14437. (cond
  14438. ((equal property "TODO")
  14439. (setq vals (org-with-point-at pom
  14440. (append org-todo-keywords-1 '("")))))
  14441. ((equal property "PRIORITY")
  14442. (let ((n org-lowest-priority))
  14443. (while (>= n org-highest-priority)
  14444. (push (char-to-string n) vals)
  14445. (setq n (1- n)))))
  14446. ((member property org-special-properties))
  14447. ((setq vals (run-hook-with-args-until-success
  14448. 'org-property-allowed-value-functions property)))
  14449. (t
  14450. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14451. (when (and vals (string-match "\\S-" vals))
  14452. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14453. (setq vals (mapcar (lambda (x)
  14454. (cond ((stringp x) x)
  14455. ((numberp x) (number-to-string x))
  14456. ((symbolp x) (symbol-name x))
  14457. (t "???")))
  14458. vals)))))
  14459. (when (member ":ETC" vals)
  14460. (setq vals (remove ":ETC" vals))
  14461. (org-add-props (car vals) '(org-unrestricted t)))
  14462. (if table (mapcar 'list vals) vals)))
  14463. (defun org-property-previous-allowed-value (&optional previous)
  14464. "Switch to the next allowed value for this property."
  14465. (interactive)
  14466. (org-property-next-allowed-value t))
  14467. (defun org-property-next-allowed-value (&optional previous)
  14468. "Switch to the next allowed value for this property."
  14469. (interactive)
  14470. (unless (org-at-property-p)
  14471. (error "Not at a property"))
  14472. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14473. (key (match-string 2))
  14474. (value (match-string 3))
  14475. (allowed (or (org-property-get-allowed-values (point) key)
  14476. (and (member value '("[ ]" "[-]" "[X]"))
  14477. '("[ ]" "[X]"))))
  14478. (heading (save-match-data (nth 4 (org-heading-components))))
  14479. nval)
  14480. (unless allowed
  14481. (error "Allowed values for this property have not been defined"))
  14482. (if previous (setq allowed (reverse allowed)))
  14483. (if (member value allowed)
  14484. (setq nval (car (cdr (member value allowed)))))
  14485. (setq nval (or nval (car allowed)))
  14486. (if (equal nval value)
  14487. (error "Only one allowed value for this property"))
  14488. (org-at-property-p)
  14489. (replace-match (concat " :" key ": " nval) t t)
  14490. (org-indent-line)
  14491. (beginning-of-line 1)
  14492. (skip-chars-forward " \t")
  14493. (when (equal prop org-effort-property)
  14494. (save-excursion
  14495. (org-back-to-heading t)
  14496. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval))
  14497. (when (string= org-clock-current-task heading)
  14498. (setq org-clock-effort nval)
  14499. (org-clock-update-mode-line)))
  14500. (run-hook-with-args 'org-property-changed-functions key nval)))
  14501. (defun org-find-olp (path &optional this-buffer)
  14502. "Return a marker pointing to the entry at outline path OLP.
  14503. If anything goes wrong, throw an error.
  14504. You can wrap this call to catch the error like this:
  14505. (condition-case msg
  14506. (org-mobile-locate-entry (match-string 4))
  14507. (error (nth 1 msg)))
  14508. The return value will then be either a string with the error message,
  14509. or a marker if everything is OK.
  14510. If THIS-BUFFER is set, the outline path does not contain a file,
  14511. only headings."
  14512. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14513. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14514. (level 1)
  14515. (lmin 1)
  14516. (lmax 1)
  14517. limit re end found pos heading cnt flevel)
  14518. (unless buffer (error "File not found :%s" file))
  14519. (with-current-buffer buffer
  14520. (save-excursion
  14521. (save-restriction
  14522. (widen)
  14523. (setq limit (point-max))
  14524. (goto-char (point-min))
  14525. (while (setq heading (pop path))
  14526. (setq re (format org-complex-heading-regexp-format
  14527. (regexp-quote heading)))
  14528. (setq cnt 0 pos (point))
  14529. (while (re-search-forward re end t)
  14530. (setq level (- (match-end 1) (match-beginning 1)))
  14531. (if (and (>= level lmin) (<= level lmax))
  14532. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14533. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14534. lmax heading))
  14535. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14536. lmax heading))
  14537. (goto-char found)
  14538. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14539. (setq end (save-excursion (org-end-of-subtree t t))))
  14540. (when (org-at-heading-p)
  14541. (point-marker)))))))
  14542. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14543. "Find node HEADING in BUFFER.
  14544. Return a marker to the heading if it was found, or nil if not.
  14545. If POS-ONLY is set, return just the position instead of a marker.
  14546. The heading text must match exact, but it may have a TODO keyword,
  14547. a priority cookie and tags in the standard locations."
  14548. (with-current-buffer (or buffer (current-buffer))
  14549. (save-excursion
  14550. (save-restriction
  14551. (widen)
  14552. (goto-char (point-min))
  14553. (let (case-fold-search)
  14554. (if (re-search-forward
  14555. (format org-complex-heading-regexp-format
  14556. (regexp-quote heading)) nil t)
  14557. (if pos-only
  14558. (match-beginning 0)
  14559. (move-marker (make-marker) (match-beginning 0)))))))))
  14560. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14561. "Find Org node headline HEADING in all .org files in directory DIR.
  14562. When the target headline is found, return a marker to this location."
  14563. (let ((files (directory-files (or dir default-directory)
  14564. nil "\\`[^.#].*\\.org\\'"))
  14565. file visiting m buffer)
  14566. (catch 'found
  14567. (while (setq file (pop files))
  14568. (message "trying %s" file)
  14569. (setq visiting (org-find-base-buffer-visiting file))
  14570. (setq buffer (or visiting (find-file-noselect file)))
  14571. (setq m (org-find-exact-headline-in-buffer
  14572. heading buffer))
  14573. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14574. (and m (throw 'found m))))))
  14575. (defun org-find-entry-with-id (ident)
  14576. "Locate the entry that contains the ID property with exact value IDENT.
  14577. IDENT can be a string, a symbol or a number, this function will search for
  14578. the string representation of it.
  14579. Return the position where this entry starts, or nil if there is no such entry."
  14580. (interactive "sID: ")
  14581. (let ((id (cond
  14582. ((stringp ident) ident)
  14583. ((symbol-name ident) (symbol-name ident))
  14584. ((numberp ident) (number-to-string ident))
  14585. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14586. (case-fold-search nil))
  14587. (save-excursion
  14588. (save-restriction
  14589. (widen)
  14590. (goto-char (point-min))
  14591. (when (re-search-forward
  14592. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14593. nil t)
  14594. (org-back-to-heading t)
  14595. (point))))))
  14596. ;;;; Timestamps
  14597. (defvar org-last-changed-timestamp nil)
  14598. (defvar org-last-inserted-timestamp nil
  14599. "The last time stamp inserted with `org-insert-time-stamp'.")
  14600. (defvar org-time-was-given) ; dynamically scoped parameter
  14601. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14602. (defvar org-ts-what) ; dynamically scoped parameter
  14603. (defun org-time-stamp (arg &optional inactive)
  14604. "Prompt for a date/time and insert a time stamp.
  14605. If the user specifies a time like HH:MM or if this command is
  14606. called with at least one prefix argument, the time stamp contains
  14607. the date and the time. Otherwise, only the date is be included.
  14608. All parts of a date not specified by the user is filled in from
  14609. the current date/time. So if you just press return without
  14610. typing anything, the time stamp will represent the current
  14611. date/time.
  14612. If there is already a timestamp at the cursor, it will be
  14613. modified.
  14614. With two universal prefix arguments, insert an active timestamp
  14615. with the current time without prompting the user."
  14616. (interactive "P")
  14617. (let* ((ts nil)
  14618. (default-time
  14619. ;; Default time is either today, or, when entering a range,
  14620. ;; the range start.
  14621. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14622. (save-excursion
  14623. (re-search-backward
  14624. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14625. (- (point) 20) t)))
  14626. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14627. (current-time)))
  14628. (default-input (and ts (org-get-compact-tod ts)))
  14629. (repeater (save-excursion
  14630. (save-match-data
  14631. (beginning-of-line)
  14632. (when (re-search-forward
  14633. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14634. (save-excursion (progn (end-of-line) (point))) t)
  14635. (match-string 0)))))
  14636. org-time-was-given org-end-time-was-given time)
  14637. (cond
  14638. ((and (org-at-timestamp-p t)
  14639. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14640. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14641. (insert "--")
  14642. (setq time (let ((this-command this-command))
  14643. (org-read-date arg 'totime nil nil
  14644. default-time default-input inactive)))
  14645. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14646. ((org-at-timestamp-p t)
  14647. (setq time (let ((this-command this-command))
  14648. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14649. (when (org-at-timestamp-p t) ; just to get the match data
  14650. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14651. (replace-match "")
  14652. (setq org-last-changed-timestamp
  14653. (org-insert-time-stamp
  14654. time (or org-time-was-given arg)
  14655. inactive nil nil (list org-end-time-was-given)))
  14656. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14657. (setq org-last-changed-timestamp
  14658. (concat (substring org-last-inserted-timestamp 0 -1)
  14659. " " repeater ">"))))
  14660. (message "Timestamp updated"))
  14661. ((equal arg '(16))
  14662. (org-insert-time-stamp (current-time) t))
  14663. (t
  14664. (setq time (let ((this-command this-command))
  14665. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14666. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14667. nil nil (list org-end-time-was-given))))))
  14668. ;; FIXME: can we use this for something else, like computing time differences?
  14669. (defun org-get-compact-tod (s)
  14670. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14671. (let* ((t1 (match-string 1 s))
  14672. (h1 (string-to-number (match-string 2 s)))
  14673. (m1 (string-to-number (match-string 3 s)))
  14674. (t2 (and (match-end 4) (match-string 5 s)))
  14675. (h2 (and t2 (string-to-number (match-string 6 s))))
  14676. (m2 (and t2 (string-to-number (match-string 7 s))))
  14677. dh dm)
  14678. (if (not t2)
  14679. t1
  14680. (setq dh (- h2 h1) dm (- m2 m1))
  14681. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14682. (concat t1 "+" (number-to-string dh)
  14683. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14684. (defun org-time-stamp-inactive (&optional arg)
  14685. "Insert an inactive time stamp.
  14686. An inactive time stamp is enclosed in square brackets instead of angle
  14687. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14688. does not link to the calendar and cannot be changed with the S-cursor keys.
  14689. So these are more for recording a certain time/date."
  14690. (interactive "P")
  14691. (org-time-stamp arg 'inactive))
  14692. (defvar org-date-ovl (make-overlay 1 1))
  14693. (overlay-put org-date-ovl 'face 'org-date-selected)
  14694. (org-detach-overlay org-date-ovl)
  14695. (defvar org-ans1) ; dynamically scoped parameter
  14696. (defvar org-ans2) ; dynamically scoped parameter
  14697. (defvar org-plain-time-of-day-regexp) ; defined below
  14698. (defvar org-overriding-default-time nil) ; dynamically scoped
  14699. (defvar org-read-date-overlay nil)
  14700. (defvar org-dcst nil) ; dynamically scoped
  14701. (defvar org-read-date-history nil)
  14702. (defvar org-read-date-final-answer nil)
  14703. (defvar org-read-date-analyze-futurep nil)
  14704. (defvar org-read-date-analyze-forced-year nil)
  14705. (defvar org-read-date-inactive)
  14706. (defvar org-read-date-minibuffer-local-map
  14707. (let ((map (make-sparse-keymap)))
  14708. (set-keymap-parent map minibuffer-local-map)
  14709. (org-defkey map (kbd ".")
  14710. (lambda () (interactive)
  14711. (org-eval-in-calendar '(calendar-goto-today))))
  14712. (org-defkey map [(meta shift left)]
  14713. (lambda () (interactive)
  14714. (org-eval-in-calendar '(calendar-backward-month 1))))
  14715. (org-defkey map [(meta shift right)]
  14716. (lambda () (interactive)
  14717. (org-eval-in-calendar '(calendar-forward-month 1))))
  14718. (org-defkey map [(meta shift up)]
  14719. (lambda () (interactive)
  14720. (org-eval-in-calendar '(calendar-backward-year 1))))
  14721. (org-defkey map [(meta shift down)]
  14722. (lambda () (interactive)
  14723. (org-eval-in-calendar '(calendar-forward-year 1))))
  14724. (org-defkey map [?\e (shift left)]
  14725. (lambda () (interactive)
  14726. (org-eval-in-calendar '(calendar-backward-month 1))))
  14727. (org-defkey map [?\e (shift right)]
  14728. (lambda () (interactive)
  14729. (org-eval-in-calendar '(calendar-forward-month 1))))
  14730. (org-defkey map [?\e (shift up)]
  14731. (lambda () (interactive)
  14732. (org-eval-in-calendar '(calendar-backward-year 1))))
  14733. (org-defkey map [?\e (shift down)]
  14734. (lambda () (interactive)
  14735. (org-eval-in-calendar '(calendar-forward-year 1))))
  14736. (org-defkey map [(shift up)]
  14737. (lambda () (interactive)
  14738. (org-eval-in-calendar '(calendar-backward-week 1))))
  14739. (org-defkey map [(shift down)]
  14740. (lambda () (interactive)
  14741. (org-eval-in-calendar '(calendar-forward-week 1))))
  14742. (org-defkey map [(shift left)]
  14743. (lambda () (interactive)
  14744. (org-eval-in-calendar '(calendar-backward-day 1))))
  14745. (org-defkey map [(shift right)]
  14746. (lambda () (interactive)
  14747. (org-eval-in-calendar '(calendar-forward-day 1))))
  14748. (org-defkey map "!"
  14749. (lambda () (interactive)
  14750. (org-eval-in-calendar '(diary-view-entries))
  14751. (message "")))
  14752. (org-defkey map ">"
  14753. (lambda () (interactive)
  14754. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14755. (org-defkey map "<"
  14756. (lambda () (interactive)
  14757. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14758. (org-defkey map "\C-v"
  14759. (lambda () (interactive)
  14760. (org-eval-in-calendar
  14761. '(calendar-scroll-left-three-months 1))))
  14762. (org-defkey map "\M-v"
  14763. (lambda () (interactive)
  14764. (org-eval-in-calendar
  14765. '(calendar-scroll-right-three-months 1))))
  14766. map)
  14767. "Keymap for minibuffer commands when using `org-read-date'.")
  14768. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14769. default-time default-input inactive)
  14770. "Read a date, possibly a time, and make things smooth for the user.
  14771. The prompt will suggest to enter an ISO date, but you can also enter anything
  14772. which will at least partially be understood by `parse-time-string'.
  14773. Unrecognized parts of the date will default to the current day, month, year,
  14774. hour and minute. If this command is called to replace a timestamp at point,
  14775. or to enter the second timestamp of a range, the default time is taken
  14776. from the existing stamp. Furthermore, the command prefers the future,
  14777. so if you are giving a date where the year is not given, and the day-month
  14778. combination is already past in the current year, it will assume you
  14779. mean next year. For details, see the manual. A few examples:
  14780. 3-2-5 --> 2003-02-05
  14781. feb 15 --> currentyear-02-15
  14782. 2/15 --> currentyear-02-15
  14783. sep 12 9 --> 2009-09-12
  14784. 12:45 --> today 12:45
  14785. 22 sept 0:34 --> currentyear-09-22 0:34
  14786. 12 --> currentyear-currentmonth-12
  14787. Fri --> nearest Friday after today
  14788. -Tue --> last Tuesday
  14789. etc.
  14790. Furthermore you can specify a relative date by giving, as the *first* thing
  14791. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14792. change in days weeks, months, years.
  14793. With a single plus or minus, the date is relative to today. With a double
  14794. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14795. +4d --> four days from today
  14796. +4 --> same as above
  14797. +2w --> two weeks from today
  14798. ++5 --> five days from default date
  14799. The function understands only English month and weekday abbreviations.
  14800. While prompting, a calendar is popped up - you can also select the
  14801. date with the mouse (button 1). The calendar shows a period of three
  14802. months. To scroll it to other months, use the keys `>' and `<'.
  14803. If you don't like the calendar, turn it off with
  14804. \(setq org-read-date-popup-calendar nil)
  14805. With optional argument TO-TIME, the date will immediately be converted
  14806. to an internal time.
  14807. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14808. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14809. still enter a time, and this function will inform the calling routine
  14810. about this change. The calling routine may then choose to change the
  14811. format used to insert the time stamp into the buffer to include the time.
  14812. With optional argument FROM-STRING, read from this string instead from
  14813. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14814. the time/date that is used for everything that is not specified by the
  14815. user."
  14816. (require 'parse-time)
  14817. (let* ((org-time-stamp-rounding-minutes
  14818. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14819. (org-dcst org-display-custom-times)
  14820. (ct (org-current-time))
  14821. (org-def (or org-overriding-default-time default-time ct))
  14822. (org-defdecode (decode-time org-def))
  14823. (dummy (progn
  14824. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14825. (setcar (nthcdr 2 org-defdecode) -1)
  14826. (setcar (nthcdr 1 org-defdecode) 59)
  14827. (setq org-def (apply 'encode-time org-defdecode)
  14828. org-defdecode (decode-time org-def)))))
  14829. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14830. (calendar-frame-setup nil)
  14831. (calendar-setup nil)
  14832. (calendar-move-hook nil)
  14833. (calendar-view-diary-initially-flag nil)
  14834. (calendar-view-holidays-initially-flag nil)
  14835. (timestr (format-time-string
  14836. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14837. (prompt (concat (if prompt (concat prompt " ") "")
  14838. (format "Date+time [%s]: " timestr)))
  14839. ans (org-ans0 "") org-ans1 org-ans2 final)
  14840. (cond
  14841. (from-string (setq ans from-string))
  14842. (org-read-date-popup-calendar
  14843. (save-excursion
  14844. (save-window-excursion
  14845. (calendar)
  14846. (org-eval-in-calendar '(setq cursor-type nil) t)
  14847. (unwind-protect
  14848. (progn
  14849. (calendar-forward-day (- (time-to-days org-def)
  14850. (calendar-absolute-from-gregorian
  14851. (calendar-current-date))))
  14852. (org-eval-in-calendar nil t)
  14853. (let* ((old-map (current-local-map))
  14854. (map (copy-keymap calendar-mode-map))
  14855. (minibuffer-local-map
  14856. (copy-keymap org-read-date-minibuffer-local-map)))
  14857. (org-defkey map (kbd "RET") 'org-calendar-select)
  14858. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14859. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14860. (unwind-protect
  14861. (progn
  14862. (use-local-map map)
  14863. (setq org-read-date-inactive inactive)
  14864. (add-hook 'post-command-hook 'org-read-date-display)
  14865. (setq org-ans0 (read-string prompt default-input
  14866. 'org-read-date-history nil))
  14867. ;; org-ans0: from prompt
  14868. ;; org-ans1: from mouse click
  14869. ;; org-ans2: from calendar motion
  14870. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14871. (remove-hook 'post-command-hook 'org-read-date-display)
  14872. (use-local-map old-map)
  14873. (when org-read-date-overlay
  14874. (delete-overlay org-read-date-overlay)
  14875. (setq org-read-date-overlay nil)))))
  14876. (bury-buffer "*Calendar*")))))
  14877. (t ; Naked prompt only
  14878. (unwind-protect
  14879. (setq ans (read-string prompt default-input
  14880. 'org-read-date-history timestr))
  14881. (when org-read-date-overlay
  14882. (delete-overlay org-read-date-overlay)
  14883. (setq org-read-date-overlay nil)))))
  14884. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14885. (when org-read-date-analyze-forced-year
  14886. (message "Year was forced into %s"
  14887. (if org-read-date-force-compatible-dates
  14888. "compatible range (1970-2037)"
  14889. "range representable on this machine"))
  14890. (ding))
  14891. ;; One round trip to get rid of 34th of August and stuff like that....
  14892. (setq final (decode-time (apply 'encode-time final)))
  14893. (setq org-read-date-final-answer ans)
  14894. (if to-time
  14895. (apply 'encode-time final)
  14896. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14897. (format "%04d-%02d-%02d %02d:%02d"
  14898. (nth 5 final) (nth 4 final) (nth 3 final)
  14899. (nth 2 final) (nth 1 final))
  14900. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14901. (defvar org-def)
  14902. (defvar org-defdecode)
  14903. (defvar org-with-time)
  14904. (defun org-read-date-display ()
  14905. "Display the current date prompt interpretation in the minibuffer."
  14906. (when org-read-date-display-live
  14907. (when org-read-date-overlay
  14908. (delete-overlay org-read-date-overlay))
  14909. (when (minibufferp (current-buffer))
  14910. (save-excursion
  14911. (end-of-line 1)
  14912. (while (not (equal (buffer-substring
  14913. (max (point-min) (- (point) 4)) (point))
  14914. " "))
  14915. (insert " ")))
  14916. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14917. " " (or org-ans1 org-ans2)))
  14918. (org-end-time-was-given nil)
  14919. (f (org-read-date-analyze ans org-def org-defdecode))
  14920. (fmts (if org-dcst
  14921. org-time-stamp-custom-formats
  14922. org-time-stamp-formats))
  14923. (fmt (if (or org-with-time
  14924. (and (boundp 'org-time-was-given) org-time-was-given))
  14925. (cdr fmts)
  14926. (car fmts)))
  14927. (txt (format-time-string fmt (apply 'encode-time f)))
  14928. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14929. (txt (concat "=> " txt)))
  14930. (when (and org-end-time-was-given
  14931. (string-match org-plain-time-of-day-regexp txt))
  14932. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14933. org-end-time-was-given
  14934. (substring txt (match-end 0)))))
  14935. (when org-read-date-analyze-futurep
  14936. (setq txt (concat txt " (=>F)")))
  14937. (setq org-read-date-overlay
  14938. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14939. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14940. (defun org-read-date-analyze (ans org-def org-defdecode)
  14941. "Analyze the combined answer of the date prompt."
  14942. ;; FIXME: cleanup and comment
  14943. (let ((nowdecode (decode-time (current-time)))
  14944. delta deltan deltaw deltadef year month day
  14945. hour minute second wday pm h2 m2 tl wday1
  14946. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14947. (setq org-read-date-analyze-futurep nil
  14948. org-read-date-analyze-forced-year nil)
  14949. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14950. (setq ans "+0"))
  14951. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14952. (setq ans (replace-match "" t t ans)
  14953. deltan (car delta)
  14954. deltaw (nth 1 delta)
  14955. deltadef (nth 2 delta)))
  14956. ;; Check if there is an iso week date in there. If yes, store the
  14957. ;; info and postpone interpreting it until the rest of the parsing
  14958. ;; is done.
  14959. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14960. (setq iso-year (if (match-end 1)
  14961. (org-small-year-to-year
  14962. (string-to-number (match-string 1 ans))))
  14963. iso-weekday (if (match-end 3)
  14964. (string-to-number (match-string 3 ans)))
  14965. iso-week (string-to-number (match-string 2 ans)))
  14966. (setq ans (replace-match "" t t ans)))
  14967. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14968. (when (string-match
  14969. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14970. (setq year (if (match-end 2)
  14971. (string-to-number (match-string 2 ans))
  14972. (progn (setq kill-year t)
  14973. (string-to-number (format-time-string "%Y"))))
  14974. month (string-to-number (match-string 3 ans))
  14975. day (string-to-number (match-string 4 ans)))
  14976. (if (< year 100) (setq year (+ 2000 year)))
  14977. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14978. t nil ans)))
  14979. ;; Help matching dotted european dates
  14980. (when (string-match
  14981. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14982. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14983. (setq kill-year t)
  14984. (string-to-number (format-time-string "%Y")))
  14985. day (string-to-number (match-string 1 ans))
  14986. month (string-to-number (match-string 2 ans))
  14987. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14988. t nil ans)))
  14989. ;; Help matching american dates, like 5/30 or 5/30/7
  14990. (when (string-match
  14991. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14992. (setq year (if (match-end 4)
  14993. (string-to-number (match-string 4 ans))
  14994. (progn (setq kill-year t)
  14995. (string-to-number (format-time-string "%Y"))))
  14996. month (string-to-number (match-string 1 ans))
  14997. day (string-to-number (match-string 2 ans)))
  14998. (if (< year 100) (setq year (+ 2000 year)))
  14999. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15000. t nil ans)))
  15001. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15002. ;; If there is a time with am/pm, and *no* time without it, we convert
  15003. ;; so that matching will be successful.
  15004. (loop for i from 1 to 2 do ; twice, for end time as well
  15005. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15006. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15007. (setq hour (string-to-number (match-string 1 ans))
  15008. minute (if (match-end 3)
  15009. (string-to-number (match-string 3 ans))
  15010. 0)
  15011. pm (equal ?p
  15012. (string-to-char (downcase (match-string 4 ans)))))
  15013. (if (and (= hour 12) (not pm))
  15014. (setq hour 0)
  15015. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15016. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15017. t t ans))))
  15018. ;; Check if a time range is given as a duration
  15019. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15020. (setq hour (string-to-number (match-string 1 ans))
  15021. h2 (+ hour (string-to-number (match-string 3 ans)))
  15022. minute (string-to-number (match-string 2 ans))
  15023. m2 (+ minute (if (match-end 5) (string-to-number
  15024. (match-string 5 ans))0)))
  15025. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15026. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15027. t t ans)))
  15028. ;; Check if there is a time range
  15029. (when (boundp 'org-end-time-was-given)
  15030. (setq org-time-was-given nil)
  15031. (when (and (string-match org-plain-time-of-day-regexp ans)
  15032. (match-end 8))
  15033. (setq org-end-time-was-given (match-string 8 ans))
  15034. (setq ans (concat (substring ans 0 (match-beginning 7))
  15035. (substring ans (match-end 7))))))
  15036. (setq tl (parse-time-string ans)
  15037. day (or (nth 3 tl) (nth 3 org-defdecode))
  15038. month (or (nth 4 tl)
  15039. (if (and org-read-date-prefer-future
  15040. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  15041. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  15042. (nth 4 org-defdecode)))
  15043. year (or (and (not kill-year) (nth 5 tl))
  15044. (if (and org-read-date-prefer-future
  15045. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  15046. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  15047. (nth 5 org-defdecode)))
  15048. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15049. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15050. second (or (nth 0 tl) 0)
  15051. wday (nth 6 tl))
  15052. (when (and (eq org-read-date-prefer-future 'time)
  15053. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15054. (equal day (nth 3 nowdecode))
  15055. (equal month (nth 4 nowdecode))
  15056. (equal year (nth 5 nowdecode))
  15057. (nth 2 tl)
  15058. (or (< (nth 2 tl) (nth 2 nowdecode))
  15059. (and (= (nth 2 tl) (nth 2 nowdecode))
  15060. (nth 1 tl)
  15061. (< (nth 1 tl) (nth 1 nowdecode)))))
  15062. (setq day (1+ day)
  15063. futurep t))
  15064. ;; Special date definitions below
  15065. (cond
  15066. (iso-week
  15067. ;; There was an iso week
  15068. (require 'cal-iso)
  15069. (setq futurep nil)
  15070. (setq year (or iso-year year)
  15071. day (or iso-weekday wday 1)
  15072. wday nil ; to make sure that the trigger below does not match
  15073. iso-date (calendar-gregorian-from-absolute
  15074. (calendar-absolute-from-iso
  15075. (list iso-week day year))))
  15076. ; FIXME: Should we also push ISO weeks into the future?
  15077. ; (when (and org-read-date-prefer-future
  15078. ; (not iso-year)
  15079. ; (< (calendar-absolute-from-gregorian iso-date)
  15080. ; (time-to-days (current-time))))
  15081. ; (setq year (1+ year)
  15082. ; iso-date (calendar-gregorian-from-absolute
  15083. ; (calendar-absolute-from-iso
  15084. ; (list iso-week day year)))))
  15085. (setq month (car iso-date)
  15086. year (nth 2 iso-date)
  15087. day (nth 1 iso-date)))
  15088. (deltan
  15089. (setq futurep nil)
  15090. (unless deltadef
  15091. (let ((now (decode-time (current-time))))
  15092. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15093. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15094. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15095. ((equal deltaw "m") (setq month (+ month deltan)))
  15096. ((equal deltaw "y") (setq year (+ year deltan)))))
  15097. ((and wday (not (nth 3 tl)))
  15098. ;; Weekday was given, but no day, so pick that day in the week
  15099. ;; on or after the derived date.
  15100. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15101. (unless (equal wday wday1)
  15102. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15103. (if (and (boundp 'org-time-was-given)
  15104. (nth 2 tl))
  15105. (setq org-time-was-given t))
  15106. (if (< year 100) (setq year (+ 2000 year)))
  15107. ;; Check of the date is representable
  15108. (if org-read-date-force-compatible-dates
  15109. (progn
  15110. (if (< year 1970)
  15111. (setq year 1970 org-read-date-analyze-forced-year t))
  15112. (if (> year 2037)
  15113. (setq year 2037 org-read-date-analyze-forced-year t)))
  15114. (condition-case nil
  15115. (ignore (encode-time second minute hour day month year))
  15116. (error
  15117. (setq year (nth 5 org-defdecode))
  15118. (setq org-read-date-analyze-forced-year t))))
  15119. (setq org-read-date-analyze-futurep futurep)
  15120. (list second minute hour day month year)))
  15121. (defvar parse-time-weekdays)
  15122. (defun org-read-date-get-relative (s today default)
  15123. "Check string S for special relative date string.
  15124. TODAY and DEFAULT are internal times, for today and for a default.
  15125. Return shift list (N what def-flag)
  15126. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15127. N is the number of WHATs to shift.
  15128. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15129. the DEFAULT date rather than TODAY."
  15130. (require 'parse-time)
  15131. (when (and
  15132. (string-match
  15133. (concat
  15134. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15135. "\\([0-9]+\\)?"
  15136. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15137. "\\([ \t]\\|$\\)") s)
  15138. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15139. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15140. (string-to-char (substring (match-string 1 s) -1))
  15141. ?+))
  15142. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15143. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15144. (what (if (match-end 3) (match-string 3 s) "d"))
  15145. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15146. (date (if rel default today))
  15147. (wday (nth 6 (decode-time date)))
  15148. delta)
  15149. (if wday1
  15150. (progn
  15151. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15152. (if (= delta 0) (setq delta 7))
  15153. (if (= dir ?-)
  15154. (progn
  15155. (setq delta (- delta 7))
  15156. (if (= delta 0) (setq delta -7))
  15157. ))
  15158. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15159. (list delta "d" rel))
  15160. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15161. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15162. "Turn a user-specified date into the internal representation.
  15163. The internal representation needed by the calendar is (month day year).
  15164. This is a wrapper to handle the brain-dead convention in calendar that
  15165. user function argument order change dependent on argument order."
  15166. (if (boundp 'calendar-date-style)
  15167. (cond
  15168. ((eq calendar-date-style 'american)
  15169. (list arg1 arg2 arg3))
  15170. ((eq calendar-date-style 'european)
  15171. (list arg2 arg1 arg3))
  15172. ((eq calendar-date-style 'iso)
  15173. (list arg2 arg3 arg1)))
  15174. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15175. (if (org-bound-and-true-p european-calendar-style)
  15176. (list arg2 arg1 arg3)
  15177. (list arg1 arg2 arg3)))))
  15178. (defun org-eval-in-calendar (form &optional keepdate)
  15179. "Eval FORM in the calendar window and return to current window.
  15180. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15181. otherwise stick to the current value of `org-ans2'."
  15182. (let ((sf (selected-frame))
  15183. (sw (selected-window)))
  15184. (select-window (get-buffer-window "*Calendar*" t))
  15185. (eval form)
  15186. (when (and (not keepdate) (calendar-cursor-to-date))
  15187. (let* ((date (calendar-cursor-to-date))
  15188. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15189. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15190. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15191. (select-window sw)
  15192. (org-select-frame-set-input-focus sf)))
  15193. (defun org-calendar-select ()
  15194. "Return to `org-read-date' with the date currently selected.
  15195. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15196. (interactive)
  15197. (when (calendar-cursor-to-date)
  15198. (let* ((date (calendar-cursor-to-date))
  15199. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15200. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15201. (if (active-minibuffer-window) (exit-minibuffer))))
  15202. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15203. "Insert a date stamp for the date given by the internal TIME.
  15204. WITH-HM means use the stamp format that includes the time of the day.
  15205. INACTIVE means use square brackets instead of angular ones, so that the
  15206. stamp will not contribute to the agenda.
  15207. PRE and POST are optional strings to be inserted before and after the
  15208. stamp.
  15209. The command returns the inserted time stamp."
  15210. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15211. stamp)
  15212. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15213. (insert-before-markers (or pre ""))
  15214. (when (listp extra)
  15215. (setq extra (car extra))
  15216. (if (and (stringp extra)
  15217. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15218. (setq extra (format "-%02d:%02d"
  15219. (string-to-number (match-string 1 extra))
  15220. (string-to-number (match-string 2 extra))))
  15221. (setq extra nil)))
  15222. (when extra
  15223. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15224. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15225. (insert-before-markers (or post ""))
  15226. (setq org-last-inserted-timestamp stamp)))
  15227. (defun org-toggle-time-stamp-overlays ()
  15228. "Toggle the use of custom time stamp formats."
  15229. (interactive)
  15230. (setq org-display-custom-times (not org-display-custom-times))
  15231. (unless org-display-custom-times
  15232. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15233. (while (setq p (next-single-property-change p 'display))
  15234. (if (and (get-text-property p 'display)
  15235. (eq (get-text-property p 'face) 'org-date))
  15236. (remove-text-properties
  15237. p (setq p (next-single-property-change p 'display))
  15238. '(display t))))
  15239. (set-buffer-modified-p bmp)))
  15240. (if (featurep 'xemacs)
  15241. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15242. (org-restart-font-lock)
  15243. (setq org-table-may-need-update t)
  15244. (if org-display-custom-times
  15245. (message "Time stamps are overlaid with custom format")
  15246. (message "Time stamp overlays removed")))
  15247. (defun org-display-custom-time (beg end)
  15248. "Overlay modified time stamp format over timestamp between BEG and END."
  15249. (let* ((ts (buffer-substring beg end))
  15250. t1 w1 with-hm tf time str w2 (off 0))
  15251. (save-match-data
  15252. (setq t1 (org-parse-time-string ts t))
  15253. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15254. (setq off (- (match-end 0) (match-beginning 0)))))
  15255. (setq end (- end off))
  15256. (setq w1 (- end beg)
  15257. with-hm (and (nth 1 t1) (nth 2 t1))
  15258. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15259. time (org-fix-decoded-time t1)
  15260. str (org-add-props
  15261. (format-time-string
  15262. (substring tf 1 -1) (apply 'encode-time time))
  15263. nil 'mouse-face 'highlight)
  15264. w2 (length str))
  15265. (if (not (= w2 w1))
  15266. (add-text-properties (1+ beg) (+ 2 beg)
  15267. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15268. (if (featurep 'xemacs)
  15269. (progn
  15270. (put-text-property beg end 'invisible t)
  15271. (put-text-property beg end 'end-glyph (make-glyph str)))
  15272. (put-text-property beg end 'display str))))
  15273. (defun org-translate-time (string)
  15274. "Translate all timestamps in STRING to custom format.
  15275. But do this only if the variable `org-display-custom-times' is set."
  15276. (when org-display-custom-times
  15277. (save-match-data
  15278. (let* ((start 0)
  15279. (re org-ts-regexp-both)
  15280. t1 with-hm inactive tf time str beg end)
  15281. (while (setq start (string-match re string start))
  15282. (setq beg (match-beginning 0)
  15283. end (match-end 0)
  15284. t1 (save-match-data
  15285. (org-parse-time-string (substring string beg end) t))
  15286. with-hm (and (nth 1 t1) (nth 2 t1))
  15287. inactive (equal (substring string beg (1+ beg)) "[")
  15288. tf (funcall (if with-hm 'cdr 'car)
  15289. org-time-stamp-custom-formats)
  15290. time (org-fix-decoded-time t1)
  15291. str (format-time-string
  15292. (concat
  15293. (if inactive "[" "<") (substring tf 1 -1)
  15294. (if inactive "]" ">"))
  15295. (apply 'encode-time time))
  15296. string (replace-match str t t string)
  15297. start (+ start (length str)))))))
  15298. string)
  15299. (defun org-fix-decoded-time (time)
  15300. "Set 0 instead of nil for the first 6 elements of time.
  15301. Don't touch the rest."
  15302. (let ((n 0))
  15303. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15304. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15305. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15306. "Difference between TIMESTAMP-STRING and now in days.
  15307. If SECONDS is non-nil, return the difference in seconds."
  15308. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15309. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15310. (funcall fdiff (current-time)))))
  15311. (defun org-deadline-close (timestamp-string &optional ndays)
  15312. "Is the time in TIMESTAMP-STRING close to the current date?"
  15313. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15314. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15315. (not (org-entry-is-done-p))))
  15316. (defun org-get-wdays (ts &optional delay zero-delay)
  15317. "Get the deadline lead time appropriate for timestring TS.
  15318. When DELAY is non-nil, get the delay time for scheduled items
  15319. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15320. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15321. don't try to find the delay cookie in the scheduled timestamp."
  15322. (let ((tv (if delay org-scheduled-delay-days
  15323. org-deadline-warning-days)))
  15324. (cond
  15325. ((or (and delay (< tv 0))
  15326. (and delay zero-delay (<= tv 0))
  15327. (and (not delay) (<= tv 0)))
  15328. ;; Enforce this value no matter what
  15329. (- tv))
  15330. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15331. ;; lead time is specified.
  15332. (floor (* (string-to-number (match-string 1 ts))
  15333. (cdr (assoc (match-string 2 ts)
  15334. '(("d" . 1) ("w" . 7)
  15335. ("m" . 30.4) ("y" . 365.25)
  15336. ("h" . 0.041667)))))))
  15337. ;; go for the default.
  15338. (t tv))))
  15339. (defun org-calendar-select-mouse (ev)
  15340. "Return to `org-read-date' with the date currently selected.
  15341. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15342. (interactive "e")
  15343. (mouse-set-point ev)
  15344. (when (calendar-cursor-to-date)
  15345. (let* ((date (calendar-cursor-to-date))
  15346. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15347. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15348. (if (active-minibuffer-window) (exit-minibuffer))))
  15349. (defun org-check-deadlines (ndays)
  15350. "Check if there are any deadlines due or past due.
  15351. A deadline is considered due if it happens within `org-deadline-warning-days'
  15352. days from today's date. If the deadline appears in an entry marked DONE,
  15353. it is not shown. The prefix arg NDAYS can be used to test that many
  15354. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15355. (interactive "P")
  15356. (let* ((org-warn-days
  15357. (cond
  15358. ((equal ndays '(4)) 100000)
  15359. (ndays (prefix-numeric-value ndays))
  15360. (t (abs org-deadline-warning-days))))
  15361. (case-fold-search nil)
  15362. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15363. (callback
  15364. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15365. (message "%d deadlines past-due or due within %d days"
  15366. (org-occur regexp nil callback)
  15367. org-warn-days)))
  15368. (defsubst org-re-timestamp (type)
  15369. "Return a regexp for timestamp TYPE.
  15370. Allowed values for TYPE are:
  15371. all: all timestamps
  15372. active: only active timestamps (<...>)
  15373. inactive: only inactive timestamps ([...])
  15374. scheduled: only scheduled timestamps
  15375. deadline: only deadline timestamps
  15376. closed: only closed time-stamps
  15377. When TYPE is nil, fall back on returning a regexp that matches
  15378. both scheduled and deadline timestamps."
  15379. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15380. ((eq type 'active) org-ts-regexp)
  15381. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15382. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15383. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15384. ((eq type 'closed) (concat org-closed-string " \\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]"))
  15385. ((eq type 'scheduled-or-deadline)
  15386. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15387. (defun org-check-before-date (date)
  15388. "Check if there are deadlines or scheduled entries before DATE."
  15389. (interactive (list (org-read-date)))
  15390. (let ((case-fold-search nil)
  15391. (regexp (org-re-timestamp org-ts-type))
  15392. (callback
  15393. (lambda () (time-less-p
  15394. (org-time-string-to-time (match-string 1))
  15395. (org-time-string-to-time date)))))
  15396. (message "%d entries before %s"
  15397. (org-occur regexp nil callback) date)))
  15398. (defun org-check-after-date (date)
  15399. "Check if there are deadlines or scheduled entries after DATE."
  15400. (interactive (list (org-read-date)))
  15401. (let ((case-fold-search nil)
  15402. (regexp (org-re-timestamp org-ts-type))
  15403. (callback
  15404. (lambda () (not
  15405. (time-less-p
  15406. (org-time-string-to-time (match-string 1))
  15407. (org-time-string-to-time date))))))
  15408. (message "%d entries after %s"
  15409. (org-occur regexp nil callback) date)))
  15410. (defun org-check-dates-range (start-date end-date)
  15411. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15412. (interactive (list (org-read-date nil nil nil "Range starts")
  15413. (org-read-date nil nil nil "Range end")))
  15414. (let ((case-fold-search nil)
  15415. (regexp (org-re-timestamp org-ts-type))
  15416. (callback
  15417. (lambda ()
  15418. (let ((match (match-string 1)))
  15419. (and
  15420. (not (time-less-p
  15421. (org-time-string-to-time match)
  15422. (org-time-string-to-time start-date)))
  15423. (time-less-p
  15424. (org-time-string-to-time match)
  15425. (org-time-string-to-time end-date)))))))
  15426. (message "%d entries between %s and %s"
  15427. (org-occur regexp nil callback) start-date end-date)))
  15428. (defun org-evaluate-time-range (&optional to-buffer)
  15429. "Evaluate a time range by computing the difference between start and end.
  15430. Normally the result is just printed in the echo area, but with prefix arg
  15431. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15432. If the time range is actually in a table, the result is inserted into the
  15433. next column.
  15434. For time difference computation, a year is assumed to be exactly 365
  15435. days in order to avoid rounding problems."
  15436. (interactive "P")
  15437. (or
  15438. (org-clock-update-time-maybe)
  15439. (save-excursion
  15440. (unless (org-at-date-range-p t)
  15441. (goto-char (point-at-bol))
  15442. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15443. (if (not (org-at-date-range-p t))
  15444. (error "Not at a time-stamp range, and none found in current line")))
  15445. (let* ((ts1 (match-string 1))
  15446. (ts2 (match-string 2))
  15447. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15448. (match-end (match-end 0))
  15449. (time1 (org-time-string-to-time ts1))
  15450. (time2 (org-time-string-to-time ts2))
  15451. (t1 (org-float-time time1))
  15452. (t2 (org-float-time time2))
  15453. (diff (abs (- t2 t1)))
  15454. (negative (< (- t2 t1) 0))
  15455. ;; (ys (floor (* 365 24 60 60)))
  15456. (ds (* 24 60 60))
  15457. (hs (* 60 60))
  15458. (fy "%dy %dd %02d:%02d")
  15459. (fy1 "%dy %dd")
  15460. (fd "%dd %02d:%02d")
  15461. (fd1 "%dd")
  15462. (fh "%02d:%02d")
  15463. y d h m align)
  15464. (if havetime
  15465. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15466. y 0
  15467. d (floor (/ diff ds)) diff (mod diff ds)
  15468. h (floor (/ diff hs)) diff (mod diff hs)
  15469. m (floor (/ diff 60)))
  15470. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15471. y 0
  15472. d (floor (+ (/ diff ds) 0.5))
  15473. h 0 m 0))
  15474. (if (not to-buffer)
  15475. (message "%s" (org-make-tdiff-string y d h m))
  15476. (if (org-at-table-p)
  15477. (progn
  15478. (goto-char match-end)
  15479. (setq align t)
  15480. (and (looking-at " *|") (goto-char (match-end 0))))
  15481. (goto-char match-end))
  15482. (if (looking-at
  15483. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15484. (replace-match ""))
  15485. (if negative (insert " -"))
  15486. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15487. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15488. (insert " " (format fh h m))))
  15489. (if align (org-table-align))
  15490. (message "Time difference inserted")))))
  15491. (defun org-make-tdiff-string (y d h m)
  15492. (let ((fmt "")
  15493. (l nil))
  15494. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15495. l (push y l)))
  15496. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15497. l (push d l)))
  15498. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15499. l (push h l)))
  15500. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15501. l (push m l)))
  15502. (apply 'format fmt (nreverse l))))
  15503. (defun org-time-string-to-time (s &optional buffer pos)
  15504. "Convert a timestamp string into internal time."
  15505. (condition-case errdata
  15506. (apply 'encode-time (org-parse-time-string s))
  15507. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15508. s (if (not (and buffer pos))
  15509. ""
  15510. (format " at %d in buffer `%s'" pos buffer))
  15511. (cdr errdata)))))
  15512. (defun org-time-string-to-seconds (s)
  15513. "Convert a timestamp string to a number of seconds."
  15514. (org-float-time (org-time-string-to-time s)))
  15515. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15516. "Convert a time stamp to an absolute day number.
  15517. If there is a specifier for a cyclic time stamp, get the closest
  15518. date to DAYNR.
  15519. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15520. The variable `date' is bound by the calendar when this is called."
  15521. (cond
  15522. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15523. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15524. daynr
  15525. (+ daynr 1000)))
  15526. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15527. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15528. (time-to-days (current-time))) (match-string 0 s)
  15529. prefer show-all))
  15530. (t (time-to-days
  15531. (condition-case errdata
  15532. (apply 'encode-time (org-parse-time-string s))
  15533. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15534. s (if (not (and buffer pos))
  15535. ""
  15536. (format " at %d in buffer `%s'" pos buffer))
  15537. (cdr errdata))))))))
  15538. (defun org-days-to-iso-week (days)
  15539. "Return the iso week number."
  15540. (require 'cal-iso)
  15541. (car (calendar-iso-from-absolute days)))
  15542. (defun org-small-year-to-year (year)
  15543. "Convert 2-digit years into 4-digit years.
  15544. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15545. The year 2000 cannot be abbreviated. Any year larger than 99
  15546. is returned unchanged."
  15547. (if (< year 38)
  15548. (setq year (+ 2000 year))
  15549. (if (< year 100)
  15550. (setq year (+ 1900 year))))
  15551. year)
  15552. (defun org-time-from-absolute (d)
  15553. "Return the time corresponding to date D.
  15554. D may be an absolute day number, or a calendar-type list (month day year)."
  15555. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15556. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15557. (defun org-calendar-holiday ()
  15558. "List of holidays, for Diary display in Org-mode."
  15559. (require 'holidays)
  15560. (let ((hl (funcall
  15561. (if (fboundp 'calendar-check-holidays)
  15562. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15563. (if hl (mapconcat 'identity hl "; "))))
  15564. (defun org-diary-sexp-entry (sexp entry date)
  15565. "Process a SEXP diary ENTRY for DATE."
  15566. (require 'diary-lib)
  15567. (let ((result (if calendar-debug-sexp
  15568. (let ((stack-trace-on-error t))
  15569. (eval (car (read-from-string sexp))))
  15570. (condition-case nil
  15571. (eval (car (read-from-string sexp)))
  15572. (error
  15573. (beep)
  15574. (message "Bad sexp at line %d in %s: %s"
  15575. (org-current-line)
  15576. (buffer-file-name) sexp)
  15577. (sleep-for 2))))))
  15578. (cond ((stringp result) (split-string result "; "))
  15579. ((and (consp result)
  15580. (not (consp (cdr result)))
  15581. (stringp (cdr result))) (cdr result))
  15582. ((and (consp result)
  15583. (stringp (car result))) result)
  15584. (result entry))))
  15585. (defun org-diary-to-ical-string (frombuf)
  15586. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15587. This uses the icalendar.el library."
  15588. (let* ((tmpdir (if (featurep 'xemacs)
  15589. (temp-directory)
  15590. temporary-file-directory))
  15591. (tmpfile (make-temp-name
  15592. (expand-file-name "orgics" tmpdir)))
  15593. buf rtn b e)
  15594. (with-current-buffer frombuf
  15595. (icalendar-export-region (point-min) (point-max) tmpfile)
  15596. (setq buf (find-buffer-visiting tmpfile))
  15597. (set-buffer buf)
  15598. (goto-char (point-min))
  15599. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15600. (setq b (match-beginning 0)))
  15601. (goto-char (point-max))
  15602. (if (re-search-backward "^END:VEVENT" nil t)
  15603. (setq e (match-end 0)))
  15604. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15605. (kill-buffer buf)
  15606. (delete-file tmpfile)
  15607. rtn))
  15608. (defun org-closest-date (start current change prefer show-all)
  15609. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15610. When PREFER is `past', return a date that is either CURRENT or past.
  15611. When PREFER is `future', return a date that is either CURRENT or future.
  15612. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15613. ;; Make the proper lists from the dates
  15614. (catch 'exit
  15615. (let ((a1 '(("h" . hour)
  15616. ("d" . day)
  15617. ("w" . week)
  15618. ("m" . month)
  15619. ("y" . year)))
  15620. (shour (nth 2 (org-parse-time-string start)))
  15621. dn dw sday cday n1 n2 n0
  15622. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15623. (setq start (org-date-to-gregorian start)
  15624. current (org-date-to-gregorian
  15625. (if show-all
  15626. current
  15627. (time-to-days (current-time))))
  15628. sday (calendar-absolute-from-gregorian start)
  15629. cday (calendar-absolute-from-gregorian current))
  15630. (if (<= cday sday) (throw 'exit sday))
  15631. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15632. (setq dn (string-to-number (match-string 1 change))
  15633. dw (cdr (assoc (match-string 2 change) a1)))
  15634. (error "Invalid change specifier: %s" change))
  15635. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15636. (cond
  15637. ((eq dw 'hour)
  15638. (let ((missing-hours
  15639. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15640. dn)))
  15641. (setq n1 (if (zerop missing-hours) cday
  15642. (- cday (1+ (floor (/ missing-hours 24)))))
  15643. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15644. ((eq dw 'day)
  15645. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15646. n2 (+ n1 dn)))
  15647. ((eq dw 'year)
  15648. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15649. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15650. (setq date1 (list m d y1)
  15651. n1 (calendar-absolute-from-gregorian date1)
  15652. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15653. n2 (calendar-absolute-from-gregorian date2)))
  15654. ((eq dw 'month)
  15655. ;; approx number of month between the two dates
  15656. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15657. ;; How often does dn fit in there?
  15658. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15659. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15660. m (+ m nm)
  15661. ny (floor (/ m 12))
  15662. y (+ y ny)
  15663. m (- m (* ny 12)))
  15664. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15665. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15666. (setq m2 (+ m dn) y2 y)
  15667. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15668. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15669. (while (<= n2 cday)
  15670. (setq n1 n2 m m2 y y2)
  15671. (setq m2 (+ m dn) y2 y)
  15672. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15673. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15674. ;; Make sure n1 is the earlier date
  15675. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15676. (if show-all
  15677. (cond
  15678. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15679. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15680. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15681. (cond
  15682. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15683. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15684. (t (if (= cday n1) n1 n2)))))))
  15685. (defun org-date-to-gregorian (date)
  15686. "Turn any specification of DATE into a Gregorian date for the calendar."
  15687. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15688. ((and (listp date) (= (length date) 3)) date)
  15689. ((stringp date)
  15690. (setq date (org-parse-time-string date))
  15691. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15692. ((listp date)
  15693. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15694. (defun org-parse-time-string (s &optional nodefault)
  15695. "Parse the standard Org-mode time string.
  15696. This should be a lot faster than the normal `parse-time-string'.
  15697. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15698. hour and minute fields will be nil if not given."
  15699. (cond ((string-match org-ts-regexp0 s)
  15700. (list 0
  15701. (if (or (match-beginning 8) (not nodefault))
  15702. (string-to-number (or (match-string 8 s) "0")))
  15703. (if (or (match-beginning 7) (not nodefault))
  15704. (string-to-number (or (match-string 7 s) "0")))
  15705. (string-to-number (match-string 4 s))
  15706. (string-to-number (match-string 3 s))
  15707. (string-to-number (match-string 2 s))
  15708. nil nil nil))
  15709. ((string-match "^<[^>]+>$" s)
  15710. (decode-time (seconds-to-time (org-matcher-time s))))
  15711. (t (error "Not a standard Org-mode time string: %s" s))))
  15712. (defun org-timestamp-up (&optional arg)
  15713. "Increase the date item at the cursor by one.
  15714. If the cursor is on the year, change the year. If it is on the month,
  15715. the day or the time, change that.
  15716. With prefix ARG, change by that many units."
  15717. (interactive "p")
  15718. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15719. (defun org-timestamp-down (&optional arg)
  15720. "Decrease the date item at the cursor by one.
  15721. If the cursor is on the year, change the year. If it is on the month,
  15722. the day or the time, change that.
  15723. With prefix ARG, change by that many units."
  15724. (interactive "p")
  15725. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15726. (defun org-timestamp-up-day (&optional arg)
  15727. "Increase the date in the time stamp by one day.
  15728. With prefix ARG, change that many days."
  15729. (interactive "p")
  15730. (if (and (not (org-at-timestamp-p t))
  15731. (org-at-heading-p))
  15732. (org-todo 'up)
  15733. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15734. (defun org-timestamp-down-day (&optional arg)
  15735. "Decrease the date in the time stamp by one day.
  15736. With prefix ARG, change that many days."
  15737. (interactive "p")
  15738. (if (and (not (org-at-timestamp-p t))
  15739. (org-at-heading-p))
  15740. (org-todo 'down)
  15741. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15742. (defun org-at-timestamp-p (&optional inactive-ok)
  15743. "Determine if the cursor is in or at a timestamp."
  15744. (interactive)
  15745. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15746. (pos (point))
  15747. (ans (or (looking-at tsr)
  15748. (save-excursion
  15749. (skip-chars-backward "^[<\n\r\t")
  15750. (if (> (point) (point-min)) (backward-char 1))
  15751. (and (looking-at tsr)
  15752. (> (- (match-end 0) pos) -1))))))
  15753. (and ans
  15754. (boundp 'org-ts-what)
  15755. (setq org-ts-what
  15756. (cond
  15757. ((= pos (match-beginning 0)) 'bracket)
  15758. ;; Point is considered to be "on the bracket" whether
  15759. ;; it's really on it or right after it.
  15760. ((= pos (1- (match-end 0))) 'bracket)
  15761. ((= pos (match-end 0)) 'after)
  15762. ((org-pos-in-match-range pos 2) 'year)
  15763. ((org-pos-in-match-range pos 3) 'month)
  15764. ((org-pos-in-match-range pos 7) 'hour)
  15765. ((org-pos-in-match-range pos 8) 'minute)
  15766. ((or (org-pos-in-match-range pos 4)
  15767. (org-pos-in-match-range pos 5)) 'day)
  15768. ((and (> pos (or (match-end 8) (match-end 5)))
  15769. (< pos (match-end 0)))
  15770. (- pos (or (match-end 8) (match-end 5))))
  15771. (t 'day))))
  15772. ans))
  15773. (defun org-toggle-timestamp-type ()
  15774. "Toggle the type (<active> or [inactive]) of a time stamp."
  15775. (interactive)
  15776. (when (org-at-timestamp-p t)
  15777. (let ((beg (match-beginning 0)) (end (match-end 0))
  15778. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15779. (save-excursion
  15780. (goto-char beg)
  15781. (while (re-search-forward "[][<>]" end t)
  15782. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15783. t t)))
  15784. (message "Timestamp is now %sactive"
  15785. (if (equal (char-after beg) ?<) "" "in")))))
  15786. (defun org-at-clock-log-p nil
  15787. "Is the cursor on the clock log line?"
  15788. (save-excursion
  15789. (move-beginning-of-line 1)
  15790. (looking-at "^[ \t]*CLOCK:")))
  15791. (defvar org-clock-history) ; defined in org-clock.el
  15792. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15793. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15794. "Change the date in the time stamp at point.
  15795. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15796. `year', `minute', `second'. If WHAT is not given, the cursor position
  15797. in the timestamp determines what will be changed.
  15798. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15799. (let ((origin (point)) origin-cat
  15800. with-hm inactive
  15801. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15802. org-ts-what
  15803. extra rem
  15804. ts time time0 fixnext clrgx)
  15805. (if (not (org-at-timestamp-p t))
  15806. (error "Not at a timestamp"))
  15807. (if (and (not what) (eq org-ts-what 'bracket))
  15808. (org-toggle-timestamp-type)
  15809. ;; Point isn't on brackets. Remember the part of the time-stamp
  15810. ;; the point was in. Indeed, size of time-stamps may change,
  15811. ;; but point must be kept in the same category nonetheless.
  15812. (setq origin-cat org-ts-what)
  15813. (if (and (not what) (not (eq org-ts-what 'day))
  15814. org-display-custom-times
  15815. (get-text-property (point) 'display)
  15816. (not (get-text-property (1- (point)) 'display)))
  15817. (setq org-ts-what 'day))
  15818. (setq org-ts-what (or what org-ts-what)
  15819. inactive (= (char-after (match-beginning 0)) ?\[)
  15820. ts (match-string 0))
  15821. (replace-match "")
  15822. (when (string-match
  15823. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15824. ts)
  15825. (setq extra (match-string 1 ts))
  15826. (if suppress-tmp-delay
  15827. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15828. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15829. (setq with-hm t))
  15830. (setq time0 (org-parse-time-string ts))
  15831. (when (and updown
  15832. (eq org-ts-what 'minute)
  15833. (not current-prefix-arg))
  15834. ;; This looks like s-up and s-down. Change by one rounding step.
  15835. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15836. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15837. (setcar (cdr time0) (+ (nth 1 time0)
  15838. (if (> n 0) (- rem) (- dm rem))))))
  15839. (setq time
  15840. (encode-time (or (car time0) 0)
  15841. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15842. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15843. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15844. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15845. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15846. (nthcdr 6 time0)))
  15847. (when (and (member org-ts-what '(hour minute))
  15848. extra
  15849. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15850. (setq extra (org-modify-ts-extra
  15851. extra
  15852. (if (eq org-ts-what 'hour) 2 5)
  15853. n dm)))
  15854. (when (integerp org-ts-what)
  15855. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15856. (if (eq what 'calendar)
  15857. (let ((cal-date (org-get-date-from-calendar)))
  15858. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15859. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15860. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15861. (setcar time0 (or (car time0) 0))
  15862. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15863. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15864. (setq time (apply 'encode-time time0))))
  15865. ;; Insert the new time-stamp, and ensure point stays in the same
  15866. ;; category as before (i.e. not after the last position in that
  15867. ;; category).
  15868. (let ((pos (point)))
  15869. ;; Stay before inserted string. `save-excursion' is of no use.
  15870. (setq org-last-changed-timestamp
  15871. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15872. (goto-char pos))
  15873. (save-match-data
  15874. (looking-at org-ts-regexp3)
  15875. (goto-char (cond
  15876. ;; `day' category ends before `hour' if any, or at
  15877. ;; the end of the day name.
  15878. ((eq origin-cat 'day)
  15879. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15880. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15881. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15882. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15883. ;; `year' and `month' have both fixed size: point
  15884. ;; couldn't have moved into another part.
  15885. (t origin))))
  15886. ;; Update clock if on a CLOCK line.
  15887. (org-clock-update-time-maybe)
  15888. ;; Maybe adjust the closest clock in `org-clock-history'
  15889. (when org-clock-adjust-closest
  15890. (if (not (and (org-at-clock-log-p)
  15891. (< 1 (length (delq nil (mapcar 'marker-position
  15892. org-clock-history))))))
  15893. (message "No clock to adjust")
  15894. (cond ((save-excursion ; fix previous clock?
  15895. (re-search-backward org-ts-regexp0 nil t)
  15896. (org-looking-back (concat org-clock-string " \\[")))
  15897. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15898. ((save-excursion ; fix next clock?
  15899. (re-search-backward org-ts-regexp0 nil t)
  15900. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15901. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15902. (save-window-excursion
  15903. ;; Find closest clock to point, adjust the previous/next one in history
  15904. (let* ((p (save-excursion (org-back-to-heading t)))
  15905. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15906. (clfixnth
  15907. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15908. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15909. (if (not clfixpos)
  15910. (message "No clock to adjust")
  15911. (save-excursion
  15912. (org-goto-marker-or-bmk clfixpos)
  15913. (org-show-subtree)
  15914. (when (re-search-forward clrgx nil t)
  15915. (goto-char (match-beginning 1))
  15916. (let (org-clock-adjust-closest)
  15917. (org-timestamp-change n org-ts-what updown))
  15918. (message "Clock adjusted in %s for heading: %s"
  15919. (file-name-nondirectory (buffer-file-name))
  15920. (org-get-heading t t)))))))))
  15921. ;; Try to recenter the calendar window, if any.
  15922. (if (and org-calendar-follow-timestamp-change
  15923. (get-buffer-window "*Calendar*" t)
  15924. (memq org-ts-what '(day month year)))
  15925. (org-recenter-calendar (time-to-days time))))))
  15926. (defun org-modify-ts-extra (s pos n dm)
  15927. "Change the different parts of the lead-time and repeat fields in timestamp."
  15928. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15929. ng h m new rem)
  15930. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15931. (cond
  15932. ((or (org-pos-in-match-range pos 2)
  15933. (org-pos-in-match-range pos 3))
  15934. (setq m (string-to-number (match-string 3 s))
  15935. h (string-to-number (match-string 2 s)))
  15936. (if (org-pos-in-match-range pos 2)
  15937. (setq h (+ h n))
  15938. (setq n (* dm (org-no-warnings (signum n))))
  15939. (when (not (= 0 (setq rem (% m dm))))
  15940. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15941. (setq m (+ m n)))
  15942. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15943. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15944. (setq h (min 24 (max 0 h)))
  15945. (setq ng 1 new (format "-%02d:%02d" h m)))
  15946. ((org-pos-in-match-range pos 6)
  15947. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15948. ((org-pos-in-match-range pos 5)
  15949. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15950. ((org-pos-in-match-range pos 9)
  15951. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15952. ((org-pos-in-match-range pos 8)
  15953. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15954. (when ng
  15955. (setq s (concat
  15956. (substring s 0 (match-beginning ng))
  15957. new
  15958. (substring s (match-end ng))))))
  15959. s))
  15960. (defun org-recenter-calendar (date)
  15961. "If the calendar is visible, recenter it to DATE."
  15962. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15963. (when cwin
  15964. (let ((calendar-move-hook nil))
  15965. (with-selected-window cwin
  15966. (calendar-goto-date (if (listp date) date
  15967. (calendar-gregorian-from-absolute date))))))))
  15968. (defun org-goto-calendar (&optional arg)
  15969. "Go to the Emacs calendar at the current date.
  15970. If there is a time stamp in the current line, go to that date.
  15971. A prefix ARG can be used to force the current date."
  15972. (interactive "P")
  15973. (let ((tsr org-ts-regexp) diff
  15974. (calendar-move-hook nil)
  15975. (calendar-view-holidays-initially-flag nil)
  15976. (calendar-view-diary-initially-flag nil))
  15977. (if (or (org-at-timestamp-p)
  15978. (save-excursion
  15979. (beginning-of-line 1)
  15980. (looking-at (concat ".*" tsr))))
  15981. (let ((d1 (time-to-days (current-time)))
  15982. (d2 (time-to-days
  15983. (org-time-string-to-time (match-string 1)))))
  15984. (setq diff (- d2 d1))))
  15985. (calendar)
  15986. (calendar-goto-today)
  15987. (if (and diff (not arg)) (calendar-forward-day diff))))
  15988. (defun org-get-date-from-calendar ()
  15989. "Return a list (month day year) of date at point in calendar."
  15990. (with-current-buffer "*Calendar*"
  15991. (save-match-data
  15992. (calendar-cursor-to-date))))
  15993. (defun org-date-from-calendar ()
  15994. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15995. If there is already a time stamp at the cursor position, update it."
  15996. (interactive)
  15997. (if (org-at-timestamp-p t)
  15998. (org-timestamp-change 0 'calendar)
  15999. (let ((cal-date (org-get-date-from-calendar)))
  16000. (org-insert-time-stamp
  16001. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16002. (defcustom org-effort-durations
  16003. `(("h" . 60)
  16004. ("d" . ,(* 60 8))
  16005. ("w" . ,(* 60 8 5))
  16006. ("m" . ,(* 60 8 5 4))
  16007. ("y" . ,(* 60 8 5 40)))
  16008. "Conversion factor to minutes for an effort modifier.
  16009. Each entry has the form (MODIFIER . MINUTES).
  16010. In an effort string, a number followed by MODIFIER is multiplied
  16011. by the specified number of MINUTES to obtain an effort in
  16012. minutes.
  16013. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16014. effort string \"2hours\" is equivalent to 120 minutes."
  16015. :group 'org-agenda
  16016. :version "24.1"
  16017. :type '(alist :key-type (string :tag "Modifier")
  16018. :value-type (number :tag "Minutes")))
  16019. (defun org-minutes-to-clocksum-string (m)
  16020. "Format number of minutes as a clocksum string.
  16021. The format is determined by `org-time-clocksum-format',
  16022. `org-time-clocksum-use-fractional' and
  16023. `org-time-clocksum-fractional-format' and
  16024. `org-time-clocksum-use-effort-durations'."
  16025. (let ((clocksum "")
  16026. (m (round m)) ; Don't allow fractions of minutes
  16027. h d w mo y fmt n)
  16028. (setq h (if org-time-clocksum-use-effort-durations
  16029. (cdr (assoc "h" org-effort-durations)) 60)
  16030. d (if org-time-clocksum-use-effort-durations
  16031. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16032. w (if org-time-clocksum-use-effort-durations
  16033. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16034. mo (if org-time-clocksum-use-effort-durations
  16035. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16036. y (if org-time-clocksum-use-effort-durations
  16037. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16038. ;; fractional format
  16039. (if org-time-clocksum-use-fractional
  16040. (cond
  16041. ;; single format string
  16042. ((stringp org-time-clocksum-fractional-format)
  16043. (format org-time-clocksum-fractional-format (/ m (float h))))
  16044. ;; choice of fractional formats for different time units
  16045. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16046. (> (/ (truncate m) (* y d h)) 0))
  16047. (format fmt (/ m (* y d (float h)))))
  16048. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16049. (> (/ (truncate m) (* mo d h)) 0))
  16050. (format fmt (/ m (* mo d (float h)))))
  16051. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16052. (> (/ (truncate m) (* w d h)) 0))
  16053. (format fmt (/ m (* w d (float h)))))
  16054. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16055. (> (/ (truncate m) (* d h)) 0))
  16056. (format fmt (/ m (* d (float h)))))
  16057. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16058. (> (/ (truncate m) h) 0))
  16059. (format fmt (/ m (float h))))
  16060. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16061. (format fmt m))
  16062. ;; fall back to smallest time unit with a format
  16063. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16064. (format fmt (/ m (float h))))
  16065. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16066. (format fmt (/ m (* d (float h)))))
  16067. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16068. (format fmt (/ m (* w d (float h)))))
  16069. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16070. (format fmt (/ m (* mo d (float h)))))
  16071. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16072. (format fmt (/ m (* y d (float h))))))
  16073. ;; standard (non-fractional) format, with single format string
  16074. (if (stringp org-time-clocksum-format)
  16075. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16076. ;; separate formats components
  16077. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16078. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16079. (plist-get org-time-clocksum-format :require-years))
  16080. (setq clocksum (concat clocksum (format fmt n))
  16081. m (- m (* n y d h))))
  16082. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16083. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16084. (plist-get org-time-clocksum-format :require-months))
  16085. (setq clocksum (concat clocksum (format fmt n))
  16086. m (- m (* n mo d h))))
  16087. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16088. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16089. (plist-get org-time-clocksum-format :require-weeks))
  16090. (setq clocksum (concat clocksum (format fmt n))
  16091. m (- m (* n w d h))))
  16092. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16093. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16094. (plist-get org-time-clocksum-format :require-days))
  16095. (setq clocksum (concat clocksum (format fmt n))
  16096. m (- m (* n d h))))
  16097. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16098. (or (> (setq n (/ (truncate m) h)) 0)
  16099. (plist-get org-time-clocksum-format :require-hours))
  16100. (setq clocksum (concat clocksum (format fmt n))
  16101. m (- m (* n h))))
  16102. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16103. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16104. (setq clocksum (concat clocksum (format fmt m))))
  16105. ;; return formatted time duration
  16106. clocksum))))
  16107. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16108. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16109. "Org mode version 8.0")
  16110. (defun org-hours-to-clocksum-string (n)
  16111. (org-minutes-to-clocksum-string (* n 60)))
  16112. (defun org-hh:mm-string-to-minutes (s)
  16113. "Convert a string H:MM to a number of minutes.
  16114. If the string is just a number, interpret it as minutes.
  16115. In fact, the first hh:mm or number in the string will be taken,
  16116. there can be extra stuff in the string.
  16117. If no number is found, the return value is 0."
  16118. (cond
  16119. ((integerp s) s)
  16120. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16121. (+ (* (string-to-number (match-string 1 s)) 60)
  16122. (string-to-number (match-string 2 s))))
  16123. ((string-match "\\([0-9]+\\)" s)
  16124. (string-to-number (match-string 1 s)))
  16125. (t 0)))
  16126. (defcustom org-image-actual-width t
  16127. "Should we use the actual width of images when inlining them?
  16128. When set to `t', always use the image width.
  16129. When set to a number, use imagemagick (when available) to set
  16130. the image's width to this value.
  16131. When set to a number in a list, try to get the width from any
  16132. #+ATTR.* keyword if it matches a width specification like
  16133. #+ATTR_HTML: :width 300px
  16134. and fall back on that number if none is found.
  16135. When set to nil, try to get the width from an #+ATTR.* keyword
  16136. and fall back on the original width if none is found.
  16137. This requires Emacs >= 24.1, build with imagemagick support."
  16138. :group 'org-appearance
  16139. :version "24.4"
  16140. :package-version '(Org . "8.0")
  16141. :type '(choice
  16142. (const :tag "Use the image width" t)
  16143. (integer :tag "Use a number of pixels")
  16144. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16145. (const :tag "Use #+ATTR* or don't resize" nil)))
  16146. (defcustom org-agenda-inhibit-startup nil
  16147. "Inhibit startup when preparing agenda buffers.
  16148. When this variable is `t' (the default), the initialization of
  16149. the Org agenda buffers is inhibited: e.g. the visibility state
  16150. is not set, the tables are not re-aligned, etc."
  16151. :type 'boolean
  16152. :version "24.3"
  16153. :group 'org-agenda)
  16154. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16155. "Convert a duration string S to minutes.
  16156. A bare number is interpreted as minutes, modifiers can be set by
  16157. customizing `org-effort-durations' (which see).
  16158. Entries containing a colon are interpreted as H:MM by
  16159. `org-hh:mm-string-to-minutes'."
  16160. (let ((result 0)
  16161. (re (concat "\\([0-9.]+\\) *\\("
  16162. (regexp-opt (mapcar 'car org-effort-durations))
  16163. "\\)")))
  16164. (while (string-match re s)
  16165. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16166. (string-to-number (match-string 1 s))))
  16167. (setq s (replace-match "" nil t s)))
  16168. (setq result (floor result))
  16169. (incf result (org-hh:mm-string-to-minutes s))
  16170. (if output-to-string (number-to-string result) result)))
  16171. ;;;; Files
  16172. (defun org-save-all-org-buffers ()
  16173. "Save all Org-mode buffers without user confirmation."
  16174. (interactive)
  16175. (message "Saving all Org-mode buffers...")
  16176. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16177. (when (featurep 'org-id) (org-id-locations-save))
  16178. (message "Saving all Org-mode buffers... done"))
  16179. (defun org-revert-all-org-buffers ()
  16180. "Revert all Org-mode buffers.
  16181. Prompt for confirmation when there are unsaved changes.
  16182. Be sure you know what you are doing before letting this function
  16183. overwrite your changes.
  16184. This function is useful in a setup where one tracks org files
  16185. with a version control system, to revert on one machine after pulling
  16186. changes from another. I believe the procedure must be like this:
  16187. 1. M-x org-save-all-org-buffers
  16188. 2. Pull changes from the other machine, resolve conflicts
  16189. 3. M-x org-revert-all-org-buffers"
  16190. (interactive)
  16191. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16192. (error "Abort"))
  16193. (save-excursion
  16194. (save-window-excursion
  16195. (mapc
  16196. (lambda (b)
  16197. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16198. (with-current-buffer b buffer-file-name))
  16199. (org-pop-to-buffer-same-window b)
  16200. (revert-buffer t 'no-confirm)))
  16201. (buffer-list))
  16202. (when (and (featurep 'org-id) org-id-track-globally)
  16203. (org-id-locations-load)))))
  16204. ;;;; Agenda files
  16205. ;;;###autoload
  16206. (defun org-switchb (&optional arg)
  16207. "Switch between Org buffers.
  16208. With one prefix argument, restrict available buffers to files.
  16209. With two prefix arguments, restrict available buffers to agenda files.
  16210. Defaults to `iswitchb' for buffer name completion.
  16211. Set `org-completion-use-ido' to make it use ido instead."
  16212. (interactive "P")
  16213. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16214. ((equal arg '(16)) (org-buffer-list 'agenda))
  16215. (t (org-buffer-list))))
  16216. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16217. (org-completion-use-ido org-completion-use-ido))
  16218. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16219. (setq org-completion-use-iswitchb t))
  16220. (org-pop-to-buffer-same-window
  16221. (org-icompleting-read "Org buffer: "
  16222. (mapcar 'list (mapcar 'buffer-name blist))
  16223. nil t))))
  16224. ;;; Define some older names previously used for this functionality
  16225. ;;;###autoload
  16226. (defalias 'org-ido-switchb 'org-switchb)
  16227. ;;;###autoload
  16228. (defalias 'org-iswitchb 'org-switchb)
  16229. (defun org-buffer-list (&optional predicate exclude-tmp)
  16230. "Return a list of Org buffers.
  16231. PREDICATE can be `export', `files' or `agenda'.
  16232. export restrict the list to Export buffers.
  16233. files restrict the list to buffers visiting Org files.
  16234. agenda restrict the list to buffers visiting agenda files.
  16235. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16236. (let* ((bfn nil)
  16237. (agenda-files (and (eq predicate 'agenda)
  16238. (mapcar 'file-truename (org-agenda-files t))))
  16239. (filter
  16240. (cond
  16241. ((eq predicate 'files)
  16242. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16243. ((eq predicate 'export)
  16244. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16245. ((eq predicate 'agenda)
  16246. (lambda (b)
  16247. (with-current-buffer b
  16248. (and (derived-mode-p 'org-mode)
  16249. (setq bfn (buffer-file-name b))
  16250. (member (file-truename bfn) agenda-files)))))
  16251. (t (lambda (b) (with-current-buffer b
  16252. (or (derived-mode-p 'org-mode)
  16253. (string-match "\*Org .*Export"
  16254. (buffer-name b)))))))))
  16255. (delq nil
  16256. (mapcar
  16257. (lambda(b)
  16258. (if (and (funcall filter b)
  16259. (or (not exclude-tmp)
  16260. (not (string-match "tmp" (buffer-name b)))))
  16261. b
  16262. nil))
  16263. (buffer-list)))))
  16264. (defun org-agenda-files (&optional unrestricted archives)
  16265. "Get the list of agenda files.
  16266. Optional UNRESTRICTED means return the full list even if a restriction
  16267. is currently in place.
  16268. When ARCHIVES is t, include all archive files that are really being
  16269. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16270. `org-agenda-archives-mode' is t."
  16271. (let ((files
  16272. (cond
  16273. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16274. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16275. ((listp org-agenda-files) org-agenda-files)
  16276. (t (error "Invalid value of `org-agenda-files'")))))
  16277. (setq files (apply 'append
  16278. (mapcar (lambda (f)
  16279. (if (file-directory-p f)
  16280. (directory-files
  16281. f t org-agenda-file-regexp)
  16282. (list f)))
  16283. files)))
  16284. (when org-agenda-skip-unavailable-files
  16285. (setq files (delq nil
  16286. (mapcar (function
  16287. (lambda (file)
  16288. (and (file-readable-p file) file)))
  16289. files))))
  16290. (when (or (eq archives t)
  16291. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16292. (setq files (org-add-archive-files files)))
  16293. files))
  16294. (defun org-agenda-file-p (&optional file)
  16295. "Return non-nil, if FILE is an agenda file.
  16296. If FILE is omitted, use the file associated with the current
  16297. buffer."
  16298. (member (or file (buffer-file-name))
  16299. (org-agenda-files t)))
  16300. (defun org-edit-agenda-file-list ()
  16301. "Edit the list of agenda files.
  16302. Depending on setup, this either uses customize to edit the variable
  16303. `org-agenda-files', or it visits the file that is holding the list. In the
  16304. latter case, the buffer is set up in a way that saving it automatically kills
  16305. the buffer and restores the previous window configuration."
  16306. (interactive)
  16307. (if (stringp org-agenda-files)
  16308. (let ((cw (current-window-configuration)))
  16309. (find-file org-agenda-files)
  16310. (org-set-local 'org-window-configuration cw)
  16311. (org-add-hook 'after-save-hook
  16312. (lambda ()
  16313. (set-window-configuration
  16314. (prog1 org-window-configuration
  16315. (kill-buffer (current-buffer))))
  16316. (org-install-agenda-files-menu)
  16317. (message "New agenda file list installed"))
  16318. nil 'local)
  16319. (message "%s" (substitute-command-keys
  16320. "Edit list and finish with \\[save-buffer]")))
  16321. (customize-variable 'org-agenda-files)))
  16322. (defun org-store-new-agenda-file-list (list)
  16323. "Set new value for the agenda file list and save it correctly."
  16324. (if (stringp org-agenda-files)
  16325. (let ((fe (org-read-agenda-file-list t)) b u)
  16326. (while (setq b (find-buffer-visiting org-agenda-files))
  16327. (kill-buffer b))
  16328. (with-temp-file org-agenda-files
  16329. (insert
  16330. (mapconcat
  16331. (lambda (f) ;; Keep un-expanded entries.
  16332. (if (setq u (assoc f fe))
  16333. (cdr u)
  16334. f))
  16335. list "\n")
  16336. "\n")))
  16337. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16338. (org-insert-mode-line-in-empty-file nil))
  16339. (setq org-agenda-files list)
  16340. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16341. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16342. "Read the list of agenda files from a file.
  16343. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16344. filenames, used by `org-store-new-agenda-file-list' to write back
  16345. un-expanded file names."
  16346. (when (file-directory-p org-agenda-files)
  16347. (error "`org-agenda-files' cannot be a single directory"))
  16348. (when (stringp org-agenda-files)
  16349. (with-temp-buffer
  16350. (insert-file-contents org-agenda-files)
  16351. (mapcar
  16352. (lambda (f)
  16353. (let ((e (expand-file-name (substitute-in-file-name f)
  16354. org-directory)))
  16355. (if pair-with-expansion
  16356. (cons e f)
  16357. e)))
  16358. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16359. ;;;###autoload
  16360. (defun org-cycle-agenda-files ()
  16361. "Cycle through the files in `org-agenda-files'.
  16362. If the current buffer visits an agenda file, find the next one in the list.
  16363. If the current buffer does not, find the first agenda file."
  16364. (interactive)
  16365. (let* ((fs (org-agenda-files t))
  16366. (files (append fs (list (car fs))))
  16367. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16368. file)
  16369. (unless files (error "No agenda files"))
  16370. (catch 'exit
  16371. (while (setq file (pop files))
  16372. (if (equal (file-truename file) tcf)
  16373. (when (car files)
  16374. (find-file (car files))
  16375. (throw 'exit t))))
  16376. (find-file (car fs)))
  16377. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16378. (defun org-agenda-file-to-front (&optional to-end)
  16379. "Move/add the current file to the top of the agenda file list.
  16380. If the file is not present in the list, it is added to the front. If it is
  16381. present, it is moved there. With optional argument TO-END, add/move to the
  16382. end of the list."
  16383. (interactive "P")
  16384. (let ((org-agenda-skip-unavailable-files nil)
  16385. (file-alist (mapcar (lambda (x)
  16386. (cons (file-truename x) x))
  16387. (org-agenda-files t)))
  16388. (ctf (file-truename
  16389. (or buffer-file-name
  16390. (error "Please save the current buffer to a file"))))
  16391. x had)
  16392. (setq x (assoc ctf file-alist) had x)
  16393. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16394. (if to-end
  16395. (setq file-alist (append (delq x file-alist) (list x)))
  16396. (setq file-alist (cons x (delq x file-alist))))
  16397. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16398. (org-install-agenda-files-menu)
  16399. (message "File %s to %s of agenda file list"
  16400. (if had "moved" "added") (if to-end "end" "front"))))
  16401. (defun org-remove-file (&optional file)
  16402. "Remove current file from the list of files in variable `org-agenda-files'.
  16403. These are the files which are being checked for agenda entries.
  16404. Optional argument FILE means use this file instead of the current."
  16405. (interactive)
  16406. (let* ((org-agenda-skip-unavailable-files nil)
  16407. (file (or file buffer-file-name
  16408. (error "Current buffer does not visit a file")))
  16409. (true-file (file-truename file))
  16410. (afile (abbreviate-file-name file))
  16411. (files (delq nil (mapcar
  16412. (lambda (x)
  16413. (if (equal true-file
  16414. (file-truename x))
  16415. nil x))
  16416. (org-agenda-files t)))))
  16417. (if (not (= (length files) (length (org-agenda-files t))))
  16418. (progn
  16419. (org-store-new-agenda-file-list files)
  16420. (org-install-agenda-files-menu)
  16421. (message "Removed file: %s" afile))
  16422. (message "File was not in list: %s (not removed)" afile))))
  16423. (defun org-file-menu-entry (file)
  16424. (vector file (list 'find-file file) t))
  16425. (defun org-check-agenda-file (file)
  16426. "Make sure FILE exists. If not, ask user what to do."
  16427. (when (not (file-exists-p file))
  16428. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16429. (abbreviate-file-name file))
  16430. (let ((r (downcase (read-char-exclusive))))
  16431. (cond
  16432. ((equal r ?r)
  16433. (org-remove-file file)
  16434. (throw 'nextfile t))
  16435. (t (error "Abort"))))))
  16436. (defun org-get-agenda-file-buffer (file)
  16437. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16438. it to the list of buffers which might be released later."
  16439. (let ((buf (org-find-base-buffer-visiting file)))
  16440. (if buf
  16441. buf ; just return it
  16442. ;; Make a new buffer and remember it
  16443. (setq buf (find-file-noselect file))
  16444. (if buf (push buf org-agenda-new-buffers))
  16445. buf)))
  16446. (defun org-release-buffers (blist)
  16447. "Release all buffers in list, asking the user for confirmation when needed.
  16448. When a buffer is unmodified, it is just killed. When modified, it is saved
  16449. \(if the user agrees) and then killed."
  16450. (let (buf file)
  16451. (while (setq buf (pop blist))
  16452. (setq file (buffer-file-name buf))
  16453. (when (and (buffer-modified-p buf)
  16454. file
  16455. (y-or-n-p (format "Save file %s? " file)))
  16456. (with-current-buffer buf (save-buffer)))
  16457. (kill-buffer buf))))
  16458. (defun org-agenda-prepare-buffers (files)
  16459. "Create buffers for all agenda files, protect archived trees and comments."
  16460. (interactive)
  16461. (let ((pa '(:org-archived t))
  16462. (pc '(:org-comment t))
  16463. (pall '(:org-archived t :org-comment t))
  16464. (inhibit-read-only t)
  16465. (org-inhibit-startup org-agenda-inhibit-startup)
  16466. (rea (concat ":" org-archive-tag ":"))
  16467. file re)
  16468. (setq org-tag-alist-for-agenda nil
  16469. org-tag-groups-alist-for-agenda nil)
  16470. (save-excursion
  16471. (save-restriction
  16472. (while (setq file (pop files))
  16473. (catch 'nextfile
  16474. (if (bufferp file)
  16475. (set-buffer file)
  16476. (org-check-agenda-file file)
  16477. (set-buffer (org-get-agenda-file-buffer file)))
  16478. (widen)
  16479. (org-set-regexps-and-options-for-tags)
  16480. (org-refresh-category-properties)
  16481. (org-refresh-properties org-effort-property 'org-effort)
  16482. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16483. (setq org-todo-keywords-for-agenda
  16484. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16485. (setq org-done-keywords-for-agenda
  16486. (append org-done-keywords-for-agenda org-done-keywords))
  16487. (setq org-todo-keyword-alist-for-agenda
  16488. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16489. (setq org-drawers-for-agenda
  16490. (append org-drawers-for-agenda org-drawers))
  16491. (unless (equal org-tag-alist-for-agenda org-tag-alist)
  16492. (setq org-tag-alist-for-agenda
  16493. (append org-tag-alist-for-agenda org-tag-alist)))
  16494. (if org-group-tags
  16495. (setq org-tag-groups-alist-for-agenda
  16496. (org-uniquify-alist
  16497. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  16498. (org-with-silent-modifications
  16499. (save-excursion
  16500. (remove-text-properties (point-min) (point-max) pall)
  16501. (when org-agenda-skip-archived-trees
  16502. (goto-char (point-min))
  16503. (while (re-search-forward rea nil t)
  16504. (if (org-at-heading-p t)
  16505. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16506. (goto-char (point-min))
  16507. (setq re (format org-heading-keyword-regexp-format
  16508. org-comment-string))
  16509. (while (re-search-forward re nil t)
  16510. (add-text-properties
  16511. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16512. (setq org-todo-keywords-for-agenda
  16513. (org-uniquify org-todo-keywords-for-agenda))
  16514. (setq org-todo-keyword-alist-for-agenda
  16515. (org-uniquify org-todo-keyword-alist-for-agenda))))
  16516. ;;;; CDLaTeX minor mode
  16517. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16518. "Keymap for the minor `org-cdlatex-mode'.")
  16519. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16520. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16521. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16522. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16523. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16524. (defvar org-cdlatex-texmathp-advice-is-done nil
  16525. "Flag remembering if we have applied the advice to texmathp already.")
  16526. (define-minor-mode org-cdlatex-mode
  16527. "Toggle the minor `org-cdlatex-mode'.
  16528. This mode supports entering LaTeX environment and math in LaTeX fragments
  16529. in Org-mode.
  16530. \\{org-cdlatex-mode-map}"
  16531. nil " OCDL" nil
  16532. (when org-cdlatex-mode
  16533. (require 'cdlatex)
  16534. (run-hooks 'cdlatex-mode-hook)
  16535. (cdlatex-compute-tables))
  16536. (unless org-cdlatex-texmathp-advice-is-done
  16537. (setq org-cdlatex-texmathp-advice-is-done t)
  16538. (defadvice texmathp (around org-math-always-on activate)
  16539. "Always return t in org-mode buffers.
  16540. This is because we want to insert math symbols without dollars even outside
  16541. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16542. an embedded LaTeX fragment, let texmathp do its job.
  16543. \\[org-cdlatex-mode-map]"
  16544. (interactive)
  16545. (let (p)
  16546. (cond
  16547. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16548. ((eq this-command 'cdlatex-math-symbol)
  16549. (setq ad-return-value t
  16550. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16551. (t
  16552. (let ((p (org-inside-LaTeX-fragment-p)))
  16553. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16554. (setq ad-return-value t
  16555. texmathp-why '("Org-mode embedded math" . 0))
  16556. (if p ad-do-it)))))))))
  16557. (defun turn-on-org-cdlatex ()
  16558. "Unconditionally turn on `org-cdlatex-mode'."
  16559. (org-cdlatex-mode 1))
  16560. (defun org-try-cdlatex-tab ()
  16561. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16562. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16563. - inside a LaTeX fragment, or
  16564. - after the first word in a line, where an abbreviation expansion could
  16565. insert a LaTeX environment."
  16566. (when org-cdlatex-mode
  16567. (cond
  16568. ;; Before any word on the line: No expansion possible.
  16569. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16570. ;; Just after first word on the line: Expand it. Make sure it
  16571. ;; cannot happen on headlines, though.
  16572. ((save-excursion
  16573. (skip-chars-backward "a-zA-Z0-9*")
  16574. (skip-chars-backward " \t")
  16575. (and (bolp) (not (org-at-heading-p))))
  16576. (cdlatex-tab) t)
  16577. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16578. (defun org-cdlatex-underscore-caret (&optional arg)
  16579. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16580. Revert to the normal definition outside of these fragments."
  16581. (interactive "P")
  16582. (if (org-inside-LaTeX-fragment-p)
  16583. (call-interactively 'cdlatex-sub-superscript)
  16584. (let (org-cdlatex-mode)
  16585. (call-interactively (key-binding (vector last-input-event))))))
  16586. (defun org-cdlatex-math-modify (&optional arg)
  16587. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16588. Revert to the normal definition outside of these fragments."
  16589. (interactive "P")
  16590. (if (org-inside-LaTeX-fragment-p)
  16591. (call-interactively 'cdlatex-math-modify)
  16592. (let (org-cdlatex-mode)
  16593. (call-interactively (key-binding (vector last-input-event))))))
  16594. ;;;; LaTeX fragments
  16595. (defvar org-latex-regexps
  16596. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16597. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16598. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16599. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16600. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16601. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16602. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16603. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16604. "Regular expressions for matching embedded LaTeX.")
  16605. (defun org-inside-LaTeX-fragment-p ()
  16606. "Test if point is inside a LaTeX fragment.
  16607. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16608. sequence appearing also before point.
  16609. Even though the matchers for math are configurable, this function assumes
  16610. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16611. delimiters are skipped when they have been removed by customization.
  16612. The return value is nil, or a cons cell with the delimiter and the
  16613. position of this delimiter.
  16614. This function does a reasonably good job, but can locally be fooled by
  16615. for example currency specifications. For example it will assume being in
  16616. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16617. fragments that are properly closed, but during editing, we have to live
  16618. with the uncertainty caused by missing closing delimiters. This function
  16619. looks only before point, not after."
  16620. (catch 'exit
  16621. (let ((pos (point))
  16622. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16623. (lim (progn
  16624. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16625. (point)))
  16626. dd-on str (start 0) m re)
  16627. (goto-char pos)
  16628. (when dodollar
  16629. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16630. re (nth 1 (assoc "$" org-latex-regexps)))
  16631. (while (string-match re str start)
  16632. (cond
  16633. ((= (match-end 0) (length str))
  16634. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16635. ((= (match-end 0) (- (length str) 5))
  16636. (throw 'exit nil))
  16637. (t (setq start (match-end 0))))))
  16638. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16639. (goto-char pos)
  16640. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16641. (and (match-beginning 2) (throw 'exit nil))
  16642. ;; count $$
  16643. (while (re-search-backward "\\$\\$" lim t)
  16644. (setq dd-on (not dd-on)))
  16645. (goto-char pos)
  16646. (if dd-on (cons "$$" m))))))
  16647. (defun org-inside-latex-macro-p ()
  16648. "Is point inside a LaTeX macro or its arguments?"
  16649. (save-match-data
  16650. (org-in-regexp
  16651. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16652. (defvar org-latex-fragment-image-overlays nil
  16653. "List of overlays carrying the images of latex fragments.")
  16654. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16655. (defun org-remove-latex-fragment-image-overlays ()
  16656. "Remove all overlays with LaTeX fragment images in current buffer."
  16657. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16658. (setq org-latex-fragment-image-overlays nil))
  16659. (defun org-preview-latex-fragment (&optional subtree)
  16660. "Preview the LaTeX fragment at point, or all locally or globally.
  16661. If the cursor is in a LaTeX fragment, create the image and overlay
  16662. it over the source code. If there is no fragment at point, display
  16663. all fragments in the current text, from one headline to the next. With
  16664. prefix SUBTREE, display all fragments in the current subtree. With a
  16665. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16666. the cursor is before the first headline,
  16667. display all fragments in the buffer.
  16668. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16669. (interactive "P")
  16670. (unless buffer-file-name
  16671. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16672. (org-remove-latex-fragment-image-overlays)
  16673. (save-excursion
  16674. (save-restriction
  16675. (let (beg end at msg)
  16676. (cond
  16677. ((or (equal subtree '(16))
  16678. (not (save-excursion
  16679. (re-search-backward org-outline-regexp-bol nil t))))
  16680. (setq beg (point-min) end (point-max)
  16681. msg "Creating images for buffer...%s"))
  16682. ((equal subtree '(4))
  16683. (org-back-to-heading)
  16684. (setq beg (point) end (org-end-of-subtree t)
  16685. msg "Creating images for subtree...%s"))
  16686. (t
  16687. (if (setq at (org-inside-LaTeX-fragment-p))
  16688. (goto-char (max (point-min) (- (cdr at) 2)))
  16689. (org-back-to-heading))
  16690. (setq beg (point) end (progn (outline-next-heading) (point))
  16691. msg (if at "Creating image...%s"
  16692. "Creating images for entry...%s"))))
  16693. (message msg "")
  16694. (narrow-to-region beg end)
  16695. (goto-char beg)
  16696. (org-format-latex
  16697. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16698. (file-name-nondirectory
  16699. buffer-file-name)))
  16700. default-directory 'overlays msg at 'forbuffer
  16701. org-latex-create-formula-image-program)
  16702. (message msg "done. Use `C-c C-c' to remove images.")))))
  16703. (defun org-format-latex (prefix &optional dir overlays msg at
  16704. forbuffer processing-type)
  16705. "Replace LaTeX fragments with links to an image, and produce images.
  16706. Some of the options can be changed using the variable
  16707. `org-format-latex-options'."
  16708. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16709. (let* ((prefixnodir (file-name-nondirectory prefix))
  16710. (absprefix (expand-file-name prefix dir))
  16711. (todir (file-name-directory absprefix))
  16712. (opt org-format-latex-options)
  16713. (optnew org-format-latex-options)
  16714. (matchers (plist-get opt :matchers))
  16715. (re-list org-latex-regexps)
  16716. (cnt 0) txt hash link beg end re e checkdir
  16717. string
  16718. m n block-type block linkfile movefile ov)
  16719. ;; Check the different regular expressions
  16720. (while (setq e (pop re-list))
  16721. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16722. block (if block-type "\n\n" ""))
  16723. (when (member m matchers)
  16724. (goto-char (point-min))
  16725. (while (re-search-forward re nil t)
  16726. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16727. (or (not overlays)
  16728. (not (eq (get-char-property (match-beginning n)
  16729. 'org-overlay-type)
  16730. 'org-latex-overlay))))
  16731. (cond
  16732. ((eq processing-type 'verbatim))
  16733. ((eq processing-type 'mathjax)
  16734. ;; Prepare for MathJax processing.
  16735. (setq string (match-string n))
  16736. (when (member m '("$" "$1"))
  16737. (save-excursion
  16738. (delete-region (match-beginning n) (match-end n))
  16739. (goto-char (match-beginning n))
  16740. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16741. ((or (eq processing-type 'dvipng)
  16742. (eq processing-type 'imagemagick))
  16743. ;; Process to an image.
  16744. (setq txt (match-string n)
  16745. beg (match-beginning n) end (match-end n)
  16746. cnt (1+ cnt))
  16747. (let ((face (face-at-point))
  16748. (fg (plist-get opt :foreground))
  16749. (bg (plist-get opt :background))
  16750. ;; Ensure full list is printed.
  16751. print-length print-level)
  16752. (when forbuffer
  16753. ;; Get the colors from the face at point.
  16754. (goto-char beg)
  16755. (when (eq fg 'auto)
  16756. (setq fg (face-attribute face :foreground nil 'default)))
  16757. (when (eq bg 'auto)
  16758. (setq bg (face-attribute face :background nil 'default)))
  16759. (setq optnew (copy-sequence opt))
  16760. (plist-put optnew :foreground fg)
  16761. (plist-put optnew :background bg))
  16762. (setq hash (sha1 (prin1-to-string
  16763. (list org-format-latex-header
  16764. org-latex-default-packages-alist
  16765. org-latex-packages-alist
  16766. org-format-latex-options
  16767. forbuffer txt fg bg)))
  16768. linkfile (format "%s_%s.png" prefix hash)
  16769. movefile (format "%s_%s.png" absprefix hash)))
  16770. (setq link (concat block "[[file:" linkfile "]]" block))
  16771. (if msg (message msg cnt))
  16772. (goto-char beg)
  16773. (unless checkdir ; Ensure the directory exists.
  16774. (setq checkdir t)
  16775. (or (file-directory-p todir) (make-directory todir t)))
  16776. (unless (file-exists-p movefile)
  16777. (org-create-formula-image
  16778. txt movefile optnew forbuffer processing-type))
  16779. (if overlays
  16780. (progn
  16781. (mapc (lambda (o)
  16782. (if (eq (overlay-get o 'org-overlay-type)
  16783. 'org-latex-overlay)
  16784. (delete-overlay o)))
  16785. (overlays-in beg end))
  16786. (setq ov (make-overlay beg end))
  16787. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16788. (if (featurep 'xemacs)
  16789. (progn
  16790. (overlay-put ov 'invisible t)
  16791. (overlay-put
  16792. ov 'end-glyph
  16793. (make-glyph (vector 'png :file movefile))))
  16794. (overlay-put
  16795. ov 'display
  16796. (list 'image :type 'png :file movefile :ascent 'center)))
  16797. (push ov org-latex-fragment-image-overlays)
  16798. (goto-char end))
  16799. (delete-region beg end)
  16800. (insert (org-add-props link
  16801. (list 'org-latex-src
  16802. (replace-regexp-in-string
  16803. "\"" "" txt)
  16804. 'org-latex-src-embed-type
  16805. (if block-type 'paragraph 'character))))))
  16806. ((eq processing-type 'mathml)
  16807. ;; Process to MathML
  16808. (unless (save-match-data (org-format-latex-mathml-available-p))
  16809. (error "LaTeX to MathML converter not configured"))
  16810. (setq txt (match-string n)
  16811. beg (match-beginning n) end (match-end n)
  16812. cnt (1+ cnt))
  16813. (if msg (message msg cnt))
  16814. (goto-char beg)
  16815. (delete-region beg end)
  16816. (insert (org-format-latex-as-mathml
  16817. txt block-type prefix dir)))
  16818. (t
  16819. (error "Unknown conversion type %s for latex fragments"
  16820. processing-type)))))))))
  16821. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16822. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16823. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16824. sucessful, return the portion between \"<math...> </math>\"
  16825. elements otherwise return nil. When MATHML-FILE is specified,
  16826. write the results in to that file. When invoked as an
  16827. interactive command, prompt for LATEX-FRAG, with initial value
  16828. set to the current active region and echo the results for user
  16829. inspection."
  16830. (interactive (list (let ((frag (when (org-region-active-p)
  16831. (buffer-substring-no-properties
  16832. (region-beginning) (region-end)))))
  16833. (read-string "LaTeX Fragment: " frag nil frag))))
  16834. (unless latex-frag (error "Invalid latex-frag"))
  16835. (let* ((tmp-in-file (file-relative-name
  16836. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16837. (ignore (write-region latex-frag nil tmp-in-file))
  16838. (tmp-out-file (file-relative-name
  16839. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16840. (cmd (format-spec
  16841. org-latex-to-mathml-convert-command
  16842. `((?j . ,(shell-quote-argument
  16843. (expand-file-name org-latex-to-mathml-jar-file)))
  16844. (?I . ,(shell-quote-argument tmp-in-file))
  16845. (?o . ,(shell-quote-argument tmp-out-file)))))
  16846. mathml shell-command-output)
  16847. (when (org-called-interactively-p 'any)
  16848. (unless (org-format-latex-mathml-available-p)
  16849. (error "LaTeX to MathML converter not configured")))
  16850. (message "Running %s" cmd)
  16851. (setq shell-command-output (shell-command-to-string cmd))
  16852. (setq mathml
  16853. (when (file-readable-p tmp-out-file)
  16854. (with-current-buffer (find-file-noselect tmp-out-file t)
  16855. (goto-char (point-min))
  16856. (when (re-search-forward
  16857. (concat
  16858. (regexp-quote
  16859. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16860. "\\(.\\|\n\\)*"
  16861. (regexp-quote "</math>")) nil t)
  16862. (prog1 (match-string 0) (kill-buffer))))))
  16863. (cond
  16864. (mathml
  16865. (setq mathml
  16866. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16867. (when mathml-file
  16868. (write-region mathml nil mathml-file))
  16869. (when (org-called-interactively-p 'any)
  16870. (message mathml)))
  16871. ((message "LaTeX to MathML conversion failed")
  16872. (message shell-command-output)))
  16873. (delete-file tmp-in-file)
  16874. (when (file-exists-p tmp-out-file)
  16875. (delete-file tmp-out-file))
  16876. mathml))
  16877. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16878. prefix &optional dir)
  16879. "Use `org-create-math-formula' but check local cache first."
  16880. (let* ((absprefix (expand-file-name prefix dir))
  16881. (print-length nil) (print-level nil)
  16882. (formula-id (concat
  16883. "formula-"
  16884. (sha1
  16885. (prin1-to-string
  16886. (list latex-frag
  16887. org-latex-to-mathml-convert-command)))))
  16888. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16889. (formula-cache-dir (file-name-directory formula-cache)))
  16890. (unless (file-directory-p formula-cache-dir)
  16891. (make-directory formula-cache-dir t))
  16892. (unless (file-exists-p formula-cache)
  16893. (org-create-math-formula latex-frag formula-cache))
  16894. (if (file-exists-p formula-cache)
  16895. ;; Successful conversion. Return the link to MathML file.
  16896. (org-add-props
  16897. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16898. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16899. 'org-latex-src-embed-type (if latex-frag-type
  16900. 'paragraph 'character)))
  16901. ;; Failed conversion. Return the LaTeX fragment verbatim
  16902. latex-frag)))
  16903. (defun org-create-formula-image (string tofile options buffer &optional type)
  16904. "Create an image from LaTeX source using dvipng or convert.
  16905. This function calls either `org-create-formula-image-with-dvipng'
  16906. or `org-create-formula-image-with-imagemagick' depending on the
  16907. value of `org-latex-create-formula-image-program' or on the value
  16908. of the optional TYPE variable.
  16909. Note: ultimately these two function should be combined as they
  16910. share a good deal of logic."
  16911. (org-check-external-command
  16912. "latex" "needed to convert LaTeX fragments to images")
  16913. (funcall
  16914. (case (or type org-latex-create-formula-image-program)
  16915. ('dvipng
  16916. (org-check-external-command
  16917. "dvipng" "needed to convert LaTeX fragments to images")
  16918. #'org-create-formula-image-with-dvipng)
  16919. ('imagemagick
  16920. (org-check-external-command
  16921. "convert" "you need to install imagemagick")
  16922. #'org-create-formula-image-with-imagemagick)
  16923. (t (error
  16924. "invalid value of `org-latex-create-formula-image-program'")))
  16925. string tofile options buffer))
  16926. (declare-function org-export--get-global-options "ox" (&optional backend))
  16927. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  16928. (defun org-create-formula--latex-header ()
  16929. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  16930. (org-latex-guess-inputenc
  16931. (org-splice-latex-header
  16932. org-format-latex-header
  16933. org-latex-default-packages-alist
  16934. org-latex-packages-alist t
  16935. (plist-get
  16936. (org-combine-plists
  16937. (org-export--get-global-options 'latex)
  16938. (org-export--get-inbuffer-options 'latex))
  16939. :latex-header))))
  16940. ;; This function borrows from Ganesh Swami's latex2png.el
  16941. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16942. "This calls dvipng."
  16943. (require 'ox-latex)
  16944. (let* ((tmpdir (if (featurep 'xemacs)
  16945. (temp-directory)
  16946. temporary-file-directory))
  16947. (texfilebase (make-temp-name
  16948. (expand-file-name "orgtex" tmpdir)))
  16949. (texfile (concat texfilebase ".tex"))
  16950. (dvifile (concat texfilebase ".dvi"))
  16951. (pngfile (concat texfilebase ".png"))
  16952. (fnh (if (featurep 'xemacs)
  16953. (font-height (face-font 'default))
  16954. (face-attribute 'default :height nil)))
  16955. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16956. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16957. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16958. "Black"))
  16959. (bg (or (plist-get options (if buffer :background :html-background))
  16960. "Transparent")))
  16961. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16962. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16963. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16964. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16965. (let ((latex-header (org-create-formula--latex-header)))
  16966. (with-temp-file texfile
  16967. (insert latex-header)
  16968. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  16969. (let ((dir default-directory))
  16970. (condition-case nil
  16971. (progn
  16972. (cd tmpdir)
  16973. (call-process "latex" nil nil nil texfile))
  16974. (error nil))
  16975. (cd dir))
  16976. (if (not (file-exists-p dvifile))
  16977. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16978. (condition-case nil
  16979. (if (featurep 'xemacs)
  16980. (call-process "dvipng" nil nil nil
  16981. "-fg" fg "-bg" bg
  16982. "-T" "tight"
  16983. "-o" pngfile
  16984. dvifile)
  16985. (call-process "dvipng" nil nil nil
  16986. "-fg" fg "-bg" bg
  16987. "-D" dpi
  16988. ;;"-x" scale "-y" scale
  16989. "-T" "tight"
  16990. "-o" pngfile
  16991. dvifile))
  16992. (error nil))
  16993. (if (not (file-exists-p pngfile))
  16994. (if org-format-latex-signal-error
  16995. (error "Failed to create png file from %s" texfile)
  16996. (message "Failed to create png file from %s" texfile)
  16997. nil)
  16998. ;; Use the requested file name and clean up
  16999. (copy-file pngfile tofile 'replace)
  17000. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17001. (if (file-exists-p (concat texfilebase e))
  17002. (delete-file (concat texfilebase e))))
  17003. pngfile))))
  17004. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17005. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17006. "This calls convert, which is included into imagemagick."
  17007. (require 'ox-latex)
  17008. (let* ((tmpdir (if (featurep 'xemacs)
  17009. (temp-directory)
  17010. temporary-file-directory))
  17011. (texfilebase (make-temp-name
  17012. (expand-file-name "orgtex" tmpdir)))
  17013. (texfile (concat texfilebase ".tex"))
  17014. (pdffile (concat texfilebase ".pdf"))
  17015. (pngfile (concat texfilebase ".png"))
  17016. (fnh (if (featurep 'xemacs)
  17017. (font-height (face-font 'default))
  17018. (face-attribute 'default :height nil)))
  17019. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17020. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  17021. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17022. "black"))
  17023. (bg (or (plist-get options (if buffer :background :html-background))
  17024. "white")))
  17025. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17026. (setq fg (org-latex-color-format fg)))
  17027. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17028. (setq bg (org-latex-color-format
  17029. (if (string= bg "Transparent") "white" bg))))
  17030. (let ((latex-header (org-create-formula--latex-header)))
  17031. (with-temp-file texfile
  17032. (insert latex-header)
  17033. (insert "\n\\begin{document}\n"
  17034. "\\definecolor{fg}{rgb}{" fg "}\n"
  17035. "\\definecolor{bg}{rgb}{" bg "}\n"
  17036. "\n\\pagecolor{bg}\n"
  17037. "\n{\\color{fg}\n"
  17038. string
  17039. "\n}\n"
  17040. "\n\\end{document}\n")))
  17041. (org-latex-compile texfile t)
  17042. (if (not (file-exists-p pdffile))
  17043. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17044. (condition-case nil
  17045. (if (featurep 'xemacs)
  17046. (call-process "convert" nil nil nil
  17047. "-density" "96"
  17048. "-trim"
  17049. "-antialias"
  17050. pdffile
  17051. "-quality" "100"
  17052. ;; "-sharpen" "0x1.0"
  17053. pngfile)
  17054. (call-process "convert" nil nil nil
  17055. "-density" dpi
  17056. "-trim"
  17057. "-antialias"
  17058. pdffile
  17059. "-quality" "100"
  17060. ;; "-sharpen" "0x1.0"
  17061. pngfile))
  17062. (error nil))
  17063. (if (not (file-exists-p pngfile))
  17064. (if org-format-latex-signal-error
  17065. (error "Failed to create png file from %s" texfile)
  17066. (message "Failed to create png file from %s" texfile)
  17067. nil)
  17068. ;; Use the requested file name and clean up
  17069. (copy-file pngfile tofile 'replace)
  17070. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17071. (if (file-exists-p (concat texfilebase e))
  17072. (delete-file (concat texfilebase e))))
  17073. pngfile))))
  17074. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17075. "Fill a LaTeX header template TPL.
  17076. In the template, the following place holders will be recognized:
  17077. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17078. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17079. [PACKAGES] \\usepackage statements for PKG
  17080. [NO-PACKAGES] do not include PKG
  17081. [EXTRA] the string EXTRA
  17082. [NO-EXTRA] do not include EXTRA
  17083. For backward compatibility, if both the positive and the negative place
  17084. holder is missing, the positive one (without the \"NO-\") will be
  17085. assumed to be present at the end of the template.
  17086. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17087. EXTRA is a string.
  17088. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17089. (let (rpl (end ""))
  17090. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17091. (setq rpl (if (or (match-end 1) (not def-pkg))
  17092. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17093. tpl (replace-match rpl t t tpl))
  17094. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17095. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17096. (setq rpl (if (or (match-end 1) (not pkg))
  17097. "" (org-latex-packages-to-string pkg snippets-p t))
  17098. tpl (replace-match rpl t t tpl))
  17099. (if pkg (setq end
  17100. (concat end "\n"
  17101. (org-latex-packages-to-string pkg snippets-p)))))
  17102. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17103. (setq rpl (if (or (match-end 1) (not extra))
  17104. "" (concat extra "\n"))
  17105. tpl (replace-match rpl t t tpl))
  17106. (if (and extra (string-match "\\S-" extra))
  17107. (setq end (concat end "\n" extra))))
  17108. (if (string-match "\\S-" end)
  17109. (concat tpl "\n" end)
  17110. tpl)))
  17111. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17112. "Turn an alist of packages into a string with the \\usepackage macros."
  17113. (setq pkg (mapconcat (lambda(p)
  17114. (cond
  17115. ((stringp p) p)
  17116. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17117. (format "%% Package %s omitted" (cadr p)))
  17118. ((equal "" (car p))
  17119. (format "\\usepackage{%s}" (cadr p)))
  17120. (t
  17121. (format "\\usepackage[%s]{%s}"
  17122. (car p) (cadr p)))))
  17123. pkg
  17124. "\n"))
  17125. (if newline (concat pkg "\n") pkg))
  17126. (defun org-dvipng-color (attr)
  17127. "Return a RGB color specification for dvipng."
  17128. (apply 'format "rgb %s %s %s"
  17129. (mapcar 'org-normalize-color
  17130. (if (featurep 'xemacs)
  17131. (color-rgb-components
  17132. (face-property 'default
  17133. (cond ((eq attr :foreground) 'foreground)
  17134. ((eq attr :background) 'background))))
  17135. (color-values (face-attribute 'default attr nil))))))
  17136. (defun org-dvipng-color-format (color-name)
  17137. "Convert COLOR-NAME to a RGB color value for dvipng."
  17138. (apply 'format "rgb %s %s %s"
  17139. (mapcar 'org-normalize-color
  17140. (color-values color-name))))
  17141. (defun org-latex-color (attr)
  17142. "Return a RGB color for the LaTeX color package."
  17143. (apply 'format "%s,%s,%s"
  17144. (mapcar 'org-normalize-color
  17145. (if (featurep 'xemacs)
  17146. (color-rgb-components
  17147. (face-property 'default
  17148. (cond ((eq attr :foreground) 'foreground)
  17149. ((eq attr :background) 'background))))
  17150. (color-values (face-attribute 'default attr nil))))))
  17151. (defun org-latex-color-format (color-name)
  17152. "Convert COLOR-NAME to a RGB color value."
  17153. (apply 'format "%s,%s,%s"
  17154. (mapcar 'org-normalize-color
  17155. (color-values color-name))))
  17156. (defun org-normalize-color (value)
  17157. "Return string to be used as color value for an RGB component."
  17158. (format "%g" (/ value 65535.0)))
  17159. ;; Image display
  17160. (defvar org-inline-image-overlays nil)
  17161. (make-variable-buffer-local 'org-inline-image-overlays)
  17162. (defun org-toggle-inline-images (&optional include-linked)
  17163. "Toggle the display of inline images.
  17164. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17165. (interactive "P")
  17166. (if org-inline-image-overlays
  17167. (progn
  17168. (org-remove-inline-images)
  17169. (message "Inline image display turned off"))
  17170. (org-display-inline-images include-linked)
  17171. (if (and (org-called-interactively-p)
  17172. org-inline-image-overlays)
  17173. (message "%d images displayed inline"
  17174. (length org-inline-image-overlays))
  17175. (message "No images to display inline"))))
  17176. (defun org-redisplay-inline-images ()
  17177. "Refresh the display of inline images."
  17178. (interactive)
  17179. (if (not org-inline-image-overlays)
  17180. (org-toggle-inline-images)
  17181. (org-toggle-inline-images)
  17182. (org-toggle-inline-images)))
  17183. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17184. "Display inline images.
  17185. Normally only links without a description part are inlined, because this
  17186. is how it will work for export. When INCLUDE-LINKED is set, also links
  17187. with a description part will be inlined. This can be nice for a quick
  17188. look at those images, but it does not reflect what exported files will look
  17189. like.
  17190. When REFRESH is set, refresh existing images between BEG and END.
  17191. This will create new image displays only if necessary.
  17192. BEG and END default to the buffer boundaries."
  17193. (interactive "P")
  17194. (unless refresh
  17195. (org-remove-inline-images)
  17196. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  17197. (save-excursion
  17198. (save-restriction
  17199. (widen)
  17200. (setq beg (or beg (point-min)) end (or end (point-max)))
  17201. (goto-char beg)
  17202. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  17203. (substring (org-image-file-name-regexp) 0 -2)
  17204. "\\)\\]" (if include-linked "" "\\]")))
  17205. (case-fold-search t)
  17206. old file ov img type attrwidth width)
  17207. (while (re-search-forward re end t)
  17208. (setq old (get-char-property-and-overlay (match-beginning 1)
  17209. 'org-image-overlay)
  17210. file (expand-file-name
  17211. (concat (or (match-string 3) "") (match-string 4))))
  17212. (when (image-type-available-p 'imagemagick)
  17213. (setq attrwidth (if (or (listp org-image-actual-width)
  17214. (null org-image-actual-width))
  17215. (save-excursion
  17216. (save-match-data
  17217. (when (re-search-backward
  17218. "#\\+attr.*:width[ \t]+\\([^ ]+\\)"
  17219. (save-excursion
  17220. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  17221. (string-to-number (match-string 1))))))
  17222. width (cond ((eq org-image-actual-width t) nil)
  17223. ((null org-image-actual-width) attrwidth)
  17224. ((numberp org-image-actual-width)
  17225. org-image-actual-width)
  17226. ((listp org-image-actual-width)
  17227. (or attrwidth (car org-image-actual-width))))
  17228. type (if width 'imagemagick)))
  17229. (when (file-exists-p file)
  17230. (if (and (car-safe old) refresh)
  17231. (image-refresh (overlay-get (cdr old) 'display))
  17232. (setq img (save-match-data (create-image file type nil :width width)))
  17233. (when img
  17234. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  17235. (overlay-put ov 'display img)
  17236. (overlay-put ov 'face 'default)
  17237. (overlay-put ov 'org-image-overlay t)
  17238. (overlay-put ov 'modification-hooks
  17239. (list 'org-display-inline-remove-overlay))
  17240. (push ov org-inline-image-overlays)))))))))
  17241. (define-obsolete-function-alias
  17242. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  17243. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  17244. "Remove inline-display overlay if a corresponding region is modified."
  17245. (let ((inhibit-modification-hooks t))
  17246. (when (and ov after)
  17247. (delete ov org-inline-image-overlays)
  17248. (delete-overlay ov))))
  17249. (defun org-remove-inline-images ()
  17250. "Remove inline display of images."
  17251. (interactive)
  17252. (mapc 'delete-overlay org-inline-image-overlays)
  17253. (setq org-inline-image-overlays nil))
  17254. ;;;; Key bindings
  17255. ;; Outline functions from `outline-mode-prefix-map'
  17256. ;; that can be remapped in Org:
  17257. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  17258. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  17259. (define-key org-mode-map [remap outline-forward-same-level]
  17260. 'org-forward-heading-same-level)
  17261. (define-key org-mode-map [remap outline-backward-same-level]
  17262. 'org-backward-heading-same-level)
  17263. (define-key org-mode-map [remap show-branches]
  17264. 'org-kill-note-or-show-branches)
  17265. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  17266. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  17267. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  17268. ;; Outline functions from `outline-mode-prefix-map' that can not
  17269. ;; be remapped in Org:
  17270. ;;
  17271. ;; - the column "key binding" shows whether the Outline function is still
  17272. ;; available in Org mode on the same key that it has been bound to in
  17273. ;; Outline mode:
  17274. ;; - "overridden": key used for a different functionality in Org mode
  17275. ;; - else: key still bound to the same Outline function in Org mode
  17276. ;;
  17277. ;; | Outline function | key binding | Org replacement |
  17278. ;; |------------------------------------+-------------+-----------------------|
  17279. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  17280. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  17281. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  17282. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  17283. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  17284. ;; | `show-entry' | overridden | no replacement |
  17285. ;; | `show-children' | `C-c C-i' | visibility cycling |
  17286. ;; | `show-branches' | `C-c C-k' | still same function |
  17287. ;; | `show-subtree' | overridden | visibility cycling |
  17288. ;; | `show-all' | overridden | no replacement |
  17289. ;; | `hide-subtree' | overridden | visibility cycling |
  17290. ;; | `hide-body' | overridden | no replacement |
  17291. ;; | `hide-entry' | overridden | visibility cycling |
  17292. ;; | `hide-leaves' | overridden | no replacement |
  17293. ;; | `hide-sublevels' | overridden | no replacement |
  17294. ;; | `hide-other' | overridden | no replacement |
  17295. ;; Make `C-c C-x' a prefix key
  17296. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  17297. ;; TAB key with modifiers
  17298. (org-defkey org-mode-map "\C-i" 'org-cycle)
  17299. (org-defkey org-mode-map [(tab)] 'org-cycle)
  17300. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  17301. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  17302. ;; The following line is necessary under Suse GNU/Linux
  17303. (unless (featurep 'xemacs)
  17304. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  17305. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  17306. (define-key org-mode-map [backtab] 'org-shifttab)
  17307. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  17308. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  17309. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  17310. ;; Cursor keys with modifiers
  17311. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  17312. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  17313. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  17314. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  17315. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  17316. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  17317. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  17318. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  17319. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  17320. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  17321. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  17322. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  17323. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  17324. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  17325. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  17326. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  17327. ;; Babel keys
  17328. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  17329. (mapc (lambda (pair)
  17330. (define-key org-babel-map (car pair) (cdr pair)))
  17331. org-babel-key-bindings)
  17332. ;;; Extra keys for tty access.
  17333. ;; We only set them when really needed because otherwise the
  17334. ;; menus don't show the simple keys
  17335. (when (or org-use-extra-keys
  17336. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17337. (not window-system))
  17338. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17339. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17340. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17341. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17342. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17343. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17344. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17345. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17346. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17347. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17348. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17349. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17350. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17351. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17352. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17353. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17354. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17355. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17356. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17357. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17358. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17359. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17360. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17361. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17362. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17363. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17364. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17365. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17366. ;; All the other keys
  17367. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17368. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17369. (if (boundp 'narrow-map)
  17370. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17371. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17372. (if (boundp 'narrow-map)
  17373. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17374. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17375. (if (boundp 'narrow-map)
  17376. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17377. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17378. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17379. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17380. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17381. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17382. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17383. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17384. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17385. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17386. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17387. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17388. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17389. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17390. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17391. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17392. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17393. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17394. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  17395. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17396. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17397. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17398. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17399. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17400. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17401. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17402. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17403. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17404. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17405. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17406. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17407. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17408. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17409. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17410. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17411. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17412. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17413. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17414. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17415. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17416. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17417. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17418. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17419. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17420. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17421. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17422. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17423. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17424. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17425. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17426. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17427. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17428. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17429. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17430. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17431. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17432. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17433. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17434. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17435. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17436. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17437. (org-defkey org-mode-map "\C-o" 'org-ctrl-o)
  17438. (org-defkey org-mode-map "\C-m" 'org-return)
  17439. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17440. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17441. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17442. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17443. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17444. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17445. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17446. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17447. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17448. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17449. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17450. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17451. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17452. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17453. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17454. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17455. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17456. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17457. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17458. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17459. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17460. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17461. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17462. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17463. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17464. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17465. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17466. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17467. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17468. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17469. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17470. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17471. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17472. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17473. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17474. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17475. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17476. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17477. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17478. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17479. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17480. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17481. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17482. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17483. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17484. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17485. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17486. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17487. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17488. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17489. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17490. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17491. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17492. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17493. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17494. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17495. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17496. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17497. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17498. (when (featurep 'xemacs)
  17499. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17500. (defconst org-speed-commands-default
  17501. '(
  17502. ("Outline Navigation")
  17503. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17504. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17505. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17506. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17507. ("F" . org-next-block)
  17508. ("B" . org-previous-block)
  17509. ("u" . (org-speed-move-safe 'outline-up-heading))
  17510. ("j" . org-goto)
  17511. ("g" . (org-refile t))
  17512. ("Outline Visibility")
  17513. ("c" . org-cycle)
  17514. ("C" . org-shifttab)
  17515. (" " . org-display-outline-path)
  17516. ("s" . org-narrow-to-subtree)
  17517. ("=" . org-columns)
  17518. ("Outline Structure Editing")
  17519. ("U" . org-shiftmetaup)
  17520. ("D" . org-shiftmetadown)
  17521. ("r" . org-metaright)
  17522. ("l" . org-metaleft)
  17523. ("R" . org-shiftmetaright)
  17524. ("L" . org-shiftmetaleft)
  17525. ("i" . (progn (forward-char 1) (call-interactively
  17526. 'org-insert-heading-respect-content)))
  17527. ("^" . org-sort)
  17528. ("w" . org-refile)
  17529. ("a" . org-archive-subtree-default-with-confirmation)
  17530. ("@" . org-mark-subtree)
  17531. ("#" . org-toggle-comment)
  17532. ("Clock Commands")
  17533. ("I" . org-clock-in)
  17534. ("O" . org-clock-out)
  17535. ("Meta Data Editing")
  17536. ("t" . org-todo)
  17537. ("," . (org-priority))
  17538. ("0" . (org-priority ?\ ))
  17539. ("1" . (org-priority ?A))
  17540. ("2" . (org-priority ?B))
  17541. ("3" . (org-priority ?C))
  17542. (":" . org-set-tags-command)
  17543. ("e" . org-set-effort)
  17544. ("E" . org-inc-effort)
  17545. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17546. (org-entry-put (point) "APPT_WARNTIME" m)))
  17547. ("Agenda Views etc")
  17548. ("v" . org-agenda)
  17549. ("/" . org-sparse-tree)
  17550. ("Misc")
  17551. ("o" . org-open-at-point)
  17552. ("?" . org-speed-command-help)
  17553. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17554. (">" . (org-agenda-remove-restriction-lock))
  17555. )
  17556. "The default speed commands.")
  17557. (defun org-print-speed-command (e)
  17558. (if (> (length (car e)) 1)
  17559. (progn
  17560. (princ "\n")
  17561. (princ (car e))
  17562. (princ "\n")
  17563. (princ (make-string (length (car e)) ?-))
  17564. (princ "\n"))
  17565. (princ (car e))
  17566. (princ " ")
  17567. (if (symbolp (cdr e))
  17568. (princ (symbol-name (cdr e)))
  17569. (prin1 (cdr e)))
  17570. (princ "\n")))
  17571. (defun org-speed-command-help ()
  17572. "Show the available speed commands."
  17573. (interactive)
  17574. (if (not org-use-speed-commands)
  17575. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17576. (with-output-to-temp-buffer "*Help*"
  17577. (princ "User-defined Speed commands\n===========================\n")
  17578. (mapc 'org-print-speed-command org-speed-commands-user)
  17579. (princ "\n")
  17580. (princ "Built-in Speed commands\n=======================\n")
  17581. (mapc 'org-print-speed-command org-speed-commands-default))
  17582. (with-current-buffer "*Help*"
  17583. (setq truncate-lines t))))
  17584. (defun org-speed-move-safe (cmd)
  17585. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17586. If not, return to the original position and throw an error."
  17587. (interactive)
  17588. (let ((pos (point)))
  17589. (call-interactively cmd)
  17590. (unless (and (bolp) (org-at-heading-p))
  17591. (goto-char pos)
  17592. (error "Boundary reached while executing %s" cmd))))
  17593. (defvar org-self-insert-command-undo-counter 0)
  17594. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17595. (defvar org-speed-command nil)
  17596. (define-obsolete-function-alias
  17597. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17598. (defun org-speed-command-activate (keys)
  17599. "Hook for activating single-letter speed commands.
  17600. `org-speed-commands-default' specifies a minimal command set.
  17601. Use `org-speed-commands-user' for further customization."
  17602. (when (or (and (bolp) (looking-at org-outline-regexp))
  17603. (and (functionp org-use-speed-commands)
  17604. (funcall org-use-speed-commands)))
  17605. (cdr (assoc keys (append org-speed-commands-user
  17606. org-speed-commands-default)))))
  17607. (define-obsolete-function-alias
  17608. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17609. (defun org-babel-speed-command-activate (keys)
  17610. "Hook for activating single-letter code block commands."
  17611. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17612. (cdr (assoc keys org-babel-key-bindings))))
  17613. (defcustom org-speed-command-hook
  17614. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17615. "Hook for activating speed commands at strategic locations.
  17616. Hook functions are called in sequence until a valid handler is
  17617. found.
  17618. Each hook takes a single argument, a user-pressed command key
  17619. which is also a `self-insert-command' from the global map.
  17620. Within the hook, examine the cursor position and the command key
  17621. and return nil or a valid handler as appropriate. Handler could
  17622. be one of an interactive command, a function, or a form.
  17623. Set `org-use-speed-commands' to non-nil value to enable this
  17624. hook. The default setting is `org-speed-command-activate'."
  17625. :group 'org-structure
  17626. :version "24.1"
  17627. :type 'hook)
  17628. (defun org-self-insert-command (N)
  17629. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17630. If the cursor is in a table looking at whitespace, the whitespace is
  17631. overwritten, and the table is not marked as requiring realignment."
  17632. (interactive "p")
  17633. (org-check-before-invisible-edit 'insert)
  17634. (cond
  17635. ((and org-use-speed-commands
  17636. (setq org-speed-command
  17637. (run-hook-with-args-until-success
  17638. 'org-speed-command-hook (this-command-keys))))
  17639. (cond
  17640. ((commandp org-speed-command)
  17641. (setq this-command org-speed-command)
  17642. (call-interactively org-speed-command))
  17643. ((functionp org-speed-command)
  17644. (funcall org-speed-command))
  17645. ((and org-speed-command (listp org-speed-command))
  17646. (eval org-speed-command))
  17647. (t (let (org-use-speed-commands)
  17648. (call-interactively 'org-self-insert-command)))))
  17649. ((and
  17650. (org-table-p)
  17651. (progn
  17652. ;; check if we blank the field, and if that triggers align
  17653. (and (featurep 'org-table) org-table-auto-blank-field
  17654. (member last-command
  17655. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17656. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17657. ;; got extra space, this field does not determine column width
  17658. (let (org-table-may-need-update) (org-table-blank-field))
  17659. ;; no extra space, this field may determine column width
  17660. (org-table-blank-field)))
  17661. t)
  17662. (eq N 1)
  17663. (looking-at "[^|\n]* |"))
  17664. (let (org-table-may-need-update)
  17665. (goto-char (1- (match-end 0)))
  17666. (backward-delete-char 1)
  17667. (goto-char (match-beginning 0))
  17668. (self-insert-command N)))
  17669. (t
  17670. (setq org-table-may-need-update t)
  17671. (self-insert-command N)
  17672. (org-fix-tags-on-the-fly)
  17673. (if org-self-insert-cluster-for-undo
  17674. (if (not (eq last-command 'org-self-insert-command))
  17675. (setq org-self-insert-command-undo-counter 1)
  17676. (if (>= org-self-insert-command-undo-counter 20)
  17677. (setq org-self-insert-command-undo-counter 1)
  17678. (and (> org-self-insert-command-undo-counter 0)
  17679. buffer-undo-list (listp buffer-undo-list)
  17680. (not (cadr buffer-undo-list)) ; remove nil entry
  17681. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17682. (setq org-self-insert-command-undo-counter
  17683. (1+ org-self-insert-command-undo-counter))))))))
  17684. (defun org-check-before-invisible-edit (kind)
  17685. "Check is editing if kind KIND would be dangerous with invisible text around.
  17686. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17687. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17688. (if (and org-catch-invisible-edits
  17689. (or (not (boundp 'visible-mode)) (not visible-mode))
  17690. (or (get-char-property (point) 'invisible)
  17691. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17692. ;; OK, we need to take a closer look
  17693. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17694. (invisible-before-point (if (bobp) nil (get-char-property
  17695. (1- (point)) 'invisible)))
  17696. (border-and-ok-direction
  17697. (or
  17698. ;; Check if we are acting predictably before invisible text
  17699. (and invisible-at-point (not invisible-before-point)
  17700. (memq kind '(insert delete-backward)))
  17701. ;; Check if we are acting predictably after invisible text
  17702. ;; This works not well, and I have turned it off. It seems
  17703. ;; better to always show and stop after invisible text.
  17704. ;; (and (not invisible-at-point) invisible-before-point
  17705. ;; (memq kind '(insert delete)))
  17706. )))
  17707. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17708. (memq invisible-before-point '(outline org-hide-block t)))
  17709. (if (eq org-catch-invisible-edits 'error)
  17710. (error "Editing in invisible areas is prohibited - make visible first"))
  17711. (if (and org-custom-properties-overlays
  17712. (y-or-n-p "Display invisible properties in this buffer? "))
  17713. (org-toggle-custom-properties-visibility)
  17714. ;; Make the area visible
  17715. (save-excursion
  17716. (if invisible-before-point
  17717. (goto-char (previous-single-char-property-change
  17718. (point) 'invisible)))
  17719. (org-cycle))
  17720. (cond
  17721. ((eq org-catch-invisible-edits 'show)
  17722. ;; That's it, we do the edit after showing
  17723. (message
  17724. "Unfolding invisible region around point before editing")
  17725. (sit-for 1))
  17726. ((and (eq org-catch-invisible-edits 'smart)
  17727. border-and-ok-direction)
  17728. (message "Unfolding invisible region around point before editing"))
  17729. (t
  17730. ;; Don't do the edit, make the user repeat it in full visibility
  17731. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17732. (defun org-fix-tags-on-the-fly ()
  17733. (when (and (equal (char-after (point-at-bol)) ?*)
  17734. (org-at-heading-p))
  17735. (org-align-tags-here org-tags-column)))
  17736. (defun org-delete-backward-char (N)
  17737. "Like `delete-backward-char', insert whitespace at field end in tables.
  17738. When deleting backwards, in tables this function will insert whitespace in
  17739. front of the next \"|\" separator, to keep the table aligned. The table will
  17740. still be marked for re-alignment if the field did fill the entire column,
  17741. because, in this case the deletion might narrow the column."
  17742. (interactive "p")
  17743. (save-match-data
  17744. (org-check-before-invisible-edit 'delete-backward)
  17745. (if (and (org-table-p)
  17746. (eq N 1)
  17747. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17748. (looking-at ".*?|"))
  17749. (let ((pos (point))
  17750. (noalign (looking-at "[^|\n\r]* |"))
  17751. (c org-table-may-need-update))
  17752. (backward-delete-char N)
  17753. (if (not overwrite-mode)
  17754. (progn
  17755. (skip-chars-forward "^|")
  17756. (insert " ")
  17757. (goto-char (1- pos))))
  17758. ;; noalign: if there were two spaces at the end, this field
  17759. ;; does not determine the width of the column.
  17760. (if noalign (setq org-table-may-need-update c)))
  17761. (backward-delete-char N)
  17762. (org-fix-tags-on-the-fly))))
  17763. (defun org-delete-char (N)
  17764. "Like `delete-char', but insert whitespace at field end in tables.
  17765. When deleting characters, in tables this function will insert whitespace in
  17766. front of the next \"|\" separator, to keep the table aligned. The table will
  17767. still be marked for re-alignment if the field did fill the entire column,
  17768. because, in this case the deletion might narrow the column."
  17769. (interactive "p")
  17770. (save-match-data
  17771. (org-check-before-invisible-edit 'delete)
  17772. (if (and (org-table-p)
  17773. (not (bolp))
  17774. (not (= (char-after) ?|))
  17775. (eq N 1))
  17776. (if (looking-at ".*?|")
  17777. (let ((pos (point))
  17778. (noalign (looking-at "[^|\n\r]* |"))
  17779. (c org-table-may-need-update))
  17780. (replace-match
  17781. (concat (substring (match-string 0) 1 -1) " |") nil t)
  17782. (goto-char pos)
  17783. ;; noalign: if there were two spaces at the end, this field
  17784. ;; does not determine the width of the column.
  17785. (if noalign (setq org-table-may-need-update c)))
  17786. (delete-char N))
  17787. (delete-char N)
  17788. (org-fix-tags-on-the-fly))))
  17789. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17790. (put 'org-self-insert-command 'delete-selection t)
  17791. (put 'orgtbl-self-insert-command 'delete-selection t)
  17792. (put 'org-delete-char 'delete-selection 'supersede)
  17793. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17794. (put 'org-yank 'delete-selection 'yank)
  17795. ;; Make `flyspell-mode' delay after some commands
  17796. (put 'org-self-insert-command 'flyspell-delayed t)
  17797. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17798. (put 'org-delete-char 'flyspell-delayed t)
  17799. (put 'org-delete-backward-char 'flyspell-delayed t)
  17800. ;; Make pabbrev-mode expand after org-mode commands
  17801. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17802. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17803. ;; How to do this: Measure non-white length of current string
  17804. ;; If equal to column width, we should realign.
  17805. (defun org-remap (map &rest commands)
  17806. "In MAP, remap the functions given in COMMANDS.
  17807. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17808. (let (new old)
  17809. (while commands
  17810. (setq old (pop commands) new (pop commands))
  17811. (if (fboundp 'command-remapping)
  17812. (org-defkey map (vector 'remap old) new)
  17813. (substitute-key-definition old new map global-map)))))
  17814. (defun org-transpose-words ()
  17815. "Transpose words for Org.
  17816. This uses the `org-mode-transpose-word-syntax-table' syntax
  17817. table, which interprets characters in `org-emphasis-alist' as
  17818. word constituants."
  17819. (interactive)
  17820. (with-syntax-table org-mode-transpose-word-syntax-table
  17821. (call-interactively 'transpose-words)))
  17822. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17823. (when (eq org-enable-table-editor 'optimized)
  17824. ;; If the user wants maximum table support, we need to hijack
  17825. ;; some standard editing functions
  17826. (org-remap org-mode-map
  17827. 'self-insert-command 'org-self-insert-command
  17828. 'delete-char 'org-delete-char
  17829. 'delete-backward-char 'org-delete-backward-char)
  17830. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17831. (defvar org-ctrl-c-ctrl-c-hook nil
  17832. "Hook for functions attaching themselves to `C-c C-c'.
  17833. This can be used to add additional functionality to the C-c C-c
  17834. key which executes context-dependent commands. This hook is run
  17835. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17836. run after the last test.
  17837. Each function will be called with no arguments. The function
  17838. must check if the context is appropriate for it to act. If yes,
  17839. it should do its thing and then return a non-nil value. If the
  17840. context is wrong, just do nothing and return nil.")
  17841. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17842. "Hook for functions attaching themselves to `C-c C-c'.
  17843. This can be used to add additional functionality to the C-c C-c
  17844. key which executes context-dependent commands. This hook is run
  17845. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17846. before the first test.
  17847. Each function will be called with no arguments. The function
  17848. must check if the context is appropriate for it to act. If yes,
  17849. it should do its thing and then return a non-nil value. If the
  17850. context is wrong, just do nothing and return nil.")
  17851. (defvar org-tab-first-hook nil
  17852. "Hook for functions to attach themselves to TAB.
  17853. See `org-ctrl-c-ctrl-c-hook' for more information.
  17854. This hook runs as the first action when TAB is pressed, even before
  17855. `org-cycle' messes around with the `outline-regexp' to cater for
  17856. inline tasks and plain list item folding.
  17857. If any function in this hook returns t, any other actions that
  17858. would have been caused by TAB (such as table field motion or visibility
  17859. cycling) will not occur.")
  17860. (defvar org-tab-after-check-for-table-hook nil
  17861. "Hook for functions to attach themselves to TAB.
  17862. See `org-ctrl-c-ctrl-c-hook' for more information.
  17863. This hook runs after it has been established that the cursor is not in a
  17864. table, but before checking if the cursor is in a headline or if global cycling
  17865. should be done.
  17866. If any function in this hook returns t, not other actions like visibility
  17867. cycling will be done.")
  17868. (defvar org-tab-after-check-for-cycling-hook nil
  17869. "Hook for functions to attach themselves to TAB.
  17870. See `org-ctrl-c-ctrl-c-hook' for more information.
  17871. This hook runs after it has been established that not table field motion and
  17872. not visibility should be done because of current context. This is probably
  17873. the place where a package like yasnippets can hook in.")
  17874. (defvar org-tab-before-tab-emulation-hook nil
  17875. "Hook for functions to attach themselves to TAB.
  17876. See `org-ctrl-c-ctrl-c-hook' for more information.
  17877. This hook runs after every other options for TAB have been exhausted, but
  17878. before indentation and \t insertion takes place.")
  17879. (defvar org-metaleft-hook nil
  17880. "Hook for functions attaching themselves to `M-left'.
  17881. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17882. (defvar org-metaright-hook nil
  17883. "Hook for functions attaching themselves to `M-right'.
  17884. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17885. (defvar org-metaup-hook nil
  17886. "Hook for functions attaching themselves to `M-up'.
  17887. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17888. (defvar org-metadown-hook nil
  17889. "Hook for functions attaching themselves to `M-down'.
  17890. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17891. (defvar org-shiftmetaleft-hook nil
  17892. "Hook for functions attaching themselves to `M-S-left'.
  17893. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17894. (defvar org-shiftmetaright-hook nil
  17895. "Hook for functions attaching themselves to `M-S-right'.
  17896. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17897. (defvar org-shiftmetaup-hook nil
  17898. "Hook for functions attaching themselves to `M-S-up'.
  17899. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17900. (defvar org-shiftmetadown-hook nil
  17901. "Hook for functions attaching themselves to `M-S-down'.
  17902. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17903. (defvar org-metareturn-hook nil
  17904. "Hook for functions attaching themselves to `M-RET'.
  17905. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17906. (defvar org-shiftup-hook nil
  17907. "Hook for functions attaching themselves to `S-up'.
  17908. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17909. (defvar org-shiftup-final-hook nil
  17910. "Hook for functions attaching themselves to `S-up'.
  17911. This one runs after all other options except shift-select have been excluded.
  17912. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17913. (defvar org-shiftdown-hook nil
  17914. "Hook for functions attaching themselves to `S-down'.
  17915. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17916. (defvar org-shiftdown-final-hook nil
  17917. "Hook for functions attaching themselves to `S-down'.
  17918. This one runs after all other options except shift-select have been excluded.
  17919. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17920. (defvar org-shiftleft-hook nil
  17921. "Hook for functions attaching themselves to `S-left'.
  17922. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17923. (defvar org-shiftleft-final-hook nil
  17924. "Hook for functions attaching themselves to `S-left'.
  17925. This one runs after all other options except shift-select have been excluded.
  17926. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17927. (defvar org-shiftright-hook nil
  17928. "Hook for functions attaching themselves to `S-right'.
  17929. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17930. (defvar org-shiftright-final-hook nil
  17931. "Hook for functions attaching themselves to `S-right'.
  17932. This one runs after all other options except shift-select have been excluded.
  17933. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17934. (defun org-modifier-cursor-error ()
  17935. "Throw an error, a modified cursor command was applied in wrong context."
  17936. (error "This command is active in special context like tables, headlines or items"))
  17937. (defun org-shiftselect-error ()
  17938. "Throw an error because Shift-Cursor command was applied in wrong context."
  17939. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17940. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17941. (error "This command works only in special context like headlines or timestamps")))
  17942. (defun org-call-for-shift-select (cmd)
  17943. (let ((this-command-keys-shift-translated t))
  17944. (call-interactively cmd)))
  17945. (defun org-shifttab (&optional arg)
  17946. "Global visibility cycling or move to previous table field.
  17947. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17948. on context.
  17949. See the individual commands for more information."
  17950. (interactive "P")
  17951. (cond
  17952. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17953. ((integerp arg)
  17954. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17955. (message "Content view to level: %d" arg)
  17956. (org-content (prefix-numeric-value arg2))
  17957. (setq org-cycle-global-status 'overview)))
  17958. (t (call-interactively 'org-global-cycle))))
  17959. (defun org-shiftmetaleft ()
  17960. "Promote subtree or delete table column.
  17961. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17962. `org-table-delete-column', depending on context. See the
  17963. individual commands for more information."
  17964. (interactive)
  17965. (cond
  17966. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17967. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17968. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17969. ((if (not (org-region-active-p)) (org-at-item-p)
  17970. (save-excursion (goto-char (region-beginning))
  17971. (org-at-item-p)))
  17972. (call-interactively 'org-outdent-item-tree))
  17973. (t (org-modifier-cursor-error))))
  17974. (defun org-shiftmetaright ()
  17975. "Demote subtree or insert table column.
  17976. Calls `org-demote-subtree', `org-indent-item-tree', or
  17977. `org-table-insert-column', depending on context. See the
  17978. individual commands for more information."
  17979. (interactive)
  17980. (cond
  17981. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17982. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17983. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17984. ((if (not (org-region-active-p)) (org-at-item-p)
  17985. (save-excursion (goto-char (region-beginning))
  17986. (org-at-item-p)))
  17987. (call-interactively 'org-indent-item-tree))
  17988. (t (org-modifier-cursor-error))))
  17989. (defun org-shiftmetaup (&optional arg)
  17990. "Move subtree up or kill table row.
  17991. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17992. `org-move-item-up' or `org-timestamp-up', depending on context.
  17993. See the individual commands for more information."
  17994. (interactive "P")
  17995. (cond
  17996. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17997. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17998. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17999. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18000. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18001. (call-interactively 'org-timestamp-up)))
  18002. (t (call-interactively 'org-drag-line-backward))))
  18003. (defun org-shiftmetadown (&optional arg)
  18004. "Move subtree down or insert table row.
  18005. Calls `org-move-subtree-down' or `org-table-insert-row' or
  18006. `org-move-item-down' or `org-timestamp-up', depending on context.
  18007. See the individual commands for more information."
  18008. (interactive "P")
  18009. (cond
  18010. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18011. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18012. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18013. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18014. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18015. (call-interactively 'org-timestamp-down)))
  18016. (t (call-interactively 'org-drag-line-forward))))
  18017. (defsubst org-hidden-tree-error ()
  18018. (error
  18019. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18020. (defun org-metaleft (&optional arg)
  18021. "Promote heading or move table column to left.
  18022. Calls `org-do-promote' or `org-table-move-column', depending on context.
  18023. With no specific context, calls the Emacs default `backward-word'.
  18024. See the individual commands for more information."
  18025. (interactive "P")
  18026. (cond
  18027. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18028. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18029. ((org-with-limited-levels
  18030. (or (org-at-heading-p)
  18031. (and (org-region-active-p)
  18032. (save-excursion
  18033. (goto-char (region-beginning))
  18034. (org-at-heading-p)))))
  18035. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18036. (call-interactively 'org-do-promote))
  18037. ;; At an inline task.
  18038. ((org-at-heading-p)
  18039. (call-interactively 'org-inlinetask-promote))
  18040. ((or (org-at-item-p)
  18041. (and (org-region-active-p)
  18042. (save-excursion
  18043. (goto-char (region-beginning))
  18044. (org-at-item-p))))
  18045. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18046. (call-interactively 'org-outdent-item))
  18047. (t (call-interactively 'backward-word))))
  18048. (defun org-metaright (&optional arg)
  18049. "Demote a subtree, a list item or move table column to right.
  18050. In front of a drawer or a block keyword, indent it correctly.
  18051. With no specific context, calls the Emacs default `forward-word'.
  18052. See the individual commands for more information."
  18053. (interactive "P")
  18054. (cond
  18055. ((run-hook-with-args-until-success 'org-metaright-hook))
  18056. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18057. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18058. ((org-at-block-p) (call-interactively 'org-indent-block))
  18059. ((org-with-limited-levels
  18060. (or (org-at-heading-p)
  18061. (and (org-region-active-p)
  18062. (save-excursion
  18063. (goto-char (region-beginning))
  18064. (org-at-heading-p)))))
  18065. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18066. (call-interactively 'org-do-demote))
  18067. ;; At an inline task.
  18068. ((org-at-heading-p)
  18069. (call-interactively 'org-inlinetask-demote))
  18070. ((or (org-at-item-p)
  18071. (and (org-region-active-p)
  18072. (save-excursion
  18073. (goto-char (region-beginning))
  18074. (org-at-item-p))))
  18075. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18076. (call-interactively 'org-indent-item))
  18077. (t (call-interactively 'forward-word))))
  18078. (defun org-check-for-hidden (what)
  18079. "Check if there are hidden headlines/items in the current visual line.
  18080. WHAT can be either `headlines' or `items'. If the current line is
  18081. an outline or item heading and it has a folded subtree below it,
  18082. this function returns t, nil otherwise."
  18083. (let ((re (cond
  18084. ((eq what 'headlines) org-outline-regexp-bol)
  18085. ((eq what 'items) (org-item-beginning-re))
  18086. (t (error "This should not happen"))))
  18087. beg end)
  18088. (save-excursion
  18089. (catch 'exit
  18090. (unless (org-region-active-p)
  18091. (setq beg (point-at-bol))
  18092. (beginning-of-line 2)
  18093. (while (and (not (eobp)) ;; this is like `next-line'
  18094. (get-char-property (1- (point)) 'invisible))
  18095. (beginning-of-line 2))
  18096. (setq end (point))
  18097. (goto-char beg)
  18098. (goto-char (point-at-eol))
  18099. (setq end (max end (point)))
  18100. (while (re-search-forward re end t)
  18101. (if (get-char-property (match-beginning 0) 'invisible)
  18102. (throw 'exit t))))
  18103. nil))))
  18104. (defun org-metaup (&optional arg)
  18105. "Move subtree up or move table row up.
  18106. Calls `org-move-subtree-up' or `org-table-move-row' or
  18107. `org-move-item-up', depending on context. See the individual commands
  18108. for more information."
  18109. (interactive "P")
  18110. (cond
  18111. ((run-hook-with-args-until-success 'org-metaup-hook))
  18112. ((org-region-active-p)
  18113. (let* ((a (min (region-beginning) (region-end)))
  18114. (b (1- (max (region-beginning) (region-end))))
  18115. (c (save-excursion (goto-char a)
  18116. (move-beginning-of-line 0)))
  18117. (d (save-excursion (goto-char a)
  18118. (move-end-of-line 0) (point))))
  18119. (transpose-regions a b c d)
  18120. (goto-char c)))
  18121. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18122. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18123. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18124. (t (org-drag-element-backward))))
  18125. (defun org-metadown (&optional arg)
  18126. "Move subtree down or move table row down.
  18127. Calls `org-move-subtree-down' or `org-table-move-row' or
  18128. `org-move-item-down', depending on context. See the individual
  18129. commands for more information."
  18130. (interactive "P")
  18131. (cond
  18132. ((run-hook-with-args-until-success 'org-metadown-hook))
  18133. ((org-region-active-p)
  18134. (let* ((a (min (region-beginning) (region-end)))
  18135. (b (max (region-beginning) (region-end)))
  18136. (c (save-excursion (goto-char b)
  18137. (move-beginning-of-line 1)))
  18138. (d (save-excursion (goto-char b)
  18139. (move-end-of-line 1) (1+ (point)))))
  18140. (transpose-regions a b c d)
  18141. (goto-char d)))
  18142. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18143. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18144. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18145. (t (org-drag-element-forward))))
  18146. (defun org-shiftup (&optional arg)
  18147. "Increase item in timestamp or increase priority of current headline.
  18148. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18149. depending on context. See the individual commands for more information."
  18150. (interactive "P")
  18151. (cond
  18152. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18153. ((and org-support-shift-select (org-region-active-p))
  18154. (org-call-for-shift-select 'previous-line))
  18155. ((org-at-timestamp-p t)
  18156. (call-interactively (if org-edit-timestamp-down-means-later
  18157. 'org-timestamp-down 'org-timestamp-up)))
  18158. ((and (not (eq org-support-shift-select 'always))
  18159. org-enable-priority-commands
  18160. (org-at-heading-p))
  18161. (call-interactively 'org-priority-up))
  18162. ((and (not org-support-shift-select) (org-at-item-p))
  18163. (call-interactively 'org-previous-item))
  18164. ((org-clocktable-try-shift 'up arg))
  18165. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18166. (org-support-shift-select
  18167. (org-call-for-shift-select 'previous-line))
  18168. (t (org-shiftselect-error))))
  18169. (defun org-shiftdown (&optional arg)
  18170. "Decrease item in timestamp or decrease priority of current headline.
  18171. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18172. depending on context. See the individual commands for more information."
  18173. (interactive "P")
  18174. (cond
  18175. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18176. ((and org-support-shift-select (org-region-active-p))
  18177. (org-call-for-shift-select 'next-line))
  18178. ((org-at-timestamp-p t)
  18179. (call-interactively (if org-edit-timestamp-down-means-later
  18180. 'org-timestamp-up 'org-timestamp-down)))
  18181. ((and (not (eq org-support-shift-select 'always))
  18182. org-enable-priority-commands
  18183. (org-at-heading-p))
  18184. (call-interactively 'org-priority-down))
  18185. ((and (not org-support-shift-select) (org-at-item-p))
  18186. (call-interactively 'org-next-item))
  18187. ((org-clocktable-try-shift 'down arg))
  18188. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18189. (org-support-shift-select
  18190. (org-call-for-shift-select 'next-line))
  18191. (t (org-shiftselect-error))))
  18192. (defun org-shiftright (&optional arg)
  18193. "Cycle the thing at point or in the current line, depending on context.
  18194. Depending on context, this does one of the following:
  18195. - switch a timestamp at point one day into the future
  18196. - on a headline, switch to the next TODO keyword.
  18197. - on an item, switch entire list to the next bullet type
  18198. - on a property line, switch to the next allowed value
  18199. - on a clocktable definition line, move time block into the future"
  18200. (interactive "P")
  18201. (cond
  18202. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18203. ((and org-support-shift-select (org-region-active-p))
  18204. (org-call-for-shift-select 'forward-char))
  18205. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18206. ((and (not (eq org-support-shift-select 'always))
  18207. (org-at-heading-p))
  18208. (let ((org-inhibit-logging
  18209. (not org-treat-S-cursor-todo-selection-as-state-change))
  18210. (org-inhibit-blocking
  18211. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18212. (org-call-with-arg 'org-todo 'right)))
  18213. ((or (and org-support-shift-select
  18214. (not (eq org-support-shift-select 'always))
  18215. (org-at-item-bullet-p))
  18216. (and (not org-support-shift-select) (org-at-item-p)))
  18217. (org-call-with-arg 'org-cycle-list-bullet nil))
  18218. ((and (not (eq org-support-shift-select 'always))
  18219. (org-at-property-p))
  18220. (call-interactively 'org-property-next-allowed-value))
  18221. ((org-clocktable-try-shift 'right arg))
  18222. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  18223. (org-support-shift-select
  18224. (org-call-for-shift-select 'forward-char))
  18225. (t (org-shiftselect-error))))
  18226. (defun org-shiftleft (&optional arg)
  18227. "Cycle the thing at point or in the current line, depending on context.
  18228. Depending on context, this does one of the following:
  18229. - switch a timestamp at point one day into the past
  18230. - on a headline, switch to the previous TODO keyword.
  18231. - on an item, switch entire list to the previous bullet type
  18232. - on a property line, switch to the previous allowed value
  18233. - on a clocktable definition line, move time block into the past"
  18234. (interactive "P")
  18235. (cond
  18236. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  18237. ((and org-support-shift-select (org-region-active-p))
  18238. (org-call-for-shift-select 'backward-char))
  18239. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  18240. ((and (not (eq org-support-shift-select 'always))
  18241. (org-at-heading-p))
  18242. (let ((org-inhibit-logging
  18243. (not org-treat-S-cursor-todo-selection-as-state-change))
  18244. (org-inhibit-blocking
  18245. (not org-treat-S-cursor-todo-selection-as-state-change)))
  18246. (org-call-with-arg 'org-todo 'left)))
  18247. ((or (and org-support-shift-select
  18248. (not (eq org-support-shift-select 'always))
  18249. (org-at-item-bullet-p))
  18250. (and (not org-support-shift-select) (org-at-item-p)))
  18251. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  18252. ((and (not (eq org-support-shift-select 'always))
  18253. (org-at-property-p))
  18254. (call-interactively 'org-property-previous-allowed-value))
  18255. ((org-clocktable-try-shift 'left arg))
  18256. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  18257. (org-support-shift-select
  18258. (org-call-for-shift-select 'backward-char))
  18259. (t (org-shiftselect-error))))
  18260. (defun org-shiftcontrolright ()
  18261. "Switch to next TODO set."
  18262. (interactive)
  18263. (cond
  18264. ((and org-support-shift-select (org-region-active-p))
  18265. (org-call-for-shift-select 'forward-word))
  18266. ((and (not (eq org-support-shift-select 'always))
  18267. (org-at-heading-p))
  18268. (org-call-with-arg 'org-todo 'nextset))
  18269. (org-support-shift-select
  18270. (org-call-for-shift-select 'forward-word))
  18271. (t (org-shiftselect-error))))
  18272. (defun org-shiftcontrolleft ()
  18273. "Switch to previous TODO set."
  18274. (interactive)
  18275. (cond
  18276. ((and org-support-shift-select (org-region-active-p))
  18277. (org-call-for-shift-select 'backward-word))
  18278. ((and (not (eq org-support-shift-select 'always))
  18279. (org-at-heading-p))
  18280. (org-call-with-arg 'org-todo 'previousset))
  18281. (org-support-shift-select
  18282. (org-call-for-shift-select 'backward-word))
  18283. (t (org-shiftselect-error))))
  18284. (defun org-shiftcontrolup (&optional n)
  18285. "Change timestamps synchronously up in CLOCK log lines.
  18286. Optional argument N tells to change by that many units."
  18287. (interactive "P")
  18288. (cond ((and (not org-support-shift-select)
  18289. (org-at-clock-log-p)
  18290. (org-at-timestamp-p t))
  18291. (org-clock-timestamps-up n))
  18292. (t (org-shiftselect-error))))
  18293. (defun org-shiftcontroldown (&optional n)
  18294. "Change timestamps synchronously down in CLOCK log lines.
  18295. Optional argument N tells to change by that many units."
  18296. (interactive "P")
  18297. (cond ((and (not org-support-shift-select)
  18298. (org-at-clock-log-p)
  18299. (org-at-timestamp-p t))
  18300. (org-clock-timestamps-down n))
  18301. (t (org-shiftselect-error))))
  18302. (defun org-ctrl-c-ret ()
  18303. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  18304. (interactive)
  18305. (cond
  18306. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  18307. (t (call-interactively 'org-insert-heading))))
  18308. (defun org-find-visible ()
  18309. (let ((s (point)))
  18310. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18311. (get-char-property s 'invisible)))
  18312. s))
  18313. (defun org-find-invisible ()
  18314. (let ((s (point)))
  18315. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  18316. (not (get-char-property s 'invisible))))
  18317. s))
  18318. (defun org-copy-visible (beg end)
  18319. "Copy the visible parts of the region."
  18320. (interactive "r")
  18321. (let (snippets s)
  18322. (save-excursion
  18323. (save-restriction
  18324. (narrow-to-region beg end)
  18325. (setq s (goto-char (point-min)))
  18326. (while (not (= (point) (point-max)))
  18327. (goto-char (org-find-invisible))
  18328. (push (buffer-substring s (point)) snippets)
  18329. (setq s (goto-char (org-find-visible))))))
  18330. (kill-new (apply 'concat (nreverse snippets)))))
  18331. (defun org-copy-special ()
  18332. "Copy region in table or copy current subtree.
  18333. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  18334. See the individual commands for more information."
  18335. (interactive)
  18336. (call-interactively
  18337. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18338. (defun org-cut-special ()
  18339. "Cut region in table or cut current subtree.
  18340. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18341. See the individual commands for more information."
  18342. (interactive)
  18343. (call-interactively
  18344. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18345. (defun org-paste-special (arg)
  18346. "Paste rectangular region into table, or past subtree relative to level.
  18347. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18348. See the individual commands for more information."
  18349. (interactive "P")
  18350. (if (org-at-table-p)
  18351. (org-table-paste-rectangle)
  18352. (org-paste-subtree arg)))
  18353. (defsubst org-in-fixed-width-region-p ()
  18354. "Is point in a fixed-width region?"
  18355. (save-match-data
  18356. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18357. (defun org-edit-special (&optional arg)
  18358. "Call a special editor for the element at point.
  18359. When at a table, call the formula editor with `org-table-edit-formulas'.
  18360. When in a source code block, call `org-edit-src-code'.
  18361. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18362. When at an #+INCLUDE keyword, visit the included file.
  18363. On a link, call `ffap' to visit the link at point.
  18364. Otherwise, return a user error."
  18365. (interactive "P")
  18366. (let ((element (org-element-at-point)))
  18367. (assert (not buffer-read-only) nil
  18368. "Buffer is read-only: %s" (buffer-name))
  18369. (case (org-element-type element)
  18370. (src-block
  18371. (if (not arg) (org-edit-src-code)
  18372. (let* ((info (org-babel-get-src-block-info))
  18373. (lang (nth 0 info))
  18374. (params (nth 2 info))
  18375. (session (cdr (assq :session params))))
  18376. (if (not session) (org-edit-src-code)
  18377. ;; At a src-block with a session and function called with
  18378. ;; an ARG: switch to the buffer related to the inferior
  18379. ;; process.
  18380. (switch-to-buffer
  18381. (funcall (intern (concat "org-babel-prep-session:" lang))
  18382. session params))))))
  18383. (keyword
  18384. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18385. (find-file
  18386. (org-remove-double-quotes
  18387. (car (org-split-string (org-element-property :value element)))))
  18388. (user-error "No special environment to edit here")))
  18389. (table
  18390. (if (eq (org-element-property :type element) 'table.el)
  18391. (org-edit-src-code)
  18392. (call-interactively 'org-table-edit-formulas)))
  18393. ;; Only Org tables contain `table-row' type elements.
  18394. (table-row (call-interactively 'org-table-edit-formulas))
  18395. ((example-block export-block) (org-edit-src-code))
  18396. (fixed-width (org-edit-fixed-width-region))
  18397. (otherwise
  18398. ;; No notable element at point. Though, we may be at a link,
  18399. ;; which is an object. Thus, scan deeper.
  18400. (if (eq (org-element-type (org-element-context element)) 'link)
  18401. (call-interactively 'ffap)
  18402. (user-error "No special environment to edit here"))))))
  18403. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18404. (defun org-ctrl-c-ctrl-c (&optional arg)
  18405. "Set tags in headline, or update according to changed information at point.
  18406. This command does many different things, depending on context:
  18407. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18408. this is what we do.
  18409. - If the cursor is on a statistics cookie, update it.
  18410. - If the cursor is in a headline, prompt for tags and insert them
  18411. into the current line, aligned to `org-tags-column'. When called
  18412. with prefix arg, realign all tags in the current buffer.
  18413. - If the cursor is in one of the special #+KEYWORD lines, this
  18414. triggers scanning the buffer for these lines and updating the
  18415. information.
  18416. - If the cursor is inside a table, realign the table. This command
  18417. works even if the automatic table editor has been turned off.
  18418. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18419. the entire table.
  18420. - If the cursor is at a footnote reference or definition, jump to
  18421. the corresponding definition or references, respectively.
  18422. - If the cursor is a the beginning of a dynamic block, update it.
  18423. - If the current buffer is a capture buffer, close note and file it.
  18424. - If the cursor is on a <<<target>>>, update radio targets and
  18425. corresponding links in this buffer.
  18426. - If the cursor is on a numbered item in a plain list, renumber the
  18427. ordered list.
  18428. - If the cursor is on a checkbox, toggle it.
  18429. - If the cursor is on a code block, evaluate it. The variable
  18430. `org-confirm-babel-evaluate' can be used to control prompting
  18431. before code block evaluation, by default every code block
  18432. evaluation requires confirmation. Code block evaluation can be
  18433. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18434. (interactive "P")
  18435. (cond
  18436. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18437. org-occur-highlights
  18438. org-latex-fragment-image-overlays)
  18439. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18440. (org-remove-occur-highlights)
  18441. (org-remove-latex-fragment-image-overlays)
  18442. (message "Temporary highlights/overlays removed from current buffer"))
  18443. ((and (local-variable-p 'org-finish-function (current-buffer))
  18444. (fboundp org-finish-function))
  18445. (funcall org-finish-function))
  18446. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18447. (t
  18448. (let* ((context (org-element-context)) (type (org-element-type context)))
  18449. ;; Test if point is within a blank line.
  18450. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18451. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18452. (user-error "C-c C-c can do nothing useful at this location"))
  18453. ;; For convenience: at the first line of a paragraph on the
  18454. ;; same line as an item, apply function on that item instead.
  18455. (when (eq type 'paragraph)
  18456. (let ((parent (org-element-property :parent context)))
  18457. (when (and (eq (org-element-type parent) 'item)
  18458. (= (point-at-bol) (org-element-property :begin parent)))
  18459. (setq context parent type 'item))))
  18460. ;; Act according to type of element or object at point.
  18461. (case type
  18462. (clock (org-clock-update-time-maybe))
  18463. (dynamic-block
  18464. (save-excursion
  18465. (goto-char (org-element-property :post-affiliated context))
  18466. (org-update-dblock)))
  18467. (footnote-definition
  18468. (goto-char (org-element-property :post-affiliated context))
  18469. (call-interactively 'org-footnote-action))
  18470. (footnote-reference (call-interactively 'org-footnote-action))
  18471. ((headline inlinetask)
  18472. (save-excursion (goto-char (org-element-property :begin context))
  18473. (call-interactively 'org-set-tags)))
  18474. (item
  18475. ;; At an item: a double C-u set checkbox to "[-]"
  18476. ;; unconditionally, whereas a single one will toggle its
  18477. ;; presence. Without an universal argument, if the item
  18478. ;; has a checkbox, toggle it. Otherwise repair the list.
  18479. (let* ((box (org-element-property :checkbox context))
  18480. (struct (org-element-property :structure context))
  18481. (old-struct (copy-tree struct))
  18482. (parents (org-list-parents-alist struct))
  18483. (prevs (org-list-prevs-alist struct))
  18484. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18485. (org-list-set-checkbox
  18486. (org-element-property :begin context) struct
  18487. (cond ((equal arg '(16)) "[-]")
  18488. ((and (not box) (equal arg '(4))) "[ ]")
  18489. ((or (not box) (equal arg '(4))) nil)
  18490. ((eq box 'on) "[ ]")
  18491. (t "[X]")))
  18492. ;; Mimic `org-list-write-struct' but with grabbing
  18493. ;; a return value from `org-list-struct-fix-box'.
  18494. (org-list-struct-fix-ind struct parents 2)
  18495. (org-list-struct-fix-item-end struct)
  18496. (org-list-struct-fix-bul struct prevs)
  18497. (org-list-struct-fix-ind struct parents)
  18498. (let ((block-item
  18499. (org-list-struct-fix-box struct parents prevs orderedp)))
  18500. (if (and box (equal struct old-struct))
  18501. (if (equal arg '(16))
  18502. (message "Checkboxes already reset")
  18503. (user-error "Cannot toggle this checkbox: %s"
  18504. (if (eq box 'on)
  18505. "all subitems checked"
  18506. "unchecked subitems")))
  18507. (org-list-struct-apply-struct struct old-struct)
  18508. (org-update-checkbox-count-maybe))
  18509. (when block-item
  18510. (message "Checkboxes were removed due to empty box at line %d"
  18511. (org-current-line block-item))))))
  18512. (keyword
  18513. (let ((org-inhibit-startup-visibility-stuff t)
  18514. (org-startup-align-all-tables nil))
  18515. (when (boundp 'org-table-coordinate-overlays)
  18516. (mapc 'delete-overlay org-table-coordinate-overlays)
  18517. (setq org-table-coordinate-overlays nil))
  18518. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18519. (message "Local setup has been refreshed"))
  18520. (plain-list
  18521. ;; At a plain list, with a double C-u argument, set
  18522. ;; checkboxes of each item to "[-]", whereas a single one
  18523. ;; will toggle their presence according to the state of the
  18524. ;; first item in the list. Without an argument, repair the
  18525. ;; list.
  18526. (let* ((begin (org-element-property :contents-begin context))
  18527. (beginm (move-marker (make-marker) begin))
  18528. (struct (org-element-property :structure context))
  18529. (old-struct (copy-tree struct))
  18530. (first-box (save-excursion
  18531. (goto-char begin)
  18532. (looking-at org-list-full-item-re)
  18533. (match-string-no-properties 3)))
  18534. (new-box (cond ((equal arg '(16)) "[-]")
  18535. ((equal arg '(4)) (unless first-box "[ ]"))
  18536. ((equal first-box "[X]") "[ ]")
  18537. (t "[X]"))))
  18538. (cond
  18539. (arg
  18540. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18541. (org-list-get-all-items
  18542. begin struct (org-list-prevs-alist struct))))
  18543. ((and first-box (eq (point) begin))
  18544. ;; For convenience, when point is at bol on the first
  18545. ;; item of the list and no argument is provided, simply
  18546. ;; toggle checkbox of that item, if any.
  18547. (org-list-set-checkbox begin struct new-box)))
  18548. (org-list-write-struct
  18549. struct (org-list-parents-alist struct) old-struct)
  18550. (org-update-checkbox-count-maybe)
  18551. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  18552. ((property-drawer node-property)
  18553. (call-interactively 'org-property-action))
  18554. ((radio-target target)
  18555. (call-interactively 'org-update-radio-target-regexp))
  18556. (statistics-cookie
  18557. (call-interactively 'org-update-statistics-cookies))
  18558. ((table table-cell table-row)
  18559. ;; At a table, recalculate every field and align it. Also
  18560. ;; send the table if necessary. If the table has
  18561. ;; a `table.el' type, just give up. At a table row or
  18562. ;; cell, maybe recalculate line but always align table.
  18563. (if (eq (org-element-property :type context) 'table.el)
  18564. (message "Use C-c ' to edit table.el tables")
  18565. (let ((org-enable-table-editor t))
  18566. (if (or (eq type 'table)
  18567. ;; Check if point is at a TBLFM line.
  18568. (and (eq type 'table-row)
  18569. (= (point) (org-element-property :end context))))
  18570. (save-excursion
  18571. (if (org-at-TBLFM-p) (org-calc-current-TBLFM)
  18572. (goto-char (org-element-property :contents-begin context))
  18573. (org-call-with-arg 'org-table-recalculate (or arg t))
  18574. (orgtbl-send-table 'maybe)))
  18575. (org-table-maybe-eval-formula)
  18576. (cond (arg (call-interactively 'org-table-recalculate))
  18577. ((org-table-maybe-recalculate-line))
  18578. (t (org-table-align)))))))
  18579. (timestamp (org-timestamp-change 0 'day))
  18580. (otherwise
  18581. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18582. (user-error
  18583. "C-c C-c can do nothing useful at this location")))))))))
  18584. (defun org-mode-restart ()
  18585. "Restart Org-mode, to scan again for special lines.
  18586. Also updates the keyword regular expressions."
  18587. (interactive)
  18588. (org-mode)
  18589. (message "Org-mode restarted"))
  18590. (defun org-kill-note-or-show-branches ()
  18591. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18592. (interactive)
  18593. (if (not org-finish-function)
  18594. (progn
  18595. (hide-subtree)
  18596. (call-interactively 'show-branches))
  18597. (let ((org-note-abort t))
  18598. (funcall org-finish-function))))
  18599. (defun org-ctrl-o (n)
  18600. "Insert a new row in tables, call `open-line' elsewhere."
  18601. (interactive "*p")
  18602. (if (org-at-table-p)
  18603. (org-table-insert-row)
  18604. (open-line n)))
  18605. (defun org-return (&optional indent)
  18606. "Goto next table row or insert a newline.
  18607. Calls `org-table-next-row' or `newline', depending on context.
  18608. See the individual commands for more information."
  18609. (interactive)
  18610. (let (org-ts-what)
  18611. (cond
  18612. ((or (bobp) (org-in-src-block-p))
  18613. (if indent (newline-and-indent) (newline)))
  18614. ((org-at-table-p)
  18615. (org-table-justify-field-maybe)
  18616. (call-interactively 'org-table-next-row))
  18617. ;; when `newline-and-indent' is called within a list, make sure
  18618. ;; text moved stays inside the item.
  18619. ((and (org-in-item-p) indent)
  18620. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18621. (progn
  18622. (save-match-data (newline))
  18623. (org-indent-line-to (length (match-string 0))))
  18624. (let ((ind (org-get-indentation)))
  18625. (newline)
  18626. (if (org-looking-back org-list-end-re)
  18627. (org-indent-line)
  18628. (org-indent-line-to ind)))))
  18629. ((and org-return-follows-link
  18630. (org-at-timestamp-p t)
  18631. (not (eq org-ts-what 'after)))
  18632. (org-follow-timestamp-link))
  18633. ((and org-return-follows-link
  18634. (let ((tprop (get-text-property (point) 'face)))
  18635. (or (eq tprop 'org-link)
  18636. (and (listp tprop) (memq 'org-link tprop)))))
  18637. (call-interactively 'org-open-at-point))
  18638. ((and (org-at-heading-p)
  18639. (looking-at
  18640. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18641. (org-show-entry)
  18642. (end-of-line 1)
  18643. (newline))
  18644. (t (if indent (newline-and-indent) (newline))))))
  18645. (defun org-return-indent ()
  18646. "Goto next table row or insert a newline and indent.
  18647. Calls `org-table-next-row' or `newline-and-indent', depending on
  18648. context. See the individual commands for more information."
  18649. (interactive)
  18650. (org-return t))
  18651. (defun org-ctrl-c-star ()
  18652. "Compute table, or change heading status of lines.
  18653. Calls `org-table-recalculate' or `org-toggle-heading',
  18654. depending on context."
  18655. (interactive)
  18656. (cond
  18657. ((org-at-table-p)
  18658. (call-interactively 'org-table-recalculate))
  18659. (t
  18660. ;; Convert all lines in region to list items
  18661. (call-interactively 'org-toggle-heading))))
  18662. (defun org-ctrl-c-minus ()
  18663. "Insert separator line in table or modify bullet status of line.
  18664. Also turns a plain line or a region of lines into list items.
  18665. Calls `org-table-insert-hline', `org-toggle-item', or
  18666. `org-cycle-list-bullet', depending on context."
  18667. (interactive)
  18668. (cond
  18669. ((org-at-table-p)
  18670. (call-interactively 'org-table-insert-hline))
  18671. ((org-region-active-p)
  18672. (call-interactively 'org-toggle-item))
  18673. ((org-in-item-p)
  18674. (call-interactively 'org-cycle-list-bullet))
  18675. (t
  18676. (call-interactively 'org-toggle-item))))
  18677. (defun org-toggle-item (arg)
  18678. "Convert headings or normal lines to items, items to normal lines.
  18679. If there is no active region, only the current line is considered.
  18680. If the first non blank line in the region is a headline, convert
  18681. all headlines to items, shifting text accordingly.
  18682. If it is an item, convert all items to normal lines.
  18683. If it is normal text, change region into a list of items.
  18684. With a prefix argument ARG, change the region in a single item."
  18685. (interactive "P")
  18686. (let ((shift-text
  18687. (function
  18688. ;; Shift text in current section to IND, from point to END.
  18689. ;; The function leaves point to END line.
  18690. (lambda (ind end)
  18691. (let ((min-i 1000) (end (copy-marker end)))
  18692. ;; First determine the minimum indentation (MIN-I) of
  18693. ;; the text.
  18694. (save-excursion
  18695. (catch 'exit
  18696. (while (< (point) end)
  18697. (let ((i (org-get-indentation)))
  18698. (cond
  18699. ;; Skip blank lines and inline tasks.
  18700. ((looking-at "^[ \t]*$"))
  18701. ((looking-at org-outline-regexp-bol))
  18702. ;; We can't find less than 0 indentation.
  18703. ((zerop i) (throw 'exit (setq min-i 0)))
  18704. ((< i min-i) (setq min-i i))))
  18705. (forward-line))))
  18706. ;; Then indent each line so that a line indented to
  18707. ;; MIN-I becomes indented to IND. Ignore blank lines
  18708. ;; and inline tasks in the process.
  18709. (let ((delta (- ind min-i)))
  18710. (while (< (point) end)
  18711. (unless (or (looking-at "^[ \t]*$")
  18712. (looking-at org-outline-regexp-bol))
  18713. (org-indent-line-to (+ (org-get-indentation) delta)))
  18714. (forward-line)))))))
  18715. (skip-blanks
  18716. (function
  18717. ;; Return beginning of first non-blank line, starting from
  18718. ;; line at POS.
  18719. (lambda (pos)
  18720. (save-excursion
  18721. (goto-char pos)
  18722. (skip-chars-forward " \r\t\n")
  18723. (point-at-bol)))))
  18724. beg end)
  18725. ;; Determine boundaries of changes.
  18726. (if (org-region-active-p)
  18727. (setq beg (funcall skip-blanks (region-beginning))
  18728. end (copy-marker (region-end)))
  18729. (setq beg (funcall skip-blanks (point-at-bol))
  18730. end (copy-marker (point-at-eol))))
  18731. ;; Depending on the starting line, choose an action on the text
  18732. ;; between BEG and END.
  18733. (org-with-limited-levels
  18734. (save-excursion
  18735. (goto-char beg)
  18736. (cond
  18737. ;; Case 1. Start at an item: de-itemize. Note that it only
  18738. ;; happens when a region is active: `org-ctrl-c-minus'
  18739. ;; would call `org-cycle-list-bullet' otherwise.
  18740. ((org-at-item-p)
  18741. (while (< (point) end)
  18742. (when (org-at-item-p)
  18743. (skip-chars-forward " \t")
  18744. (delete-region (point) (match-end 0)))
  18745. (forward-line)))
  18746. ;; Case 2. Start at an heading: convert to items.
  18747. ((org-at-heading-p)
  18748. (let* ((bul (org-list-bullet-string "-"))
  18749. (bul-len (length bul))
  18750. ;; Indentation of the first heading. It should be
  18751. ;; relative to the indentation of its parent, if any.
  18752. (start-ind (save-excursion
  18753. (cond
  18754. ((not org-adapt-indentation) 0)
  18755. ((not (outline-previous-heading)) 0)
  18756. (t (length (match-string 0))))))
  18757. ;; Level of first heading. Further headings will be
  18758. ;; compared to it to determine hierarchy in the list.
  18759. (ref-level (org-reduced-level (org-outline-level))))
  18760. (while (< (point) end)
  18761. (let* ((level (org-reduced-level (org-outline-level)))
  18762. (delta (max 0 (- level ref-level))))
  18763. ;; If current headline is less indented than the first
  18764. ;; one, set it as reference, in order to preserve
  18765. ;; subtrees.
  18766. (when (< level ref-level) (setq ref-level level))
  18767. (replace-match bul t t)
  18768. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18769. ;; Ensure all text down to END (or SECTION-END) belongs
  18770. ;; to the newly created item.
  18771. (let ((section-end (save-excursion
  18772. (or (outline-next-heading) (point)))))
  18773. (forward-line)
  18774. (funcall shift-text
  18775. (+ start-ind (* (1+ delta) bul-len))
  18776. (min end section-end)))))))
  18777. ;; Case 3. Normal line with ARG: make the first line of region
  18778. ;; an item, and shift indentation of others lines to
  18779. ;; set them as item's body.
  18780. (arg (let* ((bul (org-list-bullet-string "-"))
  18781. (bul-len (length bul))
  18782. (ref-ind (org-get-indentation)))
  18783. (skip-chars-forward " \t")
  18784. (insert bul)
  18785. (forward-line)
  18786. (while (< (point) end)
  18787. ;; Ensure that lines less indented than first one
  18788. ;; still get included in item body.
  18789. (funcall shift-text
  18790. (+ ref-ind bul-len)
  18791. (min end (save-excursion (or (outline-next-heading)
  18792. (point)))))
  18793. (forward-line))))
  18794. ;; Case 4. Normal line without ARG: turn each non-item line
  18795. ;; into an item.
  18796. (t
  18797. (while (< (point) end)
  18798. (unless (or (org-at-heading-p) (org-at-item-p))
  18799. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18800. (replace-match
  18801. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18802. (forward-line))))))))
  18803. (defun org-toggle-heading (&optional nstars)
  18804. "Convert headings to normal text, or items or text to headings.
  18805. If there is no active region, only convert the current line.
  18806. With a \\[universal-argument] prefix, convert the whole list at
  18807. point into heading.
  18808. In a region:
  18809. - If the first non blank line is a headline, remove the stars
  18810. from all headlines in the region.
  18811. - If it is a normal line, turn each and every normal line (i.e.,
  18812. not an heading or an item) in the region into headings. If you
  18813. want to convert only the first line of this region, use one
  18814. universal prefix argument.
  18815. - If it is a plain list item, turn all plain list items into headings.
  18816. When converting a line into a heading, the number of stars is chosen
  18817. such that the lines become children of the current entry. However,
  18818. when a numeric prefix argument is given, its value determines the
  18819. number of stars to add."
  18820. (interactive "P")
  18821. (let ((skip-blanks
  18822. (function
  18823. ;; Return beginning of first non-blank line, starting from
  18824. ;; line at POS.
  18825. (lambda (pos)
  18826. (save-excursion
  18827. (goto-char pos)
  18828. (while (org-at-comment-p) (forward-line))
  18829. (skip-chars-forward " \r\t\n")
  18830. (point-at-bol)))))
  18831. beg end toggled)
  18832. ;; Determine boundaries of changes. If a universal prefix has
  18833. ;; been given, put the list in a region. If region ends at a bol,
  18834. ;; do not consider the last line to be in the region.
  18835. (when (and current-prefix-arg (org-at-item-p))
  18836. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  18837. (org-mark-element))
  18838. (if (org-region-active-p)
  18839. (setq beg (funcall skip-blanks (region-beginning))
  18840. end (copy-marker (save-excursion
  18841. (goto-char (region-end))
  18842. (if (bolp) (point) (point-at-eol)))))
  18843. (setq beg (funcall skip-blanks (point-at-bol))
  18844. end (copy-marker (point-at-eol))))
  18845. ;; Ensure inline tasks don't count as headings.
  18846. (org-with-limited-levels
  18847. (save-excursion
  18848. (goto-char beg)
  18849. (cond
  18850. ;; Case 1. Started at an heading: de-star headings.
  18851. ((org-at-heading-p)
  18852. (while (< (point) end)
  18853. (when (org-at-heading-p t)
  18854. (looking-at org-outline-regexp) (replace-match "")
  18855. (setq toggled t))
  18856. (forward-line)))
  18857. ;; Case 2. Started at an item: change items into headlines.
  18858. ;; One star will be added by `org-list-to-subtree'.
  18859. ((org-at-item-p)
  18860. (let* ((stars (make-string
  18861. ;; subtract the star that will be added again by
  18862. ;; `org-list-to-subtree'
  18863. (if (numberp nstars) (1- nstars)
  18864. (or (org-current-level) 0))
  18865. ?*))
  18866. (add-stars
  18867. (cond (nstars "") ; stars from prefix only
  18868. ((equal stars "") "") ; before first heading
  18869. (org-odd-levels-only "*") ; inside heading, odd
  18870. (t "")))) ; inside heading, oddeven
  18871. (while (< (point) end)
  18872. (when (org-at-item-p)
  18873. ;; Pay attention to cases when region ends before list.
  18874. (let* ((struct (org-list-struct))
  18875. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18876. (save-restriction
  18877. (narrow-to-region (point) list-end)
  18878. (insert
  18879. (org-list-to-subtree
  18880. (org-list-parse-list t)
  18881. '(:istart (concat stars add-stars (funcall get-stars depth))
  18882. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18883. (setq toggled t))
  18884. (forward-line))))
  18885. ;; Case 3. Started at normal text: make every line an heading,
  18886. ;; skipping headlines and items.
  18887. (t (let* ((stars
  18888. (make-string
  18889. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  18890. (add-stars
  18891. (cond (nstars "") ; stars from prefix only
  18892. ((equal stars "") "*") ; before first heading
  18893. (org-odd-levels-only "**") ; inside heading, odd
  18894. (t "*"))) ; inside heading, oddeven
  18895. (rpl (concat stars add-stars " "))
  18896. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  18897. (while (< (point) (if (equal nstars '(4)) lend end))
  18898. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18899. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18900. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18901. (forward-line)))))))
  18902. (unless toggled (message "Cannot toggle heading from here"))))
  18903. (defun org-meta-return (&optional arg)
  18904. "Insert a new heading or wrap a region in a table.
  18905. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18906. See the individual commands for more information."
  18907. (interactive "P")
  18908. (cond
  18909. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18910. ((or (org-at-drawer-p) (org-at-property-p))
  18911. (newline-and-indent))
  18912. ((org-at-table-p)
  18913. (call-interactively 'org-table-wrap-region))
  18914. (t (call-interactively 'org-insert-heading))))
  18915. ;;; Menu entries
  18916. (defsubst org-in-subtree-not-table-p ()
  18917. "Are we in a subtree and not in a table?"
  18918. (and (not (org-before-first-heading-p))
  18919. (not (org-at-table-p))))
  18920. ;; Define the Org-mode menus
  18921. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18922. '("Tbl"
  18923. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18924. ["Next Field" org-cycle (org-at-table-p)]
  18925. ["Previous Field" org-shifttab (org-at-table-p)]
  18926. ["Next Row" org-return (org-at-table-p)]
  18927. "--"
  18928. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18929. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18930. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18931. "--"
  18932. ("Column"
  18933. ["Move Column Left" org-metaleft (org-at-table-p)]
  18934. ["Move Column Right" org-metaright (org-at-table-p)]
  18935. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18936. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18937. ("Row"
  18938. ["Move Row Up" org-metaup (org-at-table-p)]
  18939. ["Move Row Down" org-metadown (org-at-table-p)]
  18940. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18941. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18942. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18943. "--"
  18944. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18945. ("Rectangle"
  18946. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18947. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18948. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18949. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18950. "--"
  18951. ("Calculate"
  18952. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18953. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18954. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18955. "--"
  18956. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18957. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18958. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18959. "--"
  18960. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18961. "--"
  18962. ["Sum Column/Rectangle" org-table-sum
  18963. (or (org-at-table-p) (org-region-active-p))]
  18964. ["Which Column?" org-table-current-column (org-at-table-p)])
  18965. ["Debug Formulas"
  18966. org-table-toggle-formula-debugger
  18967. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18968. ["Show Col/Row Numbers"
  18969. org-table-toggle-coordinate-overlays
  18970. :style toggle
  18971. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18972. "--"
  18973. ["Create" org-table-create (and (not (org-at-table-p))
  18974. org-enable-table-editor)]
  18975. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18976. ["Import from File" org-table-import (not (org-at-table-p))]
  18977. ["Export to File" org-table-export (org-at-table-p)]
  18978. "--"
  18979. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18980. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18981. '("Org"
  18982. ("Show/Hide"
  18983. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18984. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18985. ["Sparse Tree..." org-sparse-tree t]
  18986. ["Reveal Context" org-reveal t]
  18987. ["Show All" show-all t]
  18988. "--"
  18989. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18990. "--"
  18991. ["New Heading" org-insert-heading t]
  18992. ("Navigate Headings"
  18993. ["Up" outline-up-heading t]
  18994. ["Next" outline-next-visible-heading t]
  18995. ["Previous" outline-previous-visible-heading t]
  18996. ["Next Same Level" outline-forward-same-level t]
  18997. ["Previous Same Level" outline-backward-same-level t]
  18998. "--"
  18999. ["Jump" org-goto t])
  19000. ("Edit Structure"
  19001. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19002. "--"
  19003. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  19004. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  19005. "--"
  19006. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19007. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19008. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19009. "--"
  19010. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19011. "--"
  19012. ["Copy visible text" org-copy-visible t]
  19013. "--"
  19014. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19015. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19016. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19017. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19018. "--"
  19019. ["Sort Region/Children" org-sort t]
  19020. "--"
  19021. ["Convert to odd levels" org-convert-to-odd-levels t]
  19022. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19023. ("Editing"
  19024. ["Emphasis..." org-emphasize t]
  19025. ["Edit Source Example" org-edit-special t]
  19026. "--"
  19027. ["Footnote new/jump" org-footnote-action t]
  19028. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19029. ("Archive"
  19030. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19031. "--"
  19032. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  19033. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19034. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19035. )
  19036. "--"
  19037. ("Hyperlinks"
  19038. ["Store Link (Global)" org-store-link t]
  19039. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19040. ["Insert Link" org-insert-link t]
  19041. ["Follow Link" org-open-at-point t]
  19042. "--"
  19043. ["Next link" org-next-link t]
  19044. ["Previous link" org-previous-link t]
  19045. "--"
  19046. ["Descriptive Links"
  19047. org-toggle-link-display
  19048. :style radio
  19049. :selected org-descriptive-links
  19050. ]
  19051. ["Literal Links"
  19052. org-toggle-link-display
  19053. :style radio
  19054. :selected (not org-descriptive-links)])
  19055. "--"
  19056. ("TODO Lists"
  19057. ["TODO/DONE/-" org-todo t]
  19058. ("Select keyword"
  19059. ["Next keyword" org-shiftright (org-at-heading-p)]
  19060. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19061. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19062. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19063. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19064. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19065. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19066. "--"
  19067. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19068. :selected org-enforce-todo-dependencies :style toggle :active t]
  19069. "Settings for tree at point"
  19070. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19071. :selected (org-entry-get nil "ORDERED")
  19072. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19073. ["Do Children parallel" org-toggle-ordered-property :style radio
  19074. :selected (not (org-entry-get nil "ORDERED"))
  19075. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19076. "--"
  19077. ["Set Priority" org-priority t]
  19078. ["Priority Up" org-shiftup t]
  19079. ["Priority Down" org-shiftdown t]
  19080. "--"
  19081. ["Get news from all feeds" org-feed-update-all t]
  19082. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19083. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19084. ("TAGS and Properties"
  19085. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19086. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19087. "--"
  19088. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19089. ["Column view of properties" org-columns t]
  19090. ["Insert Column View DBlock" org-insert-columns-dblock t])
  19091. ("Dates and Scheduling"
  19092. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19093. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19094. ("Change Date"
  19095. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19096. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19097. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19098. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  19099. ["Compute Time Range" org-evaluate-time-range t]
  19100. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  19101. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  19102. "--"
  19103. ["Custom time format" org-toggle-time-stamp-overlays
  19104. :style radio :selected org-display-custom-times]
  19105. "--"
  19106. ["Goto Calendar" org-goto-calendar t]
  19107. ["Date from Calendar" org-date-from-calendar t]
  19108. "--"
  19109. ["Start/Restart Timer" org-timer-start t]
  19110. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  19111. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  19112. ["Insert Timer String" org-timer t]
  19113. ["Insert Timer Item" org-timer-item t])
  19114. ("Logging work"
  19115. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  19116. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  19117. ["Clock out" org-clock-out t]
  19118. ["Clock cancel" org-clock-cancel t]
  19119. "--"
  19120. ["Mark as default task" org-clock-mark-default-task t]
  19121. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  19122. ["Goto running clock" org-clock-goto t]
  19123. "--"
  19124. ["Display times" org-clock-display t]
  19125. ["Create clock table" org-clock-report t]
  19126. "--"
  19127. ["Record DONE time"
  19128. (progn (setq org-log-done (not org-log-done))
  19129. (message "Switching to %s will %s record a timestamp"
  19130. (car org-done-keywords)
  19131. (if org-log-done "automatically" "not")))
  19132. :style toggle :selected org-log-done])
  19133. "--"
  19134. ["Agenda Command..." org-agenda t]
  19135. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  19136. ("File List for Agenda")
  19137. ("Special views current file"
  19138. ["TODO Tree" org-show-todo-tree t]
  19139. ["Check Deadlines" org-check-deadlines t]
  19140. ["Timeline" org-timeline t]
  19141. ["Tags/Property tree" org-match-sparse-tree t])
  19142. "--"
  19143. ["Export/Publish..." org-export-dispatch t]
  19144. ("LaTeX"
  19145. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  19146. :selected org-cdlatex-mode]
  19147. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  19148. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  19149. ["Modify math symbol" org-cdlatex-math-modify
  19150. (org-inside-LaTeX-fragment-p)]
  19151. ["Insert citation" org-reftex-citation t]
  19152. "--"
  19153. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  19154. "--"
  19155. ("MobileOrg"
  19156. ["Push Files and Views" org-mobile-push t]
  19157. ["Get Captured and Flagged" org-mobile-pull t]
  19158. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  19159. "--"
  19160. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  19161. "--"
  19162. ("Documentation"
  19163. ["Show Version" org-version t]
  19164. ["Info Documentation" org-info t])
  19165. ("Customize"
  19166. ["Browse Org Group" org-customize t]
  19167. "--"
  19168. ["Expand This Menu" org-create-customize-menu
  19169. (fboundp 'customize-menu-create)])
  19170. ["Send bug report" org-submit-bug-report t]
  19171. "--"
  19172. ("Refresh/Reload"
  19173. ["Refresh setup current buffer" org-mode-restart t]
  19174. ["Reload Org (after update)" org-reload t]
  19175. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  19176. ))
  19177. (defun org-info (&optional node)
  19178. "Read documentation for Org-mode in the info system.
  19179. With optional NODE, go directly to that node."
  19180. (interactive)
  19181. (info (format "(org)%s" (or node ""))))
  19182. ;;;###autoload
  19183. (defun org-submit-bug-report ()
  19184. "Submit a bug report on Org-mode via mail.
  19185. Don't hesitate to report any problems or inaccurate documentation.
  19186. If you don't have setup sending mail from (X)Emacs, please copy the
  19187. output buffer into your mail program, as it gives us important
  19188. information about your Org-mode version and configuration."
  19189. (interactive)
  19190. (require 'reporter)
  19191. (org-load-modules-maybe)
  19192. (org-require-autoloaded-modules)
  19193. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  19194. (reporter-submit-bug-report
  19195. "emacs-orgmode@gnu.org"
  19196. (org-version nil 'full)
  19197. (let (list)
  19198. (save-window-excursion
  19199. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  19200. (delete-other-windows)
  19201. (erase-buffer)
  19202. (insert "You are about to submit a bug report to the Org-mode mailing list.
  19203. We would like to add your full Org-mode and Outline configuration to the
  19204. bug report. This greatly simplifies the work of the maintainer and
  19205. other experts on the mailing list.
  19206. HOWEVER, some variables you have customized may contain private
  19207. information. The names of customers, colleagues, or friends, might
  19208. appear in the form of file names, tags, todo states, or search strings.
  19209. If you answer yes to the prompt, you might want to check and remove
  19210. such private information before sending the email.")
  19211. (add-text-properties (point-min) (point-max) '(face org-warning))
  19212. (when (yes-or-no-p "Include your Org-mode configuration ")
  19213. (mapatoms
  19214. (lambda (v)
  19215. (and (boundp v)
  19216. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  19217. (or (and (symbol-value v)
  19218. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  19219. (and
  19220. (get v 'custom-type) (get v 'standard-value)
  19221. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  19222. (push v list)))))
  19223. (kill-buffer (get-buffer "*Warn about privacy*"))
  19224. list))
  19225. nil nil
  19226. "Remember to cover the basics, that is, what you expected to happen and
  19227. what in fact did happen. You don't know how to make a good report? See
  19228. http://orgmode.org/manual/Feedback.html#Feedback
  19229. Your bug report will be posted to the Org-mode mailing list.
  19230. ------------------------------------------------------------------------")
  19231. (save-excursion
  19232. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  19233. (replace-match "\\1Bug: \\3 [\\2]")))))
  19234. (defun org-install-agenda-files-menu ()
  19235. (let ((bl (buffer-list)))
  19236. (save-excursion
  19237. (while bl
  19238. (set-buffer (pop bl))
  19239. (if (derived-mode-p 'org-mode) (setq bl nil)))
  19240. (when (derived-mode-p 'org-mode)
  19241. (easy-menu-change
  19242. '("Org") "File List for Agenda"
  19243. (append
  19244. (list
  19245. ["Edit File List" (org-edit-agenda-file-list) t]
  19246. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  19247. ["Remove Current File from List" org-remove-file t]
  19248. ["Cycle through agenda files" org-cycle-agenda-files t]
  19249. ["Occur in all agenda files" org-occur-in-agenda-files t]
  19250. "--")
  19251. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  19252. ;;;; Documentation
  19253. (defun org-require-autoloaded-modules ()
  19254. (interactive)
  19255. (mapc 'require
  19256. '(org-agenda org-archive org-attach org-clock org-colview org-id
  19257. org-remember org-table org-timer)))
  19258. ;;;###autoload
  19259. (defun org-reload (&optional uncompiled)
  19260. "Reload all org lisp files.
  19261. With prefix arg UNCOMPILED, load the uncompiled versions."
  19262. (interactive "P")
  19263. (require 'loadhist)
  19264. (let* ((org-dir (org-find-library-dir "org"))
  19265. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  19266. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  19267. (remove-re (mapconcat 'identity
  19268. (mapcar (lambda (f) (concat "^" f "$"))
  19269. (list (if (featurep 'xemacs)
  19270. "org-colview"
  19271. "org-colview-xemacs")
  19272. "org" "org-loaddefs" "org-version"))
  19273. "\\|"))
  19274. (feats (delete-dups
  19275. (mapcar 'file-name-sans-extension
  19276. (mapcar 'file-name-nondirectory
  19277. (delq nil
  19278. (mapcar 'feature-file
  19279. features))))))
  19280. (lfeat (append
  19281. (sort
  19282. (setq feats
  19283. (delq nil (mapcar
  19284. (lambda (f)
  19285. (if (and (string-match feature-re f)
  19286. (not (string-match remove-re f)))
  19287. f nil))
  19288. feats)))
  19289. 'string-lessp)
  19290. (list "org-version" "org")))
  19291. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  19292. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  19293. load-uncore load-misses)
  19294. (setq load-misses
  19295. (delq 't
  19296. (mapcar (lambda (f)
  19297. (or (org-load-noerror-mustsuffix (concat org-dir f))
  19298. (and (string= org-dir contrib-dir)
  19299. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  19300. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  19301. (add-to-list 'load-uncore f 'append)
  19302. 't)
  19303. f))
  19304. lfeat)))
  19305. (if load-uncore
  19306. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  19307. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  19308. (if load-misses
  19309. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  19310. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  19311. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  19312. ;;;###autoload
  19313. (defun org-customize ()
  19314. "Call the customize function with org as argument."
  19315. (interactive)
  19316. (org-load-modules-maybe)
  19317. (org-require-autoloaded-modules)
  19318. (customize-browse 'org))
  19319. (defun org-create-customize-menu ()
  19320. "Create a full customization menu for Org-mode, insert it into the menu."
  19321. (interactive)
  19322. (org-load-modules-maybe)
  19323. (org-require-autoloaded-modules)
  19324. (if (fboundp 'customize-menu-create)
  19325. (progn
  19326. (easy-menu-change
  19327. '("Org") "Customize"
  19328. `(["Browse Org group" org-customize t]
  19329. "--"
  19330. ,(customize-menu-create 'org)
  19331. ["Set" Custom-set t]
  19332. ["Save" Custom-save t]
  19333. ["Reset to Current" Custom-reset-current t]
  19334. ["Reset to Saved" Custom-reset-saved t]
  19335. ["Reset to Standard Settings" Custom-reset-standard t]))
  19336. (message "\"Org\"-menu now contains full customization menu"))
  19337. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  19338. ;;;; Miscellaneous stuff
  19339. ;;; Generally useful functions
  19340. (defun org-get-at-bol (property)
  19341. "Get text property PROPERTY at beginning of line."
  19342. (get-text-property (point-at-bol) property))
  19343. (defun org-find-text-property-in-string (prop s)
  19344. "Return the first non-nil value of property PROP in string S."
  19345. (or (get-text-property 0 prop s)
  19346. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19347. prop s)))
  19348. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19349. "Display the given MESSAGE as a warning."
  19350. (if (fboundp 'display-warning)
  19351. (display-warning 'org message
  19352. (if (featurep 'xemacs) 'warning :warning))
  19353. (let ((buf (get-buffer-create "*Org warnings*")))
  19354. (with-current-buffer buf
  19355. (goto-char (point-max))
  19356. (insert "Warning (Org): " message)
  19357. (unless (bolp)
  19358. (newline)))
  19359. (display-buffer buf)
  19360. (sit-for 0))))
  19361. (defun org-eval (form)
  19362. "Eval FORM and return result."
  19363. (condition-case error
  19364. (eval form)
  19365. (error (format "%%![Error: %s]" error))))
  19366. (defun org-in-clocktable-p ()
  19367. "Check if the cursor is in a clocktable."
  19368. (let ((pos (point)) start)
  19369. (save-excursion
  19370. (end-of-line 1)
  19371. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19372. (setq start (match-beginning 0))
  19373. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19374. (>= (match-end 0) pos)
  19375. start))))
  19376. (defun org-in-commented-line ()
  19377. "Is point in a line starting with `#'?"
  19378. (equal (char-after (point-at-bol)) ?#))
  19379. (defun org-in-indented-comment-line ()
  19380. "Is point in a line starting with `#' after some white space?"
  19381. (save-excursion
  19382. (save-match-data
  19383. (goto-char (point-at-bol))
  19384. (looking-at "[ \t]*#"))))
  19385. (defun org-in-verbatim-emphasis ()
  19386. (save-match-data
  19387. (and (org-in-regexp org-emph-re 2)
  19388. (>= (point) (match-beginning 3))
  19389. (<= (point) (match-end 4))
  19390. (member (match-string 3) '("=" "~")))))
  19391. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19392. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19393. (if (and marker (marker-buffer marker)
  19394. (buffer-live-p (marker-buffer marker)))
  19395. (progn
  19396. (org-pop-to-buffer-same-window (marker-buffer marker))
  19397. (if (or (> marker (point-max)) (< marker (point-min)))
  19398. (widen))
  19399. (goto-char marker)
  19400. (org-show-context 'org-goto))
  19401. (if bookmark
  19402. (bookmark-jump bookmark)
  19403. (error "Cannot find location"))))
  19404. (defun org-quote-csv-field (s)
  19405. "Quote field for inclusion in CSV material."
  19406. (if (string-match "[\",]" s)
  19407. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19408. s))
  19409. (defun org-force-self-insert (N)
  19410. "Needed to enforce self-insert under remapping."
  19411. (interactive "p")
  19412. (self-insert-command N))
  19413. (defun org-string-width (s)
  19414. "Compute width of string, ignoring invisible characters.
  19415. This ignores character with invisibility property `org-link', and also
  19416. characters with property `org-cwidth', because these will become invisible
  19417. upon the next fontification round."
  19418. (let (b l)
  19419. (when (or (eq t buffer-invisibility-spec)
  19420. (assq 'org-link buffer-invisibility-spec))
  19421. (while (setq b (text-property-any 0 (length s)
  19422. 'invisible 'org-link s))
  19423. (setq s (concat (substring s 0 b)
  19424. (substring s (or (next-single-property-change
  19425. b 'invisible s) (length s)))))))
  19426. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19427. (setq s (concat (substring s 0 b)
  19428. (substring s (or (next-single-property-change
  19429. b 'org-cwidth s) (length s))))))
  19430. (setq l (string-width s) b -1)
  19431. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19432. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19433. l))
  19434. (defun org-shorten-string (s maxlength)
  19435. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19436. If the string is shorter or has length MAXLENGTH, just return the
  19437. original string. If it is longer, the functions finds a space in the
  19438. string, breaks this string off at that locations and adds three dots
  19439. as ellipsis. Including the ellipsis, the string will not be longer
  19440. than MAXLENGTH. If finding a good breaking point in the string does
  19441. not work, the string is just chopped off in the middle of a word
  19442. if necessary."
  19443. (if (<= (length s) maxlength)
  19444. s
  19445. (let* ((n (max (- maxlength 4) 1))
  19446. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19447. (if (string-match re s)
  19448. (concat (match-string 1 s) "...")
  19449. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19450. (defun org-get-indentation (&optional line)
  19451. "Get the indentation of the current line, interpreting tabs.
  19452. When LINE is given, assume it represents a line and compute its indentation."
  19453. (if line
  19454. (if (string-match "^ *" (org-remove-tabs line))
  19455. (match-end 0))
  19456. (save-excursion
  19457. (beginning-of-line 1)
  19458. (skip-chars-forward " \t")
  19459. (current-column))))
  19460. (defun org-get-string-indentation (s)
  19461. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19462. (let ((n -1) (i 0) (w tab-width) c)
  19463. (catch 'exit
  19464. (while (< (setq n (1+ n)) (length s))
  19465. (setq c (aref s n))
  19466. (cond ((= c ?\ ) (setq i (1+ i)))
  19467. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19468. (t (throw 'exit t)))))
  19469. i))
  19470. (defun org-remove-tabs (s &optional width)
  19471. "Replace tabulators in S with spaces.
  19472. Assumes that s is a single line, starting in column 0."
  19473. (setq width (or width tab-width))
  19474. (while (string-match "\t" s)
  19475. (setq s (replace-match
  19476. (make-string
  19477. (- (* width (/ (+ (match-beginning 0) width) width))
  19478. (match-beginning 0)) ?\ )
  19479. t t s)))
  19480. s)
  19481. (defun org-fix-indentation (line ind)
  19482. "Fix indentation in LINE.
  19483. IND is a cons cell with target and minimum indentation.
  19484. If the current indentation in LINE is smaller than the minimum,
  19485. leave it alone. If it is larger than ind, set it to the target."
  19486. (let* ((l (org-remove-tabs line))
  19487. (i (org-get-indentation l))
  19488. (i1 (car ind)) (i2 (cdr ind)))
  19489. (if (>= i i2) (setq l (substring line i2)))
  19490. (if (> i1 0)
  19491. (concat (make-string i1 ?\ ) l)
  19492. l)))
  19493. (defun org-remove-indentation (code &optional n)
  19494. "Remove the maximum common indentation from the lines in CODE.
  19495. N may optionally be the number of spaces to remove."
  19496. (with-temp-buffer
  19497. (insert code)
  19498. (org-do-remove-indentation n)
  19499. (buffer-string)))
  19500. (defun org-do-remove-indentation (&optional n)
  19501. "Remove the maximum common indentation from the buffer."
  19502. (untabify (point-min) (point-max))
  19503. (let ((min 10000) re)
  19504. (if n
  19505. (setq min n)
  19506. (goto-char (point-min))
  19507. (while (re-search-forward "^ *[^ \n]" nil t)
  19508. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19509. (unless (or (= min 0) (= min 10000))
  19510. (setq re (format "^ \\{%d\\}" min))
  19511. (goto-char (point-min))
  19512. (while (re-search-forward re nil t)
  19513. (replace-match "")
  19514. (end-of-line 1))
  19515. min)))
  19516. (defun org-fill-template (template alist)
  19517. "Find each %key of ALIST in TEMPLATE and replace it."
  19518. (let ((case-fold-search nil)
  19519. entry key value)
  19520. (setq alist (sort (copy-sequence alist)
  19521. (lambda (a b) (< (length (car a)) (length (car b))))))
  19522. (while (setq entry (pop alist))
  19523. (setq template
  19524. (replace-regexp-in-string
  19525. (concat "%" (regexp-quote (car entry)))
  19526. (or (cdr entry) "") template t t)))
  19527. template))
  19528. (defun org-base-buffer (buffer)
  19529. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19530. (if (not buffer)
  19531. buffer
  19532. (or (buffer-base-buffer buffer)
  19533. buffer)))
  19534. (defun org-trim (s)
  19535. "Remove whitespace at beginning and end of string."
  19536. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19537. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19538. s)
  19539. (defun org-wrap (string &optional width lines)
  19540. "Wrap string to either a number of lines, or a width in characters.
  19541. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19542. that costs. If there is a word longer than WIDTH, the text is actually
  19543. wrapped to the length of that word.
  19544. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19545. many lines, whatever width that takes.
  19546. The return value is a list of lines, without newlines at the end."
  19547. (let* ((words (org-split-string string "[ \t\n]+"))
  19548. (maxword (apply 'max (mapcar 'org-string-width words)))
  19549. w ll)
  19550. (cond (width
  19551. (org-do-wrap words (max maxword width)))
  19552. (lines
  19553. (setq w maxword)
  19554. (setq ll (org-do-wrap words maxword))
  19555. (if (<= (length ll) lines)
  19556. ll
  19557. (setq ll words)
  19558. (while (> (length ll) lines)
  19559. (setq w (1+ w))
  19560. (setq ll (org-do-wrap words w)))
  19561. ll))
  19562. (t (error "Cannot wrap this")))))
  19563. (defun org-do-wrap (words width)
  19564. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19565. (let (lines line)
  19566. (while words
  19567. (setq line (pop words))
  19568. (while (and words (< (+ (length line) (length (car words))) width))
  19569. (setq line (concat line " " (pop words))))
  19570. (setq lines (push line lines)))
  19571. (nreverse lines)))
  19572. (defun org-split-string (string &optional separators)
  19573. "Splits STRING into substrings at SEPARATORS.
  19574. No empty strings are returned if there are matches at the beginning
  19575. and end of string."
  19576. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19577. (start 0)
  19578. notfirst
  19579. (list nil))
  19580. (while (and (string-match rexp string
  19581. (if (and notfirst
  19582. (= start (match-beginning 0))
  19583. (< start (length string)))
  19584. (1+ start) start))
  19585. (< (match-beginning 0) (length string)))
  19586. (setq notfirst t)
  19587. (or (eq (match-beginning 0) 0)
  19588. (and (eq (match-beginning 0) (match-end 0))
  19589. (eq (match-beginning 0) start))
  19590. (setq list
  19591. (cons (substring string start (match-beginning 0))
  19592. list)))
  19593. (setq start (match-end 0)))
  19594. (or (eq start (length string))
  19595. (setq list
  19596. (cons (substring string start)
  19597. list)))
  19598. (nreverse list)))
  19599. (defun org-quote-vert (s)
  19600. "Replace \"|\" with \"\\vert\"."
  19601. (while (string-match "|" s)
  19602. (setq s (replace-match "\\vert" t t s)))
  19603. s)
  19604. (defun org-uuidgen-p (s)
  19605. "Is S an ID created by UUIDGEN?"
  19606. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19607. (defun org-in-src-block-p (&optional inside)
  19608. "Whether point is in a code source block.
  19609. When INSIDE is non-nil, don't consider we are within a src block
  19610. when point is at #+BEGIN_SRC or #+END_SRC."
  19611. (let ((case-fold-search t) ov)
  19612. (or (and (setq ov (overlays-at (point)))
  19613. (memq 'org-block-background
  19614. (overlay-properties (car ov))))
  19615. (and (not inside)
  19616. (save-match-data
  19617. (save-excursion
  19618. (beginning-of-line)
  19619. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19620. (defun org-context ()
  19621. "Return a list of contexts of the current cursor position.
  19622. If several contexts apply, all are returned.
  19623. Each context entry is a list with a symbol naming the context, and
  19624. two positions indicating start and end of the context. Possible
  19625. contexts are:
  19626. :headline anywhere in a headline
  19627. :headline-stars on the leading stars in a headline
  19628. :todo-keyword on a TODO keyword (including DONE) in a headline
  19629. :tags on the TAGS in a headline
  19630. :priority on the priority cookie in a headline
  19631. :item on the first line of a plain list item
  19632. :item-bullet on the bullet/number of a plain list item
  19633. :checkbox on the checkbox in a plain list item
  19634. :table in an org-mode table
  19635. :table-special on a special filed in a table
  19636. :table-table in a table.el table
  19637. :clocktable in a clocktable
  19638. :src-block in a source block
  19639. :link on a hyperlink
  19640. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19641. :target on a <<target>>
  19642. :radio-target on a <<<radio-target>>>
  19643. :latex-fragment on a LaTeX fragment
  19644. :latex-preview on a LaTeX fragment with overlaid preview image
  19645. This function expects the position to be visible because it uses font-lock
  19646. faces as a help to recognize the following contexts: :table-special, :link,
  19647. and :keyword."
  19648. (let* ((f (get-text-property (point) 'face))
  19649. (faces (if (listp f) f (list f)))
  19650. (case-fold-search t)
  19651. (p (point)) clist o)
  19652. ;; First the large context
  19653. (cond
  19654. ((org-at-heading-p t)
  19655. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19656. (when (progn
  19657. (beginning-of-line 1)
  19658. (looking-at org-todo-line-tags-regexp))
  19659. (push (org-point-in-group p 1 :headline-stars) clist)
  19660. (push (org-point-in-group p 2 :todo-keyword) clist)
  19661. (push (org-point-in-group p 4 :tags) clist))
  19662. (goto-char p)
  19663. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19664. (if (looking-at "\\[#[A-Z0-9]\\]")
  19665. (push (org-point-in-group p 0 :priority) clist)))
  19666. ((org-at-item-p)
  19667. (push (org-point-in-group p 2 :item-bullet) clist)
  19668. (push (list :item (point-at-bol)
  19669. (save-excursion (org-end-of-item) (point)))
  19670. clist)
  19671. (and (org-at-item-checkbox-p)
  19672. (push (org-point-in-group p 0 :checkbox) clist)))
  19673. ((org-at-table-p)
  19674. (push (list :table (org-table-begin) (org-table-end)) clist)
  19675. (if (memq 'org-formula faces)
  19676. (push (list :table-special
  19677. (previous-single-property-change p 'face)
  19678. (next-single-property-change p 'face)) clist)))
  19679. ((org-at-table-p 'any)
  19680. (push (list :table-table) clist)))
  19681. (goto-char p)
  19682. (let ((case-fold-search t))
  19683. ;; New the "medium" contexts: clocktables, source blocks
  19684. (cond ((org-in-clocktable-p)
  19685. (push (list :clocktable
  19686. (and (or (looking-at "#\\+BEGIN: clocktable")
  19687. (search-backward "#+BEGIN: clocktable" nil t))
  19688. (match-beginning 0))
  19689. (and (re-search-forward "#\\+END:?" nil t)
  19690. (match-end 0))) clist))
  19691. ((org-in-src-block-p)
  19692. (push (list :src-block
  19693. (and (or (looking-at "#\\+BEGIN_SRC")
  19694. (search-backward "#+BEGIN_SRC" nil t))
  19695. (match-beginning 0))
  19696. (and (search-forward "#+END_SRC" nil t)
  19697. (match-beginning 0))) clist))))
  19698. (goto-char p)
  19699. ;; Now the small context
  19700. (cond
  19701. ((org-at-timestamp-p)
  19702. (push (org-point-in-group p 0 :timestamp) clist))
  19703. ((memq 'org-link faces)
  19704. (push (list :link
  19705. (previous-single-property-change p 'face)
  19706. (next-single-property-change p 'face)) clist))
  19707. ((memq 'org-special-keyword faces)
  19708. (push (list :keyword
  19709. (previous-single-property-change p 'face)
  19710. (next-single-property-change p 'face)) clist))
  19711. ((org-at-target-p)
  19712. (push (org-point-in-group p 0 :target) clist)
  19713. (goto-char (1- (match-beginning 0)))
  19714. (if (looking-at org-radio-target-regexp)
  19715. (push (org-point-in-group p 0 :radio-target) clist))
  19716. (goto-char p))
  19717. ((setq o (car (delq nil
  19718. (mapcar
  19719. (lambda (x)
  19720. (if (memq x org-latex-fragment-image-overlays) x))
  19721. (overlays-at (point))))))
  19722. (push (list :latex-fragment
  19723. (overlay-start o) (overlay-end o)) clist)
  19724. (push (list :latex-preview
  19725. (overlay-start o) (overlay-end o)) clist))
  19726. ((org-inside-LaTeX-fragment-p)
  19727. ;; FIXME: positions wrong.
  19728. (push (list :latex-fragment (point) (point)) clist)))
  19729. (setq clist (nreverse (delq nil clist)))
  19730. clist))
  19731. ;; FIXME: Compare with at-regexp-p Do we need both?
  19732. (defun org-in-regexp (re &optional nlines visually)
  19733. "Check if point is inside a match of regexp.
  19734. Normally only the current line is checked, but you can include NLINES extra
  19735. lines both before and after point into the search.
  19736. If VISUALLY is set, require that the cursor is not after the match but
  19737. really on, so that the block visually is on the match."
  19738. (catch 'exit
  19739. (let ((pos (point))
  19740. (eol (point-at-eol (+ 1 (or nlines 0))))
  19741. (inc (if visually 1 0)))
  19742. (save-excursion
  19743. (beginning-of-line (- 1 (or nlines 0)))
  19744. (while (re-search-forward re eol t)
  19745. (if (and (<= (match-beginning 0) pos)
  19746. (>= (+ inc (match-end 0)) pos))
  19747. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19748. (defun org-at-regexp-p (regexp)
  19749. "Is point inside a match of REGEXP in the current line?"
  19750. (catch 'exit
  19751. (save-excursion
  19752. (let ((pos (point)) (end (point-at-eol)))
  19753. (beginning-of-line 1)
  19754. (while (re-search-forward regexp end t)
  19755. (if (and (<= (match-beginning 0) pos)
  19756. (>= (match-end 0) pos))
  19757. (throw 'exit t)))
  19758. nil))))
  19759. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19760. "Non-nil when point is between matches of START-RE and END-RE.
  19761. Also return a non-nil value when point is on one of the matches.
  19762. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19763. buffer positions. Default values are the positions of headlines
  19764. surrounding the point.
  19765. The functions returns a cons cell whose car (resp. cdr) is the
  19766. position before START-RE (resp. after END-RE)."
  19767. (save-match-data
  19768. (let ((pos (point))
  19769. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19770. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19771. beg end)
  19772. (save-excursion
  19773. ;; Point is on a block when on START-RE or if START-RE can be
  19774. ;; found before it...
  19775. (and (or (org-at-regexp-p start-re)
  19776. (re-search-backward start-re limit-up t))
  19777. (setq beg (match-beginning 0))
  19778. ;; ... and END-RE after it...
  19779. (goto-char (match-end 0))
  19780. (re-search-forward end-re limit-down t)
  19781. (> (setq end (match-end 0)) pos)
  19782. ;; ... without another START-RE in-between.
  19783. (goto-char (match-beginning 0))
  19784. (not (re-search-backward start-re (1+ beg) t))
  19785. ;; Return value.
  19786. (cons beg end))))))
  19787. (defun org-in-block-p (names)
  19788. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19789. NAMES is a list of strings containing names of blocks.
  19790. Return first block name matched, or nil. Beware that in case of
  19791. nested blocks, the returned name may not belong to the closest
  19792. block from point."
  19793. (save-match-data
  19794. (catch 'exit
  19795. (let ((case-fold-search t)
  19796. (lim-up (save-excursion (outline-previous-heading)))
  19797. (lim-down (save-excursion (outline-next-heading))))
  19798. (mapc (lambda (name)
  19799. (let ((n (regexp-quote name)))
  19800. (when (org-between-regexps-p
  19801. (concat "^[ \t]*#\\+begin_" n)
  19802. (concat "^[ \t]*#\\+end_" n)
  19803. lim-up lim-down)
  19804. (throw 'exit n))))
  19805. names))
  19806. nil)))
  19807. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19808. "Call `multi-occur' with buffers for all agenda files."
  19809. (interactive "sOrg-files matching: \np")
  19810. (let* ((files (org-agenda-files))
  19811. (tnames (mapcar 'file-truename files))
  19812. (extra org-agenda-text-search-extra-files)
  19813. f)
  19814. (when (eq (car extra) 'agenda-archives)
  19815. (setq extra (cdr extra))
  19816. (setq files (org-add-archive-files files)))
  19817. (while (setq f (pop extra))
  19818. (unless (member (file-truename f) tnames)
  19819. (add-to-list 'files f 'append)
  19820. (add-to-list 'tnames (file-truename f) 'append)))
  19821. (multi-occur
  19822. (mapcar (lambda (x)
  19823. (with-current-buffer
  19824. (or (get-file-buffer x) (find-file-noselect x))
  19825. (widen)
  19826. (current-buffer)))
  19827. files)
  19828. regexp)))
  19829. (if (boundp 'occur-mode-find-occurrence-hook)
  19830. ;; Emacs 23
  19831. (add-hook 'occur-mode-find-occurrence-hook
  19832. (lambda ()
  19833. (when (derived-mode-p 'org-mode)
  19834. (org-reveal))))
  19835. ;; Emacs 22
  19836. (defadvice occur-mode-goto-occurrence
  19837. (after org-occur-reveal activate)
  19838. (and (derived-mode-p 'org-mode) (org-reveal)))
  19839. (defadvice occur-mode-goto-occurrence-other-window
  19840. (after org-occur-reveal activate)
  19841. (and (derived-mode-p 'org-mode) (org-reveal)))
  19842. (defadvice occur-mode-display-occurrence
  19843. (after org-occur-reveal activate)
  19844. (when (derived-mode-p 'org-mode)
  19845. (let ((pos (occur-mode-find-occurrence)))
  19846. (with-current-buffer (marker-buffer pos)
  19847. (save-excursion
  19848. (goto-char pos)
  19849. (org-reveal)))))))
  19850. (defun org-occur-link-in-agenda-files ()
  19851. "Create a link and search for it in the agendas.
  19852. The link is not stored in `org-stored-links', it is just created
  19853. for the search purpose."
  19854. (interactive)
  19855. (let ((link (condition-case nil
  19856. (org-store-link nil)
  19857. (error "Unable to create a link to here"))))
  19858. (org-occur-in-agenda-files (regexp-quote link))))
  19859. (defun org-reverse-string (string)
  19860. "Return the reverse of STRING."
  19861. (apply 'string (reverse (string-to-list string))))
  19862. (defsubst org-uniquify (list)
  19863. "Non-destructively remove duplicate elements from LIST."
  19864. (let ((res (copy-seq list))) (delete-dups res)))
  19865. (defun org-uniquify-alist (alist)
  19866. "Merge elements of ALIST with the same key.
  19867. For example, in this alist:
  19868. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  19869. => '((a 1 3) (b 2))
  19870. merge (a 1) and (a 3) into (a 1 3).
  19871. The function returns the new ALIST."
  19872. (let (rtn)
  19873. (mapc
  19874. (lambda (e)
  19875. (let (n)
  19876. (if (not (assoc (car e) rtn))
  19877. (push e rtn)
  19878. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  19879. (setq rtn (assq-delete-all (car e) rtn))
  19880. (push n rtn))))
  19881. alist)
  19882. rtn))
  19883. (defun org-delete-all (elts list)
  19884. "Remove all elements in ELTS from LIST."
  19885. (while elts
  19886. (setq list (delete (pop elts) list)))
  19887. list)
  19888. (defun org-count (cl-item cl-seq)
  19889. "Count the number of occurrences of ITEM in SEQ.
  19890. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19891. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19892. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19893. (while (< cl-start cl-end)
  19894. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19895. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19896. (setq cl-start (1+ cl-start)))
  19897. cl-count))
  19898. (defun org-remove-if (predicate seq)
  19899. "Remove everything from SEQ that fulfills PREDICATE."
  19900. (let (res e)
  19901. (while seq
  19902. (setq e (pop seq))
  19903. (if (not (funcall predicate e)) (push e res)))
  19904. (nreverse res)))
  19905. (defun org-remove-if-not (predicate seq)
  19906. "Remove everything from SEQ that does not fulfill PREDICATE."
  19907. (let (res e)
  19908. (while seq
  19909. (setq e (pop seq))
  19910. (if (funcall predicate e) (push e res)))
  19911. (nreverse res)))
  19912. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19913. "Reduce two-argument FUNCTION across SEQ.
  19914. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19915. \":initial-value\" removed."
  19916. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19917. (cadr (memq :initial-value cl-keys)))
  19918. (cl-seq (pop cl-seq))
  19919. (t (funcall cl-func)))))
  19920. (while cl-seq
  19921. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19922. cl-accum))
  19923. (defun org-back-over-empty-lines ()
  19924. "Move backwards over whitespace, to the beginning of the first empty line.
  19925. Returns the number of empty lines passed."
  19926. (let ((pos (point)))
  19927. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19928. (skip-chars-backward " \t\n\r")
  19929. (unless (eobp)
  19930. (forward-line -1)))
  19931. (beginning-of-line 2)
  19932. (goto-char (min (point) pos))
  19933. (count-lines (point) pos)))
  19934. (defun org-skip-whitespace ()
  19935. (skip-chars-forward " \t\n\r"))
  19936. (defun org-point-in-group (point group &optional context)
  19937. "Check if POINT is in match-group GROUP.
  19938. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19939. match. If the match group does not exist or point is not inside it,
  19940. return nil."
  19941. (and (match-beginning group)
  19942. (>= point (match-beginning group))
  19943. (<= point (match-end group))
  19944. (if context
  19945. (list context (match-beginning group) (match-end group))
  19946. t)))
  19947. (defun org-switch-to-buffer-other-window (&rest args)
  19948. "Switch to buffer in a second window on the current frame.
  19949. In particular, do not allow pop-up frames.
  19950. Returns the newly created buffer."
  19951. (org-no-popups
  19952. (apply 'switch-to-buffer-other-window args)))
  19953. (defun org-combine-plists (&rest plists)
  19954. "Create a single property list from all plists in PLISTS.
  19955. The process starts by copying the first list, and then setting properties
  19956. from the other lists. Settings in the last list are the most significant
  19957. ones and overrule settings in the other lists."
  19958. (let ((rtn (copy-sequence (pop plists)))
  19959. p v ls)
  19960. (while plists
  19961. (setq ls (pop plists))
  19962. (while ls
  19963. (setq p (pop ls) v (pop ls))
  19964. (setq rtn (plist-put rtn p v))))
  19965. rtn))
  19966. (defun org-replace-escapes (string table)
  19967. "Replace %-escapes in STRING with values in TABLE.
  19968. TABLE is an association list with keys like \"%a\" and string values.
  19969. The sequences in STRING may contain normal field width and padding information,
  19970. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19971. so values can contain further %-escapes if they are define later in TABLE."
  19972. (let ((tbl (copy-alist table))
  19973. (case-fold-search nil)
  19974. (pchg 0)
  19975. e re rpl)
  19976. (while (setq e (pop tbl))
  19977. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19978. (when (and (cdr e) (string-match re (cdr e)))
  19979. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19980. (safe "SREF"))
  19981. (add-text-properties 0 3 (list 'sref sref) safe)
  19982. (setcdr e (replace-match safe t t (cdr e)))))
  19983. (while (string-match re string)
  19984. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19985. (cdr e)))
  19986. (setq string (replace-match rpl t t string))))
  19987. (while (setq pchg (next-property-change pchg string))
  19988. (let ((sref (get-text-property pchg 'sref string)))
  19989. (when (and sref (string-match "SREF" string pchg))
  19990. (setq string (replace-match sref t t string)))))
  19991. string))
  19992. (defun org-sublist (list start end)
  19993. "Return a section of LIST, from START to END.
  19994. Counting starts at 1."
  19995. (let (rtn (c start))
  19996. (setq list (nthcdr (1- start) list))
  19997. (while (and list (<= c end))
  19998. (push (pop list) rtn)
  19999. (setq c (1+ c)))
  20000. (nreverse rtn)))
  20001. (defun org-find-base-buffer-visiting (file)
  20002. "Like `find-buffer-visiting' but always return the base buffer and
  20003. not an indirect buffer."
  20004. (let ((buf (or (get-file-buffer file)
  20005. (find-buffer-visiting file))))
  20006. (if buf
  20007. (or (buffer-base-buffer buf) buf)
  20008. nil)))
  20009. (defun org-image-file-name-regexp (&optional extensions)
  20010. "Return regexp matching the file names of images.
  20011. If EXTENSIONS is given, only match these."
  20012. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20013. (image-file-name-regexp)
  20014. (let ((image-file-name-extensions
  20015. (or extensions
  20016. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20017. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20018. (concat "\\."
  20019. (regexp-opt (nconc (mapcar 'upcase
  20020. image-file-name-extensions)
  20021. image-file-name-extensions)
  20022. t)
  20023. "\\'"))))
  20024. (defun org-file-image-p (file &optional extensions)
  20025. "Return non-nil if FILE is an image."
  20026. (save-match-data
  20027. (string-match (org-image-file-name-regexp extensions) file)))
  20028. (defun org-get-cursor-date (&optional with-time)
  20029. "Return the date at cursor in as a time.
  20030. This works in the calendar and in the agenda, anywhere else it just
  20031. returns the current time.
  20032. If WITH-TIME is non-nil, returns the time of the event at point (in
  20033. the agenda) or the current time of the day."
  20034. (let (date day defd tp tm hod mod)
  20035. (when with-time
  20036. (setq tp (get-text-property (point) 'time))
  20037. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20038. (setq hod (string-to-number (match-string 1 tp))
  20039. mod (string-to-number (match-string 2 tp))))
  20040. (or tp (setq hod (nth 2 (decode-time (current-time)))
  20041. mod (nth 1 (decode-time (current-time))))))
  20042. (cond
  20043. ((eq major-mode 'calendar-mode)
  20044. (setq date (calendar-cursor-to-date)
  20045. defd (encode-time 0 (or mod 0) (or hod 0)
  20046. (nth 1 date) (nth 0 date) (nth 2 date))))
  20047. ((eq major-mode 'org-agenda-mode)
  20048. (setq day (get-text-property (point) 'day))
  20049. (if day
  20050. (setq date (calendar-gregorian-from-absolute day)
  20051. defd (encode-time 0 (or mod 0) (or hod 0)
  20052. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20053. (or defd (current-time))))
  20054. (defun org-mark-subtree (&optional up)
  20055. "Mark the current subtree.
  20056. This puts point at the start of the current subtree, and mark at
  20057. the end. If a numeric prefix UP is given, move up into the
  20058. hierarchy of headlines by UP levels before marking the subtree."
  20059. (interactive "P")
  20060. (org-with-limited-levels
  20061. (cond ((org-at-heading-p) (beginning-of-line))
  20062. ((org-before-first-heading-p) (error "Not in a subtree"))
  20063. (t (outline-previous-visible-heading 1))))
  20064. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20065. (if (org-called-interactively-p 'any)
  20066. (call-interactively 'org-mark-element)
  20067. (org-mark-element)))
  20068. ;;; Indentation
  20069. (defun org-indent-line ()
  20070. "Indent line depending on context."
  20071. (interactive)
  20072. (let* ((pos (point))
  20073. (itemp (org-at-item-p))
  20074. (case-fold-search t)
  20075. (org-drawer-regexp (or org-drawer-regexp "\000"))
  20076. (inline-task-p (and (featurep 'org-inlinetask)
  20077. (org-inlinetask-in-task-p)))
  20078. (inline-re (and inline-task-p
  20079. (org-inlinetask-outline-regexp)))
  20080. column)
  20081. (if (and orgstruct-is-++ (eq pos (point)))
  20082. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  20083. (indent-according-to-mode))
  20084. (beginning-of-line 1)
  20085. (cond
  20086. ;; Headings
  20087. ((looking-at org-outline-regexp) (setq column 0))
  20088. ;; Footnote definition
  20089. ((looking-at org-footnote-definition-re) (setq column 0))
  20090. ;; Literal examples
  20091. ((looking-at "[ \t]*:\\( \\|$\\)")
  20092. (setq column (org-get-indentation))) ; do nothing
  20093. ;; Lists
  20094. ((ignore-errors (goto-char (org-in-item-p)))
  20095. (setq column (if itemp
  20096. (org-get-indentation)
  20097. (org-list-item-body-column (point))))
  20098. (goto-char pos))
  20099. ;; Drawers
  20100. ((and (looking-at "[ \t]*:END:")
  20101. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  20102. (save-excursion
  20103. (goto-char (1- (match-beginning 1)))
  20104. (setq column (current-column))))
  20105. ;; Special blocks
  20106. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  20107. (save-excursion
  20108. (re-search-backward
  20109. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  20110. (setq column (org-get-indentation (match-string 0))))
  20111. ((and (not (looking-at "[ \t]*#\\+begin_"))
  20112. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  20113. (save-excursion
  20114. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  20115. (setq column
  20116. (cond ((equal (downcase (match-string 1)) "src")
  20117. ;; src blocks: let `org-edit-src-exit' handle them
  20118. (org-get-indentation))
  20119. ((equal (downcase (match-string 1)) "example")
  20120. (max (org-get-indentation)
  20121. (org-get-indentation (match-string 0))))
  20122. (t
  20123. (org-get-indentation (match-string 0))))))
  20124. ;; This line has nothing special, look at the previous relevant
  20125. ;; line to compute indentation
  20126. (t
  20127. (beginning-of-line 0)
  20128. (while (and (not (bobp))
  20129. (not (looking-at org-table-line-regexp))
  20130. (not (looking-at org-drawer-regexp))
  20131. ;; When point started in an inline task, do not move
  20132. ;; above task starting line.
  20133. (not (and inline-task-p (looking-at inline-re)))
  20134. ;; Skip drawers, blocks, empty lines, verbatim,
  20135. ;; comments, tables, footnotes definitions, lists,
  20136. ;; inline tasks.
  20137. (or (and (looking-at "[ \t]*:END:")
  20138. (re-search-backward org-drawer-regexp nil t))
  20139. (and (looking-at "[ \t]*#\\+end_")
  20140. (re-search-backward "[ \t]*#\\+begin_"nil t))
  20141. (looking-at "[ \t]*[\n:#|]")
  20142. (looking-at org-footnote-definition-re)
  20143. (and (not inline-task-p)
  20144. (featurep 'org-inlinetask)
  20145. (org-inlinetask-in-task-p)
  20146. (or (org-inlinetask-goto-beginning) t))))
  20147. (beginning-of-line 0))
  20148. (cond
  20149. ;; There was a list item above.
  20150. ((save-excursion
  20151. (and (ignore-errors (goto-char (org-in-item-p)))
  20152. (goto-char
  20153. (org-list-get-top-point (org-list-struct)))))
  20154. (looking-at org-list-full-item-re)
  20155. (setq column (length (match-string 0))))
  20156. ;; There was an heading above.
  20157. ((looking-at "\\*+[ \t]+")
  20158. (if (not org-adapt-indentation)
  20159. (setq column 0)
  20160. (goto-char (match-end 0))
  20161. (setq column (current-column))))
  20162. ;; A drawer had started and is unfinished
  20163. ((looking-at org-drawer-regexp)
  20164. (goto-char (1- (match-beginning 1)))
  20165. (setq column (current-column)))
  20166. ;; Else, nothing noticeable found: get indentation and go on.
  20167. (t (setq column (org-get-indentation))))))
  20168. ;; Now apply indentation and move cursor accordingly
  20169. (goto-char pos)
  20170. (if (<= (current-column) (current-indentation))
  20171. (org-indent-line-to column)
  20172. (save-excursion (org-indent-line-to column)))
  20173. ;; Special polishing for properties, see `org-property-format'
  20174. (setq column (current-column))
  20175. (beginning-of-line 1)
  20176. (if (looking-at
  20177. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  20178. (replace-match (concat (match-string 1)
  20179. (format org-property-format
  20180. (match-string 2) (match-string 3)))
  20181. t t))
  20182. (org-move-to-column column))))
  20183. (defun org-indent-drawer ()
  20184. "Indent the drawer at point."
  20185. (interactive)
  20186. (let ((p (point))
  20187. (e (and (save-excursion (re-search-forward ":END:" nil t))
  20188. (match-end 0)))
  20189. (folded
  20190. (save-excursion
  20191. (end-of-line)
  20192. (when (overlays-at (point))
  20193. (member 'invisible (overlay-properties
  20194. (car (overlays-at (point)))))))))
  20195. (when folded (org-cycle))
  20196. (indent-for-tab-command)
  20197. (while (and (move-beginning-of-line 2) (< (point) e))
  20198. (indent-for-tab-command))
  20199. (goto-char p)
  20200. (when folded (org-cycle)))
  20201. (message "Drawer at point indented"))
  20202. (defun org-indent-block ()
  20203. "Indent the block at point."
  20204. (interactive)
  20205. (let ((p (point))
  20206. (case-fold-search t)
  20207. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  20208. (match-end 0)))
  20209. (folded
  20210. (save-excursion
  20211. (end-of-line)
  20212. (when (overlays-at (point))
  20213. (member 'invisible (overlay-properties
  20214. (car (overlays-at (point)))))))))
  20215. (when folded (org-cycle))
  20216. (indent-for-tab-command)
  20217. (while (and (move-beginning-of-line 2) (< (point) e))
  20218. (indent-for-tab-command))
  20219. (goto-char p)
  20220. (when folded (org-cycle)))
  20221. (message "Block at point indented"))
  20222. (defun org-indent-region (start end)
  20223. "Indent region."
  20224. (interactive "r")
  20225. (save-excursion
  20226. (let ((line-end (org-current-line end)))
  20227. (goto-char start)
  20228. (while (< (org-current-line) line-end)
  20229. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  20230. (t (call-interactively 'org-indent-line)))
  20231. (move-beginning-of-line 2)))))
  20232. ;;; Filling
  20233. ;; We use our own fill-paragraph and auto-fill functions.
  20234. ;; `org-fill-paragraph' relies on adaptive filling and context
  20235. ;; checking. Appropriate `fill-prefix' is computed with
  20236. ;; `org-adaptive-fill-function'.
  20237. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  20238. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  20239. ;; `org-adaptive-fill-function' value. Internally,
  20240. ;; `org-comment-line-break-function' breaks the line.
  20241. ;; `org-setup-filling' installs filling and auto-filling related
  20242. ;; variables during `org-mode' initialization.
  20243. (defun org-setup-filling ()
  20244. (interactive)
  20245. ;; Prevent auto-fill from inserting unwanted new items.
  20246. (when (boundp 'fill-nobreak-predicate)
  20247. (org-set-local
  20248. 'fill-nobreak-predicate
  20249. (org-uniquify
  20250. (append fill-nobreak-predicate
  20251. '(org-fill-paragraph-separate-nobreak-p
  20252. org-fill-line-break-nobreak-p
  20253. org-fill-paragraph-with-timestamp-nobreak-p)))))
  20254. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  20255. (org-set-local 'auto-fill-inhibit-regexp nil)
  20256. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  20257. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  20258. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  20259. (defvar org-element-paragraph-separate) ; org-element.el
  20260. (defun org-fill-paragraph-separate-nobreak-p ()
  20261. "Non-nil when a line break at point would insert a new item."
  20262. (looking-at (substring org-element-paragraph-separate 1)))
  20263. (defun org-fill-line-break-nobreak-p ()
  20264. "Non-nil when a line break at point would create an Org line break."
  20265. (save-excursion
  20266. (skip-chars-backward "[ \t]")
  20267. (skip-chars-backward "\\\\")
  20268. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  20269. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  20270. "Non-nil when a line break at point would insert a new item."
  20271. (and (org-at-timestamp-p t)
  20272. (not (looking-at org-ts-regexp-both))))
  20273. (declare-function message-in-body-p "message" ())
  20274. (defvar orgtbl-line-start-regexp) ; From org-table.el
  20275. (defun org-adaptive-fill-function ()
  20276. "Compute a fill prefix for the current line.
  20277. Return fill prefix, as a string, or nil if current line isn't
  20278. meant to be filled."
  20279. (let (prefix)
  20280. (catch 'exit
  20281. (when (derived-mode-p 'message-mode)
  20282. (save-excursion
  20283. (beginning-of-line)
  20284. (cond ((or (not (message-in-body-p))
  20285. (looking-at orgtbl-line-start-regexp))
  20286. (throw 'exit nil))
  20287. ((looking-at message-cite-prefix-regexp)
  20288. (throw 'exit (match-string-no-properties 0)))
  20289. ((looking-at org-outline-regexp)
  20290. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  20291. (org-with-wide-buffer
  20292. (let* ((p (line-beginning-position))
  20293. (element (save-excursion (beginning-of-line)
  20294. (org-element-at-point)))
  20295. (type (org-element-type element))
  20296. (post-affiliated (org-element-property :post-affiliated element)))
  20297. (unless (and post-affiliated (< p post-affiliated))
  20298. (case type
  20299. (comment (looking-at "[ \t]*# ?") (match-string 0))
  20300. (footnote-definition "")
  20301. ((item plain-list)
  20302. (make-string (org-list-item-body-column
  20303. (or post-affiliated
  20304. (org-element-property :begin element)))
  20305. ? ))
  20306. (paragraph
  20307. ;; Fill prefix is usually the same as the current line,
  20308. ;; except if the paragraph is at the beginning of an item.
  20309. (let ((parent (org-element-property :parent element)))
  20310. (cond ((eq (org-element-type parent) 'item)
  20311. (make-string (org-list-item-body-column
  20312. (org-element-property :begin parent))
  20313. ? ))
  20314. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20315. (match-string 0))
  20316. (t ""))))
  20317. (comment-block
  20318. ;; Only fill contents if P is within block boundaries.
  20319. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  20320. (forward-line)
  20321. (point)))
  20322. (cend (save-excursion
  20323. (goto-char (org-element-property :end element))
  20324. (skip-chars-backward " \r\t\n")
  20325. (line-beginning-position))))
  20326. (when (and (>= p cbeg) (< p cend))
  20327. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  20328. (match-string 0)
  20329. "")))))))))))
  20330. (declare-function message-goto-body "message" ())
  20331. (defvar message-cite-prefix-regexp) ; From message.el
  20332. (defun org-fill-paragraph (&optional justify)
  20333. "Fill element at point, when applicable.
  20334. This function only applies to comment blocks, comments, example
  20335. blocks and paragraphs. Also, as a special case, re-align table
  20336. when point is at one.
  20337. If JUSTIFY is non-nil (interactively, with prefix argument),
  20338. justify as well. If `sentence-end-double-space' is non-nil, then
  20339. period followed by one space does not end a sentence, so don't
  20340. break a line there. The variable `fill-column' controls the
  20341. width for filling.
  20342. For convenience, when point is at a plain list, an item or
  20343. a footnote definition, try to fill the first paragraph within."
  20344. (interactive)
  20345. (if (and (derived-mode-p 'message-mode)
  20346. (or (not (message-in-body-p))
  20347. (save-excursion (move-beginning-of-line 1)
  20348. (looking-at message-cite-prefix-regexp))))
  20349. ;; First ensure filling is correct in message-mode.
  20350. (let ((fill-paragraph-function
  20351. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  20352. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  20353. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  20354. (paragraph-separate
  20355. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  20356. (fill-paragraph nil))
  20357. (with-syntax-table org-mode-transpose-word-syntax-table
  20358. ;; Move to end of line in order to get the first paragraph
  20359. ;; within a plain list or a footnote definition.
  20360. (let ((element (save-excursion (end-of-line) (org-element-at-point))))
  20361. ;; First check if point is in a blank line at the beginning of
  20362. ;; the buffer. In that case, ignore filling.
  20363. (case (org-element-type element)
  20364. ;; Use major mode filling function is src blocks.
  20365. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  20366. ;; Align Org tables, leave table.el tables as-is.
  20367. (table-row (org-table-align) t)
  20368. (table
  20369. (when (eq (org-element-property :type element) 'org)
  20370. (org-table-align))
  20371. t)
  20372. (paragraph
  20373. ;; Paragraphs may contain `line-break' type objects.
  20374. (let ((beg (max (point-min)
  20375. (org-element-property :contents-begin element)))
  20376. (end (min (point-max)
  20377. (org-element-property :contents-end element))))
  20378. ;; Do nothing if point is at an affiliated keyword.
  20379. (if (< (line-end-position) beg) t
  20380. (when (derived-mode-p 'message-mode)
  20381. ;; In `message-mode', do not fill following citation
  20382. ;; in current paragraph nor text before message body.
  20383. (let ((body-start (save-excursion (message-goto-body))))
  20384. (when body-start (setq beg (max body-start beg))))
  20385. (when (save-excursion
  20386. (re-search-forward
  20387. (concat "^" message-cite-prefix-regexp) end t))
  20388. (setq end (match-beginning 0))))
  20389. ;; Fill paragraph, taking line breaks into account.
  20390. ;; For that, slice the paragraph using line breaks as
  20391. ;; separators, and fill the parts in reverse order to
  20392. ;; avoid messing with markers.
  20393. (save-excursion
  20394. (goto-char end)
  20395. (mapc
  20396. (lambda (pos)
  20397. (fill-region-as-paragraph pos (point) justify)
  20398. (goto-char pos))
  20399. ;; Find the list of ending positions for line breaks
  20400. ;; in the current paragraph. Add paragraph
  20401. ;; beginning to include first slice.
  20402. (nreverse
  20403. (cons beg
  20404. (org-element-map
  20405. (org-element--parse-objects
  20406. beg end nil (org-element-restriction 'paragraph))
  20407. 'line-break
  20408. (lambda (lb) (org-element-property :end lb)))))))
  20409. t)))
  20410. ;; Contents of `comment-block' type elements should be
  20411. ;; filled as plain text, but only if point is within block
  20412. ;; markers.
  20413. (comment-block
  20414. (let* ((case-fold-search t)
  20415. (beg (save-excursion
  20416. (goto-char (org-element-property :begin element))
  20417. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20418. (forward-line)
  20419. (point)))
  20420. (end (save-excursion
  20421. (goto-char (org-element-property :end element))
  20422. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20423. (line-beginning-position))))
  20424. (when (and (>= (point) beg) (< (point) end))
  20425. (fill-region-as-paragraph
  20426. (save-excursion
  20427. (end-of-line)
  20428. (re-search-backward "^[ \t]*$" beg 'move)
  20429. (line-beginning-position))
  20430. (save-excursion
  20431. (beginning-of-line)
  20432. (re-search-forward "^[ \t]*$" end 'move)
  20433. (line-beginning-position))
  20434. justify)))
  20435. t)
  20436. ;; Fill comments.
  20437. (comment (fill-comment-paragraph justify))
  20438. ;; Ignore every other element.
  20439. (otherwise t))))))
  20440. (defun org-auto-fill-function ()
  20441. "Auto-fill function."
  20442. ;; Check if auto-filling is meaningful.
  20443. (let ((fc (current-fill-column)))
  20444. (when (and fc (> (current-column) fc))
  20445. (let* ((fill-prefix (org-adaptive-fill-function))
  20446. ;; Enforce empty fill prefix, if required. Otherwise, it
  20447. ;; will be computed again.
  20448. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20449. (when fill-prefix (do-auto-fill))))))
  20450. (defun org-comment-line-break-function (&optional soft)
  20451. "Break line at point and indent, continuing comment if within one.
  20452. The inserted newline is marked hard if variable
  20453. `use-hard-newlines' is true, unless optional argument SOFT is
  20454. non-nil."
  20455. (if soft (insert-and-inherit ?\n) (newline 1))
  20456. (save-excursion (forward-char -1) (delete-horizontal-space))
  20457. (delete-horizontal-space)
  20458. (indent-to-left-margin)
  20459. (insert-before-markers-and-inherit fill-prefix))
  20460. ;;; Comments
  20461. ;; Org comments syntax is quite complex. It requires the entire line
  20462. ;; to be just a comment. Also, even with the right syntax at the
  20463. ;; beginning of line, some some elements (i.e. verse-block or
  20464. ;; example-block) don't accept comments. Usual Emacs comment commands
  20465. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20466. ;; Org still relies on `comment-dwim', but cannot trust
  20467. ;; `comment-only-p'. So, `comment-region-function' and
  20468. ;; `uncomment-region-function' both point
  20469. ;; to`org-comment-or-uncomment-region'. Eventually,
  20470. ;; `org-insert-comment' takes care of insertion of comments at the
  20471. ;; beginning of line.
  20472. ;; `org-setup-comments-handling' install comments related variables
  20473. ;; during `org-mode' initialization.
  20474. (defun org-setup-comments-handling ()
  20475. (interactive)
  20476. (org-set-local 'comment-use-syntax nil)
  20477. (org-set-local 'comment-start "# ")
  20478. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20479. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20480. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20481. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20482. (defun org-insert-comment ()
  20483. "Insert an empty comment above current line.
  20484. If the line is empty, insert comment at its beginning."
  20485. (beginning-of-line)
  20486. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20487. (org-indent-line)
  20488. (insert "# "))
  20489. (defvar comment-empty-lines) ; From newcomment.el.
  20490. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20491. "Comment or uncomment each non-blank line in the region.
  20492. Uncomment each non-blank line between BEG and END if it only
  20493. contains commented lines. Otherwise, comment them."
  20494. (save-restriction
  20495. ;; Restrict region
  20496. (narrow-to-region (save-excursion (goto-char beg)
  20497. (skip-chars-forward " \r\t\n" end)
  20498. (line-beginning-position))
  20499. (save-excursion (goto-char end)
  20500. (skip-chars-backward " \r\t\n" beg)
  20501. (line-end-position)))
  20502. (let ((uncommentp
  20503. ;; UNCOMMENTP is non-nil when every non blank line between
  20504. ;; BEG and END is a comment.
  20505. (save-excursion
  20506. (goto-char (point-min))
  20507. (while (and (not (eobp))
  20508. (let ((element (org-element-at-point)))
  20509. (and (eq (org-element-type element) 'comment)
  20510. (goto-char (min (point-max)
  20511. (org-element-property
  20512. :end element)))))))
  20513. (eobp))))
  20514. (if uncommentp
  20515. ;; Only blank lines and comments in region: uncomment it.
  20516. (save-excursion
  20517. (goto-char (point-min))
  20518. (while (not (eobp))
  20519. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20520. (replace-match "" nil nil nil 1))
  20521. (forward-line)))
  20522. ;; Comment each line in region.
  20523. (let ((min-indent (point-max)))
  20524. ;; First find the minimum indentation across all lines.
  20525. (save-excursion
  20526. (goto-char (point-min))
  20527. (while (and (not (eobp)) (not (zerop min-indent)))
  20528. (unless (looking-at "[ \t]*$")
  20529. (setq min-indent (min min-indent (current-indentation))))
  20530. (forward-line)))
  20531. ;; Then loop over all lines.
  20532. (save-excursion
  20533. (goto-char (point-min))
  20534. (while (not (eobp))
  20535. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20536. ;; Don't get fooled by invisible text (e.g. link path)
  20537. ;; when moving to column MIN-INDENT.
  20538. (let ((buffer-invisibility-spec nil))
  20539. (org-move-to-column min-indent t))
  20540. (insert comment-start))
  20541. (forward-line))))))))
  20542. ;;; Planning
  20543. ;; This section contains tools to operate on timestamp objects, as
  20544. ;; returned by, e.g. `org-element-context'.
  20545. (defun org-timestamp-has-time-p (timestamp)
  20546. "Non-nil when TIMESTAMP has a time specified."
  20547. (org-element-property :hour-start timestamp))
  20548. (defun org-timestamp-format (timestamp format &optional end utc)
  20549. "Format a TIMESTAMP element into a string.
  20550. FORMAT is a format specifier to be passed to
  20551. `format-time-string'.
  20552. When optional argument END is non-nil, use end of date-range or
  20553. time-range, if possible.
  20554. When optional argument UTC is non-nil, time will be expressed as
  20555. Universal Time."
  20556. (format-time-string
  20557. format
  20558. (apply 'encode-time
  20559. (cons 0
  20560. (mapcar
  20561. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20562. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20563. '(:minute-start :hour-start :day-start :month-start
  20564. :year-start)))))
  20565. utc))
  20566. (defun org-timestamp-split-range (timestamp &optional end)
  20567. "Extract a timestamp object from a date or time range.
  20568. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20569. the end of the range. Otherwise, extract its start.
  20570. Return a new timestamp object sharing the same parent as
  20571. TIMESTAMP."
  20572. (let ((type (org-element-property :type timestamp)))
  20573. (if (memq type '(active inactive diary)) timestamp
  20574. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20575. ;; Set new type.
  20576. (org-element-put-property
  20577. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20578. ;; Copy start properties over end properties if END is
  20579. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20580. (let ((p-alist '((:minute-start . :minute-end)
  20581. (:hour-start . :hour-end)
  20582. (:day-start . :day-end)
  20583. (:month-start . :month-end)
  20584. (:year-start . :year-end))))
  20585. (dolist (p-cell p-alist)
  20586. (org-element-put-property
  20587. split-ts
  20588. (funcall (if end 'car 'cdr) p-cell)
  20589. (org-element-property
  20590. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20591. ;; Eventually refresh `:raw-value'.
  20592. (org-element-put-property split-ts :raw-value nil)
  20593. (org-element-put-property
  20594. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20595. (defun org-timestamp-translate (timestamp &optional boundary)
  20596. "Apply `org-translate-time' on a TIMESTAMP object.
  20597. When optional argument BOUNDARY is non-nil, it is either the
  20598. symbol `start' or `end'. In this case, only translate the
  20599. starting or ending part of TIMESTAMP if it is a date or time
  20600. range. Otherwise, translate both parts."
  20601. (if (and (not boundary)
  20602. (memq (org-element-property :type timestamp)
  20603. '(active-range inactive-range)))
  20604. (concat
  20605. (org-translate-time
  20606. (org-element-property :raw-value
  20607. (org-timestamp-split-range timestamp)))
  20608. "--"
  20609. (org-translate-time
  20610. (org-element-property :raw-value
  20611. (org-timestamp-split-range timestamp t))))
  20612. (org-translate-time
  20613. (org-element-property
  20614. :raw-value
  20615. (if (not boundary) timestamp
  20616. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20617. ;;; Other stuff.
  20618. (defun org-toggle-fixed-width-section (arg)
  20619. "Toggle the fixed-width export.
  20620. If there is no active region, the QUOTE keyword at the current headline is
  20621. inserted or removed. When present, it causes the text between this headline
  20622. and the next to be exported as fixed-width text, and unmodified.
  20623. If there is an active region, this command adds or removes a colon as the
  20624. first character of this line. If the first character of a line is a colon,
  20625. this line is also exported in fixed-width font."
  20626. (interactive "P")
  20627. (let* ((cc 0)
  20628. (regionp (org-region-active-p))
  20629. (beg (if regionp (region-beginning) (point)))
  20630. (end (if regionp (region-end)))
  20631. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20632. (case-fold-search nil)
  20633. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20634. off)
  20635. (if regionp
  20636. (save-excursion
  20637. (goto-char beg)
  20638. (setq cc (current-column))
  20639. (beginning-of-line 1)
  20640. (setq off (looking-at re))
  20641. (while (> nlines 0)
  20642. (setq nlines (1- nlines))
  20643. (beginning-of-line 1)
  20644. (cond
  20645. (arg
  20646. (org-move-to-column cc t)
  20647. (insert ": \n")
  20648. (forward-line -1))
  20649. ((and off (looking-at re))
  20650. (replace-match "" t t nil 1))
  20651. ((not off) (org-move-to-column cc t) (insert ": ")))
  20652. (forward-line 1)))
  20653. (save-excursion
  20654. (org-back-to-heading)
  20655. (cond
  20656. ((looking-at (format org-heading-keyword-regexp-format
  20657. org-quote-string))
  20658. (goto-char (match-end 1))
  20659. (looking-at (concat " +" org-quote-string))
  20660. (replace-match "" t t)
  20661. (when (eolp) (insert " ")))
  20662. ((looking-at org-outline-regexp)
  20663. (goto-char (match-end 0))
  20664. (insert org-quote-string " ")))))))
  20665. (defun org-reftex-citation ()
  20666. "Use reftex-citation to insert a citation into the buffer.
  20667. This looks for a line like
  20668. #+BIBLIOGRAPHY: foo plain option:-d
  20669. and derives from it that foo.bib is the bibliography file relevant
  20670. for this document. It then installs the necessary environment for RefTeX
  20671. to work in this buffer and calls `reftex-citation' to insert a citation
  20672. into the buffer.
  20673. Export of such citations to both LaTeX and HTML is handled by the contributed
  20674. package org-exp-bibtex by Taru Karttunen."
  20675. (interactive)
  20676. (let ((reftex-docstruct-symbol 'rds)
  20677. (reftex-cite-format "\\cite{%l}")
  20678. rds bib)
  20679. (save-excursion
  20680. (save-restriction
  20681. (widen)
  20682. (let ((case-fold-search t)
  20683. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20684. (if (not (save-excursion
  20685. (or (re-search-forward re nil t)
  20686. (re-search-backward re nil t))))
  20687. (error "No bibliography defined in file")
  20688. (setq bib (concat (match-string 1) ".bib")
  20689. rds (list (list 'bib bib)))))))
  20690. (call-interactively 'reftex-citation)))
  20691. ;;;; Functions extending outline functionality
  20692. (defun org-beginning-of-line (&optional arg)
  20693. "Go to the beginning of the current line. If that is invisible, continue
  20694. to a visible line beginning. This makes the function of C-a more intuitive.
  20695. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20696. first attempt, and only move to after the tags when the cursor is already
  20697. beyond the end of the headline."
  20698. (interactive "P")
  20699. (let ((pos (point))
  20700. (special (if (consp org-special-ctrl-a/e)
  20701. (car org-special-ctrl-a/e)
  20702. org-special-ctrl-a/e))
  20703. refpos)
  20704. (if (org-bound-and-true-p visual-line-mode)
  20705. (beginning-of-visual-line 1)
  20706. (beginning-of-line 1))
  20707. (if (and arg (fboundp 'move-beginning-of-line))
  20708. (call-interactively 'move-beginning-of-line)
  20709. (if (bobp)
  20710. nil
  20711. (backward-char 1)
  20712. (if (org-truely-invisible-p)
  20713. (while (and (not (bobp)) (org-truely-invisible-p))
  20714. (backward-char 1)
  20715. (beginning-of-line 1))
  20716. (forward-char 1))))
  20717. (when special
  20718. (cond
  20719. ((and (looking-at org-complex-heading-regexp)
  20720. (= (char-after (match-end 1)) ?\ ))
  20721. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20722. (point-at-eol)))
  20723. (goto-char
  20724. (if (eq special t)
  20725. (cond ((> pos refpos) refpos)
  20726. ((= pos (point)) refpos)
  20727. (t (point)))
  20728. (cond ((> pos (point)) (point))
  20729. ((not (eq last-command this-command)) (point))
  20730. (t refpos)))))
  20731. ((org-at-item-p)
  20732. ;; Being at an item and not looking at an the item means point
  20733. ;; was previously moved to beginning of a visual line, which
  20734. ;; doesn't contain the item. Therefore, do nothing special,
  20735. ;; just stay here.
  20736. (when (looking-at org-list-full-item-re)
  20737. ;; Set special position at first white space character after
  20738. ;; bullet, and check-box, if any.
  20739. (let ((after-bullet
  20740. (let ((box (match-end 3)))
  20741. (if (not box) (match-end 1)
  20742. (let ((after (char-after box)))
  20743. (if (and after (= after ? )) (1+ box) box))))))
  20744. ;; Special case: Move point to special position when
  20745. ;; currently after it or at beginning of line.
  20746. (if (eq special t)
  20747. (when (or (> pos after-bullet) (= (point) pos))
  20748. (goto-char after-bullet))
  20749. ;; Reversed case: Move point to special position when
  20750. ;; point was already at beginning of line and command is
  20751. ;; repeated.
  20752. (when (and (= (point) pos) (eq last-command this-command))
  20753. (goto-char after-bullet))))))))
  20754. (org-no-warnings
  20755. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20756. (defun org-end-of-line (&optional arg)
  20757. "Go to the end of the line.
  20758. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20759. tags on the first attempt, and only move to after the tags when
  20760. the cursor is already beyond the end of the headline."
  20761. (interactive "P")
  20762. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20763. org-special-ctrl-a/e))
  20764. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20765. 'end-of-visual-line)
  20766. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20767. (t 'end-of-line))))
  20768. (if (or (not special) arg) (call-interactively move-fun)
  20769. (let* ((element (save-excursion (beginning-of-line)
  20770. (org-element-at-point)))
  20771. (type (org-element-type element)))
  20772. (cond
  20773. ((memq type '(headline inlinetask))
  20774. (let ((pos (point)))
  20775. (beginning-of-line 1)
  20776. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20777. (if (eq special t)
  20778. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20779. (goto-char (match-beginning 1))
  20780. (goto-char (match-end 0)))
  20781. (if (or (< pos (match-end 0))
  20782. (not (eq this-command last-command)))
  20783. (goto-char (match-end 0))
  20784. (goto-char (match-beginning 1))))
  20785. (call-interactively move-fun))))
  20786. ((org-element-property :hiddenp element)
  20787. ;; If element is hidden, `move-end-of-line' would put point
  20788. ;; after it. Use `end-of-line' to stay on current line.
  20789. (call-interactively 'end-of-line))
  20790. (t (call-interactively move-fun)))))
  20791. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20792. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20793. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20794. (defun org-backward-sentence (&optional arg)
  20795. "Go to beginning of sentence, or beginning of table field.
  20796. This will call `backward-sentence' or `org-table-beginning-of-field',
  20797. depending on context."
  20798. (interactive "P")
  20799. (cond
  20800. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20801. (t (call-interactively 'backward-sentence))))
  20802. (defun org-forward-sentence (&optional arg)
  20803. "Go to end of sentence, or end of table field.
  20804. This will call `forward-sentence' or `org-table-end-of-field',
  20805. depending on context."
  20806. (interactive "P")
  20807. (cond
  20808. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20809. (t (call-interactively 'forward-sentence))))
  20810. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20811. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20812. (defun org-kill-line (&optional arg)
  20813. "Kill line, to tags or end of line."
  20814. (interactive "P")
  20815. (cond
  20816. ((or (not org-special-ctrl-k)
  20817. (bolp)
  20818. (not (org-at-heading-p)))
  20819. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20820. org-ctrl-k-protect-subtree)
  20821. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20822. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20823. (error "C-k aborted - would kill hidden subtree")))
  20824. (call-interactively
  20825. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20826. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20827. (kill-region (point) (match-beginning 1))
  20828. (org-set-tags nil t))
  20829. (t (kill-region (point) (point-at-eol)))))
  20830. (define-key org-mode-map "\C-k" 'org-kill-line)
  20831. (defun org-yank (&optional arg)
  20832. "Yank. If the kill is a subtree, treat it specially.
  20833. This command will look at the current kill and check if is a single
  20834. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20835. cursor is at the beginning of a line or after the stars of a currently
  20836. empty headline, then the yank is handled specially. How exactly depends
  20837. on the value of the following variables, both set by default.
  20838. org-yank-folded-subtrees
  20839. When set, the subtree(s) will be folded after insertion, but only
  20840. if doing so would now swallow text after the yanked text.
  20841. org-yank-adjusted-subtrees
  20842. When set, the subtree will be promoted or demoted in order to
  20843. fit into the local outline tree structure, which means that the level
  20844. will be adjusted so that it becomes the smaller one of the two
  20845. *visible* surrounding headings.
  20846. Any prefix to this command will cause `yank' to be called directly with
  20847. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20848. will just
  20849. plainly yank the text as it is.
  20850. \[1] The test checks if the first non-white line is a heading
  20851. and if there are no other headings with fewer stars."
  20852. (interactive "P")
  20853. (org-yank-generic 'yank arg))
  20854. (defun org-yank-generic (command arg)
  20855. "Perform some yank-like command.
  20856. This function implements the behavior described in the `org-yank'
  20857. documentation. However, it has been generalized to work for any
  20858. interactive command with similar behavior."
  20859. ;; pretend to be command COMMAND
  20860. (setq this-command command)
  20861. (if arg
  20862. (call-interactively command)
  20863. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20864. (and (org-kill-is-subtree-p)
  20865. (or (bolp)
  20866. (and (looking-at "[ \t]*$")
  20867. (string-match
  20868. "\\`\\*+\\'"
  20869. (buffer-substring (point-at-bol) (point)))))))
  20870. swallowp)
  20871. (cond
  20872. ((and subtreep org-yank-folded-subtrees)
  20873. (let ((beg (point))
  20874. end)
  20875. (if (and subtreep org-yank-adjusted-subtrees)
  20876. (org-paste-subtree nil nil 'for-yank)
  20877. (call-interactively command))
  20878. (setq end (point))
  20879. (goto-char beg)
  20880. (when (and (bolp) subtreep
  20881. (not (setq swallowp
  20882. (org-yank-folding-would-swallow-text beg end))))
  20883. (org-with-limited-levels
  20884. (or (looking-at org-outline-regexp)
  20885. (re-search-forward org-outline-regexp-bol end t))
  20886. (while (and (< (point) end) (looking-at org-outline-regexp))
  20887. (hide-subtree)
  20888. (org-cycle-show-empty-lines 'folded)
  20889. (condition-case nil
  20890. (outline-forward-same-level 1)
  20891. (error (goto-char end))))))
  20892. (when swallowp
  20893. (message
  20894. "Inserted text not folded because that would swallow text"))
  20895. (goto-char end)
  20896. (skip-chars-forward " \t\n\r")
  20897. (beginning-of-line 1)
  20898. (push-mark beg 'nomsg)))
  20899. ((and subtreep org-yank-adjusted-subtrees)
  20900. (let ((beg (point-at-bol)))
  20901. (org-paste-subtree nil nil 'for-yank)
  20902. (push-mark beg 'nomsg)))
  20903. (t
  20904. (call-interactively command))))))
  20905. (defun org-yank-folding-would-swallow-text (beg end)
  20906. "Would hide-subtree at BEG swallow any text after END?"
  20907. (let (level)
  20908. (org-with-limited-levels
  20909. (save-excursion
  20910. (goto-char beg)
  20911. (when (or (looking-at org-outline-regexp)
  20912. (re-search-forward org-outline-regexp-bol end t))
  20913. (setq level (org-outline-level)))
  20914. (goto-char end)
  20915. (skip-chars-forward " \t\r\n\v\f")
  20916. (if (or (eobp)
  20917. (and (bolp) (looking-at org-outline-regexp)
  20918. (<= (org-outline-level) level)))
  20919. nil ; Nothing would be swallowed
  20920. t))))) ; something would swallow
  20921. (define-key org-mode-map "\C-y" 'org-yank)
  20922. (defun org-truely-invisible-p ()
  20923. "Check if point is at a character currently not visible.
  20924. This version does not only check the character property, but also
  20925. `visible-mode'."
  20926. ;; Early versions of noutline don't have `outline-invisible-p'.
  20927. (if (org-bound-and-true-p visible-mode)
  20928. nil
  20929. (outline-invisible-p)))
  20930. (defun org-invisible-p2 ()
  20931. "Check if point is at a character currently not visible."
  20932. (save-excursion
  20933. (if (and (eolp) (not (bobp))) (backward-char 1))
  20934. ;; Early versions of noutline don't have `outline-invisible-p'.
  20935. (outline-invisible-p)))
  20936. (defun org-back-to-heading (&optional invisible-ok)
  20937. "Call `outline-back-to-heading', but provide a better error message."
  20938. (condition-case nil
  20939. (outline-back-to-heading invisible-ok)
  20940. (error (error "Before first headline at position %d in buffer %s"
  20941. (point) (current-buffer)))))
  20942. (defun org-before-first-heading-p ()
  20943. "Before first heading?"
  20944. (save-excursion
  20945. (end-of-line)
  20946. (null (re-search-backward org-outline-regexp-bol nil t))))
  20947. (defun org-at-heading-p (&optional ignored)
  20948. (outline-on-heading-p t))
  20949. ;; Compatibility alias with Org versions < 7.8.03
  20950. (defalias 'org-on-heading-p 'org-at-heading-p)
  20951. (defun org-at-comment-p nil
  20952. "Is cursor in a line starting with a # character?"
  20953. (save-excursion
  20954. (beginning-of-line)
  20955. (looking-at "^#")))
  20956. (defun org-at-drawer-p nil
  20957. "Is cursor at a drawer keyword?"
  20958. (save-excursion
  20959. (move-beginning-of-line 1)
  20960. (looking-at org-drawer-regexp)))
  20961. (defun org-at-block-p nil
  20962. "Is cursor at a block keyword?"
  20963. (save-excursion
  20964. (move-beginning-of-line 1)
  20965. (looking-at org-block-regexp)))
  20966. (defun org-point-at-end-of-empty-headline ()
  20967. "If point is at the end of an empty headline, return t, else nil.
  20968. If the heading only contains a TODO keyword, it is still still considered
  20969. empty."
  20970. (and (looking-at "[ \t]*$")
  20971. (when org-todo-line-regexp
  20972. (save-excursion
  20973. (beginning-of-line 1)
  20974. (let ((case-fold-search nil))
  20975. (looking-at org-todo-line-regexp)
  20976. (string= (match-string 3) ""))))))
  20977. (defun org-at-heading-or-item-p ()
  20978. (or (org-at-heading-p) (org-at-item-p)))
  20979. (defun org-at-target-p ()
  20980. (or (org-in-regexp org-radio-target-regexp)
  20981. (org-in-regexp org-target-regexp)))
  20982. ;; Compatibility alias with Org versions < 7.8.03
  20983. (defalias 'org-on-target-p 'org-at-target-p)
  20984. (defun org-up-heading-all (arg)
  20985. "Move to the heading line of which the present line is a subheading.
  20986. This function considers both visible and invisible heading lines.
  20987. With argument, move up ARG levels."
  20988. (if (fboundp 'outline-up-heading-all)
  20989. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20990. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20991. (defun org-up-heading-safe ()
  20992. "Move to the heading line of which the present line is a subheading.
  20993. This version will not throw an error. It will return the level of the
  20994. headline found, or nil if no higher level is found.
  20995. Also, this function will be a lot faster than `outline-up-heading',
  20996. because it relies on stars being the outline starters. This can really
  20997. make a significant difference in outlines with very many siblings."
  20998. (let (start-level re)
  20999. (org-back-to-heading t)
  21000. (setq start-level (funcall outline-level))
  21001. (if (equal start-level 1)
  21002. nil
  21003. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  21004. (if (re-search-backward re nil t)
  21005. (funcall outline-level)))))
  21006. (defun org-first-sibling-p ()
  21007. "Is this heading the first child of its parents?"
  21008. (interactive)
  21009. (let ((re org-outline-regexp-bol)
  21010. level l)
  21011. (unless (org-at-heading-p t)
  21012. (error "Not at a heading"))
  21013. (setq level (funcall outline-level))
  21014. (save-excursion
  21015. (if (not (re-search-backward re nil t))
  21016. t
  21017. (setq l (funcall outline-level))
  21018. (< l level)))))
  21019. (defun org-goto-sibling (&optional previous)
  21020. "Goto the next sibling, even if it is invisible.
  21021. When PREVIOUS is set, go to the previous sibling instead. Returns t
  21022. when a sibling was found. When none is found, return nil and don't
  21023. move point."
  21024. (let ((fun (if previous 're-search-backward 're-search-forward))
  21025. (pos (point))
  21026. (re org-outline-regexp-bol)
  21027. level l)
  21028. (when (condition-case nil (org-back-to-heading t) (error nil))
  21029. (setq level (funcall outline-level))
  21030. (catch 'exit
  21031. (or previous (forward-char 1))
  21032. (while (funcall fun re nil t)
  21033. (setq l (funcall outline-level))
  21034. (when (< l level) (goto-char pos) (throw 'exit nil))
  21035. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  21036. (goto-char pos)
  21037. nil))))
  21038. (defun org-show-siblings ()
  21039. "Show all siblings of the current headline."
  21040. (save-excursion
  21041. (while (org-goto-sibling) (org-flag-heading nil)))
  21042. (save-excursion
  21043. (while (org-goto-sibling 'previous)
  21044. (org-flag-heading nil))))
  21045. (defun org-goto-first-child ()
  21046. "Goto the first child, even if it is invisible.
  21047. Return t when a child was found. Otherwise don't move point and
  21048. return nil."
  21049. (let (level (pos (point)) (re org-outline-regexp-bol))
  21050. (when (condition-case nil (org-back-to-heading t) (error nil))
  21051. (setq level (outline-level))
  21052. (forward-char 1)
  21053. (if (and (re-search-forward re nil t) (> (outline-level) level))
  21054. (progn (goto-char (match-beginning 0)) t)
  21055. (goto-char pos) nil))))
  21056. (defun org-show-hidden-entry ()
  21057. "Show an entry where even the heading is hidden."
  21058. (save-excursion
  21059. (org-show-entry)))
  21060. (defun org-flag-heading (flag &optional entry)
  21061. "Flag the current heading. FLAG non-nil means make invisible.
  21062. When ENTRY is non-nil, show the entire entry."
  21063. (save-excursion
  21064. (org-back-to-heading t)
  21065. ;; Check if we should show the entire entry
  21066. (if entry
  21067. (progn
  21068. (org-show-entry)
  21069. (save-excursion
  21070. (and (outline-next-heading)
  21071. (org-flag-heading nil))))
  21072. (outline-flag-region (max (point-min) (1- (point)))
  21073. (save-excursion (outline-end-of-heading) (point))
  21074. flag))))
  21075. (defun org-get-next-sibling ()
  21076. "Move to next heading of the same level, and return point.
  21077. If there is no such heading, return nil.
  21078. This is like outline-next-sibling, but invisible headings are ok."
  21079. (let ((level (funcall outline-level)))
  21080. (outline-next-heading)
  21081. (while (and (not (eobp)) (> (funcall outline-level) level))
  21082. (outline-next-heading))
  21083. (if (or (eobp) (< (funcall outline-level) level))
  21084. nil
  21085. (point))))
  21086. (defun org-get-last-sibling ()
  21087. "Move to previous heading of the same level, and return point.
  21088. If there is no such heading, return nil."
  21089. (let ((opoint (point))
  21090. (level (funcall outline-level)))
  21091. (outline-previous-heading)
  21092. (when (and (/= (point) opoint) (outline-on-heading-p t))
  21093. (while (and (> (funcall outline-level) level)
  21094. (not (bobp)))
  21095. (outline-previous-heading))
  21096. (if (< (funcall outline-level) level)
  21097. nil
  21098. (point)))))
  21099. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  21100. "Goto to the end of a subtree."
  21101. ;; This contains an exact copy of the original function, but it uses
  21102. ;; `org-back-to-heading', to make it work also in invisible
  21103. ;; trees. And is uses an invisible-ok argument.
  21104. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  21105. ;; Furthermore, when used inside Org, finding the end of a large subtree
  21106. ;; with many children and grandchildren etc, this can be much faster
  21107. ;; than the outline version.
  21108. (org-back-to-heading invisible-ok)
  21109. (let ((first t)
  21110. (level (funcall outline-level)))
  21111. (if (and (derived-mode-p 'org-mode) (< level 1000))
  21112. ;; A true heading (not a plain list item), in Org-mode
  21113. ;; This means we can easily find the end by looking
  21114. ;; only for the right number of stars. Using a regexp to do
  21115. ;; this is so much faster than using a Lisp loop.
  21116. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  21117. (forward-char 1)
  21118. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  21119. ;; something else, do it the slow way
  21120. (while (and (not (eobp))
  21121. (or first (> (funcall outline-level) level)))
  21122. (setq first nil)
  21123. (outline-next-heading)))
  21124. (unless to-heading
  21125. (if (memq (preceding-char) '(?\n ?\^M))
  21126. (progn
  21127. ;; Go to end of line before heading
  21128. (forward-char -1)
  21129. (if (memq (preceding-char) '(?\n ?\^M))
  21130. ;; leave blank line before heading
  21131. (forward-char -1))))))
  21132. (point))
  21133. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  21134. "Use Org version in org-mode, for dramatic speed-up."
  21135. (if (derived-mode-p 'org-mode)
  21136. (progn
  21137. (org-end-of-subtree nil t)
  21138. (unless (eobp) (backward-char 1)))
  21139. ad-do-it))
  21140. (defun org-end-of-meta-data-and-drawers ()
  21141. "Jump to the first text after meta data and drawers in the current entry.
  21142. This will move over empty lines, lines with planning time stamps,
  21143. clocking lines, and drawers."
  21144. (org-back-to-heading t)
  21145. (let ((end (save-excursion (outline-next-heading) (point)))
  21146. (re (concat "\\(" org-drawer-regexp "\\)"
  21147. "\\|" "[ \t]*" org-keyword-time-regexp)))
  21148. (forward-line 1)
  21149. (while (re-search-forward re end t)
  21150. (if (not (match-end 1))
  21151. ;; empty or planning line
  21152. (forward-line 1)
  21153. ;; a drawer, find the end
  21154. (re-search-forward "^[ \t]*:END:" end 'move)
  21155. (forward-line 1)))
  21156. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  21157. (point)))
  21158. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  21159. "Move forward to the ARG'th subheading at same level as this one.
  21160. Stop at the first and last subheadings of a superior heading.
  21161. Normally this only looks at visible headings, but when INVISIBLE-OK is
  21162. non-nil it will also look at invisible ones."
  21163. (interactive "p")
  21164. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  21165. (if (and arg (< arg 0))
  21166. (goto-char (point-min))
  21167. (outline-next-heading))
  21168. (org-at-heading-p)
  21169. (let ((level (- (match-end 0) (match-beginning 0) 1))
  21170. (f (if (and arg (< arg 0))
  21171. 're-search-backward
  21172. 're-search-forward))
  21173. (count (if arg (abs arg) 1))
  21174. (result (point)))
  21175. (while (and (prog1 (> count 0)
  21176. (forward-char (if (and arg (< arg 0)) -1 1)))
  21177. (funcall f org-outline-regexp-bol nil 'move))
  21178. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  21179. (cond ((< l level) (setq count 0))
  21180. ((and (= l level)
  21181. (or invisible-ok
  21182. (progn
  21183. (goto-char (line-beginning-position))
  21184. (not (outline-invisible-p)))))
  21185. (setq count (1- count))
  21186. (when (eq l level)
  21187. (setq result (point)))))))
  21188. (goto-char result))
  21189. (beginning-of-line 1)))
  21190. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  21191. "Move backward to the ARG'th subheading at same level as this one.
  21192. Stop at the first and last subheadings of a superior heading."
  21193. (interactive "p")
  21194. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  21195. (defun org-next-block (arg &optional backward block-regexp)
  21196. "Jump to the next block.
  21197. With a prefix argument ARG, jump forward ARG many source blocks.
  21198. When BACKWARD is non-nil, jump to the previous block.
  21199. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21200. (interactive "p")
  21201. (let ((re (or block-regexp org-block-regexp))
  21202. (re-search-fn (or (and backward 're-search-backward)
  21203. 're-search-forward)))
  21204. (if (looking-at re) (forward-char 1))
  21205. (condition-case nil
  21206. (funcall re-search-fn re nil nil arg)
  21207. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  21208. (goto-char (match-beginning 0)) (org-show-context)))
  21209. (defun org-previous-block (arg &optional block-regexp)
  21210. "Jump to the previous block.
  21211. With a prefix argument ARG, jump backward ARG many source blocks.
  21212. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  21213. (interactive "p")
  21214. (org-next-block arg t block-regexp))
  21215. (defun org-forward-element ()
  21216. "Move forward by one element.
  21217. Move to the next element at the same level, when possible."
  21218. (interactive)
  21219. (cond ((eobp) (user-error "Cannot move further down"))
  21220. ((org-with-limited-levels (org-at-heading-p))
  21221. (let ((origin (point)))
  21222. (goto-char (org-end-of-subtree nil t))
  21223. (unless (org-with-limited-levels (org-at-heading-p))
  21224. (goto-char origin)
  21225. (user-error "Cannot move further down"))))
  21226. (t
  21227. (let* ((elem (org-element-at-point))
  21228. (end (org-element-property :end elem))
  21229. (parent (org-element-property :parent elem)))
  21230. (if (and parent (= (org-element-property :contents-end parent) end))
  21231. (goto-char (org-element-property :end parent))
  21232. (goto-char end))))))
  21233. (defun org-backward-element ()
  21234. "Move backward by one element.
  21235. Move to the previous element at the same level, when possible."
  21236. (interactive)
  21237. (cond ((bobp) (user-error "Cannot move further up"))
  21238. ((org-with-limited-levels (org-at-heading-p))
  21239. ;; At a headline, move to the previous one, if any, or stay
  21240. ;; here.
  21241. (let ((origin (point)))
  21242. (org-with-limited-levels (org-backward-heading-same-level 1))
  21243. ;; When current headline has no sibling above, move to its
  21244. ;; parent.
  21245. (when (= (point) origin)
  21246. (or (org-with-limited-levels (org-up-heading-safe))
  21247. (progn (goto-char origin)
  21248. (user-error "Cannot move further up"))))))
  21249. (t
  21250. (let* ((trail (org-element-at-point 'keep-trail))
  21251. (elem (car trail))
  21252. (prev-elem (nth 1 trail))
  21253. (beg (org-element-property :begin elem)))
  21254. (cond
  21255. ;; Move to beginning of current element if point isn't
  21256. ;; there already.
  21257. ((/= (point) beg) (goto-char beg))
  21258. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  21259. ((org-before-first-heading-p) (goto-char (point-min)))
  21260. (t (org-back-to-heading)))))))
  21261. (defun org-up-element ()
  21262. "Move to upper element."
  21263. (interactive)
  21264. (if (org-with-limited-levels (org-at-heading-p))
  21265. (unless (org-up-heading-safe) (error "No surrounding element"))
  21266. (let* ((elem (org-element-at-point))
  21267. (parent (org-element-property :parent elem)))
  21268. (if parent (goto-char (org-element-property :begin parent))
  21269. (if (org-with-limited-levels (org-before-first-heading-p))
  21270. (error "No surrounding element")
  21271. (org-with-limited-levels (org-back-to-heading)))))))
  21272. (defvar org-element-greater-elements)
  21273. (defun org-down-element ()
  21274. "Move to inner element."
  21275. (interactive)
  21276. (let ((element (org-element-at-point)))
  21277. (cond
  21278. ((memq (org-element-type element) '(plain-list table))
  21279. (goto-char (org-element-property :contents-begin element))
  21280. (forward-char))
  21281. ((memq (org-element-type element) org-element-greater-elements)
  21282. ;; If contents are hidden, first disclose them.
  21283. (when (org-element-property :hiddenp element) (org-cycle))
  21284. (goto-char (or (org-element-property :contents-begin element)
  21285. (error "No content for this element"))))
  21286. (t (error "No inner element")))))
  21287. (defun org-drag-element-backward ()
  21288. "Move backward element at point."
  21289. (interactive)
  21290. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  21291. (let* ((trail (org-element-at-point 'keep-trail))
  21292. (elem (car trail))
  21293. (prev-elem (nth 1 trail)))
  21294. ;; Error out if no previous element or previous element is
  21295. ;; a parent of the current one.
  21296. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  21297. (error "Cannot drag element backward")
  21298. (let ((pos (point)))
  21299. (org-element-swap-A-B prev-elem elem)
  21300. (goto-char (+ (org-element-property :begin prev-elem)
  21301. (- pos (org-element-property :begin elem)))))))))
  21302. (defun org-drag-element-forward ()
  21303. "Move forward element at point."
  21304. (interactive)
  21305. (let* ((pos (point))
  21306. (elem (org-element-at-point)))
  21307. (when (= (point-max) (org-element-property :end elem))
  21308. (error "Cannot drag element forward"))
  21309. (goto-char (org-element-property :end elem))
  21310. (let ((next-elem (org-element-at-point)))
  21311. (when (or (org-element-nested-p elem next-elem)
  21312. (and (eq (org-element-type next-elem) 'headline)
  21313. (not (eq (org-element-type elem) 'headline))))
  21314. (goto-char pos)
  21315. (error "Cannot drag element forward"))
  21316. ;; Compute new position of point: it's shifted by NEXT-ELEM
  21317. ;; body's length (without final blanks) and by the length of
  21318. ;; blanks between ELEM and NEXT-ELEM.
  21319. (let ((size-next (- (save-excursion
  21320. (goto-char (org-element-property :end next-elem))
  21321. (skip-chars-backward " \r\t\n")
  21322. (forward-line)
  21323. ;; Small correction if buffer doesn't end
  21324. ;; with a newline character.
  21325. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  21326. (org-element-property :begin next-elem)))
  21327. (size-blank (- (org-element-property :end elem)
  21328. (save-excursion
  21329. (goto-char (org-element-property :end elem))
  21330. (skip-chars-backward " \r\t\n")
  21331. (forward-line)
  21332. (point)))))
  21333. (org-element-swap-A-B elem next-elem)
  21334. (goto-char (+ pos size-next size-blank))))))
  21335. (defun org-drag-line-forward (arg)
  21336. "Drag the line at point ARG lines forward."
  21337. (interactive "p")
  21338. (dotimes (n (abs arg))
  21339. (let ((c (current-column)))
  21340. (if (< 0 arg)
  21341. (progn
  21342. (beginning-of-line 2)
  21343. (transpose-lines 1)
  21344. (beginning-of-line 0))
  21345. (transpose-lines 1)
  21346. (beginning-of-line -1))
  21347. (org-move-to-column c))))
  21348. (defun org-drag-line-backward (arg)
  21349. "Drag the line at point ARG lines backward."
  21350. (interactive "p")
  21351. (org-drag-line-forward (- arg)))
  21352. (defun org-mark-element ()
  21353. "Put point at beginning of this element, mark at end.
  21354. Interactively, if this command is repeated or (in Transient Mark
  21355. mode) if the mark is active, it marks the next element after the
  21356. ones already marked."
  21357. (interactive)
  21358. (let (deactivate-mark)
  21359. (if (and (org-called-interactively-p 'any)
  21360. (or (and (eq last-command this-command) (mark t))
  21361. (and transient-mark-mode mark-active)))
  21362. (set-mark
  21363. (save-excursion
  21364. (goto-char (mark))
  21365. (goto-char (org-element-property :end (org-element-at-point)))))
  21366. (let ((element (org-element-at-point)))
  21367. (end-of-line)
  21368. (push-mark (org-element-property :end element) t t)
  21369. (goto-char (org-element-property :begin element))))))
  21370. (defun org-narrow-to-element ()
  21371. "Narrow buffer to current element."
  21372. (interactive)
  21373. (let ((elem (org-element-at-point)))
  21374. (cond
  21375. ((eq (car elem) 'headline)
  21376. (narrow-to-region
  21377. (org-element-property :begin elem)
  21378. (org-element-property :end elem)))
  21379. ((memq (car elem) org-element-greater-elements)
  21380. (narrow-to-region
  21381. (org-element-property :contents-begin elem)
  21382. (org-element-property :contents-end elem)))
  21383. (t
  21384. (narrow-to-region
  21385. (org-element-property :begin elem)
  21386. (org-element-property :end elem))))))
  21387. (defun org-transpose-element ()
  21388. "Transpose current and previous elements, keeping blank lines between.
  21389. Point is moved after both elements."
  21390. (interactive)
  21391. (org-skip-whitespace)
  21392. (let ((end (org-element-property :end (org-element-at-point))))
  21393. (org-drag-element-backward)
  21394. (goto-char end)))
  21395. (defun org-unindent-buffer ()
  21396. "Un-indent the visible part of the buffer.
  21397. Relative indentation (between items, inside blocks, etc.) isn't
  21398. modified."
  21399. (interactive)
  21400. (unless (eq major-mode 'org-mode)
  21401. (error "Cannot un-indent a buffer not in Org mode"))
  21402. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21403. unindent-tree ; For byte-compiler.
  21404. (unindent-tree
  21405. (function
  21406. (lambda (contents)
  21407. (mapc
  21408. (lambda (element)
  21409. (if (memq (org-element-type element) '(headline section))
  21410. (funcall unindent-tree (org-element-contents element))
  21411. (save-excursion
  21412. (save-restriction
  21413. (narrow-to-region
  21414. (org-element-property :begin element)
  21415. (org-element-property :end element))
  21416. (org-do-remove-indentation)))))
  21417. (reverse contents))))))
  21418. (funcall unindent-tree (org-element-contents parse-tree))))
  21419. (defun org-show-subtree ()
  21420. "Show everything after this heading at deeper levels."
  21421. (interactive)
  21422. (outline-flag-region
  21423. (point)
  21424. (save-excursion
  21425. (org-end-of-subtree t t))
  21426. nil))
  21427. (defun org-show-entry ()
  21428. "Show the body directly following this heading.
  21429. Show the heading too, if it is currently invisible."
  21430. (interactive)
  21431. (save-excursion
  21432. (condition-case nil
  21433. (progn
  21434. (org-back-to-heading t)
  21435. (outline-flag-region
  21436. (max (point-min) (1- (point)))
  21437. (save-excursion
  21438. (if (re-search-forward
  21439. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21440. (match-beginning 1)
  21441. (point-max)))
  21442. nil)
  21443. (org-cycle-hide-drawers 'children))
  21444. (error nil))))
  21445. (defun org-make-options-regexp (kwds &optional extra)
  21446. "Make a regular expression for keyword lines."
  21447. (concat
  21448. "^#\\+\\("
  21449. (mapconcat 'regexp-quote kwds "\\|")
  21450. (if extra (concat "\\|" extra))
  21451. "\\):[ \t]*\\(.*\\)"))
  21452. ;; Make isearch reveal the necessary context
  21453. (defun org-isearch-end ()
  21454. "Reveal context after isearch exits."
  21455. (when isearch-success ; only if search was successful
  21456. (if (featurep 'xemacs)
  21457. ;; Under XEmacs, the hook is run in the correct place,
  21458. ;; we directly show the context.
  21459. (org-show-context 'isearch)
  21460. ;; In Emacs the hook runs *before* restoring the overlays.
  21461. ;; So we have to use a one-time post-command-hook to do this.
  21462. ;; (Emacs 22 has a special variable, see function `org-mode')
  21463. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21464. isearch-mode-end-hook-quit)
  21465. ;; Only when the isearch was not quitted.
  21466. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21467. 'append 'local)))
  21468. (org-fix-ellipsis-at-bol)))
  21469. (defun org-isearch-post-command ()
  21470. "Remove self from hook, and show context."
  21471. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21472. (org-show-context 'isearch))
  21473. ;;;; Integration with and fixes for other packages
  21474. ;;; Imenu support
  21475. (defvar org-imenu-markers nil
  21476. "All markers currently used by Imenu.")
  21477. (make-variable-buffer-local 'org-imenu-markers)
  21478. (defun org-imenu-new-marker (&optional pos)
  21479. "Return a new marker for use by Imenu, and remember the marker."
  21480. (let ((m (make-marker)))
  21481. (move-marker m (or pos (point)))
  21482. (push m org-imenu-markers)
  21483. m))
  21484. (defun org-imenu-get-tree ()
  21485. "Produce the index for Imenu."
  21486. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21487. (setq org-imenu-markers nil)
  21488. (let* ((n org-imenu-depth)
  21489. (re (concat "^" (org-get-limited-outline-regexp)))
  21490. (subs (make-vector (1+ n) nil))
  21491. (last-level 0)
  21492. m level head0 head)
  21493. (save-excursion
  21494. (save-restriction
  21495. (widen)
  21496. (goto-char (point-max))
  21497. (while (re-search-backward re nil t)
  21498. (setq level (org-reduced-level (funcall outline-level)))
  21499. (when (and (<= level n)
  21500. (looking-at org-complex-heading-regexp)
  21501. (setq head0 (org-match-string-no-properties 4)))
  21502. (setq head (org-link-display-format head0)
  21503. m (org-imenu-new-marker))
  21504. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21505. (if (>= level last-level)
  21506. (push (cons head m) (aref subs level))
  21507. (push (cons head (aref subs (1+ level))) (aref subs level))
  21508. (loop for i from (1+ level) to n do (aset subs i nil)))
  21509. (setq last-level level)))))
  21510. (aref subs 1)))
  21511. (eval-after-load "imenu"
  21512. '(progn
  21513. (add-hook 'imenu-after-jump-hook
  21514. (lambda ()
  21515. (if (derived-mode-p 'org-mode)
  21516. (org-show-context 'org-goto))))))
  21517. (defun org-link-display-format (link)
  21518. "Replace a link with its the description.
  21519. If there is no description, use the link target."
  21520. (save-match-data
  21521. (if (string-match org-bracket-link-analytic-regexp link)
  21522. (replace-match (if (match-end 5)
  21523. (match-string 5 link)
  21524. (concat (match-string 1 link)
  21525. (match-string 3 link)))
  21526. nil t link)
  21527. link)))
  21528. (defun org-toggle-link-display ()
  21529. "Toggle the literal or descriptive display of links."
  21530. (interactive)
  21531. (if org-descriptive-links
  21532. (progn (org-remove-from-invisibility-spec '(org-link))
  21533. (org-restart-font-lock)
  21534. (setq org-descriptive-links nil))
  21535. (progn (add-to-invisibility-spec '(org-link))
  21536. (org-restart-font-lock)
  21537. (setq org-descriptive-links t))))
  21538. ;; Speedbar support
  21539. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21540. "Overlay marking the agenda restriction line in speedbar.")
  21541. (overlay-put org-speedbar-restriction-lock-overlay
  21542. 'face 'org-agenda-restriction-lock)
  21543. (overlay-put org-speedbar-restriction-lock-overlay
  21544. 'help-echo "Agendas are currently limited to this item.")
  21545. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21546. (defun org-speedbar-set-agenda-restriction ()
  21547. "Restrict future agenda commands to the location at point in speedbar.
  21548. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21549. (interactive)
  21550. (require 'org-agenda)
  21551. (let (p m tp np dir txt)
  21552. (cond
  21553. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21554. 'org-imenu t))
  21555. (setq m (get-text-property p 'org-imenu-marker))
  21556. (with-current-buffer (marker-buffer m)
  21557. (goto-char m)
  21558. (org-agenda-set-restriction-lock 'subtree)))
  21559. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21560. 'speedbar-function 'speedbar-find-file))
  21561. (setq tp (previous-single-property-change
  21562. (1+ p) 'speedbar-function)
  21563. np (next-single-property-change
  21564. tp 'speedbar-function)
  21565. dir (speedbar-line-directory)
  21566. txt (buffer-substring-no-properties (or tp (point-min))
  21567. (or np (point-max))))
  21568. (with-current-buffer (find-file-noselect
  21569. (let ((default-directory dir))
  21570. (expand-file-name txt)))
  21571. (unless (derived-mode-p 'org-mode)
  21572. (error "Cannot restrict to non-Org-mode file"))
  21573. (org-agenda-set-restriction-lock 'file)))
  21574. (t (error "Don't know how to restrict Org-mode's agenda")))
  21575. (move-overlay org-speedbar-restriction-lock-overlay
  21576. (point-at-bol) (point-at-eol))
  21577. (setq current-prefix-arg nil)
  21578. (org-agenda-maybe-redo)))
  21579. (eval-after-load "speedbar"
  21580. '(progn
  21581. (speedbar-add-supported-extension ".org")
  21582. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21583. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21584. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21585. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21586. (add-hook 'speedbar-visiting-tag-hook
  21587. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21588. ;;; Fixes and Hacks for problems with other packages
  21589. ;; Make flyspell not check words in links, to not mess up our keymap
  21590. (defvar org-element-affiliated-keywords) ; From org-element.el
  21591. (defvar org-element-block-name-alist) ; From org-element.el
  21592. (defun org-mode-flyspell-verify ()
  21593. "Don't let flyspell put overlays at active buttons, or on
  21594. {todo,all-time,additional-option-like}-keywords."
  21595. (require 'org-element) ; For `org-element-affiliated-keywords'
  21596. (let ((pos (max (1- (point)) (point-min)))
  21597. (word (thing-at-point 'word)))
  21598. (and (not (get-text-property pos 'keymap))
  21599. (not (get-text-property pos 'org-no-flyspell))
  21600. (not (member word org-todo-keywords-1))
  21601. (not (member word org-all-time-keywords))
  21602. (not (member word org-options-keywords))
  21603. (not (member word (mapcar 'car org-startup-options)))
  21604. (not (member-ignore-case word org-element-affiliated-keywords))
  21605. (not (member-ignore-case word (org-get-export-keywords)))
  21606. (not (member-ignore-case
  21607. word (mapcar 'car org-element-block-name-alist)))
  21608. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21609. (defun org-remove-flyspell-overlays-in (beg end)
  21610. "Remove flyspell overlays in region."
  21611. (and (org-bound-and-true-p flyspell-mode)
  21612. (fboundp 'flyspell-delete-region-overlays)
  21613. (flyspell-delete-region-overlays beg end))
  21614. (add-text-properties beg end '(org-no-flyspell t)))
  21615. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21616. (eval-after-load "bookmark"
  21617. '(if (boundp 'bookmark-after-jump-hook)
  21618. ;; We can use the hook
  21619. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21620. ;; Hook not available, use advice
  21621. (defadvice bookmark-jump (after org-make-visible activate)
  21622. "Make the position visible."
  21623. (org-bookmark-jump-unhide))))
  21624. ;; Make sure saveplace shows the location if it was hidden
  21625. (eval-after-load "saveplace"
  21626. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21627. "Make the position visible."
  21628. (org-bookmark-jump-unhide)))
  21629. ;; Make sure ecb shows the location if it was hidden
  21630. (eval-after-load "ecb"
  21631. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21632. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21633. (if (derived-mode-p 'org-mode)
  21634. (org-show-context))))
  21635. (defun org-bookmark-jump-unhide ()
  21636. "Unhide the current position, to show the bookmark location."
  21637. (and (derived-mode-p 'org-mode)
  21638. (or (outline-invisible-p)
  21639. (save-excursion (goto-char (max (point-min) (1- (point))))
  21640. (outline-invisible-p)))
  21641. (org-show-context 'bookmark-jump)))
  21642. ;; Make session.el ignore our circular variable
  21643. (eval-after-load "session"
  21644. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21645. ;;;; Finish up
  21646. (provide 'org)
  21647. (run-hooks 'org-load-hook)
  21648. ;;; org.el ends here