org.el 875 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142
  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. (autoload 'org-element-at-point "org-element")
  125. (autoload 'org-element-type "org-element")
  126. (declare-function org-element--parse-objects "org-element"
  127. (beg end acc restriction))
  128. (declare-function org-element-at-point "org-element" (&optional keep-trail))
  129. (declare-function org-element-contents "org-element" (element))
  130. (declare-function org-element-context "org-element" (&optional element))
  131. (declare-function org-element-interpret-data "org-element"
  132. (data &optional parent))
  133. (declare-function org-element-map "org-element"
  134. (data types fun &optional info first-match no-recursion))
  135. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  136. (declare-function org-element-parse-buffer "org-element"
  137. (&optional granularity visible-only))
  138. (declare-function org-element-property "org-element" (property element))
  139. (declare-function org-element-put-property "org-element"
  140. (element property value))
  141. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  142. (declare-function org-element--parse-objects "org-element"
  143. (beg end acc restriction))
  144. (declare-function org-element-parse-buffer "org-element"
  145. (&optional granularity visible-only))
  146. (declare-function org-element-interpret-data "org-element"
  147. (data &optional parent))
  148. ;; load languages based on value of `org-babel-load-languages'
  149. (defvar org-babel-load-languages)
  150. ;;;###autoload
  151. (defun org-babel-do-load-languages (sym value)
  152. "Load the languages defined in `org-babel-load-languages'."
  153. (set-default sym value)
  154. (mapc (lambda (pair)
  155. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  156. (if active
  157. (progn
  158. (require (intern (concat "ob-" lang))))
  159. (progn
  160. (funcall 'fmakunbound
  161. (intern (concat "org-babel-execute:" lang)))
  162. (funcall 'fmakunbound
  163. (intern (concat "org-babel-expand-body:" lang)))))))
  164. org-babel-load-languages))
  165. (defcustom org-babel-load-languages '((emacs-lisp . t))
  166. "Languages which can be evaluated in Org-mode buffers.
  167. This list can be used to load support for any of the languages
  168. below, note that each language will depend on a different set of
  169. system executables and/or Emacs modes. When a language is
  170. \"loaded\", then code blocks in that language can be evaluated
  171. with `org-babel-execute-src-block' bound by default to C-c
  172. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  173. be set to remove code block evaluation from the C-c C-c
  174. keybinding. By default only Emacs Lisp (which has no
  175. requirements) is loaded."
  176. :group 'org-babel
  177. :set 'org-babel-do-load-languages
  178. :version "24.1"
  179. :type '(alist :tag "Babel Languages"
  180. :key-type
  181. (choice
  182. (const :tag "Awk" awk)
  183. (const :tag "C" C)
  184. (const :tag "R" R)
  185. (const :tag "Asymptote" asymptote)
  186. (const :tag "Calc" calc)
  187. (const :tag "Clojure" clojure)
  188. (const :tag "CSS" css)
  189. (const :tag "Ditaa" ditaa)
  190. (const :tag "Dot" dot)
  191. (const :tag "Emacs Lisp" emacs-lisp)
  192. (const :tag "Fortran" fortran)
  193. (const :tag "Gnuplot" gnuplot)
  194. (const :tag "Haskell" haskell)
  195. (const :tag "IO" io)
  196. (const :tag "Java" java)
  197. (const :tag "Javascript" js)
  198. (const :tag "LaTeX" latex)
  199. (const :tag "Ledger" ledger)
  200. (const :tag "Lilypond" lilypond)
  201. (const :tag "Lisp" lisp)
  202. (const :tag "Makefile" makefile)
  203. (const :tag "Maxima" maxima)
  204. (const :tag "Matlab" matlab)
  205. (const :tag "Mscgen" mscgen)
  206. (const :tag "Ocaml" ocaml)
  207. (const :tag "Octave" octave)
  208. (const :tag "Org" org)
  209. (const :tag "Perl" perl)
  210. (const :tag "Pico Lisp" picolisp)
  211. (const :tag "PlantUML" plantuml)
  212. (const :tag "Python" python)
  213. (const :tag "Ruby" ruby)
  214. (const :tag "Sass" sass)
  215. (const :tag "Scala" scala)
  216. (const :tag "Scheme" scheme)
  217. (const :tag "Screen" screen)
  218. (const :tag "Shell Script" sh)
  219. (const :tag "Shen" shen)
  220. (const :tag "Sql" sql)
  221. (const :tag "Sqlite" sqlite))
  222. :value-type (boolean :tag "Activate" :value t)))
  223. ;;;; Customization variables
  224. (defcustom org-clone-delete-id nil
  225. "Remove ID property of clones of a subtree.
  226. When non-nil, clones of a subtree don't inherit the ID property.
  227. Otherwise they inherit the ID property with a new unique
  228. identifier."
  229. :type 'boolean
  230. :version "24.1"
  231. :group 'org-id)
  232. ;;; Version
  233. (org-check-version)
  234. ;;;###autoload
  235. (defun org-version (&optional here full message)
  236. "Show the org-mode version in the echo area.
  237. With prefix argument HERE, insert it at point.
  238. When FULL is non-nil, use a verbose version string.
  239. When MESSAGE is non-nil, display a message with the version."
  240. (interactive "P")
  241. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  242. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  243. (load-suffixes (list ".el"))
  244. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  245. (org-trash (or
  246. (and (fboundp 'org-release) (fboundp 'org-git-version))
  247. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  248. (load-suffixes save-load-suffixes)
  249. (org-version (org-release))
  250. (git-version (org-git-version))
  251. (version (format "Org-mode version %s (%s @ %s)"
  252. org-version
  253. git-version
  254. (if org-install-dir
  255. (if (string= org-dir org-install-dir)
  256. org-install-dir
  257. (concat "mixed installation! " org-install-dir " and " org-dir))
  258. "org-loaddefs.el can not be found!")))
  259. (_version (if full version org-version)))
  260. (if (org-called-interactively-p 'interactive)
  261. (if here
  262. (insert version)
  263. (message version))
  264. (if message (message _version))
  265. _version)))
  266. (defconst org-version (org-version))
  267. ;;; Compatibility constants
  268. ;;; The custom variables
  269. (defgroup org nil
  270. "Outline-based notes management and organizer."
  271. :tag "Org"
  272. :group 'outlines
  273. :group 'calendar)
  274. (defcustom org-mode-hook nil
  275. "Mode hook for Org-mode, run after the mode was turned on."
  276. :group 'org
  277. :type 'hook)
  278. (defcustom org-load-hook nil
  279. "Hook that is run after org.el has been loaded."
  280. :group 'org
  281. :type 'hook)
  282. (defcustom org-log-buffer-setup-hook nil
  283. "Hook that is run after an Org log buffer is created."
  284. :group 'org
  285. :version "24.1"
  286. :type 'hook)
  287. (defvar org-modules) ; defined below
  288. (defvar org-modules-loaded nil
  289. "Have the modules been loaded already?")
  290. (defun org-load-modules-maybe (&optional force)
  291. "Load all extensions listed in `org-modules'."
  292. (when (or force (not org-modules-loaded))
  293. (mapc (lambda (ext)
  294. (condition-case nil (require ext)
  295. (error (message "Problems while trying to load feature `%s'" ext))))
  296. org-modules)
  297. (setq org-modules-loaded t)))
  298. (defun org-set-modules (var value)
  299. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  300. (set var value)
  301. (when (featurep 'org)
  302. (org-load-modules-maybe 'force)))
  303. (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)
  304. "Modules that should always be loaded together with org.el.
  305. If a description starts with <C>, the file is not part of Emacs
  306. and loading it will require that you have downloaded and properly
  307. installed the Org mode distribution.
  308. You can also use this system to load external packages (i.e. neither Org
  309. core modules, nor modules from the CONTRIB directory). Just add symbols
  310. to the end of the list. If the package is called org-xyz.el, then you need
  311. to add the symbol `xyz', and the package must have a call to:
  312. \(provide 'org-xyz)
  313. For export specific modules, see also `org-export-backends'."
  314. :group 'org
  315. :set 'org-set-modules
  316. :type
  317. '(set :greedy t
  318. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  319. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  320. (const :tag " crypt: Encryption of subtrees" org-crypt)
  321. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  322. (const :tag " docview: Links to doc-view buffers" org-docview)
  323. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  324. (const :tag " id: Global IDs for identifying entries" org-id)
  325. (const :tag " info: Links to Info nodes" org-info)
  326. (const :tag " habit: Track your consistency with habits" org-habit)
  327. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  328. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  329. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  330. (const :tag " mew Links to Mew folders/messages" org-mew)
  331. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  332. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  333. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  334. (const :tag " vm: Links to VM folders/messages" org-vm)
  335. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  336. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  337. (const :tag " mouse: Additional mouse support" org-mouse)
  338. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  339. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  340. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  341. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  342. (const :tag "C collector: Collect properties into tables" org-collector)
  343. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  344. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  345. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  346. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  347. (const :tag "C eval: Include command output as text" org-eval)
  348. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  349. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  350. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  351. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  352. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  353. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  354. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  355. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  356. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  357. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  358. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  359. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  360. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  361. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  362. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  363. (const :tag "C registry: A registry for Org-mode links" org-registry)
  364. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  365. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  366. (const :tag "C secretary: Team management with org-mode" org-secretary)
  367. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  368. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  369. (const :tag "C track: Keep up with Org-mode development" org-track)
  370. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  371. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  372. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  373. (defvar org-export-registered-backends) ; From ox.el
  374. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  375. (defcustom org-export-backends '(ascii html icalendar latex)
  376. "List of export back-ends that should be always available.
  377. If a description starts with <C>, the file is not part of Emacs
  378. and loading it will require that you have downloaded and properly
  379. installed the Org mode distribution.
  380. Unlike to `org-modules', libraries in this list will not be
  381. loaded along with Org, but only once the export framework is
  382. needed.
  383. This variable needs to be set before org.el is loaded. If you
  384. need to make a change while Emacs is running, use the customize
  385. interface or run the following code, , where VALUE stands for the
  386. new value of the variable, after updating it:
  387. \(progn
  388. \(setq org-export-registered-backends
  389. \(org-remove-if-not
  390. \(lambda (backend)
  391. \(or (memq backend val)
  392. \(catch 'parentp
  393. \(mapc
  394. \(lambda (b)
  395. \(and (org-export-derived-backend-p b (car backend))
  396. \(throw 'parentp t)))
  397. val)
  398. nil)))
  399. org-export-registered-backends))
  400. \(let ((new-list (mapcar 'car org-export-registered-backends)))
  401. \(dolist (backend val)
  402. \(cond
  403. \((not (load (format \"ox-%s\" backend) t t))
  404. \(message \"Problems while trying to load export back-end `%s'\"
  405. backend))
  406. \((not (memq backend new-list)) (push backend new-list))))
  407. \(set-default var new-list)))
  408. Adding a back-end to this list will also pull the back-end it
  409. depends on, if any."
  410. :group 'org
  411. :group 'org-export
  412. :set (lambda (var val)
  413. (if (not (featurep 'ox)) (set-default var val)
  414. ;; Any back-end not required anymore (not present in VAL and not
  415. ;; a parent of any back-end in the new value) is removed from the
  416. ;; list of registered back-ends.
  417. (setq org-export-registered-backends
  418. (org-remove-if-not
  419. (lambda (backend)
  420. (or (memq backend val)
  421. (catch 'parentp
  422. (mapc
  423. (lambda (b)
  424. (and (org-export-derived-backend-p b (car backend))
  425. (throw 'parentp t)))
  426. val)
  427. nil)))
  428. org-export-registered-backends))
  429. ;; Now build NEW-LIST of both new back-ends and required
  430. ;; parents.
  431. (let ((new-list (mapcar 'car org-export-registered-backends)))
  432. (dolist (backend val)
  433. (cond
  434. ((not (load (format "ox-%s" backend) t t))
  435. (message "Problems while trying to load export back-end `%s'"
  436. backend))
  437. ((not (memq backend new-list)) (push backend new-list))))
  438. ;; Set VAR to that list with fixed dependencies.
  439. (set-default var new-list))))
  440. :initialize 'custom-initialize-default
  441. :type '(set :greedy t
  442. (const :tag " ascii Export buffer to ASCII format" ascii)
  443. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  444. (const :tag " html Export buffer to HTML format" html)
  445. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  446. (const :tag " latex Export buffer to LaTeX format" latex)
  447. (const :tag " man Export buffer to MAN format" man)
  448. (const :tag " md Export buffer to Markdown format" md)
  449. (const :tag " odt Export buffer to ODT format" odt)
  450. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  451. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" jsinfo)
  452. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  453. (const :tag "C groff Export buffer to Groff format" groff)
  454. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)))
  455. (eval-after-load 'ox
  456. '(mapc
  457. (lambda (backend)
  458. (condition-case nil (require (intern (format "ox-%s" backend)))
  459. (error (message "Problems while trying to load export back-end `%s'"
  460. backend))))
  461. org-export-backends))
  462. (defcustom org-support-shift-select nil
  463. "Non-nil means make shift-cursor commands select text when possible.
  464. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  465. start selecting a region, or enlarge regions started in this way.
  466. In Org-mode, in special contexts, these same keys are used for
  467. other purposes, important enough to compete with shift selection.
  468. Org tries to balance these needs by supporting `shift-select-mode'
  469. outside these special contexts, under control of this variable.
  470. The default of this variable is nil, to avoid confusing behavior. Shifted
  471. cursor keys will then execute Org commands in the following contexts:
  472. - on a headline, changing TODO state (left/right) and priority (up/down)
  473. - on a time stamp, changing the time
  474. - in a plain list item, changing the bullet type
  475. - in a property definition line, switching between allowed values
  476. - in the BEGIN line of a clock table (changing the time block).
  477. Outside these contexts, the commands will throw an error.
  478. When this variable is t and the cursor is not in a special
  479. context, Org-mode will support shift-selection for making and
  480. enlarging regions. To make this more effective, the bullet
  481. cycling will no longer happen anywhere in an item line, but only
  482. if the cursor is exactly on the bullet.
  483. If you set this variable to the symbol `always', then the keys
  484. will not be special in headlines, property lines, and item lines,
  485. to make shift selection work there as well. If this is what you
  486. want, you can use the following alternative commands: `C-c C-t'
  487. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  488. can be used to switch TODO sets, `C-c -' to cycle item bullet
  489. types, and properties can be edited by hand or in column view.
  490. However, when the cursor is on a timestamp, shift-cursor commands
  491. will still edit the time stamp - this is just too good to give up.
  492. XEmacs user should have this variable set to nil, because
  493. `shift-select-mode' is in Emacs 23 or later only."
  494. :group 'org
  495. :type '(choice
  496. (const :tag "Never" nil)
  497. (const :tag "When outside special context" t)
  498. (const :tag "Everywhere except timestamps" always)))
  499. (defcustom org-loop-over-headlines-in-active-region nil
  500. "Shall some commands act upon headlines in the active region?
  501. When set to `t', some commands will be performed in all headlines
  502. within the active region.
  503. When set to `start-level', some commands will be performed in all
  504. headlines within the active region, provided that these headlines
  505. are of the same level than the first one.
  506. When set to a string, those commands will be performed on the
  507. matching headlines within the active region. Such string must be
  508. a tags/property/todo match as it is used in the agenda tags view.
  509. The list of commands is: `org-schedule', `org-deadline',
  510. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  511. `org-archive-to-archive-sibling'. The archiving commands skip
  512. already archived entries."
  513. :type '(choice (const :tag "Don't loop" nil)
  514. (const :tag "All headlines in active region" t)
  515. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  516. (string :tag "Tags/Property/Todo matcher"))
  517. :version "24.1"
  518. :group 'org-todo
  519. :group 'org-archive)
  520. (defgroup org-startup nil
  521. "Options concerning startup of Org-mode."
  522. :tag "Org Startup"
  523. :group 'org)
  524. (defcustom org-startup-folded t
  525. "Non-nil means entering Org-mode will switch to OVERVIEW.
  526. This can also be configured on a per-file basis by adding one of
  527. the following lines anywhere in the buffer:
  528. #+STARTUP: fold (or `overview', this is equivalent)
  529. #+STARTUP: nofold (or `showall', this is equivalent)
  530. #+STARTUP: content
  531. #+STARTUP: showeverything"
  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-insert-mode-line-in-empty-file nil
  605. "Non-nil means insert the first line setting Org-mode in empty files.
  606. When the function `org-mode' is called interactively in an empty file, this
  607. normally means that the file name does not automatically trigger Org-mode.
  608. To ensure that the file will always be in Org-mode in the future, a
  609. line enforcing Org-mode will be inserted into the buffer, if this option
  610. has been set."
  611. :group 'org-startup
  612. :type 'boolean)
  613. (defcustom org-replace-disputed-keys nil
  614. "Non-nil means use alternative key bindings for some keys.
  615. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  616. These keys are also used by other packages like shift-selection-mode'
  617. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  618. If you want to use Org-mode together with one of these other modes,
  619. or more generally if you would like to move some Org-mode commands to
  620. other keys, set this variable and configure the keys with the variable
  621. `org-disputed-keys'.
  622. This option is only relevant at load-time of Org-mode, and must be set
  623. *before* org.el is loaded. Changing it requires a restart of Emacs to
  624. become effective."
  625. :group 'org-startup
  626. :type 'boolean)
  627. (defcustom org-use-extra-keys nil
  628. "Non-nil means use extra key sequence definitions for certain commands.
  629. This happens automatically if you run XEmacs or if `window-system'
  630. is nil. This variable lets you do the same manually. You must
  631. set it before loading org.
  632. Example: on Carbon Emacs 22 running graphically, with an external
  633. keyboard on a Powerbook, the default way of setting M-left might
  634. not work for either Alt or ESC. Setting this variable will make
  635. it work for ESC."
  636. :group 'org-startup
  637. :type 'boolean)
  638. (if (fboundp 'defvaralias)
  639. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  640. (defcustom org-disputed-keys
  641. '(([(shift up)] . [(meta p)])
  642. ([(shift down)] . [(meta n)])
  643. ([(shift left)] . [(meta -)])
  644. ([(shift right)] . [(meta +)])
  645. ([(control shift right)] . [(meta shift +)])
  646. ([(control shift left)] . [(meta shift -)]))
  647. "Keys for which Org-mode and other modes compete.
  648. This is an alist, cars are the default keys, second element specifies
  649. the alternative to use when `org-replace-disputed-keys' is t.
  650. Keys can be specified in any syntax supported by `define-key'.
  651. The value of this option takes effect only at Org-mode's startup,
  652. therefore you'll have to restart Emacs to apply it after changing."
  653. :group 'org-startup
  654. :type 'alist)
  655. (defun org-key (key)
  656. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  657. Or return the original if not disputed.
  658. Also apply the translations defined in `org-xemacs-key-equivalents'."
  659. (when org-replace-disputed-keys
  660. (let* ((nkey (key-description key))
  661. (x (org-find-if (lambda (x)
  662. (equal (key-description (car x)) nkey))
  663. org-disputed-keys)))
  664. (setq key (if x (cdr x) key))))
  665. (when (featurep 'xemacs)
  666. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  667. key)
  668. (defun org-find-if (predicate seq)
  669. (catch 'exit
  670. (while seq
  671. (if (funcall predicate (car seq))
  672. (throw 'exit (car seq))
  673. (pop seq)))))
  674. (defun org-defkey (keymap key def)
  675. "Define a key, possibly translated, as returned by `org-key'."
  676. (define-key keymap (org-key key) def))
  677. (defcustom org-ellipsis nil
  678. "The ellipsis to use in the Org-mode outline.
  679. When nil, just use the standard three dots. When a string, use that instead,
  680. When a face, use the standard 3 dots, but with the specified face.
  681. The change affects only Org-mode (which will then use its own display table).
  682. Changing this requires executing `M-x org-mode' in a buffer to become
  683. effective."
  684. :group 'org-startup
  685. :type '(choice (const :tag "Default" nil)
  686. (face :tag "Face" :value org-warning)
  687. (string :tag "String" :value "...#")))
  688. (defvar org-display-table nil
  689. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  690. (defgroup org-keywords nil
  691. "Keywords in Org-mode."
  692. :tag "Org Keywords"
  693. :group 'org)
  694. (defcustom org-deadline-string "DEADLINE:"
  695. "String to mark deadline entries.
  696. A deadline is this string, followed by a time stamp. Should be a word,
  697. terminated by a colon. You can insert a schedule keyword and
  698. a timestamp with \\[org-deadline].
  699. Changes become only effective after restarting Emacs."
  700. :group 'org-keywords
  701. :type 'string)
  702. (defcustom org-scheduled-string "SCHEDULED:"
  703. "String to mark scheduled TODO entries.
  704. A schedule is this string, followed by a time stamp. Should be a word,
  705. terminated by a colon. You can insert a schedule keyword and
  706. a timestamp with \\[org-schedule].
  707. Changes become only effective after restarting Emacs."
  708. :group 'org-keywords
  709. :type 'string)
  710. (defcustom org-closed-string "CLOSED:"
  711. "String used as the prefix for timestamps logging closing a TODO entry."
  712. :group 'org-keywords
  713. :type 'string)
  714. (defcustom org-clock-string "CLOCK:"
  715. "String used as prefix for timestamps clocking work hours on an item."
  716. :group 'org-keywords
  717. :type 'string)
  718. (defconst org-planning-or-clock-line-re (concat "^[ \t]*\\("
  719. org-scheduled-string "\\|"
  720. org-deadline-string "\\|"
  721. org-closed-string "\\|"
  722. org-clock-string "\\)")
  723. "Matches a line with planning or clock info.")
  724. (defcustom org-comment-string "COMMENT"
  725. "Entries starting with this keyword will never be exported.
  726. An entry can be toggled between COMMENT and normal with
  727. \\[org-toggle-comment].
  728. Changes become only effective after restarting Emacs."
  729. :group 'org-keywords
  730. :type 'string)
  731. (defcustom org-quote-string "QUOTE"
  732. "Entries starting with this keyword will be exported in fixed-width font.
  733. Quoting applies only to the text in the entry following the headline, and does
  734. not extend beyond the next headline, even if that is lower level.
  735. An entry can be toggled between QUOTE and normal with
  736. \\[org-toggle-fixed-width-section]."
  737. :group 'org-keywords
  738. :type 'string)
  739. (defconst org-repeat-re
  740. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  741. "Regular expression for specifying repeated events.
  742. After a match, group 1 contains the repeat expression.")
  743. (defgroup org-structure nil
  744. "Options concerning the general structure of Org-mode files."
  745. :tag "Org Structure"
  746. :group 'org)
  747. (defgroup org-reveal-location nil
  748. "Options about how to make context of a location visible."
  749. :tag "Org Reveal Location"
  750. :group 'org-structure)
  751. (defconst org-context-choice
  752. '(choice
  753. (const :tag "Always" t)
  754. (const :tag "Never" nil)
  755. (repeat :greedy t :tag "Individual contexts"
  756. (cons
  757. (choice :tag "Context"
  758. (const agenda)
  759. (const org-goto)
  760. (const occur-tree)
  761. (const tags-tree)
  762. (const link-search)
  763. (const mark-goto)
  764. (const bookmark-jump)
  765. (const isearch)
  766. (const default))
  767. (boolean))))
  768. "Contexts for the reveal options.")
  769. (defcustom org-show-hierarchy-above '((default . t))
  770. "Non-nil means show full hierarchy when revealing a location.
  771. Org-mode often shows locations in an org-mode file which might have
  772. been invisible before. When this is set, the hierarchy of headings
  773. above the exposed location is shown.
  774. Turning this off for example for sparse trees makes them very compact.
  775. Instead of t, this can also be an alist specifying this option for different
  776. contexts. Valid contexts are
  777. agenda when exposing an entry from the agenda
  778. org-goto when using the command `org-goto' on key C-c C-j
  779. occur-tree when using the command `org-occur' on key C-c /
  780. tags-tree when constructing a sparse tree based on tags matches
  781. link-search when exposing search matches associated with a link
  782. mark-goto when exposing the jump goal of a mark
  783. bookmark-jump when exposing a bookmark location
  784. isearch when exiting from an incremental search
  785. default default for all contexts not set explicitly"
  786. :group 'org-reveal-location
  787. :type org-context-choice)
  788. (defcustom org-show-following-heading '((default . nil))
  789. "Non-nil means show following heading when revealing a location.
  790. Org-mode often shows locations in an org-mode file which might have
  791. been invisible before. When this is set, the heading following the
  792. match is shown.
  793. Turning this off for example for sparse trees makes them very compact,
  794. but makes it harder to edit the location of the match. In such a case,
  795. use the command \\[org-reveal] to show more context.
  796. Instead of t, this can also be an alist specifying this option for different
  797. contexts. See `org-show-hierarchy-above' for valid contexts."
  798. :group 'org-reveal-location
  799. :type org-context-choice)
  800. (defcustom org-show-siblings '((default . nil) (isearch t))
  801. "Non-nil means show all sibling heading when revealing a location.
  802. Org-mode often shows locations in an org-mode file which might have
  803. been invisible before. When this is set, the sibling of the current entry
  804. heading are all made visible. If `org-show-hierarchy-above' is t,
  805. the same happens on each level of the hierarchy above the current entry.
  806. By default this is on for the isearch context, off for all other contexts.
  807. Turning this off for example for sparse trees makes them very compact,
  808. but makes it harder to edit the location of the match. In such a case,
  809. use the command \\[org-reveal] to show more context.
  810. Instead of t, this can also be an alist specifying this option for different
  811. contexts. See `org-show-hierarchy-above' for valid contexts."
  812. :group 'org-reveal-location
  813. :type org-context-choice)
  814. (defcustom org-show-entry-below '((default . nil))
  815. "Non-nil means show the entry below a headline when revealing a location.
  816. Org-mode often shows locations in an org-mode file which might have
  817. been invisible before. When this is set, the text below the headline that is
  818. exposed is also shown.
  819. By default this is off for all contexts.
  820. Instead of t, this can also be an alist specifying this option for different
  821. contexts. See `org-show-hierarchy-above' for valid contexts."
  822. :group 'org-reveal-location
  823. :type org-context-choice)
  824. (defcustom org-indirect-buffer-display 'other-window
  825. "How should indirect tree buffers be displayed?
  826. This applies to indirect buffers created with the commands
  827. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  828. Valid values are:
  829. current-window Display in the current window
  830. other-window Just display in another window.
  831. dedicated-frame Create one new frame, and re-use it each time.
  832. new-frame Make a new frame each time. Note that in this case
  833. previously-made indirect buffers are kept, and you need to
  834. kill these buffers yourself."
  835. :group 'org-structure
  836. :group 'org-agenda-windows
  837. :type '(choice
  838. (const :tag "In current window" current-window)
  839. (const :tag "In current frame, other window" other-window)
  840. (const :tag "Each time a new frame" new-frame)
  841. (const :tag "One dedicated frame" dedicated-frame)))
  842. (defcustom org-use-speed-commands nil
  843. "Non-nil means activate single letter commands at beginning of a headline.
  844. This may also be a function to test for appropriate locations where speed
  845. commands should be active."
  846. :group 'org-structure
  847. :type '(choice
  848. (const :tag "Never" nil)
  849. (const :tag "At beginning of headline stars" t)
  850. (function)))
  851. (defcustom org-speed-commands-user nil
  852. "Alist of additional speed commands.
  853. This list will be checked before `org-speed-commands-default'
  854. when the variable `org-use-speed-commands' is non-nil
  855. and when the cursor is at the beginning of a headline.
  856. The car if each entry is a string with a single letter, which must
  857. be assigned to `self-insert-command' in the global map.
  858. The cdr is either a command to be called interactively, a function
  859. to be called, or a form to be evaluated.
  860. An entry that is just a list with a single string will be interpreted
  861. as a descriptive headline that will be added when listing the speed
  862. commands in the Help buffer using the `?' speed command."
  863. :group 'org-structure
  864. :type '(repeat :value ("k" . ignore)
  865. (choice :value ("k" . ignore)
  866. (list :tag "Descriptive Headline" (string :tag "Headline"))
  867. (cons :tag "Letter and Command"
  868. (string :tag "Command letter")
  869. (choice
  870. (function)
  871. (sexp))))))
  872. (defgroup org-cycle nil
  873. "Options concerning visibility cycling in Org-mode."
  874. :tag "Org Cycle"
  875. :group 'org-structure)
  876. (defcustom org-cycle-skip-children-state-if-no-children t
  877. "Non-nil means skip CHILDREN state in entries that don't have any."
  878. :group 'org-cycle
  879. :type 'boolean)
  880. (defcustom org-cycle-max-level nil
  881. "Maximum level which should still be subject to visibility cycling.
  882. Levels higher than this will, for cycling, be treated as text, not a headline.
  883. When `org-odd-levels-only' is set, a value of N in this variable actually
  884. means 2N-1 stars as the limiting headline.
  885. When nil, cycle all levels.
  886. Note that the limiting level of cycling is also influenced by
  887. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  888. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  889. than its value."
  890. :group 'org-cycle
  891. :type '(choice
  892. (const :tag "No limit" nil)
  893. (integer :tag "Maximum level")))
  894. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  895. "Names of drawers. Drawers are not opened by cycling on the headline above.
  896. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  897. this:
  898. :DRAWERNAME:
  899. .....
  900. :END:
  901. The drawer \"PROPERTIES\" is special for capturing properties through
  902. the property API.
  903. Drawers can be defined on the per-file basis with a line like:
  904. #+DRAWERS: HIDDEN STATE PROPERTIES"
  905. :group 'org-structure
  906. :group 'org-cycle
  907. :type '(repeat (string :tag "Drawer Name")))
  908. (defcustom org-hide-block-startup nil
  909. "Non-nil means entering Org-mode will fold all blocks.
  910. This can also be set in on a per-file basis with
  911. #+STARTUP: hideblocks
  912. #+STARTUP: showblocks"
  913. :group 'org-startup
  914. :group 'org-cycle
  915. :type 'boolean)
  916. (defcustom org-cycle-global-at-bob nil
  917. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  918. This makes it possible to do global cycling without having to use S-TAB or
  919. \\[universal-argument] TAB. For this special case to work, the first line
  920. of the buffer must not be a headline -- it may be empty or some other text.
  921. When used in this way, `org-cycle-hook' is disabled temporarily to make
  922. sure the cursor stays at the beginning of the buffer. When this option is
  923. nil, don't do anything special at the beginning of the buffer."
  924. :group 'org-cycle
  925. :type 'boolean)
  926. (defcustom org-cycle-level-after-item/entry-creation t
  927. "Non-nil means cycle entry level or item indentation in new empty entries.
  928. When the cursor is at the end of an empty headline, i.e with only stars
  929. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  930. and then all possible ancestor states, before returning to the original state.
  931. This makes data entry extremely fast: M-RET to create a new headline,
  932. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  933. When the cursor is at the end of an empty plain list item, one TAB will
  934. make it a subitem, two or more tabs will back up to make this an item
  935. higher up in the item hierarchy."
  936. :group 'org-cycle
  937. :type 'boolean)
  938. (defcustom org-cycle-emulate-tab t
  939. "Where should `org-cycle' emulate TAB.
  940. nil Never
  941. white Only in completely white lines
  942. whitestart Only at the beginning of lines, before the first non-white char
  943. t Everywhere except in headlines
  944. exc-hl-bol Everywhere except at the start of a headline
  945. If TAB is used in a place where it does not emulate TAB, the current subtree
  946. visibility is cycled."
  947. :group 'org-cycle
  948. :type '(choice (const :tag "Never" nil)
  949. (const :tag "Only in completely white lines" white)
  950. (const :tag "Before first char in a line" whitestart)
  951. (const :tag "Everywhere except in headlines" t)
  952. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  953. ))
  954. (defcustom org-cycle-separator-lines 2
  955. "Number of empty lines needed to keep an empty line between collapsed trees.
  956. If you leave an empty line between the end of a subtree and the following
  957. headline, this empty line is hidden when the subtree is folded.
  958. Org-mode will leave (exactly) one empty line visible if the number of
  959. empty lines is equal or larger to the number given in this variable.
  960. So the default 2 means at least 2 empty lines after the end of a subtree
  961. are needed to produce free space between a collapsed subtree and the
  962. following headline.
  963. If the number is negative, and the number of empty lines is at least -N,
  964. all empty lines are shown.
  965. Special case: when 0, never leave empty lines in collapsed view."
  966. :group 'org-cycle
  967. :type 'integer)
  968. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  969. (defcustom org-pre-cycle-hook nil
  970. "Hook that is run before visibility cycling is happening.
  971. The function(s) in this hook must accept a single argument which indicates
  972. the new state that will be set right after running this hook. The
  973. argument is a symbol. Before a global state change, it can have the values
  974. `overview', `content', or `all'. Before a local state change, it can have
  975. the values `folded', `children', or `subtree'."
  976. :group 'org-cycle
  977. :type 'hook)
  978. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  979. org-cycle-hide-drawers
  980. org-cycle-hide-inline-tasks
  981. org-cycle-show-empty-lines
  982. org-optimize-window-after-visibility-change)
  983. "Hook that is run after `org-cycle' has changed the buffer visibility.
  984. The function(s) in this hook must accept a single argument which indicates
  985. the new state that was set by the most recent `org-cycle' command. The
  986. argument is a symbol. After a global state change, it can have the values
  987. `overview', `contents', or `all'. After a local state change, it can have
  988. the values `folded', `children', or `subtree'."
  989. :group 'org-cycle
  990. :type 'hook)
  991. (defgroup org-edit-structure nil
  992. "Options concerning structure editing in Org-mode."
  993. :tag "Org Edit Structure"
  994. :group 'org-structure)
  995. (defcustom org-odd-levels-only nil
  996. "Non-nil means skip even levels and only use odd levels for the outline.
  997. This has the effect that two stars are being added/taken away in
  998. promotion/demotion commands. It also influences how levels are
  999. handled by the exporters.
  1000. Changing it requires restart of `font-lock-mode' to become effective
  1001. for fontification also in regions already fontified.
  1002. You may also set this on a per-file basis by adding one of the following
  1003. lines to the buffer:
  1004. #+STARTUP: odd
  1005. #+STARTUP: oddeven"
  1006. :group 'org-edit-structure
  1007. :group 'org-appearance
  1008. :type 'boolean)
  1009. (defcustom org-adapt-indentation t
  1010. "Non-nil means adapt indentation to outline node level.
  1011. When this variable is set, Org assumes that you write outlines by
  1012. indenting text in each node to align with the headline (after the stars).
  1013. The following issues are influenced by this variable:
  1014. - When this is set and the *entire* text in an entry is indented, the
  1015. indentation is increased by one space in a demotion command, and
  1016. decreased by one in a promotion command. If any line in the entry
  1017. body starts with text at column 0, indentation is not changed at all.
  1018. - Property drawers and planning information is inserted indented when
  1019. this variable s set. When nil, they will not be indented.
  1020. - TAB indents a line relative to context. The lines below a headline
  1021. will be indented when this variable is set.
  1022. Note that this is all about true indentation, by adding and removing
  1023. space characters. See also `org-indent.el' which does level-dependent
  1024. indentation in a virtual way, i.e. at display time in Emacs."
  1025. :group 'org-edit-structure
  1026. :type 'boolean)
  1027. (defcustom org-special-ctrl-a/e nil
  1028. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1029. When t, `C-a' will bring back the cursor to the beginning of the
  1030. headline text, i.e. after the stars and after a possible TODO
  1031. keyword. In an item, this will be the position after bullet and
  1032. check-box, if any. When the cursor is already at that position,
  1033. another `C-a' will bring it to the beginning of the line.
  1034. `C-e' will jump to the end of the headline, ignoring the presence
  1035. of tags in the headline. A second `C-e' will then jump to the
  1036. true end of the line, after any tags. This also means that, when
  1037. this variable is non-nil, `C-e' also will never jump beyond the
  1038. end of the heading of a folded section, i.e. not after the
  1039. ellipses.
  1040. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1041. normally, going to the true line boundary first. Only a directly
  1042. following, identical keypress will bring the cursor to the
  1043. special positions.
  1044. This may also be a cons cell where the behavior for `C-a' and
  1045. `C-e' is set separately."
  1046. :group 'org-edit-structure
  1047. :type '(choice
  1048. (const :tag "off" nil)
  1049. (const :tag "on: after stars/bullet and before tags first" t)
  1050. (const :tag "reversed: true line boundary first" reversed)
  1051. (cons :tag "Set C-a and C-e separately"
  1052. (choice :tag "Special C-a"
  1053. (const :tag "off" nil)
  1054. (const :tag "on: after stars/bullet first" t)
  1055. (const :tag "reversed: before stars/bullet first" reversed))
  1056. (choice :tag "Special C-e"
  1057. (const :tag "off" nil)
  1058. (const :tag "on: before tags first" t)
  1059. (const :tag "reversed: after tags first" reversed)))))
  1060. (if (fboundp 'defvaralias)
  1061. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  1062. (defcustom org-special-ctrl-k nil
  1063. "Non-nil means `C-k' will behave specially in headlines.
  1064. When nil, `C-k' will call the default `kill-line' command.
  1065. When t, the following will happen while the cursor is in the headline:
  1066. - When the cursor is at the beginning of a headline, kill the entire
  1067. line and possible the folded subtree below the line.
  1068. - When in the middle of the headline text, kill the headline up to the tags.
  1069. - When after the headline text, kill the tags."
  1070. :group 'org-edit-structure
  1071. :type 'boolean)
  1072. (defcustom org-ctrl-k-protect-subtree nil
  1073. "Non-nil means, do not delete a hidden subtree with C-k.
  1074. When set to the symbol `error', simply throw an error when C-k is
  1075. used to kill (part-of) a headline that has hidden text behind it.
  1076. Any other non-nil value will result in a query to the user, if it is
  1077. OK to kill that hidden subtree. When nil, kill without remorse."
  1078. :group 'org-edit-structure
  1079. :version "24.1"
  1080. :type '(choice
  1081. (const :tag "Do not protect hidden subtrees" nil)
  1082. (const :tag "Protect hidden subtrees with a security query" t)
  1083. (const :tag "Never kill a hidden subtree with C-k" error)))
  1084. (defcustom org-catch-invisible-edits nil
  1085. "Check if in invisible region before inserting or deleting a character.
  1086. Valid values are:
  1087. nil Do not check, so just do invisible edits.
  1088. error Throw an error and do nothing.
  1089. show Make point visible, and do the requested edit.
  1090. show-and-error Make point visible, then throw an error and abort the edit.
  1091. smart Make point visible, and do insertion/deletion if it is
  1092. adjacent to visible text and the change feels predictable.
  1093. Never delete a previously invisible character or add in the
  1094. middle or right after an invisible region. Basically, this
  1095. allows insertion and backward-delete right before ellipses.
  1096. FIXME: maybe in this case we should not even show?"
  1097. :group 'org-edit-structure
  1098. :version "24.1"
  1099. :type '(choice
  1100. (const :tag "Do not check" nil)
  1101. (const :tag "Throw error when trying to edit" error)
  1102. (const :tag "Unhide, but do not do the edit" show-and-error)
  1103. (const :tag "Show invisible part and do the edit" show)
  1104. (const :tag "Be smart and do the right thing" smart)))
  1105. (defcustom org-yank-folded-subtrees t
  1106. "Non-nil means when yanking subtrees, fold them.
  1107. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1108. it starts with a heading and all other headings in it are either children
  1109. or siblings, then fold all the subtrees. However, do this only if no
  1110. text after the yank would be swallowed into a folded tree by this action."
  1111. :group 'org-edit-structure
  1112. :type 'boolean)
  1113. (defcustom org-yank-adjusted-subtrees nil
  1114. "Non-nil means when yanking subtrees, adjust the level.
  1115. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1116. this function for details."
  1117. :group 'org-edit-structure
  1118. :type 'boolean)
  1119. (defcustom org-M-RET-may-split-line '((default . t))
  1120. "Non-nil means M-RET will split the line at the cursor position.
  1121. When nil, it will go to the end of the line before making a
  1122. new line.
  1123. You may also set this option in a different way for different
  1124. contexts. Valid contexts are:
  1125. headline when creating a new headline
  1126. item when creating a new item
  1127. table in a table field
  1128. default the value to be used for all contexts not explicitly
  1129. customized"
  1130. :group 'org-structure
  1131. :group 'org-table
  1132. :type '(choice
  1133. (const :tag "Always" t)
  1134. (const :tag "Never" nil)
  1135. (repeat :greedy t :tag "Individual contexts"
  1136. (cons
  1137. (choice :tag "Context"
  1138. (const headline)
  1139. (const item)
  1140. (const table)
  1141. (const default))
  1142. (boolean)))))
  1143. (defcustom org-insert-heading-respect-content nil
  1144. "Non-nil means insert new headings after the current subtree.
  1145. When nil, the new heading is created directly after the current line.
  1146. The commands \\[org-insert-heading-respect-content] and
  1147. \\[org-insert-todo-heading-respect-content] turn this variable on
  1148. for the duration of the command."
  1149. :group 'org-structure
  1150. :type 'boolean)
  1151. (defcustom org-blank-before-new-entry '((heading . auto)
  1152. (plain-list-item . auto))
  1153. "Should `org-insert-heading' leave a blank line before new heading/item?
  1154. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1155. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1156. which case Org will look at the surrounding headings/items and try to
  1157. make an intelligent decision whether to insert a blank line or not.
  1158. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1159. set, the setting here is ignored and no empty line is inserted, to avoid
  1160. breaking the list structure."
  1161. :group 'org-edit-structure
  1162. :type '(list
  1163. (cons (const heading)
  1164. (choice (const :tag "Never" nil)
  1165. (const :tag "Always" t)
  1166. (const :tag "Auto" auto)))
  1167. (cons (const plain-list-item)
  1168. (choice (const :tag "Never" nil)
  1169. (const :tag "Always" t)
  1170. (const :tag "Auto" auto)))))
  1171. (defcustom org-insert-heading-hook nil
  1172. "Hook being run after inserting a new heading."
  1173. :group 'org-edit-structure
  1174. :type 'hook)
  1175. (defcustom org-enable-fixed-width-editor t
  1176. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1177. This currently only means they are never auto-wrapped.
  1178. When nil, such lines will be treated like ordinary lines.
  1179. See also the QUOTE keyword."
  1180. :group 'org-edit-structure
  1181. :type 'boolean)
  1182. (defcustom org-goto-auto-isearch t
  1183. "Non-nil means typing characters in `org-goto' starts incremental search.
  1184. When nil, you can use these keybindings to navigate the buffer:
  1185. q Quit the org-goto interface
  1186. n Go to the next visible heading
  1187. p Go to the previous visible heading
  1188. f Go one heading forward on same level
  1189. b Go one heading backward on same level
  1190. u Go one heading up"
  1191. :group 'org-edit-structure
  1192. :type 'boolean)
  1193. (defgroup org-sparse-trees nil
  1194. "Options concerning sparse trees in Org-mode."
  1195. :tag "Org Sparse Trees"
  1196. :group 'org-structure)
  1197. (defcustom org-highlight-sparse-tree-matches t
  1198. "Non-nil means highlight all matches that define a sparse tree.
  1199. The highlights will automatically disappear the next time the buffer is
  1200. changed by an edit command."
  1201. :group 'org-sparse-trees
  1202. :type 'boolean)
  1203. (defcustom org-remove-highlights-with-change t
  1204. "Non-nil means any change to the buffer will remove temporary highlights.
  1205. Such highlights are created by `org-occur' and `org-clock-display'.
  1206. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1207. The highlights created by `org-preview-latex-fragment' always need
  1208. `C-c C-c' to be removed."
  1209. :group 'org-sparse-trees
  1210. :group 'org-time
  1211. :type 'boolean)
  1212. (defcustom org-occur-hook '(org-first-headline-recenter)
  1213. "Hook that is run after `org-occur' has constructed a sparse tree.
  1214. This can be used to recenter the window to show as much of the structure
  1215. as possible."
  1216. :group 'org-sparse-trees
  1217. :type 'hook)
  1218. (defgroup org-imenu-and-speedbar nil
  1219. "Options concerning imenu and speedbar in Org-mode."
  1220. :tag "Org Imenu and Speedbar"
  1221. :group 'org-structure)
  1222. (defcustom org-imenu-depth 2
  1223. "The maximum level for Imenu access to Org-mode headlines.
  1224. This also applied for speedbar access."
  1225. :group 'org-imenu-and-speedbar
  1226. :type 'integer)
  1227. (defgroup org-table nil
  1228. "Options concerning tables in Org-mode."
  1229. :tag "Org Table"
  1230. :group 'org)
  1231. (defcustom org-enable-table-editor 'optimized
  1232. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1233. When nil, such lines will be treated like ordinary lines.
  1234. When equal to the symbol `optimized', the table editor will be optimized to
  1235. do the following:
  1236. - Automatic overwrite mode in front of whitespace in table fields.
  1237. This makes the structure of the table stay in tact as long as the edited
  1238. field does not exceed the column width.
  1239. - Minimize the number of realigns. Normally, the table is aligned each time
  1240. TAB or RET are pressed to move to another field. With optimization this
  1241. happens only if changes to a field might have changed the column width.
  1242. Optimization requires replacing the functions `self-insert-command',
  1243. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1244. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1245. very good at guessing when a re-align will be necessary, but you can always
  1246. force one with \\[org-ctrl-c-ctrl-c].
  1247. If you would like to use the optimized version in Org-mode, but the
  1248. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1249. This variable can be used to turn on and off the table editor during a session,
  1250. but in order to toggle optimization, a restart is required.
  1251. See also the variable `org-table-auto-blank-field'."
  1252. :group 'org-table
  1253. :type '(choice
  1254. (const :tag "off" nil)
  1255. (const :tag "on" t)
  1256. (const :tag "on, optimized" optimized)))
  1257. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1258. (version<= emacs-version "24.1"))
  1259. "Non-nil means cluster self-insert commands for undo when possible.
  1260. If this is set, then, like in the Emacs command loop, 20 consecutive
  1261. characters will be undone together.
  1262. This is configurable, because there is some impact on typing performance."
  1263. :group 'org-table
  1264. :type 'boolean)
  1265. (defcustom org-table-tab-recognizes-table.el t
  1266. "Non-nil means TAB will automatically notice a table.el table.
  1267. When it sees such a table, it moves point into it and - if necessary -
  1268. calls `table-recognize-table'."
  1269. :group 'org-table-editing
  1270. :type 'boolean)
  1271. (defgroup org-link nil
  1272. "Options concerning links in Org-mode."
  1273. :tag "Org Link"
  1274. :group 'org)
  1275. (defvar org-link-abbrev-alist-local nil
  1276. "Buffer-local version of `org-link-abbrev-alist', which see.
  1277. The value of this is taken from the #+LINK lines.")
  1278. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1279. (defcustom org-link-abbrev-alist nil
  1280. "Alist of link abbreviations.
  1281. The car of each element is a string, to be replaced at the start of a link.
  1282. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1283. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1284. [[linkkey:tag][description]]
  1285. The 'linkkey' must be a word word, starting with a letter, followed
  1286. by letters, numbers, '-' or '_'.
  1287. If REPLACE is a string, the tag will simply be appended to create the link.
  1288. If the string contains \"%s\", the tag will be inserted there. If the string
  1289. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1290. at that point (see the function `url-hexify-string'). If the string contains
  1291. the specifier \"%(my-function)\", then the custom function `my-function' will
  1292. be invoked: this function takes the tag as its only argument and must return
  1293. a string.
  1294. REPLACE may also be a function that will be called with the tag as the
  1295. only argument to create the link, which should be returned as a string.
  1296. See the manual for examples."
  1297. :group 'org-link
  1298. :type '(repeat
  1299. (cons
  1300. (string :tag "Protocol")
  1301. (choice
  1302. (string :tag "Format")
  1303. (function)))))
  1304. (defcustom org-descriptive-links t
  1305. "Non-nil means Org will display descriptive links.
  1306. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1307. \"Org Website\", hiding the link itself and just displaying its
  1308. description. When set to `nil', Org will display the full links
  1309. literally.
  1310. You can interactively set the value of this variable by calling
  1311. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1312. :group 'org-link
  1313. :type 'boolean)
  1314. (defcustom org-link-file-path-type 'adaptive
  1315. "How the path name in file links should be stored.
  1316. Valid values are:
  1317. relative Relative to the current directory, i.e. the directory of the file
  1318. into which the link is being inserted.
  1319. absolute Absolute path, if possible with ~ for home directory.
  1320. noabbrev Absolute path, no abbreviation of home directory.
  1321. adaptive Use relative path for files in the current directory and sub-
  1322. directories of it. For other files, use an absolute path."
  1323. :group 'org-link
  1324. :type '(choice
  1325. (const relative)
  1326. (const absolute)
  1327. (const noabbrev)
  1328. (const adaptive)))
  1329. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1330. "Types of links that should be activated in Org-mode files.
  1331. This is a list of symbols, each leading to the activation of a certain link
  1332. type. In principle, it does not hurt to turn on most link types - there may
  1333. be a small gain when turning off unused link types. The types are:
  1334. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1335. angle Links in angular brackets that may contain whitespace like
  1336. <bbdb:Carsten Dominik>.
  1337. plain Plain links in normal text, no whitespace, like http://google.com.
  1338. radio Text that is matched by a radio target, see manual for details.
  1339. tag Tag settings in a headline (link to tag search).
  1340. date Time stamps (link to calendar).
  1341. footnote Footnote labels.
  1342. Changing this variable requires a restart of Emacs to become effective."
  1343. :group 'org-link
  1344. :type '(set :greedy t
  1345. (const :tag "Double bracket links" bracket)
  1346. (const :tag "Angular bracket links" angle)
  1347. (const :tag "Plain text links" plain)
  1348. (const :tag "Radio target matches" radio)
  1349. (const :tag "Tags" tag)
  1350. (const :tag "Timestamps" date)
  1351. (const :tag "Footnotes" footnote)))
  1352. (defcustom org-make-link-description-function nil
  1353. "Function to use for generating link descriptions from links.
  1354. When nil, the link location will be used. This function must take
  1355. two parameters: the first one is the link, the second one is the
  1356. description generated by `org-insert-link'. The function should
  1357. return the description to use."
  1358. :group 'org-link
  1359. :type 'function)
  1360. (defgroup org-link-store nil
  1361. "Options concerning storing links in Org-mode."
  1362. :tag "Org Store Link"
  1363. :group 'org-link)
  1364. (defcustom org-url-hexify-p t
  1365. "When non-nil, hexify URL when creating a link."
  1366. :type 'boolean
  1367. :version "24.3"
  1368. :group 'org-link-store)
  1369. (defcustom org-email-link-description-format "Email %c: %.30s"
  1370. "Format of the description part of a link to an email or usenet message.
  1371. The following %-escapes will be replaced by corresponding information:
  1372. %F full \"From\" field
  1373. %f name, taken from \"From\" field, address if no name
  1374. %T full \"To\" field
  1375. %t first name in \"To\" field, address if no name
  1376. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1377. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1378. %s subject
  1379. %d date
  1380. %m message-id.
  1381. You may use normal field width specification between the % and the letter.
  1382. This is for example useful to limit the length of the subject.
  1383. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1384. :group 'org-link-store
  1385. :type 'string)
  1386. (defcustom org-from-is-user-regexp
  1387. (let (r1 r2)
  1388. (when (and user-mail-address (not (string= user-mail-address "")))
  1389. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1390. (when (and user-full-name (not (string= user-full-name "")))
  1391. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1392. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1393. "Regexp matched against the \"From:\" header of an email or usenet message.
  1394. It should match if the message is from the user him/herself."
  1395. :group 'org-link-store
  1396. :type 'regexp)
  1397. (defcustom org-context-in-file-links t
  1398. "Non-nil means file links from `org-store-link' contain context.
  1399. A search string will be added to the file name with :: as separator and
  1400. used to find the context when the link is activated by the command
  1401. `org-open-at-point'. When this option is t, the entire active region
  1402. will be placed in the search string of the file link. If set to a
  1403. positive integer, only the first n lines of context will be stored.
  1404. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1405. negates this setting for the duration of the command."
  1406. :group 'org-link-store
  1407. :type '(choice boolean integer))
  1408. (defcustom org-keep-stored-link-after-insertion nil
  1409. "Non-nil means keep link in list for entire session.
  1410. The command `org-store-link' adds a link pointing to the current
  1411. location to an internal list. These links accumulate during a session.
  1412. The command `org-insert-link' can be used to insert links into any
  1413. Org-mode file (offering completion for all stored links). When this
  1414. option is nil, every link which has been inserted once using \\[org-insert-link]
  1415. will be removed from the list, to make completing the unused links
  1416. more efficient."
  1417. :group 'org-link-store
  1418. :type 'boolean)
  1419. (defgroup org-link-follow nil
  1420. "Options concerning following links in Org-mode."
  1421. :tag "Org Follow Link"
  1422. :group 'org-link)
  1423. (defcustom org-link-translation-function nil
  1424. "Function to translate links with different syntax to Org syntax.
  1425. This can be used to translate links created for example by the Planner
  1426. or emacs-wiki packages to Org syntax.
  1427. The function must accept two parameters, a TYPE containing the link
  1428. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1429. which is everything after the link protocol. It should return a cons
  1430. with possibly modified values of type and path.
  1431. Org contains a function for this, so if you set this variable to
  1432. `org-translate-link-from-planner', you should be able follow many
  1433. links created by planner."
  1434. :group 'org-link-follow
  1435. :type 'function)
  1436. (defcustom org-follow-link-hook nil
  1437. "Hook that is run after a link has been followed."
  1438. :group 'org-link-follow
  1439. :type 'hook)
  1440. (defcustom org-tab-follows-link nil
  1441. "Non-nil means on links TAB will follow the link.
  1442. Needs to be set before org.el is loaded.
  1443. This really should not be used, it does not make sense, and the
  1444. implementation is bad."
  1445. :group 'org-link-follow
  1446. :type 'boolean)
  1447. (defcustom org-return-follows-link nil
  1448. "Non-nil means on links RET will follow the link."
  1449. :group 'org-link-follow
  1450. :type 'boolean)
  1451. (defcustom org-mouse-1-follows-link
  1452. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1453. "Non-nil means mouse-1 on a link will follow the link.
  1454. A longer mouse click will still set point. Does not work on XEmacs.
  1455. Needs to be set before org.el is loaded."
  1456. :group 'org-link-follow
  1457. :type 'boolean)
  1458. (defcustom org-mark-ring-length 4
  1459. "Number of different positions to be recorded in the ring.
  1460. Changing this requires a restart of Emacs to work correctly."
  1461. :group 'org-link-follow
  1462. :type 'integer)
  1463. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1464. "Non-nil means internal links in Org files must exactly match a headline.
  1465. When nil, the link search tries to match a phrase with all words
  1466. in the search text."
  1467. :group 'org-link-follow
  1468. :version "24.1"
  1469. :type '(choice
  1470. (const :tag "Use fuzzy text search" nil)
  1471. (const :tag "Match only exact headline" t)
  1472. (const :tag "Match exact headline or query to create it"
  1473. query-to-create)))
  1474. (defcustom org-link-frame-setup
  1475. '((vm . vm-visit-folder-other-frame)
  1476. (vm-imap . vm-visit-imap-folder-other-frame)
  1477. (gnus . org-gnus-no-new-news)
  1478. (file . find-file-other-window)
  1479. (wl . wl-other-frame))
  1480. "Setup the frame configuration for following links.
  1481. When following a link with Emacs, it may often be useful to display
  1482. this link in another window or frame. This variable can be used to
  1483. set this up for the different types of links.
  1484. For VM, use any of
  1485. `vm-visit-folder'
  1486. `vm-visit-folder-other-window'
  1487. `vm-visit-folder-other-frame'
  1488. For Gnus, use any of
  1489. `gnus'
  1490. `gnus-other-frame'
  1491. `org-gnus-no-new-news'
  1492. For FILE, use any of
  1493. `find-file'
  1494. `find-file-other-window'
  1495. `find-file-other-frame'
  1496. For Wanderlust use any of
  1497. `wl'
  1498. `wl-other-frame'
  1499. For the calendar, use the variable `calendar-setup'.
  1500. For BBDB, it is currently only possible to display the matches in
  1501. another window."
  1502. :group 'org-link-follow
  1503. :type '(list
  1504. (cons (const vm)
  1505. (choice
  1506. (const vm-visit-folder)
  1507. (const vm-visit-folder-other-window)
  1508. (const vm-visit-folder-other-frame)))
  1509. (cons (const gnus)
  1510. (choice
  1511. (const gnus)
  1512. (const gnus-other-frame)
  1513. (const org-gnus-no-new-news)))
  1514. (cons (const file)
  1515. (choice
  1516. (const find-file)
  1517. (const find-file-other-window)
  1518. (const find-file-other-frame)))
  1519. (cons (const wl)
  1520. (choice
  1521. (const wl)
  1522. (const wl-other-frame)))))
  1523. (defcustom org-display-internal-link-with-indirect-buffer nil
  1524. "Non-nil means use indirect buffer to display infile links.
  1525. Activating internal links (from one location in a file to another location
  1526. in the same file) normally just jumps to the location. When the link is
  1527. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1528. is displayed in
  1529. another window. When this option is set, the other window actually displays
  1530. an indirect buffer clone of the current buffer, to avoid any visibility
  1531. changes to the current buffer."
  1532. :group 'org-link-follow
  1533. :type 'boolean)
  1534. (defcustom org-open-non-existing-files nil
  1535. "Non-nil means `org-open-file' will open non-existing files.
  1536. When nil, an error will be generated.
  1537. This variable applies only to external applications because they
  1538. might choke on non-existing files. If the link is to a file that
  1539. will be opened in Emacs, the variable is ignored."
  1540. :group 'org-link-follow
  1541. :type 'boolean)
  1542. (defcustom org-open-directory-means-index-dot-org nil
  1543. "Non-nil means a link to a directory really means to index.org.
  1544. When nil, following a directory link will run dired or open a finder/explorer
  1545. window on that directory."
  1546. :group 'org-link-follow
  1547. :type 'boolean)
  1548. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1549. "Function and arguments to call for following mailto links.
  1550. This is a list with the first element being a Lisp function, and the
  1551. remaining elements being arguments to the function. In string arguments,
  1552. %a will be replaced by the address, and %s will be replaced by the subject
  1553. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1554. :group 'org-link-follow
  1555. :type '(choice
  1556. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1557. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1558. (const :tag "message-mail" (message-mail "%a" "%s"))
  1559. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1560. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1561. "Non-nil means ask for confirmation before executing shell links.
  1562. Shell links can be dangerous: just think about a link
  1563. [[shell:rm -rf ~/*][Google Search]]
  1564. This link would show up in your Org-mode document as \"Google Search\",
  1565. but really it would remove your entire home directory.
  1566. Therefore we advise against setting this variable to nil.
  1567. Just change it to `y-or-n-p' if you want to confirm with a
  1568. single keystroke rather than having to type \"yes\"."
  1569. :group 'org-link-follow
  1570. :type '(choice
  1571. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1572. (const :tag "with y-or-n (faster)" y-or-n-p)
  1573. (const :tag "no confirmation (dangerous)" nil)))
  1574. (put 'org-confirm-shell-link-function
  1575. 'safe-local-variable
  1576. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1577. (defcustom org-confirm-shell-link-not-regexp ""
  1578. "A regexp to skip confirmation for shell links."
  1579. :group 'org-link-follow
  1580. :version "24.1"
  1581. :type 'regexp)
  1582. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1583. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1584. Elisp links can be dangerous: just think about a link
  1585. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1586. This link would show up in your Org-mode document as \"Google Search\",
  1587. but really it would remove your entire home directory.
  1588. Therefore we advise against setting this variable to nil.
  1589. Just change it to `y-or-n-p' if you want to confirm with a
  1590. single keystroke rather than having to type \"yes\"."
  1591. :group 'org-link-follow
  1592. :type '(choice
  1593. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1594. (const :tag "with y-or-n (faster)" y-or-n-p)
  1595. (const :tag "no confirmation (dangerous)" nil)))
  1596. (put 'org-confirm-shell-link-function
  1597. 'safe-local-variable
  1598. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1599. (defcustom org-confirm-elisp-link-not-regexp ""
  1600. "A regexp to skip confirmation for Elisp links."
  1601. :group 'org-link-follow
  1602. :version "24.1"
  1603. :type 'regexp)
  1604. (defconst org-file-apps-defaults-gnu
  1605. '((remote . emacs)
  1606. (system . mailcap)
  1607. (t . mailcap))
  1608. "Default file applications on a UNIX or GNU/Linux system.
  1609. See `org-file-apps'.")
  1610. (defconst org-file-apps-defaults-macosx
  1611. '((remote . emacs)
  1612. (t . "open %s")
  1613. (system . "open %s")
  1614. ("ps.gz" . "gv %s")
  1615. ("eps.gz" . "gv %s")
  1616. ("dvi" . "xdvi %s")
  1617. ("fig" . "xfig %s"))
  1618. "Default file applications on a MacOS X system.
  1619. The system \"open\" is known as a default, but we use X11 applications
  1620. for some files for which the OS does not have a good default.
  1621. See `org-file-apps'.")
  1622. (defconst org-file-apps-defaults-windowsnt
  1623. (list
  1624. '(remote . emacs)
  1625. (cons t
  1626. (list (if (featurep 'xemacs)
  1627. 'mswindows-shell-execute
  1628. 'w32-shell-execute)
  1629. "open" 'file))
  1630. (cons 'system
  1631. (list (if (featurep 'xemacs)
  1632. 'mswindows-shell-execute
  1633. 'w32-shell-execute)
  1634. "open" 'file)))
  1635. "Default file applications on a Windows NT system.
  1636. The system \"open\" is used for most files.
  1637. See `org-file-apps'.")
  1638. (defcustom org-file-apps
  1639. '(
  1640. (auto-mode . emacs)
  1641. ("\\.mm\\'" . default)
  1642. ("\\.x?html?\\'" . default)
  1643. ("\\.pdf\\'" . default)
  1644. )
  1645. "External applications for opening `file:path' items in a document.
  1646. Org-mode uses system defaults for different file types, but
  1647. you can use this variable to set the application for a given file
  1648. extension. The entries in this list are cons cells where the car identifies
  1649. files and the cdr the corresponding command. Possible values for the
  1650. file identifier are
  1651. \"string\" A string as a file identifier can be interpreted in different
  1652. ways, depending on its contents:
  1653. - Alphanumeric characters only:
  1654. Match links with this file extension.
  1655. Example: (\"pdf\" . \"evince %s\")
  1656. to open PDFs with evince.
  1657. - Regular expression: Match links where the
  1658. filename matches the regexp. If you want to
  1659. use groups here, use shy groups.
  1660. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1661. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1662. to open *.html and *.xhtml with firefox.
  1663. - Regular expression which contains (non-shy) groups:
  1664. Match links where the whole link, including \"::\", and
  1665. anything after that, matches the regexp.
  1666. In a custom command string, %1, %2, etc. are replaced with
  1667. the parts of the link that were matched by the groups.
  1668. For backwards compatibility, if a command string is given
  1669. that does not use any of the group matches, this case is
  1670. handled identically to the second one (i.e. match against
  1671. file name only).
  1672. In a custom lisp form, you can access the group matches with
  1673. (match-string n link).
  1674. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1675. to open [[file:document.pdf::5]] with evince at page 5.
  1676. `directory' Matches a directory
  1677. `remote' Matches a remote file, accessible through tramp or efs.
  1678. Remote files most likely should be visited through Emacs
  1679. because external applications cannot handle such paths.
  1680. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1681. so all files Emacs knows how to handle. Using this with
  1682. command `emacs' will open most files in Emacs. Beware that this
  1683. will also open html files inside Emacs, unless you add
  1684. (\"html\" . default) to the list as well.
  1685. t Default for files not matched by any of the other options.
  1686. `system' The system command to open files, like `open' on Windows
  1687. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1688. that will be selected if you call `C-c C-o' with a double
  1689. \\[universal-argument] \\[universal-argument] prefix.
  1690. Possible values for the command are:
  1691. `emacs' The file will be visited by the current Emacs process.
  1692. `default' Use the default application for this file type, which is the
  1693. association for t in the list, most likely in the system-specific
  1694. part.
  1695. This can be used to overrule an unwanted setting in the
  1696. system-specific variable.
  1697. `system' Use the system command for opening files, like \"open\".
  1698. This command is specified by the entry whose car is `system'.
  1699. Most likely, the system-specific version of this variable
  1700. does define this command, but you can overrule/replace it
  1701. here.
  1702. string A command to be executed by a shell; %s will be replaced
  1703. by the path to the file.
  1704. sexp A Lisp form which will be evaluated. The file path will
  1705. be available in the Lisp variable `file'.
  1706. For more examples, see the system specific constants
  1707. `org-file-apps-defaults-macosx'
  1708. `org-file-apps-defaults-windowsnt'
  1709. `org-file-apps-defaults-gnu'."
  1710. :group 'org-link-follow
  1711. :type '(repeat
  1712. (cons (choice :value ""
  1713. (string :tag "Extension")
  1714. (const :tag "System command to open files" system)
  1715. (const :tag "Default for unrecognized files" t)
  1716. (const :tag "Remote file" remote)
  1717. (const :tag "Links to a directory" directory)
  1718. (const :tag "Any files that have Emacs modes"
  1719. auto-mode))
  1720. (choice :value ""
  1721. (const :tag "Visit with Emacs" emacs)
  1722. (const :tag "Use default" default)
  1723. (const :tag "Use the system command" system)
  1724. (string :tag "Command")
  1725. (sexp :tag "Lisp form")))))
  1726. (defcustom org-doi-server-url "http://dx.doi.org/"
  1727. "The URL of the DOI server."
  1728. :type 'string
  1729. :version "24.3"
  1730. :group 'org-link-follow)
  1731. (defgroup org-refile nil
  1732. "Options concerning refiling entries in Org-mode."
  1733. :tag "Org Refile"
  1734. :group 'org)
  1735. (defcustom org-directory "~/org"
  1736. "Directory with org files.
  1737. This is just a default location to look for Org files. There is no need
  1738. at all to put your files into this directory. It is only used in the
  1739. following situations:
  1740. 1. When a capture template specifies a target file that is not an
  1741. absolute path. The path will then be interpreted relative to
  1742. `org-directory'
  1743. 2. When a capture note is filed away in an interactive way (when exiting the
  1744. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1745. with `org-directory' as the default path."
  1746. :group 'org-refile
  1747. :group 'org-remember
  1748. :group 'org-capture
  1749. :type 'directory)
  1750. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1751. "Default target for storing notes.
  1752. Used as a fall back file for org-remember.el and org-capture.el, for
  1753. templates that do not specify a target file."
  1754. :group 'org-refile
  1755. :group 'org-remember
  1756. :group 'org-capture
  1757. :type '(choice
  1758. (const :tag "Default from remember-data-file" nil)
  1759. file))
  1760. (defcustom org-goto-interface 'outline
  1761. "The default interface to be used for `org-goto'.
  1762. Allowed values are:
  1763. outline The interface shows an outline of the relevant file
  1764. and the correct heading is found by moving through
  1765. the outline or by searching with incremental search.
  1766. outline-path-completion Headlines in the current buffer are offered via
  1767. completion. This is the interface also used by
  1768. the refile command."
  1769. :group 'org-refile
  1770. :type '(choice
  1771. (const :tag "Outline" outline)
  1772. (const :tag "Outline-path-completion" outline-path-completion)))
  1773. (defcustom org-goto-max-level 5
  1774. "Maximum target level when running `org-goto' with refile interface."
  1775. :group 'org-refile
  1776. :type 'integer)
  1777. (defcustom org-reverse-note-order nil
  1778. "Non-nil means store new notes at the beginning of a file or entry.
  1779. When nil, new notes will be filed to the end of a file or entry.
  1780. This can also be a list with cons cells of regular expressions that
  1781. are matched against file names, and values."
  1782. :group 'org-remember
  1783. :group 'org-capture
  1784. :group 'org-refile
  1785. :type '(choice
  1786. (const :tag "Reverse always" t)
  1787. (const :tag "Reverse never" nil)
  1788. (repeat :tag "By file name regexp"
  1789. (cons regexp boolean))))
  1790. (defcustom org-log-refile nil
  1791. "Information to record when a task is refiled.
  1792. Possible values are:
  1793. nil Don't add anything
  1794. time Add a time stamp to the task
  1795. note Prompt for a note and add it with template `org-log-note-headings'
  1796. This option can also be set with on a per-file-basis with
  1797. #+STARTUP: nologrefile
  1798. #+STARTUP: logrefile
  1799. #+STARTUP: lognoterefile
  1800. You can have local logging settings for a subtree by setting the LOGGING
  1801. property to one or more of these keywords.
  1802. When bulk-refiling from the agenda, the value `note' is forbidden and
  1803. will temporarily be changed to `time'."
  1804. :group 'org-refile
  1805. :group 'org-progress
  1806. :version "24.1"
  1807. :type '(choice
  1808. (const :tag "No logging" nil)
  1809. (const :tag "Record timestamp" time)
  1810. (const :tag "Record timestamp with note." note)))
  1811. (defcustom org-refile-targets nil
  1812. "Targets for refiling entries with \\[org-refile].
  1813. This is a list of cons cells. Each cell contains:
  1814. - a specification of the files to be considered, either a list of files,
  1815. or a symbol whose function or variable value will be used to retrieve
  1816. a file name or a list of file names. If you use `org-agenda-files' for
  1817. that, all agenda files will be scanned for targets. Nil means consider
  1818. headings in the current buffer.
  1819. - A specification of how to find candidate refile targets. This may be
  1820. any of:
  1821. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1822. This tag has to be present in all target headlines, inheritance will
  1823. not be considered.
  1824. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1825. todo keyword.
  1826. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1827. headlines that are refiling targets.
  1828. - a cons cell (:level . N). Any headline of level N is considered a target.
  1829. Note that, when `org-odd-levels-only' is set, level corresponds to
  1830. order in hierarchy, not to the number of stars.
  1831. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1832. Note that, when `org-odd-levels-only' is set, level corresponds to
  1833. order in hierarchy, not to the number of stars.
  1834. Each element of this list generates a set of possible targets.
  1835. The union of these sets is presented (with completion) to
  1836. the user by `org-refile'.
  1837. You can set the variable `org-refile-target-verify-function' to a function
  1838. to verify each headline found by the simple criteria above.
  1839. When this variable is nil, all top-level headlines in the current buffer
  1840. are used, equivalent to the value `((nil . (:level . 1))'."
  1841. :group 'org-refile
  1842. :type '(repeat
  1843. (cons
  1844. (choice :value org-agenda-files
  1845. (const :tag "All agenda files" org-agenda-files)
  1846. (const :tag "Current buffer" nil)
  1847. (function) (variable) (file))
  1848. (choice :tag "Identify target headline by"
  1849. (cons :tag "Specific tag" (const :value :tag) (string))
  1850. (cons :tag "TODO keyword" (const :value :todo) (string))
  1851. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1852. (cons :tag "Level number" (const :value :level) (integer))
  1853. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1854. (defcustom org-refile-target-verify-function nil
  1855. "Function to verify if the headline at point should be a refile target.
  1856. The function will be called without arguments, with point at the
  1857. beginning of the headline. It should return t and leave point
  1858. where it is if the headline is a valid target for refiling.
  1859. If the target should not be selected, the function must return nil.
  1860. In addition to this, it may move point to a place from where the search
  1861. should be continued. For example, the function may decide that the entire
  1862. subtree of the current entry should be excluded and move point to the end
  1863. of the subtree."
  1864. :group 'org-refile
  1865. :type 'function)
  1866. (defcustom org-refile-use-cache nil
  1867. "Non-nil means cache refile targets to speed up the process.
  1868. The cache for a particular file will be updated automatically when
  1869. the buffer has been killed, or when any of the marker used for flagging
  1870. refile targets no longer points at a live buffer.
  1871. If you have added new entries to a buffer that might themselves be targets,
  1872. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1873. find that easier, `C-u C-u C-u C-c C-w'."
  1874. :group 'org-refile
  1875. :version "24.1"
  1876. :type 'boolean)
  1877. (defcustom org-refile-use-outline-path nil
  1878. "Non-nil means provide refile targets as paths.
  1879. So a level 3 headline will be available as level1/level2/level3.
  1880. When the value is `file', also include the file name (without directory)
  1881. into the path. In this case, you can also stop the completion after
  1882. the file name, to get entries inserted as top level in the file.
  1883. When `full-file-path', include the full file path."
  1884. :group 'org-refile
  1885. :type '(choice
  1886. (const :tag "Not" nil)
  1887. (const :tag "Yes" t)
  1888. (const :tag "Start with file name" file)
  1889. (const :tag "Start with full file path" full-file-path)))
  1890. (defcustom org-outline-path-complete-in-steps t
  1891. "Non-nil means complete the outline path in hierarchical steps.
  1892. When Org-mode uses the refile interface to select an outline path
  1893. \(see variable `org-refile-use-outline-path'), the completion of
  1894. the path can be done is a single go, or if can be done in steps down
  1895. the headline hierarchy. Going in steps is probably the best if you
  1896. do not use a special completion package like `ido' or `icicles'.
  1897. However, when using these packages, going in one step can be very
  1898. fast, while still showing the whole path to the entry."
  1899. :group 'org-refile
  1900. :type 'boolean)
  1901. (defcustom org-refile-allow-creating-parent-nodes nil
  1902. "Non-nil means allow to create new nodes as refile targets.
  1903. New nodes are then created by adding \"/new node name\" to the completion
  1904. of an existing node. When the value of this variable is `confirm',
  1905. new node creation must be confirmed by the user (recommended)
  1906. When nil, the completion must match an existing entry.
  1907. Note that, if the new heading is not seen by the criteria
  1908. listed in `org-refile-targets', multiple instances of the same
  1909. heading would be created by trying again to file under the new
  1910. heading."
  1911. :group 'org-refile
  1912. :type '(choice
  1913. (const :tag "Never" nil)
  1914. (const :tag "Always" t)
  1915. (const :tag "Prompt for confirmation" confirm)))
  1916. (defcustom org-refile-active-region-within-subtree nil
  1917. "Non-nil means also refile active region within a subtree.
  1918. By default `org-refile' doesn't allow refiling regions if they
  1919. don't contain a set of subtrees, but it might be convenient to
  1920. do so sometimes: in that case, the first line of the region is
  1921. converted to a headline before refiling."
  1922. :group 'org-refile
  1923. :version "24.1"
  1924. :type 'boolean)
  1925. (defgroup org-todo nil
  1926. "Options concerning TODO items in Org-mode."
  1927. :tag "Org TODO"
  1928. :group 'org)
  1929. (defgroup org-progress nil
  1930. "Options concerning Progress logging in Org-mode."
  1931. :tag "Org Progress"
  1932. :group 'org-time)
  1933. (defvar org-todo-interpretation-widgets
  1934. '((:tag "Sequence (cycling hits every state)" sequence)
  1935. (:tag "Type (cycling directly to DONE)" type))
  1936. "The available interpretation symbols for customizing `org-todo-keywords'.
  1937. Interested libraries should add to this list.")
  1938. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1939. "List of TODO entry keyword sequences and their interpretation.
  1940. \\<org-mode-map>This is a list of sequences.
  1941. Each sequence starts with a symbol, either `sequence' or `type',
  1942. indicating if the keywords should be interpreted as a sequence of
  1943. action steps, or as different types of TODO items. The first
  1944. keywords are states requiring action - these states will select a headline
  1945. for inclusion into the global TODO list Org-mode produces. If one of
  1946. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1947. signify that no further action is necessary. If \"|\" is not found,
  1948. the last keyword is treated as the only DONE state of the sequence.
  1949. The command \\[org-todo] cycles an entry through these states, and one
  1950. additional state where no keyword is present. For details about this
  1951. cycling, see the manual.
  1952. TODO keywords and interpretation can also be set on a per-file basis with
  1953. the special #+SEQ_TODO and #+TYP_TODO lines.
  1954. Each keyword can optionally specify a character for fast state selection
  1955. \(in combination with the variable `org-use-fast-todo-selection')
  1956. and specifiers for state change logging, using the same syntax that
  1957. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1958. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1959. indicates to record a time stamp each time this state is selected.
  1960. Each keyword may also specify if a timestamp or a note should be
  1961. recorded when entering or leaving the state, by adding additional
  1962. characters in the parenthesis after the keyword. This looks like this:
  1963. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1964. record only the time of the state change. With X and Y being either
  1965. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1966. Y when leaving the state if and only if the *target* state does not
  1967. define X. You may omit any of the fast-selection key or X or /Y,
  1968. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1969. For backward compatibility, this variable may also be just a list
  1970. of keywords. In this case the interpretation (sequence or type) will be
  1971. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1972. :group 'org-todo
  1973. :group 'org-keywords
  1974. :type '(choice
  1975. (repeat :tag "Old syntax, just keywords"
  1976. (string :tag "Keyword"))
  1977. (repeat :tag "New syntax"
  1978. (cons
  1979. (choice
  1980. :tag "Interpretation"
  1981. ;;Quick and dirty way to see
  1982. ;;`org-todo-interpretations'. This takes the
  1983. ;;place of item arguments
  1984. :convert-widget
  1985. (lambda (widget)
  1986. (widget-put widget
  1987. :args (mapcar
  1988. #'(lambda (x)
  1989. (widget-convert
  1990. (cons 'const x)))
  1991. org-todo-interpretation-widgets))
  1992. widget))
  1993. (repeat
  1994. (string :tag "Keyword"))))))
  1995. (defvar org-todo-keywords-1 nil
  1996. "All TODO and DONE keywords active in a buffer.")
  1997. (make-variable-buffer-local 'org-todo-keywords-1)
  1998. (defvar org-todo-keywords-for-agenda nil)
  1999. (defvar org-done-keywords-for-agenda nil)
  2000. (defvar org-drawers-for-agenda nil)
  2001. (defvar org-todo-keyword-alist-for-agenda nil)
  2002. (defvar org-tag-alist-for-agenda nil)
  2003. (defvar org-agenda-contributing-files nil)
  2004. (defvar org-not-done-keywords nil)
  2005. (make-variable-buffer-local 'org-not-done-keywords)
  2006. (defvar org-done-keywords nil)
  2007. (make-variable-buffer-local 'org-done-keywords)
  2008. (defvar org-todo-heads nil)
  2009. (make-variable-buffer-local 'org-todo-heads)
  2010. (defvar org-todo-sets nil)
  2011. (make-variable-buffer-local 'org-todo-sets)
  2012. (defvar org-todo-log-states nil)
  2013. (make-variable-buffer-local 'org-todo-log-states)
  2014. (defvar org-todo-kwd-alist nil)
  2015. (make-variable-buffer-local 'org-todo-kwd-alist)
  2016. (defvar org-todo-key-alist nil)
  2017. (make-variable-buffer-local 'org-todo-key-alist)
  2018. (defvar org-todo-key-trigger nil)
  2019. (make-variable-buffer-local 'org-todo-key-trigger)
  2020. (defcustom org-todo-interpretation 'sequence
  2021. "Controls how TODO keywords are interpreted.
  2022. This variable is in principle obsolete and is only used for
  2023. backward compatibility, if the interpretation of todo keywords is
  2024. not given already in `org-todo-keywords'. See that variable for
  2025. more information."
  2026. :group 'org-todo
  2027. :group 'org-keywords
  2028. :type '(choice (const sequence)
  2029. (const type)))
  2030. (defcustom org-use-fast-todo-selection t
  2031. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2032. This variable describes if and under what circumstances the cycling
  2033. mechanism for TODO keywords will be replaced by a single-key, direct
  2034. selection scheme.
  2035. When nil, fast selection is never used.
  2036. When the symbol `prefix', it will be used when `org-todo' is called
  2037. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2038. `C-u t' in an agenda buffer.
  2039. When t, fast selection is used by default. In this case, the prefix
  2040. argument forces cycling instead.
  2041. In all cases, the special interface is only used if access keys have
  2042. actually been assigned by the user, i.e. if keywords in the configuration
  2043. are followed by a letter in parenthesis, like TODO(t)."
  2044. :group 'org-todo
  2045. :type '(choice
  2046. (const :tag "Never" nil)
  2047. (const :tag "By default" t)
  2048. (const :tag "Only with C-u C-c C-t" prefix)))
  2049. (defcustom org-provide-todo-statistics t
  2050. "Non-nil means update todo statistics after insert and toggle.
  2051. ALL-HEADLINES means update todo statistics by including headlines
  2052. with no TODO keyword as well, counting them as not done.
  2053. A list of TODO keywords means the same, but skip keywords that are
  2054. not in this list.
  2055. When this is set, todo statistics is updated in the parent of the
  2056. current entry each time a todo state is changed."
  2057. :group 'org-todo
  2058. :type '(choice
  2059. (const :tag "Yes, only for TODO entries" t)
  2060. (const :tag "Yes, including all entries" 'all-headlines)
  2061. (repeat :tag "Yes, for TODOs in this list"
  2062. (string :tag "TODO keyword"))
  2063. (other :tag "No TODO statistics" nil)))
  2064. (defcustom org-hierarchical-todo-statistics t
  2065. "Non-nil means TODO statistics covers just direct children.
  2066. When nil, all entries in the subtree are considered.
  2067. This has only an effect if `org-provide-todo-statistics' is set.
  2068. To set this to nil for only a single subtree, use a COOKIE_DATA
  2069. property and include the word \"recursive\" into the value."
  2070. :group 'org-todo
  2071. :type 'boolean)
  2072. (defcustom org-after-todo-state-change-hook nil
  2073. "Hook which is run after the state of a TODO item was changed.
  2074. The new state (a string with a TODO keyword, or nil) is available in the
  2075. Lisp variable `org-state'."
  2076. :group 'org-todo
  2077. :type 'hook)
  2078. (defvar org-blocker-hook nil
  2079. "Hook for functions that are allowed to block a state change.
  2080. Functions in this hook should not modify the buffer.
  2081. Each function gets as its single argument a property list,
  2082. see `org-trigger-hook' for more information about this list.
  2083. If any of the functions in this hook returns nil, the state change
  2084. is blocked.")
  2085. (defvar org-trigger-hook nil
  2086. "Hook for functions that are triggered by a state change.
  2087. Each function gets as its single argument a property list with at
  2088. least the following elements:
  2089. (:type type-of-change :position pos-at-entry-start
  2090. :from old-state :to new-state)
  2091. Depending on the type, more properties may be present.
  2092. This mechanism is currently implemented for:
  2093. TODO state changes
  2094. ------------------
  2095. :type todo-state-change
  2096. :from previous state (keyword as a string), or nil, or a symbol
  2097. 'todo' or 'done', to indicate the general type of state.
  2098. :to new state, like in :from")
  2099. (defcustom org-enforce-todo-dependencies nil
  2100. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2101. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2102. be blocked if any prior sibling is not yet done.
  2103. Finally, if the parent is blocked because of ordered siblings of its own,
  2104. the child will also be blocked."
  2105. :set (lambda (var val)
  2106. (set var val)
  2107. (if val
  2108. (add-hook 'org-blocker-hook
  2109. 'org-block-todo-from-children-or-siblings-or-parent)
  2110. (remove-hook 'org-blocker-hook
  2111. 'org-block-todo-from-children-or-siblings-or-parent)))
  2112. :group 'org-todo
  2113. :type 'boolean)
  2114. (defcustom org-enforce-todo-checkbox-dependencies nil
  2115. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2116. When this is nil, checkboxes have no influence on switching TODO states.
  2117. When non-nil, you first need to check off all check boxes before the TODO
  2118. entry can be switched to DONE.
  2119. This variable needs to be set before org.el is loaded, and you need to
  2120. restart Emacs after a change to make the change effective. The only way
  2121. to change is while Emacs is running is through the customize interface."
  2122. :set (lambda (var val)
  2123. (set var val)
  2124. (if val
  2125. (add-hook 'org-blocker-hook
  2126. 'org-block-todo-from-checkboxes)
  2127. (remove-hook 'org-blocker-hook
  2128. 'org-block-todo-from-checkboxes)))
  2129. :group 'org-todo
  2130. :type 'boolean)
  2131. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2132. "Non-nil means inserting a TODO heading is treated as state change.
  2133. So when the command \\[org-insert-todo-heading] is used, state change
  2134. logging will apply if appropriate. When nil, the new TODO item will
  2135. be inserted directly, and no logging will take place."
  2136. :group 'org-todo
  2137. :type 'boolean)
  2138. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2139. "Non-nil means switching TODO states with S-cursor counts as state change.
  2140. This is the default behavior. However, setting this to nil allows a
  2141. convenient way to select a TODO state and bypass any logging associated
  2142. with that."
  2143. :group 'org-todo
  2144. :type 'boolean)
  2145. (defcustom org-todo-state-tags-triggers nil
  2146. "Tag changes that should be triggered by TODO state changes.
  2147. This is a list. Each entry is
  2148. (state-change (tag . flag) .......)
  2149. State-change can be a string with a state, and empty string to indicate the
  2150. state that has no TODO keyword, or it can be one of the symbols `todo'
  2151. or `done', meaning any not-done or done state, respectively."
  2152. :group 'org-todo
  2153. :group 'org-tags
  2154. :type '(repeat
  2155. (cons (choice :tag "When changing to"
  2156. (const :tag "Not-done state" todo)
  2157. (const :tag "Done state" done)
  2158. (string :tag "State"))
  2159. (repeat
  2160. (cons :tag "Tag action"
  2161. (string :tag "Tag")
  2162. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2163. (defcustom org-log-done nil
  2164. "Information to record when a task moves to the DONE state.
  2165. Possible values are:
  2166. nil Don't add anything, just change the keyword
  2167. time Add a time stamp to the task
  2168. note Prompt for a note and add it with template `org-log-note-headings'
  2169. This option can also be set with on a per-file-basis with
  2170. #+STARTUP: nologdone
  2171. #+STARTUP: logdone
  2172. #+STARTUP: lognotedone
  2173. You can have local logging settings for a subtree by setting the LOGGING
  2174. property to one or more of these keywords."
  2175. :group 'org-todo
  2176. :group 'org-progress
  2177. :type '(choice
  2178. (const :tag "No logging" nil)
  2179. (const :tag "Record CLOSED timestamp" time)
  2180. (const :tag "Record CLOSED timestamp with note." note)))
  2181. ;; Normalize old uses of org-log-done.
  2182. (cond
  2183. ((eq org-log-done t) (setq org-log-done 'time))
  2184. ((and (listp org-log-done) (memq 'done org-log-done))
  2185. (setq org-log-done 'note)))
  2186. (defcustom org-log-reschedule nil
  2187. "Information to record when the scheduling date of a tasks is modified.
  2188. Possible values are:
  2189. nil Don't add anything, just change the date
  2190. time Add a time stamp to the task
  2191. note Prompt for a note and add it with template `org-log-note-headings'
  2192. This option can also be set with on a per-file-basis with
  2193. #+STARTUP: nologreschedule
  2194. #+STARTUP: logreschedule
  2195. #+STARTUP: lognotereschedule"
  2196. :group 'org-todo
  2197. :group 'org-progress
  2198. :type '(choice
  2199. (const :tag "No logging" nil)
  2200. (const :tag "Record timestamp" time)
  2201. (const :tag "Record timestamp with note." note)))
  2202. (defcustom org-log-redeadline nil
  2203. "Information to record when the deadline date of a tasks is modified.
  2204. Possible values are:
  2205. nil Don't add anything, just change the date
  2206. time Add a time stamp to the task
  2207. note Prompt for a note and add it with template `org-log-note-headings'
  2208. This option can also be set with on a per-file-basis with
  2209. #+STARTUP: nologredeadline
  2210. #+STARTUP: logredeadline
  2211. #+STARTUP: lognoteredeadline
  2212. You can have local logging settings for a subtree by setting the LOGGING
  2213. property to one or more of these keywords."
  2214. :group 'org-todo
  2215. :group 'org-progress
  2216. :type '(choice
  2217. (const :tag "No logging" nil)
  2218. (const :tag "Record timestamp" time)
  2219. (const :tag "Record timestamp with note." note)))
  2220. (defcustom org-log-note-clock-out nil
  2221. "Non-nil means record a note when clocking out of an item.
  2222. This can also be configured on a per-file basis by adding one of
  2223. the following lines anywhere in the buffer:
  2224. #+STARTUP: lognoteclock-out
  2225. #+STARTUP: nolognoteclock-out"
  2226. :group 'org-todo
  2227. :group 'org-progress
  2228. :type 'boolean)
  2229. (defcustom org-log-done-with-time t
  2230. "Non-nil means the CLOSED time stamp will contain date and time.
  2231. When nil, only the date will be recorded."
  2232. :group 'org-progress
  2233. :type 'boolean)
  2234. (defcustom org-log-note-headings
  2235. '((done . "CLOSING NOTE %t")
  2236. (state . "State %-12s from %-12S %t")
  2237. (note . "Note taken on %t")
  2238. (reschedule . "Rescheduled from %S on %t")
  2239. (delschedule . "Not scheduled, was %S on %t")
  2240. (redeadline . "New deadline from %S on %t")
  2241. (deldeadline . "Removed deadline, was %S on %t")
  2242. (refile . "Refiled on %t")
  2243. (clock-out . ""))
  2244. "Headings for notes added to entries.
  2245. The value is an alist, with the car being a symbol indicating the note
  2246. context, and the cdr is the heading to be used. The heading may also be the
  2247. empty string.
  2248. %t in the heading will be replaced by a time stamp.
  2249. %T will be an active time stamp instead the default inactive one
  2250. %d will be replaced by a short-format time stamp.
  2251. %D will be replaced by an active short-format time stamp.
  2252. %s will be replaced by the new TODO state, in double quotes.
  2253. %S will be replaced by the old TODO state, in double quotes.
  2254. %u will be replaced by the user name.
  2255. %U will be replaced by the full user name.
  2256. In fact, it is not a good idea to change the `state' entry, because
  2257. agenda log mode depends on the format of these entries."
  2258. :group 'org-todo
  2259. :group 'org-progress
  2260. :type '(list :greedy t
  2261. (cons (const :tag "Heading when closing an item" done) string)
  2262. (cons (const :tag
  2263. "Heading when changing todo state (todo sequence only)"
  2264. state) string)
  2265. (cons (const :tag "Heading when just taking a note" note) string)
  2266. (cons (const :tag "Heading when clocking out" clock-out) string)
  2267. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2268. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2269. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2270. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2271. (cons (const :tag "Heading when refiling" refile) string)))
  2272. (unless (assq 'note org-log-note-headings)
  2273. (push '(note . "%t") org-log-note-headings))
  2274. (defcustom org-log-into-drawer nil
  2275. "Non-nil means insert state change notes and time stamps into a drawer.
  2276. When nil, state changes notes will be inserted after the headline and
  2277. any scheduling and clock lines, but not inside a drawer.
  2278. The value of this variable should be the name of the drawer to use.
  2279. LOGBOOK is proposed as the default drawer for this purpose, you can
  2280. also set this to a string to define the drawer of your choice.
  2281. A value of t is also allowed, representing \"LOGBOOK\".
  2282. A value of t or nil can also be set with on a per-file-basis with
  2283. #+STARTUP: logdrawer
  2284. #+STARTUP: nologdrawer
  2285. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2286. will be ignored.
  2287. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2288. a subtree."
  2289. :group 'org-todo
  2290. :group 'org-progress
  2291. :type '(choice
  2292. (const :tag "Not into a drawer" nil)
  2293. (const :tag "LOGBOOK" t)
  2294. (string :tag "Other")))
  2295. (if (fboundp 'defvaralias)
  2296. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2297. (defun org-log-into-drawer ()
  2298. "Return the value of `org-log-into-drawer', but let properties overrule.
  2299. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2300. used instead of the default value."
  2301. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2302. (cond
  2303. ((not p) org-log-into-drawer)
  2304. ((equal p "nil") nil)
  2305. ((equal p "t") "LOGBOOK")
  2306. (t p))))
  2307. (defcustom org-log-state-notes-insert-after-drawers nil
  2308. "Non-nil means insert state change notes after any drawers in entry.
  2309. Only the drawers that *immediately* follow the headline and the
  2310. deadline/scheduled line are skipped.
  2311. When nil, insert notes right after the heading and perhaps the line
  2312. with deadline/scheduling if present.
  2313. This variable will have no effect if `org-log-into-drawer' is
  2314. set."
  2315. :group 'org-todo
  2316. :group 'org-progress
  2317. :type 'boolean)
  2318. (defcustom org-log-states-order-reversed t
  2319. "Non-nil means the latest state note will be directly after heading.
  2320. When nil, the state change notes will be ordered according to time.
  2321. This option can also be set with on a per-file-basis with
  2322. #+STARTUP: logstatesreversed
  2323. #+STARTUP: nologstatesreversed"
  2324. :group 'org-todo
  2325. :group 'org-progress
  2326. :type 'boolean)
  2327. (defcustom org-todo-repeat-to-state nil
  2328. "The TODO state to which a repeater should return the repeating task.
  2329. By default this is the first task in a TODO sequence, or the previous state
  2330. in a TODO_TYP set. But you can specify another task here.
  2331. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2332. :group 'org-todo
  2333. :version "24.1"
  2334. :type '(choice (const :tag "Head of sequence" nil)
  2335. (string :tag "Specific state")))
  2336. (defcustom org-log-repeat 'time
  2337. "Non-nil means record moving through the DONE state when triggering repeat.
  2338. An auto-repeating task is immediately switched back to TODO when
  2339. marked DONE. If you are not logging state changes (by adding \"@\"
  2340. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2341. record a closing note, there will be no record of the task moving
  2342. through DONE. This variable forces taking a note anyway.
  2343. nil Don't force a record
  2344. time Record a time stamp
  2345. note Prompt for a note and add it with template `org-log-note-headings'
  2346. This option can also be set with on a per-file-basis with
  2347. #+STARTUP: nologrepeat
  2348. #+STARTUP: logrepeat
  2349. #+STARTUP: lognoterepeat
  2350. You can have local logging settings for a subtree by setting the LOGGING
  2351. property to one or more of these keywords."
  2352. :group 'org-todo
  2353. :group 'org-progress
  2354. :type '(choice
  2355. (const :tag "Don't force a record" nil)
  2356. (const :tag "Force recording the DONE state" time)
  2357. (const :tag "Force recording a note with the DONE state" note)))
  2358. (defgroup org-priorities nil
  2359. "Priorities in Org-mode."
  2360. :tag "Org Priorities"
  2361. :group 'org-todo)
  2362. (defcustom org-enable-priority-commands t
  2363. "Non-nil means priority commands are active.
  2364. When nil, these commands will be disabled, so that you never accidentally
  2365. set a priority."
  2366. :group 'org-priorities
  2367. :type 'boolean)
  2368. (defcustom org-highest-priority ?A
  2369. "The highest priority of TODO items. A character like ?A, ?B etc.
  2370. Must have a smaller ASCII number than `org-lowest-priority'."
  2371. :group 'org-priorities
  2372. :type 'character)
  2373. (defcustom org-lowest-priority ?C
  2374. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2375. Must have a larger ASCII number than `org-highest-priority'."
  2376. :group 'org-priorities
  2377. :type 'character)
  2378. (defcustom org-default-priority ?B
  2379. "The default priority of TODO items.
  2380. This is the priority an item gets if no explicit priority is given.
  2381. When starting to cycle on an empty priority the first step in the cycle
  2382. depends on `org-priority-start-cycle-with-default'. The resulting first
  2383. step priority must not exceed the range from `org-highest-priority' to
  2384. `org-lowest-priority' which means that `org-default-priority' has to be
  2385. in this range exclusive or inclusive the range boundaries. Else the
  2386. first step refuses to set the default and the second will fall back
  2387. to (depending on the command used) the highest or lowest priority."
  2388. :group 'org-priorities
  2389. :type 'character)
  2390. (defcustom org-priority-start-cycle-with-default t
  2391. "Non-nil means start with default priority when starting to cycle.
  2392. When this is nil, the first step in the cycle will be (depending on the
  2393. command used) one higher or lower than the default priority.
  2394. See also `org-default-priority'."
  2395. :group 'org-priorities
  2396. :type 'boolean)
  2397. (defcustom org-get-priority-function nil
  2398. "Function to extract the priority from a string.
  2399. The string is normally the headline. If this is nil Org computes the
  2400. priority from the priority cookie like [#A] in the headline. It returns
  2401. an integer, increasing by 1000 for each priority level.
  2402. The user can set a different function here, which should take a string
  2403. as an argument and return the numeric priority."
  2404. :group 'org-priorities
  2405. :version "24.1"
  2406. :type 'function)
  2407. (defgroup org-time nil
  2408. "Options concerning time stamps and deadlines in Org-mode."
  2409. :tag "Org Time"
  2410. :group 'org)
  2411. (defcustom org-insert-labeled-timestamps-at-point nil
  2412. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2413. When nil, these labeled time stamps are forces into the second line of an
  2414. entry, just after the headline. When scheduling from the global TODO list,
  2415. the time stamp will always be forced into the second line."
  2416. :group 'org-time
  2417. :type 'boolean)
  2418. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2419. "Formats for `format-time-string' which are used for time stamps.
  2420. It is not recommended to change this constant.")
  2421. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2422. "Number of minutes to round time stamps to.
  2423. These are two values, the first applies when first creating a time stamp.
  2424. The second applies when changing it with the commands `S-up' and `S-down'.
  2425. When changing the time stamp, this means that it will change in steps
  2426. of N minutes, as given by the second value.
  2427. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2428. numbers should be factors of 60, so for example 5, 10, 15.
  2429. When this is larger than 1, you can still force an exact time stamp by using
  2430. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2431. and by using a prefix arg to `S-up/down' to specify the exact number
  2432. of minutes to shift."
  2433. :group 'org-time
  2434. :get #'(lambda (var) ; Make sure both elements are there
  2435. (if (integerp (default-value var))
  2436. (list (default-value var) 5)
  2437. (default-value var)))
  2438. :type '(list
  2439. (integer :tag "when inserting times")
  2440. (integer :tag "when modifying times")))
  2441. ;; Normalize old customizations of this variable.
  2442. (when (integerp org-time-stamp-rounding-minutes)
  2443. (setq org-time-stamp-rounding-minutes
  2444. (list org-time-stamp-rounding-minutes
  2445. org-time-stamp-rounding-minutes)))
  2446. (defcustom org-display-custom-times nil
  2447. "Non-nil means overlay custom formats over all time stamps.
  2448. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2449. To turn this on on a per-file basis, insert anywhere in the file:
  2450. #+STARTUP: customtime"
  2451. :group 'org-time
  2452. :set 'set-default
  2453. :type 'sexp)
  2454. (make-variable-buffer-local 'org-display-custom-times)
  2455. (defcustom org-time-stamp-custom-formats
  2456. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2457. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2458. These are overlaid over the default ISO format if the variable
  2459. `org-display-custom-times' is set. Time like %H:%M should be at the
  2460. end of the second format. The custom formats are also honored by export
  2461. commands, if custom time display is turned on at the time of export."
  2462. :group 'org-time
  2463. :type 'sexp)
  2464. (defun org-time-stamp-format (&optional long inactive)
  2465. "Get the right format for a time string."
  2466. (let ((f (if long (cdr org-time-stamp-formats)
  2467. (car org-time-stamp-formats))))
  2468. (if inactive
  2469. (concat "[" (substring f 1 -1) "]")
  2470. f)))
  2471. (defcustom org-time-clocksum-format
  2472. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2473. "The format string used when creating CLOCKSUM lines.
  2474. This is also used when Org mode generates a time duration.
  2475. The value can be a single format string containing two
  2476. %-sequences, which will be filled with the number of hours and
  2477. minutes in that order.
  2478. Alternatively, the value can be a plist associating any of the
  2479. keys :years, :months, :weeks, :days, :hours or :minutes with
  2480. format strings. The time duration is formatted using only the
  2481. time components that are needed and concatenating the results.
  2482. If a time unit in absent, it falls back to the next smallest
  2483. unit.
  2484. The keys :require-years, :require-months, :require-days,
  2485. :require-weeks, :require-hours, :require-minutes are also
  2486. meaningful. A non-nil value for these keys indicates that the
  2487. corresponding time component should always be included, even if
  2488. its value is 0.
  2489. For example,
  2490. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2491. :require-minutes t)
  2492. means durations longer than a day will be expressed in days,
  2493. hours and minutes, and durations less than a day will always be
  2494. expressed in hours and minutes (even for durations less than an
  2495. hour).
  2496. The value
  2497. \(:days \"%dd\" :minutes \"%dm\")
  2498. means durations longer than a day will be expressed in days and
  2499. minutes, and durations less than a day will be expressed entirely
  2500. in minutes (even for durations longer than an hour)."
  2501. :group 'org-time
  2502. :group 'org-clock
  2503. :version "24.3"
  2504. :type '(choice (string :tag "Format string")
  2505. (set :tag "Plist"
  2506. (group :inline t (const :tag "Years" :years)
  2507. (string :tag "Format string"))
  2508. (group :inline t
  2509. (const :tag "Always show years" :require-years)
  2510. (const t))
  2511. (group :inline t (const :tag "Months" :months)
  2512. (string :tag "Format string"))
  2513. (group :inline t
  2514. (const :tag "Always show months" :require-months)
  2515. (const t))
  2516. (group :inline t (const :tag "Weeks" :weeks)
  2517. (string :tag "Format string"))
  2518. (group :inline t
  2519. (const :tag "Always show weeks" :require-weeks)
  2520. (const t))
  2521. (group :inline t (const :tag "Days" :days)
  2522. (string :tag "Format string"))
  2523. (group :inline t
  2524. (const :tag "Always show days" :require-days)
  2525. (const t))
  2526. (group :inline t (const :tag "Hours" :hours)
  2527. (string :tag "Format string"))
  2528. (group :inline t
  2529. (const :tag "Always show hours" :require-hours)
  2530. (const t))
  2531. (group :inline t (const :tag "Minutes" :minutes)
  2532. (string :tag "Format string"))
  2533. (group :inline t
  2534. (const :tag "Always show minutes" :require-minutes)
  2535. (const t)))))
  2536. (defcustom org-time-clocksum-use-fractional nil
  2537. "When non-nil, \\[org-clock-display] uses fractional times.
  2538. See `org-time-clocksum-format' for more on time clock formats."
  2539. :group 'org-time
  2540. :group 'org-clock
  2541. :version "24.3"
  2542. :type 'boolean)
  2543. (defcustom org-time-clocksum-use-effort-durations t
  2544. "When non-nil, \\[org-clock-display] uses effort durations.
  2545. E.g. by default, one day is considered to be a 8 hours effort,
  2546. so a task that has been clocked for 16 hours will be displayed
  2547. as during 2 days in the clock display or in the clocktable.
  2548. See `org-effort-durations' on how to set effort durations
  2549. and `org-time-clocksum-format' for more on time clock formats."
  2550. :group 'org-time
  2551. :group 'org-clock
  2552. :version "24.3"
  2553. :type 'boolean)
  2554. (defcustom org-time-clocksum-fractional-format "%.2f"
  2555. "The format string used when creating CLOCKSUM lines,
  2556. or when Org mode generates a time duration, if
  2557. `org-time-clocksum-use-fractional' is enabled.
  2558. The value can be a single format string containing one
  2559. %-sequence, which will be filled with the number of hours as
  2560. a float.
  2561. Alternatively, the value can be a plist associating any of the
  2562. keys :years, :months, :weeks, :days, :hours or :minutes with
  2563. a format string. The time duration is formatted using the
  2564. largest time unit which gives a non-zero integer part. If all
  2565. specified formats have zero integer part, the smallest time unit
  2566. is used."
  2567. :group 'org-time
  2568. :type '(choice (string :tag "Format string")
  2569. (set (group :inline t (const :tag "Years" :years)
  2570. (string :tag "Format string"))
  2571. (group :inline t (const :tag "Months" :months)
  2572. (string :tag "Format string"))
  2573. (group :inline t (const :tag "Weeks" :weeks)
  2574. (string :tag "Format string"))
  2575. (group :inline t (const :tag "Days" :days)
  2576. (string :tag "Format string"))
  2577. (group :inline t (const :tag "Hours" :hours)
  2578. (string :tag "Format string"))
  2579. (group :inline t (const :tag "Minutes" :minutes)
  2580. (string :tag "Format string")))))
  2581. (defcustom org-deadline-warning-days 14
  2582. "Number of days before expiration during which a deadline becomes active.
  2583. This variable governs the display in sparse trees and in the agenda.
  2584. When 0 or negative, it means use this number (the absolute value of it)
  2585. even if a deadline has a different individual lead time specified.
  2586. Custom commands can set this variable in the options section."
  2587. :group 'org-time
  2588. :group 'org-agenda-daily/weekly
  2589. :type 'integer)
  2590. (defcustom org-scheduled-delay-days 0
  2591. "Number of days before a scheduled item becomes active.
  2592. This variable governs the display in sparse trees and in the agenda.
  2593. The default value (i.e. 0) means: don't delay scheduled item.
  2594. When negative, it means use this number (the absolute value of it)
  2595. even if a scheduled item has a different individual delay time
  2596. specified.
  2597. Custom commands can set this variable in the options section."
  2598. :group 'org-time
  2599. :group 'org-agenda-daily/weekly
  2600. :version "24.3"
  2601. :type 'integer)
  2602. (defcustom org-read-date-prefer-future t
  2603. "Non-nil means assume future for incomplete date input from user.
  2604. This affects the following situations:
  2605. 1. The user gives a month but not a year.
  2606. For example, if it is April and you enter \"feb 2\", this will be read
  2607. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2608. 2. The user gives a day, but no month.
  2609. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2610. read this as the third of *next* month. However, if you enter \"17\",
  2611. it will be considered as *this* month.
  2612. If you set this variable to the symbol `time', then also the following
  2613. will work:
  2614. 3. If the user gives a time.
  2615. If the time is before now, it will be interpreted as tomorrow.
  2616. Currently none of this works for ISO week specifications.
  2617. When this option is nil, the current day, month and year will always be
  2618. used as defaults.
  2619. See also `org-agenda-jump-prefer-future'."
  2620. :group 'org-time
  2621. :type '(choice
  2622. (const :tag "Never" nil)
  2623. (const :tag "Check month and day" t)
  2624. (const :tag "Check month, day, and time" time)))
  2625. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2626. "Should the agenda jump command prefer the future for incomplete dates?
  2627. The default is to do the same as configured in `org-read-date-prefer-future'.
  2628. But you can also set a deviating value here.
  2629. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2630. :group 'org-agenda
  2631. :group 'org-time
  2632. :version "24.1"
  2633. :type '(choice
  2634. (const :tag "Use org-read-date-prefer-future"
  2635. org-read-date-prefer-future)
  2636. (const :tag "Never" nil)
  2637. (const :tag "Always" t)))
  2638. (defcustom org-read-date-force-compatible-dates t
  2639. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2640. Depending on the system Emacs is running on, certain dates cannot
  2641. be represented with the type used internally to represent time.
  2642. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2643. correctly. Some systems allow for earlier dates, some for later,
  2644. some for both. One way to find out it to insert any date into an
  2645. Org buffer, putting the cursor on the year and hitting S-up and
  2646. S-down to test the range.
  2647. When this variable is set to t, the date/time prompt will not let
  2648. you specify dates outside the 1970-2037 range, so it is certain that
  2649. these dates will work in whatever version of Emacs you are
  2650. running, and also that you can move a file from one Emacs implementation
  2651. to another. WHenever Org is forcing the year for you, it will display
  2652. a message and beep.
  2653. When this variable is nil, Org will check if the date is
  2654. representable in the specific Emacs implementation you are using.
  2655. If not, it will force a year, usually the current year, and beep
  2656. to remind you. Currently this setting is not recommended because
  2657. the likelihood that you will open your Org files in an Emacs that
  2658. has limited date range is not negligible.
  2659. A workaround for this problem is to use diary sexp dates for time
  2660. stamps outside of this range."
  2661. :group 'org-time
  2662. :version "24.1"
  2663. :type 'boolean)
  2664. (defcustom org-read-date-display-live t
  2665. "Non-nil means display current interpretation of date prompt live.
  2666. This display will be in an overlay, in the minibuffer."
  2667. :group 'org-time
  2668. :type 'boolean)
  2669. (defcustom org-read-date-popup-calendar t
  2670. "Non-nil means pop up a calendar when prompting for a date.
  2671. In the calendar, the date can be selected with mouse-1. However, the
  2672. minibuffer will also be active, and you can simply enter the date as well.
  2673. When nil, only the minibuffer will be available."
  2674. :group 'org-time
  2675. :type 'boolean)
  2676. (if (fboundp 'defvaralias)
  2677. (defvaralias 'org-popup-calendar-for-date-prompt
  2678. 'org-read-date-popup-calendar))
  2679. (make-obsolete-variable
  2680. 'org-read-date-minibuffer-setup-hook
  2681. "Set `org-read-date-minibuffer-local-map' instead." "24.3")
  2682. (defcustom org-read-date-minibuffer-setup-hook nil
  2683. "Hook to be used to set up keys for the date/time interface.
  2684. Add key definitions to `minibuffer-local-map', which will be a
  2685. temporary copy.
  2686. WARNING: This option is obsolete, you should use
  2687. `org-read-date-minibuffer-local-map' to set up keys."
  2688. :group 'org-time
  2689. :type 'hook)
  2690. (defcustom org-extend-today-until 0
  2691. "The hour when your day really ends. Must be an integer.
  2692. This has influence for the following applications:
  2693. - When switching the agenda to \"today\". It it is still earlier than
  2694. the time given here, the day recognized as TODAY is actually yesterday.
  2695. - When a date is read from the user and it is still before the time given
  2696. here, the current date and time will be assumed to be yesterday, 23:59.
  2697. Also, timestamps inserted in capture templates follow this rule.
  2698. IMPORTANT: This is a feature whose implementation is and likely will
  2699. remain incomplete. Really, it is only here because past midnight seems to
  2700. be the favorite working time of John Wiegley :-)"
  2701. :group 'org-time
  2702. :type 'integer)
  2703. (defcustom org-use-effective-time nil
  2704. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2705. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2706. \"effective time\" of any timestamps between midnight and 8am will be
  2707. 23:59 of the previous day."
  2708. :group 'org-time
  2709. :version "24.1"
  2710. :type 'boolean)
  2711. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2712. "When non-nil, use the last clock out time for `org-todo'.
  2713. Note that this option has precedence over the combined use of
  2714. `org-use-effective-time' and `org-extend-today-until'."
  2715. :group 'org-time
  2716. ;; :version "24.3"
  2717. :type 'boolean)
  2718. (defcustom org-edit-timestamp-down-means-later nil
  2719. "Non-nil means S-down will increase the time in a time stamp.
  2720. When nil, S-up will increase."
  2721. :group 'org-time
  2722. :type 'boolean)
  2723. (defcustom org-calendar-follow-timestamp-change t
  2724. "Non-nil means make the calendar window follow timestamp changes.
  2725. When a timestamp is modified and the calendar window is visible, it will be
  2726. moved to the new date."
  2727. :group 'org-time
  2728. :type 'boolean)
  2729. (defgroup org-tags nil
  2730. "Options concerning tags in Org-mode."
  2731. :tag "Org Tags"
  2732. :group 'org)
  2733. (defcustom org-tag-alist nil
  2734. "List of tags allowed in Org-mode files.
  2735. When this list is nil, Org-mode will base TAG input on what is already in the
  2736. buffer.
  2737. The value of this variable is an alist, the car of each entry must be a
  2738. keyword as a string, the cdr may be a character that is used to select
  2739. that tag through the fast-tag-selection interface.
  2740. See the manual for details."
  2741. :group 'org-tags
  2742. :type '(repeat
  2743. (choice
  2744. (cons (string :tag "Tag name")
  2745. (character :tag "Access char"))
  2746. (list :tag "Start radio group"
  2747. (const :startgroup)
  2748. (option (string :tag "Group description")))
  2749. (list :tag "End radio group"
  2750. (const :endgroup)
  2751. (option (string :tag "Group description")))
  2752. (const :tag "New line" (:newline)))))
  2753. (defcustom org-tag-persistent-alist nil
  2754. "List of tags that will always appear in all Org-mode files.
  2755. This is in addition to any in buffer settings or customizations
  2756. of `org-tag-alist'.
  2757. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2758. The value of this variable is an alist, the car of each entry must be a
  2759. keyword as a string, the cdr may be a character that is used to select
  2760. that tag through the fast-tag-selection interface.
  2761. See the manual for details.
  2762. To disable these tags on a per-file basis, insert anywhere in the file:
  2763. #+STARTUP: noptag"
  2764. :group 'org-tags
  2765. :type '(repeat
  2766. (choice
  2767. (cons (string :tag "Tag name")
  2768. (character :tag "Access char"))
  2769. (const :tag "Start radio group" (:startgroup))
  2770. (const :tag "End radio group" (:endgroup))
  2771. (const :tag "New line" (:newline)))))
  2772. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2773. "If non-nil, always offer completion for all tags of all agenda files.
  2774. Instead of customizing this variable directly, you might want to
  2775. set it locally for capture buffers, because there no list of
  2776. tags in that file can be created dynamically (there are none).
  2777. (add-hook 'org-capture-mode-hook
  2778. (lambda ()
  2779. (set (make-local-variable
  2780. 'org-complete-tags-always-offer-all-agenda-tags)
  2781. t)))"
  2782. :group 'org-tags
  2783. :version "24.1"
  2784. :type 'boolean)
  2785. (defvar org-file-tags nil
  2786. "List of tags that can be inherited by all entries in the file.
  2787. The tags will be inherited if the variable `org-use-tag-inheritance'
  2788. says they should be.
  2789. This variable is populated from #+FILETAGS lines.")
  2790. (defcustom org-use-fast-tag-selection 'auto
  2791. "Non-nil means use fast tag selection scheme.
  2792. This is a special interface to select and deselect tags with single keys.
  2793. When nil, fast selection is never used.
  2794. When the symbol `auto', fast selection is used if and only if selection
  2795. characters for tags have been configured, either through the variable
  2796. `org-tag-alist' or through a #+TAGS line in the buffer.
  2797. When t, fast selection is always used and selection keys are assigned
  2798. automatically if necessary."
  2799. :group 'org-tags
  2800. :type '(choice
  2801. (const :tag "Always" t)
  2802. (const :tag "Never" nil)
  2803. (const :tag "When selection characters are configured" 'auto)))
  2804. (defcustom org-fast-tag-selection-single-key nil
  2805. "Non-nil means fast tag selection exits after first change.
  2806. When nil, you have to press RET to exit it.
  2807. During fast tag selection, you can toggle this flag with `C-c'.
  2808. This variable can also have the value `expert'. In this case, the window
  2809. displaying the tags menu is not even shown, until you press C-c again."
  2810. :group 'org-tags
  2811. :type '(choice
  2812. (const :tag "No" nil)
  2813. (const :tag "Yes" t)
  2814. (const :tag "Expert" expert)))
  2815. (defvar org-fast-tag-selection-include-todo nil
  2816. "Non-nil means fast tags selection interface will also offer TODO states.
  2817. This is an undocumented feature, you should not rely on it.")
  2818. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2819. "The column to which tags should be indented in a headline.
  2820. If this number is positive, it specifies the column. If it is negative,
  2821. it means that the tags should be flushright to that column. For example,
  2822. -80 works well for a normal 80 character screen.
  2823. When 0, place tags directly after headline text, with only one space in
  2824. between."
  2825. :group 'org-tags
  2826. :type 'integer)
  2827. (defcustom org-auto-align-tags t
  2828. "Non-nil keeps tags aligned when modifying headlines.
  2829. Some operations (i.e. demoting) change the length of a headline and
  2830. therefore shift the tags around. With this option turned on, after
  2831. each such operation the tags are again aligned to `org-tags-column'."
  2832. :group 'org-tags
  2833. :type 'boolean)
  2834. (defcustom org-use-tag-inheritance t
  2835. "Non-nil means tags in levels apply also for sublevels.
  2836. When nil, only the tags directly given in a specific line apply there.
  2837. This may also be a list of tags that should be inherited, or a regexp that
  2838. matches tags that should be inherited. Additional control is possible
  2839. with the variable `org-tags-exclude-from-inheritance' which gives an
  2840. explicit list of tags to be excluded from inheritance, even if the value of
  2841. `org-use-tag-inheritance' would select it for inheritance.
  2842. If this option is t, a match early-on in a tree can lead to a large
  2843. number of matches in the subtree when constructing the agenda or creating
  2844. a sparse tree. If you only want to see the first match in a tree during
  2845. a search, check out the variable `org-tags-match-list-sublevels'."
  2846. :group 'org-tags
  2847. :type '(choice
  2848. (const :tag "Not" nil)
  2849. (const :tag "Always" t)
  2850. (repeat :tag "Specific tags" (string :tag "Tag"))
  2851. (regexp :tag "Tags matched by regexp")))
  2852. (defcustom org-tags-exclude-from-inheritance nil
  2853. "List of tags that should never be inherited.
  2854. This is a way to exclude a few tags from inheritance. For way to do
  2855. the opposite, to actively allow inheritance for selected tags,
  2856. see the variable `org-use-tag-inheritance'."
  2857. :group 'org-tags
  2858. :type '(repeat (string :tag "Tag")))
  2859. (defun org-tag-inherit-p (tag)
  2860. "Check if TAG is one that should be inherited."
  2861. (cond
  2862. ((member tag org-tags-exclude-from-inheritance) nil)
  2863. ((eq org-use-tag-inheritance t) t)
  2864. ((not org-use-tag-inheritance) nil)
  2865. ((stringp org-use-tag-inheritance)
  2866. (string-match org-use-tag-inheritance tag))
  2867. ((listp org-use-tag-inheritance)
  2868. (member tag org-use-tag-inheritance))
  2869. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2870. (defcustom org-tags-match-list-sublevels t
  2871. "Non-nil means list also sublevels of headlines matching a search.
  2872. This variable applies to tags/property searches, and also to stuck
  2873. projects because this search is based on a tags match as well.
  2874. When set to the symbol `indented', sublevels are indented with
  2875. leading dots.
  2876. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2877. the sublevels of a headline matching a tag search often also match
  2878. the same search. Listing all of them can create very long lists.
  2879. Setting this variable to nil causes subtrees of a match to be skipped.
  2880. This variable is semi-obsolete and probably should always be true. It
  2881. is better to limit inheritance to certain tags using the variables
  2882. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2883. :group 'org-tags
  2884. :type '(choice
  2885. (const :tag "No, don't list them" nil)
  2886. (const :tag "Yes, do list them" t)
  2887. (const :tag "List them, indented with leading dots" indented)))
  2888. (defcustom org-tags-sort-function nil
  2889. "When set, tags are sorted using this function as a comparator."
  2890. :group 'org-tags
  2891. :type '(choice
  2892. (const :tag "No sorting" nil)
  2893. (const :tag "Alphabetical" string<)
  2894. (const :tag "Reverse alphabetical" string>)
  2895. (function :tag "Custom function" nil)))
  2896. (defvar org-tags-history nil
  2897. "History of minibuffer reads for tags.")
  2898. (defvar org-last-tags-completion-table nil
  2899. "The last used completion table for tags.")
  2900. (defvar org-after-tags-change-hook nil
  2901. "Hook that is run after the tags in a line have changed.")
  2902. (defgroup org-properties nil
  2903. "Options concerning properties in Org-mode."
  2904. :tag "Org Properties"
  2905. :group 'org)
  2906. (defcustom org-property-format "%-10s %s"
  2907. "How property key/value pairs should be formatted by `indent-line'.
  2908. When `indent-line' hits a property definition, it will format the line
  2909. according to this format, mainly to make sure that the values are
  2910. lined-up with respect to each other."
  2911. :group 'org-properties
  2912. :type 'string)
  2913. (defcustom org-properties-postprocess-alist nil
  2914. "Alist of properties and functions to adjust inserted values.
  2915. Elements of this alist must be of the form
  2916. ([string] [function])
  2917. where [string] must be a property name and [function] must be a
  2918. lambda expression: this lambda expression must take one argument,
  2919. the value to adjust, and return the new value as a string.
  2920. For example, this element will allow the property \"Remaining\"
  2921. to be updated wrt the relation between the \"Effort\" property
  2922. and the clock summary:
  2923. ((\"Remaining\" (lambda(value)
  2924. (let ((clocksum (org-clock-sum-current-item))
  2925. (effort (org-duration-string-to-minutes
  2926. (org-entry-get (point) \"Effort\"))))
  2927. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  2928. :group 'org-properties
  2929. :version "24.1"
  2930. :type '(alist :key-type (string :tag "Property")
  2931. :value-type (function :tag "Function")))
  2932. (defcustom org-use-property-inheritance nil
  2933. "Non-nil means properties apply also for sublevels.
  2934. This setting is chiefly used during property searches. Turning it on can
  2935. cause significant overhead when doing a search, which is why it is not
  2936. on by default.
  2937. When nil, only the properties directly given in the current entry count.
  2938. When t, every property is inherited. The value may also be a list of
  2939. properties that should have inheritance, or a regular expression matching
  2940. properties that should be inherited.
  2941. However, note that some special properties use inheritance under special
  2942. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2943. and the properties ending in \"_ALL\" when they are used as descriptor
  2944. for valid values of a property.
  2945. Note for programmers:
  2946. When querying an entry with `org-entry-get', you can control if inheritance
  2947. should be used. By default, `org-entry-get' looks only at the local
  2948. properties. You can request inheritance by setting the inherit argument
  2949. to t (to force inheritance) or to `selective' (to respect the setting
  2950. in this variable)."
  2951. :group 'org-properties
  2952. :type '(choice
  2953. (const :tag "Not" nil)
  2954. (const :tag "Always" t)
  2955. (repeat :tag "Specific properties" (string :tag "Property"))
  2956. (regexp :tag "Properties matched by regexp")))
  2957. (defun org-property-inherit-p (property)
  2958. "Check if PROPERTY is one that should be inherited."
  2959. (cond
  2960. ((eq org-use-property-inheritance t) t)
  2961. ((not org-use-property-inheritance) nil)
  2962. ((stringp org-use-property-inheritance)
  2963. (string-match org-use-property-inheritance property))
  2964. ((listp org-use-property-inheritance)
  2965. (member property org-use-property-inheritance))
  2966. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2967. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2968. "The default column format, if no other format has been defined.
  2969. This variable can be set on the per-file basis by inserting a line
  2970. #+COLUMNS: %25ITEM ....."
  2971. :group 'org-properties
  2972. :type 'string)
  2973. (defcustom org-columns-ellipses ".."
  2974. "The ellipses to be used when a field in column view is truncated.
  2975. When this is the empty string, as many characters as possible are shown,
  2976. but then there will be no visual indication that the field has been truncated.
  2977. When this is a string of length N, the last N characters of a truncated
  2978. field are replaced by this string. If the column is narrower than the
  2979. ellipses string, only part of the ellipses string will be shown."
  2980. :group 'org-properties
  2981. :type 'string)
  2982. (defcustom org-columns-modify-value-for-display-function nil
  2983. "Function that modifies values for display in column view.
  2984. For example, it can be used to cut out a certain part from a time stamp.
  2985. The function must take 2 arguments:
  2986. column-title The title of the column (*not* the property name)
  2987. value The value that should be modified.
  2988. The function should return the value that should be displayed,
  2989. or nil if the normal value should be used."
  2990. :group 'org-properties
  2991. :type 'function)
  2992. (defcustom org-effort-property "Effort"
  2993. "The property that is being used to keep track of effort estimates.
  2994. Effort estimates given in this property need to have the format H:MM."
  2995. :group 'org-properties
  2996. :group 'org-progress
  2997. :type '(string :tag "Property"))
  2998. (defconst org-global-properties-fixed
  2999. '(("VISIBILITY_ALL" . "folded children content all")
  3000. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3001. "List of property/value pairs that can be inherited by any entry.
  3002. These are fixed values, for the preset properties. The user variable
  3003. that can be used to add to this list is `org-global-properties'.
  3004. The entries in this list are cons cells where the car is a property
  3005. name and cdr is a string with the value. If the value represents
  3006. multiple items like an \"_ALL\" property, separate the items by
  3007. spaces.")
  3008. (defcustom org-global-properties nil
  3009. "List of property/value pairs that can be inherited by any entry.
  3010. This list will be combined with the constant `org-global-properties-fixed'.
  3011. The entries in this list are cons cells where the car is a property
  3012. name and cdr is a string with the value.
  3013. You can set buffer-local values for the same purpose in the variable
  3014. `org-file-properties' this by adding lines like
  3015. #+PROPERTY: NAME VALUE"
  3016. :group 'org-properties
  3017. :type '(repeat
  3018. (cons (string :tag "Property")
  3019. (string :tag "Value"))))
  3020. (defvar org-file-properties nil
  3021. "List of property/value pairs that can be inherited by any entry.
  3022. Valid for the current buffer.
  3023. This variable is populated from #+PROPERTY lines.")
  3024. (make-variable-buffer-local 'org-file-properties)
  3025. (defgroup org-agenda nil
  3026. "Options concerning agenda views in Org-mode."
  3027. :tag "Org Agenda"
  3028. :group 'org)
  3029. (defvar org-category nil
  3030. "Variable used by org files to set a category for agenda display.
  3031. Such files should use a file variable to set it, for example
  3032. # -*- mode: org; org-category: \"ELisp\"
  3033. or contain a special line
  3034. #+CATEGORY: ELisp
  3035. If the file does not specify a category, then file's base name
  3036. is used instead.")
  3037. (make-variable-buffer-local 'org-category)
  3038. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  3039. (defcustom org-agenda-files nil
  3040. "The files to be used for agenda display.
  3041. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3042. \\[org-remove-file]. You can also use customize to edit the list.
  3043. If an entry is a directory, all files in that directory that are matched by
  3044. `org-agenda-file-regexp' will be part of the file list.
  3045. If the value of the variable is not a list but a single file name, then
  3046. the list of agenda files is actually stored and maintained in that file, one
  3047. agenda file per line. In this file paths can be given relative to
  3048. `org-directory'. Tilde expansion and environment variable substitution
  3049. are also made."
  3050. :group 'org-agenda
  3051. :type '(choice
  3052. (repeat :tag "List of files and directories" file)
  3053. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3054. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3055. "Regular expression to match files for `org-agenda-files'.
  3056. If any element in the list in that variable contains a directory instead
  3057. of a normal file, all files in that directory that are matched by this
  3058. regular expression will be included."
  3059. :group 'org-agenda
  3060. :type 'regexp)
  3061. (defcustom org-agenda-text-search-extra-files nil
  3062. "List of extra files to be searched by text search commands.
  3063. These files will be search in addition to the agenda files by the
  3064. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3065. Note that these files will only be searched for text search commands,
  3066. not for the other agenda views like todo lists, tag searches or the weekly
  3067. agenda. This variable is intended to list notes and possibly archive files
  3068. that should also be searched by these two commands.
  3069. In fact, if the first element in the list is the symbol `agenda-archives',
  3070. than all archive files of all agenda files will be added to the search
  3071. scope."
  3072. :group 'org-agenda
  3073. :type '(set :greedy t
  3074. (const :tag "Agenda Archives" agenda-archives)
  3075. (repeat :inline t (file))))
  3076. (if (fboundp 'defvaralias)
  3077. (defvaralias 'org-agenda-multi-occur-extra-files
  3078. 'org-agenda-text-search-extra-files))
  3079. (defcustom org-agenda-skip-unavailable-files nil
  3080. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3081. A nil value means to remove them, after a query, from the list."
  3082. :group 'org-agenda
  3083. :type 'boolean)
  3084. (defcustom org-calendar-to-agenda-key [?c]
  3085. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3086. The command `org-calendar-goto-agenda' will be bound to this key. The
  3087. default is the character `c' because then `c' can be used to switch back and
  3088. forth between agenda and calendar."
  3089. :group 'org-agenda
  3090. :type 'sexp)
  3091. (defcustom org-calendar-insert-diary-entry-key [?i]
  3092. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3093. This option is irrelevant until `org-agenda-diary-file' has been configured
  3094. to point to an Org-mode file. When that is the case, the command
  3095. `org-agenda-diary-entry' will be bound to the key given here, by default
  3096. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3097. if you want to continue doing this, you need to change this to a different
  3098. key."
  3099. :group 'org-agenda
  3100. :type 'sexp)
  3101. (defcustom org-agenda-diary-file 'diary-file
  3102. "File to which to add new entries with the `i' key in agenda and calendar.
  3103. When this is the symbol `diary-file', the functionality in the Emacs
  3104. calendar will be used to add entries to the `diary-file'. But when this
  3105. points to a file, `org-agenda-diary-entry' will be used instead."
  3106. :group 'org-agenda
  3107. :type '(choice
  3108. (const :tag "The standard Emacs diary file" diary-file)
  3109. (file :tag "Special Org file diary entries")))
  3110. (eval-after-load "calendar"
  3111. '(progn
  3112. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3113. 'org-calendar-goto-agenda)
  3114. (add-hook 'calendar-mode-hook
  3115. (lambda ()
  3116. (unless (eq org-agenda-diary-file 'diary-file)
  3117. (define-key calendar-mode-map
  3118. org-calendar-insert-diary-entry-key
  3119. 'org-agenda-diary-entry))))))
  3120. (defgroup org-latex nil
  3121. "Options for embedding LaTeX code into Org-mode."
  3122. :tag "Org LaTeX"
  3123. :group 'org)
  3124. (defcustom org-format-latex-options
  3125. '(:foreground default :background default :scale 1.0
  3126. :html-foreground "Black" :html-background "Transparent"
  3127. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3128. "Options for creating images from LaTeX fragments.
  3129. This is a property list with the following properties:
  3130. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3131. `default' means use the foreground of the default face.
  3132. `auto' means use the foreground from the text face.
  3133. :background the background color, or \"Transparent\".
  3134. `default' means use the background of the default face.
  3135. `auto' means use the background from the text face.
  3136. :scale a scaling factor for the size of the images, to get more pixels
  3137. :html-foreground, :html-background, :html-scale
  3138. the same numbers for HTML export.
  3139. :matchers a list indicating which matchers should be used to
  3140. find LaTeX fragments. Valid members of this list are:
  3141. \"begin\" find environments
  3142. \"$1\" find single characters surrounded by $.$
  3143. \"$\" find math expressions surrounded by $...$
  3144. \"$$\" find math expressions surrounded by $$....$$
  3145. \"\\(\" find math expressions surrounded by \\(...\\)
  3146. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  3147. :group 'org-latex
  3148. :type 'plist)
  3149. (defcustom org-format-latex-signal-error t
  3150. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3151. When nil, just push out a message."
  3152. :group 'org-latex
  3153. :version "24.1"
  3154. :type 'boolean)
  3155. (defcustom org-latex-to-mathml-jar-file nil
  3156. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3157. Use this to specify additional executable file say a jar file.
  3158. When using MathToWeb as the converter, specify the full-path to
  3159. your mathtoweb.jar file."
  3160. :group 'org-latex
  3161. :version "24.1"
  3162. :type '(choice
  3163. (const :tag "None" nil)
  3164. (file :tag "JAR file" :must-match t)))
  3165. (defcustom org-latex-to-mathml-convert-command nil
  3166. "Command to convert LaTeX fragments to MathML.
  3167. Replace format-specifiers in the command as noted below and use
  3168. `shell-command' to convert LaTeX to MathML.
  3169. %j: Executable file in fully expanded form as specified by
  3170. `org-latex-to-mathml-jar-file'.
  3171. %I: Input LaTeX file in fully expanded form
  3172. %o: Output MathML file
  3173. This command is used by `org-create-math-formula'.
  3174. When using MathToWeb as the converter, set this to
  3175. \"java -jar %j -unicode -force -df %o %I\"."
  3176. :group 'org-latex
  3177. :version "24.1"
  3178. :type '(choice
  3179. (const :tag "None" nil)
  3180. (string :tag "\nShell command")))
  3181. (defcustom org-latex-create-formula-image-program 'dvipng
  3182. "Program to convert LaTeX fragments with.
  3183. dvipng Process the LaTeX fragments to dvi file, then convert
  3184. dvi files to png files using dvipng.
  3185. This will also include processing of non-math environments.
  3186. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3187. to convert pdf files to png files"
  3188. :group 'org-latex
  3189. :version "24.1"
  3190. :type '(choice
  3191. (const :tag "dvipng" dvipng)
  3192. (const :tag "imagemagick" imagemagick)))
  3193. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3194. "Path to store latex preview images.
  3195. A relative path here creates many directories relative to the
  3196. processed org files paths. An absolute path puts all preview
  3197. images at the same place."
  3198. :group 'org-latex
  3199. :version "24.3"
  3200. :type 'string)
  3201. (defun org-format-latex-mathml-available-p ()
  3202. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3203. (save-match-data
  3204. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3205. org-latex-to-mathml-convert-command)
  3206. (let ((executable (car (split-string
  3207. org-latex-to-mathml-convert-command))))
  3208. (when (executable-find executable)
  3209. (if (string-match
  3210. "%j" org-latex-to-mathml-convert-command)
  3211. (file-readable-p org-latex-to-mathml-jar-file)
  3212. t))))))
  3213. (defcustom org-format-latex-header "\\documentclass{article}
  3214. \\usepackage[usenames]{color}
  3215. \\usepackage{amsmath}
  3216. \\usepackage[mathscr]{eucal}
  3217. \\pagestyle{empty} % do not remove
  3218. \[PACKAGES]
  3219. \[DEFAULT-PACKAGES]
  3220. % The settings below are copied from fullpage.sty
  3221. \\setlength{\\textwidth}{\\paperwidth}
  3222. \\addtolength{\\textwidth}{-3cm}
  3223. \\setlength{\\oddsidemargin}{1.5cm}
  3224. \\addtolength{\\oddsidemargin}{-2.54cm}
  3225. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3226. \\setlength{\\textheight}{\\paperheight}
  3227. \\addtolength{\\textheight}{-\\headheight}
  3228. \\addtolength{\\textheight}{-\\headsep}
  3229. \\addtolength{\\textheight}{-\\footskip}
  3230. \\addtolength{\\textheight}{-3cm}
  3231. \\setlength{\\topmargin}{1.5cm}
  3232. \\addtolength{\\topmargin}{-2.54cm}"
  3233. "The document header used for processing LaTeX fragments.
  3234. It is imperative that this header make sure that no page number
  3235. appears on the page. The package defined in the variables
  3236. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3237. will either replace the placeholder \"[PACKAGES]\" in this
  3238. header, or they will be appended."
  3239. :group 'org-latex
  3240. :type 'string)
  3241. (defun org-set-packages-alist (var val)
  3242. "Set the packages alist and make sure it has 3 elements per entry."
  3243. (set var (mapcar (lambda (x)
  3244. (if (and (consp x) (= (length x) 2))
  3245. (list (car x) (nth 1 x) t)
  3246. x))
  3247. val)))
  3248. (defun org-get-packages-alist (var)
  3249. "Get the packages alist and make sure it has 3 elements per entry."
  3250. (mapcar (lambda (x)
  3251. (if (and (consp x) (= (length x) 2))
  3252. (list (car x) (nth 1 x) t)
  3253. x))
  3254. (default-value var)))
  3255. (defcustom org-latex-default-packages-alist
  3256. '(("AUTO" "inputenc" t)
  3257. ("T1" "fontenc" t)
  3258. ("" "fixltx2e" nil)
  3259. ("" "graphicx" t)
  3260. ("" "longtable" nil)
  3261. ("" "float" nil)
  3262. ("" "wrapfig" nil)
  3263. ("" "soul" t)
  3264. ("" "textcomp" t)
  3265. ("" "marvosym" t)
  3266. ("" "wasysym" t)
  3267. ("" "latexsym" t)
  3268. ("" "amssymb" t)
  3269. ("" "hyperref" nil)
  3270. "\\tolerance=1000")
  3271. "Alist of default packages to be inserted in the header.
  3272. Change this only if one of the packages here causes an
  3273. incompatibility with another package you are using.
  3274. The packages in this list are needed by one part or another of
  3275. Org mode to function properly:
  3276. - inputenc, fontenc: for basic font and character selection
  3277. - textcomp, marvosymb, wasysym, latexsym, amssym: for various
  3278. symbols used for interpreting the entities in `org-entities'.
  3279. You can skip some of these packages if you don't use any of the
  3280. symbols in it.
  3281. - graphicx: for including images
  3282. - float, wrapfig: for figure placement
  3283. - longtable: for long tables
  3284. - hyperref: for cross references
  3285. Therefore you should not modify this variable unless you know
  3286. what you are doing. The one reason to change it anyway is that
  3287. you might be loading some other package that conflicts with one
  3288. of the default packages. Each cell is of the format
  3289. \( \"options\" \"package\" snippet-flag). If SNIPPET-FLAG is t,
  3290. the package also needs to be included when compiling LaTeX
  3291. snippets into images for inclusion into non-LaTeX output."
  3292. :group 'org-latex
  3293. :group 'org-export-latex
  3294. :set 'org-set-packages-alist
  3295. :get 'org-get-packages-alist
  3296. :version "24.1"
  3297. :type '(repeat
  3298. (choice
  3299. (list :tag "options/package pair"
  3300. (string :tag "options")
  3301. (string :tag "package")
  3302. (boolean :tag "Snippet"))
  3303. (string :tag "A line of LaTeX"))))
  3304. (defcustom org-latex-packages-alist nil
  3305. "Alist of packages to be inserted in every LaTeX header.
  3306. These will be inserted after `org-latex-default-packages-alist'.
  3307. Each cell is of the format:
  3308. \(\"options\" \"package\" snippet-flag)
  3309. SNIPPET-FLAG, when t, indicates that this package is also needed
  3310. when turning LaTeX snippets into images for inclusion into
  3311. non-LaTeX output.
  3312. Make sure that you only list packages here which:
  3313. - you want in every file
  3314. - do not conflict with the setup in `org-format-latex-header'.
  3315. - do not conflict with the default packages in
  3316. `org-latex-default-packages-alist'."
  3317. :group 'org-latex
  3318. :group 'org-export-latex
  3319. :set 'org-set-packages-alist
  3320. :get 'org-get-packages-alist
  3321. :type '(repeat
  3322. (choice
  3323. (list :tag "options/package pair"
  3324. (string :tag "options")
  3325. (string :tag "package")
  3326. (boolean :tag "Snippet"))
  3327. (string :tag "A line of LaTeX"))))
  3328. (defgroup org-appearance nil
  3329. "Settings for Org-mode appearance."
  3330. :tag "Org Appearance"
  3331. :group 'org)
  3332. (defcustom org-level-color-stars-only nil
  3333. "Non-nil means fontify only the stars in each headline.
  3334. When nil, the entire headline is fontified.
  3335. Changing it requires restart of `font-lock-mode' to become effective
  3336. also in regions already fontified."
  3337. :group 'org-appearance
  3338. :type 'boolean)
  3339. (defcustom org-hide-leading-stars nil
  3340. "Non-nil means hide the first N-1 stars in a headline.
  3341. This works by using the face `org-hide' for these stars. This
  3342. face is white for a light background, and black for a dark
  3343. background. You may have to customize the face `org-hide' to
  3344. make this work.
  3345. Changing it requires restart of `font-lock-mode' to become effective
  3346. also in regions already fontified.
  3347. You may also set this on a per-file basis by adding one of the following
  3348. lines to the buffer:
  3349. #+STARTUP: hidestars
  3350. #+STARTUP: showstars"
  3351. :group 'org-appearance
  3352. :type 'boolean)
  3353. (defcustom org-hidden-keywords nil
  3354. "List of symbols corresponding to keywords to be hidden the org buffer.
  3355. For example, a value '(title) for this list will make the document's title
  3356. appear in the buffer without the initial #+TITLE: keyword."
  3357. :group 'org-appearance
  3358. :version "24.1"
  3359. :type '(set (const :tag "#+AUTHOR" author)
  3360. (const :tag "#+DATE" date)
  3361. (const :tag "#+EMAIL" email)
  3362. (const :tag "#+TITLE" title)))
  3363. (defcustom org-custom-properties nil
  3364. "List of properties (as strings) with a special meaning.
  3365. The default use of these custom properties is to let the user
  3366. hide them with `org-toggle-custom-properties-visibility'."
  3367. :group 'org-properties
  3368. :group 'org-appearance
  3369. :version "24.3"
  3370. :type '(repeat (string :tag "Property Name")))
  3371. (defcustom org-fontify-done-headline nil
  3372. "Non-nil means change the face of a headline if it is marked DONE.
  3373. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3374. When this is non-nil, the headline after the keyword is set to the
  3375. `org-headline-done' as an additional indication."
  3376. :group 'org-appearance
  3377. :type 'boolean)
  3378. (defcustom org-fontify-emphasized-text t
  3379. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3380. Changing this variable requires a restart of Emacs to take effect."
  3381. :group 'org-appearance
  3382. :type 'boolean)
  3383. (defcustom org-fontify-whole-heading-line nil
  3384. "Non-nil means fontify the whole line for headings.
  3385. This is useful when setting a background color for the
  3386. org-level-* faces."
  3387. :group 'org-appearance
  3388. :type 'boolean)
  3389. (defcustom org-hide-emphasis-markers nil
  3390. "Non-nil mean font-lock should hide the emphasis marker characters."
  3391. :group 'org-appearance
  3392. :type 'boolean)
  3393. (defcustom org-pretty-entities nil
  3394. "Non-nil means show entities as UTF8 characters.
  3395. When nil, the \\name form remains in the buffer."
  3396. :group 'org-appearance
  3397. :version "24.1"
  3398. :type 'boolean)
  3399. (defcustom org-pretty-entities-include-sub-superscripts t
  3400. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3401. :group 'org-appearance
  3402. :version "24.1"
  3403. :type 'boolean)
  3404. (defvar org-emph-re nil
  3405. "Regular expression for matching emphasis.
  3406. After a match, the match groups contain these elements:
  3407. 0 The match of the full regular expression, including the characters
  3408. before and after the proper match
  3409. 1 The character before the proper match, or empty at beginning of line
  3410. 2 The proper match, including the leading and trailing markers
  3411. 3 The leading marker like * or /, indicating the type of highlighting
  3412. 4 The text between the emphasis markers, not including the markers
  3413. 5 The character after the match, empty at the end of a line")
  3414. (defvar org-verbatim-re nil
  3415. "Regular expression for matching verbatim text.")
  3416. (defvar org-emphasis-regexp-components) ; defined just below
  3417. (defvar org-emphasis-alist) ; defined just below
  3418. (defun org-set-emph-re (var val)
  3419. "Set variable and compute the emphasis regular expression."
  3420. (set var val)
  3421. (when (and (boundp 'org-emphasis-alist)
  3422. (boundp 'org-emphasis-regexp-components)
  3423. org-emphasis-alist org-emphasis-regexp-components)
  3424. (let* ((e org-emphasis-regexp-components)
  3425. (pre (car e))
  3426. (post (nth 1 e))
  3427. (border (nth 2 e))
  3428. (body (nth 3 e))
  3429. (nl (nth 4 e))
  3430. (body1 (concat body "*?"))
  3431. (markers (mapconcat 'car org-emphasis-alist ""))
  3432. (vmarkers (mapconcat
  3433. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3434. org-emphasis-alist "")))
  3435. ;; make sure special characters appear at the right position in the class
  3436. (if (string-match "\\^" markers)
  3437. (setq markers (concat (replace-match "" t t markers) "^")))
  3438. (if (string-match "-" markers)
  3439. (setq markers (concat (replace-match "" t t markers) "-")))
  3440. (if (string-match "\\^" vmarkers)
  3441. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3442. (if (string-match "-" vmarkers)
  3443. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3444. (if (> nl 0)
  3445. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3446. (int-to-string nl) "\\}")))
  3447. ;; Make the regexp
  3448. (setq org-emph-re
  3449. (concat "\\([" pre "]\\|^\\)"
  3450. "\\("
  3451. "\\([" markers "]\\)"
  3452. "\\("
  3453. "[^" border "]\\|"
  3454. "[^" border "]"
  3455. body1
  3456. "[^" border "]"
  3457. "\\)"
  3458. "\\3\\)"
  3459. "\\([" post "]\\|$\\)"))
  3460. (setq org-verbatim-re
  3461. (concat "\\([" pre "]\\|^\\)"
  3462. "\\("
  3463. "\\([" vmarkers "]\\)"
  3464. "\\("
  3465. "[^" border "]\\|"
  3466. "[^" border "]"
  3467. body1
  3468. "[^" border "]"
  3469. "\\)"
  3470. "\\3\\)"
  3471. "\\([" post "]\\|$\\)")))))
  3472. (defcustom org-emphasis-regexp-components
  3473. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3474. "Components used to build the regular expression for emphasis.
  3475. This is a list with five entries. Terminology: In an emphasis string
  3476. like \" *strong word* \", we call the initial space PREMATCH, the final
  3477. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3478. and \"trong wor\" is the body. The different components in this variable
  3479. specify what is allowed/forbidden in each part:
  3480. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3481. post Chars allowed as postmatch. End of line will be allowed too.
  3482. border The chars *forbidden* as border characters.
  3483. body-regexp A regexp like \".\" to match a body character. Don't use
  3484. non-shy groups here, and don't allow newline here.
  3485. newline The maximum number of newlines allowed in an emphasis exp.
  3486. Use customize to modify this, or restart Emacs after changing it."
  3487. :group 'org-appearance
  3488. :set 'org-set-emph-re
  3489. :type '(list
  3490. (sexp :tag "Allowed chars in pre ")
  3491. (sexp :tag "Allowed chars in post ")
  3492. (sexp :tag "Forbidden chars in border ")
  3493. (sexp :tag "Regexp for body ")
  3494. (integer :tag "number of newlines allowed")
  3495. (option (boolean :tag "Please ignore this button"))))
  3496. (defcustom org-emphasis-alist
  3497. `(("*" bold "<b>" "</b>")
  3498. ("/" italic "<i>" "</i>")
  3499. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3500. ("=" org-code "<code>" "</code>" verbatim)
  3501. ("~" org-verbatim "<code>" "</code>" verbatim)
  3502. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3503. "<del>" "</del>")
  3504. )
  3505. "Special syntax for emphasized text.
  3506. Text starting and ending with a special character will be emphasized, for
  3507. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3508. characters, the face to be used by font-lock for highlighting in Org-mode
  3509. Emacs buffers, and the HTML tags to be used for this.
  3510. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3511. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3512. Use customize to modify this, or restart Emacs after changing it."
  3513. :group 'org-appearance
  3514. :set 'org-set-emph-re
  3515. :type '(repeat
  3516. (list
  3517. (string :tag "Marker character")
  3518. (choice
  3519. (face :tag "Font-lock-face")
  3520. (plist :tag "Face property list"))
  3521. (string :tag "HTML start tag")
  3522. (string :tag "HTML end tag")
  3523. (option (const verbatim)))))
  3524. (defvar org-syntax-table
  3525. (let ((st (make-syntax-table)))
  3526. (mapc (lambda(c) (modify-syntax-entry
  3527. (string-to-char (car c)) "w p" st))
  3528. org-emphasis-alist)
  3529. st))
  3530. (defvar org-protecting-blocks
  3531. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3532. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3533. This is needed for font-lock setup.")
  3534. ;;; Miscellaneous options
  3535. (defgroup org-completion nil
  3536. "Completion in Org-mode."
  3537. :tag "Org Completion"
  3538. :group 'org)
  3539. (defcustom org-completion-use-ido nil
  3540. "Non-nil means use ido completion wherever possible.
  3541. Note that `ido-mode' must be active for this variable to be relevant.
  3542. If you decide to turn this variable on, you might well want to turn off
  3543. `org-outline-path-complete-in-steps'.
  3544. See also `org-completion-use-iswitchb'."
  3545. :group 'org-completion
  3546. :type 'boolean)
  3547. (defcustom org-completion-use-iswitchb nil
  3548. "Non-nil means use iswitchb completion wherever possible.
  3549. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3550. If you decide to turn this variable on, you might well want to turn off
  3551. `org-outline-path-complete-in-steps'.
  3552. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3553. :group 'org-completion
  3554. :type 'boolean)
  3555. (defcustom org-completion-fallback-command 'hippie-expand
  3556. "The expansion command called by \\[pcomplete] in normal context.
  3557. Normal means, no org-mode-specific context."
  3558. :group 'org-completion
  3559. :type 'function)
  3560. ;;; Functions and variables from their packages
  3561. ;; Declared here to avoid compiler warnings
  3562. ;; XEmacs only
  3563. (defvar outline-mode-menu-heading)
  3564. (defvar outline-mode-menu-show)
  3565. (defvar outline-mode-menu-hide)
  3566. (defvar zmacs-regions) ; XEmacs regions
  3567. ;; Emacs only
  3568. (defvar mark-active)
  3569. ;; Various packages
  3570. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3571. (declare-function calendar-forward-day "cal-move" (arg))
  3572. (declare-function calendar-goto-date "cal-move" (date))
  3573. (declare-function calendar-goto-today "cal-move" ())
  3574. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3575. (defvar calc-embedded-close-formula)
  3576. (defvar calc-embedded-open-formula)
  3577. (declare-function cdlatex-tab "ext:cdlatex" ())
  3578. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3579. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3580. (defvar font-lock-unfontify-region-function)
  3581. (declare-function iswitchb-read-buffer "iswitchb"
  3582. (prompt &optional default require-match start matches-set))
  3583. (defvar iswitchb-temp-buflist)
  3584. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3585. (defvar org-agenda-tags-todo-honor-ignore-options)
  3586. (declare-function org-agenda-skip "org-agenda" ())
  3587. (declare-function
  3588. org-agenda-format-item "org-agenda"
  3589. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3590. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3591. (declare-function org-agenda-change-all-lines "org-agenda"
  3592. (newhead hdmarker &optional fixface just-this))
  3593. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3594. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3595. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3596. (beg end))
  3597. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3598. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3599. "org-agenda" (&optional end))
  3600. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3601. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3602. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3603. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3604. (declare-function org-indent-mode "org-indent" (&optional arg))
  3605. (declare-function parse-time-string "parse-time" (string))
  3606. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3607. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3608. (defvar remember-data-file)
  3609. (defvar texmathp-why)
  3610. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3611. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3612. (defvar org-latex-regexps)
  3613. ;;; Autoload and prepare some org modules
  3614. ;; Some table stuff that needs to be defined here, because it is used
  3615. ;; by the functions setting up org-mode or checking for table context.
  3616. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3617. "Detect an org-type or table-type table.")
  3618. (defconst org-table-line-regexp "^[ \t]*|"
  3619. "Detect an org-type table line.")
  3620. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3621. "Detect an org-type table line.")
  3622. (defconst org-table-hline-regexp "^[ \t]*|-"
  3623. "Detect an org-type table hline.")
  3624. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3625. "Detect a table-type table hline.")
  3626. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3627. "Detect the first line outside a table when searching from within it.
  3628. This works for both table types.")
  3629. ;; Autoload the functions in org-table.el that are needed by functions here.
  3630. (eval-and-compile
  3631. (org-autoload "org-table"
  3632. '(org-table-begin org-table-blank-field org-table-end)))
  3633. ;;;###autoload
  3634. (defun turn-on-orgtbl ()
  3635. "Unconditionally turn on `orgtbl-mode'."
  3636. (require 'org-table)
  3637. (orgtbl-mode 1))
  3638. (defun org-at-table-p (&optional table-type)
  3639. "Return t if the cursor is inside an org-type table.
  3640. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3641. (if org-enable-table-editor
  3642. (save-excursion
  3643. (beginning-of-line 1)
  3644. (looking-at (if table-type org-table-any-line-regexp
  3645. org-table-line-regexp)))
  3646. nil))
  3647. (defsubst org-table-p () (org-at-table-p))
  3648. (defun org-at-table.el-p ()
  3649. "Return t if and only if we are at a table.el table."
  3650. (and (org-at-table-p 'any)
  3651. (save-excursion
  3652. (goto-char (org-table-begin 'any))
  3653. (looking-at org-table1-hline-regexp))))
  3654. (defun org-table-recognize-table.el ()
  3655. "If there is a table.el table nearby, recognize it and move into it."
  3656. (if org-table-tab-recognizes-table.el
  3657. (if (org-at-table.el-p)
  3658. (progn
  3659. (beginning-of-line 1)
  3660. (if (looking-at org-table-dataline-regexp)
  3661. nil
  3662. (if (looking-at org-table1-hline-regexp)
  3663. (progn
  3664. (beginning-of-line 2)
  3665. (if (looking-at org-table-any-border-regexp)
  3666. (beginning-of-line -1)))))
  3667. (if (re-search-forward "|" (org-table-end t) t)
  3668. (progn
  3669. (require 'table)
  3670. (if (table--at-cell-p (point))
  3671. t
  3672. (message "recognizing table.el table...")
  3673. (table-recognize-table)
  3674. (message "recognizing table.el table...done")))
  3675. (error "This should not happen"))
  3676. t)
  3677. nil)
  3678. nil))
  3679. (defun org-at-table-hline-p ()
  3680. "Return t if the cursor is inside a hline in a table."
  3681. (if org-enable-table-editor
  3682. (save-excursion
  3683. (beginning-of-line 1)
  3684. (looking-at org-table-hline-regexp))
  3685. nil))
  3686. (defvar org-table-clean-did-remove-column nil)
  3687. (defun org-table-map-tables (function &optional quietly)
  3688. "Apply FUNCTION to the start of all tables in the buffer."
  3689. (save-excursion
  3690. (save-restriction
  3691. (widen)
  3692. (goto-char (point-min))
  3693. (while (re-search-forward org-table-any-line-regexp nil t)
  3694. (unless quietly
  3695. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3696. (beginning-of-line 1)
  3697. (when (and (looking-at org-table-line-regexp)
  3698. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3699. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3700. (save-excursion (funcall function))
  3701. (or (looking-at org-table-line-regexp)
  3702. (forward-char 1)))
  3703. (re-search-forward org-table-any-border-regexp nil 1))))
  3704. (unless quietly (message "Mapping tables: done")))
  3705. ;; Declare and autoload functions from ox.el and al.
  3706. (declare-function org-export-get-environment "ox"
  3707. (&optional backend subtreep ext-plist))
  3708. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  3709. ;; Declare and autoload functions from org-agenda.el
  3710. (eval-and-compile
  3711. (org-autoload "org-agenda"
  3712. '(org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3713. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3714. (declare-function org-clock-update-mode-line "org-clock" ())
  3715. (declare-function org-resolve-clocks "org-clock"
  3716. (&optional also-non-dangling-p prompt last-valid))
  3717. (defvar org-clock-start-time)
  3718. (defvar org-clock-marker (make-marker)
  3719. "Marker recording the last clock-in.")
  3720. (defvar org-clock-hd-marker (make-marker)
  3721. "Marker recording the last clock-in, but the headline position.")
  3722. (defvar org-clock-heading ""
  3723. "The heading of the current clock entry.")
  3724. (defun org-clock-is-active ()
  3725. "Return non-nil if clock is currently running.
  3726. The return value is actually the clock marker."
  3727. (marker-buffer org-clock-marker))
  3728. (eval-and-compile
  3729. (org-autoload "org-clock" '(org-clock-remove-overlays
  3730. org-clock-update-time-maybe
  3731. org-clocktable-shift)))
  3732. (defun org-check-running-clock ()
  3733. "Check if the current buffer contains the running clock.
  3734. If yes, offer to stop it and to save the buffer with the changes."
  3735. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3736. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3737. (buffer-name))))
  3738. (org-clock-out)
  3739. (when (y-or-n-p "Save changed buffer?")
  3740. (save-buffer))))
  3741. (defun org-clocktable-try-shift (dir n)
  3742. "Check if this line starts a clock table, if yes, shift the time block."
  3743. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3744. (org-clocktable-shift dir n)))
  3745. ;;;###autoload
  3746. (defun org-clock-persistence-insinuate ()
  3747. "Set up hooks for clock persistence."
  3748. (require 'org-clock)
  3749. (add-hook 'org-mode-hook 'org-clock-load)
  3750. (add-hook 'kill-emacs-hook 'org-clock-save))
  3751. ;; Define the variable already here, to make sure we have it.
  3752. (defvar org-indent-mode nil
  3753. "Non-nil if Org-Indent mode is enabled.
  3754. Use the command `org-indent-mode' to change this variable.")
  3755. ;; Autoload archiving code
  3756. ;; The stuff that is needed for cycling and tags has to be defined here.
  3757. (defgroup org-archive nil
  3758. "Options concerning archiving in Org-mode."
  3759. :tag "Org Archive"
  3760. :group 'org-structure)
  3761. (defcustom org-archive-location "%s_archive::"
  3762. "The location where subtrees should be archived.
  3763. The value of this variable is a string, consisting of two parts,
  3764. separated by a double-colon. The first part is a filename and
  3765. the second part is a headline.
  3766. When the filename is omitted, archiving happens in the same file.
  3767. %s in the filename will be replaced by the current file
  3768. name (without the directory part). Archiving to a different file
  3769. is useful to keep archived entries from contributing to the
  3770. Org-mode Agenda.
  3771. The archived entries will be filed as subtrees of the specified
  3772. headline. When the headline is omitted, the subtrees are simply
  3773. filed away at the end of the file, as top-level entries. Also in
  3774. the heading you can use %s to represent the file name, this can be
  3775. useful when using the same archive for a number of different files.
  3776. Here are a few examples:
  3777. \"%s_archive::\"
  3778. If the current file is Projects.org, archive in file
  3779. Projects.org_archive, as top-level trees. This is the default.
  3780. \"::* Archived Tasks\"
  3781. Archive in the current file, under the top-level headline
  3782. \"* Archived Tasks\".
  3783. \"~/org/archive.org::\"
  3784. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3785. \"~/org/archive.org::* From %s\"
  3786. Archive in file ~/org/archive.org (absolute path), under headlines
  3787. \"From FILENAME\" where file name is the current file name.
  3788. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3789. The \"datetree/\" string is special, signifying to archive
  3790. items to the datetree. Items are placed in either the CLOSED
  3791. date of the item, or the current date if there is no CLOSED date.
  3792. The heading will be a subentry to the current date. There doesn't
  3793. need to be a heading, but there always needs to be a slash after
  3794. datetree. For example, to store archived items directly in the
  3795. datetree, use \"~/org/datetree.org::datetree/\".
  3796. \"basement::** Finished Tasks\"
  3797. Archive in file ./basement (relative path), as level 3 trees
  3798. below the level 2 heading \"** Finished Tasks\".
  3799. You may set this option on a per-file basis by adding to the buffer a
  3800. line like
  3801. #+ARCHIVE: basement::** Finished Tasks
  3802. You may also define it locally for a subtree by setting an ARCHIVE property
  3803. in the entry. If such a property is found in an entry, or anywhere up
  3804. the hierarchy, it will be used."
  3805. :group 'org-archive
  3806. :type 'string)
  3807. (defcustom org-archive-tag "ARCHIVE"
  3808. "The tag that marks a subtree as archived.
  3809. An archived subtree does not open during visibility cycling, and does
  3810. not contribute to the agenda listings.
  3811. After changing this, font-lock must be restarted in the relevant buffers to
  3812. get the proper fontification."
  3813. :group 'org-archive
  3814. :group 'org-keywords
  3815. :type 'string)
  3816. (defcustom org-agenda-skip-archived-trees t
  3817. "Non-nil means the agenda will skip any items located in archived trees.
  3818. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3819. variable is no longer recommended, you should leave it at the value t.
  3820. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3821. :group 'org-archive
  3822. :group 'org-agenda-skip
  3823. :type 'boolean)
  3824. (defcustom org-columns-skip-archived-trees t
  3825. "Non-nil means ignore archived trees when creating column view."
  3826. :group 'org-archive
  3827. :group 'org-properties
  3828. :type 'boolean)
  3829. (defcustom org-cycle-open-archived-trees nil
  3830. "Non-nil means `org-cycle' will open archived trees.
  3831. An archived tree is a tree marked with the tag ARCHIVE.
  3832. When nil, archived trees will stay folded. You can still open them with
  3833. normal outline commands like `show-all', but not with the cycling commands."
  3834. :group 'org-archive
  3835. :group 'org-cycle
  3836. :type 'boolean)
  3837. (defcustom org-sparse-tree-open-archived-trees nil
  3838. "Non-nil means sparse tree construction shows matches in archived trees.
  3839. When nil, matches in these trees are highlighted, but the trees are kept in
  3840. collapsed state."
  3841. :group 'org-archive
  3842. :group 'org-sparse-trees
  3843. :type 'boolean)
  3844. (defcustom org-sparse-tree-default-date-type 'scheduled-or-deadline
  3845. "The default date type when building a sparse tree.
  3846. When this is nil, a date is a scheduled or a deadline timestamp.
  3847. Otherwise, these types are allowed:
  3848. all: all timestamps
  3849. active: only active timestamps (<...>)
  3850. inactive: only inactive timestamps (<...)
  3851. scheduled: only scheduled timestamps
  3852. deadline: only deadline timestamps"
  3853. :type '(choice (const :tag "Scheduled or deadline" 'scheduled-or-deadline)
  3854. (const :tag "All timestamps" all)
  3855. (const :tag "Only active timestamps" active)
  3856. (const :tag "Only inactive timestamps" inactive)
  3857. (const :tag "Only scheduled timestamps" scheduled)
  3858. (const :tag "Only deadline timestamps" deadline))
  3859. :version "24.3"
  3860. :group 'org-sparse-trees)
  3861. (defun org-cycle-hide-archived-subtrees (state)
  3862. "Re-hide all archived subtrees after a visibility state change."
  3863. (when (and (not org-cycle-open-archived-trees)
  3864. (not (memq state '(overview folded))))
  3865. (save-excursion
  3866. (let* ((globalp (memq state '(contents all)))
  3867. (beg (if globalp (point-min) (point)))
  3868. (end (if globalp (point-max) (org-end-of-subtree t))))
  3869. (org-hide-archived-subtrees beg end)
  3870. (goto-char beg)
  3871. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3872. (message "%s" (substitute-command-keys
  3873. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3874. (defun org-force-cycle-archived ()
  3875. "Cycle subtree even if it is archived."
  3876. (interactive)
  3877. (setq this-command 'org-cycle)
  3878. (let ((org-cycle-open-archived-trees t))
  3879. (call-interactively 'org-cycle)))
  3880. (defun org-hide-archived-subtrees (beg end)
  3881. "Re-hide all archived subtrees after a visibility state change."
  3882. (save-excursion
  3883. (let* ((re (concat ":" org-archive-tag ":")))
  3884. (goto-char beg)
  3885. (while (re-search-forward re end t)
  3886. (when (org-at-heading-p)
  3887. (org-flag-subtree t)
  3888. (org-end-of-subtree t))))))
  3889. (declare-function outline-end-of-heading "outline" ())
  3890. (declare-function outline-flag-region "outline" (from to flag))
  3891. (defun org-flag-subtree (flag)
  3892. (save-excursion
  3893. (org-back-to-heading t)
  3894. (outline-end-of-heading)
  3895. (outline-flag-region (point)
  3896. (progn (org-end-of-subtree t) (point))
  3897. flag)))
  3898. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3899. (eval-and-compile
  3900. (org-autoload "org-archive"
  3901. '(org-add-archive-files)))
  3902. ;; Autoload Column View Code
  3903. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  3904. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  3905. (declare-function org-columns-compute "org-colview" (property))
  3906. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3907. '(org-columns-number-to-string
  3908. org-columns-get-format-and-top-level
  3909. org-columns-compute
  3910. org-columns-remove-overlays))
  3911. ;; Autoload ID code
  3912. (declare-function org-id-store-link "org-id")
  3913. (declare-function org-id-locations-load "org-id")
  3914. (declare-function org-id-locations-save "org-id")
  3915. (defvar org-id-track-globally)
  3916. (org-autoload "org-id"
  3917. '(org-id-new
  3918. org-id-copy
  3919. org-id-get-with-outline-path-completion
  3920. org-id-get-with-outline-drilling))
  3921. ;;; Variables for pre-computed regular expressions, all buffer local
  3922. (defvar org-drawer-regexp "^[ \t]*:PROPERTIES:[ \t]*$"
  3923. "Matches first line of a hidden block.")
  3924. (make-variable-buffer-local 'org-drawer-regexp)
  3925. (defvar org-todo-regexp nil
  3926. "Matches any of the TODO state keywords.")
  3927. (make-variable-buffer-local 'org-todo-regexp)
  3928. (defvar org-not-done-regexp nil
  3929. "Matches any of the TODO state keywords except the last one.")
  3930. (make-variable-buffer-local 'org-not-done-regexp)
  3931. (defvar org-not-done-heading-regexp nil
  3932. "Matches a TODO headline that is not done.")
  3933. (make-variable-buffer-local 'org-not-done-regexp)
  3934. (defvar org-todo-line-regexp nil
  3935. "Matches a headline and puts TODO state into group 2 if present.")
  3936. (make-variable-buffer-local 'org-todo-line-regexp)
  3937. (defvar org-complex-heading-regexp nil
  3938. "Matches a headline and puts everything into groups:
  3939. group 1: the stars
  3940. group 2: The todo keyword, maybe
  3941. group 3: Priority cookie
  3942. group 4: True headline
  3943. group 5: Tags")
  3944. (make-variable-buffer-local 'org-complex-heading-regexp)
  3945. (defvar org-complex-heading-regexp-format nil
  3946. "Printf format to make regexp to match an exact headline.
  3947. This regexp will match the headline of any node which has the
  3948. exact headline text that is put into the format, but may have any
  3949. TODO state, priority and tags.")
  3950. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3951. (defvar org-todo-line-tags-regexp nil
  3952. "Matches a headline and puts TODO state into group 2 if present.
  3953. Also put tags into group 4 if tags are present.")
  3954. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3955. (defvar org-ds-keyword-length 12
  3956. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  3957. (make-variable-buffer-local 'org-ds-keyword-length)
  3958. (defvar org-deadline-regexp nil
  3959. "Matches the DEADLINE keyword.")
  3960. (make-variable-buffer-local 'org-deadline-regexp)
  3961. (defvar org-deadline-time-regexp nil
  3962. "Matches the DEADLINE keyword together with a time stamp.")
  3963. (make-variable-buffer-local 'org-deadline-time-regexp)
  3964. (defvar org-deadline-line-regexp nil
  3965. "Matches the DEADLINE keyword and the rest of the line.")
  3966. (make-variable-buffer-local 'org-deadline-line-regexp)
  3967. (defvar org-scheduled-regexp nil
  3968. "Matches the SCHEDULED keyword.")
  3969. (make-variable-buffer-local 'org-scheduled-regexp)
  3970. (defvar org-scheduled-time-regexp nil
  3971. "Matches the SCHEDULED keyword together with a time stamp.")
  3972. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3973. (defvar org-closed-time-regexp nil
  3974. "Matches the CLOSED keyword together with a time stamp.")
  3975. (make-variable-buffer-local 'org-closed-time-regexp)
  3976. (defvar org-keyword-time-regexp nil
  3977. "Matches any of the 4 keywords, together with the time stamp.")
  3978. (make-variable-buffer-local 'org-keyword-time-regexp)
  3979. (defvar org-keyword-time-not-clock-regexp nil
  3980. "Matches any of the 3 keywords, together with the time stamp.")
  3981. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3982. (defvar org-maybe-keyword-time-regexp nil
  3983. "Matches a timestamp, possibly preceded by a keyword.")
  3984. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3985. (defvar org-all-time-keywords nil
  3986. "List of time keywords.")
  3987. (make-variable-buffer-local 'org-all-time-keywords)
  3988. (defconst org-plain-time-of-day-regexp
  3989. (concat
  3990. "\\(\\<[012]?[0-9]"
  3991. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3992. "\\(--?"
  3993. "\\(\\<[012]?[0-9]"
  3994. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3995. "\\)?")
  3996. "Regular expression to match a plain time or time range.
  3997. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3998. groups carry important information:
  3999. 0 the full match
  4000. 1 the first time, range or not
  4001. 8 the second time, if it is a range.")
  4002. (defconst org-plain-time-extension-regexp
  4003. (concat
  4004. "\\(\\<[012]?[0-9]"
  4005. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4006. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4007. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4008. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4009. groups carry important information:
  4010. 0 the full match
  4011. 7 hours of duration
  4012. 9 minutes of duration")
  4013. (defconst org-stamp-time-of-day-regexp
  4014. (concat
  4015. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4016. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4017. "\\(--?"
  4018. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4019. "Regular expression to match a timestamp time or time range.
  4020. After a match, the following groups carry important information:
  4021. 0 the full match
  4022. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4023. 2 the first time, range or not
  4024. 4 the second time, if it is a range.")
  4025. (defconst org-startup-options
  4026. '(("fold" org-startup-folded t)
  4027. ("overview" org-startup-folded t)
  4028. ("nofold" org-startup-folded nil)
  4029. ("showall" org-startup-folded nil)
  4030. ("showeverything" org-startup-folded showeverything)
  4031. ("content" org-startup-folded content)
  4032. ("indent" org-startup-indented t)
  4033. ("noindent" org-startup-indented nil)
  4034. ("hidestars" org-hide-leading-stars t)
  4035. ("showstars" org-hide-leading-stars nil)
  4036. ("odd" org-odd-levels-only t)
  4037. ("oddeven" org-odd-levels-only nil)
  4038. ("align" org-startup-align-all-tables t)
  4039. ("noalign" org-startup-align-all-tables nil)
  4040. ("inlineimages" org-startup-with-inline-images t)
  4041. ("noinlineimages" org-startup-with-inline-images nil)
  4042. ("customtime" org-display-custom-times t)
  4043. ("logdone" org-log-done time)
  4044. ("lognotedone" org-log-done note)
  4045. ("nologdone" org-log-done nil)
  4046. ("lognoteclock-out" org-log-note-clock-out t)
  4047. ("nolognoteclock-out" org-log-note-clock-out nil)
  4048. ("logrepeat" org-log-repeat state)
  4049. ("lognoterepeat" org-log-repeat note)
  4050. ("logdrawer" org-log-into-drawer t)
  4051. ("nologdrawer" org-log-into-drawer nil)
  4052. ("logstatesreversed" org-log-states-order-reversed t)
  4053. ("nologstatesreversed" org-log-states-order-reversed nil)
  4054. ("nologrepeat" org-log-repeat nil)
  4055. ("logreschedule" org-log-reschedule time)
  4056. ("lognotereschedule" org-log-reschedule note)
  4057. ("nologreschedule" org-log-reschedule nil)
  4058. ("logredeadline" org-log-redeadline time)
  4059. ("lognoteredeadline" org-log-redeadline note)
  4060. ("nologredeadline" org-log-redeadline nil)
  4061. ("logrefile" org-log-refile time)
  4062. ("lognoterefile" org-log-refile note)
  4063. ("nologrefile" org-log-refile nil)
  4064. ("fninline" org-footnote-define-inline t)
  4065. ("nofninline" org-footnote-define-inline nil)
  4066. ("fnlocal" org-footnote-section nil)
  4067. ("fnauto" org-footnote-auto-label t)
  4068. ("fnprompt" org-footnote-auto-label nil)
  4069. ("fnconfirm" org-footnote-auto-label confirm)
  4070. ("fnplain" org-footnote-auto-label plain)
  4071. ("fnadjust" org-footnote-auto-adjust t)
  4072. ("nofnadjust" org-footnote-auto-adjust nil)
  4073. ("constcgs" constants-unit-system cgs)
  4074. ("constSI" constants-unit-system SI)
  4075. ("noptag" org-tag-persistent-alist nil)
  4076. ("hideblocks" org-hide-block-startup t)
  4077. ("nohideblocks" org-hide-block-startup nil)
  4078. ("beamer" org-startup-with-beamer-mode t)
  4079. ("entitiespretty" org-pretty-entities t)
  4080. ("entitiesplain" org-pretty-entities nil))
  4081. "Variable associated with STARTUP options for org-mode.
  4082. Each element is a list of three items: the startup options (as written
  4083. in the #+STARTUP line), the corresponding variable, and the value to set
  4084. this variable to if the option is found. An optional forth element PUSH
  4085. means to push this value onto the list in the variable.")
  4086. (defun org-update-property-plist (key val props)
  4087. "Update PROPS with KEY and VAL."
  4088. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  4089. (key (if appending (substring key 0 (- (length key) 1)) key))
  4090. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  4091. (previous (cdr (assoc key props))))
  4092. (if appending
  4093. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  4094. (cons (cons key val) remainder))))
  4095. (defconst org-block-regexp
  4096. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  4097. "Regular expression for hiding blocks.")
  4098. (defconst org-heading-keyword-regexp-format
  4099. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4100. "Printf format for a regexp matching an headline with some keyword.
  4101. This regexp will match the headline of any node which has the
  4102. exact keyword that is put into the format. The keyword isn't in
  4103. any group by default, but the stars and the body are.")
  4104. (defconst org-heading-keyword-maybe-regexp-format
  4105. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  4106. "Printf format for a regexp matching an headline, possibly with some keyword.
  4107. This regexp can match any headline with the specified keyword, or
  4108. without a keyword. The keyword isn't in any group by default,
  4109. but the stars and the body are.")
  4110. (defun org-set-regexps-and-options ()
  4111. "Precompute regular expressions for current buffer."
  4112. (when (derived-mode-p 'org-mode)
  4113. (org-set-local 'org-todo-kwd-alist nil)
  4114. (org-set-local 'org-todo-key-alist nil)
  4115. (org-set-local 'org-todo-key-trigger nil)
  4116. (org-set-local 'org-todo-keywords-1 nil)
  4117. (org-set-local 'org-done-keywords nil)
  4118. (org-set-local 'org-todo-heads nil)
  4119. (org-set-local 'org-todo-sets nil)
  4120. (org-set-local 'org-todo-log-states nil)
  4121. (org-set-local 'org-file-properties nil)
  4122. (org-set-local 'org-file-tags nil)
  4123. (let ((re (org-make-options-regexp
  4124. '("CATEGORY" "TODO" "COLUMNS"
  4125. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  4126. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  4127. "OPTIONS")
  4128. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  4129. (splitre "[ \t]+")
  4130. (scripts org-use-sub-superscripts)
  4131. kwds kws0 kwsa key log value cat arch tags const links hw dws
  4132. tail sep kws1 prio props ftags drawers beamer-p
  4133. ext-setup-or-nil setup-contents (start 0))
  4134. (save-excursion
  4135. (save-restriction
  4136. (widen)
  4137. (goto-char (point-min))
  4138. (while (or (and ext-setup-or-nil
  4139. (string-match re ext-setup-or-nil start)
  4140. (setq start (match-end 0)))
  4141. (and (setq ext-setup-or-nil nil start 0)
  4142. (re-search-forward re nil t)))
  4143. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4144. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4145. (if (stringp value) (setq value (org-trim value)))
  4146. (cond
  4147. ((equal key "CATEGORY")
  4148. (setq cat value))
  4149. ((member key '("SEQ_TODO" "TODO"))
  4150. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4151. ((equal key "TYP_TODO")
  4152. (push (cons 'type (org-split-string value splitre)) kwds))
  4153. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4154. ;; general TODO-like setup
  4155. (push (cons (intern (downcase (match-string 1 key)))
  4156. (org-split-string value splitre)) kwds))
  4157. ((equal key "TAGS")
  4158. (setq tags (append tags (if tags '("\\n") nil)
  4159. (org-split-string value splitre))))
  4160. ((equal key "COLUMNS")
  4161. (org-set-local 'org-columns-default-format value))
  4162. ((equal key "LINK")
  4163. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4164. (push (cons (match-string 1 value)
  4165. (org-trim (match-string 2 value)))
  4166. links)))
  4167. ((equal key "PRIORITIES")
  4168. (setq prio (org-split-string value " +")))
  4169. ((equal key "PROPERTY")
  4170. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4171. (setq props (org-update-property-plist (match-string 1 value)
  4172. (match-string 2 value)
  4173. props))))
  4174. ((equal key "FILETAGS")
  4175. (when (string-match "\\S-" value)
  4176. (setq ftags
  4177. (append
  4178. ftags
  4179. (apply 'append
  4180. (mapcar (lambda (x) (org-split-string x ":"))
  4181. (org-split-string value)))))))
  4182. ((equal key "DRAWERS")
  4183. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4184. ((equal key "CONSTANTS")
  4185. (setq const (append const (org-split-string value splitre))))
  4186. ((equal key "STARTUP")
  4187. (let ((opts (org-split-string value splitre))
  4188. l var val)
  4189. (while (setq l (pop opts))
  4190. (when (setq l (assoc l org-startup-options))
  4191. (setq var (nth 1 l) val (nth 2 l))
  4192. (if (not (nth 3 l))
  4193. (set (make-local-variable var) val)
  4194. (if (not (listp (symbol-value var)))
  4195. (set (make-local-variable var) nil))
  4196. (set (make-local-variable var) (symbol-value var))
  4197. (add-to-list var val))))))
  4198. ((equal key "ARCHIVE")
  4199. (setq arch value)
  4200. (remove-text-properties 0 (length arch)
  4201. '(face t fontified t) arch))
  4202. ((equal key "LATEX_CLASS")
  4203. (setq beamer-p (equal value "beamer")))
  4204. ((equal key "OPTIONS")
  4205. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4206. (setq scripts (read (match-string 2 value)))))
  4207. ((equal key "SETUPFILE")
  4208. (setq setup-contents (org-file-contents
  4209. (expand-file-name
  4210. (org-remove-double-quotes value))
  4211. 'noerror))
  4212. (if (not ext-setup-or-nil)
  4213. (setq ext-setup-or-nil setup-contents start 0)
  4214. (setq ext-setup-or-nil
  4215. (concat (substring ext-setup-or-nil 0 start)
  4216. "\n" setup-contents "\n"
  4217. (substring ext-setup-or-nil start)))))))
  4218. ;; search for property blocks
  4219. (goto-char (point-min))
  4220. (while (re-search-forward org-block-regexp nil t)
  4221. (when (equal "PROPERTY" (upcase (match-string 1)))
  4222. (setq value (replace-regexp-in-string
  4223. "[\n\r]" " " (match-string 4)))
  4224. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4225. (setq props (org-update-property-plist (match-string 1 value)
  4226. (match-string 2 value)
  4227. props)))))))
  4228. (org-set-local 'org-use-sub-superscripts scripts)
  4229. (when cat
  4230. (org-set-local 'org-category (intern cat))
  4231. (push (cons "CATEGORY" cat) props))
  4232. (when prio
  4233. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4234. (setq prio (mapcar 'string-to-char prio))
  4235. (org-set-local 'org-highest-priority (nth 0 prio))
  4236. (org-set-local 'org-lowest-priority (nth 1 prio))
  4237. (org-set-local 'org-default-priority (nth 2 prio)))
  4238. (and props (org-set-local 'org-file-properties (nreverse props)))
  4239. (and ftags (org-set-local 'org-file-tags
  4240. (mapcar 'org-add-prop-inherited ftags)))
  4241. (and drawers (org-set-local 'org-drawers drawers))
  4242. (and arch (org-set-local 'org-archive-location arch))
  4243. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4244. ;; Process the TODO keywords
  4245. (unless kwds
  4246. ;; Use the global values as if they had been given locally.
  4247. (setq kwds (default-value 'org-todo-keywords))
  4248. (if (stringp (car kwds))
  4249. (setq kwds (list (cons org-todo-interpretation
  4250. (default-value 'org-todo-keywords)))))
  4251. (setq kwds (reverse kwds)))
  4252. (setq kwds (nreverse kwds))
  4253. (let (inter kws kw)
  4254. (while (setq kws (pop kwds))
  4255. (let ((kws (or
  4256. (run-hook-with-args-until-success
  4257. 'org-todo-setup-filter-hook kws)
  4258. kws)))
  4259. (setq inter (pop kws) sep (member "|" kws)
  4260. kws0 (delete "|" (copy-sequence kws))
  4261. kwsa nil
  4262. kws1 (mapcar
  4263. (lambda (x)
  4264. ;; 1 2
  4265. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4266. (progn
  4267. (setq kw (match-string 1 x)
  4268. key (and (match-end 2) (match-string 2 x))
  4269. log (org-extract-log-state-settings x))
  4270. (push (cons kw (and key (string-to-char key))) kwsa)
  4271. (and log (push log org-todo-log-states))
  4272. kw)
  4273. (error "Invalid TODO keyword %s" x)))
  4274. kws0)
  4275. kwsa (if kwsa (append '((:startgroup))
  4276. (nreverse kwsa)
  4277. '((:endgroup))))
  4278. hw (car kws1)
  4279. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4280. tail (list inter hw (car dws) (org-last dws))))
  4281. (add-to-list 'org-todo-heads hw 'append)
  4282. (push kws1 org-todo-sets)
  4283. (setq org-done-keywords (append org-done-keywords dws nil))
  4284. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4285. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4286. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4287. (setq org-todo-sets (nreverse org-todo-sets)
  4288. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4289. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4290. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4291. ;; Process the constants
  4292. (when const
  4293. (let (e cst)
  4294. (while (setq e (pop const))
  4295. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4296. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4297. (setq org-table-formula-constants-local cst)))
  4298. ;; Process the tags.
  4299. (when tags
  4300. (let (e tgs)
  4301. (while (setq e (pop tags))
  4302. (cond
  4303. ((equal e "{") (push '(:startgroup) tgs))
  4304. ((equal e "}") (push '(:endgroup) tgs))
  4305. ((equal e "\\n") (push '(:newline) tgs))
  4306. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4307. (push (cons (match-string 1 e)
  4308. (string-to-char (match-string 2 e)))
  4309. tgs))
  4310. (t (push (list e) tgs))))
  4311. (org-set-local 'org-tag-alist nil)
  4312. (while (setq e (pop tgs))
  4313. (or (and (stringp (car e))
  4314. (assoc (car e) org-tag-alist))
  4315. (push e org-tag-alist)))))
  4316. ;; Compute the regular expressions and other local variables.
  4317. ;; Using `org-outline-regexp-bol' would complicate them much,
  4318. ;; because of the fixed white space at the end of that string.
  4319. (if (not org-done-keywords)
  4320. (setq org-done-keywords (and org-todo-keywords-1
  4321. (list (org-last org-todo-keywords-1)))))
  4322. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4323. (length org-scheduled-string)
  4324. (length org-clock-string)
  4325. (length org-closed-string)))
  4326. org-drawer-regexp
  4327. (concat "^[ \t]*:\\("
  4328. (mapconcat 'regexp-quote org-drawers "\\|")
  4329. "\\):[ \t]*$")
  4330. org-not-done-keywords
  4331. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4332. org-todo-regexp
  4333. (concat "\\("
  4334. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4335. "\\)")
  4336. org-not-done-regexp
  4337. (concat "\\("
  4338. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4339. "\\)")
  4340. org-not-done-heading-regexp
  4341. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4342. org-todo-line-regexp
  4343. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4344. org-complex-heading-regexp
  4345. (concat "^\\(\\*+\\)"
  4346. "\\(?: +" org-todo-regexp "\\)?"
  4347. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4348. "\\(?: +\\(.*?\\)\\)??"
  4349. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4350. "[ \t]*$")
  4351. org-complex-heading-regexp-format
  4352. (concat "^\\(\\*+\\)"
  4353. "\\(?: +" org-todo-regexp "\\)?"
  4354. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4355. "\\(?: +"
  4356. ;; Stats cookies can be stuck to body.
  4357. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4358. "\\(%s\\)"
  4359. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4360. "\\)"
  4361. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4362. "[ \t]*$")
  4363. org-todo-line-tags-regexp
  4364. (concat "^\\(\\*+\\)"
  4365. "\\(?: +" org-todo-regexp "\\)?"
  4366. "\\(?: +\\(.*?\\)\\)??"
  4367. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4368. "[ \t]*$")
  4369. org-deadline-regexp (concat "\\<" org-deadline-string)
  4370. org-deadline-time-regexp
  4371. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4372. org-deadline-line-regexp
  4373. (concat "\\<\\(" org-deadline-string "\\).*")
  4374. org-scheduled-regexp
  4375. (concat "\\<" org-scheduled-string)
  4376. org-scheduled-time-regexp
  4377. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4378. org-closed-time-regexp
  4379. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4380. org-keyword-time-regexp
  4381. (concat "\\<\\(" org-scheduled-string
  4382. "\\|" org-deadline-string
  4383. "\\|" org-closed-string
  4384. "\\|" org-clock-string "\\)"
  4385. " *[[<]\\([^]>]+\\)[]>]")
  4386. org-keyword-time-not-clock-regexp
  4387. (concat "\\<\\(" org-scheduled-string
  4388. "\\|" org-deadline-string
  4389. "\\|" org-closed-string
  4390. "\\)"
  4391. " *[[<]\\([^]>]+\\)[]>]")
  4392. org-maybe-keyword-time-regexp
  4393. (concat "\\(\\<\\(" org-scheduled-string
  4394. "\\|" org-deadline-string
  4395. "\\|" org-closed-string
  4396. "\\|" org-clock-string "\\)\\)?"
  4397. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4398. org-all-time-keywords
  4399. (mapcar (lambda (w) (substring w 0 -1))
  4400. (list org-scheduled-string org-deadline-string
  4401. org-clock-string org-closed-string)))
  4402. (org-set-font-lock-defaults))))
  4403. (defun org-file-contents (file &optional noerror)
  4404. "Return the contents of FILE, as a string."
  4405. (if (or (not file)
  4406. (not (file-readable-p file)))
  4407. (if noerror
  4408. (progn
  4409. (message "Cannot read file \"%s\"" file)
  4410. (ding) (sit-for 2)
  4411. "")
  4412. (error "Cannot read file \"%s\"" file))
  4413. (with-temp-buffer
  4414. (insert-file-contents file)
  4415. (buffer-string))))
  4416. (defun org-extract-log-state-settings (x)
  4417. "Extract the log state setting from a TODO keyword string.
  4418. This will extract info from a string like \"WAIT(w@/!)\"."
  4419. (let (kw key log1 log2)
  4420. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4421. (setq kw (match-string 1 x)
  4422. key (and (match-end 2) (match-string 2 x))
  4423. log1 (and (match-end 3) (match-string 3 x))
  4424. log2 (and (match-end 4) (match-string 4 x)))
  4425. (and (or log1 log2)
  4426. (list kw
  4427. (and log1 (if (equal log1 "!") 'time 'note))
  4428. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4429. (defun org-remove-keyword-keys (list)
  4430. "Remove a pair of parenthesis at the end of each string in LIST."
  4431. (mapcar (lambda (x)
  4432. (if (string-match "(.*)$" x)
  4433. (substring x 0 (match-beginning 0))
  4434. x))
  4435. list))
  4436. (defun org-assign-fast-keys (alist)
  4437. "Assign fast keys to a keyword-key alist.
  4438. Respect keys that are already there."
  4439. (let (new e (alt ?0))
  4440. (while (setq e (pop alist))
  4441. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4442. (cdr e)) ;; Key already assigned.
  4443. (push e new)
  4444. (let ((clist (string-to-list (downcase (car e))))
  4445. (used (append new alist)))
  4446. (when (= (car clist) ?@)
  4447. (pop clist))
  4448. (while (and clist (rassoc (car clist) used))
  4449. (pop clist))
  4450. (unless clist
  4451. (while (rassoc alt used)
  4452. (incf alt)))
  4453. (push (cons (car e) (or (car clist) alt)) new))))
  4454. (nreverse new)))
  4455. ;;; Some variables used in various places
  4456. (defvar org-window-configuration nil
  4457. "Used in various places to store a window configuration.")
  4458. (defvar org-selected-window nil
  4459. "Used in various places to store a window configuration.")
  4460. (defvar org-finish-function nil
  4461. "Function to be called when `C-c C-c' is used.
  4462. This is for getting out of special buffers like capture.")
  4463. ;; FIXME: Occasionally check by commenting these, to make sure
  4464. ;; no other functions uses these, forgetting to let-bind them.
  4465. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4466. (defvar org-last-state)
  4467. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4468. ;; Defined somewhere in this file, but used before definition.
  4469. (defvar org-entities) ;; defined in org-entities.el
  4470. (defvar org-struct-menu)
  4471. (defvar org-org-menu)
  4472. (defvar org-tbl-menu)
  4473. ;;;; Define the Org-mode
  4474. ;; We use a before-change function to check if a table might need
  4475. ;; an update.
  4476. (defvar org-table-may-need-update t
  4477. "Indicates that a table might need an update.
  4478. This variable is set by `org-before-change-function'.
  4479. `org-table-align' sets it back to nil.")
  4480. (defun org-before-change-function (beg end)
  4481. "Every change indicates that a table might need an update."
  4482. (setq org-table-may-need-update t))
  4483. (defvar org-mode-map)
  4484. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4485. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4486. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4487. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4488. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4489. (defvar org-table-buffer-is-an nil)
  4490. (defvar bidi-paragraph-direction)
  4491. (defvar buffer-face-mode-face)
  4492. (require 'outline)
  4493. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4494. (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"))
  4495. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4496. ;; Other stuff we need.
  4497. (require 'time-date)
  4498. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4499. (require 'easymenu)
  4500. (require 'overlay)
  4501. ;; (require 'org-macs) moved higher up in the file before it is first used
  4502. (require 'org-entities)
  4503. ;; (require 'org-compat) moved higher up in the file before it is first used
  4504. (require 'org-faces)
  4505. (require 'org-list)
  4506. (require 'org-pcomplete)
  4507. (require 'org-src)
  4508. (require 'org-footnote)
  4509. ;; babel
  4510. (require 'ob)
  4511. ;;;###autoload
  4512. (define-derived-mode org-mode outline-mode "Org"
  4513. "Outline-based notes management and organizer, alias
  4514. \"Carsten's outline-mode for keeping track of everything.\"
  4515. Org-mode develops organizational tasks around a NOTES file which
  4516. contains information about projects as plain text. Org-mode is
  4517. implemented on top of outline-mode, which is ideal to keep the content
  4518. of large files well structured. It supports ToDo items, deadlines and
  4519. time stamps, which magically appear in the diary listing of the Emacs
  4520. calendar. Tables are easily created with a built-in table editor.
  4521. Plain text URL-like links connect to websites, emails (VM), Usenet
  4522. messages (Gnus), BBDB entries, and any files related to the project.
  4523. For printing and sharing of notes, an Org-mode file (or a part of it)
  4524. can be exported as a structured ASCII or HTML file.
  4525. The following commands are available:
  4526. \\{org-mode-map}"
  4527. ;; Get rid of Outline menus, they are not needed
  4528. ;; Need to do this here because define-derived-mode sets up
  4529. ;; the keymap so late. Still, it is a waste to call this each time
  4530. ;; we switch another buffer into org-mode.
  4531. (if (featurep 'xemacs)
  4532. (when (boundp 'outline-mode-menu-heading)
  4533. ;; Assume this is Greg's port, it uses easymenu
  4534. (easy-menu-remove outline-mode-menu-heading)
  4535. (easy-menu-remove outline-mode-menu-show)
  4536. (easy-menu-remove outline-mode-menu-hide))
  4537. (define-key org-mode-map [menu-bar headings] 'undefined)
  4538. (define-key org-mode-map [menu-bar hide] 'undefined)
  4539. (define-key org-mode-map [menu-bar show] 'undefined))
  4540. (org-load-modules-maybe)
  4541. (easy-menu-add org-org-menu)
  4542. (easy-menu-add org-tbl-menu)
  4543. (org-install-agenda-files-menu)
  4544. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4545. (add-to-invisibility-spec '(org-cwidth))
  4546. (add-to-invisibility-spec '(org-hide-block . t))
  4547. (when (featurep 'xemacs)
  4548. (org-set-local 'line-move-ignore-invisible t))
  4549. (org-set-local 'outline-regexp org-outline-regexp)
  4550. (org-set-local 'outline-level 'org-outline-level)
  4551. (setq bidi-paragraph-direction 'left-to-right)
  4552. (when (and org-ellipsis
  4553. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4554. (fboundp 'make-glyph-code))
  4555. (unless org-display-table
  4556. (setq org-display-table (make-display-table)))
  4557. (set-display-table-slot
  4558. org-display-table 4
  4559. (vconcat (mapcar
  4560. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4561. org-ellipsis)))
  4562. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4563. (setq buffer-display-table org-display-table))
  4564. (org-set-regexps-and-options)
  4565. (when (and org-tag-faces (not org-tags-special-faces-re))
  4566. ;; tag faces set outside customize.... force initialization.
  4567. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4568. ;; Calc embedded
  4569. (org-set-local 'calc-embedded-open-mode "# ")
  4570. (modify-syntax-entry ?@ "w")
  4571. (modify-syntax-entry ?\" "\"")
  4572. (if org-startup-truncated (setq truncate-lines t))
  4573. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4574. (org-set-local 'font-lock-unfontify-region-function
  4575. 'org-unfontify-region)
  4576. ;; Activate before-change-function
  4577. (org-set-local 'org-table-may-need-update t)
  4578. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4579. 'local)
  4580. ;; Check for running clock before killing a buffer
  4581. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4582. ;; Initialize macros templates.
  4583. (org-macro-initialize-templates)
  4584. ;; Initialize radio targets.
  4585. (org-update-radio-target-regexp)
  4586. ;; Indentation.
  4587. (org-set-local 'indent-line-function 'org-indent-line)
  4588. (org-set-local 'indent-region-function 'org-indent-region)
  4589. ;; Filling and auto-filling.
  4590. (org-setup-filling)
  4591. ;; Comments.
  4592. (org-setup-comments-handling)
  4593. ;; Beginning/end of defun
  4594. (org-set-local 'beginning-of-defun-function 'org-back-to-heading)
  4595. (org-set-local 'end-of-defun-function (lambda () (interactive) (org-end-of-subtree nil t)))
  4596. ;; Next error for sparse trees
  4597. (org-set-local 'next-error-function 'org-occur-next-match)
  4598. ;; Make sure dependence stuff works reliably, even for users who set it
  4599. ;; too late :-(
  4600. (if org-enforce-todo-dependencies
  4601. (add-hook 'org-blocker-hook
  4602. 'org-block-todo-from-children-or-siblings-or-parent)
  4603. (remove-hook 'org-blocker-hook
  4604. 'org-block-todo-from-children-or-siblings-or-parent))
  4605. (if org-enforce-todo-checkbox-dependencies
  4606. (add-hook 'org-blocker-hook
  4607. 'org-block-todo-from-checkboxes)
  4608. (remove-hook 'org-blocker-hook
  4609. 'org-block-todo-from-checkboxes))
  4610. ;; Align options lines
  4611. (org-set-local
  4612. 'align-mode-rules-list
  4613. '((org-in-buffer-settings
  4614. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4615. (modes . '(org-mode)))))
  4616. ;; Imenu
  4617. (org-set-local 'imenu-create-index-function
  4618. 'org-imenu-get-tree)
  4619. ;; Make isearch reveal context
  4620. (if (or (featurep 'xemacs)
  4621. (not (boundp 'outline-isearch-open-invisible-function)))
  4622. ;; Emacs 21 and XEmacs make use of the hook
  4623. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4624. ;; Emacs 22 deals with this through a special variable
  4625. (org-set-local 'outline-isearch-open-invisible-function
  4626. (lambda (&rest ignore) (org-show-context 'isearch))))
  4627. ;; Setup the pcomplete hooks
  4628. (set (make-local-variable 'pcomplete-command-completion-function)
  4629. 'org-pcomplete-initial)
  4630. (set (make-local-variable 'pcomplete-command-name-function)
  4631. 'org-command-at-point)
  4632. (set (make-local-variable 'pcomplete-default-completion-function)
  4633. 'ignore)
  4634. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4635. 'org-parse-arguments)
  4636. (set (make-local-variable 'pcomplete-termination-string) "")
  4637. (when (>= emacs-major-version 23)
  4638. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4639. ;; If empty file that did not turn on org-mode automatically, make it to.
  4640. (if (and org-insert-mode-line-in-empty-file
  4641. (org-called-interactively-p 'any)
  4642. (= (point-min) (point-max)))
  4643. (insert "# -*- mode: org -*-\n\n"))
  4644. (unless org-inhibit-startup
  4645. (and org-startup-with-beamer-mode (org-beamer-mode))
  4646. (when org-startup-align-all-tables
  4647. (let ((bmp (buffer-modified-p)))
  4648. (org-table-map-tables 'org-table-align 'quietly)
  4649. (set-buffer-modified-p bmp)))
  4650. (when org-startup-with-inline-images
  4651. (org-display-inline-images))
  4652. (unless org-inhibit-startup-visibility-stuff
  4653. (org-set-startup-visibility)))
  4654. ;; Try to set org-hide correctly
  4655. (set-face-foreground 'org-hide (org-find-invisible-foreground)))
  4656. (when (fboundp 'abbrev-table-put)
  4657. (abbrev-table-put org-mode-abbrev-table
  4658. :parents (list text-mode-abbrev-table)))
  4659. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4660. (defun org-find-invisible-foreground ()
  4661. (let ((candidates (remove
  4662. "unspecified-bg"
  4663. (nconc
  4664. (list (face-background 'default)
  4665. (face-background 'org-default))
  4666. (mapcar
  4667. (lambda (alist)
  4668. (when (boundp alist)
  4669. (cdr (assoc 'background-color (symbol-value alist)))))
  4670. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4671. (list (face-foreground 'org-hide))))))
  4672. (car (remove nil candidates))))
  4673. (defun org-current-time (&optional rounding-minutes)
  4674. "Current time, possibly rounded to ROUNDING-MINUTES.
  4675. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4676. `org-time-stamp-rounding-minutes'."
  4677. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4678. (car org-time-stamp-rounding-minutes)))
  4679. (time (decode-time)))
  4680. (if (> r 1)
  4681. (apply 'encode-time
  4682. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4683. (nthcdr 2 time)))
  4684. (current-time))))
  4685. (defun org-today ()
  4686. "Return today date, considering `org-extend-today-until'."
  4687. (time-to-days
  4688. (time-subtract (current-time)
  4689. (list 0 (* 3600 org-extend-today-until) 0))))
  4690. ;;;; Font-Lock stuff, including the activators
  4691. (defvar org-mouse-map (make-sparse-keymap))
  4692. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4693. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4694. (when org-mouse-1-follows-link
  4695. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4696. (when org-tab-follows-link
  4697. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4698. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4699. (require 'font-lock)
  4700. (defconst org-non-link-chars "]\t\n\r<>")
  4701. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4702. "shell" "elisp" "doi" "message"))
  4703. (defvar org-link-types-re nil
  4704. "Matches a link that has a url-like prefix like \"http:\"")
  4705. (defvar org-link-re-with-space nil
  4706. "Matches a link with spaces, optional angular brackets around it.")
  4707. (defvar org-link-re-with-space2 nil
  4708. "Matches a link with spaces, optional angular brackets around it.")
  4709. (defvar org-link-re-with-space3 nil
  4710. "Matches a link with spaces, only for internal part in bracket links.")
  4711. (defvar org-angle-link-re nil
  4712. "Matches link with angular brackets, spaces are allowed.")
  4713. (defvar org-plain-link-re nil
  4714. "Matches plain link, without spaces.")
  4715. (defvar org-bracket-link-regexp nil
  4716. "Matches a link in double brackets.")
  4717. (defvar org-bracket-link-analytic-regexp nil
  4718. "Regular expression used to analyze links.
  4719. Here is what the match groups contain after a match:
  4720. 1: http:
  4721. 2: http
  4722. 3: path
  4723. 4: [desc]
  4724. 5: desc")
  4725. (defvar org-bracket-link-analytic-regexp++ nil
  4726. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4727. (defvar org-any-link-re nil
  4728. "Regular expression matching any link.")
  4729. (defconst org-match-sexp-depth 3
  4730. "Number of stacked braces for sub/superscript matching.")
  4731. (defun org-create-multibrace-regexp (left right n)
  4732. "Create a regular expression which will match a balanced sexp.
  4733. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4734. as single character strings.
  4735. The regexp returned will match the entire expression including the
  4736. delimiters. It will also define a single group which contains the
  4737. match except for the outermost delimiters. The maximum depth of
  4738. stacked delimiters is N. Escaping delimiters is not possible."
  4739. (let* ((nothing (concat "[^" left right "]*?"))
  4740. (or "\\|")
  4741. (re nothing)
  4742. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4743. (while (> n 1)
  4744. (setq n (1- n)
  4745. re (concat re or next)
  4746. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4747. (concat left "\\(" re "\\)" right)))
  4748. (defvar org-match-substring-regexp
  4749. (concat
  4750. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4751. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4752. "\\|"
  4753. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4754. "\\|"
  4755. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4756. "The regular expression matching a sub- or superscript.")
  4757. (defvar org-match-substring-with-braces-regexp
  4758. (concat
  4759. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4760. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4761. "\\)")
  4762. "The regular expression matching a sub- or superscript, forcing braces.")
  4763. (defun org-make-link-regexps ()
  4764. "Update the link regular expressions.
  4765. This should be called after the variable `org-link-types' has changed."
  4766. (setq org-link-types-re
  4767. (concat
  4768. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4769. org-link-re-with-space
  4770. (concat
  4771. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4772. "\\([^" org-non-link-chars " ]"
  4773. "[^" org-non-link-chars "]*"
  4774. "[^" org-non-link-chars " ]\\)>?")
  4775. org-link-re-with-space2
  4776. (concat
  4777. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4778. "\\([^" org-non-link-chars " ]"
  4779. "[^\t\n\r]*"
  4780. "[^" org-non-link-chars " ]\\)>?")
  4781. org-link-re-with-space3
  4782. (concat
  4783. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4784. "\\([^" org-non-link-chars " ]"
  4785. "[^\t\n\r]*\\)")
  4786. org-angle-link-re
  4787. (concat
  4788. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4789. "\\([^" org-non-link-chars " ]"
  4790. "[^" org-non-link-chars "]*"
  4791. "\\)>")
  4792. org-plain-link-re
  4793. (concat
  4794. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4795. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4796. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4797. org-bracket-link-regexp
  4798. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4799. org-bracket-link-analytic-regexp
  4800. (concat
  4801. "\\[\\["
  4802. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4803. "\\([^]]+\\)"
  4804. "\\]"
  4805. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4806. "\\]")
  4807. org-bracket-link-analytic-regexp++
  4808. (concat
  4809. "\\[\\["
  4810. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4811. "\\([^]]+\\)"
  4812. "\\]"
  4813. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4814. "\\]")
  4815. org-any-link-re
  4816. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4817. org-angle-link-re "\\)\\|\\("
  4818. org-plain-link-re "\\)")))
  4819. (org-make-link-regexps)
  4820. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4821. "Regular expression for fast time stamp matching.")
  4822. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4823. "Regular expression for fast time stamp matching.")
  4824. (defconst org-ts-regexp0
  4825. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4826. "Regular expression matching time strings for analysis.
  4827. This one does not require the space after the date, so it can be used
  4828. on a string that terminates immediately after the date.")
  4829. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4830. "Regular expression matching time strings for analysis.")
  4831. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4832. "Regular expression matching time stamps, with groups.")
  4833. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4834. "Regular expression matching time stamps (also [..]), with groups.")
  4835. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4836. "Regular expression matching a time stamp range.")
  4837. (defconst org-tr-regexp-both
  4838. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4839. "Regular expression matching a time stamp range.")
  4840. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4841. org-ts-regexp "\\)?")
  4842. "Regular expression matching a time stamp or time stamp range.")
  4843. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4844. org-ts-regexp-both "\\)?")
  4845. "Regular expression matching a time stamp or time stamp range.
  4846. The time stamps may be either active or inactive.")
  4847. (defvar org-emph-face nil)
  4848. (defun org-do-emphasis-faces (limit)
  4849. "Run through the buffer and add overlays to emphasized strings."
  4850. (let (rtn a)
  4851. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4852. (if (not (= (char-after (match-beginning 3))
  4853. (char-after (match-beginning 4))))
  4854. (progn
  4855. (setq rtn t)
  4856. (setq a (assoc (match-string 3) org-emphasis-alist))
  4857. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4858. 'face
  4859. (nth 1 a))
  4860. (and (nth 4 a)
  4861. (org-remove-flyspell-overlays-in
  4862. (match-beginning 0) (match-end 0)))
  4863. (add-text-properties (match-beginning 2) (match-end 2)
  4864. '(font-lock-multiline t org-emphasis t))
  4865. (when org-hide-emphasis-markers
  4866. (add-text-properties (match-end 4) (match-beginning 5)
  4867. '(invisible org-link))
  4868. (add-text-properties (match-beginning 3) (match-end 3)
  4869. '(invisible org-link)))))
  4870. (backward-char 1))
  4871. rtn))
  4872. (defun org-emphasize (&optional char)
  4873. "Insert or change an emphasis, i.e. a font like bold or italic.
  4874. If there is an active region, change that region to a new emphasis.
  4875. If there is no region, just insert the marker characters and position
  4876. the cursor between them.
  4877. CHAR should be either the marker character, or the first character of the
  4878. HTML tag associated with that emphasis. If CHAR is a space, the means
  4879. to remove the emphasis of the selected region.
  4880. If char is not given (for example in an interactive call) it
  4881. will be prompted for."
  4882. (interactive)
  4883. (let ((eal org-emphasis-alist) e det
  4884. (erc org-emphasis-regexp-components)
  4885. (prompt "")
  4886. (string "") beg end move tag c s)
  4887. (if (org-region-active-p)
  4888. (setq beg (region-beginning) end (region-end)
  4889. string (buffer-substring beg end))
  4890. (setq move t))
  4891. (while (setq e (pop eal))
  4892. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4893. c (aref tag 0))
  4894. (push (cons c (string-to-char (car e))) det)
  4895. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4896. (substring tag 1)))))
  4897. (setq det (nreverse det))
  4898. (unless char
  4899. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4900. (setq char (read-char-exclusive)))
  4901. (setq char (or (cdr (assoc char det)) char))
  4902. (if (equal char ?\ )
  4903. (setq s "" move nil)
  4904. (unless (assoc (char-to-string char) org-emphasis-alist)
  4905. (error "No such emphasis marker: \"%c\"" char))
  4906. (setq s (char-to-string char)))
  4907. (while (and (> (length string) 1)
  4908. (equal (substring string 0 1) (substring string -1))
  4909. (assoc (substring string 0 1) org-emphasis-alist))
  4910. (setq string (substring string 1 -1)))
  4911. (setq string (concat s string s))
  4912. (if beg (delete-region beg end))
  4913. (unless (or (bolp)
  4914. (string-match (concat "[" (nth 0 erc) "\n]")
  4915. (char-to-string (char-before (point)))))
  4916. (insert " "))
  4917. (unless (or (eobp)
  4918. (string-match (concat "[" (nth 1 erc) "\n]")
  4919. (char-to-string (char-after (point)))))
  4920. (insert " ") (backward-char 1))
  4921. (insert string)
  4922. (and move (backward-char 1))))
  4923. (defconst org-nonsticky-props
  4924. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  4925. (defsubst org-rear-nonsticky-at (pos)
  4926. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4927. (defun org-activate-plain-links (limit)
  4928. "Run through the buffer and add overlays to links."
  4929. (catch 'exit
  4930. (let (f hl)
  4931. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  4932. (not (org-in-src-block-p)))
  4933. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4934. (setq f (get-text-property (match-beginning 0) 'face))
  4935. (setq hl (org-match-string-no-properties 0))
  4936. (if (or (eq f 'org-tag)
  4937. (and (listp f) (memq 'org-tag f)))
  4938. nil
  4939. (add-text-properties (match-beginning 0) (match-end 0)
  4940. (list 'mouse-face 'highlight
  4941. 'face 'org-link
  4942. 'htmlize-link `(:uri ,hl)
  4943. 'keymap org-mouse-map))
  4944. (org-rear-nonsticky-at (match-end 0)))
  4945. t))))
  4946. (defun org-activate-code (limit)
  4947. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4948. (progn
  4949. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4950. (remove-text-properties (match-beginning 0) (match-end 0)
  4951. '(display t invisible t intangible t))
  4952. t)))
  4953. (defcustom org-src-fontify-natively nil
  4954. "When non-nil, fontify code in code blocks."
  4955. :type 'boolean
  4956. :version "24.1"
  4957. :group 'org-appearance
  4958. :group 'org-babel)
  4959. (defcustom org-allow-promoting-top-level-subtree nil
  4960. "When non-nil, allow promoting a top level subtree.
  4961. The leading star of the top level headline will be replaced
  4962. by a #."
  4963. :type 'boolean
  4964. :version "24.1"
  4965. :group 'org-appearance)
  4966. (defun org-fontify-meta-lines-and-blocks (limit)
  4967. (condition-case nil
  4968. (org-fontify-meta-lines-and-blocks-1 limit)
  4969. (error (message "org-mode fontification error"))))
  4970. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4971. "Fontify #+ lines and blocks, in the correct ways."
  4972. (let ((case-fold-search t))
  4973. (if (re-search-forward
  4974. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4975. limit t)
  4976. (let ((beg (match-beginning 0))
  4977. (block-start (match-end 0))
  4978. (block-end nil)
  4979. (lang (match-string 7))
  4980. (beg1 (line-beginning-position 2))
  4981. (dc1 (downcase (match-string 2)))
  4982. (dc3 (downcase (match-string 3)))
  4983. end end1 quoting block-type ovl)
  4984. (cond
  4985. ((member dc1 '("+html:" "+ascii:" "+latex:" "+docbook:"))
  4986. ;; a single line of backend-specific content
  4987. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4988. (remove-text-properties (match-beginning 0) (match-end 0)
  4989. '(display t invisible t intangible t))
  4990. (add-text-properties (match-beginning 1) (match-end 3)
  4991. '(font-lock-fontified t face org-meta-line))
  4992. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4993. '(font-lock-fontified t face org-block))
  4994. ; for backend-specific code
  4995. t)
  4996. ((and (match-end 4) (equal dc3 "+begin"))
  4997. ;; Truly a block
  4998. (setq block-type (downcase (match-string 5))
  4999. quoting (member block-type org-protecting-blocks))
  5000. (when (re-search-forward
  5001. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5002. nil t) ;; on purpose, we look further than LIMIT
  5003. (setq end (min (point-max) (match-end 0))
  5004. end1 (min (point-max) (1- (match-beginning 0))))
  5005. (setq block-end (match-beginning 0))
  5006. (when quoting
  5007. (remove-text-properties beg end
  5008. '(display t invisible t intangible t)))
  5009. (add-text-properties
  5010. beg end
  5011. '(font-lock-fontified t font-lock-multiline t))
  5012. (add-text-properties beg beg1 '(face org-meta-line))
  5013. (add-text-properties end1 (min (point-max) (1+ end))
  5014. '(face org-meta-line)) ; for end_src
  5015. (cond
  5016. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5017. (org-src-font-lock-fontify-block lang block-start block-end)
  5018. ;; remove old background overlays
  5019. (mapc (lambda (ov)
  5020. (if (eq (overlay-get ov 'face) 'org-block-background)
  5021. (delete-overlay ov)))
  5022. (overlays-at (/ (+ beg1 block-end) 2)))
  5023. ;; add a background overlay
  5024. (setq ovl (make-overlay beg1 block-end))
  5025. (overlay-put ovl 'face 'org-block-background)
  5026. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  5027. (quoting
  5028. (add-text-properties beg1 (min (point-max) (1+ end1))
  5029. '(face org-block))) ; end of source block
  5030. ((not org-fontify-quote-and-verse-blocks))
  5031. ((string= block-type "quote")
  5032. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5033. ((string= block-type "verse")
  5034. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5035. (add-text-properties beg beg1 '(face org-block-begin-line))
  5036. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5037. '(face org-block-end-line))
  5038. t))
  5039. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5040. (add-text-properties
  5041. beg (match-end 3)
  5042. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  5043. '(font-lock-fontified t invisible t)
  5044. '(font-lock-fontified t face org-document-info-keyword)))
  5045. (add-text-properties
  5046. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5047. (if (string-equal dc1 "+title:")
  5048. '(font-lock-fontified t face org-document-title)
  5049. '(font-lock-fontified t face org-document-info))))
  5050. ((or (equal dc1 "+results")
  5051. (member dc1 '("+begin:" "+end:" "+caption:" "+label:"
  5052. "+orgtbl:" "+tblfm:" "+tblname:" "+results:"
  5053. "+call:" "+header:" "+headers:" "+name:"))
  5054. (and (match-end 4) (equal dc3 "+attr")))
  5055. (add-text-properties
  5056. beg (match-end 0)
  5057. '(font-lock-fontified t face org-meta-line))
  5058. t)
  5059. ((member dc3 '(" " ""))
  5060. (add-text-properties
  5061. beg (match-end 0)
  5062. '(font-lock-fontified t face font-lock-comment-face)))
  5063. ((not (member (char-after beg) '(?\ ?\t)))
  5064. ;; just any other in-buffer setting, but not indented
  5065. (add-text-properties
  5066. beg (match-end 0)
  5067. '(font-lock-fontified t face org-meta-line))
  5068. t)
  5069. (t nil))))))
  5070. (defun org-activate-angle-links (limit)
  5071. "Run through the buffer and add overlays to links."
  5072. (if (and (re-search-forward org-angle-link-re limit t)
  5073. (not (org-in-src-block-p)))
  5074. (progn
  5075. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5076. (add-text-properties (match-beginning 0) (match-end 0)
  5077. (list 'mouse-face 'highlight
  5078. 'keymap org-mouse-map))
  5079. (org-rear-nonsticky-at (match-end 0))
  5080. t)))
  5081. (defun org-activate-footnote-links (limit)
  5082. "Run through the buffer and add overlays to footnotes."
  5083. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5084. (when fn
  5085. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  5086. (org-remove-flyspell-overlays-in beg end)
  5087. (add-text-properties beg end
  5088. (list 'mouse-face 'highlight
  5089. 'keymap org-mouse-map
  5090. 'help-echo
  5091. (if (= (point-at-bol) beg)
  5092. "Footnote definition"
  5093. "Footnote reference")
  5094. 'font-lock-fontified t
  5095. 'font-lock-multiline t
  5096. 'face 'org-footnote))))))
  5097. (defun org-activate-bracket-links (limit)
  5098. "Run through the buffer and add overlays to bracketed links."
  5099. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5100. (not (org-in-src-block-p)))
  5101. (let* ((hl (org-match-string-no-properties 1))
  5102. (help (concat "LINK: " hl))
  5103. ;; FIXME: Above we should remove the escapes. But that
  5104. ;; requires another match, protecting match data, a lot
  5105. ;; of overhead for font-lock.
  5106. (ip (org-maybe-intangible
  5107. (list 'invisible 'org-link
  5108. 'keymap org-mouse-map 'mouse-face 'highlight
  5109. 'font-lock-multiline t 'help-echo help
  5110. 'htmlize-link `(:uri ,hl))))
  5111. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5112. 'font-lock-multiline t 'help-echo help
  5113. 'htmlize-link `(:uri ,hl))))
  5114. ;; We need to remove the invisible property here. Table narrowing
  5115. ;; may have made some of this invisible.
  5116. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5117. (remove-text-properties (match-beginning 0) (match-end 0)
  5118. '(invisible nil))
  5119. (if (match-end 3)
  5120. (progn
  5121. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5122. (org-rear-nonsticky-at (match-beginning 3))
  5123. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5124. (org-rear-nonsticky-at (match-end 3))
  5125. (add-text-properties (match-end 3) (match-end 0) ip)
  5126. (org-rear-nonsticky-at (match-end 0)))
  5127. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5128. (org-rear-nonsticky-at (match-beginning 1))
  5129. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5130. (org-rear-nonsticky-at (match-end 1))
  5131. (add-text-properties (match-end 1) (match-end 0) ip)
  5132. (org-rear-nonsticky-at (match-end 0)))
  5133. t)))
  5134. (defun org-activate-dates (limit)
  5135. "Run through the buffer and add overlays to dates."
  5136. (if (re-search-forward org-tsr-regexp-both limit t)
  5137. (progn
  5138. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5139. (add-text-properties (match-beginning 0) (match-end 0)
  5140. (list 'mouse-face 'highlight
  5141. 'keymap org-mouse-map))
  5142. (org-rear-nonsticky-at (match-end 0))
  5143. (when org-display-custom-times
  5144. (if (match-end 3)
  5145. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5146. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5147. t)))
  5148. (defvar org-target-link-regexp nil
  5149. "Regular expression matching radio targets in plain text.")
  5150. (make-variable-buffer-local 'org-target-link-regexp)
  5151. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5152. "Regular expression matching a link target.")
  5153. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5154. "Regular expression matching a radio target.")
  5155. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5156. "Regular expression matching any target.")
  5157. (defun org-activate-target-links (limit)
  5158. "Run through the buffer and add overlays to target matches."
  5159. (when org-target-link-regexp
  5160. (let ((case-fold-search t))
  5161. (if (re-search-forward org-target-link-regexp limit t)
  5162. (progn
  5163. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5164. (add-text-properties (match-beginning 0) (match-end 0)
  5165. (list 'mouse-face 'highlight
  5166. 'keymap org-mouse-map
  5167. 'help-echo "Radio target link"
  5168. 'org-linked-text t))
  5169. (org-rear-nonsticky-at (match-end 0))
  5170. t)))))
  5171. (defun org-update-radio-target-regexp ()
  5172. "Find all radio targets in this file and update the regular expression."
  5173. (interactive)
  5174. (when (memq 'radio org-activate-links)
  5175. (setq org-target-link-regexp
  5176. (org-make-target-link-regexp (org-all-targets 'radio)))
  5177. (org-restart-font-lock)))
  5178. (defun org-hide-wide-columns (limit)
  5179. (let (s e)
  5180. (setq s (text-property-any (point) (or limit (point-max))
  5181. 'org-cwidth t))
  5182. (when s
  5183. (setq e (next-single-property-change s 'org-cwidth))
  5184. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5185. (goto-char e)
  5186. t)))
  5187. (defvar org-match-substring-regexp)
  5188. (defvar org-match-substring-with-braces-regexp)
  5189. (defun org-restart-font-lock ()
  5190. "Restart `font-lock-mode', to force refontification."
  5191. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5192. (font-lock-mode -1)
  5193. (font-lock-mode 1)))
  5194. (defun org-all-targets (&optional radio)
  5195. "Return a list of all targets in this file.
  5196. When optional argument RADIO is non-nil, only find radio
  5197. targets."
  5198. (let ((re (if radio org-radio-target-regexp org-target-regexp)) rtn)
  5199. (save-excursion
  5200. (goto-char (point-min))
  5201. (while (re-search-forward re nil t)
  5202. ;; Make sure point is really within the object.
  5203. (backward-char)
  5204. (let ((obj (org-element-context)))
  5205. (when (memq (org-element-type obj) '(radio-target target))
  5206. (add-to-list 'rtn (downcase (org-element-property :value obj))))))
  5207. rtn)))
  5208. (defun org-make-target-link-regexp (targets)
  5209. "Make regular expression matching all strings in TARGETS.
  5210. The regular expression finds the targets also if there is a line break
  5211. between words."
  5212. (and targets
  5213. (concat
  5214. "\\<\\("
  5215. (mapconcat
  5216. (lambda (x)
  5217. (setq x (regexp-quote x))
  5218. (while (string-match " +" x)
  5219. (setq x (replace-match "\\s-+" t t x)))
  5220. x)
  5221. targets
  5222. "\\|")
  5223. "\\)\\>")))
  5224. (defun org-activate-tags (limit)
  5225. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5226. (progn
  5227. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5228. (add-text-properties (match-beginning 1) (match-end 1)
  5229. (list 'mouse-face 'highlight
  5230. 'keymap org-mouse-map))
  5231. (org-rear-nonsticky-at (match-end 1))
  5232. t)))
  5233. (defun org-outline-level ()
  5234. "Compute the outline level of the heading at point.
  5235. If this is called at a normal headline, the level is the number of stars.
  5236. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5237. (save-excursion
  5238. (if (not (condition-case nil
  5239. (org-back-to-heading t)
  5240. (error nil)))
  5241. 0
  5242. (looking-at org-outline-regexp)
  5243. (1- (- (match-end 0) (match-beginning 0))))))
  5244. (defvar org-font-lock-keywords nil)
  5245. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5246. "Regular expression matching a property line.")
  5247. (defvar org-font-lock-hook nil
  5248. "Functions to be called for special font lock stuff.")
  5249. (defvar org-font-lock-set-keywords-hook nil
  5250. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5251. This is called after `org-font-lock-extra-keywords' is defined, but before
  5252. it is installed to be used by font lock. This can be useful if something
  5253. needs to be inserted at a specific position in the font-lock sequence.")
  5254. (defun org-font-lock-hook (limit)
  5255. "Run `org-font-lock-hook' within LIMIT."
  5256. (run-hook-with-args 'org-font-lock-hook limit))
  5257. (defun org-set-font-lock-defaults ()
  5258. "Set font lock defaults for the current buffer."
  5259. (let* ((em org-fontify-emphasized-text)
  5260. (lk org-activate-links)
  5261. (org-font-lock-extra-keywords
  5262. (list
  5263. ;; Call the hook
  5264. '(org-font-lock-hook)
  5265. ;; Headlines
  5266. `(,(if org-fontify-whole-heading-line
  5267. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5268. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5269. (1 (org-get-level-face 1))
  5270. (2 (org-get-level-face 2))
  5271. (3 (org-get-level-face 3)))
  5272. ;; Table lines
  5273. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5274. (1 'org-table t))
  5275. ;; Table internals
  5276. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5277. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5278. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5279. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5280. ;; Drawers
  5281. (list org-drawer-regexp '(0 'org-special-keyword t))
  5282. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5283. ;; Properties
  5284. (list org-property-re
  5285. '(1 'org-special-keyword t)
  5286. '(3 'org-property-value t))
  5287. ;; Links
  5288. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5289. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5290. (if (memq 'plain lk) '(org-activate-plain-links))
  5291. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5292. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5293. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5294. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5295. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5296. '(org-hide-wide-columns (0 nil append))
  5297. ;; TODO keyword
  5298. (list (format org-heading-keyword-regexp-format
  5299. org-todo-regexp)
  5300. '(2 (org-get-todo-face 2) t))
  5301. ;; DONE
  5302. (if org-fontify-done-headline
  5303. (list (format org-heading-keyword-regexp-format
  5304. (concat
  5305. "\\(?:"
  5306. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5307. "\\)"))
  5308. '(2 'org-headline-done t))
  5309. nil)
  5310. ;; Priorities
  5311. '(org-font-lock-add-priority-faces)
  5312. ;; Tags
  5313. '(org-font-lock-add-tag-faces)
  5314. ;; Special keywords
  5315. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5316. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5317. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5318. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5319. ;; Emphasis
  5320. (if em
  5321. (if (featurep 'xemacs)
  5322. '(org-do-emphasis-faces (0 nil append))
  5323. '(org-do-emphasis-faces)))
  5324. ;; Checkboxes
  5325. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5326. 1 'org-checkbox prepend)
  5327. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5328. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5329. (0 (org-get-checkbox-statistics-face) t)))
  5330. ;; Description list items
  5331. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5332. 1 'org-list-dt prepend)
  5333. ;; ARCHIVEd headings
  5334. (list (concat
  5335. org-outline-regexp-bol
  5336. "\\(.*:" org-archive-tag ":.*\\)")
  5337. '(1 'org-archived prepend))
  5338. ;; Specials
  5339. '(org-fontify-entities)
  5340. '(org-raise-scripts)
  5341. ;; Code
  5342. '(org-activate-code (1 'org-code t))
  5343. ;; COMMENT
  5344. (list (format org-heading-keyword-regexp-format
  5345. (concat "\\("
  5346. org-comment-string "\\|" org-quote-string
  5347. "\\)"))
  5348. '(2 'org-special-keyword t))
  5349. ;; Blocks and meta lines
  5350. '(org-fontify-meta-lines-and-blocks)
  5351. )))
  5352. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5353. (run-hooks 'org-font-lock-set-keywords-hook)
  5354. ;; Now set the full font-lock-keywords
  5355. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5356. (org-set-local 'font-lock-defaults
  5357. '(org-font-lock-keywords t nil nil backward-paragraph))
  5358. (kill-local-variable 'font-lock-keywords) nil))
  5359. (defun org-toggle-pretty-entities ()
  5360. "Toggle the composition display of entities as UTF8 characters."
  5361. (interactive)
  5362. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5363. (org-restart-font-lock)
  5364. (if org-pretty-entities
  5365. (message "Entities are displayed as UTF8 characters")
  5366. (save-restriction
  5367. (widen)
  5368. (org-decompose-region (point-min) (point-max))
  5369. (message "Entities are displayed plain"))))
  5370. (defvar org-custom-properties-overlays nil
  5371. "List of overlays used for custom properties.")
  5372. (make-variable-buffer-local 'org-custom-properties-overlays)
  5373. (defun org-toggle-custom-properties-visibility ()
  5374. "Display or hide properties in `org-custom-properties'."
  5375. (interactive)
  5376. (if org-custom-properties-overlays
  5377. (progn (mapc 'delete-overlay org-custom-properties-overlays)
  5378. (setq org-custom-properties-overlays nil))
  5379. (unless (not org-custom-properties)
  5380. (save-excursion
  5381. (save-restriction
  5382. (widen)
  5383. (goto-char (point-min))
  5384. (while (re-search-forward org-property-re nil t)
  5385. (mapc (lambda(p)
  5386. (when (equal p (substring (match-string 1) 1 -1))
  5387. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5388. (overlay-put o 'invisible t)
  5389. (overlay-put o 'org-custom-property t)
  5390. (push o org-custom-properties-overlays))))
  5391. org-custom-properties)))))))
  5392. (defun org-fontify-entities (limit)
  5393. "Find an entity to fontify."
  5394. (let (ee)
  5395. (when org-pretty-entities
  5396. (catch 'match
  5397. (while (re-search-forward
  5398. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5399. limit t)
  5400. (if (and (not (org-in-indented-comment-line))
  5401. (setq ee (org-entity-get (match-string 1)))
  5402. (= (length (nth 6 ee)) 1))
  5403. (let*
  5404. ((end (if (equal (match-string 2) "{}")
  5405. (match-end 2)
  5406. (match-end 1))))
  5407. (add-text-properties
  5408. (match-beginning 0) end
  5409. (list 'font-lock-fontified t))
  5410. (compose-region (match-beginning 0) end
  5411. (nth 6 ee) nil)
  5412. (backward-char 1)
  5413. (throw 'match t))))
  5414. nil))))
  5415. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5416. "Fontify string S like in Org-mode."
  5417. (with-temp-buffer
  5418. (insert s)
  5419. (let ((org-odd-levels-only odd-levels))
  5420. (org-mode)
  5421. (font-lock-fontify-buffer)
  5422. (buffer-string))))
  5423. (defvar org-m nil)
  5424. (defvar org-l nil)
  5425. (defvar org-f nil)
  5426. (defun org-get-level-face (n)
  5427. "Get the right face for match N in font-lock matching of headlines."
  5428. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5429. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5430. (if org-cycle-level-faces
  5431. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5432. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5433. (cond
  5434. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5435. ((eq n 2) org-f)
  5436. (t (if org-level-color-stars-only nil org-f))))
  5437. (defun org-get-todo-face (kwd)
  5438. "Get the right face for a TODO keyword KWD.
  5439. If KWD is a number, get the corresponding match group."
  5440. (if (numberp kwd) (setq kwd (match-string kwd)))
  5441. (or (org-face-from-face-or-color
  5442. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5443. (and (member kwd org-done-keywords) 'org-done)
  5444. 'org-todo))
  5445. (defun org-face-from-face-or-color (context inherit face-or-color)
  5446. "Create a face list that inherits INHERIT, but sets the foreground color.
  5447. When FACE-OR-COLOR is not a string, just return it."
  5448. (if (stringp face-or-color)
  5449. (list :inherit inherit
  5450. (cdr (assoc context org-faces-easy-properties))
  5451. face-or-color)
  5452. face-or-color))
  5453. (defun org-font-lock-add-tag-faces (limit)
  5454. "Add the special tag faces."
  5455. (when (and org-tag-faces org-tags-special-faces-re)
  5456. (while (re-search-forward org-tags-special-faces-re limit t)
  5457. (add-text-properties (match-beginning 1) (match-end 1)
  5458. (list 'face (org-get-tag-face 1)
  5459. 'font-lock-fontified t))
  5460. (backward-char 1))))
  5461. (defun org-font-lock-add-priority-faces (limit)
  5462. "Add the special priority faces."
  5463. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5464. (when (save-match-data (org-at-heading-p))
  5465. (add-text-properties
  5466. (match-beginning 0) (match-end 0)
  5467. (list 'face (or (org-face-from-face-or-color
  5468. 'priority 'org-priority
  5469. (cdr (assoc (char-after (match-beginning 1))
  5470. org-priority-faces)))
  5471. 'org-priority)
  5472. 'font-lock-fontified t)))))
  5473. (defun org-get-tag-face (kwd)
  5474. "Get the right face for a TODO keyword KWD.
  5475. If KWD is a number, get the corresponding match group."
  5476. (if (numberp kwd) (setq kwd (match-string kwd)))
  5477. (or (org-face-from-face-or-color
  5478. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5479. 'org-tag))
  5480. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5481. "Remove fontification and activation overlays from links."
  5482. (font-lock-default-unfontify-region beg end)
  5483. (let* ((buffer-undo-list t)
  5484. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5485. (inhibit-modification-hooks t)
  5486. deactivate-mark buffer-file-name buffer-file-truename)
  5487. (org-decompose-region beg end)
  5488. (remove-text-properties beg end
  5489. '(mouse-face t keymap t org-linked-text t
  5490. invisible t intangible t
  5491. org-no-flyspell t org-emphasis t))
  5492. (org-remove-font-lock-display-properties beg end)))
  5493. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5494. ((raise 0.3) (height 0.7))
  5495. ((raise -0.5))
  5496. ((raise 0.5)))
  5497. "Display properties for showing superscripts and subscripts.")
  5498. (defun org-remove-font-lock-display-properties (beg end)
  5499. "Remove specific display properties that have been added by font lock.
  5500. The will remove the raise properties that are used to show superscripts
  5501. and subscripts."
  5502. (let (next prop)
  5503. (while (< beg end)
  5504. (setq next (next-single-property-change beg 'display nil end)
  5505. prop (get-text-property beg 'display))
  5506. (if (member prop org-script-display)
  5507. (put-text-property beg next 'display nil))
  5508. (setq beg next))))
  5509. (defun org-raise-scripts (limit)
  5510. "Add raise properties to sub/superscripts."
  5511. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5512. (if (re-search-forward
  5513. (if (eq org-use-sub-superscripts t)
  5514. org-match-substring-regexp
  5515. org-match-substring-with-braces-regexp)
  5516. limit t)
  5517. (let* ((pos (point)) table-p comment-p
  5518. (mpos (match-beginning 3))
  5519. (emph-p (get-text-property mpos 'org-emphasis))
  5520. (link-p (get-text-property mpos 'mouse-face))
  5521. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5522. (goto-char (point-at-bol))
  5523. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5524. comment-p (org-looking-at-p "[ \t]*#"))
  5525. (goto-char pos)
  5526. ;; FIXME: Should we go back one character here, for a_b^c
  5527. ;; (goto-char (1- pos)) ;????????????????????
  5528. (if (or comment-p emph-p link-p keyw-p)
  5529. t
  5530. (put-text-property (match-beginning 3) (match-end 0)
  5531. 'display
  5532. (if (equal (char-after (match-beginning 2)) ?^)
  5533. (nth (if table-p 3 1) org-script-display)
  5534. (nth (if table-p 2 0) org-script-display)))
  5535. (add-text-properties (match-beginning 2) (match-end 2)
  5536. (list 'invisible t
  5537. 'org-dwidth t 'org-dwidth-n 1))
  5538. (if (and (eq (char-after (match-beginning 3)) ?{)
  5539. (eq (char-before (match-end 3)) ?}))
  5540. (progn
  5541. (add-text-properties
  5542. (match-beginning 3) (1+ (match-beginning 3))
  5543. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5544. (add-text-properties
  5545. (1- (match-end 3)) (match-end 3)
  5546. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5547. t)))))
  5548. ;;;; Visibility cycling, including org-goto and indirect buffer
  5549. ;;; Cycling
  5550. (defvar org-cycle-global-status nil)
  5551. (make-variable-buffer-local 'org-cycle-global-status)
  5552. (defvar org-cycle-subtree-status nil)
  5553. (make-variable-buffer-local 'org-cycle-subtree-status)
  5554. (defvar org-inlinetask-min-level)
  5555. ;;;###autoload
  5556. (defun org-cycle (&optional arg)
  5557. "TAB-action and visibility cycling for Org-mode.
  5558. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5559. is outline visibility cycling, but it also invokes other actions
  5560. in special contexts.
  5561. - When this function is called with a prefix argument, rotate the entire
  5562. buffer through 3 states (global cycling)
  5563. 1. OVERVIEW: Show only top-level headlines.
  5564. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5565. 3. SHOW ALL: Show everything.
  5566. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5567. determined by the variable `org-startup-folded', and by any VISIBILITY
  5568. properties in the buffer.
  5569. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5570. including any drawers.
  5571. - When inside a table, re-align the table and move to the next field.
  5572. - When point is at the beginning of a headline, rotate the subtree started
  5573. by this line through 3 different states (local cycling)
  5574. 1. FOLDED: Only the main headline is shown.
  5575. 2. CHILDREN: The main headline and the direct children are shown.
  5576. From this state, you can move to one of the children
  5577. and zoom in further.
  5578. 3. SUBTREE: Show the entire subtree, including body text.
  5579. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5580. - When point is at the beginning of an empty headline and the variable
  5581. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5582. of the headline by demoting and promoting it to likely levels. This
  5583. speeds up creation document structure by pressing TAB once or several
  5584. times right after creating a new headline.
  5585. - When there is a numeric prefix, go up to a heading with level ARG, do
  5586. a `show-subtree' and return to the previous cursor position. If ARG
  5587. is negative, go up that many levels.
  5588. - When point is not at the beginning of a headline, execute the global
  5589. binding for TAB, which is re-indenting the line. See the option
  5590. `org-cycle-emulate-tab' for details.
  5591. - Special case: if point is at the beginning of the buffer and there is
  5592. no headline in line 1, this function will act as if called with prefix arg
  5593. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5594. But only if also the variable `org-cycle-global-at-bob' is t."
  5595. (interactive "P")
  5596. (org-load-modules-maybe)
  5597. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5598. (and org-cycle-level-after-item/entry-creation
  5599. (or (org-cycle-level)
  5600. (org-cycle-item-indentation))))
  5601. (let* (message-log-max ; Don't populate the *Messages* buffer
  5602. (limit-level
  5603. (or org-cycle-max-level
  5604. (and (boundp 'org-inlinetask-min-level)
  5605. org-inlinetask-min-level
  5606. (1- org-inlinetask-min-level))))
  5607. (nstars (and limit-level
  5608. (if org-odd-levels-only
  5609. (and limit-level (1- (* limit-level 2)))
  5610. limit-level)))
  5611. (org-outline-regexp
  5612. (if (not (derived-mode-p 'org-mode))
  5613. outline-regexp
  5614. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5615. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5616. (not (looking-at org-outline-regexp))))
  5617. (org-cycle-hook
  5618. (if bob-special
  5619. (delq 'org-optimize-window-after-visibility-change
  5620. (copy-sequence org-cycle-hook))
  5621. org-cycle-hook))
  5622. (pos (point)))
  5623. (if (or bob-special (equal arg '(4)))
  5624. ;; special case: use global cycling
  5625. (setq arg t))
  5626. (cond
  5627. ((equal arg '(16))
  5628. (setq last-command 'dummy)
  5629. (org-set-startup-visibility)
  5630. (message "Startup visibility, plus VISIBILITY properties"))
  5631. ((equal arg '(64))
  5632. (show-all)
  5633. (message "Entire buffer visible, including drawers"))
  5634. ;; Table: enter it or move to the next field.
  5635. ((org-at-table-p 'any)
  5636. (if (org-at-table.el-p)
  5637. (message "Use C-c ' to edit table.el tables")
  5638. (if arg (org-table-edit-field t)
  5639. (org-table-justify-field-maybe)
  5640. (call-interactively 'org-table-next-field))))
  5641. ((run-hook-with-args-until-success
  5642. 'org-tab-after-check-for-table-hook))
  5643. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5644. ((eq arg t) (org-cycle-internal-global))
  5645. ;; Drawers: delegate to `org-flag-drawer'.
  5646. ((and org-drawers org-drawer-regexp
  5647. (save-excursion
  5648. (beginning-of-line 1)
  5649. (looking-at org-drawer-regexp)))
  5650. (org-flag-drawer ; toggle block visibility
  5651. (not (get-char-property (match-end 0) 'invisible))))
  5652. ;; Show-subtree, ARG levels up from here.
  5653. ((integerp arg)
  5654. (save-excursion
  5655. (org-back-to-heading)
  5656. (outline-up-heading (if (< arg 0) (- arg)
  5657. (- (funcall outline-level) arg)))
  5658. (org-show-subtree)))
  5659. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5660. ((and (featurep 'org-inlinetask)
  5661. (org-inlinetask-at-task-p)
  5662. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5663. (org-inlinetask-toggle-visibility))
  5664. ((org-try-cdlatex-tab))
  5665. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5666. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5667. (save-excursion (beginning-of-line 1)
  5668. (looking-at org-outline-regexp)))
  5669. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5670. (org-cycle-internal-local))
  5671. ;; From there: TAB emulation and template completion.
  5672. (buffer-read-only (org-back-to-heading))
  5673. ((run-hook-with-args-until-success
  5674. 'org-tab-after-check-for-cycling-hook))
  5675. ((org-try-structure-completion))
  5676. ((run-hook-with-args-until-success
  5677. 'org-tab-before-tab-emulation-hook))
  5678. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5679. (or (not (bolp))
  5680. (not (looking-at org-outline-regexp))))
  5681. (call-interactively (global-key-binding "\t")))
  5682. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5683. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5684. (or (and (eq org-cycle-emulate-tab 'white)
  5685. (= (match-end 0) (point-at-eol)))
  5686. (and (eq org-cycle-emulate-tab 'whitestart)
  5687. (>= (match-end 0) pos))))
  5688. t
  5689. (eq org-cycle-emulate-tab t))
  5690. (call-interactively (global-key-binding "\t")))
  5691. (t (save-excursion
  5692. (org-back-to-heading)
  5693. (org-cycle)))))))
  5694. (defun org-cycle-internal-global ()
  5695. "Do the global cycling action."
  5696. ;; Hack to avoid display of messages for .org attachments in Gnus
  5697. (let (message-log-max ; Don't populate the *Messages* buffer
  5698. (ga (string-match "\\*fontification" (buffer-name))))
  5699. (cond
  5700. ((and (eq last-command this-command)
  5701. (eq org-cycle-global-status 'overview))
  5702. ;; We just created the overview - now do table of contents
  5703. ;; This can be slow in very large buffers, so indicate action
  5704. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5705. (unless ga (message "CONTENTS..."))
  5706. (org-content)
  5707. (unless ga (message "CONTENTS...done"))
  5708. (setq org-cycle-global-status 'contents)
  5709. (run-hook-with-args 'org-cycle-hook 'contents))
  5710. ((and (eq last-command this-command)
  5711. (eq org-cycle-global-status 'contents))
  5712. ;; We just showed the table of contents - now show everything
  5713. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5714. (show-all)
  5715. (unless ga (message "SHOW ALL"))
  5716. (setq org-cycle-global-status 'all)
  5717. (run-hook-with-args 'org-cycle-hook 'all))
  5718. (t
  5719. ;; Default action: go to overview
  5720. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5721. (org-overview)
  5722. (unless ga (message "OVERVIEW"))
  5723. (setq org-cycle-global-status 'overview)
  5724. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5725. (defun org-cycle-internal-local ()
  5726. "Do the local cycling action."
  5727. (let (message-log-max ; Don't populate the *Messages* buffer
  5728. (goal-column 0) eoh eol eos has-children children-skipped struct)
  5729. ;; First, determine end of headline (EOH), end of subtree or item
  5730. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5731. (save-excursion
  5732. (if (org-at-item-p)
  5733. (progn
  5734. (beginning-of-line)
  5735. (setq struct (org-list-struct))
  5736. (setq eoh (point-at-eol))
  5737. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5738. (setq has-children (org-list-has-child-p (point) struct)))
  5739. (org-back-to-heading)
  5740. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5741. (setq eos (save-excursion (1- (org-end-of-subtree t t))))
  5742. (setq has-children
  5743. (or (save-excursion
  5744. (let ((level (funcall outline-level)))
  5745. (outline-next-heading)
  5746. (and (org-at-heading-p t)
  5747. (> (funcall outline-level) level))))
  5748. (save-excursion
  5749. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5750. ;; Determine end invisible part of buffer (EOL)
  5751. (beginning-of-line 2)
  5752. ;; XEmacs doesn't have `next-single-char-property-change'
  5753. (if (featurep 'xemacs)
  5754. (while (and (not (eobp)) ;; this is like `next-line'
  5755. (get-char-property (1- (point)) 'invisible))
  5756. (beginning-of-line 2))
  5757. (while (and (not (eobp)) ;; this is like `next-line'
  5758. (get-char-property (1- (point)) 'invisible))
  5759. (goto-char (next-single-char-property-change (point) 'invisible))
  5760. (and (eolp) (beginning-of-line 2))))
  5761. (setq eol (point)))
  5762. ;; Find out what to do next and set `this-command'
  5763. (cond
  5764. ((= eos eoh)
  5765. ;; Nothing is hidden behind this heading
  5766. (unless (org-before-first-heading-p)
  5767. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  5768. (message "EMPTY ENTRY")
  5769. (setq org-cycle-subtree-status nil)
  5770. (save-excursion
  5771. (goto-char eos)
  5772. (outline-next-heading)
  5773. (if (outline-invisible-p) (org-flag-heading nil))))
  5774. ((and (or (>= eol eos)
  5775. (not (string-match "\\S-" (buffer-substring eol eos))))
  5776. (or has-children
  5777. (not (setq children-skipped
  5778. org-cycle-skip-children-state-if-no-children))))
  5779. ;; Entire subtree is hidden in one line: children view
  5780. (unless (org-before-first-heading-p)
  5781. (run-hook-with-args 'org-pre-cycle-hook 'children))
  5782. (if (org-at-item-p)
  5783. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5784. (org-show-entry)
  5785. (org-with-limited-levels (show-children))
  5786. ;; FIXME: This slows down the func way too much.
  5787. ;; How keep drawers hidden in subtree anyway?
  5788. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5789. ;; (org-cycle-hide-drawers 'subtree))
  5790. ;; Fold every list in subtree to top-level items.
  5791. (when (eq org-cycle-include-plain-lists 'integrate)
  5792. (save-excursion
  5793. (org-back-to-heading)
  5794. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5795. (beginning-of-line 1)
  5796. (let* ((struct (org-list-struct))
  5797. (prevs (org-list-prevs-alist struct))
  5798. (end (org-list-get-bottom-point struct)))
  5799. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5800. (org-list-get-all-items (point) struct prevs))
  5801. (goto-char end))))))
  5802. (message "CHILDREN")
  5803. (save-excursion
  5804. (goto-char eos)
  5805. (outline-next-heading)
  5806. (if (outline-invisible-p) (org-flag-heading nil)))
  5807. (setq org-cycle-subtree-status 'children)
  5808. (unless (org-before-first-heading-p)
  5809. (run-hook-with-args 'org-cycle-hook 'children)))
  5810. ((or children-skipped
  5811. (and (eq last-command this-command)
  5812. (eq org-cycle-subtree-status 'children)))
  5813. ;; We just showed the children, or no children are there,
  5814. ;; now show everything.
  5815. (unless (org-before-first-heading-p)
  5816. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  5817. (outline-flag-region eoh eos nil)
  5818. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5819. (setq org-cycle-subtree-status 'subtree)
  5820. (unless (org-before-first-heading-p)
  5821. (run-hook-with-args 'org-cycle-hook 'subtree)))
  5822. (t
  5823. ;; Default action: hide the subtree.
  5824. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5825. (outline-flag-region eoh eos t)
  5826. (message "FOLDED")
  5827. (setq org-cycle-subtree-status 'folded)
  5828. (unless (org-before-first-heading-p)
  5829. (run-hook-with-args 'org-cycle-hook 'folded))))))
  5830. ;;;###autoload
  5831. (defun org-global-cycle (&optional arg)
  5832. "Cycle the global visibility. For details see `org-cycle'.
  5833. With \\[universal-argument] prefix arg, switch to startup visibility.
  5834. With a numeric prefix, show all headlines up to that level."
  5835. (interactive "P")
  5836. (let ((org-cycle-include-plain-lists
  5837. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5838. (cond
  5839. ((integerp arg)
  5840. (show-all)
  5841. (hide-sublevels arg)
  5842. (setq org-cycle-global-status 'contents))
  5843. ((equal arg '(4))
  5844. (org-set-startup-visibility)
  5845. (message "Startup visibility, plus VISIBILITY properties."))
  5846. (t
  5847. (org-cycle '(4))))))
  5848. (defun org-set-startup-visibility ()
  5849. "Set the visibility required by startup options and properties."
  5850. (cond
  5851. ((eq org-startup-folded t)
  5852. (org-cycle '(4)))
  5853. ((eq org-startup-folded 'content)
  5854. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5855. (org-cycle '(4)) (org-cycle '(4)))))
  5856. (unless (eq org-startup-folded 'showeverything)
  5857. (if org-hide-block-startup (org-hide-block-all))
  5858. (org-set-visibility-according-to-property 'no-cleanup)
  5859. (org-cycle-hide-archived-subtrees 'all)
  5860. (org-cycle-hide-drawers 'all)
  5861. (org-cycle-show-empty-lines t)))
  5862. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5863. "Switch subtree visibilities according to :VISIBILITY: property."
  5864. (interactive)
  5865. (let (org-show-entry-below state)
  5866. (save-excursion
  5867. (goto-char (point-min))
  5868. (while (re-search-forward
  5869. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5870. nil t)
  5871. (setq state (match-string 1))
  5872. (save-excursion
  5873. (org-back-to-heading t)
  5874. (hide-subtree)
  5875. (org-reveal)
  5876. (cond
  5877. ((equal state '("fold" "folded"))
  5878. (hide-subtree))
  5879. ((equal state "children")
  5880. (org-show-hidden-entry)
  5881. (show-children))
  5882. ((equal state "content")
  5883. (save-excursion
  5884. (save-restriction
  5885. (org-narrow-to-subtree)
  5886. (org-content))))
  5887. ((member state '("all" "showall"))
  5888. (show-subtree)))))
  5889. (unless no-cleanup
  5890. (org-cycle-hide-archived-subtrees 'all)
  5891. (org-cycle-hide-drawers 'all)
  5892. (org-cycle-show-empty-lines 'all)))))
  5893. ;; This function uses outline-regexp instead of the more fundamental
  5894. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5895. ;; buffers, where outline-regexp is needed.
  5896. (defun org-overview ()
  5897. "Switch to overview mode, showing only top-level headlines.
  5898. Really, this shows all headlines with level equal or greater than the level
  5899. of the first headline in the buffer. This is important, because if the
  5900. first headline is not level one, then (hide-sublevels 1) gives confusing
  5901. results."
  5902. (interactive)
  5903. (let ((level (save-excursion
  5904. (goto-char (point-min))
  5905. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5906. (progn
  5907. (goto-char (match-beginning 0))
  5908. (funcall outline-level))))))
  5909. (and level (hide-sublevels level))))
  5910. (defun org-content (&optional arg)
  5911. "Show all headlines in the buffer, like a table of contents.
  5912. With numerical argument N, show content up to level N."
  5913. (interactive "P")
  5914. (save-excursion
  5915. ;; Visit all headings and show their offspring
  5916. (and (integerp arg) (org-overview))
  5917. (goto-char (point-max))
  5918. (catch 'exit
  5919. (while (and (progn (condition-case nil
  5920. (outline-previous-visible-heading 1)
  5921. (error (goto-char (point-min))))
  5922. t)
  5923. (looking-at org-outline-regexp))
  5924. (if (integerp arg)
  5925. (show-children (1- arg))
  5926. (show-branches))
  5927. (if (bobp) (throw 'exit nil))))))
  5928. (defun org-optimize-window-after-visibility-change (state)
  5929. "Adjust the window after a change in outline visibility.
  5930. This function is the default value of the hook `org-cycle-hook'."
  5931. (when (get-buffer-window (current-buffer))
  5932. (cond
  5933. ((eq state 'content) nil)
  5934. ((eq state 'all) nil)
  5935. ((eq state 'folded) nil)
  5936. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5937. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5938. (defun org-remove-empty-overlays-at (pos)
  5939. "Remove outline overlays that do not contain non-white stuff."
  5940. (mapc
  5941. (lambda (o)
  5942. (and (eq 'outline (overlay-get o 'invisible))
  5943. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5944. (overlay-end o))))
  5945. (delete-overlay o)))
  5946. (overlays-at pos)))
  5947. (defun org-clean-visibility-after-subtree-move ()
  5948. "Fix visibility issues after moving a subtree."
  5949. ;; First, find a reasonable region to look at:
  5950. ;; Start two siblings above, end three below
  5951. (let* ((beg (save-excursion
  5952. (and (org-get-last-sibling)
  5953. (org-get-last-sibling))
  5954. (point)))
  5955. (end (save-excursion
  5956. (and (org-get-next-sibling)
  5957. (org-get-next-sibling)
  5958. (org-get-next-sibling))
  5959. (if (org-at-heading-p)
  5960. (point-at-eol)
  5961. (point))))
  5962. (level (looking-at "\\*+"))
  5963. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5964. (save-excursion
  5965. (save-restriction
  5966. (narrow-to-region beg end)
  5967. (when re
  5968. ;; Properly fold already folded siblings
  5969. (goto-char (point-min))
  5970. (while (re-search-forward re nil t)
  5971. (if (and (not (outline-invisible-p))
  5972. (save-excursion
  5973. (goto-char (point-at-eol)) (outline-invisible-p)))
  5974. (hide-entry))))
  5975. (org-cycle-show-empty-lines 'overview)
  5976. (org-cycle-hide-drawers 'overview)))))
  5977. (defun org-cycle-show-empty-lines (state)
  5978. "Show empty lines above all visible headlines.
  5979. The region to be covered depends on STATE when called through
  5980. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5981. entire buffer covered. Note that an empty line is only shown if there
  5982. are at least `org-cycle-separator-lines' empty lines before the headline."
  5983. (when (not (= org-cycle-separator-lines 0))
  5984. (save-excursion
  5985. (let* ((n (abs org-cycle-separator-lines))
  5986. (re (cond
  5987. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5988. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5989. (t (let ((ns (number-to-string (- n 2))))
  5990. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5991. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5992. beg end b e)
  5993. (cond
  5994. ((memq state '(overview contents t))
  5995. (setq beg (point-min) end (point-max)))
  5996. ((memq state '(children folded))
  5997. (setq beg (point) end (progn (org-end-of-subtree t t)
  5998. (beginning-of-line 2)
  5999. (point)))))
  6000. (when beg
  6001. (goto-char beg)
  6002. (while (re-search-forward re end t)
  6003. (unless (get-char-property (match-end 1) 'invisible)
  6004. (setq e (match-end 1))
  6005. (if (< org-cycle-separator-lines 0)
  6006. (setq b (save-excursion
  6007. (goto-char (match-beginning 0))
  6008. (org-back-over-empty-lines)
  6009. (if (save-excursion
  6010. (goto-char (max (point-min) (1- (point))))
  6011. (org-at-heading-p))
  6012. (1- (point))
  6013. (point))))
  6014. (setq b (match-beginning 1)))
  6015. (outline-flag-region b e nil)))))))
  6016. ;; Never hide empty lines at the end of the file.
  6017. (save-excursion
  6018. (goto-char (point-max))
  6019. (outline-previous-heading)
  6020. (outline-end-of-heading)
  6021. (if (and (looking-at "[ \t\n]+")
  6022. (= (match-end 0) (point-max)))
  6023. (outline-flag-region (point) (match-end 0) nil))))
  6024. (defun org-show-empty-lines-in-parent ()
  6025. "Move to the parent and re-show empty lines before visible headlines."
  6026. (save-excursion
  6027. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6028. (org-cycle-show-empty-lines context))))
  6029. (defun org-files-list ()
  6030. "Return `org-agenda-files' list, plus all open org-mode files.
  6031. This is useful for operations that need to scan all of a user's
  6032. open and agenda-wise Org files."
  6033. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6034. (dolist (buf (buffer-list))
  6035. (with-current-buffer buf
  6036. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6037. (let ((file (expand-file-name (buffer-file-name))))
  6038. (unless (member file files)
  6039. (push file files))))))
  6040. files))
  6041. (defsubst org-entry-beginning-position ()
  6042. "Return the beginning position of the current entry."
  6043. (save-excursion (outline-back-to-heading t) (point)))
  6044. (defsubst org-entry-end-position ()
  6045. "Return the end position of the current entry."
  6046. (save-excursion (outline-next-heading) (point)))
  6047. (defun org-cycle-hide-drawers (state)
  6048. "Re-hide all drawers after a visibility state change."
  6049. (when (and (derived-mode-p 'org-mode)
  6050. (not (memq state '(overview folded contents))))
  6051. (save-excursion
  6052. (let* ((globalp (memq state '(contents all)))
  6053. (beg (if globalp (point-min) (point)))
  6054. (end (if globalp (point-max)
  6055. (if (eq state 'children)
  6056. (save-excursion (outline-next-heading) (point))
  6057. (org-end-of-subtree t)))))
  6058. (goto-char beg)
  6059. (while (re-search-forward org-drawer-regexp end t)
  6060. (org-flag-drawer t))))))
  6061. (defun org-cycle-hide-inline-tasks (state)
  6062. "Re-hide inline task when switching to 'contents visibility state."
  6063. (when (and (eq state 'contents)
  6064. (boundp 'org-inlinetask-min-level)
  6065. org-inlinetask-min-level)
  6066. (hide-sublevels (1- org-inlinetask-min-level))))
  6067. (defun org-flag-drawer (flag)
  6068. "When FLAG is non-nil, hide the drawer we are within.
  6069. Otherwise make it visible."
  6070. (save-excursion
  6071. (beginning-of-line 1)
  6072. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  6073. (let ((b (match-end 0)))
  6074. (if (re-search-forward
  6075. "^[ \t]*:END:"
  6076. (save-excursion (outline-next-heading) (point)) t)
  6077. (outline-flag-region b (point-at-eol) flag)
  6078. (error ":END: line missing at position %s" b))))))
  6079. (defun org-subtree-end-visible-p ()
  6080. "Is the end of the current subtree visible?"
  6081. (pos-visible-in-window-p
  6082. (save-excursion (org-end-of-subtree t) (point))))
  6083. (defun org-first-headline-recenter (&optional N)
  6084. "Move cursor to the first headline and recenter the headline.
  6085. Optional argument N means put the headline into the Nth line of the window."
  6086. (goto-char (point-min))
  6087. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6088. (beginning-of-line)
  6089. (recenter (prefix-numeric-value N))))
  6090. ;;; Saving and restoring visibility
  6091. (defun org-outline-overlay-data (&optional use-markers)
  6092. "Return a list of the locations of all outline overlays.
  6093. These are overlays with the `invisible' property value `outline'.
  6094. The return value is a list of cons cells, with start and stop
  6095. positions for each overlay.
  6096. If USE-MARKERS is set, return the positions as markers."
  6097. (let (beg end)
  6098. (save-excursion
  6099. (save-restriction
  6100. (widen)
  6101. (delq nil
  6102. (mapcar (lambda (o)
  6103. (when (eq (overlay-get o 'invisible) 'outline)
  6104. (setq beg (overlay-start o)
  6105. end (overlay-end o))
  6106. (and beg end (> end beg)
  6107. (if use-markers
  6108. (cons (move-marker (make-marker) beg)
  6109. (move-marker (make-marker) end))
  6110. (cons beg end)))))
  6111. (overlays-in (point-min) (point-max))))))))
  6112. (defun org-set-outline-overlay-data (data)
  6113. "Create visibility overlays for all positions in DATA.
  6114. DATA should have been made by `org-outline-overlay-data'."
  6115. (let (o)
  6116. (save-excursion
  6117. (save-restriction
  6118. (widen)
  6119. (show-all)
  6120. (mapc (lambda (c)
  6121. (outline-flag-region (car c) (cdr c) t))
  6122. data)))))
  6123. ;;; Folding of blocks
  6124. (defvar org-hide-block-overlays nil
  6125. "Overlays hiding blocks.")
  6126. (make-variable-buffer-local 'org-hide-block-overlays)
  6127. (defun org-block-map (function &optional start end)
  6128. "Call FUNCTION at the head of all source blocks in the current buffer.
  6129. Optional arguments START and END can be used to limit the range."
  6130. (let ((start (or start (point-min)))
  6131. (end (or end (point-max))))
  6132. (save-excursion
  6133. (goto-char start)
  6134. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6135. (save-excursion
  6136. (save-match-data
  6137. (goto-char (match-beginning 0))
  6138. (funcall function)))))))
  6139. (defun org-hide-block-toggle-all ()
  6140. "Toggle the visibility of all blocks in the current buffer."
  6141. (org-block-map #'org-hide-block-toggle))
  6142. (defun org-hide-block-all ()
  6143. "Fold all blocks in the current buffer."
  6144. (interactive)
  6145. (org-show-block-all)
  6146. (org-block-map #'org-hide-block-toggle-maybe))
  6147. (defun org-show-block-all ()
  6148. "Unfold all blocks in the current buffer."
  6149. (interactive)
  6150. (mapc 'delete-overlay org-hide-block-overlays)
  6151. (setq org-hide-block-overlays nil))
  6152. (defun org-hide-block-toggle-maybe ()
  6153. "Toggle visibility of block at point."
  6154. (interactive)
  6155. (let ((case-fold-search t))
  6156. (if (save-excursion
  6157. (beginning-of-line 1)
  6158. (looking-at org-block-regexp))
  6159. (progn (org-hide-block-toggle)
  6160. t) ;; to signal that we took action
  6161. nil))) ;; to signal that we did not
  6162. (defun org-hide-block-toggle (&optional force)
  6163. "Toggle the visibility of the current block."
  6164. (interactive)
  6165. (save-excursion
  6166. (beginning-of-line)
  6167. (if (re-search-forward org-block-regexp nil t)
  6168. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6169. (end (match-end 0)) ;; end of entire body
  6170. ov)
  6171. (if (memq t (mapcar (lambda (overlay)
  6172. (eq (overlay-get overlay 'invisible)
  6173. 'org-hide-block))
  6174. (overlays-at start)))
  6175. (if (or (not force) (eq force 'off))
  6176. (mapc (lambda (ov)
  6177. (when (member ov org-hide-block-overlays)
  6178. (setq org-hide-block-overlays
  6179. (delq ov org-hide-block-overlays)))
  6180. (when (eq (overlay-get ov 'invisible)
  6181. 'org-hide-block)
  6182. (delete-overlay ov)))
  6183. (overlays-at start)))
  6184. (setq ov (make-overlay start end))
  6185. (overlay-put ov 'invisible 'org-hide-block)
  6186. ;; make the block accessible to isearch
  6187. (overlay-put
  6188. ov 'isearch-open-invisible
  6189. (lambda (ov)
  6190. (when (member ov org-hide-block-overlays)
  6191. (setq org-hide-block-overlays
  6192. (delq ov org-hide-block-overlays)))
  6193. (when (eq (overlay-get ov 'invisible)
  6194. 'org-hide-block)
  6195. (delete-overlay ov))))
  6196. (push ov org-hide-block-overlays)))
  6197. (error "Not looking at a source block"))))
  6198. ;; org-tab-after-check-for-cycling-hook
  6199. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6200. ;; Remove overlays when changing major mode
  6201. (add-hook 'org-mode-hook
  6202. (lambda () (org-add-hook 'change-major-mode-hook
  6203. 'org-show-block-all 'append 'local)))
  6204. ;;; Org-goto
  6205. (defvar org-goto-window-configuration nil)
  6206. (defvar org-goto-marker nil)
  6207. (defvar org-goto-map)
  6208. (defun org-goto-map ()
  6209. "Set the keymap `org-goto'."
  6210. (setq org-goto-map
  6211. (let ((map (make-sparse-keymap)))
  6212. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6213. mouse-drag-region universal-argument org-occur))
  6214. cmd)
  6215. (while (setq cmd (pop cmds))
  6216. (substitute-key-definition cmd cmd map global-map)))
  6217. (suppress-keymap map)
  6218. (org-defkey map "\C-m" 'org-goto-ret)
  6219. (org-defkey map [(return)] 'org-goto-ret)
  6220. (org-defkey map [(left)] 'org-goto-left)
  6221. (org-defkey map [(right)] 'org-goto-right)
  6222. (org-defkey map [(control ?g)] 'org-goto-quit)
  6223. (org-defkey map "\C-i" 'org-cycle)
  6224. (org-defkey map [(tab)] 'org-cycle)
  6225. (org-defkey map [(down)] 'outline-next-visible-heading)
  6226. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6227. (if org-goto-auto-isearch
  6228. (if (fboundp 'define-key-after)
  6229. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6230. nil)
  6231. (org-defkey map "q" 'org-goto-quit)
  6232. (org-defkey map "n" 'outline-next-visible-heading)
  6233. (org-defkey map "p" 'outline-previous-visible-heading)
  6234. (org-defkey map "f" 'outline-forward-same-level)
  6235. (org-defkey map "b" 'outline-backward-same-level)
  6236. (org-defkey map "u" 'outline-up-heading))
  6237. (org-defkey map "/" 'org-occur)
  6238. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6239. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6240. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6241. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6242. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6243. map)))
  6244. (defconst org-goto-help
  6245. "Browse buffer copy, to find location or copy text.%s
  6246. RET=jump to location C-g=quit and return to previous location
  6247. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6248. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6249. ;; FIXME: Docstring does not mention both interfaces
  6250. (defun org-goto (&optional alternative-interface)
  6251. "Look up a different location in the current file, keeping current visibility.
  6252. When you want look-up or go to a different location in a
  6253. document, the fastest way is often to fold the entire buffer and
  6254. then dive into the tree. This method has the disadvantage, that
  6255. the previous location will be folded, which may not be what you
  6256. want.
  6257. This command works around this by showing a copy of the current
  6258. buffer in an indirect buffer, in overview mode. You can dive
  6259. into the tree in that copy, use org-occur and incremental search
  6260. to find a location. When pressing RET or `Q', the command
  6261. returns to the original buffer in which the visibility is still
  6262. unchanged. After RET it will also jump to the location selected
  6263. in the indirect buffer and expose the headline hierarchy above.
  6264. With a prefix argument, use the alternative interface: e.g. if
  6265. `org-goto-interface' is 'outline use 'outline-path-completion."
  6266. (interactive "P")
  6267. (org-goto-map)
  6268. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6269. (org-refile-use-outline-path t)
  6270. (org-refile-target-verify-function nil)
  6271. (interface
  6272. (if (not alternative-interface)
  6273. org-goto-interface
  6274. (if (eq org-goto-interface 'outline)
  6275. 'outline-path-completion
  6276. 'outline)))
  6277. (org-goto-start-pos (point))
  6278. (selected-point
  6279. (if (eq interface 'outline)
  6280. (car (org-get-location (current-buffer) org-goto-help))
  6281. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6282. (org-refile-check-position pa)
  6283. (nth 3 pa)))))
  6284. (if selected-point
  6285. (progn
  6286. (org-mark-ring-push org-goto-start-pos)
  6287. (goto-char selected-point)
  6288. (if (or (outline-invisible-p) (org-invisible-p2))
  6289. (org-show-context 'org-goto)))
  6290. (message "Quit"))))
  6291. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6292. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6293. (defvar org-goto-local-auto-isearch-map) ; defined below
  6294. (defun org-get-location (buf help)
  6295. "Let the user select a location in the Org-mode buffer BUF.
  6296. This function uses a recursive edit. It returns the selected position
  6297. or nil."
  6298. (org-no-popups
  6299. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6300. (isearch-hide-immediately nil)
  6301. (isearch-search-fun-function
  6302. (lambda () 'org-goto-local-search-headings))
  6303. (org-goto-selected-point org-goto-exit-command))
  6304. (save-excursion
  6305. (save-window-excursion
  6306. (delete-other-windows)
  6307. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6308. (org-pop-to-buffer-same-window
  6309. (condition-case nil
  6310. (make-indirect-buffer (current-buffer) "*org-goto*")
  6311. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6312. (with-output-to-temp-buffer "*Org Help*"
  6313. (princ (format help (if org-goto-auto-isearch
  6314. " Just type for auto-isearch."
  6315. " n/p/f/b/u to navigate, q to quit."))))
  6316. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6317. (setq buffer-read-only nil)
  6318. (let ((org-startup-truncated t)
  6319. (org-startup-folded nil)
  6320. (org-startup-align-all-tables nil))
  6321. (org-mode)
  6322. (org-overview))
  6323. (setq buffer-read-only t)
  6324. (if (and (boundp 'org-goto-start-pos)
  6325. (integer-or-marker-p org-goto-start-pos))
  6326. (let ((org-show-hierarchy-above t)
  6327. (org-show-siblings t)
  6328. (org-show-following-heading t))
  6329. (goto-char org-goto-start-pos)
  6330. (and (outline-invisible-p) (org-show-context)))
  6331. (goto-char (point-min)))
  6332. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6333. (message "Select location and press RET")
  6334. (use-local-map org-goto-map)
  6335. (recursive-edit)))
  6336. (kill-buffer "*org-goto*")
  6337. (cons org-goto-selected-point org-goto-exit-command))))
  6338. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6339. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6340. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6341. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6342. (defun org-goto-local-search-headings (string bound noerror)
  6343. "Search and make sure that any matches are in headlines."
  6344. (catch 'return
  6345. (while (if isearch-forward
  6346. (search-forward string bound noerror)
  6347. (search-backward string bound noerror))
  6348. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6349. (and (member :headline context)
  6350. (not (member :tags context))))
  6351. (throw 'return (point))))))
  6352. (defun org-goto-local-auto-isearch ()
  6353. "Start isearch."
  6354. (interactive)
  6355. (goto-char (point-min))
  6356. (let ((keys (this-command-keys)))
  6357. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6358. (isearch-mode t)
  6359. (isearch-process-search-char (string-to-char keys)))))
  6360. (defun org-goto-ret (&optional arg)
  6361. "Finish `org-goto' by going to the new location."
  6362. (interactive "P")
  6363. (setq org-goto-selected-point (point)
  6364. org-goto-exit-command 'return)
  6365. (throw 'exit nil))
  6366. (defun org-goto-left ()
  6367. "Finish `org-goto' by going to the new location."
  6368. (interactive)
  6369. (if (org-at-heading-p)
  6370. (progn
  6371. (beginning-of-line 1)
  6372. (setq org-goto-selected-point (point)
  6373. org-goto-exit-command 'left)
  6374. (throw 'exit nil))
  6375. (error "Not on a heading")))
  6376. (defun org-goto-right ()
  6377. "Finish `org-goto' by going to the new location."
  6378. (interactive)
  6379. (if (org-at-heading-p)
  6380. (progn
  6381. (setq org-goto-selected-point (point)
  6382. org-goto-exit-command 'right)
  6383. (throw 'exit nil))
  6384. (error "Not on a heading")))
  6385. (defun org-goto-quit ()
  6386. "Finish `org-goto' without cursor motion."
  6387. (interactive)
  6388. (setq org-goto-selected-point nil)
  6389. (setq org-goto-exit-command 'quit)
  6390. (throw 'exit nil))
  6391. ;;; Indirect buffer display of subtrees
  6392. (defvar org-indirect-dedicated-frame nil
  6393. "This is the frame being used for indirect tree display.")
  6394. (defvar org-last-indirect-buffer nil)
  6395. (defun org-tree-to-indirect-buffer (&optional arg)
  6396. "Create indirect buffer and narrow it to current subtree.
  6397. With a numerical prefix ARG, go up to this level and then take that tree.
  6398. If ARG is negative, go up that many levels.
  6399. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6400. indirect buffer previously made with this command, to avoid proliferation of
  6401. indirect buffers. However, when you call the command with a \
  6402. \\[universal-argument] prefix, or
  6403. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6404. is kept so that you can work with several indirect buffers at the same time.
  6405. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6406. \\[universal-argument] prefix also
  6407. requests that a new frame be made for the new buffer, so that the dedicated
  6408. frame is not changed."
  6409. (interactive "P")
  6410. (let ((cbuf (current-buffer))
  6411. (cwin (selected-window))
  6412. (pos (point))
  6413. beg end level heading ibuf)
  6414. (save-excursion
  6415. (org-back-to-heading t)
  6416. (when (numberp arg)
  6417. (setq level (org-outline-level))
  6418. (if (< arg 0) (setq arg (+ level arg)))
  6419. (while (> (setq level (org-outline-level)) arg)
  6420. (org-up-heading-safe)))
  6421. (setq beg (point)
  6422. heading (org-get-heading))
  6423. (org-end-of-subtree t t)
  6424. (if (org-at-heading-p) (backward-char 1))
  6425. (setq end (point)))
  6426. (if (and (buffer-live-p org-last-indirect-buffer)
  6427. (not (eq org-indirect-buffer-display 'new-frame))
  6428. (not arg))
  6429. (kill-buffer org-last-indirect-buffer))
  6430. (setq ibuf (org-get-indirect-buffer cbuf)
  6431. org-last-indirect-buffer ibuf)
  6432. (cond
  6433. ((or (eq org-indirect-buffer-display 'new-frame)
  6434. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6435. (select-frame (make-frame))
  6436. (delete-other-windows)
  6437. (org-pop-to-buffer-same-window ibuf)
  6438. (org-set-frame-title heading))
  6439. ((eq org-indirect-buffer-display 'dedicated-frame)
  6440. (raise-frame
  6441. (select-frame (or (and org-indirect-dedicated-frame
  6442. (frame-live-p org-indirect-dedicated-frame)
  6443. org-indirect-dedicated-frame)
  6444. (setq org-indirect-dedicated-frame (make-frame)))))
  6445. (delete-other-windows)
  6446. (org-pop-to-buffer-same-window ibuf)
  6447. (org-set-frame-title (concat "Indirect: " heading)))
  6448. ((eq org-indirect-buffer-display 'current-window)
  6449. (org-pop-to-buffer-same-window ibuf))
  6450. ((eq org-indirect-buffer-display 'other-window)
  6451. (pop-to-buffer ibuf))
  6452. (t (error "Invalid value")))
  6453. (if (featurep 'xemacs)
  6454. (save-excursion (org-mode) (turn-on-font-lock)))
  6455. (narrow-to-region beg end)
  6456. (show-all)
  6457. (goto-char pos)
  6458. (run-hook-with-args 'org-cycle-hook 'all)
  6459. (and (window-live-p cwin) (select-window cwin))))
  6460. (defun org-get-indirect-buffer (&optional buffer)
  6461. (setq buffer (or buffer (current-buffer)))
  6462. (let ((n 1) (base (buffer-name buffer)) bname)
  6463. (while (buffer-live-p
  6464. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6465. (setq n (1+ n)))
  6466. (condition-case nil
  6467. (make-indirect-buffer buffer bname 'clone)
  6468. (error (make-indirect-buffer buffer bname)))))
  6469. (defun org-set-frame-title (title)
  6470. "Set the title of the current frame to the string TITLE."
  6471. ;; FIXME: how to name a single frame in XEmacs???
  6472. (unless (featurep 'xemacs)
  6473. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6474. ;;;; Structure editing
  6475. ;;; Inserting headlines
  6476. (defun org-previous-line-empty-p ()
  6477. (save-excursion
  6478. (and (not (bobp))
  6479. (or (beginning-of-line 0) t)
  6480. (save-match-data
  6481. (looking-at "[ \t]*$")))))
  6482. (defun org-insert-heading (&optional force-heading invisible-ok)
  6483. "Insert a new heading or item with same depth at point.
  6484. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6485. If point is at the beginning of a headline, insert a sibling before the
  6486. current headline. If point is not at the beginning, split the line,
  6487. create the new headline with the text in the current line after point
  6488. \(but see also the variable `org-M-RET-may-split-line').
  6489. With a double prefix arg, force the heading to be inserted at the
  6490. end of the parent subtree.
  6491. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6492. This is important for non-interactive uses of the command."
  6493. (interactive "P")
  6494. (if (or (= (buffer-size) 0)
  6495. (and (not (save-excursion
  6496. (and (ignore-errors (org-back-to-heading invisible-ok))
  6497. (org-at-heading-p))))
  6498. (or force-heading (not (org-in-item-p)))))
  6499. (progn
  6500. (insert "\n* ")
  6501. (run-hooks 'org-insert-heading-hook))
  6502. (when (or force-heading (not (org-insert-item)))
  6503. (let* ((empty-line-p nil)
  6504. (level nil)
  6505. (on-heading (org-at-heading-p))
  6506. (head (save-excursion
  6507. (condition-case nil
  6508. (progn
  6509. (org-back-to-heading invisible-ok)
  6510. (when (and (not on-heading)
  6511. (featurep 'org-inlinetask)
  6512. (integerp org-inlinetask-min-level)
  6513. (>= (length (match-string 0))
  6514. org-inlinetask-min-level))
  6515. ;; Find a heading level before the inline task
  6516. (while (and (setq level (org-up-heading-safe))
  6517. (>= level org-inlinetask-min-level)))
  6518. (if (org-at-heading-p)
  6519. (org-back-to-heading invisible-ok)
  6520. (error "This should not happen")))
  6521. (setq empty-line-p (org-previous-line-empty-p))
  6522. (match-string 0))
  6523. (error "*"))))
  6524. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6525. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6526. pos hide-previous previous-pos)
  6527. (cond
  6528. ((and (org-at-heading-p) (bolp)
  6529. (or (bobp)
  6530. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6531. ;; insert before the current line
  6532. (open-line (if blank 2 1)))
  6533. ((and (bolp)
  6534. (not org-insert-heading-respect-content)
  6535. (or (bobp)
  6536. (save-excursion
  6537. (backward-char 1) (not (outline-invisible-p)))))
  6538. ;; insert right here
  6539. nil)
  6540. (t
  6541. ;; somewhere in the line
  6542. (save-excursion
  6543. (setq previous-pos (point-at-bol))
  6544. (end-of-line)
  6545. (setq hide-previous (outline-invisible-p)))
  6546. (and org-insert-heading-respect-content (org-show-subtree))
  6547. (let ((split
  6548. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6549. (save-excursion
  6550. (let ((p (point)))
  6551. (goto-char (point-at-bol))
  6552. (and (looking-at org-complex-heading-regexp)
  6553. (match-beginning 4)
  6554. (> p (match-beginning 4)))))))
  6555. tags pos)
  6556. (cond
  6557. (org-insert-heading-respect-content
  6558. (if (not (equal force-heading '(16)))
  6559. (org-end-of-subtree nil t)
  6560. (org-up-heading-safe)
  6561. (org-end-of-subtree nil t))
  6562. (when (featurep 'org-inlinetask)
  6563. (while (and (not (eobp))
  6564. (looking-at "\\(\\*+\\)[ \t]+")
  6565. (>= (length (match-string 1))
  6566. org-inlinetask-min-level))
  6567. (org-end-of-subtree nil t)))
  6568. (or (bolp) (newline))
  6569. (or (org-previous-line-empty-p)
  6570. (and blank (newline)))
  6571. (open-line 1))
  6572. ((org-at-heading-p)
  6573. (when hide-previous
  6574. (show-children)
  6575. (org-show-entry))
  6576. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6577. (setq tags (and (match-end 2) (match-string 2)))
  6578. (and (match-end 1)
  6579. (delete-region (match-beginning 1) (match-end 1)))
  6580. (setq pos (point-at-bol))
  6581. (or split (end-of-line 1))
  6582. (delete-horizontal-space)
  6583. (if (string-match "\\`\\*+\\'"
  6584. (buffer-substring (point-at-bol) (point)))
  6585. (insert " "))
  6586. (newline (if blank 2 1))
  6587. (when tags
  6588. (save-excursion
  6589. (goto-char pos)
  6590. (end-of-line 1)
  6591. (insert " " tags)
  6592. (org-set-tags nil 'align))))
  6593. (t
  6594. (or split (end-of-line 1))
  6595. (newline (if blank 2 1)))))))
  6596. (insert head) (just-one-space)
  6597. (setq pos (point))
  6598. (end-of-line 1)
  6599. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6600. (when (and org-insert-heading-respect-content hide-previous)
  6601. (save-excursion
  6602. (goto-char previous-pos)
  6603. (hide-subtree)))
  6604. (run-hooks 'org-insert-heading-hook)))))
  6605. (defun org-get-heading (&optional no-tags no-todo)
  6606. "Return the heading of the current entry, without the stars.
  6607. When NO-TAGS is non-nil, don't include tags.
  6608. When NO-TODO is non-nil, don't include TODO keywords."
  6609. (save-excursion
  6610. (org-back-to-heading t)
  6611. (cond
  6612. ((and no-tags no-todo)
  6613. (looking-at org-complex-heading-regexp)
  6614. (match-string 4))
  6615. (no-tags
  6616. (looking-at (concat org-outline-regexp
  6617. "\\(.*?\\)"
  6618. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6619. (match-string 1))
  6620. (no-todo
  6621. (looking-at org-todo-line-regexp)
  6622. (match-string 3))
  6623. (t (looking-at org-heading-regexp)
  6624. (match-string 2)))))
  6625. (defun org-heading-components ()
  6626. "Return the components of the current heading.
  6627. This is a list with the following elements:
  6628. - the level as an integer
  6629. - the reduced level, different if `org-odd-levels-only' is set.
  6630. - the TODO keyword, or nil
  6631. - the priority character, like ?A, or nil if no priority is given
  6632. - the headline text itself, or the tags string if no headline text
  6633. - the tags string, or nil."
  6634. (save-excursion
  6635. (org-back-to-heading t)
  6636. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6637. (list (length (match-string 1))
  6638. (org-reduced-level (length (match-string 1)))
  6639. (org-match-string-no-properties 2)
  6640. (and (match-end 3) (aref (match-string 3) 2))
  6641. (org-match-string-no-properties 4)
  6642. (org-match-string-no-properties 5)))))
  6643. (defun org-get-entry ()
  6644. "Get the entry text, after heading, entire subtree."
  6645. (save-excursion
  6646. (org-back-to-heading t)
  6647. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6648. (defun org-insert-heading-after-current ()
  6649. "Insert a new heading with same level as current, after current subtree."
  6650. (interactive)
  6651. (org-back-to-heading)
  6652. (org-insert-heading)
  6653. (org-move-subtree-down)
  6654. (end-of-line 1))
  6655. (defun org-insert-heading-respect-content ()
  6656. (interactive)
  6657. (let ((org-insert-heading-respect-content t))
  6658. (org-insert-heading t)))
  6659. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6660. (interactive "P")
  6661. (let ((org-insert-heading-respect-content t))
  6662. (org-insert-todo-heading force-state t)))
  6663. (defun org-insert-todo-heading (arg &optional force-heading)
  6664. "Insert a new heading with the same level and TODO state as current heading.
  6665. If the heading has no TODO state, or if the state is DONE, use the first
  6666. state (TODO by default). Also one prefix arg, force first state. With two
  6667. prefix args, force inserting at the end of the parent subtree."
  6668. (interactive "P")
  6669. (when (or force-heading (not (org-insert-item 'checkbox)))
  6670. (org-insert-heading (or (and (equal arg '(16)) '(16))
  6671. force-heading))
  6672. (save-excursion
  6673. (org-back-to-heading)
  6674. (outline-previous-heading)
  6675. (looking-at org-todo-line-regexp))
  6676. (let*
  6677. ((new-mark-x
  6678. (if (or arg
  6679. (not (match-beginning 2))
  6680. (member (match-string 2) org-done-keywords))
  6681. (car org-todo-keywords-1)
  6682. (match-string 2)))
  6683. (new-mark
  6684. (or
  6685. (run-hook-with-args-until-success
  6686. 'org-todo-get-default-hook new-mark-x nil)
  6687. new-mark-x)))
  6688. (beginning-of-line 1)
  6689. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6690. (if org-treat-insert-todo-heading-as-state-change
  6691. (org-todo new-mark)
  6692. (insert new-mark " "))))
  6693. (when org-provide-todo-statistics
  6694. (org-update-parent-todo-statistics))))
  6695. (defun org-insert-subheading (arg)
  6696. "Insert a new subheading and demote it.
  6697. Works for outline headings and for plain lists alike."
  6698. (interactive "P")
  6699. (org-insert-heading arg)
  6700. (cond
  6701. ((org-at-heading-p) (org-do-demote))
  6702. ((org-at-item-p) (org-indent-item))))
  6703. (defun org-insert-todo-subheading (arg)
  6704. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6705. Works for outline headings and for plain lists alike."
  6706. (interactive "P")
  6707. (org-insert-todo-heading arg)
  6708. (cond
  6709. ((org-at-heading-p) (org-do-demote))
  6710. ((org-at-item-p) (org-indent-item))))
  6711. ;;; Promotion and Demotion
  6712. (defvar org-after-demote-entry-hook nil
  6713. "Hook run after an entry has been demoted.
  6714. The cursor will be at the beginning of the entry.
  6715. When a subtree is being demoted, the hook will be called for each node.")
  6716. (defvar org-after-promote-entry-hook nil
  6717. "Hook run after an entry has been promoted.
  6718. The cursor will be at the beginning of the entry.
  6719. When a subtree is being promoted, the hook will be called for each node.")
  6720. (defun org-promote-subtree ()
  6721. "Promote the entire subtree.
  6722. See also `org-promote'."
  6723. (interactive)
  6724. (save-excursion
  6725. (org-with-limited-levels (org-map-tree 'org-promote)))
  6726. (org-fix-position-after-promote))
  6727. (defun org-demote-subtree ()
  6728. "Demote the entire subtree. See `org-demote'.
  6729. See also `org-promote'."
  6730. (interactive)
  6731. (save-excursion
  6732. (org-with-limited-levels (org-map-tree 'org-demote)))
  6733. (org-fix-position-after-promote))
  6734. (defun org-do-promote ()
  6735. "Promote the current heading higher up the tree.
  6736. If the region is active in `transient-mark-mode', promote all headings
  6737. in the region."
  6738. (interactive)
  6739. (save-excursion
  6740. (if (org-region-active-p)
  6741. (org-map-region 'org-promote (region-beginning) (region-end))
  6742. (org-promote)))
  6743. (org-fix-position-after-promote))
  6744. (defun org-do-demote ()
  6745. "Demote the current heading lower down the tree.
  6746. If the region is active in `transient-mark-mode', demote all headings
  6747. in the region."
  6748. (interactive)
  6749. (save-excursion
  6750. (if (org-region-active-p)
  6751. (org-map-region 'org-demote (region-beginning) (region-end))
  6752. (org-demote)))
  6753. (org-fix-position-after-promote))
  6754. (defun org-fix-position-after-promote ()
  6755. "Make sure that after pro/demotion cursor position is right."
  6756. (let ((pos (point)))
  6757. (when (save-excursion
  6758. (beginning-of-line 1)
  6759. (looking-at org-todo-line-regexp)
  6760. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6761. (cond ((eobp) (insert " "))
  6762. ((eolp) (insert " "))
  6763. ((equal (char-after) ?\ ) (forward-char 1))))))
  6764. (defun org-current-level ()
  6765. "Return the level of the current entry, or nil if before the first headline.
  6766. The level is the number of stars at the beginning of the headline."
  6767. (save-excursion
  6768. (org-with-limited-levels
  6769. (if (ignore-errors (org-back-to-heading t))
  6770. (funcall outline-level)))))
  6771. (defun org-get-previous-line-level ()
  6772. "Return the outline depth of the last headline before the current line.
  6773. Returns 0 for the first headline in the buffer, and nil if before the
  6774. first headline."
  6775. (let ((current-level (org-current-level))
  6776. (prev-level (when (> (line-number-at-pos) 1)
  6777. (save-excursion
  6778. (beginning-of-line 0)
  6779. (org-current-level)))))
  6780. (cond ((null current-level) nil) ; Before first headline
  6781. ((null prev-level) 0) ; At first headline
  6782. (prev-level))))
  6783. (defun org-reduced-level (l)
  6784. "Compute the effective level of a heading.
  6785. This takes into account the setting of `org-odd-levels-only'."
  6786. (cond
  6787. ((zerop l) 0)
  6788. (org-odd-levels-only (1+ (floor (/ l 2))))
  6789. (t l)))
  6790. (defun org-level-increment ()
  6791. "Return the number of stars that will be added or removed at a
  6792. time to headlines when structure editing, based on the value of
  6793. `org-odd-levels-only'."
  6794. (if org-odd-levels-only 2 1))
  6795. (defun org-get-valid-level (level &optional change)
  6796. "Rectify a level change under the influence of `org-odd-levels-only'
  6797. LEVEL is a current level, CHANGE is by how much the level should be
  6798. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6799. even level numbers will become the next higher odd number."
  6800. (if org-odd-levels-only
  6801. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6802. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6803. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6804. (max 1 (+ level (or change 0)))))
  6805. (if (boundp 'define-obsolete-function-alias)
  6806. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6807. (define-obsolete-function-alias 'org-get-legal-level
  6808. 'org-get-valid-level)
  6809. (define-obsolete-function-alias 'org-get-legal-level
  6810. 'org-get-valid-level "23.1")))
  6811. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6812. ;; ̀org-with-limited-levels'
  6813. (defun org-promote ()
  6814. "Promote the current heading higher up the tree.
  6815. If the region is active in `transient-mark-mode', promote all headings
  6816. in the region."
  6817. (org-back-to-heading t)
  6818. (let* ((level (save-match-data (funcall outline-level)))
  6819. (after-change-functions (remove 'flyspell-after-change-function
  6820. after-change-functions))
  6821. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6822. (diff (abs (- level (length up-head) -1))))
  6823. (cond ((and (= level 1) org-called-with-limited-levels
  6824. org-allow-promoting-top-level-subtree)
  6825. (replace-match "# " nil t))
  6826. ((= level 1)
  6827. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6828. (t (replace-match up-head nil t)))
  6829. ;; Fixup tag positioning
  6830. (unless (= level 1)
  6831. (and org-auto-align-tags (org-set-tags nil t))
  6832. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6833. (run-hooks 'org-after-promote-entry-hook)))
  6834. (defun org-demote ()
  6835. "Demote the current heading lower down the tree.
  6836. If the region is active in `transient-mark-mode', demote all headings
  6837. in the region."
  6838. (org-back-to-heading t)
  6839. (let* ((level (save-match-data (funcall outline-level)))
  6840. (after-change-functions (remove 'flyspell-after-change-function
  6841. after-change-functions))
  6842. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6843. (diff (abs (- level (length down-head) -1))))
  6844. (replace-match down-head nil t)
  6845. ;; Fixup tag positioning
  6846. (and org-auto-align-tags (org-set-tags nil t))
  6847. (if org-adapt-indentation (org-fixup-indentation diff))
  6848. (run-hooks 'org-after-demote-entry-hook)))
  6849. (defun org-cycle-level ()
  6850. "Cycle the level of an empty headline through possible states.
  6851. This goes first to child, then to parent, level, then up the hierarchy.
  6852. After top level, it switches back to sibling level."
  6853. (interactive)
  6854. (let ((org-adapt-indentation nil))
  6855. (when (org-point-at-end-of-empty-headline)
  6856. (setq this-command 'org-cycle-level) ; Only needed for caching
  6857. (let ((cur-level (org-current-level))
  6858. (prev-level (org-get-previous-line-level)))
  6859. (cond
  6860. ;; If first headline in file, promote to top-level.
  6861. ((= prev-level 0)
  6862. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6863. do (org-do-promote)))
  6864. ;; If same level as prev, demote one.
  6865. ((= prev-level cur-level)
  6866. (org-do-demote))
  6867. ;; If parent is top-level, promote to top level if not already.
  6868. ((= prev-level 1)
  6869. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6870. do (org-do-promote)))
  6871. ;; If top-level, return to prev-level.
  6872. ((= cur-level 1)
  6873. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6874. do (org-do-demote)))
  6875. ;; If less than prev-level, promote one.
  6876. ((< cur-level prev-level)
  6877. (org-do-promote))
  6878. ;; If deeper than prev-level, promote until higher than
  6879. ;; prev-level.
  6880. ((> cur-level prev-level)
  6881. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6882. do (org-do-promote))))
  6883. t))))
  6884. (defun org-map-tree (fun)
  6885. "Call FUN for every heading underneath the current one."
  6886. (org-back-to-heading)
  6887. (let ((level (funcall outline-level)))
  6888. (save-excursion
  6889. (funcall fun)
  6890. (while (and (progn
  6891. (outline-next-heading)
  6892. (> (funcall outline-level) level))
  6893. (not (eobp)))
  6894. (funcall fun)))))
  6895. (defun org-map-region (fun beg end)
  6896. "Call FUN for every heading between BEG and END."
  6897. (let ((org-ignore-region t))
  6898. (save-excursion
  6899. (setq end (copy-marker end))
  6900. (goto-char beg)
  6901. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6902. (< (point) end))
  6903. (funcall fun))
  6904. (while (and (progn
  6905. (outline-next-heading)
  6906. (< (point) end))
  6907. (not (eobp)))
  6908. (funcall fun)))))
  6909. (defvar org-property-end-re) ; silence byte-compiler
  6910. (defun org-fixup-indentation (diff)
  6911. "Change the indentation in the current entry by DIFF.
  6912. However, if any line in the current entry has no indentation, or if it
  6913. would end up with no indentation after the change, nothing at all is done."
  6914. (save-excursion
  6915. (let ((end (save-excursion (outline-next-heading)
  6916. (point-marker)))
  6917. (prohibit (if (> diff 0)
  6918. "^\\S-"
  6919. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6920. col)
  6921. (unless (save-excursion (end-of-line 1)
  6922. (re-search-forward prohibit end t))
  6923. (while (and (< (point) end)
  6924. (re-search-forward "^[ \t]+" end t))
  6925. (goto-char (match-end 0))
  6926. (setq col (current-column))
  6927. (if (< diff 0) (replace-match ""))
  6928. (org-indent-to-column (+ diff col))))
  6929. (move-marker end nil))))
  6930. (defun org-convert-to-odd-levels ()
  6931. "Convert an org-mode file with all levels allowed to one with odd levels.
  6932. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6933. level 5 etc."
  6934. (interactive)
  6935. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6936. (let ((outline-level 'org-outline-level)
  6937. (org-odd-levels-only nil) n)
  6938. (save-excursion
  6939. (goto-char (point-min))
  6940. (while (re-search-forward "^\\*\\*+ " nil t)
  6941. (setq n (- (length (match-string 0)) 2))
  6942. (while (>= (setq n (1- n)) 0)
  6943. (org-demote))
  6944. (end-of-line 1))))))
  6945. (defun org-convert-to-oddeven-levels ()
  6946. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6947. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6948. file contains a section with an even level, conversion would
  6949. destroy the structure of the file. An error is signaled in this
  6950. case."
  6951. (interactive)
  6952. (goto-char (point-min))
  6953. ;; First check if there are no even levels
  6954. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6955. (org-show-context t)
  6956. (error "Not all levels are odd in this file. Conversion not possible"))
  6957. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6958. (let ((outline-regexp org-outline-regexp)
  6959. (outline-level 'org-outline-level)
  6960. (org-odd-levels-only nil) n)
  6961. (save-excursion
  6962. (goto-char (point-min))
  6963. (while (re-search-forward "^\\*\\*+ " nil t)
  6964. (setq n (/ (1- (length (match-string 0))) 2))
  6965. (while (>= (setq n (1- n)) 0)
  6966. (org-promote))
  6967. (end-of-line 1))))))
  6968. (defun org-tr-level (n)
  6969. "Make N odd if required."
  6970. (if org-odd-levels-only (1+ (/ n 2)) n))
  6971. ;;; Vertical tree motion, cutting and pasting of subtrees
  6972. (defun org-move-subtree-up (&optional arg)
  6973. "Move the current subtree up past ARG headlines of the same level."
  6974. (interactive "p")
  6975. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6976. (defun org-move-subtree-down (&optional arg)
  6977. "Move the current subtree down past ARG headlines of the same level."
  6978. (interactive "p")
  6979. (setq arg (prefix-numeric-value arg))
  6980. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6981. 'org-get-last-sibling))
  6982. (ins-point (make-marker))
  6983. (cnt (abs arg))
  6984. (col (current-column))
  6985. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6986. ;; Select the tree
  6987. (org-back-to-heading)
  6988. (setq beg0 (point))
  6989. (save-excursion
  6990. (setq ne-beg (org-back-over-empty-lines))
  6991. (setq beg (point)))
  6992. (save-match-data
  6993. (save-excursion (outline-end-of-heading)
  6994. (setq folded (outline-invisible-p)))
  6995. (outline-end-of-subtree))
  6996. (outline-next-heading)
  6997. (setq ne-end (org-back-over-empty-lines))
  6998. (setq end (point))
  6999. (goto-char beg0)
  7000. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7001. ;; include less whitespace
  7002. (save-excursion
  7003. (goto-char beg)
  7004. (forward-line (- ne-beg ne-end))
  7005. (setq beg (point))))
  7006. ;; Find insertion point, with error handling
  7007. (while (> cnt 0)
  7008. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7009. (progn (goto-char beg0)
  7010. (error "Cannot move past superior level or buffer limit")))
  7011. (setq cnt (1- cnt)))
  7012. (if (> arg 0)
  7013. ;; Moving forward - still need to move over subtree
  7014. (progn (org-end-of-subtree t t)
  7015. (save-excursion
  7016. (org-back-over-empty-lines)
  7017. (or (bolp) (newline)))))
  7018. (setq ne-ins (org-back-over-empty-lines))
  7019. (move-marker ins-point (point))
  7020. (setq txt (buffer-substring beg end))
  7021. (org-save-markers-in-region beg end)
  7022. (delete-region beg end)
  7023. (org-remove-empty-overlays-at beg)
  7024. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7025. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7026. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7027. (let ((bbb (point)))
  7028. (insert-before-markers txt)
  7029. (org-reinstall-markers-in-region bbb)
  7030. (move-marker ins-point bbb))
  7031. (or (bolp) (insert "\n"))
  7032. (setq ins-end (point))
  7033. (goto-char ins-point)
  7034. (org-skip-whitespace)
  7035. (when (and (< arg 0)
  7036. (org-first-sibling-p)
  7037. (> ne-ins ne-beg))
  7038. ;; Move whitespace back to beginning
  7039. (save-excursion
  7040. (goto-char ins-end)
  7041. (let ((kill-whole-line t))
  7042. (kill-line (- ne-ins ne-beg)) (point)))
  7043. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7044. (move-marker ins-point nil)
  7045. (if folded
  7046. (hide-subtree)
  7047. (org-show-entry)
  7048. (show-children)
  7049. (org-cycle-hide-drawers 'children))
  7050. (org-clean-visibility-after-subtree-move)
  7051. ;; move back to the initial column we were at
  7052. (move-to-column col)))
  7053. (defvar org-subtree-clip ""
  7054. "Clipboard for cut and paste of subtrees.
  7055. This is actually only a copy of the kill, because we use the normal kill
  7056. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7057. (defvar org-subtree-clip-folded nil
  7058. "Was the last copied subtree folded?
  7059. This is used to fold the tree back after pasting.")
  7060. (defun org-cut-subtree (&optional n)
  7061. "Cut the current subtree into the clipboard.
  7062. With prefix arg N, cut this many sequential subtrees.
  7063. This is a short-hand for marking the subtree and then cutting it."
  7064. (interactive "p")
  7065. (org-copy-subtree n 'cut))
  7066. (defun org-copy-subtree (&optional n cut force-store-markers)
  7067. "Cut the current subtree into the clipboard.
  7068. With prefix arg N, cut this many sequential subtrees.
  7069. This is a short-hand for marking the subtree and then copying it.
  7070. If CUT is non-nil, actually cut the subtree.
  7071. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7072. of some markers in the region, even if CUT is non-nil. This is
  7073. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7074. (interactive "p")
  7075. (let (beg end folded (beg0 (point)))
  7076. (if (org-called-interactively-p 'any)
  7077. (org-back-to-heading nil) ; take what looks like a subtree
  7078. (org-back-to-heading t)) ; take what is really there
  7079. (setq beg (point))
  7080. (skip-chars-forward " \t\r\n")
  7081. (save-match-data
  7082. (save-excursion (outline-end-of-heading)
  7083. (setq folded (outline-invisible-p)))
  7084. (condition-case nil
  7085. (org-forward-heading-same-level (1- n) t)
  7086. (error nil))
  7087. (org-end-of-subtree t t))
  7088. (setq end (point))
  7089. (goto-char beg0)
  7090. (when (> end beg)
  7091. (setq org-subtree-clip-folded folded)
  7092. (when (or cut force-store-markers)
  7093. (org-save-markers-in-region beg end))
  7094. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7095. (setq org-subtree-clip (current-kill 0))
  7096. (message "%s: Subtree(s) with %d characters"
  7097. (if cut "Cut" "Copied")
  7098. (length org-subtree-clip)))))
  7099. (defun org-paste-subtree (&optional level tree for-yank)
  7100. "Paste the clipboard as a subtree, with modification of headline level.
  7101. The entire subtree is promoted or demoted in order to match a new headline
  7102. level.
  7103. If the cursor is at the beginning of a headline, the same level as
  7104. that headline is used to paste the tree
  7105. If not, the new level is derived from the *visible* headings
  7106. before and after the insertion point, and taken to be the inferior headline
  7107. level of the two. So if the previous visible heading is level 3 and the
  7108. next is level 4 (or vice versa), level 4 will be used for insertion.
  7109. This makes sure that the subtree remains an independent subtree and does
  7110. not swallow low level entries.
  7111. You can also force a different level, either by using a numeric prefix
  7112. argument, or by inserting the heading marker by hand. For example, if the
  7113. cursor is after \"*****\", then the tree will be shifted to level 5.
  7114. If optional TREE is given, use this text instead of the kill ring.
  7115. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7116. move back over whitespace before inserting, and move point to the end of
  7117. the inserted text when done."
  7118. (interactive "P")
  7119. (setq tree (or tree (and kill-ring (current-kill 0))))
  7120. (unless (org-kill-is-subtree-p tree)
  7121. (error "%s"
  7122. (substitute-command-keys
  7123. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7124. (org-with-limited-levels
  7125. (let* ((visp (not (outline-invisible-p)))
  7126. (txt tree)
  7127. (^re_ "\\(\\*+\\)[ \t]*")
  7128. (old-level (if (string-match org-outline-regexp-bol txt)
  7129. (- (match-end 0) (match-beginning 0) 1)
  7130. -1))
  7131. (force-level (cond (level (prefix-numeric-value level))
  7132. ((and (looking-at "[ \t]*$")
  7133. (string-match
  7134. "^\\*+$" (buffer-substring
  7135. (point-at-bol) (point))))
  7136. (- (match-end 1) (match-beginning 1)))
  7137. ((and (bolp)
  7138. (looking-at org-outline-regexp))
  7139. (- (match-end 0) (point) 1))))
  7140. (previous-level (save-excursion
  7141. (condition-case nil
  7142. (progn
  7143. (outline-previous-visible-heading 1)
  7144. (if (looking-at ^re_)
  7145. (- (match-end 0) (match-beginning 0) 1)
  7146. 1))
  7147. (error 1))))
  7148. (next-level (save-excursion
  7149. (condition-case nil
  7150. (progn
  7151. (or (looking-at org-outline-regexp)
  7152. (outline-next-visible-heading 1))
  7153. (if (looking-at ^re_)
  7154. (- (match-end 0) (match-beginning 0) 1)
  7155. 1))
  7156. (error 1))))
  7157. (new-level (or force-level (max previous-level next-level)))
  7158. (shift (if (or (= old-level -1)
  7159. (= new-level -1)
  7160. (= old-level new-level))
  7161. 0
  7162. (- new-level old-level)))
  7163. (delta (if (> shift 0) -1 1))
  7164. (func (if (> shift 0) 'org-demote 'org-promote))
  7165. (org-odd-levels-only nil)
  7166. beg end newend)
  7167. ;; Remove the forced level indicator
  7168. (if force-level
  7169. (delete-region (point-at-bol) (point)))
  7170. ;; Paste
  7171. (beginning-of-line (if (bolp) 1 2))
  7172. (setq beg (point))
  7173. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7174. (insert-before-markers txt)
  7175. (unless (string-match "\n\\'" txt) (insert "\n"))
  7176. (setq newend (point))
  7177. (org-reinstall-markers-in-region beg)
  7178. (setq end (point))
  7179. (goto-char beg)
  7180. (skip-chars-forward " \t\n\r")
  7181. (setq beg (point))
  7182. (if (and (outline-invisible-p) visp)
  7183. (save-excursion (outline-show-heading)))
  7184. ;; Shift if necessary
  7185. (unless (= shift 0)
  7186. (save-restriction
  7187. (narrow-to-region beg end)
  7188. (while (not (= shift 0))
  7189. (org-map-region func (point-min) (point-max))
  7190. (setq shift (+ delta shift)))
  7191. (goto-char (point-min))
  7192. (setq newend (point-max))))
  7193. (when (or (org-called-interactively-p 'interactive) for-yank)
  7194. (message "Clipboard pasted as level %d subtree" new-level))
  7195. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7196. kill-ring
  7197. (eq org-subtree-clip (current-kill 0))
  7198. org-subtree-clip-folded)
  7199. ;; The tree was folded before it was killed/copied
  7200. (hide-subtree))
  7201. (and for-yank (goto-char newend)))))
  7202. (defun org-kill-is-subtree-p (&optional txt)
  7203. "Check if the current kill is an outline subtree, or a set of trees.
  7204. Returns nil if kill does not start with a headline, or if the first
  7205. headline level is not the largest headline level in the tree.
  7206. So this will actually accept several entries of equal levels as well,
  7207. which is OK for `org-paste-subtree'.
  7208. If optional TXT is given, check this string instead of the current kill."
  7209. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7210. (re (org-get-limited-outline-regexp))
  7211. (^re (concat "^" re))
  7212. (start-level (and kill
  7213. (string-match
  7214. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7215. kill)
  7216. (- (match-end 2) (match-beginning 2) 1)))
  7217. (start (1+ (or (match-beginning 2) -1))))
  7218. (if (not start-level)
  7219. (progn
  7220. nil) ;; does not even start with a heading
  7221. (catch 'exit
  7222. (while (setq start (string-match ^re kill (1+ start)))
  7223. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7224. (throw 'exit nil)))
  7225. t))))
  7226. (defvar org-markers-to-move nil
  7227. "Markers that should be moved with a cut-and-paste operation.
  7228. Those markers are stored together with their positions relative to
  7229. the start of the region.")
  7230. (defun org-save-markers-in-region (beg end)
  7231. "Check markers in region.
  7232. If these markers are between BEG and END, record their position relative
  7233. to BEG, so that after moving the block of text, we can put the markers back
  7234. into place.
  7235. This function gets called just before an entry or tree gets cut from the
  7236. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7237. called immediately, to move the markers with the entries."
  7238. (setq org-markers-to-move nil)
  7239. (when (featurep 'org-clock)
  7240. (org-clock-save-markers-for-cut-and-paste beg end))
  7241. (when (featurep 'org-agenda)
  7242. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7243. (defun org-check-and-save-marker (marker beg end)
  7244. "Check if MARKER is between BEG and END.
  7245. If yes, remember the marker and the distance to BEG."
  7246. (when (and (marker-buffer marker)
  7247. (equal (marker-buffer marker) (current-buffer)))
  7248. (if (and (>= marker beg) (< marker end))
  7249. (push (cons marker (- marker beg)) org-markers-to-move))))
  7250. (defun org-reinstall-markers-in-region (beg)
  7251. "Move all remembered markers to their position relative to BEG."
  7252. (mapc (lambda (x)
  7253. (move-marker (car x) (+ beg (cdr x))))
  7254. org-markers-to-move)
  7255. (setq org-markers-to-move nil))
  7256. (defun org-narrow-to-subtree ()
  7257. "Narrow buffer to the current subtree."
  7258. (interactive)
  7259. (save-excursion
  7260. (save-match-data
  7261. (org-with-limited-levels
  7262. (narrow-to-region
  7263. (progn (org-back-to-heading t) (point))
  7264. (progn (org-end-of-subtree t t)
  7265. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7266. (point)))))))
  7267. (defun org-narrow-to-block ()
  7268. "Narrow buffer to the current block."
  7269. (interactive)
  7270. (let* ((case-fold-search t)
  7271. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7272. "^[ \t]*#\\+end_.*")))
  7273. (if blockp
  7274. (narrow-to-region (car blockp) (cdr blockp))
  7275. (error "Not in a block"))))
  7276. (eval-when-compile
  7277. (defvar org-property-drawer-re))
  7278. (defvar org-property-start-re) ;; defined below
  7279. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7280. "Clone the task (subtree) at point N times.
  7281. The clones will be inserted as siblings.
  7282. In interactive use, the user will be prompted for the number of
  7283. clones to be produced, and for a time SHIFT, which may be a
  7284. repeater as used in time stamps, for example `+3d'.
  7285. When a valid repeater is given and the entry contains any time
  7286. stamps, the clones will become a sequence in time, with time
  7287. stamps in the subtree shifted for each clone produced. If SHIFT
  7288. is nil or the empty string, time stamps will be left alone. The
  7289. ID property of the original subtree is removed.
  7290. If the original subtree did contain time stamps with a repeater,
  7291. the following will happen:
  7292. - the repeater will be removed in each clone
  7293. - an additional clone will be produced, with the current, unshifted
  7294. date(s) in the entry.
  7295. - the original entry will be placed *after* all the clones, with
  7296. repeater intact.
  7297. - the start days in the repeater in the original entry will be shifted
  7298. to past the last clone.
  7299. In this way you can spell out a number of instances of a repeating task,
  7300. and still retain the repeater to cover future instances of the task."
  7301. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7302. (let (beg end template task idprop
  7303. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7304. (drawer-re org-drawer-regexp))
  7305. (if (not (and (integerp n) (> n 0)))
  7306. (error "Invalid number of replications %s" n))
  7307. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7308. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7309. shift)))
  7310. (error "Invalid shift specification %s" shift))
  7311. (when doshift
  7312. (setq shift-n (string-to-number (match-string 1 shift))
  7313. shift-what (cdr (assoc (match-string 2 shift)
  7314. '(("d" . day) ("w" . week)
  7315. ("m" . month) ("y" . year))))))
  7316. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7317. (setq nmin 1 nmax n)
  7318. (org-back-to-heading t)
  7319. (setq beg (point))
  7320. (setq idprop (org-entry-get nil "ID"))
  7321. (org-end-of-subtree t t)
  7322. (or (bolp) (insert "\n"))
  7323. (setq end (point))
  7324. (setq template (buffer-substring beg end))
  7325. (when (and doshift
  7326. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7327. (delete-region beg end)
  7328. (setq end beg)
  7329. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7330. (goto-char end)
  7331. (loop for n from nmin to nmax do
  7332. ;; prepare clone
  7333. (with-temp-buffer
  7334. (insert template)
  7335. (org-mode)
  7336. (goto-char (point-min))
  7337. (org-show-subtree)
  7338. (and idprop (if org-clone-delete-id
  7339. (org-entry-delete nil "ID")
  7340. (org-id-get-create t)))
  7341. (unless (= n 0)
  7342. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7343. (kill-whole-line))
  7344. (goto-char (point-min))
  7345. (while (re-search-forward drawer-re nil t)
  7346. (mapc (lambda (d)
  7347. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7348. (goto-char (point-min))
  7349. (when doshift
  7350. (while (re-search-forward org-ts-regexp-both nil t)
  7351. (org-timestamp-change (* n shift-n) shift-what))
  7352. (unless (= n n-no-remove)
  7353. (goto-char (point-min))
  7354. (while (re-search-forward org-ts-regexp nil t)
  7355. (save-excursion
  7356. (goto-char (match-beginning 0))
  7357. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7358. (delete-region (match-beginning 1) (match-end 1)))))))
  7359. (setq task (buffer-string)))
  7360. (insert task))
  7361. (goto-char beg)))
  7362. ;;; Outline Sorting
  7363. (defun org-sort (with-case)
  7364. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7365. Optional argument WITH-CASE means sort case-sensitively."
  7366. (interactive "P")
  7367. (cond
  7368. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7369. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7370. (t
  7371. (org-call-with-arg 'org-sort-entries with-case))))
  7372. (defun org-sort-remove-invisible (s)
  7373. (remove-text-properties 0 (length s) org-rm-props s)
  7374. (while (string-match org-bracket-link-regexp s)
  7375. (setq s (replace-match (if (match-end 2)
  7376. (match-string 3 s)
  7377. (match-string 1 s)) t t s)))
  7378. s)
  7379. (defvar org-priority-regexp) ; defined later in the file
  7380. (defvar org-after-sorting-entries-or-items-hook nil
  7381. "Hook that is run after a bunch of entries or items have been sorted.
  7382. When children are sorted, the cursor is in the parent line when this
  7383. hook gets called. When a region or a plain list is sorted, the cursor
  7384. will be in the first entry of the sorted region/list.")
  7385. (defun org-sort-entries
  7386. (&optional with-case sorting-type getkey-func compare-func property)
  7387. "Sort entries on a certain level of an outline tree.
  7388. If there is an active region, the entries in the region are sorted.
  7389. Else, if the cursor is before the first entry, sort the top-level items.
  7390. Else, the children of the entry at point are sorted.
  7391. Sorting can be alphabetically, numerically, by date/time as given by
  7392. a time stamp, by a property or by priority.
  7393. The command prompts for the sorting type unless it has been given to the
  7394. function through the SORTING-TYPE argument, which needs to be a character,
  7395. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F). Here is the
  7396. precise meaning of each character:
  7397. n Numerically, by converting the beginning of the entry/item to a number.
  7398. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7399. o By order of TODO keywords.
  7400. t By date/time, either the first active time stamp in the entry, or, if
  7401. none exist, by the first inactive one.
  7402. s By the scheduled date/time.
  7403. d By deadline date/time.
  7404. c By creation time, which is assumed to be the first inactive time stamp
  7405. at the beginning of a line.
  7406. p By priority according to the cookie.
  7407. r By the value of a property.
  7408. Capital letters will reverse the sort order.
  7409. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7410. called with point at the beginning of the record. It must return either
  7411. a string or a number that should serve as the sorting key for that record.
  7412. Comparing entries ignores case by default. However, with an optional argument
  7413. WITH-CASE, the sorting considers case as well."
  7414. (interactive "P")
  7415. (let ((case-func (if with-case 'identity 'downcase))
  7416. (cmstr
  7417. ;; The clock marker is lost when using `sort-subr', let's
  7418. ;; store the clocking string.
  7419. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7420. (save-excursion
  7421. (goto-char org-clock-marker)
  7422. (looking-back "^.*") (match-string-no-properties 0))))
  7423. start beg end stars re re2
  7424. txt what tmp)
  7425. ;; Find beginning and end of region to sort
  7426. (cond
  7427. ((org-region-active-p)
  7428. ;; we will sort the region
  7429. (setq end (region-end)
  7430. what "region")
  7431. (goto-char (region-beginning))
  7432. (if (not (org-at-heading-p)) (outline-next-heading))
  7433. (setq start (point)))
  7434. ((or (org-at-heading-p)
  7435. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7436. ;; we will sort the children of the current headline
  7437. (org-back-to-heading)
  7438. (setq start (point)
  7439. end (progn (org-end-of-subtree t t)
  7440. (or (bolp) (insert "\n"))
  7441. (org-back-over-empty-lines)
  7442. (point))
  7443. what "children")
  7444. (goto-char start)
  7445. (show-subtree)
  7446. (outline-next-heading))
  7447. (t
  7448. ;; we will sort the top-level entries in this file
  7449. (goto-char (point-min))
  7450. (or (org-at-heading-p) (outline-next-heading))
  7451. (setq start (point))
  7452. (goto-char (point-max))
  7453. (beginning-of-line 1)
  7454. (when (looking-at ".*?\\S-")
  7455. ;; File ends in a non-white line
  7456. (end-of-line 1)
  7457. (insert "\n"))
  7458. (setq end (point-max))
  7459. (setq what "top-level")
  7460. (goto-char start)
  7461. (show-all)))
  7462. (setq beg (point))
  7463. (if (>= beg end) (error "Nothing to sort"))
  7464. (looking-at "\\(\\*+\\)")
  7465. (setq stars (match-string 1)
  7466. re (concat "^" (regexp-quote stars) " +")
  7467. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7468. txt (buffer-substring beg end))
  7469. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7470. (if (and (not (equal stars "*")) (string-match re2 txt))
  7471. (error "Region to sort contains a level above the first entry"))
  7472. (unless sorting-type
  7473. (message
  7474. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7475. [t]ime [s]cheduled [d]eadline [c]reated
  7476. A/N/P/R/O/F/T/S/D/C means reversed:"
  7477. what)
  7478. (setq sorting-type (read-char-exclusive))
  7479. (unless getkey-func
  7480. (and (= (downcase sorting-type) ?f)
  7481. (setq getkey-func
  7482. (org-icompleting-read "Sort using function: "
  7483. obarray 'fboundp t nil nil))
  7484. (setq getkey-func (intern getkey-func))))
  7485. (and (= (downcase sorting-type) ?r)
  7486. (not property)
  7487. (setq property
  7488. (org-icompleting-read "Property: "
  7489. (mapcar 'list (org-buffer-property-keys t))
  7490. nil t))))
  7491. (message "Sorting entries...")
  7492. (save-restriction
  7493. (narrow-to-region start end)
  7494. (let ((dcst (downcase sorting-type))
  7495. (case-fold-search nil)
  7496. (now (current-time)))
  7497. (sort-subr
  7498. (/= dcst sorting-type)
  7499. ;; This function moves to the beginning character of the "record" to
  7500. ;; be sorted.
  7501. (lambda nil
  7502. (if (re-search-forward re nil t)
  7503. (goto-char (match-beginning 0))
  7504. (goto-char (point-max))))
  7505. ;; This function moves to the last character of the "record" being
  7506. ;; sorted.
  7507. (lambda nil
  7508. (save-match-data
  7509. (condition-case nil
  7510. (outline-forward-same-level 1)
  7511. (error
  7512. (goto-char (point-max))))))
  7513. ;; This function returns the value that gets sorted against.
  7514. (lambda nil
  7515. (cond
  7516. ((= dcst ?n)
  7517. (if (looking-at org-complex-heading-regexp)
  7518. (string-to-number (match-string 4))
  7519. nil))
  7520. ((= dcst ?a)
  7521. (if (looking-at org-complex-heading-regexp)
  7522. (funcall case-func (match-string 4))
  7523. nil))
  7524. ((= dcst ?t)
  7525. (let ((end (save-excursion (outline-next-heading) (point))))
  7526. (if (or (re-search-forward org-ts-regexp end t)
  7527. (re-search-forward org-ts-regexp-both end t))
  7528. (org-time-string-to-seconds (match-string 0))
  7529. (org-float-time now))))
  7530. ((= dcst ?c)
  7531. (let ((end (save-excursion (outline-next-heading) (point))))
  7532. (if (re-search-forward
  7533. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7534. end t)
  7535. (org-time-string-to-seconds (match-string 0))
  7536. (org-float-time now))))
  7537. ((= dcst ?s)
  7538. (let ((end (save-excursion (outline-next-heading) (point))))
  7539. (if (re-search-forward org-scheduled-time-regexp end t)
  7540. (org-time-string-to-seconds (match-string 1))
  7541. (org-float-time now))))
  7542. ((= dcst ?d)
  7543. (let ((end (save-excursion (outline-next-heading) (point))))
  7544. (if (re-search-forward org-deadline-time-regexp end t)
  7545. (org-time-string-to-seconds (match-string 1))
  7546. (org-float-time now))))
  7547. ((= dcst ?p)
  7548. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7549. (string-to-char (match-string 2))
  7550. org-default-priority))
  7551. ((= dcst ?r)
  7552. (or (org-entry-get nil property) ""))
  7553. ((= dcst ?o)
  7554. (if (looking-at org-complex-heading-regexp)
  7555. (- 9999 (length (member (match-string 2)
  7556. org-todo-keywords-1)))))
  7557. ((= dcst ?f)
  7558. (if getkey-func
  7559. (progn
  7560. (setq tmp (funcall getkey-func))
  7561. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7562. tmp)
  7563. (error "Invalid key function `%s'" getkey-func)))
  7564. (t (error "Invalid sorting type `%c'" sorting-type))))
  7565. nil
  7566. (cond
  7567. ((= dcst ?a) 'string<)
  7568. ((= dcst ?f) compare-func)
  7569. ((member dcst '(?p ?t ?s ?d ?c)) '<)))))
  7570. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7571. ;; Reset the clock marker if needed
  7572. (when cmstr
  7573. (save-excursion
  7574. (goto-char start)
  7575. (search-forward cmstr nil t)
  7576. (move-marker org-clock-marker (point))))
  7577. (message "Sorting entries...done")))
  7578. (defun org-do-sort (table what &optional with-case sorting-type)
  7579. "Sort TABLE of WHAT according to SORTING-TYPE.
  7580. The user will be prompted for the SORTING-TYPE if the call to this
  7581. function does not specify it. WHAT is only for the prompt, to indicate
  7582. what is being sorted. The sorting key will be extracted from
  7583. the car of the elements of the table.
  7584. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7585. (unless sorting-type
  7586. (message
  7587. "Sort %s: [a]lphabetic, [n]umeric, [t]ime. A/N/T means reversed:"
  7588. what)
  7589. (setq sorting-type (read-char-exclusive)))
  7590. (let ((dcst (downcase sorting-type))
  7591. extractfun comparefun)
  7592. ;; Define the appropriate functions
  7593. (cond
  7594. ((= dcst ?n)
  7595. (setq extractfun 'string-to-number
  7596. comparefun (if (= dcst sorting-type) '< '>)))
  7597. ((= dcst ?a)
  7598. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7599. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7600. comparefun (if (= dcst sorting-type)
  7601. 'string<
  7602. (lambda (a b) (and (not (string< a b))
  7603. (not (string= a b)))))))
  7604. ((= dcst ?t)
  7605. (setq extractfun
  7606. (lambda (x)
  7607. (if (or (string-match org-ts-regexp x)
  7608. (string-match org-ts-regexp-both x))
  7609. (org-float-time
  7610. (org-time-string-to-time (match-string 0 x)))
  7611. 0))
  7612. comparefun (if (= dcst sorting-type) '< '>)))
  7613. (t (error "Invalid sorting type `%c'" sorting-type)))
  7614. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7615. table)
  7616. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7617. ;;; The orgstruct minor mode
  7618. ;; Define a minor mode which can be used in other modes in order to
  7619. ;; integrate the org-mode structure editing commands.
  7620. ;; This is really a hack, because the org-mode structure commands use
  7621. ;; keys which normally belong to the major mode. Here is how it
  7622. ;; works: The minor mode defines all the keys necessary to operate the
  7623. ;; structure commands, but wraps the commands into a function which
  7624. ;; tests if the cursor is currently at a headline or a plain list
  7625. ;; item. If that is the case, the structure command is used,
  7626. ;; temporarily setting many Org-mode variables like regular
  7627. ;; expressions for filling etc. However, when any of those keys is
  7628. ;; used at a different location, function uses `key-binding' to look
  7629. ;; up if the key has an associated command in another currently active
  7630. ;; keymap (minor modes, major mode, global), and executes that
  7631. ;; command. There might be problems if any of the keys is otherwise
  7632. ;; used as a prefix key.
  7633. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7634. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7635. ;; addresses this by checking explicitly for both bindings.
  7636. (defvar orgstruct-mode-map (make-sparse-keymap)
  7637. "Keymap for the minor `orgstruct-mode'.")
  7638. (defvar org-local-vars nil
  7639. "List of local variables, for use by `orgstruct-mode'.")
  7640. ;;;###autoload
  7641. (define-minor-mode orgstruct-mode
  7642. "Toggle the minor mode `orgstruct-mode'.
  7643. This mode is for using Org-mode structure commands in other
  7644. modes. The following keys behave as if Org-mode were active, if
  7645. the cursor is on a headline, or on a plain list item (both as
  7646. defined by Org-mode).
  7647. M-up Move entry/item up
  7648. M-down Move entry/item down
  7649. M-left Promote
  7650. M-right Demote
  7651. M-S-up Move entry/item up
  7652. M-S-down Move entry/item down
  7653. M-S-left Promote subtree
  7654. M-S-right Demote subtree
  7655. M-q Fill paragraph and items like in Org-mode
  7656. C-c ^ Sort entries
  7657. C-c - Cycle list bullet
  7658. TAB Cycle item visibility
  7659. M-RET Insert new heading/item
  7660. S-M-RET Insert new TODO heading / Checkbox item
  7661. C-c C-c Set tags / toggle checkbox"
  7662. nil " OrgStruct" nil
  7663. (org-load-modules-maybe)
  7664. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7665. ;;;###autoload
  7666. (defun turn-on-orgstruct ()
  7667. "Unconditionally turn on `orgstruct-mode'."
  7668. (orgstruct-mode 1))
  7669. (defvar org-fb-vars nil)
  7670. (make-variable-buffer-local 'org-fb-vars)
  7671. (defun orgstruct++-mode (&optional arg)
  7672. "Toggle `orgstruct-mode', the enhanced version of it.
  7673. In addition to setting orgstruct-mode, this also exports all
  7674. indentation and autofilling variables from org-mode into the
  7675. buffer. It will also recognize item context in multiline items."
  7676. (interactive "P")
  7677. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7678. (if (< arg 1)
  7679. (progn (orgstruct-mode -1)
  7680. (mapc (lambda(v)
  7681. (org-set-local (car v)
  7682. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7683. org-fb-vars))
  7684. (orgstruct-mode 1)
  7685. (setq org-fb-vars nil)
  7686. (let (var val)
  7687. (mapc
  7688. (lambda (x)
  7689. (when (string-match
  7690. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  7691. (symbol-name (car x)))
  7692. (setq var (car x) val (nth 1 x))
  7693. (push (list var `(quote ,(eval var))) org-fb-vars)
  7694. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7695. org-local-vars)
  7696. (org-set-local 'orgstruct-is-++ t))))
  7697. (defvar orgstruct-is-++ nil
  7698. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7699. (make-variable-buffer-local 'orgstruct-is-++)
  7700. ;;;###autoload
  7701. (defun turn-on-orgstruct++ ()
  7702. "Unconditionally turn on `orgstruct++-mode'."
  7703. (orgstruct++-mode 1))
  7704. (defun orgstruct-error ()
  7705. "Error when there is no default binding for a structure key."
  7706. (interactive)
  7707. (error "This key has no function outside structure elements"))
  7708. (defun orgstruct-setup ()
  7709. "Setup orgstruct keymaps."
  7710. (let ((nfunc 0)
  7711. (bindings
  7712. (list
  7713. '([(meta up)] org-metaup)
  7714. '([(meta down)] org-metadown)
  7715. '([(meta left)] org-metaleft)
  7716. '([(meta right)] org-metaright)
  7717. '([(meta shift up)] org-shiftmetaup)
  7718. '([(meta shift down)] org-shiftmetadown)
  7719. '([(meta shift left)] org-shiftmetaleft)
  7720. '([(meta shift right)] org-shiftmetaright)
  7721. '([?\e (up)] org-metaup)
  7722. '([?\e (down)] org-metadown)
  7723. '([?\e (left)] org-metaleft)
  7724. '([?\e (right)] org-metaright)
  7725. '([?\e (shift up)] org-shiftmetaup)
  7726. '([?\e (shift down)] org-shiftmetadown)
  7727. '([?\e (shift left)] org-shiftmetaleft)
  7728. '([?\e (shift right)] org-shiftmetaright)
  7729. '([(shift up)] org-shiftup)
  7730. '([(shift down)] org-shiftdown)
  7731. '([(shift left)] org-shiftleft)
  7732. '([(shift right)] org-shiftright)
  7733. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7734. '("\M-q" fill-paragraph)
  7735. '("\C-c^" org-sort)
  7736. '("\C-c-" org-cycle-list-bullet)))
  7737. elt key fun cmd)
  7738. (while (setq elt (pop bindings))
  7739. (setq nfunc (1+ nfunc))
  7740. (setq key (org-key (car elt))
  7741. fun (nth 1 elt)
  7742. cmd (orgstruct-make-binding fun nfunc key))
  7743. (org-defkey orgstruct-mode-map key cmd))
  7744. ;; Prevent an error for users who forgot to make autoloads
  7745. (require 'org-element)
  7746. ;; Special treatment needed for TAB and RET
  7747. (org-defkey orgstruct-mode-map [(tab)]
  7748. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7749. (org-defkey orgstruct-mode-map "\C-i"
  7750. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7751. (org-defkey orgstruct-mode-map "\M-\C-m"
  7752. (orgstruct-make-binding 'org-insert-heading 105
  7753. "\M-\C-m" [(meta return)]))
  7754. (org-defkey orgstruct-mode-map [(meta return)]
  7755. (orgstruct-make-binding 'org-insert-heading 106
  7756. [(meta return)] "\M-\C-m"))
  7757. (org-defkey orgstruct-mode-map [(shift meta return)]
  7758. (orgstruct-make-binding 'org-insert-todo-heading 107
  7759. [(meta return)] "\M-\C-m"))
  7760. (org-defkey orgstruct-mode-map "\e\C-m"
  7761. (orgstruct-make-binding 'org-insert-heading 108
  7762. "\e\C-m" [?\e (return)]))
  7763. (org-defkey orgstruct-mode-map [?\e (return)]
  7764. (orgstruct-make-binding 'org-insert-heading 109
  7765. [?\e (return)] "\e\C-m"))
  7766. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7767. (orgstruct-make-binding 'org-insert-todo-heading 110
  7768. [?\e (return)] "\e\C-m"))
  7769. (unless org-local-vars
  7770. (setq org-local-vars (org-get-local-variables)))
  7771. t))
  7772. (defun orgstruct-make-binding (fun n &rest keys)
  7773. "Create a function for binding in the structure minor mode.
  7774. FUN is the command to call inside a table. N is used to create a unique
  7775. command name. KEYS are keys that should be checked in for a command
  7776. to execute outside of tables."
  7777. (eval
  7778. (list 'defun
  7779. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7780. '(arg)
  7781. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7782. "Outside of structure, run the binding of `"
  7783. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7784. "'.")
  7785. '(interactive "p")
  7786. (list 'if
  7787. `(org-context-p 'headline 'item
  7788. (and orgstruct-is-++
  7789. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7790. 'item-body))
  7791. (list 'org-run-like-in-org-mode (list 'quote fun))
  7792. (list 'let '(orgstruct-mode)
  7793. (list 'call-interactively
  7794. (append '(or)
  7795. (mapcar (lambda (k)
  7796. (list 'key-binding k))
  7797. keys)
  7798. '('orgstruct-error))))))))
  7799. (defun org-contextualize-keys (alist contexts)
  7800. "Return valid elements in ALIST depending on CONTEXTS.
  7801. `org-agenda-custom-commands' or `org-capture-templates' are the
  7802. values used for ALIST, and `org-agenda-custom-commands-contexts'
  7803. or `org-capture-templates-contexts' are the associated contexts
  7804. definitions."
  7805. (let ((contexts
  7806. ;; normalize contexts
  7807. (mapcar
  7808. (lambda(c) (cond ((listp (cadr c))
  7809. (list (car c) (car c) (cadr c)))
  7810. ((string= "" (cadr c))
  7811. (list (car c) (car c) (caddr c)))
  7812. (t c))) contexts))
  7813. (a alist) c r s)
  7814. ;; loop over all commands or templates
  7815. (while (setq c (pop a))
  7816. (let (vrules repl)
  7817. (cond
  7818. ((not (assoc (car c) contexts))
  7819. (push c r))
  7820. ((and (assoc (car c) contexts)
  7821. (setq vrules (org-contextualize-validate-key
  7822. (car c) contexts)))
  7823. (mapc (lambda (vr)
  7824. (when (not (equal (car vr) (cadr vr)))
  7825. (setq repl vr))) vrules)
  7826. (if (not repl) (push c r)
  7827. (push (cadr repl) s)
  7828. (push
  7829. (cons (car c)
  7830. (cdr (or (assoc (cadr repl) alist)
  7831. (error "Undefined key `%s' as contextual replacement for `%s'"
  7832. (cadr repl) (car c)))))
  7833. r))))))
  7834. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  7835. (delq
  7836. nil
  7837. (delete-dups
  7838. (mapcar (lambda (x)
  7839. (let ((tpl (car x)))
  7840. (when (not (delq
  7841. nil
  7842. (mapcar (lambda(y)
  7843. (equal y tpl)) s))) x)))
  7844. (reverse r))))))
  7845. (defun org-contextualize-validate-key (key contexts)
  7846. "Check CONTEXTS for agenda or capture KEY."
  7847. (let (r rr res)
  7848. (while (setq r (pop contexts))
  7849. (mapc
  7850. (lambda (rr)
  7851. (when
  7852. (and (equal key (car r))
  7853. (if (functionp rr) (funcall rr)
  7854. (or (and (eq (car rr) 'in-file)
  7855. (buffer-file-name)
  7856. (string-match (cdr rr) (buffer-file-name)))
  7857. (and (eq (car rr) 'in-mode)
  7858. (string-match (cdr rr) (symbol-name major-mode)))
  7859. (and (eq (car rr) 'in-buffer)
  7860. (string-match (cdr rr) (buffer-name)))
  7861. (when (and (eq (car rr) 'not-in-file)
  7862. (buffer-file-name))
  7863. (not (string-match (cdr rr) (buffer-file-name))))
  7864. (when (eq (car rr) 'not-in-mode)
  7865. (not (string-match (cdr rr) (symbol-name major-mode))))
  7866. (when (eq (car rr) 'not-in-buffer)
  7867. (not (string-match (cdr rr) (buffer-name)))))))
  7868. (push r res)))
  7869. (car (last r))))
  7870. (delete-dups (delq nil res))))
  7871. (defun org-context-p (&rest contexts)
  7872. "Check if local context is any of CONTEXTS.
  7873. Possible values in the list of contexts are `table', `headline', and `item'."
  7874. (let ((pos (point)))
  7875. (goto-char (point-at-bol))
  7876. (prog1 (or (and (memq 'table contexts)
  7877. (looking-at "[ \t]*|"))
  7878. (and (memq 'headline contexts)
  7879. (looking-at org-outline-regexp))
  7880. (and (memq 'item contexts)
  7881. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7882. (and (memq 'item-body contexts)
  7883. (org-in-item-p)))
  7884. (goto-char pos))))
  7885. (defun org-get-local-variables ()
  7886. "Return a list of all local variables in an Org mode buffer."
  7887. (let (varlist)
  7888. (with-current-buffer (get-buffer-create "*Org tmp*")
  7889. (erase-buffer)
  7890. (org-mode)
  7891. (setq varlist (buffer-local-variables)))
  7892. (kill-buffer "*Org tmp*")
  7893. (delq nil
  7894. (mapcar
  7895. (lambda (x)
  7896. (setq x
  7897. (if (symbolp x)
  7898. (list x)
  7899. (list (car x) (list 'quote (cdr x)))))
  7900. (if (string-match
  7901. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  7902. (symbol-name (car x)))
  7903. x nil))
  7904. varlist))))
  7905. (defun org-clone-local-variables (from-buffer &optional regexp)
  7906. "Clone local variables from FROM-BUFFER.
  7907. Optional argument REGEXP selects variables to clone."
  7908. (mapc
  7909. (lambda (pair)
  7910. (and (symbolp (car pair))
  7911. (or (null regexp)
  7912. (string-match regexp (symbol-name (car pair))))
  7913. (set (make-local-variable (car pair))
  7914. (cdr pair))))
  7915. (buffer-local-variables from-buffer)))
  7916. ;;;###autoload
  7917. (defun org-run-like-in-org-mode (cmd)
  7918. "Run a command, pretending that the current buffer is in Org-mode.
  7919. This will temporarily bind local variables that are typically bound in
  7920. Org-mode to the values they have in Org-mode, and then interactively
  7921. call CMD."
  7922. (org-load-modules-maybe)
  7923. (unless org-local-vars
  7924. (setq org-local-vars (org-get-local-variables)))
  7925. (eval (list 'let org-local-vars
  7926. (list 'call-interactively (list 'quote cmd)))))
  7927. ;;;; Archiving
  7928. (defun org-get-category (&optional pos force-refresh)
  7929. "Get the category applying to position POS."
  7930. (save-match-data
  7931. (if force-refresh (org-refresh-category-properties))
  7932. (let ((pos (or pos (point))))
  7933. (or (get-text-property pos 'org-category)
  7934. (progn (org-refresh-category-properties)
  7935. (get-text-property pos 'org-category))))))
  7936. (defun org-refresh-category-properties ()
  7937. "Refresh category text properties in the buffer."
  7938. (let ((case-fold-search t)
  7939. (inhibit-read-only t)
  7940. (def-cat (cond
  7941. ((null org-category)
  7942. (if buffer-file-name
  7943. (file-name-sans-extension
  7944. (file-name-nondirectory buffer-file-name))
  7945. "???"))
  7946. ((symbolp org-category) (symbol-name org-category))
  7947. (t org-category)))
  7948. beg end cat pos optionp)
  7949. (org-unmodified
  7950. (save-excursion
  7951. (save-restriction
  7952. (widen)
  7953. (goto-char (point-min))
  7954. (put-text-property (point) (point-max) 'org-category def-cat)
  7955. (while (re-search-forward
  7956. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7957. (setq pos (match-end 0)
  7958. optionp (equal (char-after (match-beginning 0)) ?#)
  7959. cat (org-trim (match-string 2)))
  7960. (if optionp
  7961. (setq beg (point-at-bol) end (point-max))
  7962. (org-back-to-heading t)
  7963. (setq beg (point) end (org-end-of-subtree t t)))
  7964. (put-text-property beg end 'org-category cat)
  7965. (put-text-property beg end 'org-category-position beg)
  7966. (goto-char pos)))))))
  7967. (defun org-refresh-properties (dprop tprop)
  7968. "Refresh buffer text properties.
  7969. DPROP is the drawer property and TPROP is the corresponding text
  7970. property to set."
  7971. (let ((case-fold-search t)
  7972. (inhibit-read-only t) p)
  7973. (org-unmodified
  7974. (save-excursion
  7975. (save-restriction
  7976. (widen)
  7977. (goto-char (point-min))
  7978. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  7979. (setq p (org-match-string-no-properties 1))
  7980. (save-excursion
  7981. (org-back-to-heading t)
  7982. (put-text-property
  7983. (point-at-bol) (point-at-eol) tprop p))))))))
  7984. ;;;; Link Stuff
  7985. ;;; Link abbreviations
  7986. (defun org-link-expand-abbrev (link)
  7987. "Apply replacements as defined in `org-link-abbrev-alist'."
  7988. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7989. (let* ((key (match-string 1 link))
  7990. (as (or (assoc key org-link-abbrev-alist-local)
  7991. (assoc key org-link-abbrev-alist)))
  7992. (tag (and (match-end 2) (match-string 3 link)))
  7993. rpl)
  7994. (if (not as)
  7995. link
  7996. (setq rpl (cdr as))
  7997. (cond
  7998. ((symbolp rpl) (funcall rpl tag))
  7999. ((string-match "%(\\([^)]+\\))" rpl)
  8000. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  8001. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8002. ((string-match "%h" rpl)
  8003. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8004. (t (concat rpl tag)))))
  8005. link))
  8006. ;;; Storing and inserting links
  8007. (defvar org-insert-link-history nil
  8008. "Minibuffer history for links inserted with `org-insert-link'.")
  8009. (defvar org-stored-links nil
  8010. "Contains the links stored with `org-store-link'.")
  8011. (defvar org-store-link-plist nil
  8012. "Plist with info about the most recently link created with `org-store-link'.")
  8013. (defvar org-link-protocols nil
  8014. "Link protocols added to Org-mode using `org-add-link-type'.")
  8015. (defvar org-store-link-functions nil
  8016. "List of functions that are called to create and store a link.
  8017. Each function will be called in turn until one returns a non-nil
  8018. value. Each function should check if it is responsible for creating
  8019. this link (for example by looking at the major mode).
  8020. If not, it must exit and return nil.
  8021. If yes, it should return a non-nil value after a calling
  8022. `org-store-link-props' with a list of properties and values.
  8023. Special properties are:
  8024. :type The link prefix, like \"http\". This must be given.
  8025. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8026. This is obligatory as well.
  8027. :description Optional default description for the second pair
  8028. of brackets in an Org-mode link. The user can still change
  8029. this when inserting this link into an Org-mode buffer.
  8030. In addition to these, any additional properties can be specified
  8031. and then used in capture templates.")
  8032. (defun org-add-link-type (type &optional follow export)
  8033. "Add TYPE to the list of `org-link-types'.
  8034. Re-compute all regular expressions depending on `org-link-types'
  8035. FOLLOW and EXPORT are two functions.
  8036. FOLLOW should take the link path as the single argument and do whatever
  8037. is necessary to follow the link, for example find a file or display
  8038. a mail message.
  8039. EXPORT should format the link path for export to one of the export formats.
  8040. It should be a function accepting three arguments:
  8041. path the path of the link, the text after the prefix (like \"http:\")
  8042. desc the description of the link, if any, or a description added by
  8043. org-export-normalize-links if there is none
  8044. format the export format, a symbol like `html' or `latex' or `ascii'..
  8045. The function may use the FORMAT information to return different values
  8046. depending on the format. The return value will be put literally into
  8047. the exported file. If the return value is nil, this means Org should
  8048. do what it normally does with links which do not have EXPORT defined.
  8049. Org-mode has a built-in default for exporting links. If you are happy with
  8050. this default, there is no need to define an export function for the link
  8051. type. For a simple example of an export function, see `org-bbdb.el'."
  8052. (add-to-list 'org-link-types type t)
  8053. (org-make-link-regexps)
  8054. (if (assoc type org-link-protocols)
  8055. (setcdr (assoc type org-link-protocols) (list follow export))
  8056. (push (list type follow export) org-link-protocols)))
  8057. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8058. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8059. ;;;###autoload
  8060. (defun org-store-link (arg)
  8061. "\\<org-mode-map>Store an org-link to the current location.
  8062. This link is added to `org-stored-links' and can later be inserted
  8063. into an org-buffer with \\[org-insert-link].
  8064. For some link types, a prefix arg is interpreted.
  8065. For links to Usenet articles, arg negates `org-gnus-prefer-web-links'.
  8066. For file links, arg negates `org-context-in-file-links'.
  8067. A double prefix arg force skipping storing functions that are not
  8068. part of Org's core."
  8069. (interactive "P")
  8070. (org-load-modules-maybe)
  8071. (setq org-store-link-plist nil) ; reset
  8072. (org-with-limited-levels
  8073. (let (link cpltxt desc description search txt custom-id agenda-link sfuns sfunsn)
  8074. (cond
  8075. ((and (not (equal arg '(16)))
  8076. (setq sfuns
  8077. (delq
  8078. nil (mapcar (lambda (f) (let (fs) (if (funcall f) (push f fs))))
  8079. org-store-link-functions))
  8080. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8081. (or (and (cdr sfuns)
  8082. (funcall (intern
  8083. (completing-read "Which function for creating the link? "
  8084. sfunsn t (car sfunsn)))))
  8085. (funcall (caar sfuns)))
  8086. (setq link (plist-get org-store-link-plist :link)
  8087. desc (or (plist-get org-store-link-plist :description) link))))
  8088. ((org-src-edit-buffer-p)
  8089. (let (label gc)
  8090. (while (or (not label)
  8091. (save-excursion
  8092. (save-restriction
  8093. (widen)
  8094. (goto-char (point-min))
  8095. (re-search-forward
  8096. (regexp-quote (format org-coderef-label-format label))
  8097. nil t))))
  8098. (when label (message "Label exists already") (sit-for 2))
  8099. (setq label (read-string "Code line label: " label)))
  8100. (end-of-line 1)
  8101. (setq link (format org-coderef-label-format label))
  8102. (setq gc (- 79 (length link)))
  8103. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  8104. (insert link)
  8105. (setq link (concat "(" label ")") desc nil)))
  8106. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8107. ;; We are in the agenda, link to referenced location
  8108. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8109. (get-text-property (point) 'org-marker))))
  8110. (when m
  8111. (org-with-point-at m
  8112. (setq agenda-link
  8113. (if (org-called-interactively-p 'any)
  8114. (call-interactively 'org-store-link)
  8115. (org-store-link nil)))))))
  8116. ((eq major-mode 'calendar-mode)
  8117. (let ((cd (calendar-cursor-to-date)))
  8118. (setq link
  8119. (format-time-string
  8120. (car org-time-stamp-formats)
  8121. (apply 'encode-time
  8122. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8123. nil nil nil))))
  8124. (org-store-link-props :type "calendar" :date cd)))
  8125. ((eq major-mode 'help-mode)
  8126. (setq link (concat "help:" (save-excursion
  8127. (goto-char (point-min))
  8128. (looking-at "^[^ ]+")
  8129. (match-string 0))))
  8130. (org-store-link-props :type "help"))
  8131. ((eq major-mode 'w3-mode)
  8132. (setq cpltxt (if (and (buffer-name)
  8133. (not (string-match "Untitled" (buffer-name))))
  8134. (buffer-name)
  8135. (url-view-url t))
  8136. link (url-view-url t))
  8137. (org-store-link-props :type "w3" :url (url-view-url t)))
  8138. ((setq search (run-hook-with-args-until-success
  8139. 'org-create-file-search-functions))
  8140. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8141. "::" search))
  8142. (setq cpltxt (or description link)))
  8143. ((eq major-mode 'image-mode)
  8144. (setq cpltxt (concat "file:"
  8145. (abbreviate-file-name buffer-file-name))
  8146. link cpltxt)
  8147. (org-store-link-props :type "image" :file buffer-file-name))
  8148. ((eq major-mode 'dired-mode)
  8149. ;; link to the file in the current line
  8150. (let ((file (dired-get-filename nil t)))
  8151. (setq file (if file
  8152. (abbreviate-file-name
  8153. (expand-file-name (dired-get-filename nil t)))
  8154. ;; otherwise, no file so use current directory.
  8155. default-directory))
  8156. (setq cpltxt (concat "file:" file)
  8157. link cpltxt)))
  8158. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8159. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8160. (cond
  8161. ((org-in-regexp "<<\\(.*?\\)>>")
  8162. (setq cpltxt
  8163. (concat "file:"
  8164. (abbreviate-file-name
  8165. (buffer-file-name (buffer-base-buffer)))
  8166. "::" (match-string 1))
  8167. link cpltxt))
  8168. ((and (featurep 'org-id)
  8169. (or (eq org-id-link-to-org-use-id t)
  8170. (and (org-called-interactively-p 'any)
  8171. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8172. (and (eq org-id-link-to-org-use-id
  8173. 'create-if-interactive-and-no-custom-id)
  8174. (not custom-id))))
  8175. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8176. ;; We can make a link using the ID.
  8177. (setq link (condition-case nil
  8178. (prog1 (org-id-store-link)
  8179. (setq desc (plist-get org-store-link-plist :description)))
  8180. (error
  8181. ;; probably before first headline, link to file only
  8182. (concat "file:"
  8183. (abbreviate-file-name
  8184. (buffer-file-name (buffer-base-buffer))))))))
  8185. (t
  8186. ;; Just link to current headline
  8187. (setq cpltxt (concat "file:"
  8188. (abbreviate-file-name
  8189. (buffer-file-name (buffer-base-buffer)))))
  8190. ;; Add a context search string
  8191. (when (org-xor org-context-in-file-links arg)
  8192. (let* ((ee (org-element-at-point))
  8193. (et (org-element-type ee))
  8194. (ev (plist-get (cadr ee) :value)))
  8195. (setq txt (cond
  8196. ((org-at-heading-p) nil)
  8197. ((eq et 'keyword) ev)
  8198. ((org-region-active-p)
  8199. (buffer-substring (region-beginning) (region-end)))))
  8200. (when (or (null txt) (string-match "\\S-" txt))
  8201. (setq cpltxt
  8202. (concat cpltxt "::"
  8203. (condition-case nil
  8204. (org-make-org-heading-search-string txt)
  8205. (error "")))
  8206. desc (or (and (eq et 'keyword) ev)
  8207. (nth 4 (ignore-errors (org-heading-components)))
  8208. "NONE")))))
  8209. (if (string-match "::\\'" cpltxt)
  8210. (setq cpltxt (substring cpltxt 0 -2)))
  8211. (setq link cpltxt))))
  8212. ((buffer-file-name (buffer-base-buffer))
  8213. ;; Just link to this file here.
  8214. (setq cpltxt (concat "file:"
  8215. (abbreviate-file-name
  8216. (buffer-file-name (buffer-base-buffer)))))
  8217. ;; Add a context string
  8218. (when (org-xor org-context-in-file-links arg)
  8219. (setq txt (if (org-region-active-p)
  8220. (buffer-substring (region-beginning) (region-end))
  8221. (buffer-substring (point-at-bol) (point-at-eol))))
  8222. ;; Only use search option if there is some text.
  8223. (when (string-match "\\S-" txt)
  8224. (setq cpltxt
  8225. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8226. desc "NONE")))
  8227. (setq link cpltxt))
  8228. ((org-called-interactively-p 'interactive)
  8229. (user-error "No method for storing a link from this buffer"))
  8230. (t (setq link nil)))
  8231. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8232. (setq link (or link cpltxt)
  8233. desc (or desc cpltxt))
  8234. (cond ((equal desc "NONE") (setq desc nil))
  8235. ((string-match org-bracket-link-regexp desc)
  8236. (setq desc (replace-regexp-in-string
  8237. org-bracket-link-regexp
  8238. (concat "\\3" (if (equal (length (match-string 0 desc))
  8239. (length desc)) "*" "")) desc))))
  8240. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8241. (progn
  8242. (setq org-stored-links
  8243. (cons (list link desc) org-stored-links))
  8244. (message "Stored: %s" (or desc link))
  8245. (when custom-id
  8246. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8247. "::#" custom-id))
  8248. (setq org-stored-links
  8249. (cons (list link desc) org-stored-links))))
  8250. (or agenda-link (and link (org-make-link-string link desc)))))))
  8251. (defun org-store-link-props (&rest plist)
  8252. "Store link properties, extract names and addresses."
  8253. (let (x adr)
  8254. (when (setq x (plist-get plist :from))
  8255. (setq adr (mail-extract-address-components x))
  8256. (setq plist (plist-put plist :fromname (car adr)))
  8257. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8258. (when (setq x (plist-get plist :to))
  8259. (setq adr (mail-extract-address-components x))
  8260. (setq plist (plist-put plist :toname (car adr)))
  8261. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8262. (let ((from (plist-get plist :from))
  8263. (to (plist-get plist :to)))
  8264. (when (and from to org-from-is-user-regexp)
  8265. (setq plist
  8266. (plist-put plist :fromto
  8267. (if (string-match org-from-is-user-regexp from)
  8268. (concat "to %t")
  8269. (concat "from %f"))))))
  8270. (setq org-store-link-plist plist))
  8271. (defun org-add-link-props (&rest plist)
  8272. "Add these properties to the link property list."
  8273. (let (key value)
  8274. (while plist
  8275. (setq key (pop plist) value (pop plist))
  8276. (setq org-store-link-plist
  8277. (plist-put org-store-link-plist key value)))))
  8278. (defun org-email-link-description (&optional fmt)
  8279. "Return the description part of an email link.
  8280. This takes information from `org-store-link-plist' and formats it
  8281. according to FMT (default from `org-email-link-description-format')."
  8282. (setq fmt (or fmt org-email-link-description-format))
  8283. (let* ((p org-store-link-plist)
  8284. (to (plist-get p :toaddress))
  8285. (from (plist-get p :fromaddress))
  8286. (table
  8287. (list
  8288. (cons "%c" (plist-get p :fromto))
  8289. (cons "%F" (plist-get p :from))
  8290. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8291. (cons "%T" (plist-get p :to))
  8292. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8293. (cons "%s" (plist-get p :subject))
  8294. (cons "%d" (plist-get p :date))
  8295. (cons "%m" (plist-get p :message-id)))))
  8296. (when (string-match "%c" fmt)
  8297. ;; Check if the user wrote this message
  8298. (if (and org-from-is-user-regexp from to
  8299. (save-match-data (string-match org-from-is-user-regexp from)))
  8300. (setq fmt (replace-match "to %t" t t fmt))
  8301. (setq fmt (replace-match "from %f" t t fmt))))
  8302. (org-replace-escapes fmt table)))
  8303. (defun org-make-org-heading-search-string (&optional string)
  8304. "Make search string for the current headline or STRING."
  8305. (let ((s (or string
  8306. (and (derived-mode-p 'org-mode)
  8307. (save-excursion
  8308. (org-back-to-heading t)
  8309. (plist-get (cadr (org-element-at-point))
  8310. :raw-value)))))
  8311. (lines org-context-in-file-links))
  8312. (or string (setq s (concat "*" s))) ; Add * for headlines
  8313. (when (and string (integerp lines) (> lines 0))
  8314. (let ((slines (org-split-string s "\n")))
  8315. (when (< lines (length slines))
  8316. (setq s (mapconcat
  8317. 'identity
  8318. (reverse (nthcdr (- (length slines) lines)
  8319. (reverse slines))) "\n")))))
  8320. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8321. (defun org-make-link-string (link &optional description)
  8322. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8323. (unless (string-match "\\S-" link)
  8324. (error "Empty link"))
  8325. (when (and description
  8326. (stringp description)
  8327. (not (string-match "\\S-" description)))
  8328. (setq description nil))
  8329. (when (stringp description)
  8330. ;; Remove brackets from the description, they are fatal.
  8331. (while (string-match "\\[" description)
  8332. (setq description (replace-match "{" t t description)))
  8333. (while (string-match "\\]" description)
  8334. (setq description (replace-match "}" t t description))))
  8335. (when (equal link description)
  8336. ;; No description needed, it is identical
  8337. (setq description nil))
  8338. (when (and (not description)
  8339. (not (string-match (org-image-file-name-regexp) link))
  8340. (not (equal link (org-link-escape link))))
  8341. (setq description (org-extract-attributes link)))
  8342. (setq link
  8343. (cond ((string-match (org-image-file-name-regexp) link) link)
  8344. ((string-match org-link-types-re link)
  8345. (concat (match-string 1 link)
  8346. (org-link-escape (substring link (match-end 1)))))
  8347. (t (org-link-escape link))))
  8348. (concat "[[" link "]"
  8349. (if description (concat "[" description "]") "")
  8350. "]"))
  8351. (defconst org-link-escape-chars
  8352. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8353. "List of characters that should be escaped in link.
  8354. This is the list that is used for internal purposes.")
  8355. (defconst org-link-escape-chars-browser
  8356. '(?\ )
  8357. "List of escapes for characters that are problematic in links.
  8358. This is the list that is used before handing over to the browser.")
  8359. (defun org-link-escape (text &optional table merge)
  8360. "Return percent escaped representation of TEXT.
  8361. TEXT is a string with the text to escape.
  8362. Optional argument TABLE is a list with characters that should be
  8363. escaped. When nil, `org-link-escape-chars' is used.
  8364. If optional argument MERGE is set, merge TABLE into
  8365. `org-link-escape-chars'."
  8366. (cond
  8367. ((and table merge)
  8368. (mapc (lambda (defchr)
  8369. (unless (member defchr table)
  8370. (setq table (cons defchr table)))) org-link-escape-chars))
  8371. ((null table)
  8372. (setq table org-link-escape-chars)))
  8373. (mapconcat
  8374. (lambda (char)
  8375. (if (or (member char table)
  8376. (and (or (< char 32) (= char 37) (> char 126))
  8377. org-url-hexify-p))
  8378. (mapconcat (lambda (sequence-element)
  8379. (format "%%%.2X" sequence-element))
  8380. (or (encode-coding-char char 'utf-8)
  8381. (error "Unable to percent escape character: %s"
  8382. (char-to-string char))) "")
  8383. (char-to-string char))) text ""))
  8384. (defun org-link-unescape (str)
  8385. "Unhex hexified Unicode strings as returned from the JavaScript function
  8386. encodeURIComponent. E.g. `%C3%B6' is the german o-Umlaut."
  8387. (unless (and (null str) (string= "" str))
  8388. (let ((pos 0) (case-fold-search t) unhexed)
  8389. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8390. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8391. (setq str (replace-match unhexed t t str))
  8392. (setq pos (+ pos (length unhexed))))))
  8393. str)
  8394. (defun org-link-unescape-compound (hex)
  8395. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  8396. Note: this function also decodes single byte encodings like
  8397. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  8398. (save-match-data
  8399. (let* ((bytes (cdr (split-string hex "%")))
  8400. (ret "")
  8401. (eat 0)
  8402. (sum 0))
  8403. (while bytes
  8404. (let* ((val (string-to-number (pop bytes) 16))
  8405. (shift-xor
  8406. (if (= 0 eat)
  8407. (cond
  8408. ((>= val 252) (cons 6 252))
  8409. ((>= val 248) (cons 5 248))
  8410. ((>= val 240) (cons 4 240))
  8411. ((>= val 224) (cons 3 224))
  8412. ((>= val 192) (cons 2 192))
  8413. (t (cons 0 0)))
  8414. (cons 6 128))))
  8415. (if (>= val 192) (setq eat (car shift-xor)))
  8416. (setq val (logxor val (cdr shift-xor)))
  8417. (setq sum (+ (lsh sum (car shift-xor)) val))
  8418. (if (> eat 0) (setq eat (- eat 1)))
  8419. (cond
  8420. ((= 0 eat) ;multi byte
  8421. (setq ret (concat ret (org-char-to-string sum)))
  8422. (setq sum 0))
  8423. ((not bytes) ; single byte(s)
  8424. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8425. )) ;; end (while bytes
  8426. ret )))
  8427. (defun org-link-unescape-single-byte-sequence (hex)
  8428. "Unhexify hex-encoded single byte character sequences."
  8429. (mapconcat (lambda (byte)
  8430. (char-to-string (string-to-number byte 16)))
  8431. (cdr (split-string hex "%")) ""))
  8432. (defun org-xor (a b)
  8433. "Exclusive or."
  8434. (if a (not b) b))
  8435. (defun org-fixup-message-id-for-http (s)
  8436. "Replace special characters in a message id, so it can be used in an http query."
  8437. (when (string-match "%" s)
  8438. (setq s (mapconcat (lambda (c)
  8439. (if (eq c ?%)
  8440. "%25"
  8441. (char-to-string c)))
  8442. s "")))
  8443. (while (string-match "<" s)
  8444. (setq s (replace-match "%3C" t t s)))
  8445. (while (string-match ">" s)
  8446. (setq s (replace-match "%3E" t t s)))
  8447. (while (string-match "@" s)
  8448. (setq s (replace-match "%40" t t s)))
  8449. s)
  8450. (defun org-link-prettify (link)
  8451. "Return a human-readable representation of LINK.
  8452. The car of LINK must be a raw link the cdr of LINK must be either
  8453. a link description or nil."
  8454. (let ((desc (or (cadr link) "<no description>")))
  8455. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8456. "<" (car link) ">")))
  8457. ;;;###autoload
  8458. (defun org-insert-link-global ()
  8459. "Insert a link like Org-mode does.
  8460. This command can be called in any mode to insert a link in Org-mode syntax."
  8461. (interactive)
  8462. (org-load-modules-maybe)
  8463. (org-run-like-in-org-mode 'org-insert-link))
  8464. (defun org-insert-all-links (&optional keep)
  8465. "Insert all links in `org-stored-links'."
  8466. (interactive "P")
  8467. (let ((links (copy-sequence org-stored-links)) l)
  8468. (while (setq l (if keep (pop links) (pop org-stored-links)))
  8469. (insert "- ")
  8470. (org-insert-link nil (car l) (cadr l))
  8471. (insert "\n"))))
  8472. (defun org-link-fontify-links-to-this-file ()
  8473. "Fontify links to the current file in `org-stored-links'."
  8474. (let ((f (buffer-file-name)) a b)
  8475. (setq a (mapcar (lambda(l)
  8476. (let ((ll (car l)))
  8477. (when (and (string-match "^file:\\(.+\\)::" ll)
  8478. (equal f (expand-file-name (match-string 1 ll))))
  8479. ll)))
  8480. org-stored-links))
  8481. (when (featurep 'org-id)
  8482. (setq b (mapcar (lambda(l)
  8483. (let ((ll (car l)))
  8484. (when (and (string-match "^id:\\(.+\\)$" ll)
  8485. (equal f (expand-file-name
  8486. (or (org-id-find-id-file
  8487. (match-string 1 ll)) ""))))
  8488. ll)))
  8489. org-stored-links)))
  8490. (mapcar (lambda(l)
  8491. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  8492. (delq nil (append a b)))))
  8493. (defvar org-link-links-in-this-file nil)
  8494. (defun org-insert-link (&optional complete-file link-location default-description)
  8495. "Insert a link. At the prompt, enter the link.
  8496. Completion can be used to insert any of the link protocol prefixes like
  8497. http or ftp in use.
  8498. The history can be used to select a link previously stored with
  8499. `org-store-link'. When the empty string is entered (i.e. if you just
  8500. press RET at the prompt), the link defaults to the most recently
  8501. stored link. As SPC triggers completion in the minibuffer, you need to
  8502. use M-SPC or C-q SPC to force the insertion of a space character.
  8503. You will also be prompted for a description, and if one is given, it will
  8504. be displayed in the buffer instead of the link.
  8505. If there is already a link at point, this command will allow you to edit link
  8506. and description parts.
  8507. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8508. be selected using completion. The path to the file will be relative to the
  8509. current directory if the file is in the current directory or a subdirectory.
  8510. Otherwise, the link will be the absolute path as completed in the minibuffer
  8511. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8512. option `org-link-file-path-type'.
  8513. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8514. the current directory or below.
  8515. With three \\[universal-argument] prefixes, negate the meaning of
  8516. `org-keep-stored-link-after-insertion'.
  8517. If `org-make-link-description-function' is non-nil, this function will be
  8518. called with the link target, and the result will be the default
  8519. link description.
  8520. If the LINK-LOCATION parameter is non-nil, this value will be
  8521. used as the link location instead of reading one interactively.
  8522. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8523. be used as the default description."
  8524. (interactive "P")
  8525. (let* ((wcf (current-window-configuration))
  8526. (origbuf (current-buffer))
  8527. (region (if (org-region-active-p)
  8528. (buffer-substring (region-beginning) (region-end))))
  8529. (remove (and region (list (region-beginning) (region-end))))
  8530. (desc region)
  8531. tmphist ; byte-compile incorrectly complains about this
  8532. (link link-location)
  8533. (abbrevs org-link-abbrev-alist-local)
  8534. entry file all-prefixes auto-desc)
  8535. (cond
  8536. (link-location) ; specified by arg, just use it.
  8537. ((org-in-regexp org-bracket-link-regexp 1)
  8538. ;; We do have a link at point, and we are going to edit it.
  8539. (setq remove (list (match-beginning 0) (match-end 0)))
  8540. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8541. (setq link (read-string "Link: "
  8542. (org-link-unescape
  8543. (org-match-string-no-properties 1)))))
  8544. ((or (org-in-regexp org-angle-link-re)
  8545. (org-in-regexp org-plain-link-re))
  8546. ;; Convert to bracket link
  8547. (setq remove (list (match-beginning 0) (match-end 0))
  8548. link (read-string "Link: "
  8549. (org-remove-angle-brackets (match-string 0)))))
  8550. ((member complete-file '((4) (16)))
  8551. ;; Completing read for file names.
  8552. (setq link (org-file-complete-link complete-file)))
  8553. (t
  8554. ;; Read link, with completion for stored links.
  8555. (org-link-fontify-links-to-this-file)
  8556. (org-switch-to-buffer-other-window "*Org Links*")
  8557. (with-current-buffer "*Org Links*"
  8558. (erase-buffer)
  8559. (insert "Insert a link.
  8560. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8561. (when org-stored-links
  8562. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8563. (insert (mapconcat 'org-link-prettify
  8564. (reverse org-stored-links) "\n")))
  8565. (goto-char (point-min)))
  8566. (let ((cw (selected-window)))
  8567. (select-window (get-buffer-window "*Org Links*" 'visible))
  8568. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8569. (unless (pos-visible-in-window-p (point-max))
  8570. (org-fit-window-to-buffer))
  8571. (and (window-live-p cw) (select-window cw)))
  8572. ;; Fake a link history, containing the stored links.
  8573. (setq tmphist (append (mapcar 'car org-stored-links)
  8574. org-insert-link-history))
  8575. (setq all-prefixes (append (mapcar 'car abbrevs)
  8576. (mapcar 'car org-link-abbrev-alist)
  8577. org-link-types))
  8578. (unwind-protect
  8579. (progn
  8580. (setq link
  8581. (org-completing-read
  8582. "Link: "
  8583. (append
  8584. (mapcar (lambda (x) (concat x ":"))
  8585. all-prefixes)
  8586. (mapcar 'car org-stored-links))
  8587. nil nil nil
  8588. 'tmphist
  8589. (caar org-stored-links)))
  8590. (if (not (string-match "\\S-" link))
  8591. (error "No link selected"))
  8592. (mapc (lambda(l)
  8593. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  8594. org-stored-links)
  8595. (if (or (member link all-prefixes)
  8596. (and (equal ":" (substring link -1))
  8597. (member (substring link 0 -1) all-prefixes)
  8598. (setq link (substring link 0 -1))))
  8599. (setq link (with-current-buffer origbuf
  8600. (org-link-try-special-completion link)))))
  8601. (set-window-configuration wcf)
  8602. (kill-buffer "*Org Links*"))
  8603. (setq entry (assoc link org-stored-links))
  8604. (or entry (push link org-insert-link-history))
  8605. (setq desc (or desc (nth 1 entry)))))
  8606. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8607. (not org-keep-stored-link-after-insertion))
  8608. (setq org-stored-links (delq (assoc link org-stored-links)
  8609. org-stored-links)))
  8610. (if (string-match org-plain-link-re link)
  8611. ;; URL-like link, normalize the use of angular brackets.
  8612. (setq link (org-remove-angle-brackets link)))
  8613. ;; Check if we are linking to the current file with a search
  8614. ;; option If yes, simplify the link by using only the search
  8615. ;; option.
  8616. (when (and buffer-file-name
  8617. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  8618. (let* ((path (match-string 1 link))
  8619. (case-fold-search nil)
  8620. (search (match-string 2 link)))
  8621. (save-match-data
  8622. (if (equal (file-truename buffer-file-name) (file-truename path))
  8623. ;; We are linking to this same file, with a search option
  8624. (setq link search)))))
  8625. ;; Check if we can/should use a relative path. If yes, simplify the link
  8626. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8627. (let* ((type (match-string 1 link))
  8628. (path (match-string 2 link))
  8629. (origpath path)
  8630. (case-fold-search nil))
  8631. (cond
  8632. ((or (eq org-link-file-path-type 'absolute)
  8633. (equal complete-file '(16)))
  8634. (setq path (abbreviate-file-name (expand-file-name path))))
  8635. ((eq org-link-file-path-type 'noabbrev)
  8636. (setq path (expand-file-name path)))
  8637. ((eq org-link-file-path-type 'relative)
  8638. (setq path (file-relative-name path)))
  8639. (t
  8640. (save-match-data
  8641. (if (string-match (concat "^" (regexp-quote
  8642. (expand-file-name
  8643. (file-name-as-directory
  8644. default-directory))))
  8645. (expand-file-name path))
  8646. ;; We are linking a file with relative path name.
  8647. (setq path (substring (expand-file-name path)
  8648. (match-end 0)))
  8649. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8650. (setq link (concat type path))
  8651. (if (equal desc origpath)
  8652. (setq desc path))))
  8653. (if org-make-link-description-function
  8654. (setq desc
  8655. (or (condition-case nil
  8656. (funcall org-make-link-description-function link desc)
  8657. (error (progn (message "Can't get link description from `%s'"
  8658. (symbol-name org-make-link-description-function))
  8659. (sit-for 2) nil)))
  8660. (read-string "Description: " default-description)))
  8661. (if default-description (setq desc default-description)
  8662. (setq desc (or (and auto-desc desc)
  8663. (read-string "Description: " desc)))))
  8664. (unless (string-match "\\S-" desc) (setq desc nil))
  8665. (if remove (apply 'delete-region remove))
  8666. (insert (org-make-link-string link desc))))
  8667. (defun org-link-try-special-completion (type)
  8668. "If there is completion support for link type TYPE, offer it."
  8669. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8670. (if (functionp fun)
  8671. (funcall fun)
  8672. (read-string "Link (no completion support): " (concat type ":")))))
  8673. (defun org-file-complete-link (&optional arg)
  8674. "Create a file link using completion."
  8675. (let (file link)
  8676. (setq file (org-iread-file-name "File: "))
  8677. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8678. (pwd1 (file-name-as-directory (abbreviate-file-name
  8679. (expand-file-name ".")))))
  8680. (cond
  8681. ((equal arg '(16))
  8682. (setq link (concat
  8683. "file:"
  8684. (abbreviate-file-name (expand-file-name file)))))
  8685. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8686. (setq link (concat "file:" (match-string 1 file))))
  8687. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8688. (expand-file-name file))
  8689. (setq link (concat
  8690. "file:" (match-string 1 (expand-file-name file)))))
  8691. (t (setq link (concat "file:" file)))))
  8692. link))
  8693. (defun org-iread-file-name (&rest args)
  8694. "Read-file-name using `ido-mode' speedup if available.
  8695. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  8696. See `read-file-name' for a description of parameters."
  8697. (org-without-partial-completion
  8698. (if (and org-completion-use-ido
  8699. (fboundp 'ido-read-file-name)
  8700. (boundp 'ido-mode) ido-mode
  8701. (listp (second args)))
  8702. (let ((ido-enter-matching-directory nil))
  8703. (apply 'ido-read-file-name args))
  8704. (apply 'read-file-name args))))
  8705. (defun org-completing-read (&rest args)
  8706. "Completing-read with SPACE being a normal character."
  8707. (let ((enable-recursive-minibuffers t)
  8708. (minibuffer-local-completion-map
  8709. (copy-keymap minibuffer-local-completion-map)))
  8710. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8711. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8712. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8713. (apply 'org-icompleting-read args)))
  8714. (defun org-completing-read-no-i (&rest args)
  8715. (let (org-completion-use-ido org-completion-use-iswitchb)
  8716. (apply 'org-completing-read args)))
  8717. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8718. "Use iswitch as a completing-read replacement to choose from choices.
  8719. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8720. from."
  8721. (let* ((iswitchb-use-virtual-buffers nil)
  8722. (iswitchb-make-buflist-hook
  8723. (lambda ()
  8724. (setq iswitchb-temp-buflist choices))))
  8725. (iswitchb-read-buffer prompt)))
  8726. (defun org-icompleting-read (&rest args)
  8727. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8728. (org-without-partial-completion
  8729. (if (and org-completion-use-ido
  8730. (fboundp 'ido-completing-read)
  8731. (boundp 'ido-mode) ido-mode
  8732. (listp (second args)))
  8733. (let ((ido-enter-matching-directory nil))
  8734. (apply 'ido-completing-read (concat (car args))
  8735. (if (consp (car (nth 1 args)))
  8736. (mapcar 'car (nth 1 args))
  8737. (nth 1 args))
  8738. (cddr args)))
  8739. (if (and org-completion-use-iswitchb
  8740. (boundp 'iswitchb-mode) iswitchb-mode
  8741. (listp (second args)))
  8742. (apply 'org-iswitchb-completing-read (concat (car args))
  8743. (if (consp (car (nth 1 args)))
  8744. (mapcar 'car (nth 1 args))
  8745. (nth 1 args))
  8746. (cddr args))
  8747. (apply 'completing-read args)))))
  8748. (defun org-extract-attributes (s)
  8749. "Extract the attributes cookie from a string and set as text property."
  8750. (let (a attr (start 0) key value)
  8751. (save-match-data
  8752. (when (string-match "{{\\([^}]+\\)}}$" s)
  8753. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8754. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8755. (setq key (match-string 1 a) value (match-string 2 a)
  8756. start (match-end 0)
  8757. attr (plist-put attr (intern key) value))))
  8758. (org-add-props s nil 'org-attr attr))
  8759. s))
  8760. (defun org-extract-attributes-from-string (tag)
  8761. (let (key value attr)
  8762. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8763. (setq key (match-string 1 tag) value (match-string 2 tag)
  8764. tag (replace-match "" t t tag)
  8765. attr (plist-put attr (intern key) value)))
  8766. (cons tag attr)))
  8767. (defun org-attributes-to-string (plist)
  8768. "Format a property list into an HTML attribute list."
  8769. (let ((s "") key value)
  8770. (while plist
  8771. (setq key (pop plist) value (pop plist))
  8772. (and value
  8773. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8774. s))
  8775. ;;; Opening/following a link
  8776. (defvar org-link-search-failed nil)
  8777. (defvar org-open-link-functions nil
  8778. "Hook for functions finding a plain text link.
  8779. These functions must take a single argument, the link content.
  8780. They will be called for links that look like [[link text][description]]
  8781. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8782. like a filename (e.g. \"./blue.png\").
  8783. These functions will be called *before* Org attempts to resolve the
  8784. link by doing text searches in the current buffer - so if you want a
  8785. link \"[[target]]\" to still find \"<<target>>\", your function should
  8786. handle this as a special case.
  8787. When the function does handle the link, it must return a non-nil value.
  8788. If it decides that it is not responsible for this link, it must return
  8789. nil to indicate that that Org-mode can continue with other options
  8790. like exact and fuzzy text search.")
  8791. (defun org-next-link ()
  8792. "Move forward to the next link.
  8793. If the link is in hidden text, expose it."
  8794. (interactive)
  8795. (when (and org-link-search-failed (eq this-command last-command))
  8796. (goto-char (point-min))
  8797. (message "Link search wrapped back to beginning of buffer"))
  8798. (setq org-link-search-failed nil)
  8799. (let* ((pos (point))
  8800. (ct (org-context))
  8801. (a (assoc :link ct)))
  8802. (if a (goto-char (nth 2 a)))
  8803. (if (re-search-forward org-any-link-re nil t)
  8804. (progn
  8805. (goto-char (match-beginning 0))
  8806. (if (outline-invisible-p) (org-show-context)))
  8807. (goto-char pos)
  8808. (setq org-link-search-failed t)
  8809. (error "No further link found"))))
  8810. (defun org-previous-link ()
  8811. "Move backward to the previous link.
  8812. If the link is in hidden text, expose it."
  8813. (interactive)
  8814. (when (and org-link-search-failed (eq this-command last-command))
  8815. (goto-char (point-max))
  8816. (message "Link search wrapped back to end of buffer"))
  8817. (setq org-link-search-failed nil)
  8818. (let* ((pos (point))
  8819. (ct (org-context))
  8820. (a (assoc :link ct)))
  8821. (if a (goto-char (nth 1 a)))
  8822. (if (re-search-backward org-any-link-re nil t)
  8823. (progn
  8824. (goto-char (match-beginning 0))
  8825. (if (outline-invisible-p) (org-show-context)))
  8826. (goto-char pos)
  8827. (setq org-link-search-failed t)
  8828. (error "No further link found"))))
  8829. (defun org-translate-link (s)
  8830. "Translate a link string if a translation function has been defined."
  8831. (if (and org-link-translation-function
  8832. (fboundp org-link-translation-function)
  8833. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8834. (progn
  8835. (setq s (funcall org-link-translation-function
  8836. (match-string 1 s) (match-string 2 s)))
  8837. (concat (car s) ":" (cdr s)))
  8838. s))
  8839. (defun org-translate-link-from-planner (type path)
  8840. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8841. This is still an experimental function, your mileage may vary."
  8842. (cond
  8843. ((member type '("http" "https" "news" "ftp"))
  8844. ;; standard Internet links are the same.
  8845. nil)
  8846. ((and (equal type "irc") (string-match "^//" path))
  8847. ;; Planner has two / at the beginning of an irc link, we have 1.
  8848. ;; We should have zero, actually....
  8849. (setq path (substring path 1)))
  8850. ((and (equal type "lisp") (string-match "^/" path))
  8851. ;; Planner has a slash, we do not.
  8852. (setq type "elisp" path (substring path 1)))
  8853. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8854. ;; A typical message link. Planner has the id after the final slash,
  8855. ;; we separate it with a hash mark
  8856. (setq path (concat (match-string 1 path) "#"
  8857. (org-remove-angle-brackets (match-string 2 path)))))
  8858. )
  8859. (cons type path))
  8860. (defun org-find-file-at-mouse (ev)
  8861. "Open file link or URL at mouse."
  8862. (interactive "e")
  8863. (mouse-set-point ev)
  8864. (org-open-at-point 'in-emacs))
  8865. (defun org-open-at-mouse (ev)
  8866. "Open file link or URL at mouse.
  8867. See the docstring of `org-open-file' for details."
  8868. (interactive "e")
  8869. (mouse-set-point ev)
  8870. (if (eq major-mode 'org-agenda-mode)
  8871. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8872. (org-open-at-point))
  8873. (defvar org-window-config-before-follow-link nil
  8874. "The window configuration before following a link.
  8875. This is saved in case the need arises to restore it.")
  8876. (defvar org-open-link-marker (make-marker)
  8877. "Marker pointing to the location where `org-open-at-point; was called.")
  8878. ;;;###autoload
  8879. (defun org-open-at-point-global ()
  8880. "Follow a link like Org-mode does.
  8881. This command can be called in any mode to follow a link that has
  8882. Org-mode syntax."
  8883. (interactive)
  8884. (org-run-like-in-org-mode 'org-open-at-point))
  8885. ;;;###autoload
  8886. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8887. "Open a link in the string S, as if it was in Org-mode."
  8888. (interactive "sLink: \nP")
  8889. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8890. (with-temp-buffer
  8891. (let ((org-inhibit-startup (not reference-buffer)))
  8892. (org-mode)
  8893. (insert s)
  8894. (goto-char (point-min))
  8895. (when reference-buffer
  8896. (setq org-link-abbrev-alist-local
  8897. (with-current-buffer reference-buffer
  8898. org-link-abbrev-alist-local)))
  8899. (org-open-at-point arg reference-buffer)))))
  8900. (defvar org-open-at-point-functions nil
  8901. "Hook that is run when following a link at point.
  8902. Functions in this hook must return t if they identify and follow
  8903. a link at point. If they don't find anything interesting at point,
  8904. they must return nil.")
  8905. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  8906. (defun org-open-at-point (&optional arg reference-buffer)
  8907. "Open link at or after point.
  8908. If there is no link at point, this function will search forward up to
  8909. the end of the current line.
  8910. Normally, files will be opened by an appropriate application. If the
  8911. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8912. With a double prefix argument, try to open outside of Emacs, in the
  8913. application the system uses for this file type."
  8914. (interactive "P")
  8915. ;; if in a code block, then open the block's results
  8916. (unless (call-interactively #'org-babel-open-src-block-result)
  8917. (org-load-modules-maybe)
  8918. (move-marker org-open-link-marker (point))
  8919. (setq org-window-config-before-follow-link (current-window-configuration))
  8920. (org-remove-occur-highlights nil nil t)
  8921. (cond
  8922. ((and (org-at-heading-p)
  8923. (not (org-at-timestamp-p t))
  8924. (not (org-in-regexp
  8925. (concat org-plain-link-re "\\|"
  8926. org-bracket-link-regexp "\\|"
  8927. org-angle-link-re "\\|"
  8928. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8929. (not (get-text-property (point) 'org-linked-text)))
  8930. (or (let* ((lkall (org-offer-links-in-entry (current-buffer) (point) arg))
  8931. (lk0 (car lkall))
  8932. (lk (if (stringp lk0) (list lk0) lk0))
  8933. (lkend (cdr lkall)))
  8934. (mapcar (lambda(l)
  8935. (search-forward l nil lkend)
  8936. (goto-char (match-beginning 0))
  8937. (org-open-at-point))
  8938. lk))
  8939. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8940. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8941. ((and (org-at-timestamp-p t)
  8942. (not (org-in-regexp org-bracket-link-regexp)))
  8943. (org-follow-timestamp-link))
  8944. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8945. (not (org-in-regexp org-any-link-re)))
  8946. (org-footnote-action))
  8947. (t
  8948. (let (type path link line search (pos (point)))
  8949. (catch 'match
  8950. (save-excursion
  8951. (skip-chars-forward "^]\n\r")
  8952. (when (org-in-regexp org-bracket-link-regexp 1)
  8953. (setq link (org-extract-attributes
  8954. (org-link-unescape (org-match-string-no-properties 1))))
  8955. (while (string-match " *\n *" link)
  8956. (setq link (replace-match " " t t link)))
  8957. (setq link (org-link-expand-abbrev link))
  8958. (cond
  8959. ((or (file-name-absolute-p link)
  8960. (string-match "^\\.\\.?/" link))
  8961. (setq type "file" path link))
  8962. ((string-match org-link-re-with-space3 link)
  8963. (setq type (match-string 1 link) path (match-string 2 link)))
  8964. ((string-match "^help:+\\(.+\\)" link)
  8965. (setq type "help" path (match-string 1 link)))
  8966. (t (setq type "thisfile" path link)))
  8967. (throw 'match t)))
  8968. (when (get-text-property (point) 'org-linked-text)
  8969. (setq type "thisfile"
  8970. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8971. (1+ (point)) (point))
  8972. path (buffer-substring
  8973. (or (previous-single-property-change pos 'org-linked-text)
  8974. (point-min))
  8975. (or (next-single-property-change pos 'org-linked-text)
  8976. (point-max))))
  8977. (throw 'match t))
  8978. (save-excursion
  8979. (let ((plinkpos (org-in-regexp org-plain-link-re)))
  8980. (when (or (org-in-regexp org-angle-link-re)
  8981. (and plinkpos (goto-char (car plinkpos))
  8982. (save-match-data (not (looking-back "\\[\\[")))))
  8983. (setq type (match-string 1)
  8984. path (org-link-unescape (match-string 2)))
  8985. (throw 'match t))))
  8986. (save-excursion
  8987. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8988. (setq type "tags"
  8989. path (match-string 1))
  8990. (while (string-match ":" path)
  8991. (setq path (replace-match "+" t t path)))
  8992. (throw 'match t)))
  8993. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8994. (setq type "tree-match"
  8995. path (match-string 1))
  8996. (throw 'match t)))
  8997. (unless path
  8998. (user-error "No link found"))
  8999. ;; switch back to reference buffer
  9000. ;; needed when if called in a temporary buffer through
  9001. ;; org-open-link-from-string
  9002. (with-current-buffer (or reference-buffer (current-buffer))
  9003. ;; Remove any trailing spaces in path
  9004. (if (string-match " +\\'" path)
  9005. (setq path (replace-match "" t t path)))
  9006. (if (and org-link-translation-function
  9007. (fboundp org-link-translation-function))
  9008. ;; Check if we need to translate the link
  9009. (let ((tmp (funcall org-link-translation-function type path)))
  9010. (setq type (car tmp) path (cdr tmp))))
  9011. (cond
  9012. ((assoc type org-link-protocols)
  9013. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9014. ((equal type "help")
  9015. (let ((f-or-v (intern path)))
  9016. (cond ((fboundp f-or-v)
  9017. (describe-function f-or-v))
  9018. ((boundp f-or-v)
  9019. (describe-variable f-or-v))
  9020. (t (error "Not a known function or variable")))))
  9021. ((equal type "mailto")
  9022. (let ((cmd (car org-link-mailto-program))
  9023. (args (cdr org-link-mailto-program)) args1
  9024. (address path) (subject "") a)
  9025. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  9026. (setq address (match-string 1 path)
  9027. subject (org-link-escape (match-string 2 path))))
  9028. (while args
  9029. (cond
  9030. ((not (stringp (car args))) (push (pop args) args1))
  9031. (t (setq a (pop args))
  9032. (if (string-match "%a" a)
  9033. (setq a (replace-match address t t a)))
  9034. (if (string-match "%s" a)
  9035. (setq a (replace-match subject t t a)))
  9036. (push a args1))))
  9037. (apply cmd (nreverse args1))))
  9038. ((member type '("http" "https" "ftp" "news"))
  9039. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  9040. (org-link-escape
  9041. path org-link-escape-chars-browser)
  9042. path))))
  9043. ((string= type "doi")
  9044. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  9045. (org-link-escape
  9046. path org-link-escape-chars-browser)
  9047. path))))
  9048. ((member type '("message"))
  9049. (browse-url (concat type ":" path)))
  9050. ((string= type "tags")
  9051. (org-tags-view arg path))
  9052. ((string= type "tree-match")
  9053. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  9054. ((string= type "file")
  9055. (if (string-match "::\\([0-9]+\\)\\'" path)
  9056. (setq line (string-to-number (match-string 1 path))
  9057. path (substring path 0 (match-beginning 0)))
  9058. (if (string-match "::\\(.+\\)\\'" path)
  9059. (setq search (match-string 1 path)
  9060. path (substring path 0 (match-beginning 0)))))
  9061. (if (string-match "[*?{]" (file-name-nondirectory path))
  9062. (dired path)
  9063. (org-open-file path arg line search)))
  9064. ((string= type "shell")
  9065. (let ((buf (generate-new-buffer "*Org Shell Output"))
  9066. (cmd path))
  9067. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  9068. (string-match org-confirm-shell-link-not-regexp cmd))
  9069. (not org-confirm-shell-link-function)
  9070. (funcall org-confirm-shell-link-function
  9071. (format "Execute \"%s\" in shell? "
  9072. (org-add-props cmd nil
  9073. 'face 'org-warning))))
  9074. (progn
  9075. (message "Executing %s" cmd)
  9076. (shell-command cmd buf)
  9077. (if (featurep 'midnight)
  9078. (setq clean-buffer-list-kill-buffer-names
  9079. (cons buf clean-buffer-list-kill-buffer-names))))
  9080. (error "Abort"))))
  9081. ((string= type "elisp")
  9082. (let ((cmd path))
  9083. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  9084. (string-match org-confirm-elisp-link-not-regexp cmd))
  9085. (not org-confirm-elisp-link-function)
  9086. (funcall org-confirm-elisp-link-function
  9087. (format "Execute \"%s\" as elisp? "
  9088. (org-add-props cmd nil
  9089. 'face 'org-warning))))
  9090. (message "%s => %s" cmd
  9091. (if (equal (string-to-char cmd) ?\()
  9092. (eval (read cmd))
  9093. (call-interactively (read cmd))))
  9094. (error "Abort"))))
  9095. ((and (string= type "thisfile")
  9096. (run-hook-with-args-until-success
  9097. 'org-open-link-functions path)))
  9098. ((string= type "thisfile")
  9099. (if arg
  9100. (switch-to-buffer-other-window
  9101. (org-get-buffer-for-internal-link (current-buffer)))
  9102. (org-mark-ring-push))
  9103. (let ((cmd `(org-link-search
  9104. ,path
  9105. ,(cond ((equal arg '(4)) ''occur)
  9106. ((equal arg '(16)) ''org-occur))
  9107. ,pos)))
  9108. (condition-case nil (let ((org-link-search-inhibit-query t))
  9109. (eval cmd))
  9110. (error (progn (widen) (eval cmd))))))
  9111. (t (browse-url-at-point)))))))
  9112. (move-marker org-open-link-marker nil)
  9113. (run-hook-with-args 'org-follow-link-hook)))
  9114. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9115. "Offer links in the current entry and return the selected link.
  9116. If there is only one link, return it.
  9117. If NTH is an integer, return the NTH link found.
  9118. If ZERO is a string, check also this string for a link, and if
  9119. there is one, return it."
  9120. (with-current-buffer buffer
  9121. (save-excursion
  9122. (save-restriction
  9123. (widen)
  9124. (goto-char marker)
  9125. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  9126. "\\(" org-angle-link-re "\\)\\|"
  9127. "\\(" org-plain-link-re "\\)"))
  9128. (cnt ?0)
  9129. (in-emacs (if (integerp nth) nil nth))
  9130. have-zero end links link c)
  9131. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9132. (push (match-string 0 zero) links)
  9133. (setq cnt (1- cnt) have-zero t))
  9134. (save-excursion
  9135. (org-back-to-heading t)
  9136. (setq end (save-excursion (outline-next-heading) (point)))
  9137. (while (re-search-forward re end t)
  9138. (push (match-string 0) links))
  9139. (setq links (org-uniquify (reverse links))))
  9140. (cond
  9141. ((null links)
  9142. (message "No links"))
  9143. ((equal (length links) 1)
  9144. (setq link (car links)))
  9145. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9146. (setq link (nth (if have-zero nth (1- nth)) links)))
  9147. (t ; we have to select a link
  9148. (save-excursion
  9149. (save-window-excursion
  9150. (delete-other-windows)
  9151. (with-output-to-temp-buffer "*Select Link*"
  9152. (mapc (lambda (l)
  9153. (if (not (string-match org-bracket-link-regexp l))
  9154. (princ (format "[%c] %s\n" (incf cnt)
  9155. (org-remove-angle-brackets l)))
  9156. (if (match-end 3)
  9157. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9158. (match-string 3 l) (match-string 1 l)))
  9159. (princ (format "[%c] %s\n" (incf cnt)
  9160. (match-string 1 l))))))
  9161. links))
  9162. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9163. (message "Select link to open, RET to open all:")
  9164. (setq c (read-char-exclusive))
  9165. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9166. (when (equal c ?q) (error "Abort"))
  9167. (if (equal c ?\C-m)
  9168. (setq link links)
  9169. (setq nth (- c ?0))
  9170. (if have-zero (setq nth (1+ nth)))
  9171. (unless (and (integerp nth) (>= (length links) nth))
  9172. (error "Invalid link selection"))
  9173. (setq link (nth (1- nth) links)))))
  9174. (cons link end))))))
  9175. ;; Add special file links that specify the way of opening
  9176. (org-add-link-type "file+sys" 'org-open-file-with-system)
  9177. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  9178. (defun org-open-file-with-system (path)
  9179. "Open file at PATH using the system way of opening it."
  9180. (org-open-file path 'system))
  9181. (defun org-open-file-with-emacs (path)
  9182. "Open file at PATH in Emacs."
  9183. (org-open-file path 'emacs))
  9184. ;;; File search
  9185. (defvar org-create-file-search-functions nil
  9186. "List of functions to construct the right search string for a file link.
  9187. These functions are called in turn with point at the location to
  9188. which the link should point.
  9189. A function in the hook should first test if it would like to
  9190. handle this file type, for example by checking the `major-mode'
  9191. or the file extension. If it decides not to handle this file, it
  9192. should just return nil to give other functions a chance. If it
  9193. does handle the file, it must return the search string to be used
  9194. when following the link. The search string will be part of the
  9195. file link, given after a double colon, and `org-open-at-point'
  9196. will automatically search for it. If special measures must be
  9197. taken to make the search successful, another function should be
  9198. added to the companion hook `org-execute-file-search-functions',
  9199. which see.
  9200. A function in this hook may also use `setq' to set the variable
  9201. `description' to provide a suggestion for the descriptive text to
  9202. be used for this link when it gets inserted into an Org-mode
  9203. buffer with \\[org-insert-link].")
  9204. (defvar org-execute-file-search-functions nil
  9205. "List of functions to execute a file search triggered by a link.
  9206. Functions added to this hook must accept a single argument, the
  9207. search string that was part of the file link, the part after the
  9208. double colon. The function must first check if it would like to
  9209. handle this search, for example by checking the `major-mode' or
  9210. the file extension. If it decides not to handle this search, it
  9211. should just return nil to give other functions a chance. If it
  9212. does handle the search, it must return a non-nil value to keep
  9213. other functions from trying.
  9214. Each function can access the current prefix argument through the
  9215. variable `current-prefix-argument'. Note that a single prefix is
  9216. used to force opening a link in Emacs, so it may be good to only
  9217. use a numeric or double prefix to guide the search function.
  9218. In case this is needed, a function in this hook can also restore
  9219. the window configuration before `org-open-at-point' was called using:
  9220. (set-window-configuration org-window-config-before-follow-link)")
  9221. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9222. (defun org-link-search (s &optional type avoid-pos stealth)
  9223. "Search for a link search option.
  9224. If S is surrounded by forward slashes, it is interpreted as a
  9225. regular expression. In org-mode files, this will create an `org-occur'
  9226. sparse tree. In ordinary files, `occur' will be used to list matches.
  9227. If the current buffer is in `dired-mode', grep will be used to search
  9228. in all files. If AVOID-POS is given, ignore matches near that position.
  9229. When optional argument STEALTH is non-nil, do not modify
  9230. visibility around point, thus ignoring
  9231. `org-show-hierarchy-above', `org-show-following-heading' and
  9232. `org-show-siblings' variables."
  9233. (let ((case-fold-search t)
  9234. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  9235. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  9236. (append '(("") (" ") ("\t") ("\n"))
  9237. org-emphasis-alist)
  9238. "\\|") "\\)"))
  9239. (pos (point))
  9240. (pre nil) (post nil)
  9241. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  9242. (cond
  9243. ;; First check if there are any special search functions
  9244. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9245. ;; Now try the builtin stuff
  9246. ((and (equal (string-to-char s0) ?#)
  9247. (> (length s0) 1)
  9248. (save-excursion
  9249. (goto-char (point-min))
  9250. (and
  9251. (re-search-forward
  9252. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  9253. (setq type 'dedicated
  9254. pos (match-beginning 0))))
  9255. ;; There is an exact target for this
  9256. (goto-char pos)
  9257. (org-back-to-heading t)))
  9258. ((save-excursion
  9259. (goto-char (point-min))
  9260. (and
  9261. (re-search-forward
  9262. (concat "<<" (regexp-quote s0) ">>") nil t)
  9263. (setq type 'dedicated
  9264. pos (match-beginning 0))))
  9265. ;; There is an exact target for this
  9266. (goto-char pos))
  9267. ((save-excursion
  9268. (goto-char (point-min))
  9269. (and
  9270. (re-search-forward
  9271. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  9272. (setq type 'dedicated pos (match-beginning 0))))
  9273. ;; Found an invisible target.
  9274. (goto-char pos))
  9275. ((save-excursion
  9276. (goto-char (point-min))
  9277. (and
  9278. (re-search-forward
  9279. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9280. (setq type 'dedicated pos (match-beginning 0))))
  9281. ;; Found an element with a matching #+name affiliated keyword.
  9282. (goto-char pos))
  9283. ((and (string-match "^(\\(.*\\))$" s0)
  9284. (save-excursion
  9285. (goto-char (point-min))
  9286. (and
  9287. (re-search-forward
  9288. (concat "[^[]" (regexp-quote
  9289. (format org-coderef-label-format
  9290. (match-string 1 s0))))
  9291. nil t)
  9292. (setq type 'dedicated
  9293. pos (1+ (match-beginning 0))))))
  9294. ;; There is a coderef target for this
  9295. (goto-char pos))
  9296. ((string-match "^/\\(.*\\)/$" s)
  9297. ;; A regular expression
  9298. (cond
  9299. ((derived-mode-p 'org-mode)
  9300. (org-occur (match-string 1 s)))
  9301. ;;((eq major-mode 'dired-mode)
  9302. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9303. (t (org-do-occur (match-string 1 s)))))
  9304. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9305. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9306. (goto-char (point-min))
  9307. (cond
  9308. ((let (case-fold-search)
  9309. (re-search-forward (format org-complex-heading-regexp-format
  9310. (regexp-quote s))
  9311. nil t))
  9312. ;; OK, found a match
  9313. (setq type 'dedicated)
  9314. (goto-char (match-beginning 0)))
  9315. ((and (not org-link-search-inhibit-query)
  9316. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9317. (y-or-n-p "No match - create this as a new heading? "))
  9318. (goto-char (point-max))
  9319. (or (bolp) (newline))
  9320. (insert "* " s "\n")
  9321. (beginning-of-line 0))
  9322. (t
  9323. (goto-char pos)
  9324. (error "No match"))))
  9325. (t
  9326. ;; A normal search string
  9327. (when (equal (string-to-char s) ?*)
  9328. ;; Anchor on headlines, post may include tags.
  9329. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9330. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9331. s (substring s 1)))
  9332. (remove-text-properties
  9333. 0 (length s)
  9334. '(face nil mouse-face nil keymap nil fontified nil) s)
  9335. ;; Make a series of regular expressions to find a match
  9336. (setq words (org-split-string s "[ \n\r\t]+")
  9337. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9338. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9339. "\\)" markers)
  9340. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9341. re2a (concat "[ \t\r\n]" re2a_)
  9342. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9343. re4 (concat "[^a-zA-Z_]" re4_)
  9344. re1 (concat pre re2 post)
  9345. re3 (concat pre (if pre re4_ re4) post)
  9346. re5 (concat pre ".*" re4)
  9347. re2 (concat pre re2)
  9348. re2a (concat pre (if pre re2a_ re2a))
  9349. re4 (concat pre (if pre re4_ re4))
  9350. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9351. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9352. re5 "\\)"
  9353. ))
  9354. (cond
  9355. ((eq type 'org-occur) (org-occur reall))
  9356. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9357. (t (goto-char (point-min))
  9358. (setq type 'fuzzy)
  9359. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9360. (org-search-not-self 1 re1 nil t)
  9361. (org-search-not-self 1 re2 nil t)
  9362. (org-search-not-self 1 re2a nil t)
  9363. (org-search-not-self 1 re3 nil t)
  9364. (org-search-not-self 1 re4 nil t)
  9365. (org-search-not-self 1 re5 nil t)
  9366. )
  9367. (goto-char (match-beginning 1))
  9368. (goto-char pos)
  9369. (error "No match"))))))
  9370. (and (derived-mode-p 'org-mode)
  9371. (not stealth)
  9372. (org-show-context 'link-search))
  9373. type))
  9374. (defun org-search-not-self (group &rest args)
  9375. "Execute `re-search-forward', but only accept matches that do not
  9376. enclose the position of `org-open-link-marker'."
  9377. (let ((m org-open-link-marker))
  9378. (catch 'exit
  9379. (while (apply 're-search-forward args)
  9380. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9381. (goto-char (match-end group))
  9382. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9383. (> (match-beginning 0) (marker-position m))
  9384. (< (match-end 0) (marker-position m)))
  9385. (save-match-data
  9386. (or (not (org-in-regexp
  9387. org-bracket-link-analytic-regexp 1))
  9388. (not (match-end 4)) ; no description
  9389. (and (<= (match-beginning 4) (point))
  9390. (>= (match-end 4) (point))))))
  9391. (throw 'exit (point))))))))
  9392. (defun org-get-buffer-for-internal-link (buffer)
  9393. "Return a buffer to be used for displaying the link target of internal links."
  9394. (cond
  9395. ((not org-display-internal-link-with-indirect-buffer)
  9396. buffer)
  9397. ((string-match "(Clone)$" (buffer-name buffer))
  9398. (message "Buffer is already a clone, not making another one")
  9399. ;; we also do not modify visibility in this case
  9400. buffer)
  9401. (t ; make a new indirect buffer for displaying the link
  9402. (let* ((bn (buffer-name buffer))
  9403. (ibn (concat bn "(Clone)"))
  9404. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9405. (with-current-buffer ib (org-overview))
  9406. ib))))
  9407. (defun org-do-occur (regexp &optional cleanup)
  9408. "Call the Emacs command `occur'.
  9409. If CLEANUP is non-nil, remove the printout of the regular expression
  9410. in the *Occur* buffer. This is useful if the regex is long and not useful
  9411. to read."
  9412. (occur regexp)
  9413. (when cleanup
  9414. (let ((cwin (selected-window)) win beg end)
  9415. (when (setq win (get-buffer-window "*Occur*"))
  9416. (select-window win))
  9417. (goto-char (point-min))
  9418. (when (re-search-forward "match[a-z]+" nil t)
  9419. (setq beg (match-end 0))
  9420. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9421. (setq end (1- (match-beginning 0)))))
  9422. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9423. (goto-char (point-min))
  9424. (select-window cwin))))
  9425. ;;; The mark ring for links jumps
  9426. (defvar org-mark-ring nil
  9427. "Mark ring for positions before jumps in Org-mode.")
  9428. (defvar org-mark-ring-last-goto nil
  9429. "Last position in the mark ring used to go back.")
  9430. ;; Fill and close the ring
  9431. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9432. (loop for i from 1 to org-mark-ring-length do
  9433. (push (make-marker) org-mark-ring))
  9434. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9435. org-mark-ring)
  9436. (defun org-mark-ring-push (&optional pos buffer)
  9437. "Put the current position or POS into the mark ring and rotate it."
  9438. (interactive)
  9439. (setq pos (or pos (point)))
  9440. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9441. (move-marker (car org-mark-ring)
  9442. (or pos (point))
  9443. (or buffer (current-buffer)))
  9444. (message "%s"
  9445. (substitute-command-keys
  9446. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9447. (defun org-mark-ring-goto (&optional n)
  9448. "Jump to the previous position in the mark ring.
  9449. With prefix arg N, jump back that many stored positions. When
  9450. called several times in succession, walk through the entire ring.
  9451. Org-mode commands jumping to a different position in the current file,
  9452. or to another Org-mode file, automatically push the old position
  9453. onto the ring."
  9454. (interactive "p")
  9455. (let (p m)
  9456. (if (eq last-command this-command)
  9457. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9458. (setq p org-mark-ring))
  9459. (setq org-mark-ring-last-goto p)
  9460. (setq m (car p))
  9461. (org-pop-to-buffer-same-window (marker-buffer m))
  9462. (goto-char m)
  9463. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9464. (defun org-remove-angle-brackets (s)
  9465. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9466. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9467. s)
  9468. (defun org-add-angle-brackets (s)
  9469. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9470. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9471. s)
  9472. (defun org-remove-double-quotes (s)
  9473. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9474. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9475. s)
  9476. ;;; Following specific links
  9477. (defun org-follow-timestamp-link ()
  9478. "Open an agenda view for the time-stamp date/range at point."
  9479. (cond
  9480. ((org-at-date-range-p t)
  9481. (let ((org-agenda-start-on-weekday)
  9482. (t1 (match-string 1))
  9483. (t2 (match-string 2)) tt1 tt2)
  9484. (setq tt1 (time-to-days (org-time-string-to-time t1))
  9485. tt2 (time-to-days (org-time-string-to-time t2)))
  9486. (let ((org-agenda-buffer-tmp-name
  9487. (format "*Org Agenda(a:%s)"
  9488. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  9489. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  9490. ((org-at-timestamp-p t)
  9491. (let ((org-agenda-buffer-tmp-name
  9492. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  9493. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9494. (substring (match-string 1) 0 10)))
  9495. 1)))
  9496. (t (error "This should not happen"))))
  9497. ;;; Following file links
  9498. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9499. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9500. (declare-function mailcap-mime-info
  9501. "mailcap" (string &optional request no-decode))
  9502. (defvar org-wait nil)
  9503. (defun org-open-file (path &optional in-emacs line search)
  9504. "Open the file at PATH.
  9505. First, this expands any special file name abbreviations. Then the
  9506. configuration variable `org-file-apps' is checked if it contains an
  9507. entry for this file type, and if yes, the corresponding command is launched.
  9508. If no application is found, Emacs simply visits the file.
  9509. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9510. With a double \\[universal-argument] \\[universal-argument] \
  9511. prefix arg, Org tries to avoid opening in Emacs
  9512. and to use an external application to visit the file.
  9513. Optional LINE specifies a line to go to, optional SEARCH a string
  9514. to search for. If LINE or SEARCH is given, the file will be
  9515. opened in Emacs, unless an entry from org-file-apps that makes
  9516. use of groups in a regexp matches.
  9517. If you want to change the way frames are used when following a
  9518. link, please customize `org-link-frame-setup'.
  9519. If the file does not exist, an error is thrown."
  9520. (let* ((file (if (equal path "")
  9521. buffer-file-name
  9522. (substitute-in-file-name (expand-file-name path))))
  9523. (file-apps (append org-file-apps (org-default-apps)))
  9524. (apps (org-remove-if
  9525. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9526. (apps-dlink (org-remove-if-not
  9527. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9528. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9529. (dirp (if remp nil (file-directory-p file)))
  9530. (file (if (and dirp org-open-directory-means-index-dot-org)
  9531. (concat (file-name-as-directory file) "index.org")
  9532. file))
  9533. (a-m-a-p (assq 'auto-mode apps))
  9534. (dfile (downcase file))
  9535. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9536. (link (cond ((and (eq line nil)
  9537. (eq search nil))
  9538. file)
  9539. (line
  9540. (concat file "::" (number-to-string line)))
  9541. (search
  9542. (concat file "::" search))))
  9543. (dlink (downcase link))
  9544. (old-buffer (current-buffer))
  9545. (old-pos (point))
  9546. (old-mode major-mode)
  9547. ext cmd link-match-data)
  9548. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9549. (setq ext (match-string 1 dfile))
  9550. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9551. (setq ext (match-string 1 dfile))))
  9552. (cond
  9553. ((member in-emacs '((16) system))
  9554. (setq cmd (cdr (assoc 'system apps))))
  9555. (in-emacs (setq cmd 'emacs))
  9556. (t
  9557. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9558. (and dirp (cdr (assoc 'directory apps)))
  9559. ; first, try matching against apps-dlink
  9560. ; if we get a match here, store the match data for later
  9561. (let ((match (assoc-default dlink apps-dlink
  9562. 'string-match)))
  9563. (if match
  9564. (progn (setq link-match-data (match-data))
  9565. match)
  9566. (progn (setq in-emacs (or in-emacs line search))
  9567. nil))) ; if we have no match in apps-dlink,
  9568. ; always open the file in emacs if line or search
  9569. ; is given (for backwards compatibility)
  9570. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9571. 'string-match)
  9572. (cdr (assoc ext apps))
  9573. (cdr (assoc t apps))))))
  9574. (when (eq cmd 'system)
  9575. (setq cmd (cdr (assoc 'system apps))))
  9576. (when (eq cmd 'default)
  9577. (setq cmd (cdr (assoc t apps))))
  9578. (when (eq cmd 'mailcap)
  9579. (require 'mailcap)
  9580. (mailcap-parse-mailcaps)
  9581. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9582. (command (mailcap-mime-info mime-type)))
  9583. (if (stringp command)
  9584. (setq cmd command)
  9585. (setq cmd 'emacs))))
  9586. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9587. (not (file-exists-p file))
  9588. (not org-open-non-existing-files))
  9589. (error "No such file: %s" file))
  9590. (cond
  9591. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9592. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9593. (while (string-match "['\"]%s['\"]" cmd)
  9594. (setq cmd (replace-match "%s" t t cmd)))
  9595. (while (string-match "%s" cmd)
  9596. (setq cmd (replace-match
  9597. (save-match-data
  9598. (shell-quote-argument
  9599. (convert-standard-filename file)))
  9600. t t cmd)))
  9601. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9602. (save-match-data
  9603. (let ((match-index 1)
  9604. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9605. (set-match-data link-match-data)
  9606. (while (<= match-index number-of-groups)
  9607. (let ((regex (concat "%" (number-to-string match-index)))
  9608. (replace-with (match-string match-index dlink)))
  9609. (while (string-match regex cmd)
  9610. (setq cmd (replace-match replace-with t t cmd))))
  9611. (setq match-index (+ match-index 1)))))
  9612. (save-window-excursion
  9613. (message "Running %s...done" cmd)
  9614. (start-process-shell-command cmd nil cmd)
  9615. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  9616. ((or (stringp cmd)
  9617. (eq cmd 'emacs))
  9618. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9619. (widen)
  9620. (if line (org-goto-line line)
  9621. (if search (org-link-search search))))
  9622. ((consp cmd)
  9623. (let ((file (convert-standard-filename file)))
  9624. (save-match-data
  9625. (set-match-data link-match-data)
  9626. (eval cmd))))
  9627. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9628. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9629. (or (not (equal old-buffer (current-buffer)))
  9630. (not (equal old-pos (point))))
  9631. (org-mark-ring-push old-pos old-buffer))))
  9632. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9633. "This function returns non-nil if `entry' uses a regular
  9634. expression which should be matched against the whole link by
  9635. org-open-file.
  9636. It assumes that is the case when the entry uses a regular
  9637. expression which has at least one grouping construct and the
  9638. action is either a lisp form or a command string containing
  9639. '%1', i.e. using at least one subexpression match as a
  9640. parameter."
  9641. (let ((selector (car entry))
  9642. (action (cdr entry)))
  9643. (if (stringp selector)
  9644. (and (> (regexp-opt-depth selector) 0)
  9645. (or (and (stringp action)
  9646. (string-match "%[0-9]" action))
  9647. (consp action)))
  9648. nil)))
  9649. (defun org-default-apps ()
  9650. "Return the default applications for this operating system."
  9651. (cond
  9652. ((eq system-type 'darwin)
  9653. org-file-apps-defaults-macosx)
  9654. ((eq system-type 'windows-nt)
  9655. org-file-apps-defaults-windowsnt)
  9656. (t org-file-apps-defaults-gnu)))
  9657. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9658. "Convert extensions to regular expressions in the cars of LIST.
  9659. Also, weed out any non-string entries, because the return value is used
  9660. only for regexp matching.
  9661. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9662. point to the symbol `emacs', indicating that the file should
  9663. be opened in Emacs."
  9664. (append
  9665. (delq nil
  9666. (mapcar (lambda (x)
  9667. (if (not (stringp (car x)))
  9668. nil
  9669. (if (string-match "\\W" (car x))
  9670. x
  9671. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9672. list))
  9673. (if add-auto-mode
  9674. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9675. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9676. (defun org-file-remote-p (file)
  9677. "Test whether FILE specifies a location on a remote system.
  9678. Return non-nil if the location is indeed remote.
  9679. For example, the filename \"/user@host:/foo\" specifies a location
  9680. on the system \"/user@host:\"."
  9681. (cond ((fboundp 'file-remote-p)
  9682. (file-remote-p file))
  9683. ((fboundp 'tramp-handle-file-remote-p)
  9684. (tramp-handle-file-remote-p file))
  9685. ((and (boundp 'ange-ftp-name-format)
  9686. (string-match (car ange-ftp-name-format) file))
  9687. t)))
  9688. ;;;; Refiling
  9689. (defun org-get-org-file ()
  9690. "Read a filename, with default directory `org-directory'."
  9691. (let ((default (or org-default-notes-file remember-data-file)))
  9692. (read-file-name (format "File name [%s]: " default)
  9693. (file-name-as-directory org-directory)
  9694. default)))
  9695. (defun org-notes-order-reversed-p ()
  9696. "Check if the current file should receive notes in reversed order."
  9697. (cond
  9698. ((not org-reverse-note-order) nil)
  9699. ((eq t org-reverse-note-order) t)
  9700. ((not (listp org-reverse-note-order)) nil)
  9701. (t (catch 'exit
  9702. (let ((all org-reverse-note-order)
  9703. entry)
  9704. (while (setq entry (pop all))
  9705. (if (string-match (car entry) buffer-file-name)
  9706. (throw 'exit (cdr entry))))
  9707. nil)))))
  9708. (defvar org-refile-target-table nil
  9709. "The list of refile targets, created by `org-refile'.")
  9710. (defvar org-agenda-new-buffers nil
  9711. "Buffers created to visit agenda files.")
  9712. (defvar org-refile-cache nil
  9713. "Cache for refile targets.")
  9714. (defvar org-refile-markers nil
  9715. "All the markers used for caching refile locations.")
  9716. (defun org-refile-marker (pos)
  9717. "Get a new refile marker, but only if caching is in use."
  9718. (if (not org-refile-use-cache)
  9719. pos
  9720. (let ((m (make-marker)))
  9721. (move-marker m pos)
  9722. (push m org-refile-markers)
  9723. m)))
  9724. (defun org-refile-cache-clear ()
  9725. "Clear the refile cache and disable all the markers."
  9726. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9727. (setq org-refile-markers nil)
  9728. (setq org-refile-cache nil)
  9729. (message "Refile cache has been cleared"))
  9730. (defun org-refile-cache-check-set (set)
  9731. "Check if all the markers in the cache still have live buffers."
  9732. (let (marker)
  9733. (catch 'exit
  9734. (while (and set (setq marker (nth 3 (pop set))))
  9735. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9736. (when (and marker (null (marker-buffer marker)))
  9737. (message "not found") (sit-for 3)
  9738. (throw 'exit nil)))
  9739. t)))
  9740. (defun org-refile-cache-put (set &rest identifiers)
  9741. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9742. (let* ((key (sha1 (prin1-to-string identifiers)))
  9743. (entry (assoc key org-refile-cache)))
  9744. (if entry
  9745. (setcdr entry set)
  9746. (push (cons key set) org-refile-cache))))
  9747. (defun org-refile-cache-get (&rest identifiers)
  9748. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9749. (cond
  9750. ((not org-refile-cache) nil)
  9751. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9752. (t
  9753. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9754. org-refile-cache))))
  9755. (and set (org-refile-cache-check-set set) set)))))
  9756. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9757. "Produce a table with refile targets."
  9758. (let ((case-fold-search nil)
  9759. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9760. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9761. targets tgs txt re files f desc descre fast-path-p level pos0)
  9762. (message "Getting targets...")
  9763. (with-current-buffer (or default-buffer (current-buffer))
  9764. (while (setq entry (pop entries))
  9765. (setq files (car entry) desc (cdr entry))
  9766. (setq fast-path-p nil)
  9767. (cond
  9768. ((null files) (setq files (list (current-buffer))))
  9769. ((eq files 'org-agenda-files)
  9770. (setq files (org-agenda-files 'unrestricted)))
  9771. ((and (symbolp files) (fboundp files))
  9772. (setq files (funcall files)))
  9773. ((and (symbolp files) (boundp files))
  9774. (setq files (symbol-value files))))
  9775. (if (stringp files) (setq files (list files)))
  9776. (cond
  9777. ((eq (car desc) :tag)
  9778. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9779. ((eq (car desc) :todo)
  9780. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9781. ((eq (car desc) :regexp)
  9782. (setq descre (cdr desc)))
  9783. ((eq (car desc) :level)
  9784. (setq descre (concat "^\\*\\{" (number-to-string
  9785. (if org-odd-levels-only
  9786. (1- (* 2 (cdr desc)))
  9787. (cdr desc)))
  9788. "\\}[ \t]")))
  9789. ((eq (car desc) :maxlevel)
  9790. (setq fast-path-p t)
  9791. (setq descre (concat "^\\*\\{1," (number-to-string
  9792. (if org-odd-levels-only
  9793. (1- (* 2 (cdr desc)))
  9794. (cdr desc)))
  9795. "\\}[ \t]")))
  9796. (t (error "Bad refiling target description %s" desc)))
  9797. (while (setq f (pop files))
  9798. (with-current-buffer
  9799. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9800. (or
  9801. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9802. (progn
  9803. (if (bufferp f) (setq f (buffer-file-name
  9804. (buffer-base-buffer f))))
  9805. (setq f (and f (expand-file-name f)))
  9806. (if (eq org-refile-use-outline-path 'file)
  9807. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9808. (save-excursion
  9809. (save-restriction
  9810. (widen)
  9811. (goto-char (point-min))
  9812. (while (re-search-forward descre nil t)
  9813. (goto-char (setq pos0 (point-at-bol)))
  9814. (catch 'next
  9815. (when org-refile-target-verify-function
  9816. (save-match-data
  9817. (or (funcall org-refile-target-verify-function)
  9818. (throw 'next t))))
  9819. (when (and (looking-at org-complex-heading-regexp)
  9820. (not (member (match-string 4) excluded-entries))
  9821. (match-string 4))
  9822. (setq level (org-reduced-level
  9823. (- (match-end 1) (match-beginning 1)))
  9824. txt (org-link-display-format (match-string 4))
  9825. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9826. re (format org-complex-heading-regexp-format
  9827. (regexp-quote (match-string 4))))
  9828. (when org-refile-use-outline-path
  9829. (setq txt (mapconcat
  9830. 'org-protect-slash
  9831. (append
  9832. (if (eq org-refile-use-outline-path
  9833. 'file)
  9834. (list (file-name-nondirectory
  9835. (buffer-file-name
  9836. (buffer-base-buffer))))
  9837. (if (eq org-refile-use-outline-path
  9838. 'full-file-path)
  9839. (list (buffer-file-name
  9840. (buffer-base-buffer)))))
  9841. (org-get-outline-path fast-path-p
  9842. level txt)
  9843. (list txt))
  9844. "/")))
  9845. (push (list txt f re (org-refile-marker (point)))
  9846. tgs)))
  9847. (when (= (point) pos0)
  9848. ;; verification function has not moved point
  9849. (goto-char (point-at-eol))))))))
  9850. (when org-refile-use-cache
  9851. (org-refile-cache-put tgs (buffer-file-name) descre))
  9852. (setq targets (append tgs targets))
  9853. ))))
  9854. (message "Getting targets...done")
  9855. (nreverse targets)))
  9856. (defun org-protect-slash (s)
  9857. (while (string-match "/" s)
  9858. (setq s (replace-match "\\" t t s)))
  9859. s)
  9860. (defvar org-olpa (make-vector 20 nil))
  9861. (defun org-get-outline-path (&optional fastp level heading)
  9862. "Return the outline path to the current entry, as a list.
  9863. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9864. routine which makes outline path derivations for an entire file,
  9865. avoiding backtracing. Refile target collection makes use of that."
  9866. (if fastp
  9867. (progn
  9868. (if (> level 19)
  9869. (error "Outline path failure, more than 19 levels"))
  9870. (loop for i from level upto 19 do
  9871. (aset org-olpa i nil))
  9872. (prog1
  9873. (delq nil (append org-olpa nil))
  9874. (aset org-olpa level heading)))
  9875. (let (rtn case-fold-search)
  9876. (save-excursion
  9877. (save-restriction
  9878. (widen)
  9879. (while (org-up-heading-safe)
  9880. (when (looking-at org-complex-heading-regexp)
  9881. (push (org-match-string-no-properties 4) rtn)))
  9882. rtn)))))
  9883. (defun org-format-outline-path (path &optional width prefix separator)
  9884. "Format the outline path PATH for display.
  9885. WIDTH is the maximum number of characters that is available.
  9886. PREFIX is a prefix to be included in the returned string,
  9887. such as the file name.
  9888. SEPARATOR is inserted between the different parts of the path,
  9889. the default is \"/\"."
  9890. (setq width (or width 79))
  9891. (if prefix (setq width (- width (length prefix))))
  9892. (if (not path)
  9893. (or prefix "")
  9894. (let* ((nsteps (length path))
  9895. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9896. (maxwidth (if (<= total-width width)
  9897. 10000 ;; everything fits
  9898. ;; we need to shorten the level headings
  9899. (/ (- width nsteps) nsteps)))
  9900. (org-odd-levels-only nil)
  9901. (n 0)
  9902. (total (1+ (length prefix))))
  9903. (setq maxwidth (max maxwidth 10))
  9904. (concat prefix
  9905. (if prefix (or separator "/"))
  9906. (mapconcat
  9907. (lambda (h)
  9908. (setq n (1+ n))
  9909. (if (and (= n nsteps) (< maxwidth 10000))
  9910. (setq maxwidth (- total-width total)))
  9911. (if (< (length h) maxwidth)
  9912. (progn (setq total (+ total (length h) 1)) h)
  9913. (setq h (substring h 0 (- maxwidth 2))
  9914. total (+ total maxwidth 1))
  9915. (if (string-match "[ \t]+\\'" h)
  9916. (setq h (substring h 0 (match-beginning 0))))
  9917. (setq h (concat h "..")))
  9918. (org-add-props h nil 'face
  9919. (nth (% (1- n) org-n-level-faces)
  9920. org-level-faces))
  9921. h)
  9922. path (or separator "/"))))))
  9923. (defun org-display-outline-path (&optional file current separator just-return-string)
  9924. "Display the current outline path in the echo area.
  9925. If FILE is non-nil, prepend the output with the file name.
  9926. If CURRENT is non-nil, append the current heading to the output.
  9927. SEPARATOR is passed through to `org-format-outline-path'. It separates
  9928. the different parts of the path and defaults to \"/\".
  9929. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  9930. (interactive "P")
  9931. (let* (case-fold-search
  9932. message-log-max ; Don't populate the *Messages* buffer
  9933. (bfn (buffer-file-name (buffer-base-buffer)))
  9934. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  9935. res)
  9936. (if current (setq path (append path
  9937. (save-excursion
  9938. (org-back-to-heading t)
  9939. (if (looking-at org-complex-heading-regexp)
  9940. (list (match-string 4)))))))
  9941. (setq res
  9942. (org-format-outline-path
  9943. path
  9944. (1- (frame-width))
  9945. (and file bfn (concat (file-name-nondirectory bfn) separator))
  9946. separator))
  9947. (if just-return-string
  9948. (org-no-properties res)
  9949. (message "%s" res))))
  9950. (defvar org-refile-history nil
  9951. "History for refiling operations.")
  9952. (defvar org-after-refile-insert-hook nil
  9953. "Hook run after `org-refile' has inserted its stuff at the new location.
  9954. Note that this is still *before* the stuff will be removed from
  9955. the *old* location.")
  9956. (defvar org-capture-last-stored-marker)
  9957. (defvar org-refile-keep nil
  9958. "Non-nil means `org-refile' will copy instead of refile.")
  9959. (defun org-copy ()
  9960. "Like `org-refile', but copy."
  9961. (interactive)
  9962. (let ((org-refile-keep t))
  9963. (funcall 'org-refile nil nil nil "Copy")))
  9964. (defun org-refile (&optional goto default-buffer rfloc msg)
  9965. "Move the entry or entries at point to another heading.
  9966. The list of target headings is compiled using the information in
  9967. `org-refile-targets', which see.
  9968. At the target location, the entry is filed as a subitem of the target
  9969. heading. Depending on `org-reverse-note-order', the new subitem will
  9970. either be the first or the last subitem.
  9971. If there is an active region, all entries in that region will be moved.
  9972. However, the region must fulfill the requirement that the first heading
  9973. is the first one sets the top-level of the moved text - at most siblings
  9974. below it are allowed.
  9975. With prefix arg GOTO, the command will only visit the target location
  9976. and not actually move anything.
  9977. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9978. go to the location where the last refiling operation has put the subtree.
  9979. With a prefix argument of `2', refile to the running clock.
  9980. RFLOC can be a refile location obtained in a different way.
  9981. MSG is a string to replace \"Refile\" in the default prompt with
  9982. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  9983. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9984. If you are using target caching (see `org-refile-use-cache'),
  9985. you have to clear the target cache in order to find new targets.
  9986. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9987. prefix argument (`C-u C-u C-u C-c C-w')."
  9988. (interactive "P")
  9989. (if (member goto '(0 (64)))
  9990. (org-refile-cache-clear)
  9991. (let* ((actionmsg (or msg "Refile"))
  9992. (cbuf (current-buffer))
  9993. (regionp (org-region-active-p))
  9994. (region-start (and regionp (region-beginning)))
  9995. (region-end (and regionp (region-end)))
  9996. (region-length (and regionp (- region-end region-start)))
  9997. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9998. pos it nbuf file re level reversed)
  9999. (setq last-command nil)
  10000. (when regionp
  10001. (goto-char region-start)
  10002. (or (bolp) (goto-char (point-at-bol)))
  10003. (setq region-start (point))
  10004. (unless (or (org-kill-is-subtree-p
  10005. (buffer-substring region-start region-end))
  10006. (prog1 org-refile-active-region-within-subtree
  10007. (org-toggle-heading)))
  10008. (error "The region is not a (sequence of) subtree(s)")))
  10009. (if (equal goto '(16))
  10010. (org-refile-goto-last-stored)
  10011. (when (or
  10012. (and (equal goto 2)
  10013. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10014. (prog1
  10015. (setq it (list (or org-clock-heading "running clock")
  10016. (buffer-file-name
  10017. (marker-buffer org-clock-hd-marker))
  10018. ""
  10019. (marker-position org-clock-hd-marker)))
  10020. (setq goto nil)))
  10021. (setq it (or rfloc
  10022. (let (heading-text)
  10023. (save-excursion
  10024. (unless goto
  10025. (org-back-to-heading t)
  10026. (setq heading-text
  10027. (nth 4 (org-heading-components))))
  10028. (org-refile-get-location
  10029. (cond (goto "Goto")
  10030. (regionp (concat actionmsg " region to"))
  10031. (t (concat actionmsg " subtree \""
  10032. heading-text "\" to")))
  10033. default-buffer
  10034. (and (not (equal '(4) goto))
  10035. org-refile-allow-creating-parent-nodes)
  10036. goto))))))
  10037. (setq file (nth 1 it)
  10038. re (nth 2 it)
  10039. pos (nth 3 it))
  10040. (if (and (not goto)
  10041. pos
  10042. (equal (buffer-file-name) file)
  10043. (if regionp
  10044. (and (>= pos region-start)
  10045. (<= pos region-end))
  10046. (and (>= pos (point))
  10047. (< pos (save-excursion
  10048. (org-end-of-subtree t t))))))
  10049. (error "Cannot refile to position inside the tree or region"))
  10050. (setq nbuf (or (find-buffer-visiting file)
  10051. (find-file-noselect file)))
  10052. (if goto
  10053. (progn
  10054. (org-pop-to-buffer-same-window nbuf)
  10055. (goto-char pos)
  10056. (org-show-context 'org-goto))
  10057. (if regionp
  10058. (progn
  10059. (org-kill-new (buffer-substring region-start region-end))
  10060. (org-save-markers-in-region region-start region-end))
  10061. (org-copy-subtree 1 nil t))
  10062. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10063. (find-file-noselect file)))
  10064. (setq reversed (org-notes-order-reversed-p))
  10065. (save-excursion
  10066. (save-restriction
  10067. (widen)
  10068. (if pos
  10069. (progn
  10070. (goto-char pos)
  10071. (looking-at org-outline-regexp)
  10072. (setq level (org-get-valid-level (funcall outline-level) 1))
  10073. (goto-char
  10074. (if reversed
  10075. (or (outline-next-heading) (point-max))
  10076. (or (save-excursion (org-get-next-sibling))
  10077. (org-end-of-subtree t t)
  10078. (point-max)))))
  10079. (setq level 1)
  10080. (if (not reversed)
  10081. (goto-char (point-max))
  10082. (goto-char (point-min))
  10083. (or (outline-next-heading) (goto-char (point-max)))))
  10084. (if (not (bolp)) (newline))
  10085. (org-paste-subtree level)
  10086. (when org-log-refile
  10087. (org-add-log-setup 'refile nil nil 'findpos
  10088. org-log-refile)
  10089. (unless (eq org-log-refile 'note)
  10090. (save-excursion (org-add-log-note))))
  10091. (and org-auto-align-tags
  10092. (let ((org-loop-over-headlines-in-active-region nil))
  10093. (org-set-tags nil t)))
  10094. (with-demoted-errors
  10095. (bookmark-set "org-refile-last-stored"))
  10096. ;; If we are refiling for capture, make sure that the
  10097. ;; last-capture pointers point here
  10098. (when (org-bound-and-true-p org-refile-for-capture)
  10099. (with-demoted-errors
  10100. (bookmark-set "org-capture-last-stored-marker"))
  10101. (move-marker org-capture-last-stored-marker (point)))
  10102. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10103. (run-hooks 'org-after-refile-insert-hook))))
  10104. (unless org-refile-keep
  10105. (if regionp
  10106. (delete-region (point) (+ (point) region-length))
  10107. (org-cut-subtree)))
  10108. (when (featurep 'org-inlinetask)
  10109. (org-inlinetask-remove-END-maybe))
  10110. (setq org-markers-to-move nil)
  10111. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10112. (defun org-refile-goto-last-stored ()
  10113. "Go to the location where the last refile was stored."
  10114. (interactive)
  10115. (bookmark-jump "org-refile-last-stored")
  10116. (message "This is the location of the last refile"))
  10117. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10118. no-exclude)
  10119. "Prompt the user for a refile location, using PROMPT.
  10120. PROMPT should not be suffixed with a colon and a space, because
  10121. this function appends the default value from
  10122. `org-refile-history' automatically, if that is not empty.
  10123. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10124. this is used for the GOTO interface."
  10125. (let ((org-refile-targets org-refile-targets)
  10126. (org-refile-use-outline-path org-refile-use-outline-path)
  10127. excluded-entries)
  10128. (when (and (derived-mode-p 'org-mode)
  10129. (not org-refile-use-cache)
  10130. (not no-exclude))
  10131. (org-map-tree
  10132. (lambda()
  10133. (setq excluded-entries
  10134. (append excluded-entries (list (org-get-heading t t)))))))
  10135. (setq org-refile-target-table
  10136. (org-refile-get-targets default-buffer excluded-entries)))
  10137. (unless org-refile-target-table
  10138. (error "No refile targets"))
  10139. (let* ((cbuf (current-buffer))
  10140. (partial-completion-mode nil)
  10141. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10142. (cfunc (if (and org-refile-use-outline-path
  10143. org-outline-path-complete-in-steps)
  10144. 'org-olpath-completing-read
  10145. 'org-icompleting-read))
  10146. (extra (if org-refile-use-outline-path "/" ""))
  10147. (cbnex (concat (buffer-name) extra))
  10148. (filename (and cfn (expand-file-name cfn)))
  10149. (tbl (mapcar
  10150. (lambda (x)
  10151. (if (and (not (member org-refile-use-outline-path
  10152. '(file full-file-path)))
  10153. (not (equal filename (nth 1 x))))
  10154. (cons (concat (car x) extra " ("
  10155. (file-name-nondirectory (nth 1 x)) ")")
  10156. (cdr x))
  10157. (cons (concat (car x) extra) (cdr x))))
  10158. org-refile-target-table))
  10159. (completion-ignore-case t)
  10160. cdef
  10161. (prompt (concat prompt
  10162. (or (and (car org-refile-history)
  10163. (concat " (default " (car org-refile-history) ")"))
  10164. (and (assoc cbnex tbl) (setq cdef cbnex)
  10165. (concat " (default " cbnex ")"))) ": "))
  10166. pa answ parent-target child parent old-hist)
  10167. (setq old-hist org-refile-history)
  10168. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10169. nil 'org-refile-history (or cdef (car org-refile-history))))
  10170. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  10171. (org-refile-check-position pa)
  10172. (if pa
  10173. (progn
  10174. (when (or (not org-refile-history)
  10175. (not (eq old-hist org-refile-history))
  10176. (not (equal (car pa) (car org-refile-history))))
  10177. (setq org-refile-history
  10178. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10179. org-refile-history
  10180. (cdr org-refile-history))))
  10181. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10182. (pop org-refile-history)))
  10183. pa)
  10184. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10185. (progn
  10186. (setq parent (match-string 1 answ)
  10187. child (match-string 2 answ))
  10188. (setq parent-target (or (assoc parent tbl)
  10189. (assoc (concat parent "/") tbl)))
  10190. (when (and parent-target
  10191. (or (eq new-nodes t)
  10192. (and (eq new-nodes 'confirm)
  10193. (y-or-n-p (format "Create new node \"%s\"? "
  10194. child)))))
  10195. (org-refile-new-child parent-target child)))
  10196. (error "Invalid target location")))))
  10197. (declare-function org-string-nw-p "org-macs" (s))
  10198. (defun org-refile-check-position (refile-pointer)
  10199. "Check if the refile pointer matches the headline to which it points."
  10200. (let* ((file (nth 1 refile-pointer))
  10201. (re (nth 2 refile-pointer))
  10202. (pos (nth 3 refile-pointer))
  10203. buffer)
  10204. (if (and (not (markerp pos)) (not file))
  10205. (error "Please save the buffer to a file before refiling")
  10206. (when (org-string-nw-p re)
  10207. (setq buffer (if (markerp pos)
  10208. (marker-buffer pos)
  10209. (or (find-buffer-visiting file)
  10210. (find-file-noselect file))))
  10211. (with-current-buffer buffer
  10212. (save-excursion
  10213. (save-restriction
  10214. (widen)
  10215. (goto-char pos)
  10216. (beginning-of-line 1)
  10217. (unless (org-looking-at-p re)
  10218. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10219. (defun org-refile-new-child (parent-target child)
  10220. "Use refile target PARENT-TARGET to add new CHILD below it."
  10221. (unless parent-target
  10222. (error "Cannot find parent for new node"))
  10223. (let ((file (nth 1 parent-target))
  10224. (pos (nth 3 parent-target))
  10225. level)
  10226. (with-current-buffer (or (find-buffer-visiting file)
  10227. (find-file-noselect file))
  10228. (save-excursion
  10229. (save-restriction
  10230. (widen)
  10231. (if pos
  10232. (goto-char pos)
  10233. (goto-char (point-max))
  10234. (if (not (bolp)) (newline)))
  10235. (when (looking-at org-outline-regexp)
  10236. (setq level (funcall outline-level))
  10237. (org-end-of-subtree t t))
  10238. (org-back-over-empty-lines)
  10239. (insert "\n" (make-string
  10240. (if pos (org-get-valid-level level 1) 1) ?*)
  10241. " " child "\n")
  10242. (beginning-of-line 0)
  10243. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10244. (defun org-olpath-completing-read (prompt collection &rest args)
  10245. "Read an outline path like a file name."
  10246. (let ((thetable collection)
  10247. (org-completion-use-ido nil) ; does not work with ido.
  10248. (org-completion-use-iswitchb nil)) ; or iswitchb
  10249. (apply
  10250. 'org-icompleting-read prompt
  10251. (lambda (string predicate &optional flag)
  10252. (let (rtn r f (l (length string)))
  10253. (cond
  10254. ((eq flag nil)
  10255. ;; try completion
  10256. (try-completion string thetable))
  10257. ((eq flag t)
  10258. ;; all-completions
  10259. (setq rtn (all-completions string thetable predicate))
  10260. (mapcar
  10261. (lambda (x)
  10262. (setq r (substring x l))
  10263. (if (string-match " ([^)]*)$" x)
  10264. (setq f (match-string 0 x))
  10265. (setq f ""))
  10266. (if (string-match "/" r)
  10267. (concat string (substring r 0 (match-end 0)) f)
  10268. x))
  10269. rtn))
  10270. ((eq flag 'lambda)
  10271. ;; exact match?
  10272. (assoc string thetable)))))
  10273. args)))
  10274. ;;;; Dynamic blocks
  10275. (defun org-find-dblock (name)
  10276. "Find the first dynamic block with name NAME in the buffer.
  10277. If not found, stay at current position and return nil."
  10278. (let ((case-fold-search t) pos)
  10279. (save-excursion
  10280. (goto-char (point-min))
  10281. (setq pos (and (re-search-forward
  10282. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10283. (match-beginning 0))))
  10284. (if pos (goto-char pos))
  10285. pos))
  10286. (defconst org-dblock-start-re
  10287. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  10288. "Matches the start line of a dynamic block, with parameters.")
  10289. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  10290. "Matches the end of a dynamic block.")
  10291. (defun org-create-dblock (plist)
  10292. "Create a dynamic block section, with parameters taken from PLIST.
  10293. PLIST must contain a :name entry which is used as name of the block."
  10294. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10295. (end-of-line 1)
  10296. (newline))
  10297. (let ((col (current-column))
  10298. (name (plist-get plist :name)))
  10299. (insert "#+BEGIN: " name)
  10300. (while plist
  10301. (if (eq (car plist) :name)
  10302. (setq plist (cddr plist))
  10303. (insert " " (prin1-to-string (pop plist)))))
  10304. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10305. (beginning-of-line -2)))
  10306. (defun org-prepare-dblock ()
  10307. "Prepare dynamic block for refresh.
  10308. This empties the block, puts the cursor at the insert position and returns
  10309. the property list including an extra property :name with the block name."
  10310. (unless (looking-at org-dblock-start-re)
  10311. (error "Not at a dynamic block"))
  10312. (let* ((begdel (1+ (match-end 0)))
  10313. (name (org-no-properties (match-string 1)))
  10314. (params (append (list :name name)
  10315. (read (concat "(" (match-string 3) ")")))))
  10316. (save-excursion
  10317. (beginning-of-line 1)
  10318. (skip-chars-forward " \t")
  10319. (setq params (plist-put params :indentation-column (current-column))))
  10320. (unless (re-search-forward org-dblock-end-re nil t)
  10321. (error "Dynamic block not terminated"))
  10322. (setq params
  10323. (append params
  10324. (list :content (buffer-substring
  10325. begdel (match-beginning 0)))))
  10326. (delete-region begdel (match-beginning 0))
  10327. (goto-char begdel)
  10328. (open-line 1)
  10329. params))
  10330. (defun org-map-dblocks (&optional command)
  10331. "Apply COMMAND to all dynamic blocks in the current buffer.
  10332. If COMMAND is not given, use `org-update-dblock'."
  10333. (let ((cmd (or command 'org-update-dblock)))
  10334. (save-excursion
  10335. (goto-char (point-min))
  10336. (while (re-search-forward org-dblock-start-re nil t)
  10337. (goto-char (match-beginning 0))
  10338. (save-excursion
  10339. (condition-case nil
  10340. (funcall cmd)
  10341. (error (message "Error during update of dynamic block"))))
  10342. (unless (re-search-forward org-dblock-end-re nil t)
  10343. (error "Dynamic block not terminated"))))))
  10344. (defun org-dblock-update (&optional arg)
  10345. "User command for updating dynamic blocks.
  10346. Update the dynamic block at point. With prefix ARG, update all dynamic
  10347. blocks in the buffer."
  10348. (interactive "P")
  10349. (if arg
  10350. (org-update-all-dblocks)
  10351. (or (looking-at org-dblock-start-re)
  10352. (org-beginning-of-dblock))
  10353. (org-update-dblock)))
  10354. (defun org-update-dblock ()
  10355. "Update the dynamic block at point.
  10356. This means to empty the block, parse for parameters and then call
  10357. the correct writing function."
  10358. (interactive)
  10359. (save-window-excursion
  10360. (let* ((pos (point))
  10361. (line (org-current-line))
  10362. (params (org-prepare-dblock))
  10363. (name (plist-get params :name))
  10364. (indent (plist-get params :indentation-column))
  10365. (cmd (intern (concat "org-dblock-write:" name))))
  10366. (message "Updating dynamic block `%s' at line %d..." name line)
  10367. (funcall cmd params)
  10368. (message "Updating dynamic block `%s' at line %d...done" name line)
  10369. (goto-char pos)
  10370. (when (and indent (> indent 0))
  10371. (setq indent (make-string indent ?\ ))
  10372. (save-excursion
  10373. (org-beginning-of-dblock)
  10374. (forward-line 1)
  10375. (while (not (looking-at org-dblock-end-re))
  10376. (insert indent)
  10377. (beginning-of-line 2))
  10378. (when (looking-at org-dblock-end-re)
  10379. (and (looking-at "[ \t]+")
  10380. (replace-match ""))
  10381. (insert indent)))))))
  10382. (defun org-beginning-of-dblock ()
  10383. "Find the beginning of the dynamic block at point.
  10384. Error if there is no such block at point."
  10385. (let ((pos (point))
  10386. beg)
  10387. (end-of-line 1)
  10388. (if (and (re-search-backward org-dblock-start-re nil t)
  10389. (setq beg (match-beginning 0))
  10390. (re-search-forward org-dblock-end-re nil t)
  10391. (> (match-end 0) pos))
  10392. (goto-char beg)
  10393. (goto-char pos)
  10394. (error "Not in a dynamic block"))))
  10395. (defun org-update-all-dblocks ()
  10396. "Update all dynamic blocks in the buffer.
  10397. This function can be used in a hook."
  10398. (interactive)
  10399. (when (derived-mode-p 'org-mode)
  10400. (org-map-dblocks 'org-update-dblock)))
  10401. ;;;; Completion
  10402. (defun org-get-export-keywords ()
  10403. "Return a list of all currently understood export keywords.
  10404. Export keywords include options, block names, attributes and
  10405. keywords relative to each registered export back-end."
  10406. (delq nil
  10407. (let (keywords)
  10408. (mapc
  10409. (lambda (back-end)
  10410. (let ((props (cdr back-end)))
  10411. ;; Back-end name (for keywords, like #+LATEX:)
  10412. (push (upcase (symbol-name (car back-end))) keywords)
  10413. ;; Back-end options.
  10414. (mapc (lambda (option) (push (cadr option) keywords))
  10415. (plist-get (cdr back-end) :options-alist))))
  10416. (org-bound-and-true-p org-export-registered-backends))
  10417. keywords)))
  10418. (defconst org-options-keywords
  10419. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE"
  10420. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  10421. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY"
  10422. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  10423. "TITLE:" "TODO:" "TYP_TODO:"))
  10424. (defcustom org-structure-template-alist
  10425. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10426. "<src lang=\"?\">\n\n</src>")
  10427. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10428. "<example>\n?\n</example>")
  10429. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10430. "<quote>\n?\n</quote>")
  10431. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10432. "<verse>\n?\n</verse>")
  10433. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM"
  10434. "<verbatim>\n?\n</verbatim>")
  10435. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10436. "<center>\n?\n</center>")
  10437. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10438. "<literal style=\"latex\">\n?\n</literal>")
  10439. ("L" "#+LaTeX: "
  10440. "<literal style=\"latex\">?</literal>")
  10441. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10442. "<literal style=\"html\">\n?\n</literal>")
  10443. ("H" "#+HTML: "
  10444. "<literal style=\"html\">?</literal>")
  10445. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10446. ("A" "#+ASCII: ")
  10447. ("i" "#+INDEX: ?"
  10448. "#+INDEX: ?")
  10449. ("I" "#+INCLUDE: %file ?"
  10450. "<include file=%file markup=\"?\">"))
  10451. "Structure completion elements.
  10452. This is a list of abbreviation keys and values. The value gets inserted
  10453. if you type `<' followed by the key and then press the completion key,
  10454. usually `M-TAB'. %file will be replaced by a file name after prompting
  10455. for the file using completion. The cursor will be placed at the position
  10456. of the `?` in the template.
  10457. There are two templates for each key, the first uses the original Org syntax,
  10458. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10459. the default when the /org-mtags.el/ module has been loaded. See also the
  10460. variable `org-mtags-prefer-muse-templates'."
  10461. :group 'org-completion
  10462. :type '(repeat
  10463. (string :tag "Key")
  10464. (string :tag "Template")
  10465. (string :tag "Muse Template")))
  10466. (defun org-try-structure-completion ()
  10467. "Try to complete a structure template before point.
  10468. This looks for strings like \"<e\" on an otherwise empty line and
  10469. expands them."
  10470. (let ((l (buffer-substring (point-at-bol) (point)))
  10471. a)
  10472. (when (and (looking-at "[ \t]*$")
  10473. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10474. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10475. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10476. (match-beginning 1)) a)
  10477. t)))
  10478. (defun org-complete-expand-structure-template (start cell)
  10479. "Expand a structure template."
  10480. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10481. (rpl (nth (if musep 2 1) cell))
  10482. (ind ""))
  10483. (delete-region start (point))
  10484. (when (string-match "\\`#\\+" rpl)
  10485. (cond
  10486. ((bolp))
  10487. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10488. (setq ind (buffer-substring (point-at-bol) (point))))
  10489. (t (newline))))
  10490. (setq start (point))
  10491. (if (string-match "%file" rpl)
  10492. (setq rpl (replace-match
  10493. (concat
  10494. "\""
  10495. (save-match-data
  10496. (abbreviate-file-name (read-file-name "Include file: ")))
  10497. "\"")
  10498. t t rpl)))
  10499. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10500. (concat "\n" ind)))
  10501. (insert rpl)
  10502. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10503. ;;;; TODO, DEADLINE, Comments
  10504. (defun org-toggle-comment ()
  10505. "Change the COMMENT state of an entry."
  10506. (interactive)
  10507. (save-excursion
  10508. (org-back-to-heading)
  10509. (let (case-fold-search)
  10510. (cond
  10511. ((looking-at (format org-heading-keyword-regexp-format
  10512. org-comment-string))
  10513. (goto-char (match-end 1))
  10514. (looking-at (concat " +" org-comment-string))
  10515. (replace-match "" t t)
  10516. (when (eolp) (insert " ")))
  10517. ((looking-at org-outline-regexp)
  10518. (goto-char (match-end 0))
  10519. (insert org-comment-string " "))))))
  10520. (defvar org-last-todo-state-is-todo nil
  10521. "This is non-nil when the last TODO state change led to a TODO state.
  10522. If the last change removed the TODO tag or switched to DONE, then
  10523. this is nil.")
  10524. (defvar org-setting-tags nil) ; dynamically skipped
  10525. (defvar org-todo-setup-filter-hook nil
  10526. "Hook for functions that pre-filter todo specs.
  10527. Each function takes a todo spec and returns either nil or the spec
  10528. transformed into canonical form." )
  10529. (defvar org-todo-get-default-hook nil
  10530. "Hook for functions that get a default item for todo.
  10531. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10532. nil or a string to be used for the todo mark." )
  10533. (defvar org-agenda-headline-snapshot-before-repeat)
  10534. (defun org-current-effective-time ()
  10535. "Return current time adjusted for `org-extend-today-until' variable."
  10536. (let* ((ct (org-current-time))
  10537. (dct (decode-time ct))
  10538. (ct1
  10539. (cond
  10540. (org-use-last-clock-out-time-as-effective-time
  10541. (or (org-clock-get-last-clock-out-time) ct))
  10542. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  10543. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  10544. (t ct))))
  10545. ct1))
  10546. (defun org-todo-yesterday (&optional arg)
  10547. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10548. (interactive "P")
  10549. (if (eq major-mode 'org-agenda-mode)
  10550. (apply 'org-agenda-todo-yesterday arg)
  10551. (let* ((hour (third (decode-time
  10552. (org-current-time))))
  10553. (org-extend-today-until (1+ hour)))
  10554. (org-todo arg))))
  10555. (defvar org-block-entry-blocking ""
  10556. "First entry preventing the TODO state change.")
  10557. (defun org-todo (&optional arg)
  10558. "Change the TODO state of an item.
  10559. The state of an item is given by a keyword at the start of the heading,
  10560. like
  10561. *** TODO Write paper
  10562. *** DONE Call mom
  10563. The different keywords are specified in the variable `org-todo-keywords'.
  10564. By default the available states are \"TODO\" and \"DONE\".
  10565. So for this example: when the item starts with TODO, it is changed to DONE.
  10566. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10567. DONE are present, add TODO at the beginning of the heading.
  10568. With \\[universal-argument] prefix arg, use completion to determine the new \
  10569. state.
  10570. With numeric prefix arg, switch to that state.
  10571. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10572. keywords (nextset).
  10573. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10574. With a numeric prefix arg of 0, inhibit note taking for the change.
  10575. For calling through lisp, arg is also interpreted in the following way:
  10576. 'none -> empty state
  10577. \"\"(empty string) -> switch to empty state
  10578. 'done -> switch to DONE
  10579. 'nextset -> switch to the next set of keywords
  10580. 'previousset -> switch to the previous set of keywords
  10581. \"WAITING\" -> switch to the specified keyword, but only if it
  10582. really is a member of `org-todo-keywords'."
  10583. (interactive "P")
  10584. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10585. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10586. 'region-start-level 'region))
  10587. org-loop-over-headlines-in-active-region)
  10588. (org-map-entries
  10589. `(org-todo ,arg)
  10590. org-loop-over-headlines-in-active-region
  10591. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10592. (if (equal arg '(16)) (setq arg 'nextset))
  10593. (let ((org-blocker-hook org-blocker-hook)
  10594. commentp
  10595. case-fold-search)
  10596. (when (equal arg '(64))
  10597. (setq arg nil org-blocker-hook nil))
  10598. (when (and org-blocker-hook
  10599. (or org-inhibit-blocking
  10600. (org-entry-get nil "NOBLOCKING")))
  10601. (setq org-blocker-hook nil))
  10602. (save-excursion
  10603. (catch 'exit
  10604. (org-back-to-heading t)
  10605. (when (looking-at (concat "^\\*+ " org-comment-string))
  10606. (org-toggle-comment)
  10607. (setq commentp t))
  10608. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10609. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10610. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10611. (let* ((match-data (match-data))
  10612. (startpos (point-at-bol))
  10613. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10614. (org-log-done org-log-done)
  10615. (org-log-repeat org-log-repeat)
  10616. (org-todo-log-states org-todo-log-states)
  10617. (org-inhibit-logging
  10618. (if (equal arg 0)
  10619. (progn (setq arg nil) 'note) org-inhibit-logging))
  10620. (this (match-string 1))
  10621. (hl-pos (match-beginning 0))
  10622. (head (org-get-todo-sequence-head this))
  10623. (ass (assoc head org-todo-kwd-alist))
  10624. (interpret (nth 1 ass))
  10625. (done-word (nth 3 ass))
  10626. (final-done-word (nth 4 ass))
  10627. (org-last-state (or this ""))
  10628. (completion-ignore-case t)
  10629. (member (member this org-todo-keywords-1))
  10630. (tail (cdr member))
  10631. (org-state (cond
  10632. ((and org-todo-key-trigger
  10633. (or (and (equal arg '(4))
  10634. (eq org-use-fast-todo-selection 'prefix))
  10635. (and (not arg) org-use-fast-todo-selection
  10636. (not (eq org-use-fast-todo-selection
  10637. 'prefix)))))
  10638. ;; Use fast selection
  10639. (org-fast-todo-selection))
  10640. ((and (equal arg '(4))
  10641. (or (not org-use-fast-todo-selection)
  10642. (not org-todo-key-trigger)))
  10643. ;; Read a state with completion
  10644. (org-icompleting-read
  10645. "State: " (mapcar (lambda(x) (list x))
  10646. org-todo-keywords-1)
  10647. nil t))
  10648. ((eq arg 'right)
  10649. (if this
  10650. (if tail (car tail) nil)
  10651. (car org-todo-keywords-1)))
  10652. ((eq arg 'left)
  10653. (if (equal member org-todo-keywords-1)
  10654. nil
  10655. (if this
  10656. (nth (- (length org-todo-keywords-1)
  10657. (length tail) 2)
  10658. org-todo-keywords-1)
  10659. (org-last org-todo-keywords-1))))
  10660. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10661. (setq arg nil))) ; hack to fall back to cycling
  10662. (arg
  10663. ;; user or caller requests a specific state
  10664. (cond
  10665. ((equal arg "") nil)
  10666. ((eq arg 'none) nil)
  10667. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10668. ((eq arg 'nextset)
  10669. (or (car (cdr (member head org-todo-heads)))
  10670. (car org-todo-heads)))
  10671. ((eq arg 'previousset)
  10672. (let ((org-todo-heads (reverse org-todo-heads)))
  10673. (or (car (cdr (member head org-todo-heads)))
  10674. (car org-todo-heads))))
  10675. ((car (member arg org-todo-keywords-1)))
  10676. ((stringp arg)
  10677. (error "State `%s' not valid in this file" arg))
  10678. ((nth (1- (prefix-numeric-value arg))
  10679. org-todo-keywords-1))))
  10680. ((null member) (or head (car org-todo-keywords-1)))
  10681. ((equal this final-done-word) nil) ;; -> make empty
  10682. ((null tail) nil) ;; -> first entry
  10683. ((memq interpret '(type priority))
  10684. (if (eq this-command last-command)
  10685. (car tail)
  10686. (if (> (length tail) 0)
  10687. (or done-word (car org-done-keywords))
  10688. nil)))
  10689. (t
  10690. (car tail))))
  10691. (org-state (or
  10692. (run-hook-with-args-until-success
  10693. 'org-todo-get-default-hook org-state org-last-state)
  10694. org-state))
  10695. (next (if org-state (concat " " org-state " ") " "))
  10696. (change-plist (list :type 'todo-state-change :from this :to org-state
  10697. :position startpos))
  10698. dolog now-done-p)
  10699. (when org-blocker-hook
  10700. (setq org-last-todo-state-is-todo
  10701. (not (member this org-done-keywords)))
  10702. (unless (save-excursion
  10703. (save-match-data
  10704. (org-with-wide-buffer
  10705. (run-hook-with-args-until-failure
  10706. 'org-blocker-hook change-plist))))
  10707. (if (org-called-interactively-p 'interactive)
  10708. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  10709. this org-state org-block-entry-blocking)
  10710. ;; fail silently
  10711. (message "TODO state change from %s to %s blocked (by \"%s\")"
  10712. this org-state org-block-entry-blocking)
  10713. (throw 'exit nil))))
  10714. (store-match-data match-data)
  10715. (replace-match next t t)
  10716. (unless (pos-visible-in-window-p hl-pos)
  10717. (message "TODO state changed to %s" (org-trim next)))
  10718. (unless head
  10719. (setq head (org-get-todo-sequence-head org-state)
  10720. ass (assoc head org-todo-kwd-alist)
  10721. interpret (nth 1 ass)
  10722. done-word (nth 3 ass)
  10723. final-done-word (nth 4 ass)))
  10724. (when (memq arg '(nextset previousset))
  10725. (message "Keyword-Set %d/%d: %s"
  10726. (- (length org-todo-sets) -1
  10727. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10728. (length org-todo-sets)
  10729. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10730. (setq org-last-todo-state-is-todo
  10731. (not (member org-state org-done-keywords)))
  10732. (setq now-done-p (and (member org-state org-done-keywords)
  10733. (not (member this org-done-keywords))))
  10734. (and logging (org-local-logging logging))
  10735. (when (and (or org-todo-log-states org-log-done)
  10736. (not (eq org-inhibit-logging t))
  10737. (not (memq arg '(nextset previousset))))
  10738. ;; we need to look at recording a time and note
  10739. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10740. (nth 2 (assoc this org-todo-log-states))))
  10741. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10742. (setq dolog 'time))
  10743. (when (and org-state
  10744. (member org-state org-not-done-keywords)
  10745. (not (member this org-not-done-keywords)))
  10746. ;; This is now a todo state and was not one before
  10747. ;; If there was a CLOSED time stamp, get rid of it.
  10748. (org-add-planning-info nil nil 'closed))
  10749. (when (and now-done-p org-log-done)
  10750. ;; It is now done, and it was not done before
  10751. (org-add-planning-info 'closed (org-current-effective-time))
  10752. (if (and (not dolog) (eq 'note org-log-done))
  10753. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10754. (when (and org-state dolog)
  10755. ;; This is a non-nil state, and we need to log it
  10756. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10757. ;; Fixup tag positioning
  10758. (org-todo-trigger-tag-changes org-state)
  10759. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10760. (when org-provide-todo-statistics
  10761. (org-update-parent-todo-statistics))
  10762. (run-hooks 'org-after-todo-state-change-hook)
  10763. (if (and arg (not (member org-state org-done-keywords)))
  10764. (setq head (org-get-todo-sequence-head org-state)))
  10765. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10766. ;; Do we need to trigger a repeat?
  10767. (when now-done-p
  10768. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10769. ;; This is for the agenda, take a snapshot of the headline.
  10770. (save-match-data
  10771. (setq org-agenda-headline-snapshot-before-repeat
  10772. (org-get-heading))))
  10773. (org-auto-repeat-maybe org-state))
  10774. ;; Fixup cursor location if close to the keyword
  10775. (if (and (outline-on-heading-p)
  10776. (not (bolp))
  10777. (save-excursion (beginning-of-line 1)
  10778. (looking-at org-todo-line-regexp))
  10779. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10780. (progn
  10781. (goto-char (or (match-end 2) (match-end 1)))
  10782. (and (looking-at " ") (just-one-space))))
  10783. (when org-trigger-hook
  10784. (save-excursion
  10785. (run-hook-with-args 'org-trigger-hook change-plist)))
  10786. (when commentp (org-toggle-comment))))))))
  10787. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10788. "Block turning an entry into a TODO, using the hierarchy.
  10789. This checks whether the current task should be blocked from state
  10790. changes. Such blocking occurs when:
  10791. 1. The task has children which are not all in a completed state.
  10792. 2. A task has a parent with the property :ORDERED:, and there
  10793. are siblings prior to the current task with incomplete
  10794. status.
  10795. 3. The parent of the task is blocked because it has siblings that should
  10796. be done first, or is child of a block grandparent TODO entry."
  10797. (if (not org-enforce-todo-dependencies)
  10798. t ; if locally turned off don't block
  10799. (catch 'dont-block
  10800. ;; If this is not a todo state change, or if this entry is already DONE,
  10801. ;; do not block
  10802. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10803. (member (plist-get change-plist :from)
  10804. (cons 'done org-done-keywords))
  10805. (member (plist-get change-plist :to)
  10806. (cons 'todo org-not-done-keywords))
  10807. (not (plist-get change-plist :to)))
  10808. (throw 'dont-block t))
  10809. ;; If this task has children, and any are undone, it's blocked
  10810. (save-excursion
  10811. (org-back-to-heading t)
  10812. (let ((this-level (funcall outline-level)))
  10813. (outline-next-heading)
  10814. (let ((child-level (funcall outline-level)))
  10815. (while (and (not (eobp))
  10816. (> child-level this-level))
  10817. ;; this todo has children, check whether they are all
  10818. ;; completed
  10819. (if (and (not (org-entry-is-done-p))
  10820. (org-entry-is-todo-p))
  10821. (progn (setq org-block-entry-blocking (org-get-heading))
  10822. (throw 'dont-block nil)))
  10823. (outline-next-heading)
  10824. (setq child-level (funcall outline-level))))))
  10825. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10826. ;; any previous siblings are undone, it's blocked
  10827. (save-excursion
  10828. (org-back-to-heading t)
  10829. (let* ((pos (point))
  10830. (parent-pos (and (org-up-heading-safe) (point))))
  10831. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10832. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10833. (forward-line 1)
  10834. (re-search-forward org-not-done-heading-regexp pos t))
  10835. (setq org-block-entry-blocking (match-string 0))
  10836. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10837. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10838. (while t
  10839. (goto-char parent-pos)
  10840. (if (not (looking-at org-not-done-heading-regexp))
  10841. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10842. (setq pos (point))
  10843. (setq parent-pos (and (org-up-heading-safe) (point)))
  10844. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10845. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10846. (forward-line 1)
  10847. (re-search-forward org-not-done-heading-regexp pos t)
  10848. (setq org-block-entry-blocking (org-get-heading)))
  10849. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10850. (defcustom org-track-ordered-property-with-tag nil
  10851. "Should the ORDERED property also be shown as a tag?
  10852. The ORDERED property decides if an entry should require subtasks to be
  10853. completed in sequence. Since a property is not very visible, setting
  10854. this option means that toggling the ORDERED property with the command
  10855. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10856. not relevant for the behavior, but it makes things more visible.
  10857. Note that toggling the tag with tags commands will not change the property
  10858. and therefore not influence behavior!
  10859. This can be t, meaning the tag ORDERED should be used, It can also be a
  10860. string to select a different tag for this task."
  10861. :group 'org-todo
  10862. :type '(choice
  10863. (const :tag "No tracking" nil)
  10864. (const :tag "Track with ORDERED tag" t)
  10865. (string :tag "Use other tag")))
  10866. (defun org-toggle-ordered-property ()
  10867. "Toggle the ORDERED property of the current entry.
  10868. For better visibility, you can track the value of this property with a tag.
  10869. See variable `org-track-ordered-property-with-tag'."
  10870. (interactive)
  10871. (let* ((t1 org-track-ordered-property-with-tag)
  10872. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10873. (save-excursion
  10874. (org-back-to-heading)
  10875. (if (org-entry-get nil "ORDERED")
  10876. (progn
  10877. (org-delete-property "ORDERED")
  10878. (and tag (org-toggle-tag tag 'off))
  10879. (message "Subtasks can be completed in arbitrary order"))
  10880. (org-entry-put nil "ORDERED" "t")
  10881. (and tag (org-toggle-tag tag 'on))
  10882. (message "Subtasks must be completed in sequence")))))
  10883. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10884. (defun org-block-todo-from-checkboxes (change-plist)
  10885. "Block turning an entry into a TODO, using checkboxes.
  10886. This checks whether the current task should be blocked from state
  10887. changes because there are unchecked boxes in this entry."
  10888. (if (not org-enforce-todo-checkbox-dependencies)
  10889. t ; if locally turned off don't block
  10890. (catch 'dont-block
  10891. ;; If this is not a todo state change, or if this entry is already DONE,
  10892. ;; do not block
  10893. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10894. (member (plist-get change-plist :from)
  10895. (cons 'done org-done-keywords))
  10896. (member (plist-get change-plist :to)
  10897. (cons 'todo org-not-done-keywords))
  10898. (not (plist-get change-plist :to)))
  10899. (throw 'dont-block t))
  10900. ;; If this task has checkboxes that are not checked, it's blocked
  10901. (save-excursion
  10902. (org-back-to-heading t)
  10903. (let ((beg (point)) end)
  10904. (outline-next-heading)
  10905. (setq end (point))
  10906. (goto-char beg)
  10907. (if (org-list-search-forward
  10908. (concat (org-item-beginning-re)
  10909. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10910. "\\[[- ]\\]")
  10911. end t)
  10912. (progn
  10913. (if (boundp 'org-blocked-by-checkboxes)
  10914. (setq org-blocked-by-checkboxes t))
  10915. (throw 'dont-block nil)))))
  10916. t))) ; do not block
  10917. (defun org-entry-blocked-p ()
  10918. "Is the current entry blocked?"
  10919. (org-with-buffer-modified-unmodified
  10920. (if (org-entry-get nil "NOBLOCKING")
  10921. nil ;; Never block this entry
  10922. (not
  10923. (run-hook-with-args-until-failure
  10924. 'org-blocker-hook
  10925. (list :type 'todo-state-change
  10926. :position (point)
  10927. :from 'todo
  10928. :to 'done))))))
  10929. (defun org-update-statistics-cookies (all)
  10930. "Update the statistics cookie, either from TODO or from checkboxes.
  10931. This should be called with the cursor in a line with a statistics cookie."
  10932. (interactive "P")
  10933. (if all
  10934. (progn
  10935. (org-update-checkbox-count 'all)
  10936. (org-map-entries 'org-update-parent-todo-statistics))
  10937. (if (not (org-at-heading-p))
  10938. (org-update-checkbox-count)
  10939. (let ((pos (point-marker))
  10940. end l1 l2)
  10941. (ignore-errors (org-back-to-heading t))
  10942. (if (not (org-at-heading-p))
  10943. (org-update-checkbox-count)
  10944. (setq l1 (org-outline-level))
  10945. (setq end (save-excursion
  10946. (outline-next-heading)
  10947. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10948. (point)))
  10949. (if (and (save-excursion
  10950. (re-search-forward
  10951. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10952. (not (save-excursion (re-search-forward
  10953. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10954. (org-update-checkbox-count)
  10955. (if (and l2 (> l2 l1))
  10956. (progn
  10957. (goto-char end)
  10958. (org-update-parent-todo-statistics))
  10959. (goto-char pos)
  10960. (beginning-of-line 1)
  10961. (while (re-search-forward
  10962. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10963. (point-at-eol) t)
  10964. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10965. (goto-char pos)
  10966. (move-marker pos nil)))))
  10967. (defvar org-entry-property-inherited-from) ;; defined below
  10968. (defun org-update-parent-todo-statistics ()
  10969. "Update any statistics cookie in the parent of the current headline.
  10970. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10971. the entire subtree and this will travel up the hierarchy and update
  10972. statistics everywhere."
  10973. (let* ((prop (save-excursion (org-up-heading-safe)
  10974. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10975. (recursive (or (not org-hierarchical-todo-statistics)
  10976. (and prop (string-match "\\<recursive\\>" prop))))
  10977. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10978. 0))
  10979. (first t)
  10980. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10981. level ltoggle l1 new ndel
  10982. (cnt-all 0) (cnt-done 0) is-percent kwd
  10983. checkbox-beg ov ovs ove cookie-present)
  10984. (catch 'exit
  10985. (save-excursion
  10986. (beginning-of-line 1)
  10987. (setq ltoggle (funcall outline-level))
  10988. ;; Three situations are to consider:
  10989. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10990. ;; to the top-level ancestor on the headline;
  10991. ;; 2. If parent has "recursive" property, repeat up to the
  10992. ;; headline setting that property, taking inheritance into
  10993. ;; account;
  10994. ;; 3. Else, move up to direct parent and proceed only once.
  10995. (while (and (setq level (org-up-heading-safe))
  10996. (or recursive first)
  10997. (>= (point) lim))
  10998. (setq first nil cookie-present nil)
  10999. (unless (and level
  11000. (not (string-match
  11001. "\\<checkbox\\>"
  11002. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11003. "")))))
  11004. (throw 'exit nil))
  11005. (while (re-search-forward box-re (point-at-eol) t)
  11006. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11007. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11008. (save-match-data
  11009. (unless (outline-next-heading) (throw 'exit nil))
  11010. (while (and (looking-at org-complex-heading-regexp)
  11011. (> (setq l1 (length (match-string 1))) level))
  11012. (setq kwd (and (or recursive (= l1 ltoggle))
  11013. (match-string 2)))
  11014. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11015. (and (listp org-provide-todo-statistics)
  11016. (or (member kwd org-provide-todo-statistics)
  11017. (member kwd org-done-keywords))))
  11018. (setq cnt-all (1+ cnt-all))
  11019. (if (eq org-provide-todo-statistics t)
  11020. (and kwd (setq cnt-all (1+ cnt-all)))))
  11021. (and (member kwd org-done-keywords)
  11022. (setq cnt-done (1+ cnt-done)))
  11023. (outline-next-heading)))
  11024. (setq new
  11025. (if is-percent
  11026. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  11027. (format "[%d/%d]" cnt-done cnt-all))
  11028. ndel (- (match-end 0) checkbox-beg))
  11029. ;; handle overlays when updating cookie from column view
  11030. (when (setq ov (car (overlays-at checkbox-beg)))
  11031. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11032. (delete-overlay ov))
  11033. (goto-char checkbox-beg)
  11034. (insert new)
  11035. (delete-region (point) (+ (point) ndel))
  11036. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11037. (when ov (move-overlay ov ovs ove)))
  11038. (when cookie-present
  11039. (run-hook-with-args 'org-after-todo-statistics-hook
  11040. cnt-done (- cnt-all cnt-done))))))
  11041. (run-hooks 'org-todo-statistics-hook)))
  11042. (defvar org-after-todo-statistics-hook nil
  11043. "Hook that is called after a TODO statistics cookie has been updated.
  11044. Each function is called with two arguments: the number of not-done entries
  11045. and the number of done entries.
  11046. For example, the following function, when added to this hook, will switch
  11047. an entry to DONE when all children are done, and back to TODO when new
  11048. entries are set to a TODO status. Note that this hook is only called
  11049. when there is a statistics cookie in the headline!
  11050. (defun org-summary-todo (n-done n-not-done)
  11051. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11052. (let (org-log-done org-log-states) ; turn off logging
  11053. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11054. ")
  11055. (defvar org-todo-statistics-hook nil
  11056. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11057. This hook runs even if there is no statistics cookie present, in which case
  11058. `org-after-todo-statistics-hook' would not run.")
  11059. (defun org-todo-trigger-tag-changes (state)
  11060. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11061. (let ((l org-todo-state-tags-triggers)
  11062. changes)
  11063. (when (or (not state) (equal state ""))
  11064. (setq changes (append changes (cdr (assoc "" l)))))
  11065. (when (and (stringp state) (> (length state) 0))
  11066. (setq changes (append changes (cdr (assoc state l)))))
  11067. (when (member state org-not-done-keywords)
  11068. (setq changes (append changes (cdr (assoc 'todo l)))))
  11069. (when (member state org-done-keywords)
  11070. (setq changes (append changes (cdr (assoc 'done l)))))
  11071. (dolist (c changes)
  11072. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11073. (defun org-local-logging (value)
  11074. "Get logging settings from a property VALUE."
  11075. (let* (words w a)
  11076. ;; directly set the variables, they are already local.
  11077. (setq org-log-done nil
  11078. org-log-repeat nil
  11079. org-todo-log-states nil)
  11080. (setq words (org-split-string value))
  11081. (while (setq w (pop words))
  11082. (cond
  11083. ((setq a (assoc w org-startup-options))
  11084. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11085. (set (nth 1 a) (nth 2 a))))
  11086. ((setq a (org-extract-log-state-settings w))
  11087. (and (member (car a) org-todo-keywords-1)
  11088. (push a org-todo-log-states)))))))
  11089. (defun org-get-todo-sequence-head (kwd)
  11090. "Return the head of the TODO sequence to which KWD belongs.
  11091. If KWD is not set, check if there is a text property remembering the
  11092. right sequence."
  11093. (let (p)
  11094. (cond
  11095. ((not kwd)
  11096. (or (get-text-property (point-at-bol) 'org-todo-head)
  11097. (progn
  11098. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11099. nil (point-at-eol)))
  11100. (get-text-property p 'org-todo-head))))
  11101. ((not (member kwd org-todo-keywords-1))
  11102. (car org-todo-keywords-1))
  11103. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11104. (defun org-fast-todo-selection ()
  11105. "Fast TODO keyword selection with single keys.
  11106. Returns the new TODO keyword, or nil if no state change should occur."
  11107. (let* ((fulltable org-todo-key-alist)
  11108. (done-keywords org-done-keywords) ;; needed for the faces.
  11109. (maxlen (apply 'max (mapcar
  11110. (lambda (x)
  11111. (if (stringp (car x)) (string-width (car x)) 0))
  11112. fulltable)))
  11113. (expert nil)
  11114. (fwidth (+ maxlen 3 1 3))
  11115. (ncol (/ (- (window-width) 4) fwidth))
  11116. tg cnt e c tbl
  11117. groups ingroup)
  11118. (save-excursion
  11119. (save-window-excursion
  11120. (if expert
  11121. (set-buffer (get-buffer-create " *Org todo*"))
  11122. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11123. (erase-buffer)
  11124. (org-set-local 'org-done-keywords done-keywords)
  11125. (setq tbl fulltable cnt 0)
  11126. (while (setq e (pop tbl))
  11127. (cond
  11128. ((equal e '(:startgroup))
  11129. (push '() groups) (setq ingroup t)
  11130. (when (not (= cnt 0))
  11131. (setq cnt 0)
  11132. (insert "\n"))
  11133. (insert "{ "))
  11134. ((equal e '(:endgroup))
  11135. (setq ingroup nil cnt 0)
  11136. (insert "}\n"))
  11137. ((equal e '(:newline))
  11138. (when (not (= cnt 0))
  11139. (setq cnt 0)
  11140. (insert "\n")
  11141. (setq e (car tbl))
  11142. (while (equal (car tbl) '(:newline))
  11143. (insert "\n")
  11144. (setq tbl (cdr tbl)))))
  11145. (t
  11146. (setq tg (car e) c (cdr e))
  11147. (if ingroup (push tg (car groups)))
  11148. (setq tg (org-add-props tg nil 'face
  11149. (org-get-todo-face tg)))
  11150. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11151. (insert "[" c "] " tg (make-string
  11152. (- fwidth 4 (length tg)) ?\ ))
  11153. (when (= (setq cnt (1+ cnt)) ncol)
  11154. (insert "\n")
  11155. (if ingroup (insert " "))
  11156. (setq cnt 0)))))
  11157. (insert "\n")
  11158. (goto-char (point-min))
  11159. (if (not expert) (org-fit-window-to-buffer))
  11160. (message "[a-z..]:Set [SPC]:clear")
  11161. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11162. (cond
  11163. ((or (= c ?\C-g)
  11164. (and (= c ?q) (not (rassoc c fulltable))))
  11165. (setq quit-flag t))
  11166. ((= c ?\ ) nil)
  11167. ((setq e (rassoc c fulltable) tg (car e))
  11168. tg)
  11169. (t (setq quit-flag t)))))))
  11170. (defun org-entry-is-todo-p ()
  11171. (member (org-get-todo-state) org-not-done-keywords))
  11172. (defun org-entry-is-done-p ()
  11173. (member (org-get-todo-state) org-done-keywords))
  11174. (defun org-get-todo-state ()
  11175. (save-excursion
  11176. (org-back-to-heading t)
  11177. (and (looking-at org-todo-line-regexp)
  11178. (match-end 2)
  11179. (match-string 2))))
  11180. (defun org-at-date-range-p (&optional inactive-ok)
  11181. "Is the cursor inside a date range?"
  11182. (interactive)
  11183. (save-excursion
  11184. (catch 'exit
  11185. (let ((pos (point)))
  11186. (skip-chars-backward "^[<\r\n")
  11187. (skip-chars-backward "<[")
  11188. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11189. (>= (match-end 0) pos)
  11190. (throw 'exit t))
  11191. (skip-chars-backward "^<[\r\n")
  11192. (skip-chars-backward "<[")
  11193. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11194. (>= (match-end 0) pos)
  11195. (throw 'exit t)))
  11196. nil)))
  11197. (defun org-get-repeat (&optional tagline)
  11198. "Check if there is a deadline/schedule with repeater in this entry."
  11199. (save-match-data
  11200. (save-excursion
  11201. (org-back-to-heading t)
  11202. (and (re-search-forward (if tagline
  11203. (concat tagline "\\s-*" org-repeat-re)
  11204. org-repeat-re)
  11205. (org-entry-end-position) t)
  11206. (match-string-no-properties 1)))))
  11207. (defvar org-last-changed-timestamp)
  11208. (defvar org-last-inserted-timestamp)
  11209. (defvar org-log-post-message)
  11210. (defvar org-log-note-purpose)
  11211. (defvar org-log-note-how)
  11212. (defvar org-log-note-extra)
  11213. (defun org-auto-repeat-maybe (done-word)
  11214. "Check if the current headline contains a repeated deadline/schedule.
  11215. If yes, set TODO state back to what it was and change the base date
  11216. of repeating deadline/scheduled time stamps to new date.
  11217. This function is run automatically after each state change to a DONE state."
  11218. ;; last-state is dynamically scoped into this function
  11219. (let* ((repeat (org-get-repeat))
  11220. (aa (assoc org-last-state org-todo-kwd-alist))
  11221. (interpret (nth 1 aa))
  11222. (head (nth 2 aa))
  11223. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11224. (msg "Entry repeats: ")
  11225. (org-log-done nil)
  11226. (org-todo-log-states nil)
  11227. re type n what ts time to-state)
  11228. (when repeat
  11229. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11230. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11231. org-todo-repeat-to-state))
  11232. (unless (and to-state (member to-state org-todo-keywords-1))
  11233. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11234. (org-todo to-state)
  11235. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11236. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11237. (org-time-stamp-format t t))))
  11238. (when org-log-repeat
  11239. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11240. (memq 'org-add-log-note post-command-hook))
  11241. ;; OK, we are already setup for some record
  11242. (if (eq org-log-repeat 'note)
  11243. ;; make sure we take a note, not only a time stamp
  11244. (setq org-log-note-how 'note))
  11245. ;; Set up for taking a record
  11246. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11247. org-last-state
  11248. 'findpos org-log-repeat)))
  11249. (org-back-to-heading t)
  11250. (org-add-planning-info nil nil 'closed)
  11251. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11252. org-deadline-time-regexp "\\)\\|\\("
  11253. org-ts-regexp "\\)"))
  11254. (while (re-search-forward
  11255. re (save-excursion (outline-next-heading) (point)) t)
  11256. (setq type (if (match-end 1) org-scheduled-string
  11257. (if (match-end 3) org-deadline-string "Plain:"))
  11258. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11259. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11260. (setq n (string-to-number (match-string 2 ts))
  11261. what (match-string 3 ts))
  11262. (if (equal what "w") (setq n (* n 7) what "d"))
  11263. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11264. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11265. ;; Preparation, see if we need to modify the start date for the change
  11266. (when (match-end 1)
  11267. (setq time (save-match-data (org-time-string-to-time ts)))
  11268. (cond
  11269. ((equal (match-string 1 ts) ".")
  11270. ;; Shift starting date to today
  11271. (org-timestamp-change
  11272. (- (org-today) (time-to-days time))
  11273. 'day))
  11274. ((equal (match-string 1 ts) "+")
  11275. (let ((nshiftmax 10) (nshift 0))
  11276. (while (or (= nshift 0)
  11277. (<= (time-to-days time)
  11278. (time-to-days (current-time))))
  11279. (when (= (incf nshift) nshiftmax)
  11280. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11281. (error "Abort")))
  11282. (org-timestamp-change n (cdr (assoc what whata)))
  11283. (org-at-timestamp-p t)
  11284. (setq ts (match-string 1))
  11285. (setq time (save-match-data (org-time-string-to-time ts)))))
  11286. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11287. ;; rematch, so that we have everything in place for the real shift
  11288. (org-at-timestamp-p t)
  11289. (setq ts (match-string 1))
  11290. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11291. (org-timestamp-change n (cdr (assoc what whata)))
  11292. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  11293. (setq org-log-post-message msg)
  11294. (message "%s" msg))))
  11295. (defun org-show-todo-tree (arg)
  11296. "Make a compact tree which shows all headlines marked with TODO.
  11297. The tree will show the lines where the regexp matches, and all higher
  11298. headlines above the match.
  11299. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11300. With a numeric prefix N, construct a sparse tree for the Nth element
  11301. of `org-todo-keywords-1'."
  11302. (interactive "P")
  11303. (let ((case-fold-search nil)
  11304. (kwd-re
  11305. (cond ((null arg) org-not-done-regexp)
  11306. ((equal arg '(4))
  11307. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  11308. (mapcar 'list org-todo-keywords-1))))
  11309. (concat "\\("
  11310. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11311. "\\)\\>")))
  11312. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11313. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11314. org-todo-keywords-1)))
  11315. (t (error "Invalid prefix argument: %s" arg)))))
  11316. (message "%d TODO entries found"
  11317. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11318. (defun org-deadline (&optional arg time)
  11319. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11320. With one universal prefix argument, remove any deadline from the item.
  11321. With two universal prefix arguments, prompt for a warning delay.
  11322. With argument TIME, set the deadline at the corresponding date. TIME
  11323. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11324. (interactive "P")
  11325. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11326. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11327. 'region-start-level 'region))
  11328. org-loop-over-headlines-in-active-region)
  11329. (org-map-entries
  11330. `(org-deadline ',arg ,time)
  11331. org-loop-over-headlines-in-active-region
  11332. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11333. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11334. (repeater (and old-date
  11335. (string-match
  11336. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11337. old-date)
  11338. (match-string 1 old-date))))
  11339. (cond
  11340. ((equal arg '(4))
  11341. (when (and old-date org-log-redeadline)
  11342. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11343. org-log-redeadline))
  11344. (org-remove-timestamp-with-keyword org-deadline-string)
  11345. (message "Item no longer has a deadline."))
  11346. ((equal arg '(16))
  11347. (save-excursion
  11348. (if (re-search-forward
  11349. org-deadline-time-regexp
  11350. (save-excursion (outline-next-heading) (point)) t)
  11351. (let* ((rpl0 (match-string 1))
  11352. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11353. (replace-match
  11354. (concat org-deadline-string
  11355. " <" rpl
  11356. (format " -%dd" (abs
  11357. (- (time-to-days
  11358. (save-match-data
  11359. (org-read-date nil t nil "Warn starting from")))
  11360. (time-to-days nil))))
  11361. ">") t t))
  11362. (user-error "No deadline information to update"))))
  11363. (t
  11364. (org-add-planning-info 'deadline time 'closed)
  11365. (when (and old-date org-log-redeadline
  11366. (not (equal old-date
  11367. (substring org-last-inserted-timestamp 1 -1))))
  11368. (org-add-log-setup 'redeadline nil old-date 'findpos
  11369. org-log-redeadline))
  11370. (when repeater
  11371. (save-excursion
  11372. (org-back-to-heading t)
  11373. (when (re-search-forward (concat org-deadline-string " "
  11374. org-last-inserted-timestamp)
  11375. (save-excursion
  11376. (outline-next-heading) (point)) t)
  11377. (goto-char (1- (match-end 0)))
  11378. (insert " " repeater)
  11379. (setq org-last-inserted-timestamp
  11380. (concat (substring org-last-inserted-timestamp 0 -1)
  11381. " " repeater
  11382. (substring org-last-inserted-timestamp -1))))))
  11383. (message "Deadline on %s" org-last-inserted-timestamp))))))
  11384. (defun org-schedule (&optional arg time)
  11385. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11386. With one universal prefix argument, remove any scheduling date from the item.
  11387. With two universal prefix arguments, prompt for a delay cookie.
  11388. With argument TIME, scheduled at the corresponding date. TIME can
  11389. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11390. (interactive "P")
  11391. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11392. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11393. 'region-start-level 'region))
  11394. org-loop-over-headlines-in-active-region)
  11395. (org-map-entries
  11396. `(org-schedule ',arg ,time)
  11397. org-loop-over-headlines-in-active-region
  11398. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11399. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11400. (repeater (and old-date
  11401. (string-match
  11402. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11403. old-date)
  11404. (match-string 1 old-date))))
  11405. (cond
  11406. ((equal arg '(4))
  11407. (progn
  11408. (when (and old-date org-log-reschedule)
  11409. (org-add-log-setup 'delschedule nil old-date 'findpos
  11410. org-log-reschedule))
  11411. (org-remove-timestamp-with-keyword org-scheduled-string)
  11412. (message "Item is no longer scheduled.")))
  11413. ((equal arg '(16))
  11414. (save-excursion
  11415. (if (re-search-forward
  11416. org-scheduled-time-regexp
  11417. (save-excursion (outline-next-heading) (point)) t)
  11418. (let* ((rpl0 (match-string 1))
  11419. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  11420. (replace-match
  11421. (concat org-scheduled-string
  11422. " <" rpl
  11423. (format " -%dd" (abs
  11424. (- (time-to-days
  11425. (save-match-data
  11426. (org-read-date nil t nil "Delay until")))
  11427. (time-to-days nil))))
  11428. ">") t t))
  11429. (user-error "No scheduled information to update"))))
  11430. (t
  11431. (org-add-planning-info 'scheduled time 'closed)
  11432. (when (and old-date org-log-reschedule
  11433. (not (equal old-date
  11434. (substring org-last-inserted-timestamp 1 -1))))
  11435. (org-add-log-setup 'reschedule nil old-date 'findpos
  11436. org-log-reschedule))
  11437. (when repeater
  11438. (save-excursion
  11439. (org-back-to-heading t)
  11440. (when (re-search-forward (concat org-scheduled-string " "
  11441. org-last-inserted-timestamp)
  11442. (save-excursion
  11443. (outline-next-heading) (point)) t)
  11444. (goto-char (1- (match-end 0)))
  11445. (insert " " repeater)
  11446. (setq org-last-inserted-timestamp
  11447. (concat (substring org-last-inserted-timestamp 0 -1)
  11448. " " repeater
  11449. (substring org-last-inserted-timestamp -1))))))
  11450. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  11451. (defun org-get-scheduled-time (pom &optional inherit)
  11452. "Get the scheduled time as a time tuple, of a format suitable
  11453. for calling org-schedule with, or if there is no scheduling,
  11454. returns nil."
  11455. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11456. (when time
  11457. (apply 'encode-time (org-parse-time-string time)))))
  11458. (defun org-get-deadline-time (pom &optional inherit)
  11459. "Get the deadline as a time tuple, of a format suitable for
  11460. calling org-deadline with, or if there is no scheduling, returns
  11461. nil."
  11462. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11463. (when time
  11464. (apply 'encode-time (org-parse-time-string time)))))
  11465. (defun org-remove-timestamp-with-keyword (keyword)
  11466. "Remove all time stamps with KEYWORD in the current entry."
  11467. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11468. beg)
  11469. (save-excursion
  11470. (org-back-to-heading t)
  11471. (setq beg (point))
  11472. (outline-next-heading)
  11473. (while (re-search-backward re beg t)
  11474. (replace-match "")
  11475. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11476. (equal (char-before) ?\ ))
  11477. (backward-delete-char 1)
  11478. (if (string-match "^[ \t]*$" (buffer-substring
  11479. (point-at-bol) (point-at-eol)))
  11480. (delete-region (point-at-bol)
  11481. (min (point-max) (1+ (point-at-eol))))))))))
  11482. (defun org-add-planning-info (what &optional time &rest remove)
  11483. "Insert new timestamp with keyword in the line directly after the headline.
  11484. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11485. If non is given, the user is prompted for a date.
  11486. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11487. be removed."
  11488. (interactive)
  11489. (let (org-time-was-given org-end-time-was-given ts
  11490. end default-time default-input)
  11491. (catch 'exit
  11492. (when (and (memq what '(scheduled deadline))
  11493. (or (not time)
  11494. (and (stringp time)
  11495. (string-match "^[-+]+[0-9]" time))))
  11496. ;; Try to get a default date/time from existing timestamp
  11497. (save-excursion
  11498. (org-back-to-heading t)
  11499. (setq end (save-excursion (outline-next-heading) (point)))
  11500. (when (re-search-forward (if (eq what 'scheduled)
  11501. org-scheduled-time-regexp
  11502. org-deadline-time-regexp)
  11503. end t)
  11504. (setq ts (match-string 1)
  11505. default-time
  11506. (apply 'encode-time (org-parse-time-string ts))
  11507. default-input (and ts (org-get-compact-tod ts))))))
  11508. (when what
  11509. (setq time
  11510. (if (stringp time)
  11511. ;; This is a string (relative or absolute), set proper date
  11512. (apply 'encode-time
  11513. (org-read-date-analyze
  11514. time default-time (decode-time default-time)))
  11515. ;; If necessary, get the time from the user
  11516. (or time (org-read-date nil 'to-time nil nil
  11517. default-time default-input)))))
  11518. (when (and org-insert-labeled-timestamps-at-point
  11519. (member what '(scheduled deadline)))
  11520. (insert
  11521. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11522. (org-insert-time-stamp time org-time-was-given
  11523. nil nil nil (list org-end-time-was-given))
  11524. (setq what nil))
  11525. (save-excursion
  11526. (save-restriction
  11527. (let (col list elt ts buffer-invisibility-spec)
  11528. (org-back-to-heading t)
  11529. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11530. (goto-char (match-end 1))
  11531. (setq col (current-column))
  11532. (goto-char (match-end 0))
  11533. (if (eobp) (insert "\n") (forward-char 1))
  11534. (when (and (not what)
  11535. (not (looking-at
  11536. (concat "[ \t]*"
  11537. org-keyword-time-not-clock-regexp))))
  11538. ;; Nothing to add, nothing to remove...... :-)
  11539. (throw 'exit nil))
  11540. (if (and (not (looking-at org-outline-regexp))
  11541. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11542. "[^\r\n]*"))
  11543. (not (equal (match-string 1) org-clock-string)))
  11544. (narrow-to-region (match-beginning 0) (match-end 0))
  11545. (insert-before-markers "\n")
  11546. (backward-char 1)
  11547. (narrow-to-region (point) (point))
  11548. (and org-adapt-indentation (org-indent-to-column col)))
  11549. ;; Check if we have to remove something.
  11550. (setq list (cons what remove))
  11551. (while list
  11552. (setq elt (pop list))
  11553. (when (or (and (eq elt 'scheduled)
  11554. (re-search-forward org-scheduled-time-regexp nil t))
  11555. (and (eq elt 'deadline)
  11556. (re-search-forward org-deadline-time-regexp nil t))
  11557. (and (eq elt 'closed)
  11558. (re-search-forward org-closed-time-regexp nil t)))
  11559. (replace-match "")
  11560. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11561. (and (looking-at "[ \t]+") (replace-match ""))
  11562. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11563. (when what
  11564. (insert
  11565. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11566. (cond ((eq what 'scheduled) org-scheduled-string)
  11567. ((eq what 'deadline) org-deadline-string)
  11568. ((eq what 'closed) org-closed-string))
  11569. " ")
  11570. (setq ts (org-insert-time-stamp
  11571. time
  11572. (or org-time-was-given
  11573. (and (eq what 'closed) org-log-done-with-time))
  11574. (eq what 'closed)
  11575. nil nil (list org-end-time-was-given)))
  11576. (insert
  11577. (if (not (or (bolp) (eq (char-before) ?\ )
  11578. (memq (char-after) '(32 10))
  11579. (eobp))) " " ""))
  11580. (end-of-line 1))
  11581. (goto-char (point-min))
  11582. (widen)
  11583. (if (and (looking-at "[ \t]*\n")
  11584. (equal (char-before) ?\n))
  11585. (delete-region (1- (point)) (point-at-eol)))
  11586. ts))))))
  11587. (defvar org-log-note-marker (make-marker))
  11588. (defvar org-log-note-purpose nil)
  11589. (defvar org-log-note-state nil)
  11590. (defvar org-log-note-previous-state nil)
  11591. (defvar org-log-note-how nil)
  11592. (defvar org-log-note-extra nil)
  11593. (defvar org-log-note-window-configuration nil)
  11594. (defvar org-log-note-return-to (make-marker))
  11595. (defvar org-log-note-effective-time nil
  11596. "Remembered current time so that dynamically scoped
  11597. `org-extend-today-until' affects tha timestamps in state change
  11598. log")
  11599. (defvar org-log-post-message nil
  11600. "Message to be displayed after a log note has been stored.
  11601. The auto-repeater uses this.")
  11602. (defun org-add-note ()
  11603. "Add a note to the current entry.
  11604. This is done in the same way as adding a state change note."
  11605. (interactive)
  11606. (org-add-log-setup 'note nil nil 'findpos nil))
  11607. (defvar org-property-end-re)
  11608. (defun org-add-log-setup (&optional purpose state prev-state
  11609. findpos how extra)
  11610. "Set up the post command hook to take a note.
  11611. If this is about to TODO state change, the new state is expected in STATE.
  11612. When FINDPOS is non-nil, find the correct position for the note in
  11613. the current entry. If not, assume that it can be inserted at point.
  11614. HOW is an indicator what kind of note should be created.
  11615. EXTRA is additional text that will be inserted into the notes buffer."
  11616. (let* ((org-log-into-drawer (org-log-into-drawer))
  11617. (drawer (cond ((stringp org-log-into-drawer)
  11618. org-log-into-drawer)
  11619. (org-log-into-drawer "LOGBOOK"))))
  11620. (save-restriction
  11621. (save-excursion
  11622. (when findpos
  11623. (org-back-to-heading t)
  11624. (narrow-to-region (point) (save-excursion
  11625. (outline-next-heading) (point)))
  11626. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11627. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11628. "[^\r\n]*\\)?"))
  11629. (goto-char (match-end 0))
  11630. (cond
  11631. (drawer
  11632. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11633. nil t)
  11634. (progn
  11635. (goto-char (match-end 0))
  11636. (or org-log-states-order-reversed
  11637. (and (re-search-forward org-property-end-re nil t)
  11638. (goto-char (1- (match-beginning 0))))))
  11639. (insert "\n:" drawer ":\n:END:")
  11640. (beginning-of-line 0)
  11641. (org-indent-line)
  11642. (beginning-of-line 2)
  11643. (org-indent-line)
  11644. (end-of-line 0)))
  11645. ((and org-log-state-notes-insert-after-drawers
  11646. (save-excursion
  11647. (forward-line) (looking-at org-drawer-regexp)))
  11648. (forward-line)
  11649. (while (looking-at org-drawer-regexp)
  11650. (goto-char (match-end 0))
  11651. (re-search-forward org-property-end-re (point-max) t)
  11652. (forward-line))
  11653. (forward-line -1)))
  11654. (unless org-log-states-order-reversed
  11655. (and (= (char-after) ?\n) (forward-char 1))
  11656. (org-skip-over-state-notes)
  11657. (skip-chars-backward " \t\n\r")))
  11658. (move-marker org-log-note-marker (point))
  11659. (setq org-log-note-purpose purpose
  11660. org-log-note-state state
  11661. org-log-note-previous-state prev-state
  11662. org-log-note-how how
  11663. org-log-note-extra extra
  11664. org-log-note-effective-time (org-current-effective-time))
  11665. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11666. (defun org-skip-over-state-notes ()
  11667. "Skip past the list of State notes in an entry."
  11668. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11669. (when (ignore-errors (goto-char (org-in-item-p)))
  11670. (let* ((struct (org-list-struct))
  11671. (prevs (org-list-prevs-alist struct)))
  11672. (while (looking-at "[ \t]*- State")
  11673. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11674. (org-list-get-item-end (point) struct)))))))
  11675. (defun org-add-log-note (&optional purpose)
  11676. "Pop up a window for taking a note, and add this note later at point."
  11677. (remove-hook 'post-command-hook 'org-add-log-note)
  11678. (setq org-log-note-window-configuration (current-window-configuration))
  11679. (delete-other-windows)
  11680. (move-marker org-log-note-return-to (point))
  11681. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11682. (goto-char org-log-note-marker)
  11683. (org-switch-to-buffer-other-window "*Org Note*")
  11684. (erase-buffer)
  11685. (if (memq org-log-note-how '(time state))
  11686. (let (current-prefix-arg) (org-store-log-note))
  11687. (let ((org-inhibit-startup t)) (org-mode))
  11688. (insert (format "# Insert note for %s.
  11689. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11690. (cond
  11691. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11692. ((eq org-log-note-purpose 'done) "closed todo item")
  11693. ((eq org-log-note-purpose 'state)
  11694. (format "state change from \"%s\" to \"%s\""
  11695. (or org-log-note-previous-state "")
  11696. (or org-log-note-state "")))
  11697. ((eq org-log-note-purpose 'reschedule)
  11698. "rescheduling")
  11699. ((eq org-log-note-purpose 'delschedule)
  11700. "no longer scheduled")
  11701. ((eq org-log-note-purpose 'redeadline)
  11702. "changing deadline")
  11703. ((eq org-log-note-purpose 'deldeadline)
  11704. "removing deadline")
  11705. ((eq org-log-note-purpose 'refile)
  11706. "refiling")
  11707. ((eq org-log-note-purpose 'note)
  11708. "this entry")
  11709. (t (error "This should not happen")))))
  11710. (if org-log-note-extra (insert org-log-note-extra))
  11711. (org-set-local 'org-finish-function 'org-store-log-note)
  11712. (run-hooks 'org-log-buffer-setup-hook)))
  11713. (defvar org-note-abort nil) ; dynamically scoped
  11714. (defun org-store-log-note ()
  11715. "Finish taking a log note, and insert it to where it belongs."
  11716. (let ((txt (buffer-string)))
  11717. (kill-buffer (current-buffer))
  11718. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11719. lines ind bul)
  11720. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  11721. (setq txt (replace-match "" t t txt)))
  11722. (if (string-match "\\s-+\\'" txt)
  11723. (setq txt (replace-match "" t t txt)))
  11724. (setq lines (org-split-string txt "\n"))
  11725. (when (and note (string-match "\\S-" note))
  11726. (setq note
  11727. (org-replace-escapes
  11728. note
  11729. (list (cons "%u" (user-login-name))
  11730. (cons "%U" user-full-name)
  11731. (cons "%t" (format-time-string
  11732. (org-time-stamp-format 'long 'inactive)
  11733. org-log-note-effective-time))
  11734. (cons "%T" (format-time-string
  11735. (org-time-stamp-format 'long nil)
  11736. org-log-note-effective-time))
  11737. (cons "%d" (format-time-string
  11738. (org-time-stamp-format nil 'inactive)
  11739. org-log-note-effective-time))
  11740. (cons "%D" (format-time-string
  11741. (org-time-stamp-format nil nil)
  11742. org-log-note-effective-time))
  11743. (cons "%s" (if org-log-note-state
  11744. (concat "\"" org-log-note-state "\"")
  11745. ""))
  11746. (cons "%S" (if org-log-note-previous-state
  11747. (concat "\"" org-log-note-previous-state "\"")
  11748. "\"\"")))))
  11749. (if lines (setq note (concat note " \\\\")))
  11750. (push note lines))
  11751. (when (or current-prefix-arg org-note-abort)
  11752. (when org-log-into-drawer
  11753. (org-remove-empty-drawer-at
  11754. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11755. org-log-note-marker))
  11756. (setq lines nil))
  11757. (when lines
  11758. (with-current-buffer (marker-buffer org-log-note-marker)
  11759. (save-excursion
  11760. (goto-char org-log-note-marker)
  11761. (move-marker org-log-note-marker nil)
  11762. (end-of-line 1)
  11763. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11764. (setq ind (save-excursion
  11765. (if (ignore-errors (goto-char (org-in-item-p)))
  11766. (let ((struct (org-list-struct)))
  11767. (org-list-get-ind
  11768. (org-list-get-top-point struct) struct))
  11769. (skip-chars-backward " \r\t\n")
  11770. (cond
  11771. ((and (org-at-heading-p)
  11772. org-adapt-indentation)
  11773. (1+ (org-current-level)))
  11774. ((org-at-heading-p) 0)
  11775. (t (org-get-indentation))))))
  11776. (setq bul (org-list-bullet-string "-"))
  11777. (org-indent-line-to ind)
  11778. (insert bul (pop lines))
  11779. (let ((ind-body (+ (length bul) ind)))
  11780. (while lines
  11781. (insert "\n")
  11782. (org-indent-line-to ind-body)
  11783. (insert (pop lines))))
  11784. (message "Note stored")
  11785. (org-back-to-heading t)
  11786. (org-cycle-hide-drawers 'children))))))
  11787. (set-window-configuration org-log-note-window-configuration)
  11788. (with-current-buffer (marker-buffer org-log-note-return-to)
  11789. (goto-char org-log-note-return-to))
  11790. (move-marker org-log-note-return-to nil)
  11791. (and org-log-post-message (message "%s" org-log-post-message)))
  11792. (defun org-remove-empty-drawer-at (drawer pos)
  11793. "Remove an empty drawer DRAWER at position POS.
  11794. POS may also be a marker."
  11795. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11796. (save-excursion
  11797. (save-restriction
  11798. (widen)
  11799. (goto-char pos)
  11800. (if (org-in-regexp
  11801. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11802. (replace-match ""))))))
  11803. (defvar org-ts-type nil)
  11804. (defun org-sparse-tree (&optional arg type)
  11805. "Create a sparse tree, prompt for the details.
  11806. This command can create sparse trees. You first need to select the type
  11807. of match used to create the tree:
  11808. t Show all TODO entries.
  11809. T Show entries with a specific TODO keyword.
  11810. m Show entries selected by a tags/property match.
  11811. p Enter a property name and its value (both with completion on existing
  11812. names/values) and show entries with that property.
  11813. r Show entries matching a regular expression (`/' can be used as well).
  11814. b Show deadlines and scheduled items before a date.
  11815. a Show deadlines and scheduled items after a date.
  11816. d Show deadlines due within `org-deadline-warning-days'.
  11817. D Show deadlines and scheduled items between a date range."
  11818. (interactive "P")
  11819. (let (ans kwd value ts-type)
  11820. (setq type (or type org-sparse-tree-default-date-type))
  11821. (setq org-ts-type type)
  11822. (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"
  11823. (cond ((eq type 'all) "all timestamps")
  11824. ((eq type 'scheduled) "only scheduled")
  11825. ((eq type 'deadline) "only deadline")
  11826. ((eq type 'active) "only active timestamps")
  11827. ((eq type 'inactive) "only inactive timestamps")
  11828. ((eq type 'scheduled-or-deadline) "scheduled/deadline")
  11829. (t "scheduled/deadline")))
  11830. (setq ans (read-char-exclusive))
  11831. (cond
  11832. ((equal ans ?c)
  11833. (org-sparse-tree arg (cadr (member type '(scheduled-or-deadline all scheduled deadline active inactive)))))
  11834. ((equal ans ?d)
  11835. (call-interactively 'org-check-deadlines))
  11836. ((equal ans ?b)
  11837. (call-interactively 'org-check-before-date))
  11838. ((equal ans ?a)
  11839. (call-interactively 'org-check-after-date))
  11840. ((equal ans ?D)
  11841. (call-interactively 'org-check-dates-range))
  11842. ((equal ans ?t)
  11843. (call-interactively 'org-show-todo-tree))
  11844. ((equal ans ?T)
  11845. (org-show-todo-tree '(4)))
  11846. ((member ans '(?T ?m))
  11847. (call-interactively 'org-match-sparse-tree))
  11848. ((member ans '(?p ?P))
  11849. (setq kwd (org-icompleting-read "Property: "
  11850. (mapcar 'list (org-buffer-property-keys))))
  11851. (setq value (org-icompleting-read "Value: "
  11852. (mapcar 'list (org-property-values kwd))))
  11853. (unless (string-match "\\`{.*}\\'" value)
  11854. (setq value (concat "\"" value "\"")))
  11855. (org-match-sparse-tree arg (concat kwd "=" value)))
  11856. ((member ans '(?r ?R ?/))
  11857. (call-interactively 'org-occur))
  11858. (t (error "No such sparse tree command \"%c\"" ans)))))
  11859. (defvar org-occur-highlights nil
  11860. "List of overlays used for occur matches.")
  11861. (make-variable-buffer-local 'org-occur-highlights)
  11862. (defvar org-occur-parameters nil
  11863. "Parameters of the active org-occur calls.
  11864. This is a list, each call to org-occur pushes as cons cell,
  11865. containing the regular expression and the callback, onto the list.
  11866. The list can contain several entries if `org-occur' has been called
  11867. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11868. will only contain one set of parameters. When the highlights are
  11869. removed (for example with `C-c C-c', or with the next edit (depending
  11870. on `org-remove-highlights-with-change'), this variable is emptied
  11871. as well.")
  11872. (make-variable-buffer-local 'org-occur-parameters)
  11873. (defun org-occur (regexp &optional keep-previous callback)
  11874. "Make a compact tree which shows all matches of REGEXP.
  11875. The tree will show the lines where the regexp matches, and all higher
  11876. headlines above the match. It will also show the heading after the match,
  11877. to make sure editing the matching entry is easy.
  11878. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11879. call to `org-occur' will be kept, to allow stacking of calls to this
  11880. command.
  11881. If CALLBACK is non-nil, it is a function which is called to confirm
  11882. that the match should indeed be shown."
  11883. (interactive "sRegexp: \nP")
  11884. (when (equal regexp "")
  11885. (error "Regexp cannot be empty"))
  11886. (unless keep-previous
  11887. (org-remove-occur-highlights nil nil t))
  11888. (push (cons regexp callback) org-occur-parameters)
  11889. (let ((cnt 0))
  11890. (save-excursion
  11891. (goto-char (point-min))
  11892. (if (or (not keep-previous) ; do not want to keep
  11893. (not org-occur-highlights)) ; no previous matches
  11894. ;; hide everything
  11895. (org-overview))
  11896. (while (re-search-forward regexp nil t)
  11897. (when (or (not callback)
  11898. (save-match-data (funcall callback)))
  11899. (setq cnt (1+ cnt))
  11900. (when org-highlight-sparse-tree-matches
  11901. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11902. (org-show-context 'occur-tree))))
  11903. (when org-remove-highlights-with-change
  11904. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11905. nil 'local))
  11906. (unless org-sparse-tree-open-archived-trees
  11907. (org-hide-archived-subtrees (point-min) (point-max)))
  11908. (run-hooks 'org-occur-hook)
  11909. (if (org-called-interactively-p 'interactive)
  11910. (message "%d match(es) for regexp %s" cnt regexp))
  11911. cnt))
  11912. (defun org-occur-next-match (&optional n reset)
  11913. "Function for `next-error-function' to find sparse tree matches.
  11914. N is the number of matches to move, when negative move backwards.
  11915. RESET is entirely ignored - this function always goes back to the
  11916. starting point when no match is found."
  11917. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11918. (search-func (if (< n 0)
  11919. 'previous-single-char-property-change
  11920. 'next-single-char-property-change))
  11921. (n (abs n))
  11922. (pos (point))
  11923. p1)
  11924. (catch 'exit
  11925. (while (setq p1 (funcall search-func (point) 'org-type))
  11926. (when (equal p1 limit)
  11927. (goto-char pos)
  11928. (error "No more matches"))
  11929. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11930. (setq n (1- n))
  11931. (when (= n 0)
  11932. (goto-char p1)
  11933. (throw 'exit (point))))
  11934. (goto-char p1))
  11935. (goto-char p1)
  11936. (error "No more matches"))))
  11937. (defun org-show-context (&optional key)
  11938. "Make sure point and context are visible.
  11939. How much context is shown depends upon the variables
  11940. `org-show-hierarchy-above', `org-show-following-heading',
  11941. `org-show-entry-below' and `org-show-siblings'."
  11942. (let ((heading-p (org-at-heading-p t))
  11943. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11944. (following-p (org-get-alist-option org-show-following-heading key))
  11945. (entry-p (org-get-alist-option org-show-entry-below key))
  11946. (siblings-p (org-get-alist-option org-show-siblings key)))
  11947. (catch 'exit
  11948. ;; Show heading or entry text
  11949. (if (and heading-p (not entry-p))
  11950. (org-flag-heading nil) ; only show the heading
  11951. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11952. (org-show-hidden-entry))) ; show entire entry
  11953. (when following-p
  11954. ;; Show next sibling, or heading below text
  11955. (save-excursion
  11956. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11957. (org-flag-heading nil))))
  11958. (when siblings-p (org-show-siblings))
  11959. (when hierarchy-p
  11960. ;; show all higher headings, possibly with siblings
  11961. (save-excursion
  11962. (while (and (condition-case nil
  11963. (progn (org-up-heading-all 1) t)
  11964. (error nil))
  11965. (not (bobp)))
  11966. (org-flag-heading nil)
  11967. (when siblings-p (org-show-siblings))))))))
  11968. (defvar org-reveal-start-hook nil
  11969. "Hook run before revealing a location.")
  11970. (defun org-reveal (&optional siblings)
  11971. "Show current entry, hierarchy above it, and the following headline.
  11972. This can be used to show a consistent set of context around locations
  11973. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11974. not t for the search context.
  11975. With optional argument SIBLINGS, on each level of the hierarchy all
  11976. siblings are shown. This repairs the tree structure to what it would
  11977. look like when opened with hierarchical calls to `org-cycle'.
  11978. With double optional argument \\[universal-argument] \\[universal-argument], \
  11979. go to the parent and show the
  11980. entire tree."
  11981. (interactive "P")
  11982. (run-hooks 'org-reveal-start-hook)
  11983. (let ((org-show-hierarchy-above t)
  11984. (org-show-following-heading t)
  11985. (org-show-siblings (if siblings t org-show-siblings)))
  11986. (org-show-context nil))
  11987. (when (equal siblings '(16))
  11988. (save-excursion
  11989. (when (org-up-heading-safe)
  11990. (org-show-subtree)
  11991. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11992. (defun org-highlight-new-match (beg end)
  11993. "Highlight from BEG to END and mark the highlight is an occur headline."
  11994. (let ((ov (make-overlay beg end)))
  11995. (overlay-put ov 'face 'secondary-selection)
  11996. (overlay-put ov 'org-type 'org-occur)
  11997. (push ov org-occur-highlights)))
  11998. (defun org-remove-occur-highlights (&optional beg end noremove)
  11999. "Remove the occur highlights from the buffer.
  12000. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12001. from the `before-change-functions' in the current buffer."
  12002. (interactive)
  12003. (unless org-inhibit-highlight-removal
  12004. (mapc 'delete-overlay org-occur-highlights)
  12005. (setq org-occur-highlights nil)
  12006. (setq org-occur-parameters nil)
  12007. (unless noremove
  12008. (remove-hook 'before-change-functions
  12009. 'org-remove-occur-highlights 'local))))
  12010. ;;;; Priorities
  12011. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12012. "Regular expression matching the priority indicator.")
  12013. (defvar org-remove-priority-next-time nil)
  12014. (defun org-priority-up ()
  12015. "Increase the priority of the current item."
  12016. (interactive)
  12017. (org-priority 'up))
  12018. (defun org-priority-down ()
  12019. "Decrease the priority of the current item."
  12020. (interactive)
  12021. (org-priority 'down))
  12022. (defun org-priority (&optional action show)
  12023. "Change the priority of an item.
  12024. ACTION can be `set', `up', `down', or a character."
  12025. (interactive "P")
  12026. (if (equal action '(4))
  12027. (org-show-priority)
  12028. (unless org-enable-priority-commands
  12029. (error "Priority commands are disabled"))
  12030. (setq action (or action 'set))
  12031. (let (current new news have remove)
  12032. (save-excursion
  12033. (org-back-to-heading t)
  12034. (if (looking-at org-priority-regexp)
  12035. (setq current (string-to-char (match-string 2))
  12036. have t))
  12037. (cond
  12038. ((eq action 'remove)
  12039. (setq remove t new ?\ ))
  12040. ((or (eq action 'set)
  12041. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12042. (if (not (eq action 'set))
  12043. (setq new action)
  12044. (message "Priority %c-%c, SPC to remove: "
  12045. org-highest-priority org-lowest-priority)
  12046. (save-match-data
  12047. (setq new (read-char-exclusive))))
  12048. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12049. (= (upcase org-lowest-priority) org-lowest-priority))
  12050. (setq new (upcase new)))
  12051. (cond ((equal new ?\ ) (setq remove t))
  12052. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12053. (error "Priority must be between `%c' and `%c'"
  12054. org-highest-priority org-lowest-priority))))
  12055. ((eq action 'up)
  12056. (setq new (if have
  12057. (1- current) ; normal cycling
  12058. ;; last priority was empty
  12059. (if (eq last-command this-command)
  12060. org-lowest-priority ; wrap around empty to lowest
  12061. ;; default
  12062. (if org-priority-start-cycle-with-default
  12063. org-default-priority
  12064. (1- org-default-priority))))))
  12065. ((eq action 'down)
  12066. (setq new (if have
  12067. (1+ current) ; normal cycling
  12068. ;; last priority was empty
  12069. (if (eq last-command this-command)
  12070. org-highest-priority ; wrap around empty to highest
  12071. ;; default
  12072. (if org-priority-start-cycle-with-default
  12073. org-default-priority
  12074. (1+ org-default-priority))))))
  12075. (t (error "Invalid action")))
  12076. (if (or (< (upcase new) org-highest-priority)
  12077. (> (upcase new) org-lowest-priority))
  12078. (if (and (memq action '(up down))
  12079. (not have) (not (eq last-command this-command)))
  12080. ;; `new' is from default priority
  12081. (error
  12082. "The default can not be set, see `org-default-priority' why")
  12083. ;; normal cycling: `new' is beyond highest/lowest priority
  12084. ;; and is wrapped around to the empty priority
  12085. (setq remove t)))
  12086. (setq news (format "%c" new))
  12087. (if have
  12088. (if remove
  12089. (replace-match "" t t nil 1)
  12090. (replace-match news t t nil 2))
  12091. (if remove
  12092. (error "No priority cookie found in line")
  12093. (let ((case-fold-search nil))
  12094. (looking-at org-todo-line-regexp))
  12095. (if (match-end 2)
  12096. (progn
  12097. (goto-char (match-end 2))
  12098. (insert " [#" news "]"))
  12099. (goto-char (match-beginning 3))
  12100. (insert "[#" news "] "))))
  12101. (org-preserve-lc (org-set-tags nil 'align)))
  12102. (if remove
  12103. (message "Priority removed")
  12104. (message "Priority of current item set to %s" news)))))
  12105. (defun org-show-priority ()
  12106. "Show the priority of the current item.
  12107. This priority is composed of the main priority given with the [#A] cookies,
  12108. and by additional input from the age of a schedules or deadline entry."
  12109. (interactive)
  12110. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12111. (org-get-at-bol 'priority)
  12112. (save-excursion
  12113. (save-match-data
  12114. (beginning-of-line)
  12115. (and (looking-at org-heading-regexp)
  12116. (org-get-priority (match-string 0))))))))
  12117. (message "Priority is %d" (if pri pri -1000))))
  12118. (defun org-get-priority (s)
  12119. "Find priority cookie and return priority."
  12120. (save-match-data
  12121. (if (functionp org-get-priority-function)
  12122. (funcall org-get-priority-function)
  12123. (if (not (string-match org-priority-regexp s))
  12124. (* 1000 (- org-lowest-priority org-default-priority))
  12125. (* 1000 (- org-lowest-priority
  12126. (string-to-char (match-string 2 s))))))))
  12127. ;;;; Tags
  12128. (defvar org-agenda-archives-mode)
  12129. (defvar org-map-continue-from nil
  12130. "Position from where mapping should continue.
  12131. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12132. (defvar org-scanner-tags nil
  12133. "The current tag list while the tags scanner is running.")
  12134. (defvar org-trust-scanner-tags nil
  12135. "Should `org-get-tags-at' use the tags for the scanner.
  12136. This is for internal dynamical scoping only.
  12137. When this is non-nil, the function `org-get-tags-at' will return the value
  12138. of `org-scanner-tags' instead of building the list by itself. This
  12139. can lead to large speed-ups when the tags scanner is used in a file with
  12140. many entries, and when the list of tags is retrieved, for example to
  12141. obtain a list of properties. Building the tags list for each entry in such
  12142. a file becomes an N^2 operation - but with this variable set, it scales
  12143. as N.")
  12144. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12145. "Scan headline tags with inheritance and produce output ACTION.
  12146. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12147. or `agenda' to produce an entry list for an agenda view. It can also be
  12148. a Lisp form or a function that should be called at each matched headline, in
  12149. this case the return value is a list of all return values from these calls.
  12150. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12151. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12152. only lines with a not-done TODO keyword are included in the output.
  12153. This should be the same variable that was scoped into
  12154. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12155. START-LEVEL can be a string with asterisks, reducing the scope to
  12156. headlines matching this string."
  12157. (require 'org-agenda)
  12158. (let* ((re (concat "^"
  12159. (if start-level
  12160. ;; Get the correct level to match
  12161. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12162. org-outline-regexp)
  12163. " *\\(\\<\\("
  12164. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12165. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12166. (props (list 'face 'default
  12167. 'done-face 'org-agenda-done
  12168. 'undone-face 'default
  12169. 'mouse-face 'highlight
  12170. 'org-not-done-regexp org-not-done-regexp
  12171. 'org-todo-regexp org-todo-regexp
  12172. 'org-complex-heading-regexp org-complex-heading-regexp
  12173. 'help-echo
  12174. (format "mouse-2 or RET jump to org file %s"
  12175. (abbreviate-file-name
  12176. (or (buffer-file-name (buffer-base-buffer))
  12177. (buffer-name (buffer-base-buffer)))))))
  12178. (case-fold-search nil)
  12179. (org-map-continue-from nil)
  12180. lspos tags tags-list
  12181. (tags-alist (list (cons 0 org-file-tags)))
  12182. (llast 0) rtn rtn1 level category i txt
  12183. todo marker entry priority)
  12184. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12185. (setq action (list 'lambda nil action)))
  12186. (save-excursion
  12187. (goto-char (point-min))
  12188. (when (eq action 'sparse-tree)
  12189. (org-overview)
  12190. (org-remove-occur-highlights))
  12191. (while (re-search-forward re nil t)
  12192. (setq org-map-continue-from nil)
  12193. (catch :skip
  12194. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12195. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12196. (goto-char (setq lspos (match-beginning 0)))
  12197. (setq level (org-reduced-level (org-outline-level))
  12198. category (org-get-category))
  12199. (setq i llast llast level)
  12200. ;; remove tag lists from same and sublevels
  12201. (while (>= i level)
  12202. (when (setq entry (assoc i tags-alist))
  12203. (setq tags-alist (delete entry tags-alist)))
  12204. (setq i (1- i)))
  12205. ;; add the next tags
  12206. (when tags
  12207. (setq tags (org-split-string tags ":")
  12208. tags-alist
  12209. (cons (cons level tags) tags-alist)))
  12210. ;; compile tags for current headline
  12211. (setq tags-list
  12212. (if org-use-tag-inheritance
  12213. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12214. tags)
  12215. org-scanner-tags tags-list)
  12216. (when org-use-tag-inheritance
  12217. (setcdr (car tags-alist)
  12218. (mapcar (lambda (x)
  12219. (setq x (copy-sequence x))
  12220. (org-add-prop-inherited x))
  12221. (cdar tags-alist))))
  12222. (when (and tags org-use-tag-inheritance
  12223. (or (not (eq t org-use-tag-inheritance))
  12224. org-tags-exclude-from-inheritance))
  12225. ;; selective inheritance, remove uninherited ones
  12226. (setcdr (car tags-alist)
  12227. (org-remove-uninherited-tags (cdar tags-alist))))
  12228. (when (and
  12229. ;; eval matcher only when the todo condition is OK
  12230. (and (or (not todo-only) (member todo org-not-done-keywords))
  12231. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12232. (eval matcher)))
  12233. ;; Call the skipper, but return t if it does not skip,
  12234. ;; so that the `and' form continues evaluating
  12235. (progn
  12236. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12237. t)
  12238. ;; Check if timestamps are deselecting this entry
  12239. (or (not todo-only)
  12240. (and (member todo org-not-done-keywords)
  12241. (or (not org-agenda-tags-todo-honor-ignore-options)
  12242. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12243. ;; select this headline
  12244. (cond
  12245. ((eq action 'sparse-tree)
  12246. (and org-highlight-sparse-tree-matches
  12247. (org-get-heading) (match-end 0)
  12248. (org-highlight-new-match
  12249. (match-beginning 1) (match-end 1)))
  12250. (org-show-context 'tags-tree))
  12251. ((eq action 'agenda)
  12252. (setq txt (org-agenda-format-item
  12253. ""
  12254. (concat
  12255. (if (eq org-tags-match-list-sublevels 'indented)
  12256. (make-string (1- level) ?.) "")
  12257. (org-get-heading))
  12258. level category
  12259. tags-list)
  12260. priority (org-get-priority txt))
  12261. (goto-char lspos)
  12262. (setq marker (org-agenda-new-marker))
  12263. (org-add-props txt props
  12264. 'org-marker marker 'org-hd-marker marker 'org-category category
  12265. 'todo-state todo
  12266. 'priority priority 'type "tagsmatch")
  12267. (push txt rtn))
  12268. ((functionp action)
  12269. (setq org-map-continue-from nil)
  12270. (save-excursion
  12271. (setq rtn1 (funcall action))
  12272. (push rtn1 rtn)))
  12273. (t (error "Invalid action")))
  12274. ;; if we are to skip sublevels, jump to end of subtree
  12275. (unless org-tags-match-list-sublevels
  12276. (org-end-of-subtree t)
  12277. (backward-char 1))))
  12278. ;; Get the correct position from where to continue
  12279. (if org-map-continue-from
  12280. (goto-char org-map-continue-from)
  12281. (and (= (point) lspos) (end-of-line 1)))))
  12282. (when (and (eq action 'sparse-tree)
  12283. (not org-sparse-tree-open-archived-trees))
  12284. (org-hide-archived-subtrees (point-min) (point-max)))
  12285. (nreverse rtn)))
  12286. (defun org-remove-uninherited-tags (tags)
  12287. "Remove all tags that are not inherited from the list TAGS."
  12288. (cond
  12289. ((eq org-use-tag-inheritance t)
  12290. (if org-tags-exclude-from-inheritance
  12291. (org-delete-all org-tags-exclude-from-inheritance tags)
  12292. tags))
  12293. ((not org-use-tag-inheritance) nil)
  12294. ((stringp org-use-tag-inheritance)
  12295. (delq nil (mapcar
  12296. (lambda (x)
  12297. (if (and (string-match org-use-tag-inheritance x)
  12298. (not (member x org-tags-exclude-from-inheritance)))
  12299. x nil))
  12300. tags)))
  12301. ((listp org-use-tag-inheritance)
  12302. (delq nil (mapcar
  12303. (lambda (x)
  12304. (if (member x org-use-tag-inheritance) x nil))
  12305. tags)))))
  12306. (defun org-match-sparse-tree (&optional todo-only match)
  12307. "Create a sparse tree according to tags string MATCH.
  12308. MATCH can contain positive and negative selection of tags, like
  12309. \"+WORK+URGENT-WITHBOSS\".
  12310. If optional argument TODO-ONLY is non-nil, only select lines that are
  12311. also TODO lines."
  12312. (interactive "P")
  12313. (org-agenda-prepare-buffers (list (current-buffer)))
  12314. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  12315. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  12316. (defvar org-cached-props nil)
  12317. (defun org-cached-entry-get (pom property)
  12318. (if (or (eq t org-use-property-inheritance)
  12319. (and (stringp org-use-property-inheritance)
  12320. (string-match org-use-property-inheritance property))
  12321. (and (listp org-use-property-inheritance)
  12322. (member property org-use-property-inheritance)))
  12323. ;; Caching is not possible, check it directly
  12324. (org-entry-get pom property 'inherit)
  12325. ;; Get all properties, so that we can do complicated checks easily
  12326. (cdr (assoc property (or org-cached-props
  12327. (setq org-cached-props
  12328. (org-entry-properties pom)))))))
  12329. (defun org-global-tags-completion-table (&optional files)
  12330. "Return the list of all tags in all agenda buffer/files.
  12331. Optional FILES argument is a list of files which can be used
  12332. instead of the agenda files."
  12333. (save-excursion
  12334. (org-uniquify
  12335. (delq nil
  12336. (apply 'append
  12337. (mapcar
  12338. (lambda (file)
  12339. (set-buffer (find-file-noselect file))
  12340. (append (org-get-buffer-tags)
  12341. (mapcar (lambda (x) (if (stringp (car-safe x))
  12342. (list (car-safe x)) nil))
  12343. org-tag-alist)))
  12344. (if (and files (car files))
  12345. files
  12346. (org-agenda-files))))))))
  12347. (defun org-make-tags-matcher (match)
  12348. "Create the TAGS/TODO matcher form for the selection string MATCH.
  12349. The variable `todo-only' is scoped dynamically into this function.
  12350. It will be set to t if the matcher restricts matching to TODO entries,
  12351. otherwise will not be touched.
  12352. Returns a cons of the selection string MATCH and the constructed
  12353. lisp form implementing the matcher. The matcher is to be evaluated
  12354. at an Org entry, with point on the headline, and returns t if the
  12355. entry matches the selection string MATCH. The returned lisp form
  12356. references two variables with information about the entry, which
  12357. must be bound around the form's evaluation: todo, the TODO keyword
  12358. at the entry (or nil of none); and tags-list, the list of all tags
  12359. at the entry including inherited ones. Additionally, the category
  12360. of the entry (if any) must be specified as the text property
  12361. 'org-category on the headline.
  12362. See also `org-scan-tags'.
  12363. "
  12364. (declare (special todo-only))
  12365. (unless (boundp 'todo-only)
  12366. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  12367. (unless match
  12368. ;; Get a new match request, with completion
  12369. (let ((org-last-tags-completion-table
  12370. (org-global-tags-completion-table)))
  12371. (setq match (org-completing-read-no-i
  12372. "Match: " 'org-tags-completion-function nil nil nil
  12373. 'org-tags-history))))
  12374. ;; Parse the string and create a lisp form
  12375. (let ((match0 match)
  12376. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  12377. minus tag mm
  12378. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  12379. orterms term orlist re-p str-p level-p level-op time-p
  12380. prop-p pn pv po gv rest)
  12381. (if (string-match "/+" match)
  12382. ;; match contains also a todo-matching request
  12383. (progn
  12384. (setq tagsmatch (substring match 0 (match-beginning 0))
  12385. todomatch (substring match (match-end 0)))
  12386. (if (string-match "^!" todomatch)
  12387. (setq todo-only t todomatch (substring todomatch 1)))
  12388. (if (string-match "^\\s-*$" todomatch)
  12389. (setq todomatch nil)))
  12390. ;; only matching tags
  12391. (setq tagsmatch match todomatch nil))
  12392. ;; Make the tags matcher
  12393. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12394. (setq tagsmatcher t)
  12395. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12396. (while (setq term (pop orterms))
  12397. (while (and (equal (substring term -1) "\\") orterms)
  12398. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12399. (while (string-match re term)
  12400. (setq rest (substring term (match-end 0))
  12401. minus (and (match-end 1)
  12402. (equal (match-string 1 term) "-"))
  12403. tag (save-match-data (replace-regexp-in-string
  12404. "\\\\-" "-"
  12405. (match-string 2 term)))
  12406. re-p (equal (string-to-char tag) ?{)
  12407. level-p (match-end 4)
  12408. prop-p (match-end 5)
  12409. mm (cond
  12410. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12411. (level-p
  12412. (setq level-op (org-op-to-function (match-string 3 term)))
  12413. `(,level-op level ,(string-to-number
  12414. (match-string 4 term))))
  12415. (prop-p
  12416. (setq pn (match-string 5 term)
  12417. po (match-string 6 term)
  12418. pv (match-string 7 term)
  12419. re-p (equal (string-to-char pv) ?{)
  12420. str-p (equal (string-to-char pv) ?\")
  12421. time-p (save-match-data
  12422. (string-match "^\"[[<].*[]>]\"$" pv))
  12423. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12424. (if time-p (setq pv (org-matcher-time pv)))
  12425. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12426. (cond
  12427. ((equal pn "CATEGORY")
  12428. (setq gv '(get-text-property (point) 'org-category)))
  12429. ((equal pn "TODO")
  12430. (setq gv 'todo))
  12431. (t
  12432. (setq gv `(org-cached-entry-get nil ,pn))))
  12433. (if re-p
  12434. (if (eq po 'org<>)
  12435. `(not (string-match ,pv (or ,gv "")))
  12436. `(string-match ,pv (or ,gv "")))
  12437. (if str-p
  12438. `(,po (or ,gv "") ,pv)
  12439. `(,po (string-to-number (or ,gv ""))
  12440. ,(string-to-number pv) ))))
  12441. (t `(member ,tag tags-list)))
  12442. mm (if minus (list 'not mm) mm)
  12443. term rest)
  12444. (push mm tagsmatcher))
  12445. (push (if (> (length tagsmatcher) 1)
  12446. (cons 'and tagsmatcher)
  12447. (car tagsmatcher))
  12448. orlist)
  12449. (setq tagsmatcher nil))
  12450. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12451. (setq tagsmatcher
  12452. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12453. ;; Make the todo matcher
  12454. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12455. (setq todomatcher t)
  12456. (setq orterms (org-split-string todomatch "|") orlist nil)
  12457. (while (setq term (pop orterms))
  12458. (while (string-match re term)
  12459. (setq minus (and (match-end 1)
  12460. (equal (match-string 1 term) "-"))
  12461. kwd (match-string 2 term)
  12462. re-p (equal (string-to-char kwd) ?{)
  12463. term (substring term (match-end 0))
  12464. mm (if re-p
  12465. `(string-match ,(substring kwd 1 -1) todo)
  12466. (list 'equal 'todo kwd))
  12467. mm (if minus (list 'not mm) mm))
  12468. (push mm todomatcher))
  12469. (push (if (> (length todomatcher) 1)
  12470. (cons 'and todomatcher)
  12471. (car todomatcher))
  12472. orlist)
  12473. (setq todomatcher nil))
  12474. (setq todomatcher (if (> (length orlist) 1)
  12475. (cons 'or orlist) (car orlist))))
  12476. ;; Return the string and lisp forms of the matcher
  12477. (setq matcher (if todomatcher
  12478. (list 'and tagsmatcher todomatcher)
  12479. tagsmatcher))
  12480. (when todo-only
  12481. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12482. matcher)))
  12483. (cons match0 matcher)))
  12484. (defun org-op-to-function (op &optional stringp)
  12485. "Turn an operator into the appropriate function."
  12486. (setq op
  12487. (cond
  12488. ((equal op "<" ) '(< string< org-time<))
  12489. ((equal op ">" ) '(> org-string> org-time>))
  12490. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12491. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12492. ((member op '("=" "==")) '(= string= org-time=))
  12493. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12494. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12495. (defun org<> (a b) (not (= a b)))
  12496. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12497. (defun org-string>= (a b) (not (string< a b)))
  12498. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12499. (defun org-string<> (a b) (not (string= a b)))
  12500. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12501. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12502. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12503. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12504. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12505. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12506. (defun org-2ft (s)
  12507. "Convert S to a floating point time.
  12508. If S is already a number, just return it. If it is a string, parse
  12509. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12510. (cond
  12511. ((numberp s) s)
  12512. ((stringp s)
  12513. (condition-case nil
  12514. (float-time (apply 'encode-time (org-parse-time-string s)))
  12515. (error 0.)))
  12516. (t 0.)))
  12517. (defun org-time-today ()
  12518. "Time in seconds today at 0:00.
  12519. Returns the float number of seconds since the beginning of the
  12520. epoch to the beginning of today (00:00)."
  12521. (float-time (apply 'encode-time
  12522. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12523. (defun org-matcher-time (s)
  12524. "Interpret a time comparison value."
  12525. (save-match-data
  12526. (cond
  12527. ((string= s "<now>") (float-time))
  12528. ((string= s "<today>") (org-time-today))
  12529. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12530. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12531. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12532. (+ (org-time-today)
  12533. (* (string-to-number (match-string 1 s))
  12534. (cdr (assoc (match-string 2 s)
  12535. '(("d" . 86400.0) ("w" . 604800.0)
  12536. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12537. (t (org-2ft s)))))
  12538. (defun org-match-any-p (re list)
  12539. "Does re match any element of list?"
  12540. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12541. (delq nil list))
  12542. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12543. (defvar org-tags-overlay (make-overlay 1 1))
  12544. (org-detach-overlay org-tags-overlay)
  12545. (defun org-get-local-tags-at (&optional pos)
  12546. "Get a list of tags defined in the current headline."
  12547. (org-get-tags-at pos 'local))
  12548. (defun org-get-local-tags ()
  12549. "Get a list of tags defined in the current headline."
  12550. (org-get-tags-at nil 'local))
  12551. (defun org-get-tags-at (&optional pos local)
  12552. "Get a list of all headline tags applicable at POS.
  12553. POS defaults to point. If tags are inherited, the list contains
  12554. the targets in the same sequence as the headlines appear, i.e.
  12555. the tags of the current headline come last.
  12556. When LOCAL is non-nil, only return tags from the current headline,
  12557. ignore inherited ones."
  12558. (interactive)
  12559. (if (and org-trust-scanner-tags
  12560. (or (not pos) (equal pos (point)))
  12561. (not local))
  12562. org-scanner-tags
  12563. (let (tags ltags lastpos parent)
  12564. (save-excursion
  12565. (save-restriction
  12566. (widen)
  12567. (goto-char (or pos (point)))
  12568. (save-match-data
  12569. (catch 'done
  12570. (condition-case nil
  12571. (progn
  12572. (org-back-to-heading t)
  12573. (while (not (equal lastpos (point)))
  12574. (setq lastpos (point))
  12575. (when (looking-at
  12576. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12577. (setq ltags (org-split-string
  12578. (org-match-string-no-properties 1) ":"))
  12579. (when parent
  12580. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12581. (setq tags (append
  12582. (if parent
  12583. (org-remove-uninherited-tags ltags)
  12584. ltags)
  12585. tags)))
  12586. (or org-use-tag-inheritance (throw 'done t))
  12587. (if local (throw 'done t))
  12588. (or (org-up-heading-safe) (error nil))
  12589. (setq parent t)))
  12590. (error nil)))))
  12591. (if local
  12592. tags
  12593. (reverse (delete-dups
  12594. (reverse (append
  12595. (org-remove-uninherited-tags
  12596. org-file-tags) tags)))))))))
  12597. (defun org-add-prop-inherited (s)
  12598. (add-text-properties 0 (length s) '(inherited t) s)
  12599. s)
  12600. (defun org-toggle-tag (tag &optional onoff)
  12601. "Toggle the tag TAG for the current line.
  12602. If ONOFF is `on' or `off', don't toggle but set to this state."
  12603. (let (res current)
  12604. (save-excursion
  12605. (org-back-to-heading t)
  12606. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12607. (point-at-eol) t)
  12608. (progn
  12609. (setq current (match-string 1))
  12610. (replace-match ""))
  12611. (setq current ""))
  12612. (setq current (nreverse (org-split-string current ":")))
  12613. (cond
  12614. ((eq onoff 'on)
  12615. (setq res t)
  12616. (or (member tag current) (push tag current)))
  12617. ((eq onoff 'off)
  12618. (or (not (member tag current)) (setq current (delete tag current))))
  12619. (t (if (member tag current)
  12620. (setq current (delete tag current))
  12621. (setq res t)
  12622. (push tag current))))
  12623. (end-of-line 1)
  12624. (if current
  12625. (progn
  12626. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12627. (org-set-tags nil t))
  12628. (delete-horizontal-space))
  12629. (run-hooks 'org-after-tags-change-hook))
  12630. res))
  12631. (defun org-align-tags-here (to-col)
  12632. ;; Assumes that this is a headline
  12633. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12634. (beginning-of-line 1)
  12635. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12636. (< pos (match-beginning 2)))
  12637. (progn
  12638. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12639. (goto-char (match-beginning 1))
  12640. (insert " ")
  12641. (delete-region (point) (1+ (match-beginning 2)))
  12642. (setq ncol (max (current-column)
  12643. (1+ col)
  12644. (if (> to-col 0)
  12645. to-col
  12646. (- (abs to-col) tags-l))))
  12647. (setq p (point))
  12648. (insert (make-string (- ncol (current-column)) ?\ ))
  12649. (setq ncol (current-column))
  12650. (when indent-tabs-mode (tabify p (point-at-eol)))
  12651. (org-move-to-column (min ncol col) t))
  12652. (goto-char pos))))
  12653. (defun org-set-tags-command (&optional arg just-align)
  12654. "Call the set-tags command for the current entry."
  12655. (interactive "P")
  12656. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12657. (org-set-tags arg just-align)
  12658. (save-excursion
  12659. (org-back-to-heading t)
  12660. (org-set-tags arg just-align))))
  12661. (defun org-set-tags-to (data)
  12662. "Set the tags of the current entry to DATA, replacing the current tags.
  12663. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12664. If DATA is nil or the empty string, any tags will be removed."
  12665. (interactive "sTags: ")
  12666. (setq data
  12667. (cond
  12668. ((eq data nil) "")
  12669. ((equal data "") "")
  12670. ((stringp data)
  12671. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12672. ":"))
  12673. ((listp data)
  12674. (concat ":" (mapconcat 'identity data ":") ":"))))
  12675. (when data
  12676. (save-excursion
  12677. (org-back-to-heading t)
  12678. (when (looking-at org-complex-heading-regexp)
  12679. (if (match-end 5)
  12680. (progn
  12681. (goto-char (match-beginning 5))
  12682. (insert data)
  12683. (delete-region (point) (point-at-eol))
  12684. (org-set-tags nil 'align))
  12685. (goto-char (point-at-eol))
  12686. (insert " " data)
  12687. (org-set-tags nil 'align)))
  12688. (beginning-of-line 1)
  12689. (if (looking-at ".*?\\([ \t]+\\)$")
  12690. (delete-region (match-beginning 1) (match-end 1))))))
  12691. (defun org-align-all-tags ()
  12692. "Align the tags i all headings."
  12693. (interactive)
  12694. (save-excursion
  12695. (or (ignore-errors (org-back-to-heading t))
  12696. (outline-next-heading))
  12697. (if (org-at-heading-p)
  12698. (org-set-tags t)
  12699. (message "No headings"))))
  12700. (defvar org-indent-indentation-per-level)
  12701. (defun org-set-tags (&optional arg just-align)
  12702. "Set the tags for the current headline.
  12703. With prefix ARG, realign all tags in headings in the current buffer."
  12704. (interactive "P")
  12705. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12706. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12707. 'region-start-level 'region))
  12708. org-loop-over-headlines-in-active-region)
  12709. (org-map-entries
  12710. ;; We don't use ARG and JUST-ALIGN here these args are not
  12711. ;; useful when looping over headlines
  12712. `(org-set-tags)
  12713. org-loop-over-headlines-in-active-region
  12714. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12715. (let* ((re org-outline-regexp-bol)
  12716. (current (unless arg (org-get-tags-string)))
  12717. (col (current-column))
  12718. (org-setting-tags t)
  12719. table current-tags inherited-tags ; computed below when needed
  12720. tags p0 c0 c1 rpl di tc level)
  12721. (if arg
  12722. (save-excursion
  12723. (goto-char (point-min))
  12724. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12725. (while (re-search-forward re nil t)
  12726. (org-set-tags nil t)
  12727. (end-of-line 1)))
  12728. (message "All tags realigned to column %d" org-tags-column))
  12729. (if just-align
  12730. (setq tags current)
  12731. ;; Get a new set of tags from the user
  12732. (save-excursion
  12733. (setq table (append org-tag-persistent-alist
  12734. (or org-tag-alist (org-get-buffer-tags))
  12735. (and
  12736. org-complete-tags-always-offer-all-agenda-tags
  12737. (org-global-tags-completion-table
  12738. (org-agenda-files))))
  12739. org-last-tags-completion-table table
  12740. current-tags (org-split-string current ":")
  12741. inherited-tags (nreverse
  12742. (nthcdr (length current-tags)
  12743. (nreverse (org-get-tags-at))))
  12744. tags
  12745. (if (or (eq t org-use-fast-tag-selection)
  12746. (and org-use-fast-tag-selection
  12747. (delq nil (mapcar 'cdr table))))
  12748. (org-fast-tag-selection
  12749. current-tags inherited-tags table
  12750. (if org-fast-tag-selection-include-todo
  12751. org-todo-key-alist))
  12752. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12753. (org-trim
  12754. (org-icompleting-read "Tags: "
  12755. 'org-tags-completion-function
  12756. nil nil current 'org-tags-history))))))
  12757. (while (string-match "[-+&]+" tags)
  12758. ;; No boolean logic, just a list
  12759. (setq tags (replace-match ":" t t tags))))
  12760. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12761. (if org-tags-sort-function
  12762. (setq tags (mapconcat 'identity
  12763. (sort (org-split-string
  12764. tags (org-re "[^[:alnum:]_@#%]+"))
  12765. org-tags-sort-function) ":")))
  12766. (if (string-match "\\`[\t ]*\\'" tags)
  12767. (setq tags "")
  12768. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12769. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12770. ;; Insert new tags at the correct column
  12771. (beginning-of-line 1)
  12772. (setq level (or (and (looking-at org-outline-regexp)
  12773. (- (match-end 0) (point) 1))
  12774. 1))
  12775. (cond
  12776. ((and (equal current "") (equal tags "")))
  12777. ((re-search-forward
  12778. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12779. (point-at-eol) t)
  12780. (if (equal tags "")
  12781. (setq rpl "")
  12782. (goto-char (match-beginning 0))
  12783. (setq c0 (current-column)
  12784. ;; compute offset for the case of org-indent-mode active
  12785. di (if org-indent-mode
  12786. (* (1- org-indent-indentation-per-level) (1- level))
  12787. 0)
  12788. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12789. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12790. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12791. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12792. (replace-match rpl t t)
  12793. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12794. tags)
  12795. (t (error "Tags alignment failed")))
  12796. (org-move-to-column col)
  12797. (unless just-align
  12798. (run-hooks 'org-after-tags-change-hook))))))
  12799. (defun org-change-tag-in-region (beg end tag off)
  12800. "Add or remove TAG for each entry in the region.
  12801. This works in the agenda, and also in an org-mode buffer."
  12802. (interactive
  12803. (list (region-beginning) (region-end)
  12804. (let ((org-last-tags-completion-table
  12805. (if (derived-mode-p 'org-mode)
  12806. (org-get-buffer-tags)
  12807. (org-global-tags-completion-table))))
  12808. (org-icompleting-read
  12809. "Tag: " 'org-tags-completion-function nil nil nil
  12810. 'org-tags-history))
  12811. (progn
  12812. (message "[s]et or [r]emove? ")
  12813. (equal (read-char-exclusive) ?r))))
  12814. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12815. (let ((agendap (equal major-mode 'org-agenda-mode))
  12816. l1 l2 m buf pos newhead (cnt 0))
  12817. (goto-char end)
  12818. (setq l2 (1- (org-current-line)))
  12819. (goto-char beg)
  12820. (setq l1 (org-current-line))
  12821. (loop for l from l1 to l2 do
  12822. (org-goto-line l)
  12823. (setq m (get-text-property (point) 'org-hd-marker))
  12824. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12825. (and agendap m))
  12826. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12827. pos (if agendap m (point)))
  12828. (with-current-buffer buf
  12829. (save-excursion
  12830. (save-restriction
  12831. (goto-char pos)
  12832. (setq cnt (1+ cnt))
  12833. (org-toggle-tag tag (if off 'off 'on))
  12834. (setq newhead (org-get-heading)))))
  12835. (and agendap (org-agenda-change-all-lines newhead m))))
  12836. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12837. (defun org-tags-completion-function (string predicate &optional flag)
  12838. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12839. (confirm (lambda (x) (stringp (car x)))))
  12840. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12841. (setq s1 (match-string 1 string)
  12842. s2 (match-string 2 string))
  12843. (setq s1 "" s2 string))
  12844. (cond
  12845. ((eq flag nil)
  12846. ;; try completion
  12847. (setq rtn (try-completion s2 ctable confirm))
  12848. (if (stringp rtn)
  12849. (setq rtn
  12850. (concat s1 s2 (substring rtn (length s2))
  12851. (if (and org-add-colon-after-tag-completion
  12852. (assoc rtn ctable))
  12853. ":" ""))))
  12854. rtn)
  12855. ((eq flag t)
  12856. ;; all-completions
  12857. (all-completions s2 ctable confirm)
  12858. )
  12859. ((eq flag 'lambda)
  12860. ;; exact match?
  12861. (assoc s2 ctable)))
  12862. ))
  12863. (defun org-fast-tag-insert (kwd tags face &optional end)
  12864. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12865. (insert (format "%-12s" (concat kwd ":"))
  12866. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12867. (or end "")))
  12868. (defun org-fast-tag-show-exit (flag)
  12869. (save-excursion
  12870. (org-goto-line 3)
  12871. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12872. (replace-match ""))
  12873. (when flag
  12874. (end-of-line 1)
  12875. (org-move-to-column (- (window-width) 19) t)
  12876. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12877. (defun org-set-current-tags-overlay (current prefix)
  12878. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12879. (if (featurep 'xemacs)
  12880. (org-overlay-display org-tags-overlay (concat prefix s)
  12881. 'secondary-selection)
  12882. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12883. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12884. (defvar org-last-tag-selection-key nil)
  12885. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12886. "Fast tag selection with single keys.
  12887. CURRENT is the current list of tags in the headline, INHERITED is the
  12888. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12889. possibly with grouping information. TODO-TABLE is a similar table with
  12890. TODO keywords, should these have keys assigned to them.
  12891. If the keys are nil, a-z are automatically assigned.
  12892. Returns the new tags string, or nil to not change the current settings."
  12893. (let* ((fulltable (append table todo-table))
  12894. (maxlen (apply 'max (mapcar
  12895. (lambda (x)
  12896. (if (stringp (car x)) (string-width (car x)) 0))
  12897. fulltable)))
  12898. (buf (current-buffer))
  12899. (expert (eq org-fast-tag-selection-single-key 'expert))
  12900. (buffer-tags nil)
  12901. (fwidth (+ maxlen 3 1 3))
  12902. (ncol (/ (- (window-width) 4) fwidth))
  12903. (i-face 'org-done)
  12904. (c-face 'org-todo)
  12905. tg cnt e c char c1 c2 ntable tbl rtn
  12906. ov-start ov-end ov-prefix
  12907. (exit-after-next org-fast-tag-selection-single-key)
  12908. (done-keywords org-done-keywords)
  12909. groups ingroup)
  12910. (save-excursion
  12911. (beginning-of-line 1)
  12912. (if (looking-at
  12913. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12914. (setq ov-start (match-beginning 1)
  12915. ov-end (match-end 1)
  12916. ov-prefix "")
  12917. (setq ov-start (1- (point-at-eol))
  12918. ov-end (1+ ov-start))
  12919. (skip-chars-forward "^\n\r")
  12920. (setq ov-prefix
  12921. (concat
  12922. (buffer-substring (1- (point)) (point))
  12923. (if (> (current-column) org-tags-column)
  12924. " "
  12925. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12926. (move-overlay org-tags-overlay ov-start ov-end)
  12927. (save-window-excursion
  12928. (if expert
  12929. (set-buffer (get-buffer-create " *Org tags*"))
  12930. (delete-other-windows)
  12931. (split-window-vertically)
  12932. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12933. (erase-buffer)
  12934. (org-set-local 'org-done-keywords done-keywords)
  12935. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12936. (org-fast-tag-insert "Current" current c-face "\n\n")
  12937. (org-fast-tag-show-exit exit-after-next)
  12938. (org-set-current-tags-overlay current ov-prefix)
  12939. (setq tbl fulltable char ?a cnt 0)
  12940. (while (setq e (pop tbl))
  12941. (cond
  12942. ((equal (car e) :startgroup)
  12943. (push '() groups) (setq ingroup t)
  12944. (when (not (= cnt 0))
  12945. (setq cnt 0)
  12946. (insert "\n"))
  12947. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12948. ((equal (car e) :endgroup)
  12949. (setq ingroup nil cnt 0)
  12950. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12951. ((equal e '(:newline))
  12952. (when (not (= cnt 0))
  12953. (setq cnt 0)
  12954. (insert "\n")
  12955. (setq e (car tbl))
  12956. (while (equal (car tbl) '(:newline))
  12957. (insert "\n")
  12958. (setq tbl (cdr tbl)))))
  12959. (t
  12960. (setq tg (copy-sequence (car e)) c2 nil)
  12961. (if (cdr e)
  12962. (setq c (cdr e))
  12963. ;; automatically assign a character.
  12964. (setq c1 (string-to-char
  12965. (downcase (substring
  12966. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12967. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12968. (while (or (rassoc char ntable) (rassoc char table))
  12969. (setq char (1+ char)))
  12970. (setq c2 c1))
  12971. (setq c (or c2 char)))
  12972. (if ingroup (push tg (car groups)))
  12973. (setq tg (org-add-props tg nil 'face
  12974. (cond
  12975. ((not (assoc tg table))
  12976. (org-get-todo-face tg))
  12977. ((member tg current) c-face)
  12978. ((member tg inherited) i-face))))
  12979. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12980. (insert "[" c "] " tg (make-string
  12981. (- fwidth 4 (length tg)) ?\ ))
  12982. (push (cons tg c) ntable)
  12983. (when (= (setq cnt (1+ cnt)) ncol)
  12984. (insert "\n")
  12985. (if ingroup (insert " "))
  12986. (setq cnt 0)))))
  12987. (setq ntable (nreverse ntable))
  12988. (insert "\n")
  12989. (goto-char (point-min))
  12990. (if (not expert) (org-fit-window-to-buffer))
  12991. (setq rtn
  12992. (catch 'exit
  12993. (while t
  12994. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12995. (if (not groups) "no " "")
  12996. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12997. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12998. (setq org-last-tag-selection-key c)
  12999. (cond
  13000. ((= c ?\r) (throw 'exit t))
  13001. ((= c ?!)
  13002. (setq groups (not groups))
  13003. (goto-char (point-min))
  13004. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13005. ((= c ?\C-c)
  13006. (if (not expert)
  13007. (org-fast-tag-show-exit
  13008. (setq exit-after-next (not exit-after-next)))
  13009. (setq expert nil)
  13010. (delete-other-windows)
  13011. (set-window-buffer (split-window-vertically) " *Org tags*")
  13012. (org-switch-to-buffer-other-window " *Org tags*")
  13013. (org-fit-window-to-buffer)))
  13014. ((or (= c ?\C-g)
  13015. (and (= c ?q) (not (rassoc c ntable))))
  13016. (org-detach-overlay org-tags-overlay)
  13017. (setq quit-flag t))
  13018. ((= c ?\ )
  13019. (setq current nil)
  13020. (if exit-after-next (setq exit-after-next 'now)))
  13021. ((= c ?\t)
  13022. (condition-case nil
  13023. (setq tg (org-icompleting-read
  13024. "Tag: "
  13025. (or buffer-tags
  13026. (with-current-buffer buf
  13027. (org-get-buffer-tags)))))
  13028. (quit (setq tg "")))
  13029. (when (string-match "\\S-" tg)
  13030. (add-to-list 'buffer-tags (list tg))
  13031. (if (member tg current)
  13032. (setq current (delete tg current))
  13033. (push tg current)))
  13034. (if exit-after-next (setq exit-after-next 'now)))
  13035. ((setq e (rassoc c todo-table) tg (car e))
  13036. (with-current-buffer buf
  13037. (save-excursion (org-todo tg)))
  13038. (if exit-after-next (setq exit-after-next 'now)))
  13039. ((setq e (rassoc c ntable) tg (car e))
  13040. (if (member tg current)
  13041. (setq current (delete tg current))
  13042. (loop for g in groups do
  13043. (if (member tg g)
  13044. (mapc (lambda (x)
  13045. (setq current (delete x current)))
  13046. g)))
  13047. (push tg current))
  13048. (if exit-after-next (setq exit-after-next 'now))))
  13049. ;; Create a sorted list
  13050. (setq current
  13051. (sort current
  13052. (lambda (a b)
  13053. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13054. (if (eq exit-after-next 'now) (throw 'exit t))
  13055. (goto-char (point-min))
  13056. (beginning-of-line 2)
  13057. (delete-region (point) (point-at-eol))
  13058. (org-fast-tag-insert "Current" current c-face)
  13059. (org-set-current-tags-overlay current ov-prefix)
  13060. (while (re-search-forward
  13061. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13062. (setq tg (match-string 1))
  13063. (add-text-properties
  13064. (match-beginning 1) (match-end 1)
  13065. (list 'face
  13066. (cond
  13067. ((member tg current) c-face)
  13068. ((member tg inherited) i-face)
  13069. (t (get-text-property (match-beginning 1) 'face))))))
  13070. (goto-char (point-min)))))
  13071. (org-detach-overlay org-tags-overlay)
  13072. (if rtn
  13073. (mapconcat 'identity current ":")
  13074. nil))))
  13075. (defun org-get-tags-string ()
  13076. "Get the TAGS string in the current headline."
  13077. (unless (org-at-heading-p t)
  13078. (error "Not on a heading"))
  13079. (save-excursion
  13080. (beginning-of-line 1)
  13081. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13082. (org-match-string-no-properties 1)
  13083. "")))
  13084. (defun org-get-tags ()
  13085. "Get the list of tags specified in the current headline."
  13086. (org-split-string (org-get-tags-string) ":"))
  13087. (defun org-get-buffer-tags ()
  13088. "Get a table of all tags used in the buffer, for completion."
  13089. (let (tags)
  13090. (save-excursion
  13091. (goto-char (point-min))
  13092. (while (re-search-forward
  13093. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  13094. (when (equal (char-after (point-at-bol 0)) ?*)
  13095. (mapc (lambda (x) (add-to-list 'tags x))
  13096. (org-split-string (org-match-string-no-properties 1) ":")))))
  13097. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  13098. (mapcar 'list tags)))
  13099. ;;;; The mapping API
  13100. (defun org-map-entries (func &optional match scope &rest skip)
  13101. "Call FUNC at each headline selected by MATCH in SCOPE.
  13102. FUNC is a function or a lisp form. The function will be called without
  13103. arguments, with the cursor positioned at the beginning of the headline.
  13104. The return values of all calls to the function will be collected and
  13105. returned as a list.
  13106. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13107. does not need to preserve point. After evaluation, the cursor will be
  13108. moved to the end of the line (presumably of the headline of the
  13109. processed entry) and search continues from there. Under some
  13110. circumstances, this may not produce the wanted results. For example,
  13111. if you have removed (e.g. archived) the current (sub)tree it could
  13112. mean that the next entry will be skipped entirely. In such cases, you
  13113. can specify the position from where search should continue by making
  13114. FUNC set the variable `org-map-continue-from' to the desired buffer
  13115. position.
  13116. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13117. Only headlines that are matched by this query will be considered during
  13118. the iteration. When MATCH is nil or t, all headlines will be
  13119. visited by the iteration.
  13120. SCOPE determines the scope of this command. It can be any of:
  13121. nil The current buffer, respecting the restriction if any
  13122. tree The subtree started with the entry at point
  13123. region The entries within the active region, if any
  13124. region-start-level
  13125. The entries within the active region, but only those at
  13126. the same level than the first one.
  13127. file The current buffer, without restriction
  13128. file-with-archives
  13129. The current buffer, and any archives associated with it
  13130. agenda All agenda files
  13131. agenda-with-archives
  13132. All agenda files with any archive files associated with them
  13133. \(file1 file2 ...)
  13134. If this is a list, all files in the list will be scanned
  13135. The remaining args are treated as settings for the skipping facilities of
  13136. the scanner. The following items can be given here:
  13137. archive skip trees with the archive tag.
  13138. comment skip trees with the COMMENT keyword
  13139. function or Emacs Lisp form:
  13140. will be used as value for `org-agenda-skip-function', so whenever
  13141. the function returns t, FUNC will not be called for that
  13142. entry and search will continue from the point where the
  13143. function leaves it.
  13144. If your function needs to retrieve the tags including inherited tags
  13145. at the *current* entry, you can use the value of the variable
  13146. `org-scanner-tags' which will be much faster than getting the value
  13147. with `org-get-tags-at'. If your function gets properties with
  13148. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  13149. to t around the call to `org-entry-properties' to get the same speedup.
  13150. Note that if your function moves around to retrieve tags and properties at
  13151. a *different* entry, you cannot use these techniques."
  13152. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  13153. (not (org-region-active-p)))
  13154. (let* ((org-agenda-archives-mode nil) ; just to make sure
  13155. (org-agenda-skip-archived-trees (memq 'archive skip))
  13156. (org-agenda-skip-comment-trees (memq 'comment skip))
  13157. (org-agenda-skip-function
  13158. (car (org-delete-all '(comment archive) skip)))
  13159. (org-tags-match-list-sublevels t)
  13160. (start-level (eq scope 'region-start-level))
  13161. matcher file res
  13162. org-todo-keywords-for-agenda
  13163. org-done-keywords-for-agenda
  13164. org-todo-keyword-alist-for-agenda
  13165. org-drawers-for-agenda
  13166. org-tag-alist-for-agenda
  13167. todo-only)
  13168. (cond
  13169. ((eq match t) (setq matcher t))
  13170. ((eq match nil) (setq matcher t))
  13171. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  13172. (save-excursion
  13173. (save-restriction
  13174. (cond ((eq scope 'tree)
  13175. (org-back-to-heading t)
  13176. (org-narrow-to-subtree)
  13177. (setq scope nil))
  13178. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  13179. (org-region-active-p))
  13180. ;; If needed, set start-level to a string like "2"
  13181. (when start-level
  13182. (save-excursion
  13183. (goto-char (region-beginning))
  13184. (unless (org-at-heading-p) (outline-next-heading))
  13185. (setq start-level (org-current-level))))
  13186. (narrow-to-region (region-beginning)
  13187. (save-excursion
  13188. (goto-char (region-end))
  13189. (unless (and (bolp) (org-at-heading-p))
  13190. (outline-next-heading))
  13191. (point)))
  13192. (setq scope nil)))
  13193. (if (not scope)
  13194. (progn
  13195. (org-agenda-prepare-buffers
  13196. (list (buffer-file-name (current-buffer))))
  13197. (setq res (org-scan-tags func matcher todo-only start-level)))
  13198. ;; Get the right scope
  13199. (cond
  13200. ((and scope (listp scope) (symbolp (car scope)))
  13201. (setq scope (eval scope)))
  13202. ((eq scope 'agenda)
  13203. (setq scope (org-agenda-files t)))
  13204. ((eq scope 'agenda-with-archives)
  13205. (setq scope (org-agenda-files t))
  13206. (setq scope (org-add-archive-files scope)))
  13207. ((eq scope 'file)
  13208. (setq scope (list (buffer-file-name))))
  13209. ((eq scope 'file-with-archives)
  13210. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  13211. (org-agenda-prepare-buffers scope)
  13212. (while (setq file (pop scope))
  13213. (with-current-buffer (org-find-base-buffer-visiting file)
  13214. (save-excursion
  13215. (save-restriction
  13216. (widen)
  13217. (goto-char (point-min))
  13218. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  13219. res)))
  13220. ;;;; Properties
  13221. ;;; Setting and retrieving properties
  13222. (defconst org-special-properties
  13223. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  13224. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM" "CLOCKSUM_T")
  13225. "The special properties valid in Org-mode.
  13226. These are properties that are not defined in the property drawer,
  13227. but in some other way.")
  13228. (defconst org-default-properties
  13229. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  13230. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  13231. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  13232. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  13233. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  13234. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  13235. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  13236. "Some properties that are used by Org-mode for various purposes.
  13237. Being in this list makes sure that they are offered for completion.")
  13238. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  13239. "Regular expression matching the first line of a property drawer.")
  13240. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  13241. "Regular expression matching the last line of a property drawer.")
  13242. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  13243. "Regular expression matching the first line of a property drawer.")
  13244. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  13245. "Regular expression matching the first line of a property drawer.")
  13246. (defconst org-property-drawer-re
  13247. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  13248. org-property-end-re "\\)\n?")
  13249. "Matches an entire property drawer.")
  13250. (defconst org-clock-drawer-re
  13251. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  13252. org-property-end-re "\\)\n?")
  13253. "Matches an entire clock drawer.")
  13254. (defsubst org-re-property (property)
  13255. "Return a regexp matching a PROPERTY line.
  13256. Match group 1 will be set to the value."
  13257. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  13258. (defsubst org-re-property-keyword (property)
  13259. "Return a regexp matching a PROPERTY line, possibly with no
  13260. value for the property."
  13261. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)?"))
  13262. (defun org-property-action ()
  13263. "Do an action on properties."
  13264. (interactive)
  13265. (let (c)
  13266. (org-at-property-p)
  13267. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  13268. (setq c (read-char-exclusive))
  13269. (cond
  13270. ((equal c ?s)
  13271. (call-interactively 'org-set-property))
  13272. ((equal c ?d)
  13273. (call-interactively 'org-delete-property))
  13274. ((equal c ?D)
  13275. (call-interactively 'org-delete-property-globally))
  13276. ((equal c ?c)
  13277. (call-interactively 'org-compute-property-at-point))
  13278. (t (error "No such property action %c" c)))))
  13279. (defun org-inc-effort ()
  13280. "Increment the value of the effort property in the current entry."
  13281. (interactive)
  13282. (org-set-effort nil t))
  13283. (defun org-set-effort (&optional value increment)
  13284. "Set the effort property of the current entry.
  13285. With numerical prefix arg, use the nth allowed value, 0 stands for the
  13286. 10th allowed value.
  13287. When INCREMENT is non-nil, set the property to the next allowed value."
  13288. (interactive "P")
  13289. (if (equal value 0) (setq value 10))
  13290. (let* ((completion-ignore-case t)
  13291. (prop org-effort-property)
  13292. (cur (org-entry-get nil prop))
  13293. (allowed (org-property-get-allowed-values nil prop 'table))
  13294. (existing (mapcar 'list (org-property-values prop)))
  13295. rpl
  13296. (val (cond
  13297. ((stringp value) value)
  13298. ((and allowed (integerp value))
  13299. (or (car (nth (1- value) allowed))
  13300. (car (org-last allowed))))
  13301. ((and allowed increment)
  13302. (or (caadr (member (list cur) allowed))
  13303. (error "Allowed effort values are not set")))
  13304. (allowed
  13305. (message "Select 1-9,0, [RET%s]: %s"
  13306. (if cur (concat "=" cur) "")
  13307. (mapconcat 'car allowed " "))
  13308. (setq rpl (read-char-exclusive))
  13309. (if (equal rpl ?\r)
  13310. cur
  13311. (setq rpl (- rpl ?0))
  13312. (if (equal rpl 0) (setq rpl 10))
  13313. (if (and (> rpl 0) (<= rpl (length allowed)))
  13314. (car (nth (1- rpl) allowed))
  13315. (org-completing-read "Effort: " allowed nil))))
  13316. (t
  13317. (let (org-completion-use-ido org-completion-use-iswitchb)
  13318. (org-completing-read
  13319. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  13320. (concat "[" cur "]") "")
  13321. ": ")
  13322. existing nil nil "" nil cur))))))
  13323. (unless (equal (org-entry-get nil prop) val)
  13324. (org-entry-put nil prop val))
  13325. (save-excursion
  13326. (org-back-to-heading t)
  13327. (put-text-property (point-at-bol) (point-at-eol) 'org-effort val))
  13328. (message "%s is now %s" prop val)))
  13329. (defun org-at-property-p ()
  13330. "Is cursor inside a property drawer?"
  13331. (save-excursion
  13332. (beginning-of-line 1)
  13333. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  13334. (save-match-data ;; Used by calling procedures
  13335. (let ((p (point))
  13336. (range (unless (org-before-first-heading-p)
  13337. (org-get-property-block))))
  13338. (and range (<= (car range) p) (< p (cdr range))))))))
  13339. (defun org-get-property-block (&optional beg end force)
  13340. "Return the (beg . end) range of the body of the property drawer.
  13341. BEG and END are the beginning and end of the current subtree, or of
  13342. the part before the first headline. If they are not given, they will
  13343. be found. If the drawer does not exist and FORCE is non-nil, create
  13344. the drawer."
  13345. (catch 'exit
  13346. (save-excursion
  13347. (let* ((beg (or beg (and (org-before-first-heading-p) (point-min))
  13348. (progn (org-back-to-heading t) (point))))
  13349. (end (or end (and (not (outline-next-heading)) (point-max))
  13350. (point))))
  13351. (goto-char beg)
  13352. (if (re-search-forward org-property-start-re end t)
  13353. (setq beg (1+ (match-end 0)))
  13354. (if force
  13355. (save-excursion
  13356. (org-insert-property-drawer)
  13357. (setq end (progn (outline-next-heading) (point))))
  13358. (throw 'exit nil))
  13359. (goto-char beg)
  13360. (if (re-search-forward org-property-start-re end t)
  13361. (setq beg (1+ (match-end 0)))))
  13362. (if (re-search-forward org-property-end-re end t)
  13363. (setq end (match-beginning 0))
  13364. (or force (throw 'exit nil))
  13365. (goto-char beg)
  13366. (setq end beg)
  13367. (org-indent-line)
  13368. (insert ":END:\n"))
  13369. (cons beg end)))))
  13370. (defun org-entry-properties (&optional pom which specific)
  13371. "Get all properties of the entry at point-or-marker POM.
  13372. This includes the TODO keyword, the tags, time strings for deadline,
  13373. scheduled, and clocking, and any additional properties defined in the
  13374. entry. The return value is an alist, keys may occur multiple times
  13375. if the property key was used several times.
  13376. POM may also be nil, in which case the current entry is used.
  13377. If WHICH is nil or `all', get all properties. If WHICH is
  13378. `special' or `standard', only get that subclass. If WHICH
  13379. is a string only get exactly this property. SPECIFIC can be a string, the
  13380. specific property we are interested in. Specifying it can speed
  13381. things up because then unnecessary parsing is avoided."
  13382. (setq which (or which 'all))
  13383. (org-with-point-at pom
  13384. (let ((clockstr (substring org-clock-string 0 -1))
  13385. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  13386. (case-fold-search nil)
  13387. beg end range props sum-props key key1 value string clocksum clocksumt)
  13388. (save-excursion
  13389. (when (condition-case nil
  13390. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  13391. (error nil))
  13392. (setq beg (point))
  13393. (setq sum-props (get-text-property (point) 'org-summaries))
  13394. (setq clocksum (get-text-property (point) :org-clock-minutes)
  13395. clocksumt (get-text-property (point) :org-clock-minutes-today))
  13396. (outline-next-heading)
  13397. (setq end (point))
  13398. (when (memq which '(all special))
  13399. ;; Get the special properties, like TODO and tags
  13400. (goto-char beg)
  13401. (when (and (or (not specific) (string= specific "TODO"))
  13402. (looking-at org-todo-line-regexp) (match-end 2))
  13403. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  13404. (when (and (or (not specific) (string= specific "PRIORITY"))
  13405. (looking-at org-priority-regexp))
  13406. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13407. (when (or (not specific) (string= specific "FILE"))
  13408. (push (cons "FILE" buffer-file-name) props))
  13409. (when (and (or (not specific) (string= specific "TAGS"))
  13410. (setq value (org-get-tags-string))
  13411. (string-match "\\S-" value))
  13412. (push (cons "TAGS" value) props))
  13413. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13414. (setq value (org-get-tags-at)))
  13415. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13416. ":"))
  13417. props))
  13418. (when (or (not specific) (string= specific "BLOCKED"))
  13419. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13420. (when (or (not specific)
  13421. (member specific
  13422. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13423. "TIMESTAMP" "TIMESTAMP_IA")))
  13424. (catch 'match
  13425. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13426. (setq key (if (match-end 1)
  13427. (substring (org-match-string-no-properties 1)
  13428. 0 -1))
  13429. string (if (equal key clockstr)
  13430. (org-trim
  13431. (buffer-substring-no-properties
  13432. (match-beginning 3) (goto-char
  13433. (point-at-eol))))
  13434. (substring (org-match-string-no-properties 3)
  13435. 1 -1)))
  13436. ;; Get the correct property name from the key. This is
  13437. ;; necessary if the user has configured time keywords.
  13438. (setq key1 (concat key ":"))
  13439. (cond
  13440. ((not key)
  13441. (setq key
  13442. (if (= (char-after (match-beginning 3)) ?\[)
  13443. "TIMESTAMP_IA" "TIMESTAMP")))
  13444. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13445. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13446. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13447. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13448. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13449. (progn
  13450. (push (cons key string) props)
  13451. ;; no need to search further if match is found
  13452. (throw 'match t))
  13453. (when (or (equal key "CLOCK") (not (assoc key props)))
  13454. (push (cons key string) props)))))))
  13455. (when (memq which '(all standard))
  13456. ;; Get the standard properties, like :PROP: ...
  13457. (setq range (org-get-property-block beg end))
  13458. (when range
  13459. (goto-char (car range))
  13460. (while (re-search-forward
  13461. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13462. (cdr range) t)
  13463. (setq key (org-match-string-no-properties 1)
  13464. value (org-trim (or (org-match-string-no-properties 2) "")))
  13465. (unless (member key excluded)
  13466. (push (cons key (or value "")) props)))))
  13467. (if clocksum
  13468. (push (cons "CLOCKSUM"
  13469. (org-columns-number-to-string (/ (float clocksum) 60.)
  13470. 'add_times))
  13471. props))
  13472. (if clocksumt
  13473. (push (cons "CLOCKSUM_T"
  13474. (org-columns-number-to-string (/ (float clocksumt) 60.)
  13475. 'add_times))
  13476. props))
  13477. (unless (assoc "CATEGORY" props)
  13478. (push (cons "CATEGORY" (org-get-category)) props))
  13479. (append sum-props (nreverse props)))))))
  13480. (defun org-entry-get (pom property &optional inherit literal-nil)
  13481. "Get value of PROPERTY for entry or content at point-or-marker POM.
  13482. If INHERIT is non-nil and the entry does not have the property,
  13483. then also check higher levels of the hierarchy.
  13484. If INHERIT is the symbol `selective', use inheritance only if the setting
  13485. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13486. If the property is present but empty, the return value is the empty string.
  13487. If the property is not present at all, nil is returned.
  13488. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13489. do not interpret it as the list atom nil. This is used for inheritance
  13490. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13491. (org-with-point-at pom
  13492. (if (and inherit (if (eq inherit 'selective)
  13493. (org-property-inherit-p property)
  13494. t))
  13495. (org-entry-get-with-inheritance property literal-nil)
  13496. (if (member property org-special-properties)
  13497. ;; We need a special property. Use `org-entry-properties' to
  13498. ;; retrieve it, but specify the wanted property
  13499. (cdr (assoc property (org-entry-properties nil 'special property)))
  13500. (let ((range (org-get-property-block)))
  13501. (when (and range (not (eq (car range) (cdr range))))
  13502. (let* ((props (list (or (assoc property org-file-properties)
  13503. (assoc property org-global-properties)
  13504. (assoc property org-global-properties-fixed))))
  13505. (ap (lambda (key)
  13506. (when (re-search-forward
  13507. (org-re-property key) (cdr range) t)
  13508. (setq props
  13509. (org-update-property-plist
  13510. key
  13511. (if (match-end 1)
  13512. (org-match-string-no-properties 1) "")
  13513. props)))))
  13514. val)
  13515. (goto-char (car range))
  13516. (funcall ap property)
  13517. (goto-char (car range))
  13518. (while (funcall ap (concat property "+")))
  13519. (setq val (cdr (assoc property props)))
  13520. (when val (if literal-nil val (org-not-nil val))))))))))
  13521. (defun org-property-or-variable-value (var &optional inherit)
  13522. "Check if there is a property fixing the value of VAR.
  13523. If yes, return this value. If not, return the current value of the variable."
  13524. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13525. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13526. (read prop)
  13527. (symbol-value var))))
  13528. (defun org-entry-delete (pom property)
  13529. "Delete the property PROPERTY from entry at point-or-marker POM."
  13530. (org-with-point-at pom
  13531. (if (member property org-special-properties)
  13532. nil ; cannot delete these properties.
  13533. (let ((range (org-get-property-block)))
  13534. (if (and range
  13535. (goto-char (car range))
  13536. (re-search-forward
  13537. (org-re-property property)
  13538. (cdr range) t))
  13539. (progn
  13540. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13541. t)
  13542. nil)))))
  13543. ;; Multi-values properties are properties that contain multiple values
  13544. ;; These values are assumed to be single words, separated by whitespace.
  13545. (defun org-entry-add-to-multivalued-property (pom property value)
  13546. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13547. (let* ((old (org-entry-get pom property))
  13548. (values (and old (org-split-string old "[ \t]"))))
  13549. (setq value (org-entry-protect-space value))
  13550. (unless (member value values)
  13551. (setq values (cons value values))
  13552. (org-entry-put pom property
  13553. (mapconcat 'identity values " ")))))
  13554. (defun org-entry-remove-from-multivalued-property (pom property value)
  13555. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13556. (let* ((old (org-entry-get pom property))
  13557. (values (and old (org-split-string old "[ \t]"))))
  13558. (setq value (org-entry-protect-space value))
  13559. (when (member value values)
  13560. (setq values (delete value values))
  13561. (org-entry-put pom property
  13562. (mapconcat 'identity values " ")))))
  13563. (defun org-entry-member-in-multivalued-property (pom property value)
  13564. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13565. (let* ((old (org-entry-get pom property))
  13566. (values (and old (org-split-string old "[ \t]"))))
  13567. (setq value (org-entry-protect-space value))
  13568. (member value values)))
  13569. (defun org-entry-get-multivalued-property (pom property)
  13570. "Return a list of values in a multivalued property."
  13571. (let* ((value (org-entry-get pom property))
  13572. (values (and value (org-split-string value "[ \t]"))))
  13573. (mapcar 'org-entry-restore-space values)))
  13574. (defun org-entry-put-multivalued-property (pom property &rest values)
  13575. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13576. VALUES should be a list of strings. Spaces will be protected."
  13577. (org-entry-put pom property
  13578. (mapconcat 'org-entry-protect-space values " "))
  13579. (let* ((value (org-entry-get pom property))
  13580. (values (and value (org-split-string value "[ \t]"))))
  13581. (mapcar 'org-entry-restore-space values)))
  13582. (defun org-entry-protect-space (s)
  13583. "Protect spaces and newline in string S."
  13584. (while (string-match " " s)
  13585. (setq s (replace-match "%20" t t s)))
  13586. (while (string-match "\n" s)
  13587. (setq s (replace-match "%0A" t t s)))
  13588. s)
  13589. (defun org-entry-restore-space (s)
  13590. "Restore spaces and newline in string S."
  13591. (while (string-match "%20" s)
  13592. (setq s (replace-match " " t t s)))
  13593. (while (string-match "%0A" s)
  13594. (setq s (replace-match "\n" t t s)))
  13595. s)
  13596. (defvar org-entry-property-inherited-from (make-marker)
  13597. "Marker pointing to the entry from where a property was inherited.
  13598. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13599. location of the entry where the inheritance search matched. If there was
  13600. no match, the marker will point nowhere.
  13601. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13602. is set.")
  13603. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13604. "Get PROPERTY of entry or content at point, search higher levels if needed.
  13605. The search will stop at the first ancestor which has the property defined.
  13606. If the value found is \"nil\", return nil to show that the property
  13607. should be considered as undefined (this is the meaning of nil here).
  13608. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13609. (move-marker org-entry-property-inherited-from nil)
  13610. (let (tmp)
  13611. (save-excursion
  13612. (save-restriction
  13613. (widen)
  13614. (catch 'ex
  13615. (while t
  13616. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13617. (or (ignore-errors (org-back-to-heading t))
  13618. (goto-char (point-min)))
  13619. (move-marker org-entry-property-inherited-from (point))
  13620. (throw 'ex tmp))
  13621. (or (ignore-errors (org-up-heading-safe))
  13622. (throw 'ex nil))))))
  13623. (setq tmp (or tmp
  13624. (cdr (assoc property org-file-properties))
  13625. (cdr (assoc property org-global-properties))
  13626. (cdr (assoc property org-global-properties-fixed))))
  13627. (if literal-nil tmp (org-not-nil tmp))))
  13628. (defvar org-property-changed-functions nil
  13629. "Hook called when the value of a property has changed.
  13630. Each hook function should accept two arguments, the name of the property
  13631. and the new value.")
  13632. (defun org-entry-put (pom property value)
  13633. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13634. (org-with-point-at pom
  13635. (org-back-to-heading t)
  13636. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13637. range)
  13638. (cond
  13639. ((equal property "TODO")
  13640. (when (and (stringp value) (string-match "\\S-" value)
  13641. (not (member value org-todo-keywords-1)))
  13642. (error "\"%s\" is not a valid TODO state" value))
  13643. (if (or (not value)
  13644. (not (string-match "\\S-" value)))
  13645. (setq value 'none))
  13646. (org-todo value)
  13647. (org-set-tags nil 'align))
  13648. ((equal property "PRIORITY")
  13649. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13650. (string-to-char value) ?\ ))
  13651. (org-set-tags nil 'align))
  13652. ((equal property "CLOCKSUM")
  13653. (if (not (re-search-forward
  13654. (concat org-clock-string ".*\\]--\\(\\[[^]]+\\]\\)") nil t))
  13655. (error "Cannot find a clock log")
  13656. (goto-char (- (match-end 1) 2))
  13657. (cond
  13658. ((eq value 'earlier) (org-timestamp-down))
  13659. ((eq value 'later) (org-timestamp-up)))
  13660. (org-clock-sum-current-item)))
  13661. ((equal property "SCHEDULED")
  13662. (if (re-search-forward org-scheduled-time-regexp end t)
  13663. (cond
  13664. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13665. ((eq value 'later) (org-timestamp-change 1 'day))
  13666. (t (call-interactively 'org-schedule)))
  13667. (call-interactively 'org-schedule)))
  13668. ((equal property "DEADLINE")
  13669. (if (re-search-forward org-deadline-time-regexp end t)
  13670. (cond
  13671. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13672. ((eq value 'later) (org-timestamp-change 1 'day))
  13673. (t (call-interactively 'org-deadline)))
  13674. (call-interactively 'org-deadline)))
  13675. ((member property org-special-properties)
  13676. (error "The %s property can not yet be set with `org-entry-put'"
  13677. property))
  13678. (t ; a non-special property
  13679. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13680. (setq range (org-get-property-block beg end 'force))
  13681. (goto-char (car range))
  13682. (if (re-search-forward
  13683. (org-re-property-keyword property) (cdr range) t)
  13684. (progn
  13685. (delete-region (match-beginning 0) (match-end 0))
  13686. (goto-char (match-beginning 0)))
  13687. (goto-char (cdr range))
  13688. (insert "\n")
  13689. (backward-char 1)
  13690. (org-indent-line))
  13691. (insert ":" property ":")
  13692. (and value (insert " " value))
  13693. (org-indent-line)))))
  13694. (run-hook-with-args 'org-property-changed-functions property value)))
  13695. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13696. "Get all property keys in the current buffer.
  13697. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13698. like tags and TODO state.
  13699. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13700. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13701. and others.
  13702. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13703. formats in the current buffer."
  13704. (let (rtn range cfmt s p)
  13705. (save-excursion
  13706. (save-restriction
  13707. (widen)
  13708. (goto-char (point-min))
  13709. (while (re-search-forward org-property-start-re nil t)
  13710. (setq range (org-get-property-block))
  13711. (goto-char (car range))
  13712. (while (re-search-forward
  13713. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13714. (cdr range) t)
  13715. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13716. (outline-next-heading))))
  13717. (when include-specials
  13718. (setq rtn (append org-special-properties rtn)))
  13719. (when include-defaults
  13720. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13721. (add-to-list 'rtn org-effort-property))
  13722. (when include-columns
  13723. (save-excursion
  13724. (save-restriction
  13725. (widen)
  13726. (goto-char (point-min))
  13727. (while (re-search-forward
  13728. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13729. nil t)
  13730. (setq cfmt (match-string 2) s 0)
  13731. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13732. cfmt s)
  13733. (setq s (match-end 0)
  13734. p (match-string 1 cfmt))
  13735. (unless (or (equal p "ITEM")
  13736. (member p org-special-properties))
  13737. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13738. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13739. (defun org-property-values (key)
  13740. "Return a list of all values of property KEY in the current buffer."
  13741. (save-excursion
  13742. (save-restriction
  13743. (widen)
  13744. (goto-char (point-min))
  13745. (let ((re (org-re-property key))
  13746. values)
  13747. (while (re-search-forward re nil t)
  13748. (add-to-list 'values (org-trim (match-string 1))))
  13749. (delete "" values)))))
  13750. (defun org-insert-property-drawer ()
  13751. "Insert a property drawer into the current entry."
  13752. (org-back-to-heading t)
  13753. (looking-at org-outline-regexp)
  13754. (let ((indent (if org-adapt-indentation
  13755. (- (match-end 0) (match-beginning 0))
  13756. 0))
  13757. (beg (point))
  13758. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13759. end hiddenp)
  13760. (outline-next-heading)
  13761. (setq end (point))
  13762. (goto-char beg)
  13763. (while (re-search-forward re end t))
  13764. (setq hiddenp (outline-invisible-p))
  13765. (end-of-line 1)
  13766. (and (equal (char-after) ?\n) (forward-char 1))
  13767. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13768. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13769. ;; just skip this line
  13770. (beginning-of-line 2)
  13771. ;; Drawer start, find the end
  13772. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13773. (beginning-of-line 1)))
  13774. (org-skip-over-state-notes)
  13775. (skip-chars-backward " \t\n\r")
  13776. (if (eq (char-before) ?*) (forward-char 1))
  13777. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13778. (beginning-of-line 0)
  13779. (org-indent-to-column indent)
  13780. (beginning-of-line 2)
  13781. (org-indent-to-column indent)
  13782. (beginning-of-line 0)
  13783. (if hiddenp
  13784. (save-excursion
  13785. (org-back-to-heading t)
  13786. (hide-entry))
  13787. (org-flag-drawer t))))
  13788. (defun org-insert-drawer (&optional arg drawer)
  13789. "Insert a drawer at point.
  13790. Optional argument DRAWER, when non-nil, is a string representing
  13791. drawer's name. Otherwise, the user is prompted for a name.
  13792. If a region is active, insert the drawer around that region
  13793. instead.
  13794. Point is left between drawer's boundaries."
  13795. (interactive "P")
  13796. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13797. "LOGBOOK"))
  13798. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13799. ;; internally by Org. They are meant to be inserted
  13800. ;; automatically.
  13801. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13802. ;; Remove system drawers from list. Note: For some reason,
  13803. ;; `org-completing-read' ignores the predicate while
  13804. ;; `completing-read' handles it fine.
  13805. (drawer (if arg "PROPERTIES"
  13806. (or drawer
  13807. (completing-read
  13808. "Drawer: " org-drawers
  13809. (lambda (d) (not (member d system-drawers))))))))
  13810. (cond
  13811. ;; With C-u, fall back on `org-insert-property-drawer'
  13812. (arg (org-insert-property-drawer))
  13813. ;; With an active region, insert a drawer at point.
  13814. ((not (org-region-active-p))
  13815. (progn
  13816. (unless (bolp) (insert "\n"))
  13817. (insert (format ":%s:\n\n:END:\n" drawer))
  13818. (forward-line -2)))
  13819. ;; Otherwise, insert the drawer at point
  13820. (t
  13821. (let ((rbeg (region-beginning))
  13822. (rend (copy-marker (region-end))))
  13823. (unwind-protect
  13824. (progn
  13825. (goto-char rbeg)
  13826. (beginning-of-line)
  13827. (when (save-excursion
  13828. (re-search-forward org-outline-regexp-bol rend t))
  13829. (error "Drawers cannot contain headlines"))
  13830. ;; Position point at the beginning of the first
  13831. ;; non-blank line in region. Insert drawer's opening
  13832. ;; there, then indent it.
  13833. (org-skip-whitespace)
  13834. (beginning-of-line)
  13835. (insert ":" drawer ":\n")
  13836. (forward-line -1)
  13837. (indent-for-tab-command)
  13838. ;; Move point to the beginning of the first blank line
  13839. ;; after the last non-blank line in region. Insert
  13840. ;; drawer's closing, then indent it.
  13841. (goto-char rend)
  13842. (skip-chars-backward " \r\t\n")
  13843. (insert "\n:END:")
  13844. (deactivate-mark t)
  13845. (indent-for-tab-command)
  13846. (unless (eolp) (insert "\n")))
  13847. ;; Clear marker, whatever the outcome of insertion is.
  13848. (set-marker rend nil)))))))
  13849. (defvar org-property-set-functions-alist nil
  13850. "Property set function alist.
  13851. Each entry should have the following format:
  13852. (PROPERTY . READ-FUNCTION)
  13853. The read function will be called with the same argument as
  13854. `org-completing-read'.")
  13855. (defun org-set-property-function (property)
  13856. "Get the function that should be used to set PROPERTY.
  13857. This is computed according to `org-property-set-functions-alist'."
  13858. (or (cdr (assoc property org-property-set-functions-alist))
  13859. 'org-completing-read))
  13860. (defun org-read-property-value (property)
  13861. "Read PROPERTY value from user."
  13862. (let* ((completion-ignore-case t)
  13863. (allowed (org-property-get-allowed-values nil property 'table))
  13864. (cur (org-entry-get nil property))
  13865. (prompt (concat property " value"
  13866. (if (and cur (string-match "\\S-" cur))
  13867. (concat " [" cur "]") "") ": "))
  13868. (set-function (org-set-property-function property))
  13869. (val (if allowed
  13870. (funcall set-function prompt allowed nil
  13871. (not (get-text-property 0 'org-unrestricted
  13872. (caar allowed))))
  13873. (let (org-completion-use-ido org-completion-use-iswitchb)
  13874. (funcall set-function prompt
  13875. (mapcar 'list (org-property-values property))
  13876. nil nil "" nil cur)))))
  13877. (if (equal val "")
  13878. cur
  13879. val)))
  13880. (defvar org-last-set-property nil)
  13881. (defvar org-last-set-property-value nil)
  13882. (defun org-read-property-name ()
  13883. "Read a property name."
  13884. (let* ((completion-ignore-case t)
  13885. (keys (org-buffer-property-keys nil t t))
  13886. (default-prop (or (save-excursion
  13887. (save-match-data
  13888. (beginning-of-line)
  13889. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13890. (null (string= (match-string 1) "END"))
  13891. (match-string 1))))
  13892. org-last-set-property))
  13893. (property (org-icompleting-read
  13894. (concat "Property"
  13895. (if default-prop (concat " [" default-prop "]") "")
  13896. ": ")
  13897. (mapcar 'list keys)
  13898. nil nil nil nil
  13899. default-prop)))
  13900. (if (member property keys)
  13901. property
  13902. (or (cdr (assoc (downcase property)
  13903. (mapcar (lambda (x) (cons (downcase x) x))
  13904. keys)))
  13905. property))))
  13906. (defun org-set-property-and-value (use-last)
  13907. "Allow to set [PROPERTY]: [value] direction from prompt.
  13908. When use-default, don't even ask, just use the last
  13909. \"[PROPERTY]: [value]\" string from the history."
  13910. (interactive "P")
  13911. (let* ((completion-ignore-case t)
  13912. (pv (or (and use-last org-last-set-property-value)
  13913. (org-completing-read
  13914. "Enter a \"[Property]: [value]\" pair: "
  13915. nil nil nil nil nil
  13916. org-last-set-property-value)))
  13917. prop val)
  13918. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  13919. (setq prop (match-string 1 pv)
  13920. val (match-string 2 pv))
  13921. (org-set-property prop val))))
  13922. (defun org-set-property (property value)
  13923. "In the current entry, set PROPERTY to VALUE.
  13924. When called interactively, this will prompt for a property name, offering
  13925. completion on existing and default properties. And then it will prompt
  13926. for a value, offering completion either on allowed values (via an inherited
  13927. xxx_ALL property) or on existing values in other instances of this property
  13928. in the current file."
  13929. (interactive (list nil nil))
  13930. (let* ((property (or property (org-read-property-name)))
  13931. (value (or value (org-read-property-value property)))
  13932. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13933. (setq org-last-set-property property)
  13934. (setq org-last-set-property-value (concat property ": " value))
  13935. ;; Possibly postprocess the inserted value:
  13936. (when fn (setq value (funcall fn value)))
  13937. (unless (equal (org-entry-get nil property) value)
  13938. (org-entry-put nil property value))))
  13939. (defun org-delete-property (property)
  13940. "In the current entry, delete PROPERTY."
  13941. (interactive
  13942. (let* ((completion-ignore-case t)
  13943. (prop (org-icompleting-read "Property: "
  13944. (org-entry-properties nil 'standard))))
  13945. (list prop)))
  13946. (message "Property %s %s" property
  13947. (if (org-entry-delete nil property)
  13948. "deleted"
  13949. "was not present in the entry")))
  13950. (defun org-delete-property-globally (property)
  13951. "Remove PROPERTY globally, from all entries."
  13952. (interactive
  13953. (let* ((completion-ignore-case t)
  13954. (prop (org-icompleting-read
  13955. "Globally remove property: "
  13956. (mapcar 'list (org-buffer-property-keys)))))
  13957. (list prop)))
  13958. (save-excursion
  13959. (save-restriction
  13960. (widen)
  13961. (goto-char (point-min))
  13962. (let ((cnt 0))
  13963. (while (re-search-forward
  13964. (org-re-property property)
  13965. nil t)
  13966. (setq cnt (1+ cnt))
  13967. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13968. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13969. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13970. (defun org-compute-property-at-point ()
  13971. "Compute the property at point.
  13972. This looks for an enclosing column format, extracts the operator and
  13973. then applies it to the property in the column format's scope."
  13974. (interactive)
  13975. (unless (org-at-property-p)
  13976. (error "Not at a property"))
  13977. (let ((prop (org-match-string-no-properties 2)))
  13978. (org-columns-get-format-and-top-level)
  13979. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13980. (error "No operator defined for property %s" prop))
  13981. (org-columns-compute prop)))
  13982. (defvar org-property-allowed-value-functions nil
  13983. "Hook for functions supplying allowed values for a specific property.
  13984. The functions must take a single argument, the name of the property, and
  13985. return a flat list of allowed values. If \":ETC\" is one of
  13986. the values, this means that these values are intended as defaults for
  13987. completion, but that other values should be allowed too.
  13988. The functions must return nil if they are not responsible for this
  13989. property.")
  13990. (defun org-property-get-allowed-values (pom property &optional table)
  13991. "Get allowed values for the property PROPERTY.
  13992. When TABLE is non-nil, return an alist that can directly be used for
  13993. completion."
  13994. (let (vals)
  13995. (cond
  13996. ((equal property "TODO")
  13997. (setq vals (org-with-point-at pom
  13998. (append org-todo-keywords-1 '("")))))
  13999. ((equal property "PRIORITY")
  14000. (let ((n org-lowest-priority))
  14001. (while (>= n org-highest-priority)
  14002. (push (char-to-string n) vals)
  14003. (setq n (1- n)))))
  14004. ((member property org-special-properties))
  14005. ((setq vals (run-hook-with-args-until-success
  14006. 'org-property-allowed-value-functions property)))
  14007. (t
  14008. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14009. (when (and vals (string-match "\\S-" vals))
  14010. (setq vals (car (read-from-string (concat "(" vals ")"))))
  14011. (setq vals (mapcar (lambda (x)
  14012. (cond ((stringp x) x)
  14013. ((numberp x) (number-to-string x))
  14014. ((symbolp x) (symbol-name x))
  14015. (t "???")))
  14016. vals)))))
  14017. (when (member ":ETC" vals)
  14018. (setq vals (remove ":ETC" vals))
  14019. (org-add-props (car vals) '(org-unrestricted t)))
  14020. (if table (mapcar 'list vals) vals)))
  14021. (defun org-property-previous-allowed-value (&optional previous)
  14022. "Switch to the next allowed value for this property."
  14023. (interactive)
  14024. (org-property-next-allowed-value t))
  14025. (defun org-property-next-allowed-value (&optional previous)
  14026. "Switch to the next allowed value for this property."
  14027. (interactive)
  14028. (unless (org-at-property-p)
  14029. (error "Not at a property"))
  14030. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  14031. (key (match-string 2))
  14032. (value (match-string 3))
  14033. (allowed (or (org-property-get-allowed-values (point) key)
  14034. (and (member value '("[ ]" "[-]" "[X]"))
  14035. '("[ ]" "[X]"))))
  14036. nval)
  14037. (unless allowed
  14038. (error "Allowed values for this property have not been defined"))
  14039. (if previous (setq allowed (reverse allowed)))
  14040. (if (member value allowed)
  14041. (setq nval (car (cdr (member value allowed)))))
  14042. (setq nval (or nval (car allowed)))
  14043. (if (equal nval value)
  14044. (error "Only one allowed value for this property"))
  14045. (org-at-property-p)
  14046. (replace-match (concat " :" key ": " nval) t t)
  14047. (org-indent-line)
  14048. (beginning-of-line 1)
  14049. (skip-chars-forward " \t")
  14050. (when (equal prop org-effort-property)
  14051. (save-excursion
  14052. (org-back-to-heading t)
  14053. (put-text-property (point-at-bol) (point-at-eol) 'org-effort nval)))
  14054. (run-hook-with-args 'org-property-changed-functions key nval)))
  14055. (defun org-find-olp (path &optional this-buffer)
  14056. "Return a marker pointing to the entry at outline path OLP.
  14057. If anything goes wrong, throw an error.
  14058. You can wrap this call to catch the error like this:
  14059. (condition-case msg
  14060. (org-mobile-locate-entry (match-string 4))
  14061. (error (nth 1 msg)))
  14062. The return value will then be either a string with the error message,
  14063. or a marker if everything is OK.
  14064. If THIS-BUFFER is set, the outline path does not contain a file,
  14065. only headings."
  14066. (let* ((file (if this-buffer buffer-file-name (pop path)))
  14067. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  14068. (level 1)
  14069. (lmin 1)
  14070. (lmax 1)
  14071. limit re end found pos heading cnt flevel)
  14072. (unless buffer (error "File not found :%s" file))
  14073. (with-current-buffer buffer
  14074. (save-excursion
  14075. (save-restriction
  14076. (widen)
  14077. (setq limit (point-max))
  14078. (goto-char (point-min))
  14079. (while (setq heading (pop path))
  14080. (setq re (format org-complex-heading-regexp-format
  14081. (regexp-quote heading)))
  14082. (setq cnt 0 pos (point))
  14083. (while (re-search-forward re end t)
  14084. (setq level (- (match-end 1) (match-beginning 1)))
  14085. (if (and (>= level lmin) (<= level lmax))
  14086. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  14087. (when (= cnt 0) (error "Heading not found on level %d: %s"
  14088. lmax heading))
  14089. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  14090. lmax heading))
  14091. (goto-char found)
  14092. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  14093. (setq end (save-excursion (org-end-of-subtree t t))))
  14094. (when (org-at-heading-p)
  14095. (point-marker)))))))
  14096. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  14097. "Find node HEADING in BUFFER.
  14098. Return a marker to the heading if it was found, or nil if not.
  14099. If POS-ONLY is set, return just the position instead of a marker.
  14100. The heading text must match exact, but it may have a TODO keyword,
  14101. a priority cookie and tags in the standard locations."
  14102. (with-current-buffer (or buffer (current-buffer))
  14103. (save-excursion
  14104. (save-restriction
  14105. (widen)
  14106. (goto-char (point-min))
  14107. (let (case-fold-search)
  14108. (if (re-search-forward
  14109. (format org-complex-heading-regexp-format
  14110. (regexp-quote heading)) nil t)
  14111. (if pos-only
  14112. (match-beginning 0)
  14113. (move-marker (make-marker) (match-beginning 0)))))))))
  14114. (defun org-find-exact-heading-in-directory (heading &optional dir)
  14115. "Find Org node headline HEADING in all .org files in directory DIR.
  14116. When the target headline is found, return a marker to this location."
  14117. (let ((files (directory-files (or dir default-directory)
  14118. nil "\\`[^.#].*\\.org\\'"))
  14119. file visiting m buffer)
  14120. (catch 'found
  14121. (while (setq file (pop files))
  14122. (message "trying %s" file)
  14123. (setq visiting (org-find-base-buffer-visiting file))
  14124. (setq buffer (or visiting (find-file-noselect file)))
  14125. (setq m (org-find-exact-headline-in-buffer
  14126. heading buffer))
  14127. (when (and (not m) (not visiting)) (kill-buffer buffer))
  14128. (and m (throw 'found m))))))
  14129. (defun org-find-entry-with-id (ident)
  14130. "Locate the entry that contains the ID property with exact value IDENT.
  14131. IDENT can be a string, a symbol or a number, this function will search for
  14132. the string representation of it.
  14133. Return the position where this entry starts, or nil if there is no such entry."
  14134. (interactive "sID: ")
  14135. (let ((id (cond
  14136. ((stringp ident) ident)
  14137. ((symbol-name ident) (symbol-name ident))
  14138. ((numberp ident) (number-to-string ident))
  14139. (t (error "IDENT %s must be a string, symbol or number" ident))))
  14140. (case-fold-search nil))
  14141. (save-excursion
  14142. (save-restriction
  14143. (widen)
  14144. (goto-char (point-min))
  14145. (when (re-search-forward
  14146. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  14147. nil t)
  14148. (org-back-to-heading t)
  14149. (point))))))
  14150. ;;;; Timestamps
  14151. (defvar org-last-changed-timestamp nil)
  14152. (defvar org-last-inserted-timestamp nil
  14153. "The last time stamp inserted with `org-insert-time-stamp'.")
  14154. (defvar org-time-was-given) ; dynamically scoped parameter
  14155. (defvar org-end-time-was-given) ; dynamically scoped parameter
  14156. (defvar org-ts-what) ; dynamically scoped parameter
  14157. (defun org-time-stamp (arg &optional inactive)
  14158. "Prompt for a date/time and insert a time stamp.
  14159. If the user specifies a time like HH:MM or if this command is
  14160. called with at least one prefix argument, the time stamp contains
  14161. the date and the time. Otherwise, only the date is be included.
  14162. All parts of a date not specified by the user is filled in from
  14163. the current date/time. So if you just press return without
  14164. typing anything, the time stamp will represent the current
  14165. date/time.
  14166. If there is already a timestamp at the cursor, it will be
  14167. modified.
  14168. With two universal prefix arguments, insert an active timestamp
  14169. with the current time without prompting the user."
  14170. (interactive "P")
  14171. (let* ((ts nil)
  14172. (default-time
  14173. ;; Default time is either today, or, when entering a range,
  14174. ;; the range start.
  14175. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  14176. (save-excursion
  14177. (re-search-backward
  14178. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  14179. (- (point) 20) t)))
  14180. (apply 'encode-time (org-parse-time-string (match-string 1)))
  14181. (current-time)))
  14182. (default-input (and ts (org-get-compact-tod ts)))
  14183. (repeater (save-excursion
  14184. (save-match-data
  14185. (beginning-of-line)
  14186. (when (re-search-forward
  14187. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  14188. (save-excursion (progn (end-of-line) (point))) t)
  14189. (match-string 0)))))
  14190. org-time-was-given org-end-time-was-given time)
  14191. (cond
  14192. ((and (org-at-timestamp-p t)
  14193. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  14194. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  14195. (insert "--")
  14196. (setq time (let ((this-command this-command))
  14197. (org-read-date arg 'totime nil nil
  14198. default-time default-input inactive)))
  14199. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  14200. ((org-at-timestamp-p t)
  14201. (setq time (let ((this-command this-command))
  14202. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14203. (when (org-at-timestamp-p t) ; just to get the match data
  14204. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  14205. (replace-match "")
  14206. (setq org-last-changed-timestamp
  14207. (org-insert-time-stamp
  14208. time (or org-time-was-given arg)
  14209. inactive nil nil (list org-end-time-was-given)))
  14210. (when repeater (goto-char (1- (point))) (insert " " repeater)
  14211. (setq org-last-changed-timestamp
  14212. (concat (substring org-last-inserted-timestamp 0 -1)
  14213. " " repeater ">"))))
  14214. (message "Timestamp updated"))
  14215. ((equal arg '(16))
  14216. (org-insert-time-stamp (current-time) t))
  14217. (t
  14218. (setq time (let ((this-command this-command))
  14219. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  14220. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  14221. nil nil (list org-end-time-was-given))))))
  14222. ;; FIXME: can we use this for something else, like computing time differences?
  14223. (defun org-get-compact-tod (s)
  14224. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  14225. (let* ((t1 (match-string 1 s))
  14226. (h1 (string-to-number (match-string 2 s)))
  14227. (m1 (string-to-number (match-string 3 s)))
  14228. (t2 (and (match-end 4) (match-string 5 s)))
  14229. (h2 (and t2 (string-to-number (match-string 6 s))))
  14230. (m2 (and t2 (string-to-number (match-string 7 s))))
  14231. dh dm)
  14232. (if (not t2)
  14233. t1
  14234. (setq dh (- h2 h1) dm (- m2 m1))
  14235. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  14236. (concat t1 "+" (number-to-string dh)
  14237. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  14238. (defun org-time-stamp-inactive (&optional arg)
  14239. "Insert an inactive time stamp.
  14240. An inactive time stamp is enclosed in square brackets instead of angle
  14241. brackets. It is inactive in the sense that it does not trigger agenda entries,
  14242. does not link to the calendar and cannot be changed with the S-cursor keys.
  14243. So these are more for recording a certain time/date."
  14244. (interactive "P")
  14245. (org-time-stamp arg 'inactive))
  14246. (defvar org-date-ovl (make-overlay 1 1))
  14247. (overlay-put org-date-ovl 'face 'org-date-selected)
  14248. (org-detach-overlay org-date-ovl)
  14249. (defvar org-ans1) ; dynamically scoped parameter
  14250. (defvar org-ans2) ; dynamically scoped parameter
  14251. (defvar org-plain-time-of-day-regexp) ; defined below
  14252. (defvar org-overriding-default-time nil) ; dynamically scoped
  14253. (defvar org-read-date-overlay nil)
  14254. (defvar org-dcst nil) ; dynamically scoped
  14255. (defvar org-read-date-history nil)
  14256. (defvar org-read-date-final-answer nil)
  14257. (defvar org-read-date-analyze-futurep nil)
  14258. (defvar org-read-date-analyze-forced-year nil)
  14259. (defvar org-read-date-inactive)
  14260. (defvar org-read-date-minibuffer-local-map
  14261. (let ((map (make-sparse-keymap)))
  14262. (set-keymap-parent map minibuffer-local-map)
  14263. (org-defkey map [(meta shift left)]
  14264. (lambda () (interactive)
  14265. (org-eval-in-calendar '(calendar-backward-month 1))))
  14266. (org-defkey map [(meta shift right)]
  14267. (lambda () (interactive)
  14268. (org-eval-in-calendar '(calendar-forward-month 1))))
  14269. (org-defkey map [(meta shift up)]
  14270. (lambda () (interactive)
  14271. (org-eval-in-calendar '(calendar-backward-year 1))))
  14272. (org-defkey map [(meta shift down)]
  14273. (lambda () (interactive)
  14274. (org-eval-in-calendar '(calendar-forward-year 1))))
  14275. (org-defkey map [?\e (shift left)]
  14276. (lambda () (interactive)
  14277. (org-eval-in-calendar '(calendar-backward-month 1))))
  14278. (org-defkey map [?\e (shift right)]
  14279. (lambda () (interactive)
  14280. (org-eval-in-calendar '(calendar-forward-month 1))))
  14281. (org-defkey map [?\e (shift up)]
  14282. (lambda () (interactive)
  14283. (org-eval-in-calendar '(calendar-backward-year 1))))
  14284. (org-defkey map [?\e (shift down)]
  14285. (lambda () (interactive)
  14286. (org-eval-in-calendar '(calendar-forward-year 1))))
  14287. (org-defkey map [(shift up)]
  14288. (lambda () (interactive)
  14289. (org-eval-in-calendar '(calendar-backward-week 1))))
  14290. (org-defkey map [(shift down)]
  14291. (lambda () (interactive)
  14292. (org-eval-in-calendar '(calendar-forward-week 1))))
  14293. (org-defkey map [(shift left)]
  14294. (lambda () (interactive)
  14295. (org-eval-in-calendar '(calendar-backward-day 1))))
  14296. (org-defkey map [(shift right)]
  14297. (lambda () (interactive)
  14298. (org-eval-in-calendar '(calendar-forward-day 1))))
  14299. (org-defkey map "?"
  14300. (lambda () (interactive)
  14301. (org-eval-in-calendar '(diary-view-entries))
  14302. (message "")))
  14303. (org-defkey map ">"
  14304. (lambda () (interactive)
  14305. (org-eval-in-calendar '(scroll-calendar-left 1))))
  14306. (org-defkey map "<"
  14307. (lambda () (interactive)
  14308. (org-eval-in-calendar '(scroll-calendar-right 1))))
  14309. (org-defkey map "\C-v"
  14310. (lambda () (interactive)
  14311. (org-eval-in-calendar
  14312. '(calendar-scroll-left-three-months 1))))
  14313. (org-defkey map "\M-v"
  14314. (lambda () (interactive)
  14315. (org-eval-in-calendar
  14316. '(calendar-scroll-right-three-months 1))))
  14317. map)
  14318. "Keymap for minibuffer commands when using `org-read-date'.")
  14319. (defun org-read-date (&optional org-with-time to-time from-string prompt
  14320. default-time default-input inactive)
  14321. "Read a date, possibly a time, and make things smooth for the user.
  14322. The prompt will suggest to enter an ISO date, but you can also enter anything
  14323. which will at least partially be understood by `parse-time-string'.
  14324. Unrecognized parts of the date will default to the current day, month, year,
  14325. hour and minute. If this command is called to replace a timestamp at point,
  14326. or to enter the second timestamp of a range, the default time is taken
  14327. from the existing stamp. Furthermore, the command prefers the future,
  14328. so if you are giving a date where the year is not given, and the day-month
  14329. combination is already past in the current year, it will assume you
  14330. mean next year. For details, see the manual. A few examples:
  14331. 3-2-5 --> 2003-02-05
  14332. feb 15 --> currentyear-02-15
  14333. 2/15 --> currentyear-02-15
  14334. sep 12 9 --> 2009-09-12
  14335. 12:45 --> today 12:45
  14336. 22 sept 0:34 --> currentyear-09-22 0:34
  14337. 12 --> currentyear-currentmonth-12
  14338. Fri --> nearest Friday (today or later)
  14339. etc.
  14340. Furthermore you can specify a relative date by giving, as the *first* thing
  14341. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  14342. change in days weeks, months, years.
  14343. With a single plus or minus, the date is relative to today. With a double
  14344. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  14345. +4d --> four days from today
  14346. +4 --> same as above
  14347. +2w --> two weeks from today
  14348. ++5 --> five days from default date
  14349. The function understands only English month and weekday abbreviations.
  14350. While prompting, a calendar is popped up - you can also select the
  14351. date with the mouse (button 1). The calendar shows a period of three
  14352. months. To scroll it to other months, use the keys `>' and `<'.
  14353. If you don't like the calendar, turn it off with
  14354. \(setq org-read-date-popup-calendar nil)
  14355. With optional argument TO-TIME, the date will immediately be converted
  14356. to an internal time.
  14357. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  14358. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  14359. still enter a time, and this function will inform the calling routine
  14360. about this change. The calling routine may then choose to change the
  14361. format used to insert the time stamp into the buffer to include the time.
  14362. With optional argument FROM-STRING, read from this string instead from
  14363. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  14364. the time/date that is used for everything that is not specified by the
  14365. user."
  14366. (require 'parse-time)
  14367. (let* ((org-time-stamp-rounding-minutes
  14368. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  14369. (org-dcst org-display-custom-times)
  14370. (ct (org-current-time))
  14371. (org-def (or org-overriding-default-time default-time ct))
  14372. (org-defdecode (decode-time org-def))
  14373. (dummy (progn
  14374. (when (< (nth 2 org-defdecode) org-extend-today-until)
  14375. (setcar (nthcdr 2 org-defdecode) -1)
  14376. (setcar (nthcdr 1 org-defdecode) 59)
  14377. (setq org-def (apply 'encode-time org-defdecode)
  14378. org-defdecode (decode-time org-def)))))
  14379. (mouse-autoselect-window nil) ; Don't let the mouse jump
  14380. (calendar-frame-setup nil)
  14381. (calendar-setup nil)
  14382. (calendar-move-hook nil)
  14383. (calendar-view-diary-initially-flag nil)
  14384. (calendar-view-holidays-initially-flag nil)
  14385. (timestr (format-time-string
  14386. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  14387. (prompt (concat (if prompt (concat prompt " ") "")
  14388. (format "Date+time [%s]: " timestr)))
  14389. ans (org-ans0 "") org-ans1 org-ans2 final)
  14390. (cond
  14391. (from-string (setq ans from-string))
  14392. (org-read-date-popup-calendar
  14393. (save-excursion
  14394. (save-window-excursion
  14395. (calendar)
  14396. (org-eval-in-calendar '(setq cursor-type nil) t)
  14397. (unwind-protect
  14398. (progn
  14399. (calendar-forward-day (- (time-to-days org-def)
  14400. (calendar-absolute-from-gregorian
  14401. (calendar-current-date))))
  14402. (org-eval-in-calendar nil t)
  14403. (let* ((old-map (current-local-map))
  14404. (map (copy-keymap calendar-mode-map))
  14405. (minibuffer-local-map
  14406. (copy-keymap org-read-date-minibuffer-local-map)))
  14407. (org-defkey map (kbd "RET") 'org-calendar-select)
  14408. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  14409. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  14410. (unwind-protect
  14411. (progn
  14412. (use-local-map map)
  14413. (setq org-read-date-inactive inactive)
  14414. (add-hook 'post-command-hook 'org-read-date-display)
  14415. (setq org-ans0 (read-string prompt default-input
  14416. 'org-read-date-history nil))
  14417. ;; org-ans0: from prompt
  14418. ;; org-ans1: from mouse click
  14419. ;; org-ans2: from calendar motion
  14420. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  14421. (remove-hook 'post-command-hook 'org-read-date-display)
  14422. (use-local-map old-map)
  14423. (when org-read-date-overlay
  14424. (delete-overlay org-read-date-overlay)
  14425. (setq org-read-date-overlay nil)))))
  14426. (bury-buffer "*Calendar*")))))
  14427. (t ; Naked prompt only
  14428. (unwind-protect
  14429. (setq ans (read-string prompt default-input
  14430. 'org-read-date-history timestr))
  14431. (when org-read-date-overlay
  14432. (delete-overlay org-read-date-overlay)
  14433. (setq org-read-date-overlay nil)))))
  14434. (setq final (org-read-date-analyze ans org-def org-defdecode))
  14435. (when org-read-date-analyze-forced-year
  14436. (message "Year was forced into %s"
  14437. (if org-read-date-force-compatible-dates
  14438. "compatible range (1970-2037)"
  14439. "range representable on this machine"))
  14440. (ding))
  14441. ;; One round trip to get rid of 34th of August and stuff like that....
  14442. (setq final (decode-time (apply 'encode-time final)))
  14443. (setq org-read-date-final-answer ans)
  14444. (if to-time
  14445. (apply 'encode-time final)
  14446. (if (and (boundp 'org-time-was-given) org-time-was-given)
  14447. (format "%04d-%02d-%02d %02d:%02d"
  14448. (nth 5 final) (nth 4 final) (nth 3 final)
  14449. (nth 2 final) (nth 1 final))
  14450. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14451. (defvar org-def)
  14452. (defvar org-defdecode)
  14453. (defvar org-with-time)
  14454. (defun org-read-date-display ()
  14455. "Display the current date prompt interpretation in the minibuffer."
  14456. (when org-read-date-display-live
  14457. (when org-read-date-overlay
  14458. (delete-overlay org-read-date-overlay))
  14459. (when (minibufferp (current-buffer))
  14460. (save-excursion
  14461. (end-of-line 1)
  14462. (while (not (equal (buffer-substring
  14463. (max (point-min) (- (point) 4)) (point))
  14464. " "))
  14465. (insert " ")))
  14466. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14467. " " (or org-ans1 org-ans2)))
  14468. (org-end-time-was-given nil)
  14469. (f (org-read-date-analyze ans org-def org-defdecode))
  14470. (fmts (if org-dcst
  14471. org-time-stamp-custom-formats
  14472. org-time-stamp-formats))
  14473. (fmt (if (or org-with-time
  14474. (and (boundp 'org-time-was-given) org-time-was-given))
  14475. (cdr fmts)
  14476. (car fmts)))
  14477. (txt (format-time-string fmt (apply 'encode-time f)))
  14478. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14479. (txt (concat "=> " txt)))
  14480. (when (and org-end-time-was-given
  14481. (string-match org-plain-time-of-day-regexp txt))
  14482. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14483. org-end-time-was-given
  14484. (substring txt (match-end 0)))))
  14485. (when org-read-date-analyze-futurep
  14486. (setq txt (concat txt " (=>F)")))
  14487. (setq org-read-date-overlay
  14488. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14489. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14490. (defun org-read-date-analyze (ans org-def org-defdecode)
  14491. "Analyze the combined answer of the date prompt."
  14492. ;; FIXME: cleanup and comment
  14493. (let ((nowdecode (decode-time (current-time)))
  14494. delta deltan deltaw deltadef year month day
  14495. hour minute second wday pm h2 m2 tl wday1
  14496. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14497. (setq org-read-date-analyze-futurep nil
  14498. org-read-date-analyze-forced-year nil)
  14499. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14500. (setq ans "+0"))
  14501. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14502. (setq ans (replace-match "" t t ans)
  14503. deltan (car delta)
  14504. deltaw (nth 1 delta)
  14505. deltadef (nth 2 delta)))
  14506. ;; Check if there is an iso week date in there. If yes, store the
  14507. ;; info and postpone interpreting it until the rest of the parsing
  14508. ;; is done.
  14509. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14510. (setq iso-year (if (match-end 1)
  14511. (org-small-year-to-year
  14512. (string-to-number (match-string 1 ans))))
  14513. iso-weekday (if (match-end 3)
  14514. (string-to-number (match-string 3 ans)))
  14515. iso-week (string-to-number (match-string 2 ans)))
  14516. (setq ans (replace-match "" t t ans)))
  14517. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14518. (when (string-match
  14519. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14520. (setq year (if (match-end 2)
  14521. (string-to-number (match-string 2 ans))
  14522. (progn (setq kill-year t)
  14523. (string-to-number (format-time-string "%Y"))))
  14524. month (string-to-number (match-string 3 ans))
  14525. day (string-to-number (match-string 4 ans)))
  14526. (if (< year 100) (setq year (+ 2000 year)))
  14527. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14528. t nil ans)))
  14529. ;; Help matching dotted european dates
  14530. (when (string-match
  14531. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  14532. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  14533. (setq kill-year t)
  14534. (string-to-number (format-time-string "%Y")))
  14535. day (string-to-number (match-string 1 ans))
  14536. month (string-to-number (match-string 2 ans))
  14537. ans (replace-match (format "%04d-%02d-%02d" year month day)
  14538. t nil ans)))
  14539. ;; Help matching american dates, like 5/30 or 5/30/7
  14540. (when (string-match
  14541. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14542. (setq year (if (match-end 4)
  14543. (string-to-number (match-string 4 ans))
  14544. (progn (setq kill-year t)
  14545. (string-to-number (format-time-string "%Y"))))
  14546. month (string-to-number (match-string 1 ans))
  14547. day (string-to-number (match-string 2 ans)))
  14548. (if (< year 100) (setq year (+ 2000 year)))
  14549. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14550. t nil ans)))
  14551. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14552. ;; If there is a time with am/pm, and *no* time without it, we convert
  14553. ;; so that matching will be successful.
  14554. (loop for i from 1 to 2 do ; twice, for end time as well
  14555. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14556. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14557. (setq hour (string-to-number (match-string 1 ans))
  14558. minute (if (match-end 3)
  14559. (string-to-number (match-string 3 ans))
  14560. 0)
  14561. pm (equal ?p
  14562. (string-to-char (downcase (match-string 4 ans)))))
  14563. (if (and (= hour 12) (not pm))
  14564. (setq hour 0)
  14565. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14566. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14567. t t ans))))
  14568. ;; Check if a time range is given as a duration
  14569. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14570. (setq hour (string-to-number (match-string 1 ans))
  14571. h2 (+ hour (string-to-number (match-string 3 ans)))
  14572. minute (string-to-number (match-string 2 ans))
  14573. m2 (+ minute (if (match-end 5) (string-to-number
  14574. (match-string 5 ans))0)))
  14575. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14576. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14577. t t ans)))
  14578. ;; Check if there is a time range
  14579. (when (boundp 'org-end-time-was-given)
  14580. (setq org-time-was-given nil)
  14581. (when (and (string-match org-plain-time-of-day-regexp ans)
  14582. (match-end 8))
  14583. (setq org-end-time-was-given (match-string 8 ans))
  14584. (setq ans (concat (substring ans 0 (match-beginning 7))
  14585. (substring ans (match-end 7))))))
  14586. (setq tl (parse-time-string ans)
  14587. day (or (nth 3 tl) (nth 3 org-defdecode))
  14588. month (or (nth 4 tl)
  14589. (if (and org-read-date-prefer-future
  14590. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14591. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14592. (nth 4 org-defdecode)))
  14593. year (or (and (not kill-year) (nth 5 tl))
  14594. (if (and org-read-date-prefer-future
  14595. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14596. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14597. (nth 5 org-defdecode)))
  14598. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14599. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14600. second (or (nth 0 tl) 0)
  14601. wday (nth 6 tl))
  14602. (when (and (eq org-read-date-prefer-future 'time)
  14603. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14604. (equal day (nth 3 nowdecode))
  14605. (equal month (nth 4 nowdecode))
  14606. (equal year (nth 5 nowdecode))
  14607. (nth 2 tl)
  14608. (or (< (nth 2 tl) (nth 2 nowdecode))
  14609. (and (= (nth 2 tl) (nth 2 nowdecode))
  14610. (nth 1 tl)
  14611. (< (nth 1 tl) (nth 1 nowdecode)))))
  14612. (setq day (1+ day)
  14613. futurep t))
  14614. ;; Special date definitions below
  14615. (cond
  14616. (iso-week
  14617. ;; There was an iso week
  14618. (require 'cal-iso)
  14619. (setq futurep nil)
  14620. (setq year (or iso-year year)
  14621. day (or iso-weekday wday 1)
  14622. wday nil ; to make sure that the trigger below does not match
  14623. iso-date (calendar-gregorian-from-absolute
  14624. (calendar-absolute-from-iso
  14625. (list iso-week day year))))
  14626. ; FIXME: Should we also push ISO weeks into the future?
  14627. ; (when (and org-read-date-prefer-future
  14628. ; (not iso-year)
  14629. ; (< (calendar-absolute-from-gregorian iso-date)
  14630. ; (time-to-days (current-time))))
  14631. ; (setq year (1+ year)
  14632. ; iso-date (calendar-gregorian-from-absolute
  14633. ; (calendar-absolute-from-iso
  14634. ; (list iso-week day year)))))
  14635. (setq month (car iso-date)
  14636. year (nth 2 iso-date)
  14637. day (nth 1 iso-date)))
  14638. (deltan
  14639. (setq futurep nil)
  14640. (unless deltadef
  14641. (let ((now (decode-time (current-time))))
  14642. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14643. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14644. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14645. ((equal deltaw "m") (setq month (+ month deltan)))
  14646. ((equal deltaw "y") (setq year (+ year deltan)))))
  14647. ((and wday (not (nth 3 tl)))
  14648. ;; Weekday was given, but no day, so pick that day in the week
  14649. ;; on or after the derived date.
  14650. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14651. (unless (equal wday wday1)
  14652. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14653. (if (and (boundp 'org-time-was-given)
  14654. (nth 2 tl))
  14655. (setq org-time-was-given t))
  14656. (if (< year 100) (setq year (+ 2000 year)))
  14657. ;; Check of the date is representable
  14658. (if org-read-date-force-compatible-dates
  14659. (progn
  14660. (if (< year 1970)
  14661. (setq year 1970 org-read-date-analyze-forced-year t))
  14662. (if (> year 2037)
  14663. (setq year 2037 org-read-date-analyze-forced-year t)))
  14664. (condition-case nil
  14665. (ignore (encode-time second minute hour day month year))
  14666. (error
  14667. (setq year (nth 5 org-defdecode))
  14668. (setq org-read-date-analyze-forced-year t))))
  14669. (setq org-read-date-analyze-futurep futurep)
  14670. (list second minute hour day month year)))
  14671. (defvar parse-time-weekdays)
  14672. (defun org-read-date-get-relative (s today default)
  14673. "Check string S for special relative date string.
  14674. TODAY and DEFAULT are internal times, for today and for a default.
  14675. Return shift list (N what def-flag)
  14676. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14677. N is the number of WHATs to shift.
  14678. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14679. the DEFAULT date rather than TODAY."
  14680. (require 'parse-time)
  14681. (when (and
  14682. (string-match
  14683. (concat
  14684. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14685. "\\([0-9]+\\)?"
  14686. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14687. "\\([ \t]\\|$\\)") s)
  14688. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14689. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14690. (string-to-char (substring (match-string 1 s) -1))
  14691. ?+))
  14692. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14693. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14694. (what (if (match-end 3) (match-string 3 s) "d"))
  14695. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14696. (date (if rel default today))
  14697. (wday (nth 6 (decode-time date)))
  14698. delta)
  14699. (if wday1
  14700. (progn
  14701. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14702. (if (= dir ?-) (setq delta (- delta 7)))
  14703. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14704. (list delta "d" rel))
  14705. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14706. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14707. "Turn a user-specified date into the internal representation.
  14708. The internal representation needed by the calendar is (month day year).
  14709. This is a wrapper to handle the brain-dead convention in calendar that
  14710. user function argument order change dependent on argument order."
  14711. (if (boundp 'calendar-date-style)
  14712. (cond
  14713. ((eq calendar-date-style 'american)
  14714. (list arg1 arg2 arg3))
  14715. ((eq calendar-date-style 'european)
  14716. (list arg2 arg1 arg3))
  14717. ((eq calendar-date-style 'iso)
  14718. (list arg2 arg3 arg1)))
  14719. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14720. (if (org-bound-and-true-p european-calendar-style)
  14721. (list arg2 arg1 arg3)
  14722. (list arg1 arg2 arg3)))))
  14723. (defun org-eval-in-calendar (form &optional keepdate)
  14724. "Eval FORM in the calendar window and return to current window.
  14725. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14726. otherwise stick to the current value of `org-ans2'."
  14727. (let ((sf (selected-frame))
  14728. (sw (selected-window)))
  14729. (select-window (get-buffer-window "*Calendar*" t))
  14730. (eval form)
  14731. (when (and (not keepdate) (calendar-cursor-to-date))
  14732. (let* ((date (calendar-cursor-to-date))
  14733. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14734. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14735. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14736. (select-window sw)
  14737. (org-select-frame-set-input-focus sf)))
  14738. (defun org-calendar-select ()
  14739. "Return to `org-read-date' with the date currently selected.
  14740. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14741. (interactive)
  14742. (when (calendar-cursor-to-date)
  14743. (let* ((date (calendar-cursor-to-date))
  14744. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14745. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14746. (if (active-minibuffer-window) (exit-minibuffer))))
  14747. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14748. "Insert a date stamp for the date given by the internal TIME.
  14749. WITH-HM means use the stamp format that includes the time of the day.
  14750. INACTIVE means use square brackets instead of angular ones, so that the
  14751. stamp will not contribute to the agenda.
  14752. PRE and POST are optional strings to be inserted before and after the
  14753. stamp.
  14754. The command returns the inserted time stamp."
  14755. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14756. stamp)
  14757. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14758. (insert-before-markers (or pre ""))
  14759. (when (listp extra)
  14760. (setq extra (car extra))
  14761. (if (and (stringp extra)
  14762. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14763. (setq extra (format "-%02d:%02d"
  14764. (string-to-number (match-string 1 extra))
  14765. (string-to-number (match-string 2 extra))))
  14766. (setq extra nil)))
  14767. (when extra
  14768. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14769. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14770. (insert-before-markers (or post ""))
  14771. (setq org-last-inserted-timestamp stamp)))
  14772. (defun org-toggle-time-stamp-overlays ()
  14773. "Toggle the use of custom time stamp formats."
  14774. (interactive)
  14775. (setq org-display-custom-times (not org-display-custom-times))
  14776. (unless org-display-custom-times
  14777. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14778. (while (setq p (next-single-property-change p 'display))
  14779. (if (and (get-text-property p 'display)
  14780. (eq (get-text-property p 'face) 'org-date))
  14781. (remove-text-properties
  14782. p (setq p (next-single-property-change p 'display))
  14783. '(display t))))
  14784. (set-buffer-modified-p bmp)))
  14785. (if (featurep 'xemacs)
  14786. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14787. (org-restart-font-lock)
  14788. (setq org-table-may-need-update t)
  14789. (if org-display-custom-times
  14790. (message "Time stamps are overlaid with custom format")
  14791. (message "Time stamp overlays removed")))
  14792. (defun org-display-custom-time (beg end)
  14793. "Overlay modified time stamp format over timestamp between BEG and END."
  14794. (let* ((ts (buffer-substring beg end))
  14795. t1 w1 with-hm tf time str w2 (off 0))
  14796. (save-match-data
  14797. (setq t1 (org-parse-time-string ts t))
  14798. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14799. (setq off (- (match-end 0) (match-beginning 0)))))
  14800. (setq end (- end off))
  14801. (setq w1 (- end beg)
  14802. with-hm (and (nth 1 t1) (nth 2 t1))
  14803. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14804. time (org-fix-decoded-time t1)
  14805. str (org-add-props
  14806. (format-time-string
  14807. (substring tf 1 -1) (apply 'encode-time time))
  14808. nil 'mouse-face 'highlight)
  14809. w2 (length str))
  14810. (if (not (= w2 w1))
  14811. (add-text-properties (1+ beg) (+ 2 beg)
  14812. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14813. (if (featurep 'xemacs)
  14814. (progn
  14815. (put-text-property beg end 'invisible t)
  14816. (put-text-property beg end 'end-glyph (make-glyph str)))
  14817. (put-text-property beg end 'display str))))
  14818. (defun org-translate-time (string)
  14819. "Translate all timestamps in STRING to custom format.
  14820. But do this only if the variable `org-display-custom-times' is set."
  14821. (when org-display-custom-times
  14822. (save-match-data
  14823. (let* ((start 0)
  14824. (re org-ts-regexp-both)
  14825. t1 with-hm inactive tf time str beg end)
  14826. (while (setq start (string-match re string start))
  14827. (setq beg (match-beginning 0)
  14828. end (match-end 0)
  14829. t1 (save-match-data
  14830. (org-parse-time-string (substring string beg end) t))
  14831. with-hm (and (nth 1 t1) (nth 2 t1))
  14832. inactive (equal (substring string beg (1+ beg)) "[")
  14833. tf (funcall (if with-hm 'cdr 'car)
  14834. org-time-stamp-custom-formats)
  14835. time (org-fix-decoded-time t1)
  14836. str (format-time-string
  14837. (concat
  14838. (if inactive "[" "<") (substring tf 1 -1)
  14839. (if inactive "]" ">"))
  14840. (apply 'encode-time time))
  14841. string (replace-match str t t string)
  14842. start (+ start (length str)))))))
  14843. string)
  14844. (defun org-fix-decoded-time (time)
  14845. "Set 0 instead of nil for the first 6 elements of time.
  14846. Don't touch the rest."
  14847. (let ((n 0))
  14848. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14849. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.3")
  14850. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  14851. "Difference between TIMESTAMP-STRING and now in days.
  14852. If SECONDS is non-nil, return the difference in seconds."
  14853. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  14854. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  14855. (funcall fdiff (current-time)))))
  14856. (defun org-deadline-close (timestamp-string &optional ndays)
  14857. "Is the time in TIMESTAMP-STRING close to the current date?"
  14858. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14859. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  14860. (not (org-entry-is-done-p))))
  14861. (defun org-get-wdays (ts &optional delay zero-delay)
  14862. "Get the deadline lead time appropriate for timestring TS.
  14863. When DELAY is non-nil, get the delay time for scheduled items
  14864. instead of the deadline lead time. When ZERO-DELAY is non-nil
  14865. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  14866. don't try to find the delay cookie in the scheduled timestamp."
  14867. (let ((tv (if delay org-scheduled-delay-days
  14868. org-deadline-warning-days)))
  14869. (cond
  14870. ((or (and delay (< tv 0))
  14871. (and delay zero-delay (<= tv 0))
  14872. (and (not delay) (<= tv 0)))
  14873. ;; Enforce this value no matter what
  14874. (- tv))
  14875. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14876. ;; lead time is specified.
  14877. (floor (* (string-to-number (match-string 1 ts))
  14878. (cdr (assoc (match-string 2 ts)
  14879. '(("d" . 1) ("w" . 7)
  14880. ("m" . 30.4) ("y" . 365.25)
  14881. ("h" . 0.041667)))))))
  14882. ;; go for the default.
  14883. (t tv))))
  14884. (defun org-calendar-select-mouse (ev)
  14885. "Return to `org-read-date' with the date currently selected.
  14886. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14887. (interactive "e")
  14888. (mouse-set-point ev)
  14889. (when (calendar-cursor-to-date)
  14890. (let* ((date (calendar-cursor-to-date))
  14891. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14892. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14893. (if (active-minibuffer-window) (exit-minibuffer))))
  14894. (defun org-check-deadlines (ndays)
  14895. "Check if there are any deadlines due or past due.
  14896. A deadline is considered due if it happens within `org-deadline-warning-days'
  14897. days from today's date. If the deadline appears in an entry marked DONE,
  14898. it is not shown. The prefix arg NDAYS can be used to test that many
  14899. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14900. (interactive "P")
  14901. (let* ((org-warn-days
  14902. (cond
  14903. ((equal ndays '(4)) 100000)
  14904. (ndays (prefix-numeric-value ndays))
  14905. (t (abs org-deadline-warning-days))))
  14906. (case-fold-search nil)
  14907. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14908. (callback
  14909. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14910. (message "%d deadlines past-due or due within %d days"
  14911. (org-occur regexp nil callback)
  14912. org-warn-days)))
  14913. (defsubst org-re-timestamp (type)
  14914. "Return a regexp for timestamp TYPE.
  14915. Allowed values for TYPE are:
  14916. all: all timestamps
  14917. active: only active timestamps (<...>)
  14918. inactive: only inactive timestamps ([...])
  14919. scheduled: only scheduled timestamps
  14920. deadline: only deadline timestamps
  14921. When TYPE is nil, fall back on returning a regexp that matches
  14922. both scheduled and deadline timestamps."
  14923. (cond ((eq type 'all) "\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\(?: +[^]+0-9> \n -]+\\)?\\(?: +[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\)")
  14924. ((eq type 'active) org-ts-regexp)
  14925. ((eq type 'inactive) "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^ \n>]*?\\)\\]")
  14926. ((eq type 'scheduled) (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>"))
  14927. ((eq type 'deadline) (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14928. ((eq type 'scheduled-or-deadline)
  14929. (concat "\\<\\(?:" org-deadline-string "\\|" org-scheduled-string "\\) *<\\([^>]+\\)>"))))
  14930. (defun org-check-before-date (date)
  14931. "Check if there are deadlines or scheduled entries before DATE."
  14932. (interactive (list (org-read-date)))
  14933. (let ((case-fold-search nil)
  14934. (regexp (org-re-timestamp org-ts-type))
  14935. (callback
  14936. (lambda () (time-less-p
  14937. (org-time-string-to-time (match-string 1))
  14938. (org-time-string-to-time date)))))
  14939. (message "%d entries before %s"
  14940. (org-occur regexp nil callback) date)))
  14941. (defun org-check-after-date (date)
  14942. "Check if there are deadlines or scheduled entries after DATE."
  14943. (interactive (list (org-read-date)))
  14944. (let ((case-fold-search nil)
  14945. (regexp (org-re-timestamp org-ts-type))
  14946. (callback
  14947. (lambda () (not
  14948. (time-less-p
  14949. (org-time-string-to-time (match-string 1))
  14950. (org-time-string-to-time date))))))
  14951. (message "%d entries after %s"
  14952. (org-occur regexp nil callback) date)))
  14953. (defun org-check-dates-range (start-date end-date)
  14954. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14955. (interactive (list (org-read-date nil nil nil "Range starts")
  14956. (org-read-date nil nil nil "Range end")))
  14957. (let ((case-fold-search nil)
  14958. (regexp (org-re-timestamp org-ts-type))
  14959. (callback
  14960. (lambda ()
  14961. (let ((match (match-string 1)))
  14962. (and
  14963. (not (time-less-p
  14964. (org-time-string-to-time match)
  14965. (org-time-string-to-time start-date)))
  14966. (time-less-p
  14967. (org-time-string-to-time match)
  14968. (org-time-string-to-time end-date)))))))
  14969. (message "%d entries between %s and %s"
  14970. (org-occur regexp nil callback) start-date end-date)))
  14971. (defun org-evaluate-time-range (&optional to-buffer)
  14972. "Evaluate a time range by computing the difference between start and end.
  14973. Normally the result is just printed in the echo area, but with prefix arg
  14974. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14975. If the time range is actually in a table, the result is inserted into the
  14976. next column.
  14977. For time difference computation, a year is assumed to be exactly 365
  14978. days in order to avoid rounding problems."
  14979. (interactive "P")
  14980. (or
  14981. (org-clock-update-time-maybe)
  14982. (save-excursion
  14983. (unless (org-at-date-range-p t)
  14984. (goto-char (point-at-bol))
  14985. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14986. (if (not (org-at-date-range-p t))
  14987. (error "Not at a time-stamp range, and none found in current line")))
  14988. (let* ((ts1 (match-string 1))
  14989. (ts2 (match-string 2))
  14990. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14991. (match-end (match-end 0))
  14992. (time1 (org-time-string-to-time ts1))
  14993. (time2 (org-time-string-to-time ts2))
  14994. (t1 (org-float-time time1))
  14995. (t2 (org-float-time time2))
  14996. (diff (abs (- t2 t1)))
  14997. (negative (< (- t2 t1) 0))
  14998. ;; (ys (floor (* 365 24 60 60)))
  14999. (ds (* 24 60 60))
  15000. (hs (* 60 60))
  15001. (fy "%dy %dd %02d:%02d")
  15002. (fy1 "%dy %dd")
  15003. (fd "%dd %02d:%02d")
  15004. (fd1 "%dd")
  15005. (fh "%02d:%02d")
  15006. y d h m align)
  15007. (if havetime
  15008. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15009. y 0
  15010. d (floor (/ diff ds)) diff (mod diff ds)
  15011. h (floor (/ diff hs)) diff (mod diff hs)
  15012. m (floor (/ diff 60)))
  15013. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  15014. y 0
  15015. d (floor (+ (/ diff ds) 0.5))
  15016. h 0 m 0))
  15017. (if (not to-buffer)
  15018. (message "%s" (org-make-tdiff-string y d h m))
  15019. (if (org-at-table-p)
  15020. (progn
  15021. (goto-char match-end)
  15022. (setq align t)
  15023. (and (looking-at " *|") (goto-char (match-end 0))))
  15024. (goto-char match-end))
  15025. (if (looking-at
  15026. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  15027. (replace-match ""))
  15028. (if negative (insert " -"))
  15029. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  15030. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  15031. (insert " " (format fh h m))))
  15032. (if align (org-table-align))
  15033. (message "Time difference inserted")))))
  15034. (defun org-make-tdiff-string (y d h m)
  15035. (let ((fmt "")
  15036. (l nil))
  15037. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  15038. l (push y l)))
  15039. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  15040. l (push d l)))
  15041. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  15042. l (push h l)))
  15043. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  15044. l (push m l)))
  15045. (apply 'format fmt (nreverse l))))
  15046. (defun org-time-string-to-time (s &optional buffer pos)
  15047. "Convert a timestamp string into internal time."
  15048. (condition-case errdata
  15049. (apply 'encode-time (org-parse-time-string s))
  15050. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15051. s (if (not (and buffer pos))
  15052. ""
  15053. (format " at %d in buffer `%s'" pos buffer))
  15054. (cdr errdata)))))
  15055. (defun org-time-string-to-seconds (s)
  15056. "Convert a timestamp string to a number of seconds."
  15057. (org-float-time (org-time-string-to-time s)))
  15058. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  15059. "Convert a time stamp to an absolute day number.
  15060. If there is a specifier for a cyclic time stamp, get the closest date to
  15061. DAYNR.
  15062. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  15063. The variable date is bound by the calendar when this is called."
  15064. (cond
  15065. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  15066. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  15067. daynr
  15068. (+ daynr 1000)))
  15069. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  15070. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  15071. (time-to-days (current-time))) (match-string 0 s)
  15072. prefer show-all))
  15073. (t (time-to-days
  15074. (condition-case errdata
  15075. (apply 'encode-time (org-parse-time-string s))
  15076. (error (error "Bad timestamp `%s'%s\nError was: %s"
  15077. s (if (not (and buffer pos))
  15078. ""
  15079. (format " at %d in buffer `%s'" pos buffer))
  15080. (cdr errdata))))))))
  15081. (defun org-days-to-iso-week (days)
  15082. "Return the iso week number."
  15083. (require 'cal-iso)
  15084. (car (calendar-iso-from-absolute days)))
  15085. (defun org-small-year-to-year (year)
  15086. "Convert 2-digit years into 4-digit years.
  15087. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  15088. The year 2000 cannot be abbreviated. Any year larger than 99
  15089. is returned unchanged."
  15090. (if (< year 38)
  15091. (setq year (+ 2000 year))
  15092. (if (< year 100)
  15093. (setq year (+ 1900 year))))
  15094. year)
  15095. (defun org-time-from-absolute (d)
  15096. "Return the time corresponding to date D.
  15097. D may be an absolute day number, or a calendar-type list (month day year)."
  15098. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  15099. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  15100. (defun org-calendar-holiday ()
  15101. "List of holidays, for Diary display in Org-mode."
  15102. (require 'holidays)
  15103. (let ((hl (funcall
  15104. (if (fboundp 'calendar-check-holidays)
  15105. 'calendar-check-holidays 'check-calendar-holidays) date)))
  15106. (if hl (mapconcat 'identity hl "; "))))
  15107. (defun org-diary-sexp-entry (sexp entry date)
  15108. "Process a SEXP diary ENTRY for DATE."
  15109. (require 'diary-lib)
  15110. (let ((result (if calendar-debug-sexp
  15111. (let ((stack-trace-on-error t))
  15112. (eval (car (read-from-string sexp))))
  15113. (condition-case nil
  15114. (eval (car (read-from-string sexp)))
  15115. (error
  15116. (beep)
  15117. (message "Bad sexp at line %d in %s: %s"
  15118. (org-current-line)
  15119. (buffer-file-name) sexp)
  15120. (sleep-for 2))))))
  15121. (cond ((stringp result) (split-string result "; "))
  15122. ((and (consp result)
  15123. (not (consp (cdr result)))
  15124. (stringp (cdr result))) (cdr result))
  15125. ((and (consp result)
  15126. (stringp (car result))) result)
  15127. (result entry))))
  15128. (defun org-diary-to-ical-string (frombuf)
  15129. "Get iCalendar entries from diary entries in buffer FROMBUF.
  15130. This uses the icalendar.el library."
  15131. (let* ((tmpdir (if (featurep 'xemacs)
  15132. (temp-directory)
  15133. temporary-file-directory))
  15134. (tmpfile (make-temp-name
  15135. (expand-file-name "orgics" tmpdir)))
  15136. buf rtn b e)
  15137. (with-current-buffer frombuf
  15138. (icalendar-export-region (point-min) (point-max) tmpfile)
  15139. (setq buf (find-buffer-visiting tmpfile))
  15140. (set-buffer buf)
  15141. (goto-char (point-min))
  15142. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  15143. (setq b (match-beginning 0)))
  15144. (goto-char (point-max))
  15145. (if (re-search-backward "^END:VEVENT" nil t)
  15146. (setq e (match-end 0)))
  15147. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  15148. (kill-buffer buf)
  15149. (delete-file tmpfile)
  15150. rtn))
  15151. (defun org-closest-date (start current change prefer show-all)
  15152. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  15153. When PREFER is `past', return a date that is either CURRENT or past.
  15154. When PREFER is `future', return a date that is either CURRENT or future.
  15155. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  15156. ;; Make the proper lists from the dates
  15157. (catch 'exit
  15158. (let ((a1 '(("h" . hour)
  15159. ("d" . day)
  15160. ("w" . week)
  15161. ("m" . month)
  15162. ("y" . year)))
  15163. (shour (nth 2 (org-parse-time-string start)))
  15164. dn dw sday cday n1 n2 n0
  15165. d m y y1 y2 date1 date2 nmonths nm ny m2)
  15166. (setq start (org-date-to-gregorian start)
  15167. current (org-date-to-gregorian
  15168. (if show-all
  15169. current
  15170. (time-to-days (current-time))))
  15171. sday (calendar-absolute-from-gregorian start)
  15172. cday (calendar-absolute-from-gregorian current))
  15173. (if (<= cday sday) (throw 'exit sday))
  15174. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  15175. (setq dn (string-to-number (match-string 1 change))
  15176. dw (cdr (assoc (match-string 2 change) a1)))
  15177. (error "Invalid change specifier: %s" change))
  15178. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  15179. (cond
  15180. ((eq dw 'hour)
  15181. (let ((missing-hours
  15182. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  15183. dn)))
  15184. (setq n1 (if (zerop missing-hours) cday
  15185. (- cday (1+ (floor (/ missing-hours 24)))))
  15186. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  15187. ((eq dw 'day)
  15188. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  15189. n2 (+ n1 dn)))
  15190. ((eq dw 'year)
  15191. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  15192. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  15193. (setq date1 (list m d y1)
  15194. n1 (calendar-absolute-from-gregorian date1)
  15195. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  15196. n2 (calendar-absolute-from-gregorian date2)))
  15197. ((eq dw 'month)
  15198. ;; approx number of month between the two dates
  15199. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  15200. ;; How often does dn fit in there?
  15201. (setq d (nth 1 start) m (car start) y (nth 2 start)
  15202. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  15203. m (+ m nm)
  15204. ny (floor (/ m 12))
  15205. y (+ y ny)
  15206. m (- m (* ny 12)))
  15207. (while (> m 12) (setq m (- m 12) y (1+ y)))
  15208. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  15209. (setq m2 (+ m dn) y2 y)
  15210. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15211. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  15212. (while (<= n2 cday)
  15213. (setq n1 n2 m m2 y y2)
  15214. (setq m2 (+ m dn) y2 y)
  15215. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  15216. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  15217. ;; Make sure n1 is the earlier date
  15218. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  15219. (if show-all
  15220. (cond
  15221. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15222. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15223. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  15224. (cond
  15225. ((eq prefer 'past) (if (= cday n2) n2 n1))
  15226. ((eq prefer 'future) (if (= cday n1) n1 n2))
  15227. (t (if (= cday n1) n1 n2)))))))
  15228. (defun org-date-to-gregorian (date)
  15229. "Turn any specification of DATE into a Gregorian date for the calendar."
  15230. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  15231. ((and (listp date) (= (length date) 3)) date)
  15232. ((stringp date)
  15233. (setq date (org-parse-time-string date))
  15234. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  15235. ((listp date)
  15236. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  15237. (defun org-parse-time-string (s &optional nodefault)
  15238. "Parse the standard Org-mode time string.
  15239. This should be a lot faster than the normal `parse-time-string'.
  15240. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  15241. hour and minute fields will be nil if not given."
  15242. (cond ((string-match org-ts-regexp0 s)
  15243. (list 0
  15244. (if (or (match-beginning 8) (not nodefault))
  15245. (string-to-number (or (match-string 8 s) "0")))
  15246. (if (or (match-beginning 7) (not nodefault))
  15247. (string-to-number (or (match-string 7 s) "0")))
  15248. (string-to-number (match-string 4 s))
  15249. (string-to-number (match-string 3 s))
  15250. (string-to-number (match-string 2 s))
  15251. nil nil nil))
  15252. ((string-match "^<[^>]+>$" s)
  15253. (decode-time (seconds-to-time (org-matcher-time s))))
  15254. (t (error "Not a standard Org-mode time string: %s" s))))
  15255. (defun org-timestamp-up (&optional arg)
  15256. "Increase the date item at the cursor by one.
  15257. If the cursor is on the year, change the year. If it is on the month,
  15258. the day or the time, change that.
  15259. With prefix ARG, change by that many units."
  15260. (interactive "p")
  15261. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  15262. (defun org-timestamp-down (&optional arg)
  15263. "Decrease the date item at the cursor by one.
  15264. If the cursor is on the year, change the year. If it is on the month,
  15265. the day or the time, change that.
  15266. With prefix ARG, change by that many units."
  15267. (interactive "p")
  15268. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  15269. (defun org-timestamp-up-day (&optional arg)
  15270. "Increase the date in the time stamp by one day.
  15271. With prefix ARG, change that many days."
  15272. (interactive "p")
  15273. (if (and (not (org-at-timestamp-p t))
  15274. (org-at-heading-p))
  15275. (org-todo 'up)
  15276. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  15277. (defun org-timestamp-down-day (&optional arg)
  15278. "Decrease the date in the time stamp by one day.
  15279. With prefix ARG, change that many days."
  15280. (interactive "p")
  15281. (if (and (not (org-at-timestamp-p t))
  15282. (org-at-heading-p))
  15283. (org-todo 'down)
  15284. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  15285. (defun org-at-timestamp-p (&optional inactive-ok)
  15286. "Determine if the cursor is in or at a timestamp."
  15287. (interactive)
  15288. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  15289. (pos (point))
  15290. (ans (or (looking-at tsr)
  15291. (save-excursion
  15292. (skip-chars-backward "^[<\n\r\t")
  15293. (if (> (point) (point-min)) (backward-char 1))
  15294. (and (looking-at tsr)
  15295. (> (- (match-end 0) pos) -1))))))
  15296. (and ans
  15297. (boundp 'org-ts-what)
  15298. (setq org-ts-what
  15299. (cond
  15300. ((= pos (match-beginning 0)) 'bracket)
  15301. ;; Point is considered to be "on the bracket" whether
  15302. ;; it's really on it or right after it.
  15303. ((= pos (1- (match-end 0))) 'bracket)
  15304. ((= pos (match-end 0)) 'after)
  15305. ((org-pos-in-match-range pos 2) 'year)
  15306. ((org-pos-in-match-range pos 3) 'month)
  15307. ((org-pos-in-match-range pos 7) 'hour)
  15308. ((org-pos-in-match-range pos 8) 'minute)
  15309. ((or (org-pos-in-match-range pos 4)
  15310. (org-pos-in-match-range pos 5)) 'day)
  15311. ((and (> pos (or (match-end 8) (match-end 5)))
  15312. (< pos (match-end 0)))
  15313. (- pos (or (match-end 8) (match-end 5))))
  15314. (t 'day))))
  15315. ans))
  15316. (defun org-toggle-timestamp-type ()
  15317. "Toggle the type (<active> or [inactive]) of a time stamp."
  15318. (interactive)
  15319. (when (org-at-timestamp-p t)
  15320. (let ((beg (match-beginning 0)) (end (match-end 0))
  15321. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  15322. (save-excursion
  15323. (goto-char beg)
  15324. (while (re-search-forward "[][<>]" end t)
  15325. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  15326. t t)))
  15327. (message "Timestamp is now %sactive"
  15328. (if (equal (char-after beg) ?<) "" "in")))))
  15329. (defvar org-clock-history) ; defined in org-clock.el
  15330. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  15331. (defun org-timestamp-change (n &optional what updown)
  15332. "Change the date in the time stamp at point.
  15333. The date will be changed by N times WHAT. WHAT can be `day', `month',
  15334. `year', `minute', `second'. If WHAT is not given, the cursor position
  15335. in the timestamp determines what will be changed."
  15336. (let ((origin (point)) origin-cat
  15337. with-hm inactive
  15338. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  15339. org-ts-what
  15340. extra rem
  15341. ts time time0 fixnext clrgx)
  15342. (if (not (org-at-timestamp-p t))
  15343. (error "Not at a timestamp"))
  15344. (if (and (not what) (eq org-ts-what 'bracket))
  15345. (org-toggle-timestamp-type)
  15346. ;; Point isn't on brackets. Remember the part of the time-stamp
  15347. ;; the point was in. Indeed, size of time-stamps may change,
  15348. ;; but point must be kept in the same category nonetheless.
  15349. (setq origin-cat org-ts-what)
  15350. (if (and (not what) (not (eq org-ts-what 'day))
  15351. org-display-custom-times
  15352. (get-text-property (point) 'display)
  15353. (not (get-text-property (1- (point)) 'display)))
  15354. (setq org-ts-what 'day))
  15355. (setq org-ts-what (or what org-ts-what)
  15356. inactive (= (char-after (match-beginning 0)) ?\[)
  15357. ts (match-string 0))
  15358. (replace-match "")
  15359. (if (string-match
  15360. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  15361. ts)
  15362. (setq extra (match-string 1 ts)))
  15363. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  15364. (setq with-hm t))
  15365. (setq time0 (org-parse-time-string ts))
  15366. (when (and updown
  15367. (eq org-ts-what 'minute)
  15368. (not current-prefix-arg))
  15369. ;; This looks like s-up and s-down. Change by one rounding step.
  15370. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  15371. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  15372. (setcar (cdr time0) (+ (nth 1 time0)
  15373. (if (> n 0) (- rem) (- dm rem))))))
  15374. (setq time
  15375. (encode-time (or (car time0) 0)
  15376. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  15377. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  15378. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  15379. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  15380. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  15381. (nthcdr 6 time0)))
  15382. (when (and (member org-ts-what '(hour minute))
  15383. extra
  15384. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  15385. (setq extra (org-modify-ts-extra
  15386. extra
  15387. (if (eq org-ts-what 'hour) 2 5)
  15388. n dm)))
  15389. (when (integerp org-ts-what)
  15390. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  15391. (if (eq what 'calendar)
  15392. (let ((cal-date (org-get-date-from-calendar)))
  15393. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  15394. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  15395. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  15396. (setcar time0 (or (car time0) 0))
  15397. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  15398. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  15399. (setq time (apply 'encode-time time0))))
  15400. ;; Insert the new time-stamp, and ensure point stays in the same
  15401. ;; category as before (i.e. not after the last position in that
  15402. ;; category).
  15403. (let ((pos (point)))
  15404. ;; Stay before inserted string. `save-excursion' is of no use.
  15405. (setq org-last-changed-timestamp
  15406. (org-insert-time-stamp time with-hm inactive nil nil extra))
  15407. (goto-char pos))
  15408. (save-match-data
  15409. (looking-at org-ts-regexp3)
  15410. (goto-char (cond
  15411. ;; `day' category ends before `hour' if any, or at
  15412. ;; the end of the day name.
  15413. ((eq origin-cat 'day)
  15414. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  15415. ((eq origin-cat 'hour) (min (match-end 7) origin))
  15416. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  15417. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  15418. ;; `year' and `month' have both fixed size: point
  15419. ;; couldn't have moved into another part.
  15420. (t origin))))
  15421. ;; Update clock if on a CLOCK line.
  15422. (org-clock-update-time-maybe)
  15423. ;; Maybe adjust the closest clock in `org-clock-history'
  15424. (when org-clock-adjust-closest
  15425. (if (not (and (org-at-clock-log-p)
  15426. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  15427. org-clock-history))))))
  15428. (message "No clock to adjust")
  15429. (cond ((save-excursion ; fix previous clock?
  15430. (re-search-backward org-ts-regexp0 nil t)
  15431. (org-looking-back (concat org-clock-string " \\[")))
  15432. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  15433. ((save-excursion ; fix next clock?
  15434. (re-search-backward org-ts-regexp0 nil t)
  15435. (looking-at (concat org-ts-regexp0 "\\] =>")))
  15436. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  15437. (save-window-excursion
  15438. ;; Find closest clock to point, adjust the previous/next one in history
  15439. (let* ((p (save-excursion (org-back-to-heading t)))
  15440. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  15441. (clfixnth
  15442. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  15443. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  15444. (if (not clfixpos)
  15445. (message "No clock to adjust")
  15446. (save-excursion
  15447. (org-goto-marker-or-bmk clfixpos)
  15448. (org-show-subtree)
  15449. (when (re-search-forward clrgx nil t)
  15450. (goto-char (match-beginning 1))
  15451. (let (org-clock-adjust-closest)
  15452. (org-timestamp-change n org-ts-what updown))
  15453. (message "Clock adjusted in %s for heading: %s"
  15454. (file-name-nondirectory (buffer-file-name))
  15455. (org-get-heading t t)))))))))
  15456. ;; Try to recenter the calendar window, if any.
  15457. (if (and org-calendar-follow-timestamp-change
  15458. (get-buffer-window "*Calendar*" t)
  15459. (memq org-ts-what '(day month year)))
  15460. (org-recenter-calendar (time-to-days time))))))
  15461. (defun org-modify-ts-extra (s pos n dm)
  15462. "Change the different parts of the lead-time and repeat fields in timestamp."
  15463. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  15464. ng h m new rem)
  15465. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  15466. (cond
  15467. ((or (org-pos-in-match-range pos 2)
  15468. (org-pos-in-match-range pos 3))
  15469. (setq m (string-to-number (match-string 3 s))
  15470. h (string-to-number (match-string 2 s)))
  15471. (if (org-pos-in-match-range pos 2)
  15472. (setq h (+ h n))
  15473. (setq n (* dm (org-no-warnings (signum n))))
  15474. (when (not (= 0 (setq rem (% m dm))))
  15475. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  15476. (setq m (+ m n)))
  15477. (if (< m 0) (setq m (+ m 60) h (1- h)))
  15478. (if (> m 59) (setq m (- m 60) h (1+ h)))
  15479. (setq h (min 24 (max 0 h)))
  15480. (setq ng 1 new (format "-%02d:%02d" h m)))
  15481. ((org-pos-in-match-range pos 6)
  15482. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  15483. ((org-pos-in-match-range pos 5)
  15484. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  15485. ((org-pos-in-match-range pos 9)
  15486. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  15487. ((org-pos-in-match-range pos 8)
  15488. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15489. (when ng
  15490. (setq s (concat
  15491. (substring s 0 (match-beginning ng))
  15492. new
  15493. (substring s (match-end ng))))))
  15494. s))
  15495. (defun org-recenter-calendar (date)
  15496. "If the calendar is visible, recenter it to DATE."
  15497. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15498. (when cwin
  15499. (let ((calendar-move-hook nil))
  15500. (with-selected-window cwin
  15501. (calendar-goto-date (if (listp date) date
  15502. (calendar-gregorian-from-absolute date))))))))
  15503. (defun org-goto-calendar (&optional arg)
  15504. "Go to the Emacs calendar at the current date.
  15505. If there is a time stamp in the current line, go to that date.
  15506. A prefix ARG can be used to force the current date."
  15507. (interactive "P")
  15508. (let ((tsr org-ts-regexp) diff
  15509. (calendar-move-hook nil)
  15510. (calendar-view-holidays-initially-flag nil)
  15511. (calendar-view-diary-initially-flag nil))
  15512. (if (or (org-at-timestamp-p)
  15513. (save-excursion
  15514. (beginning-of-line 1)
  15515. (looking-at (concat ".*" tsr))))
  15516. (let ((d1 (time-to-days (current-time)))
  15517. (d2 (time-to-days
  15518. (org-time-string-to-time (match-string 1)))))
  15519. (setq diff (- d2 d1))))
  15520. (calendar)
  15521. (calendar-goto-today)
  15522. (if (and diff (not arg)) (calendar-forward-day diff))))
  15523. (defun org-get-date-from-calendar ()
  15524. "Return a list (month day year) of date at point in calendar."
  15525. (with-current-buffer "*Calendar*"
  15526. (save-match-data
  15527. (calendar-cursor-to-date))))
  15528. (defun org-date-from-calendar ()
  15529. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15530. If there is already a time stamp at the cursor position, update it."
  15531. (interactive)
  15532. (if (org-at-timestamp-p t)
  15533. (org-timestamp-change 0 'calendar)
  15534. (let ((cal-date (org-get-date-from-calendar)))
  15535. (org-insert-time-stamp
  15536. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15537. (defcustom org-effort-durations
  15538. `(("h" . 60)
  15539. ("d" . ,(* 60 8))
  15540. ("w" . ,(* 60 8 5))
  15541. ("m" . ,(* 60 8 5 4))
  15542. ("y" . ,(* 60 8 5 40)))
  15543. "Conversion factor to minutes for an effort modifier.
  15544. Each entry has the form (MODIFIER . MINUTES).
  15545. In an effort string, a number followed by MODIFIER is multiplied
  15546. by the specified number of MINUTES to obtain an effort in
  15547. minutes.
  15548. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15549. effort string \"2hours\" is equivalent to 120 minutes."
  15550. :group 'org-agenda
  15551. :version "24.1"
  15552. :type '(alist :key-type (string :tag "Modifier")
  15553. :value-type (number :tag "Minutes")))
  15554. (defun org-minutes-to-clocksum-string (m)
  15555. "Format number of minutes as a clocksum string.
  15556. The format is determined by `org-time-clocksum-format',
  15557. `org-time-clocksum-use-fractional' and
  15558. `org-time-clocksum-fractional-format' and
  15559. `org-time-clocksum-use-effort-durations'."
  15560. (let ((clocksum "") h d w mo y fmt n)
  15561. (setq h (if org-time-clocksum-use-effort-durations
  15562. (cdr (assoc "h" org-effort-durations)) 60)
  15563. d (if org-time-clocksum-use-effort-durations
  15564. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  15565. w (if org-time-clocksum-use-effort-durations
  15566. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  15567. mo (if org-time-clocksum-use-effort-durations
  15568. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  15569. y (if org-time-clocksum-use-effort-durations
  15570. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  15571. ;; fractional format
  15572. (if org-time-clocksum-use-fractional
  15573. (cond
  15574. ;; single format string
  15575. ((stringp org-time-clocksum-fractional-format)
  15576. (format org-time-clocksum-fractional-format (/ m (float h))))
  15577. ;; choice of fractional formats for different time units
  15578. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15579. (> (/ (truncate m) (* y d h)) 0))
  15580. (format fmt (/ m (* y d (float h)))))
  15581. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15582. (> (/ (truncate m) (* mo d h)) 0))
  15583. (format fmt (/ m (* mo d (float h)))))
  15584. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15585. (> (/ (truncate m) (* w d h)) 0))
  15586. (format fmt (/ m (* w d (float h)))))
  15587. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15588. (> (/ (truncate m) (* d h)) 0))
  15589. (format fmt (/ m (* d (float h)))))
  15590. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15591. (> (/ (truncate m) h) 0))
  15592. (format fmt (/ m (float h))))
  15593. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  15594. (format fmt m))
  15595. ;; fall back to smallest time unit with a format
  15596. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  15597. (format fmt (/ m (float h))))
  15598. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  15599. (format fmt (/ m (* d (float h)))))
  15600. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  15601. (format fmt (/ m (* w d (float h)))))
  15602. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  15603. (format fmt (/ m (* mo d (float h)))))
  15604. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  15605. (format fmt (/ m (* y d (float h))))))
  15606. ;; standard (non-fractional) format, with single format string
  15607. (if (stringp org-time-clocksum-format)
  15608. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  15609. ;; separate formats components
  15610. (and (setq fmt (plist-get org-time-clocksum-format :years))
  15611. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  15612. (plist-get org-time-clocksum-format :require-years))
  15613. (setq clocksum (concat clocksum (format fmt n))
  15614. m (- m (* n y d h))))
  15615. (and (setq fmt (plist-get org-time-clocksum-format :months))
  15616. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  15617. (plist-get org-time-clocksum-format :require-months))
  15618. (setq clocksum (concat clocksum (format fmt n))
  15619. m (- m (* n mo d h))))
  15620. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  15621. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  15622. (plist-get org-time-clocksum-format :require-weeks))
  15623. (setq clocksum (concat clocksum (format fmt n))
  15624. m (- m (* n w d h))))
  15625. (and (setq fmt (plist-get org-time-clocksum-format :days))
  15626. (or (> (setq n (/ (truncate m) (* d h))) 0)
  15627. (plist-get org-time-clocksum-format :require-days))
  15628. (setq clocksum (concat clocksum (format fmt n))
  15629. m (- m (* n d h))))
  15630. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  15631. (or (> (setq n (/ (truncate m) h)) 0)
  15632. (plist-get org-time-clocksum-format :require-hours))
  15633. (setq clocksum (concat clocksum (format fmt n))
  15634. m (- m (* n h))))
  15635. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  15636. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  15637. (setq clocksum (concat clocksum (format fmt m))))
  15638. ;; return formatted time duration
  15639. clocksum))))
  15640. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  15641. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  15642. "Org mode version 8.0")
  15643. (defun org-hours-to-clocksum-string (n)
  15644. (org-minutes-to-clocksum-string (* n 60)))
  15645. (defun org-hh:mm-string-to-minutes (s)
  15646. "Convert a string H:MM to a number of minutes.
  15647. If the string is just a number, interpret it as minutes.
  15648. In fact, the first hh:mm or number in the string will be taken,
  15649. there can be extra stuff in the string.
  15650. If no number is found, the return value is 0."
  15651. (cond
  15652. ((integerp s) s)
  15653. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15654. (+ (* (string-to-number (match-string 1 s)) 60)
  15655. (string-to-number (match-string 2 s))))
  15656. ((string-match "\\([0-9]+\\)" s)
  15657. (string-to-number (match-string 1 s)))
  15658. (t 0)))
  15659. (defcustom org-image-actual-width t
  15660. "Should we use the actual width of images when inlining them?
  15661. When set to `t', always use the image width.
  15662. When set to a number, use imagemagick (when available) to set
  15663. the image's width to this value.
  15664. When set to a number in a list, try to get the width from the
  15665. #+ATTR.* keyword if it matches a width specification like
  15666. width=\"[0-9]+\", and fall back on that number if none is found.
  15667. When set to nil, try to get the width from an #+ATTR.* keyword
  15668. and fall back on the original width if none is found.
  15669. This requires Emacs >= 24.1, build with imagemagick support."
  15670. :group 'org-appearance
  15671. :version "24.3"
  15672. :type '(choice
  15673. (const :tag "Use the image width" t)
  15674. (integer :tag "Use a number of pixels")
  15675. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  15676. (const :tag "Use #+ATTR* or don't resize" nil)))
  15677. (defcustom org-agenda-inhibit-startup t
  15678. "Inhibit startup when preparing agenda buffers.
  15679. When this variable is `t' (the default), the initialization of
  15680. the Org agenda buffers is inhibited: e.g. the visibility state
  15681. is not set, the tables are not re-aligned, etc."
  15682. :type 'boolean
  15683. :version "24.3"
  15684. :group 'org-agenda)
  15685. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15686. "Convert a duration string S to minutes.
  15687. A bare number is interpreted as minutes, modifiers can be set by
  15688. customizing `org-effort-durations' (which see).
  15689. Entries containing a colon are interpreted as H:MM by
  15690. `org-hh:mm-string-to-minutes'."
  15691. (let ((result 0)
  15692. (re (concat "\\([0-9.]+\\) *\\("
  15693. (regexp-opt (mapcar 'car org-effort-durations))
  15694. "\\)")))
  15695. (while (string-match re s)
  15696. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15697. (string-to-number (match-string 1 s))))
  15698. (setq s (replace-match "" nil t s)))
  15699. (setq result (floor result))
  15700. (incf result (org-hh:mm-string-to-minutes s))
  15701. (if output-to-string (number-to-string result) result)))
  15702. ;;;; Files
  15703. (defun org-save-all-org-buffers ()
  15704. "Save all Org-mode buffers without user confirmation."
  15705. (interactive)
  15706. (message "Saving all Org-mode buffers...")
  15707. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15708. (when (featurep 'org-id) (org-id-locations-save))
  15709. (message "Saving all Org-mode buffers... done"))
  15710. (defun org-revert-all-org-buffers ()
  15711. "Revert all Org-mode buffers.
  15712. Prompt for confirmation when there are unsaved changes.
  15713. Be sure you know what you are doing before letting this function
  15714. overwrite your changes.
  15715. This function is useful in a setup where one tracks org files
  15716. with a version control system, to revert on one machine after pulling
  15717. changes from another. I believe the procedure must be like this:
  15718. 1. M-x org-save-all-org-buffers
  15719. 2. Pull changes from the other machine, resolve conflicts
  15720. 3. M-x org-revert-all-org-buffers"
  15721. (interactive)
  15722. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15723. (error "Abort"))
  15724. (save-excursion
  15725. (save-window-excursion
  15726. (mapc
  15727. (lambda (b)
  15728. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15729. (with-current-buffer b buffer-file-name))
  15730. (org-pop-to-buffer-same-window b)
  15731. (revert-buffer t 'no-confirm)))
  15732. (buffer-list))
  15733. (when (and (featurep 'org-id) org-id-track-globally)
  15734. (org-id-locations-load)))))
  15735. ;;;; Agenda files
  15736. ;;;###autoload
  15737. (defun org-switchb (&optional arg)
  15738. "Switch between Org buffers.
  15739. With one prefix argument, restrict available buffers to files.
  15740. With two prefix arguments, restrict available buffers to agenda files.
  15741. Defaults to `iswitchb' for buffer name completion.
  15742. Set `org-completion-use-ido' to make it use ido instead."
  15743. (interactive "P")
  15744. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15745. ((equal arg '(16)) (org-buffer-list 'agenda))
  15746. (t (org-buffer-list))))
  15747. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15748. (org-completion-use-ido org-completion-use-ido))
  15749. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15750. (setq org-completion-use-iswitchb t))
  15751. (org-pop-to-buffer-same-window
  15752. (org-icompleting-read "Org buffer: "
  15753. (mapcar 'list (mapcar 'buffer-name blist))
  15754. nil t))))
  15755. ;;; Define some older names previously used for this functionality
  15756. ;;;###autoload
  15757. (defalias 'org-ido-switchb 'org-switchb)
  15758. ;;;###autoload
  15759. (defalias 'org-iswitchb 'org-switchb)
  15760. (defun org-buffer-list (&optional predicate exclude-tmp)
  15761. "Return a list of Org buffers.
  15762. PREDICATE can be `export', `files' or `agenda'.
  15763. export restrict the list to Export buffers.
  15764. files restrict the list to buffers visiting Org files.
  15765. agenda restrict the list to buffers visiting agenda files.
  15766. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15767. (let* ((bfn nil)
  15768. (agenda-files (and (eq predicate 'agenda)
  15769. (mapcar 'file-truename (org-agenda-files t))))
  15770. (filter
  15771. (cond
  15772. ((eq predicate 'files)
  15773. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15774. ((eq predicate 'export)
  15775. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15776. ((eq predicate 'agenda)
  15777. (lambda (b)
  15778. (with-current-buffer b
  15779. (and (derived-mode-p 'org-mode)
  15780. (setq bfn (buffer-file-name b))
  15781. (member (file-truename bfn) agenda-files)))))
  15782. (t (lambda (b) (with-current-buffer b
  15783. (or (derived-mode-p 'org-mode)
  15784. (string-match "\*Org .*Export"
  15785. (buffer-name b)))))))))
  15786. (delq nil
  15787. (mapcar
  15788. (lambda(b)
  15789. (if (and (funcall filter b)
  15790. (or (not exclude-tmp)
  15791. (not (string-match "tmp" (buffer-name b)))))
  15792. b
  15793. nil))
  15794. (buffer-list)))))
  15795. (defun org-agenda-files (&optional unrestricted archives)
  15796. "Get the list of agenda files.
  15797. Optional UNRESTRICTED means return the full list even if a restriction
  15798. is currently in place.
  15799. When ARCHIVES is t, include all archive files that are really being
  15800. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15801. `org-agenda-archives-mode' is t."
  15802. (let ((files
  15803. (cond
  15804. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15805. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15806. ((listp org-agenda-files) org-agenda-files)
  15807. (t (error "Invalid value of `org-agenda-files'")))))
  15808. (setq files (apply 'append
  15809. (mapcar (lambda (f)
  15810. (if (file-directory-p f)
  15811. (directory-files
  15812. f t org-agenda-file-regexp)
  15813. (list f)))
  15814. files)))
  15815. (when org-agenda-skip-unavailable-files
  15816. (setq files (delq nil
  15817. (mapcar (function
  15818. (lambda (file)
  15819. (and (file-readable-p file) file)))
  15820. files))))
  15821. (when (or (eq archives t)
  15822. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15823. (setq files (org-add-archive-files files)))
  15824. files))
  15825. (defun org-agenda-file-p (&optional file)
  15826. "Return non-nil, if FILE is an agenda file.
  15827. If FILE is omitted, use the file associated with the current
  15828. buffer."
  15829. (member (or file (buffer-file-name))
  15830. (org-agenda-files t)))
  15831. (defun org-edit-agenda-file-list ()
  15832. "Edit the list of agenda files.
  15833. Depending on setup, this either uses customize to edit the variable
  15834. `org-agenda-files', or it visits the file that is holding the list. In the
  15835. latter case, the buffer is set up in a way that saving it automatically kills
  15836. the buffer and restores the previous window configuration."
  15837. (interactive)
  15838. (if (stringp org-agenda-files)
  15839. (let ((cw (current-window-configuration)))
  15840. (find-file org-agenda-files)
  15841. (org-set-local 'org-window-configuration cw)
  15842. (org-add-hook 'after-save-hook
  15843. (lambda ()
  15844. (set-window-configuration
  15845. (prog1 org-window-configuration
  15846. (kill-buffer (current-buffer))))
  15847. (org-install-agenda-files-menu)
  15848. (message "New agenda file list installed"))
  15849. nil 'local)
  15850. (message "%s" (substitute-command-keys
  15851. "Edit list and finish with \\[save-buffer]")))
  15852. (customize-variable 'org-agenda-files)))
  15853. (defun org-store-new-agenda-file-list (list)
  15854. "Set new value for the agenda file list and save it correctly."
  15855. (if (stringp org-agenda-files)
  15856. (let ((fe (org-read-agenda-file-list t)) b u)
  15857. (while (setq b (find-buffer-visiting org-agenda-files))
  15858. (kill-buffer b))
  15859. (with-temp-file org-agenda-files
  15860. (insert
  15861. (mapconcat
  15862. (lambda (f) ;; Keep un-expanded entries.
  15863. (if (setq u (assoc f fe))
  15864. (cdr u)
  15865. f))
  15866. list "\n")
  15867. "\n")))
  15868. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15869. (org-insert-mode-line-in-empty-file nil))
  15870. (setq org-agenda-files list)
  15871. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15872. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15873. "Read the list of agenda files from a file.
  15874. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15875. filenames, used by `org-store-new-agenda-file-list' to write back
  15876. un-expanded file names."
  15877. (when (file-directory-p org-agenda-files)
  15878. (error "`org-agenda-files' cannot be a single directory"))
  15879. (when (stringp org-agenda-files)
  15880. (with-temp-buffer
  15881. (insert-file-contents org-agenda-files)
  15882. (mapcar
  15883. (lambda (f)
  15884. (let ((e (expand-file-name (substitute-in-file-name f)
  15885. org-directory)))
  15886. (if pair-with-expansion
  15887. (cons e f)
  15888. e)))
  15889. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15890. ;;;###autoload
  15891. (defun org-cycle-agenda-files ()
  15892. "Cycle through the files in `org-agenda-files'.
  15893. If the current buffer visits an agenda file, find the next one in the list.
  15894. If the current buffer does not, find the first agenda file."
  15895. (interactive)
  15896. (let* ((fs (org-agenda-files t))
  15897. (files (append fs (list (car fs))))
  15898. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15899. file)
  15900. (unless files (error "No agenda files"))
  15901. (catch 'exit
  15902. (while (setq file (pop files))
  15903. (if (equal (file-truename file) tcf)
  15904. (when (car files)
  15905. (find-file (car files))
  15906. (throw 'exit t))))
  15907. (find-file (car fs)))
  15908. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15909. (defun org-agenda-file-to-front (&optional to-end)
  15910. "Move/add the current file to the top of the agenda file list.
  15911. If the file is not present in the list, it is added to the front. If it is
  15912. present, it is moved there. With optional argument TO-END, add/move to the
  15913. end of the list."
  15914. (interactive "P")
  15915. (let ((org-agenda-skip-unavailable-files nil)
  15916. (file-alist (mapcar (lambda (x)
  15917. (cons (file-truename x) x))
  15918. (org-agenda-files t)))
  15919. (ctf (file-truename
  15920. (or buffer-file-name
  15921. (error "Please save the current buffer to a file"))))
  15922. x had)
  15923. (setq x (assoc ctf file-alist) had x)
  15924. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15925. (if to-end
  15926. (setq file-alist (append (delq x file-alist) (list x)))
  15927. (setq file-alist (cons x (delq x file-alist))))
  15928. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15929. (org-install-agenda-files-menu)
  15930. (message "File %s to %s of agenda file list"
  15931. (if had "moved" "added") (if to-end "end" "front"))))
  15932. (defun org-remove-file (&optional file)
  15933. "Remove current file from the list of files in variable `org-agenda-files'.
  15934. These are the files which are being checked for agenda entries.
  15935. Optional argument FILE means use this file instead of the current."
  15936. (interactive)
  15937. (let* ((org-agenda-skip-unavailable-files nil)
  15938. (file (or file buffer-file-name
  15939. (error "Current buffer does not visit a file")))
  15940. (true-file (file-truename file))
  15941. (afile (abbreviate-file-name file))
  15942. (files (delq nil (mapcar
  15943. (lambda (x)
  15944. (if (equal true-file
  15945. (file-truename x))
  15946. nil x))
  15947. (org-agenda-files t)))))
  15948. (if (not (= (length files) (length (org-agenda-files t))))
  15949. (progn
  15950. (org-store-new-agenda-file-list files)
  15951. (org-install-agenda-files-menu)
  15952. (message "Removed file: %s" afile))
  15953. (message "File was not in list: %s (not removed)" afile))))
  15954. (defun org-file-menu-entry (file)
  15955. (vector file (list 'find-file file) t))
  15956. (defun org-check-agenda-file (file)
  15957. "Make sure FILE exists. If not, ask user what to do."
  15958. (when (not (file-exists-p file))
  15959. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  15960. (abbreviate-file-name file))
  15961. (let ((r (downcase (read-char-exclusive))))
  15962. (cond
  15963. ((equal r ?r)
  15964. (org-remove-file file)
  15965. (throw 'nextfile t))
  15966. (t (error "Abort"))))))
  15967. (defun org-get-agenda-file-buffer (file)
  15968. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15969. it to the list of buffers which might be released later."
  15970. (let ((buf (org-find-base-buffer-visiting file)))
  15971. (if buf
  15972. buf ; just return it
  15973. ;; Make a new buffer and remember it
  15974. (setq buf (find-file-noselect file))
  15975. (if buf (push buf org-agenda-new-buffers))
  15976. buf)))
  15977. (defun org-release-buffers (blist)
  15978. "Release all buffers in list, asking the user for confirmation when needed.
  15979. When a buffer is unmodified, it is just killed. When modified, it is saved
  15980. \(if the user agrees) and then killed."
  15981. (let (buf file)
  15982. (while (setq buf (pop blist))
  15983. (setq file (buffer-file-name buf))
  15984. (when (and (buffer-modified-p buf)
  15985. file
  15986. (y-or-n-p (format "Save file %s? " file)))
  15987. (with-current-buffer buf (save-buffer)))
  15988. (kill-buffer buf))))
  15989. (defun org-agenda-prepare-buffers (files)
  15990. "Create buffers for all agenda files, protect archived trees and comments."
  15991. (interactive)
  15992. (let ((pa '(:org-archived t))
  15993. (pc '(:org-comment t))
  15994. (pall '(:org-archived t :org-comment t))
  15995. (inhibit-read-only t)
  15996. (org-inhibit-startup org-agenda-inhibit-startup)
  15997. (rea (concat ":" org-archive-tag ":"))
  15998. bmp file re)
  15999. (save-excursion
  16000. (save-restriction
  16001. (while (setq file (pop files))
  16002. (catch 'nextfile
  16003. (if (bufferp file)
  16004. (set-buffer file)
  16005. (org-check-agenda-file file)
  16006. (set-buffer (org-get-agenda-file-buffer file)))
  16007. (widen)
  16008. (setq bmp (buffer-modified-p))
  16009. (org-refresh-category-properties)
  16010. (org-refresh-properties org-effort-property 'org-effort)
  16011. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime)
  16012. (setq org-todo-keywords-for-agenda
  16013. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  16014. (setq org-done-keywords-for-agenda
  16015. (append org-done-keywords-for-agenda org-done-keywords))
  16016. (setq org-todo-keyword-alist-for-agenda
  16017. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  16018. (setq org-drawers-for-agenda
  16019. (append org-drawers-for-agenda org-drawers))
  16020. (setq org-tag-alist-for-agenda
  16021. (append org-tag-alist-for-agenda org-tag-alist))
  16022. (save-excursion
  16023. (remove-text-properties (point-min) (point-max) pall)
  16024. (when org-agenda-skip-archived-trees
  16025. (goto-char (point-min))
  16026. (while (re-search-forward rea nil t)
  16027. (if (org-at-heading-p t)
  16028. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  16029. (goto-char (point-min))
  16030. (setq re (format org-heading-keyword-regexp-format
  16031. org-comment-string))
  16032. (while (re-search-forward re nil t)
  16033. (add-text-properties
  16034. (match-beginning 0) (org-end-of-subtree t) pc)))
  16035. (set-buffer-modified-p bmp)))))
  16036. (setq org-todo-keywords-for-agenda
  16037. (org-uniquify org-todo-keywords-for-agenda))
  16038. (setq org-todo-keyword-alist-for-agenda
  16039. (org-uniquify org-todo-keyword-alist-for-agenda)
  16040. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  16041. ;;;; CDLaTeX minor mode
  16042. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  16043. "Keymap for the minor `org-cdlatex-mode'.")
  16044. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  16045. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  16046. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  16047. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  16048. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  16049. (defvar org-cdlatex-texmathp-advice-is-done nil
  16050. "Flag remembering if we have applied the advice to texmathp already.")
  16051. (define-minor-mode org-cdlatex-mode
  16052. "Toggle the minor `org-cdlatex-mode'.
  16053. This mode supports entering LaTeX environment and math in LaTeX fragments
  16054. in Org-mode.
  16055. \\{org-cdlatex-mode-map}"
  16056. nil " OCDL" nil
  16057. (when org-cdlatex-mode
  16058. (require 'cdlatex)
  16059. (run-hooks 'cdlatex-mode-hook)
  16060. (cdlatex-compute-tables))
  16061. (unless org-cdlatex-texmathp-advice-is-done
  16062. (setq org-cdlatex-texmathp-advice-is-done t)
  16063. (defadvice texmathp (around org-math-always-on activate)
  16064. "Always return t in org-mode buffers.
  16065. This is because we want to insert math symbols without dollars even outside
  16066. the LaTeX math segments. If Orgmode thinks that point is actually inside
  16067. an embedded LaTeX fragment, let texmathp do its job.
  16068. \\[org-cdlatex-mode-map]"
  16069. (interactive)
  16070. (let (p)
  16071. (cond
  16072. ((not (derived-mode-p 'org-mode)) ad-do-it)
  16073. ((eq this-command 'cdlatex-math-symbol)
  16074. (setq ad-return-value t
  16075. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  16076. (t
  16077. (let ((p (org-inside-LaTeX-fragment-p)))
  16078. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  16079. (setq ad-return-value t
  16080. texmathp-why '("Org-mode embedded math" . 0))
  16081. (if p ad-do-it)))))))))
  16082. (defun turn-on-org-cdlatex ()
  16083. "Unconditionally turn on `org-cdlatex-mode'."
  16084. (org-cdlatex-mode 1))
  16085. (defun org-try-cdlatex-tab ()
  16086. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  16087. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  16088. - inside a LaTeX fragment, or
  16089. - after the first word in a line, where an abbreviation expansion could
  16090. insert a LaTeX environment."
  16091. (when org-cdlatex-mode
  16092. (cond
  16093. ;; Before any word on the line: No expansion possible.
  16094. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  16095. ;; Just after first word on the line: Expand it. Make sure it
  16096. ;; cannot happen on headlines, though.
  16097. ((save-excursion
  16098. (skip-chars-backward "a-zA-Z0-9*")
  16099. (skip-chars-backward " \t")
  16100. (and (bolp) (not (org-at-heading-p))))
  16101. (cdlatex-tab) t)
  16102. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  16103. (defun org-cdlatex-underscore-caret (&optional arg)
  16104. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  16105. Revert to the normal definition outside of these fragments."
  16106. (interactive "P")
  16107. (if (org-inside-LaTeX-fragment-p)
  16108. (call-interactively 'cdlatex-sub-superscript)
  16109. (let (org-cdlatex-mode)
  16110. (call-interactively (key-binding (vector last-input-event))))))
  16111. (defun org-cdlatex-math-modify (&optional arg)
  16112. "Execute `cdlatex-math-modify' in LaTeX fragments.
  16113. Revert to the normal definition outside of these fragments."
  16114. (interactive "P")
  16115. (if (org-inside-LaTeX-fragment-p)
  16116. (call-interactively 'cdlatex-math-modify)
  16117. (let (org-cdlatex-mode)
  16118. (call-interactively (key-binding (vector last-input-event))))))
  16119. ;;;; LaTeX fragments
  16120. (defvar org-latex-regexps
  16121. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  16122. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  16123. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  16124. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16125. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  16126. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  16127. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  16128. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  16129. "Regular expressions for matching embedded LaTeX.")
  16130. (defun org-inside-LaTeX-fragment-p ()
  16131. "Test if point is inside a LaTeX fragment.
  16132. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  16133. sequence appearing also before point.
  16134. Even though the matchers for math are configurable, this function assumes
  16135. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  16136. delimiters are skipped when they have been removed by customization.
  16137. The return value is nil, or a cons cell with the delimiter and the
  16138. position of this delimiter.
  16139. This function does a reasonably good job, but can locally be fooled by
  16140. for example currency specifications. For example it will assume being in
  16141. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  16142. fragments that are properly closed, but during editing, we have to live
  16143. with the uncertainty caused by missing closing delimiters. This function
  16144. looks only before point, not after."
  16145. (catch 'exit
  16146. (let ((pos (point))
  16147. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  16148. (lim (progn
  16149. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  16150. (point)))
  16151. dd-on str (start 0) m re)
  16152. (goto-char pos)
  16153. (when dodollar
  16154. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  16155. re (nth 1 (assoc "$" org-latex-regexps)))
  16156. (while (string-match re str start)
  16157. (cond
  16158. ((= (match-end 0) (length str))
  16159. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  16160. ((= (match-end 0) (- (length str) 5))
  16161. (throw 'exit nil))
  16162. (t (setq start (match-end 0))))))
  16163. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  16164. (goto-char pos)
  16165. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  16166. (and (match-beginning 2) (throw 'exit nil))
  16167. ;; count $$
  16168. (while (re-search-backward "\\$\\$" lim t)
  16169. (setq dd-on (not dd-on)))
  16170. (goto-char pos)
  16171. (if dd-on (cons "$$" m))))))
  16172. (defun org-inside-latex-macro-p ()
  16173. "Is point inside a LaTeX macro or its arguments?"
  16174. (save-match-data
  16175. (org-in-regexp
  16176. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  16177. (defvar org-latex-fragment-image-overlays nil
  16178. "List of overlays carrying the images of latex fragments.")
  16179. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  16180. (defun org-remove-latex-fragment-image-overlays ()
  16181. "Remove all overlays with LaTeX fragment images in current buffer."
  16182. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  16183. (setq org-latex-fragment-image-overlays nil))
  16184. (defun org-preview-latex-fragment (&optional subtree)
  16185. "Preview the LaTeX fragment at point, or all locally or globally.
  16186. If the cursor is in a LaTeX fragment, create the image and overlay
  16187. it over the source code. If there is no fragment at point, display
  16188. all fragments in the current text, from one headline to the next. With
  16189. prefix SUBTREE, display all fragments in the current subtree. With a
  16190. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  16191. the cursor is before the first headline,
  16192. display all fragments in the buffer.
  16193. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  16194. (interactive "P")
  16195. (unless buffer-file-name
  16196. (error "Can't preview LaTeX fragment in a non-file buffer"))
  16197. (org-remove-latex-fragment-image-overlays)
  16198. (save-excursion
  16199. (save-restriction
  16200. (let (beg end at msg)
  16201. (cond
  16202. ((or (equal subtree '(16))
  16203. (not (save-excursion
  16204. (re-search-backward org-outline-regexp-bol nil t))))
  16205. (setq beg (point-min) end (point-max)
  16206. msg "Creating images for buffer...%s"))
  16207. ((equal subtree '(4))
  16208. (org-back-to-heading)
  16209. (setq beg (point) end (org-end-of-subtree t)
  16210. msg "Creating images for subtree...%s"))
  16211. (t
  16212. (if (setq at (org-inside-LaTeX-fragment-p))
  16213. (goto-char (max (point-min) (- (cdr at) 2)))
  16214. (org-back-to-heading))
  16215. (setq beg (point) end (progn (outline-next-heading) (point))
  16216. msg (if at "Creating image...%s"
  16217. "Creating images for entry...%s"))))
  16218. (message msg "")
  16219. (narrow-to-region beg end)
  16220. (goto-char beg)
  16221. (org-format-latex
  16222. (concat org-latex-preview-ltxpng-directory (file-name-sans-extension
  16223. (file-name-nondirectory
  16224. buffer-file-name)))
  16225. default-directory 'overlays msg at 'forbuffer
  16226. org-latex-create-formula-image-program)
  16227. (message msg "done. Use `C-c C-c' to remove images.")))))
  16228. (defun org-format-latex (prefix &optional dir overlays msg at
  16229. forbuffer processing-type)
  16230. "Replace LaTeX fragments with links to an image, and produce images.
  16231. Some of the options can be changed using the variable
  16232. `org-format-latex-options'."
  16233. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  16234. (let* ((prefixnodir (file-name-nondirectory prefix))
  16235. (absprefix (expand-file-name prefix dir))
  16236. (todir (file-name-directory absprefix))
  16237. (opt org-format-latex-options)
  16238. (optnew org-format-latex-options)
  16239. (matchers (plist-get opt :matchers))
  16240. (re-list org-latex-regexps)
  16241. (cnt 0) txt hash link beg end re e checkdir
  16242. string
  16243. m n block-type block linkfile movefile ov)
  16244. ;; Check the different regular expressions
  16245. (while (setq e (pop re-list))
  16246. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  16247. block (if block-type "\n\n" ""))
  16248. (when (member m matchers)
  16249. (goto-char (point-min))
  16250. (while (re-search-forward re nil t)
  16251. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  16252. (or (not overlays)
  16253. (not (eq (get-char-property (match-beginning n)
  16254. 'org-overlay-type)
  16255. 'org-latex-overlay))))
  16256. (cond
  16257. ((eq processing-type 'verbatim))
  16258. ((eq processing-type 'mathjax)
  16259. ;; Prepare for MathJax processing.
  16260. (setq string (match-string n))
  16261. (when (member m '("$" "$1"))
  16262. (save-excursion
  16263. (delete-region (match-beginning n) (match-end n))
  16264. (goto-char (match-beginning n))
  16265. (insert (concat "\\(" (substring string 1 -1) "\\)")))))
  16266. ((or (eq processing-type 'dvipng)
  16267. (eq processing-type 'imagemagick))
  16268. ;; Process to an image.
  16269. (setq txt (match-string n)
  16270. beg (match-beginning n) end (match-end n)
  16271. cnt (1+ cnt))
  16272. (let ((face (face-at-point))
  16273. (fg (plist-get opt :foreground))
  16274. (bg (plist-get opt :background))
  16275. ;; Ensure full list is printed.
  16276. print-length print-level)
  16277. (when forbuffer
  16278. ;; Get the colors from the face at point.
  16279. (goto-char beg)
  16280. (when (eq fg 'auto)
  16281. (setq fg (face-attribute face :foreground nil 'default)))
  16282. (when (eq bg 'auto)
  16283. (setq bg (face-attribute face :background nil 'default)))
  16284. (setq optnew (copy-sequence opt))
  16285. (plist-put optnew :foreground fg)
  16286. (plist-put optnew :background bg))
  16287. (setq hash (sha1 (prin1-to-string
  16288. (list org-format-latex-header
  16289. org-latex-default-packages-alist
  16290. org-latex-packages-alist
  16291. org-format-latex-options
  16292. forbuffer txt fg bg)))
  16293. linkfile (format "%s_%s.png" prefix hash)
  16294. movefile (format "%s_%s.png" absprefix hash)))
  16295. (setq link (concat block "[[file:" linkfile "]]" block))
  16296. (if msg (message msg cnt))
  16297. (goto-char beg)
  16298. (unless checkdir ; Ensure the directory exists.
  16299. (setq checkdir t)
  16300. (or (file-directory-p todir) (make-directory todir t)))
  16301. (unless (file-exists-p movefile)
  16302. (org-create-formula-image
  16303. txt movefile optnew forbuffer processing-type))
  16304. (if overlays
  16305. (progn
  16306. (mapc (lambda (o)
  16307. (if (eq (overlay-get o 'org-overlay-type)
  16308. 'org-latex-overlay)
  16309. (delete-overlay o)))
  16310. (overlays-in beg end))
  16311. (setq ov (make-overlay beg end))
  16312. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  16313. (if (featurep 'xemacs)
  16314. (progn
  16315. (overlay-put ov 'invisible t)
  16316. (overlay-put
  16317. ov 'end-glyph
  16318. (make-glyph (vector 'png :file movefile))))
  16319. (overlay-put
  16320. ov 'display
  16321. (list 'image :type 'png :file movefile :ascent 'center)))
  16322. (push ov org-latex-fragment-image-overlays)
  16323. (goto-char end))
  16324. (delete-region beg end)
  16325. (insert (org-add-props link
  16326. (list 'org-latex-src
  16327. (replace-regexp-in-string
  16328. "\"" "" txt)
  16329. 'org-latex-src-embed-type
  16330. (if block-type 'paragraph 'character))))))
  16331. ((eq processing-type 'mathml)
  16332. ;; Process to MathML
  16333. (unless (save-match-data (org-format-latex-mathml-available-p))
  16334. (error "LaTeX to MathML converter not configured"))
  16335. (setq txt (match-string n)
  16336. beg (match-beginning n) end (match-end n)
  16337. cnt (1+ cnt))
  16338. (if msg (message msg cnt))
  16339. (goto-char beg)
  16340. (delete-region beg end)
  16341. (insert (org-format-latex-as-mathml
  16342. txt block-type prefix dir)))
  16343. (t
  16344. (error "Unknown conversion type %s for latex fragments"
  16345. processing-type)))))))))
  16346. (defun org-create-math-formula (latex-frag &optional mathml-file)
  16347. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  16348. Use `org-latex-to-mathml-convert-command'. If the conversion is
  16349. sucessful, return the portion between \"<math...> </math>\"
  16350. elements otherwise return nil. When MATHML-FILE is specified,
  16351. write the results in to that file. When invoked as an
  16352. interactive command, prompt for LATEX-FRAG, with initial value
  16353. set to the current active region and echo the results for user
  16354. inspection."
  16355. (interactive (list (let ((frag (when (org-region-active-p)
  16356. (buffer-substring-no-properties
  16357. (region-beginning) (region-end)))))
  16358. (read-string "LaTeX Fragment: " frag nil frag))))
  16359. (unless latex-frag (error "Invalid latex-frag"))
  16360. (let* ((tmp-in-file (file-relative-name
  16361. (make-temp-name (expand-file-name "ltxmathml-in"))))
  16362. (ignore (write-region latex-frag nil tmp-in-file))
  16363. (tmp-out-file (file-relative-name
  16364. (make-temp-name (expand-file-name "ltxmathml-out"))))
  16365. (cmd (format-spec
  16366. org-latex-to-mathml-convert-command
  16367. `((?j . ,(shell-quote-argument
  16368. (expand-file-name org-latex-to-mathml-jar-file)))
  16369. (?I . ,(shell-quote-argument tmp-in-file))
  16370. (?o . ,(shell-quote-argument tmp-out-file)))))
  16371. mathml shell-command-output)
  16372. (when (org-called-interactively-p 'any)
  16373. (unless (org-format-latex-mathml-available-p)
  16374. (error "LaTeX to MathML converter not configured")))
  16375. (message "Running %s" cmd)
  16376. (setq shell-command-output (shell-command-to-string cmd))
  16377. (setq mathml
  16378. (when (file-readable-p tmp-out-file)
  16379. (with-current-buffer (find-file-noselect tmp-out-file t)
  16380. (goto-char (point-min))
  16381. (when (re-search-forward
  16382. (concat
  16383. (regexp-quote
  16384. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  16385. "\\(.\\|\n\\)*"
  16386. (regexp-quote "</math>")) nil t)
  16387. (prog1 (match-string 0) (kill-buffer))))))
  16388. (cond
  16389. (mathml
  16390. (setq mathml
  16391. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  16392. (when mathml-file
  16393. (write-region mathml nil mathml-file))
  16394. (when (org-called-interactively-p 'any)
  16395. (message mathml)))
  16396. ((message "LaTeX to MathML conversion failed")
  16397. (message shell-command-output)))
  16398. (delete-file tmp-in-file)
  16399. (when (file-exists-p tmp-out-file)
  16400. (delete-file tmp-out-file))
  16401. mathml))
  16402. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  16403. prefix &optional dir)
  16404. "Use `org-create-math-formula' but check local cache first."
  16405. (let* ((absprefix (expand-file-name prefix dir))
  16406. (print-length nil) (print-level nil)
  16407. (formula-id (concat
  16408. "formula-"
  16409. (sha1
  16410. (prin1-to-string
  16411. (list latex-frag
  16412. org-latex-to-mathml-convert-command)))))
  16413. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  16414. (formula-cache-dir (file-name-directory formula-cache)))
  16415. (unless (file-directory-p formula-cache-dir)
  16416. (make-directory formula-cache-dir t))
  16417. (unless (file-exists-p formula-cache)
  16418. (org-create-math-formula latex-frag formula-cache))
  16419. (if (file-exists-p formula-cache)
  16420. ;; Successful conversion. Return the link to MathML file.
  16421. (org-add-props
  16422. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  16423. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  16424. 'org-latex-src-embed-type (if latex-frag-type
  16425. 'paragraph 'character)))
  16426. ;; Failed conversion. Return the LaTeX fragment verbatim
  16427. latex-frag)))
  16428. (defun org-create-formula-image (string tofile options buffer &optional type)
  16429. "Create an image from LaTeX source using dvipng or convert.
  16430. This function calls either `org-create-formula-image-with-dvipng'
  16431. or `org-create-formula-image-with-imagemagick' depending on the
  16432. value of `org-latex-create-formula-image-program' or on the value
  16433. of the optional TYPE variable.
  16434. Note: ultimately these two function should be combined as they
  16435. share a good deal of logic."
  16436. (org-check-external-command
  16437. "latex" "needed to convert LaTeX fragments to images")
  16438. (funcall
  16439. (case (or type org-latex-create-formula-image-program)
  16440. ('dvipng
  16441. (org-check-external-command
  16442. "dvipng" "needed to convert LaTeX fragments to images")
  16443. #'org-create-formula-image-with-dvipng)
  16444. ('imagemagick
  16445. (org-check-external-command
  16446. "convert" "you need to install imagemagick")
  16447. #'org-create-formula-image-with-imagemagick)
  16448. (t (error
  16449. "invalid value of `org-latex-create-formula-image-program'")))
  16450. string tofile options buffer))
  16451. ;; This function borrows from Ganesh Swami's latex2png.el
  16452. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  16453. "This calls dvipng."
  16454. (let* ((tmpdir (if (featurep 'xemacs)
  16455. (temp-directory)
  16456. temporary-file-directory))
  16457. (texfilebase (make-temp-name
  16458. (expand-file-name "orgtex" tmpdir)))
  16459. (texfile (concat texfilebase ".tex"))
  16460. (dvifile (concat texfilebase ".dvi"))
  16461. (pngfile (concat texfilebase ".png"))
  16462. (fnh (if (featurep 'xemacs)
  16463. (font-height (face-font 'default))
  16464. (face-attribute 'default :height nil)))
  16465. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16466. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16467. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16468. "Black"))
  16469. (bg (or (plist-get options (if buffer :background :html-background))
  16470. "Transparent")))
  16471. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  16472. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  16473. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  16474. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  16475. (with-temp-file texfile
  16476. (require 'ox-latex)
  16477. (insert (org-latex-guess-inputenc
  16478. (org-splice-latex-header
  16479. org-format-latex-header
  16480. org-latex-default-packages-alist
  16481. org-latex-packages-alist t)))
  16482. (insert "\n\\begin{document}\n" string "\n\\end{document}\n"))
  16483. (let ((dir default-directory))
  16484. (condition-case nil
  16485. (progn
  16486. (cd tmpdir)
  16487. (call-process "latex" nil nil nil texfile))
  16488. (error nil))
  16489. (cd dir))
  16490. (if (not (file-exists-p dvifile))
  16491. (progn (message "Failed to create dvi file from %s" texfile) nil)
  16492. (condition-case nil
  16493. (if (featurep 'xemacs)
  16494. (call-process "dvipng" nil nil nil
  16495. "-fg" fg "-bg" bg
  16496. "-T" "tight"
  16497. "-o" pngfile
  16498. dvifile)
  16499. (call-process "dvipng" nil nil nil
  16500. "-fg" fg "-bg" bg
  16501. "-D" dpi
  16502. ;;"-x" scale "-y" scale
  16503. "-T" "tight"
  16504. "-o" pngfile
  16505. dvifile))
  16506. (error nil))
  16507. (if (not (file-exists-p pngfile))
  16508. (if org-format-latex-signal-error
  16509. (error "Failed to create png file from %s" texfile)
  16510. (message "Failed to create png file from %s" texfile)
  16511. nil)
  16512. ;; Use the requested file name and clean up
  16513. (copy-file pngfile tofile 'replace)
  16514. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  16515. (if (file-exists-p (concat texfilebase e))
  16516. (delete-file (concat texfilebase e))))
  16517. pngfile))))
  16518. (defvar org-latex-pdf-process) ; From ox-latex.el
  16519. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  16520. "This calls convert, which is included into imagemagick."
  16521. (let* ((tmpdir (if (featurep 'xemacs)
  16522. (temp-directory)
  16523. temporary-file-directory))
  16524. (texfilebase (make-temp-name
  16525. (expand-file-name "orgtex" tmpdir)))
  16526. (texfile (concat texfilebase ".tex"))
  16527. (pdffile (concat texfilebase ".pdf"))
  16528. (pngfile (concat texfilebase ".png"))
  16529. (fnh (if (featurep 'xemacs)
  16530. (font-height (face-font 'default))
  16531. (face-attribute 'default :height nil)))
  16532. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  16533. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  16534. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  16535. "black"))
  16536. (bg (or (plist-get options (if buffer :background :html-background))
  16537. "white")))
  16538. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  16539. (setq fg (org-latex-color-format fg)))
  16540. (if (eq bg 'default) (setq bg (org-latex-color :background))
  16541. (setq bg (org-latex-color-format
  16542. (if (string= bg "Transparent") "white" bg))))
  16543. (with-temp-file texfile
  16544. (require 'ox-latex)
  16545. (insert (org-latex-guess-inputenc
  16546. (org-splice-latex-header
  16547. org-format-latex-header
  16548. org-latex-default-packages-alist
  16549. org-latex-packages-alist t)))
  16550. (insert "\n\\begin{document}\n"
  16551. "\\definecolor{fg}{rgb}{" fg "}\n"
  16552. "\\definecolor{bg}{rgb}{" bg "}\n"
  16553. "\n\\pagecolor{bg}\n"
  16554. "\n{\\color{fg}\n"
  16555. string
  16556. "\n}\n"
  16557. "\n\\end{document}\n"))
  16558. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  16559. (condition-case nil
  16560. (progn
  16561. (cd tmpdir)
  16562. (setq cmds org-latex-pdf-process)
  16563. (while cmds
  16564. (setq latex-frags-cmds (pop cmds))
  16565. (if (listp latex-frags-cmds)
  16566. (setq cmds nil)
  16567. (setq latex-frags-cmds (list (car org-latex-pdf-process)))))
  16568. (while latex-frags-cmds
  16569. (setq cmd (pop latex-frags-cmds))
  16570. (while (string-match "%b" cmd)
  16571. (setq cmd (replace-match
  16572. (save-match-data
  16573. (shell-quote-argument texfile))
  16574. t t cmd)))
  16575. (while (string-match "%f" cmd)
  16576. (setq cmd (replace-match
  16577. (save-match-data
  16578. (shell-quote-argument
  16579. (file-name-nondirectory texfile)))
  16580. t t cmd)))
  16581. (while (string-match "%o" cmd)
  16582. (setq cmd (replace-match
  16583. (save-match-data
  16584. (shell-quote-argument
  16585. (file-name-directory texfile)))
  16586. t t cmd)))
  16587. (setq cmd (split-string cmd))
  16588. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  16589. (error nil))
  16590. (cd dir))
  16591. (if (not (file-exists-p pdffile))
  16592. (progn (message "Failed to create pdf file from %s" texfile) nil)
  16593. (condition-case nil
  16594. (if (featurep 'xemacs)
  16595. (call-process "convert" nil nil nil
  16596. "-density" "96"
  16597. "-trim"
  16598. "-antialias"
  16599. pdffile
  16600. "-quality" "100"
  16601. ;; "-sharpen" "0x1.0"
  16602. pngfile)
  16603. (call-process "convert" nil nil nil
  16604. "-density" dpi
  16605. "-trim"
  16606. "-antialias"
  16607. pdffile
  16608. "-quality" "100"
  16609. ;; "-sharpen" "0x1.0"
  16610. pngfile))
  16611. (error nil))
  16612. (if (not (file-exists-p pngfile))
  16613. (if org-format-latex-signal-error
  16614. (error "Failed to create png file from %s" texfile)
  16615. (message "Failed to create png file from %s" texfile)
  16616. nil)
  16617. ;; Use the requested file name and clean up
  16618. (copy-file pngfile tofile 'replace)
  16619. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16620. (if (file-exists-p (concat texfilebase e))
  16621. (delete-file (concat texfilebase e))))
  16622. pngfile))))
  16623. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16624. "Fill a LaTeX header template TPL.
  16625. In the template, the following place holders will be recognized:
  16626. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16627. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16628. [PACKAGES] \\usepackage statements for PKG
  16629. [NO-PACKAGES] do not include PKG
  16630. [EXTRA] the string EXTRA
  16631. [NO-EXTRA] do not include EXTRA
  16632. For backward compatibility, if both the positive and the negative place
  16633. holder is missing, the positive one (without the \"NO-\") will be
  16634. assumed to be present at the end of the template.
  16635. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16636. EXTRA is a string.
  16637. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16638. (let (rpl (end ""))
  16639. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16640. (setq rpl (if (or (match-end 1) (not def-pkg))
  16641. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16642. tpl (replace-match rpl t t tpl))
  16643. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16644. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16645. (setq rpl (if (or (match-end 1) (not pkg))
  16646. "" (org-latex-packages-to-string pkg snippets-p t))
  16647. tpl (replace-match rpl t t tpl))
  16648. (if pkg (setq end
  16649. (concat end "\n"
  16650. (org-latex-packages-to-string pkg snippets-p)))))
  16651. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16652. (setq rpl (if (or (match-end 1) (not extra))
  16653. "" (concat extra "\n"))
  16654. tpl (replace-match rpl t t tpl))
  16655. (if (and extra (string-match "\\S-" extra))
  16656. (setq end (concat end "\n" extra))))
  16657. (if (string-match "\\S-" end)
  16658. (concat tpl "\n" end)
  16659. tpl)))
  16660. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16661. "Turn an alist of packages into a string with the \\usepackage macros."
  16662. (setq pkg (mapconcat (lambda(p)
  16663. (cond
  16664. ((stringp p) p)
  16665. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16666. (format "%% Package %s omitted" (cadr p)))
  16667. ((equal "" (car p))
  16668. (format "\\usepackage{%s}" (cadr p)))
  16669. (t
  16670. (format "\\usepackage[%s]{%s}"
  16671. (car p) (cadr p)))))
  16672. pkg
  16673. "\n"))
  16674. (if newline (concat pkg "\n") pkg))
  16675. (defun org-dvipng-color (attr)
  16676. "Return a RGB color specification for dvipng."
  16677. (apply 'format "rgb %s %s %s"
  16678. (mapcar 'org-normalize-color
  16679. (if (featurep 'xemacs)
  16680. (color-rgb-components
  16681. (face-property 'default
  16682. (cond ((eq attr :foreground) 'foreground)
  16683. ((eq attr :background) 'background))))
  16684. (color-values (face-attribute 'default attr nil))))))
  16685. (defun org-dvipng-color-format (color-name)
  16686. "Convert COLOR-NAME to a RGB color value for dvipng."
  16687. (apply 'format "rgb %s %s %s"
  16688. (mapcar 'org-normalize-color
  16689. (color-values color-name))))
  16690. (defun org-latex-color (attr)
  16691. "Return a RGB color for the LaTeX color package."
  16692. (apply 'format "%s,%s,%s"
  16693. (mapcar 'org-normalize-color
  16694. (if (featurep 'xemacs)
  16695. (color-rgb-components
  16696. (face-property 'default
  16697. (cond ((eq attr :foreground) 'foreground)
  16698. ((eq attr :background) 'background))))
  16699. (color-values (face-attribute 'default attr nil))))))
  16700. (defun org-latex-color-format (color-name)
  16701. "Convert COLOR-NAME to a RGB color value."
  16702. (apply 'format "%s,%s,%s"
  16703. (mapcar 'org-normalize-color
  16704. (color-values color-name))))
  16705. (defun org-normalize-color (value)
  16706. "Return string to be used as color value for an RGB component."
  16707. (format "%g" (/ value 65535.0)))
  16708. ;; Image display
  16709. (defvar org-inline-image-overlays nil)
  16710. (make-variable-buffer-local 'org-inline-image-overlays)
  16711. (defun org-toggle-inline-images (&optional include-linked)
  16712. "Toggle the display of inline images.
  16713. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16714. (interactive "P")
  16715. (if org-inline-image-overlays
  16716. (progn
  16717. (org-remove-inline-images)
  16718. (message "Inline image display turned off"))
  16719. (org-display-inline-images include-linked)
  16720. (if (and (org-called-interactively-p)
  16721. org-inline-image-overlays)
  16722. (message "%d images displayed inline"
  16723. (length org-inline-image-overlays))
  16724. (message "No images to display inline"))))
  16725. (defun org-redisplay-inline-images ()
  16726. "Refresh the display of inline images."
  16727. (interactive)
  16728. (if (not org-inline-image-overlays)
  16729. (org-toggle-inline-images)
  16730. (org-toggle-inline-images)
  16731. (org-toggle-inline-images)))
  16732. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16733. "Display inline images.
  16734. Normally only links without a description part are inlined, because this
  16735. is how it will work for export. When INCLUDE-LINKED is set, also links
  16736. with a description part will be inlined. This can be nice for a quick
  16737. look at those images, but it does not reflect what exported files will look
  16738. like.
  16739. When REFRESH is set, refresh existing images between BEG and END.
  16740. This will create new image displays only if necessary.
  16741. BEG and END default to the buffer boundaries."
  16742. (interactive "P")
  16743. (unless refresh
  16744. (org-remove-inline-images)
  16745. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16746. (save-excursion
  16747. (save-restriction
  16748. (widen)
  16749. (setq beg (or beg (point-min)) end (or end (point-max)))
  16750. (goto-char beg)
  16751. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16752. (substring (org-image-file-name-regexp) 0 -2)
  16753. "\\)\\]" (if include-linked "" "\\]")))
  16754. old file ov img type attrwidth width)
  16755. (while (re-search-forward re end t)
  16756. (setq old (get-char-property-and-overlay (match-beginning 1)
  16757. 'org-image-overlay)
  16758. file (expand-file-name
  16759. (concat (or (match-string 3) "") (match-string 4))))
  16760. (when (image-type-available-p 'imagemagick)
  16761. (setq attrwidth (if (or (listp org-image-actual-width)
  16762. (null org-image-actual-width))
  16763. (save-excursion
  16764. (save-match-data
  16765. (when (re-search-backward
  16766. "#\\+ATTR.*width=\"\\([^\"]+\\)\""
  16767. (save-excursion
  16768. (re-search-backward "^[ \t]*$\\|\\`" nil t)) t)
  16769. (string-to-number (match-string 1))))))
  16770. width (cond ((eq org-image-actual-width t) nil)
  16771. ((null org-image-actual-width) attrwidth)
  16772. ((numberp org-image-actual-width)
  16773. org-image-actual-width)
  16774. ((listp org-image-actual-width)
  16775. (or attrwidth (car org-image-actual-width))))
  16776. type (if width 'imagemagick)))
  16777. (when (file-exists-p file)
  16778. (if (and (car-safe old) refresh)
  16779. (image-refresh (overlay-get (cdr old) 'display))
  16780. (setq img (save-match-data (create-image file type nil :width width)))
  16781. (when img
  16782. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16783. (overlay-put ov 'display img)
  16784. (overlay-put ov 'face 'default)
  16785. (overlay-put ov 'org-image-overlay t)
  16786. (overlay-put ov 'modification-hooks
  16787. (list 'org-display-inline-remove-overlay))
  16788. (push ov org-inline-image-overlays)))))))))
  16789. (define-obsolete-function-alias
  16790. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  16791. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  16792. "Remove inline-display overlay if a corresponding region is modified."
  16793. (let ((inhibit-modification-hooks t))
  16794. (when (and ov after)
  16795. (delete ov org-inline-image-overlays)
  16796. (delete-overlay ov))))
  16797. (defun org-remove-inline-images ()
  16798. "Remove inline display of images."
  16799. (interactive)
  16800. (mapc 'delete-overlay org-inline-image-overlays)
  16801. (setq org-inline-image-overlays nil))
  16802. ;;;; Key bindings
  16803. ;; Outline functions from `outline-mode-prefix-map'
  16804. ;; that can be remapped in Org:
  16805. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16806. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16807. (define-key org-mode-map [remap outline-forward-same-level]
  16808. 'org-forward-heading-same-level)
  16809. (define-key org-mode-map [remap outline-backward-same-level]
  16810. 'org-backward-heading-same-level)
  16811. (define-key org-mode-map [remap show-branches]
  16812. 'org-kill-note-or-show-branches)
  16813. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16814. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16815. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16816. ;; Outline functions from `outline-mode-prefix-map' that can not
  16817. ;; be remapped in Org:
  16818. ;;
  16819. ;; - the column "key binding" shows whether the Outline function is still
  16820. ;; available in Org mode on the same key that it has been bound to in
  16821. ;; Outline mode:
  16822. ;; - "overridden": key used for a different functionality in Org mode
  16823. ;; - else: key still bound to the same Outline function in Org mode
  16824. ;;
  16825. ;; | Outline function | key binding | Org replacement |
  16826. ;; |------------------------------------+-------------+-----------------------|
  16827. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16828. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16829. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16830. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  16831. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16832. ;; | `show-entry' | overridden | no replacement |
  16833. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16834. ;; | `show-branches' | `C-c C-k' | still same function |
  16835. ;; | `show-subtree' | overridden | visibility cycling |
  16836. ;; | `show-all' | overridden | no replacement |
  16837. ;; | `hide-subtree' | overridden | visibility cycling |
  16838. ;; | `hide-body' | overridden | no replacement |
  16839. ;; | `hide-entry' | overridden | visibility cycling |
  16840. ;; | `hide-leaves' | overridden | no replacement |
  16841. ;; | `hide-sublevels' | overridden | no replacement |
  16842. ;; | `hide-other' | overridden | no replacement |
  16843. ;; Make `C-c C-x' a prefix key
  16844. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16845. ;; TAB key with modifiers
  16846. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16847. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16848. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16849. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16850. ;; The following line is necessary under Suse GNU/Linux
  16851. (unless (featurep 'xemacs)
  16852. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16853. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16854. (define-key org-mode-map [backtab] 'org-shifttab)
  16855. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16856. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16857. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16858. ;; Cursor keys with modifiers
  16859. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16860. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16861. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16862. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16863. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16864. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16865. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16866. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16867. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16868. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16869. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16870. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16871. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16872. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16873. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16874. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16875. ;; Babel keys
  16876. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16877. (mapc (lambda (pair)
  16878. (define-key org-babel-map (car pair) (cdr pair)))
  16879. org-babel-key-bindings)
  16880. ;;; Extra keys for tty access.
  16881. ;; We only set them when really needed because otherwise the
  16882. ;; menus don't show the simple keys
  16883. (when (or org-use-extra-keys
  16884. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16885. (not window-system))
  16886. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16887. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16888. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16889. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16890. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16891. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16892. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16893. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16894. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16895. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16896. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16897. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16898. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16899. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16900. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16901. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16902. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16903. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16904. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16905. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16906. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16907. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16908. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16909. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16910. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16911. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16912. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16913. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16914. ;; All the other keys
  16915. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16916. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16917. (if (boundp 'narrow-map)
  16918. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16919. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16920. (if (boundp 'narrow-map)
  16921. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16922. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16923. (if (boundp 'narrow-map)
  16924. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16925. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16926. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  16927. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  16928. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  16929. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  16930. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  16931. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  16932. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  16933. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16934. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16935. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16936. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16937. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16938. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16939. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16940. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16941. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16942. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16943. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16944. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16945. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16946. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16947. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  16948. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16949. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16950. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16951. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16952. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16953. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16954. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16955. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16956. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16957. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16958. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16959. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  16960. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16961. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16962. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16963. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16964. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16965. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16966. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16967. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16968. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16969. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16970. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16971. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16972. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16973. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16974. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16975. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16976. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16977. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16978. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16979. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16980. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16981. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16982. (org-defkey org-mode-map "\C-m" 'org-return)
  16983. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16984. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16985. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16986. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16987. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16988. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16989. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16990. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16991. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16992. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16993. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16994. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16995. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16996. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  16997. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16998. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16999. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  17000. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  17001. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  17002. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  17003. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  17004. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  17005. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  17006. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  17007. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  17008. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  17009. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  17010. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  17011. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  17012. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  17013. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  17014. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  17015. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  17016. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  17017. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  17018. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  17019. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  17020. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  17021. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  17022. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  17023. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  17024. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  17025. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  17026. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  17027. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  17028. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  17029. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  17030. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  17031. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  17032. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  17033. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  17034. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  17035. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  17036. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  17037. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  17038. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  17039. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  17040. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  17041. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  17042. (when (featurep 'xemacs)
  17043. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  17044. (defconst org-speed-commands-default
  17045. '(
  17046. ("Outline Navigation")
  17047. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  17048. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  17049. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  17050. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  17051. ("u" . (org-speed-move-safe 'outline-up-heading))
  17052. ("j" . org-goto)
  17053. ("g" . (org-refile t))
  17054. ("Outline Visibility")
  17055. ("c" . org-cycle)
  17056. ("C" . org-shifttab)
  17057. (" " . org-display-outline-path)
  17058. ("=" . org-columns)
  17059. ("Outline Structure Editing")
  17060. ("U" . org-shiftmetaup)
  17061. ("D" . org-shiftmetadown)
  17062. ("r" . org-metaright)
  17063. ("l" . org-metaleft)
  17064. ("R" . org-shiftmetaright)
  17065. ("L" . org-shiftmetaleft)
  17066. ("i" . (progn (forward-char 1) (call-interactively
  17067. 'org-insert-heading-respect-content)))
  17068. ("^" . org-sort)
  17069. ("w" . org-refile)
  17070. ("a" . org-archive-subtree-default-with-confirmation)
  17071. ("." . org-mark-subtree)
  17072. ("#" . org-toggle-comment)
  17073. ("Clock Commands")
  17074. ("I" . org-clock-in)
  17075. ("O" . org-clock-out)
  17076. ("Meta Data Editing")
  17077. ("t" . org-todo)
  17078. ("," . (org-priority))
  17079. ("0" . (org-priority ?\ ))
  17080. ("1" . (org-priority ?A))
  17081. ("2" . (org-priority ?B))
  17082. ("3" . (org-priority ?C))
  17083. (":" . org-set-tags-command)
  17084. ("e" . org-set-effort)
  17085. ("E" . org-inc-effort)
  17086. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  17087. (org-entry-put (point) "APPT_WARNTIME" m)))
  17088. ("Agenda Views etc")
  17089. ("v" . org-agenda)
  17090. ("/" . org-sparse-tree)
  17091. ("Misc")
  17092. ("o" . org-open-at-point)
  17093. ("?" . org-speed-command-help)
  17094. ("<" . (org-agenda-set-restriction-lock 'subtree))
  17095. (">" . (org-agenda-remove-restriction-lock))
  17096. )
  17097. "The default speed commands.")
  17098. (defun org-print-speed-command (e)
  17099. (if (> (length (car e)) 1)
  17100. (progn
  17101. (princ "\n")
  17102. (princ (car e))
  17103. (princ "\n")
  17104. (princ (make-string (length (car e)) ?-))
  17105. (princ "\n"))
  17106. (princ (car e))
  17107. (princ " ")
  17108. (if (symbolp (cdr e))
  17109. (princ (symbol-name (cdr e)))
  17110. (prin1 (cdr e)))
  17111. (princ "\n")))
  17112. (defun org-speed-command-help ()
  17113. "Show the available speed commands."
  17114. (interactive)
  17115. (if (not org-use-speed-commands)
  17116. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  17117. (with-output-to-temp-buffer "*Help*"
  17118. (princ "User-defined Speed commands\n===========================\n")
  17119. (mapc 'org-print-speed-command org-speed-commands-user)
  17120. (princ "\n")
  17121. (princ "Built-in Speed commands\n=======================\n")
  17122. (mapc 'org-print-speed-command org-speed-commands-default))
  17123. (with-current-buffer "*Help*"
  17124. (setq truncate-lines t))))
  17125. (defun org-speed-move-safe (cmd)
  17126. "Execute CMD, but make sure that the cursor always ends up in a headline.
  17127. If not, return to the original position and throw an error."
  17128. (interactive)
  17129. (let ((pos (point)))
  17130. (call-interactively cmd)
  17131. (unless (and (bolp) (org-at-heading-p))
  17132. (goto-char pos)
  17133. (error "Boundary reached while executing %s" cmd))))
  17134. (defvar org-self-insert-command-undo-counter 0)
  17135. (defvar org-table-auto-blank-field) ; defined in org-table.el
  17136. (defvar org-speed-command nil)
  17137. (define-obsolete-function-alias
  17138. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  17139. (defun org-speed-command-activate (keys)
  17140. "Hook for activating single-letter speed commands.
  17141. `org-speed-commands-default' specifies a minimal command set.
  17142. Use `org-speed-commands-user' for further customization."
  17143. (when (or (and (bolp) (looking-at org-outline-regexp))
  17144. (and (functionp org-use-speed-commands)
  17145. (funcall org-use-speed-commands)))
  17146. (cdr (assoc keys (append org-speed-commands-user
  17147. org-speed-commands-default)))))
  17148. (define-obsolete-function-alias
  17149. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  17150. (defun org-babel-speed-command-activate (keys)
  17151. "Hook for activating single-letter code block commands."
  17152. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  17153. (cdr (assoc keys org-babel-key-bindings))))
  17154. (defcustom org-speed-command-hook
  17155. '(org-speed-command-default-hook org-babel-speed-command-hook)
  17156. "Hook for activating speed commands at strategic locations.
  17157. Hook functions are called in sequence until a valid handler is
  17158. found.
  17159. Each hook takes a single argument, a user-pressed command key
  17160. which is also a `self-insert-command' from the global map.
  17161. Within the hook, examine the cursor position and the command key
  17162. and return nil or a valid handler as appropriate. Handler could
  17163. be one of an interactive command, a function, or a form.
  17164. Set `org-use-speed-commands' to non-nil value to enable this
  17165. hook. The default setting is `org-speed-command-activate'."
  17166. :group 'org-structure
  17167. :version "24.1"
  17168. :type 'hook)
  17169. (defun org-self-insert-command (N)
  17170. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  17171. If the cursor is in a table looking at whitespace, the whitespace is
  17172. overwritten, and the table is not marked as requiring realignment."
  17173. (interactive "p")
  17174. (org-check-before-invisible-edit 'insert)
  17175. (cond
  17176. ((and org-use-speed-commands
  17177. (setq org-speed-command
  17178. (run-hook-with-args-until-success
  17179. 'org-speed-command-hook (this-command-keys))))
  17180. (cond
  17181. ((commandp org-speed-command)
  17182. (setq this-command org-speed-command)
  17183. (call-interactively org-speed-command))
  17184. ((functionp org-speed-command)
  17185. (funcall org-speed-command))
  17186. ((and org-speed-command (listp org-speed-command))
  17187. (eval org-speed-command))
  17188. (t (let (org-use-speed-commands)
  17189. (call-interactively 'org-self-insert-command)))))
  17190. ((and
  17191. (org-table-p)
  17192. (progn
  17193. ;; check if we blank the field, and if that triggers align
  17194. (and (featurep 'org-table) org-table-auto-blank-field
  17195. (member last-command
  17196. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  17197. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  17198. ;; got extra space, this field does not determine column width
  17199. (let (org-table-may-need-update) (org-table-blank-field))
  17200. ;; no extra space, this field may determine column width
  17201. (org-table-blank-field)))
  17202. t)
  17203. (eq N 1)
  17204. (looking-at "[^|\n]* |"))
  17205. (let (org-table-may-need-update)
  17206. (goto-char (1- (match-end 0)))
  17207. (backward-delete-char 1)
  17208. (goto-char (match-beginning 0))
  17209. (self-insert-command N)))
  17210. (t
  17211. (setq org-table-may-need-update t)
  17212. (self-insert-command N)
  17213. (org-fix-tags-on-the-fly)
  17214. (if org-self-insert-cluster-for-undo
  17215. (if (not (eq last-command 'org-self-insert-command))
  17216. (setq org-self-insert-command-undo-counter 1)
  17217. (if (>= org-self-insert-command-undo-counter 20)
  17218. (setq org-self-insert-command-undo-counter 1)
  17219. (and (> org-self-insert-command-undo-counter 0)
  17220. buffer-undo-list (listp buffer-undo-list)
  17221. (not (cadr buffer-undo-list)) ; remove nil entry
  17222. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  17223. (setq org-self-insert-command-undo-counter
  17224. (1+ org-self-insert-command-undo-counter))))))))
  17225. (defun org-check-before-invisible-edit (kind)
  17226. "Check is editing if kind KIND would be dangerous with invisible text around.
  17227. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  17228. ;; First, try to get out of here as quickly as possible, to reduce overhead
  17229. (if (and org-catch-invisible-edits
  17230. (or (not (boundp 'visible-mode)) (not visible-mode))
  17231. (or (get-char-property (point) 'invisible)
  17232. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  17233. ;; OK, we need to take a closer look
  17234. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  17235. (invisible-before-point (if (bobp) nil (get-char-property
  17236. (1- (point)) 'invisible)))
  17237. (border-and-ok-direction
  17238. (or
  17239. ;; Check if we are acting predictably before invisible text
  17240. (and invisible-at-point (not invisible-before-point)
  17241. (memq kind '(insert delete-backward)))
  17242. ;; Check if we are acting predictably after invisible text
  17243. ;; This works not well, and I have turned it off. It seems
  17244. ;; better to always show and stop after invisible text.
  17245. ;; (and (not invisible-at-point) invisible-before-point
  17246. ;; (memq kind '(insert delete)))
  17247. )))
  17248. (when (or (memq invisible-at-point '(outline org-hide-block t))
  17249. (memq invisible-before-point '(outline org-hide-block t)))
  17250. (if (eq org-catch-invisible-edits 'error)
  17251. (error "Editing in invisible areas is prohibited - make visible first"))
  17252. (if (and org-custom-properties-overlays
  17253. (y-or-n-p "Display invisible properties in this buffer? "))
  17254. (org-toggle-custom-properties-visibility)
  17255. ;; Make the area visible
  17256. (save-excursion
  17257. (if invisible-before-point
  17258. (goto-char (previous-single-char-property-change
  17259. (point) 'invisible)))
  17260. (org-cycle))
  17261. (cond
  17262. ((eq org-catch-invisible-edits 'show)
  17263. ;; That's it, we do the edit after showing
  17264. (message
  17265. "Unfolding invisible region around point before editing")
  17266. (sit-for 1))
  17267. ((and (eq org-catch-invisible-edits 'smart)
  17268. border-and-ok-direction)
  17269. (message "Unfolding invisible region around point before editing"))
  17270. (t
  17271. ;; Don't do the edit, make the user repeat it in full visibility
  17272. (error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  17273. (defun org-fix-tags-on-the-fly ()
  17274. (when (and (equal (char-after (point-at-bol)) ?*)
  17275. (org-at-heading-p))
  17276. (org-align-tags-here org-tags-column)))
  17277. (defun org-delete-backward-char (N)
  17278. "Like `delete-backward-char', insert whitespace at field end in tables.
  17279. When deleting backwards, in tables this function will insert whitespace in
  17280. front of the next \"|\" separator, to keep the table aligned. The table will
  17281. still be marked for re-alignment if the field did fill the entire column,
  17282. because, in this case the deletion might narrow the column."
  17283. (interactive "p")
  17284. (save-match-data
  17285. (org-check-before-invisible-edit 'delete-backward)
  17286. (if (and (org-table-p)
  17287. (eq N 1)
  17288. (string-match "|" (buffer-substring (point-at-bol) (point)))
  17289. (looking-at ".*?|"))
  17290. (let ((pos (point))
  17291. (noalign (looking-at "[^|\n\r]* |"))
  17292. (c org-table-may-need-update))
  17293. (backward-delete-char N)
  17294. (if (not overwrite-mode)
  17295. (progn
  17296. (skip-chars-forward "^|")
  17297. (insert " ")
  17298. (goto-char (1- pos))))
  17299. ;; noalign: if there were two spaces at the end, this field
  17300. ;; does not determine the width of the column.
  17301. (if noalign (setq org-table-may-need-update c)))
  17302. (backward-delete-char N)
  17303. (org-fix-tags-on-the-fly))))
  17304. (defun org-delete-char (N)
  17305. "Like `delete-char', but insert whitespace at field end in tables.
  17306. When deleting characters, in tables this function will insert whitespace in
  17307. front of the next \"|\" separator, to keep the table aligned. The table will
  17308. still be marked for re-alignment if the field did fill the entire column,
  17309. because, in this case the deletion might narrow the column."
  17310. (interactive "p")
  17311. (save-match-data
  17312. (org-check-before-invisible-edit 'delete)
  17313. (if (and (org-table-p)
  17314. (not (bolp))
  17315. (not (= (char-after) ?|))
  17316. (eq N 1))
  17317. (if (looking-at ".*?|")
  17318. (let ((pos (point))
  17319. (noalign (looking-at "[^|\n\r]* |"))
  17320. (c org-table-may-need-update))
  17321. (replace-match (concat
  17322. (substring (match-string 0) 1 -1)
  17323. " |"))
  17324. (goto-char pos)
  17325. ;; noalign: if there were two spaces at the end, this field
  17326. ;; does not determine the width of the column.
  17327. (if noalign (setq org-table-may-need-update c)))
  17328. (delete-char N))
  17329. (delete-char N)
  17330. (org-fix-tags-on-the-fly))))
  17331. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  17332. (put 'org-self-insert-command 'delete-selection t)
  17333. (put 'orgtbl-self-insert-command 'delete-selection t)
  17334. (put 'org-delete-char 'delete-selection 'supersede)
  17335. (put 'org-delete-backward-char 'delete-selection 'supersede)
  17336. (put 'org-yank 'delete-selection 'yank)
  17337. ;; Make `flyspell-mode' delay after some commands
  17338. (put 'org-self-insert-command 'flyspell-delayed t)
  17339. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  17340. (put 'org-delete-char 'flyspell-delayed t)
  17341. (put 'org-delete-backward-char 'flyspell-delayed t)
  17342. ;; Make pabbrev-mode expand after org-mode commands
  17343. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  17344. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  17345. ;; How to do this: Measure non-white length of current string
  17346. ;; If equal to column width, we should realign.
  17347. (defun org-remap (map &rest commands)
  17348. "In MAP, remap the functions given in COMMANDS.
  17349. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  17350. (let (new old)
  17351. (while commands
  17352. (setq old (pop commands) new (pop commands))
  17353. (if (fboundp 'command-remapping)
  17354. (org-defkey map (vector 'remap old) new)
  17355. (substitute-key-definition old new map global-map)))))
  17356. (when (eq org-enable-table-editor 'optimized)
  17357. ;; If the user wants maximum table support, we need to hijack
  17358. ;; some standard editing functions
  17359. (org-remap org-mode-map
  17360. 'self-insert-command 'org-self-insert-command
  17361. 'delete-char 'org-delete-char
  17362. 'delete-backward-char 'org-delete-backward-char)
  17363. (org-defkey org-mode-map "|" 'org-force-self-insert))
  17364. (defvar org-ctrl-c-ctrl-c-hook nil
  17365. "Hook for functions attaching themselves to `C-c C-c'.
  17366. This can be used to add additional functionality to the C-c C-c
  17367. key which executes context-dependent commands. This hook is run
  17368. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  17369. run after the last test.
  17370. Each function will be called with no arguments. The function
  17371. must check if the context is appropriate for it to act. If yes,
  17372. it should do its thing and then return a non-nil value. If the
  17373. context is wrong, just do nothing and return nil.")
  17374. (defvar org-ctrl-c-ctrl-c-final-hook nil
  17375. "Hook for functions attaching themselves to `C-c C-c'.
  17376. This can be used to add additional functionality to the C-c C-c
  17377. key which executes context-dependent commands. This hook is run
  17378. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  17379. before the first test.
  17380. Each function will be called with no arguments. The function
  17381. must check if the context is appropriate for it to act. If yes,
  17382. it should do its thing and then return a non-nil value. If the
  17383. context is wrong, just do nothing and return nil.")
  17384. (defvar org-tab-first-hook nil
  17385. "Hook for functions to attach themselves to TAB.
  17386. See `org-ctrl-c-ctrl-c-hook' for more information.
  17387. This hook runs as the first action when TAB is pressed, even before
  17388. `org-cycle' messes around with the `outline-regexp' to cater for
  17389. inline tasks and plain list item folding.
  17390. If any function in this hook returns t, any other actions that
  17391. would have been caused by TAB (such as table field motion or visibility
  17392. cycling) will not occur.")
  17393. (defvar org-tab-after-check-for-table-hook nil
  17394. "Hook for functions to attach themselves to TAB.
  17395. See `org-ctrl-c-ctrl-c-hook' for more information.
  17396. This hook runs after it has been established that the cursor is not in a
  17397. table, but before checking if the cursor is in a headline or if global cycling
  17398. should be done.
  17399. If any function in this hook returns t, not other actions like visibility
  17400. cycling will be done.")
  17401. (defvar org-tab-after-check-for-cycling-hook nil
  17402. "Hook for functions to attach themselves to TAB.
  17403. See `org-ctrl-c-ctrl-c-hook' for more information.
  17404. This hook runs after it has been established that not table field motion and
  17405. not visibility should be done because of current context. This is probably
  17406. the place where a package like yasnippets can hook in.")
  17407. (defvar org-tab-before-tab-emulation-hook nil
  17408. "Hook for functions to attach themselves to TAB.
  17409. See `org-ctrl-c-ctrl-c-hook' for more information.
  17410. This hook runs after every other options for TAB have been exhausted, but
  17411. before indentation and \t insertion takes place.")
  17412. (defvar org-metaleft-hook nil
  17413. "Hook for functions attaching themselves to `M-left'.
  17414. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17415. (defvar org-metaright-hook nil
  17416. "Hook for functions attaching themselves to `M-right'.
  17417. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17418. (defvar org-metaup-hook nil
  17419. "Hook for functions attaching themselves to `M-up'.
  17420. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17421. (defvar org-metadown-hook nil
  17422. "Hook for functions attaching themselves to `M-down'.
  17423. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17424. (defvar org-shiftmetaleft-hook nil
  17425. "Hook for functions attaching themselves to `M-S-left'.
  17426. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17427. (defvar org-shiftmetaright-hook nil
  17428. "Hook for functions attaching themselves to `M-S-right'.
  17429. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17430. (defvar org-shiftmetaup-hook nil
  17431. "Hook for functions attaching themselves to `M-S-up'.
  17432. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17433. (defvar org-shiftmetadown-hook nil
  17434. "Hook for functions attaching themselves to `M-S-down'.
  17435. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17436. (defvar org-metareturn-hook nil
  17437. "Hook for functions attaching themselves to `M-RET'.
  17438. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17439. (defvar org-shiftup-hook nil
  17440. "Hook for functions attaching themselves to `S-up'.
  17441. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17442. (defvar org-shiftup-final-hook nil
  17443. "Hook for functions attaching themselves to `S-up'.
  17444. This one runs after all other options except shift-select have been excluded.
  17445. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17446. (defvar org-shiftdown-hook nil
  17447. "Hook for functions attaching themselves to `S-down'.
  17448. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17449. (defvar org-shiftdown-final-hook nil
  17450. "Hook for functions attaching themselves to `S-down'.
  17451. This one runs after all other options except shift-select have been excluded.
  17452. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17453. (defvar org-shiftleft-hook nil
  17454. "Hook for functions attaching themselves to `S-left'.
  17455. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17456. (defvar org-shiftleft-final-hook nil
  17457. "Hook for functions attaching themselves to `S-left'.
  17458. This one runs after all other options except shift-select have been excluded.
  17459. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17460. (defvar org-shiftright-hook nil
  17461. "Hook for functions attaching themselves to `S-right'.
  17462. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17463. (defvar org-shiftright-final-hook nil
  17464. "Hook for functions attaching themselves to `S-right'.
  17465. This one runs after all other options except shift-select have been excluded.
  17466. See `org-ctrl-c-ctrl-c-hook' for more information.")
  17467. (defun org-modifier-cursor-error ()
  17468. "Throw an error, a modified cursor command was applied in wrong context."
  17469. (error "This command is active in special context like tables, headlines or items"))
  17470. (defun org-shiftselect-error ()
  17471. "Throw an error because Shift-Cursor command was applied in wrong context."
  17472. (if (and (boundp 'shift-select-mode) shift-select-mode)
  17473. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  17474. (error "This command works only in special context like headlines or timestamps")))
  17475. (defun org-call-for-shift-select (cmd)
  17476. (let ((this-command-keys-shift-translated t))
  17477. (call-interactively cmd)))
  17478. (defun org-shifttab (&optional arg)
  17479. "Global visibility cycling or move to previous table field.
  17480. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  17481. on context.
  17482. See the individual commands for more information."
  17483. (interactive "P")
  17484. (cond
  17485. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  17486. ((integerp arg)
  17487. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  17488. (message "Content view to level: %d" arg)
  17489. (org-content (prefix-numeric-value arg2))
  17490. (setq org-cycle-global-status 'overview)))
  17491. (t (call-interactively 'org-global-cycle))))
  17492. (defun org-shiftmetaleft ()
  17493. "Promote subtree or delete table column.
  17494. Calls `org-promote-subtree', `org-outdent-item-tree', or
  17495. `org-table-delete-column', depending on context. See the
  17496. individual commands for more information."
  17497. (interactive)
  17498. (cond
  17499. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  17500. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  17501. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  17502. ((if (not (org-region-active-p)) (org-at-item-p)
  17503. (save-excursion (goto-char (region-beginning))
  17504. (org-at-item-p)))
  17505. (call-interactively 'org-outdent-item-tree))
  17506. (t (org-modifier-cursor-error))))
  17507. (defun org-shiftmetaright ()
  17508. "Demote subtree or insert table column.
  17509. Calls `org-demote-subtree', `org-indent-item-tree', or
  17510. `org-table-insert-column', depending on context. See the
  17511. individual commands for more information."
  17512. (interactive)
  17513. (cond
  17514. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  17515. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  17516. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  17517. ((if (not (org-region-active-p)) (org-at-item-p)
  17518. (save-excursion (goto-char (region-beginning))
  17519. (org-at-item-p)))
  17520. (call-interactively 'org-indent-item-tree))
  17521. (t (org-modifier-cursor-error))))
  17522. (defun org-shiftmetaup (&optional arg)
  17523. "Move subtree up or kill table row.
  17524. Calls `org-move-subtree-up' or `org-table-kill-row' or
  17525. `org-move-item-up' or `org-timestamp-up', depending on context.
  17526. See the individual commands for more information."
  17527. (interactive "P")
  17528. (cond
  17529. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  17530. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  17531. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17532. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17533. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17534. (call-interactively 'org-timestamp-up)))
  17535. (t (org-modifier-cursor-error))))
  17536. (defun org-shiftmetadown (&optional arg)
  17537. "Move subtree down or insert table row.
  17538. Calls `org-move-subtree-down' or `org-table-insert-row' or
  17539. `org-move-item-down' or `org-timestamp-up', depending on context.
  17540. See the individual commands for more information."
  17541. (interactive "P")
  17542. (cond
  17543. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  17544. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  17545. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17546. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17547. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  17548. (call-interactively 'org-timestamp-down)))
  17549. (t (org-modifier-cursor-error))))
  17550. (defsubst org-hidden-tree-error ()
  17551. (error
  17552. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  17553. (defun org-metaleft (&optional arg)
  17554. "Promote heading or move table column to left.
  17555. Calls `org-do-promote' or `org-table-move-column', depending on context.
  17556. With no specific context, calls the Emacs default `backward-word'.
  17557. See the individual commands for more information."
  17558. (interactive "P")
  17559. (cond
  17560. ((run-hook-with-args-until-success 'org-metaleft-hook))
  17561. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  17562. ((org-with-limited-levels
  17563. (or (org-at-heading-p)
  17564. (and (org-region-active-p)
  17565. (save-excursion
  17566. (goto-char (region-beginning))
  17567. (org-at-heading-p)))))
  17568. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17569. (call-interactively 'org-do-promote))
  17570. ;; At an inline task.
  17571. ((org-at-heading-p)
  17572. (call-interactively 'org-inlinetask-promote))
  17573. ((or (org-at-item-p)
  17574. (and (org-region-active-p)
  17575. (save-excursion
  17576. (goto-char (region-beginning))
  17577. (org-at-item-p))))
  17578. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17579. (call-interactively 'org-outdent-item))
  17580. (t (call-interactively 'backward-word))))
  17581. (defun org-metaright (&optional arg)
  17582. "Demote a subtree, a list item or move table column to right.
  17583. In front of a drawer or a block keyword, indent it correctly.
  17584. With no specific context, calls the Emacs default `forward-word'.
  17585. See the individual commands for more information."
  17586. (interactive "P")
  17587. (cond
  17588. ((run-hook-with-args-until-success 'org-metaright-hook))
  17589. ((org-at-table-p) (call-interactively 'org-table-move-column))
  17590. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  17591. ((org-at-block-p) (call-interactively 'org-indent-block))
  17592. ((org-with-limited-levels
  17593. (or (org-at-heading-p)
  17594. (and (org-region-active-p)
  17595. (save-excursion
  17596. (goto-char (region-beginning))
  17597. (org-at-heading-p)))))
  17598. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  17599. (call-interactively 'org-do-demote))
  17600. ;; At an inline task.
  17601. ((org-at-heading-p)
  17602. (call-interactively 'org-inlinetask-demote))
  17603. ((or (org-at-item-p)
  17604. (and (org-region-active-p)
  17605. (save-excursion
  17606. (goto-char (region-beginning))
  17607. (org-at-item-p))))
  17608. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  17609. (call-interactively 'org-indent-item))
  17610. (t (call-interactively 'forward-word))))
  17611. (defun org-check-for-hidden (what)
  17612. "Check if there are hidden headlines/items in the current visual line.
  17613. WHAT can be either `headlines' or `items'. If the current line is
  17614. an outline or item heading and it has a folded subtree below it,
  17615. this function returns t, nil otherwise."
  17616. (let ((re (cond
  17617. ((eq what 'headlines) org-outline-regexp-bol)
  17618. ((eq what 'items) (org-item-beginning-re))
  17619. (t (error "This should not happen"))))
  17620. beg end)
  17621. (save-excursion
  17622. (catch 'exit
  17623. (unless (org-region-active-p)
  17624. (setq beg (point-at-bol))
  17625. (beginning-of-line 2)
  17626. (while (and (not (eobp)) ;; this is like `next-line'
  17627. (get-char-property (1- (point)) 'invisible))
  17628. (beginning-of-line 2))
  17629. (setq end (point))
  17630. (goto-char beg)
  17631. (goto-char (point-at-eol))
  17632. (setq end (max end (point)))
  17633. (while (re-search-forward re end t)
  17634. (if (get-char-property (match-beginning 0) 'invisible)
  17635. (throw 'exit t))))
  17636. nil))))
  17637. (defun org-metaup (&optional arg)
  17638. "Move subtree up or move table row up.
  17639. Calls `org-move-subtree-up' or `org-table-move-row' or
  17640. `org-move-item-up', depending on context. See the individual commands
  17641. for more information."
  17642. (interactive "P")
  17643. (cond
  17644. ((run-hook-with-args-until-success 'org-metaup-hook))
  17645. ((org-region-active-p)
  17646. (let* ((a (min (region-beginning) (region-end)))
  17647. (b (1- (max (region-beginning) (region-end))))
  17648. (c (save-excursion (goto-char a)
  17649. (move-beginning-of-line 0)))
  17650. (d (save-excursion (goto-char a)
  17651. (move-end-of-line 0) (point))))
  17652. (transpose-regions a b c d)
  17653. (goto-char c)))
  17654. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  17655. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  17656. ((org-at-item-p) (call-interactively 'org-move-item-up))
  17657. (t (org-drag-element-backward))))
  17658. (defun org-metadown (&optional arg)
  17659. "Move subtree down or move table row down.
  17660. Calls `org-move-subtree-down' or `org-table-move-row' or
  17661. `org-move-item-down', depending on context. See the individual
  17662. commands for more information."
  17663. (interactive "P")
  17664. (cond
  17665. ((run-hook-with-args-until-success 'org-metadown-hook))
  17666. ((org-region-active-p)
  17667. (let* ((a (min (region-beginning) (region-end)))
  17668. (b (max (region-beginning) (region-end)))
  17669. (c (save-excursion (goto-char b)
  17670. (move-beginning-of-line 1)))
  17671. (d (save-excursion (goto-char b)
  17672. (move-end-of-line 1) (1+ (point)))))
  17673. (transpose-regions a b c d)
  17674. (goto-char d)))
  17675. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17676. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17677. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17678. (t (org-drag-element-forward))))
  17679. (defun org-shiftup (&optional arg)
  17680. "Increase item in timestamp or increase priority of current headline.
  17681. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17682. depending on context. See the individual commands for more information."
  17683. (interactive "P")
  17684. (cond
  17685. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17686. ((and org-support-shift-select (org-region-active-p))
  17687. (org-call-for-shift-select 'previous-line))
  17688. ((org-at-timestamp-p t)
  17689. (call-interactively (if org-edit-timestamp-down-means-later
  17690. 'org-timestamp-down 'org-timestamp-up)))
  17691. ((and (not (eq org-support-shift-select 'always))
  17692. org-enable-priority-commands
  17693. (org-at-heading-p))
  17694. (call-interactively 'org-priority-up))
  17695. ((and (not org-support-shift-select) (org-at-item-p))
  17696. (call-interactively 'org-previous-item))
  17697. ((org-clocktable-try-shift 'up arg))
  17698. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17699. (org-support-shift-select
  17700. (org-call-for-shift-select 'previous-line))
  17701. (t (org-shiftselect-error))))
  17702. (defun org-shiftdown (&optional arg)
  17703. "Decrease item in timestamp or decrease priority of current headline.
  17704. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17705. depending on context. See the individual commands for more information."
  17706. (interactive "P")
  17707. (cond
  17708. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17709. ((and org-support-shift-select (org-region-active-p))
  17710. (org-call-for-shift-select 'next-line))
  17711. ((org-at-timestamp-p t)
  17712. (call-interactively (if org-edit-timestamp-down-means-later
  17713. 'org-timestamp-up 'org-timestamp-down)))
  17714. ((and (not (eq org-support-shift-select 'always))
  17715. org-enable-priority-commands
  17716. (org-at-heading-p))
  17717. (call-interactively 'org-priority-down))
  17718. ((and (not org-support-shift-select) (org-at-item-p))
  17719. (call-interactively 'org-next-item))
  17720. ((org-clocktable-try-shift 'down arg))
  17721. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17722. (org-support-shift-select
  17723. (org-call-for-shift-select 'next-line))
  17724. (t (org-shiftselect-error))))
  17725. (defun org-shiftright (&optional arg)
  17726. "Cycle the thing at point or in the current line, depending on context.
  17727. Depending on context, this does one of the following:
  17728. - switch a timestamp at point one day into the future
  17729. - on a headline, switch to the next TODO keyword.
  17730. - on an item, switch entire list to the next bullet type
  17731. - on a property line, switch to the next allowed value
  17732. - on a clocktable definition line, move time block into the future"
  17733. (interactive "P")
  17734. (cond
  17735. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17736. ((and org-support-shift-select (org-region-active-p))
  17737. (org-call-for-shift-select 'forward-char))
  17738. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17739. ((and (not (eq org-support-shift-select 'always))
  17740. (org-at-heading-p))
  17741. (let ((org-inhibit-logging
  17742. (not org-treat-S-cursor-todo-selection-as-state-change))
  17743. (org-inhibit-blocking
  17744. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17745. (org-call-with-arg 'org-todo 'right)))
  17746. ((or (and org-support-shift-select
  17747. (not (eq org-support-shift-select 'always))
  17748. (org-at-item-bullet-p))
  17749. (and (not org-support-shift-select) (org-at-item-p)))
  17750. (org-call-with-arg 'org-cycle-list-bullet nil))
  17751. ((and (not (eq org-support-shift-select 'always))
  17752. (org-at-property-p))
  17753. (call-interactively 'org-property-next-allowed-value))
  17754. ((org-clocktable-try-shift 'right arg))
  17755. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17756. (org-support-shift-select
  17757. (org-call-for-shift-select 'forward-char))
  17758. (t (org-shiftselect-error))))
  17759. (defun org-shiftleft (&optional arg)
  17760. "Cycle the thing at point or in the current line, depending on context.
  17761. Depending on context, this does one of the following:
  17762. - switch a timestamp at point one day into the past
  17763. - on a headline, switch to the previous TODO keyword.
  17764. - on an item, switch entire list to the previous bullet type
  17765. - on a property line, switch to the previous allowed value
  17766. - on a clocktable definition line, move time block into the past"
  17767. (interactive "P")
  17768. (cond
  17769. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17770. ((and org-support-shift-select (org-region-active-p))
  17771. (org-call-for-shift-select 'backward-char))
  17772. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17773. ((and (not (eq org-support-shift-select 'always))
  17774. (org-at-heading-p))
  17775. (let ((org-inhibit-logging
  17776. (not org-treat-S-cursor-todo-selection-as-state-change))
  17777. (org-inhibit-blocking
  17778. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17779. (org-call-with-arg 'org-todo 'left)))
  17780. ((or (and org-support-shift-select
  17781. (not (eq org-support-shift-select 'always))
  17782. (org-at-item-bullet-p))
  17783. (and (not org-support-shift-select) (org-at-item-p)))
  17784. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17785. ((and (not (eq org-support-shift-select 'always))
  17786. (org-at-property-p))
  17787. (call-interactively 'org-property-previous-allowed-value))
  17788. ((org-clocktable-try-shift 'left arg))
  17789. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17790. (org-support-shift-select
  17791. (org-call-for-shift-select 'backward-char))
  17792. (t (org-shiftselect-error))))
  17793. (defun org-shiftcontrolright ()
  17794. "Switch to next TODO set."
  17795. (interactive)
  17796. (cond
  17797. ((and org-support-shift-select (org-region-active-p))
  17798. (org-call-for-shift-select 'forward-word))
  17799. ((and (not (eq org-support-shift-select 'always))
  17800. (org-at-heading-p))
  17801. (org-call-with-arg 'org-todo 'nextset))
  17802. (org-support-shift-select
  17803. (org-call-for-shift-select 'forward-word))
  17804. (t (org-shiftselect-error))))
  17805. (defun org-shiftcontrolleft ()
  17806. "Switch to previous TODO set."
  17807. (interactive)
  17808. (cond
  17809. ((and org-support-shift-select (org-region-active-p))
  17810. (org-call-for-shift-select 'backward-word))
  17811. ((and (not (eq org-support-shift-select 'always))
  17812. (org-at-heading-p))
  17813. (org-call-with-arg 'org-todo 'previousset))
  17814. (org-support-shift-select
  17815. (org-call-for-shift-select 'backward-word))
  17816. (t (org-shiftselect-error))))
  17817. (defun org-shiftcontrolup (&optional n)
  17818. "Change timestamps synchronously up in CLOCK log lines.
  17819. Optional argument N tells to change by that many units."
  17820. (interactive "P")
  17821. (cond ((and (not org-support-shift-select)
  17822. (org-at-clock-log-p)
  17823. (org-at-timestamp-p t))
  17824. (org-clock-timestamps-up n))
  17825. (t (org-shiftselect-error))))
  17826. (defun org-shiftcontroldown (&optional n)
  17827. "Change timestamps synchronously down in CLOCK log lines.
  17828. Optional argument N tells to change by that many units."
  17829. (interactive "P")
  17830. (cond ((and (not org-support-shift-select)
  17831. (org-at-clock-log-p)
  17832. (org-at-timestamp-p t))
  17833. (org-clock-timestamps-down n))
  17834. (t (org-shiftselect-error))))
  17835. (defun org-ctrl-c-ret ()
  17836. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17837. (interactive)
  17838. (cond
  17839. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17840. (t (call-interactively 'org-insert-heading))))
  17841. (defun org-find-visible ()
  17842. (let ((s (point)))
  17843. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17844. (get-char-property s 'invisible)))
  17845. s))
  17846. (defun org-find-invisible ()
  17847. (let ((s (point)))
  17848. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17849. (not (get-char-property s 'invisible))))
  17850. s))
  17851. (defun org-copy-visible (beg end)
  17852. "Copy the visible parts of the region."
  17853. (interactive "r")
  17854. (let (snippets s)
  17855. (save-excursion
  17856. (save-restriction
  17857. (narrow-to-region beg end)
  17858. (setq s (goto-char (point-min)))
  17859. (while (not (= (point) (point-max)))
  17860. (goto-char (org-find-invisible))
  17861. (push (buffer-substring s (point)) snippets)
  17862. (setq s (goto-char (org-find-visible))))))
  17863. (kill-new (apply 'concat (nreverse snippets)))))
  17864. (defun org-copy-special ()
  17865. "Copy region in table or copy current subtree.
  17866. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17867. See the individual commands for more information."
  17868. (interactive)
  17869. (call-interactively
  17870. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17871. (defun org-cut-special ()
  17872. "Cut region in table or cut current subtree.
  17873. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17874. See the individual commands for more information."
  17875. (interactive)
  17876. (call-interactively
  17877. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17878. (defun org-paste-special (arg)
  17879. "Paste rectangular region into table, or past subtree relative to level.
  17880. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17881. See the individual commands for more information."
  17882. (interactive "P")
  17883. (if (org-at-table-p)
  17884. (org-table-paste-rectangle)
  17885. (org-paste-subtree arg)))
  17886. (defsubst org-in-fixed-width-region-p ()
  17887. "Is point in a fixed-width region?"
  17888. (save-match-data
  17889. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  17890. (defun org-edit-special (&optional arg)
  17891. "Call a special editor for the element at point.
  17892. When at a table, call the formula editor with `org-table-edit-formulas'.
  17893. When in a source code block, call `org-edit-src-code'.
  17894. When in a fixed-width region, call `org-edit-fixed-width-region'.
  17895. When at an #+INCLUDE keyword, visit the included file.
  17896. On a link, call `ffap' to visit the link at point.
  17897. Otherwise, return a user error."
  17898. (interactive)
  17899. (let ((element (org-element-at-point)))
  17900. (case (org-element-type element)
  17901. (src-block
  17902. (if (not arg) (org-edit-src-code)
  17903. (let* ((info (org-babel-get-src-block-info))
  17904. (lang (nth 0 info))
  17905. (params (nth 2 info))
  17906. (session (cdr (assq :session params))))
  17907. (if (not session) (org-edit-src-code)
  17908. ;; At a src-block with a session and function called with
  17909. ;; an ARG: switch to the buffer related to the inferior
  17910. ;; process.
  17911. (funcall (intern (concat "org-babel-prep-session:" lang))
  17912. session params)))))
  17913. (keyword
  17914. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  17915. (find-file
  17916. (org-remove-double-quotes
  17917. (car (org-split-string (org-element-property :value element)))))
  17918. (user-error "No special environment to edit here")))
  17919. (table
  17920. (if (eq (org-element-property :type element) 'table.el)
  17921. (org-edit-src-code)
  17922. (call-interactively 'org-table-edit-formulas)))
  17923. ;; Only Org tables contain `table-row' type elements.
  17924. (table-row (call-interactively 'org-table-edit-formulas))
  17925. ((example-block export-block) (org-edit-src-code))
  17926. (fixed-width (org-edit-fixed-width-region))
  17927. (otherwise
  17928. ;; No notable element at point. Though, we may be at a link,
  17929. ;; which is an object. Thus, scan deeper.
  17930. (if (eq (org-element-type (org-element-context element)) 'link)
  17931. (call-interactively 'ffap)
  17932. (user-error "No special environment to edit here"))))))
  17933. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17934. (defun org-ctrl-c-ctrl-c (&optional arg)
  17935. "Set tags in headline, or update according to changed information at point.
  17936. This command does many different things, depending on context:
  17937. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17938. this is what we do.
  17939. - If the cursor is on a statistics cookie, update it.
  17940. - If the cursor is in a headline, prompt for tags and insert them
  17941. into the current line, aligned to `org-tags-column'. When called
  17942. with prefix arg, realign all tags in the current buffer.
  17943. - If the cursor is in one of the special #+KEYWORD lines, this
  17944. triggers scanning the buffer for these lines and updating the
  17945. information.
  17946. - If the cursor is inside a table, realign the table. This command
  17947. works even if the automatic table editor has been turned off.
  17948. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17949. the entire table.
  17950. - If the cursor is at a footnote reference or definition, jump to
  17951. the corresponding definition or references, respectively.
  17952. - If the cursor is a the beginning of a dynamic block, update it.
  17953. - If the current buffer is a capture buffer, close note and file it.
  17954. - If the cursor is on a <<<target>>>, update radio targets and
  17955. corresponding links in this buffer.
  17956. - If the cursor is on a numbered item in a plain list, renumber the
  17957. ordered list.
  17958. - If the cursor is on a checkbox, toggle it.
  17959. - If the cursor is on a code block, evaluate it. The variable
  17960. `org-confirm-babel-evaluate' can be used to control prompting
  17961. before code block evaluation, by default every code block
  17962. evaluation requires confirmation. Code block evaluation can be
  17963. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17964. (interactive "P")
  17965. (cond
  17966. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17967. org-occur-highlights
  17968. org-latex-fragment-image-overlays)
  17969. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17970. (org-remove-occur-highlights)
  17971. (org-remove-latex-fragment-image-overlays)
  17972. (message "Temporary highlights/overlays removed from current buffer"))
  17973. ((and (local-variable-p 'org-finish-function (current-buffer))
  17974. (fboundp org-finish-function))
  17975. (funcall org-finish-function))
  17976. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17977. (t
  17978. (let* ((context (org-element-context)) (type (org-element-type context)))
  17979. ;; Test if point is within blanks at the end of an element.
  17980. (if (save-excursion
  17981. (or (not context)
  17982. (beginning-of-line)
  17983. (and (looking-at "[ \t]*$")
  17984. (skip-chars-forward " \r\t\n")
  17985. (>= (point) (org-element-property :end context)))))
  17986. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17987. (user-error "C-c C-c can do nothing useful at this location"))
  17988. ;; For convenience: at the first line of a paragraph on the
  17989. ;; same line as an item, apply function on that item instead.
  17990. (when (eq type 'paragraph)
  17991. (let ((parent (org-element-property :parent context)))
  17992. (when (and (eq (org-element-type parent) 'item)
  17993. (= (point-at-bol) (org-element-property :begin parent)))
  17994. (setq context parent type 'item))))
  17995. ;; Act according to type of element or object at point.
  17996. (case type
  17997. (clock (org-clock-update-time-maybe))
  17998. (dynamic-block
  17999. (save-excursion
  18000. (goto-char (org-element-property :post-affiliated context))
  18001. (org-update-dblock)))
  18002. (footnote-definition
  18003. (goto-char (org-element-property :post-affiliated context))
  18004. (call-interactively 'org-footnote-action))
  18005. (footnote-reference (call-interactively 'org-footnote-action))
  18006. ((headline inlinetask)
  18007. (save-excursion (goto-char (org-element-property :begin context))
  18008. (call-interactively 'org-set-tags)))
  18009. (item
  18010. ;; At an item: a double C-u set checkbox to "[-]"
  18011. ;; unconditionally, whereas a single one will toggle its
  18012. ;; presence. Without an universal argument, if the item
  18013. ;; has a checkbox, toggle it. Otherwise repair the list.
  18014. (let* ((box (org-element-property :checkbox context))
  18015. (struct (org-element-property :structure context))
  18016. (old-struct (copy-tree struct))
  18017. (parents (org-list-parents-alist struct))
  18018. (prevs (org-list-prevs-alist struct))
  18019. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  18020. (org-list-set-checkbox
  18021. (org-element-property :begin context) struct
  18022. (cond ((equal arg '(16)) "[-]")
  18023. ((and (not box) (equal arg '(4))) "[ ]")
  18024. ((or (not box) (equal arg '(4))) nil)
  18025. ((eq box 'on) "[ ]")
  18026. (t "[X]")))
  18027. ;; Mimic `org-list-write-struct' but with grabbing
  18028. ;; a return value from `org-list-struct-fix-box'.
  18029. (org-list-struct-fix-ind struct parents 2)
  18030. (org-list-struct-fix-item-end struct)
  18031. (org-list-struct-fix-bul struct prevs)
  18032. (org-list-struct-fix-ind struct parents)
  18033. (let ((block-item
  18034. (org-list-struct-fix-box struct parents prevs orderedp)))
  18035. (if (and box (equal struct old-struct))
  18036. (if (equal arg '(16))
  18037. (message "Checkboxes already reset")
  18038. (user-error "Cannot toggle this checkbox: %s"
  18039. (if (eq box 'on)
  18040. "all subitems checked"
  18041. "unchecked subitems")))
  18042. (org-list-struct-apply-struct struct old-struct)
  18043. (org-update-checkbox-count-maybe))
  18044. (when block-item
  18045. (message "Checkboxes were removed due to empty box at line %d"
  18046. (org-current-line block-item))))))
  18047. (keyword
  18048. (let ((org-inhibit-startup-visibility-stuff t)
  18049. (org-startup-align-all-tables nil))
  18050. (when (boundp 'org-table-coordinate-overlays)
  18051. (mapc 'delete-overlay org-table-coordinate-overlays)
  18052. (setq org-table-coordinate-overlays nil))
  18053. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  18054. (message "Local setup has been refreshed"))
  18055. (plain-list
  18056. ;; At a plain list, with a double C-u argument, set
  18057. ;; checkboxes of each item to "[-]", whereas a single one
  18058. ;; will toggle their presence according to the state of the
  18059. ;; first item in the list. Without an argument, repair the
  18060. ;; list.
  18061. (let* ((begin (org-element-property :contents-begin context))
  18062. (struct (org-element-property :structure context))
  18063. (old-struct (copy-tree struct))
  18064. (first-box (save-excursion
  18065. (goto-char begin)
  18066. (looking-at org-list-full-item-re)
  18067. (match-string-no-properties 3)))
  18068. (new-box (cond ((equal arg '(16)) "[-]")
  18069. ((equal arg '(4)) (unless first-box "[ ]"))
  18070. ((equal first-box "[X]") "[ ]")
  18071. (t "[X]"))))
  18072. (cond
  18073. (arg
  18074. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  18075. (org-list-get-all-items
  18076. begin struct (org-list-prevs-alist struct))))
  18077. ((and first-box (eq (point) begin))
  18078. ;; For convenience, when point is at bol on the first
  18079. ;; item of the list and no argument is provided, simply
  18080. ;; toggle checkbox of that item, if any.
  18081. (org-list-set-checkbox begin struct new-box)))
  18082. (org-list-write-struct
  18083. struct (org-list-parents-alist struct) old-struct)
  18084. (org-update-checkbox-count-maybe)
  18085. (save-excursion (goto-char begin) (org-list-send-list 'maybe))))
  18086. ((property-drawer node-property)
  18087. (call-interactively 'org-property-action))
  18088. ((radio-target target)
  18089. (call-interactively 'org-update-radio-target-regexp))
  18090. (statistics-cookie
  18091. (call-interactively 'org-update-statistics-cookies))
  18092. ((table table-cell table-row)
  18093. ;; At a table, recalculate every field and align it. Also
  18094. ;; send the table if necessary. If the table has
  18095. ;; a `table.el' type, just give up. At a table row or
  18096. ;; cell, maybe recalculate line but always align table.
  18097. (if (eq (org-element-property :type context) 'table.el)
  18098. (message "Use C-c ' to edit table.el tables")
  18099. (let ((org-enable-table-editor t))
  18100. (if (or (eq type 'table)
  18101. ;; Check if point is at a TBLFM line.
  18102. (and (eq type 'table-row)
  18103. (= (point) (org-element-property :end context))))
  18104. (save-excursion
  18105. (goto-char (org-element-property :contents-begin context))
  18106. (org-call-with-arg 'org-table-recalculate (or arg t))
  18107. (orgtbl-send-table 'maybe))
  18108. (org-table-maybe-eval-formula)
  18109. (cond (arg (call-interactively 'org-table-recalculate))
  18110. ((org-table-maybe-recalculate-line))
  18111. (t (org-table-align)))))))
  18112. (timestamp (org-timestamp-change 0 'day))
  18113. (otherwise
  18114. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  18115. (user-error
  18116. "C-c C-c can do nothing useful at this location")))))))))
  18117. (defun org-mode-restart ()
  18118. "Restart Org-mode, to scan again for special lines.
  18119. Also updates the keyword regular expressions."
  18120. (interactive)
  18121. (org-mode)
  18122. (message "Org-mode restarted"))
  18123. (defun org-kill-note-or-show-branches ()
  18124. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  18125. (interactive)
  18126. (if (not org-finish-function)
  18127. (progn
  18128. (hide-subtree)
  18129. (call-interactively 'show-branches))
  18130. (let ((org-note-abort t))
  18131. (funcall org-finish-function))))
  18132. (defun org-return (&optional indent)
  18133. "Goto next table row or insert a newline.
  18134. Calls `org-table-next-row' or `newline', depending on context.
  18135. See the individual commands for more information."
  18136. (interactive)
  18137. (let (org-ts-what)
  18138. (cond
  18139. ((or (bobp) (org-in-src-block-p))
  18140. (if indent (newline-and-indent) (newline)))
  18141. ((org-at-table-p)
  18142. (org-table-justify-field-maybe)
  18143. (call-interactively 'org-table-next-row))
  18144. ;; when `newline-and-indent' is called within a list, make sure
  18145. ;; text moved stays inside the item.
  18146. ((and (org-in-item-p) indent)
  18147. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  18148. (progn
  18149. (save-match-data (newline))
  18150. (org-indent-line-to (length (match-string 0))))
  18151. (let ((ind (org-get-indentation)))
  18152. (newline)
  18153. (if (org-looking-back org-list-end-re)
  18154. (org-indent-line)
  18155. (org-indent-line-to ind)))))
  18156. ((and org-return-follows-link
  18157. (org-at-timestamp-p t)
  18158. (not (eq org-ts-what 'after)))
  18159. (org-follow-timestamp-link))
  18160. ((and org-return-follows-link
  18161. (let ((tprop (get-text-property (point) 'face)))
  18162. (or (eq tprop 'org-link)
  18163. (and (listp tprop) (memq 'org-link tprop)))))
  18164. (call-interactively 'org-open-at-point))
  18165. ((and (org-at-heading-p)
  18166. (looking-at
  18167. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  18168. (org-show-entry)
  18169. (end-of-line 1)
  18170. (newline))
  18171. (t (if indent (newline-and-indent) (newline))))))
  18172. (defun org-return-indent ()
  18173. "Goto next table row or insert a newline and indent.
  18174. Calls `org-table-next-row' or `newline-and-indent', depending on
  18175. context. See the individual commands for more information."
  18176. (interactive)
  18177. (org-return t))
  18178. (defun org-ctrl-c-star ()
  18179. "Compute table, or change heading status of lines.
  18180. Calls `org-table-recalculate' or `org-toggle-heading',
  18181. depending on context."
  18182. (interactive)
  18183. (cond
  18184. ((org-at-table-p)
  18185. (call-interactively 'org-table-recalculate))
  18186. (t
  18187. ;; Convert all lines in region to list items
  18188. (call-interactively 'org-toggle-heading))))
  18189. (defun org-ctrl-c-minus ()
  18190. "Insert separator line in table or modify bullet status of line.
  18191. Also turns a plain line or a region of lines into list items.
  18192. Calls `org-table-insert-hline', `org-toggle-item', or
  18193. `org-cycle-list-bullet', depending on context."
  18194. (interactive)
  18195. (cond
  18196. ((org-at-table-p)
  18197. (call-interactively 'org-table-insert-hline))
  18198. ((org-region-active-p)
  18199. (call-interactively 'org-toggle-item))
  18200. ((org-in-item-p)
  18201. (call-interactively 'org-cycle-list-bullet))
  18202. (t
  18203. (call-interactively 'org-toggle-item))))
  18204. (defun org-toggle-item (arg)
  18205. "Convert headings or normal lines to items, items to normal lines.
  18206. If there is no active region, only the current line is considered.
  18207. If the first non blank line in the region is an headline, convert
  18208. all headlines to items, shifting text accordingly.
  18209. If it is an item, convert all items to normal lines.
  18210. If it is normal text, change region into an item. With a prefix
  18211. argument ARG, change each line in region into an item."
  18212. (interactive "P")
  18213. (let ((shift-text
  18214. (function
  18215. ;; Shift text in current section to IND, from point to END.
  18216. ;; The function leaves point to END line.
  18217. (lambda (ind end)
  18218. (let ((min-i 1000) (end (copy-marker end)))
  18219. ;; First determine the minimum indentation (MIN-I) of
  18220. ;; the text.
  18221. (save-excursion
  18222. (catch 'exit
  18223. (while (< (point) end)
  18224. (let ((i (org-get-indentation)))
  18225. (cond
  18226. ;; Skip blank lines and inline tasks.
  18227. ((looking-at "^[ \t]*$"))
  18228. ((looking-at org-outline-regexp-bol))
  18229. ;; We can't find less than 0 indentation.
  18230. ((zerop i) (throw 'exit (setq min-i 0)))
  18231. ((< i min-i) (setq min-i i))))
  18232. (forward-line))))
  18233. ;; Then indent each line so that a line indented to
  18234. ;; MIN-I becomes indented to IND. Ignore blank lines
  18235. ;; and inline tasks in the process.
  18236. (let ((delta (- ind min-i)))
  18237. (while (< (point) end)
  18238. (unless (or (looking-at "^[ \t]*$")
  18239. (looking-at org-outline-regexp-bol))
  18240. (org-indent-line-to (+ (org-get-indentation) delta)))
  18241. (forward-line)))))))
  18242. (skip-blanks
  18243. (function
  18244. ;; Return beginning of first non-blank line, starting from
  18245. ;; line at POS.
  18246. (lambda (pos)
  18247. (save-excursion
  18248. (goto-char pos)
  18249. (skip-chars-forward " \r\t\n")
  18250. (point-at-bol)))))
  18251. beg end)
  18252. ;; Determine boundaries of changes.
  18253. (if (org-region-active-p)
  18254. (setq beg (funcall skip-blanks (region-beginning))
  18255. end (copy-marker (region-end)))
  18256. (setq beg (funcall skip-blanks (point-at-bol))
  18257. end (copy-marker (point-at-eol))))
  18258. ;; Depending on the starting line, choose an action on the text
  18259. ;; between BEG and END.
  18260. (org-with-limited-levels
  18261. (save-excursion
  18262. (goto-char beg)
  18263. (cond
  18264. ;; Case 1. Start at an item: de-itemize. Note that it only
  18265. ;; happens when a region is active: `org-ctrl-c-minus'
  18266. ;; would call `org-cycle-list-bullet' otherwise.
  18267. ((org-at-item-p)
  18268. (while (< (point) end)
  18269. (when (org-at-item-p)
  18270. (skip-chars-forward " \t")
  18271. (delete-region (point) (match-end 0)))
  18272. (forward-line)))
  18273. ;; Case 2. Start at an heading: convert to items.
  18274. ((org-at-heading-p)
  18275. (let* ((bul (org-list-bullet-string "-"))
  18276. (bul-len (length bul))
  18277. ;; Indentation of the first heading. It should be
  18278. ;; relative to the indentation of its parent, if any.
  18279. (start-ind (save-excursion
  18280. (cond
  18281. ((not org-adapt-indentation) 0)
  18282. ((not (outline-previous-heading)) 0)
  18283. (t (length (match-string 0))))))
  18284. ;; Level of first heading. Further headings will be
  18285. ;; compared to it to determine hierarchy in the list.
  18286. (ref-level (org-reduced-level (org-outline-level))))
  18287. (while (< (point) end)
  18288. (let* ((level (org-reduced-level (org-outline-level)))
  18289. (delta (max 0 (- level ref-level))))
  18290. ;; If current headline is less indented than the first
  18291. ;; one, set it as reference, in order to preserve
  18292. ;; subtrees.
  18293. (when (< level ref-level) (setq ref-level level))
  18294. (replace-match bul t t)
  18295. (org-indent-line-to (+ start-ind (* delta bul-len)))
  18296. ;; Ensure all text down to END (or SECTION-END) belongs
  18297. ;; to the newly created item.
  18298. (let ((section-end (save-excursion
  18299. (or (outline-next-heading) (point)))))
  18300. (forward-line)
  18301. (funcall shift-text
  18302. (+ start-ind (* (1+ delta) bul-len))
  18303. (min end section-end)))))))
  18304. ;; Case 3. Normal line with ARG: turn each non-item line into
  18305. ;; an item.
  18306. (arg
  18307. (while (< (point) end)
  18308. (unless (or (org-at-heading-p) (org-at-item-p))
  18309. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  18310. (replace-match
  18311. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  18312. (forward-line)))
  18313. ;; Case 4. Normal line without ARG: make the first line of
  18314. ;; region an item, and shift indentation of others
  18315. ;; lines to set them as item's body.
  18316. (t (let* ((bul (org-list-bullet-string "-"))
  18317. (bul-len (length bul))
  18318. (ref-ind (org-get-indentation)))
  18319. (skip-chars-forward " \t")
  18320. (insert bul)
  18321. (forward-line)
  18322. (while (< (point) end)
  18323. ;; Ensure that lines less indented than first one
  18324. ;; still get included in item body.
  18325. (funcall shift-text
  18326. (+ ref-ind bul-len)
  18327. (min end (save-excursion (or (outline-next-heading)
  18328. (point)))))
  18329. (forward-line)))))))))
  18330. (defun org-toggle-heading (&optional nstars)
  18331. "Convert headings to normal text, or items or text to headings.
  18332. If there is no active region, only the current line is considered.
  18333. With a \\[universal-argument] prefix, convert the whole list at
  18334. point into heading.
  18335. In a region:
  18336. - If the first non blank line is an headline, remove the stars
  18337. from all headlines in the region.
  18338. - If it is a normal line turn each and every normal line (i.e. not an
  18339. heading or an item) in the region into a heading.
  18340. - If it is a plain list item, turn all plain list items into headings.
  18341. When converting a line into a heading, the number of stars is chosen
  18342. such that the lines become children of the current entry. However,
  18343. when a prefix argument is given, its value determines the number of
  18344. stars to add."
  18345. (interactive "P")
  18346. (let ((skip-blanks
  18347. (function
  18348. ;; Return beginning of first non-blank line, starting from
  18349. ;; line at POS.
  18350. (lambda (pos)
  18351. (save-excursion
  18352. (goto-char pos)
  18353. (while (org-at-comment-p) (forward-line))
  18354. (skip-chars-forward " \r\t\n")
  18355. (point-at-bol)))))
  18356. beg end toggled)
  18357. ;; Determine boundaries of changes. If a universal prefix has
  18358. ;; been given, put the list in a region. If region ends at a bol,
  18359. ;; do not consider the last line to be in the region.
  18360. (when (and current-prefix-arg (org-at-item-p))
  18361. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  18362. (org-mark-element))
  18363. (if (org-region-active-p)
  18364. (setq beg (funcall skip-blanks (region-beginning))
  18365. end (copy-marker (save-excursion
  18366. (goto-char (region-end))
  18367. (if (bolp) (point) (point-at-eol)))))
  18368. (setq beg (funcall skip-blanks (point-at-bol))
  18369. end (copy-marker (point-at-eol))))
  18370. ;; Ensure inline tasks don't count as headings.
  18371. (org-with-limited-levels
  18372. (save-excursion
  18373. (goto-char beg)
  18374. (cond
  18375. ;; Case 1. Started at an heading: de-star headings.
  18376. ((org-at-heading-p)
  18377. (while (< (point) end)
  18378. (when (org-at-heading-p t)
  18379. (looking-at org-outline-regexp) (replace-match "")
  18380. (setq toggled t))
  18381. (forward-line)))
  18382. ;; Case 2. Started at an item: change items into headlines.
  18383. ;; One star will be added by `org-list-to-subtree'.
  18384. ((org-at-item-p)
  18385. (let* ((stars (make-string
  18386. (if nstars
  18387. ;; subtract the star that will be added again by
  18388. ;; `org-list-to-subtree'
  18389. (1- (prefix-numeric-value current-prefix-arg))
  18390. (or (org-current-level) 0))
  18391. ?*))
  18392. (add-stars
  18393. (cond (nstars "") ; stars from prefix only
  18394. ((equal stars "") "") ; before first heading
  18395. (org-odd-levels-only "*") ; inside heading, odd
  18396. (t "")))) ; inside heading, oddeven
  18397. (while (< (point) end)
  18398. (when (org-at-item-p)
  18399. ;; Pay attention to cases when region ends before list.
  18400. (let* ((struct (org-list-struct))
  18401. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  18402. (save-restriction
  18403. (narrow-to-region (point) list-end)
  18404. (insert
  18405. (org-list-to-subtree
  18406. (org-list-parse-list t)
  18407. '(:istart (concat stars add-stars (funcall get-stars depth))
  18408. :icount (concat stars add-stars (funcall get-stars depth)))))))
  18409. (setq toggled t))
  18410. (forward-line))))
  18411. ;; Case 3. Started at normal text: make every line an heading,
  18412. ;; skipping headlines and items.
  18413. (t (let* ((stars (make-string
  18414. (if nstars
  18415. (prefix-numeric-value current-prefix-arg)
  18416. (or (org-current-level) 0))
  18417. ?*))
  18418. (add-stars
  18419. (cond (nstars "") ; stars from prefix only
  18420. ((equal stars "") "*") ; before first heading
  18421. (org-odd-levels-only "**") ; inside heading, odd
  18422. (t "*"))) ; inside heading, oddeven
  18423. (rpl (concat stars add-stars " ")))
  18424. (while (< (point) end)
  18425. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  18426. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  18427. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  18428. (forward-line)))))))
  18429. (unless toggled (message "Cannot toggle heading from here"))))
  18430. (defun org-meta-return (&optional arg)
  18431. "Insert a new heading or wrap a region in a table.
  18432. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  18433. See the individual commands for more information."
  18434. (interactive "P")
  18435. (cond
  18436. ((run-hook-with-args-until-success 'org-metareturn-hook))
  18437. ((or (org-at-drawer-p) (org-at-property-p))
  18438. (newline-and-indent))
  18439. ((org-at-table-p)
  18440. (call-interactively 'org-table-wrap-region))
  18441. (t (call-interactively 'org-insert-heading))))
  18442. ;;; Menu entries
  18443. (defsubst org-in-subtree-not-table-p ()
  18444. "Are we in a subtree and not in a table?"
  18445. (and (not (org-before-first-heading-p))
  18446. (not (org-at-table-p))))
  18447. ;; Define the Org-mode menus
  18448. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  18449. '("Tbl"
  18450. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  18451. ["Next Field" org-cycle (org-at-table-p)]
  18452. ["Previous Field" org-shifttab (org-at-table-p)]
  18453. ["Next Row" org-return (org-at-table-p)]
  18454. "--"
  18455. ["Blank Field" org-table-blank-field (org-at-table-p)]
  18456. ["Edit Field" org-table-edit-field (org-at-table-p)]
  18457. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  18458. "--"
  18459. ("Column"
  18460. ["Move Column Left" org-metaleft (org-at-table-p)]
  18461. ["Move Column Right" org-metaright (org-at-table-p)]
  18462. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  18463. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  18464. ("Row"
  18465. ["Move Row Up" org-metaup (org-at-table-p)]
  18466. ["Move Row Down" org-metadown (org-at-table-p)]
  18467. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  18468. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  18469. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  18470. "--"
  18471. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  18472. ("Rectangle"
  18473. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  18474. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  18475. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  18476. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  18477. "--"
  18478. ("Calculate"
  18479. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  18480. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  18481. ["Edit Formulas" org-edit-special (org-at-table-p)]
  18482. "--"
  18483. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  18484. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  18485. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  18486. "--"
  18487. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  18488. "--"
  18489. ["Sum Column/Rectangle" org-table-sum
  18490. (or (org-at-table-p) (org-region-active-p))]
  18491. ["Which Column?" org-table-current-column (org-at-table-p)])
  18492. ["Debug Formulas"
  18493. org-table-toggle-formula-debugger
  18494. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  18495. ["Show Col/Row Numbers"
  18496. org-table-toggle-coordinate-overlays
  18497. :style toggle
  18498. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  18499. "--"
  18500. ["Create" org-table-create (and (not (org-at-table-p))
  18501. org-enable-table-editor)]
  18502. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  18503. ["Import from File" org-table-import (not (org-at-table-p))]
  18504. ["Export to File" org-table-export (org-at-table-p)]
  18505. "--"
  18506. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  18507. (easy-menu-define org-org-menu org-mode-map "Org menu"
  18508. '("Org"
  18509. ("Show/Hide"
  18510. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  18511. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  18512. ["Sparse Tree..." org-sparse-tree t]
  18513. ["Reveal Context" org-reveal t]
  18514. ["Show All" show-all t]
  18515. "--"
  18516. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  18517. "--"
  18518. ["New Heading" org-insert-heading t]
  18519. ("Navigate Headings"
  18520. ["Up" outline-up-heading t]
  18521. ["Next" outline-next-visible-heading t]
  18522. ["Previous" outline-previous-visible-heading t]
  18523. ["Next Same Level" outline-forward-same-level t]
  18524. ["Previous Same Level" outline-backward-same-level t]
  18525. "--"
  18526. ["Jump" org-goto t])
  18527. ("Edit Structure"
  18528. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  18529. "--"
  18530. ["Move Subtree Up" org-shiftmetaup (org-in-subtree-not-table-p)]
  18531. ["Move Subtree Down" org-shiftmetadown (org-in-subtree-not-table-p)]
  18532. "--"
  18533. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  18534. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  18535. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  18536. "--"
  18537. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  18538. "--"
  18539. ["Copy visible text" org-copy-visible t]
  18540. "--"
  18541. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  18542. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  18543. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  18544. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  18545. "--"
  18546. ["Sort Region/Children" org-sort t]
  18547. "--"
  18548. ["Convert to odd levels" org-convert-to-odd-levels t]
  18549. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  18550. ("Editing"
  18551. ["Emphasis..." org-emphasize t]
  18552. ["Edit Source Example" org-edit-special t]
  18553. "--"
  18554. ["Footnote new/jump" org-footnote-action t]
  18555. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  18556. ("Archive"
  18557. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  18558. "--"
  18559. ["Move Subtree to Archive file" org-advertized-archive-subtree (org-in-subtree-not-table-p)]
  18560. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  18561. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  18562. )
  18563. "--"
  18564. ("Hyperlinks"
  18565. ["Store Link (Global)" org-store-link t]
  18566. ["Find existing link to here" org-occur-link-in-agenda-files t]
  18567. ["Insert Link" org-insert-link t]
  18568. ["Follow Link" org-open-at-point t]
  18569. "--"
  18570. ["Next link" org-next-link t]
  18571. ["Previous link" org-previous-link t]
  18572. "--"
  18573. ["Descriptive Links"
  18574. org-toggle-link-display
  18575. :style radio
  18576. :selected org-descriptive-links
  18577. ]
  18578. ["Literal Links"
  18579. org-toggle-link-display
  18580. :style radio
  18581. :selected (not org-descriptive-links)])
  18582. "--"
  18583. ("TODO Lists"
  18584. ["TODO/DONE/-" org-todo t]
  18585. ("Select keyword"
  18586. ["Next keyword" org-shiftright (org-at-heading-p)]
  18587. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  18588. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  18589. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  18590. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  18591. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  18592. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  18593. "--"
  18594. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  18595. :selected org-enforce-todo-dependencies :style toggle :active t]
  18596. "Settings for tree at point"
  18597. ["Do Children sequentially" org-toggle-ordered-property :style radio
  18598. :selected (org-entry-get nil "ORDERED")
  18599. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18600. ["Do Children parallel" org-toggle-ordered-property :style radio
  18601. :selected (not (org-entry-get nil "ORDERED"))
  18602. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  18603. "--"
  18604. ["Set Priority" org-priority t]
  18605. ["Priority Up" org-shiftup t]
  18606. ["Priority Down" org-shiftdown t]
  18607. "--"
  18608. ["Get news from all feeds" org-feed-update-all t]
  18609. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  18610. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  18611. ("TAGS and Properties"
  18612. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  18613. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  18614. "--"
  18615. ["Set property" org-set-property (not (org-before-first-heading-p))]
  18616. ["Column view of properties" org-columns t]
  18617. ["Insert Column View DBlock" org-insert-columns-dblock t])
  18618. ("Dates and Scheduling"
  18619. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  18620. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  18621. ("Change Date"
  18622. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  18623. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  18624. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  18625. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  18626. ["Compute Time Range" org-evaluate-time-range t]
  18627. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  18628. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  18629. "--"
  18630. ["Custom time format" org-toggle-time-stamp-overlays
  18631. :style radio :selected org-display-custom-times]
  18632. "--"
  18633. ["Goto Calendar" org-goto-calendar t]
  18634. ["Date from Calendar" org-date-from-calendar t]
  18635. "--"
  18636. ["Start/Restart Timer" org-timer-start t]
  18637. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  18638. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  18639. ["Insert Timer String" org-timer t]
  18640. ["Insert Timer Item" org-timer-item t])
  18641. ("Logging work"
  18642. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  18643. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  18644. ["Clock out" org-clock-out t]
  18645. ["Clock cancel" org-clock-cancel t]
  18646. "--"
  18647. ["Mark as default task" org-clock-mark-default-task t]
  18648. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  18649. ["Goto running clock" org-clock-goto t]
  18650. "--"
  18651. ["Display times" org-clock-display t]
  18652. ["Create clock table" org-clock-report t]
  18653. "--"
  18654. ["Record DONE time"
  18655. (progn (setq org-log-done (not org-log-done))
  18656. (message "Switching to %s will %s record a timestamp"
  18657. (car org-done-keywords)
  18658. (if org-log-done "automatically" "not")))
  18659. :style toggle :selected org-log-done])
  18660. "--"
  18661. ["Agenda Command..." org-agenda t]
  18662. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  18663. ("File List for Agenda")
  18664. ("Special views current file"
  18665. ["TODO Tree" org-show-todo-tree t]
  18666. ["Check Deadlines" org-check-deadlines t]
  18667. ["Timeline" org-timeline t]
  18668. ["Tags/Property tree" org-match-sparse-tree t])
  18669. "--"
  18670. ["Export/Publish..." org-export-dispatch t]
  18671. ("LaTeX"
  18672. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  18673. :selected org-cdlatex-mode]
  18674. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  18675. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  18676. ["Modify math symbol" org-cdlatex-math-modify
  18677. (org-inside-LaTeX-fragment-p)]
  18678. ["Insert citation" org-reftex-citation t]
  18679. "--"
  18680. ["Template for BEAMER" (progn (require 'ox-beamer)
  18681. (org-insert-beamer-options-template)) t])
  18682. "--"
  18683. ("MobileOrg"
  18684. ["Push Files and Views" org-mobile-push t]
  18685. ["Get Captured and Flagged" org-mobile-pull t]
  18686. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  18687. "--"
  18688. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  18689. "--"
  18690. ("Documentation"
  18691. ["Show Version" org-version t]
  18692. ["Info Documentation" org-info t])
  18693. ("Customize"
  18694. ["Browse Org Group" org-customize t]
  18695. "--"
  18696. ["Expand This Menu" org-create-customize-menu
  18697. (fboundp 'customize-menu-create)])
  18698. ["Send bug report" org-submit-bug-report t]
  18699. "--"
  18700. ("Refresh/Reload"
  18701. ["Refresh setup current buffer" org-mode-restart t]
  18702. ["Reload Org (after update)" org-reload t]
  18703. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  18704. ))
  18705. (defun org-info (&optional node)
  18706. "Read documentation for Org-mode in the info system.
  18707. With optional NODE, go directly to that node."
  18708. (interactive)
  18709. (info (format "(org)%s" (or node ""))))
  18710. ;;;###autoload
  18711. (defun org-submit-bug-report ()
  18712. "Submit a bug report on Org-mode via mail.
  18713. Don't hesitate to report any problems or inaccurate documentation.
  18714. If you don't have setup sending mail from (X)Emacs, please copy the
  18715. output buffer into your mail program, as it gives us important
  18716. information about your Org-mode version and configuration."
  18717. (interactive)
  18718. (require 'reporter)
  18719. (org-load-modules-maybe)
  18720. (org-require-autoloaded-modules)
  18721. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18722. (reporter-submit-bug-report
  18723. "emacs-orgmode@gnu.org"
  18724. (org-version nil 'full)
  18725. (let (list)
  18726. (save-window-excursion
  18727. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18728. (delete-other-windows)
  18729. (erase-buffer)
  18730. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18731. We would like to add your full Org-mode and Outline configuration to the
  18732. bug report. This greatly simplifies the work of the maintainer and
  18733. other experts on the mailing list.
  18734. HOWEVER, some variables you have customized may contain private
  18735. information. The names of customers, colleagues, or friends, might
  18736. appear in the form of file names, tags, todo states, or search strings.
  18737. If you answer yes to the prompt, you might want to check and remove
  18738. such private information before sending the email.")
  18739. (add-text-properties (point-min) (point-max) '(face org-warning))
  18740. (when (yes-or-no-p "Include your Org-mode configuration ")
  18741. (mapatoms
  18742. (lambda (v)
  18743. (and (boundp v)
  18744. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18745. (or (and (symbol-value v)
  18746. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18747. (and
  18748. (get v 'custom-type) (get v 'standard-value)
  18749. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18750. (push v list)))))
  18751. (kill-buffer (get-buffer "*Warn about privacy*"))
  18752. list))
  18753. nil nil
  18754. "Remember to cover the basics, that is, what you expected to happen and
  18755. what in fact did happen. You don't know how to make a good report? See
  18756. http://orgmode.org/manual/Feedback.html#Feedback
  18757. Your bug report will be posted to the Org-mode mailing list.
  18758. ------------------------------------------------------------------------")
  18759. (save-excursion
  18760. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18761. (replace-match "\\1Bug: \\3 [\\2]")))))
  18762. (defun org-install-agenda-files-menu ()
  18763. (let ((bl (buffer-list)))
  18764. (save-excursion
  18765. (while bl
  18766. (set-buffer (pop bl))
  18767. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18768. (when (derived-mode-p 'org-mode)
  18769. (easy-menu-change
  18770. '("Org") "File List for Agenda"
  18771. (append
  18772. (list
  18773. ["Edit File List" (org-edit-agenda-file-list) t]
  18774. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18775. ["Remove Current File from List" org-remove-file t]
  18776. ["Cycle through agenda files" org-cycle-agenda-files t]
  18777. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18778. "--")
  18779. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18780. ;;;; Documentation
  18781. (defun org-require-autoloaded-modules ()
  18782. (interactive)
  18783. (mapc 'require
  18784. '(org-agenda org-archive org-attach org-clock org-colview org-id
  18785. org-remember org-table org-timer)))
  18786. ;;;###autoload
  18787. (defun org-reload (&optional uncompiled)
  18788. "Reload all org lisp files.
  18789. With prefix arg UNCOMPILED, load the uncompiled versions."
  18790. (interactive "P")
  18791. (require 'loadhist)
  18792. (let* ((org-dir (org-find-library-dir "org"))
  18793. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  18794. (feature-re "^\\(org\\|ob\\)\\(-.*\\)?")
  18795. (remove-re (mapconcat 'identity
  18796. (mapcar (lambda (f) (concat "^" f "$"))
  18797. (list (if (featurep 'xemacs)
  18798. "org-colview"
  18799. "org-colview-xemacs")
  18800. "org" "org-loaddefs" "org-version"))
  18801. "\\|"))
  18802. (feats (delete-dups
  18803. (mapcar 'file-name-sans-extension
  18804. (mapcar 'file-name-nondirectory
  18805. (delq nil
  18806. (mapcar 'feature-file
  18807. features))))))
  18808. (lfeat (append
  18809. (sort
  18810. (setq feats
  18811. (delq nil (mapcar
  18812. (lambda (f)
  18813. (if (and (string-match feature-re f)
  18814. (not (string-match remove-re f)))
  18815. f nil))
  18816. feats)))
  18817. 'string-lessp)
  18818. (list "org-version" "org")))
  18819. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  18820. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  18821. load-uncore load-misses)
  18822. (setq load-misses
  18823. (delq 't
  18824. (mapcar (lambda (f)
  18825. (or (org-load-noerror-mustsuffix (concat org-dir f))
  18826. (and (string= org-dir contrib-dir)
  18827. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  18828. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  18829. (add-to-list 'load-uncore f 'append)
  18830. 't)
  18831. f))
  18832. lfeat)))
  18833. (if load-uncore
  18834. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  18835. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  18836. (if load-misses
  18837. (message "Some error occured while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  18838. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  18839. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  18840. ;;;###autoload
  18841. (defun org-customize ()
  18842. "Call the customize function with org as argument."
  18843. (interactive)
  18844. (org-load-modules-maybe)
  18845. (org-require-autoloaded-modules)
  18846. (customize-browse 'org))
  18847. (defun org-create-customize-menu ()
  18848. "Create a full customization menu for Org-mode, insert it into the menu."
  18849. (interactive)
  18850. (org-load-modules-maybe)
  18851. (org-require-autoloaded-modules)
  18852. (if (fboundp 'customize-menu-create)
  18853. (progn
  18854. (easy-menu-change
  18855. '("Org") "Customize"
  18856. `(["Browse Org group" org-customize t]
  18857. "--"
  18858. ,(customize-menu-create 'org)
  18859. ["Set" Custom-set t]
  18860. ["Save" Custom-save t]
  18861. ["Reset to Current" Custom-reset-current t]
  18862. ["Reset to Saved" Custom-reset-saved t]
  18863. ["Reset to Standard Settings" Custom-reset-standard t]))
  18864. (message "\"Org\"-menu now contains full customization menu"))
  18865. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18866. ;;;; Miscellaneous stuff
  18867. ;;; Generally useful functions
  18868. (defun org-get-at-bol (property)
  18869. "Get text property PROPERTY at beginning of line."
  18870. (get-text-property (point-at-bol) property))
  18871. (defun org-find-text-property-in-string (prop s)
  18872. "Return the first non-nil value of property PROP in string S."
  18873. (or (get-text-property 0 prop s)
  18874. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18875. prop s)))
  18876. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18877. "Display the given MESSAGE as a warning."
  18878. (if (fboundp 'display-warning)
  18879. (display-warning 'org message
  18880. (if (featurep 'xemacs) 'warning :warning))
  18881. (let ((buf (get-buffer-create "*Org warnings*")))
  18882. (with-current-buffer buf
  18883. (goto-char (point-max))
  18884. (insert "Warning (Org): " message)
  18885. (unless (bolp)
  18886. (newline)))
  18887. (display-buffer buf)
  18888. (sit-for 0))))
  18889. (defun org-eval (form)
  18890. "Eval FORM and return result."
  18891. (condition-case error
  18892. (eval form)
  18893. (error (format "%%![Error: %s]" error))))
  18894. (defun org-in-clocktable-p ()
  18895. "Check if the cursor is in a clocktable."
  18896. (let ((pos (point)) start)
  18897. (save-excursion
  18898. (end-of-line 1)
  18899. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18900. (setq start (match-beginning 0))
  18901. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18902. (>= (match-end 0) pos)
  18903. start))))
  18904. (defun org-in-commented-line ()
  18905. "Is point in a line starting with `#'?"
  18906. (equal (char-after (point-at-bol)) ?#))
  18907. (defun org-in-indented-comment-line ()
  18908. "Is point in a line starting with `#' after some white space?"
  18909. (save-excursion
  18910. (save-match-data
  18911. (goto-char (point-at-bol))
  18912. (looking-at "[ \t]*#"))))
  18913. (defun org-in-verbatim-emphasis ()
  18914. (save-match-data
  18915. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18916. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18917. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18918. (if (and marker (marker-buffer marker)
  18919. (buffer-live-p (marker-buffer marker)))
  18920. (progn
  18921. (org-pop-to-buffer-same-window (marker-buffer marker))
  18922. (if (or (> marker (point-max)) (< marker (point-min)))
  18923. (widen))
  18924. (goto-char marker)
  18925. (org-show-context 'org-goto))
  18926. (if bookmark
  18927. (bookmark-jump bookmark)
  18928. (error "Cannot find location"))))
  18929. (defun org-quote-csv-field (s)
  18930. "Quote field for inclusion in CSV material."
  18931. (if (string-match "[\",]" s)
  18932. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18933. s))
  18934. (defun org-force-self-insert (N)
  18935. "Needed to enforce self-insert under remapping."
  18936. (interactive "p")
  18937. (self-insert-command N))
  18938. (defun org-string-width (s)
  18939. "Compute width of string, ignoring invisible characters.
  18940. This ignores character with invisibility property `org-link', and also
  18941. characters with property `org-cwidth', because these will become invisible
  18942. upon the next fontification round."
  18943. (let (b l)
  18944. (when (or (eq t buffer-invisibility-spec)
  18945. (assq 'org-link buffer-invisibility-spec))
  18946. (while (setq b (text-property-any 0 (length s)
  18947. 'invisible 'org-link s))
  18948. (setq s (concat (substring s 0 b)
  18949. (substring s (or (next-single-property-change
  18950. b 'invisible s) (length s)))))))
  18951. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18952. (setq s (concat (substring s 0 b)
  18953. (substring s (or (next-single-property-change
  18954. b 'org-cwidth s) (length s))))))
  18955. (setq l (string-width s) b -1)
  18956. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18957. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18958. l))
  18959. (defun org-shorten-string (s maxlength)
  18960. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18961. If the string is shorter or has length MAXLENGTH, just return the
  18962. original string. If it is longer, the functions finds a space in the
  18963. string, breaks this string off at that locations and adds three dots
  18964. as ellipsis. Including the ellipsis, the string will not be longer
  18965. than MAXLENGTH. If finding a good breaking point in the string does
  18966. not work, the string is just chopped off in the middle of a word
  18967. if necessary."
  18968. (if (<= (length s) maxlength)
  18969. s
  18970. (let* ((n (max (- maxlength 4) 1))
  18971. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18972. (if (string-match re s)
  18973. (concat (match-string 1 s) "...")
  18974. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18975. (defun org-get-indentation (&optional line)
  18976. "Get the indentation of the current line, interpreting tabs.
  18977. When LINE is given, assume it represents a line and compute its indentation."
  18978. (if line
  18979. (if (string-match "^ *" (org-remove-tabs line))
  18980. (match-end 0))
  18981. (save-excursion
  18982. (beginning-of-line 1)
  18983. (skip-chars-forward " \t")
  18984. (current-column))))
  18985. (defun org-get-string-indentation (s)
  18986. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18987. (let ((n -1) (i 0) (w tab-width) c)
  18988. (catch 'exit
  18989. (while (< (setq n (1+ n)) (length s))
  18990. (setq c (aref s n))
  18991. (cond ((= c ?\ ) (setq i (1+ i)))
  18992. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18993. (t (throw 'exit t)))))
  18994. i))
  18995. (defun org-remove-tabs (s &optional width)
  18996. "Replace tabulators in S with spaces.
  18997. Assumes that s is a single line, starting in column 0."
  18998. (setq width (or width tab-width))
  18999. (while (string-match "\t" s)
  19000. (setq s (replace-match
  19001. (make-string
  19002. (- (* width (/ (+ (match-beginning 0) width) width))
  19003. (match-beginning 0)) ?\ )
  19004. t t s)))
  19005. s)
  19006. (defun org-fix-indentation (line ind)
  19007. "Fix indentation in LINE.
  19008. IND is a cons cell with target and minimum indentation.
  19009. If the current indentation in LINE is smaller than the minimum,
  19010. leave it alone. If it is larger than ind, set it to the target."
  19011. (let* ((l (org-remove-tabs line))
  19012. (i (org-get-indentation l))
  19013. (i1 (car ind)) (i2 (cdr ind)))
  19014. (if (>= i i2) (setq l (substring line i2)))
  19015. (if (> i1 0)
  19016. (concat (make-string i1 ?\ ) l)
  19017. l)))
  19018. (defun org-remove-indentation (code &optional n)
  19019. "Remove the maximum common indentation from the lines in CODE.
  19020. N may optionally be the number of spaces to remove."
  19021. (with-temp-buffer
  19022. (insert code)
  19023. (org-do-remove-indentation n)
  19024. (buffer-string)))
  19025. (defun org-do-remove-indentation (&optional n)
  19026. "Remove the maximum common indentation from the buffer."
  19027. (untabify (point-min) (point-max))
  19028. (let ((min 10000) re)
  19029. (if n
  19030. (setq min n)
  19031. (goto-char (point-min))
  19032. (while (re-search-forward "^ *[^ \n]" nil t)
  19033. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  19034. (unless (or (= min 0) (= min 10000))
  19035. (setq re (format "^ \\{%d\\}" min))
  19036. (goto-char (point-min))
  19037. (while (re-search-forward re nil t)
  19038. (replace-match "")
  19039. (end-of-line 1))
  19040. min)))
  19041. (defun org-fill-template (template alist)
  19042. "Find each %key of ALIST in TEMPLATE and replace it."
  19043. (let ((case-fold-search nil)
  19044. entry key value)
  19045. (setq alist (sort (copy-sequence alist)
  19046. (lambda (a b) (< (length (car a)) (length (car b))))))
  19047. (while (setq entry (pop alist))
  19048. (setq template
  19049. (replace-regexp-in-string
  19050. (concat "%" (regexp-quote (car entry)))
  19051. (or (cdr entry) "") template t t)))
  19052. template))
  19053. (defun org-base-buffer (buffer)
  19054. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  19055. (if (not buffer)
  19056. buffer
  19057. (or (buffer-base-buffer buffer)
  19058. buffer)))
  19059. (defun org-trim (s)
  19060. "Remove whitespace at beginning and end of string."
  19061. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  19062. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  19063. s)
  19064. (defun org-wrap (string &optional width lines)
  19065. "Wrap string to either a number of lines, or a width in characters.
  19066. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  19067. that costs. If there is a word longer than WIDTH, the text is actually
  19068. wrapped to the length of that word.
  19069. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  19070. many lines, whatever width that takes.
  19071. The return value is a list of lines, without newlines at the end."
  19072. (let* ((words (org-split-string string "[ \t\n]+"))
  19073. (maxword (apply 'max (mapcar 'org-string-width words)))
  19074. w ll)
  19075. (cond (width
  19076. (org-do-wrap words (max maxword width)))
  19077. (lines
  19078. (setq w maxword)
  19079. (setq ll (org-do-wrap words maxword))
  19080. (if (<= (length ll) lines)
  19081. ll
  19082. (setq ll words)
  19083. (while (> (length ll) lines)
  19084. (setq w (1+ w))
  19085. (setq ll (org-do-wrap words w)))
  19086. ll))
  19087. (t (error "Cannot wrap this")))))
  19088. (defun org-do-wrap (words width)
  19089. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  19090. (let (lines line)
  19091. (while words
  19092. (setq line (pop words))
  19093. (while (and words (< (+ (length line) (length (car words))) width))
  19094. (setq line (concat line " " (pop words))))
  19095. (setq lines (push line lines)))
  19096. (nreverse lines)))
  19097. (defun org-split-string (string &optional separators)
  19098. "Splits STRING into substrings at SEPARATORS.
  19099. No empty strings are returned if there are matches at the beginning
  19100. and end of string."
  19101. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  19102. (start 0)
  19103. notfirst
  19104. (list nil))
  19105. (while (and (string-match rexp string
  19106. (if (and notfirst
  19107. (= start (match-beginning 0))
  19108. (< start (length string)))
  19109. (1+ start) start))
  19110. (< (match-beginning 0) (length string)))
  19111. (setq notfirst t)
  19112. (or (eq (match-beginning 0) 0)
  19113. (and (eq (match-beginning 0) (match-end 0))
  19114. (eq (match-beginning 0) start))
  19115. (setq list
  19116. (cons (substring string start (match-beginning 0))
  19117. list)))
  19118. (setq start (match-end 0)))
  19119. (or (eq start (length string))
  19120. (setq list
  19121. (cons (substring string start)
  19122. list)))
  19123. (nreverse list)))
  19124. (defun org-quote-vert (s)
  19125. "Replace \"|\" with \"\\vert\"."
  19126. (while (string-match "|" s)
  19127. (setq s (replace-match "\\vert" t t s)))
  19128. s)
  19129. (defun org-uuidgen-p (s)
  19130. "Is S an ID created by UUIDGEN?"
  19131. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  19132. (defun org-in-src-block-p (&optional inside)
  19133. "Whether point is in a code source block.
  19134. When INSIDE is non-nil, don't consider we are within a src block
  19135. when point is at #+BEGIN_SRC or #+END_SRC."
  19136. (let ((case-fold-search t) ov)
  19137. (or (and (setq ov (overlays-at (point)))
  19138. (memq 'org-block-background
  19139. (overlay-properties (car ov))))
  19140. (and (not inside)
  19141. (save-match-data
  19142. (save-excursion
  19143. (beginning-of-line)
  19144. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  19145. (defun org-context ()
  19146. "Return a list of contexts of the current cursor position.
  19147. If several contexts apply, all are returned.
  19148. Each context entry is a list with a symbol naming the context, and
  19149. two positions indicating start and end of the context. Possible
  19150. contexts are:
  19151. :headline anywhere in a headline
  19152. :headline-stars on the leading stars in a headline
  19153. :todo-keyword on a TODO keyword (including DONE) in a headline
  19154. :tags on the TAGS in a headline
  19155. :priority on the priority cookie in a headline
  19156. :item on the first line of a plain list item
  19157. :item-bullet on the bullet/number of a plain list item
  19158. :checkbox on the checkbox in a plain list item
  19159. :table in an org-mode table
  19160. :table-special on a special filed in a table
  19161. :table-table in a table.el table
  19162. :clocktable in a clocktable
  19163. :src-block in a source block
  19164. :link on a hyperlink
  19165. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  19166. :target on a <<target>>
  19167. :radio-target on a <<<radio-target>>>
  19168. :latex-fragment on a LaTeX fragment
  19169. :latex-preview on a LaTeX fragment with overlaid preview image
  19170. This function expects the position to be visible because it uses font-lock
  19171. faces as a help to recognize the following contexts: :table-special, :link,
  19172. and :keyword."
  19173. (let* ((f (get-text-property (point) 'face))
  19174. (faces (if (listp f) f (list f)))
  19175. (case-fold-search t)
  19176. (p (point)) clist o)
  19177. ;; First the large context
  19178. (cond
  19179. ((org-at-heading-p t)
  19180. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  19181. (when (progn
  19182. (beginning-of-line 1)
  19183. (looking-at org-todo-line-tags-regexp))
  19184. (push (org-point-in-group p 1 :headline-stars) clist)
  19185. (push (org-point-in-group p 2 :todo-keyword) clist)
  19186. (push (org-point-in-group p 4 :tags) clist))
  19187. (goto-char p)
  19188. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  19189. (if (looking-at "\\[#[A-Z0-9]\\]")
  19190. (push (org-point-in-group p 0 :priority) clist)))
  19191. ((org-at-item-p)
  19192. (push (org-point-in-group p 2 :item-bullet) clist)
  19193. (push (list :item (point-at-bol)
  19194. (save-excursion (org-end-of-item) (point)))
  19195. clist)
  19196. (and (org-at-item-checkbox-p)
  19197. (push (org-point-in-group p 0 :checkbox) clist)))
  19198. ((org-at-table-p)
  19199. (push (list :table (org-table-begin) (org-table-end)) clist)
  19200. (if (memq 'org-formula faces)
  19201. (push (list :table-special
  19202. (previous-single-property-change p 'face)
  19203. (next-single-property-change p 'face)) clist)))
  19204. ((org-at-table-p 'any)
  19205. (push (list :table-table) clist)))
  19206. (goto-char p)
  19207. (let ((case-fold-search t))
  19208. ;; New the "medium" contexts: clocktables, source blocks
  19209. (cond ((org-in-clocktable-p)
  19210. (push (list :clocktable
  19211. (and (or (looking-at "#\\+BEGIN: clocktable")
  19212. (search-backward "#+BEGIN: clocktable" nil t))
  19213. (match-beginning 0))
  19214. (and (re-search-forward "#\\+END:?" nil t)
  19215. (match-end 0))) clist))
  19216. ((org-in-src-block-p)
  19217. (push (list :src-block
  19218. (and (or (looking-at "#\\+BEGIN_SRC")
  19219. (search-backward "#+BEGIN_SRC" nil t))
  19220. (match-beginning 0))
  19221. (and (search-forward "#+END_SRC" nil t)
  19222. (match-beginning 0))) clist))))
  19223. (goto-char p)
  19224. ;; Now the small context
  19225. (cond
  19226. ((org-at-timestamp-p)
  19227. (push (org-point-in-group p 0 :timestamp) clist))
  19228. ((memq 'org-link faces)
  19229. (push (list :link
  19230. (previous-single-property-change p 'face)
  19231. (next-single-property-change p 'face)) clist))
  19232. ((memq 'org-special-keyword faces)
  19233. (push (list :keyword
  19234. (previous-single-property-change p 'face)
  19235. (next-single-property-change p 'face)) clist))
  19236. ((org-at-target-p)
  19237. (push (org-point-in-group p 0 :target) clist)
  19238. (goto-char (1- (match-beginning 0)))
  19239. (if (looking-at org-radio-target-regexp)
  19240. (push (org-point-in-group p 0 :radio-target) clist))
  19241. (goto-char p))
  19242. ((setq o (car (delq nil
  19243. (mapcar
  19244. (lambda (x)
  19245. (if (memq x org-latex-fragment-image-overlays) x))
  19246. (overlays-at (point))))))
  19247. (push (list :latex-fragment
  19248. (overlay-start o) (overlay-end o)) clist)
  19249. (push (list :latex-preview
  19250. (overlay-start o) (overlay-end o)) clist))
  19251. ((org-inside-LaTeX-fragment-p)
  19252. ;; FIXME: positions wrong.
  19253. (push (list :latex-fragment (point) (point)) clist)))
  19254. (setq clist (nreverse (delq nil clist)))
  19255. clist))
  19256. ;; FIXME: Compare with at-regexp-p Do we need both?
  19257. (defun org-in-regexp (re &optional nlines visually)
  19258. "Check if point is inside a match of regexp.
  19259. Normally only the current line is checked, but you can include NLINES extra
  19260. lines both before and after point into the search.
  19261. If VISUALLY is set, require that the cursor is not after the match but
  19262. really on, so that the block visually is on the match."
  19263. (catch 'exit
  19264. (let ((pos (point))
  19265. (eol (point-at-eol (+ 1 (or nlines 0))))
  19266. (inc (if visually 1 0)))
  19267. (save-excursion
  19268. (beginning-of-line (- 1 (or nlines 0)))
  19269. (while (re-search-forward re eol t)
  19270. (if (and (<= (match-beginning 0) pos)
  19271. (>= (+ inc (match-end 0)) pos))
  19272. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  19273. (defun org-at-regexp-p (regexp)
  19274. "Is point inside a match of REGEXP in the current line?"
  19275. (catch 'exit
  19276. (save-excursion
  19277. (let ((pos (point)) (end (point-at-eol)))
  19278. (beginning-of-line 1)
  19279. (while (re-search-forward regexp end t)
  19280. (if (and (<= (match-beginning 0) pos)
  19281. (>= (match-end 0) pos))
  19282. (throw 'exit t)))
  19283. nil))))
  19284. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  19285. "Non-nil when point is between matches of START-RE and END-RE.
  19286. Also return a non-nil value when point is on one of the matches.
  19287. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  19288. buffer positions. Default values are the positions of headlines
  19289. surrounding the point.
  19290. The functions returns a cons cell whose car (resp. cdr) is the
  19291. position before START-RE (resp. after END-RE)."
  19292. (save-match-data
  19293. (let ((pos (point))
  19294. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  19295. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  19296. beg end)
  19297. (save-excursion
  19298. ;; Point is on a block when on START-RE or if START-RE can be
  19299. ;; found before it...
  19300. (and (or (org-at-regexp-p start-re)
  19301. (re-search-backward start-re limit-up t))
  19302. (setq beg (match-beginning 0))
  19303. ;; ... and END-RE after it...
  19304. (goto-char (match-end 0))
  19305. (re-search-forward end-re limit-down t)
  19306. (> (setq end (match-end 0)) pos)
  19307. ;; ... without another START-RE in-between.
  19308. (goto-char (match-beginning 0))
  19309. (not (re-search-backward start-re (1+ beg) t))
  19310. ;; Return value.
  19311. (cons beg end))))))
  19312. (defun org-in-block-p (names)
  19313. "Non-nil when point belongs to a block whose name belongs to NAMES.
  19314. NAMES is a list of strings containing names of blocks.
  19315. Return first block name matched, or nil. Beware that in case of
  19316. nested blocks, the returned name may not belong to the closest
  19317. block from point."
  19318. (save-match-data
  19319. (catch 'exit
  19320. (let ((case-fold-search t)
  19321. (lim-up (save-excursion (outline-previous-heading)))
  19322. (lim-down (save-excursion (outline-next-heading))))
  19323. (mapc (lambda (name)
  19324. (let ((n (regexp-quote name)))
  19325. (when (org-between-regexps-p
  19326. (concat "^[ \t]*#\\+begin_" n)
  19327. (concat "^[ \t]*#\\+end_" n)
  19328. lim-up lim-down)
  19329. (throw 'exit n))))
  19330. names))
  19331. nil)))
  19332. (defun org-occur-in-agenda-files (regexp &optional nlines)
  19333. "Call `multi-occur' with buffers for all agenda files."
  19334. (interactive "sOrg-files matching: \np")
  19335. (let* ((files (org-agenda-files))
  19336. (tnames (mapcar 'file-truename files))
  19337. (extra org-agenda-text-search-extra-files)
  19338. f)
  19339. (when (eq (car extra) 'agenda-archives)
  19340. (setq extra (cdr extra))
  19341. (setq files (org-add-archive-files files)))
  19342. (while (setq f (pop extra))
  19343. (unless (member (file-truename f) tnames)
  19344. (add-to-list 'files f 'append)
  19345. (add-to-list 'tnames (file-truename f) 'append)))
  19346. (multi-occur
  19347. (mapcar (lambda (x)
  19348. (with-current-buffer
  19349. (or (get-file-buffer x) (find-file-noselect x))
  19350. (widen)
  19351. (current-buffer)))
  19352. files)
  19353. regexp)))
  19354. (if (boundp 'occur-mode-find-occurrence-hook)
  19355. ;; Emacs 23
  19356. (add-hook 'occur-mode-find-occurrence-hook
  19357. (lambda ()
  19358. (when (derived-mode-p 'org-mode)
  19359. (org-reveal))))
  19360. ;; Emacs 22
  19361. (defadvice occur-mode-goto-occurrence
  19362. (after org-occur-reveal activate)
  19363. (and (derived-mode-p 'org-mode) (org-reveal)))
  19364. (defadvice occur-mode-goto-occurrence-other-window
  19365. (after org-occur-reveal activate)
  19366. (and (derived-mode-p 'org-mode) (org-reveal)))
  19367. (defadvice occur-mode-display-occurrence
  19368. (after org-occur-reveal activate)
  19369. (when (derived-mode-p 'org-mode)
  19370. (let ((pos (occur-mode-find-occurrence)))
  19371. (with-current-buffer (marker-buffer pos)
  19372. (save-excursion
  19373. (goto-char pos)
  19374. (org-reveal)))))))
  19375. (defun org-occur-link-in-agenda-files ()
  19376. "Create a link and search for it in the agendas.
  19377. The link is not stored in `org-stored-links', it is just created
  19378. for the search purpose."
  19379. (interactive)
  19380. (let ((link (condition-case nil
  19381. (org-store-link nil)
  19382. (error "Unable to create a link to here"))))
  19383. (org-occur-in-agenda-files (regexp-quote link))))
  19384. (defun org-uniquify (list)
  19385. "Remove duplicate elements from LIST."
  19386. (let (res)
  19387. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  19388. res))
  19389. (defun org-delete-all (elts list)
  19390. "Remove all elements in ELTS from LIST."
  19391. (while elts
  19392. (setq list (delete (pop elts) list)))
  19393. list)
  19394. (defun org-count (cl-item cl-seq)
  19395. "Count the number of occurrences of ITEM in SEQ.
  19396. Taken from `count' in cl-seq.el with all keyword arguments removed."
  19397. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  19398. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  19399. (while (< cl-start cl-end)
  19400. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  19401. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  19402. (setq cl-start (1+ cl-start)))
  19403. cl-count))
  19404. (defun org-remove-if (predicate seq)
  19405. "Remove everything from SEQ that fulfills PREDICATE."
  19406. (let (res e)
  19407. (while seq
  19408. (setq e (pop seq))
  19409. (if (not (funcall predicate e)) (push e res)))
  19410. (nreverse res)))
  19411. (defun org-remove-if-not (predicate seq)
  19412. "Remove everything from SEQ that does not fulfill PREDICATE."
  19413. (let (res e)
  19414. (while seq
  19415. (setq e (pop seq))
  19416. (if (funcall predicate e) (push e res)))
  19417. (nreverse res)))
  19418. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  19419. "Reduce two-argument FUNCTION across SEQ.
  19420. Taken from `reduce' in cl-seq.el with all keyword arguments but
  19421. \":initial-value\" removed."
  19422. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  19423. (cadr (memq :initial-value cl-keys)))
  19424. (cl-seq (pop cl-seq))
  19425. (t (funcall cl-func)))))
  19426. (while cl-seq
  19427. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  19428. cl-accum))
  19429. (defun org-back-over-empty-lines ()
  19430. "Move backwards over whitespace, to the beginning of the first empty line.
  19431. Returns the number of empty lines passed."
  19432. (let ((pos (point)))
  19433. (if (cdr (assoc 'heading org-blank-before-new-entry))
  19434. (skip-chars-backward " \t\n\r")
  19435. (unless (eobp)
  19436. (forward-line -1)))
  19437. (beginning-of-line 2)
  19438. (goto-char (min (point) pos))
  19439. (count-lines (point) pos)))
  19440. (defun org-skip-whitespace ()
  19441. (skip-chars-forward " \t\n\r"))
  19442. (defun org-point-in-group (point group &optional context)
  19443. "Check if POINT is in match-group GROUP.
  19444. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  19445. match. If the match group does not exist or point is not inside it,
  19446. return nil."
  19447. (and (match-beginning group)
  19448. (>= point (match-beginning group))
  19449. (<= point (match-end group))
  19450. (if context
  19451. (list context (match-beginning group) (match-end group))
  19452. t)))
  19453. (defun org-switch-to-buffer-other-window (&rest args)
  19454. "Switch to buffer in a second window on the current frame.
  19455. In particular, do not allow pop-up frames.
  19456. Returns the newly created buffer."
  19457. (org-no-popups
  19458. (apply 'switch-to-buffer-other-window args)))
  19459. (defun org-combine-plists (&rest plists)
  19460. "Create a single property list from all plists in PLISTS.
  19461. The process starts by copying the first list, and then setting properties
  19462. from the other lists. Settings in the last list are the most significant
  19463. ones and overrule settings in the other lists."
  19464. (let ((rtn (copy-sequence (pop plists)))
  19465. p v ls)
  19466. (while plists
  19467. (setq ls (pop plists))
  19468. (while ls
  19469. (setq p (pop ls) v (pop ls))
  19470. (setq rtn (plist-put rtn p v))))
  19471. rtn))
  19472. (defun org-replace-escapes (string table)
  19473. "Replace %-escapes in STRING with values in TABLE.
  19474. TABLE is an association list with keys like \"%a\" and string values.
  19475. The sequences in STRING may contain normal field width and padding information,
  19476. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  19477. so values can contain further %-escapes if they are define later in TABLE."
  19478. (let ((tbl (copy-alist table))
  19479. (case-fold-search nil)
  19480. (pchg 0)
  19481. e re rpl)
  19482. (while (setq e (pop tbl))
  19483. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  19484. (when (and (cdr e) (string-match re (cdr e)))
  19485. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  19486. (safe "SREF"))
  19487. (add-text-properties 0 3 (list 'sref sref) safe)
  19488. (setcdr e (replace-match safe t t (cdr e)))))
  19489. (while (string-match re string)
  19490. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  19491. (cdr e)))
  19492. (setq string (replace-match rpl t t string))))
  19493. (while (setq pchg (next-property-change pchg string))
  19494. (let ((sref (get-text-property pchg 'sref string)))
  19495. (when (and sref (string-match "SREF" string pchg))
  19496. (setq string (replace-match sref t t string)))))
  19497. string))
  19498. (defun org-sublist (list start end)
  19499. "Return a section of LIST, from START to END.
  19500. Counting starts at 1."
  19501. (let (rtn (c start))
  19502. (setq list (nthcdr (1- start) list))
  19503. (while (and list (<= c end))
  19504. (push (pop list) rtn)
  19505. (setq c (1+ c)))
  19506. (nreverse rtn)))
  19507. (defun org-find-base-buffer-visiting (file)
  19508. "Like `find-buffer-visiting' but always return the base buffer and
  19509. not an indirect buffer."
  19510. (let ((buf (or (get-file-buffer file)
  19511. (find-buffer-visiting file))))
  19512. (if buf
  19513. (or (buffer-base-buffer buf) buf)
  19514. nil)))
  19515. (defun org-image-file-name-regexp (&optional extensions)
  19516. "Return regexp matching the file names of images.
  19517. If EXTENSIONS is given, only match these."
  19518. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  19519. (image-file-name-regexp)
  19520. (let ((image-file-name-extensions
  19521. (or extensions
  19522. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  19523. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  19524. (concat "\\."
  19525. (regexp-opt (nconc (mapcar 'upcase
  19526. image-file-name-extensions)
  19527. image-file-name-extensions)
  19528. t)
  19529. "\\'"))))
  19530. (defun org-file-image-p (file &optional extensions)
  19531. "Return non-nil if FILE is an image."
  19532. (save-match-data
  19533. (string-match (org-image-file-name-regexp extensions) file)))
  19534. (defun org-get-cursor-date (&optional with-time)
  19535. "Return the date at cursor in as a time.
  19536. This works in the calendar and in the agenda, anywhere else it just
  19537. returns the current time.
  19538. If WITH-TIME is non-nil, returns the time of the event at point (in
  19539. the agenda) or the current time of the day."
  19540. (let (date day defd tp tm hod mod)
  19541. (when with-time
  19542. (setq tp (get-text-property (point) 'time))
  19543. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  19544. (setq hod (string-to-number (match-string 1 tp))
  19545. mod (string-to-number (match-string 2 tp))))
  19546. (or tp (setq hod (nth 2 (decode-time (current-time)))
  19547. mod (nth 1 (decode-time (current-time))))))
  19548. (cond
  19549. ((eq major-mode 'calendar-mode)
  19550. (setq date (calendar-cursor-to-date)
  19551. defd (encode-time 0 (or mod 0) (or hod 0)
  19552. (nth 1 date) (nth 0 date) (nth 2 date))))
  19553. ((eq major-mode 'org-agenda-mode)
  19554. (setq day (get-text-property (point) 'day))
  19555. (if day
  19556. (setq date (calendar-gregorian-from-absolute day)
  19557. defd (encode-time 0 (or mod 0) (or hod 0)
  19558. (nth 1 date) (nth 0 date) (nth 2 date))))))
  19559. (or defd (current-time))))
  19560. (defun org-mark-subtree (&optional up)
  19561. "Mark the current subtree.
  19562. This puts point at the start of the current subtree, and mark at
  19563. the end. If a numeric prefix UP is given, move up into the
  19564. hierarchy of headlines by UP levels before marking the subtree."
  19565. (interactive "P")
  19566. (org-with-limited-levels
  19567. (cond ((org-at-heading-p) (beginning-of-line))
  19568. ((org-before-first-heading-p) (error "Not in a subtree"))
  19569. (t (outline-previous-visible-heading 1))))
  19570. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  19571. (if (org-called-interactively-p 'any)
  19572. (call-interactively 'org-mark-element)
  19573. (org-mark-element)))
  19574. ;;; Macros
  19575. ;; Macros are expanded with `org-macro-replace-all', which relies
  19576. ;; internally on `org-macro-expand'.
  19577. ;; Default templates for expansion are stored in the buffer-local
  19578. ;; variable `org-macro-templates'. This variable is updated by
  19579. ;; `org-macro-initialize-templates'.
  19580. ;; Along with macros defined through #+MACRO: keyword, default
  19581. ;; templates include the following hard-coded macros:
  19582. ;; {{{time(format-string)}}}, {{{property(node-property)}}},
  19583. ;; {{{input-file}}} and {{{modification-time(format-string)}}}.
  19584. ;; During export, {{{author}}}, {{{date}}}, {{{email}}} and
  19585. ;; {{{title}}} will also be provided.
  19586. (defvar org-macro-templates nil
  19587. "Alist containing all macro templates in current buffer.
  19588. Associations are in the shape of (NAME . TEMPLATE) where NAME
  19589. stands for macro's name and template for its replacement value,
  19590. both as strings. This is an internal variable. Do not set it
  19591. directly, use instead:
  19592. #+MACRO: name template")
  19593. (make-variable-buffer-local 'org-macro-templates)
  19594. (defun org-macro-expand (macro templates)
  19595. "Return expanded MACRO, as a string.
  19596. MACRO is an object, obtained, for example, with
  19597. `org-element-context'. TEMPLATES is an alist of templates used
  19598. for expansion. See `org-macro-templates' for a buffer-local
  19599. default value. Return nil if no template was found."
  19600. (let ((template
  19601. ;; Macro names are case-insensitive.
  19602. (cdr (assoc-string (org-element-property :key macro) templates t))))
  19603. (when template
  19604. (let ((value (replace-regexp-in-string
  19605. "\\$[0-9]+"
  19606. (lambda (arg)
  19607. (or (nth (1- (string-to-number (substring arg 1)))
  19608. (org-element-property :args macro))
  19609. ;; No argument provided: remove
  19610. ;; place-holder.
  19611. ""))
  19612. template)))
  19613. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  19614. (when (string-match "\\`(eval\\>" value)
  19615. (setq value (eval (read value))))
  19616. ;; Return string.
  19617. (format "%s" (or value ""))))))
  19618. (defun org-macro-replace-all (templates)
  19619. "Replace all macros in current buffer by their expansion.
  19620. TEMPLATES is an alist of templates used for expansion. See
  19621. `org-macro-templates' for a buffer-local default value."
  19622. (save-excursion
  19623. (goto-char (point-min))
  19624. (let (record)
  19625. (while (re-search-forward "{{{[-A-Za-z0-9_]" nil t)
  19626. (let ((object (org-element-context)))
  19627. (when (eq (org-element-type object) 'macro)
  19628. (let* ((value (org-macro-expand object templates))
  19629. (begin (org-element-property :begin object))
  19630. (signature (list begin
  19631. object
  19632. (org-element-property :args object))))
  19633. ;; Avoid circular dependencies by checking if the same
  19634. ;; macro with the same arguments is expanded at the same
  19635. ;; position twice.
  19636. (if (member signature record)
  19637. (error "Circular macro expansion: %s"
  19638. (org-element-property :key object))
  19639. (when value
  19640. (push signature record)
  19641. (delete-region
  19642. begin
  19643. ;; Preserve white spaces after the macro.
  19644. (progn (goto-char (org-element-property :end object))
  19645. (skip-chars-backward " \t")
  19646. (point)))
  19647. ;; Leave point before replacement in case of recursive
  19648. ;; expansions.
  19649. (save-excursion (insert value)))))))))))
  19650. (defun org-macro-initialize-templates ()
  19651. "Collect macro templates defined in current buffer.
  19652. Templates are stored in buffer-local variable
  19653. `org-macro-templates'. In addition to buffer-defined macros, the
  19654. function installs the following ones: \"property\",
  19655. \"time\". and, if the buffer is associated to a file,
  19656. \"input-file\" and \"modification-time\"."
  19657. (let ((case-fold-search t)
  19658. (set-template
  19659. (lambda (cell)
  19660. ;; Add CELL to `org-macro-templates' if there's no
  19661. ;; association matching its name already. Otherwise,
  19662. ;; replace old association with the new one in that
  19663. ;; variable.
  19664. (let ((old-template (assoc (car cell) org-macro-templates)))
  19665. (if old-template (setcdr old-template (cdr cell))
  19666. (push cell org-macro-templates))))))
  19667. ;; Install buffer-local macros.
  19668. (org-with-wide-buffer
  19669. (goto-char (point-min))
  19670. (while (re-search-forward "^[ \t]*#\\+MACRO:" nil t)
  19671. (let ((element (org-element-at-point)))
  19672. (when (eq (org-element-type element) 'keyword)
  19673. (let ((value (org-element-property :value element)))
  19674. (when (string-match "^\\(.*?\\)\\(?:\\s-+\\(.*\\)\\)?\\s-*$" value)
  19675. (funcall set-template
  19676. (cons (match-string 1 value)
  19677. (or (match-string 2 value) "")))))))))
  19678. ;; Install hard-coded macros.
  19679. (mapc (lambda (cell) (funcall set-template cell))
  19680. (list
  19681. (cons "property" "(eval (org-entry-get nil \"$1\" 'selective))")
  19682. (cons "time" "(eval (format-time-string \"$1\"))")))
  19683. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  19684. (when (and visited-file (file-exists-p visited-file))
  19685. (mapc (lambda (cell) (funcall set-template cell))
  19686. (list
  19687. (cons "input-file" (file-name-nondirectory visited-file))
  19688. (cons "modification-time"
  19689. (format "(eval (format-time-string \"$1\" '%s))"
  19690. (prin1-to-string
  19691. (nth 5 (file-attributes visited-file)))))))))))
  19692. ;;; Indentation
  19693. (defun org-indent-line ()
  19694. "Indent line depending on context."
  19695. (interactive)
  19696. (let* ((pos (point))
  19697. (itemp (org-at-item-p))
  19698. (case-fold-search t)
  19699. (org-drawer-regexp (or org-drawer-regexp "\000"))
  19700. (inline-task-p (and (featurep 'org-inlinetask)
  19701. (org-inlinetask-in-task-p)))
  19702. (inline-re (and inline-task-p
  19703. (org-inlinetask-outline-regexp)))
  19704. column)
  19705. (if (and orgstruct-is-++ (eq pos (point)))
  19706. (let ((indent-line-function (cadadr (assoc 'indent-line-function org-fb-vars))))
  19707. (indent-according-to-mode))
  19708. (beginning-of-line 1)
  19709. (cond
  19710. ;; Headings
  19711. ((looking-at org-outline-regexp) (setq column 0))
  19712. ;; Footnote definition
  19713. ((looking-at org-footnote-definition-re) (setq column 0))
  19714. ;; Literal examples
  19715. ((looking-at "[ \t]*:\\( \\|$\\)")
  19716. (setq column (org-get-indentation))) ; do nothing
  19717. ;; Lists
  19718. ((ignore-errors (goto-char (org-in-item-p)))
  19719. (setq column (if itemp
  19720. (org-get-indentation)
  19721. (org-list-item-body-column (point))))
  19722. (goto-char pos))
  19723. ;; Drawers
  19724. ((and (looking-at "[ \t]*:END:")
  19725. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  19726. (save-excursion
  19727. (goto-char (1- (match-beginning 1)))
  19728. (setq column (current-column))))
  19729. ;; Special blocks
  19730. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  19731. (save-excursion
  19732. (re-search-backward
  19733. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  19734. (setq column (org-get-indentation (match-string 0))))
  19735. ((and (not (looking-at "[ \t]*#\\+begin_"))
  19736. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  19737. (save-excursion
  19738. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  19739. (setq column
  19740. (cond ((equal (downcase (match-string 1)) "src")
  19741. ;; src blocks: let `org-edit-src-exit' handle them
  19742. (org-get-indentation))
  19743. ((equal (downcase (match-string 1)) "example")
  19744. (max (org-get-indentation)
  19745. (org-get-indentation (match-string 0))))
  19746. (t
  19747. (org-get-indentation (match-string 0))))))
  19748. ;; This line has nothing special, look at the previous relevant
  19749. ;; line to compute indentation
  19750. (t
  19751. (beginning-of-line 0)
  19752. (while (and (not (bobp))
  19753. (not (looking-at org-table-line-regexp))
  19754. (not (looking-at org-drawer-regexp))
  19755. ;; When point started in an inline task, do not move
  19756. ;; above task starting line.
  19757. (not (and inline-task-p (looking-at inline-re)))
  19758. ;; Skip drawers, blocks, empty lines, verbatim,
  19759. ;; comments, tables, footnotes definitions, lists,
  19760. ;; inline tasks.
  19761. (or (and (looking-at "[ \t]*:END:")
  19762. (re-search-backward org-drawer-regexp nil t))
  19763. (and (looking-at "[ \t]*#\\+end_")
  19764. (re-search-backward "[ \t]*#\\+begin_"nil t))
  19765. (looking-at "[ \t]*[\n:#|]")
  19766. (looking-at org-footnote-definition-re)
  19767. (and (ignore-errors (goto-char (org-in-item-p)))
  19768. (goto-char
  19769. (org-list-get-top-point (org-list-struct))))
  19770. (and (not inline-task-p)
  19771. (featurep 'org-inlinetask)
  19772. (org-inlinetask-in-task-p)
  19773. (or (org-inlinetask-goto-beginning) t))))
  19774. (beginning-of-line 0))
  19775. (cond
  19776. ;; There was an heading above.
  19777. ((looking-at "\\*+[ \t]+")
  19778. (if (not org-adapt-indentation)
  19779. (setq column 0)
  19780. (goto-char (match-end 0))
  19781. (setq column (current-column))))
  19782. ;; A drawer had started and is unfinished
  19783. ((looking-at org-drawer-regexp)
  19784. (goto-char (1- (match-beginning 1)))
  19785. (setq column (current-column)))
  19786. ;; Else, nothing noticeable found: get indentation and go on.
  19787. (t (setq column (org-get-indentation))))))
  19788. ;; Now apply indentation and move cursor accordingly
  19789. (goto-char pos)
  19790. (if (<= (current-column) (current-indentation))
  19791. (org-indent-line-to column)
  19792. (save-excursion (org-indent-line-to column)))
  19793. ;; Special polishing for properties, see `org-property-format'
  19794. (setq column (current-column))
  19795. (beginning-of-line 1)
  19796. (if (looking-at
  19797. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  19798. (replace-match (concat (match-string 1)
  19799. (format org-property-format
  19800. (match-string 2) (match-string 3)))
  19801. t t))
  19802. (org-move-to-column column))))
  19803. (defun org-indent-drawer ()
  19804. "Indent the drawer at point."
  19805. (interactive)
  19806. (let ((p (point))
  19807. (e (and (save-excursion (re-search-forward ":END:" nil t))
  19808. (match-end 0)))
  19809. (folded
  19810. (save-excursion
  19811. (end-of-line)
  19812. (when (overlays-at (point))
  19813. (member 'invisible (overlay-properties
  19814. (car (overlays-at (point)))))))))
  19815. (when folded (org-cycle))
  19816. (indent-for-tab-command)
  19817. (while (and (move-beginning-of-line 2) (< (point) e))
  19818. (indent-for-tab-command))
  19819. (goto-char p)
  19820. (when folded (org-cycle)))
  19821. (message "Drawer at point indented"))
  19822. (defun org-indent-block ()
  19823. "Indent the block at point."
  19824. (interactive)
  19825. (let ((p (point))
  19826. (case-fold-search t)
  19827. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  19828. (match-end 0)))
  19829. (folded
  19830. (save-excursion
  19831. (end-of-line)
  19832. (when (overlays-at (point))
  19833. (member 'invisible (overlay-properties
  19834. (car (overlays-at (point)))))))))
  19835. (when folded (org-cycle))
  19836. (indent-for-tab-command)
  19837. (while (and (move-beginning-of-line 2) (< (point) e))
  19838. (indent-for-tab-command))
  19839. (goto-char p)
  19840. (when folded (org-cycle)))
  19841. (message "Block at point indented"))
  19842. (defun org-indent-region (start end)
  19843. "Indent region."
  19844. (interactive "r")
  19845. (save-excursion
  19846. (let ((line-end (org-current-line end)))
  19847. (goto-char start)
  19848. (while (< (org-current-line) line-end)
  19849. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19850. (t (call-interactively 'org-indent-line)))
  19851. (move-beginning-of-line 2)))))
  19852. ;;; Filling
  19853. ;; We use our own fill-paragraph and auto-fill functions.
  19854. ;; `org-fill-paragraph' relies on adaptive filling and context
  19855. ;; checking. Appropriate `fill-prefix' is computed with
  19856. ;; `org-adaptive-fill-function'.
  19857. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  19858. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  19859. ;; `org-adaptive-fill-function' value. Internally,
  19860. ;; `org-comment-line-break-function' breaks the line.
  19861. ;; `org-setup-filling' installs filling and auto-filling related
  19862. ;; variables during `org-mode' initialization.
  19863. (defun org-setup-filling ()
  19864. (interactive)
  19865. ;; Prevent auto-fill from inserting unwanted new items.
  19866. (when (boundp 'fill-nobreak-predicate)
  19867. (org-set-local
  19868. 'fill-nobreak-predicate
  19869. (org-uniquify
  19870. (append fill-nobreak-predicate
  19871. '(org-fill-paragraph-separate-nobreak-p
  19872. org-fill-line-break-nobreak-p)))))
  19873. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19874. (org-set-local 'auto-fill-inhibit-regexp nil)
  19875. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  19876. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19877. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  19878. (defvar org-element-paragraph-separate) ; org-element.el
  19879. (defun org-fill-paragraph-separate-nobreak-p ()
  19880. "Non-nil when a line break at point would insert a new item."
  19881. (looking-at (substring org-element-paragraph-separate 1)))
  19882. (defun org-fill-line-break-nobreak-p ()
  19883. "Non-nil when a line break at point would create an Org line break."
  19884. (save-excursion
  19885. (skip-chars-backward "[ \t]")
  19886. (skip-chars-backward "\\\\")
  19887. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19888. (declare-function message-in-body-p "message" ())
  19889. (defvar orgtbl-line-start-regexp) ; From org-table.el
  19890. (defun org-adaptive-fill-function ()
  19891. "Compute a fill prefix for the current line.
  19892. Return fill prefix, as a string, or nil if current line isn't
  19893. meant to be filled."
  19894. (let (prefix)
  19895. (catch 'exit
  19896. (when (derived-mode-p 'message-mode)
  19897. (save-excursion
  19898. (beginning-of-line)
  19899. (cond ((or (not (message-in-body-p))
  19900. (looking-at orgtbl-line-start-regexp))
  19901. (throw 'exit nil))
  19902. ((looking-at message-cite-prefix-regexp)
  19903. (throw 'exit (match-string-no-properties 0)))
  19904. ((looking-at org-outline-regexp)
  19905. (throw 'exit (make-string (length (match-string 0)) ? ))))))
  19906. (org-with-wide-buffer
  19907. (let* ((p (line-beginning-position))
  19908. (element (save-excursion (beginning-of-line)
  19909. (org-element-at-point)))
  19910. (type (org-element-type element))
  19911. (post-affiliated (org-element-property :post-affiliated element)))
  19912. (unless (and post-affiliated (< p post-affiliated))
  19913. (case type
  19914. (comment (looking-at "[ \t]*# ?") (match-string 0))
  19915. (footnote-definition "")
  19916. ((item plain-list)
  19917. (make-string (org-list-item-body-column
  19918. (or post-affiliated
  19919. (org-element-property :begin element)))
  19920. ? ))
  19921. (paragraph
  19922. ;; Fill prefix is usually the same as the current line,
  19923. ;; except if the paragraph is at the beginning of an item.
  19924. (let ((parent (org-element-property :parent element)))
  19925. (cond ((eq (org-element-type parent) 'item)
  19926. (make-string (org-list-item-body-column
  19927. (org-element-property :begin parent))
  19928. ? ))
  19929. ((save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19930. (match-string 0))
  19931. (t ""))))
  19932. (comment-block
  19933. ;; Only fill contents if P is within block boundaries.
  19934. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  19935. (forward-line)
  19936. (point)))
  19937. (cend (save-excursion
  19938. (goto-char (org-element-property :end element))
  19939. (skip-chars-backward " \r\t\n")
  19940. (line-beginning-position))))
  19941. (when (and (>= p cbeg) (< p cend))
  19942. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  19943. (match-string 0)
  19944. "")))))))))))
  19945. (declare-function message-goto-body "message" ())
  19946. (defvar message-cite-prefix-regexp) ; From message.el
  19947. (defvar org-element-all-objects) ; From org-element.el
  19948. (defun org-fill-paragraph (&optional justify)
  19949. "Fill element at point, when applicable.
  19950. This function only applies to comment blocks, comments, example
  19951. blocks and paragraphs. Also, as a special case, re-align table
  19952. when point is at one.
  19953. If JUSTIFY is non-nil (interactively, with prefix argument),
  19954. justify as well. If `sentence-end-double-space' is non-nil, then
  19955. period followed by one space does not end a sentence, so don't
  19956. break a line there. The variable `fill-column' controls the
  19957. width for filling.
  19958. For convenience, when point is at a plain list, an item or
  19959. a footnote definition, try to fill the first paragraph within."
  19960. (interactive)
  19961. (if (and (derived-mode-p 'message-mode)
  19962. (or (not (message-in-body-p))
  19963. (save-excursion (move-beginning-of-line 1)
  19964. (looking-at message-cite-prefix-regexp))))
  19965. ;; First ensure filling is correct in message-mode.
  19966. (let ((fill-paragraph-function
  19967. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  19968. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  19969. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  19970. (paragraph-separate
  19971. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  19972. (fill-paragraph nil))
  19973. (save-excursion
  19974. ;; Move to end of line in order to get the first paragraph
  19975. ;; within a plain list or a footnote definition.
  19976. (end-of-line)
  19977. (let ((element (org-element-at-point)))
  19978. ;; First check if point is in a blank line at the beginning of
  19979. ;; the buffer. In that case, ignore filling.
  19980. (if (< (point) (org-element-property :begin element)) t
  19981. (case (org-element-type element)
  19982. ;; Use major mode filling function is src blocks.
  19983. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  19984. ;; Align Org tables, leave table.el tables as-is.
  19985. (table-row (org-table-align) t)
  19986. (table
  19987. (when (eq (org-element-property :type element) 'org)
  19988. (org-table-align))
  19989. t)
  19990. (paragraph
  19991. ;; Paragraphs may contain `line-break' type objects.
  19992. (let ((beg (max (point-min)
  19993. (org-element-property :contents-begin element)))
  19994. (end (min (point-max)
  19995. (org-element-property :contents-end element))))
  19996. ;; Do nothing if point is at an affiliated keyword.
  19997. (if (< (point) beg) t
  19998. (when (derived-mode-p 'message-mode)
  19999. ;; In `message-mode', do not fill following
  20000. ;; citation in current paragraph nor text before
  20001. ;; message body.
  20002. (let ((body-start (save-excursion (message-goto-body))))
  20003. (when body-start (setq beg (max body-start beg))))
  20004. (when (save-excursion
  20005. (re-search-forward
  20006. (concat "^" message-cite-prefix-regexp) end t))
  20007. (setq end (match-beginning 0))))
  20008. ;; Fill paragraph, taking line breaks into
  20009. ;; consideration. For that, slice the paragraph
  20010. ;; using line breaks as separators, and fill the
  20011. ;; parts in reverse order to avoid messing with
  20012. ;; markers.
  20013. (save-excursion
  20014. (goto-char end)
  20015. (mapc
  20016. (lambda (pos)
  20017. (fill-region-as-paragraph pos (point) justify)
  20018. (goto-char pos))
  20019. ;; Find the list of ending positions for line
  20020. ;; breaks in the current paragraph. Add paragraph
  20021. ;; beginning to include first slice.
  20022. (nreverse
  20023. (cons
  20024. beg
  20025. (org-element-map
  20026. (org-element--parse-objects
  20027. beg end nil org-element-all-objects)
  20028. 'line-break
  20029. (lambda (lb) (org-element-property :end lb)))))))
  20030. t)))
  20031. ;; Contents of `comment-block' type elements should be
  20032. ;; filled as plain text, but only if point is within block
  20033. ;; markers.
  20034. (comment-block
  20035. (let* ((case-fold-search t)
  20036. (beg (save-excursion
  20037. (goto-char (org-element-property :begin element))
  20038. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  20039. (forward-line)
  20040. (point)))
  20041. (end (save-excursion
  20042. (goto-char (org-element-property :end element))
  20043. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  20044. (line-beginning-position))))
  20045. (when (and (>= (point) beg) (< (point) end))
  20046. (fill-region-as-paragraph
  20047. (save-excursion
  20048. (end-of-line)
  20049. (re-search-backward "^[ \t]*$" beg 'move)
  20050. (line-beginning-position))
  20051. (save-excursion
  20052. (beginning-of-line)
  20053. (re-search-forward "^[ \t]*$" end 'move)
  20054. (line-beginning-position))
  20055. justify)))
  20056. t)
  20057. ;; Fill comments.
  20058. (comment (fill-comment-paragraph justify))
  20059. ;; Ignore every other element.
  20060. (otherwise t)))))))
  20061. (defun org-auto-fill-function ()
  20062. "Auto-fill function."
  20063. ;; Check if auto-filling is meaningful.
  20064. (let ((fc (current-fill-column)))
  20065. (when (and fc (> (current-column) fc))
  20066. (let* ((fill-prefix (org-adaptive-fill-function))
  20067. ;; Enforce empty fill prefix, if required. Otherwise, it
  20068. ;; will be computed again.
  20069. (adaptive-fill-mode (not (equal fill-prefix ""))))
  20070. (when fill-prefix (do-auto-fill))))))
  20071. (defun org-comment-line-break-function (&optional soft)
  20072. "Break line at point and indent, continuing comment if within one.
  20073. The inserted newline is marked hard if variable
  20074. `use-hard-newlines' is true, unless optional argument SOFT is
  20075. non-nil."
  20076. (if soft (insert-and-inherit ?\n) (newline 1))
  20077. (save-excursion (forward-char -1) (delete-horizontal-space))
  20078. (delete-horizontal-space)
  20079. (indent-to-left-margin)
  20080. (insert-before-markers-and-inherit fill-prefix))
  20081. ;;; Comments
  20082. ;; Org comments syntax is quite complex. It requires the entire line
  20083. ;; to be just a comment. Also, even with the right syntax at the
  20084. ;; beginning of line, some some elements (i.e. verse-block or
  20085. ;; example-block) don't accept comments. Usual Emacs comment commands
  20086. ;; cannot cope with those requirements. Therefore, Org replaces them.
  20087. ;; Org still relies on `comment-dwim', but cannot trust
  20088. ;; `comment-only-p'. So, `comment-region-function' and
  20089. ;; `uncomment-region-function' both point
  20090. ;; to`org-comment-or-uncomment-region'. Eventually,
  20091. ;; `org-insert-comment' takes care of insertion of comments at the
  20092. ;; beginning of line.
  20093. ;; `org-setup-comments-handling' install comments related variables
  20094. ;; during `org-mode' initialization.
  20095. (defun org-setup-comments-handling ()
  20096. (interactive)
  20097. (org-set-local 'comment-use-syntax nil)
  20098. (org-set-local 'comment-start "# ")
  20099. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  20100. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  20101. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  20102. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  20103. (defun org-insert-comment ()
  20104. "Insert an empty comment above current line.
  20105. If the line is empty, insert comment at its beginning."
  20106. (beginning-of-line)
  20107. (if (looking-at "\\s-*$") (replace-match "") (open-line 1))
  20108. (org-indent-line)
  20109. (insert "# "))
  20110. (defvar comment-empty-lines) ; From newcomment.el.
  20111. (defun org-comment-or-uncomment-region (beg end &rest ignore)
  20112. "Comment or uncomment each non-blank line in the region.
  20113. Uncomment each non-blank line between BEG and END if it only
  20114. contains commented lines. Otherwise, comment them."
  20115. (save-restriction
  20116. ;; Restrict region
  20117. (narrow-to-region (save-excursion (goto-char beg)
  20118. (skip-chars-forward " \r\t\n" end)
  20119. (line-beginning-position))
  20120. (save-excursion (goto-char end)
  20121. (skip-chars-backward " \r\t\n" beg)
  20122. (line-end-position)))
  20123. (let ((uncommentp
  20124. ;; UNCOMMENTP is non-nil when every non blank line between
  20125. ;; BEG and END is a comment.
  20126. (save-excursion
  20127. (goto-char (point-min))
  20128. (while (and (not (eobp))
  20129. (let ((element (org-element-at-point)))
  20130. (and (eq (org-element-type element) 'comment)
  20131. (goto-char (min (point-max)
  20132. (org-element-property
  20133. :end element)))))))
  20134. (eobp))))
  20135. (if uncommentp
  20136. ;; Only blank lines and comments in region: uncomment it.
  20137. (save-excursion
  20138. (goto-char (point-min))
  20139. (while (not (eobp))
  20140. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  20141. (replace-match "" nil nil nil 1))
  20142. (forward-line)))
  20143. ;; Comment each line in region.
  20144. (let ((min-indent (point-max)))
  20145. ;; First find the minimum indentation across all lines.
  20146. (save-excursion
  20147. (goto-char (point-min))
  20148. (while (and (not (eobp)) (not (zerop min-indent)))
  20149. (unless (looking-at "[ \t]*$")
  20150. (setq min-indent (min min-indent (current-indentation))))
  20151. (forward-line)))
  20152. ;; Then loop over all lines.
  20153. (save-excursion
  20154. (goto-char (point-min))
  20155. (while (not (eobp))
  20156. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  20157. (org-move-to-column min-indent t)
  20158. (insert comment-start))
  20159. (forward-line))))))))
  20160. ;;; Planning
  20161. ;; This section contains tools to operate on timestamp objects, as
  20162. ;; returned by, e.g. `org-element-context'.
  20163. (defun org-timestamp-has-time-p (timestamp)
  20164. "Non-nil when TIMESTAMP has a time specified."
  20165. (org-element-property :hour-start timestamp))
  20166. (defun org-timestamp-format (timestamp format &optional end utc)
  20167. "Format a TIMESTAMP element into a string.
  20168. FORMAT is a format specifier to be passed to
  20169. `format-time-string'.
  20170. When optional argument END is non-nil, use end of date-range or
  20171. time-range, if possible.
  20172. When optional argument UTC is non-nil, time will be expressed as
  20173. Universal Time."
  20174. (format-time-string
  20175. format
  20176. (apply 'encode-time
  20177. (cons 0
  20178. (mapcar
  20179. (lambda (prop) (or (org-element-property prop timestamp) 0))
  20180. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  20181. '(:minute-start :hour-start :day-start :month-start
  20182. :year-start)))))
  20183. utc))
  20184. (defun org-timestamp-split-range (timestamp &optional end)
  20185. "Extract a timestamp object from a date or time range.
  20186. TIMESTAMP is a timestamp object. END, when non-nil, means extract
  20187. the end of the range. Otherwise, extract its start.
  20188. Return a new timestamp object sharing the same parent as
  20189. TIMESTAMP."
  20190. (let ((type (org-element-property :type timestamp)))
  20191. (if (memq type '(active inactive diary)) timestamp
  20192. (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
  20193. ;; Set new type.
  20194. (org-element-put-property
  20195. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  20196. ;; Copy start properties over end properties if END is
  20197. ;; non-nil. Otherwise, copy end properties over `start' ones.
  20198. (let ((p-alist '((:minute-start . :minute-end)
  20199. (:hour-start . :hour-end)
  20200. (:day-start . :day-end)
  20201. (:month-start . :month-end)
  20202. (:year-start . :year-end))))
  20203. (dolist (p-cell p-alist)
  20204. (org-element-put-property
  20205. split-ts
  20206. (funcall (if end 'car 'cdr) p-cell)
  20207. (org-element-property
  20208. (funcall (if end 'cdr 'car) p-cell) split-ts)))
  20209. ;; Eventually refresh `:raw-value'.
  20210. (org-element-put-property split-ts :raw-value nil)
  20211. (org-element-put-property
  20212. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  20213. (defun org-timestamp-translate (timestamp &optional boundary)
  20214. "Apply `org-translate-time' on a TIMESTAMP object.
  20215. When optional argument BOUNDARY is non-nil, it is either the
  20216. symbol `start' or `end'. In this case, only translate the
  20217. starting or ending part of TIMESTAMP if it is a date or time
  20218. range. Otherwise, translate both parts."
  20219. (if (and (not boundary)
  20220. (memq (org-element-property :type timestamp)
  20221. '(active-range inactive-range)))
  20222. (concat
  20223. (org-translate-time
  20224. (org-element-property :raw-value
  20225. (org-timestamp-split-range timestamp)))
  20226. "--"
  20227. (org-translate-time
  20228. (org-element-property :raw-value
  20229. (org-timestamp-split-range timestamp t))))
  20230. (org-translate-time
  20231. (org-element-property
  20232. :raw-value
  20233. (if (not boundary) timestamp
  20234. (org-timestamp-split-range timestamp (eq boundary 'end)))))))
  20235. ;;; Other stuff.
  20236. (defun org-toggle-fixed-width-section (arg)
  20237. "Toggle the fixed-width export.
  20238. If there is no active region, the QUOTE keyword at the current headline is
  20239. inserted or removed. When present, it causes the text between this headline
  20240. and the next to be exported as fixed-width text, and unmodified.
  20241. If there is an active region, this command adds or removes a colon as the
  20242. first character of this line. If the first character of a line is a colon,
  20243. this line is also exported in fixed-width font."
  20244. (interactive "P")
  20245. (let* ((cc 0)
  20246. (regionp (org-region-active-p))
  20247. (beg (if regionp (region-beginning) (point)))
  20248. (end (if regionp (region-end)))
  20249. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  20250. (case-fold-search nil)
  20251. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  20252. off)
  20253. (if regionp
  20254. (save-excursion
  20255. (goto-char beg)
  20256. (setq cc (current-column))
  20257. (beginning-of-line 1)
  20258. (setq off (looking-at re))
  20259. (while (> nlines 0)
  20260. (setq nlines (1- nlines))
  20261. (beginning-of-line 1)
  20262. (cond
  20263. (arg
  20264. (org-move-to-column cc t)
  20265. (insert ": \n")
  20266. (forward-line -1))
  20267. ((and off (looking-at re))
  20268. (replace-match "" t t nil 1))
  20269. ((not off) (org-move-to-column cc t) (insert ": ")))
  20270. (forward-line 1)))
  20271. (save-excursion
  20272. (org-back-to-heading)
  20273. (cond
  20274. ((looking-at (format org-heading-keyword-regexp-format
  20275. org-quote-string))
  20276. (goto-char (match-end 1))
  20277. (looking-at (concat " +" org-quote-string))
  20278. (replace-match "" t t)
  20279. (when (eolp) (insert " ")))
  20280. ((looking-at org-outline-regexp)
  20281. (goto-char (match-end 0))
  20282. (insert org-quote-string " ")))))))
  20283. (defun org-reftex-citation ()
  20284. "Use reftex-citation to insert a citation into the buffer.
  20285. This looks for a line like
  20286. #+BIBLIOGRAPHY: foo plain option:-d
  20287. and derives from it that foo.bib is the bibliography file relevant
  20288. for this document. It then installs the necessary environment for RefTeX
  20289. to work in this buffer and calls `reftex-citation' to insert a citation
  20290. into the buffer.
  20291. Export of such citations to both LaTeX and HTML is handled by the contributed
  20292. package org-exp-bibtex by Taru Karttunen."
  20293. (interactive)
  20294. (let ((reftex-docstruct-symbol 'rds)
  20295. (reftex-cite-format "\\cite{%l}")
  20296. rds bib)
  20297. (save-excursion
  20298. (save-restriction
  20299. (widen)
  20300. (let ((case-fold-search t)
  20301. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  20302. (if (not (save-excursion
  20303. (or (re-search-forward re nil t)
  20304. (re-search-backward re nil t))))
  20305. (error "No bibliography defined in file")
  20306. (setq bib (concat (match-string 1) ".bib")
  20307. rds (list (list 'bib bib)))))))
  20308. (call-interactively 'reftex-citation)))
  20309. ;;;; Functions extending outline functionality
  20310. (defun org-beginning-of-line (&optional arg)
  20311. "Go to the beginning of the current line. If that is invisible, continue
  20312. to a visible line beginning. This makes the function of C-a more intuitive.
  20313. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  20314. first attempt, and only move to after the tags when the cursor is already
  20315. beyond the end of the headline."
  20316. (interactive "P")
  20317. (let ((pos (point))
  20318. (special (if (consp org-special-ctrl-a/e)
  20319. (car org-special-ctrl-a/e)
  20320. org-special-ctrl-a/e))
  20321. refpos)
  20322. (if (org-bound-and-true-p visual-line-mode)
  20323. (beginning-of-visual-line 1)
  20324. (beginning-of-line 1))
  20325. (if (and arg (fboundp 'move-beginning-of-line))
  20326. (call-interactively 'move-beginning-of-line)
  20327. (if (bobp)
  20328. nil
  20329. (backward-char 1)
  20330. (if (org-truely-invisible-p)
  20331. (while (and (not (bobp)) (org-truely-invisible-p))
  20332. (backward-char 1)
  20333. (beginning-of-line 1))
  20334. (forward-char 1))))
  20335. (when special
  20336. (cond
  20337. ((and (looking-at org-complex-heading-regexp)
  20338. (= (char-after (match-end 1)) ?\ ))
  20339. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  20340. (point-at-eol)))
  20341. (goto-char
  20342. (if (eq special t)
  20343. (cond ((> pos refpos) refpos)
  20344. ((= pos (point)) refpos)
  20345. (t (point)))
  20346. (cond ((> pos (point)) (point))
  20347. ((not (eq last-command this-command)) (point))
  20348. (t refpos)))))
  20349. ((org-at-item-p)
  20350. ;; Being at an item and not looking at an the item means point
  20351. ;; was previously moved to beginning of a visual line, which
  20352. ;; doesn't contain the item. Therefore, do nothing special,
  20353. ;; just stay here.
  20354. (when (looking-at org-list-full-item-re)
  20355. ;; Set special position at first white space character after
  20356. ;; bullet, and check-box, if any.
  20357. (let ((after-bullet
  20358. (let ((box (match-end 3)))
  20359. (if (not box) (match-end 1)
  20360. (let ((after (char-after box)))
  20361. (if (and after (= after ? )) (1+ box) box))))))
  20362. ;; Special case: Move point to special position when
  20363. ;; currently after it or at beginning of line.
  20364. (if (eq special t)
  20365. (when (or (> pos after-bullet) (= (point) pos))
  20366. (goto-char after-bullet))
  20367. ;; Reversed case: Move point to special position when
  20368. ;; point was already at beginning of line and command is
  20369. ;; repeated.
  20370. (when (and (= (point) pos) (eq last-command this-command))
  20371. (goto-char after-bullet))))))))
  20372. (org-no-warnings
  20373. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20374. (defun org-end-of-line (&optional arg)
  20375. "Go to the end of the line.
  20376. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  20377. tags on the first attempt, and only move to after the tags when
  20378. the cursor is already beyond the end of the headline."
  20379. (interactive "P")
  20380. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  20381. org-special-ctrl-a/e))
  20382. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  20383. 'end-of-visual-line)
  20384. ((fboundp 'move-end-of-line) 'move-end-of-line)
  20385. (t 'end-of-line))))
  20386. (if (or (not special) arg) (call-interactively move-fun)
  20387. (let* ((element (save-excursion (beginning-of-line)
  20388. (org-element-at-point)))
  20389. (type (org-element-type element)))
  20390. (cond
  20391. ((memq type '(headline inlinetask))
  20392. (let ((pos (point)))
  20393. (beginning-of-line 1)
  20394. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  20395. (if (eq special t)
  20396. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  20397. (goto-char (match-beginning 1))
  20398. (goto-char (match-end 0)))
  20399. (if (or (< pos (match-end 0))
  20400. (not (eq this-command last-command)))
  20401. (goto-char (match-end 0))
  20402. (goto-char (match-beginning 1))))
  20403. (call-interactively move-fun))))
  20404. ((org-element-property :hiddenp element)
  20405. ;; If element is hidden, `move-end-of-line' would put point
  20406. ;; after it. Use `end-of-line' to stay on current line.
  20407. (call-interactively 'end-of-line))
  20408. (t (call-interactively move-fun)))))
  20409. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  20410. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  20411. (define-key org-mode-map "\C-e" 'org-end-of-line)
  20412. (defun org-backward-sentence (&optional arg)
  20413. "Go to beginning of sentence, or beginning of table field.
  20414. This will call `backward-sentence' or `org-table-beginning-of-field',
  20415. depending on context."
  20416. (interactive "P")
  20417. (cond
  20418. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  20419. (t (call-interactively 'backward-sentence))))
  20420. (defun org-forward-sentence (&optional arg)
  20421. "Go to end of sentence, or end of table field.
  20422. This will call `forward-sentence' or `org-table-end-of-field',
  20423. depending on context."
  20424. (interactive "P")
  20425. (cond
  20426. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  20427. (t (call-interactively 'forward-sentence))))
  20428. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  20429. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  20430. (defun org-kill-line (&optional arg)
  20431. "Kill line, to tags or end of line."
  20432. (interactive "P")
  20433. (cond
  20434. ((or (not org-special-ctrl-k)
  20435. (bolp)
  20436. (not (org-at-heading-p)))
  20437. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  20438. org-ctrl-k-protect-subtree)
  20439. (if (or (eq org-ctrl-k-protect-subtree 'error)
  20440. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  20441. (error "C-k aborted - would kill hidden subtree")))
  20442. (call-interactively
  20443. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  20444. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  20445. (kill-region (point) (match-beginning 1))
  20446. (org-set-tags nil t))
  20447. (t (kill-region (point) (point-at-eol)))))
  20448. (define-key org-mode-map "\C-k" 'org-kill-line)
  20449. (defun org-yank (&optional arg)
  20450. "Yank. If the kill is a subtree, treat it specially.
  20451. This command will look at the current kill and check if is a single
  20452. subtree, or a series of subtrees[1]. If it passes the test, and if the
  20453. cursor is at the beginning of a line or after the stars of a currently
  20454. empty headline, then the yank is handled specially. How exactly depends
  20455. on the value of the following variables, both set by default.
  20456. org-yank-folded-subtrees
  20457. When set, the subtree(s) will be folded after insertion, but only
  20458. if doing so would now swallow text after the yanked text.
  20459. org-yank-adjusted-subtrees
  20460. When set, the subtree will be promoted or demoted in order to
  20461. fit into the local outline tree structure, which means that the level
  20462. will be adjusted so that it becomes the smaller one of the two
  20463. *visible* surrounding headings.
  20464. Any prefix to this command will cause `yank' to be called directly with
  20465. no special treatment. In particular, a simple \\[universal-argument] prefix \
  20466. will just
  20467. plainly yank the text as it is.
  20468. \[1] The test checks if the first non-white line is a heading
  20469. and if there are no other headings with fewer stars."
  20470. (interactive "P")
  20471. (org-yank-generic 'yank arg))
  20472. (defun org-yank-generic (command arg)
  20473. "Perform some yank-like command.
  20474. This function implements the behavior described in the `org-yank'
  20475. documentation. However, it has been generalized to work for any
  20476. interactive command with similar behavior."
  20477. ;; pretend to be command COMMAND
  20478. (setq this-command command)
  20479. (if arg
  20480. (call-interactively command)
  20481. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  20482. (and (org-kill-is-subtree-p)
  20483. (or (bolp)
  20484. (and (looking-at "[ \t]*$")
  20485. (string-match
  20486. "\\`\\*+\\'"
  20487. (buffer-substring (point-at-bol) (point)))))))
  20488. swallowp)
  20489. (cond
  20490. ((and subtreep org-yank-folded-subtrees)
  20491. (let ((beg (point))
  20492. end)
  20493. (if (and subtreep org-yank-adjusted-subtrees)
  20494. (org-paste-subtree nil nil 'for-yank)
  20495. (call-interactively command))
  20496. (setq end (point))
  20497. (goto-char beg)
  20498. (when (and (bolp) subtreep
  20499. (not (setq swallowp
  20500. (org-yank-folding-would-swallow-text beg end))))
  20501. (org-with-limited-levels
  20502. (or (looking-at org-outline-regexp)
  20503. (re-search-forward org-outline-regexp-bol end t))
  20504. (while (and (< (point) end) (looking-at org-outline-regexp))
  20505. (hide-subtree)
  20506. (org-cycle-show-empty-lines 'folded)
  20507. (condition-case nil
  20508. (outline-forward-same-level 1)
  20509. (error (goto-char end))))))
  20510. (when swallowp
  20511. (message
  20512. "Inserted text not folded because that would swallow text"))
  20513. (goto-char end)
  20514. (skip-chars-forward " \t\n\r")
  20515. (beginning-of-line 1)
  20516. (push-mark beg 'nomsg)))
  20517. ((and subtreep org-yank-adjusted-subtrees)
  20518. (let ((beg (point-at-bol)))
  20519. (org-paste-subtree nil nil 'for-yank)
  20520. (push-mark beg 'nomsg)))
  20521. (t
  20522. (call-interactively command))))))
  20523. (defun org-yank-folding-would-swallow-text (beg end)
  20524. "Would hide-subtree at BEG swallow any text after END?"
  20525. (let (level)
  20526. (org-with-limited-levels
  20527. (save-excursion
  20528. (goto-char beg)
  20529. (when (or (looking-at org-outline-regexp)
  20530. (re-search-forward org-outline-regexp-bol end t))
  20531. (setq level (org-outline-level)))
  20532. (goto-char end)
  20533. (skip-chars-forward " \t\r\n\v\f")
  20534. (if (or (eobp)
  20535. (and (bolp) (looking-at org-outline-regexp)
  20536. (<= (org-outline-level) level)))
  20537. nil ; Nothing would be swallowed
  20538. t))))) ; something would swallow
  20539. (define-key org-mode-map "\C-y" 'org-yank)
  20540. (defun org-truely-invisible-p ()
  20541. "Check if point is at a character currently not visible.
  20542. This version does not only check the character property, but also
  20543. `visible-mode'."
  20544. ;; Early versions of noutline don't have `outline-invisible-p'.
  20545. (if (org-bound-and-true-p visible-mode)
  20546. nil
  20547. (outline-invisible-p)))
  20548. (defun org-invisible-p2 ()
  20549. "Check if point is at a character currently not visible."
  20550. (save-excursion
  20551. (if (and (eolp) (not (bobp))) (backward-char 1))
  20552. ;; Early versions of noutline don't have `outline-invisible-p'.
  20553. (outline-invisible-p)))
  20554. (defun org-back-to-heading (&optional invisible-ok)
  20555. "Call `outline-back-to-heading', but provide a better error message."
  20556. (condition-case nil
  20557. (outline-back-to-heading invisible-ok)
  20558. (error (error "Before first headline at position %d in buffer %s"
  20559. (point) (current-buffer)))))
  20560. (defun org-before-first-heading-p ()
  20561. "Before first heading?"
  20562. (save-excursion
  20563. (end-of-line)
  20564. (null (re-search-backward org-outline-regexp-bol nil t))))
  20565. (defun org-at-heading-p (&optional ignored)
  20566. (outline-on-heading-p t))
  20567. ;; Compatibility alias with Org versions < 7.8.03
  20568. (defalias 'org-on-heading-p 'org-at-heading-p)
  20569. (defun org-at-comment-p nil
  20570. "Is cursor in a line starting with a # character?"
  20571. (save-excursion
  20572. (beginning-of-line)
  20573. (looking-at "^#")))
  20574. (defun org-at-drawer-p nil
  20575. "Is cursor at a drawer keyword?"
  20576. (save-excursion
  20577. (move-beginning-of-line 1)
  20578. (looking-at org-drawer-regexp)))
  20579. (defun org-at-block-p nil
  20580. "Is cursor at a block keyword?"
  20581. (save-excursion
  20582. (move-beginning-of-line 1)
  20583. (looking-at org-block-regexp)))
  20584. (defun org-point-at-end-of-empty-headline ()
  20585. "If point is at the end of an empty headline, return t, else nil.
  20586. If the heading only contains a TODO keyword, it is still still considered
  20587. empty."
  20588. (and (looking-at "[ \t]*$")
  20589. (when org-todo-line-regexp
  20590. (save-excursion
  20591. (beginning-of-line 1)
  20592. (let ((case-fold-search nil))
  20593. (looking-at org-todo-line-regexp)
  20594. (string= (match-string 3) ""))))))
  20595. (defun org-at-heading-or-item-p ()
  20596. (or (org-at-heading-p) (org-at-item-p)))
  20597. (defun org-at-target-p ()
  20598. (or (org-in-regexp org-radio-target-regexp)
  20599. (org-in-regexp org-target-regexp)))
  20600. ;; Compatibility alias with Org versions < 7.8.03
  20601. (defalias 'org-on-target-p 'org-at-target-p)
  20602. (defun org-up-heading-all (arg)
  20603. "Move to the heading line of which the present line is a subheading.
  20604. This function considers both visible and invisible heading lines.
  20605. With argument, move up ARG levels."
  20606. (if (fboundp 'outline-up-heading-all)
  20607. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  20608. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  20609. (defun org-up-heading-safe ()
  20610. "Move to the heading line of which the present line is a subheading.
  20611. This version will not throw an error. It will return the level of the
  20612. headline found, or nil if no higher level is found.
  20613. Also, this function will be a lot faster than `outline-up-heading',
  20614. because it relies on stars being the outline starters. This can really
  20615. make a significant difference in outlines with very many siblings."
  20616. (let (start-level re)
  20617. (org-back-to-heading t)
  20618. (setq start-level (funcall outline-level))
  20619. (if (equal start-level 1)
  20620. nil
  20621. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  20622. (if (re-search-backward re nil t)
  20623. (funcall outline-level)))))
  20624. (defun org-first-sibling-p ()
  20625. "Is this heading the first child of its parents?"
  20626. (interactive)
  20627. (let ((re org-outline-regexp-bol)
  20628. level l)
  20629. (unless (org-at-heading-p t)
  20630. (error "Not at a heading"))
  20631. (setq level (funcall outline-level))
  20632. (save-excursion
  20633. (if (not (re-search-backward re nil t))
  20634. t
  20635. (setq l (funcall outline-level))
  20636. (< l level)))))
  20637. (defun org-goto-sibling (&optional previous)
  20638. "Goto the next sibling, even if it is invisible.
  20639. When PREVIOUS is set, go to the previous sibling instead. Returns t
  20640. when a sibling was found. When none is found, return nil and don't
  20641. move point."
  20642. (let ((fun (if previous 're-search-backward 're-search-forward))
  20643. (pos (point))
  20644. (re org-outline-regexp-bol)
  20645. level l)
  20646. (when (condition-case nil (org-back-to-heading t) (error nil))
  20647. (setq level (funcall outline-level))
  20648. (catch 'exit
  20649. (or previous (forward-char 1))
  20650. (while (funcall fun re nil t)
  20651. (setq l (funcall outline-level))
  20652. (when (< l level) (goto-char pos) (throw 'exit nil))
  20653. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  20654. (goto-char pos)
  20655. nil))))
  20656. (defun org-show-siblings ()
  20657. "Show all siblings of the current headline."
  20658. (save-excursion
  20659. (while (org-goto-sibling) (org-flag-heading nil)))
  20660. (save-excursion
  20661. (while (org-goto-sibling 'previous)
  20662. (org-flag-heading nil))))
  20663. (defun org-goto-first-child ()
  20664. "Goto the first child, even if it is invisible.
  20665. Return t when a child was found. Otherwise don't move point and
  20666. return nil."
  20667. (let (level (pos (point)) (re org-outline-regexp-bol))
  20668. (when (condition-case nil (org-back-to-heading t) (error nil))
  20669. (setq level (outline-level))
  20670. (forward-char 1)
  20671. (if (and (re-search-forward re nil t) (> (outline-level) level))
  20672. (progn (goto-char (match-beginning 0)) t)
  20673. (goto-char pos) nil))))
  20674. (defun org-show-hidden-entry ()
  20675. "Show an entry where even the heading is hidden."
  20676. (save-excursion
  20677. (org-show-entry)))
  20678. (defun org-flag-heading (flag &optional entry)
  20679. "Flag the current heading. FLAG non-nil means make invisible.
  20680. When ENTRY is non-nil, show the entire entry."
  20681. (save-excursion
  20682. (org-back-to-heading t)
  20683. ;; Check if we should show the entire entry
  20684. (if entry
  20685. (progn
  20686. (org-show-entry)
  20687. (save-excursion
  20688. (and (outline-next-heading)
  20689. (org-flag-heading nil))))
  20690. (outline-flag-region (max (point-min) (1- (point)))
  20691. (save-excursion (outline-end-of-heading) (point))
  20692. flag))))
  20693. (defun org-get-next-sibling ()
  20694. "Move to next heading of the same level, and return point.
  20695. If there is no such heading, return nil.
  20696. This is like outline-next-sibling, but invisible headings are ok."
  20697. (let ((level (funcall outline-level)))
  20698. (outline-next-heading)
  20699. (while (and (not (eobp)) (> (funcall outline-level) level))
  20700. (outline-next-heading))
  20701. (if (or (eobp) (< (funcall outline-level) level))
  20702. nil
  20703. (point))))
  20704. (defun org-get-last-sibling ()
  20705. "Move to previous heading of the same level, and return point.
  20706. If there is no such heading, return nil."
  20707. (let ((opoint (point))
  20708. (level (funcall outline-level)))
  20709. (outline-previous-heading)
  20710. (when (and (/= (point) opoint) (outline-on-heading-p t))
  20711. (while (and (> (funcall outline-level) level)
  20712. (not (bobp)))
  20713. (outline-previous-heading))
  20714. (if (< (funcall outline-level) level)
  20715. nil
  20716. (point)))))
  20717. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  20718. "Goto to the end of a subtree."
  20719. ;; This contains an exact copy of the original function, but it uses
  20720. ;; `org-back-to-heading', to make it work also in invisible
  20721. ;; trees. And is uses an invisible-ok argument.
  20722. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  20723. ;; Furthermore, when used inside Org, finding the end of a large subtree
  20724. ;; with many children and grandchildren etc, this can be much faster
  20725. ;; than the outline version.
  20726. (org-back-to-heading invisible-ok)
  20727. (let ((first t)
  20728. (level (funcall outline-level)))
  20729. (if (and (derived-mode-p 'org-mode) (< level 1000))
  20730. ;; A true heading (not a plain list item), in Org-mode
  20731. ;; This means we can easily find the end by looking
  20732. ;; only for the right number of stars. Using a regexp to do
  20733. ;; this is so much faster than using a Lisp loop.
  20734. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  20735. (forward-char 1)
  20736. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  20737. ;; something else, do it the slow way
  20738. (while (and (not (eobp))
  20739. (or first (> (funcall outline-level) level)))
  20740. (setq first nil)
  20741. (outline-next-heading)))
  20742. (unless to-heading
  20743. (if (memq (preceding-char) '(?\n ?\^M))
  20744. (progn
  20745. ;; Go to end of line before heading
  20746. (forward-char -1)
  20747. (if (memq (preceding-char) '(?\n ?\^M))
  20748. ;; leave blank line before heading
  20749. (forward-char -1))))))
  20750. (point))
  20751. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  20752. "Use Org version in org-mode, for dramatic speed-up."
  20753. (if (derived-mode-p 'org-mode)
  20754. (progn
  20755. (org-end-of-subtree nil t)
  20756. (unless (eobp) (backward-char 1)))
  20757. ad-do-it))
  20758. (defun org-end-of-meta-data-and-drawers ()
  20759. "Jump to the first text after meta data and drawers in the current entry.
  20760. This will move over empty lines, lines with planning time stamps,
  20761. clocking lines, and drawers."
  20762. (org-back-to-heading t)
  20763. (let ((end (save-excursion (outline-next-heading) (point)))
  20764. (re (concat "\\(" org-drawer-regexp "\\)"
  20765. "\\|" "[ \t]*" org-keyword-time-regexp)))
  20766. (forward-line 1)
  20767. (while (re-search-forward re end t)
  20768. (if (not (match-end 1))
  20769. ;; empty or planning line
  20770. (forward-line 1)
  20771. ;; a drawer, find the end
  20772. (re-search-forward "^[ \t]*:END:" end 'move)
  20773. (forward-line 1)))
  20774. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  20775. (point)))
  20776. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  20777. "Move forward to the arg'th subheading at same level as this one.
  20778. Stop at the first and last subheadings of a superior heading.
  20779. Normally this only looks at visible headings, but when INVISIBLE-OK is
  20780. non-nil it will also look at invisible ones."
  20781. (interactive "p")
  20782. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  20783. (outline-next-heading)
  20784. (org-at-heading-p)
  20785. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20786. (re (format "^\\*\\{1,%d\\} " level))
  20787. l)
  20788. (forward-char 1)
  20789. (while (> arg 0)
  20790. (while (and (re-search-forward re nil 'move)
  20791. (setq l (- (match-end 0) (match-beginning 0) 1))
  20792. (= l level)
  20793. (not invisible-ok)
  20794. (progn (backward-char 1) (outline-invisible-p)))
  20795. (if (< l level) (setq arg 1)))
  20796. (setq arg (1- arg)))
  20797. (beginning-of-line 1))))
  20798. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  20799. "Move backward to the arg'th subheading at same level as this one.
  20800. Stop at the first and last subheadings of a superior heading."
  20801. (interactive "p")
  20802. (if (not (ignore-errors (org-back-to-heading)))
  20803. (goto-char (point-min))
  20804. (org-at-heading-p)
  20805. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  20806. (re (format "^\\*\\{1,%d\\} " level))
  20807. l)
  20808. (while (> arg 0)
  20809. (while (and (re-search-backward re nil 'move)
  20810. (setq l (- (match-end 0) (match-beginning 0) 1))
  20811. (= l level)
  20812. (not invisible-ok)
  20813. (outline-invisible-p))
  20814. (if (< l level) (setq arg 1)))
  20815. (setq arg (1- arg))))))
  20816. (defun org-forward-element ()
  20817. "Move forward by one element.
  20818. Move to the next element at the same level, when possible."
  20819. (interactive)
  20820. (cond ((eobp) (error "Cannot move further down"))
  20821. ((org-with-limited-levels (org-at-heading-p))
  20822. (let ((origin (point)))
  20823. (org-forward-heading-same-level 1)
  20824. (unless (org-with-limited-levels (org-at-heading-p))
  20825. (goto-char origin)
  20826. (error "Cannot move further down"))))
  20827. (t
  20828. (let* ((elem (org-element-at-point))
  20829. (end (org-element-property :end elem))
  20830. (parent (org-element-property :parent elem)))
  20831. (if (and parent (= (org-element-property :contents-end parent) end))
  20832. (goto-char (org-element-property :end parent))
  20833. (goto-char end))))))
  20834. (defun org-backward-element ()
  20835. "Move backward by one element.
  20836. Move to the previous element at the same level, when possible."
  20837. (interactive)
  20838. (cond ((bobp) (error "Cannot move further up"))
  20839. ((org-with-limited-levels (org-at-heading-p))
  20840. ;; At an headline, move to the previous one, if any, or stay
  20841. ;; here.
  20842. (let ((origin (point)))
  20843. (org-backward-heading-same-level 1)
  20844. (unless (org-with-limited-levels (org-at-heading-p))
  20845. (goto-char origin)
  20846. (error "Cannot move further up"))))
  20847. (t
  20848. (let* ((trail (org-element-at-point 'keep-trail))
  20849. (elem (car trail))
  20850. (prev-elem (nth 1 trail))
  20851. (beg (org-element-property :begin elem)))
  20852. (cond
  20853. ;; Move to beginning of current element if point isn't
  20854. ;; there already.
  20855. ((/= (point) beg) (goto-char beg))
  20856. (prev-elem (goto-char (org-element-property :begin prev-elem)))
  20857. ((org-before-first-heading-p) (goto-char (point-min)))
  20858. (t (org-back-to-heading)))))))
  20859. (defun org-up-element ()
  20860. "Move to upper element."
  20861. (interactive)
  20862. (if (org-with-limited-levels (org-at-heading-p))
  20863. (unless (org-up-heading-safe) (error "No surrounding element"))
  20864. (let* ((elem (org-element-at-point))
  20865. (parent (org-element-property :parent elem)))
  20866. (if parent (goto-char (org-element-property :begin parent))
  20867. (if (org-with-limited-levels (org-before-first-heading-p))
  20868. (error "No surrounding element")
  20869. (org-with-limited-levels (org-back-to-heading)))))))
  20870. (defvar org-element-greater-elements)
  20871. (defun org-down-element ()
  20872. "Move to inner element."
  20873. (interactive)
  20874. (let ((element (org-element-at-point)))
  20875. (cond
  20876. ((memq (org-element-type element) '(plain-list table))
  20877. (goto-char (org-element-property :contents-begin element))
  20878. (forward-char))
  20879. ((memq (org-element-type element) org-element-greater-elements)
  20880. ;; If contents are hidden, first disclose them.
  20881. (when (org-element-property :hiddenp element) (org-cycle))
  20882. (goto-char (or (org-element-property :contents-begin element)
  20883. (error "No content for this element"))))
  20884. (t (error "No inner element")))))
  20885. (defun org-drag-element-backward ()
  20886. "Move backward element at point."
  20887. (interactive)
  20888. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  20889. (let* ((trail (org-element-at-point 'keep-trail))
  20890. (elem (car trail))
  20891. (prev-elem (nth 1 trail)))
  20892. ;; Error out if no previous element or previous element is
  20893. ;; a parent of the current one.
  20894. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  20895. (error "Cannot drag element backward")
  20896. (let ((pos (point)))
  20897. (org-element-swap-A-B prev-elem elem)
  20898. (goto-char (+ (org-element-property :begin prev-elem)
  20899. (- pos (org-element-property :begin elem)))))))))
  20900. (defun org-drag-element-forward ()
  20901. "Move forward element at point."
  20902. (interactive)
  20903. (let* ((pos (point))
  20904. (elem (org-element-at-point)))
  20905. (when (= (point-max) (org-element-property :end elem))
  20906. (error "Cannot drag element forward"))
  20907. (goto-char (org-element-property :end elem))
  20908. (let ((next-elem (org-element-at-point)))
  20909. (when (or (org-element-nested-p elem next-elem)
  20910. (and (eq (org-element-type next-elem) 'headline)
  20911. (not (eq (org-element-type elem) 'headline))))
  20912. (goto-char pos)
  20913. (error "Cannot drag element forward"))
  20914. ;; Compute new position of point: it's shifted by NEXT-ELEM
  20915. ;; body's length (without final blanks) and by the length of
  20916. ;; blanks between ELEM and NEXT-ELEM.
  20917. (let ((size-next (- (save-excursion
  20918. (goto-char (org-element-property :end next-elem))
  20919. (skip-chars-backward " \r\t\n")
  20920. (forward-line)
  20921. ;; Small correction if buffer doesn't end
  20922. ;; with a newline character.
  20923. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  20924. (org-element-property :begin next-elem)))
  20925. (size-blank (- (org-element-property :end elem)
  20926. (save-excursion
  20927. (goto-char (org-element-property :end elem))
  20928. (skip-chars-backward " \r\t\n")
  20929. (forward-line)
  20930. (point)))))
  20931. (org-element-swap-A-B elem next-elem)
  20932. (goto-char (+ pos size-next size-blank))))))
  20933. (defun org-mark-element ()
  20934. "Put point at beginning of this element, mark at end.
  20935. Interactively, if this command is repeated or (in Transient Mark
  20936. mode) if the mark is active, it marks the next element after the
  20937. ones already marked."
  20938. (interactive)
  20939. (let (deactivate-mark)
  20940. (if (and (org-called-interactively-p 'any)
  20941. (or (and (eq last-command this-command) (mark t))
  20942. (and transient-mark-mode mark-active)))
  20943. (set-mark
  20944. (save-excursion
  20945. (goto-char (mark))
  20946. (goto-char (org-element-property :end (org-element-at-point)))))
  20947. (let ((element (org-element-at-point)))
  20948. (end-of-line)
  20949. (push-mark (org-element-property :end element) t t)
  20950. (goto-char (org-element-property :begin element))))))
  20951. (defun org-narrow-to-element ()
  20952. "Narrow buffer to current element."
  20953. (interactive)
  20954. (let ((elem (org-element-at-point)))
  20955. (cond
  20956. ((eq (car elem) 'headline)
  20957. (narrow-to-region
  20958. (org-element-property :begin elem)
  20959. (org-element-property :end elem)))
  20960. ((memq (car elem) org-element-greater-elements)
  20961. (narrow-to-region
  20962. (org-element-property :contents-begin elem)
  20963. (org-element-property :contents-end elem)))
  20964. (t
  20965. (narrow-to-region
  20966. (org-element-property :begin elem)
  20967. (org-element-property :end elem))))))
  20968. (defun org-transpose-words ()
  20969. "Transpose words, using `org-mode' syntax table."
  20970. (interactive)
  20971. (with-syntax-table org-syntax-table
  20972. (call-interactively 'transpose-words)))
  20973. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  20974. (defun org-transpose-element ()
  20975. "Transpose current and previous elements, keeping blank lines between.
  20976. Point is moved after both elements."
  20977. (interactive)
  20978. (org-skip-whitespace)
  20979. (let ((end (org-element-property :end (org-element-at-point))))
  20980. (org-drag-element-backward)
  20981. (goto-char end)))
  20982. (defun org-unindent-buffer ()
  20983. "Un-indent the visible part of the buffer.
  20984. Relative indentation (between items, inside blocks, etc.) isn't
  20985. modified."
  20986. (interactive)
  20987. (unless (eq major-mode 'org-mode)
  20988. (error "Cannot un-indent a buffer not in Org mode"))
  20989. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  20990. unindent-tree ; For byte-compiler.
  20991. (unindent-tree
  20992. (function
  20993. (lambda (contents)
  20994. (mapc
  20995. (lambda (element)
  20996. (if (memq (org-element-type element) '(headline section))
  20997. (funcall unindent-tree (org-element-contents element))
  20998. (save-excursion
  20999. (save-restriction
  21000. (narrow-to-region
  21001. (org-element-property :begin element)
  21002. (org-element-property :end element))
  21003. (org-do-remove-indentation)))))
  21004. (reverse contents))))))
  21005. (funcall unindent-tree (org-element-contents parse-tree))))
  21006. (defun org-show-subtree ()
  21007. "Show everything after this heading at deeper levels."
  21008. (interactive)
  21009. (outline-flag-region
  21010. (point)
  21011. (save-excursion
  21012. (org-end-of-subtree t t))
  21013. nil))
  21014. (defun org-show-entry ()
  21015. "Show the body directly following this heading.
  21016. Show the heading too, if it is currently invisible."
  21017. (interactive)
  21018. (save-excursion
  21019. (condition-case nil
  21020. (progn
  21021. (org-back-to-heading t)
  21022. (outline-flag-region
  21023. (max (point-min) (1- (point)))
  21024. (save-excursion
  21025. (if (re-search-forward
  21026. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  21027. (match-beginning 1)
  21028. (point-max)))
  21029. nil)
  21030. (org-cycle-hide-drawers 'children))
  21031. (error nil))))
  21032. (defun org-make-options-regexp (kwds &optional extra)
  21033. "Make a regular expression for keyword lines."
  21034. (concat
  21035. "^#\\+\\("
  21036. (mapconcat 'regexp-quote kwds "\\|")
  21037. (if extra (concat "\\|" extra))
  21038. "\\):[ \t]*\\(.*\\)"))
  21039. ;; Make isearch reveal the necessary context
  21040. (defun org-isearch-end ()
  21041. "Reveal context after isearch exits."
  21042. (when isearch-success ; only if search was successful
  21043. (if (featurep 'xemacs)
  21044. ;; Under XEmacs, the hook is run in the correct place,
  21045. ;; we directly show the context.
  21046. (org-show-context 'isearch)
  21047. ;; In Emacs the hook runs *before* restoring the overlays.
  21048. ;; So we have to use a one-time post-command-hook to do this.
  21049. ;; (Emacs 22 has a special variable, see function `org-mode')
  21050. (unless (and (boundp 'isearch-mode-end-hook-quit)
  21051. isearch-mode-end-hook-quit)
  21052. ;; Only when the isearch was not quitted.
  21053. (org-add-hook 'post-command-hook 'org-isearch-post-command
  21054. 'append 'local)))))
  21055. (defun org-isearch-post-command ()
  21056. "Remove self from hook, and show context."
  21057. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  21058. (org-show-context 'isearch))
  21059. ;;;; Integration with and fixes for other packages
  21060. ;;; Imenu support
  21061. (defvar org-imenu-markers nil
  21062. "All markers currently used by Imenu.")
  21063. (make-variable-buffer-local 'org-imenu-markers)
  21064. (defun org-imenu-new-marker (&optional pos)
  21065. "Return a new marker for use by Imenu, and remember the marker."
  21066. (let ((m (make-marker)))
  21067. (move-marker m (or pos (point)))
  21068. (push m org-imenu-markers)
  21069. m))
  21070. (defun org-imenu-get-tree ()
  21071. "Produce the index for Imenu."
  21072. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  21073. (setq org-imenu-markers nil)
  21074. (let* ((n org-imenu-depth)
  21075. (re (concat "^" (org-get-limited-outline-regexp)))
  21076. (subs (make-vector (1+ n) nil))
  21077. (last-level 0)
  21078. m level head)
  21079. (save-excursion
  21080. (save-restriction
  21081. (widen)
  21082. (goto-char (point-max))
  21083. (while (re-search-backward re nil t)
  21084. (setq level (org-reduced-level (funcall outline-level)))
  21085. (when (and (<= level n)
  21086. (looking-at org-complex-heading-regexp))
  21087. (setq head (org-link-display-format
  21088. (org-match-string-no-properties 4))
  21089. m (org-imenu-new-marker))
  21090. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  21091. (if (>= level last-level)
  21092. (push (cons head m) (aref subs level))
  21093. (push (cons head (aref subs (1+ level))) (aref subs level))
  21094. (loop for i from (1+ level) to n do (aset subs i nil)))
  21095. (setq last-level level)))))
  21096. (aref subs 1)))
  21097. (eval-after-load "imenu"
  21098. '(progn
  21099. (add-hook 'imenu-after-jump-hook
  21100. (lambda ()
  21101. (if (derived-mode-p 'org-mode)
  21102. (org-show-context 'org-goto))))))
  21103. (defun org-link-display-format (link)
  21104. "Replace a link with either the description, or the link target
  21105. if no description is present"
  21106. (save-match-data
  21107. (if (string-match org-bracket-link-analytic-regexp link)
  21108. (replace-match (if (match-end 5)
  21109. (match-string 5 link)
  21110. (concat (match-string 1 link)
  21111. (match-string 3 link)))
  21112. nil t link)
  21113. link)))
  21114. (defun org-toggle-link-display ()
  21115. "Toggle the literal or descriptive display of links."
  21116. (interactive)
  21117. (if org-descriptive-links
  21118. (progn (org-remove-from-invisibility-spec '(org-link))
  21119. (org-restart-font-lock)
  21120. (setq org-descriptive-links nil))
  21121. (progn (add-to-invisibility-spec '(org-link))
  21122. (org-restart-font-lock)
  21123. (setq org-descriptive-links t))))
  21124. ;; Speedbar support
  21125. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  21126. "Overlay marking the agenda restriction line in speedbar.")
  21127. (overlay-put org-speedbar-restriction-lock-overlay
  21128. 'face 'org-agenda-restriction-lock)
  21129. (overlay-put org-speedbar-restriction-lock-overlay
  21130. 'help-echo "Agendas are currently limited to this item.")
  21131. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  21132. (defun org-speedbar-set-agenda-restriction ()
  21133. "Restrict future agenda commands to the location at point in speedbar.
  21134. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  21135. (interactive)
  21136. (require 'org-agenda)
  21137. (let (p m tp np dir txt)
  21138. (cond
  21139. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21140. 'org-imenu t))
  21141. (setq m (get-text-property p 'org-imenu-marker))
  21142. (with-current-buffer (marker-buffer m)
  21143. (goto-char m)
  21144. (org-agenda-set-restriction-lock 'subtree)))
  21145. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  21146. 'speedbar-function 'speedbar-find-file))
  21147. (setq tp (previous-single-property-change
  21148. (1+ p) 'speedbar-function)
  21149. np (next-single-property-change
  21150. tp 'speedbar-function)
  21151. dir (speedbar-line-directory)
  21152. txt (buffer-substring-no-properties (or tp (point-min))
  21153. (or np (point-max))))
  21154. (with-current-buffer (find-file-noselect
  21155. (let ((default-directory dir))
  21156. (expand-file-name txt)))
  21157. (unless (derived-mode-p 'org-mode)
  21158. (error "Cannot restrict to non-Org-mode file"))
  21159. (org-agenda-set-restriction-lock 'file)))
  21160. (t (error "Don't know how to restrict Org-mode's agenda")))
  21161. (move-overlay org-speedbar-restriction-lock-overlay
  21162. (point-at-bol) (point-at-eol))
  21163. (setq current-prefix-arg nil)
  21164. (org-agenda-maybe-redo)))
  21165. (eval-after-load "speedbar"
  21166. '(progn
  21167. (speedbar-add-supported-extension ".org")
  21168. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  21169. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  21170. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  21171. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  21172. (add-hook 'speedbar-visiting-tag-hook
  21173. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  21174. ;;; Fixes and Hacks for problems with other packages
  21175. ;; Make flyspell not check words in links, to not mess up our keymap
  21176. (defvar org-element-affiliated-keywords) ; From org-element.el
  21177. (defvar org-element-block-name-alist) ; From org-element.el
  21178. (defun org-mode-flyspell-verify ()
  21179. "Don't let flyspell put overlays at active buttons, or on
  21180. {todo,all-time,additional-option-like}-keywords."
  21181. (let ((pos (max (1- (point)) (point-min)))
  21182. (word (thing-at-point 'word)))
  21183. (and (not (get-text-property pos 'keymap))
  21184. (not (get-text-property pos 'org-no-flyspell))
  21185. (not (member word org-todo-keywords-1))
  21186. (not (member word org-all-time-keywords))
  21187. (not (member word org-options-keywords))
  21188. (not (member word (mapcar 'car org-startup-options)))
  21189. (not (member-ignore-case word org-element-affiliated-keywords))
  21190. (not (member-ignore-case word (org-get-export-keywords)))
  21191. (not (member-ignore-case
  21192. word (mapcar 'car org-element-block-name-alist)))
  21193. (not (member-ignore-case word '("BEGIN" "END" "ATTR"))))))
  21194. (defun org-remove-flyspell-overlays-in (beg end)
  21195. "Remove flyspell overlays in region."
  21196. (and (org-bound-and-true-p flyspell-mode)
  21197. (fboundp 'flyspell-delete-region-overlays)
  21198. (flyspell-delete-region-overlays beg end))
  21199. (add-text-properties beg end '(org-no-flyspell t)))
  21200. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  21201. (eval-after-load "bookmark"
  21202. '(if (boundp 'bookmark-after-jump-hook)
  21203. ;; We can use the hook
  21204. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  21205. ;; Hook not available, use advice
  21206. (defadvice bookmark-jump (after org-make-visible activate)
  21207. "Make the position visible."
  21208. (org-bookmark-jump-unhide))))
  21209. ;; Make sure saveplace shows the location if it was hidden
  21210. (eval-after-load "saveplace"
  21211. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  21212. "Make the position visible."
  21213. (org-bookmark-jump-unhide)))
  21214. ;; Make sure ecb shows the location if it was hidden
  21215. (eval-after-load "ecb"
  21216. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  21217. "Make hierarchy visible when jumping into location from ECB tree buffer."
  21218. (if (derived-mode-p 'org-mode)
  21219. (org-show-context))))
  21220. (defun org-bookmark-jump-unhide ()
  21221. "Unhide the current position, to show the bookmark location."
  21222. (and (derived-mode-p 'org-mode)
  21223. (or (outline-invisible-p)
  21224. (save-excursion (goto-char (max (point-min) (1- (point))))
  21225. (outline-invisible-p)))
  21226. (org-show-context 'bookmark-jump)))
  21227. ;; Make session.el ignore our circular variable
  21228. (eval-after-load "session"
  21229. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  21230. ;;;; Experimental code
  21231. (defun org-closed-in-range ()
  21232. "Sparse tree of items closed in a certain time range.
  21233. Still experimental, may disappear in the future."
  21234. (interactive)
  21235. ;; Get the time interval from the user.
  21236. (let* ((time1 (org-float-time
  21237. (org-read-date nil 'to-time nil "Starting date: ")))
  21238. (time2 (org-float-time
  21239. (org-read-date nil 'to-time nil "End date:")))
  21240. ;; callback function
  21241. (callback (lambda ()
  21242. (let ((time
  21243. (org-float-time
  21244. (apply 'encode-time
  21245. (org-parse-time-string
  21246. (match-string 1))))))
  21247. ;; check if time in interval
  21248. (and (>= time time1) (<= time time2))))))
  21249. ;; make tree, check each match with the callback
  21250. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  21251. ;;;; Finish up
  21252. (provide 'org)
  21253. (run-hooks 'org-load-hook)
  21254. ;;; org.el ends here