org.el 877 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181
  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 an 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. (when (fboundp 'defvaralias)
  94. (unless (boundp 'calendar-view-holidays-initially-flag)
  95. (defvaralias 'calendar-view-holidays-initially-flag
  96. 'view-calendar-holidays-initially))
  97. (unless (boundp 'calendar-view-diary-initially-flag)
  98. (defvaralias 'calendar-view-diary-initially-flag
  99. 'view-diary-entries-initially))
  100. (unless (boundp 'diary-fancy-buffer)
  101. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  102. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  103. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  104. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  105. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  106. (declare-function org-at-clock-log-p "org-clock" ())
  107. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  108. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  109. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  110. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  111. (declare-function orgtbl-mode "org-table" (&optional arg))
  112. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  113. (declare-function org-beamer-mode "ox-beamer" ())
  114. (declare-function org-table-edit-field "org-table" (arg))
  115. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  116. (declare-function org-id-get-create "org-id" (&optional force))
  117. (declare-function org-id-find-id-file "org-id" (id))
  118. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  119. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  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-type "org-element" (element))
  145. ;; load languages based on value of `org-babel-load-languages'
  146. (defvar org-babel-load-languages)
  147. ;;;###autoload
  148. (defun org-babel-do-load-languages (sym value)
  149. "Load the languages defined in `org-babel-load-languages'."
  150. (set-default sym value)
  151. (mapc (lambda (pair)
  152. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  153. (if active
  154. (progn
  155. (require (intern (concat "ob-" lang))))
  156. (progn
  157. (funcall 'fmakunbound
  158. (intern (concat "org-babel-execute:" lang)))
  159. (funcall 'fmakunbound
  160. (intern (concat "org-babel-expand-body:" lang)))))))
  161. org-babel-load-languages))
  162. (defcustom org-babel-load-languages '((emacs-lisp . t))
  163. "Languages which can be evaluated in Org-mode buffers.
  164. This list can be used to load support for any of the languages
  165. below, note that each language will depend on a different set of
  166. system executables and/or Emacs modes. When a language is
  167. \"loaded\", then code blocks in that language can be evaluated
  168. with `org-babel-execute-src-block' bound by default to C-c
  169. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  170. be set to remove code block evaluation from the C-c C-c
  171. keybinding. By default only Emacs Lisp (which has no
  172. requirements) is loaded."
  173. :group 'org-babel
  174. :set 'org-babel-do-load-languages
  175. :version "24.1"
  176. :type '(alist :tag "Babel Languages"
  177. :key-type
  178. (choice
  179. (const :tag "Awk" awk)
  180. (const :tag "C" C)
  181. (const :tag "R" R)
  182. (const :tag "Asymptote" asymptote)
  183. (const :tag "Calc" calc)
  184. (const :tag "Clojure" clojure)
  185. (const :tag "CSS" css)
  186. (const :tag "Ditaa" ditaa)
  187. (const :tag "Dot" dot)
  188. (const :tag "Emacs Lisp" emacs-lisp)
  189. (const :tag "Fortran" fortran)
  190. (const :tag "Gnuplot" gnuplot)
  191. (const :tag "Haskell" haskell)
  192. (const :tag "IO" io)
  193. (const :tag "Java" java)
  194. (const :tag "Javascript" js)
  195. (const :tag "LaTeX" latex)
  196. (const :tag "Ledger" ledger)
  197. (const :tag "Lilypond" lilypond)
  198. (const :tag "Lisp" lisp)
  199. (const :tag "Makefile" makefile)
  200. (const :tag "Maxima" maxima)
  201. (const :tag "Matlab" matlab)
  202. (const :tag "Mscgen" mscgen)
  203. (const :tag "Ocaml" ocaml)
  204. (const :tag "Octave" octave)
  205. (const :tag "Org" org)
  206. (const :tag "Perl" perl)
  207. (const :tag "Pico Lisp" picolisp)
  208. (const :tag "PlantUML" plantuml)
  209. (const :tag "Python" python)
  210. (const :tag "Ruby" ruby)
  211. (const :tag "Sass" sass)
  212. (const :tag "Scala" scala)
  213. (const :tag "Scheme" scheme)
  214. (const :tag "Screen" screen)
  215. (const :tag "Shell Script" sh)
  216. (const :tag "Shen" shen)
  217. (const :tag "Sql" sql)
  218. (const :tag "Sqlite" sqlite))
  219. :value-type (boolean :tag "Activate" :value t)))
  220. ;;;; Customization variables
  221. (defcustom org-clone-delete-id nil
  222. "Remove ID property of clones of a subtree.
  223. When non-nil, clones of a subtree don't inherit the ID property.
  224. Otherwise they inherit the ID property with a new unique
  225. identifier."
  226. :type 'boolean
  227. :version "24.1"
  228. :group 'org-id)
  229. ;;; Version
  230. (org-check-version)
  231. ;;;###autoload
  232. (defun org-version (&optional here full message)
  233. "Show the org-mode version in the echo area.
  234. With prefix argument HERE, insert it at point.
  235. When FULL is non-nil, use a verbose version string.
  236. When MESSAGE is non-nil, display a message with the version."
  237. (interactive "P")
  238. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  239. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  240. (load-suffixes (list ".el"))
  241. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  242. (org-trash (or
  243. (and (fboundp 'org-release) (fboundp 'org-git-version))
  244. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  245. (load-suffixes save-load-suffixes)
  246. (org-version (org-release))
  247. (git-version (org-git-version))
  248. (version (format "Org-mode version %s (%s @ %s)"
  249. org-version
  250. git-version
  251. (if org-install-dir
  252. (if (string= org-dir org-install-dir)
  253. org-install-dir
  254. (concat "mixed installation! " org-install-dir " and " org-dir))
  255. "org-loaddefs.el can not be found!")))
  256. (_version (if full version org-version)))
  257. (if (org-called-interactively-p 'interactive)
  258. (if here
  259. (insert version)
  260. (message version))
  261. (if message (message _version))
  262. _version)))
  263. (defconst org-version (org-version))
  264. ;;; Compatibility constants
  265. ;;; The custom variables
  266. (defgroup org nil
  267. "Outline-based notes management and organizer."
  268. :tag "Org"
  269. :group 'outlines
  270. :group 'calendar)
  271. (defcustom org-mode-hook nil
  272. "Mode hook for Org-mode, run after the mode was turned on."
  273. :group 'org
  274. :type 'hook)
  275. (defcustom org-load-hook nil
  276. "Hook that is run after org.el has been loaded."
  277. :group 'org
  278. :type 'hook)
  279. (defcustom org-log-buffer-setup-hook nil
  280. "Hook that is run after an Org log buffer is created."
  281. :group 'org
  282. :version "24.1"
  283. :type 'hook)
  284. (defvar org-modules) ; defined below
  285. (defvar org-modules-loaded nil
  286. "Have the modules been loaded already?")
  287. (defun org-load-modules-maybe (&optional force)
  288. "Load all extensions listed in `org-modules'."
  289. (when (or force (not org-modules-loaded))
  290. (mapc (lambda (ext)
  291. (condition-case nil (require ext)
  292. (error (message "Problems while trying to load feature `%s'" ext))))
  293. org-modules)
  294. (setq org-modules-loaded t)))
  295. (defun org-set-modules (var value)
  296. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  297. (set var value)
  298. (when (featurep 'org)
  299. (org-load-modules-maybe 'force)))
  300. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  301. "Modules that should always be loaded together with org.el.
  302. If a description starts with <C>, the file is not part of Emacs
  303. and loading it will require that you have downloaded and properly
  304. installed the Org mode distribution.
  305. You can also use this system to load external packages (i.e. neither Org
  306. core modules, nor modules from the CONTRIB directory). Just add symbols
  307. to the end of the list. If the package is called org-xyz.el, then you need
  308. to add the symbol `xyz', and the package must have a call to:
  309. \(provide 'org-xyz)
  310. For export specific modules, see also `org-export-backends'."
  311. :group 'org
  312. :set 'org-set-modules
  313. :type
  314. '(set :greedy t
  315. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  316. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  317. (const :tag " crypt: Encryption of subtrees" org-crypt)
  318. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  319. (const :tag " docview: Links to doc-view buffers" org-docview)
  320. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  321. (const :tag " id: Global IDs for identifying entries" org-id)
  322. (const :tag " info: Links to Info nodes" org-info)
  323. (const :tag " habit: Track your consistency with habits" org-habit)
  324. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  325. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  326. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  327. (const :tag " mew Links to Mew folders/messages" org-mew)
  328. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  329. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  330. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  331. (const :tag " vm: Links to VM folders/messages" org-vm)
  332. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  333. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  334. (const :tag " mouse: Additional mouse support" org-mouse)
  335. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  336. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  337. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  338. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  339. (const :tag "C collector: Collect properties into tables" org-collector)
  340. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  341. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  342. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  343. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  344. (const :tag "C eval: Include command output as text" org-eval)
  345. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  346. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  347. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  348. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  349. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  350. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  351. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  352. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  353. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  354. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  355. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  356. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  357. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  358. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  359. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  360. (const :tag "C registry: A registry for Org-mode links" org-registry)
  361. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  362. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  363. (const :tag "C secretary: Team management with org-mode" org-secretary)
  364. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  365. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  366. (const :tag "C track: Keep up with Org-mode development" org-track)
  367. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  368. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  369. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  370. (defvar org-export-registered-backends) ; From ox.el
  371. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  372. (defcustom org-export-backends '(ascii html icalendar latex)
  373. "List of export back-ends that should be always available.
  374. If a description starts with <C>, the file is not part of Emacs
  375. and loading it will require that you have downloaded and properly
  376. installed the Org mode distribution.
  377. Unlike to `org-modules', libraries in this list will not be
  378. loaded along with Org, but only once the export framework is
  379. needed.
  380. This variable needs to be set before org.el is loaded. If you
  381. need to make a change while Emacs is running, use the customize
  382. interface or run the following code, where VALUE stands for the
  383. new value of the variable, after updating it:
  384. \(progn
  385. \(setq org-export-registered-backends
  386. \(org-remove-if-not
  387. \(lambda (backend)
  388. \(or (memq backend val)
  389. \(catch 'parentp
  390. \(mapc
  391. \(lambda (b)
  392. \(and (org-export-derived-backend-p b (car backend))
  393. \(throw 'parentp t)))
  394. val)
  395. nil)))
  396. org-export-registered-backends))
  397. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  398. \(dolist (backend val)
  399. \(cond
  400. \((not (load (format \"ox-%s\" backend) t t))
  401. \(message \"Problems while trying to load export back-end `%s'\"
  402. backend))
  403. \((not (memq backend new-list)) (push backend new-list))))
  404. \(set-default var new-list)))
  405. Adding a back-end to this list will also pull the back-end it
  406. depends on, if any."
  407. :group 'org
  408. :group 'org-export
  409. :set (lambda (var val)
  410. (if (not (featurep 'ox)) (set-default var val)
  411. ;; Any back-end not required anymore (not present in VAL and not
  412. ;; a parent of any back-end in the new value) is removed from the
  413. ;; list of registered back-ends.
  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. ;; Now build NEW-LIST of both new back-ends and required
  427. ;; parents.
  428. (let ((new-list (mapcar 'car org-export-registered-backends)))
  429. (dolist (backend val)
  430. (cond
  431. ((not (load (format "ox-%s" backend) t t))
  432. (message "Problems while trying to load export back-end `%s'"
  433. backend))
  434. ((not (memq backend new-list)) (push backend new-list))))
  435. ;; Set VAR to that list with fixed dependencies.
  436. (set-default var new-list))))
  437. :type '(set :greedy t
  438. (const :tag " ascii Export buffer to ASCII format" ascii)
  439. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  440. (const :tag " html Export buffer to HTML format" html)
  441. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  442. (const :tag " latex Export buffer to LaTeX format" latex)
  443. (const :tag " man Export buffer to MAN format" man)
  444. (const :tag " md Export buffer to Markdown format" md)
  445. (const :tag " odt Export buffer to ODT format" odt)
  446. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  447. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" jsinfo)
  448. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  449. (const :tag "C groff Export buffer to Groff format" groff)
  450. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  451. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  452. (eval-after-load 'ox
  453. '(mapc
  454. (lambda (backend)
  455. (condition-case nil (require (intern (format "ox-%s" backend)))
  456. (error (message "Problems while trying to load export back-end `%s'"
  457. backend))))
  458. org-export-backends))
  459. (defcustom org-support-shift-select nil
  460. "Non-nil means make shift-cursor commands select text when possible.
  461. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  462. start selecting a region, or enlarge regions started in this way.
  463. In Org-mode, in special contexts, these same keys are used for
  464. other purposes, important enough to compete with shift selection.
  465. Org tries to balance these needs by supporting `shift-select-mode'
  466. outside these special contexts, under control of this variable.
  467. The default of this variable is nil, to avoid confusing behavior. Shifted
  468. cursor keys will then execute Org commands in the following contexts:
  469. - on a headline, changing TODO state (left/right) and priority (up/down)
  470. - on a time stamp, changing the time
  471. - in a plain list item, changing the bullet type
  472. - in a property definition line, switching between allowed values
  473. - in the BEGIN line of a clock table (changing the time block).
  474. Outside these contexts, the commands will throw an error.
  475. When this variable is t and the cursor is not in a special
  476. context, Org-mode will support shift-selection for making and
  477. enlarging regions. To make this more effective, the bullet
  478. cycling will no longer happen anywhere in an item line, but only
  479. if the cursor is exactly on the bullet.
  480. If you set this variable to the symbol `always', then the keys
  481. will not be special in headlines, property lines, and item lines,
  482. to make shift selection work there as well. If this is what you
  483. want, you can use the following alternative commands: `C-c C-t'
  484. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  485. can be used to switch TODO sets, `C-c -' to cycle item bullet
  486. types, and properties can be edited by hand or in column view.
  487. However, when the cursor is on a timestamp, shift-cursor commands
  488. will still edit the time stamp - this is just too good to give up.
  489. XEmacs user should have this variable set to nil, because
  490. `shift-select-mode' is in Emacs 23 or later only."
  491. :group 'org
  492. :type '(choice
  493. (const :tag "Never" nil)
  494. (const :tag "When outside special context" t)
  495. (const :tag "Everywhere except timestamps" always)))
  496. (defcustom org-loop-over-headlines-in-active-region nil
  497. "Shall some commands act upon headlines in the active region?
  498. When set to `t', some commands will be performed in all headlines
  499. within the active region.
  500. When set to `start-level', some commands will be performed in all
  501. headlines within the active region, provided that these headlines
  502. are of the same level than the first one.
  503. When set to a string, those commands will be performed on the
  504. matching headlines within the active region. Such string must be
  505. a tags/property/todo match as it is used in the agenda tags view.
  506. The list of commands is: `org-schedule', `org-deadline',
  507. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  508. `org-archive-to-archive-sibling'. The archiving commands skip
  509. already archived entries."
  510. :type '(choice (const :tag "Don't loop" nil)
  511. (const :tag "All headlines in active region" t)
  512. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  513. (string :tag "Tags/Property/Todo matcher"))
  514. :version "24.1"
  515. :group 'org-todo
  516. :group 'org-archive)
  517. (defgroup org-startup nil
  518. "Options concerning startup of Org-mode."
  519. :tag "Org Startup"
  520. :group 'org)
  521. (defcustom org-startup-folded t
  522. "Non-nil means entering Org-mode will switch to OVERVIEW.
  523. This can also be configured on a per-file basis by adding one of
  524. the following lines anywhere in the buffer:
  525. #+STARTUP: fold (or `overview', this is equivalent)
  526. #+STARTUP: nofold (or `showall', this is equivalent)
  527. #+STARTUP: content
  528. #+STARTUP: showeverything
  529. By default, this option is ignored when Org opens agenda files
  530. for the first time. If you want the agenda to honor the startup
  531. option, set `org-agenda-inhibit-startup' to nil."
  532. :group 'org-startup
  533. :type '(choice
  534. (const :tag "nofold: show all" nil)
  535. (const :tag "fold: overview" t)
  536. (const :tag "content: all headlines" content)
  537. (const :tag "show everything, even drawers" showeverything)))
  538. (defcustom org-startup-truncated t
  539. "Non-nil means entering Org-mode will set `truncate-lines'.
  540. This is useful since some lines containing links can be very long and
  541. uninteresting. Also tables look terrible when wrapped."
  542. :group 'org-startup
  543. :type 'boolean)
  544. (defcustom org-startup-indented nil
  545. "Non-nil means turn on `org-indent-mode' on startup.
  546. This can also be configured on a per-file basis by adding one of
  547. the following lines anywhere in the buffer:
  548. #+STARTUP: indent
  549. #+STARTUP: noindent"
  550. :group 'org-structure
  551. :type '(choice
  552. (const :tag "Not" nil)
  553. (const :tag "Globally (slow on startup in large files)" t)))
  554. (defcustom org-use-sub-superscripts t
  555. "Non-nil means interpret \"_\" and \"^\" for display.
  556. When this option is turned on, you can use TeX-like syntax for sub- and
  557. superscripts. Several characters after \"_\" or \"^\" will be
  558. considered as a single item - so grouping with {} is normally not
  559. needed. For example, the following things will be parsed as single
  560. sub- or superscripts.
  561. 10^24 or 10^tau several digits will be considered 1 item.
  562. 10^-12 or 10^-tau a leading sign with digits or a word
  563. x^2-y^3 will be read as x^2 - y^3, because items are
  564. terminated by almost any nonword/nondigit char.
  565. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  566. Still, ambiguity is possible - so when in doubt use {} to enclose
  567. the sub/superscript. If you set this variable to the symbol
  568. `{}', the braces are *required* in order to trigger
  569. interpretations as sub/superscript. This can be helpful in
  570. documents that need \"_\" frequently in plain text."
  571. :group 'org-startup
  572. :version "24.1"
  573. :type '(choice
  574. (const :tag "Always interpret" t)
  575. (const :tag "Only with braces" {})
  576. (const :tag "Never interpret" nil)))
  577. (defcustom org-startup-with-beamer-mode nil
  578. "Non-nil means turn on `org-beamer-mode' on startup.
  579. This can also be configured on a per-file basis by adding one of
  580. the following lines anywhere in the buffer:
  581. #+STARTUP: beamer"
  582. :group 'org-startup
  583. :version "24.1"
  584. :type 'boolean)
  585. (defcustom org-startup-align-all-tables nil
  586. "Non-nil means align all tables when visiting a file.
  587. This is useful when the column width in tables is forced with <N> cookies
  588. in table fields. Such tables will look correct only after the first re-align.
  589. This can also be configured on a per-file basis by adding one of
  590. the following lines anywhere in the buffer:
  591. #+STARTUP: align
  592. #+STARTUP: noalign"
  593. :group 'org-startup
  594. :type 'boolean)
  595. (defcustom org-startup-with-inline-images nil
  596. "Non-nil means show inline images when loading a new Org file.
  597. This can also be configured on a per-file basis by adding one of
  598. the following lines anywhere in the buffer:
  599. #+STARTUP: inlineimages
  600. #+STARTUP: noinlineimages"
  601. :group 'org-startup
  602. :version "24.1"
  603. :type 'boolean)
  604. (defcustom org-startup-with-latex-preview nil
  605. "Non-nil means preview LaTeX fragments when loading a new Org file.
  606. This can also be configured on a per-file basis by adding one of
  607. the followinglines anywhere in the buffer:
  608. #+STARTUP: latexpreview
  609. #+STARTUP: nolatexpreview"
  610. :group 'org-startup
  611. :version "24.3"
  612. :type 'boolean)
  613. (defcustom org-insert-mode-line-in-empty-file nil
  614. "Non-nil means insert the first line setting Org-mode in empty files.
  615. When the function `org-mode' is called interactively in an empty file, this
  616. normally means that the file name does not automatically trigger Org-mode.
  617. To ensure that the file will always be in Org-mode in the future, a
  618. line enforcing Org-mode will be inserted into the buffer, if this option
  619. has been set."
  620. :group 'org-startup
  621. :type 'boolean)
  622. (defcustom org-replace-disputed-keys nil
  623. "Non-nil means use alternative key bindings for some keys.
  624. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  625. These keys are also used by other packages like shift-selection-mode'
  626. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  627. If you want to use Org-mode together with one of these other modes,
  628. or more generally if you would like to move some Org-mode commands to
  629. other keys, set this variable and configure the keys with the variable
  630. `org-disputed-keys'.
  631. This option is only relevant at load-time of Org-mode, and must be set
  632. *before* org.el is loaded. Changing it requires a restart of Emacs to
  633. become effective."
  634. :group 'org-startup
  635. :type 'boolean)
  636. (defcustom org-use-extra-keys nil
  637. "Non-nil means use extra key sequence definitions for certain commands.
  638. This happens automatically if you run XEmacs or if `window-system'
  639. is nil. This variable lets you do the same manually. You must
  640. set it before loading org.
  641. Example: on Carbon Emacs 22 running graphically, with an external
  642. keyboard on a Powerbook, the default way of setting M-left might
  643. not work for either Alt or ESC. Setting this variable will make
  644. it work for ESC."
  645. :group 'org-startup
  646. :type 'boolean)
  647. (if (fboundp 'defvaralias)
  648. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  649. (defcustom org-disputed-keys
  650. '(([(shift up)] . [(meta p)])
  651. ([(shift down)] . [(meta n)])
  652. ([(shift left)] . [(meta -)])
  653. ([(shift right)] . [(meta +)])
  654. ([(control shift right)] . [(meta shift +)])
  655. ([(control shift left)] . [(meta shift -)]))
  656. "Keys for which Org-mode and other modes compete.
  657. This is an alist, cars are the default keys, second element specifies
  658. the alternative to use when `org-replace-disputed-keys' is t.
  659. Keys can be specified in any syntax supported by `define-key'.
  660. The value of this option takes effect only at Org-mode's startup,
  661. therefore you'll have to restart Emacs to apply it after changing."
  662. :group 'org-startup
  663. :type 'alist)
  664. (defun org-key (key)
  665. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  666. Or return the original if not disputed.
  667. Also apply the translations defined in `org-xemacs-key-equivalents'."
  668. (when org-replace-disputed-keys
  669. (let* ((nkey (key-description key))
  670. (x (org-find-if (lambda (x)
  671. (equal (key-description (car x)) nkey))
  672. org-disputed-keys)))
  673. (setq key (if x (cdr x) key))))
  674. (when (featurep 'xemacs)
  675. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  676. key)
  677. (defun org-find-if (predicate seq)
  678. (catch 'exit
  679. (while seq
  680. (if (funcall predicate (car seq))
  681. (throw 'exit (car seq))
  682. (pop seq)))))
  683. (defun org-defkey (keymap key def)
  684. "Define a key, possibly translated, as returned by `org-key'."
  685. (define-key keymap (org-key key) def))
  686. (defcustom org-ellipsis nil
  687. "The ellipsis to use in the Org-mode outline.
  688. When nil, just use the standard three dots. When a string, use that instead,
  689. When a face, use the standard 3 dots, but with the specified face.
  690. The change affects only Org-mode (which will then use its own display table).
  691. Changing this requires executing `M-x org-mode' in a buffer to become
  692. effective."
  693. :group 'org-startup
  694. :type '(choice (const :tag "Default" nil)
  695. (face :tag "Face" :value org-warning)
  696. (string :tag "String" :value "...#")))
  697. (defvar org-display-table nil
  698. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  699. (defgroup org-keywords nil
  700. "Keywords in Org-mode."
  701. :tag "Org Keywords"
  702. :group 'org)
  703. (defcustom org-deadline-string "DEADLINE:"
  704. "String to mark deadline entries.
  705. A deadline is this string, followed by a time stamp. Should be a word,
  706. terminated by a colon. You can insert a schedule keyword and
  707. a timestamp with \\[org-deadline].
  708. Changes become only effective after restarting Emacs."
  709. :group 'org-keywords
  710. :type 'string)
  711. (defcustom org-scheduled-string "SCHEDULED:"
  712. "String to mark scheduled TODO entries.
  713. A schedule is this string, followed by a time stamp. Should be a word,
  714. terminated by a colon. You can insert a schedule keyword and
  715. a timestamp with \\[org-schedule].
  716. Changes become only effective after restarting Emacs."
  717. :group 'org-keywords
  718. :type 'string)
  719. (defcustom org-closed-string "CLOSED:"
  720. "String used as the prefix for timestamps logging closing a TODO entry."
  721. :group 'org-keywords
  722. :type 'string)
  723. (defcustom org-clock-string "CLOCK:"
  724. "String used as prefix for timestamps clocking work hours on an item."
  725. :group 'org-keywords
  726. :type 'string)
  727. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  728. org-scheduled-string "\\|"
  729. org-deadline-string "\\|"
  730. org-closed-string "\\|"
  731. org-clock-string "\\)")
  732. "Matches a line with planning or clock info.")
  733. (defcustom org-comment-string "COMMENT"
  734. "Entries starting with this keyword will never be exported.
  735. An entry can be toggled between COMMENT and normal with
  736. \\[org-toggle-comment].
  737. Changes become only effective after restarting Emacs."
  738. :group 'org-keywords
  739. :type 'string)
  740. (defcustom org-quote-string "QUOTE"
  741. "Entries starting with this keyword will be exported in fixed-width font.
  742. Quoting applies only to the text in the entry following the headline, and does
  743. not extend beyond the next headline, even if that is lower level.
  744. An entry can be toggled between QUOTE and normal with
  745. \\[org-toggle-fixed-width-section]."
  746. :group 'org-keywords
  747. :type 'string)
  748. (defconst org-repeat-re
  749. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  750. "Regular expression for specifying repeated events.
  751. After a match, group 1 contains the repeat expression.")
  752. (defgroup org-structure nil
  753. "Options concerning the general structure of Org-mode files."
  754. :tag "Org Structure"
  755. :group 'org)
  756. (defgroup org-reveal-location nil
  757. "Options about how to make context of a location visible."
  758. :tag "Org Reveal Location"
  759. :group 'org-structure)
  760. (defconst org-context-choice
  761. '(choice
  762. (const :tag "Always" t)
  763. (const :tag "Never" nil)
  764. (repeat :greedy t :tag "Individual contexts"
  765. (cons
  766. (choice :tag "Context"
  767. (const agenda)
  768. (const org-goto)
  769. (const occur-tree)
  770. (const tags-tree)
  771. (const link-search)
  772. (const mark-goto)
  773. (const bookmark-jump)
  774. (const isearch)
  775. (const default))
  776. (boolean))))
  777. "Contexts for the reveal options.")
  778. (defcustom org-show-hierarchy-above '((default . t))
  779. "Non-nil means show full hierarchy when revealing a location.
  780. Org-mode often shows locations in an org-mode file which might have
  781. been invisible before. When this is set, the hierarchy of headings
  782. above the exposed location is shown.
  783. Turning this off for example for sparse trees makes them very compact.
  784. Instead of t, this can also be an alist specifying this option for different
  785. contexts. Valid contexts are
  786. agenda when exposing an entry from the agenda
  787. org-goto when using the command `org-goto' on key C-c C-j
  788. occur-tree when using the command `org-occur' on key C-c /
  789. tags-tree when constructing a sparse tree based on tags matches
  790. link-search when exposing search matches associated with a link
  791. mark-goto when exposing the jump goal of a mark
  792. bookmark-jump when exposing a bookmark location
  793. isearch when exiting from an incremental search
  794. default default for all contexts not set explicitly"
  795. :group 'org-reveal-location
  796. :type org-context-choice)
  797. (defcustom org-show-following-heading '((default . nil))
  798. "Non-nil means show following heading when revealing a location.
  799. Org-mode often shows locations in an org-mode file which might have
  800. been invisible before. When this is set, the heading following the
  801. match is shown.
  802. Turning this off for example for sparse trees makes them very compact,
  803. but makes it harder to edit the location of the match. In such a case,
  804. use the command \\[org-reveal] to show more context.
  805. Instead of t, this can also be an alist specifying this option for different
  806. contexts. See `org-show-hierarchy-above' for valid contexts."
  807. :group 'org-reveal-location
  808. :type org-context-choice)
  809. (defcustom org-show-siblings '((default . nil) (isearch t))
  810. "Non-nil means show all sibling heading when revealing a location.
  811. Org-mode often shows locations in an org-mode file which might have
  812. been invisible before. When this is set, the sibling of the current entry
  813. heading are all made visible. If `org-show-hierarchy-above' is t,
  814. the same happens on each level of the hierarchy above the current entry.
  815. By default this is on for the isearch context, off for all other contexts.
  816. Turning this off for example for sparse trees makes them very compact,
  817. but makes it harder to edit the location of the match. In such a case,
  818. use the command \\[org-reveal] to show more context.
  819. Instead of t, this can also be an alist specifying this option for different
  820. contexts. See `org-show-hierarchy-above' for valid contexts."
  821. :group 'org-reveal-location
  822. :type org-context-choice)
  823. (defcustom org-show-entry-below '((default . nil))
  824. "Non-nil means show the entry below a headline when revealing a location.
  825. Org-mode often shows locations in an org-mode file which might have
  826. been invisible before. When this is set, the text below the headline that is
  827. exposed is also shown.
  828. By default this is off for all contexts.
  829. Instead of t, this can also be an alist specifying this option for different
  830. contexts. See `org-show-hierarchy-above' for valid contexts."
  831. :group 'org-reveal-location
  832. :type org-context-choice)
  833. (defcustom org-indirect-buffer-display 'other-window
  834. "How should indirect tree buffers be displayed?
  835. This applies to indirect buffers created with the commands
  836. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  837. Valid values are:
  838. current-window Display in the current window
  839. other-window Just display in another window.
  840. dedicated-frame Create one new frame, and re-use it each time.
  841. new-frame Make a new frame each time. Note that in this case
  842. previously-made indirect buffers are kept, and you need to
  843. kill these buffers yourself."
  844. :group 'org-structure
  845. :group 'org-agenda-windows
  846. :type '(choice
  847. (const :tag "In current window" current-window)
  848. (const :tag "In current frame, other window" other-window)
  849. (const :tag "Each time a new frame" new-frame)
  850. (const :tag "One dedicated frame" dedicated-frame)))
  851. (defcustom org-use-speed-commands nil
  852. "Non-nil means activate single letter commands at beginning of a headline.
  853. This may also be a function to test for appropriate locations where speed
  854. commands should be active."
  855. :group 'org-structure
  856. :type '(choice
  857. (const :tag "Never" nil)
  858. (const :tag "At beginning of headline stars" t)
  859. (function)))
  860. (defcustom org-speed-commands-user nil
  861. "Alist of additional speed commands.
  862. This list will be checked before `org-speed-commands-default'
  863. when the variable `org-use-speed-commands' is non-nil
  864. and when the cursor is at the beginning of a headline.
  865. The car if each entry is a string with a single letter, which must
  866. be assigned to `self-insert-command' in the global map.
  867. The cdr is either a command to be called interactively, a function
  868. to be called, or a form to be evaluated.
  869. An entry that is just a list with a single string will be interpreted
  870. as a descriptive headline that will be added when listing the speed
  871. commands in the Help buffer using the `?' speed command."
  872. :group 'org-structure
  873. :type '(repeat :value ("k" . ignore)
  874. (choice :value ("k" . ignore)
  875. (list :tag "Descriptive Headline" (string :tag "Headline"))
  876. (cons :tag "Letter and Command"
  877. (string :tag "Command letter")
  878. (choice
  879. (function)
  880. (sexp))))))
  881. (defgroup org-cycle nil
  882. "Options concerning visibility cycling in Org-mode."
  883. :tag "Org Cycle"
  884. :group 'org-structure)
  885. (defcustom org-cycle-skip-children-state-if-no-children t
  886. "Non-nil means skip CHILDREN state in entries that don't have any."
  887. :group 'org-cycle
  888. :type 'boolean)
  889. (defcustom org-cycle-max-level nil
  890. "Maximum level which should still be subject to visibility cycling.
  891. Levels higher than this will, for cycling, be treated as text, not a headline.
  892. When `org-odd-levels-only' is set, a value of N in this variable actually
  893. means 2N-1 stars as the limiting headline.
  894. When nil, cycle all levels.
  895. Note that the limiting level of cycling is also influenced by
  896. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  897. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  898. than its value."
  899. :group 'org-cycle
  900. :type '(choice
  901. (const :tag "No limit" nil)
  902. (integer :tag "Maximum level")))
  903. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  904. "Names of drawers. Drawers are not opened by cycling on the headline above.
  905. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  906. this:
  907. :DRAWERNAME:
  908. .....
  909. :END:
  910. The drawer \"PROPERTIES\" is special for capturing properties through
  911. the property API.
  912. Drawers can be defined on the per-file basis with a line like:
  913. #+DRAWERS: HIDDEN STATE PROPERTIES"
  914. :group 'org-structure
  915. :group 'org-cycle
  916. :type '(repeat (string :tag "Drawer Name")))
  917. (defcustom org-hide-block-startup nil
  918. "Non-nil means entering Org-mode will fold all blocks.
  919. This can also be set in on a per-file basis with
  920. #+STARTUP: hideblocks
  921. #+STARTUP: showblocks"
  922. :group 'org-startup
  923. :group 'org-cycle
  924. :type 'boolean)
  925. (defcustom org-cycle-global-at-bob nil
  926. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  927. This makes it possible to do global cycling without having to use S-TAB or
  928. \\[universal-argument] TAB. For this special case to work, the first line
  929. of the buffer must not be a headline -- it may be empty or some other text.
  930. When used in this way, `org-cycle-hook' is disabled temporarily to make
  931. sure the cursor stays at the beginning of the buffer. When this option is
  932. nil, don't do anything special at the beginning of the buffer."
  933. :group 'org-cycle
  934. :type 'boolean)
  935. (defcustom org-cycle-level-after-item/entry-creation t
  936. "Non-nil means cycle entry level or item indentation in new empty entries.
  937. When the cursor is at the end of an empty headline, i.e., with only stars
  938. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  939. and then all possible ancestor states, before returning to the original state.
  940. This makes data entry extremely fast: M-RET to create a new headline,
  941. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  942. When the cursor is at the end of an empty plain list item, one TAB will
  943. make it a subitem, two or more tabs will back up to make this an item
  944. higher up in the item hierarchy."
  945. :group 'org-cycle
  946. :type 'boolean)
  947. (defcustom org-cycle-emulate-tab t
  948. "Where should `org-cycle' emulate TAB.
  949. nil Never
  950. white Only in completely white lines
  951. whitestart Only at the beginning of lines, before the first non-white char
  952. t Everywhere except in headlines
  953. exc-hl-bol Everywhere except at the start of a headline
  954. If TAB is used in a place where it does not emulate TAB, the current subtree
  955. visibility is cycled."
  956. :group 'org-cycle
  957. :type '(choice (const :tag "Never" nil)
  958. (const :tag "Only in completely white lines" white)
  959. (const :tag "Before first char in a line" whitestart)
  960. (const :tag "Everywhere except in headlines" t)
  961. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  962. ))
  963. (defcustom org-cycle-separator-lines 2
  964. "Number of empty lines needed to keep an empty line between collapsed trees.
  965. If you leave an empty line between the end of a subtree and the following
  966. headline, this empty line is hidden when the subtree is folded.
  967. Org-mode will leave (exactly) one empty line visible if the number of
  968. empty lines is equal or larger to the number given in this variable.
  969. So the default 2 means at least 2 empty lines after the end of a subtree
  970. are needed to produce free space between a collapsed subtree and the
  971. following headline.
  972. If the number is negative, and the number of empty lines is at least -N,
  973. all empty lines are shown.
  974. Special case: when 0, never leave empty lines in collapsed view."
  975. :group 'org-cycle
  976. :type 'integer)
  977. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  978. (defcustom org-pre-cycle-hook nil
  979. "Hook that is run before visibility cycling is happening.
  980. The function(s) in this hook must accept a single argument which indicates
  981. the new state that will be set right after running this hook. The
  982. argument is a symbol. Before a global state change, it can have the values
  983. `overview', `content', or `all'. Before a local state change, it can have
  984. the values `folded', `children', or `subtree'."
  985. :group 'org-cycle
  986. :type 'hook)
  987. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  988. org-cycle-hide-drawers
  989. org-cycle-hide-inline-tasks
  990. org-cycle-show-empty-lines
  991. org-optimize-window-after-visibility-change)
  992. "Hook that is run after `org-cycle' has changed the buffer visibility.
  993. The function(s) in this hook must accept a single argument which indicates
  994. the new state that was set by the most recent `org-cycle' command. The
  995. argument is a symbol. After a global state change, it can have the values
  996. `overview', `contents', or `all'. After a local state change, it can have
  997. the values `folded', `children', or `subtree'."
  998. :group 'org-cycle
  999. :type 'hook)
  1000. (defgroup org-edit-structure nil
  1001. "Options concerning structure editing in Org-mode."
  1002. :tag "Org Edit Structure"
  1003. :group 'org-structure)
  1004. (defcustom org-odd-levels-only nil
  1005. "Non-nil means skip even levels and only use odd levels for the outline.
  1006. This has the effect that two stars are being added/taken away in
  1007. promotion/demotion commands. It also influences how levels are
  1008. handled by the exporters.
  1009. Changing it requires restart of `font-lock-mode' to become effective
  1010. for fontification also in regions already fontified.
  1011. You may also set this on a per-file basis by adding one of the following
  1012. lines to the buffer:
  1013. #+STARTUP: odd
  1014. #+STARTUP: oddeven"
  1015. :group 'org-edit-structure
  1016. :group 'org-appearance
  1017. :type 'boolean)
  1018. (defcustom org-adapt-indentation t
  1019. "Non-nil means adapt indentation to outline node level.
  1020. When this variable is set, Org assumes that you write outlines by
  1021. indenting text in each node to align with the headline (after the stars).
  1022. The following issues are influenced by this variable:
  1023. - When this is set and the *entire* text in an entry is indented, the
  1024. indentation is increased by one space in a demotion command, and
  1025. decreased by one in a promotion command. If any line in the entry
  1026. body starts with text at column 0, indentation is not changed at all.
  1027. - Property drawers and planning information is inserted indented when
  1028. this variable s set. When nil, they will not be indented.
  1029. - TAB indents a line relative to context. The lines below a headline
  1030. will be indented when this variable is set.
  1031. Note that this is all about true indentation, by adding and removing
  1032. space characters. See also `org-indent.el' which does level-dependent
  1033. indentation in a virtual way, i.e. at display time in Emacs."
  1034. :group 'org-edit-structure
  1035. :type 'boolean)
  1036. (defcustom org-special-ctrl-a/e nil
  1037. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1038. When t, `C-a' will bring back the cursor to the beginning of the
  1039. headline text, i.e. after the stars and after a possible TODO
  1040. keyword. In an item, this will be the position after bullet and
  1041. check-box, if any. When the cursor is already at that position,
  1042. another `C-a' will bring it to the beginning of the line.
  1043. `C-e' will jump to the end of the headline, ignoring the presence
  1044. of tags in the headline. A second `C-e' will then jump to the
  1045. true end of the line, after any tags. This also means that, when
  1046. this variable is non-nil, `C-e' also will never jump beyond the
  1047. end of the heading of a folded section, i.e. not after the
  1048. ellipses.
  1049. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1050. normally, going to the true line boundary first. Only a directly
  1051. following, identical keypress will bring the cursor to the
  1052. special positions.
  1053. This may also be a cons cell where the behavior for `C-a' and
  1054. `C-e' is set separately."
  1055. :group 'org-edit-structure
  1056. :type '(choice
  1057. (const :tag "off" nil)
  1058. (const :tag "on: after stars/bullet and before tags first" t)
  1059. (const :tag "reversed: true line boundary first" reversed)
  1060. (cons :tag "Set C-a and C-e separately"
  1061. (choice :tag "Special C-a"
  1062. (const :tag "off" nil)
  1063. (const :tag "on: after stars/bullet first" t)
  1064. (const :tag "reversed: before stars/bullet first" reversed))
  1065. (choice :tag "Special C-e"
  1066. (const :tag "off" nil)
  1067. (const :tag "on: before tags first" t)
  1068. (const :tag "reversed: after tags first" reversed)))))
  1069. (if (fboundp 'defvaralias)
  1070. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  1071. (defcustom org-special-ctrl-k nil
  1072. "Non-nil means `C-k' will behave specially in headlines.
  1073. When nil, `C-k' will call the default `kill-line' command.
  1074. When t, the following will happen while the cursor is in the headline:
  1075. - When the cursor is at the beginning of a headline, kill the entire
  1076. line and possible the folded subtree below the line.
  1077. - When in the middle of the headline text, kill the headline up to the tags.
  1078. - When after the headline text, kill the tags."
  1079. :group 'org-edit-structure
  1080. :type 'boolean)
  1081. (defcustom org-ctrl-k-protect-subtree nil
  1082. "Non-nil means, do not delete a hidden subtree with C-k.
  1083. When set to the symbol `error', simply throw an error when C-k is
  1084. used to kill (part-of) a headline that has hidden text behind it.
  1085. Any other non-nil value will result in a query to the user, if it is
  1086. OK to kill that hidden subtree. When nil, kill without remorse."
  1087. :group 'org-edit-structure
  1088. :version "24.1"
  1089. :type '(choice
  1090. (const :tag "Do not protect hidden subtrees" nil)
  1091. (const :tag "Protect hidden subtrees with a security query" t)
  1092. (const :tag "Never kill a hidden subtree with C-k" error)))
  1093. (defcustom org-catch-invisible-edits nil
  1094. "Check if in invisible region before inserting or deleting a character.
  1095. Valid values are:
  1096. nil Do not check, so just do invisible edits.
  1097. error Throw an error and do nothing.
  1098. show Make point visible, and do the requested edit.
  1099. show-and-error Make point visible, then throw an error and abort the edit.
  1100. smart Make point visible, and do insertion/deletion if it is
  1101. adjacent to visible text and the change feels predictable.
  1102. Never delete a previously invisible character or add in the
  1103. middle or right after an invisible region. Basically, this
  1104. allows insertion and backward-delete right before ellipses.
  1105. FIXME: maybe in this case we should not even show?"
  1106. :group 'org-edit-structure
  1107. :version "24.1"
  1108. :type '(choice
  1109. (const :tag "Do not check" nil)
  1110. (const :tag "Throw error when trying to edit" error)
  1111. (const :tag "Unhide, but do not do the edit" show-and-error)
  1112. (const :tag "Show invisible part and do the edit" show)
  1113. (const :tag "Be smart and do the right thing" smart)))
  1114. (defcustom org-yank-folded-subtrees t
  1115. "Non-nil means when yanking subtrees, fold them.
  1116. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1117. it starts with a heading and all other headings in it are either children
  1118. or siblings, then fold all the subtrees. However, do this only if no
  1119. text after the yank would be swallowed into a folded tree by this action."
  1120. :group 'org-edit-structure
  1121. :type 'boolean)
  1122. (defcustom org-yank-adjusted-subtrees nil
  1123. "Non-nil means when yanking subtrees, adjust the level.
  1124. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1125. this function for details."
  1126. :group 'org-edit-structure
  1127. :type 'boolean)
  1128. (defcustom org-M-RET-may-split-line '((default . t))
  1129. "Non-nil means M-RET will split the line at the cursor position.
  1130. When nil, it will go to the end of the line before making a
  1131. new line.
  1132. You may also set this option in a different way for different
  1133. contexts. Valid contexts are:
  1134. headline when creating a new headline
  1135. item when creating a new item
  1136. table in a table field
  1137. default the value to be used for all contexts not explicitly
  1138. customized"
  1139. :group 'org-structure
  1140. :group 'org-table
  1141. :type '(choice
  1142. (const :tag "Always" t)
  1143. (const :tag "Never" nil)
  1144. (repeat :greedy t :tag "Individual contexts"
  1145. (cons
  1146. (choice :tag "Context"
  1147. (const headline)
  1148. (const item)
  1149. (const table)
  1150. (const default))
  1151. (boolean)))))
  1152. (defcustom org-insert-heading-respect-content nil
  1153. "Non-nil means insert new headings after the current subtree.
  1154. When nil, the new heading is created directly after the current line.
  1155. The commands \\[org-insert-heading-respect-content] and
  1156. \\[org-insert-todo-heading-respect-content] turn this variable on
  1157. for the duration of the command."
  1158. :group 'org-structure
  1159. :type 'boolean)
  1160. (defcustom org-blank-before-new-entry '((heading . auto)
  1161. (plain-list-item . auto))
  1162. "Should `org-insert-heading' leave a blank line before new heading/item?
  1163. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1164. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1165. which case Org will look at the surrounding headings/items and try to
  1166. make an intelligent decision whether to insert a blank line or not.
  1167. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1168. set, the setting here is ignored and no empty line is inserted, to avoid
  1169. breaking the list structure."
  1170. :group 'org-edit-structure
  1171. :type '(list
  1172. (cons (const heading)
  1173. (choice (const :tag "Never" nil)
  1174. (const :tag "Always" t)
  1175. (const :tag "Auto" auto)))
  1176. (cons (const plain-list-item)
  1177. (choice (const :tag "Never" nil)
  1178. (const :tag "Always" t)
  1179. (const :tag "Auto" auto)))))
  1180. (defcustom org-insert-heading-hook nil
  1181. "Hook being run after inserting a new heading."
  1182. :group 'org-edit-structure
  1183. :type 'hook)
  1184. (defcustom org-enable-fixed-width-editor t
  1185. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1186. This currently only means they are never auto-wrapped.
  1187. When nil, such lines will be treated like ordinary lines.
  1188. See also the QUOTE keyword."
  1189. :group 'org-edit-structure
  1190. :type 'boolean)
  1191. (defcustom org-goto-auto-isearch t
  1192. "Non-nil means typing characters in `org-goto' starts incremental search.
  1193. When nil, you can use these keybindings to navigate the buffer:
  1194. q Quit the org-goto interface
  1195. n Go to the next visible heading
  1196. p Go to the previous visible heading
  1197. f Go one heading forward on same level
  1198. b Go one heading backward on same level
  1199. u Go one heading up"
  1200. :group 'org-edit-structure
  1201. :type 'boolean)
  1202. (defgroup org-sparse-trees nil
  1203. "Options concerning sparse trees in Org-mode."
  1204. :tag "Org Sparse Trees"
  1205. :group 'org-structure)
  1206. (defcustom org-highlight-sparse-tree-matches t
  1207. "Non-nil means highlight all matches that define a sparse tree.
  1208. The highlights will automatically disappear the next time the buffer is
  1209. changed by an edit command."
  1210. :group 'org-sparse-trees
  1211. :type 'boolean)
  1212. (defcustom org-remove-highlights-with-change t
  1213. "Non-nil means any change to the buffer will remove temporary highlights.
  1214. Such highlights are created by `org-occur' and `org-clock-display'.
  1215. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1216. The highlights created by `org-preview-latex-fragment' always need
  1217. `C-c C-c' to be removed."
  1218. :group 'org-sparse-trees
  1219. :group 'org-time
  1220. :type 'boolean)
  1221. (defcustom org-occur-hook '(org-first-headline-recenter)
  1222. "Hook that is run after `org-occur' has constructed a sparse tree.
  1223. This can be used to recenter the window to show as much of the structure
  1224. as possible."
  1225. :group 'org-sparse-trees
  1226. :type 'hook)
  1227. (defgroup org-imenu-and-speedbar nil
  1228. "Options concerning imenu and speedbar in Org-mode."
  1229. :tag "Org Imenu and Speedbar"
  1230. :group 'org-structure)
  1231. (defcustom org-imenu-depth 2
  1232. "The maximum level for Imenu access to Org-mode headlines.
  1233. This also applied for speedbar access."
  1234. :group 'org-imenu-and-speedbar
  1235. :type 'integer)
  1236. (defgroup org-table nil
  1237. "Options concerning tables in Org-mode."
  1238. :tag "Org Table"
  1239. :group 'org)
  1240. (defcustom org-enable-table-editor 'optimized
  1241. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1242. When nil, such lines will be treated like ordinary lines.
  1243. When equal to the symbol `optimized', the table editor will be optimized to
  1244. do the following:
  1245. - Automatic overwrite mode in front of whitespace in table fields.
  1246. This makes the structure of the table stay in tact as long as the edited
  1247. field does not exceed the column width.
  1248. - Minimize the number of realigns. Normally, the table is aligned each time
  1249. TAB or RET are pressed to move to another field. With optimization this
  1250. happens only if changes to a field might have changed the column width.
  1251. Optimization requires replacing the functions `self-insert-command',
  1252. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1253. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1254. very good at guessing when a re-align will be necessary, but you can always
  1255. force one with \\[org-ctrl-c-ctrl-c].
  1256. If you would like to use the optimized version in Org-mode, but the
  1257. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1258. This variable can be used to turn on and off the table editor during a session,
  1259. but in order to toggle optimization, a restart is required.
  1260. See also the variable `org-table-auto-blank-field'."
  1261. :group 'org-table
  1262. :type '(choice
  1263. (const :tag "off" nil)
  1264. (const :tag "on" t)
  1265. (const :tag "on, optimized" optimized)))
  1266. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1267. (version<= emacs-version "24.1"))
  1268. "Non-nil means cluster self-insert commands for undo when possible.
  1269. If this is set, then, like in the Emacs command loop, 20 consecutive
  1270. characters will be undone together.
  1271. This is configurable, because there is some impact on typing performance."
  1272. :group 'org-table
  1273. :type 'boolean)
  1274. (defcustom org-table-tab-recognizes-table.el t
  1275. "Non-nil means TAB will automatically notice a table.el table.
  1276. When it sees such a table, it moves point into it and - if necessary -
  1277. calls `table-recognize-table'."
  1278. :group 'org-table-editing
  1279. :type 'boolean)
  1280. (defgroup org-link nil
  1281. "Options concerning links in Org-mode."
  1282. :tag "Org Link"
  1283. :group 'org)
  1284. (defvar org-link-abbrev-alist-local nil
  1285. "Buffer-local version of `org-link-abbrev-alist', which see.
  1286. The value of this is taken from the #+LINK lines.")
  1287. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1288. (defcustom org-link-abbrev-alist nil
  1289. "Alist of link abbreviations.
  1290. The car of each element is a string, to be replaced at the start of a link.
  1291. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1292. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1293. [[linkkey:tag][description]]
  1294. The 'linkkey' must be a word word, starting with a letter, followed
  1295. by letters, numbers, '-' or '_'.
  1296. If REPLACE is a string, the tag will simply be appended to create the link.
  1297. If the string contains \"%s\", the tag will be inserted there. If the string
  1298. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1299. at that point (see the function `url-hexify-string'). If the string contains
  1300. the specifier \"%(my-function)\", then the custom function `my-function' will
  1301. be invoked: this function takes the tag as its only argument and must return
  1302. a string.
  1303. REPLACE may also be a function that will be called with the tag as the
  1304. only argument to create the link, which should be returned as a string.
  1305. See the manual for examples."
  1306. :group 'org-link
  1307. :type '(repeat
  1308. (cons
  1309. (string :tag "Protocol")
  1310. (choice
  1311. (string :tag "Format")
  1312. (function)))))
  1313. (defcustom org-descriptive-links t
  1314. "Non-nil means Org will display descriptive links.
  1315. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1316. \"Org Website\", hiding the link itself and just displaying its
  1317. description. When set to `nil', Org will display the full links
  1318. literally.
  1319. You can interactively set the value of this variable by calling
  1320. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1321. :group 'org-link
  1322. :type 'boolean)
  1323. (defcustom org-link-file-path-type 'adaptive
  1324. "How the path name in file links should be stored.
  1325. Valid values are:
  1326. relative Relative to the current directory, i.e. the directory of the file
  1327. into which the link is being inserted.
  1328. absolute Absolute path, if possible with ~ for home directory.
  1329. noabbrev Absolute path, no abbreviation of home directory.
  1330. adaptive Use relative path for files in the current directory and sub-
  1331. directories of it. For other files, use an absolute path."
  1332. :group 'org-link
  1333. :type '(choice
  1334. (const relative)
  1335. (const absolute)
  1336. (const noabbrev)
  1337. (const adaptive)))
  1338. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1339. "Types of links that should be activated in Org-mode files.
  1340. This is a list of symbols, each leading to the activation of a certain link
  1341. type. In principle, it does not hurt to turn on most link types - there may
  1342. be a small gain when turning off unused link types. The types are:
  1343. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1344. angle Links in angular brackets that may contain whitespace like
  1345. <bbdb:Carsten Dominik>.
  1346. plain Plain links in normal text, no whitespace, like http://google.com.
  1347. radio Text that is matched by a radio target, see manual for details.
  1348. tag Tag settings in a headline (link to tag search).
  1349. date Time stamps (link to calendar).
  1350. footnote Footnote labels.
  1351. Changing this variable requires a restart of Emacs to become effective."
  1352. :group 'org-link
  1353. :type '(set :greedy t
  1354. (const :tag "Double bracket links" bracket)
  1355. (const :tag "Angular bracket links" angle)
  1356. (const :tag "Plain text links" plain)
  1357. (const :tag "Radio target matches" radio)
  1358. (const :tag "Tags" tag)
  1359. (const :tag "Timestamps" date)
  1360. (const :tag "Footnotes" footnote)))
  1361. (defcustom org-make-link-description-function nil
  1362. "Function to use for generating link descriptions from links.
  1363. When nil, the link location will be used. This function must take
  1364. two parameters: the first one is the link, the second one is the
  1365. description generated by `org-insert-link'. The function should
  1366. return the description to use."
  1367. :group 'org-link
  1368. :type 'function)
  1369. (defgroup org-link-store nil
  1370. "Options concerning storing links in Org-mode."
  1371. :tag "Org Store Link"
  1372. :group 'org-link)
  1373. (defcustom org-url-hexify-p t
  1374. "When non-nil, hexify URL when creating a link."
  1375. :type 'boolean
  1376. :version "24.3"
  1377. :group 'org-link-store)
  1378. (defcustom org-email-link-description-format "Email %c: %.30s"
  1379. "Format of the description part of a link to an email or usenet message.
  1380. The following %-escapes will be replaced by corresponding information:
  1381. %F full \"From\" field
  1382. %f name, taken from \"From\" field, address if no name
  1383. %T full \"To\" field
  1384. %t first name in \"To\" field, address if no name
  1385. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1386. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1387. %s subject
  1388. %d date
  1389. %m message-id.
  1390. You may use normal field width specification between the % and the letter.
  1391. This is for example useful to limit the length of the subject.
  1392. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1393. :group 'org-link-store
  1394. :type 'string)
  1395. (defcustom org-from-is-user-regexp
  1396. (let (r1 r2)
  1397. (when (and user-mail-address (not (string= user-mail-address "")))
  1398. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1399. (when (and user-full-name (not (string= user-full-name "")))
  1400. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1401. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1402. "Regexp matched against the \"From:\" header of an email or usenet message.
  1403. It should match if the message is from the user him/herself."
  1404. :group 'org-link-store
  1405. :type 'regexp)
  1406. (defcustom org-context-in-file-links t
  1407. "Non-nil means file links from `org-store-link' contain context.
  1408. A search string will be added to the file name with :: as separator and
  1409. used to find the context when the link is activated by the command
  1410. `org-open-at-point'. When this option is t, the entire active region
  1411. will be placed in the search string of the file link. If set to a
  1412. positive integer, only the first n lines of context will be stored.
  1413. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1414. negates this setting for the duration of the command."
  1415. :group 'org-link-store
  1416. :type '(choice boolean integer))
  1417. (defcustom org-keep-stored-link-after-insertion nil
  1418. "Non-nil means keep link in list for entire session.
  1419. The command `org-store-link' adds a link pointing to the current
  1420. location to an internal list. These links accumulate during a session.
  1421. The command `org-insert-link' can be used to insert links into any
  1422. Org-mode file (offering completion for all stored links). When this
  1423. option is nil, every link which has been inserted once using \\[org-insert-link]
  1424. will be removed from the list, to make completing the unused links
  1425. more efficient."
  1426. :group 'org-link-store
  1427. :type 'boolean)
  1428. (defgroup org-link-follow nil
  1429. "Options concerning following links in Org-mode."
  1430. :tag "Org Follow Link"
  1431. :group 'org-link)
  1432. (defcustom org-link-translation-function nil
  1433. "Function to translate links with different syntax to Org syntax.
  1434. This can be used to translate links created for example by the Planner
  1435. or emacs-wiki packages to Org syntax.
  1436. The function must accept two parameters, a TYPE containing the link
  1437. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1438. which is everything after the link protocol. It should return a cons
  1439. with possibly modified values of type and path.
  1440. Org contains a function for this, so if you set this variable to
  1441. `org-translate-link-from-planner', you should be able follow many
  1442. links created by planner."
  1443. :group 'org-link-follow
  1444. :type 'function)
  1445. (defcustom org-follow-link-hook nil
  1446. "Hook that is run after a link has been followed."
  1447. :group 'org-link-follow
  1448. :type 'hook)
  1449. (defcustom org-tab-follows-link nil
  1450. "Non-nil means on links TAB will follow the link.
  1451. Needs to be set before org.el is loaded.
  1452. This really should not be used, it does not make sense, and the
  1453. implementation is bad."
  1454. :group 'org-link-follow
  1455. :type 'boolean)
  1456. (defcustom org-return-follows-link nil
  1457. "Non-nil means on links RET will follow the link.
  1458. In tables, the special behavior of RET has precedence."
  1459. :group 'org-link-follow
  1460. :type 'boolean)
  1461. (defcustom org-mouse-1-follows-link
  1462. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1463. "Non-nil means mouse-1 on a link will follow the link.
  1464. A longer mouse click will still set point. Does not work on XEmacs.
  1465. Needs to be set before org.el is loaded."
  1466. :group 'org-link-follow
  1467. :type 'boolean)
  1468. (defcustom org-mark-ring-length 4
  1469. "Number of different positions to be recorded in the ring.
  1470. Changing this requires a restart of Emacs to work correctly."
  1471. :group 'org-link-follow
  1472. :type 'integer)
  1473. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1474. "Non-nil means internal links in Org files must exactly match a headline.
  1475. When nil, the link search tries to match a phrase with all words
  1476. in the search text."
  1477. :group 'org-link-follow
  1478. :version "24.1"
  1479. :type '(choice
  1480. (const :tag "Use fuzzy text search" nil)
  1481. (const :tag "Match only exact headline" t)
  1482. (const :tag "Match exact headline or query to create it"
  1483. query-to-create)))
  1484. (defcustom org-link-frame-setup
  1485. '((vm . vm-visit-folder-other-frame)
  1486. (vm-imap . vm-visit-imap-folder-other-frame)
  1487. (gnus . org-gnus-no-new-news)
  1488. (file . find-file-other-window)
  1489. (wl . wl-other-frame))
  1490. "Setup the frame configuration for following links.
  1491. When following a link with Emacs, it may often be useful to display
  1492. this link in another window or frame. This variable can be used to
  1493. set this up for the different types of links.
  1494. For VM, use any of
  1495. `vm-visit-folder'
  1496. `vm-visit-folder-other-window'
  1497. `vm-visit-folder-other-frame'
  1498. For Gnus, use any of
  1499. `gnus'
  1500. `gnus-other-frame'
  1501. `org-gnus-no-new-news'
  1502. For FILE, use any of
  1503. `find-file'
  1504. `find-file-other-window'
  1505. `find-file-other-frame'
  1506. For Wanderlust use any of
  1507. `wl'
  1508. `wl-other-frame'
  1509. For the calendar, use the variable `calendar-setup'.
  1510. For BBDB, it is currently only possible to display the matches in
  1511. another window."
  1512. :group 'org-link-follow
  1513. :type '(list
  1514. (cons (const vm)
  1515. (choice
  1516. (const vm-visit-folder)
  1517. (const vm-visit-folder-other-window)
  1518. (const vm-visit-folder-other-frame)))
  1519. (cons (const gnus)
  1520. (choice
  1521. (const gnus)
  1522. (const gnus-other-frame)
  1523. (const org-gnus-no-new-news)))
  1524. (cons (const file)
  1525. (choice
  1526. (const find-file)
  1527. (const find-file-other-window)
  1528. (const find-file-other-frame)))
  1529. (cons (const wl)
  1530. (choice
  1531. (const wl)
  1532. (const wl-other-frame)))))
  1533. (defcustom org-display-internal-link-with-indirect-buffer nil
  1534. "Non-nil means use indirect buffer to display infile links.
  1535. Activating internal links (from one location in a file to another location
  1536. in the same file) normally just jumps to the location. When the link is
  1537. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1538. is displayed in
  1539. another window. When this option is set, the other window actually displays
  1540. an indirect buffer clone of the current buffer, to avoid any visibility
  1541. changes to the current buffer."
  1542. :group 'org-link-follow
  1543. :type 'boolean)
  1544. (defcustom org-open-non-existing-files nil
  1545. "Non-nil means `org-open-file' will open non-existing files.
  1546. When nil, an error will be generated.
  1547. This variable applies only to external applications because they
  1548. might choke on non-existing files. If the link is to a file that
  1549. will be opened in Emacs, the variable is ignored."
  1550. :group 'org-link-follow
  1551. :type 'boolean)
  1552. (defcustom org-open-directory-means-index-dot-org nil
  1553. "Non-nil means a link to a directory really means to index.org.
  1554. When nil, following a directory link will run dired or open a finder/explorer
  1555. window on that directory."
  1556. :group 'org-link-follow
  1557. :type 'boolean)
  1558. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1559. "Function and arguments to call for following mailto links.
  1560. This is a list with the first element being a Lisp function, and the
  1561. remaining elements being arguments to the function. In string arguments,
  1562. %a will be replaced by the address, and %s will be replaced by the subject
  1563. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1564. :group 'org-link-follow
  1565. :type '(choice
  1566. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1567. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1568. (const :tag "message-mail" (message-mail "%a" "%s"))
  1569. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1570. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1571. "Non-nil means ask for confirmation before executing shell links.
  1572. Shell links can be dangerous: just think about a link
  1573. [[shell:rm -rf ~/*][Google Search]]
  1574. This link would show up in your Org-mode document as \"Google Search\",
  1575. but really it would remove your entire home directory.
  1576. Therefore we advise against setting this variable to nil.
  1577. Just change it to `y-or-n-p' if you want to confirm with a
  1578. single keystroke rather than having to type \"yes\"."
  1579. :group 'org-link-follow
  1580. :type '(choice
  1581. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1582. (const :tag "with y-or-n (faster)" y-or-n-p)
  1583. (const :tag "no confirmation (dangerous)" nil)))
  1584. (put 'org-confirm-shell-link-function
  1585. 'safe-local-variable
  1586. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1587. (defcustom org-confirm-shell-link-not-regexp ""
  1588. "A regexp to skip confirmation for shell links."
  1589. :group 'org-link-follow
  1590. :version "24.1"
  1591. :type 'regexp)
  1592. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1593. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1594. Elisp links can be dangerous: just think about a link
  1595. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1596. This link would show up in your Org-mode document as \"Google Search\",
  1597. but really it would remove your entire home directory.
  1598. Therefore we advise against setting this variable to nil.
  1599. Just change it to `y-or-n-p' if you want to confirm with a
  1600. single keystroke rather than having to type \"yes\"."
  1601. :group 'org-link-follow
  1602. :type '(choice
  1603. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1604. (const :tag "with y-or-n (faster)" y-or-n-p)
  1605. (const :tag "no confirmation (dangerous)" nil)))
  1606. (put 'org-confirm-shell-link-function
  1607. 'safe-local-variable
  1608. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1609. (defcustom org-confirm-elisp-link-not-regexp ""
  1610. "A regexp to skip confirmation for Elisp links."
  1611. :group 'org-link-follow
  1612. :version "24.1"
  1613. :type 'regexp)
  1614. (defconst org-file-apps-defaults-gnu
  1615. '((remote . emacs)
  1616. (system . mailcap)
  1617. (t . mailcap))
  1618. "Default file applications on a UNIX or GNU/Linux system.
  1619. See `org-file-apps'.")
  1620. (defconst org-file-apps-defaults-macosx
  1621. '((remote . emacs)
  1622. (t . "open %s")
  1623. (system . "open %s")
  1624. ("ps.gz" . "gv %s")
  1625. ("eps.gz" . "gv %s")
  1626. ("dvi" . "xdvi %s")
  1627. ("fig" . "xfig %s"))
  1628. "Default file applications on a MacOS X system.
  1629. The system \"open\" is known as a default, but we use X11 applications
  1630. for some files for which the OS does not have a good default.
  1631. See `org-file-apps'.")
  1632. (defconst org-file-apps-defaults-windowsnt
  1633. (list
  1634. '(remote . emacs)
  1635. (cons t
  1636. (list (if (featurep 'xemacs)
  1637. 'mswindows-shell-execute
  1638. 'w32-shell-execute)
  1639. "open" 'file))
  1640. (cons 'system
  1641. (list (if (featurep 'xemacs)
  1642. 'mswindows-shell-execute
  1643. 'w32-shell-execute)
  1644. "open" 'file)))
  1645. "Default file applications on a Windows NT system.
  1646. The system \"open\" is used for most files.
  1647. See `org-file-apps'.")
  1648. (defcustom org-file-apps
  1649. '(
  1650. (auto-mode . emacs)
  1651. ("\\.mm\\'" . default)
  1652. ("\\.x?html?\\'" . default)
  1653. ("\\.pdf\\'" . default)
  1654. )
  1655. "External applications for opening `file:path' items in a document.
  1656. Org-mode uses system defaults for different file types, but
  1657. you can use this variable to set the application for a given file
  1658. extension. The entries in this list are cons cells where the car identifies
  1659. files and the cdr the corresponding command. Possible values for the
  1660. file identifier are
  1661. \"string\" A string as a file identifier can be interpreted in different
  1662. ways, depending on its contents:
  1663. - Alphanumeric characters only:
  1664. Match links with this file extension.
  1665. Example: (\"pdf\" . \"evince %s\")
  1666. to open PDFs with evince.
  1667. - Regular expression: Match links where the
  1668. filename matches the regexp. If you want to
  1669. use groups here, use shy groups.
  1670. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1671. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1672. to open *.html and *.xhtml with firefox.
  1673. - Regular expression which contains (non-shy) groups:
  1674. Match links where the whole link, including \"::\", and
  1675. anything after that, matches the regexp.
  1676. In a custom command string, %1, %2, etc. are replaced with
  1677. the parts of the link that were matched by the groups.
  1678. For backwards compatibility, if a command string is given
  1679. that does not use any of the group matches, this case is
  1680. handled identically to the second one (i.e. match against
  1681. file name only).
  1682. In a custom lisp form, you can access the group matches with
  1683. (match-string n link).
  1684. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1685. to open [[file:document.pdf::5]] with evince at page 5.
  1686. `directory' Matches a directory
  1687. `remote' Matches a remote file, accessible through tramp or efs.
  1688. Remote files most likely should be visited through Emacs
  1689. because external applications cannot handle such paths.
  1690. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1691. so all files Emacs knows how to handle. Using this with
  1692. command `emacs' will open most files in Emacs. Beware that this
  1693. will also open html files inside Emacs, unless you add
  1694. (\"html\" . default) to the list as well.
  1695. t Default for files not matched by any of the other options.
  1696. `system' The system command to open files, like `open' on Windows
  1697. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1698. that will be selected if you call `C-c C-o' with a double
  1699. \\[universal-argument] \\[universal-argument] prefix.
  1700. Possible values for the command are:
  1701. `emacs' The file will be visited by the current Emacs process.
  1702. `default' Use the default application for this file type, which is the
  1703. association for t in the list, most likely in the system-specific
  1704. part.
  1705. This can be used to overrule an unwanted setting in the
  1706. system-specific variable.
  1707. `system' Use the system command for opening files, like \"open\".
  1708. This command is specified by the entry whose car is `system'.
  1709. Most likely, the system-specific version of this variable
  1710. does define this command, but you can overrule/replace it
  1711. here.
  1712. string A command to be executed by a shell; %s will be replaced
  1713. by the path to the file.
  1714. sexp A Lisp form which will be evaluated. The file path will
  1715. be available in the Lisp variable `file'.
  1716. For more examples, see the system specific constants
  1717. `org-file-apps-defaults-macosx'
  1718. `org-file-apps-defaults-windowsnt'
  1719. `org-file-apps-defaults-gnu'."
  1720. :group 'org-link-follow
  1721. :type '(repeat
  1722. (cons (choice :value ""
  1723. (string :tag "Extension")
  1724. (const :tag "System command to open files" system)
  1725. (const :tag "Default for unrecognized files" t)
  1726. (const :tag "Remote file" remote)
  1727. (const :tag "Links to a directory" directory)
  1728. (const :tag "Any files that have Emacs modes"
  1729. auto-mode))
  1730. (choice :value ""
  1731. (const :tag "Visit with Emacs" emacs)
  1732. (const :tag "Use default" default)
  1733. (const :tag "Use the system command" system)
  1734. (string :tag "Command")
  1735. (sexp :tag "Lisp form")))))
  1736. (defcustom org-doi-server-url "http://dx.doi.org/"
  1737. "The URL of the DOI server."
  1738. :type 'string
  1739. :version "24.3"
  1740. :group 'org-link-follow)
  1741. (defgroup org-refile nil
  1742. "Options concerning refiling entries in Org-mode."
  1743. :tag "Org Refile"
  1744. :group 'org)
  1745. (defcustom org-directory "~/org"
  1746. "Directory with org files.
  1747. This is just a default location to look for Org files. There is no need
  1748. at all to put your files into this directory. It is only used in the
  1749. following situations:
  1750. 1. When a capture template specifies a target file that is not an
  1751. absolute path. The path will then be interpreted relative to
  1752. `org-directory'
  1753. 2. When a capture note is filed away in an interactive way (when exiting the
  1754. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1755. with `org-directory' as the default path."
  1756. :group 'org-refile
  1757. :group 'org-remember
  1758. :group 'org-capture
  1759. :type 'directory)
  1760. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1761. "Default target for storing notes.
  1762. Used as a fall back file for org-remember.el and org-capture.el, for
  1763. templates that do not specify a target file."
  1764. :group 'org-refile
  1765. :group 'org-remember
  1766. :group 'org-capture
  1767. :type '(choice
  1768. (const :tag "Default from remember-data-file" nil)
  1769. file))
  1770. (defcustom org-goto-interface 'outline
  1771. "The default interface to be used for `org-goto'.
  1772. Allowed values are:
  1773. outline The interface shows an outline of the relevant file
  1774. and the correct heading is found by moving through
  1775. the outline or by searching with incremental search.
  1776. outline-path-completion Headlines in the current buffer are offered via
  1777. completion. This is the interface also used by
  1778. the refile command."
  1779. :group 'org-refile
  1780. :type '(choice
  1781. (const :tag "Outline" outline)
  1782. (const :tag "Outline-path-completion" outline-path-completion)))
  1783. (defcustom org-goto-max-level 5
  1784. "Maximum target level when running `org-goto' with refile interface."
  1785. :group 'org-refile
  1786. :type 'integer)
  1787. (defcustom org-reverse-note-order nil
  1788. "Non-nil means store new notes at the beginning of a file or entry.
  1789. When nil, new notes will be filed to the end of a file or entry.
  1790. This can also be a list with cons cells of regular expressions that
  1791. are matched against file names, and values."
  1792. :group 'org-remember
  1793. :group 'org-capture
  1794. :group 'org-refile
  1795. :type '(choice
  1796. (const :tag "Reverse always" t)
  1797. (const :tag "Reverse never" nil)
  1798. (repeat :tag "By file name regexp"
  1799. (cons regexp boolean))))
  1800. (defcustom org-log-refile nil
  1801. "Information to record when a task is refiled.
  1802. Possible values are:
  1803. nil Don't add anything
  1804. time Add a time stamp to the task
  1805. note Prompt for a note and add it with template `org-log-note-headings'
  1806. This option can also be set with on a per-file-basis with
  1807. #+STARTUP: nologrefile
  1808. #+STARTUP: logrefile
  1809. #+STARTUP: lognoterefile
  1810. You can have local logging settings for a subtree by setting the LOGGING
  1811. property to one or more of these keywords.
  1812. When bulk-refiling from the agenda, the value `note' is forbidden and
  1813. will temporarily be changed to `time'."
  1814. :group 'org-refile
  1815. :group 'org-progress
  1816. :version "24.1"
  1817. :type '(choice
  1818. (const :tag "No logging" nil)
  1819. (const :tag "Record timestamp" time)
  1820. (const :tag "Record timestamp with note." note)))
  1821. (defcustom org-refile-targets nil
  1822. "Targets for refiling entries with \\[org-refile].
  1823. This is a list of cons cells. Each cell contains:
  1824. - a specification of the files to be considered, either a list of files,
  1825. or a symbol whose function or variable value will be used to retrieve
  1826. a file name or a list of file names. If you use `org-agenda-files' for
  1827. that, all agenda files will be scanned for targets. Nil means consider
  1828. headings in the current buffer.
  1829. - A specification of how to find candidate refile targets. This may be
  1830. any of:
  1831. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1832. This tag has to be present in all target headlines, inheritance will
  1833. not be considered.
  1834. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1835. todo keyword.
  1836. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1837. headlines that are refiling targets.
  1838. - a cons cell (:level . N). Any headline of level N is considered a target.
  1839. Note that, when `org-odd-levels-only' is set, level corresponds to
  1840. order in hierarchy, not to the number of stars.
  1841. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1842. Note that, when `org-odd-levels-only' is set, level corresponds to
  1843. order in hierarchy, not to the number of stars.
  1844. Each element of this list generates a set of possible targets.
  1845. The union of these sets is presented (with completion) to
  1846. the user by `org-refile'.
  1847. You can set the variable `org-refile-target-verify-function' to a function
  1848. to verify each headline found by the simple criteria above.
  1849. When this variable is nil, all top-level headlines in the current buffer
  1850. are used, equivalent to the value `((nil . (:level . 1))'."
  1851. :group 'org-refile
  1852. :type '(repeat
  1853. (cons
  1854. (choice :value org-agenda-files
  1855. (const :tag "All agenda files" org-agenda-files)
  1856. (const :tag "Current buffer" nil)
  1857. (function) (variable) (file))
  1858. (choice :tag "Identify target headline by"
  1859. (cons :tag "Specific tag" (const :value :tag) (string))
  1860. (cons :tag "TODO keyword" (const :value :todo) (string))
  1861. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1862. (cons :tag "Level number" (const :value :level) (integer))
  1863. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1864. (defcustom org-refile-target-verify-function nil
  1865. "Function to verify if the headline at point should be a refile target.
  1866. The function will be called without arguments, with point at the
  1867. beginning of the headline. It should return t and leave point
  1868. where it is if the headline is a valid target for refiling.
  1869. If the target should not be selected, the function must return nil.
  1870. In addition to this, it may move point to a place from where the search
  1871. should be continued. For example, the function may decide that the entire
  1872. subtree of the current entry should be excluded and move point to the end
  1873. of the subtree."
  1874. :group 'org-refile
  1875. :type 'function)
  1876. (defcustom org-refile-use-cache nil
  1877. "Non-nil means cache refile targets to speed up the process.
  1878. The cache for a particular file will be updated automatically when
  1879. the buffer has been killed, or when any of the marker used for flagging
  1880. refile targets no longer points at a live buffer.
  1881. If you have added new entries to a buffer that might themselves be targets,
  1882. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1883. find that easier, `C-u C-u C-u C-c C-w'."
  1884. :group 'org-refile
  1885. :version "24.1"
  1886. :type 'boolean)
  1887. (defcustom org-refile-use-outline-path nil
  1888. "Non-nil means provide refile targets as paths.
  1889. So a level 3 headline will be available as level1/level2/level3.
  1890. When the value is `file', also include the file name (without directory)
  1891. into the path. In this case, you can also stop the completion after
  1892. the file name, to get entries inserted as top level in the file.
  1893. When `full-file-path', include the full file path."
  1894. :group 'org-refile
  1895. :type '(choice
  1896. (const :tag "Not" nil)
  1897. (const :tag "Yes" t)
  1898. (const :tag "Start with file name" file)
  1899. (const :tag "Start with full file path" full-file-path)))
  1900. (defcustom org-outline-path-complete-in-steps t
  1901. "Non-nil means complete the outline path in hierarchical steps.
  1902. When Org-mode uses the refile interface to select an outline path
  1903. \(see variable `org-refile-use-outline-path'), the completion of
  1904. the path can be done is a single go, or if can be done in steps down
  1905. the headline hierarchy. Going in steps is probably the best if you
  1906. do not use a special completion package like `ido' or `icicles'.
  1907. However, when using these packages, going in one step can be very
  1908. fast, while still showing the whole path to the entry."
  1909. :group 'org-refile
  1910. :type 'boolean)
  1911. (defcustom org-refile-allow-creating-parent-nodes nil
  1912. "Non-nil means allow to create new nodes as refile targets.
  1913. New nodes are then created by adding \"/new node name\" to the completion
  1914. of an existing node. When the value of this variable is `confirm',
  1915. new node creation must be confirmed by the user (recommended)
  1916. When nil, the completion must match an existing entry.
  1917. Note that, if the new heading is not seen by the criteria
  1918. listed in `org-refile-targets', multiple instances of the same
  1919. heading would be created by trying again to file under the new
  1920. heading."
  1921. :group 'org-refile
  1922. :type '(choice
  1923. (const :tag "Never" nil)
  1924. (const :tag "Always" t)
  1925. (const :tag "Prompt for confirmation" confirm)))
  1926. (defcustom org-refile-active-region-within-subtree nil
  1927. "Non-nil means also refile active region within a subtree.
  1928. By default `org-refile' doesn't allow refiling regions if they
  1929. don't contain a set of subtrees, but it might be convenient to
  1930. do so sometimes: in that case, the first line of the region is
  1931. converted to a headline before refiling."
  1932. :group 'org-refile
  1933. :version "24.1"
  1934. :type 'boolean)
  1935. (defgroup org-todo nil
  1936. "Options concerning TODO items in Org-mode."
  1937. :tag "Org TODO"
  1938. :group 'org)
  1939. (defgroup org-progress nil
  1940. "Options concerning Progress logging in Org-mode."
  1941. :tag "Org Progress"
  1942. :group 'org-time)
  1943. (defvar org-todo-interpretation-widgets
  1944. '((:tag "Sequence (cycling hits every state)" sequence)
  1945. (:tag "Type (cycling directly to DONE)" type))
  1946. "The available interpretation symbols for customizing `org-todo-keywords'.
  1947. Interested libraries should add to this list.")
  1948. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1949. "List of TODO entry keyword sequences and their interpretation.
  1950. \\<org-mode-map>This is a list of sequences.
  1951. Each sequence starts with a symbol, either `sequence' or `type',
  1952. indicating if the keywords should be interpreted as a sequence of
  1953. action steps, or as different types of TODO items. The first
  1954. keywords are states requiring action - these states will select a headline
  1955. for inclusion into the global TODO list Org-mode produces. If one of
  1956. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1957. signify that no further action is necessary. If \"|\" is not found,
  1958. the last keyword is treated as the only DONE state of the sequence.
  1959. The command \\[org-todo] cycles an entry through these states, and one
  1960. additional state where no keyword is present. For details about this
  1961. cycling, see the manual.
  1962. TODO keywords and interpretation can also be set on a per-file basis with
  1963. the special #+SEQ_TODO and #+TYP_TODO lines.
  1964. Each keyword can optionally specify a character for fast state selection
  1965. \(in combination with the variable `org-use-fast-todo-selection')
  1966. and specifiers for state change logging, using the same syntax that
  1967. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1968. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1969. indicates to record a time stamp each time this state is selected.
  1970. Each keyword may also specify if a timestamp or a note should be
  1971. recorded when entering or leaving the state, by adding additional
  1972. characters in the parenthesis after the keyword. This looks like this:
  1973. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1974. record only the time of the state change. With X and Y being either
  1975. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1976. Y when leaving the state if and only if the *target* state does not
  1977. define X. You may omit any of the fast-selection key or X or /Y,
  1978. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1979. For backward compatibility, this variable may also be just a list
  1980. of keywords. In this case the interpretation (sequence or type) will be
  1981. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1982. :group 'org-todo
  1983. :group 'org-keywords
  1984. :type '(choice
  1985. (repeat :tag "Old syntax, just keywords"
  1986. (string :tag "Keyword"))
  1987. (repeat :tag "New syntax"
  1988. (cons
  1989. (choice
  1990. :tag "Interpretation"
  1991. ;;Quick and dirty way to see
  1992. ;;`org-todo-interpretations'. This takes the
  1993. ;;place of item arguments
  1994. :convert-widget
  1995. (lambda (widget)
  1996. (widget-put widget
  1997. :args (mapcar
  1998. #'(lambda (x)
  1999. (widget-convert
  2000. (cons 'const x)))
  2001. org-todo-interpretation-widgets))
  2002. widget))
  2003. (repeat
  2004. (string :tag "Keyword"))))))
  2005. (defvar org-todo-keywords-1 nil
  2006. "All TODO and DONE keywords active in a buffer.")
  2007. (make-variable-buffer-local 'org-todo-keywords-1)
  2008. (defvar org-todo-keywords-for-agenda nil)
  2009. (defvar org-done-keywords-for-agenda nil)
  2010. (defvar org-drawers-for-agenda nil)
  2011. (defvar org-todo-keyword-alist-for-agenda nil)
  2012. (defvar org-tag-alist-for-agenda nil)
  2013. (defvar org-agenda-contributing-files nil)
  2014. (defvar org-not-done-keywords nil)
  2015. (make-variable-buffer-local 'org-not-done-keywords)
  2016. (defvar org-done-keywords nil)
  2017. (make-variable-buffer-local 'org-done-keywords)
  2018. (defvar org-todo-heads nil)
  2019. (make-variable-buffer-local 'org-todo-heads)
  2020. (defvar org-todo-sets nil)
  2021. (make-variable-buffer-local 'org-todo-sets)
  2022. (defvar org-todo-log-states nil)
  2023. (make-variable-buffer-local 'org-todo-log-states)
  2024. (defvar org-todo-kwd-alist nil)
  2025. (make-variable-buffer-local 'org-todo-kwd-alist)
  2026. (defvar org-todo-key-alist nil)
  2027. (make-variable-buffer-local 'org-todo-key-alist)
  2028. (defvar org-todo-key-trigger nil)
  2029. (make-variable-buffer-local 'org-todo-key-trigger)
  2030. (defcustom org-todo-interpretation 'sequence
  2031. "Controls how TODO keywords are interpreted.
  2032. This variable is in principle obsolete and is only used for
  2033. backward compatibility, if the interpretation of todo keywords is
  2034. not given already in `org-todo-keywords'. See that variable for
  2035. more information."
  2036. :group 'org-todo
  2037. :group 'org-keywords
  2038. :type '(choice (const sequence)
  2039. (const type)))
  2040. (defcustom org-use-fast-todo-selection t
  2041. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2042. This variable describes if and under what circumstances the cycling
  2043. mechanism for TODO keywords will be replaced by a single-key, direct
  2044. selection scheme.
  2045. When nil, fast selection is never used.
  2046. When the symbol `prefix', it will be used when `org-todo' is called
  2047. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2048. `C-u t' in an agenda buffer.
  2049. When t, fast selection is used by default. In this case, the prefix
  2050. argument forces cycling instead.
  2051. In all cases, the special interface is only used if access keys have
  2052. actually been assigned by the user, i.e. if keywords in the configuration
  2053. are followed by a letter in parenthesis, like TODO(t)."
  2054. :group 'org-todo
  2055. :type '(choice
  2056. (const :tag "Never" nil)
  2057. (const :tag "By default" t)
  2058. (const :tag "Only with C-u C-c C-t" prefix)))
  2059. (defcustom org-provide-todo-statistics t
  2060. "Non-nil means update todo statistics after insert and toggle.
  2061. ALL-HEADLINES means update todo statistics by including headlines
  2062. with no TODO keyword as well, counting them as not done.
  2063. A list of TODO keywords means the same, but skip keywords that are
  2064. not in this list.
  2065. When this is set, todo statistics is updated in the parent of the
  2066. current entry each time a todo state is changed."
  2067. :group 'org-todo
  2068. :type '(choice
  2069. (const :tag "Yes, only for TODO entries" t)
  2070. (const :tag "Yes, including all entries" 'all-headlines)
  2071. (repeat :tag "Yes, for TODOs in this list"
  2072. (string :tag "TODO keyword"))
  2073. (other :tag "No TODO statistics" nil)))
  2074. (defcustom org-hierarchical-todo-statistics t
  2075. "Non-nil means TODO statistics covers just direct children.
  2076. When nil, all entries in the subtree are considered.
  2077. This has only an effect if `org-provide-todo-statistics' is set.
  2078. To set this to nil for only a single subtree, use a COOKIE_DATA
  2079. property and include the word \"recursive\" into the value."
  2080. :group 'org-todo
  2081. :type 'boolean)
  2082. (defcustom org-after-todo-state-change-hook nil
  2083. "Hook which is run after the state of a TODO item was changed.
  2084. The new state (a string with a TODO keyword, or nil) is available in the
  2085. Lisp variable `org-state'."
  2086. :group 'org-todo
  2087. :type 'hook)
  2088. (defvar org-blocker-hook nil
  2089. "Hook for functions that are allowed to block a state change.
  2090. Functions in this hook should not modify the buffer.
  2091. Each function gets as its single argument a property list,
  2092. see `org-trigger-hook' for more information about this list.
  2093. If any of the functions in this hook returns nil, the state change
  2094. is blocked.")
  2095. (defvar org-trigger-hook nil
  2096. "Hook for functions that are triggered by a state change.
  2097. Each function gets as its single argument a property list with at
  2098. least the following elements:
  2099. (:type type-of-change :position pos-at-entry-start
  2100. :from old-state :to new-state)
  2101. Depending on the type, more properties may be present.
  2102. This mechanism is currently implemented for:
  2103. TODO state changes
  2104. ------------------
  2105. :type todo-state-change
  2106. :from previous state (keyword as a string), or nil, or a symbol
  2107. 'todo' or 'done', to indicate the general type of state.
  2108. :to new state, like in :from")
  2109. (defcustom org-enforce-todo-dependencies nil
  2110. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2111. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2112. be blocked if any prior sibling is not yet done.
  2113. Finally, if the parent is blocked because of ordered siblings of its own,
  2114. the child will also be blocked."
  2115. :set (lambda (var val)
  2116. (set var val)
  2117. (if val
  2118. (add-hook 'org-blocker-hook
  2119. 'org-block-todo-from-children-or-siblings-or-parent)
  2120. (remove-hook 'org-blocker-hook
  2121. 'org-block-todo-from-children-or-siblings-or-parent)))
  2122. :group 'org-todo
  2123. :type 'boolean)
  2124. (defcustom org-enforce-todo-checkbox-dependencies nil
  2125. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2126. When this is nil, checkboxes have no influence on switching TODO states.
  2127. When non-nil, you first need to check off all check boxes before the TODO
  2128. entry can be switched to DONE.
  2129. This variable needs to be set before org.el is loaded, and you need to
  2130. restart Emacs after a change to make the change effective. The only way
  2131. to change is while Emacs is running is through the customize interface."
  2132. :set (lambda (var val)
  2133. (set var val)
  2134. (if val
  2135. (add-hook 'org-blocker-hook
  2136. 'org-block-todo-from-checkboxes)
  2137. (remove-hook 'org-blocker-hook
  2138. 'org-block-todo-from-checkboxes)))
  2139. :group 'org-todo
  2140. :type 'boolean)
  2141. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2142. "Non-nil means inserting a TODO heading is treated as state change.
  2143. So when the command \\[org-insert-todo-heading] is used, state change
  2144. logging will apply if appropriate. When nil, the new TODO item will
  2145. be inserted directly, and no logging will take place."
  2146. :group 'org-todo
  2147. :type 'boolean)
  2148. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2149. "Non-nil means switching TODO states with S-cursor counts as state change.
  2150. This is the default behavior. However, setting this to nil allows a
  2151. convenient way to select a TODO state and bypass any logging associated
  2152. with that."
  2153. :group 'org-todo
  2154. :type 'boolean)
  2155. (defcustom org-todo-state-tags-triggers nil
  2156. "Tag changes that should be triggered by TODO state changes.
  2157. This is a list. Each entry is
  2158. (state-change (tag . flag) .......)
  2159. State-change can be a string with a state, and empty string to indicate the
  2160. state that has no TODO keyword, or it can be one of the symbols `todo'
  2161. or `done', meaning any not-done or done state, respectively."
  2162. :group 'org-todo
  2163. :group 'org-tags
  2164. :type '(repeat
  2165. (cons (choice :tag "When changing to"
  2166. (const :tag "Not-done state" todo)
  2167. (const :tag "Done state" done)
  2168. (string :tag "State"))
  2169. (repeat
  2170. (cons :tag "Tag action"
  2171. (string :tag "Tag")
  2172. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2173. (defcustom org-log-done nil
  2174. "Information to record when a task moves to the DONE state.
  2175. Possible values are:
  2176. nil Don't add anything, just change the keyword
  2177. time Add a time stamp to the task
  2178. note Prompt for a note and add it with template `org-log-note-headings'
  2179. This option can also be set with on a per-file-basis with
  2180. #+STARTUP: nologdone
  2181. #+STARTUP: logdone
  2182. #+STARTUP: lognotedone
  2183. You can have local logging settings for a subtree by setting the LOGGING
  2184. property to one or more of these keywords."
  2185. :group 'org-todo
  2186. :group 'org-progress
  2187. :type '(choice
  2188. (const :tag "No logging" nil)
  2189. (const :tag "Record CLOSED timestamp" time)
  2190. (const :tag "Record CLOSED timestamp with note." note)))
  2191. ;; Normalize old uses of org-log-done.
  2192. (cond
  2193. ((eq org-log-done t) (setq org-log-done 'time))
  2194. ((and (listp org-log-done) (memq 'done org-log-done))
  2195. (setq org-log-done 'note)))
  2196. (defcustom org-log-reschedule nil
  2197. "Information to record when the scheduling date of a tasks is modified.
  2198. Possible values are:
  2199. nil Don't add anything, just change the date
  2200. time Add a time stamp to the task
  2201. note Prompt for a note and add it with template `org-log-note-headings'
  2202. This option can also be set with on a per-file-basis with
  2203. #+STARTUP: nologreschedule
  2204. #+STARTUP: logreschedule
  2205. #+STARTUP: lognotereschedule"
  2206. :group 'org-todo
  2207. :group 'org-progress
  2208. :type '(choice
  2209. (const :tag "No logging" nil)
  2210. (const :tag "Record timestamp" time)
  2211. (const :tag "Record timestamp with note." note)))
  2212. (defcustom org-log-redeadline nil
  2213. "Information to record when the deadline date of a tasks is modified.
  2214. Possible values are:
  2215. nil Don't add anything, just change the date
  2216. time Add a time stamp to the task
  2217. note Prompt for a note and add it with template `org-log-note-headings'
  2218. This option can also be set with on a per-file-basis with
  2219. #+STARTUP: nologredeadline
  2220. #+STARTUP: logredeadline
  2221. #+STARTUP: lognoteredeadline
  2222. You can have local logging settings for a subtree by setting the LOGGING
  2223. property to one or more of these keywords."
  2224. :group 'org-todo
  2225. :group 'org-progress
  2226. :type '(choice
  2227. (const :tag "No logging" nil)
  2228. (const :tag "Record timestamp" time)
  2229. (const :tag "Record timestamp with note." note)))
  2230. (defcustom org-log-note-clock-out nil
  2231. "Non-nil means record a note when clocking out of an item.
  2232. This can also be configured on a per-file basis by adding one of
  2233. the following lines anywhere in the buffer:
  2234. #+STARTUP: lognoteclock-out
  2235. #+STARTUP: nolognoteclock-out"
  2236. :group 'org-todo
  2237. :group 'org-progress
  2238. :type 'boolean)
  2239. (defcustom org-log-done-with-time t
  2240. "Non-nil means the CLOSED time stamp will contain date and time.
  2241. When nil, only the date will be recorded."
  2242. :group 'org-progress
  2243. :type 'boolean)
  2244. (defcustom org-log-note-headings
  2245. '((done . "CLOSING NOTE %t")
  2246. (state . "State %-12s from %-12S %t")
  2247. (note . "Note taken on %t")
  2248. (reschedule . "Rescheduled from %S on %t")
  2249. (delschedule . "Not scheduled, was %S on %t")
  2250. (redeadline . "New deadline from %S on %t")
  2251. (deldeadline . "Removed deadline, was %S on %t")
  2252. (refile . "Refiled on %t")
  2253. (clock-out . ""))
  2254. "Headings for notes added to entries.
  2255. The value is an alist, with the car being a symbol indicating the note
  2256. context, and the cdr is the heading to be used. The heading may also be the
  2257. empty string.
  2258. %t in the heading will be replaced by a time stamp.
  2259. %T will be an active time stamp instead the default inactive one
  2260. %d will be replaced by a short-format time stamp.
  2261. %D will be replaced by an active short-format time stamp.
  2262. %s will be replaced by the new TODO state, in double quotes.
  2263. %S will be replaced by the old TODO state, in double quotes.
  2264. %u will be replaced by the user name.
  2265. %U will be replaced by the full user name.
  2266. In fact, it is not a good idea to change the `state' entry, because
  2267. agenda log mode depends on the format of these entries."
  2268. :group 'org-todo
  2269. :group 'org-progress
  2270. :type '(list :greedy t
  2271. (cons (const :tag "Heading when closing an item" done) string)
  2272. (cons (const :tag
  2273. "Heading when changing todo state (todo sequence only)"
  2274. state) string)
  2275. (cons (const :tag "Heading when just taking a note" note) string)
  2276. (cons (const :tag "Heading when clocking out" clock-out) string)
  2277. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2278. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2279. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2280. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2281. (cons (const :tag "Heading when refiling" refile) string)))
  2282. (unless (assq 'note org-log-note-headings)
  2283. (push '(note . "%t") org-log-note-headings))
  2284. (defcustom org-log-into-drawer nil
  2285. "Non-nil means insert state change notes and time stamps into a drawer.
  2286. When nil, state changes notes will be inserted after the headline and
  2287. any scheduling and clock lines, but not inside a drawer.
  2288. The value of this variable should be the name of the drawer to use.
  2289. LOGBOOK is proposed as the default drawer for this purpose, you can
  2290. also set this to a string to define the drawer of your choice.
  2291. A value of t is also allowed, representing \"LOGBOOK\".
  2292. A value of t or nil can also be set with on a per-file-basis with
  2293. #+STARTUP: logdrawer
  2294. #+STARTUP: nologdrawer
  2295. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2296. will be ignored.
  2297. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2298. a subtree."
  2299. :group 'org-todo
  2300. :group 'org-progress
  2301. :type '(choice
  2302. (const :tag "Not into a drawer" nil)
  2303. (const :tag "LOGBOOK" t)
  2304. (string :tag "Other")))
  2305. (if (fboundp 'defvaralias)
  2306. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2307. (defun org-log-into-drawer ()
  2308. "Return the value of `org-log-into-drawer', but let properties overrule.
  2309. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2310. used instead of the default value."
  2311. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2312. (cond
  2313. ((not p) org-log-into-drawer)
  2314. ((equal p "nil") nil)
  2315. ((equal p "t") "LOGBOOK")
  2316. (t p))))
  2317. (defcustom org-log-state-notes-insert-after-drawers nil
  2318. "Non-nil means insert state change notes after any drawers in entry.
  2319. Only the drawers that *immediately* follow the headline and the
  2320. deadline/scheduled line are skipped.
  2321. When nil, insert notes right after the heading and perhaps the line
  2322. with deadline/scheduling if present.
  2323. This variable will have no effect if `org-log-into-drawer' is
  2324. set."
  2325. :group 'org-todo
  2326. :group 'org-progress
  2327. :type 'boolean)
  2328. (defcustom org-log-states-order-reversed t
  2329. "Non-nil means the latest state note will be directly after heading.
  2330. When nil, the state change notes will be ordered according to time.
  2331. This option can also be set with on a per-file-basis with
  2332. #+STARTUP: logstatesreversed
  2333. #+STARTUP: nologstatesreversed"
  2334. :group 'org-todo
  2335. :group 'org-progress
  2336. :type 'boolean)
  2337. (defcustom org-todo-repeat-to-state nil
  2338. "The TODO state to which a repeater should return the repeating task.
  2339. By default this is the first task in a TODO sequence, or the previous state
  2340. in a TODO_TYP set. But you can specify another task here.
  2341. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2342. :group 'org-todo
  2343. :version "24.1"
  2344. :type '(choice (const :tag "Head of sequence" nil)
  2345. (string :tag "Specific state")))
  2346. (defcustom org-log-repeat 'time
  2347. "Non-nil means record moving through the DONE state when triggering repeat.
  2348. An auto-repeating task is immediately switched back to TODO when
  2349. marked DONE. If you are not logging state changes (by adding \"@\"
  2350. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2351. record a closing note, there will be no record of the task moving
  2352. through DONE. This variable forces taking a note anyway.
  2353. nil Don't force a record
  2354. time Record a time stamp
  2355. note Prompt for a note and add it with template `org-log-note-headings'
  2356. This option can also be set with on a per-file-basis with
  2357. #+STARTUP: nologrepeat
  2358. #+STARTUP: logrepeat
  2359. #+STARTUP: lognoterepeat
  2360. You can have local logging settings for a subtree by setting the LOGGING
  2361. property to one or more of these keywords."
  2362. :group 'org-todo
  2363. :group 'org-progress
  2364. :type '(choice
  2365. (const :tag "Don't force a record" nil)
  2366. (const :tag "Force recording the DONE state" time)
  2367. (const :tag "Force recording a note with the DONE state" note)))
  2368. (defgroup org-priorities nil
  2369. "Priorities in Org-mode."
  2370. :tag "Org Priorities"
  2371. :group 'org-todo)
  2372. (defcustom org-enable-priority-commands t
  2373. "Non-nil means priority commands are active.
  2374. When nil, these commands will be disabled, so that you never accidentally
  2375. set a priority."
  2376. :group 'org-priorities
  2377. :type 'boolean)
  2378. (defcustom org-highest-priority ?A
  2379. "The highest priority of TODO items. A character like ?A, ?B etc.
  2380. Must have a smaller ASCII number than `org-lowest-priority'."
  2381. :group 'org-priorities
  2382. :type 'character)
  2383. (defcustom org-lowest-priority ?C
  2384. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2385. Must have a larger ASCII number than `org-highest-priority'."
  2386. :group 'org-priorities
  2387. :type 'character)
  2388. (defcustom org-default-priority ?B
  2389. "The default priority of TODO items.
  2390. This is the priority an item gets if no explicit priority is given.
  2391. When starting to cycle on an empty priority the first step in the cycle
  2392. depends on `org-priority-start-cycle-with-default'. The resulting first
  2393. step priority must not exceed the range from `org-highest-priority' to
  2394. `org-lowest-priority' which means that `org-default-priority' has to be
  2395. in this range exclusive or inclusive the range boundaries. Else the
  2396. first step refuses to set the default and the second will fall back
  2397. to (depending on the command used) the highest or lowest priority."
  2398. :group 'org-priorities
  2399. :type 'character)
  2400. (defcustom org-priority-start-cycle-with-default t
  2401. "Non-nil means start with default priority when starting to cycle.
  2402. When this is nil, the first step in the cycle will be (depending on the
  2403. command used) one higher or lower than the default priority.
  2404. See also `org-default-priority'."
  2405. :group 'org-priorities
  2406. :type 'boolean)
  2407. (defcustom org-get-priority-function nil
  2408. "Function to extract the priority from a string.
  2409. The string is normally the headline. If this is nil Org computes the
  2410. priority from the priority cookie like [#A] in the headline. It returns
  2411. an integer, increasing by 1000 for each priority level.
  2412. The user can set a different function here, which should take a string
  2413. as an argument and return the numeric priority."
  2414. :group 'org-priorities
  2415. :version "24.1"
  2416. :type 'function)
  2417. (defgroup org-time nil
  2418. "Options concerning time stamps and deadlines in Org-mode."
  2419. :tag "Org Time"
  2420. :group 'org)
  2421. (defcustom org-insert-labeled-timestamps-at-point nil
  2422. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2423. When nil, these labeled time stamps are forces into the second line of an
  2424. entry, just after the headline. When scheduling from the global TODO list,
  2425. the time stamp will always be forced into the second line."
  2426. :group 'org-time
  2427. :type 'boolean)
  2428. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2429. "Formats for `format-time-string' which are used for time stamps.
  2430. It is not recommended to change this constant.")
  2431. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2432. "Number of minutes to round time stamps to.
  2433. These are two values, the first applies when first creating a time stamp.
  2434. The second applies when changing it with the commands `S-up' and `S-down'.
  2435. When changing the time stamp, this means that it will change in steps
  2436. of N minutes, as given by the second value.
  2437. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2438. numbers should be factors of 60, so for example 5, 10, 15.
  2439. When this is larger than 1, you can still force an exact time stamp by using
  2440. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2441. and by using a prefix arg to `S-up/down' to specify the exact number
  2442. of minutes to shift."
  2443. :group 'org-time
  2444. :get #'(lambda (var) ; Make sure both elements are there
  2445. (if (integerp (default-value var))
  2446. (list (default-value var) 5)
  2447. (default-value var)))
  2448. :type '(list
  2449. (integer :tag "when inserting times")
  2450. (integer :tag "when modifying times")))
  2451. ;; Normalize old customizations of this variable.
  2452. (when (integerp org-time-stamp-rounding-minutes)
  2453. (setq org-time-stamp-rounding-minutes
  2454. (list org-time-stamp-rounding-minutes
  2455. org-time-stamp-rounding-minutes)))
  2456. (defcustom org-display-custom-times nil
  2457. "Non-nil means overlay custom formats over all time stamps.
  2458. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2459. To turn this on on a per-file basis, insert anywhere in the file:
  2460. #+STARTUP: customtime"
  2461. :group 'org-time
  2462. :set 'set-default
  2463. :type 'sexp)
  2464. (make-variable-buffer-local 'org-display-custom-times)
  2465. (defcustom org-time-stamp-custom-formats
  2466. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2467. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2468. These are overlaid over the default ISO format if the variable
  2469. `org-display-custom-times' is set. Time like %H:%M should be at the
  2470. end of the second format. The custom formats are also honored by export
  2471. commands, if custom time display is turned on at the time of export."
  2472. :group 'org-time
  2473. :type 'sexp)
  2474. (defun org-time-stamp-format (&optional long inactive)
  2475. "Get the right format for a time string."
  2476. (let ((f (if long (cdr org-time-stamp-formats)
  2477. (car org-time-stamp-formats))))
  2478. (if inactive
  2479. (concat "[" (substring f 1 -1) "]")
  2480. f)))
  2481. (defcustom org-time-clocksum-format
  2482. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2483. "The format string used when creating CLOCKSUM lines.
  2484. This is also used when Org mode generates a time duration.
  2485. The value can be a single format string containing two
  2486. %-sequences, which will be filled with the number of hours and
  2487. minutes in that order.
  2488. Alternatively, the value can be a plist associating any of the
  2489. keys :years, :months, :weeks, :days, :hours or :minutes with
  2490. format strings. The time duration is formatted using only the
  2491. time components that are needed and concatenating the results.
  2492. If a time unit in absent, it falls back to the next smallest
  2493. unit.
  2494. The keys :require-years, :require-months, :require-days,
  2495. :require-weeks, :require-hours, :require-minutes are also
  2496. meaningful. A non-nil value for these keys indicates that the
  2497. corresponding time component should always be included, even if
  2498. its value is 0.
  2499. For example,
  2500. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2501. :require-minutes t)
  2502. means durations longer than a day will be expressed in days,
  2503. hours and minutes, and durations less than a day will always be
  2504. expressed in hours and minutes (even for durations less than an
  2505. hour).
  2506. The value
  2507. \(:days \"%dd\" :minutes \"%dm\")
  2508. means durations longer than a day will be expressed in days and
  2509. minutes, and durations less than a day will be expressed entirely
  2510. in minutes (even for durations longer than an hour)."
  2511. :group 'org-time
  2512. :group 'org-clock
  2513. :version "24.3"
  2514. :type '(choice (string :tag "Format string")
  2515. (set :tag "Plist"
  2516. (group :inline t (const :tag "Years" :years)
  2517. (string :tag "Format string"))
  2518. (group :inline t
  2519. (const :tag "Always show years" :require-years)
  2520. (const t))
  2521. (group :inline t (const :tag "Months" :months)
  2522. (string :tag "Format string"))
  2523. (group :inline t
  2524. (const :tag "Always show months" :require-months)
  2525. (const t))
  2526. (group :inline t (const :tag "Weeks" :weeks)
  2527. (string :tag "Format string"))
  2528. (group :inline t
  2529. (const :tag "Always show weeks" :require-weeks)
  2530. (const t))
  2531. (group :inline t (const :tag "Days" :days)
  2532. (string :tag "Format string"))
  2533. (group :inline t
  2534. (const :tag "Always show days" :require-days)
  2535. (const t))
  2536. (group :inline t (const :tag "Hours" :hours)
  2537. (string :tag "Format string"))
  2538. (group :inline t
  2539. (const :tag "Always show hours" :require-hours)
  2540. (const t))
  2541. (group :inline t (const :tag "Minutes" :minutes)
  2542. (string :tag "Format string"))
  2543. (group :inline t
  2544. (const :tag "Always show minutes" :require-minutes)
  2545. (const t)))))
  2546. (defcustom org-time-clocksum-use-fractional nil
  2547. "When non-nil, \\[org-clock-display] uses fractional times.
  2548. See `org-time-clocksum-format' for more on time clock formats."
  2549. :group 'org-time
  2550. :group 'org-clock
  2551. :version "24.3"
  2552. :type 'boolean)
  2553. (defcustom org-time-clocksum-use-effort-durations t
  2554. "When non-nil, \\[org-clock-display] uses effort durations.
  2555. E.g. by default, one day is considered to be a 8 hours effort,
  2556. so a task that has been clocked for 16 hours will be displayed
  2557. as during 2 days in the clock display or in the clocktable.
  2558. See `org-effort-durations' on how to set effort durations
  2559. and `org-time-clocksum-format' for more on time clock formats."
  2560. :group 'org-time
  2561. :group 'org-clock
  2562. :version "24.3"
  2563. :type 'boolean)
  2564. (defcustom org-time-clocksum-fractional-format "%.2f"
  2565. "The format string used when creating CLOCKSUM lines,
  2566. or when Org mode generates a time duration, if
  2567. `org-time-clocksum-use-fractional' is enabled.
  2568. The value can be a single format string containing one
  2569. %-sequence, which will be filled with the number of hours as
  2570. a float.
  2571. Alternatively, the value can be a plist associating any of the
  2572. keys :years, :months, :weeks, :days, :hours or :minutes with
  2573. a format string. The time duration is formatted using the
  2574. largest time unit which gives a non-zero integer part. If all
  2575. specified formats have zero integer part, the smallest time unit
  2576. is used."
  2577. :group 'org-time
  2578. :type '(choice (string :tag "Format string")
  2579. (set (group :inline t (const :tag "Years" :years)
  2580. (string :tag "Format string"))
  2581. (group :inline t (const :tag "Months" :months)
  2582. (string :tag "Format string"))
  2583. (group :inline t (const :tag "Weeks" :weeks)
  2584. (string :tag "Format string"))
  2585. (group :inline t (const :tag "Days" :days)
  2586. (string :tag "Format string"))
  2587. (group :inline t (const :tag "Hours" :hours)
  2588. (string :tag "Format string"))
  2589. (group :inline t (const :tag "Minutes" :minutes)
  2590. (string :tag "Format string")))))
  2591. (defcustom org-deadline-warning-days 14
  2592. "Number of days before expiration during which a deadline becomes active.
  2593. This variable governs the display in sparse trees and in the agenda.
  2594. When 0 or negative, it means use this number (the absolute value of it)
  2595. even if a deadline has a different individual lead time specified.
  2596. Custom commands can set this variable in the options section."
  2597. :group 'org-time
  2598. :group 'org-agenda-daily/weekly
  2599. :type 'integer)
  2600. (defcustom org-scheduled-delay-days 0
  2601. "Number of days before a scheduled item becomes active.
  2602. This variable governs the display in sparse trees and in the agenda.
  2603. The default value (i.e. 0) means: don't delay scheduled item.
  2604. When negative, it means use this number (the absolute value of it)
  2605. even if a scheduled item has a different individual delay time
  2606. specified.
  2607. Custom commands can set this variable in the options section."
  2608. :group 'org-time
  2609. :group 'org-agenda-daily/weekly
  2610. :version "24.3"
  2611. :type 'integer)
  2612. (defcustom org-read-date-prefer-future t
  2613. "Non-nil means assume future for incomplete date input from user.
  2614. This affects the following situations:
  2615. 1. The user gives a month but not a year.
  2616. For example, if it is April and you enter \"feb 2\", this will be read
  2617. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2618. 2. The user gives a day, but no month.
  2619. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2620. read this as the third of *next* month. However, if you enter \"17\",
  2621. it will be considered as *this* month.
  2622. If you set this variable to the symbol `time', then also the following
  2623. will work:
  2624. 3. If the user gives a time.
  2625. If the time is before now, it will be interpreted as tomorrow.
  2626. Currently none of this works for ISO week specifications.
  2627. When this option is nil, the current day, month and year will always be
  2628. used as defaults.
  2629. See also `org-agenda-jump-prefer-future'."
  2630. :group 'org-time
  2631. :type '(choice
  2632. (const :tag "Never" nil)
  2633. (const :tag "Check month and day" t)
  2634. (const :tag "Check month, day, and time" time)))
  2635. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2636. "Should the agenda jump command prefer the future for incomplete dates?
  2637. The default is to do the same as configured in `org-read-date-prefer-future'.
  2638. But you can also set a deviating value here.
  2639. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2640. :group 'org-agenda
  2641. :group 'org-time
  2642. :version "24.1"
  2643. :type '(choice
  2644. (const :tag "Use org-read-date-prefer-future"
  2645. org-read-date-prefer-future)
  2646. (const :tag "Never" nil)
  2647. (const :tag "Always" t)))
  2648. (defcustom org-read-date-force-compatible-dates t
  2649. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2650. Depending on the system Emacs is running on, certain dates cannot
  2651. be represented with the type used internally to represent time.
  2652. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2653. correctly. Some systems allow for earlier dates, some for later,
  2654. some for both. One way to find out it to insert any date into an
  2655. Org buffer, putting the cursor on the year and hitting S-up and
  2656. S-down to test the range.
  2657. When this variable is set to t, the date/time prompt will not let
  2658. you specify dates outside the 1970-2037 range, so it is certain that
  2659. these dates will work in whatever version of Emacs you are
  2660. running, and also that you can move a file from one Emacs implementation
  2661. to another. WHenever Org is forcing the year for you, it will display
  2662. a message and beep.
  2663. When this variable is nil, Org will check if the date is
  2664. representable in the specific Emacs implementation you are using.
  2665. If not, it will force a year, usually the current year, and beep
  2666. to remind you. Currently this setting is not recommended because
  2667. the likelihood that you will open your Org files in an Emacs that
  2668. has limited date range is not negligible.
  2669. A workaround for this problem is to use diary sexp dates for time
  2670. stamps outside of this range."
  2671. :group 'org-time
  2672. :version "24.1"
  2673. :type 'boolean)
  2674. (defcustom org-read-date-display-live t
  2675. "Non-nil means display current interpretation of date prompt live.
  2676. This display will be in an overlay, in the minibuffer."
  2677. :group 'org-time
  2678. :type 'boolean)
  2679. (defcustom org-read-date-popup-calendar t
  2680. "Non-nil means pop up a calendar when prompting for a date.
  2681. In the calendar, the date can be selected with mouse-1. However, the
  2682. minibuffer will also be active, and you can simply enter the date as well.
  2683. When nil, only the minibuffer will be available."
  2684. :group 'org-time
  2685. :type 'boolean)
  2686. (if (fboundp 'defvaralias)
  2687. (defvaralias 'org-popup-calendar-for-date-prompt
  2688. 'org-read-date-popup-calendar))
  2689. (make-obsolete-variable
  2690. 'org-read-date-minibuffer-setup-hook
  2691. "Set `org-read-date-minibuffer-local-map' instead." "24.3")
  2692. (defcustom org-read-date-minibuffer-setup-hook nil
  2693. "Hook to be used to set up keys for the date/time interface.
  2694. Add key definitions to `minibuffer-local-map', which will be a
  2695. temporary copy.
  2696. WARNING: This option is obsolete, you should use
  2697. `org-read-date-minibuffer-local-map' to set up keys."
  2698. :group 'org-time
  2699. :type 'hook)
  2700. (defcustom org-extend-today-until 0
  2701. "The hour when your day really ends. Must be an integer.
  2702. This has influence for the following applications:
  2703. - When switching the agenda to \"today\". It it is still earlier than
  2704. the time given here, the day recognized as TODAY is actually yesterday.
  2705. - When a date is read from the user and it is still before the time given
  2706. here, the current date and time will be assumed to be yesterday, 23:59.
  2707. Also, timestamps inserted in capture templates follow this rule.
  2708. IMPORTANT: This is a feature whose implementation is and likely will
  2709. remain incomplete. Really, it is only here because past midnight seems to
  2710. be the favorite working time of John Wiegley :-)"
  2711. :group 'org-time
  2712. :type 'integer)
  2713. (defcustom org-use-effective-time nil
  2714. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2715. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2716. \"effective time\" of any timestamps between midnight and 8am will be
  2717. 23:59 of the previous day."
  2718. :group 'org-time
  2719. :version "24.1"
  2720. :type 'boolean)
  2721. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2722. "When non-nil, use the last clock out time for `org-todo'.
  2723. Note that this option has precedence over the combined use of
  2724. `org-use-effective-time' and `org-extend-today-until'."
  2725. :group 'org-time
  2726. ;; :version "24.3"
  2727. :type 'boolean)
  2728. (defcustom org-edit-timestamp-down-means-later nil
  2729. "Non-nil means S-down will increase the time in a time stamp.
  2730. When nil, S-up will increase."
  2731. :group 'org-time
  2732. :type 'boolean)
  2733. (defcustom org-calendar-follow-timestamp-change t
  2734. "Non-nil means make the calendar window follow timestamp changes.
  2735. When a timestamp is modified and the calendar window is visible, it will be
  2736. moved to the new date."
  2737. :group 'org-time
  2738. :type 'boolean)
  2739. (defgroup org-tags nil
  2740. "Options concerning tags in Org-mode."
  2741. :tag "Org Tags"
  2742. :group 'org)
  2743. (defcustom org-tag-alist nil
  2744. "List of tags allowed in Org-mode files.
  2745. When this list is nil, Org-mode will base TAG input on what is already in the
  2746. buffer.
  2747. The value of this variable is an alist, the car of each entry must be a
  2748. keyword as a string, the cdr may be a character that is used to select
  2749. that tag through the fast-tag-selection interface.
  2750. See the manual for details."
  2751. :group 'org-tags
  2752. :type '(repeat
  2753. (choice
  2754. (cons (string :tag "Tag name")
  2755. (character :tag "Access char"))
  2756. (list :tag "Start radio group"
  2757. (const :startgroup)
  2758. (option (string :tag "Group description")))
  2759. (list :tag "End radio group"
  2760. (const :endgroup)
  2761. (option (string :tag "Group description")))
  2762. (const :tag "New line" (:newline)))))
  2763. (defcustom org-tag-persistent-alist nil
  2764. "List of tags that will always appear in all Org-mode files.
  2765. This is in addition to any in buffer settings or customizations
  2766. of `org-tag-alist'.
  2767. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2768. The value of this variable is an alist, the car of each entry must be a
  2769. keyword as a string, the cdr may be a character that is used to select
  2770. that tag through the fast-tag-selection interface.
  2771. See the manual for details.
  2772. To disable these tags on a per-file basis, insert anywhere in the file:
  2773. #+STARTUP: noptag"
  2774. :group 'org-tags
  2775. :type '(repeat
  2776. (choice
  2777. (cons (string :tag "Tag name")
  2778. (character :tag "Access char"))
  2779. (const :tag "Start radio group" (:startgroup))
  2780. (const :tag "End radio group" (:endgroup))
  2781. (const :tag "New line" (:newline)))))
  2782. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2783. "If non-nil, always offer completion for all tags of all agenda files.
  2784. Instead of customizing this variable directly, you might want to
  2785. set it locally for capture buffers, because there no list of
  2786. tags in that file can be created dynamically (there are none).
  2787. (add-hook 'org-capture-mode-hook
  2788. (lambda ()
  2789. (set (make-local-variable
  2790. 'org-complete-tags-always-offer-all-agenda-tags)
  2791. t)))"
  2792. :group 'org-tags
  2793. :version "24.1"
  2794. :type 'boolean)
  2795. (defvar org-file-tags nil
  2796. "List of tags that can be inherited by all entries in the file.
  2797. The tags will be inherited if the variable `org-use-tag-inheritance'
  2798. says they should be.
  2799. This variable is populated from #+FILETAGS lines.")
  2800. (defcustom org-use-fast-tag-selection 'auto
  2801. "Non-nil means use fast tag selection scheme.
  2802. This is a special interface to select and deselect tags with single keys.
  2803. When nil, fast selection is never used.
  2804. When the symbol `auto', fast selection is used if and only if selection
  2805. characters for tags have been configured, either through the variable
  2806. `org-tag-alist' or through a #+TAGS line in the buffer.
  2807. When t, fast selection is always used and selection keys are assigned
  2808. automatically if necessary."
  2809. :group 'org-tags
  2810. :type '(choice
  2811. (const :tag "Always" t)
  2812. (const :tag "Never" nil)
  2813. (const :tag "When selection characters are configured" 'auto)))
  2814. (defcustom org-fast-tag-selection-single-key nil
  2815. "Non-nil means fast tag selection exits after first change.
  2816. When nil, you have to press RET to exit it.
  2817. During fast tag selection, you can toggle this flag with `C-c'.
  2818. This variable can also have the value `expert'. In this case, the window
  2819. displaying the tags menu is not even shown, until you press C-c again."
  2820. :group 'org-tags
  2821. :type '(choice
  2822. (const :tag "No" nil)
  2823. (const :tag "Yes" t)
  2824. (const :tag "Expert" expert)))
  2825. (defvar org-fast-tag-selection-include-todo nil
  2826. "Non-nil means fast tags selection interface will also offer TODO states.
  2827. This is an undocumented feature, you should not rely on it.")
  2828. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2829. "The column to which tags should be indented in a headline.
  2830. If this number is positive, it specifies the column. If it is negative,
  2831. it means that the tags should be flushright to that column. For example,
  2832. -80 works well for a normal 80 character screen.
  2833. When 0, place tags directly after headline text, with only one space in
  2834. between."
  2835. :group 'org-tags
  2836. :type 'integer)
  2837. (defcustom org-auto-align-tags t
  2838. "Non-nil keeps tags aligned when modifying headlines.
  2839. Some operations (i.e. demoting) change the length of a headline and
  2840. therefore shift the tags around. With this option turned on, after
  2841. each such operation the tags are again aligned to `org-tags-column'."
  2842. :group 'org-tags
  2843. :type 'boolean)
  2844. (defcustom org-use-tag-inheritance t
  2845. "Non-nil means tags in levels apply also for sublevels.
  2846. When nil, only the tags directly given in a specific line apply there.
  2847. This may also be a list of tags that should be inherited, or a regexp that
  2848. matches tags that should be inherited. Additional control is possible
  2849. with the variable `org-tags-exclude-from-inheritance' which gives an
  2850. explicit list of tags to be excluded from inheritance, even if the value of
  2851. `org-use-tag-inheritance' would select it for inheritance.
  2852. If this option is t, a match early-on in a tree can lead to a large
  2853. number of matches in the subtree when constructing the agenda or creating
  2854. a sparse tree. If you only want to see the first match in a tree during
  2855. a search, check out the variable `org-tags-match-list-sublevels'."
  2856. :group 'org-tags
  2857. :type '(choice
  2858. (const :tag "Not" nil)
  2859. (const :tag "Always" t)
  2860. (repeat :tag "Specific tags" (string :tag "Tag"))
  2861. (regexp :tag "Tags matched by regexp")))
  2862. (defcustom org-tags-exclude-from-inheritance nil
  2863. "List of tags that should never be inherited.
  2864. This is a way to exclude a few tags from inheritance. For way to do
  2865. the opposite, to actively allow inheritance for selected tags,
  2866. see the variable `org-use-tag-inheritance'."
  2867. :group 'org-tags
  2868. :type '(repeat (string :tag "Tag")))
  2869. (defun org-tag-inherit-p (tag)
  2870. "Check if TAG is one that should be inherited."
  2871. (cond
  2872. ((member tag org-tags-exclude-from-inheritance) nil)
  2873. ((eq org-use-tag-inheritance t) t)
  2874. ((not org-use-tag-inheritance) nil)
  2875. ((stringp org-use-tag-inheritance)
  2876. (string-match org-use-tag-inheritance tag))
  2877. ((listp org-use-tag-inheritance)
  2878. (member tag org-use-tag-inheritance))
  2879. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2880. (defcustom org-tags-match-list-sublevels t
  2881. "Non-nil means list also sublevels of headlines matching a search.
  2882. This variable applies to tags/property searches, and also to stuck
  2883. projects because this search is based on a tags match as well.
  2884. When set to the symbol `indented', sublevels are indented with
  2885. leading dots.
  2886. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2887. the sublevels of a headline matching a tag search often also match
  2888. the same search. Listing all of them can create very long lists.
  2889. Setting this variable to nil causes subtrees of a match to be skipped.
  2890. This variable is semi-obsolete and probably should always be true. It
  2891. is better to limit inheritance to certain tags using the variables
  2892. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2893. :group 'org-tags
  2894. :type '(choice
  2895. (const :tag "No, don't list them" nil)
  2896. (const :tag "Yes, do list them" t)
  2897. (const :tag "List them, indented with leading dots" indented)))
  2898. (defcustom org-tags-sort-function nil
  2899. "When set, tags are sorted using this function as a comparator."
  2900. :group 'org-tags
  2901. :type '(choice
  2902. (const :tag "No sorting" nil)
  2903. (const :tag "Alphabetical" string<)
  2904. (const :tag "Reverse alphabetical" string>)
  2905. (function :tag "Custom function" nil)))
  2906. (defvar org-tags-history nil
  2907. "History of minibuffer reads for tags.")
  2908. (defvar org-last-tags-completion-table nil
  2909. "The last used completion table for tags.")
  2910. (defvar org-after-tags-change-hook nil
  2911. "Hook that is run after the tags in a line have changed.")
  2912. (defgroup org-properties nil
  2913. "Options concerning properties in Org-mode."
  2914. :tag "Org Properties"
  2915. :group 'org)
  2916. (defcustom org-property-format "%-10s %s"
  2917. "How property key/value pairs should be formatted by `indent-line'.
  2918. When `indent-line' hits a property definition, it will format the line
  2919. according to this format, mainly to make sure that the values are
  2920. lined-up with respect to each other."
  2921. :group 'org-properties
  2922. :type 'string)
  2923. (defcustom org-properties-postprocess-alist nil
  2924. "Alist of properties and functions to adjust inserted values.
  2925. Elements of this alist must be of the form
  2926. ([string] [function])
  2927. where [string] must be a property name and [function] must be a
  2928. lambda expression: this lambda expression must take one argument,
  2929. the value to adjust, and return the new value as a string.
  2930. For example, this element will allow the property \"Remaining\"
  2931. to be updated wrt the relation between the \"Effort\" property
  2932. and the clock summary:
  2933. ((\"Remaining\" (lambda(value)
  2934. (let ((clocksum (org-clock-sum-current-item))
  2935. (effort (org-duration-string-to-minutes
  2936. (org-entry-get (point) \"Effort\"))))
  2937. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2938. :group 'org-properties
  2939. :version "24.1"
  2940. :type '(alist :key-type (string :tag "Property")
  2941. :value-type (function :tag "Function")))
  2942. (defcustom org-use-property-inheritance nil
  2943. "Non-nil means properties apply also for sublevels.
  2944. This setting is chiefly used during property searches. Turning it on can
  2945. cause significant overhead when doing a search, which is why it is not
  2946. on by default.
  2947. When nil, only the properties directly given in the current entry count.
  2948. When t, every property is inherited. The value may also be a list of
  2949. properties that should have inheritance, or a regular expression matching
  2950. properties that should be inherited.
  2951. However, note that some special properties use inheritance under special
  2952. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2953. and the properties ending in \"_ALL\" when they are used as descriptor
  2954. for valid values of a property.
  2955. Note for programmers:
  2956. When querying an entry with `org-entry-get', you can control if inheritance
  2957. should be used. By default, `org-entry-get' looks only at the local
  2958. properties. You can request inheritance by setting the inherit argument
  2959. to t (to force inheritance) or to `selective' (to respect the setting
  2960. in this variable)."
  2961. :group 'org-properties
  2962. :type '(choice
  2963. (const :tag "Not" nil)
  2964. (const :tag "Always" t)
  2965. (repeat :tag "Specific properties" (string :tag "Property"))
  2966. (regexp :tag "Properties matched by regexp")))
  2967. (defun org-property-inherit-p (property)
  2968. "Check if PROPERTY is one that should be inherited."
  2969. (cond
  2970. ((eq org-use-property-inheritance t) t)
  2971. ((not org-use-property-inheritance) nil)
  2972. ((stringp org-use-property-inheritance)
  2973. (string-match org-use-property-inheritance property))
  2974. ((listp org-use-property-inheritance)
  2975. (member property org-use-property-inheritance))
  2976. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2977. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2978. "The default column format, if no other format has been defined.
  2979. This variable can be set on the per-file basis by inserting a line
  2980. #+COLUMNS: %25ITEM ....."
  2981. :group 'org-properties
  2982. :type 'string)
  2983. (defcustom org-columns-ellipses ".."
  2984. "The ellipses to be used when a field in column view is truncated.
  2985. When this is the empty string, as many characters as possible are shown,
  2986. but then there will be no visual indication that the field has been truncated.
  2987. When this is a string of length N, the last N characters of a truncated
  2988. field are replaced by this string. If the column is narrower than the
  2989. ellipses string, only part of the ellipses string will be shown."
  2990. :group 'org-properties
  2991. :type 'string)
  2992. (defcustom org-columns-modify-value-for-display-function nil
  2993. "Function that modifies values for display in column view.
  2994. For example, it can be used to cut out a certain part from a time stamp.
  2995. The function must take 2 arguments:
  2996. column-title The title of the column (*not* the property name)
  2997. value The value that should be modified.
  2998. The function should return the value that should be displayed,
  2999. or nil if the normal value should be used."
  3000. :group 'org-properties
  3001. :type 'function)
  3002. (defcustom org-effort-property "Effort"
  3003. "The property that is being used to keep track of effort estimates.
  3004. Effort estimates given in this property need to have the format H:MM."
  3005. :group 'org-properties
  3006. :group 'org-progress
  3007. :type '(string :tag "Property"))
  3008. (defconst org-global-properties-fixed
  3009. '(("VISIBILITY_ALL" . "folded children content all")
  3010. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3011. "List of property/value pairs that can be inherited by any entry.
  3012. These are fixed values, for the preset properties. The user variable
  3013. that can be used to add to this list is `org-global-properties'.
  3014. The entries in this list are cons cells where the car is a property
  3015. name and cdr is a string with the value. If the value represents
  3016. multiple items like an \"_ALL\" property, separate the items by
  3017. spaces.")
  3018. (defcustom org-global-properties nil
  3019. "List of property/value pairs that can be inherited by any entry.
  3020. This list will be combined with the constant `org-global-properties-fixed'.
  3021. The entries in this list are cons cells where the car is a property
  3022. name and cdr is a string with the value.
  3023. You can set buffer-local values for the same purpose in the variable
  3024. `org-file-properties' this by adding lines like
  3025. #+PROPERTY: NAME VALUE"
  3026. :group 'org-properties
  3027. :type '(repeat
  3028. (cons (string :tag "Property")
  3029. (string :tag "Value"))))
  3030. (defvar org-file-properties nil
  3031. "List of property/value pairs that can be inherited by any entry.
  3032. Valid for the current buffer.
  3033. This variable is populated from #+PROPERTY lines.")
  3034. (make-variable-buffer-local 'org-file-properties)
  3035. (defgroup org-agenda nil
  3036. "Options concerning agenda views in Org-mode."
  3037. :tag "Org Agenda"
  3038. :group 'org)
  3039. (defvar org-category nil
  3040. "Variable used by org files to set a category for agenda display.
  3041. Such files should use a file variable to set it, for example
  3042. # -*- mode: org; org-category: \"ELisp\"
  3043. or contain a special line
  3044. #+CATEGORY: ELisp
  3045. If the file does not specify a category, then file's base name
  3046. is used instead.")
  3047. (make-variable-buffer-local 'org-category)
  3048. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3049. (defcustom org-agenda-files nil
  3050. "The files to be used for agenda display.
  3051. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3052. \\[org-remove-file]. You can also use customize to edit the list.
  3053. If an entry is a directory, all files in that directory that are matched by
  3054. `org-agenda-file-regexp' will be part of the file list.
  3055. If the value of the variable is not a list but a single file name, then
  3056. the list of agenda files is actually stored and maintained in that file, one
  3057. agenda file per line. In this file paths can be given relative to
  3058. `org-directory'. Tilde expansion and environment variable substitution
  3059. are also made."
  3060. :group 'org-agenda
  3061. :type '(choice
  3062. (repeat :tag "List of files and directories" file)
  3063. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3064. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3065. "Regular expression to match files for `org-agenda-files'.
  3066. If any element in the list in that variable contains a directory instead
  3067. of a normal file, all files in that directory that are matched by this
  3068. regular expression will be included."
  3069. :group 'org-agenda
  3070. :type 'regexp)
  3071. (defcustom org-agenda-text-search-extra-files nil
  3072. "List of extra files to be searched by text search commands.
  3073. These files will be search in addition to the agenda files by the
  3074. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3075. Note that these files will only be searched for text search commands,
  3076. not for the other agenda views like todo lists, tag searches or the weekly
  3077. agenda. This variable is intended to list notes and possibly archive files
  3078. that should also be searched by these two commands.
  3079. In fact, if the first element in the list is the symbol `agenda-archives',
  3080. than all archive files of all agenda files will be added to the search
  3081. scope."
  3082. :group 'org-agenda
  3083. :type '(set :greedy t
  3084. (const :tag "Agenda Archives" agenda-archives)
  3085. (repeat :inline t (file))))
  3086. (if (fboundp 'defvaralias)
  3087. (defvaralias 'org-agenda-multi-occur-extra-files
  3088. 'org-agenda-text-search-extra-files))
  3089. (defcustom org-agenda-skip-unavailable-files nil
  3090. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3091. A nil value means to remove them, after a query, from the list."
  3092. :group 'org-agenda
  3093. :type 'boolean)
  3094. (defcustom org-calendar-to-agenda-key [?c]
  3095. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3096. The command `org-calendar-goto-agenda' will be bound to this key. The
  3097. default is the character `c' because then `c' can be used to switch back and
  3098. forth between agenda and calendar."
  3099. :group 'org-agenda
  3100. :type 'sexp)
  3101. (defcustom org-calendar-insert-diary-entry-key [?i]
  3102. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3103. This option is irrelevant until `org-agenda-diary-file' has been configured
  3104. to point to an Org-mode file. When that is the case, the command
  3105. `org-agenda-diary-entry' will be bound to the key given here, by default
  3106. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3107. if you want to continue doing this, you need to change this to a different
  3108. key."
  3109. :group 'org-agenda
  3110. :type 'sexp)
  3111. (defcustom org-agenda-diary-file 'diary-file
  3112. "File to which to add new entries with the `i' key in agenda and calendar.
  3113. When this is the symbol `diary-file', the functionality in the Emacs
  3114. calendar will be used to add entries to the `diary-file'. But when this
  3115. points to a file, `org-agenda-diary-entry' will be used instead."
  3116. :group 'org-agenda
  3117. :type '(choice
  3118. (const :tag "The standard Emacs diary file" diary-file)
  3119. (file :tag "Special Org file diary entries")))
  3120. (eval-after-load "calendar"
  3121. '(progn
  3122. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3123. 'org-calendar-goto-agenda)
  3124. (add-hook 'calendar-mode-hook
  3125. (lambda ()
  3126. (unless (eq org-agenda-diary-file 'diary-file)
  3127. (define-key calendar-mode-map
  3128. org-calendar-insert-diary-entry-key
  3129. 'org-agenda-diary-entry))))))
  3130. (defgroup org-latex nil
  3131. "Options for embedding LaTeX code into Org-mode."
  3132. :tag "Org LaTeX"
  3133. :group 'org)
  3134. (defcustom org-format-latex-options
  3135. '(:foreground default :background default :scale 1.0
  3136. :html-foreground "Black" :html-background "Transparent"
  3137. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3138. "Options for creating images from LaTeX fragments.
  3139. This is a property list with the following properties:
  3140. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3141. `default' means use the foreground of the default face.
  3142. `auto' means use the foreground from the text face.
  3143. :background the background color, or \"Transparent\".
  3144. `default' means use the background of the default face.
  3145. `auto' means use the background from the text face.
  3146. :scale a scaling factor for the size of the images, to get more pixels
  3147. :html-foreground, :html-background, :html-scale
  3148. the same numbers for HTML export.
  3149. :matchers a list indicating which matchers should be used to
  3150. find LaTeX fragments. Valid members of this list are:
  3151. \"begin\" find environments
  3152. \"$1\" find single characters surrounded by $.$
  3153. \"$\" find math expressions surrounded by $...$
  3154. \"$$\" find math expressions surrounded by $$....$$
  3155. \"\\(\" find math expressions surrounded by \\(...\\)
  3156. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3157. :group 'org-latex
  3158. :type 'plist)
  3159. (defcustom org-format-latex-signal-error t
  3160. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3161. When nil, just push out a message."
  3162. :group 'org-latex
  3163. :version "24.1"
  3164. :type 'boolean)
  3165. (defcustom org-latex-to-mathml-jar-file nil
  3166. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3167. Use this to specify additional executable file say a jar file.
  3168. When using MathToWeb as the converter, specify the full-path to
  3169. your mathtoweb.jar file."
  3170. :group 'org-latex
  3171. :version "24.1"
  3172. :type '(choice
  3173. (const :tag "None" nil)
  3174. (file :tag "JAR file" :must-match t)))
  3175. (defcustom org-latex-to-mathml-convert-command nil
  3176. "Command to convert LaTeX fragments to MathML.
  3177. Replace format-specifiers in the command as noted below and use
  3178. `shell-command' to convert LaTeX to MathML.
  3179. %j: Executable file in fully expanded form as specified by
  3180. `org-latex-to-mathml-jar-file'.
  3181. %I: Input LaTeX file in fully expanded form
  3182. %o: Output MathML file
  3183. This command is used by `org-create-math-formula'.
  3184. When using MathToWeb as the converter, set this to
  3185. \"java -jar %j -unicode -force -df %o %I\"."
  3186. :group 'org-latex
  3187. :version "24.1"
  3188. :type '(choice
  3189. (const :tag "None" nil)
  3190. (string :tag "\nShell command")))
  3191. (defcustom org-latex-create-formula-image-program 'dvipng
  3192. "Program to convert LaTeX fragments with.
  3193. dvipng Process the LaTeX fragments to dvi file, then convert
  3194. dvi files to png files using dvipng.
  3195. This will also include processing of non-math environments.
  3196. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3197. to convert pdf files to png files"
  3198. :group 'org-latex
  3199. :version "24.1"
  3200. :type '(choice
  3201. (const :tag "dvipng" dvipng)
  3202. (const :tag "imagemagick" imagemagick)))
  3203. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3204. "Path to store latex preview images.
  3205. A relative path here creates many directories relative to the
  3206. processed org files paths. An absolute path puts all preview
  3207. images at the same place."
  3208. :group 'org-latex
  3209. :version "24.3"
  3210. :type 'string)
  3211. (defun org-format-latex-mathml-available-p ()
  3212. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3213. (save-match-data
  3214. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3215. org-latex-to-mathml-convert-command)
  3216. (let ((executable (car (split-string
  3217. org-latex-to-mathml-convert-command))))
  3218. (when (executable-find executable)
  3219. (if (string-match
  3220. "%j" org-latex-to-mathml-convert-command)
  3221. (file-readable-p org-latex-to-mathml-jar-file)
  3222. t))))))
  3223. (defcustom org-format-latex-header "\\documentclass{article}
  3224. \\usepackage[usenames]{color}
  3225. \\usepackage{amsmath}
  3226. \\usepackage[mathscr]{eucal}
  3227. \\pagestyle{empty} % do not remove
  3228. \[PACKAGES]
  3229. \[DEFAULT-PACKAGES]
  3230. % The settings below are copied from fullpage.sty
  3231. \\setlength{\\textwidth}{\\paperwidth}
  3232. \\addtolength{\\textwidth}{-3cm}
  3233. \\setlength{\\oddsidemargin}{1.5cm}
  3234. \\addtolength{\\oddsidemargin}{-2.54cm}
  3235. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3236. \\setlength{\\textheight}{\\paperheight}
  3237. \\addtolength{\\textheight}{-\\headheight}
  3238. \\addtolength{\\textheight}{-\\headsep}
  3239. \\addtolength{\\textheight}{-\\footskip}
  3240. \\addtolength{\\textheight}{-3cm}
  3241. \\setlength{\\topmargin}{1.5cm}
  3242. \\addtolength{\\topmargin}{-2.54cm}"
  3243. "The document header used for processing LaTeX fragments.
  3244. It is imperative that this header make sure that no page number
  3245. appears on the page. The package defined in the variables
  3246. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3247. will either replace the placeholder \"[PACKAGES]\" in this
  3248. header, or they will be appended."
  3249. :group 'org-latex
  3250. :type 'string)
  3251. (defun org-set-packages-alist (var val)
  3252. "Set the packages alist and make sure it has 3 elements per entry."
  3253. (set var (mapcar (lambda (x)
  3254. (if (and (consp x) (= (length x) 2))
  3255. (list (car x) (nth 1 x) t)
  3256. x))
  3257. val)))
  3258. (defun org-get-packages-alist (var)
  3259. "Get the packages alist and make sure it has 3 elements per entry."
  3260. (mapcar (lambda (x)
  3261. (if (and (consp x) (= (length x) 2))
  3262. (list (car x) (nth 1 x) t)
  3263. x))
  3264. (default-value var)))
  3265. (defcustom org-latex-default-packages-alist
  3266. '(("AUTO" "inputenc" t)
  3267. ("T1" "fontenc" t)
  3268. ("" "fixltx2e" nil)
  3269. ("" "graphicx" t)
  3270. ("" "longtable" nil)
  3271. ("" "float" nil)
  3272. ("" "wrapfig" nil)
  3273. ("" "soul" t)
  3274. ("" "textcomp" t)
  3275. ("" "marvosym" t)
  3276. ("" "wasysym" t)
  3277. ("" "latexsym" t)
  3278. ("" "amssymb" t)
  3279. ("" "hyperref" nil)
  3280. "\\tolerance=1000")
  3281. "Alist of default packages to be inserted in the header.
  3282. Change this only if one of the packages here causes an
  3283. incompatibility with another package you are using.
  3284. The packages in this list are needed by one part or another of
  3285. Org mode to function properly:
  3286. - inputenc, fontenc: for basic font and character selection
  3287. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3288. symbols used for interpreting the entities in `org-entities'.
  3289. You can skip some of these packages if you don't use any of the
  3290. symbols in it.
  3291. - graphicx: for including images
  3292. - float, wrapfig: for figure placement
  3293. - longtable: for long tables
  3294. - hyperref: for cross references
  3295. Therefore you should not modify this variable unless you know
  3296. what you are doing. The one reason to change it anyway is that
  3297. you might be loading some other package that conflicts with one
  3298. of the default packages. Each cell is of the format
  3299. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3300. the package also needs to be included when compiling LaTeX
  3301. snippets into images for inclusion into non-LaTeX output."
  3302. :group 'org-latex
  3303. :group 'org-export-latex
  3304. :set 'org-set-packages-alist
  3305. :get 'org-get-packages-alist
  3306. :version "24.1"
  3307. :type '(repeat
  3308. (choice
  3309. (list :tag "options/package pair"
  3310. (string :tag "options")
  3311. (string :tag "package")
  3312. (boolean :tag "Snippet"))
  3313. (string :tag "A line of LaTeX"))))
  3314. (defcustom org-latex-packages-alist nil
  3315. "Alist of packages to be inserted in every LaTeX header.
  3316. These will be inserted after `org-latex-default-packages-alist'.
  3317. Each cell is of the format:
  3318. \(\"options\" \"package\" snippet-flag)
  3319. SNIPPET-FLAG, when t, indicates that this package is also needed
  3320. when turning LaTeX snippets into images for inclusion into
  3321. non-LaTeX output.
  3322. Make sure that you only list packages here which:
  3323. - you want in every file
  3324. - do not conflict with the setup in `org-format-latex-header'.
  3325. - do not conflict with the default packages in
  3326. `org-latex-default-packages-alist'."
  3327. :group 'org-latex
  3328. :group 'org-export-latex
  3329. :set 'org-set-packages-alist
  3330. :get 'org-get-packages-alist
  3331. :type '(repeat
  3332. (choice
  3333. (list :tag "options/package pair"
  3334. (string :tag "options")
  3335. (string :tag "package")
  3336. (boolean :tag "Snippet"))
  3337. (string :tag "A line of LaTeX"))))
  3338. (defgroup org-appearance nil
  3339. "Settings for Org-mode appearance."
  3340. :tag "Org Appearance"
  3341. :group 'org)
  3342. (defcustom org-level-color-stars-only nil
  3343. "Non-nil means fontify only the stars in each headline.
  3344. When nil, the entire headline is fontified.
  3345. Changing it requires restart of `font-lock-mode' to become effective
  3346. also in regions already fontified."
  3347. :group 'org-appearance
  3348. :type 'boolean)
  3349. (defcustom org-hide-leading-stars nil
  3350. "Non-nil means hide the first N-1 stars in a headline.
  3351. This works by using the face `org-hide' for these stars. This
  3352. face is white for a light background, and black for a dark
  3353. background. You may have to customize the face `org-hide' to
  3354. make this work.
  3355. Changing it requires restart of `font-lock-mode' to become effective
  3356. also in regions already fontified.
  3357. You may also set this on a per-file basis by adding one of the following
  3358. lines to the buffer:
  3359. #+STARTUP: hidestars
  3360. #+STARTUP: showstars"
  3361. :group 'org-appearance
  3362. :type 'boolean)
  3363. (defcustom org-hidden-keywords nil
  3364. "List of symbols corresponding to keywords to be hidden the org buffer.
  3365. For example, a value '(title) for this list will make the document's title
  3366. appear in the buffer without the initial #+TITLE: keyword."
  3367. :group 'org-appearance
  3368. :version "24.1"
  3369. :type '(set (const :tag "#+AUTHOR" author)
  3370. (const :tag "#+DATE" date)
  3371. (const :tag "#+EMAIL" email)
  3372. (const :tag "#+TITLE" title)))
  3373. (defcustom org-custom-properties nil
  3374. "List of properties (as strings) with a special meaning.
  3375. The default use of these custom properties is to let the user
  3376. hide them with `org-toggle-custom-properties-visibility'."
  3377. :group 'org-properties
  3378. :group 'org-appearance
  3379. :version "24.3"
  3380. :type '(repeat (string :tag "Property Name")))
  3381. (defcustom org-fontify-done-headline nil
  3382. "Non-nil means change the face of a headline if it is marked DONE.
  3383. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3384. When this is non-nil, the headline after the keyword is set to the
  3385. `org-headline-done' as an additional indication."
  3386. :group 'org-appearance
  3387. :type 'boolean)
  3388. (defcustom org-fontify-emphasized-text t
  3389. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3390. Changing this variable requires a restart of Emacs to take effect."
  3391. :group 'org-appearance
  3392. :type 'boolean)
  3393. (defcustom org-fontify-whole-heading-line nil
  3394. "Non-nil means fontify the whole line for headings.
  3395. This is useful when setting a background color for the
  3396. org-level-* faces."
  3397. :group 'org-appearance
  3398. :type 'boolean)
  3399. (defcustom org-highlight-latex-and-related nil
  3400. "Non-nil means highlight LaTeX related syntax in the buffer.
  3401. When non nil, the value should be a list containing any of the
  3402. following symbols:
  3403. `latex' Highlight LaTeX snippets and environments.
  3404. `script' Highlight subscript and superscript.
  3405. `entities' Highlight entities."
  3406. :group 'org-appearance
  3407. :type '(choice
  3408. (const :tag "No highlighting" nil)
  3409. (set :greedy t :tag "Highlight"
  3410. (const :tag "LaTeX snippets and environments" latex)
  3411. (const :tag "Subscript and superscript" script)
  3412. (const :tag "Entities" entities))))
  3413. (defcustom org-hide-emphasis-markers nil
  3414. "Non-nil mean font-lock should hide the emphasis marker characters."
  3415. :group 'org-appearance
  3416. :type 'boolean)
  3417. (defcustom org-pretty-entities nil
  3418. "Non-nil means show entities as UTF8 characters.
  3419. When nil, the \\name form remains in the buffer."
  3420. :group 'org-appearance
  3421. :version "24.1"
  3422. :type 'boolean)
  3423. (defcustom org-pretty-entities-include-sub-superscripts t
  3424. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3425. :group 'org-appearance
  3426. :version "24.1"
  3427. :type 'boolean)
  3428. (defvar org-emph-re nil
  3429. "Regular expression for matching emphasis.
  3430. After a match, the match groups contain these elements:
  3431. 0 The match of the full regular expression, including the characters
  3432. before and after the proper match
  3433. 1 The character before the proper match, or empty at beginning of line
  3434. 2 The proper match, including the leading and trailing markers
  3435. 3 The leading marker like * or /, indicating the type of highlighting
  3436. 4 The text between the emphasis markers, not including the markers
  3437. 5 The character after the match, empty at the end of a line")
  3438. (defvar org-verbatim-re nil
  3439. "Regular expression for matching verbatim text.")
  3440. (defvar org-emphasis-regexp-components) ; defined just below
  3441. (defvar org-emphasis-alist) ; defined just below
  3442. (defun org-set-emph-re (var val)
  3443. "Set variable and compute the emphasis regular expression."
  3444. (set var val)
  3445. (when (and (boundp 'org-emphasis-alist)
  3446. (boundp 'org-emphasis-regexp-components)
  3447. org-emphasis-alist org-emphasis-regexp-components)
  3448. (let* ((e org-emphasis-regexp-components)
  3449. (pre (car e))
  3450. (post (nth 1 e))
  3451. (border (nth 2 e))
  3452. (body (nth 3 e))
  3453. (nl (nth 4 e))
  3454. (body1 (concat body "*?"))
  3455. (markers (mapconcat 'car org-emphasis-alist ""))
  3456. (vmarkers (mapconcat
  3457. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3458. org-emphasis-alist "")))
  3459. ;; make sure special characters appear at the right position in the class
  3460. (if (string-match "\\^" markers)
  3461. (setq markers (concat (replace-match "" t t markers) "^")))
  3462. (if (string-match "-" markers)
  3463. (setq markers (concat (replace-match "" t t markers) "-")))
  3464. (if (string-match "\\^" vmarkers)
  3465. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3466. (if (string-match "-" vmarkers)
  3467. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3468. (if (> nl 0)
  3469. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3470. (int-to-string nl) "\\}")))
  3471. ;; Make the regexp
  3472. (setq org-emph-re
  3473. (concat "\\([" pre "]\\|^\\)"
  3474. "\\("
  3475. "\\([" markers "]\\)"
  3476. "\\("
  3477. "[^" border "]\\|"
  3478. "[^" border "]"
  3479. body1
  3480. "[^" border "]"
  3481. "\\)"
  3482. "\\3\\)"
  3483. "\\([" post "]\\|$\\)"))
  3484. (setq org-verbatim-re
  3485. (concat "\\([" pre "]\\|^\\)"
  3486. "\\("
  3487. "\\([" vmarkers "]\\)"
  3488. "\\("
  3489. "[^" border "]\\|"
  3490. "[^" border "]"
  3491. body1
  3492. "[^" border "]"
  3493. "\\)"
  3494. "\\3\\)"
  3495. "\\([" post "]\\|$\\)")))))
  3496. (defcustom org-emphasis-regexp-components
  3497. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3498. "Components used to build the regular expression for emphasis.
  3499. This is a list with five entries. Terminology: In an emphasis string
  3500. like \" *strong word* \", we call the initial space PREMATCH, the final
  3501. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3502. and \"trong wor\" is the body. The different components in this variable
  3503. specify what is allowed/forbidden in each part:
  3504. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3505. post Chars allowed as postmatch. End of line will be allowed too.
  3506. border The chars *forbidden* as border characters.
  3507. body-regexp A regexp like \".\" to match a body character. Don't use
  3508. non-shy groups here, and don't allow newline here.
  3509. newline The maximum number of newlines allowed in an emphasis exp.
  3510. Use customize to modify this, or restart Emacs after changing it."
  3511. :group 'org-appearance
  3512. :set 'org-set-emph-re
  3513. :type '(list
  3514. (sexp :tag "Allowed chars in pre ")
  3515. (sexp :tag "Allowed chars in post ")
  3516. (sexp :tag "Forbidden chars in border ")
  3517. (sexp :tag "Regexp for body ")
  3518. (integer :tag "number of newlines allowed")
  3519. (option (boolean :tag "Please ignore this button"))))
  3520. (defcustom org-emphasis-alist
  3521. `(("*" bold "<b>" "</b>")
  3522. ("/" italic "<i>" "</i>")
  3523. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3524. ("=" org-code "<code>" "</code>" verbatim)
  3525. ("~" org-verbatim "<code>" "</code>" verbatim)
  3526. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3527. "<del>" "</del>")
  3528. )
  3529. "Special syntax for emphasized text.
  3530. Text starting and ending with a special character will be emphasized, for
  3531. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3532. characters, the face to be used by font-lock for highlighting in Org-mode
  3533. Emacs buffers, and the HTML tags to be used for this.
  3534. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3535. Use customize to modify this, or restart Emacs after changing it."
  3536. :group 'org-appearance
  3537. :set 'org-set-emph-re
  3538. :type '(repeat
  3539. (list
  3540. (string :tag "Marker character")
  3541. (choice
  3542. (face :tag "Font-lock-face")
  3543. (plist :tag "Face property list"))
  3544. (string :tag "HTML start tag")
  3545. (string :tag "HTML end tag")
  3546. (option (const verbatim)))))
  3547. (defvar org-protecting-blocks
  3548. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3549. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3550. This is needed for font-lock setup.")
  3551. ;;; Miscellaneous options
  3552. (defgroup org-completion nil
  3553. "Completion in Org-mode."
  3554. :tag "Org Completion"
  3555. :group 'org)
  3556. (defcustom org-completion-use-ido nil
  3557. "Non-nil means use ido completion wherever possible.
  3558. Note that `ido-mode' must be active for this variable to be relevant.
  3559. If you decide to turn this variable on, you might well want to turn off
  3560. `org-outline-path-complete-in-steps'.
  3561. See also `org-completion-use-iswitchb'."
  3562. :group 'org-completion
  3563. :type 'boolean)
  3564. (defcustom org-completion-use-iswitchb nil
  3565. "Non-nil means use iswitchb completion wherever possible.
  3566. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3567. If you decide to turn this variable on, you might well want to turn off
  3568. `org-outline-path-complete-in-steps'.
  3569. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3570. :group 'org-completion
  3571. :type 'boolean)
  3572. (defcustom org-completion-fallback-command 'hippie-expand
  3573. "The expansion command called by \\[pcomplete] in normal context.
  3574. Normal means, no org-mode-specific context."
  3575. :group 'org-completion
  3576. :type 'function)
  3577. ;;; Functions and variables from their packages
  3578. ;; Declared here to avoid compiler warnings
  3579. ;; XEmacs only
  3580. (defvar outline-mode-menu-heading)
  3581. (defvar outline-mode-menu-show)
  3582. (defvar outline-mode-menu-hide)
  3583. (defvar zmacs-regions) ; XEmacs regions
  3584. ;; Emacs only
  3585. (defvar mark-active)
  3586. ;; Various packages
  3587. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3588. (declare-function calendar-forward-day "cal-move" (arg))
  3589. (declare-function calendar-goto-date "cal-move" (date))
  3590. (declare-function calendar-goto-today "cal-move" ())
  3591. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3592. (defvar calc-embedded-close-formula)
  3593. (defvar calc-embedded-open-formula)
  3594. (declare-function cdlatex-tab "ext:cdlatex" ())
  3595. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3596. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3597. (defvar font-lock-unfontify-region-function)
  3598. (declare-function iswitchb-read-buffer "iswitchb"
  3599. (prompt &optional default require-match start matches-set))
  3600. (defvar iswitchb-temp-buflist)
  3601. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3602. (defvar org-agenda-tags-todo-honor-ignore-options)
  3603. (declare-function org-agenda-skip "org-agenda" ())
  3604. (declare-function
  3605. org-agenda-format-item "org-agenda"
  3606. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3607. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3608. (declare-function org-agenda-change-all-lines "org-agenda"
  3609. (newhead hdmarker &optional fixface just-this))
  3610. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3611. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3612. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3613. (beg end))
  3614. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3615. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3616. "org-agenda" (&optional end))
  3617. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3618. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3619. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3620. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3621. (declare-function org-indent-mode "org-indent" (&optional arg))
  3622. (declare-function parse-time-string "parse-time" (string))
  3623. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3624. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3625. (defvar remember-data-file)
  3626. (defvar texmathp-why)
  3627. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3628. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3629. (defvar org-latex-regexps)
  3630. ;;; Autoload and prepare some org modules
  3631. ;; Some table stuff that needs to be defined here, because it is used
  3632. ;; by the functions setting up org-mode or checking for table context.
  3633. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3634. "Detect an org-type or table-type table.")
  3635. (defconst org-table-line-regexp "^[ \t]*|"
  3636. "Detect an org-type table line.")
  3637. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3638. "Detect an org-type table line.")
  3639. (defconst org-table-hline-regexp "^[ \t]*|-"
  3640. "Detect an org-type table hline.")
  3641. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3642. "Detect a table-type table hline.")
  3643. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3644. "Detect the first line outside a table when searching from within it.
  3645. This works for both table types.")
  3646. ;; Autoload the functions in org-table.el that are needed by functions here.
  3647. (eval-and-compile
  3648. (org-autoload "org-table"
  3649. '(org-table-begin org-table-blank-field org-table-end)))
  3650. ;;;###autoload
  3651. (defun turn-on-orgtbl ()
  3652. "Unconditionally turn on `orgtbl-mode'."
  3653. (require 'org-table)
  3654. (orgtbl-mode 1))
  3655. (defun org-at-table-p (&optional table-type)
  3656. "Return t if the cursor is inside an org-type table.
  3657. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3658. (if org-enable-table-editor
  3659. (save-excursion
  3660. (beginning-of-line 1)
  3661. (looking-at (if table-type org-table-any-line-regexp
  3662. org-table-line-regexp)))
  3663. nil))
  3664. (defsubst org-table-p () (org-at-table-p))
  3665. (defun org-at-table.el-p ()
  3666. "Return t if and only if we are at a table.el table."
  3667. (and (org-at-table-p 'any)
  3668. (save-excursion
  3669. (goto-char (org-table-begin 'any))
  3670. (looking-at org-table1-hline-regexp))))
  3671. (defun org-table-recognize-table.el ()
  3672. "If there is a table.el table nearby, recognize it and move into it."
  3673. (if org-table-tab-recognizes-table.el
  3674. (if (org-at-table.el-p)
  3675. (progn
  3676. (beginning-of-line 1)
  3677. (if (looking-at org-table-dataline-regexp)
  3678. nil
  3679. (if (looking-at org-table1-hline-regexp)
  3680. (progn
  3681. (beginning-of-line 2)
  3682. (if (looking-at org-table-any-border-regexp)
  3683. (beginning-of-line -1)))))
  3684. (if (re-search-forward "|" (org-table-end t) t)
  3685. (progn
  3686. (require 'table)
  3687. (if (table--at-cell-p (point))
  3688. t
  3689. (message "recognizing table.el table...")
  3690. (table-recognize-table)
  3691. (message "recognizing table.el table...done")))
  3692. (error "This should not happen"))
  3693. t)
  3694. nil)
  3695. nil))
  3696. (defun org-at-table-hline-p ()
  3697. "Return t if the cursor is inside a hline in a table."
  3698. (if org-enable-table-editor
  3699. (save-excursion
  3700. (beginning-of-line 1)
  3701. (looking-at org-table-hline-regexp))
  3702. nil))
  3703. (defvar org-table-clean-did-remove-column nil)
  3704. (defun org-table-map-tables (function &optional quietly)
  3705. "Apply FUNCTION to the start of all tables in the buffer."
  3706. (save-excursion
  3707. (save-restriction
  3708. (widen)
  3709. (goto-char (point-min))
  3710. (while (re-search-forward org-table-any-line-regexp nil t)
  3711. (unless quietly
  3712. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3713. (beginning-of-line 1)
  3714. (when (and (looking-at org-table-line-regexp)
  3715. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3716. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3717. (save-excursion (funcall function))
  3718. (or (looking-at org-table-line-regexp)
  3719. (forward-char 1)))
  3720. (re-search-forward org-table-any-border-regexp nil 1))))
  3721. (unless quietly (message "Mapping tables: done")))
  3722. ;; Declare and autoload functions from ox.el and al.
  3723. (declare-function org-export-get-environment "ox"
  3724. (&optional backend subtreep ext-plist))
  3725. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3726. ;; Declare and autoload functions from org-agenda.el
  3727. (eval-and-compile
  3728. (org-autoload "org-agenda"
  3729. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3730. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3731. (declare-function org-clock-update-mode-line "org-clock" ())
  3732. (declare-function org-resolve-clocks "org-clock"
  3733. (&optional also-non-dangling-p prompt last-valid))
  3734. (defvar org-clock-start-time)
  3735. (defvar org-clock-marker (make-marker)
  3736. "Marker recording the last clock-in.")
  3737. (defvar org-clock-hd-marker (make-marker)
  3738. "Marker recording the last clock-in, but the headline position.")
  3739. (defvar org-clock-heading ""
  3740. "The heading of the current clock entry.")
  3741. (defun org-clock-is-active ()
  3742. "Return non-nil if clock is currently running.
  3743. The return value is actually the clock marker."
  3744. (marker-buffer org-clock-marker))
  3745. (eval-and-compile
  3746. (org-autoload "org-clock" '(org-clock-remove-overlays
  3747. org-clock-update-time-maybe
  3748. org-clocktable-shift)))
  3749. (defun org-check-running-clock ()
  3750. "Check if the current buffer contains the running clock.
  3751. If yes, offer to stop it and to save the buffer with the changes."
  3752. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3753. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3754. (buffer-name))))
  3755. (org-clock-out)
  3756. (when (y-or-n-p "Save changed buffer?")
  3757. (save-buffer))))
  3758. (defun org-clocktable-try-shift (dir n)
  3759. "Check if this line starts a clock table, if yes, shift the time block."
  3760. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3761. (org-clocktable-shift dir n)))
  3762. ;;;###autoload
  3763. (defun org-clock-persistence-insinuate ()
  3764. "Set up hooks for clock persistence."
  3765. (require 'org-clock)
  3766. (add-hook 'org-mode-hook 'org-clock-load)
  3767. (add-hook 'kill-emacs-hook 'org-clock-save))
  3768. ;; Define the variable already here, to make sure we have it.
  3769. (defvar org-indent-mode nil
  3770. "Non-nil if Org-Indent mode is enabled.
  3771. Use the command `org-indent-mode' to change this variable.")
  3772. ;; Autoload archiving code
  3773. ;; The stuff that is needed for cycling and tags has to be defined here.
  3774. (defgroup org-archive nil
  3775. "Options concerning archiving in Org-mode."
  3776. :tag "Org Archive"
  3777. :group 'org-structure)
  3778. (defcustom org-archive-location "%s_archive::"
  3779. "The location where subtrees should be archived.
  3780. The value of this variable is a string, consisting of two parts,
  3781. separated by a double-colon. The first part is a filename and
  3782. the second part is a headline.
  3783. When the filename is omitted, archiving happens in the same file.
  3784. %s in the filename will be replaced by the current file
  3785. name (without the directory part). Archiving to a different file
  3786. is useful to keep archived entries from contributing to the
  3787. Org-mode Agenda.
  3788. The archived entries will be filed as subtrees of the specified
  3789. headline. When the headline is omitted, the subtrees are simply
  3790. filed away at the end of the file, as top-level entries. Also in
  3791. the heading you can use %s to represent the file name, this can be
  3792. useful when using the same archive for a number of different files.
  3793. Here are a few examples:
  3794. \"%s_archive::\"
  3795. If the current file is Projects.org, archive in file
  3796. Projects.org_archive, as top-level trees. This is the default.
  3797. \"::* Archived Tasks\"
  3798. Archive in the current file, under the top-level headline
  3799. \"* Archived Tasks\".
  3800. \"~/org/archive.org::\"
  3801. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3802. \"~/org/archive.org::* From %s\"
  3803. Archive in file ~/org/archive.org (absolute path), under headlines
  3804. \"From FILENAME\" where file name is the current file name.
  3805. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3806. The \"datetree/\" string is special, signifying to archive
  3807. items to the datetree. Items are placed in either the CLOSED
  3808. date of the item, or the current date if there is no CLOSED date.
  3809. The heading will be a subentry to the current date. There doesn't
  3810. need to be a heading, but there always needs to be a slash after
  3811. datetree. For example, to store archived items directly in the
  3812. datetree, use \"~/org/datetree.org::datetree/\".
  3813. \"basement::** Finished Tasks\"
  3814. Archive in file ./basement (relative path), as level 3 trees
  3815. below the level 2 heading \"** Finished Tasks\".
  3816. You may set this option on a per-file basis by adding to the buffer a
  3817. line like
  3818. #+ARCHIVE: basement::** Finished Tasks
  3819. You may also define it locally for a subtree by setting an ARCHIVE property
  3820. in the entry. If such a property is found in an entry, or anywhere up
  3821. the hierarchy, it will be used."
  3822. :group 'org-archive
  3823. :type 'string)
  3824. (defcustom org-archive-tag "ARCHIVE"
  3825. "The tag that marks a subtree as archived.
  3826. An archived subtree does not open during visibility cycling, and does
  3827. not contribute to the agenda listings.
  3828. After changing this, font-lock must be restarted in the relevant buffers to
  3829. get the proper fontification."
  3830. :group 'org-archive
  3831. :group 'org-keywords
  3832. :type 'string)
  3833. (defcustom org-agenda-skip-archived-trees t
  3834. "Non-nil means the agenda will skip any items located in archived trees.
  3835. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3836. variable is no longer recommended, you should leave it at the value t.
  3837. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3838. :group 'org-archive
  3839. :group 'org-agenda-skip
  3840. :type 'boolean)
  3841. (defcustom org-columns-skip-archived-trees t
  3842. "Non-nil means ignore archived trees when creating column view."
  3843. :group 'org-archive
  3844. :group 'org-properties
  3845. :type 'boolean)
  3846. (defcustom org-cycle-open-archived-trees nil
  3847. "Non-nil means `org-cycle' will open archived trees.
  3848. An archived tree is a tree marked with the tag ARCHIVE.
  3849. When nil, archived trees will stay folded. You can still open them with
  3850. normal outline commands like `show-all', but not with the cycling commands."
  3851. :group 'org-archive
  3852. :group 'org-cycle
  3853. :type 'boolean)
  3854. (defcustom org-sparse-tree-open-archived-trees nil
  3855. "Non-nil means sparse tree construction shows matches in archived trees.
  3856. When nil, matches in these trees are highlighted, but the trees are kept in
  3857. collapsed state."
  3858. :group 'org-archive
  3859. :group 'org-sparse-trees
  3860. :type 'boolean)
  3861. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3862. "The default date type when building a sparse tree.
  3863. When this is nil, a date is a scheduled or a deadline timestamp.
  3864. Otherwise, these types are allowed:
  3865. all: all timestamps
  3866. active: only active timestamps (<...>)
  3867. inactive: only inactive timestamps (<...)
  3868. scheduled: only scheduled timestamps
  3869. deadline: only deadline timestamps"
  3870. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3871. (const :tag "All timestamps" all)
  3872. (const :tag "Only active timestamps" active)
  3873. (const :tag "Only inactive timestamps" inactive)
  3874. (const :tag "Only scheduled timestamps" scheduled)
  3875. (const :tag "Only deadline timestamps" deadline))
  3876. :version "24.3"
  3877. :group 'org-sparse-trees)
  3878. (defun org-cycle-hide-archived-subtrees (state)
  3879. "Re-hide all archived subtrees after a visibility state change."
  3880. (when (and (not org-cycle-open-archived-trees)
  3881. (not (memq state '(overview folded))))
  3882. (save-excursion
  3883. (let* ((globalp (memq state '(contents all)))
  3884. (beg (if globalp (point-min) (point)))
  3885. (end (if globalp (point-max) (org-end-of-subtree t))))
  3886. (org-hide-archived-subtrees beg end)
  3887. (goto-char beg)
  3888. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3889. (message "%s" (substitute-command-keys
  3890. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3891. (defun org-force-cycle-archived ()
  3892. "Cycle subtree even if it is archived."
  3893. (interactive)
  3894. (setq this-command 'org-cycle)
  3895. (let ((org-cycle-open-archived-trees t))
  3896. (call-interactively 'org-cycle)))
  3897. (defun org-hide-archived-subtrees (beg end)
  3898. "Re-hide all archived subtrees after a visibility state change."
  3899. (save-excursion
  3900. (let* ((re (concat ":" org-archive-tag ":")))
  3901. (goto-char beg)
  3902. (while (re-search-forward re end t)
  3903. (when (org-at-heading-p)
  3904. (org-flag-subtree t)
  3905. (org-end-of-subtree t))))))
  3906. (declare-function outline-end-of-heading "outline" ())
  3907. (declare-function outline-flag-region "outline" (from to flag))
  3908. (defun org-flag-subtree (flag)
  3909. (save-excursion
  3910. (org-back-to-heading t)
  3911. (outline-end-of-heading)
  3912. (outline-flag-region (point)
  3913. (progn (org-end-of-subtree t) (point))
  3914. flag)))
  3915. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3916. (eval-and-compile
  3917. (org-autoload "org-archive"
  3918. '(org-add-archive-files)))
  3919. ;; Autoload Column View Code
  3920. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3921. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3922. (declare-function org-columns-compute "org-colview" (property))
  3923. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3924. '(org-columns-number-to-string
  3925. org-columns-get-format-and-top-level
  3926. org-columns-compute
  3927. org-columns-remove-overlays))
  3928. ;; Autoload ID code
  3929. (declare-function org-id-store-link "org-id")
  3930. (declare-function org-id-locations-load "org-id")
  3931. (declare-function org-id-locations-save "org-id")
  3932. (defvar org-id-track-globally)
  3933. (org-autoload "org-id"
  3934. '(org-id-new
  3935. org-id-copy
  3936. org-id-get-with-outline-path-completion
  3937. org-id-get-with-outline-drilling))
  3938. ;;; Variables for pre-computed regular expressions, all buffer local
  3939. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3940. "Matches first line of a hidden block.")
  3941. (make-variable-buffer-local 'org-drawer-regexp)
  3942. (defvar org-todo-regexp nil
  3943. "Matches any of the TODO state keywords.")
  3944. (make-variable-buffer-local 'org-todo-regexp)
  3945. (defvar org-not-done-regexp nil
  3946. "Matches any of the TODO state keywords except the last one.")
  3947. (make-variable-buffer-local 'org-not-done-regexp)
  3948. (defvar org-not-done-heading-regexp nil
  3949. "Matches a TODO headline that is not done.")
  3950. (make-variable-buffer-local 'org-not-done-regexp)
  3951. (defvar org-todo-line-regexp nil
  3952. "Matches a headline and puts TODO state into group 2 if present.")
  3953. (make-variable-buffer-local 'org-todo-line-regexp)
  3954. (defvar org-complex-heading-regexp nil
  3955. "Matches a headline and puts everything into groups:
  3956. group 1: the stars
  3957. group 2: The todo keyword, maybe
  3958. group 3: Priority cookie
  3959. group 4: True headline
  3960. group 5: Tags")
  3961. (make-variable-buffer-local 'org-complex-heading-regexp)
  3962. (defvar org-complex-heading-regexp-format nil
  3963. "Printf format to make regexp to match an exact headline.
  3964. This regexp will match the headline of any node which has the
  3965. exact headline text that is put into the format, but may have any
  3966. TODO state, priority and tags.")
  3967. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3968. (defvar org-todo-line-tags-regexp nil
  3969. "Matches a headline and puts TODO state into group 2 if present.
  3970. Also put tags into group 4 if tags are present.")
  3971. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3972. (defvar org-ds-keyword-length 12
  3973. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3974. (make-variable-buffer-local 'org-ds-keyword-length)
  3975. (defvar org-deadline-regexp nil
  3976. "Matches the DEADLINE keyword.")
  3977. (make-variable-buffer-local 'org-deadline-regexp)
  3978. (defvar org-deadline-time-regexp nil
  3979. "Matches the DEADLINE keyword together with a time stamp.")
  3980. (make-variable-buffer-local 'org-deadline-time-regexp)
  3981. (defvar org-deadline-line-regexp nil
  3982. "Matches the DEADLINE keyword and the rest of the line.")
  3983. (make-variable-buffer-local 'org-deadline-line-regexp)
  3984. (defvar org-scheduled-regexp nil
  3985. "Matches the SCHEDULED keyword.")
  3986. (make-variable-buffer-local 'org-scheduled-regexp)
  3987. (defvar org-scheduled-time-regexp nil
  3988. "Matches the SCHEDULED keyword together with a time stamp.")
  3989. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3990. (defvar org-closed-time-regexp nil
  3991. "Matches the CLOSED keyword together with a time stamp.")
  3992. (make-variable-buffer-local 'org-closed-time-regexp)
  3993. (defvar org-keyword-time-regexp nil
  3994. "Matches any of the 4 keywords, together with the time stamp.")
  3995. (make-variable-buffer-local 'org-keyword-time-regexp)
  3996. (defvar org-keyword-time-not-clock-regexp nil
  3997. "Matches any of the 3 keywords, together with the time stamp.")
  3998. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3999. (defvar org-maybe-keyword-time-regexp nil
  4000. "Matches a timestamp, possibly preceded by a keyword.")
  4001. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  4002. (defvar org-all-time-keywords nil
  4003. "List of time keywords.")
  4004. (make-variable-buffer-local 'org-all-time-keywords)
  4005. (defconst org-plain-time-of-day-regexp
  4006. (concat
  4007. "\\(\\<[012]?[0-9]"
  4008. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4009. "\\(--?"
  4010. "\\(\\<[012]?[0-9]"
  4011. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4012. "\\)?")
  4013. "Regular expression to match a plain time or time range.
  4014. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4015. groups carry important information:
  4016. 0 the full match
  4017. 1 the first time, range or not
  4018. 8 the second time, if it is a range.")
  4019. (defconst org-plain-time-extension-regexp
  4020. (concat
  4021. "\\(\\<[012]?[0-9]"
  4022. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4023. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4024. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4025. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4026. groups carry important information:
  4027. 0 the full match
  4028. 7 hours of duration
  4029. 9 minutes of duration")
  4030. (defconst org-stamp-time-of-day-regexp
  4031. (concat
  4032. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4033. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4034. "\\(--?"
  4035. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4036. "Regular expression to match a timestamp time or time range.
  4037. After a match, the following groups carry important information:
  4038. 0 the full match
  4039. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4040. 2 the first time, range or not
  4041. 4 the second time, if it is a range.")
  4042. (defconst org-startup-options
  4043. '(("fold" org-startup-folded t)
  4044. ("overview" org-startup-folded t)
  4045. ("nofold" org-startup-folded nil)
  4046. ("showall" org-startup-folded nil)
  4047. ("showeverything" org-startup-folded showeverything)
  4048. ("content" org-startup-folded content)
  4049. ("indent" org-startup-indented t)
  4050. ("noindent" org-startup-indented nil)
  4051. ("hidestars" org-hide-leading-stars t)
  4052. ("showstars" org-hide-leading-stars nil)
  4053. ("odd" org-odd-levels-only t)
  4054. ("oddeven" org-odd-levels-only nil)
  4055. ("align" org-startup-align-all-tables t)
  4056. ("noalign" org-startup-align-all-tables nil)
  4057. ("inlineimages" org-startup-with-inline-images t)
  4058. ("noinlineimages" org-startup-with-inline-images nil)
  4059. ("latexpreview" org-startup-with-latex-preview t)
  4060. ("nolatexpreview" org-startup-with-latex-preview nil)
  4061. ("customtime" org-display-custom-times t)
  4062. ("logdone" org-log-done time)
  4063. ("lognotedone" org-log-done note)
  4064. ("nologdone" org-log-done nil)
  4065. ("lognoteclock-out" org-log-note-clock-out t)
  4066. ("nolognoteclock-out" org-log-note-clock-out nil)
  4067. ("logrepeat" org-log-repeat state)
  4068. ("lognoterepeat" org-log-repeat note)
  4069. ("logdrawer" org-log-into-drawer t)
  4070. ("nologdrawer" org-log-into-drawer nil)
  4071. ("logstatesreversed" org-log-states-order-reversed t)
  4072. ("nologstatesreversed" org-log-states-order-reversed nil)
  4073. ("nologrepeat" org-log-repeat nil)
  4074. ("logreschedule" org-log-reschedule time)
  4075. ("lognotereschedule" org-log-reschedule note)
  4076. ("nologreschedule" org-log-reschedule nil)
  4077. ("logredeadline" org-log-redeadline time)
  4078. ("lognoteredeadline" org-log-redeadline note)
  4079. ("nologredeadline" org-log-redeadline nil)
  4080. ("logrefile" org-log-refile time)
  4081. ("lognoterefile" org-log-refile note)
  4082. ("nologrefile" org-log-refile nil)
  4083. ("fninline" org-footnote-define-inline t)
  4084. ("nofninline" org-footnote-define-inline nil)
  4085. ("fnlocal" org-footnote-section nil)
  4086. ("fnauto" org-footnote-auto-label t)
  4087. ("fnprompt" org-footnote-auto-label nil)
  4088. ("fnconfirm" org-footnote-auto-label confirm)
  4089. ("fnplain" org-footnote-auto-label plain)
  4090. ("fnadjust" org-footnote-auto-adjust t)
  4091. ("nofnadjust" org-footnote-auto-adjust nil)
  4092. ("constcgs" constants-unit-system cgs)
  4093. ("constSI" constants-unit-system SI)
  4094. ("noptag" org-tag-persistent-alist nil)
  4095. ("hideblocks" org-hide-block-startup t)
  4096. ("nohideblocks" org-hide-block-startup nil)
  4097. ("beamer" org-startup-with-beamer-mode t)
  4098. ("entitiespretty" org-pretty-entities t)
  4099. ("entitiesplain" org-pretty-entities nil))
  4100. "Variable associated with STARTUP options for org-mode.
  4101. Each element is a list of three items: the startup options (as written
  4102. in the #+STARTUP line), the corresponding variable, and the value to set
  4103. this variable to if the option is found. An optional forth element PUSH
  4104. means to push this value onto the list in the variable.")
  4105. (defun org-update-property-plist (key val props)
  4106. "Update PROPS with KEY and VAL."
  4107. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4108. (key (if appending (substring key 0 (- (length key) 1)) key))
  4109. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4110. (previous (cdr (assoc key props))))
  4111. (if appending
  4112. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4113. (cons (cons key val) remainder))))
  4114. (defconst org-block-regexp
  4115. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4116. "Regular expression for hiding blocks.")
  4117. (defconst org-heading-keyword-regexp-format
  4118. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4119. "Printf format for a regexp matching an headline with some keyword.
  4120. This regexp will match the headline of any node which has the
  4121. exact keyword that is put into the format. The keyword isn't in
  4122. any group by default, but the stars and the body are.")
  4123. (defconst org-heading-keyword-maybe-regexp-format
  4124. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4125. "Printf format for a regexp matching an headline, possibly with some keyword.
  4126. This regexp can match any headline with the specified keyword, or
  4127. without a keyword. The keyword isn't in any group by default,
  4128. but the stars and the body are.")
  4129. (defun org-set-regexps-and-options ()
  4130. "Precompute regular expressions for current buffer."
  4131. (when (derived-mode-p 'org-mode)
  4132. (org-set-local 'org-todo-kwd-alist nil)
  4133. (org-set-local 'org-todo-key-alist nil)
  4134. (org-set-local 'org-todo-key-trigger nil)
  4135. (org-set-local 'org-todo-keywords-1 nil)
  4136. (org-set-local 'org-done-keywords nil)
  4137. (org-set-local 'org-todo-heads nil)
  4138. (org-set-local 'org-todo-sets nil)
  4139. (org-set-local 'org-todo-log-states nil)
  4140. (org-set-local 'org-file-properties nil)
  4141. (org-set-local 'org-file-tags nil)
  4142. (let ((re (org-make-options-regexp
  4143. '("CATEGORY" "TODO" "COLUMNS" "STARTUP" "ARCHIVE" "FILETAGS"
  4144. "TAGS" "LINK" "PRIORITIES" "CONSTANTS" "PROPERTY" "DRAWERS"
  4145. "SETUPFILE" "OPTIONS")
  4146. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4147. (splitre "[ \t]+")
  4148. (scripts org-use-sub-superscripts)
  4149. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4150. tail sep kws1 prio props ftags drawers ext-setup-or-nil setup-contents
  4151. (start 0))
  4152. (save-excursion
  4153. (save-restriction
  4154. (widen)
  4155. (goto-char (point-min))
  4156. (while (or (and ext-setup-or-nil
  4157. (string-match re ext-setup-or-nil start)
  4158. (setq start (match-end 0)))
  4159. (and (setq ext-setup-or-nil nil start 0)
  4160. (re-search-forward re nil t)))
  4161. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4162. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4163. (if (stringp value) (setq value (org-trim value)))
  4164. (cond
  4165. ((equal key "CATEGORY")
  4166. (setq cat value))
  4167. ((member key '("SEQ_TODO" "TODO"))
  4168. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4169. ((equal key "TYP_TODO")
  4170. (push (cons 'type (org-split-string value splitre)) kwds))
  4171. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4172. ;; general TODO-like setup
  4173. (push (cons (intern (downcase (match-string 1 key)))
  4174. (org-split-string value splitre)) kwds))
  4175. ((equal key "TAGS")
  4176. (setq tags (append tags (if tags '("\\n") nil)
  4177. (org-split-string value splitre))))
  4178. ((equal key "COLUMNS")
  4179. (org-set-local 'org-columns-default-format value))
  4180. ((equal key "LINK")
  4181. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4182. (push (cons (match-string 1 value)
  4183. (org-trim (match-string 2 value)))
  4184. links)))
  4185. ((equal key "PRIORITIES")
  4186. (setq prio (org-split-string value " +")))
  4187. ((equal key "PROPERTY")
  4188. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4189. (setq props (org-update-property-plist (match-string 1 value)
  4190. (match-string 2 value)
  4191. props))))
  4192. ((equal key "FILETAGS")
  4193. (when (string-match "\\S-" value)
  4194. (setq ftags
  4195. (append
  4196. ftags
  4197. (apply 'append
  4198. (mapcar (lambda (x) (org-split-string x ":"))
  4199. (org-split-string value)))))))
  4200. ((equal key "DRAWERS")
  4201. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4202. ((equal key "CONSTANTS")
  4203. (setq const (append const (org-split-string value splitre))))
  4204. ((equal key "STARTUP")
  4205. (let ((opts (org-split-string value splitre))
  4206. l var val)
  4207. (while (setq l (pop opts))
  4208. (when (setq l (assoc l org-startup-options))
  4209. (setq var (nth 1 l) val (nth 2 l))
  4210. (if (not (nth 3 l))
  4211. (set (make-local-variable var) val)
  4212. (if (not (listp (symbol-value var)))
  4213. (set (make-local-variable var) nil))
  4214. (set (make-local-variable var) (symbol-value var))
  4215. (add-to-list var val))))))
  4216. ((equal key "ARCHIVE")
  4217. (setq arch value)
  4218. (remove-text-properties 0 (length arch)
  4219. '(face t fontified t) arch))
  4220. ((equal key "OPTIONS")
  4221. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4222. (setq scripts (read (match-string 2 value)))))
  4223. ((equal key "SETUPFILE")
  4224. (setq setup-contents (org-file-contents
  4225. (expand-file-name
  4226. (org-remove-double-quotes value))
  4227. 'noerror))
  4228. (if (not ext-setup-or-nil)
  4229. (setq ext-setup-or-nil setup-contents start 0)
  4230. (setq ext-setup-or-nil
  4231. (concat (substring ext-setup-or-nil 0 start)
  4232. "\n" setup-contents "\n"
  4233. (substring ext-setup-or-nil start)))))))
  4234. ;; search for property blocks
  4235. (goto-char (point-min))
  4236. (while (re-search-forward org-block-regexp nil t)
  4237. (when (equal "PROPERTY" (upcase (match-string 1)))
  4238. (setq value (replace-regexp-in-string
  4239. "[\n\r]" " " (match-string 4)))
  4240. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4241. (setq props (org-update-property-plist (match-string 1 value)
  4242. (match-string 2 value)
  4243. props)))))))
  4244. (org-set-local 'org-use-sub-superscripts scripts)
  4245. (when cat
  4246. (org-set-local 'org-category (intern cat))
  4247. (push (cons "CATEGORY" cat) props))
  4248. (when prio
  4249. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4250. (setq prio (mapcar 'string-to-char prio))
  4251. (org-set-local 'org-highest-priority (nth 0 prio))
  4252. (org-set-local 'org-lowest-priority (nth 1 prio))
  4253. (org-set-local 'org-default-priority (nth 2 prio)))
  4254. (and props (org-set-local 'org-file-properties (nreverse props)))
  4255. (and ftags (org-set-local 'org-file-tags
  4256. (mapcar 'org-add-prop-inherited ftags)))
  4257. (and drawers (org-set-local 'org-drawers drawers))
  4258. (and arch (org-set-local 'org-archive-location arch))
  4259. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4260. ;; Process the TODO keywords
  4261. (unless kwds
  4262. ;; Use the global values as if they had been given locally.
  4263. (setq kwds (default-value 'org-todo-keywords))
  4264. (if (stringp (car kwds))
  4265. (setq kwds (list (cons org-todo-interpretation
  4266. (default-value 'org-todo-keywords)))))
  4267. (setq kwds (reverse kwds)))
  4268. (setq kwds (nreverse kwds))
  4269. (let (inter kws kw)
  4270. (while (setq kws (pop kwds))
  4271. (let ((kws (or
  4272. (run-hook-with-args-until-success
  4273. 'org-todo-setup-filter-hook kws)
  4274. kws)))
  4275. (setq inter (pop kws) sep (member "|" kws)
  4276. kws0 (delete "|" (copy-sequence kws))
  4277. kwsa nil
  4278. kws1 (mapcar
  4279. (lambda (x)
  4280. ;; 1 2
  4281. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4282. (progn
  4283. (setq kw (match-string 1 x)
  4284. key (and (match-end 2) (match-string 2 x))
  4285. log (org-extract-log-state-settings x))
  4286. (push (cons kw (and key (string-to-char key))) kwsa)
  4287. (and log (push log org-todo-log-states))
  4288. kw)
  4289. (error "Invalid TODO keyword %s" x)))
  4290. kws0)
  4291. kwsa (if kwsa (append '((:startgroup))
  4292. (nreverse kwsa)
  4293. '((:endgroup))))
  4294. hw (car kws1)
  4295. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4296. tail (list inter hw (car dws) (org-last dws))))
  4297. (add-to-list 'org-todo-heads hw 'append)
  4298. (push kws1 org-todo-sets)
  4299. (setq org-done-keywords (append org-done-keywords dws nil))
  4300. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4301. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4302. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4303. (setq org-todo-sets (nreverse org-todo-sets)
  4304. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4305. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4306. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4307. ;; Process the constants
  4308. (when const
  4309. (let (e cst)
  4310. (while (setq e (pop const))
  4311. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4312. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4313. (setq org-table-formula-constants-local cst)))
  4314. ;; Process the tags.
  4315. (when tags
  4316. (let (e tgs)
  4317. (while (setq e (pop tags))
  4318. (cond
  4319. ((equal e "{") (push '(:startgroup) tgs))
  4320. ((equal e "}") (push '(:endgroup) tgs))
  4321. ((equal e "\\n") (push '(:newline) tgs))
  4322. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4323. (push (cons (match-string 1 e)
  4324. (string-to-char (match-string 2 e)))
  4325. tgs))
  4326. (t (push (list e) tgs))))
  4327. (org-set-local 'org-tag-alist nil)
  4328. (while (setq e (pop tgs))
  4329. (or (and (stringp (car e))
  4330. (assoc (car e) org-tag-alist))
  4331. (push e org-tag-alist)))))
  4332. ;; Compute the regular expressions and other local variables.
  4333. ;; Using `org-outline-regexp-bol' would complicate them much,
  4334. ;; because of the fixed white space at the end of that string.
  4335. (if (not org-done-keywords)
  4336. (setq org-done-keywords (and org-todo-keywords-1
  4337. (list (org-last org-todo-keywords-1)))))
  4338. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4339. (length org-scheduled-string)
  4340. (length org-clock-string)
  4341. (length org-closed-string)))
  4342. org-drawer-regexp
  4343. (concat "^[ \t]*:\\("
  4344. (mapconcat 'regexp-quote org-drawers "\\|")
  4345. "\\):[ \t]*$")
  4346. org-not-done-keywords
  4347. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4348. org-todo-regexp
  4349. (concat "\\("
  4350. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4351. "\\)")
  4352. org-not-done-regexp
  4353. (concat "\\("
  4354. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4355. "\\)")
  4356. org-not-done-heading-regexp
  4357. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4358. org-todo-line-regexp
  4359. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4360. org-complex-heading-regexp
  4361. (concat "^\\(\\*+\\)"
  4362. "\\(?: +" org-todo-regexp "\\)?"
  4363. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4364. "\\(?: +\\(.*?\\)\\)??"
  4365. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4366. "[ \t]*$")
  4367. org-complex-heading-regexp-format
  4368. (concat "^\\(\\*+\\)"
  4369. "\\(?: +" org-todo-regexp "\\)?"
  4370. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4371. "\\(?: +"
  4372. ;; Stats cookies can be stuck to body.
  4373. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4374. "\\(%s\\)"
  4375. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4376. "\\)"
  4377. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4378. "[ \t]*$")
  4379. org-todo-line-tags-regexp
  4380. (concat "^\\(\\*+\\)"
  4381. "\\(?: +" org-todo-regexp "\\)?"
  4382. "\\(?: +\\(.*?\\)\\)??"
  4383. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4384. "[ \t]*$")
  4385. org-deadline-regexp (concat "\\<" org-deadline-string)
  4386. org-deadline-time-regexp
  4387. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4388. org-deadline-line-regexp
  4389. (concat "\\<\\(" org-deadline-string "\\).*")
  4390. org-scheduled-regexp
  4391. (concat "\\<" org-scheduled-string)
  4392. org-scheduled-time-regexp
  4393. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4394. org-closed-time-regexp
  4395. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4396. org-keyword-time-regexp
  4397. (concat "\\<\\(" org-scheduled-string
  4398. "\\|" org-deadline-string
  4399. "\\|" org-closed-string
  4400. "\\|" org-clock-string "\\)"
  4401. " *[[<]\\([^]>]+\\)[]>]")
  4402. org-keyword-time-not-clock-regexp
  4403. (concat "\\<\\(" org-scheduled-string
  4404. "\\|" org-deadline-string
  4405. "\\|" org-closed-string
  4406. "\\)"
  4407. " *[[<]\\([^]>]+\\)[]>]")
  4408. org-maybe-keyword-time-regexp
  4409. (concat "\\(\\<\\(" org-scheduled-string
  4410. "\\|" org-deadline-string
  4411. "\\|" org-closed-string
  4412. "\\|" org-clock-string "\\)\\)?"
  4413. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4414. org-all-time-keywords
  4415. (mapcar (lambda (w) (substring w 0 -1))
  4416. (list org-scheduled-string org-deadline-string
  4417. org-clock-string org-closed-string)))
  4418. (org-compute-latex-and-related-regexp)
  4419. (org-set-font-lock-defaults))))
  4420. (defun org-file-contents (file &optional noerror)
  4421. "Return the contents of FILE, as a string."
  4422. (if (or (not file)
  4423. (not (file-readable-p file)))
  4424. (if noerror
  4425. (progn
  4426. (message "Cannot read file \"%s\"" file)
  4427. (ding) (sit-for 2)
  4428. "")
  4429. (error "Cannot read file \"%s\"" file))
  4430. (with-temp-buffer
  4431. (insert-file-contents file)
  4432. (buffer-string))))
  4433. (defun org-extract-log-state-settings (x)
  4434. "Extract the log state setting from a TODO keyword string.
  4435. This will extract info from a string like \"WAIT(w@/!)\"."
  4436. (let (kw key log1 log2)
  4437. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4438. (setq kw (match-string 1 x)
  4439. key (and (match-end 2) (match-string 2 x))
  4440. log1 (and (match-end 3) (match-string 3 x))
  4441. log2 (and (match-end 4) (match-string 4 x)))
  4442. (and (or log1 log2)
  4443. (list kw
  4444. (and log1 (if (equal log1 "!") 'time 'note))
  4445. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4446. (defun org-remove-keyword-keys (list)
  4447. "Remove a pair of parenthesis at the end of each string in LIST."
  4448. (mapcar (lambda (x)
  4449. (if (string-match "(.*)$" x)
  4450. (substring x 0 (match-beginning 0))
  4451. x))
  4452. list))
  4453. (defun org-assign-fast-keys (alist)
  4454. "Assign fast keys to a keyword-key alist.
  4455. Respect keys that are already there."
  4456. (let (new e (alt ?0))
  4457. (while (setq e (pop alist))
  4458. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4459. (cdr e)) ;; Key already assigned.
  4460. (push e new)
  4461. (let ((clist (string-to-list (downcase (car e))))
  4462. (used (append new alist)))
  4463. (when (= (car clist) ?@)
  4464. (pop clist))
  4465. (while (and clist (rassoc (car clist) used))
  4466. (pop clist))
  4467. (unless clist
  4468. (while (rassoc alt used)
  4469. (incf alt)))
  4470. (push (cons (car e) (or (car clist) alt)) new))))
  4471. (nreverse new)))
  4472. ;;; Some variables used in various places
  4473. (defvar org-window-configuration nil
  4474. "Used in various places to store a window configuration.")
  4475. (defvar org-selected-window nil
  4476. "Used in various places to store a window configuration.")
  4477. (defvar org-finish-function nil
  4478. "Function to be called when `C-c C-c' is used.
  4479. This is for getting out of special buffers like capture.")
  4480. ;; FIXME: Occasionally check by commenting these, to make sure
  4481. ;; no other functions uses these, forgetting to let-bind them.
  4482. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4483. (defvar org-last-state)
  4484. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4485. ;; Defined somewhere in this file, but used before definition.
  4486. (defvar org-entities) ;; defined in org-entities.el
  4487. (defvar org-struct-menu)
  4488. (defvar org-org-menu)
  4489. (defvar org-tbl-menu)
  4490. ;;;; Define the Org-mode
  4491. ;; We use a before-change function to check if a table might need
  4492. ;; an update.
  4493. (defvar org-table-may-need-update t
  4494. "Indicates that a table might need an update.
  4495. This variable is set by `org-before-change-function'.
  4496. `org-table-align' sets it back to nil.")
  4497. (defun org-before-change-function (beg end)
  4498. "Every change indicates that a table might need an update."
  4499. (setq org-table-may-need-update t))
  4500. (defvar org-mode-map)
  4501. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4502. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4503. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4504. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4505. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4506. (defvar org-table-buffer-is-an nil)
  4507. (defvar bidi-paragraph-direction)
  4508. (defvar buffer-face-mode-face)
  4509. (require 'outline)
  4510. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4511. (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"))
  4512. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4513. ;; Other stuff we need.
  4514. (require 'time-date)
  4515. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4516. (require 'easymenu)
  4517. (require 'overlay)
  4518. ;; (require 'org-macs) moved higher up in the file before it is first used
  4519. (require 'org-entities)
  4520. ;; (require 'org-compat) moved higher up in the file before it is first used
  4521. (require 'org-faces)
  4522. (require 'org-list)
  4523. (require 'org-pcomplete)
  4524. (require 'org-src)
  4525. (require 'org-footnote)
  4526. (require 'org-macro)
  4527. ;; babel
  4528. (require 'ob)
  4529. ;;;###autoload
  4530. (define-derived-mode org-mode outline-mode "Org"
  4531. "Outline-based notes management and organizer, alias
  4532. \"Carsten's outline-mode for keeping track of everything.\"
  4533. Org-mode develops organizational tasks around a NOTES file which
  4534. contains information about projects as plain text. Org-mode is
  4535. implemented on top of outline-mode, which is ideal to keep the content
  4536. of large files well structured. It supports ToDo items, deadlines and
  4537. time stamps, which magically appear in the diary listing of the Emacs
  4538. calendar. Tables are easily created with a built-in table editor.
  4539. Plain text URL-like links connect to websites, emails (VM), Usenet
  4540. messages (Gnus), BBDB entries, and any files related to the project.
  4541. For printing and sharing of notes, an Org-mode file (or a part of it)
  4542. can be exported as a structured ASCII or HTML file.
  4543. The following commands are available:
  4544. \\{org-mode-map}"
  4545. ;; Get rid of Outline menus, they are not needed
  4546. ;; Need to do this here because define-derived-mode sets up
  4547. ;; the keymap so late. Still, it is a waste to call this each time
  4548. ;; we switch another buffer into org-mode.
  4549. (if (featurep 'xemacs)
  4550. (when (boundp 'outline-mode-menu-heading)
  4551. ;; Assume this is Greg's port, it uses easymenu
  4552. (easy-menu-remove outline-mode-menu-heading)
  4553. (easy-menu-remove outline-mode-menu-show)
  4554. (easy-menu-remove outline-mode-menu-hide))
  4555. (define-key org-mode-map [menu-bar headings] 'undefined)
  4556. (define-key org-mode-map [menu-bar hide] 'undefined)
  4557. (define-key org-mode-map [menu-bar show] 'undefined))
  4558. (org-load-modules-maybe)
  4559. (easy-menu-add org-org-menu)
  4560. (easy-menu-add org-tbl-menu)
  4561. (org-install-agenda-files-menu)
  4562. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4563. (add-to-invisibility-spec '(org-cwidth))
  4564. (add-to-invisibility-spec '(org-hide-block . t))
  4565. (when (featurep 'xemacs)
  4566. (org-set-local 'line-move-ignore-invisible t))
  4567. (org-set-local 'outline-regexp org-outline-regexp)
  4568. (org-set-local 'outline-level 'org-outline-level)
  4569. (setq bidi-paragraph-direction 'left-to-right)
  4570. (when (and org-ellipsis
  4571. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4572. (fboundp 'make-glyph-code))
  4573. (unless org-display-table
  4574. (setq org-display-table (make-display-table)))
  4575. (set-display-table-slot
  4576. org-display-table 4
  4577. (vconcat (mapcar
  4578. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4579. org-ellipsis)))
  4580. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4581. (setq buffer-display-table org-display-table))
  4582. (org-set-regexps-and-options)
  4583. (when (and org-tag-faces (not org-tags-special-faces-re))
  4584. ;; tag faces set outside customize.... force initialization.
  4585. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4586. ;; Calc embedded
  4587. (org-set-local 'calc-embedded-open-mode "# ")
  4588. ;; Modify a few syntax entries
  4589. (modify-syntax-entry ?< "(")
  4590. (modify-syntax-entry ?> ")")
  4591. (modify-syntax-entry ?{ "(")
  4592. (modify-syntax-entry ?} ")")
  4593. (modify-syntax-entry ?@ "w")
  4594. (modify-syntax-entry ?\" "\"")
  4595. (if org-startup-truncated (setq truncate-lines t))
  4596. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4597. (org-set-local 'font-lock-unfontify-region-function
  4598. 'org-unfontify-region)
  4599. ;; Activate before-change-function
  4600. (org-set-local 'org-table-may-need-update t)
  4601. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4602. 'local)
  4603. ;; Check for running clock before killing a buffer
  4604. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4605. ;; Initialize macros templates.
  4606. (org-macro-initialize-templates)
  4607. ;; Initialize radio targets.
  4608. (org-update-radio-target-regexp)
  4609. ;; Indentation.
  4610. (org-set-local 'indent-line-function 'org-indent-line)
  4611. (org-set-local 'indent-region-function 'org-indent-region)
  4612. ;; Filling and auto-filling.
  4613. (org-setup-filling)
  4614. ;; Comments.
  4615. (org-setup-comments-handling)
  4616. ;; Beginning/end of defun
  4617. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4618. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4619. ;; Next error for sparse trees
  4620. (org-set-local 'next-error-function 'org-occur-next-match)
  4621. ;; Make sure dependence stuff works reliably, even for users who set it
  4622. ;; too late :-(
  4623. (if org-enforce-todo-dependencies
  4624. (add-hook 'org-blocker-hook
  4625. 'org-block-todo-from-children-or-siblings-or-parent)
  4626. (remove-hook 'org-blocker-hook
  4627. 'org-block-todo-from-children-or-siblings-or-parent))
  4628. (if org-enforce-todo-checkbox-dependencies
  4629. (add-hook 'org-blocker-hook
  4630. 'org-block-todo-from-checkboxes)
  4631. (remove-hook 'org-blocker-hook
  4632. 'org-block-todo-from-checkboxes))
  4633. ;; Align options lines
  4634. (org-set-local
  4635. 'align-mode-rules-list
  4636. '((org-in-buffer-settings
  4637. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4638. (modes . '(org-mode)))))
  4639. ;; Imenu
  4640. (org-set-local 'imenu-create-index-function
  4641. 'org-imenu-get-tree)
  4642. ;; Make isearch reveal context
  4643. (if (or (featurep 'xemacs)
  4644. (not (boundp 'outline-isearch-open-invisible-function)))
  4645. ;; Emacs 21 and XEmacs make use of the hook
  4646. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4647. ;; Emacs 22 deals with this through a special variable
  4648. (org-set-local 'outline-isearch-open-invisible-function
  4649. (lambda (&rest ignore) (org-show-context 'isearch))))
  4650. ;; Setup the pcomplete hooks
  4651. (set (make-local-variable 'pcomplete-command-completion-function)
  4652. 'org-pcomplete-initial)
  4653. (set (make-local-variable 'pcomplete-command-name-function)
  4654. 'org-command-at-point)
  4655. (set (make-local-variable 'pcomplete-default-completion-function)
  4656. 'ignore)
  4657. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4658. 'org-parse-arguments)
  4659. (set (make-local-variable 'pcomplete-termination-string) "")
  4660. (when (>= emacs-major-version 23)
  4661. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4662. ;; If empty file that did not turn on org-mode automatically, make it to.
  4663. (if (and org-insert-mode-line-in-empty-file
  4664. (org-called-interactively-p 'any)
  4665. (= (point-min) (point-max)))
  4666. (insert "# -*- mode: org -*-\n\n"))
  4667. (unless org-inhibit-startup
  4668. (org-unmodified
  4669. (and org-startup-with-beamer-mode (org-beamer-mode))
  4670. (when org-startup-align-all-tables
  4671. (org-table-map-tables 'org-table-align 'quietly))
  4672. (when org-startup-with-inline-images
  4673. (org-display-inline-images))
  4674. (when org-startup-with-latex-preview
  4675. (org-preview-latex-fragment))
  4676. (unless org-inhibit-startup-visibility-stuff
  4677. (org-set-startup-visibility))))
  4678. ;; Try to set org-hide correctly
  4679. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4680. (defvar org-mode-transpose-word-syntax-table
  4681. (let ((st (make-syntax-table)))
  4682. (mapc (lambda(c) (modify-syntax-entry
  4683. (string-to-char (car c)) "w p" st))
  4684. org-emphasis-alist)
  4685. st))
  4686. (when (fboundp 'abbrev-table-put)
  4687. (abbrev-table-put org-mode-abbrev-table
  4688. :parents (list text-mode-abbrev-table)))
  4689. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4690. (defun org-find-invisible-foreground ()
  4691. (let ((candidates (remove
  4692. "unspecified-bg"
  4693. (nconc
  4694. (list (face-background 'default)
  4695. (face-background 'org-default))
  4696. (mapcar
  4697. (lambda (alist)
  4698. (when (boundp alist)
  4699. (cdr (assoc 'background-color (symbol-value alist)))))
  4700. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4701. (list (face-foreground 'org-hide))))))
  4702. (car (remove nil candidates))))
  4703. (defun org-current-time (&optional rounding-minutes)
  4704. "Current time, possibly rounded to ROUNDING-MINUTES.
  4705. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4706. `org-time-stamp-rounding-minutes'."
  4707. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4708. (car org-time-stamp-rounding-minutes)))
  4709. (time (decode-time)))
  4710. (if (> r 1)
  4711. (apply 'encode-time
  4712. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4713. (nthcdr 2 time)))
  4714. (current-time))))
  4715. (defun org-today ()
  4716. "Return today date, considering `org-extend-today-until'."
  4717. (time-to-days
  4718. (time-subtract (current-time)
  4719. (list 0 (* 3600 org-extend-today-until) 0))))
  4720. ;;;; Font-Lock stuff, including the activators
  4721. (defvar org-mouse-map (make-sparse-keymap))
  4722. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4723. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4724. (when org-mouse-1-follows-link
  4725. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4726. (when org-tab-follows-link
  4727. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4728. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4729. (require 'font-lock)
  4730. (defconst org-non-link-chars "]\t\n\r<>")
  4731. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4732. "shell" "elisp" "doi" "message"))
  4733. (defvar org-link-types-re nil
  4734. "Matches a link that has a url-like prefix like \"http:\"")
  4735. (defvar org-link-re-with-space nil
  4736. "Matches a link with spaces, optional angular brackets around it.")
  4737. (defvar org-link-re-with-space2 nil
  4738. "Matches a link with spaces, optional angular brackets around it.")
  4739. (defvar org-link-re-with-space3 nil
  4740. "Matches a link with spaces, only for internal part in bracket links.")
  4741. (defvar org-angle-link-re nil
  4742. "Matches link with angular brackets, spaces are allowed.")
  4743. (defvar org-plain-link-re nil
  4744. "Matches plain link, without spaces.")
  4745. (defvar org-bracket-link-regexp nil
  4746. "Matches a link in double brackets.")
  4747. (defvar org-bracket-link-analytic-regexp nil
  4748. "Regular expression used to analyze links.
  4749. Here is what the match groups contain after a match:
  4750. 1: http:
  4751. 2: http
  4752. 3: path
  4753. 4: [desc]
  4754. 5: desc")
  4755. (defvar org-bracket-link-analytic-regexp++ nil
  4756. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4757. (defvar org-any-link-re nil
  4758. "Regular expression matching any link.")
  4759. (defconst org-match-sexp-depth 3
  4760. "Number of stacked braces for sub/superscript matching.")
  4761. (defun org-create-multibrace-regexp (left right n)
  4762. "Create a regular expression which will match a balanced sexp.
  4763. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4764. as single character strings.
  4765. The regexp returned will match the entire expression including the
  4766. delimiters. It will also define a single group which contains the
  4767. match except for the outermost delimiters. The maximum depth of
  4768. stacked delimiters is N. Escaping delimiters is not possible."
  4769. (let* ((nothing (concat "[^" left right "]*?"))
  4770. (or "\\|")
  4771. (re nothing)
  4772. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4773. (while (> n 1)
  4774. (setq n (1- n)
  4775. re (concat re or next)
  4776. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4777. (concat left "\\(" re "\\)" right)))
  4778. (defvar org-match-substring-regexp
  4779. (concat
  4780. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4781. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4782. "\\|"
  4783. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4784. "\\|"
  4785. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4786. "The regular expression matching a sub- or superscript.")
  4787. (defvar org-match-substring-with-braces-regexp
  4788. (concat
  4789. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4790. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4791. "\\)")
  4792. "The regular expression matching a sub- or superscript, forcing braces.")
  4793. (defun org-make-link-regexps ()
  4794. "Update the link regular expressions.
  4795. This should be called after the variable `org-link-types' has changed."
  4796. (setq org-link-types-re
  4797. (concat
  4798. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4799. org-link-re-with-space
  4800. (concat
  4801. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4802. "\\([^" org-non-link-chars " ]"
  4803. "[^" org-non-link-chars "]*"
  4804. "[^" org-non-link-chars " ]\\)>?")
  4805. org-link-re-with-space2
  4806. (concat
  4807. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4808. "\\([^" org-non-link-chars " ]"
  4809. "[^\t\n\r]*"
  4810. "[^" org-non-link-chars " ]\\)>?")
  4811. org-link-re-with-space3
  4812. (concat
  4813. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4814. "\\([^" org-non-link-chars " ]"
  4815. "[^\t\n\r]*\\)")
  4816. org-angle-link-re
  4817. (concat
  4818. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4819. "\\([^" org-non-link-chars " ]"
  4820. "[^" org-non-link-chars "]*"
  4821. "\\)>")
  4822. org-plain-link-re
  4823. (concat
  4824. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4825. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4826. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4827. org-bracket-link-regexp
  4828. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4829. org-bracket-link-analytic-regexp
  4830. (concat
  4831. "\\[\\["
  4832. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4833. "\\([^]]+\\)"
  4834. "\\]"
  4835. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4836. "\\]")
  4837. org-bracket-link-analytic-regexp++
  4838. (concat
  4839. "\\[\\["
  4840. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4841. "\\([^]]+\\)"
  4842. "\\]"
  4843. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4844. "\\]")
  4845. org-any-link-re
  4846. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4847. org-angle-link-re "\\)\\|\\("
  4848. org-plain-link-re "\\)")))
  4849. (org-make-link-regexps)
  4850. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4851. "Regular expression for fast time stamp matching.")
  4852. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4853. "Regular expression for fast time stamp matching.")
  4854. (defconst org-ts-regexp0
  4855. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4856. "Regular expression matching time strings for analysis.
  4857. This one does not require the space after the date, so it can be used
  4858. on a string that terminates immediately after the date.")
  4859. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4860. "Regular expression matching time strings for analysis.")
  4861. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4862. "Regular expression matching time stamps, with groups.")
  4863. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4864. "Regular expression matching time stamps (also [..]), with groups.")
  4865. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4866. "Regular expression matching a time stamp range.")
  4867. (defconst org-tr-regexp-both
  4868. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4869. "Regular expression matching a time stamp range.")
  4870. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4871. org-ts-regexp "\\)?")
  4872. "Regular expression matching a time stamp or time stamp range.")
  4873. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4874. org-ts-regexp-both "\\)?")
  4875. "Regular expression matching a time stamp or time stamp range.
  4876. The time stamps may be either active or inactive.")
  4877. (defvar org-emph-face nil)
  4878. (defun org-do-emphasis-faces (limit)
  4879. "Run through the buffer and add overlays to emphasized strings."
  4880. (let (rtn a)
  4881. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4882. (if (not (= (char-after (match-beginning 3))
  4883. (char-after (match-beginning 4))))
  4884. (progn
  4885. (setq rtn t)
  4886. (setq a (assoc (match-string 3) org-emphasis-alist))
  4887. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4888. 'face
  4889. (nth 1 a))
  4890. (and (nth 4 a)
  4891. (org-remove-flyspell-overlays-in
  4892. (match-beginning 0) (match-end 0)))
  4893. (add-text-properties (match-beginning 2) (match-end 2)
  4894. '(font-lock-multiline t org-emphasis t))
  4895. (when org-hide-emphasis-markers
  4896. (add-text-properties (match-end 4) (match-beginning 5)
  4897. '(invisible org-link))
  4898. (add-text-properties (match-beginning 3) (match-end 3)
  4899. '(invisible org-link)))))
  4900. (backward-char 1))
  4901. rtn))
  4902. (defun org-emphasize (&optional char)
  4903. "Insert or change an emphasis, i.e. a font like bold or italic.
  4904. If there is an active region, change that region to a new emphasis.
  4905. If there is no region, just insert the marker characters and position
  4906. the cursor between them.
  4907. CHAR should be either the marker character, or the first character of the
  4908. HTML tag associated with that emphasis. If CHAR is a space, the means
  4909. to remove the emphasis of the selected region.
  4910. If char is not given (for example in an interactive call) it
  4911. will be prompted for."
  4912. (interactive)
  4913. (let ((eal org-emphasis-alist) e det
  4914. (erc org-emphasis-regexp-components)
  4915. (prompt "")
  4916. (string "") beg end move tag c s)
  4917. (if (org-region-active-p)
  4918. (setq beg (region-beginning) end (region-end)
  4919. string (buffer-substring beg end))
  4920. (setq move t))
  4921. (while (setq e (pop eal))
  4922. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4923. c (aref tag 0))
  4924. (push (cons c (string-to-char (car e))) det)
  4925. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4926. (substring tag 1)))))
  4927. (setq det (nreverse det))
  4928. (unless char
  4929. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4930. (setq char (read-char-exclusive)))
  4931. (setq char (or (cdr (assoc char det)) char))
  4932. (if (equal char ?\ )
  4933. (setq s "" move nil)
  4934. (unless (assoc (char-to-string char) org-emphasis-alist)
  4935. (error "No such emphasis marker: \"%c\"" char))
  4936. (setq s (char-to-string char)))
  4937. (while (and (> (length string) 1)
  4938. (equal (substring string 0 1) (substring string -1))
  4939. (assoc (substring string 0 1) org-emphasis-alist))
  4940. (setq string (substring string 1 -1)))
  4941. (setq string (concat s string s))
  4942. (if beg (delete-region beg end))
  4943. (unless (or (bolp)
  4944. (string-match (concat "[" (nth 0 erc) "\n]")
  4945. (char-to-string (char-before (point)))))
  4946. (insert " "))
  4947. (unless (or (eobp)
  4948. (string-match (concat "[" (nth 1 erc) "\n]")
  4949. (char-to-string (char-after (point)))))
  4950. (insert " ") (backward-char 1))
  4951. (insert string)
  4952. (and move (backward-char 1))))
  4953. (defconst org-nonsticky-props
  4954. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4955. (defsubst org-rear-nonsticky-at (pos)
  4956. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4957. (defun org-activate-plain-links (limit)
  4958. "Run through the buffer and add overlays to links."
  4959. (catch 'exit
  4960. (let (f hl)
  4961. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4962. (not (org-in-src-block-p)))
  4963. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4964. (setq f (get-text-property (match-beginning 0) 'face))
  4965. (setq hl (org-match-string-no-properties 0))
  4966. (if (or (eq f 'org-tag)
  4967. (and (listp f) (memq 'org-tag f)))
  4968. nil
  4969. (add-text-properties (match-beginning 0) (match-end 0)
  4970. (list 'mouse-face 'highlight
  4971. 'face 'org-link
  4972. 'htmlize-link `(:uri ,hl)
  4973. 'keymap org-mouse-map))
  4974. (org-rear-nonsticky-at (match-end 0)))
  4975. t))))
  4976. (defun org-activate-code (limit)
  4977. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4978. (progn
  4979. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4980. (remove-text-properties (match-beginning 0) (match-end 0)
  4981. '(display t invisible t intangible t))
  4982. t)))
  4983. (defcustom org-src-fontify-natively nil
  4984. "When non-nil, fontify code in code blocks."
  4985. :type 'boolean
  4986. :version "24.1"
  4987. :group 'org-appearance
  4988. :group 'org-babel)
  4989. (defcustom org-allow-promoting-top-level-subtree nil
  4990. "When non-nil, allow promoting a top level subtree.
  4991. The leading star of the top level headline will be replaced
  4992. by a #."
  4993. :type 'boolean
  4994. :version "24.1"
  4995. :group 'org-appearance)
  4996. (defun org-fontify-meta-lines-and-blocks (limit)
  4997. (condition-case nil
  4998. (org-fontify-meta-lines-and-blocks-1 limit)
  4999. (error (message "org-mode fontification error"))))
  5000. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5001. "Fontify #+ lines and blocks, in the correct ways."
  5002. (let ((case-fold-search t))
  5003. (if (re-search-forward
  5004. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5005. limit t)
  5006. (let ((beg (match-beginning 0))
  5007. (block-start (match-end 0))
  5008. (block-end nil)
  5009. (lang (match-string 7))
  5010. (beg1 (line-beginning-position 2))
  5011. (dc1 (downcase (match-string 2)))
  5012. (dc3 (downcase (match-string 3)))
  5013. end end1 quoting block-type ovl)
  5014. (cond
  5015. ((member dc1 '("+html:" "+ascii:" "+latex:"))
  5016. ;; a single line of backend-specific content
  5017. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5018. (remove-text-properties (match-beginning 0) (match-end 0)
  5019. '(display t invisible t intangible t))
  5020. (add-text-properties (match-beginning 1) (match-end 3)
  5021. '(font-lock-fontified t face org-meta-line))
  5022. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5023. '(font-lock-fontified t face org-block))
  5024. ; for backend-specific code
  5025. t)
  5026. ((and (match-end 4) (equal dc3 "+begin"))
  5027. ;; Truly a block
  5028. (setq block-type (downcase (match-string 5))
  5029. quoting (member block-type org-protecting-blocks))
  5030. (when (re-search-forward
  5031. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5032. nil t) ;; on purpose, we look further than LIMIT
  5033. (setq end (min (point-max) (match-end 0))
  5034. end1 (min (point-max) (1- (match-beginning 0))))
  5035. (setq block-end (match-beginning 0))
  5036. (when quoting
  5037. (remove-text-properties beg end
  5038. '(display t invisible t intangible t)))
  5039. (add-text-properties
  5040. beg end
  5041. '(font-lock-fontified t font-lock-multiline t))
  5042. (add-text-properties beg beg1 '(face org-meta-line))
  5043. (add-text-properties end1 (min (point-max) (1+ end))
  5044. '(face org-meta-line)) ; for end_src
  5045. (cond
  5046. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5047. (org-src-font-lock-fontify-block lang block-start block-end)
  5048. ;; remove old background overlays
  5049. (mapc (lambda (ov)
  5050. (if (eq (overlay-get ov 'face) 'org-block-background)
  5051. (delete-overlay ov)))
  5052. (overlays-at (/ (+ beg1 block-end) 2)))
  5053. ;; add a background overlay
  5054. (setq ovl (make-overlay beg1 block-end))
  5055. (overlay-put ovl 'face 'org-block-background)
  5056. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5057. (quoting
  5058. (add-text-properties beg1 (min (point-max) (1+ end1))
  5059. '(face org-block))) ; end of source block
  5060. ((not org-fontify-quote-and-verse-blocks))
  5061. ((string= block-type "quote")
  5062. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5063. ((string= block-type "verse")
  5064. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5065. (add-text-properties beg beg1 '(face org-block-begin-line))
  5066. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5067. '(face org-block-end-line))
  5068. t))
  5069. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5070. (add-text-properties
  5071. beg (match-end 3)
  5072. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5073. '(font-lock-fontified t invisible t)
  5074. '(font-lock-fontified t face org-document-info-keyword)))
  5075. (add-text-properties
  5076. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5077. (if (string-equal dc1 "+title:")
  5078. '(font-lock-fontified t face org-document-title)
  5079. '(font-lock-fontified t face org-document-info))))
  5080. ((or (equal dc1 "+results")
  5081. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5082. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5083. "+call:" "+header:" "+headers:" "+name:"))
  5084. (and (match-end 4) (equal dc3 "+attr")))
  5085. (add-text-properties
  5086. beg (match-end 0)
  5087. '(font-lock-fontified t face org-meta-line))
  5088. t)
  5089. ((member dc3 '(" " ""))
  5090. (add-text-properties
  5091. beg (match-end 0)
  5092. '(font-lock-fontified t face font-lock-comment-face)))
  5093. ((not (member (char-after beg) '(?\ ?\t)))
  5094. ;; just any other in-buffer setting, but not indented
  5095. (add-text-properties
  5096. beg (match-end 0)
  5097. '(font-lock-fontified t face org-meta-line))
  5098. t)
  5099. (t nil))))))
  5100. (defun org-activate-angle-links (limit)
  5101. "Run through the buffer and add overlays to links."
  5102. (if (and (re-search-forward org-angle-link-re limit t)
  5103. (not (org-in-src-block-p)))
  5104. (progn
  5105. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5106. (add-text-properties (match-beginning 0) (match-end 0)
  5107. (list 'mouse-face 'highlight
  5108. 'keymap org-mouse-map))
  5109. (org-rear-nonsticky-at (match-end 0))
  5110. t)))
  5111. (defun org-activate-footnote-links (limit)
  5112. "Run through the buffer and add overlays to footnotes."
  5113. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5114. (when fn
  5115. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5116. (org-remove-flyspell-overlays-in beg end)
  5117. (add-text-properties beg end
  5118. (list 'mouse-face 'highlight
  5119. 'keymap org-mouse-map
  5120. 'help-echo
  5121. (if (= (point-at-bol) beg)
  5122. "Footnote definition"
  5123. "Footnote reference")
  5124. 'font-lock-fontified t
  5125. 'font-lock-multiline t
  5126. 'face 'org-footnote))))))
  5127. (defun org-activate-bracket-links (limit)
  5128. "Run through the buffer and add overlays to bracketed links."
  5129. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5130. (not (org-in-src-block-p)))
  5131. (let* ((hl (org-match-string-no-properties 1))
  5132. (help (concat "LINK: " hl))
  5133. ;; FIXME: Above we should remove the escapes. But that
  5134. ;; requires another match, protecting match data, a lot
  5135. ;; of overhead for font-lock.
  5136. (ip (org-maybe-intangible
  5137. (list 'invisible 'org-link
  5138. 'keymap org-mouse-map 'mouse-face 'highlight
  5139. 'font-lock-multiline t 'help-echo help
  5140. 'htmlize-link `(:uri ,hl))))
  5141. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5142. 'font-lock-multiline t 'help-echo help
  5143. 'htmlize-link `(:uri ,hl))))
  5144. ;; We need to remove the invisible property here. Table narrowing
  5145. ;; may have made some of this invisible.
  5146. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5147. (remove-text-properties (match-beginning 0) (match-end 0)
  5148. '(invisible nil))
  5149. (if (match-end 3)
  5150. (progn
  5151. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5152. (org-rear-nonsticky-at (match-beginning 3))
  5153. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5154. (org-rear-nonsticky-at (match-end 3))
  5155. (add-text-properties (match-end 3) (match-end 0) ip)
  5156. (org-rear-nonsticky-at (match-end 0)))
  5157. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5158. (org-rear-nonsticky-at (match-beginning 1))
  5159. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5160. (org-rear-nonsticky-at (match-end 1))
  5161. (add-text-properties (match-end 1) (match-end 0) ip)
  5162. (org-rear-nonsticky-at (match-end 0)))
  5163. t)))
  5164. (defun org-activate-dates (limit)
  5165. "Run through the buffer and add overlays to dates."
  5166. (if (re-search-forward org-tsr-regexp-both limit t)
  5167. (progn
  5168. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5169. (add-text-properties (match-beginning 0) (match-end 0)
  5170. (list 'mouse-face 'highlight
  5171. 'keymap org-mouse-map))
  5172. (org-rear-nonsticky-at (match-end 0))
  5173. (when org-display-custom-times
  5174. (if (match-end 3)
  5175. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5176. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5177. t)))
  5178. (defvar org-target-link-regexp nil
  5179. "Regular expression matching radio targets in plain text.")
  5180. (make-variable-buffer-local 'org-target-link-regexp)
  5181. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5182. "Regular expression matching a link target.")
  5183. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5184. "Regular expression matching a radio target.")
  5185. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5186. "Regular expression matching any target.")
  5187. (defun org-activate-target-links (limit)
  5188. "Run through the buffer and add overlays to target matches."
  5189. (when org-target-link-regexp
  5190. (let ((case-fold-search t))
  5191. (if (re-search-forward org-target-link-regexp limit t)
  5192. (progn
  5193. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5194. (add-text-properties (match-beginning 0) (match-end 0)
  5195. (list 'mouse-face 'highlight
  5196. 'keymap org-mouse-map
  5197. 'help-echo "Radio target link"
  5198. 'org-linked-text t))
  5199. (org-rear-nonsticky-at (match-end 0))
  5200. t)))))
  5201. (defun org-update-radio-target-regexp ()
  5202. "Find all radio targets in this file and update the regular expression."
  5203. (interactive)
  5204. (when (memq 'radio org-activate-links)
  5205. (setq org-target-link-regexp
  5206. (org-make-target-link-regexp (org-all-targets 'radio)))
  5207. (org-restart-font-lock)))
  5208. (defun org-hide-wide-columns (limit)
  5209. (let (s e)
  5210. (setq s (text-property-any (point) (or limit (point-max))
  5211. 'org-cwidth t))
  5212. (when s
  5213. (setq e (next-single-property-change s 'org-cwidth))
  5214. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5215. (goto-char e)
  5216. t)))
  5217. (defvar org-latex-and-related-regexp nil
  5218. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5219. (defvar org-match-substring-regexp)
  5220. (defvar org-match-substring-with-braces-regexp)
  5221. (defun org-compute-latex-and-related-regexp ()
  5222. "Compute regular expression for LaTeX, entities and sub/superscript.
  5223. Result depends on variable `org-highlight-latex-and-related'."
  5224. (org-set-local
  5225. 'org-latex-and-related-regexp
  5226. (let* ((re-sub
  5227. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5228. ((eq org-use-sub-superscripts '{})
  5229. (list org-match-substring-with-braces-regexp))
  5230. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5231. (re-latex
  5232. (when (memq 'latex org-highlight-latex-and-related)
  5233. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5234. (delq nil
  5235. (mapcar (lambda (x)
  5236. (and (member (car x) matchers) (nth 1 x)))
  5237. org-latex-regexps)))))
  5238. (re-entities
  5239. (when (memq 'entities org-highlight-latex-and-related)
  5240. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5241. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5242. (defun org-do-latex-and-related (limit)
  5243. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5244. LIMIT bounds the search for syntax to highlight. Stop at first
  5245. highlighted object, if any. Return t if some highlighting was
  5246. done, nil otherwise."
  5247. (when org-highlight-latex-and-related
  5248. (catch 'found
  5249. (while (re-search-forward org-latex-and-related-regexp limit t)
  5250. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5251. 'face))
  5252. '(org-code org-verbatim underline))
  5253. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5254. '(?_ ?^))
  5255. 1
  5256. 0)))
  5257. (font-lock-prepend-text-property
  5258. (+ offset (match-beginning 0)) (match-end 0)
  5259. 'face 'org-latex-and-related)
  5260. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5261. '(font-lock-multiline t)))
  5262. (throw 'found t)))
  5263. nil)))
  5264. (defun org-restart-font-lock ()
  5265. "Restart `font-lock-mode', to force refontification."
  5266. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5267. (font-lock-mode -1)
  5268. (font-lock-mode 1)))
  5269. (defun org-all-targets (&optional radio)
  5270. "Return a list of all targets in this file.
  5271. When optional argument RADIO is non-nil, only find radio
  5272. targets."
  5273. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5274. (save-excursion
  5275. (goto-char (point-min))
  5276. (while (re-search-forward re nil t)
  5277. ;; Make sure point is really within the object.
  5278. (backward-char)
  5279. (let ((obj (org-element-context)))
  5280. (when (memq (org-element-type obj) '(radio-target target))
  5281. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5282. rtn)))
  5283. (defun org-make-target-link-regexp (targets)
  5284. "Make regular expression matching all strings in TARGETS.
  5285. The regular expression finds the targets also if there is a line break
  5286. between words."
  5287. (and targets
  5288. (concat
  5289. "\\<\\("
  5290. (mapconcat
  5291. (lambda (x)
  5292. (setq x (regexp-quote x))
  5293. (while (string-match " +" x)
  5294. (setq x (replace-match "\\s-+" t t x)))
  5295. x)
  5296. targets
  5297. "\\|")
  5298. "\\)\\>")))
  5299. (defun org-activate-tags (limit)
  5300. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5301. (progn
  5302. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5303. (add-text-properties (match-beginning 1) (match-end 1)
  5304. (list 'mouse-face 'highlight
  5305. 'keymap org-mouse-map))
  5306. (org-rear-nonsticky-at (match-end 1))
  5307. t)))
  5308. (defun org-outline-level ()
  5309. "Compute the outline level of the heading at point.
  5310. If this is called at a normal headline, the level is the number of stars.
  5311. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5312. (save-excursion
  5313. (if (not (condition-case nil
  5314. (org-back-to-heading t)
  5315. (error nil)))
  5316. 0
  5317. (looking-at org-outline-regexp)
  5318. (1- (- (match-end 0) (match-beginning 0))))))
  5319. (defvar org-font-lock-keywords nil)
  5320. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5321. "Regular expression matching a property line.")
  5322. (defvar org-font-lock-hook nil
  5323. "Functions to be called for special font lock stuff.")
  5324. (defvar org-font-lock-set-keywords-hook nil
  5325. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5326. This is called after `org-font-lock-extra-keywords' is defined, but before
  5327. it is installed to be used by font lock. This can be useful if something
  5328. needs to be inserted at a specific position in the font-lock sequence.")
  5329. (defun org-font-lock-hook (limit)
  5330. "Run `org-font-lock-hook' within LIMIT."
  5331. (run-hook-with-args 'org-font-lock-hook limit))
  5332. (defun org-set-font-lock-defaults ()
  5333. "Set font lock defaults for the current buffer."
  5334. (let* ((em org-fontify-emphasized-text)
  5335. (lk org-activate-links)
  5336. (org-font-lock-extra-keywords
  5337. (list
  5338. ;; Call the hook
  5339. '(org-font-lock-hook)
  5340. ;; Headlines
  5341. `(,(if org-fontify-whole-heading-line
  5342. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5343. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5344. (1 (org-get-level-face 1))
  5345. (2 (org-get-level-face 2))
  5346. (3 (org-get-level-face 3)))
  5347. ;; Table lines
  5348. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5349. (1 'org-table t))
  5350. ;; Table internals
  5351. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5352. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5353. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5354. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5355. ;; Drawers
  5356. (list org-drawer-regexp '(0 'org-special-keyword t))
  5357. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5358. ;; Properties
  5359. (list org-property-re
  5360. '(1 'org-special-keyword t)
  5361. '(3 'org-property-value t))
  5362. ;; Links
  5363. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5364. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5365. (if (memq 'plain lk) '(org-activate-plain-links))
  5366. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5367. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5368. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5369. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5370. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5371. '(org-hide-wide-columns (0 nil append))
  5372. ;; TODO keyword
  5373. (list (format org-heading-keyword-regexp-format
  5374. org-todo-regexp)
  5375. '(2 (org-get-todo-face 2) t))
  5376. ;; DONE
  5377. (if org-fontify-done-headline
  5378. (list (format org-heading-keyword-regexp-format
  5379. (concat
  5380. "\\(?:"
  5381. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5382. "\\)"))
  5383. '(2 'org-headline-done t))
  5384. nil)
  5385. ;; Priorities
  5386. '(org-font-lock-add-priority-faces)
  5387. ;; Tags
  5388. '(org-font-lock-add-tag-faces)
  5389. ;; Special keywords
  5390. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5391. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5392. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5393. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5394. ;; Emphasis
  5395. (if em
  5396. (if (featurep 'xemacs)
  5397. '(org-do-emphasis-faces (0 nil append))
  5398. '(org-do-emphasis-faces)))
  5399. ;; Checkboxes
  5400. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5401. 1 'org-checkbox prepend)
  5402. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5403. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5404. (0 (org-get-checkbox-statistics-face) t)))
  5405. ;; Description list items
  5406. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5407. 1 'org-list-dt prepend)
  5408. ;; ARCHIVEd headings
  5409. (list (concat
  5410. org-outline-regexp-bol
  5411. "\\(.*:" org-archive-tag ":.*\\)")
  5412. '(1 'org-archived prepend))
  5413. ;; Specials
  5414. '(org-do-latex-and-related)
  5415. '(org-fontify-entities)
  5416. '(org-raise-scripts)
  5417. ;; Code
  5418. '(org-activate-code (1 'org-code t))
  5419. ;; COMMENT
  5420. (list (format org-heading-keyword-regexp-format
  5421. (concat "\\("
  5422. org-comment-string "\\|" org-quote-string
  5423. "\\)"))
  5424. '(2 'org-special-keyword t))
  5425. ;; Blocks and meta lines
  5426. '(org-fontify-meta-lines-and-blocks)
  5427. )))
  5428. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5429. (run-hooks 'org-font-lock-set-keywords-hook)
  5430. ;; Now set the full font-lock-keywords
  5431. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5432. (org-set-local 'font-lock-defaults
  5433. '(org-font-lock-keywords t nil nil backward-paragraph))
  5434. (kill-local-variable 'font-lock-keywords) nil))
  5435. (defun org-toggle-pretty-entities ()
  5436. "Toggle the composition display of entities as UTF8 characters."
  5437. (interactive)
  5438. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5439. (org-restart-font-lock)
  5440. (if org-pretty-entities
  5441. (message "Entities are displayed as UTF8 characters")
  5442. (save-restriction
  5443. (widen)
  5444. (org-decompose-region (point-min) (point-max))
  5445. (message "Entities are displayed plain"))))
  5446. (defvar org-custom-properties-overlays nil
  5447. "List of overlays used for custom properties.")
  5448. (make-variable-buffer-local 'org-custom-properties-overlays)
  5449. (defun org-toggle-custom-properties-visibility ()
  5450. "Display or hide properties in `org-custom-properties'."
  5451. (interactive)
  5452. (if org-custom-properties-overlays
  5453. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5454. (setq org-custom-properties-overlays nil))
  5455. (unless (not org-custom-properties)
  5456. (save-excursion
  5457. (save-restriction
  5458. (widen)
  5459. (goto-char (point-min))
  5460. (while (re-search-forward org-property-re nil t)
  5461. (mapc (lambda(p)
  5462. (when (equal p (substring (match-string 1) 1 -1))
  5463. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5464. (overlay-put o 'invisible t)
  5465. (overlay-put o 'org-custom-property t)
  5466. (push o org-custom-properties-overlays))))
  5467. org-custom-properties)))))))
  5468. (defun org-fontify-entities (limit)
  5469. "Find an entity to fontify."
  5470. (let (ee)
  5471. (when org-pretty-entities
  5472. (catch 'match
  5473. (while (re-search-forward
  5474. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5475. limit t)
  5476. (if (and (not (org-in-indented-comment-line))
  5477. (setq ee (org-entity-get (match-string 1)))
  5478. (= (length (nth 6 ee)) 1))
  5479. (let*
  5480. ((end (if (equal (match-string 2) "{}")
  5481. (match-end 2)
  5482. (match-end 1))))
  5483. (add-text-properties
  5484. (match-beginning 0) end
  5485. (list 'font-lock-fontified t))
  5486. (compose-region (match-beginning 0) end
  5487. (nth 6 ee) nil)
  5488. (backward-char 1)
  5489. (throw 'match t))))
  5490. nil))))
  5491. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5492. "Fontify string S like in Org-mode."
  5493. (with-temp-buffer
  5494. (insert s)
  5495. (let ((org-odd-levels-only odd-levels))
  5496. (org-mode)
  5497. (font-lock-fontify-buffer)
  5498. (buffer-string))))
  5499. (defvar org-m nil)
  5500. (defvar org-l nil)
  5501. (defvar org-f nil)
  5502. (defun org-get-level-face (n)
  5503. "Get the right face for match N in font-lock matching of headlines."
  5504. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5505. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5506. (if org-cycle-level-faces
  5507. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5508. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5509. (cond
  5510. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5511. ((eq n 2) org-f)
  5512. (t (if org-level-color-stars-only nil org-f))))
  5513. (defun org-get-todo-face (kwd)
  5514. "Get the right face for a TODO keyword KWD.
  5515. If KWD is a number, get the corresponding match group."
  5516. (if (numberp kwd) (setq kwd (match-string kwd)))
  5517. (or (org-face-from-face-or-color
  5518. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5519. (and (member kwd org-done-keywords) 'org-done)
  5520. 'org-todo))
  5521. (defun org-face-from-face-or-color (context inherit face-or-color)
  5522. "Create a face list that inherits INHERIT, but sets the foreground color.
  5523. When FACE-OR-COLOR is not a string, just return it."
  5524. (if (stringp face-or-color)
  5525. (list :inherit inherit
  5526. (cdr (assoc context org-faces-easy-properties))
  5527. face-or-color)
  5528. face-or-color))
  5529. (defun org-font-lock-add-tag-faces (limit)
  5530. "Add the special tag faces."
  5531. (when (and org-tag-faces org-tags-special-faces-re)
  5532. (while (re-search-forward org-tags-special-faces-re limit t)
  5533. (add-text-properties (match-beginning 1) (match-end 1)
  5534. (list 'face (org-get-tag-face 1)
  5535. 'font-lock-fontified t))
  5536. (backward-char 1))))
  5537. (defun org-font-lock-add-priority-faces (limit)
  5538. "Add the special priority faces."
  5539. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5540. (when (save-match-data (org-at-heading-p))
  5541. (add-text-properties
  5542. (match-beginning 0) (match-end 0)
  5543. (list 'face (or (org-face-from-face-or-color
  5544. 'priority 'org-priority
  5545. (cdr (assoc (char-after (match-beginning 1))
  5546. org-priority-faces)))
  5547. 'org-priority)
  5548. 'font-lock-fontified t)))))
  5549. (defun org-get-tag-face (kwd)
  5550. "Get the right face for a TODO keyword KWD.
  5551. If KWD is a number, get the corresponding match group."
  5552. (if (numberp kwd) (setq kwd (match-string kwd)))
  5553. (or (org-face-from-face-or-color
  5554. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5555. 'org-tag))
  5556. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5557. "Remove fontification and activation overlays from links."
  5558. (font-lock-default-unfontify-region beg end)
  5559. (let* ((buffer-undo-list t)
  5560. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5561. (inhibit-modification-hooks t)
  5562. deactivate-mark buffer-file-name buffer-file-truename)
  5563. (org-decompose-region beg end)
  5564. (remove-text-properties beg end
  5565. '(mouse-face t keymap t org-linked-text t
  5566. invisible t intangible t
  5567. org-no-flyspell t org-emphasis t))
  5568. (org-remove-font-lock-display-properties beg end)))
  5569. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5570. ((raise 0.3) (height 0.7))
  5571. ((raise -0.5))
  5572. ((raise 0.5)))
  5573. "Display properties for showing superscripts and subscripts.")
  5574. (defun org-remove-font-lock-display-properties (beg end)
  5575. "Remove specific display properties that have been added by font lock.
  5576. The will remove the raise properties that are used to show superscripts
  5577. and subscripts."
  5578. (let (next prop)
  5579. (while (< beg end)
  5580. (setq next (next-single-property-change beg 'display nil end)
  5581. prop (get-text-property beg 'display))
  5582. (if (member prop org-script-display)
  5583. (put-text-property beg next 'display nil))
  5584. (setq beg next))))
  5585. (defun org-raise-scripts (limit)
  5586. "Add raise properties to sub/superscripts."
  5587. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5588. (if (re-search-forward
  5589. (if (eq org-use-sub-superscripts t)
  5590. org-match-substring-regexp
  5591. org-match-substring-with-braces-regexp)
  5592. limit t)
  5593. (let* ((pos (point)) table-p comment-p
  5594. (mpos (match-beginning 3))
  5595. (emph-p (get-text-property mpos 'org-emphasis))
  5596. (link-p (get-text-property mpos 'mouse-face))
  5597. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5598. (goto-char (point-at-bol))
  5599. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5600. comment-p (org-looking-at-p "[ \t]*#"))
  5601. (goto-char pos)
  5602. ;; FIXME: Should we go back one character here, for a_b^c
  5603. ;; (goto-char (1- pos)) ;????????????????????
  5604. (if (or comment-p emph-p link-p keyw-p)
  5605. t
  5606. (put-text-property (match-beginning 3) (match-end 0)
  5607. 'display
  5608. (if (equal (char-after (match-beginning 2)) ?^)
  5609. (nth (if table-p 3 1) org-script-display)
  5610. (nth (if table-p 2 0) org-script-display)))
  5611. (add-text-properties (match-beginning 2) (match-end 2)
  5612. (list 'invisible t
  5613. 'org-dwidth t 'org-dwidth-n 1))
  5614. (if (and (eq (char-after (match-beginning 3)) ?{)
  5615. (eq (char-before (match-end 3)) ?}))
  5616. (progn
  5617. (add-text-properties
  5618. (match-beginning 3) (1+ (match-beginning 3))
  5619. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5620. (add-text-properties
  5621. (1- (match-end 3)) (match-end 3)
  5622. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5623. t)))))
  5624. ;;;; Visibility cycling, including org-goto and indirect buffer
  5625. ;;; Cycling
  5626. (defvar org-cycle-global-status nil)
  5627. (make-variable-buffer-local 'org-cycle-global-status)
  5628. (put 'org-cycle-global-status 'org-state t)
  5629. (defvar org-cycle-subtree-status nil)
  5630. (make-variable-buffer-local 'org-cycle-subtree-status)
  5631. (put 'org-cycle-subtree-status 'org-state t)
  5632. (defvar org-inlinetask-min-level)
  5633. ;;;###autoload
  5634. (defun org-cycle (&optional arg)
  5635. "TAB-action and visibility cycling for Org-mode.
  5636. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5637. is outline visibility cycling, but it also invokes other actions
  5638. in special contexts.
  5639. - When this function is called with a prefix argument, rotate the entire
  5640. buffer through 3 states (global cycling)
  5641. 1. OVERVIEW: Show only top-level headlines.
  5642. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5643. 3. SHOW ALL: Show everything.
  5644. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5645. determined by the variable `org-startup-folded', and by any VISIBILITY
  5646. properties in the buffer.
  5647. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5648. including any drawers.
  5649. - When inside a table, re-align the table and move to the next field.
  5650. - When point is at the beginning of a headline, rotate the subtree started
  5651. by this line through 3 different states (local cycling)
  5652. 1. FOLDED: Only the main headline is shown.
  5653. 2. CHILDREN: The main headline and the direct children are shown.
  5654. From this state, you can move to one of the children
  5655. and zoom in further.
  5656. 3. SUBTREE: Show the entire subtree, including body text.
  5657. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5658. - When point is at the beginning of an empty headline and the variable
  5659. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5660. of the headline by demoting and promoting it to likely levels. This
  5661. speeds up creation document structure by pressing TAB once or several
  5662. times right after creating a new headline.
  5663. - When there is a numeric prefix, go up to a heading with level ARG, do
  5664. a `show-subtree' and return to the previous cursor position. If ARG
  5665. is negative, go up that many levels.
  5666. - When point is not at the beginning of a headline, execute the global
  5667. binding for TAB, which is re-indenting the line. See the option
  5668. `org-cycle-emulate-tab' for details.
  5669. - Special case: if point is at the beginning of the buffer and there is
  5670. no headline in line 1, this function will act as if called with prefix arg
  5671. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5672. But only if also the variable `org-cycle-global-at-bob' is t."
  5673. (interactive "P")
  5674. (org-load-modules-maybe)
  5675. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5676. (and org-cycle-level-after-item/entry-creation
  5677. (or (org-cycle-level)
  5678. (org-cycle-item-indentation))))
  5679. (let* (message-log-max ; Don't populate the *Messages* buffer
  5680. (limit-level
  5681. (or org-cycle-max-level
  5682. (and (boundp 'org-inlinetask-min-level)
  5683. org-inlinetask-min-level
  5684. (1- org-inlinetask-min-level))))
  5685. (nstars (and limit-level
  5686. (if org-odd-levels-only
  5687. (and limit-level (1- (* limit-level 2)))
  5688. limit-level)))
  5689. (org-outline-regexp
  5690. (if (not (derived-mode-p 'org-mode))
  5691. outline-regexp
  5692. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5693. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5694. (not (looking-at org-outline-regexp))))
  5695. (org-cycle-hook
  5696. (if bob-special
  5697. (delq 'org-optimize-window-after-visibility-change
  5698. (copy-sequence org-cycle-hook))
  5699. org-cycle-hook))
  5700. (pos (point)))
  5701. (if (or bob-special (equal arg '(4)))
  5702. ;; special case: use global cycling
  5703. (setq arg t))
  5704. (cond
  5705. ((equal arg '(16))
  5706. (setq last-command 'dummy)
  5707. (org-set-startup-visibility)
  5708. (message "Startup visibility, plus VISIBILITY properties"))
  5709. ((equal arg '(64))
  5710. (show-all)
  5711. (message "Entire buffer visible, including drawers"))
  5712. ;; Table: enter it or move to the next field.
  5713. ((org-at-table-p 'any)
  5714. (if (org-at-table.el-p)
  5715. (message "Use C-c ' to edit table.el tables")
  5716. (if arg (org-table-edit-field t)
  5717. (org-table-justify-field-maybe)
  5718. (call-interactively 'org-table-next-field))))
  5719. ((run-hook-with-args-until-success
  5720. 'org-tab-after-check-for-table-hook))
  5721. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5722. ((eq arg t) (org-cycle-internal-global))
  5723. ;; Drawers: delegate to `org-flag-drawer'.
  5724. ((and org-drawers org-drawer-regexp
  5725. (save-excursion
  5726. (beginning-of-line 1)
  5727. (looking-at org-drawer-regexp)))
  5728. (org-flag-drawer ; toggle block visibility
  5729. (not (get-char-property (match-end 0) 'invisible))))
  5730. ;; Show-subtree, ARG levels up from here.
  5731. ((integerp arg)
  5732. (save-excursion
  5733. (org-back-to-heading)
  5734. (outline-up-heading (if (< arg 0) (- arg)
  5735. (- (funcall outline-level) arg)))
  5736. (org-show-subtree)))
  5737. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5738. ((and (featurep 'org-inlinetask)
  5739. (org-inlinetask-at-task-p)
  5740. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5741. (org-inlinetask-toggle-visibility))
  5742. ((org-try-cdlatex-tab))
  5743. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5744. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5745. (save-excursion (beginning-of-line 1)
  5746. (looking-at org-outline-regexp)))
  5747. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5748. (org-cycle-internal-local))
  5749. ;; From there: TAB emulation and template completion.
  5750. (buffer-read-only (org-back-to-heading))
  5751. ((run-hook-with-args-until-success
  5752. 'org-tab-after-check-for-cycling-hook))
  5753. ((org-try-structure-completion))
  5754. ((run-hook-with-args-until-success
  5755. 'org-tab-before-tab-emulation-hook))
  5756. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5757. (or (not (bolp))
  5758. (not (looking-at org-outline-regexp))))
  5759. (call-interactively (global-key-binding "\t")))
  5760. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5761. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5762. (or (and (eq org-cycle-emulate-tab 'white)
  5763. (= (match-end 0) (point-at-eol)))
  5764. (and (eq org-cycle-emulate-tab 'whitestart)
  5765. (>= (match-end 0) pos))))
  5766. t
  5767. (eq org-cycle-emulate-tab t))
  5768. (call-interactively (global-key-binding "\t")))
  5769. (t (save-excursion
  5770. (org-back-to-heading)
  5771. (org-cycle)))))))
  5772. (defun org-cycle-internal-global ()
  5773. "Do the global cycling action."
  5774. ;; Hack to avoid display of messages for .org attachments in Gnus
  5775. (let (message-log-max ; Don't populate the *Messages* buffer
  5776. (ga (string-match "\\*fontification" (buffer-name))))
  5777. (cond
  5778. ((and (eq last-command this-command)
  5779. (eq org-cycle-global-status 'overview))
  5780. ;; We just created the overview - now do table of contents
  5781. ;; This can be slow in very large buffers, so indicate action
  5782. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5783. (unless ga (message "CONTENTS..."))
  5784. (org-content)
  5785. (unless ga (message "CONTENTS...done"))
  5786. (setq org-cycle-global-status 'contents)
  5787. (run-hook-with-args 'org-cycle-hook 'contents))
  5788. ((and (eq last-command this-command)
  5789. (eq org-cycle-global-status 'contents))
  5790. ;; We just showed the table of contents - now show everything
  5791. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5792. (show-all)
  5793. (unless ga (message "SHOW ALL"))
  5794. (setq org-cycle-global-status 'all)
  5795. (run-hook-with-args 'org-cycle-hook 'all))
  5796. (t
  5797. ;; Default action: go to overview
  5798. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5799. (org-overview)
  5800. (unless ga (message "OVERVIEW"))
  5801. (setq org-cycle-global-status 'overview)
  5802. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5803. (defun org-cycle-internal-local ()
  5804. "Do the local cycling action."
  5805. (let (message-log-max ; Don't populate the *Messages* buffer
  5806. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5807. ;; First, determine end of headline (EOH), end of subtree or item
  5808. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5809. (save-excursion
  5810. (if (org-at-item-p)
  5811. (progn
  5812. (beginning-of-line)
  5813. (setq struct (org-list-struct))
  5814. (setq eoh (point-at-eol))
  5815. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5816. (setq has-children (org-list-has-child-p (point) struct)))
  5817. (org-back-to-heading)
  5818. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5819. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5820. (setq has-children
  5821. (or (save-excursion
  5822. (let ((level (funcall outline-level)))
  5823. (outline-next-heading)
  5824. (and (org-at-heading-p t)
  5825. (> (funcall outline-level) level))))
  5826. (save-excursion
  5827. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5828. ;; Determine end invisible part of buffer (EOL)
  5829. (beginning-of-line 2)
  5830. ;; XEmacs doesn't have `next-single-char-property-change'
  5831. (if (featurep 'xemacs)
  5832. (while (and (not (eobp)) ;; this is like `next-line'
  5833. (get-char-property (1- (point)) 'invisible))
  5834. (beginning-of-line 2))
  5835. (while (and (not (eobp)) ;; this is like `next-line'
  5836. (get-char-property (1- (point)) 'invisible))
  5837. (goto-char (next-single-char-property-change (point) 'invisible))
  5838. (and (eolp) (beginning-of-line 2))))
  5839. (setq eol (point)))
  5840. ;; Find out what to do next and set `this-command'
  5841. (cond
  5842. ((= eos eoh)
  5843. ;; Nothing is hidden behind this heading
  5844. (unless (org-before-first-heading-p)
  5845. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5846. (message "EMPTY ENTRY")
  5847. (setq org-cycle-subtree-status nil)
  5848. (save-excursion
  5849. (goto-char eos)
  5850. (outline-next-heading)
  5851. (if (outline-invisible-p) (org-flag-heading nil))))
  5852. ((and (or (>= eol eos)
  5853. (not (string-match "\\S-" (buffer-substring eol eos))))
  5854. (or has-children
  5855. (not (setq children-skipped
  5856. org-cycle-skip-children-state-if-no-children))))
  5857. ;; Entire subtree is hidden in one line: children view
  5858. (unless (org-before-first-heading-p)
  5859. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5860. (if (org-at-item-p)
  5861. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5862. (org-show-entry)
  5863. (org-with-limited-levels (show-children))
  5864. ;; FIXME: This slows down the func way too much.
  5865. ;; How keep drawers hidden in subtree anyway?
  5866. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5867. ;; (org-cycle-hide-drawers 'subtree))
  5868. ;; Fold every list in subtree to top-level items.
  5869. (when (eq org-cycle-include-plain-lists 'integrate)
  5870. (save-excursion
  5871. (org-back-to-heading)
  5872. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5873. (beginning-of-line 1)
  5874. (let* ((struct (org-list-struct))
  5875. (prevs (org-list-prevs-alist struct))
  5876. (end (org-list-get-bottom-point struct)))
  5877. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5878. (org-list-get-all-items (point) struct prevs))
  5879. (goto-char end))))))
  5880. (message "CHILDREN")
  5881. (save-excursion
  5882. (goto-char eos)
  5883. (outline-next-heading)
  5884. (if (outline-invisible-p) (org-flag-heading nil)))
  5885. (setq org-cycle-subtree-status 'children)
  5886. (unless (org-before-first-heading-p)
  5887. (run-hook-with-args 'org-cycle-hook 'children)))
  5888. ((or children-skipped
  5889. (and (eq last-command this-command)
  5890. (eq org-cycle-subtree-status 'children)))
  5891. ;; We just showed the children, or no children are there,
  5892. ;; now show everything.
  5893. (unless (org-before-first-heading-p)
  5894. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5895. (outline-flag-region eoh eos nil)
  5896. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5897. (setq org-cycle-subtree-status 'subtree)
  5898. (unless (org-before-first-heading-p)
  5899. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5900. (t
  5901. ;; Default action: hide the subtree.
  5902. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5903. (outline-flag-region eoh eos t)
  5904. (message "FOLDED")
  5905. (setq org-cycle-subtree-status 'folded)
  5906. (unless (org-before-first-heading-p)
  5907. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5908. ;;;###autoload
  5909. (defun org-global-cycle (&optional arg)
  5910. "Cycle the global visibility. For details see `org-cycle'.
  5911. With \\[universal-argument] prefix arg, switch to startup visibility.
  5912. With a numeric prefix, show all headlines up to that level."
  5913. (interactive "P")
  5914. (let ((org-cycle-include-plain-lists
  5915. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5916. (cond
  5917. ((integerp arg)
  5918. (show-all)
  5919. (hide-sublevels arg)
  5920. (setq org-cycle-global-status 'contents))
  5921. ((equal arg '(4))
  5922. (org-set-startup-visibility)
  5923. (message "Startup visibility, plus VISIBILITY properties."))
  5924. (t
  5925. (org-cycle '(4))))))
  5926. (defun org-set-startup-visibility ()
  5927. "Set the visibility required by startup options and properties."
  5928. (cond
  5929. ((eq org-startup-folded t)
  5930. (org-cycle '(4)))
  5931. ((eq org-startup-folded 'content)
  5932. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5933. (org-cycle '(4)) (org-cycle '(4)))))
  5934. (unless (eq org-startup-folded 'showeverything)
  5935. (if org-hide-block-startup (org-hide-block-all))
  5936. (org-set-visibility-according-to-property 'no-cleanup)
  5937. (org-cycle-hide-archived-subtrees 'all)
  5938. (org-cycle-hide-drawers 'all)
  5939. (org-cycle-show-empty-lines t)))
  5940. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5941. "Switch subtree visibilities according to :VISIBILITY: property."
  5942. (interactive)
  5943. (let (org-show-entry-below state)
  5944. (save-excursion
  5945. (goto-char (point-min))
  5946. (while (re-search-forward
  5947. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5948. nil t)
  5949. (setq state (match-string 1))
  5950. (save-excursion
  5951. (org-back-to-heading t)
  5952. (hide-subtree)
  5953. (org-reveal)
  5954. (cond
  5955. ((equal state '("fold" "folded"))
  5956. (hide-subtree))
  5957. ((equal state "children")
  5958. (org-show-hidden-entry)
  5959. (show-children))
  5960. ((equal state "content")
  5961. (save-excursion
  5962. (save-restriction
  5963. (org-narrow-to-subtree)
  5964. (org-content))))
  5965. ((member state '("all" "showall"))
  5966. (show-subtree)))))
  5967. (unless no-cleanup
  5968. (org-cycle-hide-archived-subtrees 'all)
  5969. (org-cycle-hide-drawers 'all)
  5970. (org-cycle-show-empty-lines 'all)))))
  5971. ;; This function uses outline-regexp instead of the more fundamental
  5972. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5973. ;; buffers, where outline-regexp is needed.
  5974. (defun org-overview ()
  5975. "Switch to overview mode, showing only top-level headlines.
  5976. Really, this shows all headlines with level equal or greater than the level
  5977. of the first headline in the buffer. This is important, because if the
  5978. first headline is not level one, then (hide-sublevels 1) gives confusing
  5979. results."
  5980. (interactive)
  5981. (let ((level (save-excursion
  5982. (goto-char (point-min))
  5983. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5984. (progn
  5985. (goto-char (match-beginning 0))
  5986. (funcall outline-level))))))
  5987. (and level (hide-sublevels level))))
  5988. (defun org-content (&optional arg)
  5989. "Show all headlines in the buffer, like a table of contents.
  5990. With numerical argument N, show content up to level N."
  5991. (interactive "P")
  5992. (save-excursion
  5993. ;; Visit all headings and show their offspring
  5994. (and (integerp arg) (org-overview))
  5995. (goto-char (point-max))
  5996. (catch 'exit
  5997. (while (and (progn (condition-case nil
  5998. (outline-previous-visible-heading 1)
  5999. (error (goto-char (point-min))))
  6000. t)
  6001. (looking-at org-outline-regexp))
  6002. (if (integerp arg)
  6003. (show-children (1- arg))
  6004. (show-branches))
  6005. (if (bobp) (throw 'exit nil))))))
  6006. (defun org-optimize-window-after-visibility-change (state)
  6007. "Adjust the window after a change in outline visibility.
  6008. This function is the default value of the hook `org-cycle-hook'."
  6009. (when (get-buffer-window (current-buffer))
  6010. (cond
  6011. ((eq state 'content) nil)
  6012. ((eq state 'all) nil)
  6013. ((eq state 'folded) nil)
  6014. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6015. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6016. (defun org-remove-empty-overlays-at (pos)
  6017. "Remove outline overlays that do not contain non-white stuff."
  6018. (mapc
  6019. (lambda (o)
  6020. (and (eq 'outline (overlay-get o 'invisible))
  6021. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6022. (overlay-end o))))
  6023. (delete-overlay o)))
  6024. (overlays-at pos)))
  6025. (defun org-clean-visibility-after-subtree-move ()
  6026. "Fix visibility issues after moving a subtree."
  6027. ;; First, find a reasonable region to look at:
  6028. ;; Start two siblings above, end three below
  6029. (let* ((beg (save-excursion
  6030. (and (org-get-last-sibling)
  6031. (org-get-last-sibling))
  6032. (point)))
  6033. (end (save-excursion
  6034. (and (org-get-next-sibling)
  6035. (org-get-next-sibling)
  6036. (org-get-next-sibling))
  6037. (if (org-at-heading-p)
  6038. (point-at-eol)
  6039. (point))))
  6040. (level (looking-at "\\*+"))
  6041. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6042. (save-excursion
  6043. (save-restriction
  6044. (narrow-to-region beg end)
  6045. (when re
  6046. ;; Properly fold already folded siblings
  6047. (goto-char (point-min))
  6048. (while (re-search-forward re nil t)
  6049. (if (and (not (outline-invisible-p))
  6050. (save-excursion
  6051. (goto-char (point-at-eol)) (outline-invisible-p)))
  6052. (hide-entry))))
  6053. (org-cycle-show-empty-lines 'overview)
  6054. (org-cycle-hide-drawers 'overview)))))
  6055. (defun org-cycle-show-empty-lines (state)
  6056. "Show empty lines above all visible headlines.
  6057. The region to be covered depends on STATE when called through
  6058. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6059. entire buffer covered. Note that an empty line is only shown if there
  6060. are at least `org-cycle-separator-lines' empty lines before the headline."
  6061. (when (not (= org-cycle-separator-lines 0))
  6062. (save-excursion
  6063. (let* ((n (abs org-cycle-separator-lines))
  6064. (re (cond
  6065. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6066. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6067. (t (let ((ns (number-to-string (- n 2))))
  6068. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6069. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6070. beg end b e)
  6071. (cond
  6072. ((memq state '(overview contents t))
  6073. (setq beg (point-min) end (point-max)))
  6074. ((memq state '(children folded))
  6075. (setq beg (point) end (progn (org-end-of-subtree t t)
  6076. (beginning-of-line 2)
  6077. (point)))))
  6078. (when beg
  6079. (goto-char beg)
  6080. (while (re-search-forward re end t)
  6081. (unless (get-char-property (match-end 1) 'invisible)
  6082. (setq e (match-end 1))
  6083. (if (< org-cycle-separator-lines 0)
  6084. (setq b (save-excursion
  6085. (goto-char (match-beginning 0))
  6086. (org-back-over-empty-lines)
  6087. (if (save-excursion
  6088. (goto-char (max (point-min) (1- (point))))
  6089. (org-at-heading-p))
  6090. (1- (point))
  6091. (point))))
  6092. (setq b (match-beginning 1)))
  6093. (outline-flag-region b e nil)))))))
  6094. ;; Never hide empty lines at the end of the file.
  6095. (save-excursion
  6096. (goto-char (point-max))
  6097. (outline-previous-heading)
  6098. (outline-end-of-heading)
  6099. (if (and (looking-at "[ \t\n]+")
  6100. (= (match-end 0) (point-max)))
  6101. (outline-flag-region (point) (match-end 0) nil))))
  6102. (defun org-show-empty-lines-in-parent ()
  6103. "Move to the parent and re-show empty lines before visible headlines."
  6104. (save-excursion
  6105. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6106. (org-cycle-show-empty-lines context))))
  6107. (defun org-files-list ()
  6108. "Return `org-agenda-files' list, plus all open org-mode files.
  6109. This is useful for operations that need to scan all of a user's
  6110. open and agenda-wise Org files."
  6111. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6112. (dolist (buf (buffer-list))
  6113. (with-current-buffer buf
  6114. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6115. (let ((file (expand-file-name (buffer-file-name))))
  6116. (unless (member file files)
  6117. (push file files))))))
  6118. files))
  6119. (defsubst org-entry-beginning-position ()
  6120. "Return the beginning position of the current entry."
  6121. (save-excursion (outline-back-to-heading t) (point)))
  6122. (defsubst org-entry-end-position ()
  6123. "Return the end position of the current entry."
  6124. (save-excursion (outline-next-heading) (point)))
  6125. (defun org-cycle-hide-drawers (state)
  6126. "Re-hide all drawers after a visibility state change."
  6127. (when (and (derived-mode-p 'org-mode)
  6128. (not (memq state '(overview folded contents))))
  6129. (save-excursion
  6130. (let* ((globalp (memq state '(contents all)))
  6131. (beg (if globalp (point-min) (point)))
  6132. (end (if globalp (point-max)
  6133. (if (eq state 'children)
  6134. (save-excursion (outline-next-heading) (point))
  6135. (org-end-of-subtree t)))))
  6136. (goto-char beg)
  6137. (while (re-search-forward org-drawer-regexp end t)
  6138. (org-flag-drawer t))))))
  6139. (defun org-cycle-hide-inline-tasks (state)
  6140. "Re-hide inline task when switching to 'contents visibility state."
  6141. (when (and (eq state 'contents)
  6142. (boundp 'org-inlinetask-min-level)
  6143. org-inlinetask-min-level)
  6144. (hide-sublevels (1- org-inlinetask-min-level))))
  6145. (defun org-flag-drawer (flag)
  6146. "When FLAG is non-nil, hide the drawer we are within.
  6147. Otherwise make it visible."
  6148. (save-excursion
  6149. (beginning-of-line 1)
  6150. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6151. (let ((b (match-end 0)))
  6152. (if (re-search-forward
  6153. "^[ \t]*:END:"
  6154. (save-excursion (outline-next-heading) (point)) t)
  6155. (outline-flag-region b (point-at-eol) flag)
  6156. (error ":END: line missing at position %s" b))))))
  6157. (defun org-subtree-end-visible-p ()
  6158. "Is the end of the current subtree visible?"
  6159. (pos-visible-in-window-p
  6160. (save-excursion (org-end-of-subtree t) (point))))
  6161. (defun org-first-headline-recenter (&optional N)
  6162. "Move cursor to the first headline and recenter the headline.
  6163. Optional argument N means put the headline into the Nth line of the window."
  6164. (goto-char (point-min))
  6165. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6166. (beginning-of-line)
  6167. (recenter (prefix-numeric-value N))))
  6168. ;;; Saving and restoring visibility
  6169. (defun org-outline-overlay-data (&optional use-markers)
  6170. "Return a list of the locations of all outline overlays.
  6171. These are overlays with the `invisible' property value `outline'.
  6172. The return value is a list of cons cells, with start and stop
  6173. positions for each overlay.
  6174. If USE-MARKERS is set, return the positions as markers."
  6175. (let (beg end)
  6176. (save-excursion
  6177. (save-restriction
  6178. (widen)
  6179. (delq nil
  6180. (mapcar (lambda (o)
  6181. (when (eq (overlay-get o 'invisible) 'outline)
  6182. (setq beg (overlay-start o)
  6183. end (overlay-end o))
  6184. (and beg end (> end beg)
  6185. (if use-markers
  6186. (cons (move-marker (make-marker) beg)
  6187. (move-marker (make-marker) end))
  6188. (cons beg end)))))
  6189. (overlays-in (point-min) (point-max))))))))
  6190. (defun org-set-outline-overlay-data (data)
  6191. "Create visibility overlays for all positions in DATA.
  6192. DATA should have been made by `org-outline-overlay-data'."
  6193. (let (o)
  6194. (save-excursion
  6195. (save-restriction
  6196. (widen)
  6197. (show-all)
  6198. (mapc (lambda (c)
  6199. (outline-flag-region (car c) (cdr c) t))
  6200. data)))))
  6201. ;;; Folding of blocks
  6202. (defvar org-hide-block-overlays nil
  6203. "Overlays hiding blocks.")
  6204. (make-variable-buffer-local 'org-hide-block-overlays)
  6205. (defun org-block-map (function &optional start end)
  6206. "Call FUNCTION at the head of all source blocks in the current buffer.
  6207. Optional arguments START and END can be used to limit the range."
  6208. (let ((start (or start (point-min)))
  6209. (end (or end (point-max))))
  6210. (save-excursion
  6211. (goto-char start)
  6212. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6213. (save-excursion
  6214. (save-match-data
  6215. (goto-char (match-beginning 0))
  6216. (funcall function)))))))
  6217. (defun org-hide-block-toggle-all ()
  6218. "Toggle the visibility of all blocks in the current buffer."
  6219. (org-block-map #'org-hide-block-toggle))
  6220. (defun org-hide-block-all ()
  6221. "Fold all blocks in the current buffer."
  6222. (interactive)
  6223. (org-show-block-all)
  6224. (org-block-map #'org-hide-block-toggle-maybe))
  6225. (defun org-show-block-all ()
  6226. "Unfold all blocks in the current buffer."
  6227. (interactive)
  6228. (mapc 'delete-overlay org-hide-block-overlays)
  6229. (setq org-hide-block-overlays nil))
  6230. (defun org-hide-block-toggle-maybe ()
  6231. "Toggle visibility of block at point."
  6232. (interactive)
  6233. (let ((case-fold-search t))
  6234. (if (save-excursion
  6235. (beginning-of-line 1)
  6236. (looking-at org-block-regexp))
  6237. (progn (org-hide-block-toggle)
  6238. t) ;; to signal that we took action
  6239. nil))) ;; to signal that we did not
  6240. (defun org-hide-block-toggle (&optional force)
  6241. "Toggle the visibility of the current block."
  6242. (interactive)
  6243. (save-excursion
  6244. (beginning-of-line)
  6245. (if (re-search-forward org-block-regexp nil t)
  6246. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6247. (end (match-end 0)) ;; end of entire body
  6248. ov)
  6249. (if (memq t (mapcar (lambda (overlay)
  6250. (eq (overlay-get overlay 'invisible)
  6251. 'org-hide-block))
  6252. (overlays-at start)))
  6253. (if (or (not force) (eq force 'off))
  6254. (mapc (lambda (ov)
  6255. (when (member ov org-hide-block-overlays)
  6256. (setq org-hide-block-overlays
  6257. (delq ov org-hide-block-overlays)))
  6258. (when (eq (overlay-get ov 'invisible)
  6259. 'org-hide-block)
  6260. (delete-overlay ov)))
  6261. (overlays-at start)))
  6262. (setq ov (make-overlay start end))
  6263. (overlay-put ov 'invisible 'org-hide-block)
  6264. ;; make the block accessible to isearch
  6265. (overlay-put
  6266. ov 'isearch-open-invisible
  6267. (lambda (ov)
  6268. (when (member ov org-hide-block-overlays)
  6269. (setq org-hide-block-overlays
  6270. (delq ov org-hide-block-overlays)))
  6271. (when (eq (overlay-get ov 'invisible)
  6272. 'org-hide-block)
  6273. (delete-overlay ov))))
  6274. (push ov org-hide-block-overlays)))
  6275. (error "Not looking at a source block"))))
  6276. ;; org-tab-after-check-for-cycling-hook
  6277. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6278. ;; Remove overlays when changing major mode
  6279. (add-hook 'org-mode-hook
  6280. (lambda () (org-add-hook 'change-major-mode-hook
  6281. 'org-show-block-all 'append 'local)))
  6282. ;;; Org-goto
  6283. (defvar org-goto-window-configuration nil)
  6284. (defvar org-goto-marker nil)
  6285. (defvar org-goto-map)
  6286. (defun org-goto-map ()
  6287. "Set the keymap `org-goto'."
  6288. (setq org-goto-map
  6289. (let ((map (make-sparse-keymap)))
  6290. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6291. mouse-drag-region universal-argument org-occur))
  6292. cmd)
  6293. (while (setq cmd (pop cmds))
  6294. (substitute-key-definition cmd cmd map global-map)))
  6295. (suppress-keymap map)
  6296. (org-defkey map "\C-m" 'org-goto-ret)
  6297. (org-defkey map [(return)] 'org-goto-ret)
  6298. (org-defkey map [(left)] 'org-goto-left)
  6299. (org-defkey map [(right)] 'org-goto-right)
  6300. (org-defkey map [(control ?g)] 'org-goto-quit)
  6301. (org-defkey map "\C-i" 'org-cycle)
  6302. (org-defkey map [(tab)] 'org-cycle)
  6303. (org-defkey map [(down)] 'outline-next-visible-heading)
  6304. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6305. (if org-goto-auto-isearch
  6306. (if (fboundp 'define-key-after)
  6307. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6308. nil)
  6309. (org-defkey map "q" 'org-goto-quit)
  6310. (org-defkey map "n" 'outline-next-visible-heading)
  6311. (org-defkey map "p" 'outline-previous-visible-heading)
  6312. (org-defkey map "f" 'outline-forward-same-level)
  6313. (org-defkey map "b" 'outline-backward-same-level)
  6314. (org-defkey map "u" 'outline-up-heading))
  6315. (org-defkey map "/" 'org-occur)
  6316. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6317. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6318. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6319. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6320. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6321. map)))
  6322. (defconst org-goto-help
  6323. "Browse buffer copy, to find location or copy text.%s
  6324. RET=jump to location C-g=quit and return to previous location
  6325. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6326. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6327. ;; FIXME: Docstring does not mention both interfaces
  6328. (defun org-goto (&optional alternative-interface)
  6329. "Look up a different location in the current file, keeping current visibility.
  6330. When you want look-up or go to a different location in a
  6331. document, the fastest way is often to fold the entire buffer and
  6332. then dive into the tree. This method has the disadvantage, that
  6333. the previous location will be folded, which may not be what you
  6334. want.
  6335. This command works around this by showing a copy of the current
  6336. buffer in an indirect buffer, in overview mode. You can dive
  6337. into the tree in that copy, use org-occur and incremental search
  6338. to find a location. When pressing RET or `Q', the command
  6339. returns to the original buffer in which the visibility is still
  6340. unchanged. After RET it will also jump to the location selected
  6341. in the indirect buffer and expose the headline hierarchy above.
  6342. With a prefix argument, use the alternative interface: e.g. if
  6343. `org-goto-interface' is 'outline use 'outline-path-completion."
  6344. (interactive "P")
  6345. (org-goto-map)
  6346. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6347. (org-refile-use-outline-path t)
  6348. (org-refile-target-verify-function nil)
  6349. (interface
  6350. (if (not alternative-interface)
  6351. org-goto-interface
  6352. (if (eq org-goto-interface 'outline)
  6353. 'outline-path-completion
  6354. 'outline)))
  6355. (org-goto-start-pos (point))
  6356. (selected-point
  6357. (if (eq interface 'outline)
  6358. (car (org-get-location (current-buffer) org-goto-help))
  6359. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6360. (org-refile-check-position pa)
  6361. (nth 3 pa)))))
  6362. (if selected-point
  6363. (progn
  6364. (org-mark-ring-push org-goto-start-pos)
  6365. (goto-char selected-point)
  6366. (if (or (outline-invisible-p) (org-invisible-p2))
  6367. (org-show-context 'org-goto)))
  6368. (message "Quit"))))
  6369. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6370. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6371. (defvar org-goto-local-auto-isearch-map) ; defined below
  6372. (defun org-get-location (buf help)
  6373. "Let the user select a location in the Org-mode buffer BUF.
  6374. This function uses a recursive edit. It returns the selected position
  6375. or nil."
  6376. (org-no-popups
  6377. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6378. (isearch-hide-immediately nil)
  6379. (isearch-search-fun-function
  6380. (lambda () 'org-goto-local-search-headings))
  6381. (org-goto-selected-point org-goto-exit-command))
  6382. (save-excursion
  6383. (save-window-excursion
  6384. (delete-other-windows)
  6385. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6386. (org-pop-to-buffer-same-window
  6387. (condition-case nil
  6388. (make-indirect-buffer (current-buffer) "*org-goto*")
  6389. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6390. (with-output-to-temp-buffer "*Org Help*"
  6391. (princ (format help (if org-goto-auto-isearch
  6392. " Just type for auto-isearch."
  6393. " n/p/f/b/u to navigate, q to quit."))))
  6394. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6395. (setq buffer-read-only nil)
  6396. (let ((org-startup-truncated t)
  6397. (org-startup-folded nil)
  6398. (org-startup-align-all-tables nil))
  6399. (org-mode)
  6400. (org-overview))
  6401. (setq buffer-read-only t)
  6402. (if (and (boundp 'org-goto-start-pos)
  6403. (integer-or-marker-p org-goto-start-pos))
  6404. (let ((org-show-hierarchy-above t)
  6405. (org-show-siblings t)
  6406. (org-show-following-heading t))
  6407. (goto-char org-goto-start-pos)
  6408. (and (outline-invisible-p) (org-show-context)))
  6409. (goto-char (point-min)))
  6410. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6411. (message "Select location and press RET")
  6412. (use-local-map org-goto-map)
  6413. (recursive-edit)))
  6414. (kill-buffer "*org-goto*")
  6415. (cons org-goto-selected-point org-goto-exit-command))))
  6416. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6417. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6418. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6419. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6420. (defun org-goto-local-search-headings (string bound noerror)
  6421. "Search and make sure that any matches are in headlines."
  6422. (catch 'return
  6423. (while (if isearch-forward
  6424. (search-forward string bound noerror)
  6425. (search-backward string bound noerror))
  6426. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6427. (and (member :headline context)
  6428. (not (member :tags context))))
  6429. (throw 'return (point))))))
  6430. (defun org-goto-local-auto-isearch ()
  6431. "Start isearch."
  6432. (interactive)
  6433. (goto-char (point-min))
  6434. (let ((keys (this-command-keys)))
  6435. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6436. (isearch-mode t)
  6437. (isearch-process-search-char (string-to-char keys)))))
  6438. (defun org-goto-ret (&optional arg)
  6439. "Finish `org-goto' by going to the new location."
  6440. (interactive "P")
  6441. (setq org-goto-selected-point (point)
  6442. org-goto-exit-command 'return)
  6443. (throw 'exit nil))
  6444. (defun org-goto-left ()
  6445. "Finish `org-goto' by going to the new location."
  6446. (interactive)
  6447. (if (org-at-heading-p)
  6448. (progn
  6449. (beginning-of-line 1)
  6450. (setq org-goto-selected-point (point)
  6451. org-goto-exit-command 'left)
  6452. (throw 'exit nil))
  6453. (error "Not on a heading")))
  6454. (defun org-goto-right ()
  6455. "Finish `org-goto' by going to the new location."
  6456. (interactive)
  6457. (if (org-at-heading-p)
  6458. (progn
  6459. (setq org-goto-selected-point (point)
  6460. org-goto-exit-command 'right)
  6461. (throw 'exit nil))
  6462. (error "Not on a heading")))
  6463. (defun org-goto-quit ()
  6464. "Finish `org-goto' without cursor motion."
  6465. (interactive)
  6466. (setq org-goto-selected-point nil)
  6467. (setq org-goto-exit-command 'quit)
  6468. (throw 'exit nil))
  6469. ;;; Indirect buffer display of subtrees
  6470. (defvar org-indirect-dedicated-frame nil
  6471. "This is the frame being used for indirect tree display.")
  6472. (defvar org-last-indirect-buffer nil)
  6473. (defun org-tree-to-indirect-buffer (&optional arg)
  6474. "Create indirect buffer and narrow it to current subtree.
  6475. With a numerical prefix ARG, go up to this level and then take that tree.
  6476. If ARG is negative, go up that many levels.
  6477. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6478. indirect buffer previously made with this command, to avoid proliferation of
  6479. indirect buffers. However, when you call the command with a \
  6480. \\[universal-argument] prefix, or
  6481. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6482. is kept so that you can work with several indirect buffers at the same time.
  6483. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6484. \\[universal-argument] prefix also
  6485. requests that a new frame be made for the new buffer, so that the dedicated
  6486. frame is not changed."
  6487. (interactive "P")
  6488. (let ((cbuf (current-buffer))
  6489. (cwin (selected-window))
  6490. (pos (point))
  6491. beg end level heading ibuf)
  6492. (save-excursion
  6493. (org-back-to-heading t)
  6494. (when (numberp arg)
  6495. (setq level (org-outline-level))
  6496. (if (< arg 0) (setq arg (+ level arg)))
  6497. (while (> (setq level (org-outline-level)) arg)
  6498. (org-up-heading-safe)))
  6499. (setq beg (point)
  6500. heading (org-get-heading))
  6501. (org-end-of-subtree t t)
  6502. (if (org-at-heading-p) (backward-char 1))
  6503. (setq end (point)))
  6504. (if (and (buffer-live-p org-last-indirect-buffer)
  6505. (not (eq org-indirect-buffer-display 'new-frame))
  6506. (not arg))
  6507. (kill-buffer org-last-indirect-buffer))
  6508. (setq ibuf (org-get-indirect-buffer cbuf)
  6509. org-last-indirect-buffer ibuf)
  6510. (cond
  6511. ((or (eq org-indirect-buffer-display 'new-frame)
  6512. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6513. (select-frame (make-frame))
  6514. (delete-other-windows)
  6515. (org-pop-to-buffer-same-window ibuf)
  6516. (org-set-frame-title heading))
  6517. ((eq org-indirect-buffer-display 'dedicated-frame)
  6518. (raise-frame
  6519. (select-frame (or (and org-indirect-dedicated-frame
  6520. (frame-live-p org-indirect-dedicated-frame)
  6521. org-indirect-dedicated-frame)
  6522. (setq org-indirect-dedicated-frame (make-frame)))))
  6523. (delete-other-windows)
  6524. (org-pop-to-buffer-same-window ibuf)
  6525. (org-set-frame-title (concat "Indirect: " heading)))
  6526. ((eq org-indirect-buffer-display 'current-window)
  6527. (org-pop-to-buffer-same-window ibuf))
  6528. ((eq org-indirect-buffer-display 'other-window)
  6529. (pop-to-buffer ibuf))
  6530. (t (error "Invalid value")))
  6531. (if (featurep 'xemacs)
  6532. (save-excursion (org-mode) (turn-on-font-lock)))
  6533. (narrow-to-region beg end)
  6534. (show-all)
  6535. (goto-char pos)
  6536. (run-hook-with-args 'org-cycle-hook 'all)
  6537. (and (window-live-p cwin) (select-window cwin))))
  6538. (defun org-get-indirect-buffer (&optional buffer)
  6539. (setq buffer (or buffer (current-buffer)))
  6540. (let ((n 1) (base (buffer-name buffer)) bname)
  6541. (while (buffer-live-p
  6542. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6543. (setq n (1+ n)))
  6544. (condition-case nil
  6545. (make-indirect-buffer buffer bname 'clone)
  6546. (error (make-indirect-buffer buffer bname)))))
  6547. (defun org-set-frame-title (title)
  6548. "Set the title of the current frame to the string TITLE."
  6549. ;; FIXME: how to name a single frame in XEmacs???
  6550. (unless (featurep 'xemacs)
  6551. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6552. ;;;; Structure editing
  6553. ;;; Inserting headlines
  6554. (defun org-previous-line-empty-p ()
  6555. (save-excursion
  6556. (and (not (bobp))
  6557. (or (beginning-of-line 0) t)
  6558. (save-match-data
  6559. (looking-at "[ \t]*$")))))
  6560. (defun org-insert-heading (&optional force-heading invisible-ok)
  6561. "Insert a new heading or item with same depth at point.
  6562. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6563. If point is at the beginning of a headline, insert a sibling before the
  6564. current headline. If point is not at the beginning, split the line,
  6565. create the new headline with the text in the current line after point
  6566. \(but see also the variable `org-M-RET-may-split-line').
  6567. With a double prefix arg, force the heading to be inserted at the
  6568. end of the parent subtree.
  6569. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6570. This is important for non-interactive uses of the command."
  6571. (interactive "P")
  6572. (if (or (= (buffer-size) 0)
  6573. (and (not (save-excursion
  6574. (and (ignore-errors (org-back-to-heading invisible-ok))
  6575. (org-at-heading-p))))
  6576. (or force-heading (not (org-in-item-p)))))
  6577. (progn
  6578. (insert "\n* ")
  6579. (run-hooks 'org-insert-heading-hook))
  6580. (when (or force-heading (not (org-insert-item)))
  6581. (let* ((empty-line-p nil)
  6582. (level nil)
  6583. (on-heading (org-at-heading-p))
  6584. (head (save-excursion
  6585. (condition-case nil
  6586. (progn
  6587. (org-back-to-heading invisible-ok)
  6588. (when (and (not on-heading)
  6589. (featurep 'org-inlinetask)
  6590. (integerp org-inlinetask-min-level)
  6591. (>= (length (match-string 0))
  6592. org-inlinetask-min-level))
  6593. ;; Find a heading level before the inline task
  6594. (while (and (setq level (org-up-heading-safe))
  6595. (>= level org-inlinetask-min-level)))
  6596. (if (org-at-heading-p)
  6597. (org-back-to-heading invisible-ok)
  6598. (error "This should not happen")))
  6599. (setq empty-line-p (org-previous-line-empty-p))
  6600. (match-string 0))
  6601. (error "*"))))
  6602. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6603. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6604. pos hide-previous previous-pos)
  6605. (cond
  6606. ((and (org-at-heading-p) (bolp)
  6607. (or (bobp)
  6608. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6609. ;; insert before the current line
  6610. (open-line (if blank 2 1)))
  6611. ((and (bolp)
  6612. (not org-insert-heading-respect-content)
  6613. (or (bobp)
  6614. (save-excursion
  6615. (backward-char 1) (not (outline-invisible-p)))))
  6616. ;; insert right here
  6617. nil)
  6618. (t
  6619. ;; somewhere in the line
  6620. (save-excursion
  6621. (setq previous-pos (point-at-bol))
  6622. (end-of-line)
  6623. (setq hide-previous (outline-invisible-p)))
  6624. (and org-insert-heading-respect-content (org-show-subtree))
  6625. (let ((split
  6626. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6627. (save-excursion
  6628. (let ((p (point)))
  6629. (goto-char (point-at-bol))
  6630. (and (looking-at org-complex-heading-regexp)
  6631. (match-beginning 4)
  6632. (> p (match-beginning 4)))))))
  6633. tags pos)
  6634. (cond
  6635. (org-insert-heading-respect-content
  6636. (if (not (equal force-heading '(16)))
  6637. (progn
  6638. (org-end-of-subtree nil t)
  6639. (and (looking-at "^\\*") (backward-char 1))
  6640. (while (and (not (bobp))
  6641. (member (char-before) '(?\ ?\t ?\n)))
  6642. (backward-delete-char 1)))
  6643. (org-up-heading-safe)
  6644. (org-end-of-subtree nil t))
  6645. (when (featurep 'org-inlinetask)
  6646. (while (and (not (eobp))
  6647. (looking-at "\\(\\*+\\)[ \t]+")
  6648. (>= (length (match-string 1))
  6649. org-inlinetask-min-level))
  6650. (org-end-of-subtree nil t)))
  6651. (or (bolp) (newline))
  6652. (or (org-previous-line-empty-p)
  6653. (and blank (newline)))
  6654. (open-line 1))
  6655. ((org-at-heading-p)
  6656. (when hide-previous
  6657. (show-children)
  6658. (org-show-entry))
  6659. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6660. (setq tags (and (match-end 2) (match-string 2)))
  6661. (and (match-end 1)
  6662. (delete-region (match-beginning 1) (match-end 1)))
  6663. (setq pos (point-at-bol))
  6664. (or split (end-of-line 1))
  6665. (delete-horizontal-space)
  6666. (if (string-match "\\`\\*+\\'"
  6667. (buffer-substring (point-at-bol) (point)))
  6668. (insert " "))
  6669. (newline (if blank 2 1))
  6670. (when tags
  6671. (save-excursion
  6672. (goto-char pos)
  6673. (end-of-line 1)
  6674. (insert " " tags)
  6675. (org-set-tags nil 'align))))
  6676. (t
  6677. (or split (end-of-line 1))
  6678. (newline (if blank 2 1)))))))
  6679. (insert head) (just-one-space)
  6680. (setq pos (point))
  6681. (end-of-line 1)
  6682. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6683. (when (and org-insert-heading-respect-content hide-previous)
  6684. (save-excursion
  6685. (goto-char previous-pos)
  6686. (hide-subtree)))
  6687. (run-hooks 'org-insert-heading-hook)))))
  6688. (defun org-get-heading (&optional no-tags no-todo)
  6689. "Return the heading of the current entry, without the stars.
  6690. When NO-TAGS is non-nil, don't include tags.
  6691. When NO-TODO is non-nil, don't include TODO keywords."
  6692. (save-excursion
  6693. (org-back-to-heading t)
  6694. (cond
  6695. ((and no-tags no-todo)
  6696. (looking-at org-complex-heading-regexp)
  6697. (match-string 4))
  6698. (no-tags
  6699. (looking-at (concat org-outline-regexp
  6700. "\\(.*?\\)"
  6701. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6702. (match-string 1))
  6703. (no-todo
  6704. (looking-at org-todo-line-regexp)
  6705. (match-string 3))
  6706. (t (looking-at org-heading-regexp)
  6707. (match-string 2)))))
  6708. (defvar orgstruct-mode) ; defined below
  6709. (defun org-heading-components ()
  6710. "Return the components of the current heading.
  6711. This is a list with the following elements:
  6712. - the level as an integer
  6713. - the reduced level, different if `org-odd-levels-only' is set.
  6714. - the TODO keyword, or nil
  6715. - the priority character, like ?A, or nil if no priority is given
  6716. - the headline text itself, or the tags string if no headline text
  6717. - the tags string, or nil."
  6718. (save-excursion
  6719. (org-back-to-heading t)
  6720. (if (let (case-fold-search)
  6721. (looking-at
  6722. (if orgstruct-mode
  6723. org-heading-regexp
  6724. org-complex-heading-regexp)))
  6725. (if orgstruct-mode
  6726. (list (length (match-string 1))
  6727. (org-reduced-level (length (match-string 1)))
  6728. nil
  6729. nil
  6730. (match-string 2)
  6731. nil)
  6732. (list (length (match-string 1))
  6733. (org-reduced-level (length (match-string 1)))
  6734. (org-match-string-no-properties 2)
  6735. (and (match-end 3) (aref (match-string 3) 2))
  6736. (org-match-string-no-properties 4)
  6737. (org-match-string-no-properties 5))))))
  6738. (defun org-get-entry ()
  6739. "Get the entry text, after heading, entire subtree."
  6740. (save-excursion
  6741. (org-back-to-heading t)
  6742. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6743. (defun org-insert-heading-after-current ()
  6744. "Insert a new heading with same level as current, after current subtree."
  6745. (interactive)
  6746. (org-back-to-heading)
  6747. (org-insert-heading)
  6748. (org-move-subtree-down)
  6749. (end-of-line 1))
  6750. (defun org-insert-heading-respect-content (invisible-ok)
  6751. "Insert heading with `org-insert-heading-respect-content' set to t."
  6752. (interactive "P")
  6753. (let ((org-insert-heading-respect-content t))
  6754. (org-insert-heading t invisible-ok)))
  6755. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6756. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  6757. (interactive "P")
  6758. (let ((org-insert-heading-respect-content t))
  6759. (org-insert-todo-heading force-state t)))
  6760. (defun org-insert-todo-heading (arg &optional force-heading)
  6761. "Insert a new heading with the same level and TODO state as current heading.
  6762. If the heading has no TODO state, or if the state is DONE, use the first
  6763. state (TODO by default). Also one prefix arg, force first state. With two
  6764. prefix args, force inserting at the end of the parent subtree."
  6765. (interactive "P")
  6766. (when (or force-heading (not (org-insert-item 'checkbox)))
  6767. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6768. force-heading))
  6769. (save-excursion
  6770. (org-back-to-heading)
  6771. (outline-previous-heading)
  6772. (looking-at org-todo-line-regexp))
  6773. (let*
  6774. ((new-mark-x
  6775. (if (or arg
  6776. (not (match-beginning 2))
  6777. (member (match-string 2) org-done-keywords))
  6778. (car org-todo-keywords-1)
  6779. (match-string 2)))
  6780. (new-mark
  6781. (or
  6782. (run-hook-with-args-until-success
  6783. 'org-todo-get-default-hook new-mark-x nil)
  6784. new-mark-x)))
  6785. (beginning-of-line 1)
  6786. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6787. (if org-treat-insert-todo-heading-as-state-change
  6788. (org-todo new-mark)
  6789. (insert new-mark " "))))
  6790. (when org-provide-todo-statistics
  6791. (org-update-parent-todo-statistics))))
  6792. (defun org-insert-subheading (arg)
  6793. "Insert a new subheading and demote it.
  6794. Works for outline headings and for plain lists alike."
  6795. (interactive "P")
  6796. (org-insert-heading arg)
  6797. (cond
  6798. ((org-at-heading-p) (org-do-demote))
  6799. ((org-at-item-p) (org-indent-item))))
  6800. (defun org-insert-todo-subheading (arg)
  6801. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6802. Works for outline headings and for plain lists alike."
  6803. (interactive "P")
  6804. (org-insert-todo-heading arg)
  6805. (cond
  6806. ((org-at-heading-p) (org-do-demote))
  6807. ((org-at-item-p) (org-indent-item))))
  6808. ;;; Promotion and Demotion
  6809. (defvar org-after-demote-entry-hook nil
  6810. "Hook run after an entry has been demoted.
  6811. The cursor will be at the beginning of the entry.
  6812. When a subtree is being demoted, the hook will be called for each node.")
  6813. (defvar org-after-promote-entry-hook nil
  6814. "Hook run after an entry has been promoted.
  6815. The cursor will be at the beginning of the entry.
  6816. When a subtree is being promoted, the hook will be called for each node.")
  6817. (defun org-promote-subtree ()
  6818. "Promote the entire subtree.
  6819. See also `org-promote'."
  6820. (interactive)
  6821. (save-excursion
  6822. (org-with-limited-levels (org-map-tree 'org-promote)))
  6823. (org-fix-position-after-promote))
  6824. (defun org-demote-subtree ()
  6825. "Demote the entire subtree. See `org-demote'.
  6826. See also `org-promote'."
  6827. (interactive)
  6828. (save-excursion
  6829. (org-with-limited-levels (org-map-tree 'org-demote)))
  6830. (org-fix-position-after-promote))
  6831. (defun org-do-promote ()
  6832. "Promote the current heading higher up the tree.
  6833. If the region is active in `transient-mark-mode', promote all headings
  6834. in the region."
  6835. (interactive)
  6836. (save-excursion
  6837. (if (org-region-active-p)
  6838. (org-map-region 'org-promote (region-beginning) (region-end))
  6839. (org-promote)))
  6840. (org-fix-position-after-promote))
  6841. (defun org-do-demote ()
  6842. "Demote the current heading lower down the tree.
  6843. If the region is active in `transient-mark-mode', demote all headings
  6844. in the region."
  6845. (interactive)
  6846. (save-excursion
  6847. (if (org-region-active-p)
  6848. (org-map-region 'org-demote (region-beginning) (region-end))
  6849. (org-demote)))
  6850. (org-fix-position-after-promote))
  6851. (defun org-fix-position-after-promote ()
  6852. "Make sure that after pro/demotion cursor position is right."
  6853. (let ((pos (point)))
  6854. (when (save-excursion
  6855. (beginning-of-line 1)
  6856. (looking-at org-todo-line-regexp)
  6857. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6858. (cond ((eobp) (insert " "))
  6859. ((eolp) (insert " "))
  6860. ((equal (char-after) ?\ ) (forward-char 1))))))
  6861. (defun org-current-level ()
  6862. "Return the level of the current entry, or nil if before the first headline.
  6863. The level is the number of stars at the beginning of the headline."
  6864. (save-excursion
  6865. (org-with-limited-levels
  6866. (if (ignore-errors (org-back-to-heading t))
  6867. (funcall outline-level)))))
  6868. (defun org-get-previous-line-level ()
  6869. "Return the outline depth of the last headline before the current line.
  6870. Returns 0 for the first headline in the buffer, and nil if before the
  6871. first headline."
  6872. (let ((current-level (org-current-level))
  6873. (prev-level (when (> (line-number-at-pos) 1)
  6874. (save-excursion
  6875. (beginning-of-line 0)
  6876. (org-current-level)))))
  6877. (cond ((null current-level) nil) ; Before first headline
  6878. ((null prev-level) 0) ; At first headline
  6879. (prev-level))))
  6880. (defun org-reduced-level (l)
  6881. "Compute the effective level of a heading.
  6882. This takes into account the setting of `org-odd-levels-only'."
  6883. (cond
  6884. ((zerop l) 0)
  6885. (org-odd-levels-only (1+ (floor (/ l 2))))
  6886. (t l)))
  6887. (defun org-level-increment ()
  6888. "Return the number of stars that will be added or removed at a
  6889. time to headlines when structure editing, based on the value of
  6890. `org-odd-levels-only'."
  6891. (if org-odd-levels-only 2 1))
  6892. (defun org-get-valid-level (level &optional change)
  6893. "Rectify a level change under the influence of `org-odd-levels-only'
  6894. LEVEL is a current level, CHANGE is by how much the level should be
  6895. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6896. even level numbers will become the next higher odd number."
  6897. (if org-odd-levels-only
  6898. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6899. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6900. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6901. (max 1 (+ level (or change 0)))))
  6902. (if (boundp 'define-obsolete-function-alias)
  6903. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6904. (define-obsolete-function-alias 'org-get-legal-level
  6905. 'org-get-valid-level)
  6906. (define-obsolete-function-alias 'org-get-legal-level
  6907. 'org-get-valid-level "23.1")))
  6908. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6909. ;; ̀org-with-limited-levels'
  6910. (defun org-promote ()
  6911. "Promote the current heading higher up the tree.
  6912. If the region is active in `transient-mark-mode', promote all headings
  6913. in the region."
  6914. (org-back-to-heading t)
  6915. (let* ((level (save-match-data (funcall outline-level)))
  6916. (after-change-functions (remove 'flyspell-after-change-function
  6917. after-change-functions))
  6918. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6919. (diff (abs (- level (length up-head) -1))))
  6920. (cond ((and (= level 1) org-called-with-limited-levels
  6921. org-allow-promoting-top-level-subtree)
  6922. (replace-match "# " nil t))
  6923. ((= level 1)
  6924. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6925. (t (replace-match up-head nil t)))
  6926. ;; Fixup tag positioning
  6927. (unless (= level 1)
  6928. (and org-auto-align-tags (org-set-tags nil t))
  6929. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6930. (run-hooks 'org-after-promote-entry-hook)))
  6931. (defun org-demote ()
  6932. "Demote the current heading lower down the tree.
  6933. If the region is active in `transient-mark-mode', demote all headings
  6934. in the region."
  6935. (org-back-to-heading t)
  6936. (let* ((level (save-match-data (funcall outline-level)))
  6937. (after-change-functions (remove 'flyspell-after-change-function
  6938. after-change-functions))
  6939. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6940. (diff (abs (- level (length down-head) -1))))
  6941. (replace-match down-head nil t)
  6942. ;; Fixup tag positioning
  6943. (and org-auto-align-tags (org-set-tags nil t))
  6944. (if org-adapt-indentation (org-fixup-indentation diff))
  6945. (run-hooks 'org-after-demote-entry-hook)))
  6946. (defun org-cycle-level ()
  6947. "Cycle the level of an empty headline through possible states.
  6948. This goes first to child, then to parent, level, then up the hierarchy.
  6949. After top level, it switches back to sibling level."
  6950. (interactive)
  6951. (let ((org-adapt-indentation nil))
  6952. (when (org-point-at-end-of-empty-headline)
  6953. (setq this-command 'org-cycle-level) ; Only needed for caching
  6954. (let ((cur-level (org-current-level))
  6955. (prev-level (org-get-previous-line-level)))
  6956. (cond
  6957. ;; If first headline in file, promote to top-level.
  6958. ((= prev-level 0)
  6959. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6960. do (org-do-promote)))
  6961. ;; If same level as prev, demote one.
  6962. ((= prev-level cur-level)
  6963. (org-do-demote))
  6964. ;; If parent is top-level, promote to top level if not already.
  6965. ((= prev-level 1)
  6966. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6967. do (org-do-promote)))
  6968. ;; If top-level, return to prev-level.
  6969. ((= cur-level 1)
  6970. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6971. do (org-do-demote)))
  6972. ;; If less than prev-level, promote one.
  6973. ((< cur-level prev-level)
  6974. (org-do-promote))
  6975. ;; If deeper than prev-level, promote until higher than
  6976. ;; prev-level.
  6977. ((> cur-level prev-level)
  6978. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6979. do (org-do-promote))))
  6980. t))))
  6981. (defun org-map-tree (fun)
  6982. "Call FUN for every heading underneath the current one."
  6983. (org-back-to-heading)
  6984. (let ((level (funcall outline-level)))
  6985. (save-excursion
  6986. (funcall fun)
  6987. (while (and (progn
  6988. (outline-next-heading)
  6989. (> (funcall outline-level) level))
  6990. (not (eobp)))
  6991. (funcall fun)))))
  6992. (defun org-map-region (fun beg end)
  6993. "Call FUN for every heading between BEG and END."
  6994. (let ((org-ignore-region t))
  6995. (save-excursion
  6996. (setq end (copy-marker end))
  6997. (goto-char beg)
  6998. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6999. (< (point) end))
  7000. (funcall fun))
  7001. (while (and (progn
  7002. (outline-next-heading)
  7003. (< (point) end))
  7004. (not (eobp)))
  7005. (funcall fun)))))
  7006. (defvar org-property-end-re) ; silence byte-compiler
  7007. (defun org-fixup-indentation (diff)
  7008. "Change the indentation in the current entry by DIFF.
  7009. However, if any line in the current entry has no indentation, or if it
  7010. would end up with no indentation after the change, nothing at all is done."
  7011. (save-excursion
  7012. (let ((end (save-excursion (outline-next-heading)
  7013. (point-marker)))
  7014. (prohibit (if (> diff 0)
  7015. "^\\S-"
  7016. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  7017. col)
  7018. (unless (save-excursion (end-of-line 1)
  7019. (re-search-forward prohibit end t))
  7020. (while (and (< (point) end)
  7021. (re-search-forward "^[ \t]+" end t))
  7022. (goto-char (match-end 0))
  7023. (setq col (current-column))
  7024. (if (< diff 0) (replace-match ""))
  7025. (org-indent-to-column (+ diff col))))
  7026. (move-marker end nil))))
  7027. (defun org-convert-to-odd-levels ()
  7028. "Convert an org-mode file with all levels allowed to one with odd levels.
  7029. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7030. level 5 etc."
  7031. (interactive)
  7032. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7033. (let ((outline-level 'org-outline-level)
  7034. (org-odd-levels-only nil) n)
  7035. (save-excursion
  7036. (goto-char (point-min))
  7037. (while (re-search-forward "^\\*\\*+ " nil t)
  7038. (setq n (- (length (match-string 0)) 2))
  7039. (while (>= (setq n (1- n)) 0)
  7040. (org-demote))
  7041. (end-of-line 1))))))
  7042. (defun org-convert-to-oddeven-levels ()
  7043. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7044. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7045. file contains a section with an even level, conversion would
  7046. destroy the structure of the file. An error is signaled in this
  7047. case."
  7048. (interactive)
  7049. (goto-char (point-min))
  7050. ;; First check if there are no even levels
  7051. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7052. (org-show-context t)
  7053. (error "Not all levels are odd in this file. Conversion not possible"))
  7054. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7055. (let ((outline-regexp org-outline-regexp)
  7056. (outline-level 'org-outline-level)
  7057. (org-odd-levels-only nil) n)
  7058. (save-excursion
  7059. (goto-char (point-min))
  7060. (while (re-search-forward "^\\*\\*+ " nil t)
  7061. (setq n (/ (1- (length (match-string 0))) 2))
  7062. (while (>= (setq n (1- n)) 0)
  7063. (org-promote))
  7064. (end-of-line 1))))))
  7065. (defun org-tr-level (n)
  7066. "Make N odd if required."
  7067. (if org-odd-levels-only (1+ (/ n 2)) n))
  7068. ;;; Vertical tree motion, cutting and pasting of subtrees
  7069. (defun org-move-subtree-up (&optional arg)
  7070. "Move the current subtree up past ARG headlines of the same level."
  7071. (interactive "p")
  7072. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7073. (defun org-move-subtree-down (&optional arg)
  7074. "Move the current subtree down past ARG headlines of the same level."
  7075. (interactive "p")
  7076. (setq arg (prefix-numeric-value arg))
  7077. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7078. 'org-get-last-sibling))
  7079. (ins-point (make-marker))
  7080. (cnt (abs arg))
  7081. (col (current-column))
  7082. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7083. ;; Select the tree
  7084. (org-back-to-heading)
  7085. (setq beg0 (point))
  7086. (save-excursion
  7087. (setq ne-beg (org-back-over-empty-lines))
  7088. (setq beg (point)))
  7089. (save-match-data
  7090. (save-excursion (outline-end-of-heading)
  7091. (setq folded (outline-invisible-p)))
  7092. (outline-end-of-subtree))
  7093. (outline-next-heading)
  7094. (setq ne-end (org-back-over-empty-lines))
  7095. (setq end (point))
  7096. (goto-char beg0)
  7097. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7098. ;; include less whitespace
  7099. (save-excursion
  7100. (goto-char beg)
  7101. (forward-line (- ne-beg ne-end))
  7102. (setq beg (point))))
  7103. ;; Find insertion point, with error handling
  7104. (while (> cnt 0)
  7105. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7106. (progn (goto-char beg0)
  7107. (error "Cannot move past superior level or buffer limit")))
  7108. (setq cnt (1- cnt)))
  7109. (if (> arg 0)
  7110. ;; Moving forward - still need to move over subtree
  7111. (progn (org-end-of-subtree t t)
  7112. (save-excursion
  7113. (org-back-over-empty-lines)
  7114. (or (bolp) (newline)))))
  7115. (setq ne-ins (org-back-over-empty-lines))
  7116. (move-marker ins-point (point))
  7117. (setq txt (buffer-substring beg end))
  7118. (org-save-markers-in-region beg end)
  7119. (delete-region beg end)
  7120. (org-remove-empty-overlays-at beg)
  7121. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7122. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7123. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7124. (let ((bbb (point)))
  7125. (insert-before-markers txt)
  7126. (org-reinstall-markers-in-region bbb)
  7127. (move-marker ins-point bbb))
  7128. (or (bolp) (insert "\n"))
  7129. (setq ins-end (point))
  7130. (goto-char ins-point)
  7131. (org-skip-whitespace)
  7132. (when (and (< arg 0)
  7133. (org-first-sibling-p)
  7134. (> ne-ins ne-beg))
  7135. ;; Move whitespace back to beginning
  7136. (save-excursion
  7137. (goto-char ins-end)
  7138. (let ((kill-whole-line t))
  7139. (kill-line (- ne-ins ne-beg)) (point)))
  7140. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7141. (move-marker ins-point nil)
  7142. (if folded
  7143. (hide-subtree)
  7144. (org-show-entry)
  7145. (show-children)
  7146. (org-cycle-hide-drawers 'children))
  7147. (org-clean-visibility-after-subtree-move)
  7148. ;; move back to the initial column we were at
  7149. (move-to-column col)))
  7150. (defvar org-subtree-clip ""
  7151. "Clipboard for cut and paste of subtrees.
  7152. This is actually only a copy of the kill, because we use the normal kill
  7153. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7154. (defvar org-subtree-clip-folded nil
  7155. "Was the last copied subtree folded?
  7156. This is used to fold the tree back after pasting.")
  7157. (defun org-cut-subtree (&optional n)
  7158. "Cut the current subtree into the clipboard.
  7159. With prefix arg N, cut this many sequential subtrees.
  7160. This is a short-hand for marking the subtree and then cutting it."
  7161. (interactive "p")
  7162. (org-copy-subtree n 'cut))
  7163. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7164. "Cut the current subtree into the clipboard.
  7165. With prefix arg N, cut this many sequential subtrees.
  7166. This is a short-hand for marking the subtree and then copying it.
  7167. If CUT is non-nil, actually cut the subtree.
  7168. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7169. of some markers in the region, even if CUT is non-nil. This is
  7170. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7171. (interactive "p")
  7172. (let (beg end folded (beg0 (point)))
  7173. (if (org-called-interactively-p 'any)
  7174. (org-back-to-heading nil) ; take what looks like a subtree
  7175. (org-back-to-heading t)) ; take what is really there
  7176. (setq beg (point))
  7177. (skip-chars-forward " \t\r\n")
  7178. (save-match-data
  7179. (if nosubtrees
  7180. (outline-next-heading)
  7181. (save-excursion (outline-end-of-heading)
  7182. (setq folded (outline-invisible-p)))
  7183. (condition-case nil
  7184. (org-forward-heading-same-level (1- n) t)
  7185. (error nil))
  7186. (org-end-of-subtree t t)))
  7187. (setq end (point))
  7188. (goto-char beg0)
  7189. (when (> end beg)
  7190. (setq org-subtree-clip-folded folded)
  7191. (when (or cut force-store-markers)
  7192. (org-save-markers-in-region beg end))
  7193. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7194. (setq org-subtree-clip (current-kill 0))
  7195. (message "%s: Subtree(s) with %d characters"
  7196. (if cut "Cut" "Copied")
  7197. (length org-subtree-clip)))))
  7198. (defun org-paste-subtree (&optional level tree for-yank)
  7199. "Paste the clipboard as a subtree, with modification of headline level.
  7200. The entire subtree is promoted or demoted in order to match a new headline
  7201. level.
  7202. If the cursor is at the beginning of a headline, the same level as
  7203. that headline is used to paste the tree.
  7204. If not, the new level is derived from the *visible* headings
  7205. before and after the insertion point, and taken to be the inferior headline
  7206. level of the two. So if the previous visible heading is level 3 and the
  7207. next is level 4 (or vice versa), level 4 will be used for insertion.
  7208. This makes sure that the subtree remains an independent subtree and does
  7209. not swallow low level entries.
  7210. You can also force a different level, either by using a numeric prefix
  7211. argument, or by inserting the heading marker by hand. For example, if the
  7212. cursor is after \"*****\", then the tree will be shifted to level 5.
  7213. If optional TREE is given, use this text instead of the kill ring.
  7214. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7215. move back over whitespace before inserting, and move point to the end of
  7216. the inserted text when done."
  7217. (interactive "P")
  7218. (setq tree (or tree (and kill-ring (current-kill 0))))
  7219. (unless (org-kill-is-subtree-p tree)
  7220. (error "%s"
  7221. (substitute-command-keys
  7222. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7223. (org-with-limited-levels
  7224. (let* ((visp (not (outline-invisible-p)))
  7225. (txt tree)
  7226. (^re_ "\\(\\*+\\)[ \t]*")
  7227. (old-level (if (string-match org-outline-regexp-bol txt)
  7228. (- (match-end 0) (match-beginning 0) 1)
  7229. -1))
  7230. (force-level (cond (level (prefix-numeric-value level))
  7231. ((and (looking-at "[ \t]*$")
  7232. (string-match
  7233. "^\\*+$" (buffer-substring
  7234. (point-at-bol) (point))))
  7235. (- (match-end 1) (match-beginning 1)))
  7236. ((and (bolp)
  7237. (looking-at org-outline-regexp))
  7238. (- (match-end 0) (point) 1))))
  7239. (previous-level (save-excursion
  7240. (condition-case nil
  7241. (progn
  7242. (outline-previous-visible-heading 1)
  7243. (if (looking-at ^re_)
  7244. (- (match-end 0) (match-beginning 0) 1)
  7245. 1))
  7246. (error 1))))
  7247. (next-level (save-excursion
  7248. (condition-case nil
  7249. (progn
  7250. (or (looking-at org-outline-regexp)
  7251. (outline-next-visible-heading 1))
  7252. (if (looking-at ^re_)
  7253. (- (match-end 0) (match-beginning 0) 1)
  7254. 1))
  7255. (error 1))))
  7256. (new-level (or force-level (max previous-level next-level)))
  7257. (shift (if (or (= old-level -1)
  7258. (= new-level -1)
  7259. (= old-level new-level))
  7260. 0
  7261. (- new-level old-level)))
  7262. (delta (if (> shift 0) -1 1))
  7263. (func (if (> shift 0) 'org-demote 'org-promote))
  7264. (org-odd-levels-only nil)
  7265. beg end newend)
  7266. ;; Remove the forced level indicator
  7267. (if force-level
  7268. (delete-region (point-at-bol) (point)))
  7269. ;; Paste
  7270. (beginning-of-line (if (bolp) 1 2))
  7271. (setq beg (point))
  7272. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7273. (insert-before-markers txt)
  7274. (unless (string-match "\n\\'" txt) (insert "\n"))
  7275. (setq newend (point))
  7276. (org-reinstall-markers-in-region beg)
  7277. (setq end (point))
  7278. (goto-char beg)
  7279. (skip-chars-forward " \t\n\r")
  7280. (setq beg (point))
  7281. (if (and (outline-invisible-p) visp)
  7282. (save-excursion (outline-show-heading)))
  7283. ;; Shift if necessary
  7284. (unless (= shift 0)
  7285. (save-restriction
  7286. (narrow-to-region beg end)
  7287. (while (not (= shift 0))
  7288. (org-map-region func (point-min) (point-max))
  7289. (setq shift (+ delta shift)))
  7290. (goto-char (point-min))
  7291. (setq newend (point-max))))
  7292. (when (or (org-called-interactively-p 'interactive) for-yank)
  7293. (message "Clipboard pasted as level %d subtree" new-level))
  7294. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7295. kill-ring
  7296. (eq org-subtree-clip (current-kill 0))
  7297. org-subtree-clip-folded)
  7298. ;; The tree was folded before it was killed/copied
  7299. (hide-subtree))
  7300. (and for-yank (goto-char newend)))))
  7301. (defun org-kill-is-subtree-p (&optional txt)
  7302. "Check if the current kill is an outline subtree, or a set of trees.
  7303. Returns nil if kill does not start with a headline, or if the first
  7304. headline level is not the largest headline level in the tree.
  7305. So this will actually accept several entries of equal levels as well,
  7306. which is OK for `org-paste-subtree'.
  7307. If optional TXT is given, check this string instead of the current kill."
  7308. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7309. (re (org-get-limited-outline-regexp))
  7310. (^re (concat "^" re))
  7311. (start-level (and kill
  7312. (string-match
  7313. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7314. kill)
  7315. (- (match-end 2) (match-beginning 2) 1)))
  7316. (start (1+ (or (match-beginning 2) -1))))
  7317. (if (not start-level)
  7318. (progn
  7319. nil) ;; does not even start with a heading
  7320. (catch 'exit
  7321. (while (setq start (string-match ^re kill (1+ start)))
  7322. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7323. (throw 'exit nil)))
  7324. t))))
  7325. (defvar org-markers-to-move nil
  7326. "Markers that should be moved with a cut-and-paste operation.
  7327. Those markers are stored together with their positions relative to
  7328. the start of the region.")
  7329. (defun org-save-markers-in-region (beg end)
  7330. "Check markers in region.
  7331. If these markers are between BEG and END, record their position relative
  7332. to BEG, so that after moving the block of text, we can put the markers back
  7333. into place.
  7334. This function gets called just before an entry or tree gets cut from the
  7335. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7336. called immediately, to move the markers with the entries."
  7337. (setq org-markers-to-move nil)
  7338. (when (featurep 'org-clock)
  7339. (org-clock-save-markers-for-cut-and-paste beg end))
  7340. (when (featurep 'org-agenda)
  7341. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7342. (defun org-check-and-save-marker (marker beg end)
  7343. "Check if MARKER is between BEG and END.
  7344. If yes, remember the marker and the distance to BEG."
  7345. (when (and (marker-buffer marker)
  7346. (equal (marker-buffer marker) (current-buffer)))
  7347. (if (and (>= marker beg) (< marker end))
  7348. (push (cons marker (- marker beg)) org-markers-to-move))))
  7349. (defun org-reinstall-markers-in-region (beg)
  7350. "Move all remembered markers to their position relative to BEG."
  7351. (mapc (lambda (x)
  7352. (move-marker (car x) (+ beg (cdr x))))
  7353. org-markers-to-move)
  7354. (setq org-markers-to-move nil))
  7355. (defun org-narrow-to-subtree ()
  7356. "Narrow buffer to the current subtree."
  7357. (interactive)
  7358. (save-excursion
  7359. (save-match-data
  7360. (org-with-limited-levels
  7361. (narrow-to-region
  7362. (progn (org-back-to-heading t) (point))
  7363. (progn (org-end-of-subtree t t)
  7364. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7365. (point)))))))
  7366. (defun org-narrow-to-block ()
  7367. "Narrow buffer to the current block."
  7368. (interactive)
  7369. (let* ((case-fold-search t)
  7370. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7371. "^[ \t]*#\\+end_.*")))
  7372. (if blockp
  7373. (narrow-to-region (car blockp) (cdr blockp))
  7374. (error "Not in a block"))))
  7375. (eval-when-compile
  7376. (defvar org-property-drawer-re))
  7377. (defvar org-property-start-re) ;; defined below
  7378. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7379. "Clone the task (subtree) at point N times.
  7380. The clones will be inserted as siblings.
  7381. In interactive use, the user will be prompted for the number of
  7382. clones to be produced, and for a time SHIFT, which may be a
  7383. repeater as used in time stamps, for example `+3d'.
  7384. When a valid repeater is given and the entry contains any time
  7385. stamps, the clones will become a sequence in time, with time
  7386. stamps in the subtree shifted for each clone produced. If SHIFT
  7387. is nil or the empty string, time stamps will be left alone. The
  7388. ID property of the original subtree is removed.
  7389. If the original subtree did contain time stamps with a repeater,
  7390. the following will happen:
  7391. - the repeater will be removed in each clone
  7392. - an additional clone will be produced, with the current, unshifted
  7393. date(s) in the entry.
  7394. - the original entry will be placed *after* all the clones, with
  7395. repeater intact.
  7396. - the start days in the repeater in the original entry will be shifted
  7397. to past the last clone.
  7398. In this way you can spell out a number of instances of a repeating task,
  7399. and still retain the repeater to cover future instances of the task."
  7400. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7401. (let (beg end template task idprop
  7402. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7403. (drawer-re org-drawer-regexp))
  7404. (if (not (and (integerp n) (> n 0)))
  7405. (error "Invalid number of replications %s" n))
  7406. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7407. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7408. shift)))
  7409. (error "Invalid shift specification %s" shift))
  7410. (when doshift
  7411. (setq shift-n (string-to-number (match-string 1 shift))
  7412. shift-what (cdr (assoc (match-string 2 shift)
  7413. '(("d" . day) ("w" . week)
  7414. ("m" . month) ("y" . year))))))
  7415. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7416. (setq nmin 1 nmax n)
  7417. (org-back-to-heading t)
  7418. (setq beg (point))
  7419. (setq idprop (org-entry-get nil "ID"))
  7420. (org-end-of-subtree t t)
  7421. (or (bolp) (insert "\n"))
  7422. (setq end (point))
  7423. (setq template (buffer-substring beg end))
  7424. (when (and doshift
  7425. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7426. (delete-region beg end)
  7427. (setq end beg)
  7428. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7429. (goto-char end)
  7430. (loop for n from nmin to nmax do
  7431. ;; prepare clone
  7432. (with-temp-buffer
  7433. (insert template)
  7434. (org-mode)
  7435. (goto-char (point-min))
  7436. (org-show-subtree)
  7437. (and idprop (if org-clone-delete-id
  7438. (org-entry-delete nil "ID")
  7439. (org-id-get-create t)))
  7440. (unless (= n 0)
  7441. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7442. (kill-whole-line))
  7443. (goto-char (point-min))
  7444. (while (re-search-forward drawer-re nil t)
  7445. (mapc (lambda (d)
  7446. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7447. (goto-char (point-min))
  7448. (when doshift
  7449. (while (re-search-forward org-ts-regexp-both nil t)
  7450. (org-timestamp-change (* n shift-n) shift-what))
  7451. (unless (= n n-no-remove)
  7452. (goto-char (point-min))
  7453. (while (re-search-forward org-ts-regexp nil t)
  7454. (save-excursion
  7455. (goto-char (match-beginning 0))
  7456. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7457. (delete-region (match-beginning 1) (match-end 1)))))))
  7458. (setq task (buffer-string)))
  7459. (insert task))
  7460. (goto-char beg)))
  7461. ;;; Outline Sorting
  7462. (defun org-sort (with-case)
  7463. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7464. Optional argument WITH-CASE means sort case-sensitively."
  7465. (interactive "P")
  7466. (cond
  7467. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7468. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7469. (t
  7470. (org-call-with-arg 'org-sort-entries with-case))))
  7471. (defun org-sort-remove-invisible (s)
  7472. (remove-text-properties 0 (length s) org-rm-props s)
  7473. (while (string-match org-bracket-link-regexp s)
  7474. (setq s (replace-match (if (match-end 2)
  7475. (match-string 3 s)
  7476. (match-string 1 s)) t t s)))
  7477. s)
  7478. (defvar org-priority-regexp) ; defined later in the file
  7479. (defvar org-after-sorting-entries-or-items-hook nil
  7480. "Hook that is run after a bunch of entries or items have been sorted.
  7481. When children are sorted, the cursor is in the parent line when this
  7482. hook gets called. When a region or a plain list is sorted, the cursor
  7483. will be in the first entry of the sorted region/list.")
  7484. (defun org-sort-entries
  7485. (&optional with-case sorting-type getkey-func compare-func property)
  7486. "Sort entries on a certain level of an outline tree.
  7487. If there is an active region, the entries in the region are sorted.
  7488. Else, if the cursor is before the first entry, sort the top-level items.
  7489. Else, the children of the entry at point are sorted.
  7490. Sorting can be alphabetically, numerically, by date/time as given by
  7491. a time stamp, by a property or by priority.
  7492. The command prompts for the sorting type unless it has been given to the
  7493. function through the SORTING-TYPE argument, which needs to be a character,
  7494. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7495. precise meaning of each character:
  7496. n Numerically, by converting the beginning of the entry/item to a number.
  7497. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7498. o By order of TODO keywords.
  7499. t By date/time, either the first active time stamp in the entry, or, if
  7500. none exist, by the first inactive one.
  7501. s By the scheduled date/time.
  7502. d By deadline date/time.
  7503. c By creation time, which is assumed to be the first inactive time stamp
  7504. at the beginning of a line.
  7505. p By priority according to the cookie.
  7506. r By the value of a property.
  7507. Capital letters will reverse the sort order.
  7508. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7509. called with point at the beginning of the record. It must return either
  7510. a string or a number that should serve as the sorting key for that record.
  7511. Comparing entries ignores case by default. However, with an optional argument
  7512. WITH-CASE, the sorting considers case as well."
  7513. (interactive "P")
  7514. (let ((case-func (if with-case 'identity 'downcase))
  7515. (cmstr
  7516. ;; The clock marker is lost when using `sort-subr', let's
  7517. ;; store the clocking string.
  7518. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7519. (save-excursion
  7520. (goto-char org-clock-marker)
  7521. (looking-back "^.*") (match-string-no-properties 0))))
  7522. start beg end stars re re2
  7523. txt what tmp)
  7524. ;; Find beginning and end of region to sort
  7525. (cond
  7526. ((org-region-active-p)
  7527. ;; we will sort the region
  7528. (setq end (region-end)
  7529. what "region")
  7530. (goto-char (region-beginning))
  7531. (if (not (org-at-heading-p)) (outline-next-heading))
  7532. (setq start (point)))
  7533. ((or (org-at-heading-p)
  7534. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7535. ;; we will sort the children of the current headline
  7536. (org-back-to-heading)
  7537. (setq start (point)
  7538. end (progn (org-end-of-subtree t t)
  7539. (or (bolp) (insert "\n"))
  7540. (org-back-over-empty-lines)
  7541. (point))
  7542. what "children")
  7543. (goto-char start)
  7544. (show-subtree)
  7545. (outline-next-heading))
  7546. (t
  7547. ;; we will sort the top-level entries in this file
  7548. (goto-char (point-min))
  7549. (or (org-at-heading-p) (outline-next-heading))
  7550. (setq start (point))
  7551. (goto-char (point-max))
  7552. (beginning-of-line 1)
  7553. (when (looking-at ".*?\\S-")
  7554. ;; File ends in a non-white line
  7555. (end-of-line 1)
  7556. (insert "\n"))
  7557. (setq end (point-max))
  7558. (setq what "top-level")
  7559. (goto-char start)
  7560. (show-all)))
  7561. (setq beg (point))
  7562. (if (>= beg end) (error "Nothing to sort"))
  7563. (looking-at "\\(\\*+\\)")
  7564. (setq stars (match-string 1)
  7565. re (concat "^" (regexp-quote stars) " +")
  7566. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7567. txt (buffer-substring beg end))
  7568. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7569. (if (and (not (equal stars "*")) (string-match re2 txt))
  7570. (error "Region to sort contains a level above the first entry"))
  7571. (unless sorting-type
  7572. (message
  7573. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7574. [t]ime [s]cheduled [d]eadline [c]reated
  7575. A/N/P/R/O/F/T/S/D/C means reversed:"
  7576. what)
  7577. (setq sorting-type (read-char-exclusive))
  7578. (unless getkey-func
  7579. (and (= (downcase sorting-type) ?f)
  7580. (setq getkey-func
  7581. (org-icompleting-read "Sort using function: "
  7582. obarray 'fboundp t nil nil))
  7583. (setq getkey-func (intern getkey-func))))
  7584. (and (= (downcase sorting-type) ?r)
  7585. (not property)
  7586. (setq property
  7587. (org-icompleting-read "Property: "
  7588. (mapcar 'list (org-buffer-property-keys t))
  7589. nil t))))
  7590. (message "Sorting entries...")
  7591. (save-restriction
  7592. (narrow-to-region start end)
  7593. (let ((dcst (downcase sorting-type))
  7594. (case-fold-search nil)
  7595. (now (current-time)))
  7596. (sort-subr
  7597. (/= dcst sorting-type)
  7598. ;; This function moves to the beginning character of the "record" to
  7599. ;; be sorted.
  7600. (lambda nil
  7601. (if (re-search-forward re nil t)
  7602. (goto-char (match-beginning 0))
  7603. (goto-char (point-max))))
  7604. ;; This function moves to the last character of the "record" being
  7605. ;; sorted.
  7606. (lambda nil
  7607. (save-match-data
  7608. (condition-case nil
  7609. (outline-forward-same-level 1)
  7610. (error
  7611. (goto-char (point-max))))))
  7612. ;; This function returns the value that gets sorted against.
  7613. (lambda nil
  7614. (cond
  7615. ((= dcst ?n)
  7616. (if (looking-at org-complex-heading-regexp)
  7617. (string-to-number (match-string 4))
  7618. nil))
  7619. ((= dcst ?a)
  7620. (if (looking-at org-complex-heading-regexp)
  7621. (funcall case-func (match-string 4))
  7622. nil))
  7623. ((= dcst ?t)
  7624. (let ((end (save-excursion (outline-next-heading) (point))))
  7625. (if (or (re-search-forward org-ts-regexp end t)
  7626. (re-search-forward org-ts-regexp-both end t))
  7627. (org-time-string-to-seconds (match-string 0))
  7628. (org-float-time now))))
  7629. ((= dcst ?c)
  7630. (let ((end (save-excursion (outline-next-heading) (point))))
  7631. (if (re-search-forward
  7632. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7633. end t)
  7634. (org-time-string-to-seconds (match-string 0))
  7635. (org-float-time now))))
  7636. ((= dcst ?s)
  7637. (let ((end (save-excursion (outline-next-heading) (point))))
  7638. (if (re-search-forward org-scheduled-time-regexp end t)
  7639. (org-time-string-to-seconds (match-string 1))
  7640. (org-float-time now))))
  7641. ((= dcst ?d)
  7642. (let ((end (save-excursion (outline-next-heading) (point))))
  7643. (if (re-search-forward org-deadline-time-regexp end t)
  7644. (org-time-string-to-seconds (match-string 1))
  7645. (org-float-time now))))
  7646. ((= dcst ?p)
  7647. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7648. (string-to-char (match-string 2))
  7649. org-default-priority))
  7650. ((= dcst ?r)
  7651. (or (org-entry-get nil property) ""))
  7652. ((= dcst ?o)
  7653. (if (looking-at org-complex-heading-regexp)
  7654. (- 9999 (length (member (match-string 2)
  7655. org-todo-keywords-1)))))
  7656. ((= dcst ?f)
  7657. (if getkey-func
  7658. (progn
  7659. (setq tmp (funcall getkey-func))
  7660. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7661. tmp)
  7662. (error "Invalid key function `%s'" getkey-func)))
  7663. (t (error "Invalid sorting type `%c'" sorting-type))))
  7664. nil
  7665. (cond
  7666. ((= dcst ?a) 'string<)
  7667. ((= dcst ?f) compare-func)
  7668. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7669. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7670. ;; Reset the clock marker if needed
  7671. (when cmstr
  7672. (save-excursion
  7673. (goto-char start)
  7674. (search-forward cmstr nil t)
  7675. (move-marker org-clock-marker (point))))
  7676. (message "Sorting entries...done")))
  7677. (defun org-do-sort (table what &optional with-case sorting-type)
  7678. "Sort TABLE of WHAT according to SORTING-TYPE.
  7679. The user will be prompted for the SORTING-TYPE if the call to this
  7680. function does not specify it. WHAT is only for the prompt, to indicate
  7681. what is being sorted. The sorting key will be extracted from
  7682. the car of the elements of the table.
  7683. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7684. (unless sorting-type
  7685. (message
  7686. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7687. what)
  7688. (setq sorting-type (read-char-exclusive)))
  7689. (let ((dcst (downcase sorting-type))
  7690. extractfun comparefun)
  7691. ;; Define the appropriate functions
  7692. (cond
  7693. ((= dcst ?n)
  7694. (setq extractfun 'string-to-number
  7695. comparefun (if (= dcst sorting-type) '< '>)))
  7696. ((= dcst ?a)
  7697. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7698. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7699. comparefun (if (= dcst sorting-type)
  7700. 'string<
  7701. (lambda (a b) (and (not (string< a b))
  7702. (not (string= a b)))))))
  7703. ((= dcst ?t)
  7704. (setq extractfun
  7705. (lambda (x)
  7706. (if (or (string-match org-ts-regexp x)
  7707. (string-match org-ts-regexp-both x))
  7708. (org-float-time
  7709. (org-time-string-to-time (match-string 0 x)))
  7710. 0))
  7711. comparefun (if (= dcst sorting-type) '< '>)))
  7712. (t (error "Invalid sorting type `%c'" sorting-type)))
  7713. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7714. table)
  7715. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7716. ;;; The orgstruct minor mode
  7717. ;; Define a minor mode which can be used in other modes in order to
  7718. ;; integrate the org-mode structure editing commands.
  7719. ;; This is really a hack, because the org-mode structure commands use
  7720. ;; keys which normally belong to the major mode. Here is how it
  7721. ;; works: The minor mode defines all the keys necessary to operate the
  7722. ;; structure commands, but wraps the commands into a function which
  7723. ;; tests if the cursor is currently at a headline or a plain list
  7724. ;; item. If that is the case, the structure command is used,
  7725. ;; temporarily setting many Org-mode variables like regular
  7726. ;; expressions for filling etc. However, when any of those keys is
  7727. ;; used at a different location, function uses `key-binding' to look
  7728. ;; up if the key has an associated command in another currently active
  7729. ;; keymap (minor modes, major mode, global), and executes that
  7730. ;; command. There might be problems if any of the keys is otherwise
  7731. ;; used as a prefix key.
  7732. (defcustom orgstruct-heading-prefix-regexp ""
  7733. "Regexp that matches the custom prefix of Org headlines in
  7734. orgstruct(++)-mode."
  7735. :group 'org
  7736. :type 'string)
  7737. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  7738. (defcustom orgstruct-setup-hook nil
  7739. "Hook run after orgstruct-mode-map is filled."
  7740. :group 'org
  7741. :type 'hook)
  7742. (defvar orgstruct-initialized nil)
  7743. (defvar org-local-vars nil
  7744. "List of local variables, for use by `orgstruct-mode'.")
  7745. ;;;###autoload
  7746. (define-minor-mode orgstruct-mode
  7747. "Toggle the minor mode `orgstruct-mode'.
  7748. This mode is for using Org-mode structure commands in other
  7749. modes. The following keys behave as if Org-mode were active, if
  7750. the cursor is on a headline, or on a plain list item (both as
  7751. defined by Org-mode)."
  7752. nil " OrgStruct" (make-sparse-keymap)
  7753. (when orgstruct-mode
  7754. (org-load-modules-maybe)
  7755. (unless orgstruct-initialized
  7756. (orgstruct-setup)
  7757. (setq orgstruct-initialized t))))
  7758. ;;;###autoload
  7759. (defun turn-on-orgstruct ()
  7760. "Unconditionally turn on `orgstruct-mode'."
  7761. (orgstruct-mode 1))
  7762. (defvar org-fb-vars nil)
  7763. (make-variable-buffer-local 'org-fb-vars)
  7764. (defun orgstruct++-mode (&optional arg)
  7765. "Toggle `orgstruct-mode', the enhanced version of it.
  7766. In addition to setting orgstruct-mode, this also exports all
  7767. indentation and autofilling variables from org-mode into the
  7768. buffer. It will also recognize item context in multiline items."
  7769. (interactive "P")
  7770. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7771. (if (< arg 1)
  7772. (progn (orgstruct-mode -1)
  7773. (mapc (lambda(v)
  7774. (org-set-local (car v)
  7775. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7776. org-fb-vars))
  7777. (orgstruct-mode 1)
  7778. (setq org-fb-vars nil)
  7779. (let (var val)
  7780. (mapc
  7781. (lambda (x)
  7782. (when (string-match
  7783. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7784. (symbol-name (car x)))
  7785. (setq var (car x) val (nth 1 x))
  7786. (push (list var `(quote ,(eval var))) org-fb-vars)
  7787. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7788. org-local-vars)
  7789. (org-set-local 'orgstruct-is-++ t))))
  7790. (defvar orgstruct-is-++ nil
  7791. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7792. (make-variable-buffer-local 'orgstruct-is-++)
  7793. ;;;###autoload
  7794. (defun turn-on-orgstruct++ ()
  7795. "Unconditionally turn on `orgstruct++-mode'."
  7796. (orgstruct++-mode 1))
  7797. (defun orgstruct-error ()
  7798. "Error when there is no default binding for a structure key."
  7799. (interactive)
  7800. (error "This key has no function outside structure elements"))
  7801. (defun orgstruct-setup ()
  7802. "Setup orgstruct keymap."
  7803. (dolist (f
  7804. '("org-meta"
  7805. "org-shiftmeta"
  7806. org-shifttab
  7807. org-backward-element
  7808. org-backward-heading-same-level
  7809. org-ctrl-c-ret
  7810. org-cycle
  7811. org-forward-heading-same-level
  7812. org-insert-heading
  7813. org-insert-heading-respect-content
  7814. org-kill-note-or-show-branches
  7815. org-mark-subtree
  7816. org-narrow-to-subtree
  7817. org-promote-subtree
  7818. org-reveal
  7819. org-show-subtree
  7820. org-sort
  7821. org-up-element
  7822. outline-demote
  7823. outline-next-visible-heading
  7824. outline-previous-visible-heading
  7825. outline-promote
  7826. outline-up-heading
  7827. show-children))
  7828. (dolist (f (if (stringp f)
  7829. (let ((flist))
  7830. (dolist (postfix
  7831. '("-return" "tab" "left" "right" "up" "down")
  7832. flist)
  7833. (let ((f (intern (concat f postfix))))
  7834. (when (fboundp f)
  7835. (push f flist)))))
  7836. (list f)))
  7837. (dolist (binding (nconc (where-is-internal f org-mode-map)
  7838. (where-is-internal f outline-mode-map)))
  7839. ;; TODO use local-function-key-map
  7840. (dolist (rep '(("<tab>" . "TAB")
  7841. ("<ret>" . "RET")
  7842. ("<esc>" . "ESC")
  7843. ("<del>" . "DEL")))
  7844. (setq binding (read-kbd-macro (replace-regexp-in-string
  7845. (regexp-quote (car rep))
  7846. (cdr rep)
  7847. (key-description binding)))))
  7848. (let ((key (lookup-key orgstruct-mode-map binding)))
  7849. (when (or (not key) (numberp key))
  7850. (condition-case nil
  7851. (org-defkey orgstruct-mode-map
  7852. binding
  7853. (orgstruct-make-binding f binding))
  7854. (error nil)))))))
  7855. (run-hooks 'orgstruct-setup-hook))
  7856. (defun orgstruct-make-binding (fun key)
  7857. "Create a function for binding in the structure minor mode.
  7858. FUN is the command to call inside a table. KEY is the key that
  7859. should be checked in for a command to execute outside of tables."
  7860. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  7861. (let ((nname name)
  7862. (i 0))
  7863. (while (fboundp (intern nname))
  7864. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  7865. (setq name (intern nname)))
  7866. (eval
  7867. `(defun ,name (arg)
  7868. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  7869. "Outside of structure, run the binding of `"
  7870. (key-description key) "'.")
  7871. (interactive "p")
  7872. (unless
  7873. (let* ((org-heading-regexp
  7874. (concat "^"
  7875. orgstruct-heading-prefix-regexp
  7876. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  7877. (org-outline-regexp
  7878. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  7879. (org-outline-regexp-bol
  7880. (concat "^" org-outline-regexp))
  7881. (outline-regexp org-outline-regexp)
  7882. (outline-heading-end-regexp "\n")
  7883. (outline-level 'outline-level)
  7884. (outline-heading-alist))
  7885. (when (org-context-p 'headline 'item
  7886. ,(when (memq fun '(org-insert-heading))
  7887. '(when orgstruct-is-++
  7888. 'item-body)))
  7889. (org-run-like-in-org-mode ',fun)
  7890. t))
  7891. (let* ((orgstruct-mode)
  7892. (binding (key-binding ,key)))
  7893. (if (keymapp binding)
  7894. (set-temporary-overlay-map binding)
  7895. (call-interactively
  7896. (or binding 'orgstruct-error)))))))
  7897. name))
  7898. (defun org-contextualize-keys (alist contexts)
  7899. "Return valid elements in ALIST depending on CONTEXTS.
  7900. `org-agenda-custom-commands' or `org-capture-templates' are the
  7901. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7902. or `org-capture-templates-contexts' are the associated contexts
  7903. definitions."
  7904. (let ((contexts
  7905. ;; normalize contexts
  7906. (mapcar
  7907. (lambda(c) (cond ((listp (cadr c))
  7908. (list (car c) (car c) (cadr c)))
  7909. ((string= "" (cadr c))
  7910. (list (car c) (car c) (caddr c)))
  7911. (t c))) contexts))
  7912. (a alist) c r s)
  7913. ;; loop over all commands or templates
  7914. (while (setq c (pop a))
  7915. (let (vrules repl)
  7916. (cond
  7917. ((not (assoc (car c) contexts))
  7918. (push c r))
  7919. ((and (assoc (car c) contexts)
  7920. (setq vrules (org-contextualize-validate-key
  7921. (car c) contexts)))
  7922. (mapc (lambda (vr)
  7923. (when (not (equal (car vr) (cadr vr)))
  7924. (setq repl vr))) vrules)
  7925. (if (not repl) (push c r)
  7926. (push (cadr repl) s)
  7927. (push
  7928. (cons (car c)
  7929. (cdr (or (assoc (cadr repl) alist)
  7930. (error "Undefined key `%s' as contextual replacement for `%s'"
  7931. (cadr repl) (car c)))))
  7932. r))))))
  7933. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7934. (delq
  7935. nil
  7936. (delete-dups
  7937. (mapcar (lambda (x)
  7938. (let ((tpl (car x)))
  7939. (when (not (delq
  7940. nil
  7941. (mapcar (lambda(y)
  7942. (equal y tpl)) s))) x)))
  7943. (reverse r))))))
  7944. (defun org-contextualize-validate-key (key contexts)
  7945. "Check CONTEXTS for agenda or capture KEY."
  7946. (let (r rr res)
  7947. (while (setq r (pop contexts))
  7948. (mapc
  7949. (lambda (rr)
  7950. (when
  7951. (and (equal key (car r))
  7952. (if (functionp rr) (funcall rr)
  7953. (or (and (eq (car rr) 'in-file)
  7954. (buffer-file-name)
  7955. (string-match (cdr rr) (buffer-file-name)))
  7956. (and (eq (car rr) 'in-mode)
  7957. (string-match (cdr rr) (symbol-name major-mode)))
  7958. (and (eq (car rr) 'in-buffer)
  7959. (string-match (cdr rr) (buffer-name)))
  7960. (when (and (eq (car rr) 'not-in-file)
  7961. (buffer-file-name))
  7962. (not (string-match (cdr rr) (buffer-file-name))))
  7963. (when (eq (car rr) 'not-in-mode)
  7964. (not (string-match (cdr rr) (symbol-name major-mode))))
  7965. (when (eq (car rr) 'not-in-buffer)
  7966. (not (string-match (cdr rr) (buffer-name)))))))
  7967. (push r res)))
  7968. (car (last r))))
  7969. (delete-dups (delq nil res))))
  7970. (defun org-context-p (&rest contexts)
  7971. "Check if local context is any of CONTEXTS.
  7972. Possible values in the list of contexts are `table', `headline', and `item'."
  7973. (let ((pos (point)))
  7974. (goto-char (point-at-bol))
  7975. (prog1 (or (and (memq 'table contexts)
  7976. (looking-at "[ \t]*|"))
  7977. (and (memq 'headline contexts)
  7978. (looking-at org-outline-regexp))
  7979. (and (memq 'item contexts)
  7980. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7981. (and (memq 'item-body contexts)
  7982. (org-in-item-p)))
  7983. (goto-char pos))))
  7984. (defun org-get-local-variables ()
  7985. "Return a list of all local variables in an Org mode buffer."
  7986. (let (varlist)
  7987. (with-current-buffer (get-buffer-create "*Org tmp*")
  7988. (erase-buffer)
  7989. (org-mode)
  7990. (setq varlist (buffer-local-variables)))
  7991. (kill-buffer "*Org tmp*")
  7992. (delq nil
  7993. (mapcar
  7994. (lambda (x)
  7995. (setq x
  7996. (if (symbolp x)
  7997. (list x)
  7998. (list (car x) (cdr x))))
  7999. (if (and (not (get (car x) 'org-state))
  8000. (string-match
  8001. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8002. (symbol-name (car x))))
  8003. x nil))
  8004. varlist))))
  8005. (defun org-clone-local-variables (from-buffer &optional regexp)
  8006. "Clone local variables from FROM-BUFFER.
  8007. Optional argument REGEXP selects variables to clone."
  8008. (mapc
  8009. (lambda (pair)
  8010. (and (symbolp (car pair))
  8011. (or (null regexp)
  8012. (string-match regexp (symbol-name (car pair))))
  8013. (set (make-local-variable (car pair))
  8014. (cdr pair))))
  8015. (buffer-local-variables from-buffer)))
  8016. ;;;###autoload
  8017. (defun org-run-like-in-org-mode (cmd)
  8018. "Run a command, pretending that the current buffer is in Org-mode.
  8019. This will temporarily bind local variables that are typically bound in
  8020. Org-mode to the values they have in Org-mode, and then interactively
  8021. call CMD."
  8022. (org-load-modules-maybe)
  8023. (unless org-local-vars
  8024. (setq org-local-vars (org-get-local-variables)))
  8025. (let (binds)
  8026. (dolist (var org-local-vars)
  8027. (when (or (not (boundp (car var)))
  8028. (eq (symbol-value (car var))
  8029. (default-value (car var))))
  8030. (push (list (car var) `(quote ,(cadr var))) binds)))
  8031. (eval `(let ,binds
  8032. (call-interactively (quote ,cmd))))))
  8033. ;;;; Archiving
  8034. (defun org-get-category (&optional pos force-refresh)
  8035. "Get the category applying to position POS."
  8036. (save-match-data
  8037. (if force-refresh (org-refresh-category-properties))
  8038. (let ((pos (or pos (point))))
  8039. (or (get-text-property pos 'org-category)
  8040. (progn (org-refresh-category-properties)
  8041. (get-text-property pos 'org-category))))))
  8042. (defun org-refresh-category-properties ()
  8043. "Refresh category text properties in the buffer."
  8044. (let ((case-fold-search t)
  8045. (inhibit-read-only t)
  8046. (def-cat (cond
  8047. ((null org-category)
  8048. (if buffer-file-name
  8049. (file-name-sans-extension
  8050. (file-name-nondirectory buffer-file-name))
  8051. "???"))
  8052. ((symbolp org-category) (symbol-name org-category))
  8053. (t org-category)))
  8054. beg end cat pos optionp)
  8055. (org-unmodified
  8056. (save-excursion
  8057. (save-restriction
  8058. (widen)
  8059. (goto-char (point-min))
  8060. (put-text-property (point) (point-max) 'org-category def-cat)
  8061. (while (re-search-forward
  8062. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  8063. (setq pos (match-end 0)
  8064. optionp (equal (char-after (match-beginning 0)) ?#)
  8065. cat (org-trim (match-string 2)))
  8066. (if optionp
  8067. (setq beg (point-at-bol) end (point-max))
  8068. (org-back-to-heading t)
  8069. (setq beg (point) end (org-end-of-subtree t t)))
  8070. (put-text-property beg end 'org-category cat)
  8071. (put-text-property beg end 'org-category-position beg)
  8072. (goto-char pos)))))))
  8073. (defun org-refresh-properties (dprop tprop)
  8074. "Refresh buffer text properties.
  8075. DPROP is the drawer property and TPROP is the corresponding text
  8076. property to set."
  8077. (let ((case-fold-search t)
  8078. (inhibit-read-only t) p)
  8079. (org-unmodified
  8080. (save-excursion
  8081. (save-restriction
  8082. (widen)
  8083. (goto-char (point-min))
  8084. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8085. (setq p (org-match-string-no-properties 1))
  8086. (save-excursion
  8087. (org-back-to-heading t)
  8088. (put-text-property
  8089. (point-at-bol) (point-at-eol) tprop p))))))))
  8090. ;;;; Link Stuff
  8091. ;;; Link abbreviations
  8092. (defun org-link-expand-abbrev (link)
  8093. "Apply replacements as defined in `org-link-abbrev-alist'."
  8094. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8095. (let* ((key (match-string 1 link))
  8096. (as (or (assoc key org-link-abbrev-alist-local)
  8097. (assoc key org-link-abbrev-alist)))
  8098. (tag (and (match-end 2) (match-string 3 link)))
  8099. rpl)
  8100. (if (not as)
  8101. link
  8102. (setq rpl (cdr as))
  8103. (cond
  8104. ((symbolp rpl) (funcall rpl tag))
  8105. ((string-match "%(\\([^)]+\\))" rpl)
  8106. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8107. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8108. ((string-match "%h" rpl)
  8109. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8110. (t (concat rpl tag)))))
  8111. link))
  8112. ;;; Storing and inserting links
  8113. (defvar org-insert-link-history nil
  8114. "Minibuffer history for links inserted with `org-insert-link'.")
  8115. (defvar org-stored-links nil
  8116. "Contains the links stored with `org-store-link'.")
  8117. (defvar org-store-link-plist nil
  8118. "Plist with info about the most recently link created with `org-store-link'.")
  8119. (defvar org-link-protocols nil
  8120. "Link protocols added to Org-mode using `org-add-link-type'.")
  8121. (defvar org-store-link-functions nil
  8122. "List of functions that are called to create and store a link.
  8123. Each function will be called in turn until one returns a non-nil
  8124. value. Each function should check if it is responsible for creating
  8125. this link (for example by looking at the major mode).
  8126. If not, it must exit and return nil.
  8127. If yes, it should return a non-nil value after a calling
  8128. `org-store-link-props' with a list of properties and values.
  8129. Special properties are:
  8130. :type The link prefix, like \"http\". This must be given.
  8131. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8132. This is obligatory as well.
  8133. :description Optional default description for the second pair
  8134. of brackets in an Org-mode link. The user can still change
  8135. this when inserting this link into an Org-mode buffer.
  8136. In addition to these, any additional properties can be specified
  8137. and then used in capture templates.")
  8138. (defun org-add-link-type (type &optional follow export)
  8139. "Add TYPE to the list of `org-link-types'.
  8140. Re-compute all regular expressions depending on `org-link-types'
  8141. FOLLOW and EXPORT are two functions.
  8142. FOLLOW should take the link path as the single argument and do whatever
  8143. is necessary to follow the link, for example find a file or display
  8144. a mail message.
  8145. EXPORT should format the link path for export to one of the export formats.
  8146. It should be a function accepting three arguments:
  8147. path the path of the link, the text after the prefix (like \"http:\")
  8148. desc the description of the link, if any, or a description added by
  8149. org-export-normalize-links if there is none
  8150. format the export format, a symbol like `html' or `latex' or `ascii'..
  8151. The function may use the FORMAT information to return different values
  8152. depending on the format. The return value will be put literally into
  8153. the exported file. If the return value is nil, this means Org should
  8154. do what it normally does with links which do not have EXPORT defined.
  8155. Org-mode has a built-in default for exporting links. If you are happy with
  8156. this default, there is no need to define an export function for the link
  8157. type. For a simple example of an export function, see `org-bbdb.el'."
  8158. (add-to-list 'org-link-types type t)
  8159. (org-make-link-regexps)
  8160. (if (assoc type org-link-protocols)
  8161. (setcdr (assoc type org-link-protocols) (list follow export))
  8162. (push (list type follow export) org-link-protocols)))
  8163. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8164. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8165. ;;;###autoload
  8166. (defun org-store-link (arg)
  8167. "\\<org-mode-map>Store an org-link to the current location.
  8168. This link is added to `org-stored-links' and can later be inserted
  8169. into an org-buffer with \\[org-insert-link].
  8170. For some link types, a prefix arg is interpreted.
  8171. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8172. For file links, arg negates `org-context-in-file-links'.
  8173. A double prefix arg force skipping storing functions that are not
  8174. part of Org's core."
  8175. (interactive "P")
  8176. (org-load-modules-maybe)
  8177. (setq org-store-link-plist nil) ; reset
  8178. (org-with-limited-levels
  8179. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8180. (cond
  8181. ((and (not (equal arg '(16)))
  8182. (setq sfuns
  8183. (delq
  8184. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8185. org-store-link-functions))
  8186. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8187. (or (and (cdr sfuns)
  8188. (funcall (intern
  8189. (completing-read "Which function for creating the link? "
  8190. sfunsn t (car sfunsn)))))
  8191. (funcall (caar sfuns)))
  8192. (setq link (plist-get org-store-link-plist :link)
  8193. desc (or (plist-get org-store-link-plist :description) link))))
  8194. ((org-src-edit-buffer-p)
  8195. (let (label gc)
  8196. (while (or (not label)
  8197. (save-excursion
  8198. (save-restriction
  8199. (widen)
  8200. (goto-char (point-min))
  8201. (re-search-forward
  8202. (regexp-quote (format org-coderef-label-format label))
  8203. nil t))))
  8204. (when label (message "Label exists already") (sit-for 2))
  8205. (setq label (read-string "Code line label: " label)))
  8206. (end-of-line 1)
  8207. (setq link (format org-coderef-label-format label))
  8208. (setq gc (- 79 (length link)))
  8209. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8210. (insert link)
  8211. (setq link (concat "(" label ")") desc nil)))
  8212. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8213. ;; We are in the agenda, link to referenced location
  8214. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8215. (get-text-property (point) 'org-marker))))
  8216. (when m
  8217. (org-with-point-at m
  8218. (setq agenda-link
  8219. (if (org-called-interactively-p 'any)
  8220. (call-interactively 'org-store-link)
  8221. (org-store-link nil)))))))
  8222. ((eq major-mode 'calendar-mode)
  8223. (let ((cd (calendar-cursor-to-date)))
  8224. (setq link
  8225. (format-time-string
  8226. (car org-time-stamp-formats)
  8227. (apply 'encode-time
  8228. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8229. nil nil nil))))
  8230. (org-store-link-props :type "calendar" :date cd)))
  8231. ((eq major-mode 'help-mode)
  8232. (setq link (concat "help:" (save-excursion
  8233. (goto-char (point-min))
  8234. (looking-at "^[^ ]+")
  8235. (match-string 0))))
  8236. (org-store-link-props :type "help"))
  8237. ((eq major-mode 'w3-mode)
  8238. (setq cpltxt (if (and (buffer-name)
  8239. (not (string-match "Untitled" (buffer-name))))
  8240. (buffer-name)
  8241. (url-view-url t))
  8242. link (url-view-url t))
  8243. (org-store-link-props :type "w3" :url (url-view-url t)))
  8244. ((setq search (run-hook-with-args-until-success
  8245. 'org-create-file-search-functions))
  8246. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8247. "::" search))
  8248. (setq cpltxt (or description link)))
  8249. ((eq major-mode 'image-mode)
  8250. (setq cpltxt (concat "file:"
  8251. (abbreviate-file-name buffer-file-name))
  8252. link cpltxt)
  8253. (org-store-link-props :type "image" :file buffer-file-name))
  8254. ((eq major-mode 'dired-mode)
  8255. ;; link to the file in the current line
  8256. (let ((file (dired-get-filename nil t)))
  8257. (setq file (if file
  8258. (abbreviate-file-name
  8259. (expand-file-name (dired-get-filename nil t)))
  8260. ;; otherwise, no file so use current directory.
  8261. default-directory))
  8262. (setq cpltxt (concat "file:" file)
  8263. link cpltxt)))
  8264. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8265. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8266. (cond
  8267. ((org-in-regexp "<<\\(.*?\\)>>")
  8268. (setq cpltxt
  8269. (concat "file:"
  8270. (abbreviate-file-name
  8271. (buffer-file-name (buffer-base-buffer)))
  8272. "::" (match-string 1))
  8273. link cpltxt))
  8274. ((and (featurep 'org-id)
  8275. (or (eq org-id-link-to-org-use-id t)
  8276. (and (org-called-interactively-p 'any)
  8277. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8278. (and (eq org-id-link-to-org-use-id
  8279. 'create-if-interactive-and-no-custom-id)
  8280. (not custom-id))))
  8281. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8282. ;; We can make a link using the ID.
  8283. (setq link (condition-case nil
  8284. (prog1 (org-id-store-link)
  8285. (setq desc (plist-get org-store-link-plist :description)))
  8286. (error
  8287. ;; probably before first headline, link to file only
  8288. (concat "file:"
  8289. (abbreviate-file-name
  8290. (buffer-file-name (buffer-base-buffer))))))))
  8291. (t
  8292. ;; Just link to current headline
  8293. (setq cpltxt (concat "file:"
  8294. (abbreviate-file-name
  8295. (buffer-file-name (buffer-base-buffer)))))
  8296. ;; Add a context search string
  8297. (when (org-xor org-context-in-file-links arg)
  8298. (let* ((ee (org-element-at-point))
  8299. (et (org-element-type ee))
  8300. (ev (plist-get (cadr ee) :value)))
  8301. (setq txt (cond
  8302. ((org-at-heading-p) nil)
  8303. ((eq et 'keyword) ev)
  8304. ((org-region-active-p)
  8305. (buffer-substring (region-beginning) (region-end)))))
  8306. (when (or (null txt) (string-match "\\S-" txt))
  8307. (setq cpltxt
  8308. (concat cpltxt "::"
  8309. (condition-case nil
  8310. (org-make-org-heading-search-string txt)
  8311. (error "")))
  8312. desc (or (and (eq et 'keyword) ev)
  8313. (nth 4 (ignore-errors (org-heading-components)))
  8314. "NONE")))))
  8315. (if (string-match "::\\'" cpltxt)
  8316. (setq cpltxt (substring cpltxt 0 -2)))
  8317. (setq link cpltxt))))
  8318. ((buffer-file-name (buffer-base-buffer))
  8319. ;; Just link to this file here.
  8320. (setq cpltxt (concat "file:"
  8321. (abbreviate-file-name
  8322. (buffer-file-name (buffer-base-buffer)))))
  8323. ;; Add a context string
  8324. (when (org-xor org-context-in-file-links arg)
  8325. (setq txt (if (org-region-active-p)
  8326. (buffer-substring (region-beginning) (region-end))
  8327. (buffer-substring (point-at-bol) (point-at-eol))))
  8328. ;; Only use search option if there is some text.
  8329. (when (string-match "\\S-" txt)
  8330. (setq cpltxt
  8331. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8332. desc "NONE")))
  8333. (setq link cpltxt))
  8334. ((org-called-interactively-p 'interactive)
  8335. (user-error "No method for storing a link from this buffer"))
  8336. (t (setq link nil)))
  8337. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8338. (setq link (or link cpltxt)
  8339. desc (or desc cpltxt))
  8340. (cond ((equal desc "NONE") (setq desc nil))
  8341. ((string-match org-bracket-link-regexp desc)
  8342. (setq desc (replace-regexp-in-string
  8343. org-bracket-link-regexp
  8344. (concat "\\3" (if (equal (length (match-string 0 desc))
  8345. (length desc)) "*" "")) desc))))
  8346. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8347. (progn
  8348. (setq org-stored-links
  8349. (cons (list link desc) org-stored-links))
  8350. (message "Stored: %s" (or desc link))
  8351. (when custom-id
  8352. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8353. "::#" custom-id))
  8354. (setq org-stored-links
  8355. (cons (list link desc) org-stored-links))))
  8356. (or agenda-link (and link (org-make-link-string link desc)))))))
  8357. (defun org-store-link-props (&rest plist)
  8358. "Store link properties, extract names and addresses."
  8359. (let (x adr)
  8360. (when (setq x (plist-get plist :from))
  8361. (setq adr (mail-extract-address-components x))
  8362. (setq plist (plist-put plist :fromname (car adr)))
  8363. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8364. (when (setq x (plist-get plist :to))
  8365. (setq adr (mail-extract-address-components x))
  8366. (setq plist (plist-put plist :toname (car adr)))
  8367. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8368. (let ((from (plist-get plist :from))
  8369. (to (plist-get plist :to)))
  8370. (when (and from to org-from-is-user-regexp)
  8371. (setq plist
  8372. (plist-put plist :fromto
  8373. (if (string-match org-from-is-user-regexp from)
  8374. (concat "to %t")
  8375. (concat "from %f"))))))
  8376. (setq org-store-link-plist plist))
  8377. (defun org-add-link-props (&rest plist)
  8378. "Add these properties to the link property list."
  8379. (let (key value)
  8380. (while plist
  8381. (setq key (pop plist) value (pop plist))
  8382. (setq org-store-link-plist
  8383. (plist-put org-store-link-plist key value)))))
  8384. (defun org-email-link-description (&optional fmt)
  8385. "Return the description part of an email link.
  8386. This takes information from `org-store-link-plist' and formats it
  8387. according to FMT (default from `org-email-link-description-format')."
  8388. (setq fmt (or fmt org-email-link-description-format))
  8389. (let* ((p org-store-link-plist)
  8390. (to (plist-get p :toaddress))
  8391. (from (plist-get p :fromaddress))
  8392. (table
  8393. (list
  8394. (cons "%c" (plist-get p :fromto))
  8395. (cons "%F" (plist-get p :from))
  8396. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8397. (cons "%T" (plist-get p :to))
  8398. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8399. (cons "%s" (plist-get p :subject))
  8400. (cons "%d" (plist-get p :date))
  8401. (cons "%m" (plist-get p :message-id)))))
  8402. (when (string-match "%c" fmt)
  8403. ;; Check if the user wrote this message
  8404. (if (and org-from-is-user-regexp from to
  8405. (save-match-data (string-match org-from-is-user-regexp from)))
  8406. (setq fmt (replace-match "to %t" t t fmt))
  8407. (setq fmt (replace-match "from %f" t t fmt))))
  8408. (org-replace-escapes fmt table)))
  8409. (defun org-make-org-heading-search-string (&optional string)
  8410. "Make search string for the current headline or STRING."
  8411. (let ((s (or string
  8412. (and (derived-mode-p 'org-mode)
  8413. (save-excursion
  8414. (org-back-to-heading t)
  8415. (org-element-property :raw-value (org-element-at-point))))))
  8416. (lines org-context-in-file-links))
  8417. (or string (setq s (concat "*" s))) ; Add * for headlines
  8418. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8419. (when (and string (integerp lines) (> lines 0))
  8420. (let ((slines (org-split-string s "\n")))
  8421. (when (< lines (length slines))
  8422. (setq s (mapconcat
  8423. 'identity
  8424. (reverse (nthcdr (- (length slines) lines)
  8425. (reverse slines))) "\n")))))
  8426. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8427. (defun org-make-link-string (link &optional description)
  8428. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8429. (unless (string-match "\\S-" link)
  8430. (error "Empty link"))
  8431. (when (and description
  8432. (stringp description)
  8433. (not (string-match "\\S-" description)))
  8434. (setq description nil))
  8435. (when (stringp description)
  8436. ;; Remove brackets from the description, they are fatal.
  8437. (while (string-match "\\[" description)
  8438. (setq description (replace-match "{" t t description)))
  8439. (while (string-match "\\]" description)
  8440. (setq description (replace-match "}" t t description))))
  8441. (when (equal link description)
  8442. ;; No description needed, it is identical
  8443. (setq description nil))
  8444. (when (and (not description)
  8445. (not (string-match (org-image-file-name-regexp) link))
  8446. (not (equal link (org-link-escape link))))
  8447. (setq description (org-extract-attributes link)))
  8448. (setq link
  8449. (cond ((string-match (org-image-file-name-regexp) link) link)
  8450. ((string-match org-link-types-re link)
  8451. (concat (match-string 1 link)
  8452. (org-link-escape (substring link (match-end 1)))))
  8453. (t (org-link-escape link))))
  8454. (concat "[[" link "]"
  8455. (if description (concat "[" description "]") "")
  8456. "]"))
  8457. (defconst org-link-escape-chars
  8458. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8459. "List of characters that should be escaped in link.
  8460. This is the list that is used for internal purposes.")
  8461. (defconst org-link-escape-chars-browser
  8462. '(?\ )
  8463. "List of escapes for characters that are problematic in links.
  8464. This is the list that is used before handing over to the browser.")
  8465. (defun org-link-escape (text &optional table merge)
  8466. "Return percent escaped representation of TEXT.
  8467. TEXT is a string with the text to escape.
  8468. Optional argument TABLE is a list with characters that should be
  8469. escaped. When nil, `org-link-escape-chars' is used.
  8470. If optional argument MERGE is set, merge TABLE into
  8471. `org-link-escape-chars'."
  8472. (cond
  8473. ((and table merge)
  8474. (mapc (lambda (defchr)
  8475. (unless (member defchr table)
  8476. (setq table (cons defchr table)))) org-link-escape-chars))
  8477. ((null table)
  8478. (setq table org-link-escape-chars)))
  8479. (mapconcat
  8480. (lambda (char)
  8481. (if (or (member char table)
  8482. (and (or (< char 32) (= char 37) (> char 126))
  8483. org-url-hexify-p))
  8484. (mapconcat (lambda (sequence-element)
  8485. (format "%%%.2X" sequence-element))
  8486. (or (encode-coding-char char 'utf-8)
  8487. (error "Unable to percent escape character: %s"
  8488. (char-to-string char))) "")
  8489. (char-to-string char))) text ""))
  8490. (defun org-link-unescape (str)
  8491. "Unhex hexified Unicode strings as returned from the JavaScript function
  8492. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8493. (unless (and (null str) (string= "" str))
  8494. (let ((pos 0) (case-fold-search t) unhexed)
  8495. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8496. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8497. (setq str (replace-match unhexed t t str))
  8498. (setq pos (+ pos (length unhexed))))))
  8499. str)
  8500. (defun org-link-unescape-compound (hex)
  8501. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8502. Note: this function also decodes single byte encodings like
  8503. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8504. (save-match-data
  8505. (let* ((bytes (cdr (split-string hex "%")))
  8506. (ret "")
  8507. (eat 0)
  8508. (sum 0))
  8509. (while bytes
  8510. (let* ((val (string-to-number (pop bytes) 16))
  8511. (shift-xor
  8512. (if (= 0 eat)
  8513. (cond
  8514. ((>= val 252) (cons 6 252))
  8515. ((>= val 248) (cons 5 248))
  8516. ((>= val 240) (cons 4 240))
  8517. ((>= val 224) (cons 3 224))
  8518. ((>= val 192) (cons 2 192))
  8519. (t (cons 0 0)))
  8520. (cons 6 128))))
  8521. (if (>= val 192) (setq eat (car shift-xor)))
  8522. (setq val (logxor val (cdr shift-xor)))
  8523. (setq sum (+ (lsh sum (car shift-xor)) val))
  8524. (if (> eat 0) (setq eat (- eat 1)))
  8525. (cond
  8526. ((= 0 eat) ;multi byte
  8527. (setq ret (concat ret (org-char-to-string sum)))
  8528. (setq sum 0))
  8529. ((not bytes) ; single byte(s)
  8530. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8531. )) ;; end (while bytes
  8532. ret )))
  8533. (defun org-link-unescape-single-byte-sequence (hex)
  8534. "Unhexify hex-encoded single byte character sequences."
  8535. (mapconcat (lambda (byte)
  8536. (char-to-string (string-to-number byte 16)))
  8537. (cdr (split-string hex "%")) ""))
  8538. (defun org-xor (a b)
  8539. "Exclusive or."
  8540. (if a (not b) b))
  8541. (defun org-fixup-message-id-for-http (s)
  8542. "Replace special characters in a message id, so it can be used in an http query."
  8543. (when (string-match "%" s)
  8544. (setq s (mapconcat (lambda (c)
  8545. (if (eq c ?%)
  8546. "%25"
  8547. (char-to-string c)))
  8548. s "")))
  8549. (while (string-match "<" s)
  8550. (setq s (replace-match "%3C" t t s)))
  8551. (while (string-match ">" s)
  8552. (setq s (replace-match "%3E" t t s)))
  8553. (while (string-match "@" s)
  8554. (setq s (replace-match "%40" t t s)))
  8555. s)
  8556. (defun org-link-prettify (link)
  8557. "Return a human-readable representation of LINK.
  8558. The car of LINK must be a raw link the cdr of LINK must be either
  8559. a link description or nil."
  8560. (let ((desc (or (cadr link) "<no description>")))
  8561. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8562. "<" (car link) ">")))
  8563. ;;;###autoload
  8564. (defun org-insert-link-global ()
  8565. "Insert a link like Org-mode does.
  8566. This command can be called in any mode to insert a link in Org-mode syntax."
  8567. (interactive)
  8568. (org-load-modules-maybe)
  8569. (org-run-like-in-org-mode 'org-insert-link))
  8570. (defun org-insert-all-links (&optional keep)
  8571. "Insert all links in `org-stored-links'."
  8572. (interactive "P")
  8573. (let ((links (copy-sequence org-stored-links)) l)
  8574. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8575. (insert "- ")
  8576. (org-insert-link nil (car l) (cadr l))
  8577. (insert "\n"))))
  8578. (defun org-link-fontify-links-to-this-file ()
  8579. "Fontify links to the current file in `org-stored-links'."
  8580. (let ((f (buffer-file-name)) a b)
  8581. (setq a (mapcar (lambda(l)
  8582. (let ((ll (car l)))
  8583. (when (and (string-match "^file:\\(.+\\)::" ll)
  8584. (equal f (expand-file-name (match-string 1 ll))))
  8585. ll)))
  8586. org-stored-links))
  8587. (when (featurep 'org-id)
  8588. (setq b (mapcar (lambda(l)
  8589. (let ((ll (car l)))
  8590. (when (and (string-match "^id:\\(.+\\)$" ll)
  8591. (equal f (expand-file-name
  8592. (or (org-id-find-id-file
  8593. (match-string 1 ll)) ""))))
  8594. ll)))
  8595. org-stored-links)))
  8596. (mapcar (lambda(l)
  8597. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8598. (delq nil (append a b)))))
  8599. (defvar org-link-links-in-this-file nil)
  8600. (defun org-insert-link (&optional complete-file link-location default-description)
  8601. "Insert a link. At the prompt, enter the link.
  8602. Completion can be used to insert any of the link protocol prefixes like
  8603. http or ftp in use.
  8604. The history can be used to select a link previously stored with
  8605. `org-store-link'. When the empty string is entered (i.e. if you just
  8606. press RET at the prompt), the link defaults to the most recently
  8607. stored link. As SPC triggers completion in the minibuffer, you need to
  8608. use M-SPC or C-q SPC to force the insertion of a space character.
  8609. You will also be prompted for a description, and if one is given, it will
  8610. be displayed in the buffer instead of the link.
  8611. If there is already a link at point, this command will allow you to edit link
  8612. and description parts.
  8613. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8614. be selected using completion. The path to the file will be relative to the
  8615. current directory if the file is in the current directory or a subdirectory.
  8616. Otherwise, the link will be the absolute path as completed in the minibuffer
  8617. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8618. option `org-link-file-path-type'.
  8619. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8620. the current directory or below.
  8621. With three \\[universal-argument] prefixes, negate the meaning of
  8622. `org-keep-stored-link-after-insertion'.
  8623. If `org-make-link-description-function' is non-nil, this function will be
  8624. called with the link target, and the result will be the default
  8625. link description.
  8626. If the LINK-LOCATION parameter is non-nil, this value will be
  8627. used as the link location instead of reading one interactively.
  8628. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8629. be used as the default description."
  8630. (interactive "P")
  8631. (let* ((wcf (current-window-configuration))
  8632. (origbuf (current-buffer))
  8633. (region (if (org-region-active-p)
  8634. (buffer-substring (region-beginning) (region-end))))
  8635. (remove (and region (list (region-beginning) (region-end))))
  8636. (desc region)
  8637. tmphist ; byte-compile incorrectly complains about this
  8638. (link link-location)
  8639. (abbrevs org-link-abbrev-alist-local)
  8640. entry file all-prefixes auto-desc)
  8641. (cond
  8642. (link-location) ; specified by arg, just use it.
  8643. ((org-in-regexp org-bracket-link-regexp 1)
  8644. ;; We do have a link at point, and we are going to edit it.
  8645. (setq remove (list (match-beginning 0) (match-end 0)))
  8646. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8647. (setq link (read-string "Link: "
  8648. (org-link-unescape
  8649. (org-match-string-no-properties 1)))))
  8650. ((or (org-in-regexp org-angle-link-re)
  8651. (org-in-regexp org-plain-link-re))
  8652. ;; Convert to bracket link
  8653. (setq remove (list (match-beginning 0) (match-end 0))
  8654. link (read-string "Link: "
  8655. (org-remove-angle-brackets (match-string 0)))))
  8656. ((member complete-file '((4) (16)))
  8657. ;; Completing read for file names.
  8658. (setq link (org-file-complete-link complete-file)))
  8659. (t
  8660. ;; Read link, with completion for stored links.
  8661. (org-link-fontify-links-to-this-file)
  8662. (org-switch-to-buffer-other-window "*Org Links*")
  8663. (with-current-buffer "*Org Links*"
  8664. (erase-buffer)
  8665. (insert "Insert a link.
  8666. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8667. (when org-stored-links
  8668. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8669. (insert (mapconcat 'org-link-prettify
  8670. (reverse org-stored-links) "\n")))
  8671. (goto-char (point-min)))
  8672. (let ((cw (selected-window)))
  8673. (select-window (get-buffer-window "*Org Links*" 'visible))
  8674. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8675. (unless (pos-visible-in-window-p (point-max))
  8676. (org-fit-window-to-buffer))
  8677. (and (window-live-p cw) (select-window cw)))
  8678. ;; Fake a link history, containing the stored links.
  8679. (setq tmphist (append (mapcar 'car org-stored-links)
  8680. org-insert-link-history))
  8681. (setq all-prefixes (append (mapcar 'car abbrevs)
  8682. (mapcar 'car org-link-abbrev-alist)
  8683. org-link-types))
  8684. (unwind-protect
  8685. (progn
  8686. (setq link
  8687. (org-completing-read
  8688. "Link: "
  8689. (append
  8690. (mapcar (lambda (x) (concat x ":"))
  8691. all-prefixes)
  8692. (mapcar 'car org-stored-links))
  8693. nil nil nil
  8694. 'tmphist
  8695. (caar org-stored-links)))
  8696. (if (not (string-match "\\S-" link))
  8697. (error "No link selected"))
  8698. (mapc (lambda(l)
  8699. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8700. org-stored-links)
  8701. (if (or (member link all-prefixes)
  8702. (and (equal ":" (substring link -1))
  8703. (member (substring link 0 -1) all-prefixes)
  8704. (setq link (substring link 0 -1))))
  8705. (setq link (with-current-buffer origbuf
  8706. (org-link-try-special-completion link)))))
  8707. (set-window-configuration wcf)
  8708. (kill-buffer "*Org Links*"))
  8709. (setq entry (assoc link org-stored-links))
  8710. (or entry (push link org-insert-link-history))
  8711. (setq desc (or desc (nth 1 entry)))))
  8712. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8713. (not org-keep-stored-link-after-insertion))
  8714. (setq org-stored-links (delq (assoc link org-stored-links)
  8715. org-stored-links)))
  8716. (if (string-match org-plain-link-re link)
  8717. ;; URL-like link, normalize the use of angular brackets.
  8718. (setq link (org-remove-angle-brackets link)))
  8719. ;; Check if we are linking to the current file with a search
  8720. ;; option If yes, simplify the link by using only the search
  8721. ;; option.
  8722. (when (and buffer-file-name
  8723. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8724. (let* ((path (match-string 1 link))
  8725. (case-fold-search nil)
  8726. (search (match-string 2 link)))
  8727. (save-match-data
  8728. (if (equal (file-truename buffer-file-name) (file-truename path))
  8729. ;; We are linking to this same file, with a search option
  8730. (setq link search)))))
  8731. ;; Check if we can/should use a relative path. If yes, simplify the link
  8732. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8733. (let* ((type (match-string 1 link))
  8734. (path (match-string 2 link))
  8735. (origpath path)
  8736. (case-fold-search nil))
  8737. (cond
  8738. ((or (eq org-link-file-path-type 'absolute)
  8739. (equal complete-file '(16)))
  8740. (setq path (abbreviate-file-name (expand-file-name path))))
  8741. ((eq org-link-file-path-type 'noabbrev)
  8742. (setq path (expand-file-name path)))
  8743. ((eq org-link-file-path-type 'relative)
  8744. (setq path (file-relative-name path)))
  8745. (t
  8746. (save-match-data
  8747. (if (string-match (concat "^" (regexp-quote
  8748. (expand-file-name
  8749. (file-name-as-directory
  8750. default-directory))))
  8751. (expand-file-name path))
  8752. ;; We are linking a file with relative path name.
  8753. (setq path (substring (expand-file-name path)
  8754. (match-end 0)))
  8755. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8756. (setq link (concat type path))
  8757. (if (equal desc origpath)
  8758. (setq desc path))))
  8759. (if org-make-link-description-function
  8760. (setq desc
  8761. (or (condition-case nil
  8762. (funcall org-make-link-description-function link desc)
  8763. (error (progn (message "Can't get link description from `%s'"
  8764. (symbol-name org-make-link-description-function))
  8765. (sit-for 2) nil)))
  8766. (read-string "Description: " default-description)))
  8767. (if default-description (setq desc default-description)
  8768. (setq desc (or (and auto-desc desc)
  8769. (read-string "Description: " desc)))))
  8770. (unless (string-match "\\S-" desc) (setq desc nil))
  8771. (if remove (apply 'delete-region remove))
  8772. (insert (org-make-link-string link desc))))
  8773. (defun org-link-try-special-completion (type)
  8774. "If there is completion support for link type TYPE, offer it."
  8775. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8776. (if (functionp fun)
  8777. (funcall fun)
  8778. (read-string "Link (no completion support): " (concat type ":")))))
  8779. (defun org-file-complete-link (&optional arg)
  8780. "Create a file link using completion."
  8781. (let (file link)
  8782. (setq file (org-iread-file-name "File: "))
  8783. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8784. (pwd1 (file-name-as-directory (abbreviate-file-name
  8785. (expand-file-name ".")))))
  8786. (cond
  8787. ((equal arg '(16))
  8788. (setq link (concat
  8789. "file:"
  8790. (abbreviate-file-name (expand-file-name file)))))
  8791. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8792. (setq link (concat "file:" (match-string 1 file))))
  8793. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8794. (expand-file-name file))
  8795. (setq link (concat
  8796. "file:" (match-string 1 (expand-file-name file)))))
  8797. (t (setq link (concat "file:" file)))))
  8798. link))
  8799. (defun org-iread-file-name (&rest args)
  8800. "Read-file-name using `ido-mode' speedup if available.
  8801. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8802. See `read-file-name' for a description of parameters."
  8803. (org-without-partial-completion
  8804. (if (and org-completion-use-ido
  8805. (fboundp 'ido-read-file-name)
  8806. (boundp 'ido-mode) ido-mode
  8807. (listp (second args)))
  8808. (let ((ido-enter-matching-directory nil))
  8809. (apply 'ido-read-file-name args))
  8810. (apply 'read-file-name args))))
  8811. (defun org-completing-read (&rest args)
  8812. "Completing-read with SPACE being a normal character."
  8813. (let ((enable-recursive-minibuffers t)
  8814. (minibuffer-local-completion-map
  8815. (copy-keymap minibuffer-local-completion-map)))
  8816. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8817. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8818. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8819. (apply 'org-icompleting-read args)))
  8820. (defun org-completing-read-no-i (&rest args)
  8821. (let (org-completion-use-ido org-completion-use-iswitchb)
  8822. (apply 'org-completing-read args)))
  8823. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8824. "Use iswitch as a completing-read replacement to choose from choices.
  8825. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8826. from."
  8827. (let* ((iswitchb-use-virtual-buffers nil)
  8828. (iswitchb-make-buflist-hook
  8829. (lambda ()
  8830. (setq iswitchb-temp-buflist choices))))
  8831. (iswitchb-read-buffer prompt)))
  8832. (defun org-icompleting-read (&rest args)
  8833. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8834. (org-without-partial-completion
  8835. (if (and org-completion-use-ido
  8836. (fboundp 'ido-completing-read)
  8837. (boundp 'ido-mode) ido-mode
  8838. (listp (second args)))
  8839. (let ((ido-enter-matching-directory nil))
  8840. (apply 'ido-completing-read (concat (car args))
  8841. (if (consp (car (nth 1 args)))
  8842. (mapcar 'car (nth 1 args))
  8843. (nth 1 args))
  8844. (cddr args)))
  8845. (if (and org-completion-use-iswitchb
  8846. (boundp 'iswitchb-mode) iswitchb-mode
  8847. (listp (second args)))
  8848. (apply 'org-iswitchb-completing-read (concat (car args))
  8849. (if (consp (car (nth 1 args)))
  8850. (mapcar 'car (nth 1 args))
  8851. (nth 1 args))
  8852. (cddr args))
  8853. (apply 'completing-read args)))))
  8854. (defun org-extract-attributes (s)
  8855. "Extract the attributes cookie from a string and set as text property."
  8856. (let (a attr (start 0) key value)
  8857. (save-match-data
  8858. (when (string-match "{{\\([^}]+\\)}}$" s)
  8859. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8860. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8861. (setq key (match-string 1 a) value (match-string 2 a)
  8862. start (match-end 0)
  8863. attr (plist-put attr (intern key) value))))
  8864. (org-add-props s nil 'org-attr attr))
  8865. s))
  8866. (defun org-extract-attributes-from-string (tag)
  8867. (let (key value attr)
  8868. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8869. (setq key (match-string 1 tag) value (match-string 2 tag)
  8870. tag (replace-match "" t t tag)
  8871. attr (plist-put attr (intern key) value)))
  8872. (cons tag attr)))
  8873. (defun org-attributes-to-string (plist)
  8874. "Format a property list into an HTML attribute list."
  8875. (let ((s "") key value)
  8876. (while plist
  8877. (setq key (pop plist) value (pop plist))
  8878. (and value
  8879. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8880. s))
  8881. ;;; Opening/following a link
  8882. (defvar org-link-search-failed nil)
  8883. (defvar org-open-link-functions nil
  8884. "Hook for functions finding a plain text link.
  8885. These functions must take a single argument, the link content.
  8886. They will be called for links that look like [[link text][description]]
  8887. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8888. like a filename (e.g. \"./blue.png\").
  8889. These functions will be called *before* Org attempts to resolve the
  8890. link by doing text searches in the current buffer - so if you want a
  8891. link \"[[target]]\" to still find \"<<target>>\", your function should
  8892. handle this as a special case.
  8893. When the function does handle the link, it must return a non-nil value.
  8894. If it decides that it is not responsible for this link, it must return
  8895. nil to indicate that that Org-mode can continue with other options
  8896. like exact and fuzzy text search.")
  8897. (defun org-next-link (&optional search-backward)
  8898. "Move forward to the next link.
  8899. If the link is in hidden text, expose it."
  8900. (interactive "P")
  8901. (when (and org-link-search-failed (eq this-command last-command))
  8902. (goto-char (point-min))
  8903. (message "Link search wrapped back to beginning of buffer"))
  8904. (setq org-link-search-failed nil)
  8905. (let* ((pos (point))
  8906. (ct (org-context))
  8907. (a (assoc :link ct))
  8908. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  8909. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  8910. ((looking-at org-any-link-re)
  8911. ;; Don't stay stuck at link without an org-link face
  8912. (forward-char (if search-backward -1 1))))
  8913. (if (funcall srch-fun org-any-link-re nil t)
  8914. (progn
  8915. (goto-char (match-beginning 0))
  8916. (if (outline-invisible-p) (org-show-context)))
  8917. (goto-char pos)
  8918. (setq org-link-search-failed t)
  8919. (message "No further link found"))))
  8920. (defun org-previous-link ()
  8921. "Move backward to the previous link.
  8922. If the link is in hidden text, expose it."
  8923. (interactive)
  8924. (funcall 'org-next-link t))
  8925. (defun org-translate-link (s)
  8926. "Translate a link string if a translation function has been defined."
  8927. (if (and org-link-translation-function
  8928. (fboundp org-link-translation-function)
  8929. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8930. (progn
  8931. (setq s (funcall org-link-translation-function
  8932. (match-string 1 s) (match-string 2 s)))
  8933. (concat (car s) ":" (cdr s)))
  8934. s))
  8935. (defun org-translate-link-from-planner (type path)
  8936. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8937. This is still an experimental function, your mileage may vary."
  8938. (cond
  8939. ((member type '("http" "https" "news" "ftp"))
  8940. ;; standard Internet links are the same.
  8941. nil)
  8942. ((and (equal type "irc") (string-match "^//" path))
  8943. ;; Planner has two / at the beginning of an irc link, we have 1.
  8944. ;; We should have zero, actually....
  8945. (setq path (substring path 1)))
  8946. ((and (equal type "lisp") (string-match "^/" path))
  8947. ;; Planner has a slash, we do not.
  8948. (setq type "elisp" path (substring path 1)))
  8949. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8950. ;; A typical message link. Planner has the id after the final slash,
  8951. ;; we separate it with a hash mark
  8952. (setq path (concat (match-string 1 path) "#"
  8953. (org-remove-angle-brackets (match-string 2 path)))))
  8954. )
  8955. (cons type path))
  8956. (defun org-find-file-at-mouse (ev)
  8957. "Open file link or URL at mouse."
  8958. (interactive "e")
  8959. (mouse-set-point ev)
  8960. (org-open-at-point 'in-emacs))
  8961. (defun org-open-at-mouse (ev)
  8962. "Open file link or URL at mouse.
  8963. See the docstring of `org-open-file' for details."
  8964. (interactive "e")
  8965. (mouse-set-point ev)
  8966. (if (eq major-mode 'org-agenda-mode)
  8967. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8968. (org-open-at-point))
  8969. (defvar org-window-config-before-follow-link nil
  8970. "The window configuration before following a link.
  8971. This is saved in case the need arises to restore it.")
  8972. (defvar org-open-link-marker (make-marker)
  8973. "Marker pointing to the location where `org-open-at-point; was called.")
  8974. ;;;###autoload
  8975. (defun org-open-at-point-global ()
  8976. "Follow a link like Org-mode does.
  8977. This command can be called in any mode to follow a link that has
  8978. Org-mode syntax."
  8979. (interactive)
  8980. (org-run-like-in-org-mode 'org-open-at-point))
  8981. ;;;###autoload
  8982. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8983. "Open a link in the string S, as if it was in Org-mode."
  8984. (interactive "sLink: \nP")
  8985. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8986. (with-temp-buffer
  8987. (let ((org-inhibit-startup (not reference-buffer)))
  8988. (org-mode)
  8989. (insert s)
  8990. (goto-char (point-min))
  8991. (when reference-buffer
  8992. (setq org-link-abbrev-alist-local
  8993. (with-current-buffer reference-buffer
  8994. org-link-abbrev-alist-local)))
  8995. (org-open-at-point arg reference-buffer)))))
  8996. (defvar org-open-at-point-functions nil
  8997. "Hook that is run when following a link at point.
  8998. Functions in this hook must return t if they identify and follow
  8999. a link at point. If they don't find anything interesting at point,
  9000. they must return nil.")
  9001. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9002. (defun org-open-at-point (&optional arg reference-buffer)
  9003. "Open link at or after point.
  9004. If there is no link at point, this function will search forward up to
  9005. the end of the current line.
  9006. Normally, files will be opened by an appropriate application. If the
  9007. optional prefix argument ARG is non-nil, Emacs will visit the file.
  9008. With a double prefix argument, try to open outside of Emacs, in the
  9009. application the system uses for this file type."
  9010. (interactive "P")
  9011. ;; if in a code block, then open the block's results
  9012. (unless (call-interactively #'org-babel-open-src-block-result)
  9013. (org-load-modules-maybe)
  9014. (move-marker org-open-link-marker (point))
  9015. (setq org-window-config-before-follow-link (current-window-configuration))
  9016. (org-remove-occur-highlights nil nil t)
  9017. (cond
  9018. ((and (org-at-heading-p)
  9019. (not (org-at-timestamp-p t))
  9020. (not (org-in-regexp
  9021. (concat org-plain-link-re "\\|"
  9022. org-bracket-link-regexp "\\|"
  9023. org-angle-link-re "\\|"
  9024. "[ \t]:[^ \t\n]+:[ \t]*$")))
  9025. (not (get-text-property (point) 'org-linked-text)))
  9026. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  9027. (lk0 (car lkall))
  9028. (lk (if (stringp lk0) (list lk0) lk0))
  9029. (lkend (cdr lkall)))
  9030. (mapcar (lambda(l)
  9031. (search-forward l nil lkend)
  9032. (goto-char (match-beginning 0))
  9033. (org-open-at-point))
  9034. lk))
  9035. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  9036. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  9037. ((and (org-at-timestamp-p t)
  9038. (not (org-in-regexp org-bracket-link-regexp)))
  9039. (org-follow-timestamp-link))
  9040. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  9041. (not (org-in-regexp org-any-link-re)))
  9042. (org-footnote-action))
  9043. (t
  9044. (let (type path link line search (pos (point)))
  9045. (catch 'match
  9046. (save-excursion
  9047. (skip-chars-forward "^]\n\r")
  9048. (when (org-in-regexp org-bracket-link-regexp 1)
  9049. (setq link (org-extract-attributes
  9050. (org-link-unescape (org-match-string-no-properties 1))))
  9051. (while (string-match " *\n *" link)
  9052. (setq link (replace-match " " t t link)))
  9053. (setq link (org-link-expand-abbrev link))
  9054. (cond
  9055. ((or (file-name-absolute-p link)
  9056. (string-match "^\\.\\.?/" link))
  9057. (setq type "file" path link))
  9058. ((string-match org-link-re-with-space3 link)
  9059. (setq type (match-string 1 link) path (match-string 2 link)))
  9060. ((string-match "^help:+\\(.+\\)" link)
  9061. (setq type "help" path (match-string 1 link)))
  9062. (t (setq type "thisfile" path link)))
  9063. (throw 'match t)))
  9064. (when (get-text-property (point) 'org-linked-text)
  9065. (setq type "thisfile"
  9066. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  9067. (1+ (point)) (point))
  9068. path (buffer-substring
  9069. (or (previous-single-property-change pos 'org-linked-text)
  9070. (point-min))
  9071. (or (next-single-property-change pos 'org-linked-text)
  9072. (point-max))))
  9073. (throw 'match t))
  9074. (save-excursion
  9075. (when (or (org-in-regexp org-angle-link-re)
  9076. (let ((match (org-in-regexp org-plain-link-re)))
  9077. ;; Check a plain link is not within a bracket link
  9078. (and match
  9079. (save-excursion
  9080. (progn
  9081. (goto-char (car match))
  9082. (not (org-in-regexp org-bracket-link-regexp))))))
  9083. (let ((line_ending (save-excursion (end-of-line) (point))))
  9084. ;; We are in a line before a plain or bracket link
  9085. (or (re-search-forward org-plain-link-re line_ending t)
  9086. (re-search-forward org-bracket-link-regexp line_ending t))))
  9087. (setq type (match-string 1)
  9088. path (org-link-unescape (match-string 2)))
  9089. (throw 'match t)))
  9090. (save-excursion
  9091. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  9092. (setq type "tags"
  9093. path (match-string 1))
  9094. (while (string-match ":" path)
  9095. (setq path (replace-match "+" t t path)))
  9096. (throw 'match t)))
  9097. (when (org-in-regexp "<\\([^><\n]+\\)>")
  9098. (setq type "tree-match"
  9099. path (match-string 1))
  9100. (throw 'match t)))
  9101. (unless path
  9102. (user-error "No link found"))
  9103. ;; switch back to reference buffer
  9104. ;; needed when if called in a temporary buffer through
  9105. ;; org-open-link-from-string
  9106. (with-current-buffer (or reference-buffer (current-buffer))
  9107. ;; Remove any trailing spaces in path
  9108. (if (string-match " +\\'" path)
  9109. (setq path (replace-match "" t t path)))
  9110. (if (and org-link-translation-function
  9111. (fboundp org-link-translation-function))
  9112. ;; Check if we need to translate the link
  9113. (let ((tmp (funcall org-link-translation-function type path)))
  9114. (setq type (car tmp) path (cdr tmp))))
  9115. (cond
  9116. ((assoc type org-link-protocols)
  9117. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9118. ((equal type "help")
  9119. (let ((f-or-v (intern path)))
  9120. (cond ((fboundp f-or-v)
  9121. (describe-function f-or-v))
  9122. ((boundp f-or-v)
  9123. (describe-variable f-or-v))
  9124. (t (error "Not a known function or variable")))))
  9125. ((equal type "mailto")
  9126. (let ((cmd (car org-link-mailto-program))
  9127. (args (cdr org-link-mailto-program)) args1
  9128. (address path) (subject "") a)
  9129. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9130. (setq address (match-string 1 path)
  9131. subject (org-link-escape (match-string 2 path))))
  9132. (while args
  9133. (cond
  9134. ((not (stringp (car args))) (push (pop args) args1))
  9135. (t (setq a (pop args))
  9136. (if (string-match "%a" a)
  9137. (setq a (replace-match address t t a)))
  9138. (if (string-match "%s" a)
  9139. (setq a (replace-match subject t t a)))
  9140. (push a args1))))
  9141. (apply cmd (nreverse args1))))
  9142. ((member type '("http" "https" "ftp" "news"))
  9143. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9144. (org-link-escape
  9145. path org-link-escape-chars-browser)
  9146. path))))
  9147. ((string= type "doi")
  9148. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9149. (org-link-escape
  9150. path org-link-escape-chars-browser)
  9151. path))))
  9152. ((member type '("message"))
  9153. (browse-url (concat type ":" path)))
  9154. ((string= type "tags")
  9155. (org-tags-view arg path))
  9156. ((string= type "tree-match")
  9157. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9158. ((string= type "file")
  9159. (if (string-match "::\\([0-9]+\\)\\'" path)
  9160. (setq line (string-to-number (match-string 1 path))
  9161. path (substring path 0 (match-beginning 0)))
  9162. (if (string-match "::\\(.+\\)\\'" path)
  9163. (setq search (match-string 1 path)
  9164. path (substring path 0 (match-beginning 0)))))
  9165. (if (string-match "[*?{]" (file-name-nondirectory path))
  9166. (dired path)
  9167. (org-open-file path arg line search)))
  9168. ((string= type "shell")
  9169. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9170. (cmd path))
  9171. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9172. (string-match org-confirm-shell-link-not-regexp cmd))
  9173. (not org-confirm-shell-link-function)
  9174. (funcall org-confirm-shell-link-function
  9175. (format "Execute \"%s\" in shell? "
  9176. (org-add-props cmd nil
  9177. 'face 'org-warning))))
  9178. (progn
  9179. (message "Executing %s" cmd)
  9180. (shell-command cmd buf)
  9181. (if (featurep 'midnight)
  9182. (setq clean-buffer-list-kill-buffer-names
  9183. (cons buf clean-buffer-list-kill-buffer-names))))
  9184. (error "Abort"))))
  9185. ((string= type "elisp")
  9186. (let ((cmd path))
  9187. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9188. (string-match org-confirm-elisp-link-not-regexp cmd))
  9189. (not org-confirm-elisp-link-function)
  9190. (funcall org-confirm-elisp-link-function
  9191. (format "Execute \"%s\" as elisp? "
  9192. (org-add-props cmd nil
  9193. 'face 'org-warning))))
  9194. (message "%s => %s" cmd
  9195. (if (equal (string-to-char cmd) ?\()
  9196. (eval (read cmd))
  9197. (call-interactively (read cmd))))
  9198. (error "Abort"))))
  9199. ((and (string= type "thisfile")
  9200. (run-hook-with-args-until-success
  9201. 'org-open-link-functions path)))
  9202. ((string= type "thisfile")
  9203. (if arg
  9204. (switch-to-buffer-other-window
  9205. (org-get-buffer-for-internal-link (current-buffer)))
  9206. (org-mark-ring-push))
  9207. (let ((cmd `(org-link-search
  9208. ,path
  9209. ,(cond ((equal arg '(4)) ''occur)
  9210. ((equal arg '(16)) ''org-occur))
  9211. ,pos)))
  9212. (condition-case nil (let ((org-link-search-inhibit-query t))
  9213. (eval cmd))
  9214. (error (progn (widen) (eval cmd))))))
  9215. (t (browse-url-at-point)))))))
  9216. (move-marker org-open-link-marker nil)
  9217. (run-hook-with-args 'org-follow-link-hook)))
  9218. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9219. "Offer links in the current entry and return the selected link.
  9220. If there is only one link, return it.
  9221. If NTH is an integer, return the NTH link found.
  9222. If ZERO is a string, check also this string for a link, and if
  9223. there is one, return it."
  9224. (with-current-buffer buffer
  9225. (save-excursion
  9226. (save-restriction
  9227. (widen)
  9228. (goto-char marker)
  9229. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9230. "\\(" org-angle-link-re "\\)\\|"
  9231. "\\(" org-plain-link-re "\\)"))
  9232. (cnt ?0)
  9233. (in-emacs (if (integerp nth) nil nth))
  9234. have-zero end links link c)
  9235. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9236. (push (match-string 0 zero) links)
  9237. (setq cnt (1- cnt) have-zero t))
  9238. (save-excursion
  9239. (org-back-to-heading t)
  9240. (setq end (save-excursion (outline-next-heading) (point)))
  9241. (while (re-search-forward re end t)
  9242. (push (match-string 0) links))
  9243. (setq links (org-uniquify (reverse links))))
  9244. (cond
  9245. ((null links)
  9246. (message "No links"))
  9247. ((equal (length links) 1)
  9248. (setq link (car links)))
  9249. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9250. (setq link (nth (if have-zero nth (1- nth)) links)))
  9251. (t ; we have to select a link
  9252. (save-excursion
  9253. (save-window-excursion
  9254. (delete-other-windows)
  9255. (with-output-to-temp-buffer "*Select Link*"
  9256. (mapc (lambda (l)
  9257. (if (not (string-match org-bracket-link-regexp l))
  9258. (princ (format "[%c] %s\n" (incf cnt)
  9259. (org-remove-angle-brackets l)))
  9260. (if (match-end 3)
  9261. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9262. (match-string 3 l) (match-string 1 l)))
  9263. (princ (format "[%c] %s\n" (incf cnt)
  9264. (match-string 1 l))))))
  9265. links))
  9266. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9267. (message "Select link to open, RET to open all:")
  9268. (setq c (read-char-exclusive))
  9269. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9270. (when (equal c ?q) (error "Abort"))
  9271. (if (equal c ?\C-m)
  9272. (setq link links)
  9273. (setq nth (- c ?0))
  9274. (if have-zero (setq nth (1+ nth)))
  9275. (unless (and (integerp nth) (>= (length links) nth))
  9276. (error "Invalid link selection"))
  9277. (setq link (nth (1- nth) links)))))
  9278. (cons link end))))))
  9279. ;; Add special file links that specify the way of opening
  9280. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9281. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9282. (defun org-open-file-with-system (path)
  9283. "Open file at PATH using the system way of opening it."
  9284. (org-open-file path 'system))
  9285. (defun org-open-file-with-emacs (path)
  9286. "Open file at PATH in Emacs."
  9287. (org-open-file path 'emacs))
  9288. ;;; File search
  9289. (defvar org-create-file-search-functions nil
  9290. "List of functions to construct the right search string for a file link.
  9291. These functions are called in turn with point at the location to
  9292. which the link should point.
  9293. A function in the hook should first test if it would like to
  9294. handle this file type, for example by checking the `major-mode'
  9295. or the file extension. If it decides not to handle this file, it
  9296. should just return nil to give other functions a chance. If it
  9297. does handle the file, it must return the search string to be used
  9298. when following the link. The search string will be part of the
  9299. file link, given after a double colon, and `org-open-at-point'
  9300. will automatically search for it. If special measures must be
  9301. taken to make the search successful, another function should be
  9302. added to the companion hook `org-execute-file-search-functions',
  9303. which see.
  9304. A function in this hook may also use `setq' to set the variable
  9305. `description' to provide a suggestion for the descriptive text to
  9306. be used for this link when it gets inserted into an Org-mode
  9307. buffer with \\[org-insert-link].")
  9308. (defvar org-execute-file-search-functions nil
  9309. "List of functions to execute a file search triggered by a link.
  9310. Functions added to this hook must accept a single argument, the
  9311. search string that was part of the file link, the part after the
  9312. double colon. The function must first check if it would like to
  9313. handle this search, for example by checking the `major-mode' or
  9314. the file extension. If it decides not to handle this search, it
  9315. should just return nil to give other functions a chance. If it
  9316. does handle the search, it must return a non-nil value to keep
  9317. other functions from trying.
  9318. Each function can access the current prefix argument through the
  9319. variable `current-prefix-argument'. Note that a single prefix is
  9320. used to force opening a link in Emacs, so it may be good to only
  9321. use a numeric or double prefix to guide the search function.
  9322. In case this is needed, a function in this hook can also restore
  9323. the window configuration before `org-open-at-point' was called using:
  9324. (set-window-configuration org-window-config-before-follow-link)")
  9325. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9326. (defun org-link-search (s &optional type avoid-pos stealth)
  9327. "Search for a link search option.
  9328. If S is surrounded by forward slashes, it is interpreted as a
  9329. regular expression. In org-mode files, this will create an `org-occur'
  9330. sparse tree. In ordinary files, `occur' will be used to list matches.
  9331. If the current buffer is in `dired-mode', grep will be used to search
  9332. in all files. If AVOID-POS is given, ignore matches near that position.
  9333. When optional argument STEALTH is non-nil, do not modify
  9334. visibility around point, thus ignoring
  9335. `org-show-hierarchy-above', `org-show-following-heading' and
  9336. `org-show-siblings' variables."
  9337. (let ((case-fold-search t)
  9338. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9339. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9340. (append '(("") (" ") ("\t") ("\n"))
  9341. org-emphasis-alist)
  9342. "\\|") "\\)"))
  9343. (pos (point))
  9344. (pre nil) (post nil)
  9345. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9346. (cond
  9347. ;; First check if there are any special search functions
  9348. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9349. ;; Now try the builtin stuff
  9350. ((and (equal (string-to-char s0) ?#)
  9351. (> (length s0) 1)
  9352. (save-excursion
  9353. (goto-char (point-min))
  9354. (and
  9355. (re-search-forward
  9356. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9357. (setq type 'dedicated
  9358. pos (match-beginning 0))))
  9359. ;; There is an exact target for this
  9360. (goto-char pos)
  9361. (org-back-to-heading t)))
  9362. ((save-excursion
  9363. (goto-char (point-min))
  9364. (and
  9365. (re-search-forward
  9366. (concat "<<" (regexp-quote s0) ">>") nil t)
  9367. (setq type 'dedicated
  9368. pos (match-beginning 0))))
  9369. ;; There is an exact target for this
  9370. (goto-char pos))
  9371. ((save-excursion
  9372. (goto-char (point-min))
  9373. (and
  9374. (re-search-forward
  9375. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9376. (setq type 'dedicated pos (match-beginning 0))))
  9377. ;; Found an invisible target.
  9378. (goto-char pos))
  9379. ((save-excursion
  9380. (goto-char (point-min))
  9381. (and
  9382. (re-search-forward
  9383. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9384. (setq type 'dedicated pos (match-beginning 0))))
  9385. ;; Found an element with a matching #+name affiliated keyword.
  9386. (goto-char pos))
  9387. ((and (string-match "^(\\(.*\\))$" s0)
  9388. (save-excursion
  9389. (goto-char (point-min))
  9390. (and
  9391. (re-search-forward
  9392. (concat "[^[]" (regexp-quote
  9393. (format org-coderef-label-format
  9394. (match-string 1 s0))))
  9395. nil t)
  9396. (setq type 'dedicated
  9397. pos (1+ (match-beginning 0))))))
  9398. ;; There is a coderef target for this
  9399. (goto-char pos))
  9400. ((string-match "^/\\(.*\\)/$" s)
  9401. ;; A regular expression
  9402. (cond
  9403. ((derived-mode-p 'org-mode)
  9404. (org-occur (match-string 1 s)))
  9405. (t (org-do-occur (match-string 1 s)))))
  9406. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9407. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9408. (goto-char (point-min))
  9409. (cond
  9410. ((let (case-fold-search)
  9411. (re-search-forward (format org-complex-heading-regexp-format
  9412. (regexp-quote s))
  9413. nil t))
  9414. ;; OK, found a match
  9415. (setq type 'dedicated)
  9416. (goto-char (match-beginning 0)))
  9417. ((and (not org-link-search-inhibit-query)
  9418. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9419. (y-or-n-p "No match - create this as a new heading? "))
  9420. (goto-char (point-max))
  9421. (or (bolp) (newline))
  9422. (insert "* " s "\n")
  9423. (beginning-of-line 0))
  9424. (t
  9425. (goto-char pos)
  9426. (error "No match"))))
  9427. (t
  9428. ;; A normal search string
  9429. (when (equal (string-to-char s) ?*)
  9430. ;; Anchor on headlines, post may include tags.
  9431. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9432. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9433. s (substring s 1)))
  9434. (remove-text-properties
  9435. 0 (length s)
  9436. '(face nil mouse-face nil keymap nil fontified nil) s)
  9437. ;; Make a series of regular expressions to find a match
  9438. (setq words (org-split-string s "[ \n\r\t]+")
  9439. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9440. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9441. "\\)" markers)
  9442. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9443. re2a (concat "[ \t\r\n]" re2a_)
  9444. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9445. re4 (concat "[^a-zA-Z_]" re4_)
  9446. re1 (concat pre re2 post)
  9447. re3 (concat pre (if pre re4_ re4) post)
  9448. re5 (concat pre ".*" re4)
  9449. re2 (concat pre re2)
  9450. re2a (concat pre (if pre re2a_ re2a))
  9451. re4 (concat pre (if pre re4_ re4))
  9452. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9453. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9454. re5 "\\)"
  9455. ))
  9456. (cond
  9457. ((eq type 'org-occur) (org-occur reall))
  9458. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9459. (t (goto-char (point-min))
  9460. (setq type 'fuzzy)
  9461. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9462. (org-search-not-self 1 re1 nil t)
  9463. (org-search-not-self 1 re2 nil t)
  9464. (org-search-not-self 1 re2a nil t)
  9465. (org-search-not-self 1 re3 nil t)
  9466. (org-search-not-self 1 re4 nil t)
  9467. (org-search-not-self 1 re5 nil t)
  9468. )
  9469. (goto-char (match-beginning 1))
  9470. (goto-char pos)
  9471. (error "No match"))))))
  9472. (and (derived-mode-p 'org-mode)
  9473. (not stealth)
  9474. (org-show-context 'link-search))
  9475. type))
  9476. (defun org-search-not-self (group &rest args)
  9477. "Execute `re-search-forward', but only accept matches that do not
  9478. enclose the position of `org-open-link-marker'."
  9479. (let ((m org-open-link-marker))
  9480. (catch 'exit
  9481. (while (apply 're-search-forward args)
  9482. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9483. (goto-char (match-end group))
  9484. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9485. (> (match-beginning 0) (marker-position m))
  9486. (< (match-end 0) (marker-position m)))
  9487. (save-match-data
  9488. (or (not (org-in-regexp
  9489. org-bracket-link-analytic-regexp 1))
  9490. (not (match-end 4)) ; no description
  9491. (and (<= (match-beginning 4) (point))
  9492. (>= (match-end 4) (point))))))
  9493. (throw 'exit (point))))))))
  9494. (defun org-get-buffer-for-internal-link (buffer)
  9495. "Return a buffer to be used for displaying the link target of internal links."
  9496. (cond
  9497. ((not org-display-internal-link-with-indirect-buffer)
  9498. buffer)
  9499. ((string-match "(Clone)$" (buffer-name buffer))
  9500. (message "Buffer is already a clone, not making another one")
  9501. ;; we also do not modify visibility in this case
  9502. buffer)
  9503. (t ; make a new indirect buffer for displaying the link
  9504. (let* ((bn (buffer-name buffer))
  9505. (ibn (concat bn "(Clone)"))
  9506. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9507. (with-current-buffer ib (org-overview))
  9508. ib))))
  9509. (defun org-do-occur (regexp &optional cleanup)
  9510. "Call the Emacs command `occur'.
  9511. If CLEANUP is non-nil, remove the printout of the regular expression
  9512. in the *Occur* buffer. This is useful if the regex is long and not useful
  9513. to read."
  9514. (occur regexp)
  9515. (when cleanup
  9516. (let ((cwin (selected-window)) win beg end)
  9517. (when (setq win (get-buffer-window "*Occur*"))
  9518. (select-window win))
  9519. (goto-char (point-min))
  9520. (when (re-search-forward "match[a-z]+" nil t)
  9521. (setq beg (match-end 0))
  9522. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9523. (setq end (1- (match-beginning 0)))))
  9524. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9525. (goto-char (point-min))
  9526. (select-window cwin))))
  9527. ;;; The mark ring for links jumps
  9528. (defvar org-mark-ring nil
  9529. "Mark ring for positions before jumps in Org-mode.")
  9530. (defvar org-mark-ring-last-goto nil
  9531. "Last position in the mark ring used to go back.")
  9532. ;; Fill and close the ring
  9533. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9534. (loop for i from 1 to org-mark-ring-length do
  9535. (push (make-marker) org-mark-ring))
  9536. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9537. org-mark-ring)
  9538. (defun org-mark-ring-push (&optional pos buffer)
  9539. "Put the current position or POS into the mark ring and rotate it."
  9540. (interactive)
  9541. (setq pos (or pos (point)))
  9542. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9543. (move-marker (car org-mark-ring)
  9544. (or pos (point))
  9545. (or buffer (current-buffer)))
  9546. (message "%s"
  9547. (substitute-command-keys
  9548. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9549. (defun org-mark-ring-goto (&optional n)
  9550. "Jump to the previous position in the mark ring.
  9551. With prefix arg N, jump back that many stored positions. When
  9552. called several times in succession, walk through the entire ring.
  9553. Org-mode commands jumping to a different position in the current file,
  9554. or to another Org-mode file, automatically push the old position
  9555. onto the ring."
  9556. (interactive "p")
  9557. (let (p m)
  9558. (if (eq last-command this-command)
  9559. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9560. (setq p org-mark-ring))
  9561. (setq org-mark-ring-last-goto p)
  9562. (setq m (car p))
  9563. (org-pop-to-buffer-same-window (marker-buffer m))
  9564. (goto-char m)
  9565. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9566. (defun org-remove-angle-brackets (s)
  9567. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9568. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9569. s)
  9570. (defun org-add-angle-brackets (s)
  9571. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9572. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9573. s)
  9574. (defun org-remove-double-quotes (s)
  9575. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9576. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9577. s)
  9578. ;;; Following specific links
  9579. (defun org-follow-timestamp-link ()
  9580. "Open an agenda view for the time-stamp date/range at point."
  9581. (cond
  9582. ((org-at-date-range-p t)
  9583. (let ((org-agenda-start-on-weekday)
  9584. (t1 (match-string 1))
  9585. (t2 (match-string 2)) tt1 tt2)
  9586. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9587. tt2 (time-to-days (org-time-string-to-time t2)))
  9588. (let ((org-agenda-buffer-tmp-name
  9589. (format "*Org Agenda(a:%s)"
  9590. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9591. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9592. ((org-at-timestamp-p t)
  9593. (let ((org-agenda-buffer-tmp-name
  9594. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9595. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9596. (substring (match-string 1) 0 10)))
  9597. 1)))
  9598. (t (error "This should not happen"))))
  9599. ;;; Following file links
  9600. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9601. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9602. (declare-function mailcap-mime-info
  9603. "mailcap" (string &optional request no-decode))
  9604. (defvar org-wait nil)
  9605. (defun org-open-file (path &optional in-emacs line search)
  9606. "Open the file at PATH.
  9607. First, this expands any special file name abbreviations. Then the
  9608. configuration variable `org-file-apps' is checked if it contains an
  9609. entry for this file type, and if yes, the corresponding command is launched.
  9610. If no application is found, Emacs simply visits the file.
  9611. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9612. With a double \\[universal-argument] \\[universal-argument] \
  9613. prefix arg, Org tries to avoid opening in Emacs
  9614. and to use an external application to visit the file.
  9615. Optional LINE specifies a line to go to, optional SEARCH a string
  9616. to search for. If LINE or SEARCH is given, the file will be
  9617. opened in Emacs, unless an entry from org-file-apps that makes
  9618. use of groups in a regexp matches.
  9619. If you want to change the way frames are used when following a
  9620. link, please customize `org-link-frame-setup'.
  9621. If the file does not exist, an error is thrown."
  9622. (let* ((file (if (equal path "")
  9623. buffer-file-name
  9624. (substitute-in-file-name (expand-file-name path))))
  9625. (file-apps (append org-file-apps (org-default-apps)))
  9626. (apps (org-remove-if
  9627. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9628. (apps-dlink (org-remove-if-not
  9629. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9630. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9631. (dirp (if remp nil (file-directory-p file)))
  9632. (file (if (and dirp org-open-directory-means-index-dot-org)
  9633. (concat (file-name-as-directory file) "index.org")
  9634. file))
  9635. (a-m-a-p (assq 'auto-mode apps))
  9636. (dfile (downcase file))
  9637. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9638. (link (cond ((and (eq line nil)
  9639. (eq search nil))
  9640. file)
  9641. (line
  9642. (concat file "::" (number-to-string line)))
  9643. (search
  9644. (concat file "::" search))))
  9645. (dlink (downcase link))
  9646. (old-buffer (current-buffer))
  9647. (old-pos (point))
  9648. (old-mode major-mode)
  9649. ext cmd link-match-data)
  9650. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9651. (setq ext (match-string 1 dfile))
  9652. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9653. (setq ext (match-string 1 dfile))))
  9654. (cond
  9655. ((member in-emacs '((16) system))
  9656. (setq cmd (cdr (assoc 'system apps))))
  9657. (in-emacs (setq cmd 'emacs))
  9658. (t
  9659. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9660. (and dirp (cdr (assoc 'directory apps)))
  9661. ; first, try matching against apps-dlink
  9662. ; if we get a match here, store the match data for later
  9663. (let ((match (assoc-default dlink apps-dlink
  9664. 'string-match)))
  9665. (if match
  9666. (progn (setq link-match-data (match-data))
  9667. match)
  9668. (progn (setq in-emacs (or in-emacs line search))
  9669. nil))) ; if we have no match in apps-dlink,
  9670. ; always open the file in emacs if line or search
  9671. ; is given (for backwards compatibility)
  9672. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9673. 'string-match)
  9674. (cdr (assoc ext apps))
  9675. (cdr (assoc t apps))))))
  9676. (when (eq cmd 'system)
  9677. (setq cmd (cdr (assoc 'system apps))))
  9678. (when (eq cmd 'default)
  9679. (setq cmd (cdr (assoc t apps))))
  9680. (when (eq cmd 'mailcap)
  9681. (require 'mailcap)
  9682. (mailcap-parse-mailcaps)
  9683. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9684. (command (mailcap-mime-info mime-type)))
  9685. (if (stringp command)
  9686. (setq cmd command)
  9687. (setq cmd 'emacs))))
  9688. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9689. (not (file-exists-p file))
  9690. (not org-open-non-existing-files))
  9691. (error "No such file: %s" file))
  9692. (cond
  9693. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9694. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9695. (while (string-match "['\"]%s['\"]" cmd)
  9696. (setq cmd (replace-match "%s" t t cmd)))
  9697. (while (string-match "%s" cmd)
  9698. (setq cmd (replace-match
  9699. (save-match-data
  9700. (shell-quote-argument
  9701. (convert-standard-filename file)))
  9702. t t cmd)))
  9703. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9704. (save-match-data
  9705. (let ((match-index 1)
  9706. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9707. (set-match-data link-match-data)
  9708. (while (<= match-index number-of-groups)
  9709. (let ((regex (concat "%" (number-to-string match-index)))
  9710. (replace-with (match-string match-index dlink)))
  9711. (while (string-match regex cmd)
  9712. (setq cmd (replace-match replace-with t t cmd))))
  9713. (setq match-index (+ match-index 1)))))
  9714. (save-window-excursion
  9715. (message "Running %s...done" cmd)
  9716. (start-process-shell-command cmd nil cmd)
  9717. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9718. ((or (stringp cmd)
  9719. (eq cmd 'emacs))
  9720. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9721. (widen)
  9722. (if line (org-goto-line line)
  9723. (if search (org-link-search search))))
  9724. ((consp cmd)
  9725. (let ((file (convert-standard-filename file)))
  9726. (save-match-data
  9727. (set-match-data link-match-data)
  9728. (eval cmd))))
  9729. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9730. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9731. (or (not (equal old-buffer (current-buffer)))
  9732. (not (equal old-pos (point))))
  9733. (org-mark-ring-push old-pos old-buffer))))
  9734. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9735. "This function returns non-nil if `entry' uses a regular
  9736. expression which should be matched against the whole link by
  9737. org-open-file.
  9738. It assumes that is the case when the entry uses a regular
  9739. expression which has at least one grouping construct and the
  9740. action is either a lisp form or a command string containing
  9741. '%1', i.e. using at least one subexpression match as a
  9742. parameter."
  9743. (let ((selector (car entry))
  9744. (action (cdr entry)))
  9745. (if (stringp selector)
  9746. (and (> (regexp-opt-depth selector) 0)
  9747. (or (and (stringp action)
  9748. (string-match "%[0-9]" action))
  9749. (consp action)))
  9750. nil)))
  9751. (defun org-default-apps ()
  9752. "Return the default applications for this operating system."
  9753. (cond
  9754. ((eq system-type 'darwin)
  9755. org-file-apps-defaults-macosx)
  9756. ((eq system-type 'windows-nt)
  9757. org-file-apps-defaults-windowsnt)
  9758. (t org-file-apps-defaults-gnu)))
  9759. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9760. "Convert extensions to regular expressions in the cars of LIST.
  9761. Also, weed out any non-string entries, because the return value is used
  9762. only for regexp matching.
  9763. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9764. point to the symbol `emacs', indicating that the file should
  9765. be opened in Emacs."
  9766. (append
  9767. (delq nil
  9768. (mapcar (lambda (x)
  9769. (if (not (stringp (car x)))
  9770. nil
  9771. (if (string-match "\\W" (car x))
  9772. x
  9773. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9774. list))
  9775. (if add-auto-mode
  9776. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9777. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9778. (defun org-file-remote-p (file)
  9779. "Test whether FILE specifies a location on a remote system.
  9780. Return non-nil if the location is indeed remote.
  9781. For example, the filename \"/user@host:/foo\" specifies a location
  9782. on the system \"/user@host:\"."
  9783. (cond ((fboundp 'file-remote-p)
  9784. (file-remote-p file))
  9785. ((fboundp 'tramp-handle-file-remote-p)
  9786. (tramp-handle-file-remote-p file))
  9787. ((and (boundp 'ange-ftp-name-format)
  9788. (string-match (car ange-ftp-name-format) file))
  9789. t)))
  9790. ;;;; Refiling
  9791. (defun org-get-org-file ()
  9792. "Read a filename, with default directory `org-directory'."
  9793. (let ((default (or org-default-notes-file remember-data-file)))
  9794. (read-file-name (format "File name [%s]: " default)
  9795. (file-name-as-directory org-directory)
  9796. default)))
  9797. (defun org-notes-order-reversed-p ()
  9798. "Check if the current file should receive notes in reversed order."
  9799. (cond
  9800. ((not org-reverse-note-order) nil)
  9801. ((eq t org-reverse-note-order) t)
  9802. ((not (listp org-reverse-note-order)) nil)
  9803. (t (catch 'exit
  9804. (let ((all org-reverse-note-order)
  9805. entry)
  9806. (while (setq entry (pop all))
  9807. (if (string-match (car entry) buffer-file-name)
  9808. (throw 'exit (cdr entry))))
  9809. nil)))))
  9810. (defvar org-refile-target-table nil
  9811. "The list of refile targets, created by `org-refile'.")
  9812. (defvar org-agenda-new-buffers nil
  9813. "Buffers created to visit agenda files.")
  9814. (defvar org-refile-cache nil
  9815. "Cache for refile targets.")
  9816. (defvar org-refile-markers nil
  9817. "All the markers used for caching refile locations.")
  9818. (defun org-refile-marker (pos)
  9819. "Get a new refile marker, but only if caching is in use."
  9820. (if (not org-refile-use-cache)
  9821. pos
  9822. (let ((m (make-marker)))
  9823. (move-marker m pos)
  9824. (push m org-refile-markers)
  9825. m)))
  9826. (defun org-refile-cache-clear ()
  9827. "Clear the refile cache and disable all the markers."
  9828. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9829. (setq org-refile-markers nil)
  9830. (setq org-refile-cache nil)
  9831. (message "Refile cache has been cleared"))
  9832. (defun org-refile-cache-check-set (set)
  9833. "Check if all the markers in the cache still have live buffers."
  9834. (let (marker)
  9835. (catch 'exit
  9836. (while (and set (setq marker (nth 3 (pop set))))
  9837. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9838. (when (and marker (null (marker-buffer marker)))
  9839. (message "not found") (sit-for 3)
  9840. (throw 'exit nil)))
  9841. t)))
  9842. (defun org-refile-cache-put (set &rest identifiers)
  9843. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9844. (let* ((key (sha1 (prin1-to-string identifiers)))
  9845. (entry (assoc key org-refile-cache)))
  9846. (if entry
  9847. (setcdr entry set)
  9848. (push (cons key set) org-refile-cache))))
  9849. (defun org-refile-cache-get (&rest identifiers)
  9850. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9851. (cond
  9852. ((not org-refile-cache) nil)
  9853. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9854. (t
  9855. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9856. org-refile-cache))))
  9857. (and set (org-refile-cache-check-set set) set)))))
  9858. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9859. "Produce a table with refile targets."
  9860. (let ((case-fold-search nil)
  9861. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9862. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9863. targets tgs txt re files f desc descre fast-path-p level pos0)
  9864. (message "Getting targets...")
  9865. (with-current-buffer (or default-buffer (current-buffer))
  9866. (while (setq entry (pop entries))
  9867. (setq files (car entry) desc (cdr entry))
  9868. (setq fast-path-p nil)
  9869. (cond
  9870. ((null files) (setq files (list (current-buffer))))
  9871. ((eq files 'org-agenda-files)
  9872. (setq files (org-agenda-files 'unrestricted)))
  9873. ((and (symbolp files) (fboundp files))
  9874. (setq files (funcall files)))
  9875. ((and (symbolp files) (boundp files))
  9876. (setq files (symbol-value files))))
  9877. (if (stringp files) (setq files (list files)))
  9878. (cond
  9879. ((eq (car desc) :tag)
  9880. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9881. ((eq (car desc) :todo)
  9882. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9883. ((eq (car desc) :regexp)
  9884. (setq descre (cdr desc)))
  9885. ((eq (car desc) :level)
  9886. (setq descre (concat "^\\*\\{" (number-to-string
  9887. (if org-odd-levels-only
  9888. (1- (* 2 (cdr desc)))
  9889. (cdr desc)))
  9890. "\\}[ \t]")))
  9891. ((eq (car desc) :maxlevel)
  9892. (setq fast-path-p t)
  9893. (setq descre (concat "^\\*\\{1," (number-to-string
  9894. (if org-odd-levels-only
  9895. (1- (* 2 (cdr desc)))
  9896. (cdr desc)))
  9897. "\\}[ \t]")))
  9898. (t (error "Bad refiling target description %s" desc)))
  9899. (while (setq f (pop files))
  9900. (with-current-buffer
  9901. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9902. (or
  9903. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9904. (progn
  9905. (if (bufferp f) (setq f (buffer-file-name
  9906. (buffer-base-buffer f))))
  9907. (setq f (and f (expand-file-name f)))
  9908. (if (eq org-refile-use-outline-path 'file)
  9909. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9910. (save-excursion
  9911. (save-restriction
  9912. (widen)
  9913. (goto-char (point-min))
  9914. (while (re-search-forward descre nil t)
  9915. (goto-char (setq pos0 (point-at-bol)))
  9916. (catch 'next
  9917. (when org-refile-target-verify-function
  9918. (save-match-data
  9919. (or (funcall org-refile-target-verify-function)
  9920. (throw 'next t))))
  9921. (when (and (looking-at org-complex-heading-regexp)
  9922. (not (member (match-string 4) excluded-entries))
  9923. (match-string 4))
  9924. (setq level (org-reduced-level
  9925. (- (match-end 1) (match-beginning 1)))
  9926. txt (org-link-display-format (match-string 4))
  9927. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9928. re (format org-complex-heading-regexp-format
  9929. (regexp-quote (match-string 4))))
  9930. (when org-refile-use-outline-path
  9931. (setq txt (mapconcat
  9932. 'org-protect-slash
  9933. (append
  9934. (if (eq org-refile-use-outline-path
  9935. 'file)
  9936. (list (file-name-nondirectory
  9937. (buffer-file-name
  9938. (buffer-base-buffer))))
  9939. (if (eq org-refile-use-outline-path
  9940. 'full-file-path)
  9941. (list (buffer-file-name
  9942. (buffer-base-buffer)))))
  9943. (org-get-outline-path fast-path-p
  9944. level txt)
  9945. (list txt))
  9946. "/")))
  9947. (push (list txt f re (org-refile-marker (point)))
  9948. tgs)))
  9949. (when (= (point) pos0)
  9950. ;; verification function has not moved point
  9951. (goto-char (point-at-eol))))))))
  9952. (when org-refile-use-cache
  9953. (org-refile-cache-put tgs (buffer-file-name) descre))
  9954. (setq targets (append tgs targets))
  9955. ))))
  9956. (message "Getting targets...done")
  9957. (nreverse targets)))
  9958. (defun org-protect-slash (s)
  9959. (while (string-match "/" s)
  9960. (setq s (replace-match "\\" t t s)))
  9961. s)
  9962. (defvar org-olpa (make-vector 20 nil))
  9963. (defun org-get-outline-path (&optional fastp level heading)
  9964. "Return the outline path to the current entry, as a list.
  9965. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9966. routine which makes outline path derivations for an entire file,
  9967. avoiding backtracing. Refile target collection makes use of that."
  9968. (if fastp
  9969. (progn
  9970. (if (> level 19)
  9971. (error "Outline path failure, more than 19 levels"))
  9972. (loop for i from level upto 19 do
  9973. (aset org-olpa i nil))
  9974. (prog1
  9975. (delq nil (append org-olpa nil))
  9976. (aset org-olpa level heading)))
  9977. (let (rtn case-fold-search)
  9978. (save-excursion
  9979. (save-restriction
  9980. (widen)
  9981. (while (org-up-heading-safe)
  9982. (when (looking-at org-complex-heading-regexp)
  9983. (push (org-match-string-no-properties 4) rtn)))
  9984. rtn)))))
  9985. (defun org-format-outline-path (path &optional width prefix separator)
  9986. "Format the outline path PATH for display.
  9987. WIDTH is the maximum number of characters that is available.
  9988. PREFIX is a prefix to be included in the returned string,
  9989. such as the file name.
  9990. SEPARATOR is inserted between the different parts of the path,
  9991. the default is \"/\"."
  9992. (setq width (or width 79))
  9993. (if prefix (setq width (- width (length prefix))))
  9994. (if (not path)
  9995. (or prefix "")
  9996. (let* ((nsteps (length path))
  9997. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9998. (maxwidth (if (<= total-width width)
  9999. 10000 ;; everything fits
  10000. ;; we need to shorten the level headings
  10001. (/ (- width nsteps) nsteps)))
  10002. (org-odd-levels-only nil)
  10003. (n 0)
  10004. (total (1+ (length prefix))))
  10005. (setq maxwidth (max maxwidth 10))
  10006. (concat prefix
  10007. (if prefix (or separator "/"))
  10008. (mapconcat
  10009. (lambda (h)
  10010. (setq n (1+ n))
  10011. (if (and (= n nsteps) (< maxwidth 10000))
  10012. (setq maxwidth (- total-width total)))
  10013. (if (< (length h) maxwidth)
  10014. (progn (setq total (+ total (length h) 1)) h)
  10015. (setq h (substring h 0 (- maxwidth 2))
  10016. total (+ total maxwidth 1))
  10017. (if (string-match "[ \t]+\\'" h)
  10018. (setq h (substring h 0 (match-beginning 0))))
  10019. (setq h (concat h "..")))
  10020. (org-add-props h nil 'face
  10021. (nth (% (1- n) org-n-level-faces)
  10022. org-level-faces))
  10023. h)
  10024. path (or separator "/"))))))
  10025. (defun org-display-outline-path (&optional file current separator just-return-string)
  10026. "Display the current outline path in the echo area.
  10027. If FILE is non-nil, prepend the output with the file name.
  10028. If CURRENT is non-nil, append the current heading to the output.
  10029. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10030. the different parts of the path and defaults to \"/\".
  10031. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10032. (interactive "P")
  10033. (let* (case-fold-search
  10034. message-log-max ; Don't populate the *Messages* buffer
  10035. (bfn (buffer-file-name (buffer-base-buffer)))
  10036. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10037. res)
  10038. (if current (setq path (append path
  10039. (save-excursion
  10040. (org-back-to-heading t)
  10041. (if (looking-at org-complex-heading-regexp)
  10042. (list (match-string 4)))))))
  10043. (setq res
  10044. (org-format-outline-path
  10045. path
  10046. (1- (frame-width))
  10047. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10048. separator))
  10049. (if just-return-string
  10050. (org-no-properties res)
  10051. (message "%s" res))))
  10052. (defvar org-refile-history nil
  10053. "History for refiling operations.")
  10054. (defvar org-after-refile-insert-hook nil
  10055. "Hook run after `org-refile' has inserted its stuff at the new location.
  10056. Note that this is still *before* the stuff will be removed from
  10057. the *old* location.")
  10058. (defvar org-capture-last-stored-marker)
  10059. (defvar org-refile-keep nil
  10060. "Non-nil means `org-refile' will copy instead of refile.")
  10061. (defun org-copy ()
  10062. "Like `org-refile', but copy."
  10063. (interactive)
  10064. (let ((org-refile-keep t))
  10065. (funcall 'org-refile nil nil nil "Copy")))
  10066. (defun org-refile (&optional goto default-buffer rfloc msg)
  10067. "Move the entry or entries at point to another heading.
  10068. The list of target headings is compiled using the information in
  10069. `org-refile-targets', which see.
  10070. At the target location, the entry is filed as a subitem of the target
  10071. heading. Depending on `org-reverse-note-order', the new subitem will
  10072. either be the first or the last subitem.
  10073. If there is an active region, all entries in that region will be moved.
  10074. However, the region must fulfill the requirement that the first heading
  10075. is the first one sets the top-level of the moved text - at most siblings
  10076. below it are allowed.
  10077. With prefix arg GOTO, the command will only visit the target location
  10078. and not actually move anything.
  10079. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  10080. go to the location where the last refiling operation has put the subtree.
  10081. With a prefix argument of `2', refile to the running clock.
  10082. RFLOC can be a refile location obtained in a different way.
  10083. MSG is a string to replace \"Refile\" in the default prompt with
  10084. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10085. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10086. If you are using target caching (see `org-refile-use-cache'),
  10087. you have to clear the target cache in order to find new targets.
  10088. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10089. prefix argument (`C-u C-u C-u C-c C-w')."
  10090. (interactive "P")
  10091. (if (member goto '(0 (64)))
  10092. (org-refile-cache-clear)
  10093. (let* ((actionmsg (or msg "Refile"))
  10094. (cbuf (current-buffer))
  10095. (regionp (org-region-active-p))
  10096. (region-start (and regionp (region-beginning)))
  10097. (region-end (and regionp (region-end)))
  10098. (region-length (and regionp (- region-end region-start)))
  10099. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10100. pos it nbuf file re level reversed)
  10101. (setq last-command nil)
  10102. (when regionp
  10103. (goto-char region-start)
  10104. (or (bolp) (goto-char (point-at-bol)))
  10105. (setq region-start (point))
  10106. (unless (or (org-kill-is-subtree-p
  10107. (buffer-substring region-start region-end))
  10108. (prog1 org-refile-active-region-within-subtree
  10109. (org-toggle-heading)))
  10110. (error "The region is not a (sequence of) subtree(s)")))
  10111. (if (equal goto '(16))
  10112. (org-refile-goto-last-stored)
  10113. (when (or
  10114. (and (equal goto 2)
  10115. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10116. (prog1
  10117. (setq it (list (or org-clock-heading "running clock")
  10118. (buffer-file-name
  10119. (marker-buffer org-clock-hd-marker))
  10120. ""
  10121. (marker-position org-clock-hd-marker)))
  10122. (setq goto nil)))
  10123. (setq it (or rfloc
  10124. (let (heading-text)
  10125. (save-excursion
  10126. (unless goto
  10127. (org-back-to-heading t)
  10128. (setq heading-text
  10129. (nth 4 (org-heading-components))))
  10130. (org-refile-get-location
  10131. (cond (goto "Goto")
  10132. (regionp (concat actionmsg " region to"))
  10133. (t (concat actionmsg " subtree \""
  10134. heading-text "\" to")))
  10135. default-buffer
  10136. (and (not (equal '(4) goto))
  10137. org-refile-allow-creating-parent-nodes)
  10138. goto))))))
  10139. (setq file (nth 1 it)
  10140. re (nth 2 it)
  10141. pos (nth 3 it))
  10142. (if (and (not goto)
  10143. pos
  10144. (equal (buffer-file-name) file)
  10145. (if regionp
  10146. (and (>= pos region-start)
  10147. (<= pos region-end))
  10148. (and (>= pos (point))
  10149. (< pos (save-excursion
  10150. (org-end-of-subtree t t))))))
  10151. (error "Cannot refile to position inside the tree or region"))
  10152. (setq nbuf (or (find-buffer-visiting file)
  10153. (find-file-noselect file)))
  10154. (if goto
  10155. (progn
  10156. (org-pop-to-buffer-same-window nbuf)
  10157. (goto-char pos)
  10158. (org-show-context 'org-goto))
  10159. (if regionp
  10160. (progn
  10161. (org-kill-new (buffer-substring region-start region-end))
  10162. (org-save-markers-in-region region-start region-end))
  10163. (org-copy-subtree 1 nil t))
  10164. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10165. (find-file-noselect file)))
  10166. (setq reversed (org-notes-order-reversed-p))
  10167. (save-excursion
  10168. (save-restriction
  10169. (widen)
  10170. (if pos
  10171. (progn
  10172. (goto-char pos)
  10173. (looking-at org-outline-regexp)
  10174. (setq level (org-get-valid-level (funcall outline-level) 1))
  10175. (goto-char
  10176. (if reversed
  10177. (or (outline-next-heading) (point-max))
  10178. (or (save-excursion (org-get-next-sibling))
  10179. (org-end-of-subtree t t)
  10180. (point-max)))))
  10181. (setq level 1)
  10182. (if (not reversed)
  10183. (goto-char (point-max))
  10184. (goto-char (point-min))
  10185. (or (outline-next-heading) (goto-char (point-max)))))
  10186. (if (not (bolp)) (newline))
  10187. (org-paste-subtree level)
  10188. (when org-log-refile
  10189. (org-add-log-setup 'refile nil nil 'findpos
  10190. org-log-refile)
  10191. (unless (eq org-log-refile 'note)
  10192. (save-excursion (org-add-log-note))))
  10193. (and org-auto-align-tags
  10194. (let ((org-loop-over-headlines-in-active-region nil))
  10195. (org-set-tags nil t)))
  10196. (with-demoted-errors
  10197. (bookmark-set "org-refile-last-stored"))
  10198. ;; If we are refiling for capture, make sure that the
  10199. ;; last-capture pointers point here
  10200. (when (org-bound-and-true-p org-refile-for-capture)
  10201. (with-demoted-errors
  10202. (bookmark-set "org-capture-last-stored-marker"))
  10203. (move-marker org-capture-last-stored-marker (point)))
  10204. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10205. (run-hooks 'org-after-refile-insert-hook))))
  10206. (unless org-refile-keep
  10207. (if regionp
  10208. (delete-region (point) (+ (point) region-length))
  10209. (org-cut-subtree)))
  10210. (when (featurep 'org-inlinetask)
  10211. (org-inlinetask-remove-END-maybe))
  10212. (setq org-markers-to-move nil)
  10213. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10214. (defun org-refile-goto-last-stored ()
  10215. "Go to the location where the last refile was stored."
  10216. (interactive)
  10217. (bookmark-jump "org-refile-last-stored")
  10218. (message "This is the location of the last refile"))
  10219. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10220. no-exclude)
  10221. "Prompt the user for a refile location, using PROMPT.
  10222. PROMPT should not be suffixed with a colon and a space, because
  10223. this function appends the default value from
  10224. `org-refile-history' automatically, if that is not empty.
  10225. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10226. this is used for the GOTO interface."
  10227. (let ((org-refile-targets org-refile-targets)
  10228. (org-refile-use-outline-path org-refile-use-outline-path)
  10229. excluded-entries)
  10230. (when (and (derived-mode-p 'org-mode)
  10231. (not org-refile-use-cache)
  10232. (not no-exclude))
  10233. (org-map-tree
  10234. (lambda()
  10235. (setq excluded-entries
  10236. (append excluded-entries (list (org-get-heading t t)))))))
  10237. (setq org-refile-target-table
  10238. (org-refile-get-targets default-buffer excluded-entries)))
  10239. (unless org-refile-target-table
  10240. (error "No refile targets"))
  10241. (let* ((cbuf (current-buffer))
  10242. (partial-completion-mode nil)
  10243. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10244. (cfunc (if (and org-refile-use-outline-path
  10245. org-outline-path-complete-in-steps)
  10246. 'org-olpath-completing-read
  10247. 'org-icompleting-read))
  10248. (extra (if org-refile-use-outline-path "/" ""))
  10249. (cbnex (concat (buffer-name) extra))
  10250. (filename (and cfn (expand-file-name cfn)))
  10251. (tbl (mapcar
  10252. (lambda (x)
  10253. (if (and (not (member org-refile-use-outline-path
  10254. '(file full-file-path)))
  10255. (not (equal filename (nth 1 x))))
  10256. (cons (concat (car x) extra " ("
  10257. (file-name-nondirectory (nth 1 x)) ")")
  10258. (cdr x))
  10259. (cons (concat (car x) extra) (cdr x))))
  10260. org-refile-target-table))
  10261. (completion-ignore-case t)
  10262. cdef
  10263. (prompt (concat prompt
  10264. (or (and (car org-refile-history)
  10265. (concat " (default " (car org-refile-history) ")"))
  10266. (and (assoc cbnex tbl) (setq cdef cbnex)
  10267. (concat " (default " cbnex ")"))) ": "))
  10268. pa answ parent-target child parent old-hist)
  10269. (setq old-hist org-refile-history)
  10270. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10271. nil 'org-refile-history (or cdef (car org-refile-history))))
  10272. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10273. (org-refile-check-position pa)
  10274. (if pa
  10275. (progn
  10276. (when (or (not org-refile-history)
  10277. (not (eq old-hist org-refile-history))
  10278. (not (equal (car pa) (car org-refile-history))))
  10279. (setq org-refile-history
  10280. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10281. org-refile-history
  10282. (cdr org-refile-history))))
  10283. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10284. (pop org-refile-history)))
  10285. pa)
  10286. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10287. (progn
  10288. (setq parent (match-string 1 answ)
  10289. child (match-string 2 answ))
  10290. (setq parent-target (or (assoc parent tbl)
  10291. (assoc (concat parent "/") tbl)))
  10292. (when (and parent-target
  10293. (or (eq new-nodes t)
  10294. (and (eq new-nodes 'confirm)
  10295. (y-or-n-p (format "Create new node \"%s\"? "
  10296. child)))))
  10297. (org-refile-new-child parent-target child)))
  10298. (error "Invalid target location")))))
  10299. (declare-function org-string-nw-p "org-macs" (s))
  10300. (defun org-refile-check-position (refile-pointer)
  10301. "Check if the refile pointer matches the headline to which it points."
  10302. (let* ((file (nth 1 refile-pointer))
  10303. (re (nth 2 refile-pointer))
  10304. (pos (nth 3 refile-pointer))
  10305. buffer)
  10306. (if (and (not (markerp pos)) (not file))
  10307. (error "Please save the buffer to a file before refiling")
  10308. (when (org-string-nw-p re)
  10309. (setq buffer (if (markerp pos)
  10310. (marker-buffer pos)
  10311. (or (find-buffer-visiting file)
  10312. (find-file-noselect file))))
  10313. (with-current-buffer buffer
  10314. (save-excursion
  10315. (save-restriction
  10316. (widen)
  10317. (goto-char pos)
  10318. (beginning-of-line 1)
  10319. (unless (org-looking-at-p re)
  10320. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10321. (defun org-refile-new-child (parent-target child)
  10322. "Use refile target PARENT-TARGET to add new CHILD below it."
  10323. (unless parent-target
  10324. (error "Cannot find parent for new node"))
  10325. (let ((file (nth 1 parent-target))
  10326. (pos (nth 3 parent-target))
  10327. level)
  10328. (with-current-buffer (or (find-buffer-visiting file)
  10329. (find-file-noselect file))
  10330. (save-excursion
  10331. (save-restriction
  10332. (widen)
  10333. (if pos
  10334. (goto-char pos)
  10335. (goto-char (point-max))
  10336. (if (not (bolp)) (newline)))
  10337. (when (looking-at org-outline-regexp)
  10338. (setq level (funcall outline-level))
  10339. (org-end-of-subtree t t))
  10340. (org-back-over-empty-lines)
  10341. (insert "\n" (make-string
  10342. (if pos (org-get-valid-level level 1) 1) ?*)
  10343. " " child "\n")
  10344. (beginning-of-line 0)
  10345. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10346. (defun org-olpath-completing-read (prompt collection &rest args)
  10347. "Read an outline path like a file name."
  10348. (let ((thetable collection)
  10349. (org-completion-use-ido nil) ; does not work with ido.
  10350. (org-completion-use-iswitchb nil)) ; or iswitchb
  10351. (apply
  10352. 'org-icompleting-read prompt
  10353. (lambda (string predicate &optional flag)
  10354. (let (rtn r f (l (length string)))
  10355. (cond
  10356. ((eq flag nil)
  10357. ;; try completion
  10358. (try-completion string thetable))
  10359. ((eq flag t)
  10360. ;; all-completions
  10361. (setq rtn (all-completions string thetable predicate))
  10362. (mapcar
  10363. (lambda (x)
  10364. (setq r (substring x l))
  10365. (if (string-match " ([^)]*)$" x)
  10366. (setq f (match-string 0 x))
  10367. (setq f ""))
  10368. (if (string-match "/" r)
  10369. (concat string (substring r 0 (match-end 0)) f)
  10370. x))
  10371. rtn))
  10372. ((eq flag 'lambda)
  10373. ;; exact match?
  10374. (assoc string thetable)))))
  10375. args)))
  10376. ;;;; Dynamic blocks
  10377. (defun org-find-dblock (name)
  10378. "Find the first dynamic block with name NAME in the buffer.
  10379. If not found, stay at current position and return nil."
  10380. (let ((case-fold-search t) pos)
  10381. (save-excursion
  10382. (goto-char (point-min))
  10383. (setq pos (and (re-search-forward
  10384. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10385. (match-beginning 0))))
  10386. (if pos (goto-char pos))
  10387. pos))
  10388. (defconst org-dblock-start-re
  10389. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10390. "Matches the start line of a dynamic block, with parameters.")
  10391. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10392. "Matches the end of a dynamic block.")
  10393. (defun org-create-dblock (plist)
  10394. "Create a dynamic block section, with parameters taken from PLIST.
  10395. PLIST must contain a :name entry which is used as name of the block."
  10396. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10397. (end-of-line 1)
  10398. (newline))
  10399. (let ((col (current-column))
  10400. (name (plist-get plist :name)))
  10401. (insert "#+BEGIN: " name)
  10402. (while plist
  10403. (if (eq (car plist) :name)
  10404. (setq plist (cddr plist))
  10405. (insert " " (prin1-to-string (pop plist)))))
  10406. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10407. (beginning-of-line -2)))
  10408. (defun org-prepare-dblock ()
  10409. "Prepare dynamic block for refresh.
  10410. This empties the block, puts the cursor at the insert position and returns
  10411. the property list including an extra property :name with the block name."
  10412. (unless (looking-at org-dblock-start-re)
  10413. (error "Not at a dynamic block"))
  10414. (let* ((begdel (1+ (match-end 0)))
  10415. (name (org-no-properties (match-string 1)))
  10416. (params (append (list :name name)
  10417. (read (concat "(" (match-string 3) ")")))))
  10418. (save-excursion
  10419. (beginning-of-line 1)
  10420. (skip-chars-forward " \t")
  10421. (setq params (plist-put params :indentation-column (current-column))))
  10422. (unless (re-search-forward org-dblock-end-re nil t)
  10423. (error "Dynamic block not terminated"))
  10424. (setq params
  10425. (append params
  10426. (list :content (buffer-substring
  10427. begdel (match-beginning 0)))))
  10428. (delete-region begdel (match-beginning 0))
  10429. (goto-char begdel)
  10430. (open-line 1)
  10431. params))
  10432. (defun org-map-dblocks (&optional command)
  10433. "Apply COMMAND to all dynamic blocks in the current buffer.
  10434. If COMMAND is not given, use `org-update-dblock'."
  10435. (let ((cmd (or command 'org-update-dblock)))
  10436. (save-excursion
  10437. (goto-char (point-min))
  10438. (while (re-search-forward org-dblock-start-re nil t)
  10439. (goto-char (match-beginning 0))
  10440. (save-excursion
  10441. (condition-case nil
  10442. (funcall cmd)
  10443. (error (message "Error during update of dynamic block"))))
  10444. (unless (re-search-forward org-dblock-end-re nil t)
  10445. (error "Dynamic block not terminated"))))))
  10446. (defun org-dblock-update (&optional arg)
  10447. "User command for updating dynamic blocks.
  10448. Update the dynamic block at point. With prefix ARG, update all dynamic
  10449. blocks in the buffer."
  10450. (interactive "P")
  10451. (if arg
  10452. (org-update-all-dblocks)
  10453. (or (looking-at org-dblock-start-re)
  10454. (org-beginning-of-dblock))
  10455. (org-update-dblock)))
  10456. (defun org-update-dblock ()
  10457. "Update the dynamic block at point.
  10458. This means to empty the block, parse for parameters and then call
  10459. the correct writing function."
  10460. (interactive)
  10461. (save-window-excursion
  10462. (let* ((pos (point))
  10463. (line (org-current-line))
  10464. (params (org-prepare-dblock))
  10465. (name (plist-get params :name))
  10466. (indent (plist-get params :indentation-column))
  10467. (cmd (intern (concat "org-dblock-write:" name))))
  10468. (message "Updating dynamic block `%s' at line %d..." name line)
  10469. (funcall cmd params)
  10470. (message "Updating dynamic block `%s' at line %d...done" name line)
  10471. (goto-char pos)
  10472. (when (and indent (> indent 0))
  10473. (setq indent (make-string indent ?\ ))
  10474. (save-excursion
  10475. (org-beginning-of-dblock)
  10476. (forward-line 1)
  10477. (while (not (looking-at org-dblock-end-re))
  10478. (insert indent)
  10479. (beginning-of-line 2))
  10480. (when (looking-at org-dblock-end-re)
  10481. (and (looking-at "[ \t]+")
  10482. (replace-match ""))
  10483. (insert indent)))))))
  10484. (defun org-beginning-of-dblock ()
  10485. "Find the beginning of the dynamic block at point.
  10486. Error if there is no such block at point."
  10487. (let ((pos (point))
  10488. beg)
  10489. (end-of-line 1)
  10490. (if (and (re-search-backward org-dblock-start-re nil t)
  10491. (setq beg (match-beginning 0))
  10492. (re-search-forward org-dblock-end-re nil t)
  10493. (> (match-end 0) pos))
  10494. (goto-char beg)
  10495. (goto-char pos)
  10496. (error "Not in a dynamic block"))))
  10497. (defun org-update-all-dblocks ()
  10498. "Update all dynamic blocks in the buffer.
  10499. This function can be used in a hook."
  10500. (interactive)
  10501. (when (derived-mode-p 'org-mode)
  10502. (org-map-dblocks 'org-update-dblock)))
  10503. ;;;; Completion
  10504. (defun org-get-export-keywords ()
  10505. "Return a list of all currently understood export keywords.
  10506. Export keywords include options, block names, attributes and
  10507. keywords relative to each registered export back-end."
  10508. (delq nil
  10509. (let (keywords)
  10510. (mapc
  10511. (lambda (back-end)
  10512. (let ((props (cdr back-end)))
  10513. ;; Back-end name (for keywords, like #+LATEX:)
  10514. (push (upcase (symbol-name (car back-end))) keywords)
  10515. ;; Back-end options.
  10516. (mapc (lambda (option) (push (cadr option) keywords))
  10517. (plist-get (cdr back-end) :options-alist))))
  10518. (org-bound-and-true-p org-export-registered-backends))
  10519. keywords)))
  10520. (defconst org-options-keywords
  10521. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  10522. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10523. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  10524. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10525. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:" "INFOJS_OPT:"))
  10526. (defcustom org-structure-template-alist
  10527. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10528. "<src lang=\"?\">\n\n</src>")
  10529. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10530. "<example>\n?\n</example>")
  10531. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10532. "<quote>\n?\n</quote>")
  10533. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10534. "<verse>\n?\n</verse>")
  10535. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10536. "<verbatim>\n?\n</verbatim>")
  10537. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10538. "<center>\n?\n</center>")
  10539. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10540. "<literal style=\"latex\">\n?\n</literal>")
  10541. ("L" "#+LaTeX: "
  10542. "<literal style=\"latex\">?</literal>")
  10543. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10544. "<literal style=\"html\">\n?\n</literal>")
  10545. ("H" "#+HTML: "
  10546. "<literal style=\"html\">?</literal>")
  10547. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10548. ("A" "#+ASCII: ")
  10549. ("i" "#+INDEX: ?"
  10550. "#+INDEX: ?")
  10551. ("I" "#+INCLUDE: %file ?"
  10552. "<include file=%file markup=\"?\">"))
  10553. "Structure completion elements.
  10554. This is a list of abbreviation keys and values. The value gets inserted
  10555. if you type `<' followed by the key and then press the completion key,
  10556. usually `M-TAB'. %file will be replaced by a file name after prompting
  10557. for the file using completion. The cursor will be placed at the position
  10558. of the `?` in the template.
  10559. There are two templates for each key, the first uses the original Org syntax,
  10560. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10561. the default when the /org-mtags.el/ module has been loaded. See also the
  10562. variable `org-mtags-prefer-muse-templates'."
  10563. :group 'org-completion
  10564. :type '(repeat
  10565. (string :tag "Key")
  10566. (string :tag "Template")
  10567. (string :tag "Muse Template")))
  10568. (defun org-try-structure-completion ()
  10569. "Try to complete a structure template before point.
  10570. This looks for strings like \"<e\" on an otherwise empty line and
  10571. expands them."
  10572. (let ((l (buffer-substring (point-at-bol) (point)))
  10573. a)
  10574. (when (and (looking-at "[ \t]*$")
  10575. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10576. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10577. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10578. (match-beginning 1)) a)
  10579. t)))
  10580. (defun org-complete-expand-structure-template (start cell)
  10581. "Expand a structure template."
  10582. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10583. (rpl (nth (if musep 2 1) cell))
  10584. (ind ""))
  10585. (delete-region start (point))
  10586. (when (string-match "\\`#\\+" rpl)
  10587. (cond
  10588. ((bolp))
  10589. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10590. (setq ind (buffer-substring (point-at-bol) (point))))
  10591. (t (newline))))
  10592. (setq start (point))
  10593. (if (string-match "%file" rpl)
  10594. (setq rpl (replace-match
  10595. (concat
  10596. "\""
  10597. (save-match-data
  10598. (abbreviate-file-name (read-file-name "Include file: ")))
  10599. "\"")
  10600. t t rpl)))
  10601. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10602. (concat "\n" ind)))
  10603. (insert rpl)
  10604. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10605. ;;;; TODO, DEADLINE, Comments
  10606. (defun org-toggle-comment ()
  10607. "Change the COMMENT state of an entry."
  10608. (interactive)
  10609. (save-excursion
  10610. (org-back-to-heading)
  10611. (let (case-fold-search)
  10612. (cond
  10613. ((looking-at (format org-heading-keyword-regexp-format
  10614. org-comment-string))
  10615. (goto-char (match-end 1))
  10616. (looking-at (concat " +" org-comment-string))
  10617. (replace-match "" t t)
  10618. (when (eolp) (insert " ")))
  10619. ((looking-at org-outline-regexp)
  10620. (goto-char (match-end 0))
  10621. (insert org-comment-string " "))))))
  10622. (defvar org-last-todo-state-is-todo nil
  10623. "This is non-nil when the last TODO state change led to a TODO state.
  10624. If the last change removed the TODO tag or switched to DONE, then
  10625. this is nil.")
  10626. (defvar org-setting-tags nil) ; dynamically skipped
  10627. (defvar org-todo-setup-filter-hook nil
  10628. "Hook for functions that pre-filter todo specs.
  10629. Each function takes a todo spec and returns either nil or the spec
  10630. transformed into canonical form." )
  10631. (defvar org-todo-get-default-hook nil
  10632. "Hook for functions that get a default item for todo.
  10633. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10634. nil or a string to be used for the todo mark." )
  10635. (defvar org-agenda-headline-snapshot-before-repeat)
  10636. (defun org-current-effective-time ()
  10637. "Return current time adjusted for `org-extend-today-until' variable."
  10638. (let* ((ct (org-current-time))
  10639. (dct (decode-time ct))
  10640. (ct1
  10641. (cond
  10642. (org-use-last-clock-out-time-as-effective-time
  10643. (or (org-clock-get-last-clock-out-time) ct))
  10644. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10645. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10646. (t ct))))
  10647. ct1))
  10648. (defun org-todo-yesterday (&optional arg)
  10649. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10650. (interactive "P")
  10651. (if (eq major-mode 'org-agenda-mode)
  10652. (apply 'org-agenda-todo-yesterday arg)
  10653. (let* ((hour (third (decode-time
  10654. (org-current-time))))
  10655. (org-extend-today-until (1+ hour)))
  10656. (org-todo arg))))
  10657. (defvar org-block-entry-blocking ""
  10658. "First entry preventing the TODO state change.")
  10659. (defun org-todo (&optional arg)
  10660. "Change the TODO state of an item.
  10661. The state of an item is given by a keyword at the start of the heading,
  10662. like
  10663. *** TODO Write paper
  10664. *** DONE Call mom
  10665. The different keywords are specified in the variable `org-todo-keywords'.
  10666. By default the available states are \"TODO\" and \"DONE\".
  10667. So for this example: when the item starts with TODO, it is changed to DONE.
  10668. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10669. DONE are present, add TODO at the beginning of the heading.
  10670. With \\[universal-argument] prefix arg, use completion to determine the new \
  10671. state.
  10672. With numeric prefix arg, switch to that state.
  10673. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10674. keywords (nextset).
  10675. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10676. With a numeric prefix arg of 0, inhibit note taking for the change.
  10677. For calling through lisp, arg is also interpreted in the following way:
  10678. 'none -> empty state
  10679. \"\"(empty string) -> switch to empty state
  10680. 'done -> switch to DONE
  10681. 'nextset -> switch to the next set of keywords
  10682. 'previousset -> switch to the previous set of keywords
  10683. \"WAITING\" -> switch to the specified keyword, but only if it
  10684. really is a member of `org-todo-keywords'."
  10685. (interactive "P")
  10686. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10687. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10688. 'region-start-level 'region))
  10689. org-loop-over-headlines-in-active-region)
  10690. (org-map-entries
  10691. `(org-todo ,arg)
  10692. org-loop-over-headlines-in-active-region
  10693. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10694. (if (equal arg '(16)) (setq arg 'nextset))
  10695. (let ((org-blocker-hook org-blocker-hook)
  10696. commentp
  10697. case-fold-search)
  10698. (when (equal arg '(64))
  10699. (setq arg nil org-blocker-hook nil))
  10700. (when (and org-blocker-hook
  10701. (or org-inhibit-blocking
  10702. (org-entry-get nil "NOBLOCKING")))
  10703. (setq org-blocker-hook nil))
  10704. (save-excursion
  10705. (catch 'exit
  10706. (org-back-to-heading t)
  10707. (when (looking-at (concat "^\\*+ " org-comment-string))
  10708. (org-toggle-comment)
  10709. (setq commentp t))
  10710. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10711. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10712. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10713. (let* ((match-data (match-data))
  10714. (startpos (point-at-bol))
  10715. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10716. (org-log-done org-log-done)
  10717. (org-log-repeat org-log-repeat)
  10718. (org-todo-log-states org-todo-log-states)
  10719. (org-inhibit-logging
  10720. (if (equal arg 0)
  10721. (progn (setq arg nil) 'note) org-inhibit-logging))
  10722. (this (match-string 1))
  10723. (hl-pos (match-beginning 0))
  10724. (head (org-get-todo-sequence-head this))
  10725. (ass (assoc head org-todo-kwd-alist))
  10726. (interpret (nth 1 ass))
  10727. (done-word (nth 3 ass))
  10728. (final-done-word (nth 4 ass))
  10729. (org-last-state (or this ""))
  10730. (completion-ignore-case t)
  10731. (member (member this org-todo-keywords-1))
  10732. (tail (cdr member))
  10733. (org-state (cond
  10734. ((and org-todo-key-trigger
  10735. (or (and (equal arg '(4))
  10736. (eq org-use-fast-todo-selection 'prefix))
  10737. (and (not arg) org-use-fast-todo-selection
  10738. (not (eq org-use-fast-todo-selection
  10739. 'prefix)))))
  10740. ;; Use fast selection
  10741. (org-fast-todo-selection))
  10742. ((and (equal arg '(4))
  10743. (or (not org-use-fast-todo-selection)
  10744. (not org-todo-key-trigger)))
  10745. ;; Read a state with completion
  10746. (org-icompleting-read
  10747. "State: " (mapcar (lambda(x) (list x))
  10748. org-todo-keywords-1)
  10749. nil t))
  10750. ((eq arg 'right)
  10751. (if this
  10752. (if tail (car tail) nil)
  10753. (car org-todo-keywords-1)))
  10754. ((eq arg 'left)
  10755. (if (equal member org-todo-keywords-1)
  10756. nil
  10757. (if this
  10758. (nth (- (length org-todo-keywords-1)
  10759. (length tail) 2)
  10760. org-todo-keywords-1)
  10761. (org-last org-todo-keywords-1))))
  10762. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10763. (setq arg nil))) ; hack to fall back to cycling
  10764. (arg
  10765. ;; user or caller requests a specific state
  10766. (cond
  10767. ((equal arg "") nil)
  10768. ((eq arg 'none) nil)
  10769. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10770. ((eq arg 'nextset)
  10771. (or (car (cdr (member head org-todo-heads)))
  10772. (car org-todo-heads)))
  10773. ((eq arg 'previousset)
  10774. (let ((org-todo-heads (reverse org-todo-heads)))
  10775. (or (car (cdr (member head org-todo-heads)))
  10776. (car org-todo-heads))))
  10777. ((car (member arg org-todo-keywords-1)))
  10778. ((stringp arg)
  10779. (error "State `%s' not valid in this file" arg))
  10780. ((nth (1- (prefix-numeric-value arg))
  10781. org-todo-keywords-1))))
  10782. ((null member) (or head (car org-todo-keywords-1)))
  10783. ((equal this final-done-word) nil) ;; -> make empty
  10784. ((null tail) nil) ;; -> first entry
  10785. ((memq interpret '(type priority))
  10786. (if (eq this-command last-command)
  10787. (car tail)
  10788. (if (> (length tail) 0)
  10789. (or done-word (car org-done-keywords))
  10790. nil)))
  10791. (t
  10792. (car tail))))
  10793. (org-state (or
  10794. (run-hook-with-args-until-success
  10795. 'org-todo-get-default-hook org-state org-last-state)
  10796. org-state))
  10797. (next (if org-state (concat " " org-state " ") " "))
  10798. (change-plist (list :type 'todo-state-change :from this :to org-state
  10799. :position startpos))
  10800. dolog now-done-p)
  10801. (when org-blocker-hook
  10802. (setq org-last-todo-state-is-todo
  10803. (not (member this org-done-keywords)))
  10804. (unless (save-excursion
  10805. (save-match-data
  10806. (org-with-wide-buffer
  10807. (run-hook-with-args-until-failure
  10808. 'org-blocker-hook change-plist))))
  10809. (if (org-called-interactively-p 'interactive)
  10810. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10811. this org-state org-block-entry-blocking)
  10812. ;; fail silently
  10813. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10814. this org-state org-block-entry-blocking)
  10815. (throw 'exit nil))))
  10816. (store-match-data match-data)
  10817. (replace-match next t t)
  10818. (unless (pos-visible-in-window-p hl-pos)
  10819. (message "TODO state changed to %s" (org-trim next)))
  10820. (unless head
  10821. (setq head (org-get-todo-sequence-head org-state)
  10822. ass (assoc head org-todo-kwd-alist)
  10823. interpret (nth 1 ass)
  10824. done-word (nth 3 ass)
  10825. final-done-word (nth 4 ass)))
  10826. (when (memq arg '(nextset previousset))
  10827. (message "Keyword-Set %d/%d: %s"
  10828. (- (length org-todo-sets) -1
  10829. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10830. (length org-todo-sets)
  10831. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10832. (setq org-last-todo-state-is-todo
  10833. (not (member org-state org-done-keywords)))
  10834. (setq now-done-p (and (member org-state org-done-keywords)
  10835. (not (member this org-done-keywords))))
  10836. (and logging (org-local-logging logging))
  10837. (when (and (or org-todo-log-states org-log-done)
  10838. (not (eq org-inhibit-logging t))
  10839. (not (memq arg '(nextset previousset))))
  10840. ;; we need to look at recording a time and note
  10841. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10842. (nth 2 (assoc this org-todo-log-states))))
  10843. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10844. (setq dolog 'time))
  10845. (when (and org-state
  10846. (member org-state org-not-done-keywords)
  10847. (not (member this org-not-done-keywords)))
  10848. ;; This is now a todo state and was not one before
  10849. ;; If there was a CLOSED time stamp, get rid of it.
  10850. (org-add-planning-info nil nil 'closed))
  10851. (when (and now-done-p org-log-done)
  10852. ;; It is now done, and it was not done before
  10853. (org-add-planning-info 'closed (org-current-effective-time))
  10854. (if (and (not dolog) (eq 'note org-log-done))
  10855. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10856. (when (and org-state dolog)
  10857. ;; This is a non-nil state, and we need to log it
  10858. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10859. ;; Fixup tag positioning
  10860. (org-todo-trigger-tag-changes org-state)
  10861. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10862. (when org-provide-todo-statistics
  10863. (org-update-parent-todo-statistics))
  10864. (run-hooks 'org-after-todo-state-change-hook)
  10865. (if (and arg (not (member org-state org-done-keywords)))
  10866. (setq head (org-get-todo-sequence-head org-state)))
  10867. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10868. ;; Do we need to trigger a repeat?
  10869. (when now-done-p
  10870. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10871. ;; This is for the agenda, take a snapshot of the headline.
  10872. (save-match-data
  10873. (setq org-agenda-headline-snapshot-before-repeat
  10874. (org-get-heading))))
  10875. (org-auto-repeat-maybe org-state))
  10876. ;; Fixup cursor location if close to the keyword
  10877. (if (and (outline-on-heading-p)
  10878. (not (bolp))
  10879. (save-excursion (beginning-of-line 1)
  10880. (looking-at org-todo-line-regexp))
  10881. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10882. (progn
  10883. (goto-char (or (match-end 2) (match-end 1)))
  10884. (and (looking-at " ") (just-one-space))))
  10885. (when org-trigger-hook
  10886. (save-excursion
  10887. (run-hook-with-args 'org-trigger-hook change-plist)))
  10888. (when commentp (org-toggle-comment))))))))
  10889. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10890. "Block turning an entry into a TODO, using the hierarchy.
  10891. This checks whether the current task should be blocked from state
  10892. changes. Such blocking occurs when:
  10893. 1. The task has children which are not all in a completed state.
  10894. 2. A task has a parent with the property :ORDERED:, and there
  10895. are siblings prior to the current task with incomplete
  10896. status.
  10897. 3. The parent of the task is blocked because it has siblings that should
  10898. be done first, or is child of a block grandparent TODO entry."
  10899. (if (not org-enforce-todo-dependencies)
  10900. t ; if locally turned off don't block
  10901. (catch 'dont-block
  10902. ;; If this is not a todo state change, or if this entry is already DONE,
  10903. ;; do not block
  10904. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10905. (member (plist-get change-plist :from)
  10906. (cons 'done org-done-keywords))
  10907. (member (plist-get change-plist :to)
  10908. (cons 'todo org-not-done-keywords))
  10909. (not (plist-get change-plist :to)))
  10910. (throw 'dont-block t))
  10911. ;; If this task has children, and any are undone, it's blocked
  10912. (save-excursion
  10913. (org-back-to-heading t)
  10914. (let ((this-level (funcall outline-level)))
  10915. (outline-next-heading)
  10916. (let ((child-level (funcall outline-level)))
  10917. (while (and (not (eobp))
  10918. (> child-level this-level))
  10919. ;; this todo has children, check whether they are all
  10920. ;; completed
  10921. (if (and (not (org-entry-is-done-p))
  10922. (org-entry-is-todo-p))
  10923. (progn (setq org-block-entry-blocking (org-get-heading))
  10924. (throw 'dont-block nil)))
  10925. (outline-next-heading)
  10926. (setq child-level (funcall outline-level))))))
  10927. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10928. ;; any previous siblings are undone, it's blocked
  10929. (save-excursion
  10930. (org-back-to-heading t)
  10931. (let* ((pos (point))
  10932. (parent-pos (and (org-up-heading-safe) (point))))
  10933. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10934. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10935. (forward-line 1)
  10936. (re-search-forward org-not-done-heading-regexp pos t))
  10937. (setq org-block-entry-blocking (match-string 0))
  10938. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10939. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10940. (while t
  10941. (goto-char parent-pos)
  10942. (if (not (looking-at org-not-done-heading-regexp))
  10943. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10944. (setq pos (point))
  10945. (setq parent-pos (and (org-up-heading-safe) (point)))
  10946. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10947. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10948. (forward-line 1)
  10949. (re-search-forward org-not-done-heading-regexp pos t)
  10950. (setq org-block-entry-blocking (org-get-heading)))
  10951. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10952. (defcustom org-track-ordered-property-with-tag nil
  10953. "Should the ORDERED property also be shown as a tag?
  10954. The ORDERED property decides if an entry should require subtasks to be
  10955. completed in sequence. Since a property is not very visible, setting
  10956. this option means that toggling the ORDERED property with the command
  10957. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10958. not relevant for the behavior, but it makes things more visible.
  10959. Note that toggling the tag with tags commands will not change the property
  10960. and therefore not influence behavior!
  10961. This can be t, meaning the tag ORDERED should be used, It can also be a
  10962. string to select a different tag for this task."
  10963. :group 'org-todo
  10964. :type '(choice
  10965. (const :tag "No tracking" nil)
  10966. (const :tag "Track with ORDERED tag" t)
  10967. (string :tag "Use other tag")))
  10968. (defun org-toggle-ordered-property ()
  10969. "Toggle the ORDERED property of the current entry.
  10970. For better visibility, you can track the value of this property with a tag.
  10971. See variable `org-track-ordered-property-with-tag'."
  10972. (interactive)
  10973. (let* ((t1 org-track-ordered-property-with-tag)
  10974. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10975. (save-excursion
  10976. (org-back-to-heading)
  10977. (if (org-entry-get nil "ORDERED")
  10978. (progn
  10979. (org-delete-property "ORDERED" "PROPERTIES")
  10980. (and tag (org-toggle-tag tag 'off))
  10981. (message "Subtasks can be completed in arbitrary order"))
  10982. (org-entry-put nil "ORDERED" "t")
  10983. (and tag (org-toggle-tag tag 'on))
  10984. (message "Subtasks must be completed in sequence")))))
  10985. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10986. (defun org-block-todo-from-checkboxes (change-plist)
  10987. "Block turning an entry into a TODO, using checkboxes.
  10988. This checks whether the current task should be blocked from state
  10989. changes because there are unchecked boxes in this entry."
  10990. (if (not org-enforce-todo-checkbox-dependencies)
  10991. t ; if locally turned off don't block
  10992. (catch 'dont-block
  10993. ;; If this is not a todo state change, or if this entry is already DONE,
  10994. ;; do not block
  10995. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10996. (member (plist-get change-plist :from)
  10997. (cons 'done org-done-keywords))
  10998. (member (plist-get change-plist :to)
  10999. (cons 'todo org-not-done-keywords))
  11000. (not (plist-get change-plist :to)))
  11001. (throw 'dont-block t))
  11002. ;; If this task has checkboxes that are not checked, it's blocked
  11003. (save-excursion
  11004. (org-back-to-heading t)
  11005. (let ((beg (point)) end)
  11006. (outline-next-heading)
  11007. (setq end (point))
  11008. (goto-char beg)
  11009. (if (org-list-search-forward
  11010. (concat (org-item-beginning-re)
  11011. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11012. "\\[[- ]\\]")
  11013. end t)
  11014. (progn
  11015. (if (boundp 'org-blocked-by-checkboxes)
  11016. (setq org-blocked-by-checkboxes t))
  11017. (throw 'dont-block nil)))))
  11018. t))) ; do not block
  11019. (defun org-entry-blocked-p ()
  11020. "Is the current entry blocked?"
  11021. (org-with-buffer-modified-unmodified
  11022. (if (org-entry-get nil "NOBLOCKING")
  11023. nil ;; Never block this entry
  11024. (not
  11025. (run-hook-with-args-until-failure
  11026. 'org-blocker-hook
  11027. (list :type 'todo-state-change
  11028. :position (point)
  11029. :from 'todo
  11030. :to 'done))))))
  11031. (defun org-update-statistics-cookies (all)
  11032. "Update the statistics cookie, either from TODO or from checkboxes.
  11033. This should be called with the cursor in a line with a statistics cookie."
  11034. (interactive "P")
  11035. (if all
  11036. (progn
  11037. (org-update-checkbox-count 'all)
  11038. (org-map-entries 'org-update-parent-todo-statistics))
  11039. (if (not (org-at-heading-p))
  11040. (org-update-checkbox-count)
  11041. (let ((pos (point-marker))
  11042. end l1 l2)
  11043. (ignore-errors (org-back-to-heading t))
  11044. (if (not (org-at-heading-p))
  11045. (org-update-checkbox-count)
  11046. (setq l1 (org-outline-level))
  11047. (setq end (save-excursion
  11048. (outline-next-heading)
  11049. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11050. (point)))
  11051. (if (and (save-excursion
  11052. (re-search-forward
  11053. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11054. (not (save-excursion (re-search-forward
  11055. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11056. (org-update-checkbox-count)
  11057. (if (and l2 (> l2 l1))
  11058. (progn
  11059. (goto-char end)
  11060. (org-update-parent-todo-statistics))
  11061. (goto-char pos)
  11062. (beginning-of-line 1)
  11063. (while (re-search-forward
  11064. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11065. (point-at-eol) t)
  11066. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11067. (goto-char pos)
  11068. (move-marker pos nil)))))
  11069. (defvar org-entry-property-inherited-from) ;; defined below
  11070. (defun org-update-parent-todo-statistics ()
  11071. "Update any statistics cookie in the parent of the current headline.
  11072. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11073. the entire subtree and this will travel up the hierarchy and update
  11074. statistics everywhere."
  11075. (let* ((prop (save-excursion (org-up-heading-safe)
  11076. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11077. (recursive (or (not org-hierarchical-todo-statistics)
  11078. (and prop (string-match "\\<recursive\\>" prop))))
  11079. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11080. 0))
  11081. (first t)
  11082. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11083. level ltoggle l1 new ndel
  11084. (cnt-all 0) (cnt-done 0) is-percent kwd
  11085. checkbox-beg ov ovs ove cookie-present)
  11086. (catch 'exit
  11087. (save-excursion
  11088. (beginning-of-line 1)
  11089. (setq ltoggle (funcall outline-level))
  11090. ;; Three situations are to consider:
  11091. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11092. ;; to the top-level ancestor on the headline;
  11093. ;; 2. If parent has "recursive" property, repeat up to the
  11094. ;; headline setting that property, taking inheritance into
  11095. ;; account;
  11096. ;; 3. Else, move up to direct parent and proceed only once.
  11097. (while (and (setq level (org-up-heading-safe))
  11098. (or recursive first)
  11099. (>= (point) lim))
  11100. (setq first nil cookie-present nil)
  11101. (unless (and level
  11102. (not (string-match
  11103. "\\<checkbox\\>"
  11104. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11105. "")))))
  11106. (throw 'exit nil))
  11107. (while (re-search-forward box-re (point-at-eol) t)
  11108. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11109. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11110. (save-match-data
  11111. (unless (outline-next-heading) (throw 'exit nil))
  11112. (while (and (looking-at org-complex-heading-regexp)
  11113. (> (setq l1 (length (match-string 1))) level))
  11114. (setq kwd (and (or recursive (= l1 ltoggle))
  11115. (match-string 2)))
  11116. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11117. (and (listp org-provide-todo-statistics)
  11118. (or (member kwd org-provide-todo-statistics)
  11119. (member kwd org-done-keywords))))
  11120. (setq cnt-all (1+ cnt-all))
  11121. (if (eq org-provide-todo-statistics t)
  11122. (and kwd (setq cnt-all (1+ cnt-all)))))
  11123. (and (member kwd org-done-keywords)
  11124. (setq cnt-done (1+ cnt-done)))
  11125. (outline-next-heading)))
  11126. (setq new
  11127. (if is-percent
  11128. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11129. (format "[%d/%d]" cnt-done cnt-all))
  11130. ndel (- (match-end 0) checkbox-beg))
  11131. ;; handle overlays when updating cookie from column view
  11132. (when (setq ov (car (overlays-at checkbox-beg)))
  11133. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11134. (delete-overlay ov))
  11135. (goto-char checkbox-beg)
  11136. (insert new)
  11137. (delete-region (point) (+ (point) ndel))
  11138. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11139. (when ov (move-overlay ov ovs ove)))
  11140. (when cookie-present
  11141. (run-hook-with-args 'org-after-todo-statistics-hook
  11142. cnt-done (- cnt-all cnt-done))))))
  11143. (run-hooks 'org-todo-statistics-hook)))
  11144. (defvar org-after-todo-statistics-hook nil
  11145. "Hook that is called after a TODO statistics cookie has been updated.
  11146. Each function is called with two arguments: the number of not-done entries
  11147. and the number of done entries.
  11148. For example, the following function, when added to this hook, will switch
  11149. an entry to DONE when all children are done, and back to TODO when new
  11150. entries are set to a TODO status. Note that this hook is only called
  11151. when there is a statistics cookie in the headline!
  11152. (defun org-summary-todo (n-done n-not-done)
  11153. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11154. (let (org-log-done org-log-states) ; turn off logging
  11155. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11156. ")
  11157. (defvar org-todo-statistics-hook nil
  11158. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11159. This hook runs even if there is no statistics cookie present, in which case
  11160. `org-after-todo-statistics-hook' would not run.")
  11161. (defun org-todo-trigger-tag-changes (state)
  11162. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11163. (let ((l org-todo-state-tags-triggers)
  11164. changes)
  11165. (when (or (not state) (equal state ""))
  11166. (setq changes (append changes (cdr (assoc "" l)))))
  11167. (when (and (stringp state) (> (length state) 0))
  11168. (setq changes (append changes (cdr (assoc state l)))))
  11169. (when (member state org-not-done-keywords)
  11170. (setq changes (append changes (cdr (assoc 'todo l)))))
  11171. (when (member state org-done-keywords)
  11172. (setq changes (append changes (cdr (assoc 'done l)))))
  11173. (dolist (c changes)
  11174. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11175. (defun org-local-logging (value)
  11176. "Get logging settings from a property VALUE."
  11177. (let* (words w a)
  11178. ;; directly set the variables, they are already local.
  11179. (setq org-log-done nil
  11180. org-log-repeat nil
  11181. org-todo-log-states nil)
  11182. (setq words (org-split-string value))
  11183. (while (setq w (pop words))
  11184. (cond
  11185. ((setq a (assoc w org-startup-options))
  11186. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11187. (set (nth 1 a) (nth 2 a))))
  11188. ((setq a (org-extract-log-state-settings w))
  11189. (and (member (car a) org-todo-keywords-1)
  11190. (push a org-todo-log-states)))))))
  11191. (defun org-get-todo-sequence-head (kwd)
  11192. "Return the head of the TODO sequence to which KWD belongs.
  11193. If KWD is not set, check if there is a text property remembering the
  11194. right sequence."
  11195. (let (p)
  11196. (cond
  11197. ((not kwd)
  11198. (or (get-text-property (point-at-bol) 'org-todo-head)
  11199. (progn
  11200. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11201. nil (point-at-eol)))
  11202. (get-text-property p 'org-todo-head))))
  11203. ((not (member kwd org-todo-keywords-1))
  11204. (car org-todo-keywords-1))
  11205. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11206. (defun org-fast-todo-selection ()
  11207. "Fast TODO keyword selection with single keys.
  11208. Returns the new TODO keyword, or nil if no state change should occur."
  11209. (let* ((fulltable org-todo-key-alist)
  11210. (done-keywords org-done-keywords) ;; needed for the faces.
  11211. (maxlen (apply 'max (mapcar
  11212. (lambda (x)
  11213. (if (stringp (car x)) (string-width (car x)) 0))
  11214. fulltable)))
  11215. (expert nil)
  11216. (fwidth (+ maxlen 3 1 3))
  11217. (ncol (/ (- (window-width) 4) fwidth))
  11218. tg cnt e c tbl
  11219. groups ingroup)
  11220. (save-excursion
  11221. (save-window-excursion
  11222. (if expert
  11223. (set-buffer (get-buffer-create " *Org todo*"))
  11224. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11225. (erase-buffer)
  11226. (org-set-local 'org-done-keywords done-keywords)
  11227. (setq tbl fulltable cnt 0)
  11228. (while (setq e (pop tbl))
  11229. (cond
  11230. ((equal e '(:startgroup))
  11231. (push '() groups) (setq ingroup t)
  11232. (when (not (= cnt 0))
  11233. (setq cnt 0)
  11234. (insert "\n"))
  11235. (insert "{ "))
  11236. ((equal e '(:endgroup))
  11237. (setq ingroup nil cnt 0)
  11238. (insert "}\n"))
  11239. ((equal e '(:newline))
  11240. (when (not (= cnt 0))
  11241. (setq cnt 0)
  11242. (insert "\n")
  11243. (setq e (car tbl))
  11244. (while (equal (car tbl) '(:newline))
  11245. (insert "\n")
  11246. (setq tbl (cdr tbl)))))
  11247. (t
  11248. (setq tg (car e) c (cdr e))
  11249. (if ingroup (push tg (car groups)))
  11250. (setq tg (org-add-props tg nil 'face
  11251. (org-get-todo-face tg)))
  11252. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11253. (insert "[" c "] " tg (make-string
  11254. (- fwidth 4 (length tg)) ?\ ))
  11255. (when (= (setq cnt (1+ cnt)) ncol)
  11256. (insert "\n")
  11257. (if ingroup (insert " "))
  11258. (setq cnt 0)))))
  11259. (insert "\n")
  11260. (goto-char (point-min))
  11261. (if (not expert) (org-fit-window-to-buffer))
  11262. (message "[a-z..]:Set [SPC]:clear")
  11263. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11264. (cond
  11265. ((or (= c ?\C-g)
  11266. (and (= c ?q) (not (rassoc c fulltable))))
  11267. (setq quit-flag t))
  11268. ((= c ?\ ) nil)
  11269. ((setq e (rassoc c fulltable) tg (car e))
  11270. tg)
  11271. (t (setq quit-flag t)))))))
  11272. (defun org-entry-is-todo-p ()
  11273. (member (org-get-todo-state) org-not-done-keywords))
  11274. (defun org-entry-is-done-p ()
  11275. (member (org-get-todo-state) org-done-keywords))
  11276. (defun org-get-todo-state ()
  11277. (save-excursion
  11278. (org-back-to-heading t)
  11279. (and (looking-at org-todo-line-regexp)
  11280. (match-end 2)
  11281. (match-string 2))))
  11282. (defun org-at-date-range-p (&optional inactive-ok)
  11283. "Is the cursor inside a date range?"
  11284. (interactive)
  11285. (save-excursion
  11286. (catch 'exit
  11287. (let ((pos (point)))
  11288. (skip-chars-backward "^[<\r\n")
  11289. (skip-chars-backward "<[")
  11290. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11291. (>= (match-end 0) pos)
  11292. (throw 'exit t))
  11293. (skip-chars-backward "^<[\r\n")
  11294. (skip-chars-backward "<[")
  11295. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11296. (>= (match-end 0) pos)
  11297. (throw 'exit t)))
  11298. nil)))
  11299. (defun org-get-repeat (&optional tagline)
  11300. "Check if there is a deadline/schedule with repeater in this entry."
  11301. (save-match-data
  11302. (save-excursion
  11303. (org-back-to-heading t)
  11304. (and (re-search-forward (if tagline
  11305. (concat tagline "\\s-*" org-repeat-re)
  11306. org-repeat-re)
  11307. (org-entry-end-position) t)
  11308. (match-string-no-properties 1)))))
  11309. (defvar org-last-changed-timestamp)
  11310. (defvar org-last-inserted-timestamp)
  11311. (defvar org-log-post-message)
  11312. (defvar org-log-note-purpose)
  11313. (defvar org-log-note-how)
  11314. (defvar org-log-note-extra)
  11315. (defun org-auto-repeat-maybe (done-word)
  11316. "Check if the current headline contains a repeated deadline/schedule.
  11317. If yes, set TODO state back to what it was and change the base date
  11318. of repeating deadline/scheduled time stamps to new date.
  11319. This function is run automatically after each state change to a DONE state."
  11320. ;; last-state is dynamically scoped into this function
  11321. (let* ((repeat (org-get-repeat))
  11322. (aa (assoc org-last-state org-todo-kwd-alist))
  11323. (interpret (nth 1 aa))
  11324. (head (nth 2 aa))
  11325. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11326. (msg "Entry repeats: ")
  11327. (org-log-done nil)
  11328. (org-todo-log-states nil)
  11329. re type n what ts time to-state)
  11330. (when repeat
  11331. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11332. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11333. org-todo-repeat-to-state))
  11334. (unless (and to-state (member to-state org-todo-keywords-1))
  11335. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11336. (org-todo to-state)
  11337. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11338. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11339. (org-time-stamp-format t t))))
  11340. (when org-log-repeat
  11341. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11342. (memq 'org-add-log-note post-command-hook))
  11343. ;; OK, we are already setup for some record
  11344. (if (eq org-log-repeat 'note)
  11345. ;; make sure we take a note, not only a time stamp
  11346. (setq org-log-note-how 'note))
  11347. ;; Set up for taking a record
  11348. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11349. org-last-state
  11350. 'findpos org-log-repeat)))
  11351. (org-back-to-heading t)
  11352. (org-add-planning-info nil nil 'closed)
  11353. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11354. org-deadline-time-regexp "\\)\\|\\("
  11355. org-ts-regexp "\\)"))
  11356. (while (re-search-forward
  11357. re (save-excursion (outline-next-heading) (point)) t)
  11358. (setq type (if (match-end 1) org-scheduled-string
  11359. (if (match-end 3) org-deadline-string "Plain:"))
  11360. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11361. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11362. (setq n (string-to-number (match-string 2 ts))
  11363. what (match-string 3 ts))
  11364. (if (equal what "w") (setq n (* n 7) what "d"))
  11365. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11366. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11367. ;; Preparation, see if we need to modify the start date for the change
  11368. (when (match-end 1)
  11369. (setq time (save-match-data (org-time-string-to-time ts)))
  11370. (cond
  11371. ((equal (match-string 1 ts) ".")
  11372. ;; Shift starting date to today
  11373. (org-timestamp-change
  11374. (- (org-today) (time-to-days time))
  11375. 'day))
  11376. ((equal (match-string 1 ts) "+")
  11377. (let ((nshiftmax 10) (nshift 0))
  11378. (while (or (= nshift 0)
  11379. (<= (time-to-days time)
  11380. (time-to-days (current-time))))
  11381. (when (= (incf nshift) nshiftmax)
  11382. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11383. (error "Abort")))
  11384. (org-timestamp-change n (cdr (assoc what whata)))
  11385. (org-at-timestamp-p t)
  11386. (setq ts (match-string 1))
  11387. (setq time (save-match-data (org-time-string-to-time ts)))))
  11388. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11389. ;; rematch, so that we have everything in place for the real shift
  11390. (org-at-timestamp-p t)
  11391. (setq ts (match-string 1))
  11392. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11393. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11394. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11395. (setq org-log-post-message msg)
  11396. (message "%s" msg))))
  11397. (defun org-show-todo-tree (arg)
  11398. "Make a compact tree which shows all headlines marked with TODO.
  11399. The tree will show the lines where the regexp matches, and all higher
  11400. headlines above the match.
  11401. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11402. With a numeric prefix N, construct a sparse tree for the Nth element
  11403. of `org-todo-keywords-1'."
  11404. (interactive "P")
  11405. (let ((case-fold-search nil)
  11406. (kwd-re
  11407. (cond ((null arg) org-not-done-regexp)
  11408. ((equal arg '(4))
  11409. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11410. (mapcar 'list org-todo-keywords-1))))
  11411. (concat "\\("
  11412. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11413. "\\)\\>")))
  11414. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11415. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11416. org-todo-keywords-1)))
  11417. (t (error "Invalid prefix argument: %s" arg)))))
  11418. (message "%d TODO entries found"
  11419. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11420. (defun org-deadline (&optional arg time)
  11421. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11422. With one universal prefix argument, remove any deadline from the item.
  11423. With two universal prefix arguments, prompt for a warning delay.
  11424. With argument TIME, set the deadline at the corresponding date. TIME
  11425. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11426. (interactive "P")
  11427. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11428. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11429. 'region-start-level 'region))
  11430. org-loop-over-headlines-in-active-region)
  11431. (org-map-entries
  11432. `(org-deadline ',arg ,time)
  11433. org-loop-over-headlines-in-active-region
  11434. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11435. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11436. (repeater (and old-date
  11437. (string-match
  11438. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11439. old-date)
  11440. (match-string 1 old-date))))
  11441. (cond
  11442. ((equal arg '(4))
  11443. (when (and old-date org-log-redeadline)
  11444. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11445. org-log-redeadline))
  11446. (org-remove-timestamp-with-keyword org-deadline-string)
  11447. (message "Item no longer has a deadline."))
  11448. ((equal arg '(16))
  11449. (save-excursion
  11450. (if (re-search-forward
  11451. org-deadline-time-regexp
  11452. (save-excursion (outline-next-heading) (point)) t)
  11453. (let* ((rpl0 (match-string 1))
  11454. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11455. (replace-match
  11456. (concat org-deadline-string
  11457. " <" rpl
  11458. (format " -%dd" (abs
  11459. (- (time-to-days
  11460. (save-match-data
  11461. (org-read-date nil t nil "Warn starting from")))
  11462. (time-to-days nil))))
  11463. ">") t t))
  11464. (user-error "No deadline information to update"))))
  11465. (t
  11466. (org-add-planning-info 'deadline time 'closed)
  11467. (when (and old-date org-log-redeadline
  11468. (not (equal old-date
  11469. (substring org-last-inserted-timestamp 1 -1))))
  11470. (org-add-log-setup 'redeadline nil old-date 'findpos
  11471. org-log-redeadline))
  11472. (when repeater
  11473. (save-excursion
  11474. (org-back-to-heading t)
  11475. (when (re-search-forward (concat org-deadline-string " "
  11476. org-last-inserted-timestamp)
  11477. (save-excursion
  11478. (outline-next-heading) (point)) t)
  11479. (goto-char (1- (match-end 0)))
  11480. (insert " " repeater)
  11481. (setq org-last-inserted-timestamp
  11482. (concat (substring org-last-inserted-timestamp 0 -1)
  11483. " " repeater
  11484. (substring org-last-inserted-timestamp -1))))))
  11485. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11486. (defun org-schedule (&optional arg time)
  11487. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11488. With one universal prefix argument, remove any scheduling date from the item.
  11489. With two universal prefix arguments, prompt for a delay cookie.
  11490. With argument TIME, scheduled at the corresponding date. TIME can
  11491. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11492. (interactive "P")
  11493. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11494. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11495. 'region-start-level 'region))
  11496. org-loop-over-headlines-in-active-region)
  11497. (org-map-entries
  11498. `(org-schedule ',arg ,time)
  11499. org-loop-over-headlines-in-active-region
  11500. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11501. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11502. (repeater (and old-date
  11503. (string-match
  11504. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11505. old-date)
  11506. (match-string 1 old-date))))
  11507. (cond
  11508. ((equal arg '(4))
  11509. (progn
  11510. (when (and old-date org-log-reschedule)
  11511. (org-add-log-setup 'delschedule nil old-date 'findpos
  11512. org-log-reschedule))
  11513. (org-remove-timestamp-with-keyword org-scheduled-string)
  11514. (message "Item is no longer scheduled.")))
  11515. ((equal arg '(16))
  11516. (save-excursion
  11517. (if (re-search-forward
  11518. org-scheduled-time-regexp
  11519. (save-excursion (outline-next-heading) (point)) t)
  11520. (let* ((rpl0 (match-string 1))
  11521. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11522. (replace-match
  11523. (concat org-scheduled-string
  11524. " <" rpl
  11525. (format " -%dd" (abs
  11526. (- (time-to-days
  11527. (save-match-data
  11528. (org-read-date nil t nil "Delay until")))
  11529. (time-to-days nil))))
  11530. ">") t t))
  11531. (user-error "No scheduled information to update"))))
  11532. (t
  11533. (org-add-planning-info 'scheduled time 'closed)
  11534. (when (and old-date org-log-reschedule
  11535. (not (equal old-date
  11536. (substring org-last-inserted-timestamp 1 -1))))
  11537. (org-add-log-setup 'reschedule nil old-date 'findpos
  11538. org-log-reschedule))
  11539. (when repeater
  11540. (save-excursion
  11541. (org-back-to-heading t)
  11542. (when (re-search-forward (concat org-scheduled-string " "
  11543. org-last-inserted-timestamp)
  11544. (save-excursion
  11545. (outline-next-heading) (point)) t)
  11546. (goto-char (1- (match-end 0)))
  11547. (insert " " repeater)
  11548. (setq org-last-inserted-timestamp
  11549. (concat (substring org-last-inserted-timestamp 0 -1)
  11550. " " repeater
  11551. (substring org-last-inserted-timestamp -1))))))
  11552. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11553. (defun org-get-scheduled-time (pom &optional inherit)
  11554. "Get the scheduled time as a time tuple, of a format suitable
  11555. for calling org-schedule with, or if there is no scheduling,
  11556. returns nil."
  11557. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11558. (when time
  11559. (apply 'encode-time (org-parse-time-string time)))))
  11560. (defun org-get-deadline-time (pom &optional inherit)
  11561. "Get the deadline as a time tuple, of a format suitable for
  11562. calling org-deadline with, or if there is no scheduling, returns
  11563. nil."
  11564. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11565. (when time
  11566. (apply 'encode-time (org-parse-time-string time)))))
  11567. (defun org-remove-timestamp-with-keyword (keyword)
  11568. "Remove all time stamps with KEYWORD in the current entry."
  11569. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11570. beg)
  11571. (save-excursion
  11572. (org-back-to-heading t)
  11573. (setq beg (point))
  11574. (outline-next-heading)
  11575. (while (re-search-backward re beg t)
  11576. (replace-match "")
  11577. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11578. (equal (char-before) ?\ ))
  11579. (backward-delete-char 1)
  11580. (if (string-match "^[ \t]*$" (buffer-substring
  11581. (point-at-bol) (point-at-eol)))
  11582. (delete-region (point-at-bol)
  11583. (min (point-max) (1+ (point-at-eol))))))))))
  11584. (defun org-add-planning-info (what &optional time &rest remove)
  11585. "Insert new timestamp with keyword in the line directly after the headline.
  11586. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11587. If non is given, the user is prompted for a date.
  11588. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11589. be removed."
  11590. (interactive)
  11591. (let (org-time-was-given org-end-time-was-given ts
  11592. end default-time default-input)
  11593. (catch 'exit
  11594. (when (and (memq what '(scheduled deadline))
  11595. (or (not time)
  11596. (and (stringp time)
  11597. (string-match "^[-+]+[0-9]" time))))
  11598. ;; Try to get a default date/time from existing timestamp
  11599. (save-excursion
  11600. (org-back-to-heading t)
  11601. (setq end (save-excursion (outline-next-heading) (point)))
  11602. (when (re-search-forward (if (eq what 'scheduled)
  11603. org-scheduled-time-regexp
  11604. org-deadline-time-regexp)
  11605. end t)
  11606. (setq ts (match-string 1)
  11607. default-time
  11608. (apply 'encode-time (org-parse-time-string ts))
  11609. default-input (and ts (org-get-compact-tod ts))))))
  11610. (when what
  11611. (setq time
  11612. (if (stringp time)
  11613. ;; This is a string (relative or absolute), set proper date
  11614. (apply 'encode-time
  11615. (org-read-date-analyze
  11616. time default-time (decode-time default-time)))
  11617. ;; If necessary, get the time from the user
  11618. (or time (org-read-date nil 'to-time nil nil
  11619. default-time default-input)))))
  11620. (when (and org-insert-labeled-timestamps-at-point
  11621. (member what '(scheduled deadline)))
  11622. (insert
  11623. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11624. (org-insert-time-stamp time org-time-was-given
  11625. nil nil nil (list org-end-time-was-given))
  11626. (setq what nil))
  11627. (save-excursion
  11628. (save-restriction
  11629. (let (col list elt ts buffer-invisibility-spec)
  11630. (org-back-to-heading t)
  11631. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11632. (goto-char (match-end 1))
  11633. (setq col (current-column))
  11634. (goto-char (match-end 0))
  11635. (if (eobp) (insert "\n") (forward-char 1))
  11636. (when (and (not what)
  11637. (not (looking-at
  11638. (concat "[ \t]*"
  11639. org-keyword-time-not-clock-regexp))))
  11640. ;; Nothing to add, nothing to remove...... :-)
  11641. (throw 'exit nil))
  11642. (if (and (not (looking-at org-outline-regexp))
  11643. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11644. "[^\r\n]*"))
  11645. (not (equal (match-string 1) org-clock-string)))
  11646. (narrow-to-region (match-beginning 0) (match-end 0))
  11647. (insert-before-markers "\n")
  11648. (backward-char 1)
  11649. (narrow-to-region (point) (point))
  11650. (and org-adapt-indentation (org-indent-to-column col)))
  11651. ;; Check if we have to remove something.
  11652. (setq list (cons what remove))
  11653. (while list
  11654. (setq elt (pop list))
  11655. (when (or (and (eq elt 'scheduled)
  11656. (re-search-forward org-scheduled-time-regexp nil t))
  11657. (and (eq elt 'deadline)
  11658. (re-search-forward org-deadline-time-regexp nil t))
  11659. (and (eq elt 'closed)
  11660. (re-search-forward org-closed-time-regexp nil t)))
  11661. (replace-match "")
  11662. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11663. (and (looking-at "[ \t]+") (replace-match ""))
  11664. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11665. (when what
  11666. (insert
  11667. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11668. (cond ((eq what 'scheduled) org-scheduled-string)
  11669. ((eq what 'deadline) org-deadline-string)
  11670. ((eq what 'closed) org-closed-string))
  11671. " ")
  11672. (setq ts (org-insert-time-stamp
  11673. time
  11674. (or org-time-was-given
  11675. (and (eq what 'closed) org-log-done-with-time))
  11676. (eq what 'closed)
  11677. nil nil (list org-end-time-was-given)))
  11678. (insert
  11679. (if (not (or (bolp) (eq (char-before) ?\ )
  11680. (memq (char-after) '(32 10))
  11681. (eobp))) " " ""))
  11682. (end-of-line 1))
  11683. (goto-char (point-min))
  11684. (widen)
  11685. (if (and (looking-at "[ \t]*\n")
  11686. (equal (char-before) ?\n))
  11687. (delete-region (1- (point)) (point-at-eol)))
  11688. ts))))))
  11689. (defvar org-log-note-marker (make-marker))
  11690. (defvar org-log-note-purpose nil)
  11691. (defvar org-log-note-state nil)
  11692. (defvar org-log-note-previous-state nil)
  11693. (defvar org-log-note-how nil)
  11694. (defvar org-log-note-extra nil)
  11695. (defvar org-log-note-window-configuration nil)
  11696. (defvar org-log-note-return-to (make-marker))
  11697. (defvar org-log-note-effective-time nil
  11698. "Remembered current time so that dynamically scoped
  11699. `org-extend-today-until' affects tha timestamps in state change
  11700. log")
  11701. (defvar org-log-post-message nil
  11702. "Message to be displayed after a log note has been stored.
  11703. The auto-repeater uses this.")
  11704. (defun org-add-note ()
  11705. "Add a note to the current entry.
  11706. This is done in the same way as adding a state change note."
  11707. (interactive)
  11708. (org-add-log-setup 'note nil nil 'findpos nil))
  11709. (defvar org-property-end-re)
  11710. (defun org-add-log-setup (&optional purpose state prev-state
  11711. findpos how extra)
  11712. "Set up the post command hook to take a note.
  11713. If this is about to TODO state change, the new state is expected in STATE.
  11714. When FINDPOS is non-nil, find the correct position for the note in
  11715. the current entry. If not, assume that it can be inserted at point.
  11716. HOW is an indicator what kind of note should be created.
  11717. EXTRA is additional text that will be inserted into the notes buffer."
  11718. (let* ((org-log-into-drawer (org-log-into-drawer))
  11719. (drawer (cond ((stringp org-log-into-drawer)
  11720. org-log-into-drawer)
  11721. (org-log-into-drawer "LOGBOOK"))))
  11722. (save-restriction
  11723. (save-excursion
  11724. (when findpos
  11725. (org-back-to-heading t)
  11726. (narrow-to-region (point) (save-excursion
  11727. (outline-next-heading) (point)))
  11728. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11729. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11730. "[^\r\n]*\\)?"))
  11731. (goto-char (match-end 0))
  11732. (cond
  11733. (drawer
  11734. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11735. nil t)
  11736. (progn
  11737. (goto-char (match-end 0))
  11738. (or org-log-states-order-reversed
  11739. (and (re-search-forward org-property-end-re nil t)
  11740. (goto-char (1- (match-beginning 0))))))
  11741. (insert "\n:" drawer ":\n:END:")
  11742. (beginning-of-line 0)
  11743. (org-indent-line)
  11744. (beginning-of-line 2)
  11745. (org-indent-line)
  11746. (end-of-line 0)))
  11747. ((and org-log-state-notes-insert-after-drawers
  11748. (save-excursion
  11749. (forward-line) (looking-at org-drawer-regexp)))
  11750. (forward-line)
  11751. (while (looking-at org-drawer-regexp)
  11752. (goto-char (match-end 0))
  11753. (re-search-forward org-property-end-re (point-max) t)
  11754. (forward-line))
  11755. (forward-line -1)))
  11756. (unless org-log-states-order-reversed
  11757. (and (= (char-after) ?\n) (forward-char 1))
  11758. (org-skip-over-state-notes)
  11759. (skip-chars-backward " \t\n\r")))
  11760. (move-marker org-log-note-marker (point))
  11761. (setq org-log-note-purpose purpose
  11762. org-log-note-state state
  11763. org-log-note-previous-state prev-state
  11764. org-log-note-how how
  11765. org-log-note-extra extra
  11766. org-log-note-effective-time (org-current-effective-time))
  11767. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11768. (defun org-skip-over-state-notes ()
  11769. "Skip past the list of State notes in an entry."
  11770. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11771. (when (ignore-errors (goto-char (org-in-item-p)))
  11772. (let* ((struct (org-list-struct))
  11773. (prevs (org-list-prevs-alist struct)))
  11774. (while (looking-at "[ \t]*- State")
  11775. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11776. (org-list-get-item-end (point) struct)))))))
  11777. (defun org-add-log-note (&optional purpose)
  11778. "Pop up a window for taking a note, and add this note later at point."
  11779. (remove-hook 'post-command-hook 'org-add-log-note)
  11780. (setq org-log-note-window-configuration (current-window-configuration))
  11781. (delete-other-windows)
  11782. (move-marker org-log-note-return-to (point))
  11783. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11784. (goto-char org-log-note-marker)
  11785. (org-switch-to-buffer-other-window "*Org Note*")
  11786. (erase-buffer)
  11787. (if (memq org-log-note-how '(time state))
  11788. (let (current-prefix-arg) (org-store-log-note))
  11789. (let ((org-inhibit-startup t)) (org-mode))
  11790. (insert (format "# Insert note for %s.
  11791. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11792. (cond
  11793. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11794. ((eq org-log-note-purpose 'done) "closed todo item")
  11795. ((eq org-log-note-purpose 'state)
  11796. (format "state change from \"%s\" to \"%s\""
  11797. (or org-log-note-previous-state "")
  11798. (or org-log-note-state "")))
  11799. ((eq org-log-note-purpose 'reschedule)
  11800. "rescheduling")
  11801. ((eq org-log-note-purpose 'delschedule)
  11802. "no longer scheduled")
  11803. ((eq org-log-note-purpose 'redeadline)
  11804. "changing deadline")
  11805. ((eq org-log-note-purpose 'deldeadline)
  11806. "removing deadline")
  11807. ((eq org-log-note-purpose 'refile)
  11808. "refiling")
  11809. ((eq org-log-note-purpose 'note)
  11810. "this entry")
  11811. (t (error "This should not happen")))))
  11812. (if org-log-note-extra (insert org-log-note-extra))
  11813. (org-set-local 'org-finish-function 'org-store-log-note)
  11814. (run-hooks 'org-log-buffer-setup-hook)))
  11815. (defvar org-note-abort nil) ; dynamically scoped
  11816. (defun org-store-log-note ()
  11817. "Finish taking a log note, and insert it to where it belongs."
  11818. (let ((txt (buffer-string)))
  11819. (kill-buffer (current-buffer))
  11820. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11821. lines ind bul)
  11822. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11823. (setq txt (replace-match "" t t txt)))
  11824. (if (string-match "\\s-+\\'" txt)
  11825. (setq txt (replace-match "" t t txt)))
  11826. (setq lines (org-split-string txt "\n"))
  11827. (when (and note (string-match "\\S-" note))
  11828. (setq note
  11829. (org-replace-escapes
  11830. note
  11831. (list (cons "%u" (user-login-name))
  11832. (cons "%U" user-full-name)
  11833. (cons "%t" (format-time-string
  11834. (org-time-stamp-format 'long 'inactive)
  11835. org-log-note-effective-time))
  11836. (cons "%T" (format-time-string
  11837. (org-time-stamp-format 'long nil)
  11838. org-log-note-effective-time))
  11839. (cons "%d" (format-time-string
  11840. (org-time-stamp-format nil 'inactive)
  11841. org-log-note-effective-time))
  11842. (cons "%D" (format-time-string
  11843. (org-time-stamp-format nil nil)
  11844. org-log-note-effective-time))
  11845. (cons "%s" (if org-log-note-state
  11846. (concat "\"" org-log-note-state "\"")
  11847. ""))
  11848. (cons "%S" (if org-log-note-previous-state
  11849. (concat "\"" org-log-note-previous-state "\"")
  11850. "\"\"")))))
  11851. (if lines (setq note (concat note " \\\\")))
  11852. (push note lines))
  11853. (when (or current-prefix-arg org-note-abort)
  11854. (when org-log-into-drawer
  11855. (org-remove-empty-drawer-at
  11856. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11857. org-log-note-marker))
  11858. (setq lines nil))
  11859. (when lines
  11860. (with-current-buffer (marker-buffer org-log-note-marker)
  11861. (save-excursion
  11862. (goto-char org-log-note-marker)
  11863. (move-marker org-log-note-marker nil)
  11864. (end-of-line 1)
  11865. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11866. (setq ind (save-excursion
  11867. (if (ignore-errors (goto-char (org-in-item-p)))
  11868. (let ((struct (org-list-struct)))
  11869. (org-list-get-ind
  11870. (org-list-get-top-point struct) struct))
  11871. (skip-chars-backward " \r\t\n")
  11872. (cond
  11873. ((and (org-at-heading-p)
  11874. org-adapt-indentation)
  11875. (1+ (org-current-level)))
  11876. ((org-at-heading-p) 0)
  11877. (t (org-get-indentation))))))
  11878. (setq bul (org-list-bullet-string "-"))
  11879. (org-indent-line-to ind)
  11880. (insert bul (pop lines))
  11881. (let ((ind-body (+ (length bul) ind)))
  11882. (while lines
  11883. (insert "\n")
  11884. (org-indent-line-to ind-body)
  11885. (insert (pop lines))))
  11886. (message "Note stored")
  11887. (org-back-to-heading t)
  11888. (org-cycle-hide-drawers 'children))))))
  11889. (set-window-configuration org-log-note-window-configuration)
  11890. (with-current-buffer (marker-buffer org-log-note-return-to)
  11891. (goto-char org-log-note-return-to))
  11892. (move-marker org-log-note-return-to nil)
  11893. (and org-log-post-message (message "%s" org-log-post-message)))
  11894. (defun org-remove-empty-drawer-at (drawer pos)
  11895. "Remove an empty drawer DRAWER at position POS.
  11896. POS may also be a marker."
  11897. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11898. (save-excursion
  11899. (save-restriction
  11900. (widen)
  11901. (goto-char pos)
  11902. (if (org-in-regexp
  11903. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11904. (replace-match ""))))))
  11905. (defvar org-ts-type nil)
  11906. (defun org-sparse-tree (&optional arg type)
  11907. "Create a sparse tree, prompt for the details.
  11908. This command can create sparse trees. You first need to select the type
  11909. of match used to create the tree:
  11910. t Show all TODO entries.
  11911. T Show entries with a specific TODO keyword.
  11912. m Show entries selected by a tags/property match.
  11913. p Enter a property name and its value (both with completion on existing
  11914. names/values) and show entries with that property.
  11915. r Show entries matching a regular expression (`/' can be used as well).
  11916. b Show deadlines and scheduled items before a date.
  11917. a Show deadlines and scheduled items after a date.
  11918. d Show deadlines due within `org-deadline-warning-days'.
  11919. D Show deadlines and scheduled items between a date range."
  11920. (interactive "P")
  11921. (let (ans kwd value ts-type)
  11922. (setq type (or type org-sparse-tree-default-date-type))
  11923. (setq org-ts-type type)
  11924. (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"
  11925. (cond ((eq type 'all) "all timestamps")
  11926. ((eq type 'scheduled) "only scheduled")
  11927. ((eq type 'deadline) "only deadline")
  11928. ((eq type 'active) "only active timestamps")
  11929. ((eq type 'inactive) "only inactive timestamps")
  11930. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11931. (t "scheduled/deadline")))
  11932. (setq ans (read-char-exclusive))
  11933. (cond
  11934. ((equal ans ?c)
  11935. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11936. ((equal ans ?d)
  11937. (call-interactively 'org-check-deadlines))
  11938. ((equal ans ?b)
  11939. (call-interactively 'org-check-before-date))
  11940. ((equal ans ?a)
  11941. (call-interactively 'org-check-after-date))
  11942. ((equal ans ?D)
  11943. (call-interactively 'org-check-dates-range))
  11944. ((equal ans ?t)
  11945. (call-interactively 'org-show-todo-tree))
  11946. ((equal ans ?T)
  11947. (org-show-todo-tree '(4)))
  11948. ((member ans '(?T ?m))
  11949. (call-interactively 'org-match-sparse-tree))
  11950. ((member ans '(?p ?P))
  11951. (setq kwd (org-icompleting-read "Property: "
  11952. (mapcar 'list (org-buffer-property-keys))))
  11953. (setq value (org-icompleting-read "Value: "
  11954. (mapcar 'list (org-property-values kwd))))
  11955. (unless (string-match "\\`{.*}\\'" value)
  11956. (setq value (concat "\"" value "\"")))
  11957. (org-match-sparse-tree arg (concat kwd "=" value)))
  11958. ((member ans '(?r ?R ?/))
  11959. (call-interactively 'org-occur))
  11960. (t (error "No such sparse tree command \"%c\"" ans)))))
  11961. (defvar org-occur-highlights nil
  11962. "List of overlays used for occur matches.")
  11963. (make-variable-buffer-local 'org-occur-highlights)
  11964. (defvar org-occur-parameters nil
  11965. "Parameters of the active org-occur calls.
  11966. This is a list, each call to org-occur pushes as cons cell,
  11967. containing the regular expression and the callback, onto the list.
  11968. The list can contain several entries if `org-occur' has been called
  11969. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11970. will only contain one set of parameters. When the highlights are
  11971. removed (for example with `C-c C-c', or with the next edit (depending
  11972. on `org-remove-highlights-with-change'), this variable is emptied
  11973. as well.")
  11974. (make-variable-buffer-local 'org-occur-parameters)
  11975. (defun org-occur (regexp &optional keep-previous callback)
  11976. "Make a compact tree which shows all matches of REGEXP.
  11977. The tree will show the lines where the regexp matches, and all higher
  11978. headlines above the match. It will also show the heading after the match,
  11979. to make sure editing the matching entry is easy.
  11980. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11981. call to `org-occur' will be kept, to allow stacking of calls to this
  11982. command.
  11983. If CALLBACK is non-nil, it is a function which is called to confirm
  11984. that the match should indeed be shown."
  11985. (interactive "sRegexp: \nP")
  11986. (when (equal regexp "")
  11987. (error "Regexp cannot be empty"))
  11988. (unless keep-previous
  11989. (org-remove-occur-highlights nil nil t))
  11990. (push (cons regexp callback) org-occur-parameters)
  11991. (let ((cnt 0))
  11992. (save-excursion
  11993. (goto-char (point-min))
  11994. (if (or (not keep-previous) ; do not want to keep
  11995. (not org-occur-highlights)) ; no previous matches
  11996. ;; hide everything
  11997. (org-overview))
  11998. (while (re-search-forward regexp nil t)
  11999. (when (or (not callback)
  12000. (save-match-data (funcall callback)))
  12001. (setq cnt (1+ cnt))
  12002. (when org-highlight-sparse-tree-matches
  12003. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12004. (org-show-context 'occur-tree))))
  12005. (when org-remove-highlights-with-change
  12006. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12007. nil 'local))
  12008. (unless org-sparse-tree-open-archived-trees
  12009. (org-hide-archived-subtrees (point-min) (point-max)))
  12010. (run-hooks 'org-occur-hook)
  12011. (if (org-called-interactively-p 'interactive)
  12012. (message "%d match(es) for regexp %s" cnt regexp))
  12013. cnt))
  12014. (defun org-occur-next-match (&optional n reset)
  12015. "Function for `next-error-function' to find sparse tree matches.
  12016. N is the number of matches to move, when negative move backwards.
  12017. RESET is entirely ignored - this function always goes back to the
  12018. starting point when no match is found."
  12019. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12020. (search-func (if (< n 0)
  12021. 'previous-single-char-property-change
  12022. 'next-single-char-property-change))
  12023. (n (abs n))
  12024. (pos (point))
  12025. p1)
  12026. (catch 'exit
  12027. (while (setq p1 (funcall search-func (point) 'org-type))
  12028. (when (equal p1 limit)
  12029. (goto-char pos)
  12030. (error "No more matches"))
  12031. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12032. (setq n (1- n))
  12033. (when (= n 0)
  12034. (goto-char p1)
  12035. (throw 'exit (point))))
  12036. (goto-char p1))
  12037. (goto-char p1)
  12038. (error "No more matches"))))
  12039. (defun org-show-context (&optional key)
  12040. "Make sure point and context are visible.
  12041. How much context is shown depends upon the variables
  12042. `org-show-hierarchy-above', `org-show-following-heading',
  12043. `org-show-entry-below' and `org-show-siblings'."
  12044. (let ((heading-p (org-at-heading-p t))
  12045. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  12046. (following-p (org-get-alist-option org-show-following-heading key))
  12047. (entry-p (org-get-alist-option org-show-entry-below key))
  12048. (siblings-p (org-get-alist-option org-show-siblings key)))
  12049. (catch 'exit
  12050. ;; Show heading or entry text
  12051. (if (and heading-p (not entry-p))
  12052. (org-flag-heading nil) ; only show the heading
  12053. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  12054. (org-show-hidden-entry))) ; show entire entry
  12055. (when following-p
  12056. ;; Show next sibling, or heading below text
  12057. (save-excursion
  12058. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  12059. (org-flag-heading nil))))
  12060. (when siblings-p (org-show-siblings))
  12061. (when hierarchy-p
  12062. ;; show all higher headings, possibly with siblings
  12063. (save-excursion
  12064. (while (and (condition-case nil
  12065. (progn (org-up-heading-all 1) t)
  12066. (error nil))
  12067. (not (bobp)))
  12068. (org-flag-heading nil)
  12069. (when siblings-p (org-show-siblings))))))))
  12070. (defvar org-reveal-start-hook nil
  12071. "Hook run before revealing a location.")
  12072. (defun org-reveal (&optional siblings)
  12073. "Show current entry, hierarchy above it, and the following headline.
  12074. This can be used to show a consistent set of context around locations
  12075. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  12076. not t for the search context.
  12077. With optional argument SIBLINGS, on each level of the hierarchy all
  12078. siblings are shown. This repairs the tree structure to what it would
  12079. look like when opened with hierarchical calls to `org-cycle'.
  12080. With double optional argument \\[universal-argument] \\[universal-argument], \
  12081. go to the parent and show the
  12082. entire tree."
  12083. (interactive "P")
  12084. (run-hooks 'org-reveal-start-hook)
  12085. (let ((org-show-hierarchy-above t)
  12086. (org-show-following-heading t)
  12087. (org-show-siblings (if siblings t org-show-siblings)))
  12088. (org-show-context nil))
  12089. (when (equal siblings '(16))
  12090. (save-excursion
  12091. (when (org-up-heading-safe)
  12092. (org-show-subtree)
  12093. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  12094. (defun org-highlight-new-match (beg end)
  12095. "Highlight from BEG to END and mark the highlight is an occur headline."
  12096. (let ((ov (make-overlay beg end)))
  12097. (overlay-put ov 'face 'secondary-selection)
  12098. (overlay-put ov 'org-type 'org-occur)
  12099. (push ov org-occur-highlights)))
  12100. (defun org-remove-occur-highlights (&optional beg end noremove)
  12101. "Remove the occur highlights from the buffer.
  12102. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12103. from the `before-change-functions' in the current buffer."
  12104. (interactive)
  12105. (unless org-inhibit-highlight-removal
  12106. (mapc 'delete-overlay org-occur-highlights)
  12107. (setq org-occur-highlights nil)
  12108. (setq org-occur-parameters nil)
  12109. (unless noremove
  12110. (remove-hook 'before-change-functions
  12111. 'org-remove-occur-highlights 'local))))
  12112. ;;;; Priorities
  12113. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12114. "Regular expression matching the priority indicator.")
  12115. (defvar org-remove-priority-next-time nil)
  12116. (defun org-priority-up ()
  12117. "Increase the priority of the current item."
  12118. (interactive)
  12119. (org-priority 'up))
  12120. (defun org-priority-down ()
  12121. "Decrease the priority of the current item."
  12122. (interactive)
  12123. (org-priority 'down))
  12124. (defun org-priority (&optional action show)
  12125. "Change the priority of an item.
  12126. ACTION can be `set', `up', `down', or a character."
  12127. (interactive "P")
  12128. (if (equal action '(4))
  12129. (org-show-priority)
  12130. (unless org-enable-priority-commands
  12131. (error "Priority commands are disabled"))
  12132. (setq action (or action 'set))
  12133. (let (current new news have remove)
  12134. (save-excursion
  12135. (org-back-to-heading t)
  12136. (if (looking-at org-priority-regexp)
  12137. (setq current (string-to-char (match-string 2))
  12138. have t))
  12139. (cond
  12140. ((eq action 'remove)
  12141. (setq remove t new ?\ ))
  12142. ((or (eq action 'set)
  12143. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12144. (if (not (eq action 'set))
  12145. (setq new action)
  12146. (message "Priority %c-%c, SPC to remove: "
  12147. org-highest-priority org-lowest-priority)
  12148. (save-match-data
  12149. (setq new (read-char-exclusive))))
  12150. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12151. (= (upcase org-lowest-priority) org-lowest-priority))
  12152. (setq new (upcase new)))
  12153. (cond ((equal new ?\ ) (setq remove t))
  12154. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12155. (error "Priority must be between `%c' and `%c'"
  12156. org-highest-priority org-lowest-priority))))
  12157. ((eq action 'up)
  12158. (setq new (if have
  12159. (1- current) ; normal cycling
  12160. ;; last priority was empty
  12161. (if (eq last-command this-command)
  12162. org-lowest-priority ; wrap around empty to lowest
  12163. ;; default
  12164. (if org-priority-start-cycle-with-default
  12165. org-default-priority
  12166. (1- org-default-priority))))))
  12167. ((eq action 'down)
  12168. (setq new (if have
  12169. (1+ current) ; normal cycling
  12170. ;; last priority was empty
  12171. (if (eq last-command this-command)
  12172. org-highest-priority ; wrap around empty to highest
  12173. ;; default
  12174. (if org-priority-start-cycle-with-default
  12175. org-default-priority
  12176. (1+ org-default-priority))))))
  12177. (t (error "Invalid action")))
  12178. (if (or (< (upcase new) org-highest-priority)
  12179. (> (upcase new) org-lowest-priority))
  12180. (if (and (memq action '(up down))
  12181. (not have) (not (eq last-command this-command)))
  12182. ;; `new' is from default priority
  12183. (error
  12184. "The default can not be set, see `org-default-priority' why")
  12185. ;; normal cycling: `new' is beyond highest/lowest priority
  12186. ;; and is wrapped around to the empty priority
  12187. (setq remove t)))
  12188. (setq news (format "%c" new))
  12189. (if have
  12190. (if remove
  12191. (replace-match "" t t nil 1)
  12192. (replace-match news t t nil 2))
  12193. (if remove
  12194. (error "No priority cookie found in line")
  12195. (let ((case-fold-search nil))
  12196. (looking-at org-todo-line-regexp))
  12197. (if (match-end 2)
  12198. (progn
  12199. (goto-char (match-end 2))
  12200. (insert " [#" news "]"))
  12201. (goto-char (match-beginning 3))
  12202. (insert "[#" news "] "))))
  12203. (org-preserve-lc (org-set-tags nil 'align)))
  12204. (if remove
  12205. (message "Priority removed")
  12206. (message "Priority of current item set to %s" news)))))
  12207. (defun org-show-priority ()
  12208. "Show the priority of the current item.
  12209. This priority is composed of the main priority given with the [#A] cookies,
  12210. and by additional input from the age of a schedules or deadline entry."
  12211. (interactive)
  12212. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12213. (org-get-at-bol 'priority)
  12214. (save-excursion
  12215. (save-match-data
  12216. (beginning-of-line)
  12217. (and (looking-at org-heading-regexp)
  12218. (org-get-priority (match-string 0))))))))
  12219. (message "Priority is %d" (if pri pri -1000))))
  12220. (defun org-get-priority (s)
  12221. "Find priority cookie and return priority."
  12222. (save-match-data
  12223. (if (functionp org-get-priority-function)
  12224. (funcall org-get-priority-function)
  12225. (if (not (string-match org-priority-regexp s))
  12226. (* 1000 (- org-lowest-priority org-default-priority))
  12227. (* 1000 (- org-lowest-priority
  12228. (string-to-char (match-string 2 s))))))))
  12229. ;;;; Tags
  12230. (defvar org-agenda-archives-mode)
  12231. (defvar org-map-continue-from nil
  12232. "Position from where mapping should continue.
  12233. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12234. (defvar org-scanner-tags nil
  12235. "The current tag list while the tags scanner is running.")
  12236. (defvar org-trust-scanner-tags nil
  12237. "Should `org-get-tags-at' use the tags for the scanner.
  12238. This is for internal dynamical scoping only.
  12239. When this is non-nil, the function `org-get-tags-at' will return the value
  12240. of `org-scanner-tags' instead of building the list by itself. This
  12241. can lead to large speed-ups when the tags scanner is used in a file with
  12242. many entries, and when the list of tags is retrieved, for example to
  12243. obtain a list of properties. Building the tags list for each entry in such
  12244. a file becomes an N^2 operation - but with this variable set, it scales
  12245. as N.")
  12246. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12247. "Scan headline tags with inheritance and produce output ACTION.
  12248. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12249. or `agenda' to produce an entry list for an agenda view. It can also be
  12250. a Lisp form or a function that should be called at each matched headline, in
  12251. this case the return value is a list of all return values from these calls.
  12252. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12253. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12254. only lines with a not-done TODO keyword are included in the output.
  12255. This should be the same variable that was scoped into
  12256. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12257. START-LEVEL can be a string with asterisks, reducing the scope to
  12258. headlines matching this string."
  12259. (require 'org-agenda)
  12260. (let* ((re (concat "^"
  12261. (if start-level
  12262. ;; Get the correct level to match
  12263. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12264. org-outline-regexp)
  12265. " *\\(\\<\\("
  12266. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12267. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12268. (props (list 'face 'default
  12269. 'done-face 'org-agenda-done
  12270. 'undone-face 'default
  12271. 'mouse-face 'highlight
  12272. 'org-not-done-regexp org-not-done-regexp
  12273. 'org-todo-regexp org-todo-regexp
  12274. 'org-complex-heading-regexp org-complex-heading-regexp
  12275. 'help-echo
  12276. (format "mouse-2 or RET jump to org file %s"
  12277. (abbreviate-file-name
  12278. (or (buffer-file-name (buffer-base-buffer))
  12279. (buffer-name (buffer-base-buffer)))))))
  12280. (case-fold-search nil)
  12281. (org-map-continue-from nil)
  12282. lspos tags tags-list
  12283. (tags-alist (list (cons 0 org-file-tags)))
  12284. (llast 0) rtn rtn1 level category i txt
  12285. todo marker entry priority)
  12286. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12287. (setq action (list 'lambda nil action)))
  12288. (save-excursion
  12289. (goto-char (point-min))
  12290. (when (eq action 'sparse-tree)
  12291. (org-overview)
  12292. (org-remove-occur-highlights))
  12293. (while (re-search-forward re nil t)
  12294. (setq org-map-continue-from nil)
  12295. (catch :skip
  12296. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12297. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12298. (goto-char (setq lspos (match-beginning 0)))
  12299. (setq level (org-reduced-level (org-outline-level))
  12300. category (org-get-category))
  12301. (setq i llast llast level)
  12302. ;; remove tag lists from same and sublevels
  12303. (while (>= i level)
  12304. (when (setq entry (assoc i tags-alist))
  12305. (setq tags-alist (delete entry tags-alist)))
  12306. (setq i (1- i)))
  12307. ;; add the next tags
  12308. (when tags
  12309. (setq tags (org-split-string tags ":")
  12310. tags-alist
  12311. (cons (cons level tags) tags-alist)))
  12312. ;; compile tags for current headline
  12313. (setq tags-list
  12314. (if org-use-tag-inheritance
  12315. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12316. tags)
  12317. org-scanner-tags tags-list)
  12318. (when org-use-tag-inheritance
  12319. (setcdr (car tags-alist)
  12320. (mapcar (lambda (x)
  12321. (setq x (copy-sequence x))
  12322. (org-add-prop-inherited x))
  12323. (cdar tags-alist))))
  12324. (when (and tags org-use-tag-inheritance
  12325. (or (not (eq t org-use-tag-inheritance))
  12326. org-tags-exclude-from-inheritance))
  12327. ;; selective inheritance, remove uninherited ones
  12328. (setcdr (car tags-alist)
  12329. (org-remove-uninherited-tags (cdar tags-alist))))
  12330. (when (and
  12331. ;; eval matcher only when the todo condition is OK
  12332. (and (or (not todo-only) (member todo org-not-done-keywords))
  12333. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12334. (eval matcher)))
  12335. ;; Call the skipper, but return t if it does not skip,
  12336. ;; so that the `and' form continues evaluating
  12337. (progn
  12338. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12339. t)
  12340. ;; Check if timestamps are deselecting this entry
  12341. (or (not todo-only)
  12342. (and (member todo org-not-done-keywords)
  12343. (or (not org-agenda-tags-todo-honor-ignore-options)
  12344. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12345. ;; select this headline
  12346. (cond
  12347. ((eq action 'sparse-tree)
  12348. (and org-highlight-sparse-tree-matches
  12349. (org-get-heading) (match-end 0)
  12350. (org-highlight-new-match
  12351. (match-beginning 1) (match-end 1)))
  12352. (org-show-context 'tags-tree))
  12353. ((eq action 'agenda)
  12354. (setq txt (org-agenda-format-item
  12355. ""
  12356. (concat
  12357. (if (eq org-tags-match-list-sublevels 'indented)
  12358. (make-string (1- level) ?.) "")
  12359. (org-get-heading))
  12360. level category
  12361. tags-list)
  12362. priority (org-get-priority txt))
  12363. (goto-char lspos)
  12364. (setq marker (org-agenda-new-marker))
  12365. (org-add-props txt props
  12366. 'org-marker marker 'org-hd-marker marker 'org-category category
  12367. 'todo-state todo
  12368. 'priority priority 'type "tagsmatch")
  12369. (push txt rtn))
  12370. ((functionp action)
  12371. (setq org-map-continue-from nil)
  12372. (save-excursion
  12373. (setq rtn1 (funcall action))
  12374. (push rtn1 rtn)))
  12375. (t (error "Invalid action")))
  12376. ;; if we are to skip sublevels, jump to end of subtree
  12377. (unless org-tags-match-list-sublevels
  12378. (org-end-of-subtree t)
  12379. (backward-char 1))))
  12380. ;; Get the correct position from where to continue
  12381. (if org-map-continue-from
  12382. (goto-char org-map-continue-from)
  12383. (and (= (point) lspos) (end-of-line 1)))))
  12384. (when (and (eq action 'sparse-tree)
  12385. (not org-sparse-tree-open-archived-trees))
  12386. (org-hide-archived-subtrees (point-min) (point-max)))
  12387. (nreverse rtn)))
  12388. (defun org-remove-uninherited-tags (tags)
  12389. "Remove all tags that are not inherited from the list TAGS."
  12390. (cond
  12391. ((eq org-use-tag-inheritance t)
  12392. (if org-tags-exclude-from-inheritance
  12393. (org-delete-all org-tags-exclude-from-inheritance tags)
  12394. tags))
  12395. ((not org-use-tag-inheritance) nil)
  12396. ((stringp org-use-tag-inheritance)
  12397. (delq nil (mapcar
  12398. (lambda (x)
  12399. (if (and (string-match org-use-tag-inheritance x)
  12400. (not (member x org-tags-exclude-from-inheritance)))
  12401. x nil))
  12402. tags)))
  12403. ((listp org-use-tag-inheritance)
  12404. (delq nil (mapcar
  12405. (lambda (x)
  12406. (if (member x org-use-tag-inheritance) x nil))
  12407. tags)))))
  12408. (defun org-match-sparse-tree (&optional todo-only match)
  12409. "Create a sparse tree according to tags string MATCH.
  12410. MATCH can contain positive and negative selection of tags, like
  12411. \"+WORK+URGENT-WITHBOSS\".
  12412. If optional argument TODO-ONLY is non-nil, only select lines that are
  12413. also TODO lines."
  12414. (interactive "P")
  12415. (org-agenda-prepare-buffers (list (current-buffer)))
  12416. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12417. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12418. (defvar org-cached-props nil)
  12419. (defun org-cached-entry-get (pom property)
  12420. (if (or (eq t org-use-property-inheritance)
  12421. (and (stringp org-use-property-inheritance)
  12422. (string-match org-use-property-inheritance property))
  12423. (and (listp org-use-property-inheritance)
  12424. (member property org-use-property-inheritance)))
  12425. ;; Caching is not possible, check it directly
  12426. (org-entry-get pom property 'inherit)
  12427. ;; Get all properties, so that we can do complicated checks easily
  12428. (cdr (assoc property (or org-cached-props
  12429. (setq org-cached-props
  12430. (org-entry-properties pom)))))))
  12431. (defun org-global-tags-completion-table (&optional files)
  12432. "Return the list of all tags in all agenda buffer/files.
  12433. Optional FILES argument is a list of files which can be used
  12434. instead of the agenda files."
  12435. (save-excursion
  12436. (org-uniquify
  12437. (delq nil
  12438. (apply 'append
  12439. (mapcar
  12440. (lambda (file)
  12441. (set-buffer (find-file-noselect file))
  12442. (append (org-get-buffer-tags)
  12443. (mapcar (lambda (x) (if (stringp (car-safe x))
  12444. (list (car-safe x)) nil))
  12445. org-tag-alist)))
  12446. (if (and files (car files))
  12447. files
  12448. (org-agenda-files))))))))
  12449. (defun org-make-tags-matcher (match)
  12450. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12451. The variable `todo-only' is scoped dynamically into this function.
  12452. It will be set to t if the matcher restricts matching to TODO entries,
  12453. otherwise will not be touched.
  12454. Returns a cons of the selection string MATCH and the constructed
  12455. lisp form implementing the matcher. The matcher is to be evaluated
  12456. at an Org entry, with point on the headline, and returns t if the
  12457. entry matches the selection string MATCH. The returned lisp form
  12458. references two variables with information about the entry, which
  12459. must be bound around the form's evaluation: todo, the TODO keyword
  12460. at the entry (or nil of none); and tags-list, the list of all tags
  12461. at the entry including inherited ones. Additionally, the category
  12462. of the entry (if any) must be specified as the text property
  12463. 'org-category on the headline.
  12464. See also `org-scan-tags'.
  12465. "
  12466. (declare (special todo-only))
  12467. (unless (boundp 'todo-only)
  12468. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12469. (unless match
  12470. ;; Get a new match request, with completion
  12471. (let ((org-last-tags-completion-table
  12472. (org-global-tags-completion-table)))
  12473. (setq match (org-completing-read-no-i
  12474. "Match: " 'org-tags-completion-function nil nil nil
  12475. 'org-tags-history))))
  12476. ;; Parse the string and create a lisp form
  12477. (let ((match0 match)
  12478. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12479. minus tag mm
  12480. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12481. orterms term orlist re-p str-p level-p level-op time-p
  12482. prop-p pn pv po gv rest)
  12483. (if (string-match "/+" match)
  12484. ;; match contains also a todo-matching request
  12485. (progn
  12486. (setq tagsmatch (substring match 0 (match-beginning 0))
  12487. todomatch (substring match (match-end 0)))
  12488. (if (string-match "^!" todomatch)
  12489. (setq todo-only t todomatch (substring todomatch 1)))
  12490. (if (string-match "^\\s-*$" todomatch)
  12491. (setq todomatch nil)))
  12492. ;; only matching tags
  12493. (setq tagsmatch match todomatch nil))
  12494. ;; Make the tags matcher
  12495. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12496. (setq tagsmatcher t)
  12497. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12498. (while (setq term (pop orterms))
  12499. (while (and (equal (substring term -1) "\\") orterms)
  12500. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12501. (while (string-match re term)
  12502. (setq rest (substring term (match-end 0))
  12503. minus (and (match-end 1)
  12504. (equal (match-string 1 term) "-"))
  12505. tag (save-match-data (replace-regexp-in-string
  12506. "\\\\-" "-"
  12507. (match-string 2 term)))
  12508. re-p (equal (string-to-char tag) ?{)
  12509. level-p (match-end 4)
  12510. prop-p (match-end 5)
  12511. mm (cond
  12512. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12513. (level-p
  12514. (setq level-op (org-op-to-function (match-string 3 term)))
  12515. `(,level-op level ,(string-to-number
  12516. (match-string 4 term))))
  12517. (prop-p
  12518. (setq pn (match-string 5 term)
  12519. po (match-string 6 term)
  12520. pv (match-string 7 term)
  12521. re-p (equal (string-to-char pv) ?{)
  12522. str-p (equal (string-to-char pv) ?\")
  12523. time-p (save-match-data
  12524. (string-match "^\"[[<].*[]>]\"$" pv))
  12525. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12526. (if time-p (setq pv (org-matcher-time pv)))
  12527. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12528. (cond
  12529. ((equal pn "CATEGORY")
  12530. (setq gv '(get-text-property (point) 'org-category)))
  12531. ((equal pn "TODO")
  12532. (setq gv 'todo))
  12533. (t
  12534. (setq gv `(org-cached-entry-get nil ,pn))))
  12535. (if re-p
  12536. (if (eq po 'org<>)
  12537. `(not (string-match ,pv (or ,gv "")))
  12538. `(string-match ,pv (or ,gv "")))
  12539. (if str-p
  12540. `(,po (or ,gv "") ,pv)
  12541. `(,po (string-to-number (or ,gv ""))
  12542. ,(string-to-number pv) ))))
  12543. (t `(member ,tag tags-list)))
  12544. mm (if minus (list 'not mm) mm)
  12545. term rest)
  12546. (push mm tagsmatcher))
  12547. (push (if (> (length tagsmatcher) 1)
  12548. (cons 'and tagsmatcher)
  12549. (car tagsmatcher))
  12550. orlist)
  12551. (setq tagsmatcher nil))
  12552. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12553. (setq tagsmatcher
  12554. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12555. ;; Make the todo matcher
  12556. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12557. (setq todomatcher t)
  12558. (setq orterms (org-split-string todomatch "|") orlist nil)
  12559. (while (setq term (pop orterms))
  12560. (while (string-match re term)
  12561. (setq minus (and (match-end 1)
  12562. (equal (match-string 1 term) "-"))
  12563. kwd (match-string 2 term)
  12564. re-p (equal (string-to-char kwd) ?{)
  12565. term (substring term (match-end 0))
  12566. mm (if re-p
  12567. `(string-match ,(substring kwd 1 -1) todo)
  12568. (list 'equal 'todo kwd))
  12569. mm (if minus (list 'not mm) mm))
  12570. (push mm todomatcher))
  12571. (push (if (> (length todomatcher) 1)
  12572. (cons 'and todomatcher)
  12573. (car todomatcher))
  12574. orlist)
  12575. (setq todomatcher nil))
  12576. (setq todomatcher (if (> (length orlist) 1)
  12577. (cons 'or orlist) (car orlist))))
  12578. ;; Return the string and lisp forms of the matcher
  12579. (setq matcher (if todomatcher
  12580. (list 'and tagsmatcher todomatcher)
  12581. tagsmatcher))
  12582. (when todo-only
  12583. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12584. matcher)))
  12585. (cons match0 matcher)))
  12586. (defun org-op-to-function (op &optional stringp)
  12587. "Turn an operator into the appropriate function."
  12588. (setq op
  12589. (cond
  12590. ((equal op "<" ) '(< string< org-time<))
  12591. ((equal op ">" ) '(> org-string> org-time>))
  12592. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12593. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12594. ((member op '("=" "==")) '(= string= org-time=))
  12595. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12596. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12597. (defun org<> (a b) (not (= a b)))
  12598. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12599. (defun org-string>= (a b) (not (string< a b)))
  12600. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12601. (defun org-string<> (a b) (not (string= a b)))
  12602. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12603. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12604. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12605. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12606. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12607. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12608. (defun org-2ft (s)
  12609. "Convert S to a floating point time.
  12610. If S is already a number, just return it. If it is a string, parse
  12611. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12612. (cond
  12613. ((numberp s) s)
  12614. ((stringp s)
  12615. (condition-case nil
  12616. (float-time (apply 'encode-time (org-parse-time-string s)))
  12617. (error 0.)))
  12618. (t 0.)))
  12619. (defun org-time-today ()
  12620. "Time in seconds today at 0:00.
  12621. Returns the float number of seconds since the beginning of the
  12622. epoch to the beginning of today (00:00)."
  12623. (float-time (apply 'encode-time
  12624. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12625. (defun org-matcher-time (s)
  12626. "Interpret a time comparison value."
  12627. (save-match-data
  12628. (cond
  12629. ((string= s "<now>") (float-time))
  12630. ((string= s "<today>") (org-time-today))
  12631. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12632. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12633. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12634. (+ (org-time-today)
  12635. (* (string-to-number (match-string 1 s))
  12636. (cdr (assoc (match-string 2 s)
  12637. '(("d" . 86400.0) ("w" . 604800.0)
  12638. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12639. (t (org-2ft s)))))
  12640. (defun org-match-any-p (re list)
  12641. "Does re match any element of list?"
  12642. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12643. (delq nil list))
  12644. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12645. (defvar org-tags-overlay (make-overlay 1 1))
  12646. (org-detach-overlay org-tags-overlay)
  12647. (defun org-get-local-tags-at (&optional pos)
  12648. "Get a list of tags defined in the current headline."
  12649. (org-get-tags-at pos 'local))
  12650. (defun org-get-local-tags ()
  12651. "Get a list of tags defined in the current headline."
  12652. (org-get-tags-at nil 'local))
  12653. (defun org-get-tags-at (&optional pos local)
  12654. "Get a list of all headline tags applicable at POS.
  12655. POS defaults to point. If tags are inherited, the list contains
  12656. the targets in the same sequence as the headlines appear, i.e.
  12657. the tags of the current headline come last.
  12658. When LOCAL is non-nil, only return tags from the current headline,
  12659. ignore inherited ones."
  12660. (interactive)
  12661. (if (and org-trust-scanner-tags
  12662. (or (not pos) (equal pos (point)))
  12663. (not local))
  12664. org-scanner-tags
  12665. (let (tags ltags lastpos parent)
  12666. (save-excursion
  12667. (save-restriction
  12668. (widen)
  12669. (goto-char (or pos (point)))
  12670. (save-match-data
  12671. (catch 'done
  12672. (condition-case nil
  12673. (progn
  12674. (org-back-to-heading t)
  12675. (while (not (equal lastpos (point)))
  12676. (setq lastpos (point))
  12677. (when (looking-at
  12678. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12679. (setq ltags (org-split-string
  12680. (org-match-string-no-properties 1) ":"))
  12681. (when parent
  12682. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12683. (setq tags (append
  12684. (if parent
  12685. (org-remove-uninherited-tags ltags)
  12686. ltags)
  12687. tags)))
  12688. (or org-use-tag-inheritance (throw 'done t))
  12689. (if local (throw 'done t))
  12690. (or (org-up-heading-safe) (error nil))
  12691. (setq parent t)))
  12692. (error nil)))))
  12693. (if local
  12694. tags
  12695. (reverse (delete-dups
  12696. (reverse (append
  12697. (org-remove-uninherited-tags
  12698. org-file-tags) tags)))))))))
  12699. (defun org-add-prop-inherited (s)
  12700. (add-text-properties 0 (length s) '(inherited t) s)
  12701. s)
  12702. (defun org-toggle-tag (tag &optional onoff)
  12703. "Toggle the tag TAG for the current line.
  12704. If ONOFF is `on' or `off', don't toggle but set to this state."
  12705. (let (res current)
  12706. (save-excursion
  12707. (org-back-to-heading t)
  12708. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12709. (point-at-eol) t)
  12710. (progn
  12711. (setq current (match-string 1))
  12712. (replace-match ""))
  12713. (setq current ""))
  12714. (setq current (nreverse (org-split-string current ":")))
  12715. (cond
  12716. ((eq onoff 'on)
  12717. (setq res t)
  12718. (or (member tag current) (push tag current)))
  12719. ((eq onoff 'off)
  12720. (or (not (member tag current)) (setq current (delete tag current))))
  12721. (t (if (member tag current)
  12722. (setq current (delete tag current))
  12723. (setq res t)
  12724. (push tag current))))
  12725. (end-of-line 1)
  12726. (if current
  12727. (progn
  12728. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12729. (org-set-tags nil t))
  12730. (delete-horizontal-space))
  12731. (run-hooks 'org-after-tags-change-hook))
  12732. res))
  12733. (defun org-align-tags-here (to-col)
  12734. ;; Assumes that this is a headline
  12735. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12736. (beginning-of-line 1)
  12737. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12738. (< pos (match-beginning 2)))
  12739. (progn
  12740. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12741. (goto-char (match-beginning 1))
  12742. (insert " ")
  12743. (delete-region (point) (1+ (match-beginning 2)))
  12744. (setq ncol (max (current-column)
  12745. (1+ col)
  12746. (if (> to-col 0)
  12747. to-col
  12748. (- (abs to-col) tags-l))))
  12749. (setq p (point))
  12750. (insert (make-string (- ncol (current-column)) ?\ ))
  12751. (setq ncol (current-column))
  12752. (when indent-tabs-mode (tabify p (point-at-eol)))
  12753. (org-move-to-column (min ncol col) t))
  12754. (goto-char pos))))
  12755. (defun org-set-tags-command (&optional arg just-align)
  12756. "Call the set-tags command for the current entry."
  12757. (interactive "P")
  12758. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12759. (org-set-tags arg just-align)
  12760. (save-excursion
  12761. (unless (and (org-region-active-p)
  12762. org-loop-over-headlines-in-active-region)
  12763. (org-back-to-heading t))
  12764. (org-set-tags arg just-align))))
  12765. (defun org-set-tags-to (data)
  12766. "Set the tags of the current entry to DATA, replacing the current tags.
  12767. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12768. If DATA is nil or the empty string, any tags will be removed."
  12769. (interactive "sTags: ")
  12770. (setq data
  12771. (cond
  12772. ((eq data nil) "")
  12773. ((equal data "") "")
  12774. ((stringp data)
  12775. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12776. ":"))
  12777. ((listp data)
  12778. (concat ":" (mapconcat 'identity data ":") ":"))))
  12779. (when data
  12780. (save-excursion
  12781. (org-back-to-heading t)
  12782. (when (looking-at org-complex-heading-regexp)
  12783. (if (match-end 5)
  12784. (progn
  12785. (goto-char (match-beginning 5))
  12786. (insert data)
  12787. (delete-region (point) (point-at-eol))
  12788. (org-set-tags nil 'align))
  12789. (goto-char (point-at-eol))
  12790. (insert " " data)
  12791. (org-set-tags nil 'align)))
  12792. (beginning-of-line 1)
  12793. (if (looking-at ".*?\\([ \t]+\\)$")
  12794. (delete-region (match-beginning 1) (match-end 1))))))
  12795. (defun org-align-all-tags ()
  12796. "Align the tags i all headings."
  12797. (interactive)
  12798. (save-excursion
  12799. (or (ignore-errors (org-back-to-heading t))
  12800. (outline-next-heading))
  12801. (if (org-at-heading-p)
  12802. (org-set-tags t)
  12803. (message "No headings"))))
  12804. (defvar org-indent-indentation-per-level)
  12805. (defun org-set-tags (&optional arg just-align)
  12806. "Set the tags for the current headline.
  12807. With prefix ARG, realign all tags in headings in the current buffer."
  12808. (interactive "P")
  12809. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12810. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12811. 'region-start-level 'region))
  12812. org-loop-over-headlines-in-active-region)
  12813. (org-map-entries
  12814. ;; We don't use ARG and JUST-ALIGN here these args are not
  12815. ;; useful when looping over headlines
  12816. `(org-set-tags)
  12817. org-loop-over-headlines-in-active-region
  12818. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12819. (let* ((re org-outline-regexp-bol)
  12820. (current (unless arg (org-get-tags-string)))
  12821. (col (current-column))
  12822. (org-setting-tags t)
  12823. table current-tags inherited-tags ; computed below when needed
  12824. tags p0 c0 c1 rpl di tc level)
  12825. (if arg
  12826. (save-excursion
  12827. (goto-char (point-min))
  12828. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12829. (while (re-search-forward re nil t)
  12830. (org-set-tags nil t)
  12831. (end-of-line 1)))
  12832. (message "All tags realigned to column %d" org-tags-column))
  12833. (if just-align
  12834. (setq tags current)
  12835. ;; Get a new set of tags from the user
  12836. (save-excursion
  12837. (setq table (append org-tag-persistent-alist
  12838. (or org-tag-alist (org-get-buffer-tags))
  12839. (and
  12840. org-complete-tags-always-offer-all-agenda-tags
  12841. (org-global-tags-completion-table
  12842. (org-agenda-files))))
  12843. org-last-tags-completion-table table
  12844. current-tags (org-split-string current ":")
  12845. inherited-tags (nreverse
  12846. (nthcdr (length current-tags)
  12847. (nreverse (org-get-tags-at))))
  12848. tags
  12849. (if (or (eq t org-use-fast-tag-selection)
  12850. (and org-use-fast-tag-selection
  12851. (delq nil (mapcar 'cdr table))))
  12852. (org-fast-tag-selection
  12853. current-tags inherited-tags table
  12854. (if org-fast-tag-selection-include-todo
  12855. org-todo-key-alist))
  12856. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12857. (org-trim
  12858. (org-icompleting-read "Tags: "
  12859. 'org-tags-completion-function
  12860. nil nil current 'org-tags-history))))))
  12861. (while (string-match "[-+&]+" tags)
  12862. ;; No boolean logic, just a list
  12863. (setq tags (replace-match ":" t t tags))))
  12864. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12865. (if org-tags-sort-function
  12866. (setq tags (mapconcat 'identity
  12867. (sort (org-split-string
  12868. tags (org-re "[^[:alnum:]_@#%]+"))
  12869. org-tags-sort-function) ":")))
  12870. (if (string-match "\\`[\t ]*\\'" tags)
  12871. (setq tags "")
  12872. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12873. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12874. ;; Insert new tags at the correct column
  12875. (beginning-of-line 1)
  12876. (setq level (or (and (looking-at org-outline-regexp)
  12877. (- (match-end 0) (point) 1))
  12878. 1))
  12879. (cond
  12880. ((and (equal current "") (equal tags "")))
  12881. ((re-search-forward
  12882. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12883. (point-at-eol) t)
  12884. (if (equal tags "")
  12885. (setq rpl "")
  12886. (goto-char (match-beginning 0))
  12887. (setq c0 (current-column)
  12888. ;; compute offset for the case of org-indent-mode active
  12889. di (if org-indent-mode
  12890. (* (1- org-indent-indentation-per-level) (1- level))
  12891. 0)
  12892. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12893. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12894. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12895. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12896. (replace-match rpl t t)
  12897. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12898. tags)
  12899. (t (error "Tags alignment failed")))
  12900. (org-move-to-column col)
  12901. (unless just-align
  12902. (run-hooks 'org-after-tags-change-hook))))))
  12903. (defun org-change-tag-in-region (beg end tag off)
  12904. "Add or remove TAG for each entry in the region.
  12905. This works in the agenda, and also in an org-mode buffer."
  12906. (interactive
  12907. (list (region-beginning) (region-end)
  12908. (let ((org-last-tags-completion-table
  12909. (if (derived-mode-p 'org-mode)
  12910. (org-get-buffer-tags)
  12911. (org-global-tags-completion-table))))
  12912. (org-icompleting-read
  12913. "Tag: " 'org-tags-completion-function nil nil nil
  12914. 'org-tags-history))
  12915. (progn
  12916. (message "[s]et or [r]emove? ")
  12917. (equal (read-char-exclusive) ?r))))
  12918. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12919. (let ((agendap (equal major-mode 'org-agenda-mode))
  12920. l1 l2 m buf pos newhead (cnt 0))
  12921. (goto-char end)
  12922. (setq l2 (1- (org-current-line)))
  12923. (goto-char beg)
  12924. (setq l1 (org-current-line))
  12925. (loop for l from l1 to l2 do
  12926. (org-goto-line l)
  12927. (setq m (get-text-property (point) 'org-hd-marker))
  12928. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12929. (and agendap m))
  12930. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12931. pos (if agendap m (point)))
  12932. (with-current-buffer buf
  12933. (save-excursion
  12934. (save-restriction
  12935. (goto-char pos)
  12936. (setq cnt (1+ cnt))
  12937. (org-toggle-tag tag (if off 'off 'on))
  12938. (setq newhead (org-get-heading)))))
  12939. (and agendap (org-agenda-change-all-lines newhead m))))
  12940. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12941. (defun org-tags-completion-function (string predicate &optional flag)
  12942. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12943. (confirm (lambda (x) (stringp (car x)))))
  12944. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12945. (setq s1 (match-string 1 string)
  12946. s2 (match-string 2 string))
  12947. (setq s1 "" s2 string))
  12948. (cond
  12949. ((eq flag nil)
  12950. ;; try completion
  12951. (setq rtn (try-completion s2 ctable confirm))
  12952. (if (stringp rtn)
  12953. (setq rtn
  12954. (concat s1 s2 (substring rtn (length s2))
  12955. (if (and org-add-colon-after-tag-completion
  12956. (assoc rtn ctable))
  12957. ":" ""))))
  12958. rtn)
  12959. ((eq flag t)
  12960. ;; all-completions
  12961. (all-completions s2 ctable confirm)
  12962. )
  12963. ((eq flag 'lambda)
  12964. ;; exact match?
  12965. (assoc s2 ctable)))
  12966. ))
  12967. (defun org-fast-tag-insert (kwd tags face &optional end)
  12968. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12969. (insert (format "%-12s" (concat kwd ":"))
  12970. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12971. (or end "")))
  12972. (defun org-fast-tag-show-exit (flag)
  12973. (save-excursion
  12974. (org-goto-line 3)
  12975. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12976. (replace-match ""))
  12977. (when flag
  12978. (end-of-line 1)
  12979. (org-move-to-column (- (window-width) 19) t)
  12980. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12981. (defun org-set-current-tags-overlay (current prefix)
  12982. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12983. (if (featurep 'xemacs)
  12984. (org-overlay-display org-tags-overlay (concat prefix s)
  12985. 'secondary-selection)
  12986. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12987. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12988. (defvar org-last-tag-selection-key nil)
  12989. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12990. "Fast tag selection with single keys.
  12991. CURRENT is the current list of tags in the headline, INHERITED is the
  12992. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12993. possibly with grouping information. TODO-TABLE is a similar table with
  12994. TODO keywords, should these have keys assigned to them.
  12995. If the keys are nil, a-z are automatically assigned.
  12996. Returns the new tags string, or nil to not change the current settings."
  12997. (let* ((fulltable (append table todo-table))
  12998. (maxlen (apply 'max (mapcar
  12999. (lambda (x)
  13000. (if (stringp (car x)) (string-width (car x)) 0))
  13001. fulltable)))
  13002. (buf (current-buffer))
  13003. (expert (eq org-fast-tag-selection-single-key 'expert))
  13004. (buffer-tags nil)
  13005. (fwidth (+ maxlen 3 1 3))
  13006. (ncol (/ (- (window-width) 4) fwidth))
  13007. (i-face 'org-done)
  13008. (c-face 'org-todo)
  13009. tg cnt e c char c1 c2 ntable tbl rtn
  13010. ov-start ov-end ov-prefix
  13011. (exit-after-next org-fast-tag-selection-single-key)
  13012. (done-keywords org-done-keywords)
  13013. groups ingroup)
  13014. (save-excursion
  13015. (beginning-of-line 1)
  13016. (if (looking-at
  13017. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13018. (setq ov-start (match-beginning 1)
  13019. ov-end (match-end 1)
  13020. ov-prefix "")
  13021. (setq ov-start (1- (point-at-eol))
  13022. ov-end (1+ ov-start))
  13023. (skip-chars-forward "^\n\r")
  13024. (setq ov-prefix
  13025. (concat
  13026. (buffer-substring (1- (point)) (point))
  13027. (if (> (current-column) org-tags-column)
  13028. " "
  13029. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13030. (move-overlay org-tags-overlay ov-start ov-end)
  13031. (save-window-excursion
  13032. (if expert
  13033. (set-buffer (get-buffer-create " *Org tags*"))
  13034. (delete-other-windows)
  13035. (split-window-vertically)
  13036. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  13037. (erase-buffer)
  13038. (org-set-local 'org-done-keywords done-keywords)
  13039. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13040. (org-fast-tag-insert "Current" current c-face "\n\n")
  13041. (org-fast-tag-show-exit exit-after-next)
  13042. (org-set-current-tags-overlay current ov-prefix)
  13043. (setq tbl fulltable char ?a cnt 0)
  13044. (while (setq e (pop tbl))
  13045. (cond
  13046. ((equal (car e) :startgroup)
  13047. (push '() groups) (setq ingroup t)
  13048. (when (not (= cnt 0))
  13049. (setq cnt 0)
  13050. (insert "\n"))
  13051. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13052. ((equal (car e) :endgroup)
  13053. (setq ingroup nil cnt 0)
  13054. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13055. ((equal e '(:newline))
  13056. (when (not (= cnt 0))
  13057. (setq cnt 0)
  13058. (insert "\n")
  13059. (setq e (car tbl))
  13060. (while (equal (car tbl) '(:newline))
  13061. (insert "\n")
  13062. (setq tbl (cdr tbl)))))
  13063. (t
  13064. (setq tg (copy-sequence (car e)) c2 nil)
  13065. (if (cdr e)
  13066. (setq c (cdr e))
  13067. ;; automatically assign a character.
  13068. (setq c1 (string-to-char
  13069. (downcase (substring
  13070. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13071. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13072. (while (or (rassoc char ntable) (rassoc char table))
  13073. (setq char (1+ char)))
  13074. (setq c2 c1))
  13075. (setq c (or c2 char)))
  13076. (if ingroup (push tg (car groups)))
  13077. (setq tg (org-add-props tg nil 'face
  13078. (cond
  13079. ((not (assoc tg table))
  13080. (org-get-todo-face tg))
  13081. ((member tg current) c-face)
  13082. ((member tg inherited) i-face))))
  13083. (if (and (= cnt 0) (not ingroup)) (insert " "))
  13084. (insert "[" c "] " tg (make-string
  13085. (- fwidth 4 (length tg)) ?\ ))
  13086. (push (cons tg c) ntable)
  13087. (when (= (setq cnt (1+ cnt)) ncol)
  13088. (insert "\n")
  13089. (if ingroup (insert " "))
  13090. (setq cnt 0)))))
  13091. (setq ntable (nreverse ntable))
  13092. (insert "\n")
  13093. (goto-char (point-min))
  13094. (if (not expert) (org-fit-window-to-buffer))
  13095. (setq rtn
  13096. (catch 'exit
  13097. (while t
  13098. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13099. (if (not groups) "no " "")
  13100. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13101. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13102. (setq org-last-tag-selection-key c)
  13103. (cond
  13104. ((= c ?\r) (throw 'exit t))
  13105. ((= c ?!)
  13106. (setq groups (not groups))
  13107. (goto-char (point-min))
  13108. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13109. ((= c ?\C-c)
  13110. (if (not expert)
  13111. (org-fast-tag-show-exit
  13112. (setq exit-after-next (not exit-after-next)))
  13113. (setq expert nil)
  13114. (delete-other-windows)
  13115. (set-window-buffer (split-window-vertically) " *Org tags*")
  13116. (org-switch-to-buffer-other-window " *Org tags*")
  13117. (org-fit-window-to-buffer)))
  13118. ((or (= c ?\C-g)
  13119. (and (= c ?q) (not (rassoc c ntable))))
  13120. (org-detach-overlay org-tags-overlay)
  13121. (setq quit-flag t))
  13122. ((= c ?\ )
  13123. (setq current nil)
  13124. (if exit-after-next (setq exit-after-next 'now)))
  13125. ((= c ?\t)
  13126. (condition-case nil
  13127. (setq tg (org-icompleting-read
  13128. "Tag: "
  13129. (or buffer-tags
  13130. (with-current-buffer buf
  13131. (org-get-buffer-tags)))))
  13132. (quit (setq tg "")))
  13133. (when (string-match "\\S-" tg)
  13134. (add-to-list 'buffer-tags (list tg))
  13135. (if (member tg current)
  13136. (setq current (delete tg current))
  13137. (push tg current)))
  13138. (if exit-after-next (setq exit-after-next 'now)))
  13139. ((setq e (rassoc c todo-table) tg (car e))
  13140. (with-current-buffer buf
  13141. (save-excursion (org-todo tg)))
  13142. (if exit-after-next (setq exit-after-next 'now)))
  13143. ((setq e (rassoc c ntable) tg (car e))
  13144. (if (member tg current)
  13145. (setq current (delete tg current))
  13146. (loop for g in groups do
  13147. (if (member tg g)
  13148. (mapc (lambda (x)
  13149. (setq current (delete x current)))
  13150. g)))
  13151. (push tg current))
  13152. (if exit-after-next (setq exit-after-next 'now))))
  13153. ;; Create a sorted list
  13154. (setq current
  13155. (sort current
  13156. (lambda (a b)
  13157. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13158. (if (eq exit-after-next 'now) (throw 'exit t))
  13159. (goto-char (point-min))
  13160. (beginning-of-line 2)
  13161. (delete-region (point) (point-at-eol))
  13162. (org-fast-tag-insert "Current" current c-face)
  13163. (org-set-current-tags-overlay current ov-prefix)
  13164. (while (re-search-forward
  13165. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13166. (setq tg (match-string 1))
  13167. (add-text-properties
  13168. (match-beginning 1) (match-end 1)
  13169. (list 'face
  13170. (cond
  13171. ((member tg current) c-face)
  13172. ((member tg inherited) i-face)
  13173. (t (get-text-property (match-beginning 1) 'face))))))
  13174. (goto-char (point-min)))))
  13175. (org-detach-overlay org-tags-overlay)
  13176. (if rtn
  13177. (mapconcat 'identity current ":")
  13178. nil))))
  13179. (defun org-get-tags-string ()
  13180. "Get the TAGS string in the current headline."
  13181. (unless (org-at-heading-p t)
  13182. (error "Not on a heading"))
  13183. (save-excursion
  13184. (beginning-of-line 1)
  13185. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13186. (org-match-string-no-properties 1)
  13187. "")))
  13188. (defun org-get-tags ()
  13189. "Get the list of tags specified in the current headline."
  13190. (org-split-string (org-get-tags-string) ":"))
  13191. (defun org-get-buffer-tags ()
  13192. "Get a table of all tags used in the buffer, for completion."
  13193. (let (tags)
  13194. (save-excursion
  13195. (goto-char (point-min))
  13196. (while (re-search-forward
  13197. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13198. (when (equal (char-after (point-at-bol 0)) ?*)
  13199. (mapc (lambda (x) (add-to-list 'tags x))
  13200. (org-split-string (org-match-string-no-properties 1) ":")))))
  13201. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13202. (mapcar 'list tags)))
  13203. ;;;; The mapping API
  13204. (defun org-map-entries (func &optional match scope &rest skip)
  13205. "Call FUNC at each headline selected by MATCH in SCOPE.
  13206. FUNC is a function or a lisp form. The function will be called without
  13207. arguments, with the cursor positioned at the beginning of the headline.
  13208. The return values of all calls to the function will be collected and
  13209. returned as a list.
  13210. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13211. does not need to preserve point. After evaluation, the cursor will be
  13212. moved to the end of the line (presumably of the headline of the
  13213. processed entry) and search continues from there. Under some
  13214. circumstances, this may not produce the wanted results. For example,
  13215. if you have removed (e.g. archived) the current (sub)tree it could
  13216. mean that the next entry will be skipped entirely. In such cases, you
  13217. can specify the position from where search should continue by making
  13218. FUNC set the variable `org-map-continue-from' to the desired buffer
  13219. position.
  13220. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13221. Only headlines that are matched by this query will be considered during
  13222. the iteration. When MATCH is nil or t, all headlines will be
  13223. visited by the iteration.
  13224. SCOPE determines the scope of this command. It can be any of:
  13225. nil The current buffer, respecting the restriction if any
  13226. tree The subtree started with the entry at point
  13227. region The entries within the active region, if any
  13228. region-start-level
  13229. The entries within the active region, but only those at
  13230. the same level than the first one.
  13231. file The current buffer, without restriction
  13232. file-with-archives
  13233. The current buffer, and any archives associated with it
  13234. agenda All agenda files
  13235. agenda-with-archives
  13236. All agenda files with any archive files associated with them
  13237. \(file1 file2 ...)
  13238. If this is a list, all files in the list will be scanned
  13239. The remaining args are treated as settings for the skipping facilities of
  13240. the scanner. The following items can be given here:
  13241. archive skip trees with the archive tag.
  13242. comment skip trees with the COMMENT keyword
  13243. function or Emacs Lisp form:
  13244. will be used as value for `org-agenda-skip-function', so whenever
  13245. the function returns t, FUNC will not be called for that
  13246. entry and search will continue from the point where the
  13247. function leaves it.
  13248. If your function needs to retrieve the tags including inherited tags
  13249. at the *current* entry, you can use the value of the variable
  13250. `org-scanner-tags' which will be much faster than getting the value
  13251. with `org-get-tags-at'. If your function gets properties with
  13252. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13253. to t around the call to `org-entry-properties' to get the same speedup.
  13254. Note that if your function moves around to retrieve tags and properties at
  13255. a *different* entry, you cannot use these techniques."
  13256. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13257. (not (org-region-active-p)))
  13258. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13259. (org-agenda-skip-archived-trees (memq 'archive skip))
  13260. (org-agenda-skip-comment-trees (memq 'comment skip))
  13261. (org-agenda-skip-function
  13262. (car (org-delete-all '(comment archive) skip)))
  13263. (org-tags-match-list-sublevels t)
  13264. (start-level (eq scope 'region-start-level))
  13265. matcher file res
  13266. org-todo-keywords-for-agenda
  13267. org-done-keywords-for-agenda
  13268. org-todo-keyword-alist-for-agenda
  13269. org-drawers-for-agenda
  13270. org-tag-alist-for-agenda
  13271. todo-only)
  13272. (cond
  13273. ((eq match t) (setq matcher t))
  13274. ((eq match nil) (setq matcher t))
  13275. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13276. (save-excursion
  13277. (save-restriction
  13278. (cond ((eq scope 'tree)
  13279. (org-back-to-heading t)
  13280. (org-narrow-to-subtree)
  13281. (setq scope nil))
  13282. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13283. (org-region-active-p))
  13284. ;; If needed, set start-level to a string like "2"
  13285. (when start-level
  13286. (save-excursion
  13287. (goto-char (region-beginning))
  13288. (unless (org-at-heading-p) (outline-next-heading))
  13289. (setq start-level (org-current-level))))
  13290. (narrow-to-region (region-beginning)
  13291. (save-excursion
  13292. (goto-char (region-end))
  13293. (unless (and (bolp) (org-at-heading-p))
  13294. (outline-next-heading))
  13295. (point)))
  13296. (setq scope nil)))
  13297. (if (not scope)
  13298. (progn
  13299. (org-agenda-prepare-buffers
  13300. (list (buffer-file-name (current-buffer))))
  13301. (setq res (org-scan-tags func matcher todo-only start-level)))
  13302. ;; Get the right scope
  13303. (cond
  13304. ((and scope (listp scope) (symbolp (car scope)))
  13305. (setq scope (eval scope)))
  13306. ((eq scope 'agenda)
  13307. (setq scope (org-agenda-files t)))
  13308. ((eq scope 'agenda-with-archives)
  13309. (setq scope (org-agenda-files t))
  13310. (setq scope (org-add-archive-files scope)))
  13311. ((eq scope 'file)
  13312. (setq scope (list (buffer-file-name))))
  13313. ((eq scope 'file-with-archives)
  13314. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13315. (org-agenda-prepare-buffers scope)
  13316. (while (setq file (pop scope))
  13317. (with-current-buffer (org-find-base-buffer-visiting file)
  13318. (save-excursion
  13319. (save-restriction
  13320. (widen)
  13321. (goto-char (point-min))
  13322. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13323. res)))
  13324. ;;;; Properties
  13325. ;;; Setting and retrieving properties
  13326. (defconst org-special-properties
  13327. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13328. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13329. "The special properties valid in Org-mode.
  13330. These are properties that are not defined in the property drawer,
  13331. but in some other way.")
  13332. (defconst org-default-properties
  13333. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13334. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13335. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13336. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13337. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13338. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13339. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13340. "Some properties that are used by Org-mode for various purposes.
  13341. Being in this list makes sure that they are offered for completion.")
  13342. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13343. "Regular expression matching the first line of a property drawer.")
  13344. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13345. "Regular expression matching the last line of a property drawer.")
  13346. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13347. "Regular expression matching the first line of a property drawer.")
  13348. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13349. "Regular expression matching the first line of a property drawer.")
  13350. (defconst org-property-drawer-re
  13351. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13352. org-property-end-re "\\)\n?")
  13353. "Matches an entire property drawer.")
  13354. (defconst org-clock-drawer-re
  13355. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13356. org-property-end-re "\\)\n?")
  13357. "Matches an entire clock drawer.")
  13358. (defsubst org-re-property (property)
  13359. "Return a regexp matching a PROPERTY line.
  13360. Match group 1 will be set to the value."
  13361. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13362. (defsubst org-re-property-keyword (property)
  13363. "Return a regexp matching a PROPERTY line, possibly with no
  13364. value for the property."
  13365. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13366. (defun org-property-action ()
  13367. "Do an action on properties."
  13368. (interactive)
  13369. (let (c)
  13370. (org-at-property-p)
  13371. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13372. (setq c (read-char-exclusive))
  13373. (cond
  13374. ((equal c ?s)
  13375. (call-interactively 'org-set-property))
  13376. ((equal c ?d)
  13377. (call-interactively 'org-delete-property))
  13378. ((equal c ?D)
  13379. (call-interactively 'org-delete-property-globally))
  13380. ((equal c ?c)
  13381. (call-interactively 'org-compute-property-at-point))
  13382. (t (error "No such property action %c" c)))))
  13383. (defun org-inc-effort ()
  13384. "Increment the value of the effort property in the current entry."
  13385. (interactive)
  13386. (org-set-effort nil t))
  13387. (defun org-set-effort (&optional value increment)
  13388. "Set the effort property of the current entry.
  13389. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13390. 10th allowed value.
  13391. When INCREMENT is non-nil, set the property to the next allowed value."
  13392. (interactive "P")
  13393. (if (equal value 0) (setq value 10))
  13394. (let* ((completion-ignore-case t)
  13395. (prop org-effort-property)
  13396. (cur (org-entry-get nil prop))
  13397. (allowed (org-property-get-allowed-values nil prop 'table))
  13398. (existing (mapcar 'list (org-property-values prop)))
  13399. rpl
  13400. (val (cond
  13401. ((stringp value) value)
  13402. ((and allowed (integerp value))
  13403. (or (car (nth (1- value) allowed))
  13404. (car (org-last allowed))))
  13405. ((and allowed increment)
  13406. (or (caadr (member (list cur) allowed))
  13407. (error "Allowed effort values are not set")))
  13408. (allowed
  13409. (message "Select 1-9,0, [RET%s]: %s"
  13410. (if cur (concat "=" cur) "")
  13411. (mapconcat 'car allowed " "))
  13412. (setq rpl (read-char-exclusive))
  13413. (if (equal rpl ?\r)
  13414. cur
  13415. (setq rpl (- rpl ?0))
  13416. (if (equal rpl 0) (setq rpl 10))
  13417. (if (and (> rpl 0) (<= rpl (length allowed)))
  13418. (car (nth (1- rpl) allowed))
  13419. (org-completing-read "Effort: " allowed nil))))
  13420. (t
  13421. (let (org-completion-use-ido org-completion-use-iswitchb)
  13422. (org-completing-read
  13423. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13424. (concat "[" cur "]") "")
  13425. ": ")
  13426. existing nil nil "" nil cur))))))
  13427. (unless (equal (org-entry-get nil prop) val)
  13428. (org-entry-put nil prop val))
  13429. (save-excursion
  13430. (org-back-to-heading t)
  13431. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13432. (message "%s is now %s" prop val)))
  13433. (defun org-at-property-p ()
  13434. "Is cursor inside a property drawer?"
  13435. (save-excursion
  13436. (beginning-of-line 1)
  13437. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13438. (save-match-data ;; Used by calling procedures
  13439. (let ((p (point))
  13440. (range (unless (org-before-first-heading-p)
  13441. (org-get-property-block))))
  13442. (and range (<= (car range) p) (< p (cdr range))))))))
  13443. (defun org-get-property-block (&optional beg end force)
  13444. "Return the (beg . end) range of the body of the property drawer.
  13445. BEG and END are the beginning and end of the current subtree, or of
  13446. the part before the first headline. If they are not given, they will
  13447. be found. If the drawer does not exist and FORCE is non-nil, create
  13448. the drawer."
  13449. (catch 'exit
  13450. (save-excursion
  13451. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13452. (progn (org-back-to-heading t) (point))))
  13453. (end (or end (and (not (outline-next-heading)) (point-max))
  13454. (point))))
  13455. (goto-char beg)
  13456. (if (re-search-forward org-property-start-re end t)
  13457. (setq beg (1+ (match-end 0)))
  13458. (if force
  13459. (save-excursion
  13460. (org-insert-property-drawer)
  13461. (setq end (progn (outline-next-heading) (point))))
  13462. (throw 'exit nil))
  13463. (goto-char beg)
  13464. (if (re-search-forward org-property-start-re end t)
  13465. (setq beg (1+ (match-end 0)))))
  13466. (if (re-search-forward org-property-end-re end t)
  13467. (setq end (match-beginning 0))
  13468. (or force (throw 'exit nil))
  13469. (goto-char beg)
  13470. (setq end beg)
  13471. (org-indent-line)
  13472. (insert ":END:\n"))
  13473. (cons beg end)))))
  13474. (defun org-entry-properties (&optional pom which specific)
  13475. "Get all properties of the entry at point-or-marker POM.
  13476. This includes the TODO keyword, the tags, time strings for deadline,
  13477. scheduled, and clocking, and any additional properties defined in the
  13478. entry. The return value is an alist, keys may occur multiple times
  13479. if the property key was used several times.
  13480. POM may also be nil, in which case the current entry is used.
  13481. If WHICH is nil or `all', get all properties. If WHICH is
  13482. `special' or `standard', only get that subclass. If WHICH
  13483. is a string only get exactly this property. SPECIFIC can be a string, the
  13484. specific property we are interested in. Specifying it can speed
  13485. things up because then unnecessary parsing is avoided."
  13486. (setq which (or which 'all))
  13487. (org-with-point-at pom
  13488. (let ((clockstr (substring org-clock-string 0 -1))
  13489. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13490. (case-fold-search nil)
  13491. beg end range props sum-props key key1 value string clocksum clocksumt)
  13492. (save-excursion
  13493. (when (condition-case nil
  13494. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13495. (error nil))
  13496. (setq beg (point))
  13497. (setq sum-props (get-text-property (point) 'org-summaries))
  13498. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13499. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13500. (outline-next-heading)
  13501. (setq end (point))
  13502. (when (memq which '(all special))
  13503. ;; Get the special properties, like TODO and tags
  13504. (goto-char beg)
  13505. (when (and (or (not specific) (string= specific "TODO"))
  13506. (looking-at org-todo-line-regexp) (match-end 2))
  13507. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13508. (when (and (or (not specific) (string= specific "PRIORITY"))
  13509. (looking-at org-priority-regexp))
  13510. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13511. (when (or (not specific) (string= specific "FILE"))
  13512. (push (cons "FILE" buffer-file-name) props))
  13513. (when (and (or (not specific) (string= specific "TAGS"))
  13514. (setq value (org-get-tags-string))
  13515. (string-match "\\S-" value))
  13516. (push (cons "TAGS" value) props))
  13517. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13518. (setq value (org-get-tags-at)))
  13519. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13520. ":"))
  13521. props))
  13522. (when (or (not specific) (string= specific "BLOCKED"))
  13523. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13524. (when (or (not specific)
  13525. (member specific
  13526. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13527. "TIMESTAMP" "TIMESTAMP_IA")))
  13528. (catch 'match
  13529. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13530. (setq key (if (match-end 1)
  13531. (substring (org-match-string-no-properties 1)
  13532. 0 -1))
  13533. string (if (equal key clockstr)
  13534. (org-trim
  13535. (buffer-substring-no-properties
  13536. (match-beginning 3) (goto-char
  13537. (point-at-eol))))
  13538. (substring (org-match-string-no-properties 3)
  13539. 1 -1)))
  13540. ;; Get the correct property name from the key. This is
  13541. ;; necessary if the user has configured time keywords.
  13542. (setq key1 (concat key ":"))
  13543. (cond
  13544. ((not key)
  13545. (setq key
  13546. (if (= (char-after (match-beginning 3)) ?\[)
  13547. "TIMESTAMP_IA" "TIMESTAMP")))
  13548. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13549. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13550. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13551. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13552. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13553. (progn
  13554. (push (cons key string) props)
  13555. ;; no need to search further if match is found
  13556. (throw 'match t))
  13557. (when (or (equal key "CLOCK") (not (assoc key props)))
  13558. (push (cons key string) props)))))))
  13559. (when (memq which '(all standard))
  13560. ;; Get the standard properties, like :PROP: ...
  13561. (setq range (org-get-property-block beg end))
  13562. (when range
  13563. (goto-char (car range))
  13564. (while (re-search-forward
  13565. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13566. (cdr range) t)
  13567. (setq key (org-match-string-no-properties 1)
  13568. value (org-trim (or (org-match-string-no-properties 2) "")))
  13569. (unless (member key excluded)
  13570. (push (cons key (or value "")) props)))))
  13571. (if clocksum
  13572. (push (cons "CLOCKSUM"
  13573. (org-columns-number-to-string (/ (float clocksum) 60.)
  13574. 'add_times))
  13575. props))
  13576. (if clocksumt
  13577. (push (cons "CLOCKSUM_T"
  13578. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13579. 'add_times))
  13580. props))
  13581. (unless (assoc "CATEGORY" props)
  13582. (push (cons "CATEGORY" (org-get-category)) props))
  13583. (append sum-props (nreverse props)))))))
  13584. (defun org-entry-get (pom property &optional inherit literal-nil)
  13585. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13586. If INHERIT is non-nil and the entry does not have the property,
  13587. then also check higher levels of the hierarchy.
  13588. If INHERIT is the symbol `selective', use inheritance only if the setting
  13589. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13590. If the property is present but empty, the return value is the empty string.
  13591. If the property is not present at all, nil is returned.
  13592. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13593. do not interpret it as the list atom nil. This is used for inheritance
  13594. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13595. (org-with-point-at pom
  13596. (if (and inherit (if (eq inherit 'selective)
  13597. (org-property-inherit-p property)
  13598. t))
  13599. (org-entry-get-with-inheritance property literal-nil)
  13600. (if (member property org-special-properties)
  13601. ;; We need a special property. Use `org-entry-properties' to
  13602. ;; retrieve it, but specify the wanted property
  13603. (cdr (assoc property (org-entry-properties nil 'special property)))
  13604. (let ((range (org-get-property-block)))
  13605. (when (and range (not (eq (car range) (cdr range))))
  13606. (let* ((props (list (or (assoc property org-file-properties)
  13607. (assoc property org-global-properties)
  13608. (assoc property org-global-properties-fixed))))
  13609. (ap (lambda (key)
  13610. (when (re-search-forward
  13611. (org-re-property key) (cdr range) t)
  13612. (setq props
  13613. (org-update-property-plist
  13614. key
  13615. (if (match-end 1)
  13616. (org-match-string-no-properties 1) "")
  13617. props)))))
  13618. val)
  13619. (goto-char (car range))
  13620. (funcall ap property)
  13621. (goto-char (car range))
  13622. (while (funcall ap (concat property "+")))
  13623. (setq val (cdr (assoc property props)))
  13624. (when val (if literal-nil val (org-not-nil val))))))))))
  13625. (defun org-property-or-variable-value (var &optional inherit)
  13626. "Check if there is a property fixing the value of VAR.
  13627. If yes, return this value. If not, return the current value of the variable."
  13628. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13629. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13630. (read prop)
  13631. (symbol-value var))))
  13632. (defun org-entry-delete (pom property &optional delete-empty-drawer)
  13633. "Delete the property PROPERTY from entry at point-or-marker POM.
  13634. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  13635. an empty drawer to delete."
  13636. (org-with-point-at pom
  13637. (if (member property org-special-properties)
  13638. nil ; cannot delete these properties.
  13639. (let ((range (org-get-property-block)))
  13640. (if (and range
  13641. (goto-char (car range))
  13642. (re-search-forward
  13643. (org-re-property property)
  13644. (cdr range) t))
  13645. (progn
  13646. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13647. (and delete-empty-drawer
  13648. (org-remove-empty-drawer-at
  13649. delete-empty-drawer (car range)))
  13650. t)
  13651. nil)))))
  13652. ;; Multi-values properties are properties that contain multiple values
  13653. ;; These values are assumed to be single words, separated by whitespace.
  13654. (defun org-entry-add-to-multivalued-property (pom property value)
  13655. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13656. (let* ((old (org-entry-get pom property))
  13657. (values (and old (org-split-string old "[ \t]"))))
  13658. (setq value (org-entry-protect-space value))
  13659. (unless (member value values)
  13660. (setq values (cons value values))
  13661. (org-entry-put pom property
  13662. (mapconcat 'identity values " ")))))
  13663. (defun org-entry-remove-from-multivalued-property (pom property value)
  13664. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13665. (let* ((old (org-entry-get pom property))
  13666. (values (and old (org-split-string old "[ \t]"))))
  13667. (setq value (org-entry-protect-space value))
  13668. (when (member value values)
  13669. (setq values (delete value values))
  13670. (org-entry-put pom property
  13671. (mapconcat 'identity values " ")))))
  13672. (defun org-entry-member-in-multivalued-property (pom property value)
  13673. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13674. (let* ((old (org-entry-get pom property))
  13675. (values (and old (org-split-string old "[ \t]"))))
  13676. (setq value (org-entry-protect-space value))
  13677. (member value values)))
  13678. (defun org-entry-get-multivalued-property (pom property)
  13679. "Return a list of values in a multivalued property."
  13680. (let* ((value (org-entry-get pom property))
  13681. (values (and value (org-split-string value "[ \t]"))))
  13682. (mapcar 'org-entry-restore-space values)))
  13683. (defun org-entry-put-multivalued-property (pom property &rest values)
  13684. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13685. VALUES should be a list of strings. Spaces will be protected."
  13686. (org-entry-put pom property
  13687. (mapconcat 'org-entry-protect-space values " "))
  13688. (let* ((value (org-entry-get pom property))
  13689. (values (and value (org-split-string value "[ \t]"))))
  13690. (mapcar 'org-entry-restore-space values)))
  13691. (defun org-entry-protect-space (s)
  13692. "Protect spaces and newline in string S."
  13693. (while (string-match " " s)
  13694. (setq s (replace-match "%20" t t s)))
  13695. (while (string-match "\n" s)
  13696. (setq s (replace-match "%0A" t t s)))
  13697. s)
  13698. (defun org-entry-restore-space (s)
  13699. "Restore spaces and newline in string S."
  13700. (while (string-match "%20" s)
  13701. (setq s (replace-match " " t t s)))
  13702. (while (string-match "%0A" s)
  13703. (setq s (replace-match "\n" t t s)))
  13704. s)
  13705. (defvar org-entry-property-inherited-from (make-marker)
  13706. "Marker pointing to the entry from where a property was inherited.
  13707. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13708. location of the entry where the inheritance search matched. If there was
  13709. no match, the marker will point nowhere.
  13710. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13711. is set.")
  13712. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13713. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13714. The search will stop at the first ancestor which has the property defined.
  13715. If the value found is \"nil\", return nil to show that the property
  13716. should be considered as undefined (this is the meaning of nil here).
  13717. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13718. (move-marker org-entry-property-inherited-from nil)
  13719. (let (tmp)
  13720. (save-excursion
  13721. (save-restriction
  13722. (widen)
  13723. (catch 'ex
  13724. (while t
  13725. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13726. (or (ignore-errors (org-back-to-heading t))
  13727. (goto-char (point-min)))
  13728. (move-marker org-entry-property-inherited-from (point))
  13729. (throw 'ex tmp))
  13730. (or (ignore-errors (org-up-heading-safe))
  13731. (throw 'ex nil))))))
  13732. (setq tmp (or tmp
  13733. (cdr (assoc property org-file-properties))
  13734. (cdr (assoc property org-global-properties))
  13735. (cdr (assoc property org-global-properties-fixed))))
  13736. (if literal-nil tmp (org-not-nil tmp))))
  13737. (defvar org-property-changed-functions nil
  13738. "Hook called when the value of a property has changed.
  13739. Each hook function should accept two arguments, the name of the property
  13740. and the new value.")
  13741. (defun org-entry-put (pom property value)
  13742. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13743. (org-with-point-at pom
  13744. (org-back-to-heading t)
  13745. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13746. range)
  13747. (cond
  13748. ((equal property "TODO")
  13749. (when (and (stringp value) (string-match "\\S-" value)
  13750. (not (member value org-todo-keywords-1)))
  13751. (error "\"%s\" is not a valid TODO state" value))
  13752. (if (or (not value)
  13753. (not (string-match "\\S-" value)))
  13754. (setq value 'none))
  13755. (org-todo value)
  13756. (org-set-tags nil 'align))
  13757. ((equal property "PRIORITY")
  13758. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13759. (string-to-char value) ?\ ))
  13760. (org-set-tags nil 'align))
  13761. ((equal property "CLOCKSUM")
  13762. (if (not (re-search-forward
  13763. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13764. (error "Cannot find a clock log")
  13765. (goto-char (- (match-end 1) 2))
  13766. (cond
  13767. ((eq value 'earlier) (org-timestamp-down))
  13768. ((eq value 'later) (org-timestamp-up)))
  13769. (org-clock-sum-current-item)))
  13770. ((equal property "SCHEDULED")
  13771. (if (re-search-forward org-scheduled-time-regexp end t)
  13772. (cond
  13773. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13774. ((eq value 'later) (org-timestamp-change 1 'day))
  13775. (t (call-interactively 'org-schedule)))
  13776. (call-interactively 'org-schedule)))
  13777. ((equal property "DEADLINE")
  13778. (if (re-search-forward org-deadline-time-regexp end t)
  13779. (cond
  13780. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13781. ((eq value 'later) (org-timestamp-change 1 'day))
  13782. (t (call-interactively 'org-deadline)))
  13783. (call-interactively 'org-deadline)))
  13784. ((member property org-special-properties)
  13785. (error "The %s property can not yet be set with `org-entry-put'"
  13786. property))
  13787. (t ; a non-special property
  13788. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13789. (setq range (org-get-property-block beg end 'force))
  13790. (goto-char (car range))
  13791. (if (re-search-forward
  13792. (org-re-property-keyword property) (cdr range) t)
  13793. (progn
  13794. (delete-region (match-beginning 0) (match-end 0))
  13795. (goto-char (match-beginning 0)))
  13796. (goto-char (cdr range))
  13797. (insert "\n")
  13798. (backward-char 1)
  13799. (org-indent-line))
  13800. (insert ":" property ":")
  13801. (and value (insert " " value))
  13802. (org-indent-line)))))
  13803. (run-hook-with-args 'org-property-changed-functions property value)))
  13804. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13805. "Get all property keys in the current buffer.
  13806. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13807. like tags and TODO state.
  13808. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13809. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13810. and others.
  13811. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13812. formats in the current buffer."
  13813. (let (rtn range cfmt s p)
  13814. (save-excursion
  13815. (save-restriction
  13816. (widen)
  13817. (goto-char (point-min))
  13818. (while (re-search-forward org-property-start-re nil t)
  13819. (setq range (org-get-property-block))
  13820. (goto-char (car range))
  13821. (while (re-search-forward
  13822. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13823. (cdr range) t)
  13824. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13825. (outline-next-heading))))
  13826. (when include-specials
  13827. (setq rtn (append org-special-properties rtn)))
  13828. (when include-defaults
  13829. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13830. (add-to-list 'rtn org-effort-property))
  13831. (when include-columns
  13832. (save-excursion
  13833. (save-restriction
  13834. (widen)
  13835. (goto-char (point-min))
  13836. (while (re-search-forward
  13837. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13838. nil t)
  13839. (setq cfmt (match-string 2) s 0)
  13840. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13841. cfmt s)
  13842. (setq s (match-end 0)
  13843. p (match-string 1 cfmt))
  13844. (unless (or (equal p "ITEM")
  13845. (member p org-special-properties))
  13846. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13847. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13848. (defun org-property-values (key)
  13849. "Return a list of all values of property KEY in the current buffer."
  13850. (save-excursion
  13851. (save-restriction
  13852. (widen)
  13853. (goto-char (point-min))
  13854. (let ((re (org-re-property key))
  13855. values)
  13856. (while (re-search-forward re nil t)
  13857. (add-to-list 'values (org-trim (match-string 1))))
  13858. (delete "" values)))))
  13859. (defun org-insert-property-drawer ()
  13860. "Insert a property drawer into the current entry."
  13861. (org-back-to-heading t)
  13862. (looking-at org-outline-regexp)
  13863. (let ((indent (if org-adapt-indentation
  13864. (- (match-end 0) (match-beginning 0))
  13865. 0))
  13866. (beg (point))
  13867. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13868. end hiddenp)
  13869. (outline-next-heading)
  13870. (setq end (point))
  13871. (goto-char beg)
  13872. (while (re-search-forward re end t))
  13873. (setq hiddenp (outline-invisible-p))
  13874. (end-of-line 1)
  13875. (and (equal (char-after) ?\n) (forward-char 1))
  13876. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13877. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13878. ;; just skip this line
  13879. (beginning-of-line 2)
  13880. ;; Drawer start, find the end
  13881. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13882. (beginning-of-line 1)))
  13883. (org-skip-over-state-notes)
  13884. (skip-chars-backward " \t\n\r")
  13885. (if (eq (char-before) ?*) (forward-char 1))
  13886. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13887. (beginning-of-line 0)
  13888. (org-indent-to-column indent)
  13889. (beginning-of-line 2)
  13890. (org-indent-to-column indent)
  13891. (beginning-of-line 0)
  13892. (if hiddenp
  13893. (save-excursion
  13894. (org-back-to-heading t)
  13895. (hide-entry))
  13896. (org-flag-drawer t))))
  13897. (defun org-insert-drawer (&optional arg drawer)
  13898. "Insert a drawer at point.
  13899. Optional argument DRAWER, when non-nil, is a string representing
  13900. drawer's name. Otherwise, the user is prompted for a name.
  13901. If a region is active, insert the drawer around that region
  13902. instead.
  13903. Point is left between drawer's boundaries."
  13904. (interactive "P")
  13905. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13906. "LOGBOOK"))
  13907. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13908. ;; internally by Org. They are meant to be inserted
  13909. ;; automatically.
  13910. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13911. ;; Remove system drawers from list. Note: For some reason,
  13912. ;; `org-completing-read' ignores the predicate while
  13913. ;; `completing-read' handles it fine.
  13914. (drawer (if arg "PROPERTIES"
  13915. (or drawer
  13916. (completing-read
  13917. "Drawer: " org-drawers
  13918. (lambda (d) (not (member d system-drawers))))))))
  13919. (cond
  13920. ;; With C-u, fall back on `org-insert-property-drawer'
  13921. (arg (org-insert-property-drawer))
  13922. ;; With an active region, insert a drawer at point.
  13923. ((not (org-region-active-p))
  13924. (progn
  13925. (unless (bolp) (insert "\n"))
  13926. (insert (format ":%s:\n\n:END:\n" drawer))
  13927. (forward-line -2)))
  13928. ;; Otherwise, insert the drawer at point
  13929. (t
  13930. (let ((rbeg (region-beginning))
  13931. (rend (copy-marker (region-end))))
  13932. (unwind-protect
  13933. (progn
  13934. (goto-char rbeg)
  13935. (beginning-of-line)
  13936. (when (save-excursion
  13937. (re-search-forward org-outline-regexp-bol rend t))
  13938. (error "Drawers cannot contain headlines"))
  13939. ;; Position point at the beginning of the first
  13940. ;; non-blank line in region. Insert drawer's opening
  13941. ;; there, then indent it.
  13942. (org-skip-whitespace)
  13943. (beginning-of-line)
  13944. (insert ":" drawer ":\n")
  13945. (forward-line -1)
  13946. (indent-for-tab-command)
  13947. ;; Move point to the beginning of the first blank line
  13948. ;; after the last non-blank line in region. Insert
  13949. ;; drawer's closing, then indent it.
  13950. (goto-char rend)
  13951. (skip-chars-backward " \r\t\n")
  13952. (insert "\n:END:")
  13953. (deactivate-mark t)
  13954. (indent-for-tab-command)
  13955. (unless (eolp) (insert "\n")))
  13956. ;; Clear marker, whatever the outcome of insertion is.
  13957. (set-marker rend nil)))))))
  13958. (defvar org-property-set-functions-alist nil
  13959. "Property set function alist.
  13960. Each entry should have the following format:
  13961. (PROPERTY . READ-FUNCTION)
  13962. The read function will be called with the same argument as
  13963. `org-completing-read'.")
  13964. (defun org-set-property-function (property)
  13965. "Get the function that should be used to set PROPERTY.
  13966. This is computed according to `org-property-set-functions-alist'."
  13967. (or (cdr (assoc property org-property-set-functions-alist))
  13968. 'org-completing-read))
  13969. (defun org-read-property-value (property)
  13970. "Read PROPERTY value from user."
  13971. (let* ((completion-ignore-case t)
  13972. (allowed (org-property-get-allowed-values nil property 'table))
  13973. (cur (org-entry-get nil property))
  13974. (prompt (concat property " value"
  13975. (if (and cur (string-match "\\S-" cur))
  13976. (concat " [" cur "]") "") ": "))
  13977. (set-function (org-set-property-function property))
  13978. (val (if allowed
  13979. (funcall set-function prompt allowed nil
  13980. (not (get-text-property 0 'org-unrestricted
  13981. (caar allowed))))
  13982. (let (org-completion-use-ido org-completion-use-iswitchb)
  13983. (funcall set-function prompt
  13984. (mapcar 'list (org-property-values property))
  13985. nil nil "" nil cur)))))
  13986. (if (equal val "")
  13987. cur
  13988. val)))
  13989. (defvar org-last-set-property nil)
  13990. (defvar org-last-set-property-value nil)
  13991. (defun org-read-property-name ()
  13992. "Read a property name."
  13993. (let* ((completion-ignore-case t)
  13994. (keys (org-buffer-property-keys nil t t))
  13995. (default-prop (or (save-excursion
  13996. (save-match-data
  13997. (beginning-of-line)
  13998. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13999. (null (string= (match-string 1) "END"))
  14000. (match-string 1))))
  14001. org-last-set-property))
  14002. (property (org-icompleting-read
  14003. (concat "Property"
  14004. (if default-prop (concat " [" default-prop "]") "")
  14005. ": ")
  14006. (mapcar 'list keys)
  14007. nil nil nil nil
  14008. default-prop)))
  14009. (if (member property keys)
  14010. property
  14011. (or (cdr (assoc (downcase property)
  14012. (mapcar (lambda (x) (cons (downcase x) x))
  14013. keys)))
  14014. property))))
  14015. (defun org-set-property-and-value (use-last)
  14016. "Allow to set [PROPERTY]: [value] direction from prompt.
  14017. When use-default, don't even ask, just use the last
  14018. \"[PROPERTY]: [value]\" string from the history."
  14019. (interactive "P")
  14020. (let* ((completion-ignore-case t)
  14021. (pv (or (and use-last org-last-set-property-value)
  14022. (org-completing-read
  14023. "Enter a \"[Property]: [value]\" pair: "
  14024. nil nil nil nil nil
  14025. org-last-set-property-value)))
  14026. prop val)
  14027. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14028. (setq prop (match-string 1 pv)
  14029. val (match-string 2 pv))
  14030. (org-set-property prop val))))
  14031. (defun org-set-property (property value)
  14032. "In the current entry, set PROPERTY to VALUE.
  14033. When called interactively, this will prompt for a property name, offering
  14034. completion on existing and default properties. And then it will prompt
  14035. for a value, offering completion either on allowed values (via an inherited
  14036. xxx_ALL property) or on existing values in other instances of this property
  14037. in the current file."
  14038. (interactive (list nil nil))
  14039. (let* ((property (or property (org-read-property-name)))
  14040. (value (or value (org-read-property-value property)))
  14041. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14042. (setq org-last-set-property property)
  14043. (setq org-last-set-property-value (concat property ": " value))
  14044. ;; Possibly postprocess the inserted value:
  14045. (when fn (setq value (funcall fn value)))
  14046. (unless (equal (org-entry-get nil property) value)
  14047. (org-entry-put nil property value))))
  14048. (defun org-delete-property (property &optional delete-empty-drawer)
  14049. "In the current entry, delete PROPERTY.
  14050. When optional argument DELETE-EMPTY-DRAWER is a string, it defines
  14051. an empty drawer to delete."
  14052. (interactive
  14053. (let* ((completion-ignore-case t)
  14054. (prop (org-icompleting-read "Property: "
  14055. (org-entry-properties nil 'standard))))
  14056. (list prop)))
  14057. (message "Property %s %s" property
  14058. (if (org-entry-delete nil property delete-empty-drawer)
  14059. "deleted"
  14060. "was not present in the entry")))
  14061. (defun org-delete-property-globally (property)
  14062. "Remove PROPERTY globally, from all entries."
  14063. (interactive
  14064. (let* ((completion-ignore-case t)
  14065. (prop (org-icompleting-read
  14066. "Globally remove property: "
  14067. (mapcar 'list (org-buffer-property-keys)))))
  14068. (list prop)))
  14069. (save-excursion
  14070. (save-restriction
  14071. (widen)
  14072. (goto-char (point-min))
  14073. (let ((cnt 0))
  14074. (while (re-search-forward
  14075. (org-re-property property)
  14076. nil t)
  14077. (setq cnt (1+ cnt))
  14078. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  14079. (message "Property \"%s\" removed from %d entries" property cnt)))))
  14080. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14081. (defun org-compute-property-at-point ()
  14082. "Compute the property at point.
  14083. This looks for an enclosing column format, extracts the operator and
  14084. then applies it to the property in the column format's scope."
  14085. (interactive)
  14086. (unless (org-at-property-p)
  14087. (error "Not at a property"))
  14088. (let ((prop (org-match-string-no-properties 2)))
  14089. (org-columns-get-format-and-top-level)
  14090. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14091. (error "No operator defined for property %s" prop))
  14092. (org-columns-compute prop)))
  14093. (defvar org-property-allowed-value-functions nil
  14094. "Hook for functions supplying allowed values for a specific property.
  14095. The functions must take a single argument, the name of the property, and
  14096. return a flat list of allowed values. If \":ETC\" is one of
  14097. the values, this means that these values are intended as defaults for
  14098. completion, but that other values should be allowed too.
  14099. The functions must return nil if they are not responsible for this
  14100. property.")
  14101. (defun org-property-get-allowed-values (pom property &optional table)
  14102. "Get allowed values for the property PROPERTY.
  14103. When TABLE is non-nil, return an alist that can directly be used for
  14104. completion."
  14105. (let (vals)
  14106. (cond
  14107. ((equal property "TODO")
  14108. (setq vals (org-with-point-at pom
  14109. (append org-todo-keywords-1 '("")))))
  14110. ((equal property "PRIORITY")
  14111. (let ((n org-lowest-priority))
  14112. (while (>= n org-highest-priority)
  14113. (push (char-to-string n) vals)
  14114. (setq n (1- n)))))
  14115. ((member property org-special-properties))
  14116. ((setq vals (run-hook-with-args-until-success
  14117. 'org-property-allowed-value-functions property)))
  14118. (t
  14119. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14120. (when (and vals (string-match "\\S-" vals))
  14121. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14122. (setq vals (mapcar (lambda (x)
  14123. (cond ((stringp x) x)
  14124. ((numberp x) (number-to-string x))
  14125. ((symbolp x) (symbol-name x))
  14126. (t "???")))
  14127. vals)))))
  14128. (when (member ":ETC" vals)
  14129. (setq vals (remove ":ETC" vals))
  14130. (org-add-props (car vals) '(org-unrestricted t)))
  14131. (if table (mapcar 'list vals) vals)))
  14132. (defun org-property-previous-allowed-value (&optional previous)
  14133. "Switch to the next allowed value for this property."
  14134. (interactive)
  14135. (org-property-next-allowed-value t))
  14136. (defun org-property-next-allowed-value (&optional previous)
  14137. "Switch to the next allowed value for this property."
  14138. (interactive)
  14139. (unless (org-at-property-p)
  14140. (error "Not at a property"))
  14141. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14142. (key (match-string 2))
  14143. (value (match-string 3))
  14144. (allowed (or (org-property-get-allowed-values (point) key)
  14145. (and (member value '("[ ]" "[-]" "[X]"))
  14146. '("[ ]" "[X]"))))
  14147. nval)
  14148. (unless allowed
  14149. (error "Allowed values for this property have not been defined"))
  14150. (if previous (setq allowed (reverse allowed)))
  14151. (if (member value allowed)
  14152. (setq nval (car (cdr (member value allowed)))))
  14153. (setq nval (or nval (car allowed)))
  14154. (if (equal nval value)
  14155. (error "Only one allowed value for this property"))
  14156. (org-at-property-p)
  14157. (replace-match (concat " :" key ": " nval) t t)
  14158. (org-indent-line)
  14159. (beginning-of-line 1)
  14160. (skip-chars-forward " \t")
  14161. (when (equal prop org-effort-property)
  14162. (save-excursion
  14163. (org-back-to-heading t)
  14164. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
  14165. (run-hook-with-args 'org-property-changed-functions key nval)))
  14166. (defun org-find-olp (path &optional this-buffer)
  14167. "Return a marker pointing to the entry at outline path OLP.
  14168. If anything goes wrong, throw an error.
  14169. You can wrap this call to catch the error like this:
  14170. (condition-case msg
  14171. (org-mobile-locate-entry (match-string 4))
  14172. (error (nth 1 msg)))
  14173. The return value will then be either a string with the error message,
  14174. or a marker if everything is OK.
  14175. If THIS-BUFFER is set, the outline path does not contain a file,
  14176. only headings."
  14177. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14178. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14179. (level 1)
  14180. (lmin 1)
  14181. (lmax 1)
  14182. limit re end found pos heading cnt flevel)
  14183. (unless buffer (error "File not found :%s" file))
  14184. (with-current-buffer buffer
  14185. (save-excursion
  14186. (save-restriction
  14187. (widen)
  14188. (setq limit (point-max))
  14189. (goto-char (point-min))
  14190. (while (setq heading (pop path))
  14191. (setq re (format org-complex-heading-regexp-format
  14192. (regexp-quote heading)))
  14193. (setq cnt 0 pos (point))
  14194. (while (re-search-forward re end t)
  14195. (setq level (- (match-end 1) (match-beginning 1)))
  14196. (if (and (>= level lmin) (<= level lmax))
  14197. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14198. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14199. lmax heading))
  14200. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14201. lmax heading))
  14202. (goto-char found)
  14203. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14204. (setq end (save-excursion (org-end-of-subtree t t))))
  14205. (when (org-at-heading-p)
  14206. (point-marker)))))))
  14207. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14208. "Find node HEADING in BUFFER.
  14209. Return a marker to the heading if it was found, or nil if not.
  14210. If POS-ONLY is set, return just the position instead of a marker.
  14211. The heading text must match exact, but it may have a TODO keyword,
  14212. a priority cookie and tags in the standard locations."
  14213. (with-current-buffer (or buffer (current-buffer))
  14214. (save-excursion
  14215. (save-restriction
  14216. (widen)
  14217. (goto-char (point-min))
  14218. (let (case-fold-search)
  14219. (if (re-search-forward
  14220. (format org-complex-heading-regexp-format
  14221. (regexp-quote heading)) nil t)
  14222. (if pos-only
  14223. (match-beginning 0)
  14224. (move-marker (make-marker) (match-beginning 0)))))))))
  14225. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14226. "Find Org node headline HEADING in all .org files in directory DIR.
  14227. When the target headline is found, return a marker to this location."
  14228. (let ((files (directory-files (or dir default-directory)
  14229. nil "\\`[^.#].*\\.org\\'"))
  14230. file visiting m buffer)
  14231. (catch 'found
  14232. (while (setq file (pop files))
  14233. (message "trying %s" file)
  14234. (setq visiting (org-find-base-buffer-visiting file))
  14235. (setq buffer (or visiting (find-file-noselect file)))
  14236. (setq m (org-find-exact-headline-in-buffer
  14237. heading buffer))
  14238. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14239. (and m (throw 'found m))))))
  14240. (defun org-find-entry-with-id (ident)
  14241. "Locate the entry that contains the ID property with exact value IDENT.
  14242. IDENT can be a string, a symbol or a number, this function will search for
  14243. the string representation of it.
  14244. Return the position where this entry starts, or nil if there is no such entry."
  14245. (interactive "sID: ")
  14246. (let ((id (cond
  14247. ((stringp ident) ident)
  14248. ((symbol-name ident) (symbol-name ident))
  14249. ((numberp ident) (number-to-string ident))
  14250. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14251. (case-fold-search nil))
  14252. (save-excursion
  14253. (save-restriction
  14254. (widen)
  14255. (goto-char (point-min))
  14256. (when (re-search-forward
  14257. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14258. nil t)
  14259. (org-back-to-heading t)
  14260. (point))))))
  14261. ;;;; Timestamps
  14262. (defvar org-last-changed-timestamp nil)
  14263. (defvar org-last-inserted-timestamp nil
  14264. "The last time stamp inserted with `org-insert-time-stamp'.")
  14265. (defvar org-time-was-given) ; dynamically scoped parameter
  14266. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14267. (defvar org-ts-what) ; dynamically scoped parameter
  14268. (defun org-time-stamp (arg &optional inactive)
  14269. "Prompt for a date/time and insert a time stamp.
  14270. If the user specifies a time like HH:MM or if this command is
  14271. called with at least one prefix argument, the time stamp contains
  14272. the date and the time. Otherwise, only the date is be included.
  14273. All parts of a date not specified by the user is filled in from
  14274. the current date/time. So if you just press return without
  14275. typing anything, the time stamp will represent the current
  14276. date/time.
  14277. If there is already a timestamp at the cursor, it will be
  14278. modified.
  14279. With two universal prefix arguments, insert an active timestamp
  14280. with the current time without prompting the user."
  14281. (interactive "P")
  14282. (let* ((ts nil)
  14283. (default-time
  14284. ;; Default time is either today, or, when entering a range,
  14285. ;; the range start.
  14286. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14287. (save-excursion
  14288. (re-search-backward
  14289. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14290. (- (point) 20) t)))
  14291. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14292. (current-time)))
  14293. (default-input (and ts (org-get-compact-tod ts)))
  14294. (repeater (save-excursion
  14295. (save-match-data
  14296. (beginning-of-line)
  14297. (when (re-search-forward
  14298. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14299. (save-excursion (progn (end-of-line) (point))) t)
  14300. (match-string 0)))))
  14301. org-time-was-given org-end-time-was-given time)
  14302. (cond
  14303. ((and (org-at-timestamp-p t)
  14304. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14305. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14306. (insert "--")
  14307. (setq time (let ((this-command this-command))
  14308. (org-read-date arg 'totime nil nil
  14309. default-time default-input inactive)))
  14310. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14311. ((org-at-timestamp-p t)
  14312. (setq time (let ((this-command this-command))
  14313. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14314. (when (org-at-timestamp-p t) ; just to get the match data
  14315. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14316. (replace-match "")
  14317. (setq org-last-changed-timestamp
  14318. (org-insert-time-stamp
  14319. time (or org-time-was-given arg)
  14320. inactive nil nil (list org-end-time-was-given)))
  14321. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14322. (setq org-last-changed-timestamp
  14323. (concat (substring org-last-inserted-timestamp 0 -1)
  14324. " " repeater ">"))))
  14325. (message "Timestamp updated"))
  14326. ((equal arg '(16))
  14327. (org-insert-time-stamp (current-time) t))
  14328. (t
  14329. (setq time (let ((this-command this-command))
  14330. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14331. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14332. nil nil (list org-end-time-was-given))))))
  14333. ;; FIXME: can we use this for something else, like computing time differences?
  14334. (defun org-get-compact-tod (s)
  14335. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14336. (let* ((t1 (match-string 1 s))
  14337. (h1 (string-to-number (match-string 2 s)))
  14338. (m1 (string-to-number (match-string 3 s)))
  14339. (t2 (and (match-end 4) (match-string 5 s)))
  14340. (h2 (and t2 (string-to-number (match-string 6 s))))
  14341. (m2 (and t2 (string-to-number (match-string 7 s))))
  14342. dh dm)
  14343. (if (not t2)
  14344. t1
  14345. (setq dh (- h2 h1) dm (- m2 m1))
  14346. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14347. (concat t1 "+" (number-to-string dh)
  14348. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14349. (defun org-time-stamp-inactive (&optional arg)
  14350. "Insert an inactive time stamp.
  14351. An inactive time stamp is enclosed in square brackets instead of angle
  14352. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14353. does not link to the calendar and cannot be changed with the S-cursor keys.
  14354. So these are more for recording a certain time/date."
  14355. (interactive "P")
  14356. (org-time-stamp arg 'inactive))
  14357. (defvar org-date-ovl (make-overlay 1 1))
  14358. (overlay-put org-date-ovl 'face 'org-date-selected)
  14359. (org-detach-overlay org-date-ovl)
  14360. (defvar org-ans1) ; dynamically scoped parameter
  14361. (defvar org-ans2) ; dynamically scoped parameter
  14362. (defvar org-plain-time-of-day-regexp) ; defined below
  14363. (defvar org-overriding-default-time nil) ; dynamically scoped
  14364. (defvar org-read-date-overlay nil)
  14365. (defvar org-dcst nil) ; dynamically scoped
  14366. (defvar org-read-date-history nil)
  14367. (defvar org-read-date-final-answer nil)
  14368. (defvar org-read-date-analyze-futurep nil)
  14369. (defvar org-read-date-analyze-forced-year nil)
  14370. (defvar org-read-date-inactive)
  14371. (defvar org-read-date-minibuffer-local-map
  14372. (let ((map (make-sparse-keymap)))
  14373. (set-keymap-parent map minibuffer-local-map)
  14374. (org-defkey map (kbd ".")
  14375. (lambda () (interactive)
  14376. (org-eval-in-calendar '(calendar-goto-today))))
  14377. (org-defkey map [(meta shift left)]
  14378. (lambda () (interactive)
  14379. (org-eval-in-calendar '(calendar-backward-month 1))))
  14380. (org-defkey map [(meta shift right)]
  14381. (lambda () (interactive)
  14382. (org-eval-in-calendar '(calendar-forward-month 1))))
  14383. (org-defkey map [(meta shift up)]
  14384. (lambda () (interactive)
  14385. (org-eval-in-calendar '(calendar-backward-year 1))))
  14386. (org-defkey map [(meta shift down)]
  14387. (lambda () (interactive)
  14388. (org-eval-in-calendar '(calendar-forward-year 1))))
  14389. (org-defkey map [?\e (shift left)]
  14390. (lambda () (interactive)
  14391. (org-eval-in-calendar '(calendar-backward-month 1))))
  14392. (org-defkey map [?\e (shift right)]
  14393. (lambda () (interactive)
  14394. (org-eval-in-calendar '(calendar-forward-month 1))))
  14395. (org-defkey map [?\e (shift up)]
  14396. (lambda () (interactive)
  14397. (org-eval-in-calendar '(calendar-backward-year 1))))
  14398. (org-defkey map [?\e (shift down)]
  14399. (lambda () (interactive)
  14400. (org-eval-in-calendar '(calendar-forward-year 1))))
  14401. (org-defkey map [(shift up)]
  14402. (lambda () (interactive)
  14403. (org-eval-in-calendar '(calendar-backward-week 1))))
  14404. (org-defkey map [(shift down)]
  14405. (lambda () (interactive)
  14406. (org-eval-in-calendar '(calendar-forward-week 1))))
  14407. (org-defkey map [(shift left)]
  14408. (lambda () (interactive)
  14409. (org-eval-in-calendar '(calendar-backward-day 1))))
  14410. (org-defkey map [(shift right)]
  14411. (lambda () (interactive)
  14412. (org-eval-in-calendar '(calendar-forward-day 1))))
  14413. (org-defkey map "!"
  14414. (lambda () (interactive)
  14415. (org-eval-in-calendar '(diary-view-entries))
  14416. (message "")))
  14417. (org-defkey map ">"
  14418. (lambda () (interactive)
  14419. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14420. (org-defkey map "<"
  14421. (lambda () (interactive)
  14422. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14423. (org-defkey map "\C-v"
  14424. (lambda () (interactive)
  14425. (org-eval-in-calendar
  14426. '(calendar-scroll-left-three-months 1))))
  14427. (org-defkey map "\M-v"
  14428. (lambda () (interactive)
  14429. (org-eval-in-calendar
  14430. '(calendar-scroll-right-three-months 1))))
  14431. map)
  14432. "Keymap for minibuffer commands when using `org-read-date'.")
  14433. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14434. default-time default-input inactive)
  14435. "Read a date, possibly a time, and make things smooth for the user.
  14436. The prompt will suggest to enter an ISO date, but you can also enter anything
  14437. which will at least partially be understood by `parse-time-string'.
  14438. Unrecognized parts of the date will default to the current day, month, year,
  14439. hour and minute. If this command is called to replace a timestamp at point,
  14440. or to enter the second timestamp of a range, the default time is taken
  14441. from the existing stamp. Furthermore, the command prefers the future,
  14442. so if you are giving a date where the year is not given, and the day-month
  14443. combination is already past in the current year, it will assume you
  14444. mean next year. For details, see the manual. A few examples:
  14445. 3-2-5 --> 2003-02-05
  14446. feb 15 --> currentyear-02-15
  14447. 2/15 --> currentyear-02-15
  14448. sep 12 9 --> 2009-09-12
  14449. 12:45 --> today 12:45
  14450. 22 sept 0:34 --> currentyear-09-22 0:34
  14451. 12 --> currentyear-currentmonth-12
  14452. Fri --> nearest Friday (today or later)
  14453. etc.
  14454. Furthermore you can specify a relative date by giving, as the *first* thing
  14455. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14456. change in days weeks, months, years.
  14457. With a single plus or minus, the date is relative to today. With a double
  14458. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14459. +4d --> four days from today
  14460. +4 --> same as above
  14461. +2w --> two weeks from today
  14462. ++5 --> five days from default date
  14463. The function understands only English month and weekday abbreviations.
  14464. While prompting, a calendar is popped up - you can also select the
  14465. date with the mouse (button 1). The calendar shows a period of three
  14466. months. To scroll it to other months, use the keys `>' and `<'.
  14467. If you don't like the calendar, turn it off with
  14468. \(setq org-read-date-popup-calendar nil)
  14469. With optional argument TO-TIME, the date will immediately be converted
  14470. to an internal time.
  14471. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14472. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14473. still enter a time, and this function will inform the calling routine
  14474. about this change. The calling routine may then choose to change the
  14475. format used to insert the time stamp into the buffer to include the time.
  14476. With optional argument FROM-STRING, read from this string instead from
  14477. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14478. the time/date that is used for everything that is not specified by the
  14479. user."
  14480. (require 'parse-time)
  14481. (let* ((org-time-stamp-rounding-minutes
  14482. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14483. (org-dcst org-display-custom-times)
  14484. (ct (org-current-time))
  14485. (org-def (or org-overriding-default-time default-time ct))
  14486. (org-defdecode (decode-time org-def))
  14487. (dummy (progn
  14488. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14489. (setcar (nthcdr 2 org-defdecode) -1)
  14490. (setcar (nthcdr 1 org-defdecode) 59)
  14491. (setq org-def (apply 'encode-time org-defdecode)
  14492. org-defdecode (decode-time org-def)))))
  14493. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14494. (calendar-frame-setup nil)
  14495. (calendar-setup nil)
  14496. (calendar-move-hook nil)
  14497. (calendar-view-diary-initially-flag nil)
  14498. (calendar-view-holidays-initially-flag nil)
  14499. (timestr (format-time-string
  14500. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14501. (prompt (concat (if prompt (concat prompt " ") "")
  14502. (format "Date+time [%s]: " timestr)))
  14503. ans (org-ans0 "") org-ans1 org-ans2 final)
  14504. (cond
  14505. (from-string (setq ans from-string))
  14506. (org-read-date-popup-calendar
  14507. (save-excursion
  14508. (save-window-excursion
  14509. (calendar)
  14510. (org-eval-in-calendar '(setq cursor-type nil) t)
  14511. (unwind-protect
  14512. (progn
  14513. (calendar-forward-day (- (time-to-days org-def)
  14514. (calendar-absolute-from-gregorian
  14515. (calendar-current-date))))
  14516. (org-eval-in-calendar nil t)
  14517. (let* ((old-map (current-local-map))
  14518. (map (copy-keymap calendar-mode-map))
  14519. (minibuffer-local-map
  14520. (copy-keymap org-read-date-minibuffer-local-map)))
  14521. (org-defkey map (kbd "RET") 'org-calendar-select)
  14522. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14523. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14524. (unwind-protect
  14525. (progn
  14526. (use-local-map map)
  14527. (setq org-read-date-inactive inactive)
  14528. (add-hook 'post-command-hook 'org-read-date-display)
  14529. (setq org-ans0 (read-string prompt default-input
  14530. 'org-read-date-history nil))
  14531. ;; org-ans0: from prompt
  14532. ;; org-ans1: from mouse click
  14533. ;; org-ans2: from calendar motion
  14534. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14535. (remove-hook 'post-command-hook 'org-read-date-display)
  14536. (use-local-map old-map)
  14537. (when org-read-date-overlay
  14538. (delete-overlay org-read-date-overlay)
  14539. (setq org-read-date-overlay nil)))))
  14540. (bury-buffer "*Calendar*")))))
  14541. (t ; Naked prompt only
  14542. (unwind-protect
  14543. (setq ans (read-string prompt default-input
  14544. 'org-read-date-history timestr))
  14545. (when org-read-date-overlay
  14546. (delete-overlay org-read-date-overlay)
  14547. (setq org-read-date-overlay nil)))))
  14548. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14549. (when org-read-date-analyze-forced-year
  14550. (message "Year was forced into %s"
  14551. (if org-read-date-force-compatible-dates
  14552. "compatible range (1970-2037)"
  14553. "range representable on this machine"))
  14554. (ding))
  14555. ;; One round trip to get rid of 34th of August and stuff like that....
  14556. (setq final (decode-time (apply 'encode-time final)))
  14557. (setq org-read-date-final-answer ans)
  14558. (if to-time
  14559. (apply 'encode-time final)
  14560. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14561. (format "%04d-%02d-%02d %02d:%02d"
  14562. (nth 5 final) (nth 4 final) (nth 3 final)
  14563. (nth 2 final) (nth 1 final))
  14564. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14565. (defvar org-def)
  14566. (defvar org-defdecode)
  14567. (defvar org-with-time)
  14568. (defun org-read-date-display ()
  14569. "Display the current date prompt interpretation in the minibuffer."
  14570. (when org-read-date-display-live
  14571. (when org-read-date-overlay
  14572. (delete-overlay org-read-date-overlay))
  14573. (when (minibufferp (current-buffer))
  14574. (save-excursion
  14575. (end-of-line 1)
  14576. (while (not (equal (buffer-substring
  14577. (max (point-min) (- (point) 4)) (point))
  14578. " "))
  14579. (insert " ")))
  14580. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14581. " " (or org-ans1 org-ans2)))
  14582. (org-end-time-was-given nil)
  14583. (f (org-read-date-analyze ans org-def org-defdecode))
  14584. (fmts (if org-dcst
  14585. org-time-stamp-custom-formats
  14586. org-time-stamp-formats))
  14587. (fmt (if (or org-with-time
  14588. (and (boundp 'org-time-was-given) org-time-was-given))
  14589. (cdr fmts)
  14590. (car fmts)))
  14591. (txt (format-time-string fmt (apply 'encode-time f)))
  14592. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14593. (txt (concat "=> " txt)))
  14594. (when (and org-end-time-was-given
  14595. (string-match org-plain-time-of-day-regexp txt))
  14596. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14597. org-end-time-was-given
  14598. (substring txt (match-end 0)))))
  14599. (when org-read-date-analyze-futurep
  14600. (setq txt (concat txt " (=>F)")))
  14601. (setq org-read-date-overlay
  14602. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14603. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14604. (defun org-read-date-analyze (ans org-def org-defdecode)
  14605. "Analyze the combined answer of the date prompt."
  14606. ;; FIXME: cleanup and comment
  14607. (let ((nowdecode (decode-time (current-time)))
  14608. delta deltan deltaw deltadef year month day
  14609. hour minute second wday pm h2 m2 tl wday1
  14610. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14611. (setq org-read-date-analyze-futurep nil
  14612. org-read-date-analyze-forced-year nil)
  14613. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14614. (setq ans "+0"))
  14615. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14616. (setq ans (replace-match "" t t ans)
  14617. deltan (car delta)
  14618. deltaw (nth 1 delta)
  14619. deltadef (nth 2 delta)))
  14620. ;; Check if there is an iso week date in there. If yes, store the
  14621. ;; info and postpone interpreting it until the rest of the parsing
  14622. ;; is done.
  14623. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14624. (setq iso-year (if (match-end 1)
  14625. (org-small-year-to-year
  14626. (string-to-number (match-string 1 ans))))
  14627. iso-weekday (if (match-end 3)
  14628. (string-to-number (match-string 3 ans)))
  14629. iso-week (string-to-number (match-string 2 ans)))
  14630. (setq ans (replace-match "" t t ans)))
  14631. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14632. (when (string-match
  14633. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14634. (setq year (if (match-end 2)
  14635. (string-to-number (match-string 2 ans))
  14636. (progn (setq kill-year t)
  14637. (string-to-number (format-time-string "%Y"))))
  14638. month (string-to-number (match-string 3 ans))
  14639. day (string-to-number (match-string 4 ans)))
  14640. (if (< year 100) (setq year (+ 2000 year)))
  14641. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14642. t nil ans)))
  14643. ;; Help matching dotted european dates
  14644. (when (string-match
  14645. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14646. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14647. (setq kill-year t)
  14648. (string-to-number (format-time-string "%Y")))
  14649. day (string-to-number (match-string 1 ans))
  14650. month (string-to-number (match-string 2 ans))
  14651. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14652. t nil ans)))
  14653. ;; Help matching american dates, like 5/30 or 5/30/7
  14654. (when (string-match
  14655. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14656. (setq year (if (match-end 4)
  14657. (string-to-number (match-string 4 ans))
  14658. (progn (setq kill-year t)
  14659. (string-to-number (format-time-string "%Y"))))
  14660. month (string-to-number (match-string 1 ans))
  14661. day (string-to-number (match-string 2 ans)))
  14662. (if (< year 100) (setq year (+ 2000 year)))
  14663. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14664. t nil ans)))
  14665. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14666. ;; If there is a time with am/pm, and *no* time without it, we convert
  14667. ;; so that matching will be successful.
  14668. (loop for i from 1 to 2 do ; twice, for end time as well
  14669. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14670. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14671. (setq hour (string-to-number (match-string 1 ans))
  14672. minute (if (match-end 3)
  14673. (string-to-number (match-string 3 ans))
  14674. 0)
  14675. pm (equal ?p
  14676. (string-to-char (downcase (match-string 4 ans)))))
  14677. (if (and (= hour 12) (not pm))
  14678. (setq hour 0)
  14679. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14680. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14681. t t ans))))
  14682. ;; Check if a time range is given as a duration
  14683. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14684. (setq hour (string-to-number (match-string 1 ans))
  14685. h2 (+ hour (string-to-number (match-string 3 ans)))
  14686. minute (string-to-number (match-string 2 ans))
  14687. m2 (+ minute (if (match-end 5) (string-to-number
  14688. (match-string 5 ans))0)))
  14689. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14690. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14691. t t ans)))
  14692. ;; Check if there is a time range
  14693. (when (boundp 'org-end-time-was-given)
  14694. (setq org-time-was-given nil)
  14695. (when (and (string-match org-plain-time-of-day-regexp ans)
  14696. (match-end 8))
  14697. (setq org-end-time-was-given (match-string 8 ans))
  14698. (setq ans (concat (substring ans 0 (match-beginning 7))
  14699. (substring ans (match-end 7))))))
  14700. (setq tl (parse-time-string ans)
  14701. day (or (nth 3 tl) (nth 3 org-defdecode))
  14702. month (or (nth 4 tl)
  14703. (if (and org-read-date-prefer-future
  14704. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14705. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14706. (nth 4 org-defdecode)))
  14707. year (or (and (not kill-year) (nth 5 tl))
  14708. (if (and org-read-date-prefer-future
  14709. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14710. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14711. (nth 5 org-defdecode)))
  14712. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14713. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14714. second (or (nth 0 tl) 0)
  14715. wday (nth 6 tl))
  14716. (when (and (eq org-read-date-prefer-future 'time)
  14717. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14718. (equal day (nth 3 nowdecode))
  14719. (equal month (nth 4 nowdecode))
  14720. (equal year (nth 5 nowdecode))
  14721. (nth 2 tl)
  14722. (or (< (nth 2 tl) (nth 2 nowdecode))
  14723. (and (= (nth 2 tl) (nth 2 nowdecode))
  14724. (nth 1 tl)
  14725. (< (nth 1 tl) (nth 1 nowdecode)))))
  14726. (setq day (1+ day)
  14727. futurep t))
  14728. ;; Special date definitions below
  14729. (cond
  14730. (iso-week
  14731. ;; There was an iso week
  14732. (require 'cal-iso)
  14733. (setq futurep nil)
  14734. (setq year (or iso-year year)
  14735. day (or iso-weekday wday 1)
  14736. wday nil ; to make sure that the trigger below does not match
  14737. iso-date (calendar-gregorian-from-absolute
  14738. (calendar-absolute-from-iso
  14739. (list iso-week day year))))
  14740. ; FIXME: Should we also push ISO weeks into the future?
  14741. ; (when (and org-read-date-prefer-future
  14742. ; (not iso-year)
  14743. ; (< (calendar-absolute-from-gregorian iso-date)
  14744. ; (time-to-days (current-time))))
  14745. ; (setq year (1+ year)
  14746. ; iso-date (calendar-gregorian-from-absolute
  14747. ; (calendar-absolute-from-iso
  14748. ; (list iso-week day year)))))
  14749. (setq month (car iso-date)
  14750. year (nth 2 iso-date)
  14751. day (nth 1 iso-date)))
  14752. (deltan
  14753. (setq futurep nil)
  14754. (unless deltadef
  14755. (let ((now (decode-time (current-time))))
  14756. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14757. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14758. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14759. ((equal deltaw "m") (setq month (+ month deltan)))
  14760. ((equal deltaw "y") (setq year (+ year deltan)))))
  14761. ((and wday (not (nth 3 tl)))
  14762. ;; Weekday was given, but no day, so pick that day in the week
  14763. ;; on or after the derived date.
  14764. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14765. (unless (equal wday wday1)
  14766. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14767. (if (and (boundp 'org-time-was-given)
  14768. (nth 2 tl))
  14769. (setq org-time-was-given t))
  14770. (if (< year 100) (setq year (+ 2000 year)))
  14771. ;; Check of the date is representable
  14772. (if org-read-date-force-compatible-dates
  14773. (progn
  14774. (if (< year 1970)
  14775. (setq year 1970 org-read-date-analyze-forced-year t))
  14776. (if (> year 2037)
  14777. (setq year 2037 org-read-date-analyze-forced-year t)))
  14778. (condition-case nil
  14779. (ignore (encode-time second minute hour day month year))
  14780. (error
  14781. (setq year (nth 5 org-defdecode))
  14782. (setq org-read-date-analyze-forced-year t))))
  14783. (setq org-read-date-analyze-futurep futurep)
  14784. (list second minute hour day month year)))
  14785. (defvar parse-time-weekdays)
  14786. (defun org-read-date-get-relative (s today default)
  14787. "Check string S for special relative date string.
  14788. TODAY and DEFAULT are internal times, for today and for a default.
  14789. Return shift list (N what def-flag)
  14790. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14791. N is the number of WHATs to shift.
  14792. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14793. the DEFAULT date rather than TODAY."
  14794. (require 'parse-time)
  14795. (when (and
  14796. (string-match
  14797. (concat
  14798. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14799. "\\([0-9]+\\)?"
  14800. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14801. "\\([ \t]\\|$\\)") s)
  14802. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14803. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14804. (string-to-char (substring (match-string 1 s) -1))
  14805. ?+))
  14806. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14807. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14808. (what (if (match-end 3) (match-string 3 s) "d"))
  14809. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14810. (date (if rel default today))
  14811. (wday (nth 6 (decode-time date)))
  14812. delta)
  14813. (if wday1
  14814. (progn
  14815. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14816. (if (= dir ?-) (setq delta (- delta 7)))
  14817. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14818. (list delta "d" rel))
  14819. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14820. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14821. "Turn a user-specified date into the internal representation.
  14822. The internal representation needed by the calendar is (month day year).
  14823. This is a wrapper to handle the brain-dead convention in calendar that
  14824. user function argument order change dependent on argument order."
  14825. (if (boundp 'calendar-date-style)
  14826. (cond
  14827. ((eq calendar-date-style 'american)
  14828. (list arg1 arg2 arg3))
  14829. ((eq calendar-date-style 'european)
  14830. (list arg2 arg1 arg3))
  14831. ((eq calendar-date-style 'iso)
  14832. (list arg2 arg3 arg1)))
  14833. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14834. (if (org-bound-and-true-p european-calendar-style)
  14835. (list arg2 arg1 arg3)
  14836. (list arg1 arg2 arg3)))))
  14837. (defun org-eval-in-calendar (form &optional keepdate)
  14838. "Eval FORM in the calendar window and return to current window.
  14839. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14840. otherwise stick to the current value of `org-ans2'."
  14841. (let ((sf (selected-frame))
  14842. (sw (selected-window)))
  14843. (select-window (get-buffer-window "*Calendar*" t))
  14844. (eval form)
  14845. (when (and (not keepdate) (calendar-cursor-to-date))
  14846. (let* ((date (calendar-cursor-to-date))
  14847. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14848. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14849. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14850. (select-window sw)
  14851. (org-select-frame-set-input-focus sf)))
  14852. (defun org-calendar-select ()
  14853. "Return to `org-read-date' with the date currently selected.
  14854. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14855. (interactive)
  14856. (when (calendar-cursor-to-date)
  14857. (let* ((date (calendar-cursor-to-date))
  14858. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14859. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14860. (if (active-minibuffer-window) (exit-minibuffer))))
  14861. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14862. "Insert a date stamp for the date given by the internal TIME.
  14863. WITH-HM means use the stamp format that includes the time of the day.
  14864. INACTIVE means use square brackets instead of angular ones, so that the
  14865. stamp will not contribute to the agenda.
  14866. PRE and POST are optional strings to be inserted before and after the
  14867. stamp.
  14868. The command returns the inserted time stamp."
  14869. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14870. stamp)
  14871. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14872. (insert-before-markers (or pre ""))
  14873. (when (listp extra)
  14874. (setq extra (car extra))
  14875. (if (and (stringp extra)
  14876. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14877. (setq extra (format "-%02d:%02d"
  14878. (string-to-number (match-string 1 extra))
  14879. (string-to-number (match-string 2 extra))))
  14880. (setq extra nil)))
  14881. (when extra
  14882. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14883. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14884. (insert-before-markers (or post ""))
  14885. (setq org-last-inserted-timestamp stamp)))
  14886. (defun org-toggle-time-stamp-overlays ()
  14887. "Toggle the use of custom time stamp formats."
  14888. (interactive)
  14889. (setq org-display-custom-times (not org-display-custom-times))
  14890. (unless org-display-custom-times
  14891. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14892. (while (setq p (next-single-property-change p 'display))
  14893. (if (and (get-text-property p 'display)
  14894. (eq (get-text-property p 'face) 'org-date))
  14895. (remove-text-properties
  14896. p (setq p (next-single-property-change p 'display))
  14897. '(display t))))
  14898. (set-buffer-modified-p bmp)))
  14899. (if (featurep 'xemacs)
  14900. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14901. (org-restart-font-lock)
  14902. (setq org-table-may-need-update t)
  14903. (if org-display-custom-times
  14904. (message "Time stamps are overlaid with custom format")
  14905. (message "Time stamp overlays removed")))
  14906. (defun org-display-custom-time (beg end)
  14907. "Overlay modified time stamp format over timestamp between BEG and END."
  14908. (let* ((ts (buffer-substring beg end))
  14909. t1 w1 with-hm tf time str w2 (off 0))
  14910. (save-match-data
  14911. (setq t1 (org-parse-time-string ts t))
  14912. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14913. (setq off (- (match-end 0) (match-beginning 0)))))
  14914. (setq end (- end off))
  14915. (setq w1 (- end beg)
  14916. with-hm (and (nth 1 t1) (nth 2 t1))
  14917. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14918. time (org-fix-decoded-time t1)
  14919. str (org-add-props
  14920. (format-time-string
  14921. (substring tf 1 -1) (apply 'encode-time time))
  14922. nil 'mouse-face 'highlight)
  14923. w2 (length str))
  14924. (if (not (= w2 w1))
  14925. (add-text-properties (1+ beg) (+ 2 beg)
  14926. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14927. (if (featurep 'xemacs)
  14928. (progn
  14929. (put-text-property beg end 'invisible t)
  14930. (put-text-property beg end 'end-glyph (make-glyph str)))
  14931. (put-text-property beg end 'display str))))
  14932. (defun org-translate-time (string)
  14933. "Translate all timestamps in STRING to custom format.
  14934. But do this only if the variable `org-display-custom-times' is set."
  14935. (when org-display-custom-times
  14936. (save-match-data
  14937. (let* ((start 0)
  14938. (re org-ts-regexp-both)
  14939. t1 with-hm inactive tf time str beg end)
  14940. (while (setq start (string-match re string start))
  14941. (setq beg (match-beginning 0)
  14942. end (match-end 0)
  14943. t1 (save-match-data
  14944. (org-parse-time-string (substring string beg end) t))
  14945. with-hm (and (nth 1 t1) (nth 2 t1))
  14946. inactive (equal (substring string beg (1+ beg)) "[")
  14947. tf (funcall (if with-hm 'cdr 'car)
  14948. org-time-stamp-custom-formats)
  14949. time (org-fix-decoded-time t1)
  14950. str (format-time-string
  14951. (concat
  14952. (if inactive "[" "<") (substring tf 1 -1)
  14953. (if inactive "]" ">"))
  14954. (apply 'encode-time time))
  14955. string (replace-match str t t string)
  14956. start (+ start (length str)))))))
  14957. string)
  14958. (defun org-fix-decoded-time (time)
  14959. "Set 0 instead of nil for the first 6 elements of time.
  14960. Don't touch the rest."
  14961. (let ((n 0))
  14962. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14963. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14964. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14965. "Difference between TIMESTAMP-STRING and now in days.
  14966. If SECONDS is non-nil, return the difference in seconds."
  14967. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14968. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14969. (funcall fdiff (current-time)))))
  14970. (defun org-deadline-close (timestamp-string &optional ndays)
  14971. "Is the time in TIMESTAMP-STRING close to the current date?"
  14972. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14973. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14974. (not (org-entry-is-done-p))))
  14975. (defun org-get-wdays (ts &optional delay zero-delay)
  14976. "Get the deadline lead time appropriate for timestring TS.
  14977. When DELAY is non-nil, get the delay time for scheduled items
  14978. instead of the deadline lead time. When ZERO-DELAY is non-nil
  14979. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  14980. don't try to find the delay cookie in the scheduled timestamp."
  14981. (let ((tv (if delay org-scheduled-delay-days
  14982. org-deadline-warning-days)))
  14983. (cond
  14984. ((or (and delay (< tv 0))
  14985. (and delay zero-delay (<= tv 0))
  14986. (and (not delay) (<= tv 0)))
  14987. ;; Enforce this value no matter what
  14988. (- tv))
  14989. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14990. ;; lead time is specified.
  14991. (floor (* (string-to-number (match-string 1 ts))
  14992. (cdr (assoc (match-string 2 ts)
  14993. '(("d" . 1) ("w" . 7)
  14994. ("m" . 30.4) ("y" . 365.25)
  14995. ("h" . 0.041667)))))))
  14996. ;; go for the default.
  14997. (t tv))))
  14998. (defun org-calendar-select-mouse (ev)
  14999. "Return to `org-read-date' with the date currently selected.
  15000. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15001. (interactive "e")
  15002. (mouse-set-point ev)
  15003. (when (calendar-cursor-to-date)
  15004. (let* ((date (calendar-cursor-to-date))
  15005. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15006. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15007. (if (active-minibuffer-window) (exit-minibuffer))))
  15008. (defun org-check-deadlines (ndays)
  15009. "Check if there are any deadlines due or past due.
  15010. A deadline is considered due if it happens within `org-deadline-warning-days'
  15011. days from today's date. If the deadline appears in an entry marked DONE,
  15012. it is not shown. The prefix arg NDAYS can be used to test that many
  15013. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15014. (interactive "P")
  15015. (let* ((org-warn-days
  15016. (cond
  15017. ((equal ndays '(4)) 100000)
  15018. (ndays (prefix-numeric-value ndays))
  15019. (t (abs org-deadline-warning-days))))
  15020. (case-fold-search nil)
  15021. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15022. (callback
  15023. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15024. (message "%d deadlines past-due or due within %d days"
  15025. (org-occur regexp nil callback)
  15026. org-warn-days)))
  15027. (defsubst org-re-timestamp (type)
  15028. "Return a regexp for timestamp TYPE.
  15029. Allowed values for TYPE are:
  15030. all: all timestamps
  15031. active: only active timestamps (<...>)
  15032. inactive: only inactive timestamps ([...])
  15033. scheduled: only scheduled timestamps
  15034. deadline: only deadline timestamps
  15035. When TYPE is nil, fall back on returning a regexp that matches
  15036. both scheduled and deadline timestamps."
  15037. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  15038. ((eq type 'active) org-ts-regexp)
  15039. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  15040. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  15041. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15042. ((eq type 'scheduled-or-deadline)
  15043. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  15044. (defun org-check-before-date (date)
  15045. "Check if there are deadlines or scheduled entries before DATE."
  15046. (interactive (list (org-read-date)))
  15047. (let ((case-fold-search nil)
  15048. (regexp (org-re-timestamp org-ts-type))
  15049. (callback
  15050. (lambda () (time-less-p
  15051. (org-time-string-to-time (match-string 1))
  15052. (org-time-string-to-time date)))))
  15053. (message "%d entries before %s"
  15054. (org-occur regexp nil callback) date)))
  15055. (defun org-check-after-date (date)
  15056. "Check if there are deadlines or scheduled entries after DATE."
  15057. (interactive (list (org-read-date)))
  15058. (let ((case-fold-search nil)
  15059. (regexp (org-re-timestamp org-ts-type))
  15060. (callback
  15061. (lambda () (not
  15062. (time-less-p
  15063. (org-time-string-to-time (match-string 1))
  15064. (org-time-string-to-time date))))))
  15065. (message "%d entries after %s"
  15066. (org-occur regexp nil callback) date)))
  15067. (defun org-check-dates-range (start-date end-date)
  15068. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15069. (interactive (list (org-read-date nil nil nil "Range starts")
  15070. (org-read-date nil nil nil "Range end")))
  15071. (let ((case-fold-search nil)
  15072. (regexp (org-re-timestamp org-ts-type))
  15073. (callback
  15074. (lambda ()
  15075. (let ((match (match-string 1)))
  15076. (and
  15077. (not (time-less-p
  15078. (org-time-string-to-time match)
  15079. (org-time-string-to-time start-date)))
  15080. (time-less-p
  15081. (org-time-string-to-time match)
  15082. (org-time-string-to-time end-date)))))))
  15083. (message "%d entries between %s and %s"
  15084. (org-occur regexp nil callback) start-date end-date)))
  15085. (defun org-evaluate-time-range (&optional to-buffer)
  15086. "Evaluate a time range by computing the difference between start and end.
  15087. Normally the result is just printed in the echo area, but with prefix arg
  15088. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  15089. If the time range is actually in a table, the result is inserted into the
  15090. next column.
  15091. For time difference computation, a year is assumed to be exactly 365
  15092. days in order to avoid rounding problems."
  15093. (interactive "P")
  15094. (or
  15095. (org-clock-update-time-maybe)
  15096. (save-excursion
  15097. (unless (org-at-date-range-p t)
  15098. (goto-char (point-at-bol))
  15099. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  15100. (if (not (org-at-date-range-p t))
  15101. (error "Not at a time-stamp range, and none found in current line")))
  15102. (let* ((ts1 (match-string 1))
  15103. (ts2 (match-string 2))
  15104. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  15105. (match-end (match-end 0))
  15106. (time1 (org-time-string-to-time ts1))
  15107. (time2 (org-time-string-to-time ts2))
  15108. (t1 (org-float-time time1))
  15109. (t2 (org-float-time time2))
  15110. (diff (abs (- t2 t1)))
  15111. (negative (< (- t2 t1) 0))
  15112. ;; (ys (floor (* 365 24 60 60)))
  15113. (ds (* 24 60 60))
  15114. (hs (* 60 60))
  15115. (fy "%dy %dd %02d:%02d")
  15116. (fy1 "%dy %dd")
  15117. (fd "%dd %02d:%02d")
  15118. (fd1 "%dd")
  15119. (fh "%02d:%02d")
  15120. y d h m align)
  15121. (if havetime
  15122. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15123. y 0
  15124. d (floor (/ diff ds)) diff (mod diff ds)
  15125. h (floor (/ diff hs)) diff (mod diff hs)
  15126. m (floor (/ diff 60)))
  15127. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15128. y 0
  15129. d (floor (+ (/ diff ds) 0.5))
  15130. h 0 m 0))
  15131. (if (not to-buffer)
  15132. (message "%s" (org-make-tdiff-string y d h m))
  15133. (if (org-at-table-p)
  15134. (progn
  15135. (goto-char match-end)
  15136. (setq align t)
  15137. (and (looking-at " *|") (goto-char (match-end 0))))
  15138. (goto-char match-end))
  15139. (if (looking-at
  15140. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15141. (replace-match ""))
  15142. (if negative (insert " -"))
  15143. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15144. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15145. (insert " " (format fh h m))))
  15146. (if align (org-table-align))
  15147. (message "Time difference inserted")))))
  15148. (defun org-make-tdiff-string (y d h m)
  15149. (let ((fmt "")
  15150. (l nil))
  15151. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15152. l (push y l)))
  15153. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15154. l (push d l)))
  15155. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15156. l (push h l)))
  15157. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15158. l (push m l)))
  15159. (apply 'format fmt (nreverse l))))
  15160. (defun org-time-string-to-time (s &optional buffer pos)
  15161. "Convert a timestamp string into internal time."
  15162. (condition-case errdata
  15163. (apply 'encode-time (org-parse-time-string s))
  15164. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15165. s (if (not (and buffer pos))
  15166. ""
  15167. (format " at %d in buffer `%s'" pos buffer))
  15168. (cdr errdata)))))
  15169. (defun org-time-string-to-seconds (s)
  15170. "Convert a timestamp string to a number of seconds."
  15171. (org-float-time (org-time-string-to-time s)))
  15172. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15173. "Convert a time stamp to an absolute day number.
  15174. If there is a specifier for a cyclic time stamp, get the closest
  15175. date to DAYNR.
  15176. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15177. The variable date is bound by the calendar when this is called."
  15178. (cond
  15179. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15180. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15181. daynr
  15182. (+ daynr 1000)))
  15183. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15184. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15185. (time-to-days (current-time))) (match-string 0 s)
  15186. prefer show-all))
  15187. (t (time-to-days
  15188. (condition-case errdata
  15189. (apply 'encode-time (org-parse-time-string s))
  15190. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15191. s (if (not (and buffer pos))
  15192. ""
  15193. (format " at %d in buffer `%s'" pos buffer))
  15194. (cdr errdata))))))))
  15195. (defun org-days-to-iso-week (days)
  15196. "Return the iso week number."
  15197. (require 'cal-iso)
  15198. (car (calendar-iso-from-absolute days)))
  15199. (defun org-small-year-to-year (year)
  15200. "Convert 2-digit years into 4-digit years.
  15201. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15202. The year 2000 cannot be abbreviated. Any year larger than 99
  15203. is returned unchanged."
  15204. (if (< year 38)
  15205. (setq year (+ 2000 year))
  15206. (if (< year 100)
  15207. (setq year (+ 1900 year))))
  15208. year)
  15209. (defun org-time-from-absolute (d)
  15210. "Return the time corresponding to date D.
  15211. D may be an absolute day number, or a calendar-type list (month day year)."
  15212. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15213. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15214. (defun org-calendar-holiday ()
  15215. "List of holidays, for Diary display in Org-mode."
  15216. (require 'holidays)
  15217. (let ((hl (funcall
  15218. (if (fboundp 'calendar-check-holidays)
  15219. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15220. (if hl (mapconcat 'identity hl "; "))))
  15221. (defun org-diary-sexp-entry (sexp entry date)
  15222. "Process a SEXP diary ENTRY for DATE."
  15223. (require 'diary-lib)
  15224. (let ((result (if calendar-debug-sexp
  15225. (let ((stack-trace-on-error t))
  15226. (eval (car (read-from-string sexp))))
  15227. (condition-case nil
  15228. (eval (car (read-from-string sexp)))
  15229. (error
  15230. (beep)
  15231. (message "Bad sexp at line %d in %s: %s"
  15232. (org-current-line)
  15233. (buffer-file-name) sexp)
  15234. (sleep-for 2))))))
  15235. (cond ((stringp result) (split-string result "; "))
  15236. ((and (consp result)
  15237. (not (consp (cdr result)))
  15238. (stringp (cdr result))) (cdr result))
  15239. ((and (consp result)
  15240. (stringp (car result))) result)
  15241. (result entry))))
  15242. (defun org-diary-to-ical-string (frombuf)
  15243. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15244. This uses the icalendar.el library."
  15245. (let* ((tmpdir (if (featurep 'xemacs)
  15246. (temp-directory)
  15247. temporary-file-directory))
  15248. (tmpfile (make-temp-name
  15249. (expand-file-name "orgics" tmpdir)))
  15250. buf rtn b e)
  15251. (with-current-buffer frombuf
  15252. (icalendar-export-region (point-min) (point-max) tmpfile)
  15253. (setq buf (find-buffer-visiting tmpfile))
  15254. (set-buffer buf)
  15255. (goto-char (point-min))
  15256. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15257. (setq b (match-beginning 0)))
  15258. (goto-char (point-max))
  15259. (if (re-search-backward "^END:VEVENT" nil t)
  15260. (setq e (match-end 0)))
  15261. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15262. (kill-buffer buf)
  15263. (delete-file tmpfile)
  15264. rtn))
  15265. (defun org-closest-date (start current change prefer show-all)
  15266. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15267. When PREFER is `past', return a date that is either CURRENT or past.
  15268. When PREFER is `future', return a date that is either CURRENT or future.
  15269. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15270. ;; Make the proper lists from the dates
  15271. (catch 'exit
  15272. (let ((a1 '(("h" . hour)
  15273. ("d" . day)
  15274. ("w" . week)
  15275. ("m" . month)
  15276. ("y" . year)))
  15277. (shour (nth 2 (org-parse-time-string start)))
  15278. dn dw sday cday n1 n2 n0
  15279. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15280. (setq start (org-date-to-gregorian start)
  15281. current (org-date-to-gregorian
  15282. (if show-all
  15283. current
  15284. (time-to-days (current-time))))
  15285. sday (calendar-absolute-from-gregorian start)
  15286. cday (calendar-absolute-from-gregorian current))
  15287. (if (<= cday sday) (throw 'exit sday))
  15288. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15289. (setq dn (string-to-number (match-string 1 change))
  15290. dw (cdr (assoc (match-string 2 change) a1)))
  15291. (error "Invalid change specifier: %s" change))
  15292. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15293. (cond
  15294. ((eq dw 'hour)
  15295. (let ((missing-hours
  15296. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15297. dn)))
  15298. (setq n1 (if (zerop missing-hours) cday
  15299. (- cday (1+ (floor (/ missing-hours 24)))))
  15300. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15301. ((eq dw 'day)
  15302. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15303. n2 (+ n1 dn)))
  15304. ((eq dw 'year)
  15305. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15306. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15307. (setq date1 (list m d y1)
  15308. n1 (calendar-absolute-from-gregorian date1)
  15309. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15310. n2 (calendar-absolute-from-gregorian date2)))
  15311. ((eq dw 'month)
  15312. ;; approx number of month between the two dates
  15313. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15314. ;; How often does dn fit in there?
  15315. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15316. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15317. m (+ m nm)
  15318. ny (floor (/ m 12))
  15319. y (+ y ny)
  15320. m (- m (* ny 12)))
  15321. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15322. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15323. (setq m2 (+ m dn) y2 y)
  15324. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15325. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15326. (while (<= n2 cday)
  15327. (setq n1 n2 m m2 y y2)
  15328. (setq m2 (+ m dn) y2 y)
  15329. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15330. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15331. ;; Make sure n1 is the earlier date
  15332. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15333. (if show-all
  15334. (cond
  15335. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15336. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15337. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15338. (cond
  15339. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15340. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15341. (t (if (= cday n1) n1 n2)))))))
  15342. (defun org-date-to-gregorian (date)
  15343. "Turn any specification of DATE into a Gregorian date for the calendar."
  15344. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15345. ((and (listp date) (= (length date) 3)) date)
  15346. ((stringp date)
  15347. (setq date (org-parse-time-string date))
  15348. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15349. ((listp date)
  15350. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15351. (defun org-parse-time-string (s &optional nodefault)
  15352. "Parse the standard Org-mode time string.
  15353. This should be a lot faster than the normal `parse-time-string'.
  15354. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15355. hour and minute fields will be nil if not given."
  15356. (cond ((string-match org-ts-regexp0 s)
  15357. (list 0
  15358. (if (or (match-beginning 8) (not nodefault))
  15359. (string-to-number (or (match-string 8 s) "0")))
  15360. (if (or (match-beginning 7) (not nodefault))
  15361. (string-to-number (or (match-string 7 s) "0")))
  15362. (string-to-number (match-string 4 s))
  15363. (string-to-number (match-string 3 s))
  15364. (string-to-number (match-string 2 s))
  15365. nil nil nil))
  15366. ((string-match "^<[^>]+>$" s)
  15367. (decode-time (seconds-to-time (org-matcher-time s))))
  15368. (t (error "Not a standard Org-mode time string: %s" s))))
  15369. (defun org-timestamp-up (&optional arg)
  15370. "Increase the date item at the cursor by one.
  15371. If the cursor is on the year, change the year. If it is on the month,
  15372. the day or the time, change that.
  15373. With prefix ARG, change by that many units."
  15374. (interactive "p")
  15375. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15376. (defun org-timestamp-down (&optional arg)
  15377. "Decrease the date item at the cursor by one.
  15378. If the cursor is on the year, change the year. If it is on the month,
  15379. the day or the time, change that.
  15380. With prefix ARG, change by that many units."
  15381. (interactive "p")
  15382. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15383. (defun org-timestamp-up-day (&optional arg)
  15384. "Increase the date in the time stamp by one day.
  15385. With prefix ARG, change that many days."
  15386. (interactive "p")
  15387. (if (and (not (org-at-timestamp-p t))
  15388. (org-at-heading-p))
  15389. (org-todo 'up)
  15390. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15391. (defun org-timestamp-down-day (&optional arg)
  15392. "Decrease the date in the time stamp by one day.
  15393. With prefix ARG, change that many days."
  15394. (interactive "p")
  15395. (if (and (not (org-at-timestamp-p t))
  15396. (org-at-heading-p))
  15397. (org-todo 'down)
  15398. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15399. (defun org-at-timestamp-p (&optional inactive-ok)
  15400. "Determine if the cursor is in or at a timestamp."
  15401. (interactive)
  15402. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15403. (pos (point))
  15404. (ans (or (looking-at tsr)
  15405. (save-excursion
  15406. (skip-chars-backward "^[<\n\r\t")
  15407. (if (> (point) (point-min)) (backward-char 1))
  15408. (and (looking-at tsr)
  15409. (> (- (match-end 0) pos) -1))))))
  15410. (and ans
  15411. (boundp 'org-ts-what)
  15412. (setq org-ts-what
  15413. (cond
  15414. ((= pos (match-beginning 0)) 'bracket)
  15415. ;; Point is considered to be "on the bracket" whether
  15416. ;; it's really on it or right after it.
  15417. ((= pos (1- (match-end 0))) 'bracket)
  15418. ((= pos (match-end 0)) 'after)
  15419. ((org-pos-in-match-range pos 2) 'year)
  15420. ((org-pos-in-match-range pos 3) 'month)
  15421. ((org-pos-in-match-range pos 7) 'hour)
  15422. ((org-pos-in-match-range pos 8) 'minute)
  15423. ((or (org-pos-in-match-range pos 4)
  15424. (org-pos-in-match-range pos 5)) 'day)
  15425. ((and (> pos (or (match-end 8) (match-end 5)))
  15426. (< pos (match-end 0)))
  15427. (- pos (or (match-end 8) (match-end 5))))
  15428. (t 'day))))
  15429. ans))
  15430. (defun org-toggle-timestamp-type ()
  15431. "Toggle the type (<active> or [inactive]) of a time stamp."
  15432. (interactive)
  15433. (when (org-at-timestamp-p t)
  15434. (let ((beg (match-beginning 0)) (end (match-end 0))
  15435. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15436. (save-excursion
  15437. (goto-char beg)
  15438. (while (re-search-forward "[][<>]" end t)
  15439. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15440. t t)))
  15441. (message "Timestamp is now %sactive"
  15442. (if (equal (char-after beg) ?<) "" "in")))))
  15443. (defvar org-clock-history) ; defined in org-clock.el
  15444. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15445. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  15446. "Change the date in the time stamp at point.
  15447. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15448. `year', `minute', `second'. If WHAT is not given, the cursor position
  15449. in the timestamp determines what will be changed.
  15450. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  15451. (let ((origin (point)) origin-cat
  15452. with-hm inactive
  15453. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15454. org-ts-what
  15455. extra rem
  15456. ts time time0 fixnext clrgx)
  15457. (if (not (org-at-timestamp-p t))
  15458. (error "Not at a timestamp"))
  15459. (if (and (not what) (eq org-ts-what 'bracket))
  15460. (org-toggle-timestamp-type)
  15461. ;; Point isn't on brackets. Remember the part of the time-stamp
  15462. ;; the point was in. Indeed, size of time-stamps may change,
  15463. ;; but point must be kept in the same category nonetheless.
  15464. (setq origin-cat org-ts-what)
  15465. (if (and (not what) (not (eq org-ts-what 'day))
  15466. org-display-custom-times
  15467. (get-text-property (point) 'display)
  15468. (not (get-text-property (1- (point)) 'display)))
  15469. (setq org-ts-what 'day))
  15470. (setq org-ts-what (or what org-ts-what)
  15471. inactive (= (char-after (match-beginning 0)) ?\[)
  15472. ts (match-string 0))
  15473. (replace-match "")
  15474. (when (string-match
  15475. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15476. ts)
  15477. (setq extra (match-string 1 ts))
  15478. (if suppress-tmp-delay
  15479. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  15480. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15481. (setq with-hm t))
  15482. (setq time0 (org-parse-time-string ts))
  15483. (when (and updown
  15484. (eq org-ts-what 'minute)
  15485. (not current-prefix-arg))
  15486. ;; This looks like s-up and s-down. Change by one rounding step.
  15487. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15488. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15489. (setcar (cdr time0) (+ (nth 1 time0)
  15490. (if (> n 0) (- rem) (- dm rem))))))
  15491. (setq time
  15492. (encode-time (or (car time0) 0)
  15493. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15494. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15495. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15496. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15497. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15498. (nthcdr 6 time0)))
  15499. (when (and (member org-ts-what '(hour minute))
  15500. extra
  15501. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15502. (setq extra (org-modify-ts-extra
  15503. extra
  15504. (if (eq org-ts-what 'hour) 2 5)
  15505. n dm)))
  15506. (when (integerp org-ts-what)
  15507. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15508. (if (eq what 'calendar)
  15509. (let ((cal-date (org-get-date-from-calendar)))
  15510. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15511. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15512. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15513. (setcar time0 (or (car time0) 0))
  15514. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15515. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15516. (setq time (apply 'encode-time time0))))
  15517. ;; Insert the new time-stamp, and ensure point stays in the same
  15518. ;; category as before (i.e. not after the last position in that
  15519. ;; category).
  15520. (let ((pos (point)))
  15521. ;; Stay before inserted string. `save-excursion' is of no use.
  15522. (setq org-last-changed-timestamp
  15523. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15524. (goto-char pos))
  15525. (save-match-data
  15526. (looking-at org-ts-regexp3)
  15527. (goto-char (cond
  15528. ;; `day' category ends before `hour' if any, or at
  15529. ;; the end of the day name.
  15530. ((eq origin-cat 'day)
  15531. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15532. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15533. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15534. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15535. ;; `year' and `month' have both fixed size: point
  15536. ;; couldn't have moved into another part.
  15537. (t origin))))
  15538. ;; Update clock if on a CLOCK line.
  15539. (org-clock-update-time-maybe)
  15540. ;; Maybe adjust the closest clock in `org-clock-history'
  15541. (when org-clock-adjust-closest
  15542. (if (not (and (org-at-clock-log-p)
  15543. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15544. org-clock-history))))))
  15545. (message "No clock to adjust")
  15546. (cond ((save-excursion ; fix previous clock?
  15547. (re-search-backward org-ts-regexp0 nil t)
  15548. (org-looking-back (concat org-clock-string " \\[")))
  15549. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15550. ((save-excursion ; fix next clock?
  15551. (re-search-backward org-ts-regexp0 nil t)
  15552. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15553. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15554. (save-window-excursion
  15555. ;; Find closest clock to point, adjust the previous/next one in history
  15556. (let* ((p (save-excursion (org-back-to-heading t)))
  15557. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15558. (clfixnth
  15559. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15560. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15561. (if (not clfixpos)
  15562. (message "No clock to adjust")
  15563. (save-excursion
  15564. (org-goto-marker-or-bmk clfixpos)
  15565. (org-show-subtree)
  15566. (when (re-search-forward clrgx nil t)
  15567. (goto-char (match-beginning 1))
  15568. (let (org-clock-adjust-closest)
  15569. (org-timestamp-change n org-ts-what updown))
  15570. (message "Clock adjusted in %s for heading: %s"
  15571. (file-name-nondirectory (buffer-file-name))
  15572. (org-get-heading t t)))))))))
  15573. ;; Try to recenter the calendar window, if any.
  15574. (if (and org-calendar-follow-timestamp-change
  15575. (get-buffer-window "*Calendar*" t)
  15576. (memq org-ts-what '(day month year)))
  15577. (org-recenter-calendar (time-to-days time))))))
  15578. (defun org-modify-ts-extra (s pos n dm)
  15579. "Change the different parts of the lead-time and repeat fields in timestamp."
  15580. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15581. ng h m new rem)
  15582. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15583. (cond
  15584. ((or (org-pos-in-match-range pos 2)
  15585. (org-pos-in-match-range pos 3))
  15586. (setq m (string-to-number (match-string 3 s))
  15587. h (string-to-number (match-string 2 s)))
  15588. (if (org-pos-in-match-range pos 2)
  15589. (setq h (+ h n))
  15590. (setq n (* dm (org-no-warnings (signum n))))
  15591. (when (not (= 0 (setq rem (% m dm))))
  15592. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15593. (setq m (+ m n)))
  15594. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15595. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15596. (setq h (min 24 (max 0 h)))
  15597. (setq ng 1 new (format "-%02d:%02d" h m)))
  15598. ((org-pos-in-match-range pos 6)
  15599. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15600. ((org-pos-in-match-range pos 5)
  15601. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15602. ((org-pos-in-match-range pos 9)
  15603. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15604. ((org-pos-in-match-range pos 8)
  15605. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15606. (when ng
  15607. (setq s (concat
  15608. (substring s 0 (match-beginning ng))
  15609. new
  15610. (substring s (match-end ng))))))
  15611. s))
  15612. (defun org-recenter-calendar (date)
  15613. "If the calendar is visible, recenter it to DATE."
  15614. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15615. (when cwin
  15616. (let ((calendar-move-hook nil))
  15617. (with-selected-window cwin
  15618. (calendar-goto-date (if (listp date) date
  15619. (calendar-gregorian-from-absolute date))))))))
  15620. (defun org-goto-calendar (&optional arg)
  15621. "Go to the Emacs calendar at the current date.
  15622. If there is a time stamp in the current line, go to that date.
  15623. A prefix ARG can be used to force the current date."
  15624. (interactive "P")
  15625. (let ((tsr org-ts-regexp) diff
  15626. (calendar-move-hook nil)
  15627. (calendar-view-holidays-initially-flag nil)
  15628. (calendar-view-diary-initially-flag nil))
  15629. (if (or (org-at-timestamp-p)
  15630. (save-excursion
  15631. (beginning-of-line 1)
  15632. (looking-at (concat ".*" tsr))))
  15633. (let ((d1 (time-to-days (current-time)))
  15634. (d2 (time-to-days
  15635. (org-time-string-to-time (match-string 1)))))
  15636. (setq diff (- d2 d1))))
  15637. (calendar)
  15638. (calendar-goto-today)
  15639. (if (and diff (not arg)) (calendar-forward-day diff))))
  15640. (defun org-get-date-from-calendar ()
  15641. "Return a list (month day year) of date at point in calendar."
  15642. (with-current-buffer "*Calendar*"
  15643. (save-match-data
  15644. (calendar-cursor-to-date))))
  15645. (defun org-date-from-calendar ()
  15646. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15647. If there is already a time stamp at the cursor position, update it."
  15648. (interactive)
  15649. (if (org-at-timestamp-p t)
  15650. (org-timestamp-change 0 'calendar)
  15651. (let ((cal-date (org-get-date-from-calendar)))
  15652. (org-insert-time-stamp
  15653. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15654. (defcustom org-effort-durations
  15655. `(("h" . 60)
  15656. ("d" . ,(* 60 8))
  15657. ("w" . ,(* 60 8 5))
  15658. ("m" . ,(* 60 8 5 4))
  15659. ("y" . ,(* 60 8 5 40)))
  15660. "Conversion factor to minutes for an effort modifier.
  15661. Each entry has the form (MODIFIER . MINUTES).
  15662. In an effort string, a number followed by MODIFIER is multiplied
  15663. by the specified number of MINUTES to obtain an effort in
  15664. minutes.
  15665. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15666. effort string \"2hours\" is equivalent to 120 minutes."
  15667. :group 'org-agenda
  15668. :version "24.1"
  15669. :type '(alist :key-type (string :tag "Modifier")
  15670. :value-type (number :tag "Minutes")))
  15671. (defun org-minutes-to-clocksum-string (m)
  15672. "Format number of minutes as a clocksum string.
  15673. The format is determined by `org-time-clocksum-format',
  15674. `org-time-clocksum-use-fractional' and
  15675. `org-time-clocksum-fractional-format' and
  15676. `org-time-clocksum-use-effort-durations'."
  15677. (let ((clocksum "") h d w mo y fmt n)
  15678. (setq h (if org-time-clocksum-use-effort-durations
  15679. (cdr (assoc "h" org-effort-durations)) 60)
  15680. d (if org-time-clocksum-use-effort-durations
  15681. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15682. w (if org-time-clocksum-use-effort-durations
  15683. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15684. mo (if org-time-clocksum-use-effort-durations
  15685. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15686. y (if org-time-clocksum-use-effort-durations
  15687. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15688. ;; fractional format
  15689. (if org-time-clocksum-use-fractional
  15690. (cond
  15691. ;; single format string
  15692. ((stringp org-time-clocksum-fractional-format)
  15693. (format org-time-clocksum-fractional-format (/ m (float h))))
  15694. ;; choice of fractional formats for different time units
  15695. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15696. (> (/ (truncate m) (* y d h)) 0))
  15697. (format fmt (/ m (* y d (float h)))))
  15698. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15699. (> (/ (truncate m) (* mo d h)) 0))
  15700. (format fmt (/ m (* mo d (float h)))))
  15701. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15702. (> (/ (truncate m) (* w d h)) 0))
  15703. (format fmt (/ m (* w d (float h)))))
  15704. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15705. (> (/ (truncate m) (* d h)) 0))
  15706. (format fmt (/ m (* d (float h)))))
  15707. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15708. (> (/ (truncate m) h) 0))
  15709. (format fmt (/ m (float h))))
  15710. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15711. (format fmt m))
  15712. ;; fall back to smallest time unit with a format
  15713. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15714. (format fmt (/ m (float h))))
  15715. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15716. (format fmt (/ m (* d (float h)))))
  15717. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15718. (format fmt (/ m (* w d (float h)))))
  15719. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15720. (format fmt (/ m (* mo d (float h)))))
  15721. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15722. (format fmt (/ m (* y d (float h))))))
  15723. ;; standard (non-fractional) format, with single format string
  15724. (if (stringp org-time-clocksum-format)
  15725. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15726. ;; separate formats components
  15727. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15728. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15729. (plist-get org-time-clocksum-format :require-years))
  15730. (setq clocksum (concat clocksum (format fmt n))
  15731. m (- m (* n y d h))))
  15732. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15733. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15734. (plist-get org-time-clocksum-format :require-months))
  15735. (setq clocksum (concat clocksum (format fmt n))
  15736. m (- m (* n mo d h))))
  15737. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15738. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15739. (plist-get org-time-clocksum-format :require-weeks))
  15740. (setq clocksum (concat clocksum (format fmt n))
  15741. m (- m (* n w d h))))
  15742. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15743. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15744. (plist-get org-time-clocksum-format :require-days))
  15745. (setq clocksum (concat clocksum (format fmt n))
  15746. m (- m (* n d h))))
  15747. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15748. (or (> (setq n (/ (truncate m) h)) 0)
  15749. (plist-get org-time-clocksum-format :require-hours))
  15750. (setq clocksum (concat clocksum (format fmt n))
  15751. m (- m (* n h))))
  15752. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15753. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15754. (setq clocksum (concat clocksum (format fmt m))))
  15755. ;; return formatted time duration
  15756. clocksum))))
  15757. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15758. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15759. "Org mode version 8.0")
  15760. (defun org-hours-to-clocksum-string (n)
  15761. (org-minutes-to-clocksum-string (* n 60)))
  15762. (defun org-hh:mm-string-to-minutes (s)
  15763. "Convert a string H:MM to a number of minutes.
  15764. If the string is just a number, interpret it as minutes.
  15765. In fact, the first hh:mm or number in the string will be taken,
  15766. there can be extra stuff in the string.
  15767. If no number is found, the return value is 0."
  15768. (cond
  15769. ((integerp s) s)
  15770. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15771. (+ (* (string-to-number (match-string 1 s)) 60)
  15772. (string-to-number (match-string 2 s))))
  15773. ((string-match "\\([0-9]+\\)" s)
  15774. (string-to-number (match-string 1 s)))
  15775. (t 0)))
  15776. (defcustom org-image-actual-width t
  15777. "Should we use the actual width of images when inlining them?
  15778. When set to `t', always use the image width.
  15779. When set to a number, use imagemagick (when available) to set
  15780. the image's width to this value.
  15781. When set to a number in a list, try to get the width from the
  15782. #+ATTR.* keyword if it matches a width specification like
  15783. width=\"[0-9]+\", and fall back on that number if none is found.
  15784. When set to nil, try to get the width from an #+ATTR.* keyword
  15785. and fall back on the original width if none is found.
  15786. This requires Emacs >= 24.1, build with imagemagick support."
  15787. :group 'org-appearance
  15788. :version "24.3"
  15789. :type '(choice
  15790. (const :tag "Use the image width" t)
  15791. (integer :tag "Use a number of pixels")
  15792. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15793. (const :tag "Use #+ATTR* or don't resize" nil)))
  15794. (defcustom org-agenda-inhibit-startup t
  15795. "Inhibit startup when preparing agenda buffers.
  15796. When this variable is `t' (the default), the initialization of
  15797. the Org agenda buffers is inhibited: e.g. the visibility state
  15798. is not set, the tables are not re-aligned, etc."
  15799. :type 'boolean
  15800. :version "24.3"
  15801. :group 'org-agenda)
  15802. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15803. "Convert a duration string S to minutes.
  15804. A bare number is interpreted as minutes, modifiers can be set by
  15805. customizing `org-effort-durations' (which see).
  15806. Entries containing a colon are interpreted as H:MM by
  15807. `org-hh:mm-string-to-minutes'."
  15808. (let ((result 0)
  15809. (re (concat "\\([0-9.]+\\) *\\("
  15810. (regexp-opt (mapcar 'car org-effort-durations))
  15811. "\\)")))
  15812. (while (string-match re s)
  15813. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15814. (string-to-number (match-string 1 s))))
  15815. (setq s (replace-match "" nil t s)))
  15816. (setq result (floor result))
  15817. (incf result (org-hh:mm-string-to-minutes s))
  15818. (if output-to-string (number-to-string result) result)))
  15819. ;;;; Files
  15820. (defun org-save-all-org-buffers ()
  15821. "Save all Org-mode buffers without user confirmation."
  15822. (interactive)
  15823. (message "Saving all Org-mode buffers...")
  15824. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15825. (when (featurep 'org-id) (org-id-locations-save))
  15826. (message "Saving all Org-mode buffers... done"))
  15827. (defun org-revert-all-org-buffers ()
  15828. "Revert all Org-mode buffers.
  15829. Prompt for confirmation when there are unsaved changes.
  15830. Be sure you know what you are doing before letting this function
  15831. overwrite your changes.
  15832. This function is useful in a setup where one tracks org files
  15833. with a version control system, to revert on one machine after pulling
  15834. changes from another. I believe the procedure must be like this:
  15835. 1. M-x org-save-all-org-buffers
  15836. 2. Pull changes from the other machine, resolve conflicts
  15837. 3. M-x org-revert-all-org-buffers"
  15838. (interactive)
  15839. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15840. (error "Abort"))
  15841. (save-excursion
  15842. (save-window-excursion
  15843. (mapc
  15844. (lambda (b)
  15845. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15846. (with-current-buffer b buffer-file-name))
  15847. (org-pop-to-buffer-same-window b)
  15848. (revert-buffer t 'no-confirm)))
  15849. (buffer-list))
  15850. (when (and (featurep 'org-id) org-id-track-globally)
  15851. (org-id-locations-load)))))
  15852. ;;;; Agenda files
  15853. ;;;###autoload
  15854. (defun org-switchb (&optional arg)
  15855. "Switch between Org buffers.
  15856. With one prefix argument, restrict available buffers to files.
  15857. With two prefix arguments, restrict available buffers to agenda files.
  15858. Defaults to `iswitchb' for buffer name completion.
  15859. Set `org-completion-use-ido' to make it use ido instead."
  15860. (interactive "P")
  15861. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15862. ((equal arg '(16)) (org-buffer-list 'agenda))
  15863. (t (org-buffer-list))))
  15864. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15865. (org-completion-use-ido org-completion-use-ido))
  15866. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15867. (setq org-completion-use-iswitchb t))
  15868. (org-pop-to-buffer-same-window
  15869. (org-icompleting-read "Org buffer: "
  15870. (mapcar 'list (mapcar 'buffer-name blist))
  15871. nil t))))
  15872. ;;; Define some older names previously used for this functionality
  15873. ;;;###autoload
  15874. (defalias 'org-ido-switchb 'org-switchb)
  15875. ;;;###autoload
  15876. (defalias 'org-iswitchb 'org-switchb)
  15877. (defun org-buffer-list (&optional predicate exclude-tmp)
  15878. "Return a list of Org buffers.
  15879. PREDICATE can be `export', `files' or `agenda'.
  15880. export restrict the list to Export buffers.
  15881. files restrict the list to buffers visiting Org files.
  15882. agenda restrict the list to buffers visiting agenda files.
  15883. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15884. (let* ((bfn nil)
  15885. (agenda-files (and (eq predicate 'agenda)
  15886. (mapcar 'file-truename (org-agenda-files t))))
  15887. (filter
  15888. (cond
  15889. ((eq predicate 'files)
  15890. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15891. ((eq predicate 'export)
  15892. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15893. ((eq predicate 'agenda)
  15894. (lambda (b)
  15895. (with-current-buffer b
  15896. (and (derived-mode-p 'org-mode)
  15897. (setq bfn (buffer-file-name b))
  15898. (member (file-truename bfn) agenda-files)))))
  15899. (t (lambda (b) (with-current-buffer b
  15900. (or (derived-mode-p 'org-mode)
  15901. (string-match "\*Org .*Export"
  15902. (buffer-name b)))))))))
  15903. (delq nil
  15904. (mapcar
  15905. (lambda(b)
  15906. (if (and (funcall filter b)
  15907. (or (not exclude-tmp)
  15908. (not (string-match "tmp" (buffer-name b)))))
  15909. b
  15910. nil))
  15911. (buffer-list)))))
  15912. (defun org-agenda-files (&optional unrestricted archives)
  15913. "Get the list of agenda files.
  15914. Optional UNRESTRICTED means return the full list even if a restriction
  15915. is currently in place.
  15916. When ARCHIVES is t, include all archive files that are really being
  15917. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15918. `org-agenda-archives-mode' is t."
  15919. (let ((files
  15920. (cond
  15921. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15922. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15923. ((listp org-agenda-files) org-agenda-files)
  15924. (t (error "Invalid value of `org-agenda-files'")))))
  15925. (setq files (apply 'append
  15926. (mapcar (lambda (f)
  15927. (if (file-directory-p f)
  15928. (directory-files
  15929. f t org-agenda-file-regexp)
  15930. (list f)))
  15931. files)))
  15932. (when org-agenda-skip-unavailable-files
  15933. (setq files (delq nil
  15934. (mapcar (function
  15935. (lambda (file)
  15936. (and (file-readable-p file) file)))
  15937. files))))
  15938. (when (or (eq archives t)
  15939. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15940. (setq files (org-add-archive-files files)))
  15941. files))
  15942. (defun org-agenda-file-p (&optional file)
  15943. "Return non-nil, if FILE is an agenda file.
  15944. If FILE is omitted, use the file associated with the current
  15945. buffer."
  15946. (member (or file (buffer-file-name))
  15947. (org-agenda-files t)))
  15948. (defun org-edit-agenda-file-list ()
  15949. "Edit the list of agenda files.
  15950. Depending on setup, this either uses customize to edit the variable
  15951. `org-agenda-files', or it visits the file that is holding the list. In the
  15952. latter case, the buffer is set up in a way that saving it automatically kills
  15953. the buffer and restores the previous window configuration."
  15954. (interactive)
  15955. (if (stringp org-agenda-files)
  15956. (let ((cw (current-window-configuration)))
  15957. (find-file org-agenda-files)
  15958. (org-set-local 'org-window-configuration cw)
  15959. (org-add-hook 'after-save-hook
  15960. (lambda ()
  15961. (set-window-configuration
  15962. (prog1 org-window-configuration
  15963. (kill-buffer (current-buffer))))
  15964. (org-install-agenda-files-menu)
  15965. (message "New agenda file list installed"))
  15966. nil 'local)
  15967. (message "%s" (substitute-command-keys
  15968. "Edit list and finish with \\[save-buffer]")))
  15969. (customize-variable 'org-agenda-files)))
  15970. (defun org-store-new-agenda-file-list (list)
  15971. "Set new value for the agenda file list and save it correctly."
  15972. (if (stringp org-agenda-files)
  15973. (let ((fe (org-read-agenda-file-list t)) b u)
  15974. (while (setq b (find-buffer-visiting org-agenda-files))
  15975. (kill-buffer b))
  15976. (with-temp-file org-agenda-files
  15977. (insert
  15978. (mapconcat
  15979. (lambda (f) ;; Keep un-expanded entries.
  15980. (if (setq u (assoc f fe))
  15981. (cdr u)
  15982. f))
  15983. list "\n")
  15984. "\n")))
  15985. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15986. (org-insert-mode-line-in-empty-file nil))
  15987. (setq org-agenda-files list)
  15988. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15989. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15990. "Read the list of agenda files from a file.
  15991. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15992. filenames, used by `org-store-new-agenda-file-list' to write back
  15993. un-expanded file names."
  15994. (when (file-directory-p org-agenda-files)
  15995. (error "`org-agenda-files' cannot be a single directory"))
  15996. (when (stringp org-agenda-files)
  15997. (with-temp-buffer
  15998. (insert-file-contents org-agenda-files)
  15999. (mapcar
  16000. (lambda (f)
  16001. (let ((e (expand-file-name (substitute-in-file-name f)
  16002. org-directory)))
  16003. (if pair-with-expansion
  16004. (cons e f)
  16005. e)))
  16006. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16007. ;;;###autoload
  16008. (defun org-cycle-agenda-files ()
  16009. "Cycle through the files in `org-agenda-files'.
  16010. If the current buffer visits an agenda file, find the next one in the list.
  16011. If the current buffer does not, find the first agenda file."
  16012. (interactive)
  16013. (let* ((fs (org-agenda-files t))
  16014. (files (append fs (list (car fs))))
  16015. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  16016. file)
  16017. (unless files (error "No agenda files"))
  16018. (catch 'exit
  16019. (while (setq file (pop files))
  16020. (if (equal (file-truename file) tcf)
  16021. (when (car files)
  16022. (find-file (car files))
  16023. (throw 'exit t))))
  16024. (find-file (car fs)))
  16025. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16026. (defun org-agenda-file-to-front (&optional to-end)
  16027. "Move/add the current file to the top of the agenda file list.
  16028. If the file is not present in the list, it is added to the front. If it is
  16029. present, it is moved there. With optional argument TO-END, add/move to the
  16030. end of the list."
  16031. (interactive "P")
  16032. (let ((org-agenda-skip-unavailable-files nil)
  16033. (file-alist (mapcar (lambda (x)
  16034. (cons (file-truename x) x))
  16035. (org-agenda-files t)))
  16036. (ctf (file-truename
  16037. (or buffer-file-name
  16038. (error "Please save the current buffer to a file"))))
  16039. x had)
  16040. (setq x (assoc ctf file-alist) had x)
  16041. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  16042. (if to-end
  16043. (setq file-alist (append (delq x file-alist) (list x)))
  16044. (setq file-alist (cons x (delq x file-alist))))
  16045. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  16046. (org-install-agenda-files-menu)
  16047. (message "File %s to %s of agenda file list"
  16048. (if had "moved" "added") (if to-end "end" "front"))))
  16049. (defun org-remove-file (&optional file)
  16050. "Remove current file from the list of files in variable `org-agenda-files'.
  16051. These are the files which are being checked for agenda entries.
  16052. Optional argument FILE means use this file instead of the current."
  16053. (interactive)
  16054. (let* ((org-agenda-skip-unavailable-files nil)
  16055. (file (or file buffer-file-name
  16056. (error "Current buffer does not visit a file")))
  16057. (true-file (file-truename file))
  16058. (afile (abbreviate-file-name file))
  16059. (files (delq nil (mapcar
  16060. (lambda (x)
  16061. (if (equal true-file
  16062. (file-truename x))
  16063. nil x))
  16064. (org-agenda-files t)))))
  16065. (if (not (= (length files) (length (org-agenda-files t))))
  16066. (progn
  16067. (org-store-new-agenda-file-list files)
  16068. (org-install-agenda-files-menu)
  16069. (message "Removed file: %s" afile))
  16070. (message "File was not in list: %s (not removed)" afile))))
  16071. (defun org-file-menu-entry (file)
  16072. (vector file (list 'find-file file) t))
  16073. (defun org-check-agenda-file (file)
  16074. "Make sure FILE exists. If not, ask user what to do."
  16075. (when (not (file-exists-p file))
  16076. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  16077. (abbreviate-file-name file))
  16078. (let ((r (downcase (read-char-exclusive))))
  16079. (cond
  16080. ((equal r ?r)
  16081. (org-remove-file file)
  16082. (throw 'nextfile t))
  16083. (t (error "Abort"))))))
  16084. (defun org-get-agenda-file-buffer (file)
  16085. "Get a buffer visiting FILE. If the buffer needs to be created, add
  16086. it to the list of buffers which might be released later."
  16087. (let ((buf (org-find-base-buffer-visiting file)))
  16088. (if buf
  16089. buf ; just return it
  16090. ;; Make a new buffer and remember it
  16091. (setq buf (find-file-noselect file))
  16092. (if buf (push buf org-agenda-new-buffers))
  16093. buf)))
  16094. (defun org-release-buffers (blist)
  16095. "Release all buffers in list, asking the user for confirmation when needed.
  16096. When a buffer is unmodified, it is just killed. When modified, it is saved
  16097. \(if the user agrees) and then killed."
  16098. (let (buf file)
  16099. (while (setq buf (pop blist))
  16100. (setq file (buffer-file-name buf))
  16101. (when (and (buffer-modified-p buf)
  16102. file
  16103. (y-or-n-p (format "Save file %s? " file)))
  16104. (with-current-buffer buf (save-buffer)))
  16105. (kill-buffer buf))))
  16106. (defun org-agenda-prepare-buffers (files)
  16107. "Create buffers for all agenda files, protect archived trees and comments."
  16108. (interactive)
  16109. (let ((pa '(:org-archived t))
  16110. (pc '(:org-comment t))
  16111. (pall '(:org-archived t :org-comment t))
  16112. (inhibit-read-only t)
  16113. (org-inhibit-startup org-agenda-inhibit-startup)
  16114. (rea (concat ":" org-archive-tag ":"))
  16115. file re)
  16116. (save-excursion
  16117. (save-restriction
  16118. (while (setq file (pop files))
  16119. (catch 'nextfile
  16120. (if (bufferp file)
  16121. (set-buffer file)
  16122. (org-check-agenda-file file)
  16123. (set-buffer (org-get-agenda-file-buffer file)))
  16124. (widen)
  16125. (org-unmodified
  16126. (org-refresh-category-properties)
  16127. (org-refresh-properties org-effort-property 'org-effort)
  16128. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16129. (setq org-todo-keywords-for-agenda
  16130. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16131. (setq org-done-keywords-for-agenda
  16132. (append org-done-keywords-for-agenda org-done-keywords))
  16133. (setq org-todo-keyword-alist-for-agenda
  16134. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16135. (setq org-drawers-for-agenda
  16136. (append org-drawers-for-agenda org-drawers))
  16137. (setq org-tag-alist-for-agenda
  16138. (append org-tag-alist-for-agenda org-tag-alist))
  16139. (save-excursion
  16140. (remove-text-properties (point-min) (point-max) pall)
  16141. (when org-agenda-skip-archived-trees
  16142. (goto-char (point-min))
  16143. (while (re-search-forward rea nil t)
  16144. (if (org-at-heading-p t)
  16145. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16146. (goto-char (point-min))
  16147. (setq re (format org-heading-keyword-regexp-format
  16148. org-comment-string))
  16149. (while (re-search-forward re nil t)
  16150. (add-text-properties
  16151. (match-beginning 0) (org-end-of-subtree t) pc))))))))
  16152. (setq org-todo-keywords-for-agenda
  16153. (org-uniquify org-todo-keywords-for-agenda))
  16154. (setq org-todo-keyword-alist-for-agenda
  16155. (org-uniquify org-todo-keyword-alist-for-agenda)
  16156. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16157. ;;;; CDLaTeX minor mode
  16158. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16159. "Keymap for the minor `org-cdlatex-mode'.")
  16160. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16161. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16162. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16163. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16164. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16165. (defvar org-cdlatex-texmathp-advice-is-done nil
  16166. "Flag remembering if we have applied the advice to texmathp already.")
  16167. (define-minor-mode org-cdlatex-mode
  16168. "Toggle the minor `org-cdlatex-mode'.
  16169. This mode supports entering LaTeX environment and math in LaTeX fragments
  16170. in Org-mode.
  16171. \\{org-cdlatex-mode-map}"
  16172. nil " OCDL" nil
  16173. (when org-cdlatex-mode
  16174. (require 'cdlatex)
  16175. (run-hooks 'cdlatex-mode-hook)
  16176. (cdlatex-compute-tables))
  16177. (unless org-cdlatex-texmathp-advice-is-done
  16178. (setq org-cdlatex-texmathp-advice-is-done t)
  16179. (defadvice texmathp (around org-math-always-on activate)
  16180. "Always return t in org-mode buffers.
  16181. This is because we want to insert math symbols without dollars even outside
  16182. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16183. an embedded LaTeX fragment, let texmathp do its job.
  16184. \\[org-cdlatex-mode-map]"
  16185. (interactive)
  16186. (let (p)
  16187. (cond
  16188. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16189. ((eq this-command 'cdlatex-math-symbol)
  16190. (setq ad-return-value t
  16191. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16192. (t
  16193. (let ((p (org-inside-LaTeX-fragment-p)))
  16194. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16195. (setq ad-return-value t
  16196. texmathp-why '("Org-mode embedded math" . 0))
  16197. (if p ad-do-it)))))))))
  16198. (defun turn-on-org-cdlatex ()
  16199. "Unconditionally turn on `org-cdlatex-mode'."
  16200. (org-cdlatex-mode 1))
  16201. (defun org-try-cdlatex-tab ()
  16202. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16203. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16204. - inside a LaTeX fragment, or
  16205. - after the first word in a line, where an abbreviation expansion could
  16206. insert a LaTeX environment."
  16207. (when org-cdlatex-mode
  16208. (cond
  16209. ;; Before any word on the line: No expansion possible.
  16210. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16211. ;; Just after first word on the line: Expand it. Make sure it
  16212. ;; cannot happen on headlines, though.
  16213. ((save-excursion
  16214. (skip-chars-backward "a-zA-Z0-9*")
  16215. (skip-chars-backward " \t")
  16216. (and (bolp) (not (org-at-heading-p))))
  16217. (cdlatex-tab) t)
  16218. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16219. (defun org-cdlatex-underscore-caret (&optional arg)
  16220. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16221. Revert to the normal definition outside of these fragments."
  16222. (interactive "P")
  16223. (if (org-inside-LaTeX-fragment-p)
  16224. (call-interactively 'cdlatex-sub-superscript)
  16225. (let (org-cdlatex-mode)
  16226. (call-interactively (key-binding (vector last-input-event))))))
  16227. (defun org-cdlatex-math-modify (&optional arg)
  16228. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16229. Revert to the normal definition outside of these fragments."
  16230. (interactive "P")
  16231. (if (org-inside-LaTeX-fragment-p)
  16232. (call-interactively 'cdlatex-math-modify)
  16233. (let (org-cdlatex-mode)
  16234. (call-interactively (key-binding (vector last-input-event))))))
  16235. ;;;; LaTeX fragments
  16236. (defvar org-latex-regexps
  16237. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16238. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16239. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16240. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16241. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16242. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16243. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16244. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16245. "Regular expressions for matching embedded LaTeX.")
  16246. (defun org-inside-LaTeX-fragment-p ()
  16247. "Test if point is inside a LaTeX fragment.
  16248. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16249. sequence appearing also before point.
  16250. Even though the matchers for math are configurable, this function assumes
  16251. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16252. delimiters are skipped when they have been removed by customization.
  16253. The return value is nil, or a cons cell with the delimiter and the
  16254. position of this delimiter.
  16255. This function does a reasonably good job, but can locally be fooled by
  16256. for example currency specifications. For example it will assume being in
  16257. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16258. fragments that are properly closed, but during editing, we have to live
  16259. with the uncertainty caused by missing closing delimiters. This function
  16260. looks only before point, not after."
  16261. (catch 'exit
  16262. (let ((pos (point))
  16263. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16264. (lim (progn
  16265. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16266. (point)))
  16267. dd-on str (start 0) m re)
  16268. (goto-char pos)
  16269. (when dodollar
  16270. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16271. re (nth 1 (assoc "$" org-latex-regexps)))
  16272. (while (string-match re str start)
  16273. (cond
  16274. ((= (match-end 0) (length str))
  16275. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16276. ((= (match-end 0) (- (length str) 5))
  16277. (throw 'exit nil))
  16278. (t (setq start (match-end 0))))))
  16279. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16280. (goto-char pos)
  16281. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16282. (and (match-beginning 2) (throw 'exit nil))
  16283. ;; count $$
  16284. (while (re-search-backward "\\$\\$" lim t)
  16285. (setq dd-on (not dd-on)))
  16286. (goto-char pos)
  16287. (if dd-on (cons "$$" m))))))
  16288. (defun org-inside-latex-macro-p ()
  16289. "Is point inside a LaTeX macro or its arguments?"
  16290. (save-match-data
  16291. (org-in-regexp
  16292. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16293. (defvar org-latex-fragment-image-overlays nil
  16294. "List of overlays carrying the images of latex fragments.")
  16295. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16296. (defun org-remove-latex-fragment-image-overlays ()
  16297. "Remove all overlays with LaTeX fragment images in current buffer."
  16298. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16299. (setq org-latex-fragment-image-overlays nil))
  16300. (defun org-preview-latex-fragment (&optional subtree)
  16301. "Preview the LaTeX fragment at point, or all locally or globally.
  16302. If the cursor is in a LaTeX fragment, create the image and overlay
  16303. it over the source code. If there is no fragment at point, display
  16304. all fragments in the current text, from one headline to the next. With
  16305. prefix SUBTREE, display all fragments in the current subtree. With a
  16306. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16307. the cursor is before the first headline,
  16308. display all fragments in the buffer.
  16309. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16310. (interactive "P")
  16311. (unless buffer-file-name
  16312. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16313. (org-remove-latex-fragment-image-overlays)
  16314. (save-excursion
  16315. (save-restriction
  16316. (let (beg end at msg)
  16317. (cond
  16318. ((or (equal subtree '(16))
  16319. (not (save-excursion
  16320. (re-search-backward org-outline-regexp-bol nil t))))
  16321. (setq beg (point-min) end (point-max)
  16322. msg "Creating images for buffer...%s"))
  16323. ((equal subtree '(4))
  16324. (org-back-to-heading)
  16325. (setq beg (point) end (org-end-of-subtree t)
  16326. msg "Creating images for subtree...%s"))
  16327. (t
  16328. (if (setq at (org-inside-LaTeX-fragment-p))
  16329. (goto-char (max (point-min) (- (cdr at) 2)))
  16330. (org-back-to-heading))
  16331. (setq beg (point) end (progn (outline-next-heading) (point))
  16332. msg (if at "Creating image...%s"
  16333. "Creating images for entry...%s"))))
  16334. (message msg "")
  16335. (narrow-to-region beg end)
  16336. (goto-char beg)
  16337. (org-format-latex
  16338. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16339. (file-name-nondirectory
  16340. buffer-file-name)))
  16341. default-directory 'overlays msg at 'forbuffer
  16342. org-latex-create-formula-image-program)
  16343. (message msg "done. Use `C-c C-c' to remove images.")))))
  16344. (defun org-format-latex (prefix &optional dir overlays msg at
  16345. forbuffer processing-type)
  16346. "Replace LaTeX fragments with links to an image, and produce images.
  16347. Some of the options can be changed using the variable
  16348. `org-format-latex-options'."
  16349. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16350. (let* ((prefixnodir (file-name-nondirectory prefix))
  16351. (absprefix (expand-file-name prefix dir))
  16352. (todir (file-name-directory absprefix))
  16353. (opt org-format-latex-options)
  16354. (optnew org-format-latex-options)
  16355. (matchers (plist-get opt :matchers))
  16356. (re-list org-latex-regexps)
  16357. (cnt 0) txt hash link beg end re e checkdir
  16358. string
  16359. m n block-type block linkfile movefile ov)
  16360. ;; Check the different regular expressions
  16361. (while (setq e (pop re-list))
  16362. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16363. block (if block-type "\n\n" ""))
  16364. (when (member m matchers)
  16365. (goto-char (point-min))
  16366. (while (re-search-forward re nil t)
  16367. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16368. (or (not overlays)
  16369. (not (eq (get-char-property (match-beginning n)
  16370. 'org-overlay-type)
  16371. 'org-latex-overlay))))
  16372. (cond
  16373. ((eq processing-type 'verbatim))
  16374. ((eq processing-type 'mathjax)
  16375. ;; Prepare for MathJax processing.
  16376. (setq string (match-string n))
  16377. (when (member m '("$" "$1"))
  16378. (save-excursion
  16379. (delete-region (match-beginning n) (match-end n))
  16380. (goto-char (match-beginning n))
  16381. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16382. ((or (eq processing-type 'dvipng)
  16383. (eq processing-type 'imagemagick))
  16384. ;; Process to an image.
  16385. (setq txt (match-string n)
  16386. beg (match-beginning n) end (match-end n)
  16387. cnt (1+ cnt))
  16388. (let ((face (face-at-point))
  16389. (fg (plist-get opt :foreground))
  16390. (bg (plist-get opt :background))
  16391. ;; Ensure full list is printed.
  16392. print-length print-level)
  16393. (when forbuffer
  16394. ;; Get the colors from the face at point.
  16395. (goto-char beg)
  16396. (when (eq fg 'auto)
  16397. (setq fg (face-attribute face :foreground nil 'default)))
  16398. (when (eq bg 'auto)
  16399. (setq bg (face-attribute face :background nil 'default)))
  16400. (setq optnew (copy-sequence opt))
  16401. (plist-put optnew :foreground fg)
  16402. (plist-put optnew :background bg))
  16403. (setq hash (sha1 (prin1-to-string
  16404. (list org-format-latex-header
  16405. org-latex-default-packages-alist
  16406. org-latex-packages-alist
  16407. org-format-latex-options
  16408. forbuffer txt fg bg)))
  16409. linkfile (format "%s_%s.png" prefix hash)
  16410. movefile (format "%s_%s.png" absprefix hash)))
  16411. (setq link (concat block "[[file:" linkfile "]]" block))
  16412. (if msg (message msg cnt))
  16413. (goto-char beg)
  16414. (unless checkdir ; Ensure the directory exists.
  16415. (setq checkdir t)
  16416. (or (file-directory-p todir) (make-directory todir t)))
  16417. (unless (file-exists-p movefile)
  16418. (org-create-formula-image
  16419. txt movefile optnew forbuffer processing-type))
  16420. (if overlays
  16421. (progn
  16422. (mapc (lambda (o)
  16423. (if (eq (overlay-get o 'org-overlay-type)
  16424. 'org-latex-overlay)
  16425. (delete-overlay o)))
  16426. (overlays-in beg end))
  16427. (setq ov (make-overlay beg end))
  16428. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16429. (if (featurep 'xemacs)
  16430. (progn
  16431. (overlay-put ov 'invisible t)
  16432. (overlay-put
  16433. ov 'end-glyph
  16434. (make-glyph (vector 'png :file movefile))))
  16435. (overlay-put
  16436. ov 'display
  16437. (list 'image :type 'png :file movefile :ascent 'center)))
  16438. (push ov org-latex-fragment-image-overlays)
  16439. (goto-char end))
  16440. (delete-region beg end)
  16441. (insert (org-add-props link
  16442. (list 'org-latex-src
  16443. (replace-regexp-in-string
  16444. "\"" "" txt)
  16445. 'org-latex-src-embed-type
  16446. (if block-type 'paragraph 'character))))))
  16447. ((eq processing-type 'mathml)
  16448. ;; Process to MathML
  16449. (unless (save-match-data (org-format-latex-mathml-available-p))
  16450. (error "LaTeX to MathML converter not configured"))
  16451. (setq txt (match-string n)
  16452. beg (match-beginning n) end (match-end n)
  16453. cnt (1+ cnt))
  16454. (if msg (message msg cnt))
  16455. (goto-char beg)
  16456. (delete-region beg end)
  16457. (insert (org-format-latex-as-mathml
  16458. txt block-type prefix dir)))
  16459. (t
  16460. (error "Unknown conversion type %s for latex fragments"
  16461. processing-type)))))))))
  16462. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16463. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16464. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16465. sucessful, return the portion between \"<math...> </math>\"
  16466. elements otherwise return nil. When MATHML-FILE is specified,
  16467. write the results in to that file. When invoked as an
  16468. interactive command, prompt for LATEX-FRAG, with initial value
  16469. set to the current active region and echo the results for user
  16470. inspection."
  16471. (interactive (list (let ((frag (when (org-region-active-p)
  16472. (buffer-substring-no-properties
  16473. (region-beginning) (region-end)))))
  16474. (read-string "LaTeX Fragment: " frag nil frag))))
  16475. (unless latex-frag (error "Invalid latex-frag"))
  16476. (let* ((tmp-in-file (file-relative-name
  16477. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16478. (ignore (write-region latex-frag nil tmp-in-file))
  16479. (tmp-out-file (file-relative-name
  16480. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16481. (cmd (format-spec
  16482. org-latex-to-mathml-convert-command
  16483. `((?j . ,(shell-quote-argument
  16484. (expand-file-name org-latex-to-mathml-jar-file)))
  16485. (?I . ,(shell-quote-argument tmp-in-file))
  16486. (?o . ,(shell-quote-argument tmp-out-file)))))
  16487. mathml shell-command-output)
  16488. (when (org-called-interactively-p 'any)
  16489. (unless (org-format-latex-mathml-available-p)
  16490. (error "LaTeX to MathML converter not configured")))
  16491. (message "Running %s" cmd)
  16492. (setq shell-command-output (shell-command-to-string cmd))
  16493. (setq mathml
  16494. (when (file-readable-p tmp-out-file)
  16495. (with-current-buffer (find-file-noselect tmp-out-file t)
  16496. (goto-char (point-min))
  16497. (when (re-search-forward
  16498. (concat
  16499. (regexp-quote
  16500. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16501. "\\(.\\|\n\\)*"
  16502. (regexp-quote "</math>")) nil t)
  16503. (prog1 (match-string 0) (kill-buffer))))))
  16504. (cond
  16505. (mathml
  16506. (setq mathml
  16507. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16508. (when mathml-file
  16509. (write-region mathml nil mathml-file))
  16510. (when (org-called-interactively-p 'any)
  16511. (message mathml)))
  16512. ((message "LaTeX to MathML conversion failed")
  16513. (message shell-command-output)))
  16514. (delete-file tmp-in-file)
  16515. (when (file-exists-p tmp-out-file)
  16516. (delete-file tmp-out-file))
  16517. mathml))
  16518. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16519. prefix &optional dir)
  16520. "Use `org-create-math-formula' but check local cache first."
  16521. (let* ((absprefix (expand-file-name prefix dir))
  16522. (print-length nil) (print-level nil)
  16523. (formula-id (concat
  16524. "formula-"
  16525. (sha1
  16526. (prin1-to-string
  16527. (list latex-frag
  16528. org-latex-to-mathml-convert-command)))))
  16529. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16530. (formula-cache-dir (file-name-directory formula-cache)))
  16531. (unless (file-directory-p formula-cache-dir)
  16532. (make-directory formula-cache-dir t))
  16533. (unless (file-exists-p formula-cache)
  16534. (org-create-math-formula latex-frag formula-cache))
  16535. (if (file-exists-p formula-cache)
  16536. ;; Successful conversion. Return the link to MathML file.
  16537. (org-add-props
  16538. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16539. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16540. 'org-latex-src-embed-type (if latex-frag-type
  16541. 'paragraph 'character)))
  16542. ;; Failed conversion. Return the LaTeX fragment verbatim
  16543. latex-frag)))
  16544. (defun org-create-formula-image (string tofile options buffer &optional type)
  16545. "Create an image from LaTeX source using dvipng or convert.
  16546. This function calls either `org-create-formula-image-with-dvipng'
  16547. or `org-create-formula-image-with-imagemagick' depending on the
  16548. value of `org-latex-create-formula-image-program' or on the value
  16549. of the optional TYPE variable.
  16550. Note: ultimately these two function should be combined as they
  16551. share a good deal of logic."
  16552. (org-check-external-command
  16553. "latex" "needed to convert LaTeX fragments to images")
  16554. (funcall
  16555. (case (or type org-latex-create-formula-image-program)
  16556. ('dvipng
  16557. (org-check-external-command
  16558. "dvipng" "needed to convert LaTeX fragments to images")
  16559. #'org-create-formula-image-with-dvipng)
  16560. ('imagemagick
  16561. (org-check-external-command
  16562. "convert" "you need to install imagemagick")
  16563. #'org-create-formula-image-with-imagemagick)
  16564. (t (error
  16565. "invalid value of `org-latex-create-formula-image-program'")))
  16566. string tofile options buffer))
  16567. ;; This function borrows from Ganesh Swami's latex2png.el
  16568. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16569. "This calls dvipng."
  16570. (let* ((tmpdir (if (featurep 'xemacs)
  16571. (temp-directory)
  16572. temporary-file-directory))
  16573. (texfilebase (make-temp-name
  16574. (expand-file-name "orgtex" tmpdir)))
  16575. (texfile (concat texfilebase ".tex"))
  16576. (dvifile (concat texfilebase ".dvi"))
  16577. (pngfile (concat texfilebase ".png"))
  16578. (fnh (if (featurep 'xemacs)
  16579. (font-height (face-font 'default))
  16580. (face-attribute 'default :height nil)))
  16581. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16582. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16583. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16584. "Black"))
  16585. (bg (or (plist-get options (if buffer :background :html-background))
  16586. "Transparent")))
  16587. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16588. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16589. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16590. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16591. (with-temp-file texfile
  16592. (require 'ox-latex)
  16593. (insert (org-latex-guess-inputenc
  16594. (org-splice-latex-header
  16595. org-format-latex-header
  16596. org-latex-default-packages-alist
  16597. org-latex-packages-alist t)))
  16598. (insert "\n\\begin{document}\n" string "\n\\end{document}\n"))
  16599. (let ((dir default-directory))
  16600. (condition-case nil
  16601. (progn
  16602. (cd tmpdir)
  16603. (call-process "latex" nil nil nil texfile))
  16604. (error nil))
  16605. (cd dir))
  16606. (if (not (file-exists-p dvifile))
  16607. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16608. (condition-case nil
  16609. (if (featurep 'xemacs)
  16610. (call-process "dvipng" nil nil nil
  16611. "-fg" fg "-bg" bg
  16612. "-T" "tight"
  16613. "-o" pngfile
  16614. dvifile)
  16615. (call-process "dvipng" nil nil nil
  16616. "-fg" fg "-bg" bg
  16617. "-D" dpi
  16618. ;;"-x" scale "-y" scale
  16619. "-T" "tight"
  16620. "-o" pngfile
  16621. dvifile))
  16622. (error nil))
  16623. (if (not (file-exists-p pngfile))
  16624. (if org-format-latex-signal-error
  16625. (error "Failed to create png file from %s" texfile)
  16626. (message "Failed to create png file from %s" texfile)
  16627. nil)
  16628. ;; Use the requested file name and clean up
  16629. (copy-file pngfile tofile 'replace)
  16630. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16631. (if (file-exists-p (concat texfilebase e))
  16632. (delete-file (concat texfilebase e))))
  16633. pngfile))))
  16634. (defvar org-latex-pdf-process) ; From ox-latex.el
  16635. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16636. "This calls convert, which is included into imagemagick."
  16637. (let* ((tmpdir (if (featurep 'xemacs)
  16638. (temp-directory)
  16639. temporary-file-directory))
  16640. (texfilebase (make-temp-name
  16641. (expand-file-name "orgtex" tmpdir)))
  16642. (texfile (concat texfilebase ".tex"))
  16643. (pdffile (concat texfilebase ".pdf"))
  16644. (pngfile (concat texfilebase ".png"))
  16645. (fnh (if (featurep 'xemacs)
  16646. (font-height (face-font 'default))
  16647. (face-attribute 'default :height nil)))
  16648. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16649. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16650. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16651. "black"))
  16652. (bg (or (plist-get options (if buffer :background :html-background))
  16653. "white")))
  16654. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16655. (setq fg (org-latex-color-format fg)))
  16656. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16657. (setq bg (org-latex-color-format
  16658. (if (string= bg "Transparent") "white" bg))))
  16659. (with-temp-file texfile
  16660. (require 'ox-latex)
  16661. (insert (org-latex-guess-inputenc
  16662. (org-splice-latex-header
  16663. org-format-latex-header
  16664. org-latex-default-packages-alist
  16665. org-latex-packages-alist t)))
  16666. (insert "\n\\begin{document}\n"
  16667. "\\definecolor{fg}{rgb}{" fg "}\n"
  16668. "\\definecolor{bg}{rgb}{" bg "}\n"
  16669. "\n\\pagecolor{bg}\n"
  16670. "\n{\\color{fg}\n"
  16671. string
  16672. "\n}\n"
  16673. "\n\\end{document}\n"))
  16674. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16675. (condition-case nil
  16676. (progn
  16677. (cd tmpdir)
  16678. (setq cmds org-latex-pdf-process)
  16679. (while cmds
  16680. (setq latex-frags-cmds (pop cmds))
  16681. (if (listp latex-frags-cmds)
  16682. (setq cmds nil)
  16683. (setq latex-frags-cmds (list (car org-latex-pdf-process)))))
  16684. (while latex-frags-cmds
  16685. (setq cmd (pop latex-frags-cmds))
  16686. (while (string-match "%b" cmd)
  16687. (setq cmd (replace-match
  16688. (save-match-data
  16689. (shell-quote-argument texfile))
  16690. t t cmd)))
  16691. (while (string-match "%f" cmd)
  16692. (setq cmd (replace-match
  16693. (save-match-data
  16694. (shell-quote-argument
  16695. (file-name-nondirectory texfile)))
  16696. t t cmd)))
  16697. (while (string-match "%o" cmd)
  16698. (setq cmd (replace-match
  16699. (save-match-data
  16700. (shell-quote-argument
  16701. (file-name-directory texfile)))
  16702. t t cmd)))
  16703. (setq cmd (split-string cmd))
  16704. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16705. (error nil))
  16706. (cd dir))
  16707. (if (not (file-exists-p pdffile))
  16708. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16709. (condition-case nil
  16710. (if (featurep 'xemacs)
  16711. (call-process "convert" nil nil nil
  16712. "-density" "96"
  16713. "-trim"
  16714. "-antialias"
  16715. pdffile
  16716. "-quality" "100"
  16717. ;; "-sharpen" "0x1.0"
  16718. pngfile)
  16719. (call-process "convert" nil nil nil
  16720. "-density" dpi
  16721. "-trim"
  16722. "-antialias"
  16723. pdffile
  16724. "-quality" "100"
  16725. ;; "-sharpen" "0x1.0"
  16726. pngfile))
  16727. (error nil))
  16728. (if (not (file-exists-p pngfile))
  16729. (if org-format-latex-signal-error
  16730. (error "Failed to create png file from %s" texfile)
  16731. (message "Failed to create png file from %s" texfile)
  16732. nil)
  16733. ;; Use the requested file name and clean up
  16734. (copy-file pngfile tofile 'replace)
  16735. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16736. (if (file-exists-p (concat texfilebase e))
  16737. (delete-file (concat texfilebase e))))
  16738. pngfile))))
  16739. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16740. "Fill a LaTeX header template TPL.
  16741. In the template, the following place holders will be recognized:
  16742. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16743. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16744. [PACKAGES] \\usepackage statements for PKG
  16745. [NO-PACKAGES] do not include PKG
  16746. [EXTRA] the string EXTRA
  16747. [NO-EXTRA] do not include EXTRA
  16748. For backward compatibility, if both the positive and the negative place
  16749. holder is missing, the positive one (without the \"NO-\") will be
  16750. assumed to be present at the end of the template.
  16751. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16752. EXTRA is a string.
  16753. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16754. (let (rpl (end ""))
  16755. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16756. (setq rpl (if (or (match-end 1) (not def-pkg))
  16757. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16758. tpl (replace-match rpl t t tpl))
  16759. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16760. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16761. (setq rpl (if (or (match-end 1) (not pkg))
  16762. "" (org-latex-packages-to-string pkg snippets-p t))
  16763. tpl (replace-match rpl t t tpl))
  16764. (if pkg (setq end
  16765. (concat end "\n"
  16766. (org-latex-packages-to-string pkg snippets-p)))))
  16767. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16768. (setq rpl (if (or (match-end 1) (not extra))
  16769. "" (concat extra "\n"))
  16770. tpl (replace-match rpl t t tpl))
  16771. (if (and extra (string-match "\\S-" extra))
  16772. (setq end (concat end "\n" extra))))
  16773. (if (string-match "\\S-" end)
  16774. (concat tpl "\n" end)
  16775. tpl)))
  16776. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16777. "Turn an alist of packages into a string with the \\usepackage macros."
  16778. (setq pkg (mapconcat (lambda(p)
  16779. (cond
  16780. ((stringp p) p)
  16781. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16782. (format "%% Package %s omitted" (cadr p)))
  16783. ((equal "" (car p))
  16784. (format "\\usepackage{%s}" (cadr p)))
  16785. (t
  16786. (format "\\usepackage[%s]{%s}"
  16787. (car p) (cadr p)))))
  16788. pkg
  16789. "\n"))
  16790. (if newline (concat pkg "\n") pkg))
  16791. (defun org-dvipng-color (attr)
  16792. "Return a RGB color specification for dvipng."
  16793. (apply 'format "rgb %s %s %s"
  16794. (mapcar 'org-normalize-color
  16795. (if (featurep 'xemacs)
  16796. (color-rgb-components
  16797. (face-property 'default
  16798. (cond ((eq attr :foreground) 'foreground)
  16799. ((eq attr :background) 'background))))
  16800. (color-values (face-attribute 'default attr nil))))))
  16801. (defun org-dvipng-color-format (color-name)
  16802. "Convert COLOR-NAME to a RGB color value for dvipng."
  16803. (apply 'format "rgb %s %s %s"
  16804. (mapcar 'org-normalize-color
  16805. (color-values color-name))))
  16806. (defun org-latex-color (attr)
  16807. "Return a RGB color for the LaTeX color package."
  16808. (apply 'format "%s,%s,%s"
  16809. (mapcar 'org-normalize-color
  16810. (if (featurep 'xemacs)
  16811. (color-rgb-components
  16812. (face-property 'default
  16813. (cond ((eq attr :foreground) 'foreground)
  16814. ((eq attr :background) 'background))))
  16815. (color-values (face-attribute 'default attr nil))))))
  16816. (defun org-latex-color-format (color-name)
  16817. "Convert COLOR-NAME to a RGB color value."
  16818. (apply 'format "%s,%s,%s"
  16819. (mapcar 'org-normalize-color
  16820. (color-values color-name))))
  16821. (defun org-normalize-color (value)
  16822. "Return string to be used as color value for an RGB component."
  16823. (format "%g" (/ value 65535.0)))
  16824. ;; Image display
  16825. (defvar org-inline-image-overlays nil)
  16826. (make-variable-buffer-local 'org-inline-image-overlays)
  16827. (defun org-toggle-inline-images (&optional include-linked)
  16828. "Toggle the display of inline images.
  16829. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16830. (interactive "P")
  16831. (if org-inline-image-overlays
  16832. (progn
  16833. (org-remove-inline-images)
  16834. (message "Inline image display turned off"))
  16835. (org-display-inline-images include-linked)
  16836. (if (and (org-called-interactively-p)
  16837. org-inline-image-overlays)
  16838. (message "%d images displayed inline"
  16839. (length org-inline-image-overlays))
  16840. (message "No images to display inline"))))
  16841. (defun org-redisplay-inline-images ()
  16842. "Refresh the display of inline images."
  16843. (interactive)
  16844. (if (not org-inline-image-overlays)
  16845. (org-toggle-inline-images)
  16846. (org-toggle-inline-images)
  16847. (org-toggle-inline-images)))
  16848. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16849. "Display inline images.
  16850. Normally only links without a description part are inlined, because this
  16851. is how it will work for export. When INCLUDE-LINKED is set, also links
  16852. with a description part will be inlined. This can be nice for a quick
  16853. look at those images, but it does not reflect what exported files will look
  16854. like.
  16855. When REFRESH is set, refresh existing images between BEG and END.
  16856. This will create new image displays only if necessary.
  16857. BEG and END default to the buffer boundaries."
  16858. (interactive "P")
  16859. (unless refresh
  16860. (org-remove-inline-images)
  16861. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16862. (save-excursion
  16863. (save-restriction
  16864. (widen)
  16865. (setq beg (or beg (point-min)) end (or end (point-max)))
  16866. (goto-char beg)
  16867. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16868. (substring (org-image-file-name-regexp) 0 -2)
  16869. "\\)\\]" (if include-linked "" "\\]")))
  16870. old file ov img type attrwidth width)
  16871. (while (re-search-forward re end t)
  16872. (setq old (get-char-property-and-overlay (match-beginning 1)
  16873. 'org-image-overlay)
  16874. file (expand-file-name
  16875. (concat (or (match-string 3) "") (match-string 4))))
  16876. (when (image-type-available-p 'imagemagick)
  16877. (setq attrwidth (if (or (listp org-image-actual-width)
  16878. (null org-image-actual-width))
  16879. (save-excursion
  16880. (save-match-data
  16881. (when (re-search-backward
  16882. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16883. (save-excursion
  16884. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16885. (string-to-number (match-string 1))))))
  16886. width (cond ((eq org-image-actual-width t) nil)
  16887. ((null org-image-actual-width) attrwidth)
  16888. ((numberp org-image-actual-width)
  16889. org-image-actual-width)
  16890. ((listp org-image-actual-width)
  16891. (or attrwidth (car org-image-actual-width))))
  16892. type (if width 'imagemagick)))
  16893. (when (file-exists-p file)
  16894. (if (and (car-safe old) refresh)
  16895. (image-refresh (overlay-get (cdr old) 'display))
  16896. (setq img (save-match-data (create-image file type nil :width width)))
  16897. (when img
  16898. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16899. (overlay-put ov 'display img)
  16900. (overlay-put ov 'face 'default)
  16901. (overlay-put ov 'org-image-overlay t)
  16902. (overlay-put ov 'modification-hooks
  16903. (list 'org-display-inline-remove-overlay))
  16904. (push ov org-inline-image-overlays)))))))))
  16905. (define-obsolete-function-alias
  16906. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16907. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16908. "Remove inline-display overlay if a corresponding region is modified."
  16909. (let ((inhibit-modification-hooks t))
  16910. (when (and ov after)
  16911. (delete ov org-inline-image-overlays)
  16912. (delete-overlay ov))))
  16913. (defun org-remove-inline-images ()
  16914. "Remove inline display of images."
  16915. (interactive)
  16916. (mapc 'delete-overlay org-inline-image-overlays)
  16917. (setq org-inline-image-overlays nil))
  16918. ;;;; Key bindings
  16919. ;; Outline functions from `outline-mode-prefix-map'
  16920. ;; that can be remapped in Org:
  16921. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16922. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16923. (define-key org-mode-map [remap outline-forward-same-level]
  16924. 'org-forward-heading-same-level)
  16925. (define-key org-mode-map [remap outline-backward-same-level]
  16926. 'org-backward-heading-same-level)
  16927. (define-key org-mode-map [remap show-branches]
  16928. 'org-kill-note-or-show-branches)
  16929. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16930. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16931. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16932. ;; Outline functions from `outline-mode-prefix-map' that can not
  16933. ;; be remapped in Org:
  16934. ;;
  16935. ;; - the column "key binding" shows whether the Outline function is still
  16936. ;; available in Org mode on the same key that it has been bound to in
  16937. ;; Outline mode:
  16938. ;; - "overridden": key used for a different functionality in Org mode
  16939. ;; - else: key still bound to the same Outline function in Org mode
  16940. ;;
  16941. ;; | Outline function | key binding | Org replacement |
  16942. ;; |------------------------------------+-------------+-----------------------|
  16943. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16944. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16945. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16946. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16947. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16948. ;; | `show-entry' | overridden | no replacement |
  16949. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16950. ;; | `show-branches' | `C-c C-k' | still same function |
  16951. ;; | `show-subtree' | overridden | visibility cycling |
  16952. ;; | `show-all' | overridden | no replacement |
  16953. ;; | `hide-subtree' | overridden | visibility cycling |
  16954. ;; | `hide-body' | overridden | no replacement |
  16955. ;; | `hide-entry' | overridden | visibility cycling |
  16956. ;; | `hide-leaves' | overridden | no replacement |
  16957. ;; | `hide-sublevels' | overridden | no replacement |
  16958. ;; | `hide-other' | overridden | no replacement |
  16959. ;; Make `C-c C-x' a prefix key
  16960. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16961. ;; TAB key with modifiers
  16962. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16963. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16964. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16965. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16966. ;; The following line is necessary under Suse GNU/Linux
  16967. (unless (featurep 'xemacs)
  16968. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16969. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16970. (define-key org-mode-map [backtab] 'org-shifttab)
  16971. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16972. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16973. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16974. ;; Cursor keys with modifiers
  16975. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16976. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16977. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16978. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16979. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16980. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16981. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16982. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16983. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16984. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16985. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16986. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16987. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16988. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16989. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16990. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16991. ;; Babel keys
  16992. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16993. (mapc (lambda (pair)
  16994. (define-key org-babel-map (car pair) (cdr pair)))
  16995. org-babel-key-bindings)
  16996. ;;; Extra keys for tty access.
  16997. ;; We only set them when really needed because otherwise the
  16998. ;; menus don't show the simple keys
  16999. (when (or org-use-extra-keys
  17000. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  17001. (not window-system))
  17002. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  17003. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  17004. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  17005. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  17006. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  17007. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  17008. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  17009. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  17010. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  17011. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  17012. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  17013. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  17014. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  17015. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  17016. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  17017. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  17018. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  17019. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  17020. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  17021. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  17022. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  17023. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  17024. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  17025. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  17026. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  17027. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  17028. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  17029. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  17030. ;; All the other keys
  17031. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  17032. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  17033. (if (boundp 'narrow-map)
  17034. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  17035. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  17036. (if (boundp 'narrow-map)
  17037. (org-defkey narrow-map "b" 'org-narrow-to-block)
  17038. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  17039. (if (boundp 'narrow-map)
  17040. (org-defkey narrow-map "e" 'org-narrow-to-element)
  17041. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  17042. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  17043. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  17044. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  17045. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  17046. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  17047. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  17048. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  17049. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  17050. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  17051. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  17052. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  17053. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  17054. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  17055. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  17056. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  17057. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  17058. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  17059. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  17060. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  17061. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  17062. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  17063. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  17064. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  17065. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  17066. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  17067. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  17068. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  17069. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  17070. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  17071. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  17072. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  17073. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  17074. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  17075. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  17076. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  17077. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  17078. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  17079. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  17080. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  17081. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  17082. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  17083. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  17084. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  17085. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  17086. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  17087. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  17088. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  17089. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  17090. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  17091. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  17092. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  17093. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  17094. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  17095. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  17096. (org-defkey org-mode-map "\C-c^" 'org-sort)
  17097. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  17098. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  17099. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  17100. (org-defkey org-mode-map "\C-m" 'org-return)
  17101. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  17102. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  17103. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  17104. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  17105. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  17106. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  17107. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  17108. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  17109. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  17110. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  17111. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  17112. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  17113. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  17114. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  17115. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  17116. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  17117. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17118. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17119. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17120. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17121. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17122. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17123. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17124. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17125. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17126. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17127. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17128. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17129. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17130. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17131. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17132. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17133. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17134. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17135. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17136. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17137. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17138. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17139. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17140. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17141. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17142. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17143. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17144. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17145. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17146. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17147. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17148. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17149. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17150. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17151. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17152. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17153. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17154. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17155. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17156. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17157. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17158. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17159. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17160. (when (featurep 'xemacs)
  17161. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17162. (defconst org-speed-commands-default
  17163. '(
  17164. ("Outline Navigation")
  17165. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17166. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17167. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17168. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17169. ("F" . org-next-block)
  17170. ("B" . org-previous-block)
  17171. ("u" . (org-speed-move-safe 'outline-up-heading))
  17172. ("j" . org-goto)
  17173. ("g" . (org-refile t))
  17174. ("Outline Visibility")
  17175. ("c" . org-cycle)
  17176. ("C" . org-shifttab)
  17177. (" " . org-display-outline-path)
  17178. ("=" . org-columns)
  17179. ("Outline Structure Editing")
  17180. ("U" . org-shiftmetaup)
  17181. ("D" . org-shiftmetadown)
  17182. ("r" . org-metaright)
  17183. ("l" . org-metaleft)
  17184. ("R" . org-shiftmetaright)
  17185. ("L" . org-shiftmetaleft)
  17186. ("i" . (progn (forward-char 1) (call-interactively
  17187. 'org-insert-heading-respect-content)))
  17188. ("^" . org-sort)
  17189. ("w" . org-refile)
  17190. ("a" . org-archive-subtree-default-with-confirmation)
  17191. ("." . org-mark-subtree) ;; FIXME Better use @ (see C-c @) here?
  17192. ("#" . org-toggle-comment)
  17193. ("Clock Commands")
  17194. ("I" . org-clock-in)
  17195. ("O" . org-clock-out)
  17196. ("Meta Data Editing")
  17197. ("t" . org-todo)
  17198. ("," . (org-priority))
  17199. ("0" . (org-priority ?\ ))
  17200. ("1" . (org-priority ?A))
  17201. ("2" . (org-priority ?B))
  17202. ("3" . (org-priority ?C))
  17203. (":" . org-set-tags-command)
  17204. ("e" . org-set-effort)
  17205. ("E" . org-inc-effort)
  17206. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17207. (org-entry-put (point) "APPT_WARNTIME" m)))
  17208. ("Agenda Views etc")
  17209. ("v" . org-agenda)
  17210. ("/" . org-sparse-tree)
  17211. ("Misc")
  17212. ("o" . org-open-at-point)
  17213. ("?" . org-speed-command-help)
  17214. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17215. (">" . (org-agenda-remove-restriction-lock))
  17216. )
  17217. "The default speed commands.")
  17218. (defun org-print-speed-command (e)
  17219. (if (> (length (car e)) 1)
  17220. (progn
  17221. (princ "\n")
  17222. (princ (car e))
  17223. (princ "\n")
  17224. (princ (make-string (length (car e)) ?-))
  17225. (princ "\n"))
  17226. (princ (car e))
  17227. (princ " ")
  17228. (if (symbolp (cdr e))
  17229. (princ (symbol-name (cdr e)))
  17230. (prin1 (cdr e)))
  17231. (princ "\n")))
  17232. (defun org-speed-command-help ()
  17233. "Show the available speed commands."
  17234. (interactive)
  17235. (if (not org-use-speed-commands)
  17236. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17237. (with-output-to-temp-buffer "*Help*"
  17238. (princ "User-defined Speed commands\n===========================\n")
  17239. (mapc 'org-print-speed-command org-speed-commands-user)
  17240. (princ "\n")
  17241. (princ "Built-in Speed commands\n=======================\n")
  17242. (mapc 'org-print-speed-command org-speed-commands-default))
  17243. (with-current-buffer "*Help*"
  17244. (setq truncate-lines t))))
  17245. (defun org-speed-move-safe (cmd)
  17246. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17247. If not, return to the original position and throw an error."
  17248. (interactive)
  17249. (let ((pos (point)))
  17250. (call-interactively cmd)
  17251. (unless (and (bolp) (org-at-heading-p))
  17252. (goto-char pos)
  17253. (error "Boundary reached while executing %s" cmd))))
  17254. (defvar org-self-insert-command-undo-counter 0)
  17255. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17256. (defvar org-speed-command nil)
  17257. (define-obsolete-function-alias
  17258. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17259. (defun org-speed-command-activate (keys)
  17260. "Hook for activating single-letter speed commands.
  17261. `org-speed-commands-default' specifies a minimal command set.
  17262. Use `org-speed-commands-user' for further customization."
  17263. (when (or (and (bolp) (looking-at org-outline-regexp))
  17264. (and (functionp org-use-speed-commands)
  17265. (funcall org-use-speed-commands)))
  17266. (cdr (assoc keys (append org-speed-commands-user
  17267. org-speed-commands-default)))))
  17268. (define-obsolete-function-alias
  17269. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17270. (defun org-babel-speed-command-activate (keys)
  17271. "Hook for activating single-letter code block commands."
  17272. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17273. (cdr (assoc keys org-babel-key-bindings))))
  17274. (defcustom org-speed-command-hook
  17275. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17276. "Hook for activating speed commands at strategic locations.
  17277. Hook functions are called in sequence until a valid handler is
  17278. found.
  17279. Each hook takes a single argument, a user-pressed command key
  17280. which is also a `self-insert-command' from the global map.
  17281. Within the hook, examine the cursor position and the command key
  17282. and return nil or a valid handler as appropriate. Handler could
  17283. be one of an interactive command, a function, or a form.
  17284. Set `org-use-speed-commands' to non-nil value to enable this
  17285. hook. The default setting is `org-speed-command-activate'."
  17286. :group 'org-structure
  17287. :version "24.1"
  17288. :type 'hook)
  17289. (defun org-self-insert-command (N)
  17290. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17291. If the cursor is in a table looking at whitespace, the whitespace is
  17292. overwritten, and the table is not marked as requiring realignment."
  17293. (interactive "p")
  17294. (org-check-before-invisible-edit 'insert)
  17295. (cond
  17296. ((and org-use-speed-commands
  17297. (setq org-speed-command
  17298. (run-hook-with-args-until-success
  17299. 'org-speed-command-hook (this-command-keys))))
  17300. (cond
  17301. ((commandp org-speed-command)
  17302. (setq this-command org-speed-command)
  17303. (call-interactively org-speed-command))
  17304. ((functionp org-speed-command)
  17305. (funcall org-speed-command))
  17306. ((and org-speed-command (listp org-speed-command))
  17307. (eval org-speed-command))
  17308. (t (let (org-use-speed-commands)
  17309. (call-interactively 'org-self-insert-command)))))
  17310. ((and
  17311. (org-table-p)
  17312. (progn
  17313. ;; check if we blank the field, and if that triggers align
  17314. (and (featurep 'org-table) org-table-auto-blank-field
  17315. (member last-command
  17316. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17317. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17318. ;; got extra space, this field does not determine column width
  17319. (let (org-table-may-need-update) (org-table-blank-field))
  17320. ;; no extra space, this field may determine column width
  17321. (org-table-blank-field)))
  17322. t)
  17323. (eq N 1)
  17324. (looking-at "[^|\n]* |"))
  17325. (let (org-table-may-need-update)
  17326. (goto-char (1- (match-end 0)))
  17327. (backward-delete-char 1)
  17328. (goto-char (match-beginning 0))
  17329. (self-insert-command N)))
  17330. (t
  17331. (setq org-table-may-need-update t)
  17332. (self-insert-command N)
  17333. (org-fix-tags-on-the-fly)
  17334. (if org-self-insert-cluster-for-undo
  17335. (if (not (eq last-command 'org-self-insert-command))
  17336. (setq org-self-insert-command-undo-counter 1)
  17337. (if (>= org-self-insert-command-undo-counter 20)
  17338. (setq org-self-insert-command-undo-counter 1)
  17339. (and (> org-self-insert-command-undo-counter 0)
  17340. buffer-undo-list (listp buffer-undo-list)
  17341. (not (cadr buffer-undo-list)) ; remove nil entry
  17342. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17343. (setq org-self-insert-command-undo-counter
  17344. (1+ org-self-insert-command-undo-counter))))))))
  17345. (defun org-check-before-invisible-edit (kind)
  17346. "Check is editing if kind KIND would be dangerous with invisible text around.
  17347. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17348. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17349. (if (and org-catch-invisible-edits
  17350. (or (not (boundp 'visible-mode)) (not visible-mode))
  17351. (or (get-char-property (point) 'invisible)
  17352. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17353. ;; OK, we need to take a closer look
  17354. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17355. (invisible-before-point (if (bobp) nil (get-char-property
  17356. (1- (point)) 'invisible)))
  17357. (border-and-ok-direction
  17358. (or
  17359. ;; Check if we are acting predictably before invisible text
  17360. (and invisible-at-point (not invisible-before-point)
  17361. (memq kind '(insert delete-backward)))
  17362. ;; Check if we are acting predictably after invisible text
  17363. ;; This works not well, and I have turned it off. It seems
  17364. ;; better to always show and stop after invisible text.
  17365. ;; (and (not invisible-at-point) invisible-before-point
  17366. ;; (memq kind '(insert delete)))
  17367. )))
  17368. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17369. (memq invisible-before-point '(outline org-hide-block t)))
  17370. (if (eq org-catch-invisible-edits 'error)
  17371. (error "Editing in invisible areas is prohibited - make visible first"))
  17372. (if (and org-custom-properties-overlays
  17373. (y-or-n-p "Display invisible properties in this buffer? "))
  17374. (org-toggle-custom-properties-visibility)
  17375. ;; Make the area visible
  17376. (save-excursion
  17377. (if invisible-before-point
  17378. (goto-char (previous-single-char-property-change
  17379. (point) 'invisible)))
  17380. (org-cycle))
  17381. (cond
  17382. ((eq org-catch-invisible-edits 'show)
  17383. ;; That's it, we do the edit after showing
  17384. (message
  17385. "Unfolding invisible region around point before editing")
  17386. (sit-for 1))
  17387. ((and (eq org-catch-invisible-edits 'smart)
  17388. border-and-ok-direction)
  17389. (message "Unfolding invisible region around point before editing"))
  17390. (t
  17391. ;; Don't do the edit, make the user repeat it in full visibility
  17392. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17393. (defun org-fix-tags-on-the-fly ()
  17394. (when (and (equal (char-after (point-at-bol)) ?*)
  17395. (org-at-heading-p))
  17396. (org-align-tags-here org-tags-column)))
  17397. (defun org-delete-backward-char (N)
  17398. "Like `delete-backward-char', insert whitespace at field end in tables.
  17399. When deleting backwards, in tables this function will insert whitespace in
  17400. front of the next \"|\" separator, to keep the table aligned. The table will
  17401. still be marked for re-alignment if the field did fill the entire column,
  17402. because, in this case the deletion might narrow the column."
  17403. (interactive "p")
  17404. (save-match-data
  17405. (org-check-before-invisible-edit 'delete-backward)
  17406. (if (and (org-table-p)
  17407. (eq N 1)
  17408. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17409. (looking-at ".*?|"))
  17410. (let ((pos (point))
  17411. (noalign (looking-at "[^|\n\r]* |"))
  17412. (c org-table-may-need-update))
  17413. (backward-delete-char N)
  17414. (if (not overwrite-mode)
  17415. (progn
  17416. (skip-chars-forward "^|")
  17417. (insert " ")
  17418. (goto-char (1- pos))))
  17419. ;; noalign: if there were two spaces at the end, this field
  17420. ;; does not determine the width of the column.
  17421. (if noalign (setq org-table-may-need-update c)))
  17422. (backward-delete-char N)
  17423. (org-fix-tags-on-the-fly))))
  17424. (defun org-delete-char (N)
  17425. "Like `delete-char', but insert whitespace at field end in tables.
  17426. When deleting characters, in tables this function will insert whitespace in
  17427. front of the next \"|\" separator, to keep the table aligned. The table will
  17428. still be marked for re-alignment if the field did fill the entire column,
  17429. because, in this case the deletion might narrow the column."
  17430. (interactive "p")
  17431. (save-match-data
  17432. (org-check-before-invisible-edit 'delete)
  17433. (if (and (org-table-p)
  17434. (not (bolp))
  17435. (not (= (char-after) ?|))
  17436. (eq N 1))
  17437. (if (looking-at ".*?|")
  17438. (let ((pos (point))
  17439. (noalign (looking-at "[^|\n\r]* |"))
  17440. (c org-table-may-need-update))
  17441. (replace-match (concat
  17442. (substring (match-string 0) 1 -1)
  17443. " |"))
  17444. (goto-char pos)
  17445. ;; noalign: if there were two spaces at the end, this field
  17446. ;; does not determine the width of the column.
  17447. (if noalign (setq org-table-may-need-update c)))
  17448. (delete-char N))
  17449. (delete-char N)
  17450. (org-fix-tags-on-the-fly))))
  17451. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17452. (put 'org-self-insert-command 'delete-selection t)
  17453. (put 'orgtbl-self-insert-command 'delete-selection t)
  17454. (put 'org-delete-char 'delete-selection 'supersede)
  17455. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17456. (put 'org-yank 'delete-selection 'yank)
  17457. ;; Make `flyspell-mode' delay after some commands
  17458. (put 'org-self-insert-command 'flyspell-delayed t)
  17459. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17460. (put 'org-delete-char 'flyspell-delayed t)
  17461. (put 'org-delete-backward-char 'flyspell-delayed t)
  17462. ;; Make pabbrev-mode expand after org-mode commands
  17463. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17464. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17465. ;; How to do this: Measure non-white length of current string
  17466. ;; If equal to column width, we should realign.
  17467. (defun org-remap (map &rest commands)
  17468. "In MAP, remap the functions given in COMMANDS.
  17469. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17470. (let (new old)
  17471. (while commands
  17472. (setq old (pop commands) new (pop commands))
  17473. (if (fboundp 'command-remapping)
  17474. (org-defkey map (vector 'remap old) new)
  17475. (substitute-key-definition old new map global-map)))))
  17476. (defun org-transpose-words ()
  17477. "Transpose words for Org.
  17478. This uses the `org-mode-transpose-word-syntax-table' syntax
  17479. table, which interprets characters in `org-emphasis-alist' as
  17480. word constituants."
  17481. (interactive)
  17482. (with-syntax-table org-mode-transpose-word-syntax-table
  17483. (call-interactively 'transpose-words)))
  17484. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  17485. (when (eq org-enable-table-editor 'optimized)
  17486. ;; If the user wants maximum table support, we need to hijack
  17487. ;; some standard editing functions
  17488. (org-remap org-mode-map
  17489. 'self-insert-command 'org-self-insert-command
  17490. 'delete-char 'org-delete-char
  17491. 'delete-backward-char 'org-delete-backward-char)
  17492. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17493. (defvar org-ctrl-c-ctrl-c-hook nil
  17494. "Hook for functions attaching themselves to `C-c C-c'.
  17495. This can be used to add additional functionality to the C-c C-c
  17496. key which executes context-dependent commands. This hook is run
  17497. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17498. run after the last test.
  17499. Each function will be called with no arguments. The function
  17500. must check if the context is appropriate for it to act. If yes,
  17501. it should do its thing and then return a non-nil value. If the
  17502. context is wrong, just do nothing and return nil.")
  17503. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17504. "Hook for functions attaching themselves to `C-c C-c'.
  17505. This can be used to add additional functionality to the C-c C-c
  17506. key which executes context-dependent commands. This hook is run
  17507. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17508. before the first test.
  17509. Each function will be called with no arguments. The function
  17510. must check if the context is appropriate for it to act. If yes,
  17511. it should do its thing and then return a non-nil value. If the
  17512. context is wrong, just do nothing and return nil.")
  17513. (defvar org-tab-first-hook nil
  17514. "Hook for functions to attach themselves to TAB.
  17515. See `org-ctrl-c-ctrl-c-hook' for more information.
  17516. This hook runs as the first action when TAB is pressed, even before
  17517. `org-cycle' messes around with the `outline-regexp' to cater for
  17518. inline tasks and plain list item folding.
  17519. If any function in this hook returns t, any other actions that
  17520. would have been caused by TAB (such as table field motion or visibility
  17521. cycling) will not occur.")
  17522. (defvar org-tab-after-check-for-table-hook nil
  17523. "Hook for functions to attach themselves to TAB.
  17524. See `org-ctrl-c-ctrl-c-hook' for more information.
  17525. This hook runs after it has been established that the cursor is not in a
  17526. table, but before checking if the cursor is in a headline or if global cycling
  17527. should be done.
  17528. If any function in this hook returns t, not other actions like visibility
  17529. cycling will be done.")
  17530. (defvar org-tab-after-check-for-cycling-hook nil
  17531. "Hook for functions to attach themselves to TAB.
  17532. See `org-ctrl-c-ctrl-c-hook' for more information.
  17533. This hook runs after it has been established that not table field motion and
  17534. not visibility should be done because of current context. This is probably
  17535. the place where a package like yasnippets can hook in.")
  17536. (defvar org-tab-before-tab-emulation-hook nil
  17537. "Hook for functions to attach themselves to TAB.
  17538. See `org-ctrl-c-ctrl-c-hook' for more information.
  17539. This hook runs after every other options for TAB have been exhausted, but
  17540. before indentation and \t insertion takes place.")
  17541. (defvar org-metaleft-hook nil
  17542. "Hook for functions attaching themselves to `M-left'.
  17543. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17544. (defvar org-metaright-hook nil
  17545. "Hook for functions attaching themselves to `M-right'.
  17546. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17547. (defvar org-metaup-hook nil
  17548. "Hook for functions attaching themselves to `M-up'.
  17549. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17550. (defvar org-metadown-hook nil
  17551. "Hook for functions attaching themselves to `M-down'.
  17552. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17553. (defvar org-shiftmetaleft-hook nil
  17554. "Hook for functions attaching themselves to `M-S-left'.
  17555. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17556. (defvar org-shiftmetaright-hook nil
  17557. "Hook for functions attaching themselves to `M-S-right'.
  17558. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17559. (defvar org-shiftmetaup-hook nil
  17560. "Hook for functions attaching themselves to `M-S-up'.
  17561. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17562. (defvar org-shiftmetadown-hook nil
  17563. "Hook for functions attaching themselves to `M-S-down'.
  17564. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17565. (defvar org-metareturn-hook nil
  17566. "Hook for functions attaching themselves to `M-RET'.
  17567. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17568. (defvar org-shiftup-hook nil
  17569. "Hook for functions attaching themselves to `S-up'.
  17570. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17571. (defvar org-shiftup-final-hook nil
  17572. "Hook for functions attaching themselves to `S-up'.
  17573. This one runs after all other options except shift-select have been excluded.
  17574. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17575. (defvar org-shiftdown-hook nil
  17576. "Hook for functions attaching themselves to `S-down'.
  17577. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17578. (defvar org-shiftdown-final-hook nil
  17579. "Hook for functions attaching themselves to `S-down'.
  17580. This one runs after all other options except shift-select have been excluded.
  17581. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17582. (defvar org-shiftleft-hook nil
  17583. "Hook for functions attaching themselves to `S-left'.
  17584. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17585. (defvar org-shiftleft-final-hook nil
  17586. "Hook for functions attaching themselves to `S-left'.
  17587. This one runs after all other options except shift-select have been excluded.
  17588. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17589. (defvar org-shiftright-hook nil
  17590. "Hook for functions attaching themselves to `S-right'.
  17591. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17592. (defvar org-shiftright-final-hook nil
  17593. "Hook for functions attaching themselves to `S-right'.
  17594. This one runs after all other options except shift-select have been excluded.
  17595. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17596. (defun org-modifier-cursor-error ()
  17597. "Throw an error, a modified cursor command was applied in wrong context."
  17598. (error "This command is active in special context like tables, headlines or items"))
  17599. (defun org-shiftselect-error ()
  17600. "Throw an error because Shift-Cursor command was applied in wrong context."
  17601. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17602. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17603. (error "This command works only in special context like headlines or timestamps")))
  17604. (defun org-call-for-shift-select (cmd)
  17605. (let ((this-command-keys-shift-translated t))
  17606. (call-interactively cmd)))
  17607. (defun org-shifttab (&optional arg)
  17608. "Global visibility cycling or move to previous table field.
  17609. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17610. on context.
  17611. See the individual commands for more information."
  17612. (interactive "P")
  17613. (cond
  17614. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17615. ((integerp arg)
  17616. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17617. (message "Content view to level: %d" arg)
  17618. (org-content (prefix-numeric-value arg2))
  17619. (setq org-cycle-global-status 'overview)))
  17620. (t (call-interactively 'org-global-cycle))))
  17621. (defun org-shiftmetaleft ()
  17622. "Promote subtree or delete table column.
  17623. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17624. `org-table-delete-column', depending on context. See the
  17625. individual commands for more information."
  17626. (interactive)
  17627. (cond
  17628. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17629. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17630. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17631. ((if (not (org-region-active-p)) (org-at-item-p)
  17632. (save-excursion (goto-char (region-beginning))
  17633. (org-at-item-p)))
  17634. (call-interactively 'org-outdent-item-tree))
  17635. (t (org-modifier-cursor-error))))
  17636. (defun org-shiftmetaright ()
  17637. "Demote subtree or insert table column.
  17638. Calls `org-demote-subtree', `org-indent-item-tree', or
  17639. `org-table-insert-column', depending on context. See the
  17640. individual commands for more information."
  17641. (interactive)
  17642. (cond
  17643. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17644. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17645. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17646. ((if (not (org-region-active-p)) (org-at-item-p)
  17647. (save-excursion (goto-char (region-beginning))
  17648. (org-at-item-p)))
  17649. (call-interactively 'org-indent-item-tree))
  17650. (t (org-modifier-cursor-error))))
  17651. (defun org-shiftmetaup (&optional arg)
  17652. "Move subtree up or kill table row.
  17653. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17654. `org-move-item-up' or `org-timestamp-up', depending on context.
  17655. See the individual commands for more information."
  17656. (interactive "P")
  17657. (cond
  17658. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17659. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17660. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17661. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17662. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17663. (call-interactively 'org-timestamp-up)))
  17664. (t (call-interactively 'org-drag-line-backward))))
  17665. (defun org-shiftmetadown (&optional arg)
  17666. "Move subtree down or insert table row.
  17667. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17668. `org-move-item-down' or `org-timestamp-up', depending on context.
  17669. See the individual commands for more information."
  17670. (interactive "P")
  17671. (cond
  17672. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17673. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17674. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17675. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17676. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17677. (call-interactively 'org-timestamp-down)))
  17678. (t (call-interactively 'org-drag-line-forward))))
  17679. (defsubst org-hidden-tree-error ()
  17680. (error
  17681. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17682. (defun org-metaleft (&optional arg)
  17683. "Promote heading or move table column to left.
  17684. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17685. With no specific context, calls the Emacs default `backward-word'.
  17686. See the individual commands for more information."
  17687. (interactive "P")
  17688. (cond
  17689. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17690. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17691. ((org-with-limited-levels
  17692. (or (org-at-heading-p)
  17693. (and (org-region-active-p)
  17694. (save-excursion
  17695. (goto-char (region-beginning))
  17696. (org-at-heading-p)))))
  17697. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17698. (call-interactively 'org-do-promote))
  17699. ;; At an inline task.
  17700. ((org-at-heading-p)
  17701. (call-interactively 'org-inlinetask-promote))
  17702. ((or (org-at-item-p)
  17703. (and (org-region-active-p)
  17704. (save-excursion
  17705. (goto-char (region-beginning))
  17706. (org-at-item-p))))
  17707. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17708. (call-interactively 'org-outdent-item))
  17709. (t (call-interactively 'backward-word))))
  17710. (defun org-metaright (&optional arg)
  17711. "Demote a subtree, a list item or move table column to right.
  17712. In front of a drawer or a block keyword, indent it correctly.
  17713. With no specific context, calls the Emacs default `forward-word'.
  17714. See the individual commands for more information."
  17715. (interactive "P")
  17716. (cond
  17717. ((run-hook-with-args-until-success 'org-metaright-hook))
  17718. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17719. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17720. ((org-at-block-p) (call-interactively 'org-indent-block))
  17721. ((org-with-limited-levels
  17722. (or (org-at-heading-p)
  17723. (and (org-region-active-p)
  17724. (save-excursion
  17725. (goto-char (region-beginning))
  17726. (org-at-heading-p)))))
  17727. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17728. (call-interactively 'org-do-demote))
  17729. ;; At an inline task.
  17730. ((org-at-heading-p)
  17731. (call-interactively 'org-inlinetask-demote))
  17732. ((or (org-at-item-p)
  17733. (and (org-region-active-p)
  17734. (save-excursion
  17735. (goto-char (region-beginning))
  17736. (org-at-item-p))))
  17737. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17738. (call-interactively 'org-indent-item))
  17739. (t (call-interactively 'forward-word))))
  17740. (defun org-check-for-hidden (what)
  17741. "Check if there are hidden headlines/items in the current visual line.
  17742. WHAT can be either `headlines' or `items'. If the current line is
  17743. an outline or item heading and it has a folded subtree below it,
  17744. this function returns t, nil otherwise."
  17745. (let ((re (cond
  17746. ((eq what 'headlines) org-outline-regexp-bol)
  17747. ((eq what 'items) (org-item-beginning-re))
  17748. (t (error "This should not happen"))))
  17749. beg end)
  17750. (save-excursion
  17751. (catch 'exit
  17752. (unless (org-region-active-p)
  17753. (setq beg (point-at-bol))
  17754. (beginning-of-line 2)
  17755. (while (and (not (eobp)) ;; this is like `next-line'
  17756. (get-char-property (1- (point)) 'invisible))
  17757. (beginning-of-line 2))
  17758. (setq end (point))
  17759. (goto-char beg)
  17760. (goto-char (point-at-eol))
  17761. (setq end (max end (point)))
  17762. (while (re-search-forward re end t)
  17763. (if (get-char-property (match-beginning 0) 'invisible)
  17764. (throw 'exit t))))
  17765. nil))))
  17766. (defun org-metaup (&optional arg)
  17767. "Move subtree up or move table row up.
  17768. Calls `org-move-subtree-up' or `org-table-move-row' or
  17769. `org-move-item-up', depending on context. See the individual commands
  17770. for more information."
  17771. (interactive "P")
  17772. (cond
  17773. ((run-hook-with-args-until-success 'org-metaup-hook))
  17774. ((org-region-active-p)
  17775. (let* ((a (min (region-beginning) (region-end)))
  17776. (b (1- (max (region-beginning) (region-end))))
  17777. (c (save-excursion (goto-char a)
  17778. (move-beginning-of-line 0)))
  17779. (d (save-excursion (goto-char a)
  17780. (move-end-of-line 0) (point))))
  17781. (transpose-regions a b c d)
  17782. (goto-char c)))
  17783. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17784. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17785. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17786. (t (org-drag-element-backward))))
  17787. (defun org-metadown (&optional arg)
  17788. "Move subtree down or move table row down.
  17789. Calls `org-move-subtree-down' or `org-table-move-row' or
  17790. `org-move-item-down', depending on context. See the individual
  17791. commands for more information."
  17792. (interactive "P")
  17793. (cond
  17794. ((run-hook-with-args-until-success 'org-metadown-hook))
  17795. ((org-region-active-p)
  17796. (let* ((a (min (region-beginning) (region-end)))
  17797. (b (max (region-beginning) (region-end)))
  17798. (c (save-excursion (goto-char b)
  17799. (move-beginning-of-line 1)))
  17800. (d (save-excursion (goto-char b)
  17801. (move-end-of-line 1) (1+ (point)))))
  17802. (transpose-regions a b c d)
  17803. (goto-char d)))
  17804. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17805. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17806. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17807. (t (org-drag-element-forward))))
  17808. (defun org-shiftup (&optional arg)
  17809. "Increase item in timestamp or increase priority of current headline.
  17810. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17811. depending on context. See the individual commands for more information."
  17812. (interactive "P")
  17813. (cond
  17814. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17815. ((and org-support-shift-select (org-region-active-p))
  17816. (org-call-for-shift-select 'previous-line))
  17817. ((org-at-timestamp-p t)
  17818. (call-interactively (if org-edit-timestamp-down-means-later
  17819. 'org-timestamp-down 'org-timestamp-up)))
  17820. ((and (not (eq org-support-shift-select 'always))
  17821. org-enable-priority-commands
  17822. (org-at-heading-p))
  17823. (call-interactively 'org-priority-up))
  17824. ((and (not org-support-shift-select) (org-at-item-p))
  17825. (call-interactively 'org-previous-item))
  17826. ((org-clocktable-try-shift 'up arg))
  17827. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17828. (org-support-shift-select
  17829. (org-call-for-shift-select 'previous-line))
  17830. (t (org-shiftselect-error))))
  17831. (defun org-shiftdown (&optional arg)
  17832. "Decrease item in timestamp or decrease priority of current headline.
  17833. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17834. depending on context. See the individual commands for more information."
  17835. (interactive "P")
  17836. (cond
  17837. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17838. ((and org-support-shift-select (org-region-active-p))
  17839. (org-call-for-shift-select 'next-line))
  17840. ((org-at-timestamp-p t)
  17841. (call-interactively (if org-edit-timestamp-down-means-later
  17842. 'org-timestamp-up 'org-timestamp-down)))
  17843. ((and (not (eq org-support-shift-select 'always))
  17844. org-enable-priority-commands
  17845. (org-at-heading-p))
  17846. (call-interactively 'org-priority-down))
  17847. ((and (not org-support-shift-select) (org-at-item-p))
  17848. (call-interactively 'org-next-item))
  17849. ((org-clocktable-try-shift 'down arg))
  17850. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17851. (org-support-shift-select
  17852. (org-call-for-shift-select 'next-line))
  17853. (t (org-shiftselect-error))))
  17854. (defun org-shiftright (&optional arg)
  17855. "Cycle the thing at point or in the current line, depending on context.
  17856. Depending on context, this does one of the following:
  17857. - switch a timestamp at point one day into the future
  17858. - on a headline, switch to the next TODO keyword.
  17859. - on an item, switch entire list to the next bullet type
  17860. - on a property line, switch to the next allowed value
  17861. - on a clocktable definition line, move time block into the future"
  17862. (interactive "P")
  17863. (cond
  17864. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17865. ((and org-support-shift-select (org-region-active-p))
  17866. (org-call-for-shift-select 'forward-char))
  17867. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17868. ((and (not (eq org-support-shift-select 'always))
  17869. (org-at-heading-p))
  17870. (let ((org-inhibit-logging
  17871. (not org-treat-S-cursor-todo-selection-as-state-change))
  17872. (org-inhibit-blocking
  17873. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17874. (org-call-with-arg 'org-todo 'right)))
  17875. ((or (and org-support-shift-select
  17876. (not (eq org-support-shift-select 'always))
  17877. (org-at-item-bullet-p))
  17878. (and (not org-support-shift-select) (org-at-item-p)))
  17879. (org-call-with-arg 'org-cycle-list-bullet nil))
  17880. ((and (not (eq org-support-shift-select 'always))
  17881. (org-at-property-p))
  17882. (call-interactively 'org-property-next-allowed-value))
  17883. ((org-clocktable-try-shift 'right arg))
  17884. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17885. (org-support-shift-select
  17886. (org-call-for-shift-select 'forward-char))
  17887. (t (org-shiftselect-error))))
  17888. (defun org-shiftleft (&optional arg)
  17889. "Cycle the thing at point or in the current line, depending on context.
  17890. Depending on context, this does one of the following:
  17891. - switch a timestamp at point one day into the past
  17892. - on a headline, switch to the previous TODO keyword.
  17893. - on an item, switch entire list to the previous bullet type
  17894. - on a property line, switch to the previous allowed value
  17895. - on a clocktable definition line, move time block into the past"
  17896. (interactive "P")
  17897. (cond
  17898. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17899. ((and org-support-shift-select (org-region-active-p))
  17900. (org-call-for-shift-select 'backward-char))
  17901. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17902. ((and (not (eq org-support-shift-select 'always))
  17903. (org-at-heading-p))
  17904. (let ((org-inhibit-logging
  17905. (not org-treat-S-cursor-todo-selection-as-state-change))
  17906. (org-inhibit-blocking
  17907. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17908. (org-call-with-arg 'org-todo 'left)))
  17909. ((or (and org-support-shift-select
  17910. (not (eq org-support-shift-select 'always))
  17911. (org-at-item-bullet-p))
  17912. (and (not org-support-shift-select) (org-at-item-p)))
  17913. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17914. ((and (not (eq org-support-shift-select 'always))
  17915. (org-at-property-p))
  17916. (call-interactively 'org-property-previous-allowed-value))
  17917. ((org-clocktable-try-shift 'left arg))
  17918. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17919. (org-support-shift-select
  17920. (org-call-for-shift-select 'backward-char))
  17921. (t (org-shiftselect-error))))
  17922. (defun org-shiftcontrolright ()
  17923. "Switch to next TODO set."
  17924. (interactive)
  17925. (cond
  17926. ((and org-support-shift-select (org-region-active-p))
  17927. (org-call-for-shift-select 'forward-word))
  17928. ((and (not (eq org-support-shift-select 'always))
  17929. (org-at-heading-p))
  17930. (org-call-with-arg 'org-todo 'nextset))
  17931. (org-support-shift-select
  17932. (org-call-for-shift-select 'forward-word))
  17933. (t (org-shiftselect-error))))
  17934. (defun org-shiftcontrolleft ()
  17935. "Switch to previous TODO set."
  17936. (interactive)
  17937. (cond
  17938. ((and org-support-shift-select (org-region-active-p))
  17939. (org-call-for-shift-select 'backward-word))
  17940. ((and (not (eq org-support-shift-select 'always))
  17941. (org-at-heading-p))
  17942. (org-call-with-arg 'org-todo 'previousset))
  17943. (org-support-shift-select
  17944. (org-call-for-shift-select 'backward-word))
  17945. (t (org-shiftselect-error))))
  17946. (defun org-shiftcontrolup (&optional n)
  17947. "Change timestamps synchronously up in CLOCK log lines.
  17948. Optional argument N tells to change by that many units."
  17949. (interactive "P")
  17950. (cond ((and (not org-support-shift-select)
  17951. (org-at-clock-log-p)
  17952. (org-at-timestamp-p t))
  17953. (org-clock-timestamps-up n))
  17954. (t (org-shiftselect-error))))
  17955. (defun org-shiftcontroldown (&optional n)
  17956. "Change timestamps synchronously down in CLOCK log lines.
  17957. Optional argument N tells to change by that many units."
  17958. (interactive "P")
  17959. (cond ((and (not org-support-shift-select)
  17960. (org-at-clock-log-p)
  17961. (org-at-timestamp-p t))
  17962. (org-clock-timestamps-down n))
  17963. (t (org-shiftselect-error))))
  17964. (defun org-ctrl-c-ret ()
  17965. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17966. (interactive)
  17967. (cond
  17968. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17969. (t (call-interactively 'org-insert-heading))))
  17970. (defun org-find-visible ()
  17971. (let ((s (point)))
  17972. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17973. (get-char-property s 'invisible)))
  17974. s))
  17975. (defun org-find-invisible ()
  17976. (let ((s (point)))
  17977. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17978. (not (get-char-property s 'invisible))))
  17979. s))
  17980. (defun org-copy-visible (beg end)
  17981. "Copy the visible parts of the region."
  17982. (interactive "r")
  17983. (let (snippets s)
  17984. (save-excursion
  17985. (save-restriction
  17986. (narrow-to-region beg end)
  17987. (setq s (goto-char (point-min)))
  17988. (while (not (= (point) (point-max)))
  17989. (goto-char (org-find-invisible))
  17990. (push (buffer-substring s (point)) snippets)
  17991. (setq s (goto-char (org-find-visible))))))
  17992. (kill-new (apply 'concat (nreverse snippets)))))
  17993. (defun org-copy-special ()
  17994. "Copy region in table or copy current subtree.
  17995. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17996. See the individual commands for more information."
  17997. (interactive)
  17998. (call-interactively
  17999. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  18000. (defun org-cut-special ()
  18001. "Cut region in table or cut current subtree.
  18002. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  18003. See the individual commands for more information."
  18004. (interactive)
  18005. (call-interactively
  18006. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  18007. (defun org-paste-special (arg)
  18008. "Paste rectangular region into table, or past subtree relative to level.
  18009. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  18010. See the individual commands for more information."
  18011. (interactive "P")
  18012. (if (org-at-table-p)
  18013. (org-table-paste-rectangle)
  18014. (org-paste-subtree arg)))
  18015. (defsubst org-in-fixed-width-region-p ()
  18016. "Is point in a fixed-width region?"
  18017. (save-match-data
  18018. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  18019. (defun org-edit-special (&optional arg)
  18020. "Call a special editor for the element at point.
  18021. When at a table, call the formula editor with `org-table-edit-formulas'.
  18022. When in a source code block, call `org-edit-src-code'.
  18023. When in a fixed-width region, call `org-edit-fixed-width-region'.
  18024. When at an #+INCLUDE keyword, visit the included file.
  18025. On a link, call `ffap' to visit the link at point.
  18026. Otherwise, return a user error."
  18027. (interactive)
  18028. (let ((element (org-element-at-point)))
  18029. (assert (not buffer-read-only) nil
  18030. "Buffer is read-only: %s" (buffer-name))
  18031. (case (org-element-type element)
  18032. (src-block
  18033. (if (not arg) (org-edit-src-code)
  18034. (let* ((info (org-babel-get-src-block-info))
  18035. (lang (nth 0 info))
  18036. (params (nth 2 info))
  18037. (session (cdr (assq :session params))))
  18038. (if (not session) (org-edit-src-code)
  18039. ;; At a src-block with a session and function called with
  18040. ;; an ARG: switch to the buffer related to the inferior
  18041. ;; process.
  18042. (funcall (intern (concat "org-babel-prep-session:" lang))
  18043. session params)))))
  18044. (keyword
  18045. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  18046. (find-file
  18047. (org-remove-double-quotes
  18048. (car (org-split-string (org-element-property :value element)))))
  18049. (user-error "No special environment to edit here")))
  18050. (table
  18051. (if (eq (org-element-property :type element) 'table.el)
  18052. (org-edit-src-code)
  18053. (call-interactively 'org-table-edit-formulas)))
  18054. ;; Only Org tables contain `table-row' type elements.
  18055. (table-row (call-interactively 'org-table-edit-formulas))
  18056. ((example-block export-block) (org-edit-src-code))
  18057. (fixed-width (org-edit-fixed-width-region))
  18058. (otherwise
  18059. ;; No notable element at point. Though, we may be at a link,
  18060. ;; which is an object. Thus, scan deeper.
  18061. (if (eq (org-element-type (org-element-context element)) 'link)
  18062. (call-interactively 'ffap)
  18063. (user-error "No special environment to edit here"))))))
  18064. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  18065. (defun org-ctrl-c-ctrl-c (&optional arg)
  18066. "Set tags in headline, or update according to changed information at point.
  18067. This command does many different things, depending on context:
  18068. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  18069. this is what we do.
  18070. - If the cursor is on a statistics cookie, update it.
  18071. - If the cursor is in a headline, prompt for tags and insert them
  18072. into the current line, aligned to `org-tags-column'. When called
  18073. with prefix arg, realign all tags in the current buffer.
  18074. - If the cursor is in one of the special #+KEYWORD lines, this
  18075. triggers scanning the buffer for these lines and updating the
  18076. information.
  18077. - If the cursor is inside a table, realign the table. This command
  18078. works even if the automatic table editor has been turned off.
  18079. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  18080. the entire table.
  18081. - If the cursor is at a footnote reference or definition, jump to
  18082. the corresponding definition or references, respectively.
  18083. - If the cursor is a the beginning of a dynamic block, update it.
  18084. - If the current buffer is a capture buffer, close note and file it.
  18085. - If the cursor is on a <<<target>>>, update radio targets and
  18086. corresponding links in this buffer.
  18087. - If the cursor is on a numbered item in a plain list, renumber the
  18088. ordered list.
  18089. - If the cursor is on a checkbox, toggle it.
  18090. - If the cursor is on a code block, evaluate it. The variable
  18091. `org-confirm-babel-evaluate' can be used to control prompting
  18092. before code block evaluation, by default every code block
  18093. evaluation requires confirmation. Code block evaluation can be
  18094. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  18095. (interactive "P")
  18096. (cond
  18097. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  18098. org-occur-highlights
  18099. org-latex-fragment-image-overlays)
  18100. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  18101. (org-remove-occur-highlights)
  18102. (org-remove-latex-fragment-image-overlays)
  18103. (message "Temporary highlights/overlays removed from current buffer"))
  18104. ((and (local-variable-p 'org-finish-function (current-buffer))
  18105. (fboundp org-finish-function))
  18106. (funcall org-finish-function))
  18107. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  18108. (t
  18109. (let* ((context (org-element-context)) (type (org-element-type context)))
  18110. ;; Test if point is within a blank line.
  18111. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  18112. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18113. (user-error "C-c C-c can do nothing useful at this location"))
  18114. ;; For convenience: at the first line of a paragraph on the
  18115. ;; same line as an item, apply function on that item instead.
  18116. (when (eq type 'paragraph)
  18117. (let ((parent (org-element-property :parent context)))
  18118. (when (and (eq (org-element-type parent) 'item)
  18119. (= (point-at-bol) (org-element-property :begin parent)))
  18120. (setq context parent type 'item))))
  18121. ;; Act according to type of element or object at point.
  18122. (case type
  18123. (clock (org-clock-update-time-maybe))
  18124. (dynamic-block
  18125. (save-excursion
  18126. (goto-char (org-element-property :post-affiliated context))
  18127. (org-update-dblock)))
  18128. (footnote-definition
  18129. (goto-char (org-element-property :post-affiliated context))
  18130. (call-interactively 'org-footnote-action))
  18131. (footnote-reference (call-interactively 'org-footnote-action))
  18132. ((headline inlinetask)
  18133. (save-excursion (goto-char (org-element-property :begin context))
  18134. (call-interactively 'org-set-tags)))
  18135. (item
  18136. ;; At an item: a double C-u set checkbox to "[-]"
  18137. ;; unconditionally, whereas a single one will toggle its
  18138. ;; presence. Without an universal argument, if the item
  18139. ;; has a checkbox, toggle it. Otherwise repair the list.
  18140. (let* ((box (org-element-property :checkbox context))
  18141. (struct (org-element-property :structure context))
  18142. (old-struct (copy-tree struct))
  18143. (parents (org-list-parents-alist struct))
  18144. (prevs (org-list-prevs-alist struct))
  18145. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18146. (org-list-set-checkbox
  18147. (org-element-property :begin context) struct
  18148. (cond ((equal arg '(16)) "[-]")
  18149. ((and (not box) (equal arg '(4))) "[ ]")
  18150. ((or (not box) (equal arg '(4))) nil)
  18151. ((eq box 'on) "[ ]")
  18152. (t "[X]")))
  18153. ;; Mimic `org-list-write-struct' but with grabbing
  18154. ;; a return value from `org-list-struct-fix-box'.
  18155. (org-list-struct-fix-ind struct parents 2)
  18156. (org-list-struct-fix-item-end struct)
  18157. (org-list-struct-fix-bul struct prevs)
  18158. (org-list-struct-fix-ind struct parents)
  18159. (let ((block-item
  18160. (org-list-struct-fix-box struct parents prevs orderedp)))
  18161. (if (and box (equal struct old-struct))
  18162. (if (equal arg '(16))
  18163. (message "Checkboxes already reset")
  18164. (user-error "Cannot toggle this checkbox: %s"
  18165. (if (eq box 'on)
  18166. "all subitems checked"
  18167. "unchecked subitems")))
  18168. (org-list-struct-apply-struct struct old-struct)
  18169. (org-update-checkbox-count-maybe))
  18170. (when block-item
  18171. (message "Checkboxes were removed due to empty box at line %d"
  18172. (org-current-line block-item))))))
  18173. (keyword
  18174. (let ((org-inhibit-startup-visibility-stuff t)
  18175. (org-startup-align-all-tables nil))
  18176. (when (boundp 'org-table-coordinate-overlays)
  18177. (mapc 'delete-overlay org-table-coordinate-overlays)
  18178. (setq org-table-coordinate-overlays nil))
  18179. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18180. (message "Local setup has been refreshed"))
  18181. (plain-list
  18182. ;; At a plain list, with a double C-u argument, set
  18183. ;; checkboxes of each item to "[-]", whereas a single one
  18184. ;; will toggle their presence according to the state of the
  18185. ;; first item in the list. Without an argument, repair the
  18186. ;; list.
  18187. (let* ((begin (org-element-property :contents-begin context))
  18188. (struct (org-element-property :structure context))
  18189. (old-struct (copy-tree struct))
  18190. (first-box (save-excursion
  18191. (goto-char begin)
  18192. (looking-at org-list-full-item-re)
  18193. (match-string-no-properties 3)))
  18194. (new-box (cond ((equal arg '(16)) "[-]")
  18195. ((equal arg '(4)) (unless first-box "[ ]"))
  18196. ((equal first-box "[X]") "[ ]")
  18197. (t "[X]"))))
  18198. (cond
  18199. (arg
  18200. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18201. (org-list-get-all-items
  18202. begin struct (org-list-prevs-alist struct))))
  18203. ((and first-box (eq (point) begin))
  18204. ;; For convenience, when point is at bol on the first
  18205. ;; item of the list and no argument is provided, simply
  18206. ;; toggle checkbox of that item, if any.
  18207. (org-list-set-checkbox begin struct new-box)))
  18208. (org-list-write-struct
  18209. struct (org-list-parents-alist struct) old-struct)
  18210. (org-update-checkbox-count-maybe)
  18211. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18212. ((property-drawer node-property)
  18213. (call-interactively 'org-property-action))
  18214. ((radio-target target)
  18215. (call-interactively 'org-update-radio-target-regexp))
  18216. (statistics-cookie
  18217. (call-interactively 'org-update-statistics-cookies))
  18218. ((table table-cell table-row)
  18219. ;; At a table, recalculate every field and align it. Also
  18220. ;; send the table if necessary. If the table has
  18221. ;; a `table.el' type, just give up. At a table row or
  18222. ;; cell, maybe recalculate line but always align table.
  18223. (if (eq (org-element-property :type context) 'table.el)
  18224. (message "Use C-c ' to edit table.el tables")
  18225. (let ((org-enable-table-editor t))
  18226. (if (or (eq type 'table)
  18227. ;; Check if point is at a TBLFM line.
  18228. (and (eq type 'table-row)
  18229. (= (point) (org-element-property :end context))))
  18230. (save-excursion
  18231. (goto-char (org-element-property :contents-begin context))
  18232. (org-call-with-arg 'org-table-recalculate (or arg t))
  18233. (orgtbl-send-table 'maybe))
  18234. (org-table-maybe-eval-formula)
  18235. (cond (arg (call-interactively 'org-table-recalculate))
  18236. ((org-table-maybe-recalculate-line))
  18237. (t (org-table-align)))))))
  18238. (timestamp (org-timestamp-change 0 'day))
  18239. (otherwise
  18240. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18241. (user-error
  18242. "C-c C-c can do nothing useful at this location")))))))))
  18243. (defun org-mode-restart ()
  18244. "Restart Org-mode, to scan again for special lines.
  18245. Also updates the keyword regular expressions."
  18246. (interactive)
  18247. (org-mode)
  18248. (message "Org-mode restarted"))
  18249. (defun org-kill-note-or-show-branches ()
  18250. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18251. (interactive)
  18252. (if (not org-finish-function)
  18253. (progn
  18254. (hide-subtree)
  18255. (call-interactively 'show-branches))
  18256. (let ((org-note-abort t))
  18257. (funcall org-finish-function))))
  18258. (defun org-return (&optional indent)
  18259. "Goto next table row or insert a newline.
  18260. Calls `org-table-next-row' or `newline', depending on context.
  18261. See the individual commands for more information."
  18262. (interactive)
  18263. (let (org-ts-what)
  18264. (cond
  18265. ((or (bobp) (org-in-src-block-p))
  18266. (if indent (newline-and-indent) (newline)))
  18267. ((org-at-table-p)
  18268. (org-table-justify-field-maybe)
  18269. (call-interactively 'org-table-next-row))
  18270. ;; when `newline-and-indent' is called within a list, make sure
  18271. ;; text moved stays inside the item.
  18272. ((and (org-in-item-p) indent)
  18273. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18274. (progn
  18275. (save-match-data (newline))
  18276. (org-indent-line-to (length (match-string 0))))
  18277. (let ((ind (org-get-indentation)))
  18278. (newline)
  18279. (if (org-looking-back org-list-end-re)
  18280. (org-indent-line)
  18281. (org-indent-line-to ind)))))
  18282. ((and org-return-follows-link
  18283. (org-at-timestamp-p t)
  18284. (not (eq org-ts-what 'after)))
  18285. (org-follow-timestamp-link))
  18286. ((and org-return-follows-link
  18287. (let ((tprop (get-text-property (point) 'face)))
  18288. (or (eq tprop 'org-link)
  18289. (and (listp tprop) (memq 'org-link tprop)))))
  18290. (call-interactively 'org-open-at-point))
  18291. ((and (org-at-heading-p)
  18292. (looking-at
  18293. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18294. (org-show-entry)
  18295. (end-of-line 1)
  18296. (newline))
  18297. (t (if indent (newline-and-indent) (newline))))))
  18298. (defun org-return-indent ()
  18299. "Goto next table row or insert a newline and indent.
  18300. Calls `org-table-next-row' or `newline-and-indent', depending on
  18301. context. See the individual commands for more information."
  18302. (interactive)
  18303. (org-return t))
  18304. (defun org-ctrl-c-star ()
  18305. "Compute table, or change heading status of lines.
  18306. Calls `org-table-recalculate' or `org-toggle-heading',
  18307. depending on context."
  18308. (interactive)
  18309. (cond
  18310. ((org-at-table-p)
  18311. (call-interactively 'org-table-recalculate))
  18312. (t
  18313. ;; Convert all lines in region to list items
  18314. (call-interactively 'org-toggle-heading))))
  18315. (defun org-ctrl-c-minus ()
  18316. "Insert separator line in table or modify bullet status of line.
  18317. Also turns a plain line or a region of lines into list items.
  18318. Calls `org-table-insert-hline', `org-toggle-item', or
  18319. `org-cycle-list-bullet', depending on context."
  18320. (interactive)
  18321. (cond
  18322. ((org-at-table-p)
  18323. (call-interactively 'org-table-insert-hline))
  18324. ((org-region-active-p)
  18325. (call-interactively 'org-toggle-item))
  18326. ((org-in-item-p)
  18327. (call-interactively 'org-cycle-list-bullet))
  18328. (t
  18329. (call-interactively 'org-toggle-item))))
  18330. (defun org-toggle-item (arg)
  18331. "Convert headings or normal lines to items, items to normal lines.
  18332. If there is no active region, only the current line is considered.
  18333. If the first non blank line in the region is an headline, convert
  18334. all headlines to items, shifting text accordingly.
  18335. If it is an item, convert all items to normal lines.
  18336. If it is normal text, change region into an item. With a prefix
  18337. argument ARG, change each line in region into an item."
  18338. (interactive "P")
  18339. (let ((shift-text
  18340. (function
  18341. ;; Shift text in current section to IND, from point to END.
  18342. ;; The function leaves point to END line.
  18343. (lambda (ind end)
  18344. (let ((min-i 1000) (end (copy-marker end)))
  18345. ;; First determine the minimum indentation (MIN-I) of
  18346. ;; the text.
  18347. (save-excursion
  18348. (catch 'exit
  18349. (while (< (point) end)
  18350. (let ((i (org-get-indentation)))
  18351. (cond
  18352. ;; Skip blank lines and inline tasks.
  18353. ((looking-at "^[ \t]*$"))
  18354. ((looking-at org-outline-regexp-bol))
  18355. ;; We can't find less than 0 indentation.
  18356. ((zerop i) (throw 'exit (setq min-i 0)))
  18357. ((< i min-i) (setq min-i i))))
  18358. (forward-line))))
  18359. ;; Then indent each line so that a line indented to
  18360. ;; MIN-I becomes indented to IND. Ignore blank lines
  18361. ;; and inline tasks in the process.
  18362. (let ((delta (- ind min-i)))
  18363. (while (< (point) end)
  18364. (unless (or (looking-at "^[ \t]*$")
  18365. (looking-at org-outline-regexp-bol))
  18366. (org-indent-line-to (+ (org-get-indentation) delta)))
  18367. (forward-line)))))))
  18368. (skip-blanks
  18369. (function
  18370. ;; Return beginning of first non-blank line, starting from
  18371. ;; line at POS.
  18372. (lambda (pos)
  18373. (save-excursion
  18374. (goto-char pos)
  18375. (skip-chars-forward " \r\t\n")
  18376. (point-at-bol)))))
  18377. beg end)
  18378. ;; Determine boundaries of changes.
  18379. (if (org-region-active-p)
  18380. (setq beg (funcall skip-blanks (region-beginning))
  18381. end (copy-marker (region-end)))
  18382. (setq beg (funcall skip-blanks (point-at-bol))
  18383. end (copy-marker (point-at-eol))))
  18384. ;; Depending on the starting line, choose an action on the text
  18385. ;; between BEG and END.
  18386. (org-with-limited-levels
  18387. (save-excursion
  18388. (goto-char beg)
  18389. (cond
  18390. ;; Case 1. Start at an item: de-itemize. Note that it only
  18391. ;; happens when a region is active: `org-ctrl-c-minus'
  18392. ;; would call `org-cycle-list-bullet' otherwise.
  18393. ((org-at-item-p)
  18394. (while (< (point) end)
  18395. (when (org-at-item-p)
  18396. (skip-chars-forward " \t")
  18397. (delete-region (point) (match-end 0)))
  18398. (forward-line)))
  18399. ;; Case 2. Start at an heading: convert to items.
  18400. ((org-at-heading-p)
  18401. (let* ((bul (org-list-bullet-string "-"))
  18402. (bul-len (length bul))
  18403. ;; Indentation of the first heading. It should be
  18404. ;; relative to the indentation of its parent, if any.
  18405. (start-ind (save-excursion
  18406. (cond
  18407. ((not org-adapt-indentation) 0)
  18408. ((not (outline-previous-heading)) 0)
  18409. (t (length (match-string 0))))))
  18410. ;; Level of first heading. Further headings will be
  18411. ;; compared to it to determine hierarchy in the list.
  18412. (ref-level (org-reduced-level (org-outline-level))))
  18413. (while (< (point) end)
  18414. (let* ((level (org-reduced-level (org-outline-level)))
  18415. (delta (max 0 (- level ref-level))))
  18416. ;; If current headline is less indented than the first
  18417. ;; one, set it as reference, in order to preserve
  18418. ;; subtrees.
  18419. (when (< level ref-level) (setq ref-level level))
  18420. (replace-match bul t t)
  18421. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18422. ;; Ensure all text down to END (or SECTION-END) belongs
  18423. ;; to the newly created item.
  18424. (let ((section-end (save-excursion
  18425. (or (outline-next-heading) (point)))))
  18426. (forward-line)
  18427. (funcall shift-text
  18428. (+ start-ind (* (1+ delta) bul-len))
  18429. (min end section-end)))))))
  18430. ;; Case 3. Normal line with ARG: turn each non-item line into
  18431. ;; an item.
  18432. (arg
  18433. (while (< (point) end)
  18434. (unless (or (org-at-heading-p) (org-at-item-p))
  18435. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18436. (replace-match
  18437. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18438. (forward-line)))
  18439. ;; Case 4. Normal line without ARG: make the first line of
  18440. ;; region an item, and shift indentation of others
  18441. ;; lines to set them as item's body.
  18442. (t (let* ((bul (org-list-bullet-string "-"))
  18443. (bul-len (length bul))
  18444. (ref-ind (org-get-indentation)))
  18445. (skip-chars-forward " \t")
  18446. (insert bul)
  18447. (forward-line)
  18448. (while (< (point) end)
  18449. ;; Ensure that lines less indented than first one
  18450. ;; still get included in item body.
  18451. (funcall shift-text
  18452. (+ ref-ind bul-len)
  18453. (min end (save-excursion (or (outline-next-heading)
  18454. (point)))))
  18455. (forward-line)))))))))
  18456. (defun org-toggle-heading (&optional nstars)
  18457. "Convert headings to normal text, or items or text to headings.
  18458. If there is no active region, only the current line is considered.
  18459. With a \\[universal-argument] prefix, convert the whole list at
  18460. point into heading.
  18461. In a region:
  18462. - If the first non blank line is an headline, remove the stars
  18463. from all headlines in the region.
  18464. - If it is a normal line turn each and every normal line (i.e. not an
  18465. heading or an item) in the region into a heading.
  18466. - If it is a plain list item, turn all plain list items into headings.
  18467. When converting a line into a heading, the number of stars is chosen
  18468. such that the lines become children of the current entry. However,
  18469. when a prefix argument is given, its value determines the number of
  18470. stars to add."
  18471. (interactive "P")
  18472. (let ((skip-blanks
  18473. (function
  18474. ;; Return beginning of first non-blank line, starting from
  18475. ;; line at POS.
  18476. (lambda (pos)
  18477. (save-excursion
  18478. (goto-char pos)
  18479. (while (org-at-comment-p) (forward-line))
  18480. (skip-chars-forward " \r\t\n")
  18481. (point-at-bol)))))
  18482. beg end toggled)
  18483. ;; Determine boundaries of changes. If a universal prefix has
  18484. ;; been given, put the list in a region. If region ends at a bol,
  18485. ;; do not consider the last line to be in the region.
  18486. (when (and current-prefix-arg (org-at-item-p))
  18487. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18488. (org-mark-element))
  18489. (if (org-region-active-p)
  18490. (setq beg (funcall skip-blanks (region-beginning))
  18491. end (copy-marker (save-excursion
  18492. (goto-char (region-end))
  18493. (if (bolp) (point) (point-at-eol)))))
  18494. (setq beg (funcall skip-blanks (point-at-bol))
  18495. end (copy-marker (point-at-eol))))
  18496. ;; Ensure inline tasks don't count as headings.
  18497. (org-with-limited-levels
  18498. (save-excursion
  18499. (goto-char beg)
  18500. (cond
  18501. ;; Case 1. Started at an heading: de-star headings.
  18502. ((org-at-heading-p)
  18503. (while (< (point) end)
  18504. (when (org-at-heading-p t)
  18505. (looking-at org-outline-regexp) (replace-match "")
  18506. (setq toggled t))
  18507. (forward-line)))
  18508. ;; Case 2. Started at an item: change items into headlines.
  18509. ;; One star will be added by `org-list-to-subtree'.
  18510. ((org-at-item-p)
  18511. (let* ((stars (make-string
  18512. (if nstars
  18513. ;; subtract the star that will be added again by
  18514. ;; `org-list-to-subtree'
  18515. (1- (prefix-numeric-value current-prefix-arg))
  18516. (or (org-current-level) 0))
  18517. ?*))
  18518. (add-stars
  18519. (cond (nstars "") ; stars from prefix only
  18520. ((equal stars "") "") ; before first heading
  18521. (org-odd-levels-only "*") ; inside heading, odd
  18522. (t "")))) ; inside heading, oddeven
  18523. (while (< (point) end)
  18524. (when (org-at-item-p)
  18525. ;; Pay attention to cases when region ends before list.
  18526. (let* ((struct (org-list-struct))
  18527. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18528. (save-restriction
  18529. (narrow-to-region (point) list-end)
  18530. (insert
  18531. (org-list-to-subtree
  18532. (org-list-parse-list t)
  18533. '(:istart (concat stars add-stars (funcall get-stars depth))
  18534. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18535. (setq toggled t))
  18536. (forward-line))))
  18537. ;; Case 3. Started at normal text: make every line an heading,
  18538. ;; skipping headlines and items.
  18539. (t (let* ((stars (make-string
  18540. (if nstars
  18541. (prefix-numeric-value current-prefix-arg)
  18542. (or (org-current-level) 0))
  18543. ?*))
  18544. (add-stars
  18545. (cond (nstars "") ; stars from prefix only
  18546. ((equal stars "") "*") ; before first heading
  18547. (org-odd-levels-only "**") ; inside heading, odd
  18548. (t "*"))) ; inside heading, oddeven
  18549. (rpl (concat stars add-stars " ")))
  18550. (while (< (point) end)
  18551. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18552. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18553. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18554. (forward-line)))))))
  18555. (unless toggled (message "Cannot toggle heading from here"))))
  18556. (defun org-meta-return (&optional arg)
  18557. "Insert a new heading or wrap a region in a table.
  18558. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18559. See the individual commands for more information."
  18560. (interactive "P")
  18561. (cond
  18562. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18563. ((or (org-at-drawer-p) (org-at-property-p))
  18564. (newline-and-indent))
  18565. ((org-at-table-p)
  18566. (call-interactively 'org-table-wrap-region))
  18567. (t (call-interactively 'org-insert-heading))))
  18568. ;;; Menu entries
  18569. (defsubst org-in-subtree-not-table-p ()
  18570. "Are we in a subtree and not in a table?"
  18571. (and (not (org-before-first-heading-p))
  18572. (not (org-at-table-p))))
  18573. ;; Define the Org-mode menus
  18574. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18575. '("Tbl"
  18576. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18577. ["Next Field" org-cycle (org-at-table-p)]
  18578. ["Previous Field" org-shifttab (org-at-table-p)]
  18579. ["Next Row" org-return (org-at-table-p)]
  18580. "--"
  18581. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18582. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18583. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18584. "--"
  18585. ("Column"
  18586. ["Move Column Left" org-metaleft (org-at-table-p)]
  18587. ["Move Column Right" org-metaright (org-at-table-p)]
  18588. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18589. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18590. ("Row"
  18591. ["Move Row Up" org-metaup (org-at-table-p)]
  18592. ["Move Row Down" org-metadown (org-at-table-p)]
  18593. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18594. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18595. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18596. "--"
  18597. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18598. ("Rectangle"
  18599. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18600. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18601. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18602. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18603. "--"
  18604. ("Calculate"
  18605. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18606. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18607. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18608. "--"
  18609. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18610. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18611. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18612. "--"
  18613. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18614. "--"
  18615. ["Sum Column/Rectangle" org-table-sum
  18616. (or (org-at-table-p) (org-region-active-p))]
  18617. ["Which Column?" org-table-current-column (org-at-table-p)])
  18618. ["Debug Formulas"
  18619. org-table-toggle-formula-debugger
  18620. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18621. ["Show Col/Row Numbers"
  18622. org-table-toggle-coordinate-overlays
  18623. :style toggle
  18624. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18625. "--"
  18626. ["Create" org-table-create (and (not (org-at-table-p))
  18627. org-enable-table-editor)]
  18628. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18629. ["Import from File" org-table-import (not (org-at-table-p))]
  18630. ["Export to File" org-table-export (org-at-table-p)]
  18631. "--"
  18632. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18633. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18634. '("Org"
  18635. ("Show/Hide"
  18636. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18637. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18638. ["Sparse Tree..." org-sparse-tree t]
  18639. ["Reveal Context" org-reveal t]
  18640. ["Show All" show-all t]
  18641. "--"
  18642. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18643. "--"
  18644. ["New Heading" org-insert-heading t]
  18645. ("Navigate Headings"
  18646. ["Up" outline-up-heading t]
  18647. ["Next" outline-next-visible-heading t]
  18648. ["Previous" outline-previous-visible-heading t]
  18649. ["Next Same Level" outline-forward-same-level t]
  18650. ["Previous Same Level" outline-backward-same-level t]
  18651. "--"
  18652. ["Jump" org-goto t])
  18653. ("Edit Structure"
  18654. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18655. "--"
  18656. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18657. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18658. "--"
  18659. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18660. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18661. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18662. "--"
  18663. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18664. "--"
  18665. ["Copy visible text" org-copy-visible t]
  18666. "--"
  18667. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18668. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18669. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18670. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18671. "--"
  18672. ["Sort Region/Children" org-sort t]
  18673. "--"
  18674. ["Convert to odd levels" org-convert-to-odd-levels t]
  18675. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18676. ("Editing"
  18677. ["Emphasis..." org-emphasize t]
  18678. ["Edit Source Example" org-edit-special t]
  18679. "--"
  18680. ["Footnote new/jump" org-footnote-action t]
  18681. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18682. ("Archive"
  18683. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18684. "--"
  18685. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18686. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18687. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18688. )
  18689. "--"
  18690. ("Hyperlinks"
  18691. ["Store Link (Global)" org-store-link t]
  18692. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18693. ["Insert Link" org-insert-link t]
  18694. ["Follow Link" org-open-at-point t]
  18695. "--"
  18696. ["Next link" org-next-link t]
  18697. ["Previous link" org-previous-link t]
  18698. "--"
  18699. ["Descriptive Links"
  18700. org-toggle-link-display
  18701. :style radio
  18702. :selected org-descriptive-links
  18703. ]
  18704. ["Literal Links"
  18705. org-toggle-link-display
  18706. :style radio
  18707. :selected (not org-descriptive-links)])
  18708. "--"
  18709. ("TODO Lists"
  18710. ["TODO/DONE/-" org-todo t]
  18711. ("Select keyword"
  18712. ["Next keyword" org-shiftright (org-at-heading-p)]
  18713. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18714. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18715. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18716. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18717. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18718. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18719. "--"
  18720. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18721. :selected org-enforce-todo-dependencies :style toggle :active t]
  18722. "Settings for tree at point"
  18723. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18724. :selected (org-entry-get nil "ORDERED")
  18725. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18726. ["Do Children parallel" org-toggle-ordered-property :style radio
  18727. :selected (not (org-entry-get nil "ORDERED"))
  18728. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18729. "--"
  18730. ["Set Priority" org-priority t]
  18731. ["Priority Up" org-shiftup t]
  18732. ["Priority Down" org-shiftdown t]
  18733. "--"
  18734. ["Get news from all feeds" org-feed-update-all t]
  18735. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18736. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18737. ("TAGS and Properties"
  18738. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18739. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18740. "--"
  18741. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18742. ["Column view of properties" org-columns t]
  18743. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18744. ("Dates and Scheduling"
  18745. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18746. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18747. ("Change Date"
  18748. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18749. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18750. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18751. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18752. ["Compute Time Range" org-evaluate-time-range t]
  18753. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18754. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18755. "--"
  18756. ["Custom time format" org-toggle-time-stamp-overlays
  18757. :style radio :selected org-display-custom-times]
  18758. "--"
  18759. ["Goto Calendar" org-goto-calendar t]
  18760. ["Date from Calendar" org-date-from-calendar t]
  18761. "--"
  18762. ["Start/Restart Timer" org-timer-start t]
  18763. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18764. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18765. ["Insert Timer String" org-timer t]
  18766. ["Insert Timer Item" org-timer-item t])
  18767. ("Logging work"
  18768. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18769. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18770. ["Clock out" org-clock-out t]
  18771. ["Clock cancel" org-clock-cancel t]
  18772. "--"
  18773. ["Mark as default task" org-clock-mark-default-task t]
  18774. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18775. ["Goto running clock" org-clock-goto t]
  18776. "--"
  18777. ["Display times" org-clock-display t]
  18778. ["Create clock table" org-clock-report t]
  18779. "--"
  18780. ["Record DONE time"
  18781. (progn (setq org-log-done (not org-log-done))
  18782. (message "Switching to %s will %s record a timestamp"
  18783. (car org-done-keywords)
  18784. (if org-log-done "automatically" "not")))
  18785. :style toggle :selected org-log-done])
  18786. "--"
  18787. ["Agenda Command..." org-agenda t]
  18788. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18789. ("File List for Agenda")
  18790. ("Special views current file"
  18791. ["TODO Tree" org-show-todo-tree t]
  18792. ["Check Deadlines" org-check-deadlines t]
  18793. ["Timeline" org-timeline t]
  18794. ["Tags/Property tree" org-match-sparse-tree t])
  18795. "--"
  18796. ["Export/Publish..." org-export-dispatch t]
  18797. ("LaTeX"
  18798. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18799. :selected org-cdlatex-mode]
  18800. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18801. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18802. ["Modify math symbol" org-cdlatex-math-modify
  18803. (org-inside-LaTeX-fragment-p)]
  18804. ["Insert citation" org-reftex-citation t]
  18805. "--"
  18806. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  18807. "--"
  18808. ("MobileOrg"
  18809. ["Push Files and Views" org-mobile-push t]
  18810. ["Get Captured and Flagged" org-mobile-pull t]
  18811. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18812. "--"
  18813. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18814. "--"
  18815. ("Documentation"
  18816. ["Show Version" org-version t]
  18817. ["Info Documentation" org-info t])
  18818. ("Customize"
  18819. ["Browse Org Group" org-customize t]
  18820. "--"
  18821. ["Expand This Menu" org-create-customize-menu
  18822. (fboundp 'customize-menu-create)])
  18823. ["Send bug report" org-submit-bug-report t]
  18824. "--"
  18825. ("Refresh/Reload"
  18826. ["Refresh setup current buffer" org-mode-restart t]
  18827. ["Reload Org (after update)" org-reload t]
  18828. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  18829. ))
  18830. (defun org-info (&optional node)
  18831. "Read documentation for Org-mode in the info system.
  18832. With optional NODE, go directly to that node."
  18833. (interactive)
  18834. (info (format "(org)%s" (or node ""))))
  18835. ;;;###autoload
  18836. (defun org-submit-bug-report ()
  18837. "Submit a bug report on Org-mode via mail.
  18838. Don't hesitate to report any problems or inaccurate documentation.
  18839. If you don't have setup sending mail from (X)Emacs, please copy the
  18840. output buffer into your mail program, as it gives us important
  18841. information about your Org-mode version and configuration."
  18842. (interactive)
  18843. (require 'reporter)
  18844. (org-load-modules-maybe)
  18845. (org-require-autoloaded-modules)
  18846. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18847. (reporter-submit-bug-report
  18848. "emacs-orgmode@gnu.org"
  18849. (org-version nil 'full)
  18850. (let (list)
  18851. (save-window-excursion
  18852. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18853. (delete-other-windows)
  18854. (erase-buffer)
  18855. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18856. We would like to add your full Org-mode and Outline configuration to the
  18857. bug report. This greatly simplifies the work of the maintainer and
  18858. other experts on the mailing list.
  18859. HOWEVER, some variables you have customized may contain private
  18860. information. The names of customers, colleagues, or friends, might
  18861. appear in the form of file names, tags, todo states, or search strings.
  18862. If you answer yes to the prompt, you might want to check and remove
  18863. such private information before sending the email.")
  18864. (add-text-properties (point-min) (point-max) '(face org-warning))
  18865. (when (yes-or-no-p "Include your Org-mode configuration ")
  18866. (mapatoms
  18867. (lambda (v)
  18868. (and (boundp v)
  18869. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18870. (or (and (symbol-value v)
  18871. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18872. (and
  18873. (get v 'custom-type) (get v 'standard-value)
  18874. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18875. (push v list)))))
  18876. (kill-buffer (get-buffer "*Warn about privacy*"))
  18877. list))
  18878. nil nil
  18879. "Remember to cover the basics, that is, what you expected to happen and
  18880. what in fact did happen. You don't know how to make a good report? See
  18881. http://orgmode.org/manual/Feedback.html#Feedback
  18882. Your bug report will be posted to the Org-mode mailing list.
  18883. ------------------------------------------------------------------------")
  18884. (save-excursion
  18885. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18886. (replace-match "\\1Bug: \\3 [\\2]")))))
  18887. (defun org-install-agenda-files-menu ()
  18888. (let ((bl (buffer-list)))
  18889. (save-excursion
  18890. (while bl
  18891. (set-buffer (pop bl))
  18892. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18893. (when (derived-mode-p 'org-mode)
  18894. (easy-menu-change
  18895. '("Org") "File List for Agenda"
  18896. (append
  18897. (list
  18898. ["Edit File List" (org-edit-agenda-file-list) t]
  18899. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18900. ["Remove Current File from List" org-remove-file t]
  18901. ["Cycle through agenda files" org-cycle-agenda-files t]
  18902. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18903. "--")
  18904. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18905. ;;;; Documentation
  18906. (defun org-require-autoloaded-modules ()
  18907. (interactive)
  18908. (mapc 'require
  18909. '(org-agenda org-archive org-attach org-clock org-colview org-id
  18910. org-remember org-table org-timer)))
  18911. ;;;###autoload
  18912. (defun org-reload (&optional uncompiled)
  18913. "Reload all org lisp files.
  18914. With prefix arg UNCOMPILED, load the uncompiled versions."
  18915. (interactive "P")
  18916. (require 'loadhist)
  18917. (let* ((org-dir (org-find-library-dir "org"))
  18918. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18919. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  18920. (remove-re (mapconcat 'identity
  18921. (mapcar (lambda (f) (concat "^" f "$"))
  18922. (list (if (featurep 'xemacs)
  18923. "org-colview"
  18924. "org-colview-xemacs")
  18925. "org" "org-loaddefs" "org-version"))
  18926. "\\|"))
  18927. (feats (delete-dups
  18928. (mapcar 'file-name-sans-extension
  18929. (mapcar 'file-name-nondirectory
  18930. (delq nil
  18931. (mapcar 'feature-file
  18932. features))))))
  18933. (lfeat (append
  18934. (sort
  18935. (setq feats
  18936. (delq nil (mapcar
  18937. (lambda (f)
  18938. (if (and (string-match feature-re f)
  18939. (not (string-match remove-re f)))
  18940. f nil))
  18941. feats)))
  18942. 'string-lessp)
  18943. (list "org-version" "org")))
  18944. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  18945. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18946. load-uncore load-misses)
  18947. (setq load-misses
  18948. (delq 't
  18949. (mapcar (lambda (f)
  18950. (or (org-load-noerror-mustsuffix (concat org-dir f))
  18951. (and (string= org-dir contrib-dir)
  18952. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  18953. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  18954. (add-to-list 'load-uncore f 'append)
  18955. 't)
  18956. f))
  18957. lfeat)))
  18958. (if load-uncore
  18959. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18960. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18961. (if load-misses
  18962. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18963. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18964. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18965. ;;;###autoload
  18966. (defun org-customize ()
  18967. "Call the customize function with org as argument."
  18968. (interactive)
  18969. (org-load-modules-maybe)
  18970. (org-require-autoloaded-modules)
  18971. (customize-browse 'org))
  18972. (defun org-create-customize-menu ()
  18973. "Create a full customization menu for Org-mode, insert it into the menu."
  18974. (interactive)
  18975. (org-load-modules-maybe)
  18976. (org-require-autoloaded-modules)
  18977. (if (fboundp 'customize-menu-create)
  18978. (progn
  18979. (easy-menu-change
  18980. '("Org") "Customize"
  18981. `(["Browse Org group" org-customize t]
  18982. "--"
  18983. ,(customize-menu-create 'org)
  18984. ["Set" Custom-set t]
  18985. ["Save" Custom-save t]
  18986. ["Reset to Current" Custom-reset-current t]
  18987. ["Reset to Saved" Custom-reset-saved t]
  18988. ["Reset to Standard Settings" Custom-reset-standard t]))
  18989. (message "\"Org\"-menu now contains full customization menu"))
  18990. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18991. ;;;; Miscellaneous stuff
  18992. ;;; Generally useful functions
  18993. (defun org-get-at-bol (property)
  18994. "Get text property PROPERTY at beginning of line."
  18995. (get-text-property (point-at-bol) property))
  18996. (defun org-find-text-property-in-string (prop s)
  18997. "Return the first non-nil value of property PROP in string S."
  18998. (or (get-text-property 0 prop s)
  18999. (get-text-property (or (next-single-property-change 0 prop s) 0)
  19000. prop s)))
  19001. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  19002. "Display the given MESSAGE as a warning."
  19003. (if (fboundp 'display-warning)
  19004. (display-warning 'org message
  19005. (if (featurep 'xemacs) 'warning :warning))
  19006. (let ((buf (get-buffer-create "*Org warnings*")))
  19007. (with-current-buffer buf
  19008. (goto-char (point-max))
  19009. (insert "Warning (Org): " message)
  19010. (unless (bolp)
  19011. (newline)))
  19012. (display-buffer buf)
  19013. (sit-for 0))))
  19014. (defun org-eval (form)
  19015. "Eval FORM and return result."
  19016. (condition-case error
  19017. (eval form)
  19018. (error (format "%%![Error: %s]" error))))
  19019. (defun org-in-clocktable-p ()
  19020. "Check if the cursor is in a clocktable."
  19021. (let ((pos (point)) start)
  19022. (save-excursion
  19023. (end-of-line 1)
  19024. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  19025. (setq start (match-beginning 0))
  19026. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  19027. (>= (match-end 0) pos)
  19028. start))))
  19029. (defun org-in-commented-line ()
  19030. "Is point in a line starting with `#'?"
  19031. (equal (char-after (point-at-bol)) ?#))
  19032. (defun org-in-indented-comment-line ()
  19033. "Is point in a line starting with `#' after some white space?"
  19034. (save-excursion
  19035. (save-match-data
  19036. (goto-char (point-at-bol))
  19037. (looking-at "[ \t]*#"))))
  19038. (defun org-in-verbatim-emphasis ()
  19039. (save-match-data
  19040. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  19041. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  19042. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  19043. (if (and marker (marker-buffer marker)
  19044. (buffer-live-p (marker-buffer marker)))
  19045. (progn
  19046. (org-pop-to-buffer-same-window (marker-buffer marker))
  19047. (if (or (> marker (point-max)) (< marker (point-min)))
  19048. (widen))
  19049. (goto-char marker)
  19050. (org-show-context 'org-goto))
  19051. (if bookmark
  19052. (bookmark-jump bookmark)
  19053. (error "Cannot find location"))))
  19054. (defun org-quote-csv-field (s)
  19055. "Quote field for inclusion in CSV material."
  19056. (if (string-match "[\",]" s)
  19057. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  19058. s))
  19059. (defun org-force-self-insert (N)
  19060. "Needed to enforce self-insert under remapping."
  19061. (interactive "p")
  19062. (self-insert-command N))
  19063. (defun org-string-width (s)
  19064. "Compute width of string, ignoring invisible characters.
  19065. This ignores character with invisibility property `org-link', and also
  19066. characters with property `org-cwidth', because these will become invisible
  19067. upon the next fontification round."
  19068. (let (b l)
  19069. (when (or (eq t buffer-invisibility-spec)
  19070. (assq 'org-link buffer-invisibility-spec))
  19071. (while (setq b (text-property-any 0 (length s)
  19072. 'invisible 'org-link s))
  19073. (setq s (concat (substring s 0 b)
  19074. (substring s (or (next-single-property-change
  19075. b 'invisible s) (length s)))))))
  19076. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  19077. (setq s (concat (substring s 0 b)
  19078. (substring s (or (next-single-property-change
  19079. b 'org-cwidth s) (length s))))))
  19080. (setq l (string-width s) b -1)
  19081. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  19082. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  19083. l))
  19084. (defun org-shorten-string (s maxlength)
  19085. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  19086. If the string is shorter or has length MAXLENGTH, just return the
  19087. original string. If it is longer, the functions finds a space in the
  19088. string, breaks this string off at that locations and adds three dots
  19089. as ellipsis. Including the ellipsis, the string will not be longer
  19090. than MAXLENGTH. If finding a good breaking point in the string does
  19091. not work, the string is just chopped off in the middle of a word
  19092. if necessary."
  19093. (if (<= (length s) maxlength)
  19094. s
  19095. (let* ((n (max (- maxlength 4) 1))
  19096. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  19097. (if (string-match re s)
  19098. (concat (match-string 1 s) "...")
  19099. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  19100. (defun org-get-indentation (&optional line)
  19101. "Get the indentation of the current line, interpreting tabs.
  19102. When LINE is given, assume it represents a line and compute its indentation."
  19103. (if line
  19104. (if (string-match "^ *" (org-remove-tabs line))
  19105. (match-end 0))
  19106. (save-excursion
  19107. (beginning-of-line 1)
  19108. (skip-chars-forward " \t")
  19109. (current-column))))
  19110. (defun org-get-string-indentation (s)
  19111. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  19112. (let ((n -1) (i 0) (w tab-width) c)
  19113. (catch 'exit
  19114. (while (< (setq n (1+ n)) (length s))
  19115. (setq c (aref s n))
  19116. (cond ((= c ?\ ) (setq i (1+ i)))
  19117. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  19118. (t (throw 'exit t)))))
  19119. i))
  19120. (defun org-remove-tabs (s &optional width)
  19121. "Replace tabulators in S with spaces.
  19122. Assumes that s is a single line, starting in column 0."
  19123. (setq width (or width tab-width))
  19124. (while (string-match "\t" s)
  19125. (setq s (replace-match
  19126. (make-string
  19127. (- (* width (/ (+ (match-beginning 0) width) width))
  19128. (match-beginning 0)) ?\ )
  19129. t t s)))
  19130. s)
  19131. (defun org-fix-indentation (line ind)
  19132. "Fix indentation in LINE.
  19133. IND is a cons cell with target and minimum indentation.
  19134. If the current indentation in LINE is smaller than the minimum,
  19135. leave it alone. If it is larger than ind, set it to the target."
  19136. (let* ((l (org-remove-tabs line))
  19137. (i (org-get-indentation l))
  19138. (i1 (car ind)) (i2 (cdr ind)))
  19139. (if (>= i i2) (setq l (substring line i2)))
  19140. (if (> i1 0)
  19141. (concat (make-string i1 ?\ ) l)
  19142. l)))
  19143. (defun org-remove-indentation (code &optional n)
  19144. "Remove the maximum common indentation from the lines in CODE.
  19145. N may optionally be the number of spaces to remove."
  19146. (with-temp-buffer
  19147. (insert code)
  19148. (org-do-remove-indentation n)
  19149. (buffer-string)))
  19150. (defun org-do-remove-indentation (&optional n)
  19151. "Remove the maximum common indentation from the buffer."
  19152. (untabify (point-min) (point-max))
  19153. (let ((min 10000) re)
  19154. (if n
  19155. (setq min n)
  19156. (goto-char (point-min))
  19157. (while (re-search-forward "^ *[^ \n]" nil t)
  19158. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19159. (unless (or (= min 0) (= min 10000))
  19160. (setq re (format "^ \\{%d\\}" min))
  19161. (goto-char (point-min))
  19162. (while (re-search-forward re nil t)
  19163. (replace-match "")
  19164. (end-of-line 1))
  19165. min)))
  19166. (defun org-fill-template (template alist)
  19167. "Find each %key of ALIST in TEMPLATE and replace it."
  19168. (let ((case-fold-search nil)
  19169. entry key value)
  19170. (setq alist (sort (copy-sequence alist)
  19171. (lambda (a b) (< (length (car a)) (length (car b))))))
  19172. (while (setq entry (pop alist))
  19173. (setq template
  19174. (replace-regexp-in-string
  19175. (concat "%" (regexp-quote (car entry)))
  19176. (or (cdr entry) "") template t t)))
  19177. template))
  19178. (defun org-base-buffer (buffer)
  19179. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19180. (if (not buffer)
  19181. buffer
  19182. (or (buffer-base-buffer buffer)
  19183. buffer)))
  19184. (defun org-trim (s)
  19185. "Remove whitespace at beginning and end of string."
  19186. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19187. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19188. s)
  19189. (defun org-wrap (string &optional width lines)
  19190. "Wrap string to either a number of lines, or a width in characters.
  19191. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19192. that costs. If there is a word longer than WIDTH, the text is actually
  19193. wrapped to the length of that word.
  19194. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19195. many lines, whatever width that takes.
  19196. The return value is a list of lines, without newlines at the end."
  19197. (let* ((words (org-split-string string "[ \t\n]+"))
  19198. (maxword (apply 'max (mapcar 'org-string-width words)))
  19199. w ll)
  19200. (cond (width
  19201. (org-do-wrap words (max maxword width)))
  19202. (lines
  19203. (setq w maxword)
  19204. (setq ll (org-do-wrap words maxword))
  19205. (if (<= (length ll) lines)
  19206. ll
  19207. (setq ll words)
  19208. (while (> (length ll) lines)
  19209. (setq w (1+ w))
  19210. (setq ll (org-do-wrap words w)))
  19211. ll))
  19212. (t (error "Cannot wrap this")))))
  19213. (defun org-do-wrap (words width)
  19214. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19215. (let (lines line)
  19216. (while words
  19217. (setq line (pop words))
  19218. (while (and words (< (+ (length line) (length (car words))) width))
  19219. (setq line (concat line " " (pop words))))
  19220. (setq lines (push line lines)))
  19221. (nreverse lines)))
  19222. (defun org-split-string (string &optional separators)
  19223. "Splits STRING into substrings at SEPARATORS.
  19224. No empty strings are returned if there are matches at the beginning
  19225. and end of string."
  19226. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19227. (start 0)
  19228. notfirst
  19229. (list nil))
  19230. (while (and (string-match rexp string
  19231. (if (and notfirst
  19232. (= start (match-beginning 0))
  19233. (< start (length string)))
  19234. (1+ start) start))
  19235. (< (match-beginning 0) (length string)))
  19236. (setq notfirst t)
  19237. (or (eq (match-beginning 0) 0)
  19238. (and (eq (match-beginning 0) (match-end 0))
  19239. (eq (match-beginning 0) start))
  19240. (setq list
  19241. (cons (substring string start (match-beginning 0))
  19242. list)))
  19243. (setq start (match-end 0)))
  19244. (or (eq start (length string))
  19245. (setq list
  19246. (cons (substring string start)
  19247. list)))
  19248. (nreverse list)))
  19249. (defun org-quote-vert (s)
  19250. "Replace \"|\" with \"\\vert\"."
  19251. (while (string-match "|" s)
  19252. (setq s (replace-match "\\vert" t t s)))
  19253. s)
  19254. (defun org-uuidgen-p (s)
  19255. "Is S an ID created by UUIDGEN?"
  19256. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19257. (defun org-in-src-block-p (&optional inside)
  19258. "Whether point is in a code source block.
  19259. When INSIDE is non-nil, don't consider we are within a src block
  19260. when point is at #+BEGIN_SRC or #+END_SRC."
  19261. (let ((case-fold-search t) ov)
  19262. (or (and (setq ov (overlays-at (point)))
  19263. (memq 'org-block-background
  19264. (overlay-properties (car ov))))
  19265. (and (not inside)
  19266. (save-match-data
  19267. (save-excursion
  19268. (beginning-of-line)
  19269. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19270. (defun org-context ()
  19271. "Return a list of contexts of the current cursor position.
  19272. If several contexts apply, all are returned.
  19273. Each context entry is a list with a symbol naming the context, and
  19274. two positions indicating start and end of the context. Possible
  19275. contexts are:
  19276. :headline anywhere in a headline
  19277. :headline-stars on the leading stars in a headline
  19278. :todo-keyword on a TODO keyword (including DONE) in a headline
  19279. :tags on the TAGS in a headline
  19280. :priority on the priority cookie in a headline
  19281. :item on the first line of a plain list item
  19282. :item-bullet on the bullet/number of a plain list item
  19283. :checkbox on the checkbox in a plain list item
  19284. :table in an org-mode table
  19285. :table-special on a special filed in a table
  19286. :table-table in a table.el table
  19287. :clocktable in a clocktable
  19288. :src-block in a source block
  19289. :link on a hyperlink
  19290. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19291. :target on a <<target>>
  19292. :radio-target on a <<<radio-target>>>
  19293. :latex-fragment on a LaTeX fragment
  19294. :latex-preview on a LaTeX fragment with overlaid preview image
  19295. This function expects the position to be visible because it uses font-lock
  19296. faces as a help to recognize the following contexts: :table-special, :link,
  19297. and :keyword."
  19298. (let* ((f (get-text-property (point) 'face))
  19299. (faces (if (listp f) f (list f)))
  19300. (case-fold-search t)
  19301. (p (point)) clist o)
  19302. ;; First the large context
  19303. (cond
  19304. ((org-at-heading-p t)
  19305. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19306. (when (progn
  19307. (beginning-of-line 1)
  19308. (looking-at org-todo-line-tags-regexp))
  19309. (push (org-point-in-group p 1 :headline-stars) clist)
  19310. (push (org-point-in-group p 2 :todo-keyword) clist)
  19311. (push (org-point-in-group p 4 :tags) clist))
  19312. (goto-char p)
  19313. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19314. (if (looking-at "\\[#[A-Z0-9]\\]")
  19315. (push (org-point-in-group p 0 :priority) clist)))
  19316. ((org-at-item-p)
  19317. (push (org-point-in-group p 2 :item-bullet) clist)
  19318. (push (list :item (point-at-bol)
  19319. (save-excursion (org-end-of-item) (point)))
  19320. clist)
  19321. (and (org-at-item-checkbox-p)
  19322. (push (org-point-in-group p 0 :checkbox) clist)))
  19323. ((org-at-table-p)
  19324. (push (list :table (org-table-begin) (org-table-end)) clist)
  19325. (if (memq 'org-formula faces)
  19326. (push (list :table-special
  19327. (previous-single-property-change p 'face)
  19328. (next-single-property-change p 'face)) clist)))
  19329. ((org-at-table-p 'any)
  19330. (push (list :table-table) clist)))
  19331. (goto-char p)
  19332. (let ((case-fold-search t))
  19333. ;; New the "medium" contexts: clocktables, source blocks
  19334. (cond ((org-in-clocktable-p)
  19335. (push (list :clocktable
  19336. (and (or (looking-at "#\\+BEGIN: clocktable")
  19337. (search-backward "#+BEGIN: clocktable" nil t))
  19338. (match-beginning 0))
  19339. (and (re-search-forward "#\\+END:?" nil t)
  19340. (match-end 0))) clist))
  19341. ((org-in-src-block-p)
  19342. (push (list :src-block
  19343. (and (or (looking-at "#\\+BEGIN_SRC")
  19344. (search-backward "#+BEGIN_SRC" nil t))
  19345. (match-beginning 0))
  19346. (and (search-forward "#+END_SRC" nil t)
  19347. (match-beginning 0))) clist))))
  19348. (goto-char p)
  19349. ;; Now the small context
  19350. (cond
  19351. ((org-at-timestamp-p)
  19352. (push (org-point-in-group p 0 :timestamp) clist))
  19353. ((memq 'org-link faces)
  19354. (push (list :link
  19355. (previous-single-property-change p 'face)
  19356. (next-single-property-change p 'face)) clist))
  19357. ((memq 'org-special-keyword faces)
  19358. (push (list :keyword
  19359. (previous-single-property-change p 'face)
  19360. (next-single-property-change p 'face)) clist))
  19361. ((org-at-target-p)
  19362. (push (org-point-in-group p 0 :target) clist)
  19363. (goto-char (1- (match-beginning 0)))
  19364. (if (looking-at org-radio-target-regexp)
  19365. (push (org-point-in-group p 0 :radio-target) clist))
  19366. (goto-char p))
  19367. ((setq o (car (delq nil
  19368. (mapcar
  19369. (lambda (x)
  19370. (if (memq x org-latex-fragment-image-overlays) x))
  19371. (overlays-at (point))))))
  19372. (push (list :latex-fragment
  19373. (overlay-start o) (overlay-end o)) clist)
  19374. (push (list :latex-preview
  19375. (overlay-start o) (overlay-end o)) clist))
  19376. ((org-inside-LaTeX-fragment-p)
  19377. ;; FIXME: positions wrong.
  19378. (push (list :latex-fragment (point) (point)) clist)))
  19379. (setq clist (nreverse (delq nil clist)))
  19380. clist))
  19381. ;; FIXME: Compare with at-regexp-p Do we need both?
  19382. (defun org-in-regexp (re &optional nlines visually)
  19383. "Check if point is inside a match of regexp.
  19384. Normally only the current line is checked, but you can include NLINES extra
  19385. lines both before and after point into the search.
  19386. If VISUALLY is set, require that the cursor is not after the match but
  19387. really on, so that the block visually is on the match."
  19388. (catch 'exit
  19389. (let ((pos (point))
  19390. (eol (point-at-eol (+ 1 (or nlines 0))))
  19391. (inc (if visually 1 0)))
  19392. (save-excursion
  19393. (beginning-of-line (- 1 (or nlines 0)))
  19394. (while (re-search-forward re eol t)
  19395. (if (and (<= (match-beginning 0) pos)
  19396. (>= (+ inc (match-end 0)) pos))
  19397. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19398. (defun org-at-regexp-p (regexp)
  19399. "Is point inside a match of REGEXP in the current line?"
  19400. (catch 'exit
  19401. (save-excursion
  19402. (let ((pos (point)) (end (point-at-eol)))
  19403. (beginning-of-line 1)
  19404. (while (re-search-forward regexp end t)
  19405. (if (and (<= (match-beginning 0) pos)
  19406. (>= (match-end 0) pos))
  19407. (throw 'exit t)))
  19408. nil))))
  19409. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19410. "Non-nil when point is between matches of START-RE and END-RE.
  19411. Also return a non-nil value when point is on one of the matches.
  19412. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19413. buffer positions. Default values are the positions of headlines
  19414. surrounding the point.
  19415. The functions returns a cons cell whose car (resp. cdr) is the
  19416. position before START-RE (resp. after END-RE)."
  19417. (save-match-data
  19418. (let ((pos (point))
  19419. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19420. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19421. beg end)
  19422. (save-excursion
  19423. ;; Point is on a block when on START-RE or if START-RE can be
  19424. ;; found before it...
  19425. (and (or (org-at-regexp-p start-re)
  19426. (re-search-backward start-re limit-up t))
  19427. (setq beg (match-beginning 0))
  19428. ;; ... and END-RE after it...
  19429. (goto-char (match-end 0))
  19430. (re-search-forward end-re limit-down t)
  19431. (> (setq end (match-end 0)) pos)
  19432. ;; ... without another START-RE in-between.
  19433. (goto-char (match-beginning 0))
  19434. (not (re-search-backward start-re (1+ beg) t))
  19435. ;; Return value.
  19436. (cons beg end))))))
  19437. (defun org-in-block-p (names)
  19438. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19439. NAMES is a list of strings containing names of blocks.
  19440. Return first block name matched, or nil. Beware that in case of
  19441. nested blocks, the returned name may not belong to the closest
  19442. block from point."
  19443. (save-match-data
  19444. (catch 'exit
  19445. (let ((case-fold-search t)
  19446. (lim-up (save-excursion (outline-previous-heading)))
  19447. (lim-down (save-excursion (outline-next-heading))))
  19448. (mapc (lambda (name)
  19449. (let ((n (regexp-quote name)))
  19450. (when (org-between-regexps-p
  19451. (concat "^[ \t]*#\\+begin_" n)
  19452. (concat "^[ \t]*#\\+end_" n)
  19453. lim-up lim-down)
  19454. (throw 'exit n))))
  19455. names))
  19456. nil)))
  19457. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19458. "Call `multi-occur' with buffers for all agenda files."
  19459. (interactive "sOrg-files matching: \np")
  19460. (let* ((files (org-agenda-files))
  19461. (tnames (mapcar 'file-truename files))
  19462. (extra org-agenda-text-search-extra-files)
  19463. f)
  19464. (when (eq (car extra) 'agenda-archives)
  19465. (setq extra (cdr extra))
  19466. (setq files (org-add-archive-files files)))
  19467. (while (setq f (pop extra))
  19468. (unless (member (file-truename f) tnames)
  19469. (add-to-list 'files f 'append)
  19470. (add-to-list 'tnames (file-truename f) 'append)))
  19471. (multi-occur
  19472. (mapcar (lambda (x)
  19473. (with-current-buffer
  19474. (or (get-file-buffer x) (find-file-noselect x))
  19475. (widen)
  19476. (current-buffer)))
  19477. files)
  19478. regexp)))
  19479. (if (boundp 'occur-mode-find-occurrence-hook)
  19480. ;; Emacs 23
  19481. (add-hook 'occur-mode-find-occurrence-hook
  19482. (lambda ()
  19483. (when (derived-mode-p 'org-mode)
  19484. (org-reveal))))
  19485. ;; Emacs 22
  19486. (defadvice occur-mode-goto-occurrence
  19487. (after org-occur-reveal activate)
  19488. (and (derived-mode-p 'org-mode) (org-reveal)))
  19489. (defadvice occur-mode-goto-occurrence-other-window
  19490. (after org-occur-reveal activate)
  19491. (and (derived-mode-p 'org-mode) (org-reveal)))
  19492. (defadvice occur-mode-display-occurrence
  19493. (after org-occur-reveal activate)
  19494. (when (derived-mode-p 'org-mode)
  19495. (let ((pos (occur-mode-find-occurrence)))
  19496. (with-current-buffer (marker-buffer pos)
  19497. (save-excursion
  19498. (goto-char pos)
  19499. (org-reveal)))))))
  19500. (defun org-occur-link-in-agenda-files ()
  19501. "Create a link and search for it in the agendas.
  19502. The link is not stored in `org-stored-links', it is just created
  19503. for the search purpose."
  19504. (interactive)
  19505. (let ((link (condition-case nil
  19506. (org-store-link nil)
  19507. (error "Unable to create a link to here"))))
  19508. (org-occur-in-agenda-files (regexp-quote link))))
  19509. (defun org-reverse-string (string)
  19510. "Return the reverse of STRING."
  19511. (apply 'string (reverse (string-to-list string))))
  19512. (defun org-uniquify (list)
  19513. "Remove duplicate elements from LIST."
  19514. (let (res)
  19515. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19516. res))
  19517. (defun org-delete-all (elts list)
  19518. "Remove all elements in ELTS from LIST."
  19519. (while elts
  19520. (setq list (delete (pop elts) list)))
  19521. list)
  19522. (defun org-count (cl-item cl-seq)
  19523. "Count the number of occurrences of ITEM in SEQ.
  19524. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19525. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19526. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19527. (while (< cl-start cl-end)
  19528. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19529. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19530. (setq cl-start (1+ cl-start)))
  19531. cl-count))
  19532. (defun org-remove-if (predicate seq)
  19533. "Remove everything from SEQ that fulfills PREDICATE."
  19534. (let (res e)
  19535. (while seq
  19536. (setq e (pop seq))
  19537. (if (not (funcall predicate e)) (push e res)))
  19538. (nreverse res)))
  19539. (defun org-remove-if-not (predicate seq)
  19540. "Remove everything from SEQ that does not fulfill PREDICATE."
  19541. (let (res e)
  19542. (while seq
  19543. (setq e (pop seq))
  19544. (if (funcall predicate e) (push e res)))
  19545. (nreverse res)))
  19546. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19547. "Reduce two-argument FUNCTION across SEQ.
  19548. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19549. \":initial-value\" removed."
  19550. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19551. (cadr (memq :initial-value cl-keys)))
  19552. (cl-seq (pop cl-seq))
  19553. (t (funcall cl-func)))))
  19554. (while cl-seq
  19555. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19556. cl-accum))
  19557. (defun org-back-over-empty-lines ()
  19558. "Move backwards over whitespace, to the beginning of the first empty line.
  19559. Returns the number of empty lines passed."
  19560. (let ((pos (point)))
  19561. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19562. (skip-chars-backward " \t\n\r")
  19563. (unless (eobp)
  19564. (forward-line -1)))
  19565. (beginning-of-line 2)
  19566. (goto-char (min (point) pos))
  19567. (count-lines (point) pos)))
  19568. (defun org-skip-whitespace ()
  19569. (skip-chars-forward " \t\n\r"))
  19570. (defun org-point-in-group (point group &optional context)
  19571. "Check if POINT is in match-group GROUP.
  19572. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19573. match. If the match group does not exist or point is not inside it,
  19574. return nil."
  19575. (and (match-beginning group)
  19576. (>= point (match-beginning group))
  19577. (<= point (match-end group))
  19578. (if context
  19579. (list context (match-beginning group) (match-end group))
  19580. t)))
  19581. (defun org-switch-to-buffer-other-window (&rest args)
  19582. "Switch to buffer in a second window on the current frame.
  19583. In particular, do not allow pop-up frames.
  19584. Returns the newly created buffer."
  19585. (org-no-popups
  19586. (apply 'switch-to-buffer-other-window args)))
  19587. (defun org-combine-plists (&rest plists)
  19588. "Create a single property list from all plists in PLISTS.
  19589. The process starts by copying the first list, and then setting properties
  19590. from the other lists. Settings in the last list are the most significant
  19591. ones and overrule settings in the other lists."
  19592. (let ((rtn (copy-sequence (pop plists)))
  19593. p v ls)
  19594. (while plists
  19595. (setq ls (pop plists))
  19596. (while ls
  19597. (setq p (pop ls) v (pop ls))
  19598. (setq rtn (plist-put rtn p v))))
  19599. rtn))
  19600. (defun org-replace-escapes (string table)
  19601. "Replace %-escapes in STRING with values in TABLE.
  19602. TABLE is an association list with keys like \"%a\" and string values.
  19603. The sequences in STRING may contain normal field width and padding information,
  19604. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19605. so values can contain further %-escapes if they are define later in TABLE."
  19606. (let ((tbl (copy-alist table))
  19607. (case-fold-search nil)
  19608. (pchg 0)
  19609. e re rpl)
  19610. (while (setq e (pop tbl))
  19611. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19612. (when (and (cdr e) (string-match re (cdr e)))
  19613. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19614. (safe "SREF"))
  19615. (add-text-properties 0 3 (list 'sref sref) safe)
  19616. (setcdr e (replace-match safe t t (cdr e)))))
  19617. (while (string-match re string)
  19618. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19619. (cdr e)))
  19620. (setq string (replace-match rpl t t string))))
  19621. (while (setq pchg (next-property-change pchg string))
  19622. (let ((sref (get-text-property pchg 'sref string)))
  19623. (when (and sref (string-match "SREF" string pchg))
  19624. (setq string (replace-match sref t t string)))))
  19625. string))
  19626. (defun org-sublist (list start end)
  19627. "Return a section of LIST, from START to END.
  19628. Counting starts at 1."
  19629. (let (rtn (c start))
  19630. (setq list (nthcdr (1- start) list))
  19631. (while (and list (<= c end))
  19632. (push (pop list) rtn)
  19633. (setq c (1+ c)))
  19634. (nreverse rtn)))
  19635. (defun org-find-base-buffer-visiting (file)
  19636. "Like `find-buffer-visiting' but always return the base buffer and
  19637. not an indirect buffer."
  19638. (let ((buf (or (get-file-buffer file)
  19639. (find-buffer-visiting file))))
  19640. (if buf
  19641. (or (buffer-base-buffer buf) buf)
  19642. nil)))
  19643. (defun org-image-file-name-regexp (&optional extensions)
  19644. "Return regexp matching the file names of images.
  19645. If EXTENSIONS is given, only match these."
  19646. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19647. (image-file-name-regexp)
  19648. (let ((image-file-name-extensions
  19649. (or extensions
  19650. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19651. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19652. (concat "\\."
  19653. (regexp-opt (nconc (mapcar 'upcase
  19654. image-file-name-extensions)
  19655. image-file-name-extensions)
  19656. t)
  19657. "\\'"))))
  19658. (defun org-file-image-p (file &optional extensions)
  19659. "Return non-nil if FILE is an image."
  19660. (save-match-data
  19661. (string-match (org-image-file-name-regexp extensions) file)))
  19662. (defun org-get-cursor-date (&optional with-time)
  19663. "Return the date at cursor in as a time.
  19664. This works in the calendar and in the agenda, anywhere else it just
  19665. returns the current time.
  19666. If WITH-TIME is non-nil, returns the time of the event at point (in
  19667. the agenda) or the current time of the day."
  19668. (let (date day defd tp tm hod mod)
  19669. (when with-time
  19670. (setq tp (get-text-property (point) 'time))
  19671. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19672. (setq hod (string-to-number (match-string 1 tp))
  19673. mod (string-to-number (match-string 2 tp))))
  19674. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19675. mod (nth 1 (decode-time (current-time))))))
  19676. (cond
  19677. ((eq major-mode 'calendar-mode)
  19678. (setq date (calendar-cursor-to-date)
  19679. defd (encode-time 0 (or mod 0) (or hod 0)
  19680. (nth 1 date) (nth 0 date) (nth 2 date))))
  19681. ((eq major-mode 'org-agenda-mode)
  19682. (setq day (get-text-property (point) 'day))
  19683. (if day
  19684. (setq date (calendar-gregorian-from-absolute day)
  19685. defd (encode-time 0 (or mod 0) (or hod 0)
  19686. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19687. (or defd (current-time))))
  19688. (defun org-mark-subtree (&optional up)
  19689. "Mark the current subtree.
  19690. This puts point at the start of the current subtree, and mark at
  19691. the end. If a numeric prefix UP is given, move up into the
  19692. hierarchy of headlines by UP levels before marking the subtree."
  19693. (interactive "P")
  19694. (org-with-limited-levels
  19695. (cond ((org-at-heading-p) (beginning-of-line))
  19696. ((org-before-first-heading-p) (error "Not in a subtree"))
  19697. (t (outline-previous-visible-heading 1))))
  19698. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19699. (if (org-called-interactively-p 'any)
  19700. (call-interactively 'org-mark-element)
  19701. (org-mark-element)))
  19702. ;;; Indentation
  19703. (defun org-indent-line ()
  19704. "Indent line depending on context."
  19705. (interactive)
  19706. (let* ((pos (point))
  19707. (itemp (org-at-item-p))
  19708. (case-fold-search t)
  19709. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19710. (inline-task-p (and (featurep 'org-inlinetask)
  19711. (org-inlinetask-in-task-p)))
  19712. (inline-re (and inline-task-p
  19713. (org-inlinetask-outline-regexp)))
  19714. column)
  19715. (if (and orgstruct-is-++ (eq pos (point)))
  19716. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19717. (indent-according-to-mode))
  19718. (beginning-of-line 1)
  19719. (cond
  19720. ;; Headings
  19721. ((looking-at org-outline-regexp) (setq column 0))
  19722. ;; Footnote definition
  19723. ((looking-at org-footnote-definition-re) (setq column 0))
  19724. ;; Literal examples
  19725. ((looking-at "[ \t]*:\\( \\|$\\)")
  19726. (setq column (org-get-indentation))) ; do nothing
  19727. ;; Lists
  19728. ((ignore-errors (goto-char (org-in-item-p)))
  19729. (setq column (if itemp
  19730. (org-get-indentation)
  19731. (org-list-item-body-column (point))))
  19732. (goto-char pos))
  19733. ;; Drawers
  19734. ((and (looking-at "[ \t]*:END:")
  19735. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19736. (save-excursion
  19737. (goto-char (1- (match-beginning 1)))
  19738. (setq column (current-column))))
  19739. ;; Special blocks
  19740. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19741. (save-excursion
  19742. (re-search-backward
  19743. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19744. (setq column (org-get-indentation (match-string 0))))
  19745. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19746. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19747. (save-excursion
  19748. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19749. (setq column
  19750. (cond ((equal (downcase (match-string 1)) "src")
  19751. ;; src blocks: let `org-edit-src-exit' handle them
  19752. (org-get-indentation))
  19753. ((equal (downcase (match-string 1)) "example")
  19754. (max (org-get-indentation)
  19755. (org-get-indentation (match-string 0))))
  19756. (t
  19757. (org-get-indentation (match-string 0))))))
  19758. ;; This line has nothing special, look at the previous relevant
  19759. ;; line to compute indentation
  19760. (t
  19761. (beginning-of-line 0)
  19762. (while (and (not (bobp))
  19763. (not (looking-at org-table-line-regexp))
  19764. (not (looking-at org-drawer-regexp))
  19765. ;; When point started in an inline task, do not move
  19766. ;; above task starting line.
  19767. (not (and inline-task-p (looking-at inline-re)))
  19768. ;; Skip drawers, blocks, empty lines, verbatim,
  19769. ;; comments, tables, footnotes definitions, lists,
  19770. ;; inline tasks.
  19771. (or (and (looking-at "[ \t]*:END:")
  19772. (re-search-backward org-drawer-regexp nil t))
  19773. (and (looking-at "[ \t]*#\\+end_")
  19774. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19775. (looking-at "[ \t]*[\n:#|]")
  19776. (looking-at org-footnote-definition-re)
  19777. (and (ignore-errors (goto-char (org-in-item-p)))
  19778. (goto-char
  19779. (org-list-get-top-point (org-list-struct))))
  19780. (and (not inline-task-p)
  19781. (featurep 'org-inlinetask)
  19782. (org-inlinetask-in-task-p)
  19783. (or (org-inlinetask-goto-beginning) t))))
  19784. (beginning-of-line 0))
  19785. (cond
  19786. ;; There was an heading above.
  19787. ((looking-at "\\*+[ \t]+")
  19788. (if (not org-adapt-indentation)
  19789. (setq column 0)
  19790. (goto-char (match-end 0))
  19791. (setq column (current-column))))
  19792. ;; A drawer had started and is unfinished
  19793. ((looking-at org-drawer-regexp)
  19794. (goto-char (1- (match-beginning 1)))
  19795. (setq column (current-column)))
  19796. ;; Else, nothing noticeable found: get indentation and go on.
  19797. (t (setq column (org-get-indentation))))))
  19798. ;; Now apply indentation and move cursor accordingly
  19799. (goto-char pos)
  19800. (if (<= (current-column) (current-indentation))
  19801. (org-indent-line-to column)
  19802. (save-excursion (org-indent-line-to column)))
  19803. ;; Special polishing for properties, see `org-property-format'
  19804. (setq column (current-column))
  19805. (beginning-of-line 1)
  19806. (if (looking-at
  19807. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19808. (replace-match (concat (match-string 1)
  19809. (format org-property-format
  19810. (match-string 2) (match-string 3)))
  19811. t t))
  19812. (org-move-to-column column))))
  19813. (defun org-indent-drawer ()
  19814. "Indent the drawer at point."
  19815. (interactive)
  19816. (let ((p (point))
  19817. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19818. (match-end 0)))
  19819. (folded
  19820. (save-excursion
  19821. (end-of-line)
  19822. (when (overlays-at (point))
  19823. (member 'invisible (overlay-properties
  19824. (car (overlays-at (point)))))))))
  19825. (when folded (org-cycle))
  19826. (indent-for-tab-command)
  19827. (while (and (move-beginning-of-line 2) (< (point) e))
  19828. (indent-for-tab-command))
  19829. (goto-char p)
  19830. (when folded (org-cycle)))
  19831. (message "Drawer at point indented"))
  19832. (defun org-indent-block ()
  19833. "Indent the block at point."
  19834. (interactive)
  19835. (let ((p (point))
  19836. (case-fold-search t)
  19837. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19838. (match-end 0)))
  19839. (folded
  19840. (save-excursion
  19841. (end-of-line)
  19842. (when (overlays-at (point))
  19843. (member 'invisible (overlay-properties
  19844. (car (overlays-at (point)))))))))
  19845. (when folded (org-cycle))
  19846. (indent-for-tab-command)
  19847. (while (and (move-beginning-of-line 2) (< (point) e))
  19848. (indent-for-tab-command))
  19849. (goto-char p)
  19850. (when folded (org-cycle)))
  19851. (message "Block at point indented"))
  19852. (defun org-indent-region (start end)
  19853. "Indent region."
  19854. (interactive "r")
  19855. (save-excursion
  19856. (let ((line-end (org-current-line end)))
  19857. (goto-char start)
  19858. (while (< (org-current-line) line-end)
  19859. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19860. (t (call-interactively 'org-indent-line)))
  19861. (move-beginning-of-line 2)))))
  19862. ;;; Filling
  19863. ;; We use our own fill-paragraph and auto-fill functions.
  19864. ;; `org-fill-paragraph' relies on adaptive filling and context
  19865. ;; checking. Appropriate `fill-prefix' is computed with
  19866. ;; `org-adaptive-fill-function'.
  19867. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19868. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19869. ;; `org-adaptive-fill-function' value. Internally,
  19870. ;; `org-comment-line-break-function' breaks the line.
  19871. ;; `org-setup-filling' installs filling and auto-filling related
  19872. ;; variables during `org-mode' initialization.
  19873. (defun org-setup-filling ()
  19874. (interactive)
  19875. ;; Prevent auto-fill from inserting unwanted new items.
  19876. (when (boundp 'fill-nobreak-predicate)
  19877. (org-set-local
  19878. 'fill-nobreak-predicate
  19879. (org-uniquify
  19880. (append fill-nobreak-predicate
  19881. '(org-fill-paragraph-separate-nobreak-p
  19882. org-fill-line-break-nobreak-p
  19883. org-fill-paragraph-with-timestamp-nobreak-p)))))
  19884. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19885. (org-set-local 'auto-fill-inhibit-regexp nil)
  19886. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19887. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19888. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19889. (defvar org-element-paragraph-separate) ; org-element.el
  19890. (defun org-fill-paragraph-separate-nobreak-p ()
  19891. "Non-nil when a line break at point would insert a new item."
  19892. (looking-at (substring org-element-paragraph-separate 1)))
  19893. (defun org-fill-line-break-nobreak-p ()
  19894. "Non-nil when a line break at point would create an Org line break."
  19895. (save-excursion
  19896. (skip-chars-backward "[ \t]")
  19897. (skip-chars-backward "\\\\")
  19898. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19899. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  19900. "Non-nil when a line break at point would insert a new item."
  19901. (and (org-at-timestamp-p t)
  19902. (not (looking-at org-ts-regexp-both))))
  19903. (declare-function message-in-body-p "message" ())
  19904. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19905. (defun org-adaptive-fill-function ()
  19906. "Compute a fill prefix for the current line.
  19907. Return fill prefix, as a string, or nil if current line isn't
  19908. meant to be filled."
  19909. (let (prefix)
  19910. (catch 'exit
  19911. (when (derived-mode-p 'message-mode)
  19912. (save-excursion
  19913. (beginning-of-line)
  19914. (cond ((or (not (message-in-body-p))
  19915. (looking-at orgtbl-line-start-regexp))
  19916. (throw 'exit nil))
  19917. ((looking-at message-cite-prefix-regexp)
  19918. (throw 'exit (match-string-no-properties 0)))
  19919. ((looking-at org-outline-regexp)
  19920. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19921. (org-with-wide-buffer
  19922. (let* ((p (line-beginning-position))
  19923. (element (save-excursion (beginning-of-line)
  19924. (org-element-at-point)))
  19925. (type (org-element-type element))
  19926. (post-affiliated (org-element-property :post-affiliated element)))
  19927. (unless (and post-affiliated (< p post-affiliated))
  19928. (case type
  19929. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19930. (footnote-definition "")
  19931. ((item plain-list)
  19932. (make-string (org-list-item-body-column
  19933. (or post-affiliated
  19934. (org-element-property :begin element)))
  19935. ? ))
  19936. (paragraph
  19937. ;; Fill prefix is usually the same as the current line,
  19938. ;; except if the paragraph is at the beginning of an item.
  19939. (let ((parent (org-element-property :parent element)))
  19940. (cond ((eq (org-element-type parent) 'item)
  19941. (make-string (org-list-item-body-column
  19942. (org-element-property :begin parent))
  19943. ? ))
  19944. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19945. (match-string 0))
  19946. (t ""))))
  19947. (comment-block
  19948. ;; Only fill contents if P is within block boundaries.
  19949. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19950. (forward-line)
  19951. (point)))
  19952. (cend (save-excursion
  19953. (goto-char (org-element-property :end element))
  19954. (skip-chars-backward " \r\t\n")
  19955. (line-beginning-position))))
  19956. (when (and (>= p cbeg) (< p cend))
  19957. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19958. (match-string 0)
  19959. "")))))))))))
  19960. (declare-function message-goto-body "message" ())
  19961. (defvar message-cite-prefix-regexp) ; From message.el
  19962. (defvar org-element-all-objects) ; From org-element.el
  19963. (defun org-fill-paragraph (&optional justify)
  19964. "Fill element at point, when applicable.
  19965. This function only applies to comment blocks, comments, example
  19966. blocks and paragraphs. Also, as a special case, re-align table
  19967. when point is at one.
  19968. If JUSTIFY is non-nil (interactively, with prefix argument),
  19969. justify as well. If `sentence-end-double-space' is non-nil, then
  19970. period followed by one space does not end a sentence, so don't
  19971. break a line there. The variable `fill-column' controls the
  19972. width for filling.
  19973. For convenience, when point is at a plain list, an item or
  19974. a footnote definition, try to fill the first paragraph within."
  19975. (interactive)
  19976. (if (and (derived-mode-p 'message-mode)
  19977. (or (not (message-in-body-p))
  19978. (save-excursion (move-beginning-of-line 1)
  19979. (looking-at message-cite-prefix-regexp))))
  19980. ;; First ensure filling is correct in message-mode.
  19981. (let ((fill-paragraph-function
  19982. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19983. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19984. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19985. (paragraph-separate
  19986. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19987. (fill-paragraph nil))
  19988. (save-excursion
  19989. ;; Move to end of line in order to get the first paragraph
  19990. ;; within a plain list or a footnote definition.
  19991. (end-of-line)
  19992. (let ((element (org-element-at-point)))
  19993. ;; First check if point is in a blank line at the beginning of
  19994. ;; the buffer. In that case, ignore filling.
  19995. (if (< (point) (org-element-property :begin element)) t
  19996. (case (org-element-type element)
  19997. ;; Use major mode filling function is src blocks.
  19998. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19999. ;; Align Org tables, leave table.el tables as-is.
  20000. (table-row (org-table-align) t)
  20001. (table
  20002. (when (eq (org-element-property :type element) 'org)
  20003. (org-table-align))
  20004. t)
  20005. (paragraph
  20006. ;; Paragraphs may contain `line-break' type objects.
  20007. (let ((beg (max (point-min)
  20008. (org-element-property :contents-begin element)))
  20009. (end (min (point-max)
  20010. (org-element-property :contents-end element))))
  20011. ;; Do nothing if point is at an affiliated keyword.
  20012. (if (< (point) beg) t
  20013. (when (derived-mode-p 'message-mode)
  20014. ;; In `message-mode', do not fill following
  20015. ;; citation in current paragraph nor text before
  20016. ;; message body.
  20017. (let ((body-start (save-excursion (message-goto-body))))
  20018. (when body-start (setq beg (max body-start beg))))
  20019. (when (save-excursion
  20020. (re-search-forward
  20021. (concat "^" message-cite-prefix-regexp) end t))
  20022. (setq end (match-beginning 0))))
  20023. ;; Fill paragraph, taking line breaks into
  20024. ;; consideration. For that, slice the paragraph
  20025. ;; using line breaks as separators, and fill the
  20026. ;; parts in reverse order to avoid messing with
  20027. ;; markers.
  20028. (save-excursion
  20029. (goto-char end)
  20030. (mapc
  20031. (lambda (pos)
  20032. (fill-region-as-paragraph pos (point) justify)
  20033. (goto-char pos))
  20034. ;; Find the list of ending positions for line
  20035. ;; breaks in the current paragraph. Add paragraph
  20036. ;; beginning to include first slice.
  20037. (nreverse
  20038. (cons
  20039. beg
  20040. (org-element-map
  20041. (org-element--parse-objects
  20042. beg end nil org-element-all-objects)
  20043. 'line-break
  20044. (lambda (lb) (org-element-property :end lb)))))))
  20045. t)))
  20046. ;; Contents of `comment-block' type elements should be
  20047. ;; filled as plain text, but only if point is within block
  20048. ;; markers.
  20049. (comment-block
  20050. (let* ((case-fold-search t)
  20051. (beg (save-excursion
  20052. (goto-char (org-element-property :begin element))
  20053. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20054. (forward-line)
  20055. (point)))
  20056. (end (save-excursion
  20057. (goto-char (org-element-property :end element))
  20058. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20059. (line-beginning-position))))
  20060. (when (and (>= (point) beg) (< (point) end))
  20061. (fill-region-as-paragraph
  20062. (save-excursion
  20063. (end-of-line)
  20064. (re-search-backward "^[ \t]*$" beg 'move)
  20065. (line-beginning-position))
  20066. (save-excursion
  20067. (beginning-of-line)
  20068. (re-search-forward "^[ \t]*$" end 'move)
  20069. (line-beginning-position))
  20070. justify)))
  20071. t)
  20072. ;; Fill comments.
  20073. (comment (fill-comment-paragraph justify))
  20074. ;; Ignore every other element.
  20075. (otherwise t)))))))
  20076. (defun org-auto-fill-function ()
  20077. "Auto-fill function."
  20078. ;; Check if auto-filling is meaningful.
  20079. (let ((fc (current-fill-column)))
  20080. (when (and fc (> (current-column) fc))
  20081. (let* ((fill-prefix (org-adaptive-fill-function))
  20082. ;; Enforce empty fill prefix, if required. Otherwise, it
  20083. ;; will be computed again.
  20084. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20085. (when fill-prefix (do-auto-fill))))))
  20086. (defun org-comment-line-break-function (&optional soft)
  20087. "Break line at point and indent, continuing comment if within one.
  20088. The inserted newline is marked hard if variable
  20089. `use-hard-newlines' is true, unless optional argument SOFT is
  20090. non-nil."
  20091. (if soft (insert-and-inherit ?\n) (newline 1))
  20092. (save-excursion (forward-char -1) (delete-horizontal-space))
  20093. (delete-horizontal-space)
  20094. (indent-to-left-margin)
  20095. (insert-before-markers-and-inherit fill-prefix))
  20096. ;;; Comments
  20097. ;; Org comments syntax is quite complex. It requires the entire line
  20098. ;; to be just a comment. Also, even with the right syntax at the
  20099. ;; beginning of line, some some elements (i.e. verse-block or
  20100. ;; example-block) don't accept comments. Usual Emacs comment commands
  20101. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20102. ;; Org still relies on `comment-dwim', but cannot trust
  20103. ;; `comment-only-p'. So, `comment-region-function' and
  20104. ;; `uncomment-region-function' both point
  20105. ;; to`org-comment-or-uncomment-region'. Eventually,
  20106. ;; `org-insert-comment' takes care of insertion of comments at the
  20107. ;; beginning of line.
  20108. ;; `org-setup-comments-handling' install comments related variables
  20109. ;; during `org-mode' initialization.
  20110. (defun org-setup-comments-handling ()
  20111. (interactive)
  20112. (org-set-local 'comment-use-syntax nil)
  20113. (org-set-local 'comment-start "# ")
  20114. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20115. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20116. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20117. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20118. (defun org-insert-comment ()
  20119. "Insert an empty comment above current line.
  20120. If the line is empty, insert comment at its beginning."
  20121. (beginning-of-line)
  20122. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20123. (org-indent-line)
  20124. (insert "# "))
  20125. (defvar comment-empty-lines) ; From newcomment.el.
  20126. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20127. "Comment or uncomment each non-blank line in the region.
  20128. Uncomment each non-blank line between BEG and END if it only
  20129. contains commented lines. Otherwise, comment them."
  20130. (save-restriction
  20131. ;; Restrict region
  20132. (narrow-to-region (save-excursion (goto-char beg)
  20133. (skip-chars-forward " \r\t\n" end)
  20134. (line-beginning-position))
  20135. (save-excursion (goto-char end)
  20136. (skip-chars-backward " \r\t\n" beg)
  20137. (line-end-position)))
  20138. (let ((uncommentp
  20139. ;; UNCOMMENTP is non-nil when every non blank line between
  20140. ;; BEG and END is a comment.
  20141. (save-excursion
  20142. (goto-char (point-min))
  20143. (while (and (not (eobp))
  20144. (let ((element (org-element-at-point)))
  20145. (and (eq (org-element-type element) 'comment)
  20146. (goto-char (min (point-max)
  20147. (org-element-property
  20148. :end element)))))))
  20149. (eobp))))
  20150. (if uncommentp
  20151. ;; Only blank lines and comments in region: uncomment it.
  20152. (save-excursion
  20153. (goto-char (point-min))
  20154. (while (not (eobp))
  20155. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20156. (replace-match "" nil nil nil 1))
  20157. (forward-line)))
  20158. ;; Comment each line in region.
  20159. (let ((min-indent (point-max)))
  20160. ;; First find the minimum indentation across all lines.
  20161. (save-excursion
  20162. (goto-char (point-min))
  20163. (while (and (not (eobp)) (not (zerop min-indent)))
  20164. (unless (looking-at "[ \t]*$")
  20165. (setq min-indent (min min-indent (current-indentation))))
  20166. (forward-line)))
  20167. ;; Then loop over all lines.
  20168. (save-excursion
  20169. (goto-char (point-min))
  20170. (while (not (eobp))
  20171. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20172. (org-move-to-column min-indent t)
  20173. (insert comment-start))
  20174. (forward-line))))))))
  20175. ;;; Planning
  20176. ;; This section contains tools to operate on timestamp objects, as
  20177. ;; returned by, e.g. `org-element-context'.
  20178. (defun org-timestamp-has-time-p (timestamp)
  20179. "Non-nil when TIMESTAMP has a time specified."
  20180. (org-element-property :hour-start timestamp))
  20181. (defun org-timestamp-format (timestamp format &optional end utc)
  20182. "Format a TIMESTAMP element into a string.
  20183. FORMAT is a format specifier to be passed to
  20184. `format-time-string'.
  20185. When optional argument END is non-nil, use end of date-range or
  20186. time-range, if possible.
  20187. When optional argument UTC is non-nil, time will be expressed as
  20188. Universal Time."
  20189. (format-time-string
  20190. format
  20191. (apply 'encode-time
  20192. (cons 0
  20193. (mapcar
  20194. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20195. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20196. '(:minute-start :hour-start :day-start :month-start
  20197. :year-start)))))
  20198. utc))
  20199. (defun org-timestamp-split-range (timestamp &optional end)
  20200. "Extract a timestamp object from a date or time range.
  20201. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20202. the end of the range. Otherwise, extract its start.
  20203. Return a new timestamp object sharing the same parent as
  20204. TIMESTAMP."
  20205. (let ((type (org-element-property :type timestamp)))
  20206. (if (memq type '(active inactive diary)) timestamp
  20207. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20208. ;; Set new type.
  20209. (org-element-put-property
  20210. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20211. ;; Copy start properties over end properties if END is
  20212. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20213. (let ((p-alist '((:minute-start . :minute-end)
  20214. (:hour-start . :hour-end)
  20215. (:day-start . :day-end)
  20216. (:month-start . :month-end)
  20217. (:year-start . :year-end))))
  20218. (dolist (p-cell p-alist)
  20219. (org-element-put-property
  20220. split-ts
  20221. (funcall (if end 'car 'cdr) p-cell)
  20222. (org-element-property
  20223. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20224. ;; Eventually refresh `:raw-value'.
  20225. (org-element-put-property split-ts :raw-value nil)
  20226. (org-element-put-property
  20227. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20228. (defun org-timestamp-translate (timestamp &optional boundary)
  20229. "Apply `org-translate-time' on a TIMESTAMP object.
  20230. When optional argument BOUNDARY is non-nil, it is either the
  20231. symbol `start' or `end'. In this case, only translate the
  20232. starting or ending part of TIMESTAMP if it is a date or time
  20233. range. Otherwise, translate both parts."
  20234. (if (and (not boundary)
  20235. (memq (org-element-property :type timestamp)
  20236. '(active-range inactive-range)))
  20237. (concat
  20238. (org-translate-time
  20239. (org-element-property :raw-value
  20240. (org-timestamp-split-range timestamp)))
  20241. "--"
  20242. (org-translate-time
  20243. (org-element-property :raw-value
  20244. (org-timestamp-split-range timestamp t))))
  20245. (org-translate-time
  20246. (org-element-property
  20247. :raw-value
  20248. (if (not boundary) timestamp
  20249. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20250. ;;; Other stuff.
  20251. (defun org-toggle-fixed-width-section (arg)
  20252. "Toggle the fixed-width export.
  20253. If there is no active region, the QUOTE keyword at the current headline is
  20254. inserted or removed. When present, it causes the text between this headline
  20255. and the next to be exported as fixed-width text, and unmodified.
  20256. If there is an active region, this command adds or removes a colon as the
  20257. first character of this line. If the first character of a line is a colon,
  20258. this line is also exported in fixed-width font."
  20259. (interactive "P")
  20260. (let* ((cc 0)
  20261. (regionp (org-region-active-p))
  20262. (beg (if regionp (region-beginning) (point)))
  20263. (end (if regionp (region-end)))
  20264. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20265. (case-fold-search nil)
  20266. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20267. off)
  20268. (if regionp
  20269. (save-excursion
  20270. (goto-char beg)
  20271. (setq cc (current-column))
  20272. (beginning-of-line 1)
  20273. (setq off (looking-at re))
  20274. (while (> nlines 0)
  20275. (setq nlines (1- nlines))
  20276. (beginning-of-line 1)
  20277. (cond
  20278. (arg
  20279. (org-move-to-column cc t)
  20280. (insert ": \n")
  20281. (forward-line -1))
  20282. ((and off (looking-at re))
  20283. (replace-match "" t t nil 1))
  20284. ((not off) (org-move-to-column cc t) (insert ": ")))
  20285. (forward-line 1)))
  20286. (save-excursion
  20287. (org-back-to-heading)
  20288. (cond
  20289. ((looking-at (format org-heading-keyword-regexp-format
  20290. org-quote-string))
  20291. (goto-char (match-end 1))
  20292. (looking-at (concat " +" org-quote-string))
  20293. (replace-match "" t t)
  20294. (when (eolp) (insert " ")))
  20295. ((looking-at org-outline-regexp)
  20296. (goto-char (match-end 0))
  20297. (insert org-quote-string " ")))))))
  20298. (defun org-reftex-citation ()
  20299. "Use reftex-citation to insert a citation into the buffer.
  20300. This looks for a line like
  20301. #+BIBLIOGRAPHY: foo plain option:-d
  20302. and derives from it that foo.bib is the bibliography file relevant
  20303. for this document. It then installs the necessary environment for RefTeX
  20304. to work in this buffer and calls `reftex-citation' to insert a citation
  20305. into the buffer.
  20306. Export of such citations to both LaTeX and HTML is handled by the contributed
  20307. package org-exp-bibtex by Taru Karttunen."
  20308. (interactive)
  20309. (let ((reftex-docstruct-symbol 'rds)
  20310. (reftex-cite-format "\\cite{%l}")
  20311. rds bib)
  20312. (save-excursion
  20313. (save-restriction
  20314. (widen)
  20315. (let ((case-fold-search t)
  20316. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20317. (if (not (save-excursion
  20318. (or (re-search-forward re nil t)
  20319. (re-search-backward re nil t))))
  20320. (error "No bibliography defined in file")
  20321. (setq bib (concat (match-string 1) ".bib")
  20322. rds (list (list 'bib bib)))))))
  20323. (call-interactively 'reftex-citation)))
  20324. ;;;; Functions extending outline functionality
  20325. (defun org-beginning-of-line (&optional arg)
  20326. "Go to the beginning of the current line. If that is invisible, continue
  20327. to a visible line beginning. This makes the function of C-a more intuitive.
  20328. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20329. first attempt, and only move to after the tags when the cursor is already
  20330. beyond the end of the headline."
  20331. (interactive "P")
  20332. (let ((pos (point))
  20333. (special (if (consp org-special-ctrl-a/e)
  20334. (car org-special-ctrl-a/e)
  20335. org-special-ctrl-a/e))
  20336. refpos)
  20337. (if (org-bound-and-true-p visual-line-mode)
  20338. (beginning-of-visual-line 1)
  20339. (beginning-of-line 1))
  20340. (if (and arg (fboundp 'move-beginning-of-line))
  20341. (call-interactively 'move-beginning-of-line)
  20342. (if (bobp)
  20343. nil
  20344. (backward-char 1)
  20345. (if (org-truely-invisible-p)
  20346. (while (and (not (bobp)) (org-truely-invisible-p))
  20347. (backward-char 1)
  20348. (beginning-of-line 1))
  20349. (forward-char 1))))
  20350. (when special
  20351. (cond
  20352. ((and (looking-at org-complex-heading-regexp)
  20353. (= (char-after (match-end 1)) ?\ ))
  20354. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20355. (point-at-eol)))
  20356. (goto-char
  20357. (if (eq special t)
  20358. (cond ((> pos refpos) refpos)
  20359. ((= pos (point)) refpos)
  20360. (t (point)))
  20361. (cond ((> pos (point)) (point))
  20362. ((not (eq last-command this-command)) (point))
  20363. (t refpos)))))
  20364. ((org-at-item-p)
  20365. ;; Being at an item and not looking at an the item means point
  20366. ;; was previously moved to beginning of a visual line, which
  20367. ;; doesn't contain the item. Therefore, do nothing special,
  20368. ;; just stay here.
  20369. (when (looking-at org-list-full-item-re)
  20370. ;; Set special position at first white space character after
  20371. ;; bullet, and check-box, if any.
  20372. (let ((after-bullet
  20373. (let ((box (match-end 3)))
  20374. (if (not box) (match-end 1)
  20375. (let ((after (char-after box)))
  20376. (if (and after (= after ? )) (1+ box) box))))))
  20377. ;; Special case: Move point to special position when
  20378. ;; currently after it or at beginning of line.
  20379. (if (eq special t)
  20380. (when (or (> pos after-bullet) (= (point) pos))
  20381. (goto-char after-bullet))
  20382. ;; Reversed case: Move point to special position when
  20383. ;; point was already at beginning of line and command is
  20384. ;; repeated.
  20385. (when (and (= (point) pos) (eq last-command this-command))
  20386. (goto-char after-bullet))))))))
  20387. (org-no-warnings
  20388. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20389. (defun org-end-of-line (&optional arg)
  20390. "Go to the end of the line.
  20391. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20392. tags on the first attempt, and only move to after the tags when
  20393. the cursor is already beyond the end of the headline."
  20394. (interactive "P")
  20395. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20396. org-special-ctrl-a/e))
  20397. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20398. 'end-of-visual-line)
  20399. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20400. (t 'end-of-line))))
  20401. (if (or (not special) arg) (call-interactively move-fun)
  20402. (let* ((element (save-excursion (beginning-of-line)
  20403. (org-element-at-point)))
  20404. (type (org-element-type element)))
  20405. (cond
  20406. ((memq type '(headline inlinetask))
  20407. (let ((pos (point)))
  20408. (beginning-of-line 1)
  20409. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20410. (if (eq special t)
  20411. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20412. (goto-char (match-beginning 1))
  20413. (goto-char (match-end 0)))
  20414. (if (or (< pos (match-end 0))
  20415. (not (eq this-command last-command)))
  20416. (goto-char (match-end 0))
  20417. (goto-char (match-beginning 1))))
  20418. (call-interactively move-fun))))
  20419. ((org-element-property :hiddenp element)
  20420. ;; If element is hidden, `move-end-of-line' would put point
  20421. ;; after it. Use `end-of-line' to stay on current line.
  20422. (call-interactively 'end-of-line))
  20423. (t (call-interactively move-fun)))))
  20424. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20425. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20426. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20427. (defun org-backward-sentence (&optional arg)
  20428. "Go to beginning of sentence, or beginning of table field.
  20429. This will call `backward-sentence' or `org-table-beginning-of-field',
  20430. depending on context."
  20431. (interactive "P")
  20432. (cond
  20433. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20434. (t (call-interactively 'backward-sentence))))
  20435. (defun org-forward-sentence (&optional arg)
  20436. "Go to end of sentence, or end of table field.
  20437. This will call `forward-sentence' or `org-table-end-of-field',
  20438. depending on context."
  20439. (interactive "P")
  20440. (cond
  20441. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20442. (t (call-interactively 'forward-sentence))))
  20443. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20444. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20445. (defun org-kill-line (&optional arg)
  20446. "Kill line, to tags or end of line."
  20447. (interactive "P")
  20448. (cond
  20449. ((or (not org-special-ctrl-k)
  20450. (bolp)
  20451. (not (org-at-heading-p)))
  20452. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20453. org-ctrl-k-protect-subtree)
  20454. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20455. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20456. (error "C-k aborted - would kill hidden subtree")))
  20457. (call-interactively
  20458. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20459. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20460. (kill-region (point) (match-beginning 1))
  20461. (org-set-tags nil t))
  20462. (t (kill-region (point) (point-at-eol)))))
  20463. (define-key org-mode-map "\C-k" 'org-kill-line)
  20464. (defun org-yank (&optional arg)
  20465. "Yank. If the kill is a subtree, treat it specially.
  20466. This command will look at the current kill and check if is a single
  20467. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20468. cursor is at the beginning of a line or after the stars of a currently
  20469. empty headline, then the yank is handled specially. How exactly depends
  20470. on the value of the following variables, both set by default.
  20471. org-yank-folded-subtrees
  20472. When set, the subtree(s) will be folded after insertion, but only
  20473. if doing so would now swallow text after the yanked text.
  20474. org-yank-adjusted-subtrees
  20475. When set, the subtree will be promoted or demoted in order to
  20476. fit into the local outline tree structure, which means that the level
  20477. will be adjusted so that it becomes the smaller one of the two
  20478. *visible* surrounding headings.
  20479. Any prefix to this command will cause `yank' to be called directly with
  20480. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20481. will just
  20482. plainly yank the text as it is.
  20483. \[1] The test checks if the first non-white line is a heading
  20484. and if there are no other headings with fewer stars."
  20485. (interactive "P")
  20486. (org-yank-generic 'yank arg))
  20487. (defun org-yank-generic (command arg)
  20488. "Perform some yank-like command.
  20489. This function implements the behavior described in the `org-yank'
  20490. documentation. However, it has been generalized to work for any
  20491. interactive command with similar behavior."
  20492. ;; pretend to be command COMMAND
  20493. (setq this-command command)
  20494. (if arg
  20495. (call-interactively command)
  20496. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20497. (and (org-kill-is-subtree-p)
  20498. (or (bolp)
  20499. (and (looking-at "[ \t]*$")
  20500. (string-match
  20501. "\\`\\*+\\'"
  20502. (buffer-substring (point-at-bol) (point)))))))
  20503. swallowp)
  20504. (cond
  20505. ((and subtreep org-yank-folded-subtrees)
  20506. (let ((beg (point))
  20507. end)
  20508. (if (and subtreep org-yank-adjusted-subtrees)
  20509. (org-paste-subtree nil nil 'for-yank)
  20510. (call-interactively command))
  20511. (setq end (point))
  20512. (goto-char beg)
  20513. (when (and (bolp) subtreep
  20514. (not (setq swallowp
  20515. (org-yank-folding-would-swallow-text beg end))))
  20516. (org-with-limited-levels
  20517. (or (looking-at org-outline-regexp)
  20518. (re-search-forward org-outline-regexp-bol end t))
  20519. (while (and (< (point) end) (looking-at org-outline-regexp))
  20520. (hide-subtree)
  20521. (org-cycle-show-empty-lines 'folded)
  20522. (condition-case nil
  20523. (outline-forward-same-level 1)
  20524. (error (goto-char end))))))
  20525. (when swallowp
  20526. (message
  20527. "Inserted text not folded because that would swallow text"))
  20528. (goto-char end)
  20529. (skip-chars-forward " \t\n\r")
  20530. (beginning-of-line 1)
  20531. (push-mark beg 'nomsg)))
  20532. ((and subtreep org-yank-adjusted-subtrees)
  20533. (let ((beg (point-at-bol)))
  20534. (org-paste-subtree nil nil 'for-yank)
  20535. (push-mark beg 'nomsg)))
  20536. (t
  20537. (call-interactively command))))))
  20538. (defun org-yank-folding-would-swallow-text (beg end)
  20539. "Would hide-subtree at BEG swallow any text after END?"
  20540. (let (level)
  20541. (org-with-limited-levels
  20542. (save-excursion
  20543. (goto-char beg)
  20544. (when (or (looking-at org-outline-regexp)
  20545. (re-search-forward org-outline-regexp-bol end t))
  20546. (setq level (org-outline-level)))
  20547. (goto-char end)
  20548. (skip-chars-forward " \t\r\n\v\f")
  20549. (if (or (eobp)
  20550. (and (bolp) (looking-at org-outline-regexp)
  20551. (<= (org-outline-level) level)))
  20552. nil ; Nothing would be swallowed
  20553. t))))) ; something would swallow
  20554. (define-key org-mode-map "\C-y" 'org-yank)
  20555. (defun org-truely-invisible-p ()
  20556. "Check if point is at a character currently not visible.
  20557. This version does not only check the character property, but also
  20558. `visible-mode'."
  20559. ;; Early versions of noutline don't have `outline-invisible-p'.
  20560. (if (org-bound-and-true-p visible-mode)
  20561. nil
  20562. (outline-invisible-p)))
  20563. (defun org-invisible-p2 ()
  20564. "Check if point is at a character currently not visible."
  20565. (save-excursion
  20566. (if (and (eolp) (not (bobp))) (backward-char 1))
  20567. ;; Early versions of noutline don't have `outline-invisible-p'.
  20568. (outline-invisible-p)))
  20569. (defun org-back-to-heading (&optional invisible-ok)
  20570. "Call `outline-back-to-heading', but provide a better error message."
  20571. (condition-case nil
  20572. (outline-back-to-heading invisible-ok)
  20573. (error (error "Before first headline at position %d in buffer %s"
  20574. (point) (current-buffer)))))
  20575. (defun org-before-first-heading-p ()
  20576. "Before first heading?"
  20577. (save-excursion
  20578. (end-of-line)
  20579. (null (re-search-backward org-outline-regexp-bol nil t))))
  20580. (defun org-at-heading-p (&optional ignored)
  20581. (outline-on-heading-p t))
  20582. ;; Compatibility alias with Org versions < 7.8.03
  20583. (defalias 'org-on-heading-p 'org-at-heading-p)
  20584. (defun org-at-comment-p nil
  20585. "Is cursor in a line starting with a # character?"
  20586. (save-excursion
  20587. (beginning-of-line)
  20588. (looking-at "^#")))
  20589. (defun org-at-drawer-p nil
  20590. "Is cursor at a drawer keyword?"
  20591. (save-excursion
  20592. (move-beginning-of-line 1)
  20593. (looking-at org-drawer-regexp)))
  20594. (defun org-at-block-p nil
  20595. "Is cursor at a block keyword?"
  20596. (save-excursion
  20597. (move-beginning-of-line 1)
  20598. (looking-at org-block-regexp)))
  20599. (defun org-point-at-end-of-empty-headline ()
  20600. "If point is at the end of an empty headline, return t, else nil.
  20601. If the heading only contains a TODO keyword, it is still still considered
  20602. empty."
  20603. (and (looking-at "[ \t]*$")
  20604. (when org-todo-line-regexp
  20605. (save-excursion
  20606. (beginning-of-line 1)
  20607. (let ((case-fold-search nil))
  20608. (looking-at org-todo-line-regexp)
  20609. (string= (match-string 3) ""))))))
  20610. (defun org-at-heading-or-item-p ()
  20611. (or (org-at-heading-p) (org-at-item-p)))
  20612. (defun org-at-target-p ()
  20613. (or (org-in-regexp org-radio-target-regexp)
  20614. (org-in-regexp org-target-regexp)))
  20615. ;; Compatibility alias with Org versions < 7.8.03
  20616. (defalias 'org-on-target-p 'org-at-target-p)
  20617. (defun org-up-heading-all (arg)
  20618. "Move to the heading line of which the present line is a subheading.
  20619. This function considers both visible and invisible heading lines.
  20620. With argument, move up ARG levels."
  20621. (if (fboundp 'outline-up-heading-all)
  20622. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20623. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20624. (defun org-up-heading-safe ()
  20625. "Move to the heading line of which the present line is a subheading.
  20626. This version will not throw an error. It will return the level of the
  20627. headline found, or nil if no higher level is found.
  20628. Also, this function will be a lot faster than `outline-up-heading',
  20629. because it relies on stars being the outline starters. This can really
  20630. make a significant difference in outlines with very many siblings."
  20631. (let (start-level re)
  20632. (org-back-to-heading t)
  20633. (setq start-level (funcall outline-level))
  20634. (if (equal start-level 1)
  20635. nil
  20636. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20637. (if (re-search-backward re nil t)
  20638. (funcall outline-level)))))
  20639. (defun org-first-sibling-p ()
  20640. "Is this heading the first child of its parents?"
  20641. (interactive)
  20642. (let ((re org-outline-regexp-bol)
  20643. level l)
  20644. (unless (org-at-heading-p t)
  20645. (error "Not at a heading"))
  20646. (setq level (funcall outline-level))
  20647. (save-excursion
  20648. (if (not (re-search-backward re nil t))
  20649. t
  20650. (setq l (funcall outline-level))
  20651. (< l level)))))
  20652. (defun org-goto-sibling (&optional previous)
  20653. "Goto the next sibling, even if it is invisible.
  20654. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20655. when a sibling was found. When none is found, return nil and don't
  20656. move point."
  20657. (let ((fun (if previous 're-search-backward 're-search-forward))
  20658. (pos (point))
  20659. (re org-outline-regexp-bol)
  20660. level l)
  20661. (when (condition-case nil (org-back-to-heading t) (error nil))
  20662. (setq level (funcall outline-level))
  20663. (catch 'exit
  20664. (or previous (forward-char 1))
  20665. (while (funcall fun re nil t)
  20666. (setq l (funcall outline-level))
  20667. (when (< l level) (goto-char pos) (throw 'exit nil))
  20668. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20669. (goto-char pos)
  20670. nil))))
  20671. (defun org-show-siblings ()
  20672. "Show all siblings of the current headline."
  20673. (save-excursion
  20674. (while (org-goto-sibling) (org-flag-heading nil)))
  20675. (save-excursion
  20676. (while (org-goto-sibling 'previous)
  20677. (org-flag-heading nil))))
  20678. (defun org-goto-first-child ()
  20679. "Goto the first child, even if it is invisible.
  20680. Return t when a child was found. Otherwise don't move point and
  20681. return nil."
  20682. (let (level (pos (point)) (re org-outline-regexp-bol))
  20683. (when (condition-case nil (org-back-to-heading t) (error nil))
  20684. (setq level (outline-level))
  20685. (forward-char 1)
  20686. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20687. (progn (goto-char (match-beginning 0)) t)
  20688. (goto-char pos) nil))))
  20689. (defun org-show-hidden-entry ()
  20690. "Show an entry where even the heading is hidden."
  20691. (save-excursion
  20692. (org-show-entry)))
  20693. (defun org-flag-heading (flag &optional entry)
  20694. "Flag the current heading. FLAG non-nil means make invisible.
  20695. When ENTRY is non-nil, show the entire entry."
  20696. (save-excursion
  20697. (org-back-to-heading t)
  20698. ;; Check if we should show the entire entry
  20699. (if entry
  20700. (progn
  20701. (org-show-entry)
  20702. (save-excursion
  20703. (and (outline-next-heading)
  20704. (org-flag-heading nil))))
  20705. (outline-flag-region (max (point-min) (1- (point)))
  20706. (save-excursion (outline-end-of-heading) (point))
  20707. flag))))
  20708. (defun org-get-next-sibling ()
  20709. "Move to next heading of the same level, and return point.
  20710. If there is no such heading, return nil.
  20711. This is like outline-next-sibling, but invisible headings are ok."
  20712. (let ((level (funcall outline-level)))
  20713. (outline-next-heading)
  20714. (while (and (not (eobp)) (> (funcall outline-level) level))
  20715. (outline-next-heading))
  20716. (if (or (eobp) (< (funcall outline-level) level))
  20717. nil
  20718. (point))))
  20719. (defun org-get-last-sibling ()
  20720. "Move to previous heading of the same level, and return point.
  20721. If there is no such heading, return nil."
  20722. (let ((opoint (point))
  20723. (level (funcall outline-level)))
  20724. (outline-previous-heading)
  20725. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20726. (while (and (> (funcall outline-level) level)
  20727. (not (bobp)))
  20728. (outline-previous-heading))
  20729. (if (< (funcall outline-level) level)
  20730. nil
  20731. (point)))))
  20732. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20733. "Goto to the end of a subtree."
  20734. ;; This contains an exact copy of the original function, but it uses
  20735. ;; `org-back-to-heading', to make it work also in invisible
  20736. ;; trees. And is uses an invisible-ok argument.
  20737. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20738. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20739. ;; with many children and grandchildren etc, this can be much faster
  20740. ;; than the outline version.
  20741. (org-back-to-heading invisible-ok)
  20742. (let ((first t)
  20743. (level (funcall outline-level)))
  20744. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20745. ;; A true heading (not a plain list item), in Org-mode
  20746. ;; This means we can easily find the end by looking
  20747. ;; only for the right number of stars. Using a regexp to do
  20748. ;; this is so much faster than using a Lisp loop.
  20749. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20750. (forward-char 1)
  20751. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20752. ;; something else, do it the slow way
  20753. (while (and (not (eobp))
  20754. (or first (> (funcall outline-level) level)))
  20755. (setq first nil)
  20756. (outline-next-heading)))
  20757. (unless to-heading
  20758. (if (memq (preceding-char) '(?\n ?\^M))
  20759. (progn
  20760. ;; Go to end of line before heading
  20761. (forward-char -1)
  20762. (if (memq (preceding-char) '(?\n ?\^M))
  20763. ;; leave blank line before heading
  20764. (forward-char -1))))))
  20765. (point))
  20766. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20767. "Use Org version in org-mode, for dramatic speed-up."
  20768. (if (derived-mode-p 'org-mode)
  20769. (progn
  20770. (org-end-of-subtree nil t)
  20771. (unless (eobp) (backward-char 1)))
  20772. ad-do-it))
  20773. (defun org-end-of-meta-data-and-drawers ()
  20774. "Jump to the first text after meta data and drawers in the current entry.
  20775. This will move over empty lines, lines with planning time stamps,
  20776. clocking lines, and drawers."
  20777. (org-back-to-heading t)
  20778. (let ((end (save-excursion (outline-next-heading) (point)))
  20779. (re (concat "\\(" org-drawer-regexp "\\)"
  20780. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20781. (forward-line 1)
  20782. (while (re-search-forward re end t)
  20783. (if (not (match-end 1))
  20784. ;; empty or planning line
  20785. (forward-line 1)
  20786. ;; a drawer, find the end
  20787. (re-search-forward "^[ \t]*:END:" end 'move)
  20788. (forward-line 1)))
  20789. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20790. (point)))
  20791. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20792. "Move forward to the ARG'th subheading at same level as this one.
  20793. Stop at the first and last subheadings of a superior heading.
  20794. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20795. non-nil it will also look at invisible ones."
  20796. (interactive "p")
  20797. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20798. (if (and arg (< arg 0))
  20799. (goto-char (point-min))
  20800. (outline-next-heading))
  20801. (org-at-heading-p)
  20802. (let ((level (- (match-end 0) (match-beginning 0) 1))
  20803. (f (if (and arg (< arg 0))
  20804. 're-search-backward
  20805. 're-search-forward))
  20806. (count (if arg (abs arg) 1))
  20807. (result (point)))
  20808. (forward-char (if (and arg (< arg 0)) -1 1))
  20809. (while (and (> count 0)
  20810. (funcall f org-outline-regexp-bol nil 'move))
  20811. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  20812. (cond ((< l level) (setq count 0))
  20813. ((and (= l level)
  20814. (or invisible-ok
  20815. (progn
  20816. (goto-char (line-beginning-position))
  20817. (not (outline-invisible-p)))))
  20818. (setq count (1- count))
  20819. (when (eq l level)
  20820. (setq result (point)))))))
  20821. (goto-char result))
  20822. (beginning-of-line 1)))
  20823. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20824. "Move backward to the ARG'th subheading at same level as this one.
  20825. Stop at the first and last subheadings of a superior heading."
  20826. (interactive "p")
  20827. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  20828. (defun org-next-block (arg &optional backward block-regexp)
  20829. "Jump to the next block.
  20830. With a prefix argument ARG, jump forward ARG many source blocks.
  20831. When BACKWARD is non-nil, jump to the previous block.
  20832. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20833. (interactive "p")
  20834. (let ((re (or block-regexp org-block-regexp))
  20835. (re-search-fn (or (and backward 're-search-backward)
  20836. 're-search-forward)))
  20837. (if (looking-at re) (forward-char 1))
  20838. (condition-case nil
  20839. (funcall re-search-fn re nil nil arg)
  20840. (error (error "No %s code blocks" (if backward "previous" "further" ))))
  20841. (goto-char (match-beginning 0)) (org-show-context)))
  20842. (defun org-previous-block (arg &optional block-regexp)
  20843. "Jump to the previous block.
  20844. With a prefix argument ARG, jump backward ARG many source blocks.
  20845. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  20846. (interactive "p")
  20847. (org-next-block arg t block-regexp))
  20848. (defun org-forward-element ()
  20849. "Move forward by one element.
  20850. Move to the next element at the same level, when possible."
  20851. (interactive)
  20852. (cond ((eobp) (error "Cannot move further down"))
  20853. ((org-with-limited-levels (org-at-heading-p))
  20854. (let ((origin (point)))
  20855. (goto-char (org-end-of-subtree nil t))
  20856. (unless (org-with-limited-levels (org-at-heading-p))
  20857. (goto-char origin)
  20858. (error "Cannot move further down"))))
  20859. (t
  20860. (let* ((elem (org-element-at-point))
  20861. (end (org-element-property :end elem))
  20862. (parent (org-element-property :parent elem)))
  20863. (if (and parent (= (org-element-property :contents-end parent) end))
  20864. (goto-char (org-element-property :end parent))
  20865. (goto-char end))))))
  20866. (defun org-backward-element ()
  20867. "Move backward by one element.
  20868. Move to the previous element at the same level, when possible."
  20869. (interactive)
  20870. (cond ((bobp) (error "Cannot move further up"))
  20871. ((org-with-limited-levels (org-at-heading-p))
  20872. ;; At an headline, move to the previous one, if any, or stay
  20873. ;; here.
  20874. (let ((origin (point)))
  20875. (org-with-limited-levels (org-backward-heading-same-level 1))
  20876. ;; When current headline has no sibling above, move to its
  20877. ;; parent.
  20878. (when (= (point) origin)
  20879. (or (org-with-limited-levels (org-up-heading-safe))
  20880. (progn (goto-char origin)
  20881. (error "Cannot move further up"))))))
  20882. (t
  20883. (let* ((trail (org-element-at-point 'keep-trail))
  20884. (elem (car trail))
  20885. (prev-elem (nth 1 trail))
  20886. (beg (org-element-property :begin elem)))
  20887. (cond
  20888. ;; Move to beginning of current element if point isn't
  20889. ;; there already.
  20890. ((/= (point) beg) (goto-char beg))
  20891. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20892. ((org-before-first-heading-p) (goto-char (point-min)))
  20893. (t (org-back-to-heading)))))))
  20894. (defun org-up-element ()
  20895. "Move to upper element."
  20896. (interactive)
  20897. (if (org-with-limited-levels (org-at-heading-p))
  20898. (unless (org-up-heading-safe) (error "No surrounding element"))
  20899. (let* ((elem (org-element-at-point))
  20900. (parent (org-element-property :parent elem)))
  20901. (if parent (goto-char (org-element-property :begin parent))
  20902. (if (org-with-limited-levels (org-before-first-heading-p))
  20903. (error "No surrounding element")
  20904. (org-with-limited-levels (org-back-to-heading)))))))
  20905. (defvar org-element-greater-elements)
  20906. (defun org-down-element ()
  20907. "Move to inner element."
  20908. (interactive)
  20909. (let ((element (org-element-at-point)))
  20910. (cond
  20911. ((memq (org-element-type element) '(plain-list table))
  20912. (goto-char (org-element-property :contents-begin element))
  20913. (forward-char))
  20914. ((memq (org-element-type element) org-element-greater-elements)
  20915. ;; If contents are hidden, first disclose them.
  20916. (when (org-element-property :hiddenp element) (org-cycle))
  20917. (goto-char (or (org-element-property :contents-begin element)
  20918. (error "No content for this element"))))
  20919. (t (error "No inner element")))))
  20920. (defun org-drag-element-backward ()
  20921. "Move backward element at point."
  20922. (interactive)
  20923. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20924. (let* ((trail (org-element-at-point 'keep-trail))
  20925. (elem (car trail))
  20926. (prev-elem (nth 1 trail)))
  20927. ;; Error out if no previous element or previous element is
  20928. ;; a parent of the current one.
  20929. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20930. (error "Cannot drag element backward")
  20931. (let ((pos (point)))
  20932. (org-element-swap-A-B prev-elem elem)
  20933. (goto-char (+ (org-element-property :begin prev-elem)
  20934. (- pos (org-element-property :begin elem)))))))))
  20935. (defun org-drag-element-forward ()
  20936. "Move forward element at point."
  20937. (interactive)
  20938. (let* ((pos (point))
  20939. (elem (org-element-at-point)))
  20940. (when (= (point-max) (org-element-property :end elem))
  20941. (error "Cannot drag element forward"))
  20942. (goto-char (org-element-property :end elem))
  20943. (let ((next-elem (org-element-at-point)))
  20944. (when (or (org-element-nested-p elem next-elem)
  20945. (and (eq (org-element-type next-elem) 'headline)
  20946. (not (eq (org-element-type elem) 'headline))))
  20947. (goto-char pos)
  20948. (error "Cannot drag element forward"))
  20949. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20950. ;; body's length (without final blanks) and by the length of
  20951. ;; blanks between ELEM and NEXT-ELEM.
  20952. (let ((size-next (- (save-excursion
  20953. (goto-char (org-element-property :end next-elem))
  20954. (skip-chars-backward " \r\t\n")
  20955. (forward-line)
  20956. ;; Small correction if buffer doesn't end
  20957. ;; with a newline character.
  20958. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20959. (org-element-property :begin next-elem)))
  20960. (size-blank (- (org-element-property :end elem)
  20961. (save-excursion
  20962. (goto-char (org-element-property :end elem))
  20963. (skip-chars-backward " \r\t\n")
  20964. (forward-line)
  20965. (point)))))
  20966. (org-element-swap-A-B elem next-elem)
  20967. (goto-char (+ pos size-next size-blank))))))
  20968. (defun org-drag-line-forward (arg)
  20969. "Drag the line at point ARG lines forward."
  20970. (interactive "p")
  20971. (dotimes (n (abs arg))
  20972. (let ((c (current-column)))
  20973. (if (< 0 arg)
  20974. (progn
  20975. (beginning-of-line 2)
  20976. (transpose-lines 1)
  20977. (beginning-of-line 0))
  20978. (transpose-lines 1)
  20979. (beginning-of-line -1))
  20980. (org-move-to-column c))))
  20981. (defun org-drag-line-backward (arg)
  20982. "Drag the line at point ARG lines backward."
  20983. (interactive "p")
  20984. (org-drag-line-forward (- arg)))
  20985. (defun org-mark-element ()
  20986. "Put point at beginning of this element, mark at end.
  20987. Interactively, if this command is repeated or (in Transient Mark
  20988. mode) if the mark is active, it marks the next element after the
  20989. ones already marked."
  20990. (interactive)
  20991. (let (deactivate-mark)
  20992. (if (and (org-called-interactively-p 'any)
  20993. (or (and (eq last-command this-command) (mark t))
  20994. (and transient-mark-mode mark-active)))
  20995. (set-mark
  20996. (save-excursion
  20997. (goto-char (mark))
  20998. (goto-char (org-element-property :end (org-element-at-point)))))
  20999. (let ((element (org-element-at-point)))
  21000. (end-of-line)
  21001. (push-mark (org-element-property :end element) t t)
  21002. (goto-char (org-element-property :begin element))))))
  21003. (defun org-narrow-to-element ()
  21004. "Narrow buffer to current element."
  21005. (interactive)
  21006. (let ((elem (org-element-at-point)))
  21007. (cond
  21008. ((eq (car elem) 'headline)
  21009. (narrow-to-region
  21010. (org-element-property :begin elem)
  21011. (org-element-property :end elem)))
  21012. ((memq (car elem) org-element-greater-elements)
  21013. (narrow-to-region
  21014. (org-element-property :contents-begin elem)
  21015. (org-element-property :contents-end elem)))
  21016. (t
  21017. (narrow-to-region
  21018. (org-element-property :begin elem)
  21019. (org-element-property :end elem))))))
  21020. (defun org-transpose-element ()
  21021. "Transpose current and previous elements, keeping blank lines between.
  21022. Point is moved after both elements."
  21023. (interactive)
  21024. (org-skip-whitespace)
  21025. (let ((end (org-element-property :end (org-element-at-point))))
  21026. (org-drag-element-backward)
  21027. (goto-char end)))
  21028. (defun org-unindent-buffer ()
  21029. "Un-indent the visible part of the buffer.
  21030. Relative indentation (between items, inside blocks, etc.) isn't
  21031. modified."
  21032. (interactive)
  21033. (unless (eq major-mode 'org-mode)
  21034. (error "Cannot un-indent a buffer not in Org mode"))
  21035. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  21036. unindent-tree ; For byte-compiler.
  21037. (unindent-tree
  21038. (function
  21039. (lambda (contents)
  21040. (mapc
  21041. (lambda (element)
  21042. (if (memq (org-element-type element) '(headline section))
  21043. (funcall unindent-tree (org-element-contents element))
  21044. (save-excursion
  21045. (save-restriction
  21046. (narrow-to-region
  21047. (org-element-property :begin element)
  21048. (org-element-property :end element))
  21049. (org-do-remove-indentation)))))
  21050. (reverse contents))))))
  21051. (funcall unindent-tree (org-element-contents parse-tree))))
  21052. (defun org-show-subtree ()
  21053. "Show everything after this heading at deeper levels."
  21054. (interactive)
  21055. (outline-flag-region
  21056. (point)
  21057. (save-excursion
  21058. (org-end-of-subtree t t))
  21059. nil))
  21060. (defun org-show-entry ()
  21061. "Show the body directly following this heading.
  21062. Show the heading too, if it is currently invisible."
  21063. (interactive)
  21064. (save-excursion
  21065. (condition-case nil
  21066. (progn
  21067. (org-back-to-heading t)
  21068. (outline-flag-region
  21069. (max (point-min) (1- (point)))
  21070. (save-excursion
  21071. (if (re-search-forward
  21072. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21073. (match-beginning 1)
  21074. (point-max)))
  21075. nil)
  21076. (org-cycle-hide-drawers 'children))
  21077. (error nil))))
  21078. (defun org-make-options-regexp (kwds &optional extra)
  21079. "Make a regular expression for keyword lines."
  21080. (concat
  21081. "^#\\+\\("
  21082. (mapconcat 'regexp-quote kwds "\\|")
  21083. (if extra (concat "\\|" extra))
  21084. "\\):[ \t]*\\(.*\\)"))
  21085. ;; Make isearch reveal the necessary context
  21086. (defun org-isearch-end ()
  21087. "Reveal context after isearch exits."
  21088. (when isearch-success ; only if search was successful
  21089. (if (featurep 'xemacs)
  21090. ;; Under XEmacs, the hook is run in the correct place,
  21091. ;; we directly show the context.
  21092. (org-show-context 'isearch)
  21093. ;; In Emacs the hook runs *before* restoring the overlays.
  21094. ;; So we have to use a one-time post-command-hook to do this.
  21095. ;; (Emacs 22 has a special variable, see function `org-mode')
  21096. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21097. isearch-mode-end-hook-quit)
  21098. ;; Only when the isearch was not quitted.
  21099. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21100. 'append 'local)))))
  21101. (defun org-isearch-post-command ()
  21102. "Remove self from hook, and show context."
  21103. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21104. (org-show-context 'isearch))
  21105. ;;;; Integration with and fixes for other packages
  21106. ;;; Imenu support
  21107. (defvar org-imenu-markers nil
  21108. "All markers currently used by Imenu.")
  21109. (make-variable-buffer-local 'org-imenu-markers)
  21110. (defun org-imenu-new-marker (&optional pos)
  21111. "Return a new marker for use by Imenu, and remember the marker."
  21112. (let ((m (make-marker)))
  21113. (move-marker m (or pos (point)))
  21114. (push m org-imenu-markers)
  21115. m))
  21116. (defun org-imenu-get-tree ()
  21117. "Produce the index for Imenu."
  21118. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21119. (setq org-imenu-markers nil)
  21120. (let* ((n org-imenu-depth)
  21121. (re (concat "^" (org-get-limited-outline-regexp)))
  21122. (subs (make-vector (1+ n) nil))
  21123. (last-level 0)
  21124. m level head)
  21125. (save-excursion
  21126. (save-restriction
  21127. (widen)
  21128. (goto-char (point-max))
  21129. (while (re-search-backward re nil t)
  21130. (setq level (org-reduced-level (funcall outline-level)))
  21131. (when (and (<= level n)
  21132. (looking-at org-complex-heading-regexp))
  21133. (setq head (org-link-display-format
  21134. (org-match-string-no-properties 4))
  21135. m (org-imenu-new-marker))
  21136. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21137. (if (>= level last-level)
  21138. (push (cons head m) (aref subs level))
  21139. (push (cons head (aref subs (1+ level))) (aref subs level))
  21140. (loop for i from (1+ level) to n do (aset subs i nil)))
  21141. (setq last-level level)))))
  21142. (aref subs 1)))
  21143. (eval-after-load "imenu"
  21144. '(progn
  21145. (add-hook 'imenu-after-jump-hook
  21146. (lambda ()
  21147. (if (derived-mode-p 'org-mode)
  21148. (org-show-context 'org-goto))))))
  21149. (defun org-link-display-format (link)
  21150. "Replace a link with either the description, or the link target
  21151. if no description is present"
  21152. (save-match-data
  21153. (if (string-match org-bracket-link-analytic-regexp link)
  21154. (replace-match (if (match-end 5)
  21155. (match-string 5 link)
  21156. (concat (match-string 1 link)
  21157. (match-string 3 link)))
  21158. nil t link)
  21159. link)))
  21160. (defun org-toggle-link-display ()
  21161. "Toggle the literal or descriptive display of links."
  21162. (interactive)
  21163. (if org-descriptive-links
  21164. (progn (org-remove-from-invisibility-spec '(org-link))
  21165. (org-restart-font-lock)
  21166. (setq org-descriptive-links nil))
  21167. (progn (add-to-invisibility-spec '(org-link))
  21168. (org-restart-font-lock)
  21169. (setq org-descriptive-links t))))
  21170. ;; Speedbar support
  21171. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21172. "Overlay marking the agenda restriction line in speedbar.")
  21173. (overlay-put org-speedbar-restriction-lock-overlay
  21174. 'face 'org-agenda-restriction-lock)
  21175. (overlay-put org-speedbar-restriction-lock-overlay
  21176. 'help-echo "Agendas are currently limited to this item.")
  21177. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21178. (defun org-speedbar-set-agenda-restriction ()
  21179. "Restrict future agenda commands to the location at point in speedbar.
  21180. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21181. (interactive)
  21182. (require 'org-agenda)
  21183. (let (p m tp np dir txt)
  21184. (cond
  21185. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21186. 'org-imenu t))
  21187. (setq m (get-text-property p 'org-imenu-marker))
  21188. (with-current-buffer (marker-buffer m)
  21189. (goto-char m)
  21190. (org-agenda-set-restriction-lock 'subtree)))
  21191. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21192. 'speedbar-function 'speedbar-find-file))
  21193. (setq tp (previous-single-property-change
  21194. (1+ p) 'speedbar-function)
  21195. np (next-single-property-change
  21196. tp 'speedbar-function)
  21197. dir (speedbar-line-directory)
  21198. txt (buffer-substring-no-properties (or tp (point-min))
  21199. (or np (point-max))))
  21200. (with-current-buffer (find-file-noselect
  21201. (let ((default-directory dir))
  21202. (expand-file-name txt)))
  21203. (unless (derived-mode-p 'org-mode)
  21204. (error "Cannot restrict to non-Org-mode file"))
  21205. (org-agenda-set-restriction-lock 'file)))
  21206. (t (error "Don't know how to restrict Org-mode's agenda")))
  21207. (move-overlay org-speedbar-restriction-lock-overlay
  21208. (point-at-bol) (point-at-eol))
  21209. (setq current-prefix-arg nil)
  21210. (org-agenda-maybe-redo)))
  21211. (eval-after-load "speedbar"
  21212. '(progn
  21213. (speedbar-add-supported-extension ".org")
  21214. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21215. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21216. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21217. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21218. (add-hook 'speedbar-visiting-tag-hook
  21219. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21220. ;;; Fixes and Hacks for problems with other packages
  21221. ;; Make flyspell not check words in links, to not mess up our keymap
  21222. (defvar org-element-affiliated-keywords) ; From org-element.el
  21223. (defvar org-element-block-name-alist) ; From org-element.el
  21224. (defun org-mode-flyspell-verify ()
  21225. "Don't let flyspell put overlays at active buttons, or on
  21226. {todo,all-time,additional-option-like}-keywords."
  21227. (let ((pos (max (1- (point)) (point-min)))
  21228. (word (thing-at-point 'word)))
  21229. (and (not (get-text-property pos 'keymap))
  21230. (not (get-text-property pos 'org-no-flyspell))
  21231. (not (member word org-todo-keywords-1))
  21232. (not (member word org-all-time-keywords))
  21233. (not (member word org-options-keywords))
  21234. (not (member word (mapcar 'car org-startup-options)))
  21235. (not (member-ignore-case word org-element-affiliated-keywords))
  21236. (not (member-ignore-case word (org-get-export-keywords)))
  21237. (not (member-ignore-case
  21238. word (mapcar 'car org-element-block-name-alist)))
  21239. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21240. (defun org-remove-flyspell-overlays-in (beg end)
  21241. "Remove flyspell overlays in region."
  21242. (and (org-bound-and-true-p flyspell-mode)
  21243. (fboundp 'flyspell-delete-region-overlays)
  21244. (flyspell-delete-region-overlays beg end))
  21245. (add-text-properties beg end '(org-no-flyspell t)))
  21246. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21247. (eval-after-load "bookmark"
  21248. '(if (boundp 'bookmark-after-jump-hook)
  21249. ;; We can use the hook
  21250. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21251. ;; Hook not available, use advice
  21252. (defadvice bookmark-jump (after org-make-visible activate)
  21253. "Make the position visible."
  21254. (org-bookmark-jump-unhide))))
  21255. ;; Make sure saveplace shows the location if it was hidden
  21256. (eval-after-load "saveplace"
  21257. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21258. "Make the position visible."
  21259. (org-bookmark-jump-unhide)))
  21260. ;; Make sure ecb shows the location if it was hidden
  21261. (eval-after-load "ecb"
  21262. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21263. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21264. (if (derived-mode-p 'org-mode)
  21265. (org-show-context))))
  21266. (defun org-bookmark-jump-unhide ()
  21267. "Unhide the current position, to show the bookmark location."
  21268. (and (derived-mode-p 'org-mode)
  21269. (or (outline-invisible-p)
  21270. (save-excursion (goto-char (max (point-min) (1- (point))))
  21271. (outline-invisible-p)))
  21272. (org-show-context 'bookmark-jump)))
  21273. ;; Make session.el ignore our circular variable
  21274. (eval-after-load "session"
  21275. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21276. ;;;; Experimental code
  21277. (defun org-closed-in-range ()
  21278. "Sparse tree of items closed in a certain time range.
  21279. Still experimental, may disappear in the future."
  21280. (interactive)
  21281. ;; Get the time interval from the user.
  21282. (let* ((time1 (org-float-time
  21283. (org-read-date nil 'to-time nil "Starting date: ")))
  21284. (time2 (org-float-time
  21285. (org-read-date nil 'to-time nil "End date:")))
  21286. ;; callback function
  21287. (callback (lambda ()
  21288. (let ((time
  21289. (org-float-time
  21290. (apply 'encode-time
  21291. (org-parse-time-string
  21292. (match-string 1))))))
  21293. ;; check if time in interval
  21294. (and (>= time time1) (<= time time2))))))
  21295. ;; make tree, check each match with the callback
  21296. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21297. ;;;; Finish up
  21298. (provide 'org)
  21299. (run-hooks 'org-load-hook)
  21300. ;;; org.el ends here