org.el 821 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2012 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. ;; `org-outline-regexp' ought to be a defconst but is let-binding in
  74. ;; some places -- e.g. see the macro org-with-limited-levels.
  75. ;;
  76. ;; In Org buffers, the value of `outline-regexp' is that of
  77. ;; `org-outline-regexp'. The only function still directly relying on
  78. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  79. ;; job when `orgstruct-mode' is active.
  80. (defvar org-outline-regexp "\\*+ "
  81. "Regexp to match Org headlines.")
  82. (defconst org-outline-regexp-bol "^\\*+ "
  83. "Regexp to match Org headlines.
  84. This is similar to `org-outline-regexp' but additionally makes
  85. sure that we are at the beginning of the line.")
  86. (defconst org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  87. "Matches an headline, putting stars and text into groups.
  88. Stars are put in group 1 and the trimmed body in group 2.")
  89. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  90. (when (fboundp 'defvaralias)
  91. (unless (boundp 'calendar-view-holidays-initially-flag)
  92. (defvaralias 'calendar-view-holidays-initially-flag
  93. 'view-calendar-holidays-initially))
  94. (unless (boundp 'calendar-view-diary-initially-flag)
  95. (defvaralias 'calendar-view-diary-initially-flag
  96. 'view-diary-entries-initially))
  97. (unless (boundp 'diary-fancy-buffer)
  98. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  99. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  100. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  101. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  102. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  103. (declare-function org-at-clock-log-p "org-clock" ())
  104. (declare-function org-clock-timestamps-up "org-clock" ())
  105. (declare-function org-clock-timestamps-down "org-clock" ())
  106. ;; load languages based on value of `org-babel-load-languages'
  107. (defvar org-babel-load-languages)
  108. ;;;###autoload
  109. (defun org-babel-do-load-languages (sym value)
  110. "Load the languages defined in `org-babel-load-languages'."
  111. (set-default sym value)
  112. (mapc (lambda (pair)
  113. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  114. (if active
  115. (progn
  116. (require (intern (concat "ob-" lang))))
  117. (progn
  118. (funcall 'fmakunbound
  119. (intern (concat "org-babel-execute:" lang)))
  120. (funcall 'fmakunbound
  121. (intern (concat "org-babel-expand-body:" lang)))))))
  122. org-babel-load-languages))
  123. (defcustom org-babel-load-languages '((emacs-lisp . t))
  124. "Languages which can be evaluated in Org-mode buffers.
  125. This list can be used to load support for any of the languages
  126. below, note that each language will depend on a different set of
  127. system executables and/or Emacs modes. When a language is
  128. \"loaded\", then code blocks in that language can be evaluated
  129. with `org-babel-execute-src-block' bound by default to C-c
  130. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  131. be set to remove code block evaluation from the C-c C-c
  132. keybinding. By default only Emacs Lisp (which has no
  133. requirements) is loaded."
  134. :group 'org-babel
  135. :set 'org-babel-do-load-languages
  136. :version "24.1"
  137. :type '(alist :tag "Babel Languages"
  138. :key-type
  139. (choice
  140. (const :tag "Awk" awk)
  141. (const :tag "C" C)
  142. (const :tag "R" R)
  143. (const :tag "Asymptote" asymptote)
  144. (const :tag "Calc" calc)
  145. (const :tag "Clojure" clojure)
  146. (const :tag "CSS" css)
  147. (const :tag "Ditaa" ditaa)
  148. (const :tag "Dot" dot)
  149. (const :tag "Emacs Lisp" emacs-lisp)
  150. (const :tag "Fortran" fortran)
  151. (const :tag "Gnuplot" gnuplot)
  152. (const :tag "Haskell" haskell)
  153. (const :tag "IO" io)
  154. (const :tag "Java" java)
  155. (const :tag "Javascript" js)
  156. (const :tag "LaTeX" latex)
  157. (const :tag "Ledger" ledger)
  158. (const :tag "Lilypond" lilypond)
  159. (const :tag "Maxima" maxima)
  160. (const :tag "Matlab" matlab)
  161. (const :tag "Mscgen" mscgen)
  162. (const :tag "Ocaml" ocaml)
  163. (const :tag "Octave" octave)
  164. (const :tag "Org" org)
  165. (const :tag "Perl" perl)
  166. (const :tag "Pico Lisp" picolisp)
  167. (const :tag "PlantUML" plantuml)
  168. (const :tag "Python" python)
  169. (const :tag "Ruby" ruby)
  170. (const :tag "Sass" sass)
  171. (const :tag "Scala" scala)
  172. (const :tag "Scheme" scheme)
  173. (const :tag "Screen" screen)
  174. (const :tag "Shell Script" sh)
  175. (const :tag "Shen" shen)
  176. (const :tag "Sql" sql)
  177. (const :tag "Sqlite" sqlite))
  178. :value-type (boolean :tag "Activate" :value t)))
  179. ;;;; Customization variables
  180. (defcustom org-clone-delete-id nil
  181. "Remove ID property of clones of a subtree.
  182. When non-nil, clones of a subtree don't inherit the ID property.
  183. Otherwise they inherit the ID property with a new unique
  184. identifier."
  185. :type 'boolean
  186. :version "24.1"
  187. :group 'org-id)
  188. ;;; Version
  189. (org-check-version)
  190. ;;;###autoload
  191. (defun org-version (&optional here full message)
  192. "Show the org-mode version in the echo area.
  193. With prefix arg HERE, insert it at point."
  194. (interactive "P")
  195. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  196. (org-install-dir (ignore-errors (org-find-library-dir "org-install.el")))
  197. (org-trash (or
  198. (and (fboundp 'org-release) (fboundp 'org-git-version))
  199. (load (concat org-dir "org-version.el")
  200. 'noerror 'nomessage 'nosuffix)))
  201. (org-version (org-release))
  202. (git-version (org-git-version))
  203. (version (format "Org-mode version %s (%s @ %s)"
  204. org-version
  205. git-version
  206. (if org-install-dir
  207. (if (string= org-dir org-install-dir)
  208. org-install-dir
  209. (concat "mixed installation! " org-install-dir " and " org-dir))
  210. "org-install.el can not be found!")))
  211. (_version (if full version org-version)))
  212. (if (org-called-interactively-p 'interactive)
  213. (if here
  214. (insert version)
  215. (message version))
  216. (if message (message _version))
  217. _version)))
  218. ;;; Compatibility constants
  219. ;;; The custom variables
  220. (defgroup org nil
  221. "Outline-based notes management and organizer."
  222. :tag "Org"
  223. :group 'outlines
  224. :group 'calendar)
  225. (defcustom org-mode-hook nil
  226. "Mode hook for Org-mode, run after the mode was turned on."
  227. :group 'org
  228. :type 'hook)
  229. (defcustom org-load-hook nil
  230. "Hook that is run after org.el has been loaded."
  231. :group 'org
  232. :type 'hook)
  233. (defcustom org-log-buffer-setup-hook nil
  234. "Hook that is run after an Org log buffer is created."
  235. :group 'org
  236. :version "24.1"
  237. :type 'hook)
  238. (defvar org-modules) ; defined below
  239. (defvar org-modules-loaded nil
  240. "Have the modules been loaded already?")
  241. (defun org-load-modules-maybe (&optional force)
  242. "Load all extensions listed in `org-modules'."
  243. (when (or force (not org-modules-loaded))
  244. (mapc (lambda (ext)
  245. (condition-case nil (require ext)
  246. (error (message "Problems while trying to load feature `%s'" ext))))
  247. org-modules)
  248. (setq org-modules-loaded t)))
  249. (defun org-set-modules (var value)
  250. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  251. (set var value)
  252. (when (featurep 'org)
  253. (org-load-modules-maybe 'force)))
  254. (when (org-bound-and-true-p org-modules)
  255. (let ((a (member 'org-infojs org-modules)))
  256. (and a (setcar a 'org-jsinfo))))
  257. (defcustom org-modules '(org-bbdb org-bibtex org-docview org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  258. "Modules that should always be loaded together with org.el.
  259. If a description starts with <C>, the file is not part of Emacs
  260. and loading it will require that you have downloaded and properly installed
  261. the org-mode distribution.
  262. You can also use this system to load external packages (i.e. neither Org
  263. core modules, nor modules from the CONTRIB directory). Just add symbols
  264. to the end of the list. If the package is called org-xyz.el, then you need
  265. to add the symbol `xyz', and the package must have a call to
  266. (provide 'org-xyz)"
  267. :group 'org
  268. :set 'org-set-modules
  269. :type
  270. '(set :greedy t
  271. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  272. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  273. (const :tag " crypt: Encryption of subtrees" org-crypt)
  274. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  275. (const :tag " docview: Links to doc-view buffers" org-docview)
  276. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  277. (const :tag " id: Global IDs for identifying entries" org-id)
  278. (const :tag " info: Links to Info nodes" org-info)
  279. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  280. (const :tag " habit: Track your consistency with habits" org-habit)
  281. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  282. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  283. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  284. (const :tag " mew Links to Mew folders/messages" org-mew)
  285. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  286. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  287. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  288. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  289. (const :tag " vm: Links to VM folders/messages" org-vm)
  290. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  291. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  292. (const :tag " mouse: Additional mouse support" org-mouse)
  293. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  294. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  295. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  296. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  297. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  298. (const :tag "C collector: Collect properties into tables" org-collector)
  299. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  300. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  301. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  302. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  303. (const :tag "C eval: Include command output as text" org-eval)
  304. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  305. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  306. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  307. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  308. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  309. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  310. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  311. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  312. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  313. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  314. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  315. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  316. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  317. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  318. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  319. (const :tag "C registry: A registry for Org-mode links" org-registry)
  320. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  321. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  322. (const :tag "C secretary: Team management with org-mode" org-secretary)
  323. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  324. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  325. (const :tag "C track: Keep up with Org-mode development" org-track)
  326. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  327. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  328. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  329. (defcustom org-support-shift-select nil
  330. "Non-nil means make shift-cursor commands select text when possible.
  331. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  332. start selecting a region, or enlarge regions started in this way.
  333. In Org-mode, in special contexts, these same keys are used for
  334. other purposes, important enough to compete with shift selection.
  335. Org tries to balance these needs by supporting `shift-select-mode'
  336. outside these special contexts, under control of this variable.
  337. The default of this variable is nil, to avoid confusing behavior. Shifted
  338. cursor keys will then execute Org commands in the following contexts:
  339. - on a headline, changing TODO state (left/right) and priority (up/down)
  340. - on a time stamp, changing the time
  341. - in a plain list item, changing the bullet type
  342. - in a property definition line, switching between allowed values
  343. - in the BEGIN line of a clock table (changing the time block).
  344. Outside these contexts, the commands will throw an error.
  345. When this variable is t and the cursor is not in a special
  346. context, Org-mode will support shift-selection for making and
  347. enlarging regions. To make this more effective, the bullet
  348. cycling will no longer happen anywhere in an item line, but only
  349. if the cursor is exactly on the bullet.
  350. If you set this variable to the symbol `always', then the keys
  351. will not be special in headlines, property lines, and item lines,
  352. to make shift selection work there as well. If this is what you
  353. want, you can use the following alternative commands: `C-c C-t'
  354. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  355. can be used to switch TODO sets, `C-c -' to cycle item bullet
  356. types, and properties can be edited by hand or in column view.
  357. However, when the cursor is on a timestamp, shift-cursor commands
  358. will still edit the time stamp - this is just too good to give up.
  359. XEmacs user should have this variable set to nil, because
  360. `shift-select-mode' is in Emacs 23 or later only."
  361. :group 'org
  362. :type '(choice
  363. (const :tag "Never" nil)
  364. (const :tag "When outside special context" t)
  365. (const :tag "Everywhere except timestamps" always)))
  366. (defcustom org-loop-over-headlines-in-active-region nil
  367. "Shall some commands act upon headlines in the active region?
  368. When set to `t', some commands will be performed in all headlines
  369. within the active region.
  370. When set to `start-level', some commands will be performed in all
  371. headlines within the active region, provided that these headlines
  372. are of the same level than the first one.
  373. When set to a string, those commands will be performed on the
  374. matching headlines within the active region. Such string must be
  375. a tags/property/todo match as it is used in the agenda tags view.
  376. The list of commands is: `org-schedule', `org-deadline',
  377. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  378. `org-archive-to-archive-sibling'. The archiving commands skip
  379. already archived entries."
  380. :type '(choice (const :tag "Don't loop" nil)
  381. (const :tag "All headlines in active region" t)
  382. (const :tag "In active region, headlines at the same level than the first one" 'start-level)
  383. (string :tag "Tags/Property/Todo matcher"))
  384. :version "24.1"
  385. :group 'org-todo
  386. :group 'org-archive)
  387. (defgroup org-startup nil
  388. "Options concerning startup of Org-mode."
  389. :tag "Org Startup"
  390. :group 'org)
  391. (defcustom org-startup-folded t
  392. "Non-nil means entering Org-mode will switch to OVERVIEW.
  393. This can also be configured on a per-file basis by adding one of
  394. the following lines anywhere in the buffer:
  395. #+STARTUP: fold (or `overview', this is equivalent)
  396. #+STARTUP: nofold (or `showall', this is equivalent)
  397. #+STARTUP: content
  398. #+STARTUP: showeverything"
  399. :group 'org-startup
  400. :type '(choice
  401. (const :tag "nofold: show all" nil)
  402. (const :tag "fold: overview" t)
  403. (const :tag "content: all headlines" content)
  404. (const :tag "show everything, even drawers" showeverything)))
  405. (defcustom org-startup-truncated t
  406. "Non-nil means entering Org-mode will set `truncate-lines'.
  407. This is useful since some lines containing links can be very long and
  408. uninteresting. Also tables look terrible when wrapped."
  409. :group 'org-startup
  410. :type 'boolean)
  411. (defcustom org-startup-indented nil
  412. "Non-nil means turn on `org-indent-mode' on startup.
  413. This can also be configured on a per-file basis by adding one of
  414. the following lines anywhere in the buffer:
  415. #+STARTUP: indent
  416. #+STARTUP: noindent"
  417. :group 'org-structure
  418. :type '(choice
  419. (const :tag "Not" nil)
  420. (const :tag "Globally (slow on startup in large files)" t)))
  421. (defcustom org-use-sub-superscripts t
  422. "Non-nil means interpret \"_\" and \"^\" for export.
  423. When this option is turned on, you can use TeX-like syntax for sub- and
  424. superscripts. Several characters after \"_\" or \"^\" will be
  425. considered as a single item - so grouping with {} is normally not
  426. needed. For example, the following things will be parsed as single
  427. sub- or superscripts.
  428. 10^24 or 10^tau several digits will be considered 1 item.
  429. 10^-12 or 10^-tau a leading sign with digits or a word
  430. x^2-y^3 will be read as x^2 - y^3, because items are
  431. terminated by almost any nonword/nondigit char.
  432. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  433. Still, ambiguity is possible - so when in doubt use {} to enclose the
  434. sub/superscript. If you set this variable to the symbol `{}',
  435. the braces are *required* in order to trigger interpretations as
  436. sub/superscript. This can be helpful in documents that need \"_\"
  437. frequently in plain text.
  438. Not all export backends support this, but HTML does.
  439. This option can also be set with the #+OPTIONS line, e.g. \"^:nil\"."
  440. :group 'org-startup
  441. :group 'org-export-translation
  442. :version "24.1"
  443. :type '(choice
  444. (const :tag "Always interpret" t)
  445. (const :tag "Only with braces" {})
  446. (const :tag "Never interpret" nil)))
  447. (if (fboundp 'defvaralias)
  448. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  449. (defcustom org-startup-with-beamer-mode nil
  450. "Non-nil means turn on `org-beamer-mode' on startup.
  451. This can also be configured on a per-file basis by adding one of
  452. the following lines anywhere in the buffer:
  453. #+STARTUP: beamer"
  454. :group 'org-startup
  455. :version "24.1"
  456. :type 'boolean)
  457. (defcustom org-startup-align-all-tables nil
  458. "Non-nil means align all tables when visiting a file.
  459. This is useful when the column width in tables is forced with <N> cookies
  460. in table fields. Such tables will look correct only after the first re-align.
  461. This can also be configured on a per-file basis by adding one of
  462. the following lines anywhere in the buffer:
  463. #+STARTUP: align
  464. #+STARTUP: noalign"
  465. :group 'org-startup
  466. :type 'boolean)
  467. (defcustom org-startup-with-inline-images nil
  468. "Non-nil means show inline images when loading a new Org file.
  469. This can also be configured on a per-file basis by adding one of
  470. the following lines anywhere in the buffer:
  471. #+STARTUP: inlineimages
  472. #+STARTUP: noinlineimages"
  473. :group 'org-startup
  474. :version "24.1"
  475. :type 'boolean)
  476. (defcustom org-insert-mode-line-in-empty-file nil
  477. "Non-nil means insert the first line setting Org-mode in empty files.
  478. When the function `org-mode' is called interactively in an empty file, this
  479. normally means that the file name does not automatically trigger Org-mode.
  480. To ensure that the file will always be in Org-mode in the future, a
  481. line enforcing Org-mode will be inserted into the buffer, if this option
  482. has been set."
  483. :group 'org-startup
  484. :type 'boolean)
  485. (defcustom org-replace-disputed-keys nil
  486. "Non-nil means use alternative key bindings for some keys.
  487. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  488. These keys are also used by other packages like shift-selection-mode'
  489. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  490. If you want to use Org-mode together with one of these other modes,
  491. or more generally if you would like to move some Org-mode commands to
  492. other keys, set this variable and configure the keys with the variable
  493. `org-disputed-keys'.
  494. This option is only relevant at load-time of Org-mode, and must be set
  495. *before* org.el is loaded. Changing it requires a restart of Emacs to
  496. become effective."
  497. :group 'org-startup
  498. :type 'boolean)
  499. (defcustom org-use-extra-keys nil
  500. "Non-nil means use extra key sequence definitions for certain commands.
  501. This happens automatically if you run XEmacs or if `window-system'
  502. is nil. This variable lets you do the same manually. You must
  503. set it before loading org.
  504. Example: on Carbon Emacs 22 running graphically, with an external
  505. keyboard on a Powerbook, the default way of setting M-left might
  506. not work for either Alt or ESC. Setting this variable will make
  507. it work for ESC."
  508. :group 'org-startup
  509. :type 'boolean)
  510. (if (fboundp 'defvaralias)
  511. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  512. (defcustom org-disputed-keys
  513. '(([(shift up)] . [(meta p)])
  514. ([(shift down)] . [(meta n)])
  515. ([(shift left)] . [(meta -)])
  516. ([(shift right)] . [(meta +)])
  517. ([(control shift right)] . [(meta shift +)])
  518. ([(control shift left)] . [(meta shift -)]))
  519. "Keys for which Org-mode and other modes compete.
  520. This is an alist, cars are the default keys, second element specifies
  521. the alternative to use when `org-replace-disputed-keys' is t.
  522. Keys can be specified in any syntax supported by `define-key'.
  523. The value of this option takes effect only at Org-mode's startup,
  524. therefore you'll have to restart Emacs to apply it after changing."
  525. :group 'org-startup
  526. :type 'alist)
  527. (defun org-key (key)
  528. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  529. Or return the original if not disputed.
  530. Also apply the translations defined in `org-xemacs-key-equivalents'."
  531. (when org-replace-disputed-keys
  532. (let* ((nkey (key-description key))
  533. (x (org-find-if (lambda (x)
  534. (equal (key-description (car x)) nkey))
  535. org-disputed-keys)))
  536. (setq key (if x (cdr x) key))))
  537. (when (featurep 'xemacs)
  538. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  539. key)
  540. (defun org-find-if (predicate seq)
  541. (catch 'exit
  542. (while seq
  543. (if (funcall predicate (car seq))
  544. (throw 'exit (car seq))
  545. (pop seq)))))
  546. (defun org-defkey (keymap key def)
  547. "Define a key, possibly translated, as returned by `org-key'."
  548. (define-key keymap (org-key key) def))
  549. (defcustom org-ellipsis nil
  550. "The ellipsis to use in the Org-mode outline.
  551. When nil, just use the standard three dots. When a string, use that instead,
  552. When a face, use the standard 3 dots, but with the specified face.
  553. The change affects only Org-mode (which will then use its own display table).
  554. Changing this requires executing `M-x org-mode' in a buffer to become
  555. effective."
  556. :group 'org-startup
  557. :type '(choice (const :tag "Default" nil)
  558. (face :tag "Face" :value org-warning)
  559. (string :tag "String" :value "...#")))
  560. (defvar org-display-table nil
  561. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  562. (defgroup org-keywords nil
  563. "Keywords in Org-mode."
  564. :tag "Org Keywords"
  565. :group 'org)
  566. (defcustom org-deadline-string "DEADLINE:"
  567. "String to mark deadline entries.
  568. A deadline is this string, followed by a time stamp. Should be a word,
  569. terminated by a colon. You can insert a schedule keyword and
  570. a timestamp with \\[org-deadline].
  571. Changes become only effective after restarting Emacs."
  572. :group 'org-keywords
  573. :type 'string)
  574. (defcustom org-scheduled-string "SCHEDULED:"
  575. "String to mark scheduled TODO entries.
  576. A schedule is this string, followed by a time stamp. Should be a word,
  577. terminated by a colon. You can insert a schedule keyword and
  578. a timestamp with \\[org-schedule].
  579. Changes become only effective after restarting Emacs."
  580. :group 'org-keywords
  581. :type 'string)
  582. (defcustom org-closed-string "CLOSED:"
  583. "String used as the prefix for timestamps logging closing a TODO entry."
  584. :group 'org-keywords
  585. :type 'string)
  586. (defcustom org-clock-string "CLOCK:"
  587. "String used as prefix for timestamps clocking work hours on an item."
  588. :group 'org-keywords
  589. :type 'string)
  590. (defcustom org-comment-string "COMMENT"
  591. "Entries starting with this keyword will never be exported.
  592. An entry can be toggled between COMMENT and normal with
  593. \\[org-toggle-comment].
  594. Changes become only effective after restarting Emacs."
  595. :group 'org-keywords
  596. :type 'string)
  597. (defcustom org-quote-string "QUOTE"
  598. "Entries starting with this keyword will be exported in fixed-width font.
  599. Quoting applies only to the text in the entry following the headline, and does
  600. not extend beyond the next headline, even if that is lower level.
  601. An entry can be toggled between QUOTE and normal with
  602. \\[org-toggle-fixed-width-section]."
  603. :group 'org-keywords
  604. :type 'string)
  605. (defconst org-repeat-re
  606. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  607. "Regular expression for specifying repeated events.
  608. After a match, group 1 contains the repeat expression.")
  609. (defgroup org-structure nil
  610. "Options concerning the general structure of Org-mode files."
  611. :tag "Org Structure"
  612. :group 'org)
  613. (defgroup org-reveal-location nil
  614. "Options about how to make context of a location visible."
  615. :tag "Org Reveal Location"
  616. :group 'org-structure)
  617. (defconst org-context-choice
  618. '(choice
  619. (const :tag "Always" t)
  620. (const :tag "Never" nil)
  621. (repeat :greedy t :tag "Individual contexts"
  622. (cons
  623. (choice :tag "Context"
  624. (const agenda)
  625. (const org-goto)
  626. (const occur-tree)
  627. (const tags-tree)
  628. (const link-search)
  629. (const mark-goto)
  630. (const bookmark-jump)
  631. (const isearch)
  632. (const default))
  633. (boolean))))
  634. "Contexts for the reveal options.")
  635. (defcustom org-show-hierarchy-above '((default . t))
  636. "Non-nil means show full hierarchy when revealing a location.
  637. Org-mode often shows locations in an org-mode file which might have
  638. been invisible before. When this is set, the hierarchy of headings
  639. above the exposed location is shown.
  640. Turning this off for example for sparse trees makes them very compact.
  641. Instead of t, this can also be an alist specifying this option for different
  642. contexts. Valid contexts are
  643. agenda when exposing an entry from the agenda
  644. org-goto when using the command `org-goto' on key C-c C-j
  645. occur-tree when using the command `org-occur' on key C-c /
  646. tags-tree when constructing a sparse tree based on tags matches
  647. link-search when exposing search matches associated with a link
  648. mark-goto when exposing the jump goal of a mark
  649. bookmark-jump when exposing a bookmark location
  650. isearch when exiting from an incremental search
  651. default default for all contexts not set explicitly"
  652. :group 'org-reveal-location
  653. :type org-context-choice)
  654. (defcustom org-show-following-heading '((default . nil))
  655. "Non-nil means show following heading when revealing a location.
  656. Org-mode often shows locations in an org-mode file which might have
  657. been invisible before. When this is set, the heading following the
  658. match is shown.
  659. Turning this off for example for sparse trees makes them very compact,
  660. but makes it harder to edit the location of the match. In such a case,
  661. use the command \\[org-reveal] to show more context.
  662. Instead of t, this can also be an alist specifying this option for different
  663. contexts. See `org-show-hierarchy-above' for valid contexts."
  664. :group 'org-reveal-location
  665. :type org-context-choice)
  666. (defcustom org-show-siblings '((default . nil) (isearch t))
  667. "Non-nil means show all sibling heading when revealing a location.
  668. Org-mode often shows locations in an org-mode file which might have
  669. been invisible before. When this is set, the sibling of the current entry
  670. heading are all made visible. If `org-show-hierarchy-above' is t,
  671. the same happens on each level of the hierarchy above the current entry.
  672. By default this is on for the isearch context, off for all other contexts.
  673. Turning this off for example for sparse trees makes them very compact,
  674. but makes it harder to edit the location of the match. In such a case,
  675. use the command \\[org-reveal] to show more context.
  676. Instead of t, this can also be an alist specifying this option for different
  677. contexts. See `org-show-hierarchy-above' for valid contexts."
  678. :group 'org-reveal-location
  679. :type org-context-choice)
  680. (defcustom org-show-entry-below '((default . nil))
  681. "Non-nil means show the entry below a headline when revealing a location.
  682. Org-mode often shows locations in an org-mode file which might have
  683. been invisible before. When this is set, the text below the headline that is
  684. exposed is also shown.
  685. By default this is off for all contexts.
  686. Instead of t, this can also be an alist specifying this option for different
  687. contexts. See `org-show-hierarchy-above' for valid contexts."
  688. :group 'org-reveal-location
  689. :type org-context-choice)
  690. (defcustom org-indirect-buffer-display 'other-window
  691. "How should indirect tree buffers be displayed?
  692. This applies to indirect buffers created with the commands
  693. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  694. Valid values are:
  695. current-window Display in the current window
  696. other-window Just display in another window.
  697. dedicated-frame Create one new frame, and re-use it each time.
  698. new-frame Make a new frame each time. Note that in this case
  699. previously-made indirect buffers are kept, and you need to
  700. kill these buffers yourself."
  701. :group 'org-structure
  702. :group 'org-agenda-windows
  703. :type '(choice
  704. (const :tag "In current window" current-window)
  705. (const :tag "In current frame, other window" other-window)
  706. (const :tag "Each time a new frame" new-frame)
  707. (const :tag "One dedicated frame" dedicated-frame)))
  708. (defcustom org-use-speed-commands nil
  709. "Non-nil means activate single letter commands at beginning of a headline.
  710. This may also be a function to test for appropriate locations where speed
  711. commands should be active."
  712. :group 'org-structure
  713. :type '(choice
  714. (const :tag "Never" nil)
  715. (const :tag "At beginning of headline stars" t)
  716. (function)))
  717. (defcustom org-speed-commands-user nil
  718. "Alist of additional speed commands.
  719. This list will be checked before `org-speed-commands-default'
  720. when the variable `org-use-speed-commands' is non-nil
  721. and when the cursor is at the beginning of a headline.
  722. The car if each entry is a string with a single letter, which must
  723. be assigned to `self-insert-command' in the global map.
  724. The cdr is either a command to be called interactively, a function
  725. to be called, or a form to be evaluated.
  726. An entry that is just a list with a single string will be interpreted
  727. as a descriptive headline that will be added when listing the speed
  728. commands in the Help buffer using the `?' speed command."
  729. :group 'org-structure
  730. :type '(repeat :value ("k" . ignore)
  731. (choice :value ("k" . ignore)
  732. (list :tag "Descriptive Headline" (string :tag "Headline"))
  733. (cons :tag "Letter and Command"
  734. (string :tag "Command letter")
  735. (choice
  736. (function)
  737. (sexp))))))
  738. (defgroup org-cycle nil
  739. "Options concerning visibility cycling in Org-mode."
  740. :tag "Org Cycle"
  741. :group 'org-structure)
  742. (defcustom org-cycle-skip-children-state-if-no-children t
  743. "Non-nil means skip CHILDREN state in entries that don't have any."
  744. :group 'org-cycle
  745. :type 'boolean)
  746. (defcustom org-cycle-max-level nil
  747. "Maximum level which should still be subject to visibility cycling.
  748. Levels higher than this will, for cycling, be treated as text, not a headline.
  749. When `org-odd-levels-only' is set, a value of N in this variable actually
  750. means 2N-1 stars as the limiting headline.
  751. When nil, cycle all levels.
  752. Note that the limiting level of cycling is also influenced by
  753. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  754. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  755. than its value."
  756. :group 'org-cycle
  757. :type '(choice
  758. (const :tag "No limit" nil)
  759. (integer :tag "Maximum level")))
  760. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK" "RESULTS")
  761. "Names of drawers. Drawers are not opened by cycling on the headline above.
  762. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  763. this:
  764. :DRAWERNAME:
  765. .....
  766. :END:
  767. The drawer \"PROPERTIES\" is special for capturing properties through
  768. the property API.
  769. Drawers can be defined on the per-file basis with a line like:
  770. #+DRAWERS: HIDDEN STATE PROPERTIES"
  771. :group 'org-structure
  772. :group 'org-cycle
  773. :type '(repeat (string :tag "Drawer Name")))
  774. (defcustom org-hide-block-startup nil
  775. "Non-nil means entering Org-mode will fold all blocks.
  776. This can also be set in on a per-file basis with
  777. #+STARTUP: hideblocks
  778. #+STARTUP: showblocks"
  779. :group 'org-startup
  780. :group 'org-cycle
  781. :type 'boolean)
  782. (defcustom org-cycle-global-at-bob nil
  783. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  784. This makes it possible to do global cycling without having to use S-TAB or
  785. \\[universal-argument] TAB. For this special case to work, the first line \
  786. of the buffer
  787. must not be a headline - it may be empty or some other text. When used in
  788. this way, `org-cycle-hook' is disables temporarily, to make sure the
  789. cursor stays at the beginning of the buffer.
  790. When this option is nil, don't do anything special at the beginning
  791. of the buffer."
  792. :group 'org-cycle
  793. :type 'boolean)
  794. (defcustom org-cycle-level-after-item/entry-creation t
  795. "Non-nil means cycle entry level or item indentation in new empty entries.
  796. When the cursor is at the end of an empty headline, i.e with only stars
  797. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  798. and then all possible ancestor states, before returning to the original state.
  799. This makes data entry extremely fast: M-RET to create a new headline,
  800. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  801. When the cursor is at the end of an empty plain list item, one TAB will
  802. make it a subitem, two or more tabs will back up to make this an item
  803. higher up in the item hierarchy."
  804. :group 'org-cycle
  805. :type 'boolean)
  806. (defcustom org-cycle-emulate-tab t
  807. "Where should `org-cycle' emulate TAB.
  808. nil Never
  809. white Only in completely white lines
  810. whitestart Only at the beginning of lines, before the first non-white char
  811. t Everywhere except in headlines
  812. exc-hl-bol Everywhere except at the start of a headline
  813. If TAB is used in a place where it does not emulate TAB, the current subtree
  814. visibility is cycled."
  815. :group 'org-cycle
  816. :type '(choice (const :tag "Never" nil)
  817. (const :tag "Only in completely white lines" white)
  818. (const :tag "Before first char in a line" whitestart)
  819. (const :tag "Everywhere except in headlines" t)
  820. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  821. ))
  822. (defcustom org-cycle-separator-lines 2
  823. "Number of empty lines needed to keep an empty line between collapsed trees.
  824. If you leave an empty line between the end of a subtree and the following
  825. headline, this empty line is hidden when the subtree is folded.
  826. Org-mode will leave (exactly) one empty line visible if the number of
  827. empty lines is equal or larger to the number given in this variable.
  828. So the default 2 means at least 2 empty lines after the end of a subtree
  829. are needed to produce free space between a collapsed subtree and the
  830. following headline.
  831. If the number is negative, and the number of empty lines is at least -N,
  832. all empty lines are shown.
  833. Special case: when 0, never leave empty lines in collapsed view."
  834. :group 'org-cycle
  835. :type 'integer)
  836. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  837. (defcustom org-pre-cycle-hook nil
  838. "Hook that is run before visibility cycling is happening.
  839. The function(s) in this hook must accept a single argument which indicates
  840. the new state that will be set right after running this hook. The
  841. argument is a symbol. Before a global state change, it can have the values
  842. `overview', `content', or `all'. Before a local state change, it can have
  843. the values `folded', `children', or `subtree'."
  844. :group 'org-cycle
  845. :type 'hook)
  846. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  847. org-cycle-hide-drawers
  848. org-cycle-show-empty-lines
  849. org-optimize-window-after-visibility-change)
  850. "Hook that is run after `org-cycle' has changed the buffer visibility.
  851. The function(s) in this hook must accept a single argument which indicates
  852. the new state that was set by the most recent `org-cycle' command. The
  853. argument is a symbol. After a global state change, it can have the values
  854. `overview', `content', or `all'. After a local state change, it can have
  855. the values `folded', `children', or `subtree'."
  856. :group 'org-cycle
  857. :type 'hook)
  858. (defgroup org-edit-structure nil
  859. "Options concerning structure editing in Org-mode."
  860. :tag "Org Edit Structure"
  861. :group 'org-structure)
  862. (defcustom org-odd-levels-only nil
  863. "Non-nil means skip even levels and only use odd levels for the outline.
  864. This has the effect that two stars are being added/taken away in
  865. promotion/demotion commands. It also influences how levels are
  866. handled by the exporters.
  867. Changing it requires restart of `font-lock-mode' to become effective
  868. for fontification also in regions already fontified.
  869. You may also set this on a per-file basis by adding one of the following
  870. lines to the buffer:
  871. #+STARTUP: odd
  872. #+STARTUP: oddeven"
  873. :group 'org-edit-structure
  874. :group 'org-appearance
  875. :type 'boolean)
  876. (defcustom org-adapt-indentation t
  877. "Non-nil means adapt indentation to outline node level.
  878. When this variable is set, Org assumes that you write outlines by
  879. indenting text in each node to align with the headline (after the stars).
  880. The following issues are influenced by this variable:
  881. - When this is set and the *entire* text in an entry is indented, the
  882. indentation is increased by one space in a demotion command, and
  883. decreased by one in a promotion command. If any line in the entry
  884. body starts with text at column 0, indentation is not changed at all.
  885. - Property drawers and planning information is inserted indented when
  886. this variable s set. When nil, they will not be indented.
  887. - TAB indents a line relative to context. The lines below a headline
  888. will be indented when this variable is set.
  889. Note that this is all about true indentation, by adding and removing
  890. space characters. See also `org-indent.el' which does level-dependent
  891. indentation in a virtual way, i.e. at display time in Emacs."
  892. :group 'org-edit-structure
  893. :type 'boolean)
  894. (defcustom org-special-ctrl-a/e nil
  895. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  896. When t, `C-a' will bring back the cursor to the beginning of the
  897. headline text, i.e. after the stars and after a possible TODO
  898. keyword. In an item, this will be the position after bullet and
  899. check-box, if any. When the cursor is already at that position,
  900. another `C-a' will bring it to the beginning of the line.
  901. `C-e' will jump to the end of the headline, ignoring the presence
  902. of tags in the headline. A second `C-e' will then jump to the
  903. true end of the line, after any tags. This also means that, when
  904. this variable is non-nil, `C-e' also will never jump beyond the
  905. end of the heading of a folded section, i.e. not after the
  906. ellipses.
  907. When set to the symbol `reversed', the first `C-a' or `C-e' works
  908. normally, going to the true line boundary first. Only a directly
  909. following, identical keypress will bring the cursor to the
  910. special positions.
  911. This may also be a cons cell where the behavior for `C-a' and
  912. `C-e' is set separately."
  913. :group 'org-edit-structure
  914. :type '(choice
  915. (const :tag "off" nil)
  916. (const :tag "on: after stars/bullet and before tags first" t)
  917. (const :tag "reversed: true line boundary first" reversed)
  918. (cons :tag "Set C-a and C-e separately"
  919. (choice :tag "Special C-a"
  920. (const :tag "off" nil)
  921. (const :tag "on: after stars/bullet first" t)
  922. (const :tag "reversed: before stars/bullet first" reversed))
  923. (choice :tag "Special C-e"
  924. (const :tag "off" nil)
  925. (const :tag "on: before tags first" t)
  926. (const :tag "reversed: after tags first" reversed)))))
  927. (if (fboundp 'defvaralias)
  928. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  929. (defcustom org-special-ctrl-k nil
  930. "Non-nil means `C-k' will behave specially in headlines.
  931. When nil, `C-k' will call the default `kill-line' command.
  932. When t, the following will happen while the cursor is in the headline:
  933. - When the cursor is at the beginning of a headline, kill the entire
  934. line and possible the folded subtree below the line.
  935. - When in the middle of the headline text, kill the headline up to the tags.
  936. - When after the headline text, kill the tags."
  937. :group 'org-edit-structure
  938. :type 'boolean)
  939. (defcustom org-ctrl-k-protect-subtree nil
  940. "Non-nil means, do not delete a hidden subtree with C-k.
  941. When set to the symbol `error', simply throw an error when C-k is
  942. used to kill (part-of) a headline that has hidden text behind it.
  943. Any other non-nil value will result in a query to the user, if it is
  944. OK to kill that hidden subtree. When nil, kill without remorse."
  945. :group 'org-edit-structure
  946. :version "24.1"
  947. :type '(choice
  948. (const :tag "Do not protect hidden subtrees" nil)
  949. (const :tag "Protect hidden subtrees with a security query" t)
  950. (const :tag "Never kill a hidden subtree with C-k" error)))
  951. (defcustom org-catch-invisible-edits nil
  952. "Check if in invisible region before inserting or deleting a character.
  953. Valid values are:
  954. nil Do not check, so just do invisible edits.
  955. error Throw an error and do nothing.
  956. show Make point visible, and do the requested edit.
  957. show-and-error Make point visible, then throw an error and abort the edit.
  958. smart Make point visible, and do insertion/deletion if it is
  959. adjacent to visible text and the change feels predictable.
  960. Never delete a previously invisible character or add in the
  961. middle or right after an invisible region. Basically, this
  962. allows insertion and backward-delete right before ellipses.
  963. FIXME: maybe in this case we should not even show?"
  964. :group 'org-edit-structure
  965. :version "24.1"
  966. :type '(choice
  967. (const :tag "Do not check" nil)
  968. (const :tag "Throw error when trying to edit" error)
  969. (const :tag "Unhide, but do not do the edit" show-and-error)
  970. (const :tag "Show invisible part and do the edit" show)
  971. (const :tag "Be smart and do the right thing" smart)))
  972. (defcustom org-yank-folded-subtrees t
  973. "Non-nil means when yanking subtrees, fold them.
  974. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  975. it starts with a heading and all other headings in it are either children
  976. or siblings, then fold all the subtrees. However, do this only if no
  977. text after the yank would be swallowed into a folded tree by this action."
  978. :group 'org-edit-structure
  979. :type 'boolean)
  980. (defcustom org-yank-adjusted-subtrees nil
  981. "Non-nil means when yanking subtrees, adjust the level.
  982. With this setting, `org-paste-subtree' is used to insert the subtree, see
  983. this function for details."
  984. :group 'org-edit-structure
  985. :type 'boolean)
  986. (defcustom org-M-RET-may-split-line '((default . t))
  987. "Non-nil means M-RET will split the line at the cursor position.
  988. When nil, it will go to the end of the line before making a
  989. new line.
  990. You may also set this option in a different way for different
  991. contexts. Valid contexts are:
  992. headline when creating a new headline
  993. item when creating a new item
  994. table in a table field
  995. default the value to be used for all contexts not explicitly
  996. customized"
  997. :group 'org-structure
  998. :group 'org-table
  999. :type '(choice
  1000. (const :tag "Always" t)
  1001. (const :tag "Never" nil)
  1002. (repeat :greedy t :tag "Individual contexts"
  1003. (cons
  1004. (choice :tag "Context"
  1005. (const headline)
  1006. (const item)
  1007. (const table)
  1008. (const default))
  1009. (boolean)))))
  1010. (defcustom org-insert-heading-respect-content nil
  1011. "Non-nil means insert new headings after the current subtree.
  1012. When nil, the new heading is created directly after the current line.
  1013. The commands \\[org-insert-heading-respect-content] and
  1014. \\[org-insert-todo-heading-respect-content] turn this variable on
  1015. for the duration of the command."
  1016. :group 'org-structure
  1017. :type 'boolean)
  1018. (defcustom org-blank-before-new-entry '((heading . auto)
  1019. (plain-list-item . auto))
  1020. "Should `org-insert-heading' leave a blank line before new heading/item?
  1021. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1022. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1023. which case Org will look at the surrounding headings/items and try to
  1024. make an intelligent decision whether to insert a blank line or not.
  1025. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  1026. set, the setting here is ignored and no empty line is inserted, to avoid
  1027. breaking the list structure."
  1028. :group 'org-edit-structure
  1029. :type '(list
  1030. (cons (const heading)
  1031. (choice (const :tag "Never" nil)
  1032. (const :tag "Always" t)
  1033. (const :tag "Auto" auto)))
  1034. (cons (const plain-list-item)
  1035. (choice (const :tag "Never" nil)
  1036. (const :tag "Always" t)
  1037. (const :tag "Auto" auto)))))
  1038. (defcustom org-insert-heading-hook nil
  1039. "Hook being run after inserting a new heading."
  1040. :group 'org-edit-structure
  1041. :type 'hook)
  1042. (defcustom org-enable-fixed-width-editor t
  1043. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1044. This currently only means they are never auto-wrapped.
  1045. When nil, such lines will be treated like ordinary lines.
  1046. See also the QUOTE keyword."
  1047. :group 'org-edit-structure
  1048. :type 'boolean)
  1049. (defcustom org-goto-auto-isearch t
  1050. "Non-nil means typing characters in `org-goto' starts incremental search."
  1051. :group 'org-edit-structure
  1052. :type 'boolean)
  1053. (defgroup org-sparse-trees nil
  1054. "Options concerning sparse trees in Org-mode."
  1055. :tag "Org Sparse Trees"
  1056. :group 'org-structure)
  1057. (defcustom org-highlight-sparse-tree-matches t
  1058. "Non-nil means highlight all matches that define a sparse tree.
  1059. The highlights will automatically disappear the next time the buffer is
  1060. changed by an edit command."
  1061. :group 'org-sparse-trees
  1062. :type 'boolean)
  1063. (defcustom org-remove-highlights-with-change t
  1064. "Non-nil means any change to the buffer will remove temporary highlights.
  1065. Such highlights are created by `org-occur' and `org-clock-display'.
  1066. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1067. The highlights created by `org-preview-latex-fragment' always need
  1068. `C-c C-c' to be removed."
  1069. :group 'org-sparse-trees
  1070. :group 'org-time
  1071. :type 'boolean)
  1072. (defcustom org-occur-hook '(org-first-headline-recenter)
  1073. "Hook that is run after `org-occur' has constructed a sparse tree.
  1074. This can be used to recenter the window to show as much of the structure
  1075. as possible."
  1076. :group 'org-sparse-trees
  1077. :type 'hook)
  1078. (defgroup org-imenu-and-speedbar nil
  1079. "Options concerning imenu and speedbar in Org-mode."
  1080. :tag "Org Imenu and Speedbar"
  1081. :group 'org-structure)
  1082. (defcustom org-imenu-depth 2
  1083. "The maximum level for Imenu access to Org-mode headlines.
  1084. This also applied for speedbar access."
  1085. :group 'org-imenu-and-speedbar
  1086. :type 'integer)
  1087. (defgroup org-table nil
  1088. "Options concerning tables in Org-mode."
  1089. :tag "Org Table"
  1090. :group 'org)
  1091. (defcustom org-enable-table-editor 'optimized
  1092. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1093. When nil, such lines will be treated like ordinary lines.
  1094. When equal to the symbol `optimized', the table editor will be optimized to
  1095. do the following:
  1096. - Automatic overwrite mode in front of whitespace in table fields.
  1097. This makes the structure of the table stay in tact as long as the edited
  1098. field does not exceed the column width.
  1099. - Minimize the number of realigns. Normally, the table is aligned each time
  1100. TAB or RET are pressed to move to another field. With optimization this
  1101. happens only if changes to a field might have changed the column width.
  1102. Optimization requires replacing the functions `self-insert-command',
  1103. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1104. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1105. very good at guessing when a re-align will be necessary, but you can always
  1106. force one with \\[org-ctrl-c-ctrl-c].
  1107. If you would like to use the optimized version in Org-mode, but the
  1108. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1109. This variable can be used to turn on and off the table editor during a session,
  1110. but in order to toggle optimization, a restart is required.
  1111. See also the variable `org-table-auto-blank-field'."
  1112. :group 'org-table
  1113. :type '(choice
  1114. (const :tag "off" nil)
  1115. (const :tag "on" t)
  1116. (const :tag "on, optimized" optimized)))
  1117. (defcustom org-self-insert-cluster-for-undo t
  1118. "Non-nil means cluster self-insert commands for undo when possible.
  1119. If this is set, then, like in the Emacs command loop, 20 consecutive
  1120. characters will be undone together.
  1121. This is configurable, because there is some impact on typing performance."
  1122. :group 'org-table
  1123. :type 'boolean)
  1124. (defcustom org-table-tab-recognizes-table.el t
  1125. "Non-nil means TAB will automatically notice a table.el table.
  1126. When it sees such a table, it moves point into it and - if necessary -
  1127. calls `table-recognize-table'."
  1128. :group 'org-table-editing
  1129. :type 'boolean)
  1130. (defgroup org-link nil
  1131. "Options concerning links in Org-mode."
  1132. :tag "Org Link"
  1133. :group 'org)
  1134. (defvar org-link-abbrev-alist-local nil
  1135. "Buffer-local version of `org-link-abbrev-alist', which see.
  1136. The value of this is taken from the #+LINK lines.")
  1137. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1138. (defcustom org-link-abbrev-alist nil
  1139. "Alist of link abbreviations.
  1140. The car of each element is a string, to be replaced at the start of a link.
  1141. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1142. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1143. [[linkkey:tag][description]]
  1144. The 'linkkey' must be a word word, starting with a letter, followed
  1145. by letters, numbers, '-' or '_'.
  1146. If REPLACE is a string, the tag will simply be appended to create the link.
  1147. If the string contains \"%s\", the tag will be inserted there. If the string
  1148. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1149. at that point (see the function `url-hexify-string'). If the string contains
  1150. the specifier \"%(my-function)\", then the custom function `my-function' will
  1151. be invoked: this function takes the tag as its only argument and must return
  1152. a string.
  1153. REPLACE may also be a function that will be called with the tag as the
  1154. only argument to create the link, which should be returned as a string.
  1155. See the manual for examples."
  1156. :group 'org-link
  1157. :type '(repeat
  1158. (cons
  1159. (string :tag "Protocol")
  1160. (choice
  1161. (string :tag "Format")
  1162. (function)))))
  1163. (defcustom org-descriptive-links t
  1164. "Non-nil means Org will display descriptive links.
  1165. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1166. \"Org Website\", hiding the link itself and just displaying its
  1167. description. When set to `nil', Org will display the full links
  1168. literally.
  1169. You can interactively set the value of this variable by calling
  1170. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1171. :group 'org-link
  1172. :type 'boolean)
  1173. (defcustom org-link-file-path-type 'adaptive
  1174. "How the path name in file links should be stored.
  1175. Valid values are:
  1176. relative Relative to the current directory, i.e. the directory of the file
  1177. into which the link is being inserted.
  1178. absolute Absolute path, if possible with ~ for home directory.
  1179. noabbrev Absolute path, no abbreviation of home directory.
  1180. adaptive Use relative path for files in the current directory and sub-
  1181. directories of it. For other files, use an absolute path."
  1182. :group 'org-link
  1183. :type '(choice
  1184. (const relative)
  1185. (const absolute)
  1186. (const noabbrev)
  1187. (const adaptive)))
  1188. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1189. "Types of links that should be activated in Org-mode files.
  1190. This is a list of symbols, each leading to the activation of a certain link
  1191. type. In principle, it does not hurt to turn on most link types - there may
  1192. be a small gain when turning off unused link types. The types are:
  1193. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1194. angle Links in angular brackets that may contain whitespace like
  1195. <bbdb:Carsten Dominik>.
  1196. plain Plain links in normal text, no whitespace, like http://google.com.
  1197. radio Text that is matched by a radio target, see manual for details.
  1198. tag Tag settings in a headline (link to tag search).
  1199. date Time stamps (link to calendar).
  1200. footnote Footnote labels.
  1201. Changing this variable requires a restart of Emacs to become effective."
  1202. :group 'org-link
  1203. :type '(set :greedy t
  1204. (const :tag "Double bracket links" bracket)
  1205. (const :tag "Angular bracket links" angle)
  1206. (const :tag "Plain text links" plain)
  1207. (const :tag "Radio target matches" radio)
  1208. (const :tag "Tags" tag)
  1209. (const :tag "Timestamps" date)
  1210. (const :tag "Footnotes" footnote)))
  1211. (defcustom org-make-link-description-function nil
  1212. "Function to use to generate link descriptions from links.
  1213. If nil the link location will be used. This function must take
  1214. two parameters; the first is the link and the second the
  1215. description `org-insert-link' has generated, and should return the
  1216. description to use."
  1217. :group 'org-link
  1218. :type 'function)
  1219. (defgroup org-link-store nil
  1220. "Options concerning storing links in Org-mode."
  1221. :tag "Org Store Link"
  1222. :group 'org-link)
  1223. (defcustom org-email-link-description-format "Email %c: %.30s"
  1224. "Format of the description part of a link to an email or usenet message.
  1225. The following %-escapes will be replaced by corresponding information:
  1226. %F full \"From\" field
  1227. %f name, taken from \"From\" field, address if no name
  1228. %T full \"To\" field
  1229. %t first name in \"To\" field, address if no name
  1230. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1231. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1232. %s subject
  1233. %d date
  1234. %m message-id.
  1235. You may use normal field width specification between the % and the letter.
  1236. This is for example useful to limit the length of the subject.
  1237. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1238. :group 'org-link-store
  1239. :type 'string)
  1240. (defcustom org-from-is-user-regexp
  1241. (let (r1 r2)
  1242. (when (and user-mail-address (not (string= user-mail-address "")))
  1243. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1244. (when (and user-full-name (not (string= user-full-name "")))
  1245. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1246. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1247. "Regexp matched against the \"From:\" header of an email or usenet message.
  1248. It should match if the message is from the user him/herself."
  1249. :group 'org-link-store
  1250. :type 'regexp)
  1251. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1252. "Non-nil means storing a link to an Org file will use entry IDs.
  1253. Note that before this variable is even considered, org-id must be loaded,
  1254. so please customize `org-modules' and turn it on.
  1255. The variable can have the following values:
  1256. t Create an ID if needed to make a link to the current entry.
  1257. create-if-interactive
  1258. If `org-store-link' is called directly (interactively, as a user
  1259. command), do create an ID to support the link. But when doing the
  1260. job for remember, only use the ID if it already exists. The
  1261. purpose of this setting is to avoid proliferation of unwanted
  1262. IDs, just because you happen to be in an Org file when you
  1263. call `org-remember' that automatically and preemptively
  1264. creates a link. If you do want to get an ID link in a remember
  1265. template to an entry not having an ID, create it first by
  1266. explicitly creating a link to it, using `C-c C-l' first.
  1267. create-if-interactive-and-no-custom-id
  1268. Like create-if-interactive, but do not create an ID if there is
  1269. a CUSTOM_ID property defined in the entry. This is the default.
  1270. use-existing
  1271. Use existing ID, do not create one.
  1272. nil Never use an ID to make a link, instead link using a text search for
  1273. the headline text."
  1274. :group 'org-link-store
  1275. :type '(choice
  1276. (const :tag "Create ID to make link" t)
  1277. (const :tag "Create if storing link interactively"
  1278. create-if-interactive)
  1279. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1280. create-if-interactive-and-no-custom-id)
  1281. (const :tag "Only use existing" use-existing)
  1282. (const :tag "Do not use ID to create link" nil)))
  1283. (defcustom org-context-in-file-links t
  1284. "Non-nil means file links from `org-store-link' contain context.
  1285. A search string will be added to the file name with :: as separator and
  1286. used to find the context when the link is activated by the command
  1287. `org-open-at-point'. When this option is t, the entire active region
  1288. will be placed in the search string of the file link. If set to a
  1289. positive integer, only the first n lines of context will be stored.
  1290. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1291. negates this setting for the duration of the command."
  1292. :group 'org-link-store
  1293. :type '(choice boolean integer))
  1294. (defcustom org-keep-stored-link-after-insertion nil
  1295. "Non-nil means keep link in list for entire session.
  1296. The command `org-store-link' adds a link pointing to the current
  1297. location to an internal list. These links accumulate during a session.
  1298. The command `org-insert-link' can be used to insert links into any
  1299. Org-mode file (offering completion for all stored links). When this
  1300. option is nil, every link which has been inserted once using \\[org-insert-link]
  1301. will be removed from the list, to make completing the unused links
  1302. more efficient."
  1303. :group 'org-link-store
  1304. :type 'boolean)
  1305. (defgroup org-link-follow nil
  1306. "Options concerning following links in Org-mode."
  1307. :tag "Org Follow Link"
  1308. :group 'org-link)
  1309. (defcustom org-link-translation-function nil
  1310. "Function to translate links with different syntax to Org syntax.
  1311. This can be used to translate links created for example by the Planner
  1312. or emacs-wiki packages to Org syntax.
  1313. The function must accept two parameters, a TYPE containing the link
  1314. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1315. which is everything after the link protocol. It should return a cons
  1316. with possibly modified values of type and path.
  1317. Org contains a function for this, so if you set this variable to
  1318. `org-translate-link-from-planner', you should be able follow many
  1319. links created by planner."
  1320. :group 'org-link-follow
  1321. :type 'function)
  1322. (defcustom org-follow-link-hook nil
  1323. "Hook that is run after a link has been followed."
  1324. :group 'org-link-follow
  1325. :type 'hook)
  1326. (defcustom org-tab-follows-link nil
  1327. "Non-nil means on links TAB will follow the link.
  1328. Needs to be set before org.el is loaded.
  1329. This really should not be used, it does not make sense, and the
  1330. implementation is bad."
  1331. :group 'org-link-follow
  1332. :type 'boolean)
  1333. (defcustom org-return-follows-link nil
  1334. "Non-nil means on links RET will follow the link."
  1335. :group 'org-link-follow
  1336. :type 'boolean)
  1337. (defcustom org-mouse-1-follows-link
  1338. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1339. "Non-nil means mouse-1 on a link will follow the link.
  1340. A longer mouse click will still set point. Does not work on XEmacs.
  1341. Needs to be set before org.el is loaded."
  1342. :group 'org-link-follow
  1343. :type 'boolean)
  1344. (defcustom org-mark-ring-length 4
  1345. "Number of different positions to be recorded in the ring.
  1346. Changing this requires a restart of Emacs to work correctly."
  1347. :group 'org-link-follow
  1348. :type 'integer)
  1349. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1350. "Non-nil means internal links in Org files must exactly match a headline.
  1351. When nil, the link search tries to match a phrase with all words
  1352. in the search text."
  1353. :group 'org-link-follow
  1354. :version "24.1"
  1355. :type '(choice
  1356. (const :tag "Use fuzzy text search" nil)
  1357. (const :tag "Match only exact headline" t)
  1358. (const :tag "Match exact headline or query to create it"
  1359. query-to-create)))
  1360. (defcustom org-link-frame-setup
  1361. '((vm . vm-visit-folder-other-frame)
  1362. (vm-imap . vm-visit-imap-folder-other-frame)
  1363. (gnus . org-gnus-no-new-news)
  1364. (file . find-file-other-window)
  1365. (wl . wl-other-frame))
  1366. "Setup the frame configuration for following links.
  1367. When following a link with Emacs, it may often be useful to display
  1368. this link in another window or frame. This variable can be used to
  1369. set this up for the different types of links.
  1370. For VM, use any of
  1371. `vm-visit-folder'
  1372. `vm-visit-folder-other-window'
  1373. `vm-visit-folder-other-frame'
  1374. For Gnus, use any of
  1375. `gnus'
  1376. `gnus-other-frame'
  1377. `org-gnus-no-new-news'
  1378. For FILE, use any of
  1379. `find-file'
  1380. `find-file-other-window'
  1381. `find-file-other-frame'
  1382. For Wanderlust use any of
  1383. `wl'
  1384. `wl-other-frame'
  1385. For the calendar, use the variable `calendar-setup'.
  1386. For BBDB, it is currently only possible to display the matches in
  1387. another window."
  1388. :group 'org-link-follow
  1389. :type '(list
  1390. (cons (const vm)
  1391. (choice
  1392. (const vm-visit-folder)
  1393. (const vm-visit-folder-other-window)
  1394. (const vm-visit-folder-other-frame)))
  1395. (cons (const gnus)
  1396. (choice
  1397. (const gnus)
  1398. (const gnus-other-frame)
  1399. (const org-gnus-no-new-news)))
  1400. (cons (const file)
  1401. (choice
  1402. (const find-file)
  1403. (const find-file-other-window)
  1404. (const find-file-other-frame)))
  1405. (cons (const wl)
  1406. (choice
  1407. (const wl)
  1408. (const wl-other-frame)))))
  1409. (defcustom org-display-internal-link-with-indirect-buffer nil
  1410. "Non-nil means use indirect buffer to display infile links.
  1411. Activating internal links (from one location in a file to another location
  1412. in the same file) normally just jumps to the location. When the link is
  1413. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1414. is displayed in
  1415. another window. When this option is set, the other window actually displays
  1416. an indirect buffer clone of the current buffer, to avoid any visibility
  1417. changes to the current buffer."
  1418. :group 'org-link-follow
  1419. :type 'boolean)
  1420. (defcustom org-open-non-existing-files nil
  1421. "Non-nil means `org-open-file' will open non-existing files.
  1422. When nil, an error will be generated.
  1423. This variable applies only to external applications because they
  1424. might choke on non-existing files. If the link is to a file that
  1425. will be opened in Emacs, the variable is ignored."
  1426. :group 'org-link-follow
  1427. :type 'boolean)
  1428. (defcustom org-open-directory-means-index-dot-org nil
  1429. "Non-nil means a link to a directory really means to index.org.
  1430. When nil, following a directory link will run dired or open a finder/explorer
  1431. window on that directory."
  1432. :group 'org-link-follow
  1433. :type 'boolean)
  1434. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1435. "Function and arguments to call for following mailto links.
  1436. This is a list with the first element being a Lisp function, and the
  1437. remaining elements being arguments to the function. In string arguments,
  1438. %a will be replaced by the address, and %s will be replaced by the subject
  1439. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1440. :group 'org-link-follow
  1441. :type '(choice
  1442. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1443. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1444. (const :tag "message-mail" (message-mail "%a" "%s"))
  1445. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1446. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1447. "Non-nil means ask for confirmation before executing shell links.
  1448. Shell links can be dangerous: just think about a link
  1449. [[shell:rm -rf ~/*][Google Search]]
  1450. This link would show up in your Org-mode document as \"Google Search\",
  1451. but really it would remove your entire home directory.
  1452. Therefore we advise against setting this variable to nil.
  1453. Just change it to `y-or-n-p' if you want to confirm with a
  1454. single keystroke rather than having to type \"yes\"."
  1455. :group 'org-link-follow
  1456. :type '(choice
  1457. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1458. (const :tag "with y-or-n (faster)" y-or-n-p)
  1459. (const :tag "no confirmation (dangerous)" nil)))
  1460. (put 'org-confirm-shell-link-function
  1461. 'safe-local-variable
  1462. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1463. (defcustom org-confirm-shell-link-not-regexp ""
  1464. "A regexp to skip confirmation for shell links."
  1465. :group 'org-link-follow
  1466. :version "24.1"
  1467. :type 'regexp)
  1468. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1469. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1470. Elisp links can be dangerous: just think about a link
  1471. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1472. This link would show up in your Org-mode document as \"Google Search\",
  1473. but really it would remove your entire home directory.
  1474. Therefore we advise against setting this variable to nil.
  1475. Just change it to `y-or-n-p' if you want to confirm with a
  1476. single keystroke rather than having to type \"yes\"."
  1477. :group 'org-link-follow
  1478. :type '(choice
  1479. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1480. (const :tag "with y-or-n (faster)" y-or-n-p)
  1481. (const :tag "no confirmation (dangerous)" nil)))
  1482. (put 'org-confirm-shell-link-function
  1483. 'safe-local-variable
  1484. #'(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1485. (defcustom org-confirm-elisp-link-not-regexp ""
  1486. "A regexp to skip confirmation for Elisp links."
  1487. :group 'org-link-follow
  1488. :version "24.1"
  1489. :type 'regexp)
  1490. (defconst org-file-apps-defaults-gnu
  1491. '((remote . emacs)
  1492. (system . mailcap)
  1493. (t . mailcap))
  1494. "Default file applications on a UNIX or GNU/Linux system.
  1495. See `org-file-apps'.")
  1496. (defconst org-file-apps-defaults-macosx
  1497. '((remote . emacs)
  1498. (t . "open %s")
  1499. (system . "open %s")
  1500. ("ps.gz" . "gv %s")
  1501. ("eps.gz" . "gv %s")
  1502. ("dvi" . "xdvi %s")
  1503. ("fig" . "xfig %s"))
  1504. "Default file applications on a MacOS X system.
  1505. The system \"open\" is known as a default, but we use X11 applications
  1506. for some files for which the OS does not have a good default.
  1507. See `org-file-apps'.")
  1508. (defconst org-file-apps-defaults-windowsnt
  1509. (list
  1510. '(remote . emacs)
  1511. (cons t
  1512. (list (if (featurep 'xemacs)
  1513. 'mswindows-shell-execute
  1514. 'w32-shell-execute)
  1515. "open" 'file))
  1516. (cons 'system
  1517. (list (if (featurep 'xemacs)
  1518. 'mswindows-shell-execute
  1519. 'w32-shell-execute)
  1520. "open" 'file)))
  1521. "Default file applications on a Windows NT system.
  1522. The system \"open\" is used for most files.
  1523. See `org-file-apps'.")
  1524. (defcustom org-file-apps
  1525. '(
  1526. (auto-mode . emacs)
  1527. ("\\.mm\\'" . default)
  1528. ("\\.x?html?\\'" . default)
  1529. ("\\.pdf\\'" . default)
  1530. )
  1531. "External applications for opening `file:path' items in a document.
  1532. Org-mode uses system defaults for different file types, but
  1533. you can use this variable to set the application for a given file
  1534. extension. The entries in this list are cons cells where the car identifies
  1535. files and the cdr the corresponding command. Possible values for the
  1536. file identifier are
  1537. \"string\" A string as a file identifier can be interpreted in different
  1538. ways, depending on its contents:
  1539. - Alphanumeric characters only:
  1540. Match links with this file extension.
  1541. Example: (\"pdf\" . \"evince %s\")
  1542. to open PDFs with evince.
  1543. - Regular expression: Match links where the
  1544. filename matches the regexp. If you want to
  1545. use groups here, use shy groups.
  1546. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1547. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1548. to open *.html and *.xhtml with firefox.
  1549. - Regular expression which contains (non-shy) groups:
  1550. Match links where the whole link, including \"::\", and
  1551. anything after that, matches the regexp.
  1552. In a custom command string, %1, %2, etc. are replaced with
  1553. the parts of the link that were matched by the groups.
  1554. For backwards compatibility, if a command string is given
  1555. that does not use any of the group matches, this case is
  1556. handled identically to the second one (i.e. match against
  1557. file name only).
  1558. In a custom lisp form, you can access the group matches with
  1559. (match-string n link).
  1560. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1561. to open [[file:document.pdf::5]] with evince at page 5.
  1562. `directory' Matches a directory
  1563. `remote' Matches a remote file, accessible through tramp or efs.
  1564. Remote files most likely should be visited through Emacs
  1565. because external applications cannot handle such paths.
  1566. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1567. so all files Emacs knows how to handle. Using this with
  1568. command `emacs' will open most files in Emacs. Beware that this
  1569. will also open html files inside Emacs, unless you add
  1570. (\"html\" . default) to the list as well.
  1571. t Default for files not matched by any of the other options.
  1572. `system' The system command to open files, like `open' on Windows
  1573. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1574. that will be selected if you call `C-c C-o' with a double
  1575. \\[universal-argument] \\[universal-argument] prefix.
  1576. Possible values for the command are:
  1577. `emacs' The file will be visited by the current Emacs process.
  1578. `default' Use the default application for this file type, which is the
  1579. association for t in the list, most likely in the system-specific
  1580. part.
  1581. This can be used to overrule an unwanted setting in the
  1582. system-specific variable.
  1583. `system' Use the system command for opening files, like \"open\".
  1584. This command is specified by the entry whose car is `system'.
  1585. Most likely, the system-specific version of this variable
  1586. does define this command, but you can overrule/replace it
  1587. here.
  1588. string A command to be executed by a shell; %s will be replaced
  1589. by the path to the file.
  1590. sexp A Lisp form which will be evaluated. The file path will
  1591. be available in the Lisp variable `file'.
  1592. For more examples, see the system specific constants
  1593. `org-file-apps-defaults-macosx'
  1594. `org-file-apps-defaults-windowsnt'
  1595. `org-file-apps-defaults-gnu'."
  1596. :group 'org-link-follow
  1597. :type '(repeat
  1598. (cons (choice :value ""
  1599. (string :tag "Extension")
  1600. (const :tag "System command to open files" system)
  1601. (const :tag "Default for unrecognized files" t)
  1602. (const :tag "Remote file" remote)
  1603. (const :tag "Links to a directory" directory)
  1604. (const :tag "Any files that have Emacs modes"
  1605. auto-mode))
  1606. (choice :value ""
  1607. (const :tag "Visit with Emacs" emacs)
  1608. (const :tag "Use default" default)
  1609. (const :tag "Use the system command" system)
  1610. (string :tag "Command")
  1611. (sexp :tag "Lisp form")))))
  1612. (defcustom org-doi-server-url "http://dx.doi.org/"
  1613. "The URL of the DOI server."
  1614. :type 'string
  1615. :version "24.2"
  1616. :group 'org-link-follow)
  1617. (defgroup org-refile nil
  1618. "Options concerning refiling entries in Org-mode."
  1619. :tag "Org Refile"
  1620. :group 'org)
  1621. (defcustom org-directory "~/org"
  1622. "Directory with org files.
  1623. This is just a default location to look for Org files. There is no need
  1624. at all to put your files into this directory. It is only used in the
  1625. following situations:
  1626. 1. When a remember template specifies a target file that is not an
  1627. absolute path. The path will then be interpreted relative to
  1628. `org-directory'
  1629. 2. When a remember note is filed away in an interactive way (when exiting the
  1630. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1631. with `org-directory' as the default path."
  1632. :group 'org-refile
  1633. :group 'org-remember
  1634. :type 'directory)
  1635. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1636. "Default target for storing notes.
  1637. Used as a fall back file for org-remember.el and org-capture.el, for
  1638. templates that do not specify a target file."
  1639. :group 'org-refile
  1640. :group 'org-remember
  1641. :type '(choice
  1642. (const :tag "Default from remember-data-file" nil)
  1643. file))
  1644. (defcustom org-goto-interface 'outline
  1645. "The default interface to be used for `org-goto'.
  1646. Allowed values are:
  1647. outline The interface shows an outline of the relevant file
  1648. and the correct heading is found by moving through
  1649. the outline or by searching with incremental search.
  1650. outline-path-completion Headlines in the current buffer are offered via
  1651. completion. This is the interface also used by
  1652. the refile command."
  1653. :group 'org-refile
  1654. :type '(choice
  1655. (const :tag "Outline" outline)
  1656. (const :tag "Outline-path-completion" outline-path-completion)))
  1657. (defcustom org-goto-max-level 5
  1658. "Maximum target level when running `org-goto' with refile interface."
  1659. :group 'org-refile
  1660. :type 'integer)
  1661. (defcustom org-reverse-note-order nil
  1662. "Non-nil means store new notes at the beginning of a file or entry.
  1663. When nil, new notes will be filed to the end of a file or entry.
  1664. This can also be a list with cons cells of regular expressions that
  1665. are matched against file names, and values."
  1666. :group 'org-remember
  1667. :group 'org-refile
  1668. :type '(choice
  1669. (const :tag "Reverse always" t)
  1670. (const :tag "Reverse never" nil)
  1671. (repeat :tag "By file name regexp"
  1672. (cons regexp boolean))))
  1673. (defcustom org-log-refile nil
  1674. "Information to record when a task is refiled.
  1675. Possible values are:
  1676. nil Don't add anything
  1677. time Add a time stamp to the task
  1678. note Prompt for a note and add it with template `org-log-note-headings'
  1679. This option can also be set with on a per-file-basis with
  1680. #+STARTUP: nologrefile
  1681. #+STARTUP: logrefile
  1682. #+STARTUP: lognoterefile
  1683. You can have local logging settings for a subtree by setting the LOGGING
  1684. property to one or more of these keywords.
  1685. When bulk-refiling from the agenda, the value `note' is forbidden and
  1686. will temporarily be changed to `time'."
  1687. :group 'org-refile
  1688. :group 'org-progress
  1689. :version "24.1"
  1690. :type '(choice
  1691. (const :tag "No logging" nil)
  1692. (const :tag "Record timestamp" time)
  1693. (const :tag "Record timestamp with note." note)))
  1694. (defcustom org-refile-targets nil
  1695. "Targets for refiling entries with \\[org-refile].
  1696. This is a list of cons cells. Each cell contains:
  1697. - a specification of the files to be considered, either a list of files,
  1698. or a symbol whose function or variable value will be used to retrieve
  1699. a file name or a list of file names. If you use `org-agenda-files' for
  1700. that, all agenda files will be scanned for targets. Nil means consider
  1701. headings in the current buffer.
  1702. - A specification of how to find candidate refile targets. This may be
  1703. any of:
  1704. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1705. This tag has to be present in all target headlines, inheritance will
  1706. not be considered.
  1707. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1708. todo keyword.
  1709. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1710. headlines that are refiling targets.
  1711. - a cons cell (:level . N). Any headline of level N is considered a target.
  1712. Note that, when `org-odd-levels-only' is set, level corresponds to
  1713. order in hierarchy, not to the number of stars.
  1714. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1715. Note that, when `org-odd-levels-only' is set, level corresponds to
  1716. order in hierarchy, not to the number of stars.
  1717. Each element of this list generates a set of possible targets.
  1718. The union of these sets is presented (with completion) to
  1719. the user by `org-refile'.
  1720. You can set the variable `org-refile-target-verify-function' to a function
  1721. to verify each headline found by the simple criteria above.
  1722. When this variable is nil, all top-level headlines in the current buffer
  1723. are used, equivalent to the value `((nil . (:level . 1))'."
  1724. :group 'org-refile
  1725. :type '(repeat
  1726. (cons
  1727. (choice :value org-agenda-files
  1728. (const :tag "All agenda files" org-agenda-files)
  1729. (const :tag "Current buffer" nil)
  1730. (function) (variable) (file))
  1731. (choice :tag "Identify target headline by"
  1732. (cons :tag "Specific tag" (const :value :tag) (string))
  1733. (cons :tag "TODO keyword" (const :value :todo) (string))
  1734. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1735. (cons :tag "Level number" (const :value :level) (integer))
  1736. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1737. (defcustom org-refile-target-verify-function nil
  1738. "Function to verify if the headline at point should be a refile target.
  1739. The function will be called without arguments, with point at the
  1740. beginning of the headline. It should return t and leave point
  1741. where it is if the headline is a valid target for refiling.
  1742. If the target should not be selected, the function must return nil.
  1743. In addition to this, it may move point to a place from where the search
  1744. should be continued. For example, the function may decide that the entire
  1745. subtree of the current entry should be excluded and move point to the end
  1746. of the subtree."
  1747. :group 'org-refile
  1748. :type 'function)
  1749. (defcustom org-refile-use-cache nil
  1750. "Non-nil means cache refile targets to speed up the process.
  1751. The cache for a particular file will be updated automatically when
  1752. the buffer has been killed, or when any of the marker used for flagging
  1753. refile targets no longer points at a live buffer.
  1754. If you have added new entries to a buffer that might themselves be targets,
  1755. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1756. find that easier, `C-u C-u C-u C-c C-w'."
  1757. :group 'org-refile
  1758. :version "24.1"
  1759. :type 'boolean)
  1760. (defcustom org-refile-use-outline-path nil
  1761. "Non-nil means provide refile targets as paths.
  1762. So a level 3 headline will be available as level1/level2/level3.
  1763. When the value is `file', also include the file name (without directory)
  1764. into the path. In this case, you can also stop the completion after
  1765. the file name, to get entries inserted as top level in the file.
  1766. When `full-file-path', include the full file path."
  1767. :group 'org-refile
  1768. :type '(choice
  1769. (const :tag "Not" nil)
  1770. (const :tag "Yes" t)
  1771. (const :tag "Start with file name" file)
  1772. (const :tag "Start with full file path" full-file-path)))
  1773. (defcustom org-outline-path-complete-in-steps t
  1774. "Non-nil means complete the outline path in hierarchical steps.
  1775. When Org-mode uses the refile interface to select an outline path
  1776. \(see variable `org-refile-use-outline-path'), the completion of
  1777. the path can be done is a single go, or if can be done in steps down
  1778. the headline hierarchy. Going in steps is probably the best if you
  1779. do not use a special completion package like `ido' or `icicles'.
  1780. However, when using these packages, going in one step can be very
  1781. fast, while still showing the whole path to the entry."
  1782. :group 'org-refile
  1783. :type 'boolean)
  1784. (defcustom org-refile-allow-creating-parent-nodes nil
  1785. "Non-nil means allow to create new nodes as refile targets.
  1786. New nodes are then created by adding \"/new node name\" to the completion
  1787. of an existing node. When the value of this variable is `confirm',
  1788. new node creation must be confirmed by the user (recommended)
  1789. When nil, the completion must match an existing entry.
  1790. Note that, if the new heading is not seen by the criteria
  1791. listed in `org-refile-targets', multiple instances of the same
  1792. heading would be created by trying again to file under the new
  1793. heading."
  1794. :group 'org-refile
  1795. :type '(choice
  1796. (const :tag "Never" nil)
  1797. (const :tag "Always" t)
  1798. (const :tag "Prompt for confirmation" confirm)))
  1799. (defcustom org-refile-active-region-within-subtree nil
  1800. "Non-nil means also refile active region within a subtree.
  1801. By default `org-refile' doesn't allow refiling regions if they
  1802. don't contain a set of subtrees, but it might be convenient to
  1803. do so sometimes: in that case, the first line of the region is
  1804. converted to a headline before refiling."
  1805. :group 'org-refile
  1806. :version "24.1"
  1807. :type 'boolean)
  1808. (defgroup org-todo nil
  1809. "Options concerning TODO items in Org-mode."
  1810. :tag "Org TODO"
  1811. :group 'org)
  1812. (defgroup org-progress nil
  1813. "Options concerning Progress logging in Org-mode."
  1814. :tag "Org Progress"
  1815. :group 'org-time)
  1816. (defvar org-todo-interpretation-widgets
  1817. '((:tag "Sequence (cycling hits every state)" sequence)
  1818. (:tag "Type (cycling directly to DONE)" type))
  1819. "The available interpretation symbols for customizing `org-todo-keywords'.
  1820. Interested libraries should add to this list.")
  1821. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1822. "List of TODO entry keyword sequences and their interpretation.
  1823. \\<org-mode-map>This is a list of sequences.
  1824. Each sequence starts with a symbol, either `sequence' or `type',
  1825. indicating if the keywords should be interpreted as a sequence of
  1826. action steps, or as different types of TODO items. The first
  1827. keywords are states requiring action - these states will select a headline
  1828. for inclusion into the global TODO list Org-mode produces. If one of
  1829. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1830. signify that no further action is necessary. If \"|\" is not found,
  1831. the last keyword is treated as the only DONE state of the sequence.
  1832. The command \\[org-todo] cycles an entry through these states, and one
  1833. additional state where no keyword is present. For details about this
  1834. cycling, see the manual.
  1835. TODO keywords and interpretation can also be set on a per-file basis with
  1836. the special #+SEQ_TODO and #+TYP_TODO lines.
  1837. Each keyword can optionally specify a letter for fast state selection
  1838. \(in combination with the variable `org-use-fast-todo-selection')
  1839. and specifiers for state change logging, using the same syntax that
  1840. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  1841. the WAIT state can be selected with the \"w\" letter. \"WAIT(w!)\"
  1842. indicates to record a time stamp each time this state is selected.
  1843. Each keyword may also specify if a timestamp or a note should be
  1844. recorded when entering or leaving the state, by adding additional
  1845. characters in the parenthesis after the keyword. This looks like this:
  1846. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1847. record only the time of the state change. With X and Y being either
  1848. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1849. Y when leaving the state if and only if the *target* state does not
  1850. define X. You may omit any of the fast-selection key or X or /Y,
  1851. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1852. For backward compatibility, this variable may also be just a list
  1853. of keywords. In this case the interpretation (sequence or type) will be
  1854. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1855. :group 'org-todo
  1856. :group 'org-keywords
  1857. :type '(choice
  1858. (repeat :tag "Old syntax, just keywords"
  1859. (string :tag "Keyword"))
  1860. (repeat :tag "New syntax"
  1861. (cons
  1862. (choice
  1863. :tag "Interpretation"
  1864. ;;Quick and dirty way to see
  1865. ;;`org-todo-interpretations'. This takes the
  1866. ;;place of item arguments
  1867. :convert-widget
  1868. (lambda (widget)
  1869. (widget-put widget
  1870. :args (mapcar
  1871. #'(lambda (x)
  1872. (widget-convert
  1873. (cons 'const x)))
  1874. org-todo-interpretation-widgets))
  1875. widget))
  1876. (repeat
  1877. (string :tag "Keyword"))))))
  1878. (defvar org-todo-keywords-1 nil
  1879. "All TODO and DONE keywords active in a buffer.")
  1880. (make-variable-buffer-local 'org-todo-keywords-1)
  1881. (defvar org-todo-keywords-for-agenda nil)
  1882. (defvar org-done-keywords-for-agenda nil)
  1883. (defvar org-drawers-for-agenda nil)
  1884. (defvar org-todo-keyword-alist-for-agenda nil)
  1885. (defvar org-tag-alist-for-agenda nil)
  1886. (defvar org-agenda-contributing-files nil)
  1887. (defvar org-not-done-keywords nil)
  1888. (make-variable-buffer-local 'org-not-done-keywords)
  1889. (defvar org-done-keywords nil)
  1890. (make-variable-buffer-local 'org-done-keywords)
  1891. (defvar org-todo-heads nil)
  1892. (make-variable-buffer-local 'org-todo-heads)
  1893. (defvar org-todo-sets nil)
  1894. (make-variable-buffer-local 'org-todo-sets)
  1895. (defvar org-todo-log-states nil)
  1896. (make-variable-buffer-local 'org-todo-log-states)
  1897. (defvar org-todo-kwd-alist nil)
  1898. (make-variable-buffer-local 'org-todo-kwd-alist)
  1899. (defvar org-todo-key-alist nil)
  1900. (make-variable-buffer-local 'org-todo-key-alist)
  1901. (defvar org-todo-key-trigger nil)
  1902. (make-variable-buffer-local 'org-todo-key-trigger)
  1903. (defcustom org-todo-interpretation 'sequence
  1904. "Controls how TODO keywords are interpreted.
  1905. This variable is in principle obsolete and is only used for
  1906. backward compatibility, if the interpretation of todo keywords is
  1907. not given already in `org-todo-keywords'. See that variable for
  1908. more information."
  1909. :group 'org-todo
  1910. :group 'org-keywords
  1911. :type '(choice (const sequence)
  1912. (const type)))
  1913. (defcustom org-use-fast-todo-selection t
  1914. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1915. This variable describes if and under what circumstances the cycling
  1916. mechanism for TODO keywords will be replaced by a single-key, direct
  1917. selection scheme.
  1918. When nil, fast selection is never used.
  1919. When the symbol `prefix', it will be used when `org-todo' is called
  1920. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  1921. `C-u t' in an agenda buffer.
  1922. When t, fast selection is used by default. In this case, the prefix
  1923. argument forces cycling instead.
  1924. In all cases, the special interface is only used if access keys have
  1925. actually been assigned by the user, i.e. if keywords in the configuration
  1926. are followed by a letter in parenthesis, like TODO(t)."
  1927. :group 'org-todo
  1928. :type '(choice
  1929. (const :tag "Never" nil)
  1930. (const :tag "By default" t)
  1931. (const :tag "Only with C-u C-c C-t" prefix)))
  1932. (defcustom org-provide-todo-statistics t
  1933. "Non-nil means update todo statistics after insert and toggle.
  1934. ALL-HEADLINES means update todo statistics by including headlines
  1935. with no TODO keyword as well, counting them as not done.
  1936. A list of TODO keywords means the same, but skip keywords that are
  1937. not in this list.
  1938. When this is set, todo statistics is updated in the parent of the
  1939. current entry each time a todo state is changed."
  1940. :group 'org-todo
  1941. :type '(choice
  1942. (const :tag "Yes, only for TODO entries" t)
  1943. (const :tag "Yes, including all entries" 'all-headlines)
  1944. (repeat :tag "Yes, for TODOs in this list"
  1945. (string :tag "TODO keyword"))
  1946. (other :tag "No TODO statistics" nil)))
  1947. (defcustom org-hierarchical-todo-statistics t
  1948. "Non-nil means TODO statistics covers just direct children.
  1949. When nil, all entries in the subtree are considered.
  1950. This has only an effect if `org-provide-todo-statistics' is set.
  1951. To set this to nil for only a single subtree, use a COOKIE_DATA
  1952. property and include the word \"recursive\" into the value."
  1953. :group 'org-todo
  1954. :type 'boolean)
  1955. (defcustom org-after-todo-state-change-hook nil
  1956. "Hook which is run after the state of a TODO item was changed.
  1957. The new state (a string with a TODO keyword, or nil) is available in the
  1958. Lisp variable `org-state'."
  1959. :group 'org-todo
  1960. :type 'hook)
  1961. (defvar org-blocker-hook nil
  1962. "Hook for functions that are allowed to block a state change.
  1963. Each function gets as its single argument a property list, see
  1964. `org-trigger-hook' for more information about this list.
  1965. If any of the functions in this hook returns nil, the state change
  1966. is blocked.")
  1967. (defvar org-trigger-hook nil
  1968. "Hook for functions that are triggered by a state change.
  1969. Each function gets as its single argument a property list with at least
  1970. the following elements:
  1971. (:type type-of-change :position pos-at-entry-start
  1972. :from old-state :to new-state)
  1973. Depending on the type, more properties may be present.
  1974. This mechanism is currently implemented for:
  1975. TODO state changes
  1976. ------------------
  1977. :type todo-state-change
  1978. :from previous state (keyword as a string), or nil, or a symbol
  1979. 'todo' or 'done', to indicate the general type of state.
  1980. :to new state, like in :from")
  1981. (defcustom org-enforce-todo-dependencies nil
  1982. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1983. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1984. be blocked if any prior sibling is not yet done.
  1985. Finally, if the parent is blocked because of ordered siblings of its own,
  1986. the child will also be blocked."
  1987. :set (lambda (var val)
  1988. (set var val)
  1989. (if val
  1990. (add-hook 'org-blocker-hook
  1991. 'org-block-todo-from-children-or-siblings-or-parent)
  1992. (remove-hook 'org-blocker-hook
  1993. 'org-block-todo-from-children-or-siblings-or-parent)))
  1994. :group 'org-todo
  1995. :type 'boolean)
  1996. (defcustom org-enforce-todo-checkbox-dependencies nil
  1997. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1998. When this is nil, checkboxes have no influence on switching TODO states.
  1999. When non-nil, you first need to check off all check boxes before the TODO
  2000. entry can be switched to DONE.
  2001. This variable needs to be set before org.el is loaded, and you need to
  2002. restart Emacs after a change to make the change effective. The only way
  2003. to change is while Emacs is running is through the customize interface."
  2004. :set (lambda (var val)
  2005. (set var val)
  2006. (if val
  2007. (add-hook 'org-blocker-hook
  2008. 'org-block-todo-from-checkboxes)
  2009. (remove-hook 'org-blocker-hook
  2010. 'org-block-todo-from-checkboxes)))
  2011. :group 'org-todo
  2012. :type 'boolean)
  2013. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2014. "Non-nil means inserting a TODO heading is treated as state change.
  2015. So when the command \\[org-insert-todo-heading] is used, state change
  2016. logging will apply if appropriate. When nil, the new TODO item will
  2017. be inserted directly, and no logging will take place."
  2018. :group 'org-todo
  2019. :type 'boolean)
  2020. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2021. "Non-nil means switching TODO states with S-cursor counts as state change.
  2022. This is the default behavior. However, setting this to nil allows a
  2023. convenient way to select a TODO state and bypass any logging associated
  2024. with that."
  2025. :group 'org-todo
  2026. :type 'boolean)
  2027. (defcustom org-todo-state-tags-triggers nil
  2028. "Tag changes that should be triggered by TODO state changes.
  2029. This is a list. Each entry is
  2030. (state-change (tag . flag) .......)
  2031. State-change can be a string with a state, and empty string to indicate the
  2032. state that has no TODO keyword, or it can be one of the symbols `todo'
  2033. or `done', meaning any not-done or done state, respectively."
  2034. :group 'org-todo
  2035. :group 'org-tags
  2036. :type '(repeat
  2037. (cons (choice :tag "When changing to"
  2038. (const :tag "Not-done state" todo)
  2039. (const :tag "Done state" done)
  2040. (string :tag "State"))
  2041. (repeat
  2042. (cons :tag "Tag action"
  2043. (string :tag "Tag")
  2044. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2045. (defcustom org-log-done nil
  2046. "Information to record when a task moves to the DONE state.
  2047. Possible values are:
  2048. nil Don't add anything, just change the keyword
  2049. time Add a time stamp to the task
  2050. note Prompt for a note and add it with template `org-log-note-headings'
  2051. This option can also be set with on a per-file-basis with
  2052. #+STARTUP: nologdone
  2053. #+STARTUP: logdone
  2054. #+STARTUP: lognotedone
  2055. You can have local logging settings for a subtree by setting the LOGGING
  2056. property to one or more of these keywords."
  2057. :group 'org-todo
  2058. :group 'org-progress
  2059. :type '(choice
  2060. (const :tag "No logging" nil)
  2061. (const :tag "Record CLOSED timestamp" time)
  2062. (const :tag "Record CLOSED timestamp with note." note)))
  2063. ;; Normalize old uses of org-log-done.
  2064. (cond
  2065. ((eq org-log-done t) (setq org-log-done 'time))
  2066. ((and (listp org-log-done) (memq 'done org-log-done))
  2067. (setq org-log-done 'note)))
  2068. (defcustom org-log-reschedule nil
  2069. "Information to record when the scheduling date of a tasks is modified.
  2070. Possible values are:
  2071. nil Don't add anything, just change the date
  2072. time Add a time stamp to the task
  2073. note Prompt for a note and add it with template `org-log-note-headings'
  2074. This option can also be set with on a per-file-basis with
  2075. #+STARTUP: nologreschedule
  2076. #+STARTUP: logreschedule
  2077. #+STARTUP: lognotereschedule"
  2078. :group 'org-todo
  2079. :group 'org-progress
  2080. :type '(choice
  2081. (const :tag "No logging" nil)
  2082. (const :tag "Record timestamp" time)
  2083. (const :tag "Record timestamp with note." note)))
  2084. (defcustom org-log-redeadline nil
  2085. "Information to record when the deadline date of a tasks is modified.
  2086. Possible values are:
  2087. nil Don't add anything, just change the date
  2088. time Add a time stamp to the task
  2089. note Prompt for a note and add it with template `org-log-note-headings'
  2090. This option can also be set with on a per-file-basis with
  2091. #+STARTUP: nologredeadline
  2092. #+STARTUP: logredeadline
  2093. #+STARTUP: lognoteredeadline
  2094. You can have local logging settings for a subtree by setting the LOGGING
  2095. property to one or more of these keywords."
  2096. :group 'org-todo
  2097. :group 'org-progress
  2098. :type '(choice
  2099. (const :tag "No logging" nil)
  2100. (const :tag "Record timestamp" time)
  2101. (const :tag "Record timestamp with note." note)))
  2102. (defcustom org-log-note-clock-out nil
  2103. "Non-nil means record a note when clocking out of an item.
  2104. This can also be configured on a per-file basis by adding one of
  2105. the following lines anywhere in the buffer:
  2106. #+STARTUP: lognoteclock-out
  2107. #+STARTUP: nolognoteclock-out"
  2108. :group 'org-todo
  2109. :group 'org-progress
  2110. :type 'boolean)
  2111. (defcustom org-log-done-with-time t
  2112. "Non-nil means the CLOSED time stamp will contain date and time.
  2113. When nil, only the date will be recorded."
  2114. :group 'org-progress
  2115. :type 'boolean)
  2116. (defcustom org-log-note-headings
  2117. '((done . "CLOSING NOTE %t")
  2118. (state . "State %-12s from %-12S %t")
  2119. (note . "Note taken on %t")
  2120. (reschedule . "Rescheduled from %S on %t")
  2121. (delschedule . "Not scheduled, was %S on %t")
  2122. (redeadline . "New deadline from %S on %t")
  2123. (deldeadline . "Removed deadline, was %S on %t")
  2124. (refile . "Refiled on %t")
  2125. (clock-out . ""))
  2126. "Headings for notes added to entries.
  2127. The value is an alist, with the car being a symbol indicating the note
  2128. context, and the cdr is the heading to be used. The heading may also be the
  2129. empty string.
  2130. %t in the heading will be replaced by a time stamp.
  2131. %T will be an active time stamp instead the default inactive one
  2132. %d will be replaced by a short-format time stamp.
  2133. %D will be replaced by an active short-format time stamp.
  2134. %s will be replaced by the new TODO state, in double quotes.
  2135. %S will be replaced by the old TODO state, in double quotes.
  2136. %u will be replaced by the user name.
  2137. %U will be replaced by the full user name.
  2138. In fact, it is not a good idea to change the `state' entry, because
  2139. agenda log mode depends on the format of these entries."
  2140. :group 'org-todo
  2141. :group 'org-progress
  2142. :type '(list :greedy t
  2143. (cons (const :tag "Heading when closing an item" done) string)
  2144. (cons (const :tag
  2145. "Heading when changing todo state (todo sequence only)"
  2146. state) string)
  2147. (cons (const :tag "Heading when just taking a note" note) string)
  2148. (cons (const :tag "Heading when clocking out" clock-out) string)
  2149. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2150. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2151. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2152. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2153. (cons (const :tag "Heading when refiling" refile) string)))
  2154. (unless (assq 'note org-log-note-headings)
  2155. (push '(note . "%t") org-log-note-headings))
  2156. (defcustom org-log-into-drawer nil
  2157. "Non-nil means insert state change notes and time stamps into a drawer.
  2158. When nil, state changes notes will be inserted after the headline and
  2159. any scheduling and clock lines, but not inside a drawer.
  2160. The value of this variable should be the name of the drawer to use.
  2161. LOGBOOK is proposed as the default drawer for this purpose, you can
  2162. also set this to a string to define the drawer of your choice.
  2163. A value of t is also allowed, representing \"LOGBOOK\".
  2164. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2165. will be ignored.
  2166. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2167. a subtree."
  2168. :group 'org-todo
  2169. :group 'org-progress
  2170. :type '(choice
  2171. (const :tag "Not into a drawer" nil)
  2172. (const :tag "LOGBOOK" t)
  2173. (string :tag "Other")))
  2174. (if (fboundp 'defvaralias)
  2175. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2176. (defun org-log-into-drawer ()
  2177. "Return the value of `org-log-into-drawer', but let properties overrule.
  2178. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2179. used instead of the default value."
  2180. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2181. (cond
  2182. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2183. ((equal p "t") "LOGBOOK")
  2184. (t p))))
  2185. (defcustom org-log-state-notes-insert-after-drawers nil
  2186. "Non-nil means insert state change notes after any drawers in entry.
  2187. Only the drawers that *immediately* follow the headline and the
  2188. deadline/scheduled line are skipped.
  2189. When nil, insert notes right after the heading and perhaps the line
  2190. with deadline/scheduling if present.
  2191. This variable will have no effect if `org-log-into-drawer' is
  2192. set."
  2193. :group 'org-todo
  2194. :group 'org-progress
  2195. :type 'boolean)
  2196. (defcustom org-log-states-order-reversed t
  2197. "Non-nil means the latest state note will be directly after heading.
  2198. When nil, the state change notes will be ordered according to time."
  2199. :group 'org-todo
  2200. :group 'org-progress
  2201. :type 'boolean)
  2202. (defcustom org-todo-repeat-to-state nil
  2203. "The TODO state to which a repeater should return the repeating task.
  2204. By default this is the first task in a TODO sequence, or the previous state
  2205. in a TODO_TYP set. But you can specify another task here.
  2206. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2207. :group 'org-todo
  2208. :version "24.1"
  2209. :type '(choice (const :tag "Head of sequence" nil)
  2210. (string :tag "Specific state")))
  2211. (defcustom org-log-repeat 'time
  2212. "Non-nil means record moving through the DONE state when triggering repeat.
  2213. An auto-repeating task is immediately switched back to TODO when
  2214. marked DONE. If you are not logging state changes (by adding \"@\"
  2215. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2216. record a closing note, there will be no record of the task moving
  2217. through DONE. This variable forces taking a note anyway.
  2218. nil Don't force a record
  2219. time Record a time stamp
  2220. note Record a note
  2221. This option can also be set with on a per-file-basis with
  2222. #+STARTUP: logrepeat
  2223. #+STARTUP: lognoterepeat
  2224. #+STARTUP: nologrepeat
  2225. You can have local logging settings for a subtree by setting the LOGGING
  2226. property to one or more of these keywords."
  2227. :group 'org-todo
  2228. :group 'org-progress
  2229. :type '(choice
  2230. (const :tag "Don't force a record" nil)
  2231. (const :tag "Force recording the DONE state" time)
  2232. (const :tag "Force recording a note with the DONE state" note)))
  2233. (defgroup org-priorities nil
  2234. "Priorities in Org-mode."
  2235. :tag "Org Priorities"
  2236. :group 'org-todo)
  2237. (defcustom org-enable-priority-commands t
  2238. "Non-nil means priority commands are active.
  2239. When nil, these commands will be disabled, so that you never accidentally
  2240. set a priority."
  2241. :group 'org-priorities
  2242. :type 'boolean)
  2243. (defcustom org-highest-priority ?A
  2244. "The highest priority of TODO items. A character like ?A, ?B etc.
  2245. Must have a smaller ASCII number than `org-lowest-priority'."
  2246. :group 'org-priorities
  2247. :type 'character)
  2248. (defcustom org-lowest-priority ?C
  2249. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2250. Must have a larger ASCII number than `org-highest-priority'."
  2251. :group 'org-priorities
  2252. :type 'character)
  2253. (defcustom org-default-priority ?B
  2254. "The default priority of TODO items.
  2255. This is the priority an item gets if no explicit priority is given.
  2256. When starting to cycle on an empty priority the first step in the cycle
  2257. depends on `org-priority-start-cycle-with-default'. The resulting first
  2258. step priority must not exceed the range from `org-highest-priority' to
  2259. `org-lowest-priority' which means that `org-default-priority' has to be
  2260. in this range exclusive or inclusive the range boundaries. Else the
  2261. first step refuses to set the default and the second will fall back
  2262. to (depending on the command used) the highest or lowest priority."
  2263. :group 'org-priorities
  2264. :type 'character)
  2265. (defcustom org-priority-start-cycle-with-default t
  2266. "Non-nil means start with default priority when starting to cycle.
  2267. When this is nil, the first step in the cycle will be (depending on the
  2268. command used) one higher or lower than the default priority.
  2269. See also `org-default-priority'."
  2270. :group 'org-priorities
  2271. :type 'boolean)
  2272. (defcustom org-get-priority-function nil
  2273. "Function to extract the priority from a string.
  2274. The string is normally the headline. If this is nil Org computes the
  2275. priority from the priority cookie like [#A] in the headline. It returns
  2276. an integer, increasing by 1000 for each priority level.
  2277. The user can set a different function here, which should take a string
  2278. as an argument and return the numeric priority."
  2279. :group 'org-priorities
  2280. :version "24.1"
  2281. :type 'function)
  2282. (defgroup org-time nil
  2283. "Options concerning time stamps and deadlines in Org-mode."
  2284. :tag "Org Time"
  2285. :group 'org)
  2286. (defcustom org-insert-labeled-timestamps-at-point nil
  2287. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2288. When nil, these labeled time stamps are forces into the second line of an
  2289. entry, just after the headline. When scheduling from the global TODO list,
  2290. the time stamp will always be forced into the second line."
  2291. :group 'org-time
  2292. :type 'boolean)
  2293. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2294. "Formats for `format-time-string' which are used for time stamps.
  2295. It is not recommended to change this constant.")
  2296. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2297. "Number of minutes to round time stamps to.
  2298. These are two values, the first applies when first creating a time stamp.
  2299. The second applies when changing it with the commands `S-up' and `S-down'.
  2300. When changing the time stamp, this means that it will change in steps
  2301. of N minutes, as given by the second value.
  2302. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2303. numbers should be factors of 60, so for example 5, 10, 15.
  2304. When this is larger than 1, you can still force an exact time stamp by using
  2305. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2306. and by using a prefix arg to `S-up/down' to specify the exact number
  2307. of minutes to shift."
  2308. :group 'org-time
  2309. :get #'(lambda (var) ; Make sure both elements are there
  2310. (if (integerp (default-value var))
  2311. (list (default-value var) 5)
  2312. (default-value var)))
  2313. :type '(list
  2314. (integer :tag "when inserting times")
  2315. (integer :tag "when modifying times")))
  2316. ;; Normalize old customizations of this variable.
  2317. (when (integerp org-time-stamp-rounding-minutes)
  2318. (setq org-time-stamp-rounding-minutes
  2319. (list org-time-stamp-rounding-minutes
  2320. org-time-stamp-rounding-minutes)))
  2321. (defcustom org-display-custom-times nil
  2322. "Non-nil means overlay custom formats over all time stamps.
  2323. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2324. To turn this on on a per-file basis, insert anywhere in the file:
  2325. #+STARTUP: customtime"
  2326. :group 'org-time
  2327. :set 'set-default
  2328. :type 'sexp)
  2329. (make-variable-buffer-local 'org-display-custom-times)
  2330. (defcustom org-time-stamp-custom-formats
  2331. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2332. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2333. These are overlaid over the default ISO format if the variable
  2334. `org-display-custom-times' is set. Time like %H:%M should be at the
  2335. end of the second format. The custom formats are also honored by export
  2336. commands, if custom time display is turned on at the time of export."
  2337. :group 'org-time
  2338. :type 'sexp)
  2339. (defun org-time-stamp-format (&optional long inactive)
  2340. "Get the right format for a time string."
  2341. (let ((f (if long (cdr org-time-stamp-formats)
  2342. (car org-time-stamp-formats))))
  2343. (if inactive
  2344. (concat "[" (substring f 1 -1) "]")
  2345. f)))
  2346. (defcustom org-time-clocksum-format "%d:%02d"
  2347. "The format string used when creating CLOCKSUM lines.
  2348. This is also used when org-mode generates a time duration."
  2349. :group 'org-time
  2350. :type 'string)
  2351. (defcustom org-time-clocksum-use-fractional nil
  2352. "If non-nil, \\[org-clock-display] uses fractional times.
  2353. org-mode generates a time duration."
  2354. :group 'org-time
  2355. :type 'boolean)
  2356. (defcustom org-time-clocksum-fractional-format "%.2f"
  2357. "The format string used when creating CLOCKSUM lines, or when
  2358. org-mode generates a time duration."
  2359. :group 'org-time
  2360. :type 'string)
  2361. (defcustom org-deadline-warning-days 14
  2362. "No. of days before expiration during which a deadline becomes active.
  2363. This variable governs the display in sparse trees and in the agenda.
  2364. When 0 or negative, it means use this number (the absolute value of it)
  2365. even if a deadline has a different individual lead time specified.
  2366. Custom commands can set this variable in the options section."
  2367. :group 'org-time
  2368. :group 'org-agenda-daily/weekly
  2369. :type 'integer)
  2370. (defcustom org-read-date-prefer-future t
  2371. "Non-nil means assume future for incomplete date input from user.
  2372. This affects the following situations:
  2373. 1. The user gives a month but not a year.
  2374. For example, if it is April and you enter \"feb 2\", this will be read
  2375. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2376. 2. The user gives a day, but no month.
  2377. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2378. read this as the third of *next* month. However, if you enter \"17\",
  2379. it will be considered as *this* month.
  2380. If you set this variable to the symbol `time', then also the following
  2381. will work:
  2382. 3. If the user gives a time, but no day. If the time is before now,
  2383. to will be interpreted as tomorrow.
  2384. Currently none of this works for ISO week specifications.
  2385. When this option is nil, the current day, month and year will always be
  2386. used as defaults.
  2387. See also `org-agenda-jump-prefer-future'."
  2388. :group 'org-time
  2389. :type '(choice
  2390. (const :tag "Never" nil)
  2391. (const :tag "Check month and day" t)
  2392. (const :tag "Check month, day, and time" time)))
  2393. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2394. "Should the agenda jump command prefer the future for incomplete dates?
  2395. The default is to do the same as configured in `org-read-date-prefer-future'.
  2396. But you can also set a deviating value here.
  2397. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2398. :group 'org-agenda
  2399. :group 'org-time
  2400. :version "24.1"
  2401. :type '(choice
  2402. (const :tag "Use org-read-date-prefer-future"
  2403. org-read-date-prefer-future)
  2404. (const :tag "Never" nil)
  2405. (const :tag "Always" t)))
  2406. (defcustom org-read-date-force-compatible-dates t
  2407. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2408. Depending on the system Emacs is running on, certain dates cannot
  2409. be represented with the type used internally to represent time.
  2410. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2411. correctly. Some systems allow for earlier dates, some for later,
  2412. some for both. One way to find out it to insert any date into an
  2413. Org buffer, putting the cursor on the year and hitting S-up and
  2414. S-down to test the range.
  2415. When this variable is set to t, the date/time prompt will not let
  2416. you specify dates outside the 1970-2037 range, so it is certain that
  2417. these dates will work in whatever version of Emacs you are
  2418. running, and also that you can move a file from one Emacs implementation
  2419. to another. WHenever Org is forcing the year for you, it will display
  2420. a message and beep.
  2421. When this variable is nil, Org will check if the date is
  2422. representable in the specific Emacs implementation you are using.
  2423. If not, it will force a year, usually the current year, and beep
  2424. to remind you. Currently this setting is not recommended because
  2425. the likelihood that you will open your Org files in an Emacs that
  2426. has limited date range is not negligible.
  2427. A workaround for this problem is to use diary sexp dates for time
  2428. stamps outside of this range."
  2429. :group 'org-time
  2430. :version "24.1"
  2431. :type 'boolean)
  2432. (defcustom org-read-date-display-live t
  2433. "Non-nil means display current interpretation of date prompt live.
  2434. This display will be in an overlay, in the minibuffer."
  2435. :group 'org-time
  2436. :type 'boolean)
  2437. (defcustom org-read-date-popup-calendar t
  2438. "Non-nil means pop up a calendar when prompting for a date.
  2439. In the calendar, the date can be selected with mouse-1. However, the
  2440. minibuffer will also be active, and you can simply enter the date as well.
  2441. When nil, only the minibuffer will be available."
  2442. :group 'org-time
  2443. :type 'boolean)
  2444. (if (fboundp 'defvaralias)
  2445. (defvaralias 'org-popup-calendar-for-date-prompt
  2446. 'org-read-date-popup-calendar))
  2447. (defcustom org-read-date-minibuffer-setup-hook nil
  2448. "Hook to be used to set up keys for the date/time interface.
  2449. Add key definitions to `minibuffer-local-map', which will be a temporary
  2450. copy."
  2451. :group 'org-time
  2452. :type 'hook)
  2453. (defcustom org-extend-today-until 0
  2454. "The hour when your day really ends. Must be an integer.
  2455. This has influence for the following applications:
  2456. - When switching the agenda to \"today\". It it is still earlier than
  2457. the time given here, the day recognized as TODAY is actually yesterday.
  2458. - When a date is read from the user and it is still before the time given
  2459. here, the current date and time will be assumed to be yesterday, 23:59.
  2460. Also, timestamps inserted in remember templates follow this rule.
  2461. IMPORTANT: This is a feature whose implementation is and likely will
  2462. remain incomplete. Really, it is only here because past midnight seems to
  2463. be the favorite working time of John Wiegley :-)"
  2464. :group 'org-time
  2465. :type 'integer)
  2466. (defcustom org-use-effective-time nil
  2467. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2468. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2469. \"effective time\" of any timestamps between midnight and 8am will be
  2470. 23:59 of the previous day."
  2471. :group 'org-time
  2472. :version "24.1"
  2473. :type 'boolean)
  2474. (defcustom org-edit-timestamp-down-means-later nil
  2475. "Non-nil means S-down will increase the time in a time stamp.
  2476. When nil, S-up will increase."
  2477. :group 'org-time
  2478. :type 'boolean)
  2479. (defcustom org-calendar-follow-timestamp-change t
  2480. "Non-nil means make the calendar window follow timestamp changes.
  2481. When a timestamp is modified and the calendar window is visible, it will be
  2482. moved to the new date."
  2483. :group 'org-time
  2484. :type 'boolean)
  2485. (defgroup org-tags nil
  2486. "Options concerning tags in Org-mode."
  2487. :tag "Org Tags"
  2488. :group 'org)
  2489. (defcustom org-tag-alist nil
  2490. "List of tags allowed in Org-mode files.
  2491. When this list is nil, Org-mode will base TAG input on what is already in the
  2492. buffer.
  2493. The value of this variable is an alist, the car of each entry must be a
  2494. keyword as a string, the cdr may be a character that is used to select
  2495. that tag through the fast-tag-selection interface.
  2496. See the manual for details."
  2497. :group 'org-tags
  2498. :type '(repeat
  2499. (choice
  2500. (cons (string :tag "Tag name")
  2501. (character :tag "Access char"))
  2502. (list :tag "Start radio group"
  2503. (const :startgroup)
  2504. (option (string :tag "Group description")))
  2505. (list :tag "End radio group"
  2506. (const :endgroup)
  2507. (option (string :tag "Group description")))
  2508. (const :tag "New line" (:newline)))))
  2509. (defcustom org-tag-persistent-alist nil
  2510. "List of tags that will always appear in all Org-mode files.
  2511. This is in addition to any in buffer settings or customizations
  2512. of `org-tag-alist'.
  2513. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2514. The value of this variable is an alist, the car of each entry must be a
  2515. keyword as a string, the cdr may be a character that is used to select
  2516. that tag through the fast-tag-selection interface.
  2517. See the manual for details.
  2518. To disable these tags on a per-file basis, insert anywhere in the file:
  2519. #+STARTUP: noptag"
  2520. :group 'org-tags
  2521. :type '(repeat
  2522. (choice
  2523. (cons (string :tag "Tag name")
  2524. (character :tag "Access char"))
  2525. (const :tag "Start radio group" (:startgroup))
  2526. (const :tag "End radio group" (:endgroup))
  2527. (const :tag "New line" (:newline)))))
  2528. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2529. "If non-nil, always offer completion for all tags of all agenda files.
  2530. Instead of customizing this variable directly, you might want to
  2531. set it locally for capture buffers, because there no list of
  2532. tags in that file can be created dynamically (there are none).
  2533. (add-hook 'org-capture-mode-hook
  2534. (lambda ()
  2535. (set (make-local-variable
  2536. 'org-complete-tags-always-offer-all-agenda-tags)
  2537. t)))"
  2538. :group 'org-tags
  2539. :version "24.1"
  2540. :type 'boolean)
  2541. (defvar org-file-tags nil
  2542. "List of tags that can be inherited by all entries in the file.
  2543. The tags will be inherited if the variable `org-use-tag-inheritance'
  2544. says they should be.
  2545. This variable is populated from #+FILETAGS lines.")
  2546. (defcustom org-use-fast-tag-selection 'auto
  2547. "Non-nil means use fast tag selection scheme.
  2548. This is a special interface to select and deselect tags with single keys.
  2549. When nil, fast selection is never used.
  2550. When the symbol `auto', fast selection is used if and only if selection
  2551. characters for tags have been configured, either through the variable
  2552. `org-tag-alist' or through a #+TAGS line in the buffer.
  2553. When t, fast selection is always used and selection keys are assigned
  2554. automatically if necessary."
  2555. :group 'org-tags
  2556. :type '(choice
  2557. (const :tag "Always" t)
  2558. (const :tag "Never" nil)
  2559. (const :tag "When selection characters are configured" 'auto)))
  2560. (defcustom org-fast-tag-selection-single-key nil
  2561. "Non-nil means fast tag selection exits after first change.
  2562. When nil, you have to press RET to exit it.
  2563. During fast tag selection, you can toggle this flag with `C-c'.
  2564. This variable can also have the value `expert'. In this case, the window
  2565. displaying the tags menu is not even shown, until you press C-c again."
  2566. :group 'org-tags
  2567. :type '(choice
  2568. (const :tag "No" nil)
  2569. (const :tag "Yes" t)
  2570. (const :tag "Expert" expert)))
  2571. (defvar org-fast-tag-selection-include-todo nil
  2572. "Non-nil means fast tags selection interface will also offer TODO states.
  2573. This is an undocumented feature, you should not rely on it.")
  2574. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2575. "The column to which tags should be indented in a headline.
  2576. If this number is positive, it specifies the column. If it is negative,
  2577. it means that the tags should be flushright to that column. For example,
  2578. -80 works well for a normal 80 character screen.
  2579. When 0, place tags directly after headline text, with only one space in
  2580. between."
  2581. :group 'org-tags
  2582. :type 'integer)
  2583. (defcustom org-auto-align-tags t
  2584. "Non-nil keeps tags aligned when modifying headlines.
  2585. Some operations (i.e. demoting) change the length of a headline and
  2586. therefore shift the tags around. With this option turned on, after
  2587. each such operation the tags are again aligned to `org-tags-column'."
  2588. :group 'org-tags
  2589. :type 'boolean)
  2590. (defcustom org-use-tag-inheritance t
  2591. "Non-nil means tags in levels apply also for sublevels.
  2592. When nil, only the tags directly given in a specific line apply there.
  2593. This may also be a list of tags that should be inherited, or a regexp that
  2594. matches tags that should be inherited. Additional control is possible
  2595. with the variable `org-tags-exclude-from-inheritance' which gives an
  2596. explicit list of tags to be excluded from inheritance., even if the value of
  2597. `org-use-tag-inheritance' would select it for inheritance.
  2598. If this option is t, a match early-on in a tree can lead to a large
  2599. number of matches in the subtree when constructing the agenda or creating
  2600. a sparse tree. If you only want to see the first match in a tree during
  2601. a search, check out the variable `org-tags-match-list-sublevels'."
  2602. :group 'org-tags
  2603. :type '(choice
  2604. (const :tag "Not" nil)
  2605. (const :tag "Always" t)
  2606. (repeat :tag "Specific tags" (string :tag "Tag"))
  2607. (regexp :tag "Tags matched by regexp")))
  2608. (defcustom org-tags-exclude-from-inheritance nil
  2609. "List of tags that should never be inherited.
  2610. This is a way to exclude a few tags from inheritance. For way to do
  2611. the opposite, to actively allow inheritance for selected tags,
  2612. see the variable `org-use-tag-inheritance'."
  2613. :group 'org-tags
  2614. :type '(repeat (string :tag "Tag")))
  2615. (defun org-tag-inherit-p (tag)
  2616. "Check if TAG is one that should be inherited."
  2617. (cond
  2618. ((member tag org-tags-exclude-from-inheritance) nil)
  2619. ((eq org-use-tag-inheritance t) t)
  2620. ((not org-use-tag-inheritance) nil)
  2621. ((stringp org-use-tag-inheritance)
  2622. (string-match org-use-tag-inheritance tag))
  2623. ((listp org-use-tag-inheritance)
  2624. (member tag org-use-tag-inheritance))
  2625. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2626. (defcustom org-tags-match-list-sublevels t
  2627. "Non-nil means list also sublevels of headlines matching a search.
  2628. This variable applies to tags/property searches, and also to stuck
  2629. projects because this search is based on a tags match as well.
  2630. When set to the symbol `indented', sublevels are indented with
  2631. leading dots.
  2632. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2633. the sublevels of a headline matching a tag search often also match
  2634. the same search. Listing all of them can create very long lists.
  2635. Setting this variable to nil causes subtrees of a match to be skipped.
  2636. This variable is semi-obsolete and probably should always be true. It
  2637. is better to limit inheritance to certain tags using the variables
  2638. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2639. :group 'org-tags
  2640. :type '(choice
  2641. (const :tag "No, don't list them" nil)
  2642. (const :tag "Yes, do list them" t)
  2643. (const :tag "List them, indented with leading dots" indented)))
  2644. (defcustom org-tags-sort-function nil
  2645. "When set, tags are sorted using this function as a comparator."
  2646. :group 'org-tags
  2647. :type '(choice
  2648. (const :tag "No sorting" nil)
  2649. (const :tag "Alphabetical" string<)
  2650. (const :tag "Reverse alphabetical" string>)
  2651. (function :tag "Custom function" nil)))
  2652. (defvar org-tags-history nil
  2653. "History of minibuffer reads for tags.")
  2654. (defvar org-last-tags-completion-table nil
  2655. "The last used completion table for tags.")
  2656. (defvar org-after-tags-change-hook nil
  2657. "Hook that is run after the tags in a line have changed.")
  2658. (defgroup org-properties nil
  2659. "Options concerning properties in Org-mode."
  2660. :tag "Org Properties"
  2661. :group 'org)
  2662. (defcustom org-property-format "%-10s %s"
  2663. "How property key/value pairs should be formatted by `indent-line'.
  2664. When `indent-line' hits a property definition, it will format the line
  2665. according to this format, mainly to make sure that the values are
  2666. lined-up with respect to each other."
  2667. :group 'org-properties
  2668. :type 'string)
  2669. (defcustom org-properties-postprocess-alist nil
  2670. "Alist of properties and functions to adjust inserted values.
  2671. Elements of this alist must be of the form
  2672. ([string] [function])
  2673. where [string] must be a property name and [function] must be a
  2674. lambda expression: this lambda expression must take one argument,
  2675. the value to adjust, and return the new value as a string.
  2676. For example, this element will allow the property \"Remaining\"
  2677. to be updated wrt the relation between the \"Effort\" property
  2678. and the clock summary:
  2679. ((\"Remaining\" (lambda(value)
  2680. (let ((clocksum (org-clock-sum-current-item))
  2681. (effort (org-duration-string-to-minutes
  2682. (org-entry-get (point) \"Effort\"))))
  2683. (org-minutes-to-hh:mm-string (- effort clocksum))))))"
  2684. :group 'org-properties
  2685. :version "24.1"
  2686. :type '(alist :key-type (string :tag "Property")
  2687. :value-type (function :tag "Function")))
  2688. (defcustom org-use-property-inheritance nil
  2689. "Non-nil means properties apply also for sublevels.
  2690. This setting is chiefly used during property searches. Turning it on can
  2691. cause significant overhead when doing a search, which is why it is not
  2692. on by default.
  2693. When nil, only the properties directly given in the current entry count.
  2694. When t, every property is inherited. The value may also be a list of
  2695. properties that should have inheritance, or a regular expression matching
  2696. properties that should be inherited.
  2697. However, note that some special properties use inheritance under special
  2698. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2699. and the properties ending in \"_ALL\" when they are used as descriptor
  2700. for valid values of a property.
  2701. Note for programmers:
  2702. When querying an entry with `org-entry-get', you can control if inheritance
  2703. should be used. By default, `org-entry-get' looks only at the local
  2704. properties. You can request inheritance by setting the inherit argument
  2705. to t (to force inheritance) or to `selective' (to respect the setting
  2706. in this variable)."
  2707. :group 'org-properties
  2708. :type '(choice
  2709. (const :tag "Not" nil)
  2710. (const :tag "Always" t)
  2711. (repeat :tag "Specific properties" (string :tag "Property"))
  2712. (regexp :tag "Properties matched by regexp")))
  2713. (defun org-property-inherit-p (property)
  2714. "Check if PROPERTY is one that should be inherited."
  2715. (cond
  2716. ((eq org-use-property-inheritance t) t)
  2717. ((not org-use-property-inheritance) nil)
  2718. ((stringp org-use-property-inheritance)
  2719. (string-match org-use-property-inheritance property))
  2720. ((listp org-use-property-inheritance)
  2721. (member property org-use-property-inheritance))
  2722. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2723. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2724. "The default column format, if no other format has been defined.
  2725. This variable can be set on the per-file basis by inserting a line
  2726. #+COLUMNS: %25ITEM ....."
  2727. :group 'org-properties
  2728. :type 'string)
  2729. (defcustom org-columns-ellipses ".."
  2730. "The ellipses to be used when a field in column view is truncated.
  2731. When this is the empty string, as many characters as possible are shown,
  2732. but then there will be no visual indication that the field has been truncated.
  2733. When this is a string of length N, the last N characters of a truncated
  2734. field are replaced by this string. If the column is narrower than the
  2735. ellipses string, only part of the ellipses string will be shown."
  2736. :group 'org-properties
  2737. :type 'string)
  2738. (defcustom org-columns-modify-value-for-display-function nil
  2739. "Function that modifies values for display in column view.
  2740. For example, it can be used to cut out a certain part from a time stamp.
  2741. The function must take 2 arguments:
  2742. column-title The title of the column (*not* the property name)
  2743. value The value that should be modified.
  2744. The function should return the value that should be displayed,
  2745. or nil if the normal value should be used."
  2746. :group 'org-properties
  2747. :type 'function)
  2748. (defcustom org-effort-property "Effort"
  2749. "The property that is being used to keep track of effort estimates.
  2750. Effort estimates given in this property need to have the format H:MM."
  2751. :group 'org-properties
  2752. :group 'org-progress
  2753. :type '(string :tag "Property"))
  2754. (defconst org-global-properties-fixed
  2755. '(("VISIBILITY_ALL" . "folded children content all")
  2756. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2757. "List of property/value pairs that can be inherited by any entry.
  2758. These are fixed values, for the preset properties. The user variable
  2759. that can be used to add to this list is `org-global-properties'.
  2760. The entries in this list are cons cells where the car is a property
  2761. name and cdr is a string with the value. If the value represents
  2762. multiple items like an \"_ALL\" property, separate the items by
  2763. spaces.")
  2764. (defcustom org-global-properties nil
  2765. "List of property/value pairs that can be inherited by any entry.
  2766. This list will be combined with the constant `org-global-properties-fixed'.
  2767. The entries in this list are cons cells where the car is a property
  2768. name and cdr is a string with the value.
  2769. You can set buffer-local values for the same purpose in the variable
  2770. `org-file-properties' this by adding lines like
  2771. #+PROPERTY: NAME VALUE"
  2772. :group 'org-properties
  2773. :type '(repeat
  2774. (cons (string :tag "Property")
  2775. (string :tag "Value"))))
  2776. (defvar org-file-properties nil
  2777. "List of property/value pairs that can be inherited by any entry.
  2778. Valid for the current buffer.
  2779. This variable is populated from #+PROPERTY lines.")
  2780. (make-variable-buffer-local 'org-file-properties)
  2781. (defgroup org-agenda nil
  2782. "Options concerning agenda views in Org-mode."
  2783. :tag "Org Agenda"
  2784. :group 'org)
  2785. (defvar org-category nil
  2786. "Variable used by org files to set a category for agenda display.
  2787. Such files should use a file variable to set it, for example
  2788. # -*- mode: org; org-category: \"ELisp\"
  2789. or contain a special line
  2790. #+CATEGORY: ELisp
  2791. If the file does not specify a category, then file's base name
  2792. is used instead.")
  2793. (make-variable-buffer-local 'org-category)
  2794. (put 'org-category 'safe-local-variable #'(lambda (x) (or (symbolp x) (stringp x))))
  2795. (defcustom org-agenda-files nil
  2796. "The files to be used for agenda display.
  2797. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2798. \\[org-remove-file]. You can also use customize to edit the list.
  2799. If an entry is a directory, all files in that directory that are matched by
  2800. `org-agenda-file-regexp' will be part of the file list.
  2801. If the value of the variable is not a list but a single file name, then
  2802. the list of agenda files is actually stored and maintained in that file, one
  2803. agenda file per line. In this file paths can be given relative to
  2804. `org-directory'. Tilde expansion and environment variable substitution
  2805. are also made."
  2806. :group 'org-agenda
  2807. :type '(choice
  2808. (repeat :tag "List of files and directories" file)
  2809. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2810. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2811. "Regular expression to match files for `org-agenda-files'.
  2812. If any element in the list in that variable contains a directory instead
  2813. of a normal file, all files in that directory that are matched by this
  2814. regular expression will be included."
  2815. :group 'org-agenda
  2816. :type 'regexp)
  2817. (defcustom org-agenda-text-search-extra-files nil
  2818. "List of extra files to be searched by text search commands.
  2819. These files will be search in addition to the agenda files by the
  2820. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2821. Note that these files will only be searched for text search commands,
  2822. not for the other agenda views like todo lists, tag searches or the weekly
  2823. agenda. This variable is intended to list notes and possibly archive files
  2824. that should also be searched by these two commands.
  2825. In fact, if the first element in the list is the symbol `agenda-archives',
  2826. than all archive files of all agenda files will be added to the search
  2827. scope."
  2828. :group 'org-agenda
  2829. :type '(set :greedy t
  2830. (const :tag "Agenda Archives" agenda-archives)
  2831. (repeat :inline t (file))))
  2832. (if (fboundp 'defvaralias)
  2833. (defvaralias 'org-agenda-multi-occur-extra-files
  2834. 'org-agenda-text-search-extra-files))
  2835. (defcustom org-agenda-skip-unavailable-files nil
  2836. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2837. A nil value means to remove them, after a query, from the list."
  2838. :group 'org-agenda
  2839. :type 'boolean)
  2840. (defcustom org-calendar-to-agenda-key [?c]
  2841. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2842. The command `org-calendar-goto-agenda' will be bound to this key. The
  2843. default is the character `c' because then `c' can be used to switch back and
  2844. forth between agenda and calendar."
  2845. :group 'org-agenda
  2846. :type 'sexp)
  2847. (defcustom org-calendar-agenda-action-key [?k]
  2848. "The key to be installed in `calendar-mode-map' for agenda-action.
  2849. The command `org-agenda-action' will be bound to this key. The
  2850. default is the character `k' because we use the same key in the agenda."
  2851. :group 'org-agenda
  2852. :type 'sexp)
  2853. (defcustom org-calendar-insert-diary-entry-key [?i]
  2854. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2855. This option is irrelevant until `org-agenda-diary-file' has been configured
  2856. to point to an Org-mode file. When that is the case, the command
  2857. `org-agenda-diary-entry' will be bound to the key given here, by default
  2858. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2859. if you want to continue doing this, you need to change this to a different
  2860. key."
  2861. :group 'org-agenda
  2862. :type 'sexp)
  2863. (defcustom org-agenda-diary-file 'diary-file
  2864. "File to which to add new entries with the `i' key in agenda and calendar.
  2865. When this is the symbol `diary-file', the functionality in the Emacs
  2866. calendar will be used to add entries to the `diary-file'. But when this
  2867. points to a file, `org-agenda-diary-entry' will be used instead."
  2868. :group 'org-agenda
  2869. :type '(choice
  2870. (const :tag "The standard Emacs diary file" diary-file)
  2871. (file :tag "Special Org file diary entries")))
  2872. (eval-after-load "calendar"
  2873. '(progn
  2874. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2875. 'org-calendar-goto-agenda)
  2876. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2877. 'org-agenda-action)
  2878. (add-hook 'calendar-mode-hook
  2879. (lambda ()
  2880. (unless (eq org-agenda-diary-file 'diary-file)
  2881. (define-key calendar-mode-map
  2882. org-calendar-insert-diary-entry-key
  2883. 'org-agenda-diary-entry))))))
  2884. (defgroup org-latex nil
  2885. "Options for embedding LaTeX code into Org-mode."
  2886. :tag "Org LaTeX"
  2887. :group 'org)
  2888. (defcustom org-format-latex-options
  2889. '(:foreground default :background default :scale 1.0
  2890. :html-foreground "Black" :html-background "Transparent"
  2891. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2892. "Options for creating images from LaTeX fragments.
  2893. This is a property list with the following properties:
  2894. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2895. `default' means use the foreground of the default face.
  2896. :background the background color, or \"Transparent\".
  2897. `default' means use the background of the default face.
  2898. :scale a scaling factor for the size of the images, to get more pixels
  2899. :html-foreground, :html-background, :html-scale
  2900. the same numbers for HTML export.
  2901. :matchers a list indicating which matchers should be used to
  2902. find LaTeX fragments. Valid members of this list are:
  2903. \"begin\" find environments
  2904. \"$1\" find single characters surrounded by $.$
  2905. \"$\" find math expressions surrounded by $...$
  2906. \"$$\" find math expressions surrounded by $$....$$
  2907. \"\\(\" find math expressions surrounded by \\(...\\)
  2908. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2909. :group 'org-latex
  2910. :type 'plist)
  2911. (defcustom org-format-latex-signal-error t
  2912. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2913. When nil, just push out a message."
  2914. :group 'org-latex
  2915. :version "24.1"
  2916. :type 'boolean)
  2917. (defcustom org-latex-to-mathml-jar-file nil
  2918. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  2919. Use this to specify additional executable file say a jar file.
  2920. When using MathToWeb as the converter, specify the full-path to
  2921. your mathtoweb.jar file."
  2922. :group 'org-latex
  2923. :version "24.1"
  2924. :type '(choice
  2925. (const :tag "None" nil)
  2926. (file :tag "JAR file" :must-match t)))
  2927. (defcustom org-latex-to-mathml-convert-command nil
  2928. "Command to convert LaTeX fragments to MathML.
  2929. Replace format-specifiers in the command as noted below and use
  2930. `shell-command' to convert LaTeX to MathML.
  2931. %j: Executable file in fully expanded form as specified by
  2932. `org-latex-to-mathml-jar-file'.
  2933. %I: Input LaTeX file in fully expanded form
  2934. %o: Output MathML file
  2935. This command is used by `org-create-math-formula'.
  2936. When using MathToWeb as the converter, set this to
  2937. \"java -jar %j -unicode -force -df %o %I\"."
  2938. :group 'org-latex
  2939. :version "24.1"
  2940. :type '(choice
  2941. (const :tag "None" nil)
  2942. (string :tag "\nShell command")))
  2943. (defcustom org-latex-create-formula-image-program 'dvipng
  2944. "Program to convert LaTeX fragments with.
  2945. dvipng Process the LaTeX fragments to dvi file, then convert
  2946. dvi files to png files using dvipng.
  2947. This will also include processing of non-math environments.
  2948. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  2949. to convert pdf files to png files"
  2950. :group 'org-latex
  2951. :version "24.1"
  2952. :type '(choice
  2953. (const :tag "dvipng" dvipng)
  2954. (const :tag "imagemagick" imagemagick)))
  2955. (defun org-format-latex-mathml-available-p ()
  2956. "Return t if `org-latex-to-mathml-convert-command' is usable."
  2957. (save-match-data
  2958. (when (and (boundp 'org-latex-to-mathml-convert-command)
  2959. org-latex-to-mathml-convert-command)
  2960. (let ((executable (car (split-string
  2961. org-latex-to-mathml-convert-command))))
  2962. (when (executable-find executable)
  2963. (if (string-match
  2964. "%j" org-latex-to-mathml-convert-command)
  2965. (file-readable-p org-latex-to-mathml-jar-file)
  2966. t))))))
  2967. (defcustom org-format-latex-header "\\documentclass{article}
  2968. \\usepackage[usenames]{color}
  2969. \\usepackage{amsmath}
  2970. \\usepackage[mathscr]{eucal}
  2971. \\pagestyle{empty} % do not remove
  2972. \[PACKAGES]
  2973. \[DEFAULT-PACKAGES]
  2974. % The settings below are copied from fullpage.sty
  2975. \\setlength{\\textwidth}{\\paperwidth}
  2976. \\addtolength{\\textwidth}{-3cm}
  2977. \\setlength{\\oddsidemargin}{1.5cm}
  2978. \\addtolength{\\oddsidemargin}{-2.54cm}
  2979. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2980. \\setlength{\\textheight}{\\paperheight}
  2981. \\addtolength{\\textheight}{-\\headheight}
  2982. \\addtolength{\\textheight}{-\\headsep}
  2983. \\addtolength{\\textheight}{-\\footskip}
  2984. \\addtolength{\\textheight}{-3cm}
  2985. \\setlength{\\topmargin}{1.5cm}
  2986. \\addtolength{\\topmargin}{-2.54cm}"
  2987. "The document header used for processing LaTeX fragments.
  2988. It is imperative that this header make sure that no page number
  2989. appears on the page. The package defined in the variables
  2990. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2991. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2992. will be appended."
  2993. :group 'org-latex
  2994. :type 'string)
  2995. (defvar org-format-latex-header-extra nil)
  2996. (defun org-set-packages-alist (var val)
  2997. "Set the packages alist and make sure it has 3 elements per entry."
  2998. (set var (mapcar (lambda (x)
  2999. (if (and (consp x) (= (length x) 2))
  3000. (list (car x) (nth 1 x) t)
  3001. x))
  3002. val)))
  3003. (defun org-get-packages-alist (var)
  3004. "Get the packages alist and make sure it has 3 elements per entry."
  3005. (mapcar (lambda (x)
  3006. (if (and (consp x) (= (length x) 2))
  3007. (list (car x) (nth 1 x) t)
  3008. x))
  3009. (default-value var)))
  3010. ;; The following variables are defined here because is it also used
  3011. ;; when formatting latex fragments. Originally it was part of the
  3012. ;; LaTeX exporter, which is why the name includes "export".
  3013. (defcustom org-export-latex-default-packages-alist
  3014. '(("AUTO" "inputenc" t)
  3015. ("T1" "fontenc" t)
  3016. ("" "fixltx2e" nil)
  3017. ("" "graphicx" t)
  3018. ("" "longtable" nil)
  3019. ("" "float" nil)
  3020. ("" "wrapfig" nil)
  3021. ("" "soul" t)
  3022. ("" "textcomp" t)
  3023. ("" "marvosym" t)
  3024. ("" "wasysym" t)
  3025. ("" "latexsym" t)
  3026. ("" "amssymb" t)
  3027. ("" "hyperref" nil)
  3028. "\\tolerance=1000"
  3029. )
  3030. "Alist of default packages to be inserted in the header.
  3031. Change this only if one of the packages here causes an incompatibility
  3032. with another package you are using.
  3033. The packages in this list are needed by one part or another of Org-mode
  3034. to function properly.
  3035. - inputenc, fontenc: for basic font and character selection
  3036. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  3037. for interpreting the entities in `org-entities'. You can skip some of these
  3038. packages if you don't use any of the symbols in it.
  3039. - graphicx: for including images
  3040. - float, wrapfig: for figure placement
  3041. - longtable: for long tables
  3042. - hyperref: for cross references
  3043. Therefore you should not modify this variable unless you know what you
  3044. are doing. The one reason to change it anyway is that you might be loading
  3045. some other package that conflicts with one of the default packages.
  3046. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  3047. If SNIPPET-FLAG is t, the package also needs to be included when
  3048. compiling LaTeX snippets into images for inclusion into HTML."
  3049. :group 'org-export-latex
  3050. :set 'org-set-packages-alist
  3051. :get 'org-get-packages-alist
  3052. :version "24.1"
  3053. :type '(repeat
  3054. (choice
  3055. (list :tag "options/package pair"
  3056. (string :tag "options")
  3057. (string :tag "package")
  3058. (boolean :tag "Snippet"))
  3059. (string :tag "A line of LaTeX"))))
  3060. (defcustom org-export-latex-packages-alist nil
  3061. "Alist of packages to be inserted in every LaTeX header.
  3062. These will be inserted after `org-export-latex-default-packages-alist'.
  3063. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  3064. SNIPPET-FLAG, when t, indicates that this package is also needed when
  3065. turning LaTeX snippets into images for inclusion into HTML.
  3066. Make sure that you only list packages here which:
  3067. - you want in every file
  3068. - do not conflict with the default packages in
  3069. `org-export-latex-default-packages-alist'
  3070. - do not conflict with the setup in `org-format-latex-header'."
  3071. :group 'org-export-latex
  3072. :set 'org-set-packages-alist
  3073. :get 'org-get-packages-alist
  3074. :type '(repeat
  3075. (choice
  3076. (list :tag "options/package pair"
  3077. (string :tag "options")
  3078. (string :tag "package")
  3079. (boolean :tag "Snippet"))
  3080. (string :tag "A line of LaTeX"))))
  3081. (defgroup org-appearance nil
  3082. "Settings for Org-mode appearance."
  3083. :tag "Org Appearance"
  3084. :group 'org)
  3085. (defcustom org-level-color-stars-only nil
  3086. "Non-nil means fontify only the stars in each headline.
  3087. When nil, the entire headline is fontified.
  3088. Changing it requires restart of `font-lock-mode' to become effective
  3089. also in regions already fontified."
  3090. :group 'org-appearance
  3091. :type 'boolean)
  3092. (defcustom org-hide-leading-stars nil
  3093. "Non-nil means hide the first N-1 stars in a headline.
  3094. This works by using the face `org-hide' for these stars. This
  3095. face is white for a light background, and black for a dark
  3096. background. You may have to customize the face `org-hide' to
  3097. make this work.
  3098. Changing it requires restart of `font-lock-mode' to become effective
  3099. also in regions already fontified.
  3100. You may also set this on a per-file basis by adding one of the following
  3101. lines to the buffer:
  3102. #+STARTUP: hidestars
  3103. #+STARTUP: showstars"
  3104. :group 'org-appearance
  3105. :type 'boolean)
  3106. (defcustom org-hidden-keywords nil
  3107. "List of symbols corresponding to keywords to be hidden the org buffer.
  3108. For example, a value '(title) for this list will make the document's title
  3109. appear in the buffer without the initial #+TITLE: keyword."
  3110. :group 'org-appearance
  3111. :version "24.1"
  3112. :type '(set (const :tag "#+AUTHOR" author)
  3113. (const :tag "#+DATE" date)
  3114. (const :tag "#+EMAIL" email)
  3115. (const :tag "#+TITLE" title)))
  3116. (defcustom org-fontify-done-headline nil
  3117. "Non-nil means change the face of a headline if it is marked DONE.
  3118. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3119. When this is non-nil, the headline after the keyword is set to the
  3120. `org-headline-done' as an additional indication."
  3121. :group 'org-appearance
  3122. :type 'boolean)
  3123. (defcustom org-fontify-emphasized-text t
  3124. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3125. Changing this variable requires a restart of Emacs to take effect."
  3126. :group 'org-appearance
  3127. :type 'boolean)
  3128. (defcustom org-fontify-whole-heading-line nil
  3129. "Non-nil means fontify the whole line for headings.
  3130. This is useful when setting a background color for the
  3131. org-level-* faces."
  3132. :group 'org-appearance
  3133. :type 'boolean)
  3134. (defcustom org-highlight-latex-fragments-and-specials nil
  3135. "Non-nil means fontify what is treated specially by the exporters."
  3136. :group 'org-appearance
  3137. :type 'boolean)
  3138. (defcustom org-hide-emphasis-markers nil
  3139. "Non-nil mean font-lock should hide the emphasis marker characters."
  3140. :group 'org-appearance
  3141. :type 'boolean)
  3142. (defcustom org-pretty-entities nil
  3143. "Non-nil means show entities as UTF8 characters.
  3144. When nil, the \\name form remains in the buffer."
  3145. :group 'org-appearance
  3146. :version "24.1"
  3147. :type 'boolean)
  3148. (defcustom org-pretty-entities-include-sub-superscripts t
  3149. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3150. :group 'org-appearance
  3151. :version "24.1"
  3152. :type 'boolean)
  3153. (defvar org-emph-re nil
  3154. "Regular expression for matching emphasis.
  3155. After a match, the match groups contain these elements:
  3156. 0 The match of the full regular expression, including the characters
  3157. before and after the proper match
  3158. 1 The character before the proper match, or empty at beginning of line
  3159. 2 The proper match, including the leading and trailing markers
  3160. 3 The leading marker like * or /, indicating the type of highlighting
  3161. 4 The text between the emphasis markers, not including the markers
  3162. 5 The character after the match, empty at the end of a line")
  3163. (defvar org-verbatim-re nil
  3164. "Regular expression for matching verbatim text.")
  3165. (defvar org-emphasis-regexp-components) ; defined just below
  3166. (defvar org-emphasis-alist) ; defined just below
  3167. (defun org-set-emph-re (var val)
  3168. "Set variable and compute the emphasis regular expression."
  3169. (set var val)
  3170. (when (and (boundp 'org-emphasis-alist)
  3171. (boundp 'org-emphasis-regexp-components)
  3172. org-emphasis-alist org-emphasis-regexp-components)
  3173. (let* ((e org-emphasis-regexp-components)
  3174. (pre (car e))
  3175. (post (nth 1 e))
  3176. (border (nth 2 e))
  3177. (body (nth 3 e))
  3178. (nl (nth 4 e))
  3179. (body1 (concat body "*?"))
  3180. (markers (mapconcat 'car org-emphasis-alist ""))
  3181. (vmarkers (mapconcat
  3182. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  3183. org-emphasis-alist "")))
  3184. ;; make sure special characters appear at the right position in the class
  3185. (if (string-match "\\^" markers)
  3186. (setq markers (concat (replace-match "" t t markers) "^")))
  3187. (if (string-match "-" markers)
  3188. (setq markers (concat (replace-match "" t t markers) "-")))
  3189. (if (string-match "\\^" vmarkers)
  3190. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3191. (if (string-match "-" vmarkers)
  3192. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3193. (if (> nl 0)
  3194. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3195. (int-to-string nl) "\\}")))
  3196. ;; Make the regexp
  3197. (setq org-emph-re
  3198. (concat "\\([" pre "]\\|^\\)"
  3199. "\\("
  3200. "\\([" markers "]\\)"
  3201. "\\("
  3202. "[^" border "]\\|"
  3203. "[^" border "]"
  3204. body1
  3205. "[^" border "]"
  3206. "\\)"
  3207. "\\3\\)"
  3208. "\\([" post "]\\|$\\)"))
  3209. (setq org-verbatim-re
  3210. (concat "\\([" pre "]\\|^\\)"
  3211. "\\("
  3212. "\\([" vmarkers "]\\)"
  3213. "\\("
  3214. "[^" border "]\\|"
  3215. "[^" border "]"
  3216. body1
  3217. "[^" border "]"
  3218. "\\)"
  3219. "\\3\\)"
  3220. "\\([" post "]\\|$\\)")))))
  3221. (defcustom org-emphasis-regexp-components
  3222. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3223. "Components used to build the regular expression for emphasis.
  3224. This is a list with five entries. Terminology: In an emphasis string
  3225. like \" *strong word* \", we call the initial space PREMATCH, the final
  3226. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3227. and \"trong wor\" is the body. The different components in this variable
  3228. specify what is allowed/forbidden in each part:
  3229. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3230. post Chars allowed as postmatch. End of line will be allowed too.
  3231. border The chars *forbidden* as border characters.
  3232. body-regexp A regexp like \".\" to match a body character. Don't use
  3233. non-shy groups here, and don't allow newline here.
  3234. newline The maximum number of newlines allowed in an emphasis exp.
  3235. Use customize to modify this, or restart Emacs after changing it."
  3236. :group 'org-appearance
  3237. :set 'org-set-emph-re
  3238. :type '(list
  3239. (sexp :tag "Allowed chars in pre ")
  3240. (sexp :tag "Allowed chars in post ")
  3241. (sexp :tag "Forbidden chars in border ")
  3242. (sexp :tag "Regexp for body ")
  3243. (integer :tag "number of newlines allowed")
  3244. (option (boolean :tag "Please ignore this button"))))
  3245. (defcustom org-emphasis-alist
  3246. `(("*" bold "<b>" "</b>")
  3247. ("/" italic "<i>" "</i>")
  3248. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3249. ("=" org-code "<code>" "</code>" verbatim)
  3250. ("~" org-verbatim "<code>" "</code>" verbatim)
  3251. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3252. "<del>" "</del>")
  3253. )
  3254. "Special syntax for emphasized text.
  3255. Text starting and ending with a special character will be emphasized, for
  3256. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3257. characters, the face to be used by font-lock for highlighting in Org-mode
  3258. Emacs buffers, and the HTML tags to be used for this.
  3259. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3260. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3261. Use customize to modify this, or restart Emacs after changing it."
  3262. :group 'org-appearance
  3263. :set 'org-set-emph-re
  3264. :type '(repeat
  3265. (list
  3266. (string :tag "Marker character")
  3267. (choice
  3268. (face :tag "Font-lock-face")
  3269. (plist :tag "Face property list"))
  3270. (string :tag "HTML start tag")
  3271. (string :tag "HTML end tag")
  3272. (option (const verbatim)))))
  3273. (defvar org-protecting-blocks
  3274. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3275. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3276. This is needed for font-lock setup.")
  3277. ;;; Miscellaneous options
  3278. (defgroup org-completion nil
  3279. "Completion in Org-mode."
  3280. :tag "Org Completion"
  3281. :group 'org)
  3282. (defcustom org-completion-use-ido nil
  3283. "Non-nil means use ido completion wherever possible.
  3284. Note that `ido-mode' must be active for this variable to be relevant.
  3285. If you decide to turn this variable on, you might well want to turn off
  3286. `org-outline-path-complete-in-steps'.
  3287. See also `org-completion-use-iswitchb'."
  3288. :group 'org-completion
  3289. :type 'boolean)
  3290. (defcustom org-completion-use-iswitchb nil
  3291. "Non-nil means use iswitchb completion wherever possible.
  3292. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3293. If you decide to turn this variable on, you might well want to turn off
  3294. `org-outline-path-complete-in-steps'.
  3295. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3296. :group 'org-completion
  3297. :type 'boolean)
  3298. (defcustom org-completion-fallback-command 'hippie-expand
  3299. "The expansion command called by \\[pcomplete] in normal context.
  3300. Normal means, no org-mode-specific context."
  3301. :group 'org-completion
  3302. :type 'function)
  3303. ;;; Functions and variables from their packages
  3304. ;; Declared here to avoid compiler warnings
  3305. ;; XEmacs only
  3306. (defvar outline-mode-menu-heading)
  3307. (defvar outline-mode-menu-show)
  3308. (defvar outline-mode-menu-hide)
  3309. (defvar zmacs-regions) ; XEmacs regions
  3310. ;; Emacs only
  3311. (defvar mark-active)
  3312. ;; Various packages
  3313. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3314. (declare-function calendar-forward-day "cal-move" (arg))
  3315. (declare-function calendar-goto-date "cal-move" (date))
  3316. (declare-function calendar-goto-today "cal-move" ())
  3317. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3318. (defvar calc-embedded-close-formula)
  3319. (defvar calc-embedded-open-formula)
  3320. (declare-function cdlatex-tab "ext:cdlatex" ())
  3321. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3322. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3323. (defvar font-lock-unfontify-region-function)
  3324. (declare-function iswitchb-read-buffer "iswitchb"
  3325. (prompt &optional default require-match start matches-set))
  3326. (defvar iswitchb-temp-buflist)
  3327. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3328. (defvar org-agenda-tags-todo-honor-ignore-options)
  3329. (declare-function org-agenda-skip "org-agenda" ())
  3330. (declare-function
  3331. org-agenda-format-item "org-agenda"
  3332. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3333. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3334. (declare-function org-agenda-change-all-lines "org-agenda"
  3335. (newhead hdmarker &optional fixface just-this))
  3336. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3337. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3338. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3339. (beg end))
  3340. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3341. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3342. "org-agenda" (&optional end))
  3343. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3344. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3345. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3346. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3347. (declare-function org-indent-mode "org-indent" (&optional arg))
  3348. (declare-function parse-time-string "parse-time" (string))
  3349. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3350. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3351. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3352. (defvar remember-data-file)
  3353. (defvar texmathp-why)
  3354. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3355. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3356. (defvar w3m-current-url)
  3357. (defvar w3m-current-title)
  3358. (defvar org-latex-regexps)
  3359. ;;; Autoload and prepare some org modules
  3360. ;; Some table stuff that needs to be defined here, because it is used
  3361. ;; by the functions setting up org-mode or checking for table context.
  3362. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3363. "Detect an org-type or table-type table.")
  3364. (defconst org-table-line-regexp "^[ \t]*|"
  3365. "Detect an org-type table line.")
  3366. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3367. "Detect an org-type table line.")
  3368. (defconst org-table-hline-regexp "^[ \t]*|-"
  3369. "Detect an org-type table hline.")
  3370. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3371. "Detect a table-type table hline.")
  3372. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3373. "Detect the first line outside a table when searching from within it.
  3374. This works for both table types.")
  3375. ;; Autoload the functions in org-table.el that are needed by functions here.
  3376. (eval-and-compile
  3377. (org-autoload "org-table"
  3378. '(org-table-align org-table-begin org-table-blank-field
  3379. org-table-convert org-table-convert-region org-table-copy-down
  3380. org-table-copy-region org-table-create
  3381. org-table-create-or-convert-from-region
  3382. org-table-create-with-table.el org-table-current-dline
  3383. org-table-cut-region org-table-delete-column org-table-edit-field
  3384. org-table-edit-formulas org-table-end org-table-eval-formula
  3385. org-table-export org-table-field-info
  3386. org-table-get-stored-formulas org-table-goto-column
  3387. org-table-hline-and-move org-table-import org-table-insert-column
  3388. org-table-insert-hline org-table-insert-row org-table-iterate
  3389. org-table-justify-field-maybe org-table-kill-row
  3390. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3391. org-table-move-column org-table-move-column-left
  3392. org-table-move-column-right org-table-move-row
  3393. org-table-move-row-down org-table-move-row-up
  3394. org-table-next-field org-table-next-row org-table-paste-rectangle
  3395. org-table-previous-field org-table-recalculate
  3396. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3397. org-table-toggle-coordinate-overlays
  3398. org-table-toggle-formula-debugger org-table-wrap-region
  3399. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3400. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3401. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3402. (defun org-at-table-p (&optional table-type)
  3403. "Return t if the cursor is inside an org-type table.
  3404. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3405. (if org-enable-table-editor
  3406. (save-excursion
  3407. (beginning-of-line 1)
  3408. (looking-at (if table-type org-table-any-line-regexp
  3409. org-table-line-regexp)))
  3410. nil))
  3411. (defsubst org-table-p () (org-at-table-p))
  3412. (defun org-at-table.el-p ()
  3413. "Return t if and only if we are at a table.el table."
  3414. (and (org-at-table-p 'any)
  3415. (save-excursion
  3416. (goto-char (org-table-begin 'any))
  3417. (looking-at org-table1-hline-regexp))))
  3418. (defun org-table-recognize-table.el ()
  3419. "If there is a table.el table nearby, recognize it and move into it."
  3420. (if org-table-tab-recognizes-table.el
  3421. (if (org-at-table.el-p)
  3422. (progn
  3423. (beginning-of-line 1)
  3424. (if (looking-at org-table-dataline-regexp)
  3425. nil
  3426. (if (looking-at org-table1-hline-regexp)
  3427. (progn
  3428. (beginning-of-line 2)
  3429. (if (looking-at org-table-any-border-regexp)
  3430. (beginning-of-line -1)))))
  3431. (if (re-search-forward "|" (org-table-end t) t)
  3432. (progn
  3433. (require 'table)
  3434. (if (table--at-cell-p (point))
  3435. t
  3436. (message "recognizing table.el table...")
  3437. (table-recognize-table)
  3438. (message "recognizing table.el table...done")))
  3439. (error "This should not happen"))
  3440. t)
  3441. nil)
  3442. nil))
  3443. (defun org-at-table-hline-p ()
  3444. "Return t if the cursor is inside a hline in a table."
  3445. (if org-enable-table-editor
  3446. (save-excursion
  3447. (beginning-of-line 1)
  3448. (looking-at org-table-hline-regexp))
  3449. nil))
  3450. (defvar org-table-clean-did-remove-column nil)
  3451. (defun org-table-map-tables (function &optional quietly)
  3452. "Apply FUNCTION to the start of all tables in the buffer."
  3453. (save-excursion
  3454. (save-restriction
  3455. (widen)
  3456. (goto-char (point-min))
  3457. (while (re-search-forward org-table-any-line-regexp nil t)
  3458. (unless quietly
  3459. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3460. (beginning-of-line 1)
  3461. (when (and (looking-at org-table-line-regexp)
  3462. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3463. (not (org-in-block-p '("src" "example"))))
  3464. (save-excursion (funcall function))
  3465. (or (looking-at org-table-line-regexp)
  3466. (forward-char 1)))
  3467. (re-search-forward org-table-any-border-regexp nil 1))))
  3468. (unless quietly (message "Mapping tables: done")))
  3469. ;; Declare and autoload functions from org-exp.el & Co
  3470. (declare-function org-default-export-plist "org-exp")
  3471. (declare-function org-infile-export-plist "org-exp")
  3472. (declare-function org-get-current-options "org-exp")
  3473. (eval-and-compile
  3474. (org-autoload "org-exp"
  3475. '(org-export org-export-visible
  3476. org-insert-export-options-template
  3477. org-table-clean-before-export))
  3478. (org-autoload "org-ascii"
  3479. '(org-export-as-ascii org-export-ascii-preprocess
  3480. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3481. org-export-region-as-ascii))
  3482. (org-autoload "org-latex"
  3483. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3484. org-replace-region-by-latex org-export-region-as-latex
  3485. org-export-as-latex org-export-as-pdf
  3486. org-export-as-pdf-and-open))
  3487. (org-autoload "org-html"
  3488. '(org-export-as-html-and-open
  3489. org-export-as-html-batch org-export-as-html-to-buffer
  3490. org-replace-region-by-html org-export-region-as-html
  3491. org-export-as-html))
  3492. (org-autoload "org-docbook"
  3493. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3494. org-replace-region-by-docbook org-export-region-as-docbook
  3495. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3496. org-export-as-docbook))
  3497. (org-autoload "org-icalendar"
  3498. '(org-export-icalendar-this-file
  3499. org-export-icalendar-all-agenda-files
  3500. org-export-icalendar-combine-agenda-files))
  3501. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3502. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3503. ;; Declare and autoload functions from org-agenda.el
  3504. (eval-and-compile
  3505. (org-autoload "org-agenda"
  3506. '(org-agenda org-agenda-list org-search-view
  3507. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3508. org-diary org-agenda-to-appt
  3509. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3510. ;; Autoload org-remember
  3511. (eval-and-compile
  3512. (org-autoload "org-remember"
  3513. '(org-remember-insinuate org-remember-annotation
  3514. org-remember-apply-template org-remember org-remember-handler)))
  3515. (eval-and-compile
  3516. (org-autoload "org-capture"
  3517. '(org-capture org-capture-insert-template-here
  3518. org-capture-import-remember-templates)))
  3519. ;; Autoload org-clock.el
  3520. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3521. (beg end))
  3522. (declare-function org-clock-update-mode-line "org-clock" ())
  3523. (declare-function org-resolve-clocks "org-clock"
  3524. (&optional also-non-dangling-p prompt last-valid))
  3525. (defvar org-clock-start-time)
  3526. (defvar org-clock-marker (make-marker)
  3527. "Marker recording the last clock-in.")
  3528. (defvar org-clock-hd-marker (make-marker)
  3529. "Marker recording the last clock-in, but the headline position.")
  3530. (defvar org-clock-heading ""
  3531. "The heading of the current clock entry.")
  3532. (defun org-clock-is-active ()
  3533. "Return non-nil if clock is currently running.
  3534. The return value is actually the clock marker."
  3535. (marker-buffer org-clock-marker))
  3536. (eval-and-compile
  3537. (org-autoload
  3538. "org-clock"
  3539. '(org-clock-in org-clock-out org-clock-cancel
  3540. org-clock-goto org-clock-sum org-clock-display
  3541. org-clock-remove-overlays org-clock-report
  3542. org-clocktable-shift org-dblock-write:clocktable
  3543. org-get-clocktable org-resolve-clocks)))
  3544. (defun org-clock-update-time-maybe ()
  3545. "If this is a CLOCK line, update it and return t.
  3546. Otherwise, return nil."
  3547. (interactive)
  3548. (save-excursion
  3549. (beginning-of-line 1)
  3550. (skip-chars-forward " \t")
  3551. (when (looking-at org-clock-string)
  3552. (let ((re (concat "[ \t]*" org-clock-string
  3553. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3554. "\\([ \t]*=>.*\\)?\\)?"))
  3555. ts te h m s neg)
  3556. (cond
  3557. ((not (looking-at re))
  3558. nil)
  3559. ((not (match-end 2))
  3560. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3561. (> org-clock-marker (point))
  3562. (<= org-clock-marker (point-at-eol)))
  3563. ;; The clock is running here
  3564. (setq org-clock-start-time
  3565. (apply 'encode-time
  3566. (org-parse-time-string (match-string 1))))
  3567. (org-clock-update-mode-line)))
  3568. (t
  3569. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3570. (end-of-line 1)
  3571. (setq ts (match-string 1)
  3572. te (match-string 3))
  3573. (setq s (- (org-float-time
  3574. (apply 'encode-time (org-parse-time-string te)))
  3575. (org-float-time
  3576. (apply 'encode-time (org-parse-time-string ts))))
  3577. neg (< s 0)
  3578. s (abs s)
  3579. h (floor (/ s 3600))
  3580. s (- s (* 3600 h))
  3581. m (floor (/ s 60))
  3582. s (- s (* 60 s)))
  3583. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3584. t))))))
  3585. (defun org-check-running-clock ()
  3586. "Check if the current buffer contains the running clock.
  3587. If yes, offer to stop it and to save the buffer with the changes."
  3588. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3589. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3590. (buffer-name))))
  3591. (org-clock-out)
  3592. (when (y-or-n-p "Save changed buffer?")
  3593. (save-buffer))))
  3594. (defun org-clocktable-try-shift (dir n)
  3595. "Check if this line starts a clock table, if yes, shift the time block."
  3596. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3597. (org-clocktable-shift dir n)))
  3598. ;; Autoload org-timer.el
  3599. (eval-and-compile
  3600. (org-autoload
  3601. "org-timer"
  3602. '(org-timer-start org-timer org-timer-item
  3603. org-timer-change-times-in-region
  3604. org-timer-set-timer
  3605. org-timer-reset-timers
  3606. org-timer-show-remaining-time)))
  3607. ;; Autoload org-feed.el
  3608. (eval-and-compile
  3609. (org-autoload
  3610. "org-feed"
  3611. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3612. ;; Autoload org-indent.el
  3613. ;; Define the variable already here, to make sure we have it.
  3614. (defvar org-indent-mode nil
  3615. "Non-nil if Org-Indent mode is enabled.
  3616. Use the command `org-indent-mode' to change this variable.")
  3617. (eval-and-compile
  3618. (org-autoload
  3619. "org-indent"
  3620. '(org-indent-mode)))
  3621. ;; Autoload org-mobile.el
  3622. (eval-and-compile
  3623. (org-autoload
  3624. "org-mobile"
  3625. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3626. ;; Autoload archiving code
  3627. ;; The stuff that is needed for cycling and tags has to be defined here.
  3628. (defgroup org-archive nil
  3629. "Options concerning archiving in Org-mode."
  3630. :tag "Org Archive"
  3631. :group 'org-structure)
  3632. (defcustom org-archive-location "%s_archive::"
  3633. "The location where subtrees should be archived.
  3634. The value of this variable is a string, consisting of two parts,
  3635. separated by a double-colon. The first part is a filename and
  3636. the second part is a headline.
  3637. When the filename is omitted, archiving happens in the same file.
  3638. %s in the filename will be replaced by the current file
  3639. name (without the directory part). Archiving to a different file
  3640. is useful to keep archived entries from contributing to the
  3641. Org-mode Agenda.
  3642. The archived entries will be filed as subtrees of the specified
  3643. headline. When the headline is omitted, the subtrees are simply
  3644. filed away at the end of the file, as top-level entries. Also in
  3645. the heading you can use %s to represent the file name, this can be
  3646. useful when using the same archive for a number of different files.
  3647. Here are a few examples:
  3648. \"%s_archive::\"
  3649. If the current file is Projects.org, archive in file
  3650. Projects.org_archive, as top-level trees. This is the default.
  3651. \"::* Archived Tasks\"
  3652. Archive in the current file, under the top-level headline
  3653. \"* Archived Tasks\".
  3654. \"~/org/archive.org::\"
  3655. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3656. \"~/org/archive.org::* From %s\"
  3657. Archive in file ~/org/archive.org (absolute path), under headlines
  3658. \"From FILENAME\" where file name is the current file name.
  3659. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3660. The \"datetree/\" string is special, signifying to archive
  3661. items to the datetree. Items are placed in either the CLOSED
  3662. date of the item, or the current date if there is no CLOSED date.
  3663. The heading will be a subentry to the current date. There doesn't
  3664. need to be a heading, but there always needs to be a slash after
  3665. datetree. For example, to store archived items directly in the
  3666. datetree, use \"~/org/datetree.org::datetree/\".
  3667. \"basement::** Finished Tasks\"
  3668. Archive in file ./basement (relative path), as level 3 trees
  3669. below the level 2 heading \"** Finished Tasks\".
  3670. You may set this option on a per-file basis by adding to the buffer a
  3671. line like
  3672. #+ARCHIVE: basement::** Finished Tasks
  3673. You may also define it locally for a subtree by setting an ARCHIVE property
  3674. in the entry. If such a property is found in an entry, or anywhere up
  3675. the hierarchy, it will be used."
  3676. :group 'org-archive
  3677. :type 'string)
  3678. (defcustom org-archive-tag "ARCHIVE"
  3679. "The tag that marks a subtree as archived.
  3680. An archived subtree does not open during visibility cycling, and does
  3681. not contribute to the agenda listings.
  3682. After changing this, font-lock must be restarted in the relevant buffers to
  3683. get the proper fontification."
  3684. :group 'org-archive
  3685. :group 'org-keywords
  3686. :type 'string)
  3687. (defcustom org-agenda-skip-archived-trees t
  3688. "Non-nil means the agenda will skip any items located in archived trees.
  3689. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3690. variable is no longer recommended, you should leave it at the value t.
  3691. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3692. :group 'org-archive
  3693. :group 'org-agenda-skip
  3694. :type 'boolean)
  3695. (defcustom org-columns-skip-archived-trees t
  3696. "Non-nil means ignore archived trees when creating column view."
  3697. :group 'org-archive
  3698. :group 'org-properties
  3699. :type 'boolean)
  3700. (defcustom org-cycle-open-archived-trees nil
  3701. "Non-nil means `org-cycle' will open archived trees.
  3702. An archived tree is a tree marked with the tag ARCHIVE.
  3703. When nil, archived trees will stay folded. You can still open them with
  3704. normal outline commands like `show-all', but not with the cycling commands."
  3705. :group 'org-archive
  3706. :group 'org-cycle
  3707. :type 'boolean)
  3708. (defcustom org-sparse-tree-open-archived-trees nil
  3709. "Non-nil means sparse tree construction shows matches in archived trees.
  3710. When nil, matches in these trees are highlighted, but the trees are kept in
  3711. collapsed state."
  3712. :group 'org-archive
  3713. :group 'org-sparse-trees
  3714. :type 'boolean)
  3715. (defun org-cycle-hide-archived-subtrees (state)
  3716. "Re-hide all archived subtrees after a visibility state change."
  3717. (when (and (not org-cycle-open-archived-trees)
  3718. (not (memq state '(overview folded))))
  3719. (save-excursion
  3720. (let* ((globalp (memq state '(contents all)))
  3721. (beg (if globalp (point-min) (point)))
  3722. (end (if globalp (point-max) (org-end-of-subtree t))))
  3723. (org-hide-archived-subtrees beg end)
  3724. (goto-char beg)
  3725. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3726. (message "%s" (substitute-command-keys
  3727. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3728. (defun org-force-cycle-archived ()
  3729. "Cycle subtree even if it is archived."
  3730. (interactive)
  3731. (setq this-command 'org-cycle)
  3732. (let ((org-cycle-open-archived-trees t))
  3733. (call-interactively 'org-cycle)))
  3734. (defun org-hide-archived-subtrees (beg end)
  3735. "Re-hide all archived subtrees after a visibility state change."
  3736. (save-excursion
  3737. (let* ((re (concat ":" org-archive-tag ":")))
  3738. (goto-char beg)
  3739. (while (re-search-forward re end t)
  3740. (when (org-at-heading-p)
  3741. (org-flag-subtree t)
  3742. (org-end-of-subtree t))))))
  3743. (defun org-flag-subtree (flag)
  3744. (save-excursion
  3745. (org-back-to-heading t)
  3746. (outline-end-of-heading)
  3747. (outline-flag-region (point)
  3748. (progn (org-end-of-subtree t) (point))
  3749. flag)))
  3750. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3751. (eval-and-compile
  3752. (org-autoload "org-archive"
  3753. '(org-add-archive-files org-archive-subtree
  3754. org-archive-to-archive-sibling org-toggle-archive-tag
  3755. org-archive-subtree-default
  3756. org-archive-subtree-default-with-confirmation)))
  3757. ;; Autoload Column View Code
  3758. (declare-function org-columns-number-to-string "org-colview")
  3759. (declare-function org-columns-get-format-and-top-level "org-colview")
  3760. (declare-function org-columns-compute "org-colview")
  3761. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3762. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3763. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3764. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3765. ;; Autoload ID code
  3766. (declare-function org-id-store-link "org-id")
  3767. (declare-function org-id-locations-load "org-id")
  3768. (declare-function org-id-locations-save "org-id")
  3769. (defvar org-id-track-globally)
  3770. (org-autoload "org-id"
  3771. '(org-id-get-create org-id-new org-id-copy org-id-get
  3772. org-id-get-with-outline-path-completion
  3773. org-id-get-with-outline-drilling org-id-store-link
  3774. org-id-goto org-id-find org-id-store-link))
  3775. ;; Autoload Plotting Code
  3776. (org-autoload "org-plot"
  3777. '(org-plot/gnuplot))
  3778. ;;; Variables for pre-computed regular expressions, all buffer local
  3779. (defvar org-drawer-regexp nil
  3780. "Matches first line of a hidden block.")
  3781. (make-variable-buffer-local 'org-drawer-regexp)
  3782. (defvar org-todo-regexp nil
  3783. "Matches any of the TODO state keywords.")
  3784. (make-variable-buffer-local 'org-todo-regexp)
  3785. (defvar org-not-done-regexp nil
  3786. "Matches any of the TODO state keywords except the last one.")
  3787. (make-variable-buffer-local 'org-not-done-regexp)
  3788. (defvar org-not-done-heading-regexp nil
  3789. "Matches a TODO headline that is not done.")
  3790. (make-variable-buffer-local 'org-not-done-regexp)
  3791. (defvar org-todo-line-regexp nil
  3792. "Matches a headline and puts TODO state into group 2 if present.")
  3793. (make-variable-buffer-local 'org-todo-line-regexp)
  3794. (defvar org-complex-heading-regexp nil
  3795. "Matches a headline and puts everything into groups:
  3796. group 1: the stars
  3797. group 2: The todo keyword, maybe
  3798. group 3: Priority cookie
  3799. group 4: True headline
  3800. group 5: Tags")
  3801. (make-variable-buffer-local 'org-complex-heading-regexp)
  3802. (defvar org-complex-heading-regexp-format nil
  3803. "Printf format to make regexp to match an exact headline.
  3804. This regexp will match the headline of any node which has the
  3805. exact headline text that is put into the format, but may have any
  3806. TODO state, priority and tags.")
  3807. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3808. (defvar org-todo-line-tags-regexp nil
  3809. "Matches a headline and puts TODO state into group 2 if present.
  3810. Also put tags into group 4 if tags are present.")
  3811. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3812. (defvar org-ds-keyword-length 12
  3813. "Maximum length of the Deadline and SCHEDULED keywords.")
  3814. (make-variable-buffer-local 'org-ds-keyword-length)
  3815. (defvar org-deadline-regexp nil
  3816. "Matches the DEADLINE keyword.")
  3817. (make-variable-buffer-local 'org-deadline-regexp)
  3818. (defvar org-deadline-time-regexp nil
  3819. "Matches the DEADLINE keyword together with a time stamp.")
  3820. (make-variable-buffer-local 'org-deadline-time-regexp)
  3821. (defvar org-deadline-line-regexp nil
  3822. "Matches the DEADLINE keyword and the rest of the line.")
  3823. (make-variable-buffer-local 'org-deadline-line-regexp)
  3824. (defvar org-scheduled-regexp nil
  3825. "Matches the SCHEDULED keyword.")
  3826. (make-variable-buffer-local 'org-scheduled-regexp)
  3827. (defvar org-scheduled-time-regexp nil
  3828. "Matches the SCHEDULED keyword together with a time stamp.")
  3829. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3830. (defvar org-closed-time-regexp nil
  3831. "Matches the CLOSED keyword together with a time stamp.")
  3832. (make-variable-buffer-local 'org-closed-time-regexp)
  3833. (defvar org-keyword-time-regexp nil
  3834. "Matches any of the 4 keywords, together with the time stamp.")
  3835. (make-variable-buffer-local 'org-keyword-time-regexp)
  3836. (defvar org-keyword-time-not-clock-regexp nil
  3837. "Matches any of the 3 keywords, together with the time stamp.")
  3838. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3839. (defvar org-maybe-keyword-time-regexp nil
  3840. "Matches a timestamp, possibly preceded by a keyword.")
  3841. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3842. (defvar org-planning-or-clock-line-re nil
  3843. "Matches a line with planning or clock info.")
  3844. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3845. (defvar org-all-time-keywords nil
  3846. "List of time keywords.")
  3847. (make-variable-buffer-local 'org-all-time-keywords)
  3848. (defconst org-plain-time-of-day-regexp
  3849. (concat
  3850. "\\(\\<[012]?[0-9]"
  3851. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3852. "\\(--?"
  3853. "\\(\\<[012]?[0-9]"
  3854. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3855. "\\)?")
  3856. "Regular expression to match a plain time or time range.
  3857. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3858. groups carry important information:
  3859. 0 the full match
  3860. 1 the first time, range or not
  3861. 8 the second time, if it is a range.")
  3862. (defconst org-plain-time-extension-regexp
  3863. (concat
  3864. "\\(\\<[012]?[0-9]"
  3865. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3866. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3867. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3868. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3869. groups carry important information:
  3870. 0 the full match
  3871. 7 hours of duration
  3872. 9 minutes of duration")
  3873. (defconst org-stamp-time-of-day-regexp
  3874. (concat
  3875. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3876. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3877. "\\(--?"
  3878. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3879. "Regular expression to match a timestamp time or time range.
  3880. After a match, the following groups carry important information:
  3881. 0 the full match
  3882. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3883. 2 the first time, range or not
  3884. 4 the second time, if it is a range.")
  3885. (defconst org-startup-options
  3886. '(("fold" org-startup-folded t)
  3887. ("overview" org-startup-folded t)
  3888. ("nofold" org-startup-folded nil)
  3889. ("showall" org-startup-folded nil)
  3890. ("showeverything" org-startup-folded showeverything)
  3891. ("content" org-startup-folded content)
  3892. ("indent" org-startup-indented t)
  3893. ("noindent" org-startup-indented nil)
  3894. ("hidestars" org-hide-leading-stars t)
  3895. ("showstars" org-hide-leading-stars nil)
  3896. ("odd" org-odd-levels-only t)
  3897. ("oddeven" org-odd-levels-only nil)
  3898. ("align" org-startup-align-all-tables t)
  3899. ("noalign" org-startup-align-all-tables nil)
  3900. ("inlineimages" org-startup-with-inline-images t)
  3901. ("noinlineimages" org-startup-with-inline-images nil)
  3902. ("customtime" org-display-custom-times t)
  3903. ("logdone" org-log-done time)
  3904. ("lognotedone" org-log-done note)
  3905. ("nologdone" org-log-done nil)
  3906. ("lognoteclock-out" org-log-note-clock-out t)
  3907. ("nolognoteclock-out" org-log-note-clock-out nil)
  3908. ("logrepeat" org-log-repeat state)
  3909. ("lognoterepeat" org-log-repeat note)
  3910. ("nologrepeat" org-log-repeat nil)
  3911. ("logreschedule" org-log-reschedule time)
  3912. ("lognotereschedule" org-log-reschedule note)
  3913. ("nologreschedule" org-log-reschedule nil)
  3914. ("logredeadline" org-log-redeadline time)
  3915. ("lognoteredeadline" org-log-redeadline note)
  3916. ("nologredeadline" org-log-redeadline nil)
  3917. ("logrefile" org-log-refile time)
  3918. ("lognoterefile" org-log-refile note)
  3919. ("nologrefile" org-log-refile nil)
  3920. ("fninline" org-footnote-define-inline t)
  3921. ("nofninline" org-footnote-define-inline nil)
  3922. ("fnlocal" org-footnote-section nil)
  3923. ("fnauto" org-footnote-auto-label t)
  3924. ("fnprompt" org-footnote-auto-label nil)
  3925. ("fnconfirm" org-footnote-auto-label confirm)
  3926. ("fnplain" org-footnote-auto-label plain)
  3927. ("fnadjust" org-footnote-auto-adjust t)
  3928. ("nofnadjust" org-footnote-auto-adjust nil)
  3929. ("constcgs" constants-unit-system cgs)
  3930. ("constSI" constants-unit-system SI)
  3931. ("noptag" org-tag-persistent-alist nil)
  3932. ("hideblocks" org-hide-block-startup t)
  3933. ("nohideblocks" org-hide-block-startup nil)
  3934. ("beamer" org-startup-with-beamer-mode t)
  3935. ("entitiespretty" org-pretty-entities t)
  3936. ("entitiesplain" org-pretty-entities nil))
  3937. "Variable associated with STARTUP options for org-mode.
  3938. Each element is a list of three items: the startup options (as written
  3939. in the #+STARTUP line), the corresponding variable, and the value to set
  3940. this variable to if the option is found. An optional forth element PUSH
  3941. means to push this value onto the list in the variable.")
  3942. (defun org-update-property-plist (key val props)
  3943. "Update PROPS with KEY and VAL."
  3944. (let* ((appending (string= "+" (substring key (- (length key) 1))))
  3945. (key (if appending (substring key 0 (- (length key) 1)) key))
  3946. (remainder (org-remove-if (lambda (p) (string= (car p) key)) props))
  3947. (previous (cdr (assoc key props))))
  3948. (if appending
  3949. (cons (cons key (if previous (concat previous " " val) val)) remainder)
  3950. (cons (cons key val) remainder))))
  3951. (defconst org-block-regexp
  3952. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  3953. "Regular expression for hiding blocks.")
  3954. (defconst org-heading-keyword-regexp-format
  3955. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3956. "Printf format for a regexp matching an headline with some keyword.
  3957. This regexp will match the headline of any node which has the
  3958. exact keyword that is put into the format. The keyword isn't in
  3959. any group by default, but the stars and the body are.")
  3960. (defconst org-heading-keyword-maybe-regexp-format
  3961. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  3962. "Printf format for a regexp matching an headline, possibly with some keyword.
  3963. This regexp can match any headline with the specified keyword, or
  3964. without a keyword. The keyword isn't in any group by default,
  3965. but the stars and the body are.")
  3966. (defun org-set-regexps-and-options ()
  3967. "Precompute regular expressions for current buffer."
  3968. (when (derived-mode-p 'org-mode)
  3969. (org-set-local 'org-todo-kwd-alist nil)
  3970. (org-set-local 'org-todo-key-alist nil)
  3971. (org-set-local 'org-todo-key-trigger nil)
  3972. (org-set-local 'org-todo-keywords-1 nil)
  3973. (org-set-local 'org-done-keywords nil)
  3974. (org-set-local 'org-todo-heads nil)
  3975. (org-set-local 'org-todo-sets nil)
  3976. (org-set-local 'org-todo-log-states nil)
  3977. (org-set-local 'org-file-properties nil)
  3978. (org-set-local 'org-file-tags nil)
  3979. (let ((re (org-make-options-regexp
  3980. '("CATEGORY" "TODO" "COLUMNS"
  3981. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3982. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3983. "OPTIONS")
  3984. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3985. (splitre "[ \t]+")
  3986. (scripts org-use-sub-superscripts)
  3987. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3988. tail sep kws1 prio props ftags drawers beamer-p
  3989. ext-setup-or-nil setup-contents (start 0))
  3990. (save-excursion
  3991. (save-restriction
  3992. (widen)
  3993. (goto-char (point-min))
  3994. (while (or (and ext-setup-or-nil
  3995. (string-match re ext-setup-or-nil start)
  3996. (setq start (match-end 0)))
  3997. (and (setq ext-setup-or-nil nil start 0)
  3998. (re-search-forward re nil t)))
  3999. (setq key (upcase (match-string 1 ext-setup-or-nil))
  4000. value (org-match-string-no-properties 2 ext-setup-or-nil))
  4001. (if (stringp value) (setq value (org-trim value)))
  4002. (cond
  4003. ((equal key "CATEGORY")
  4004. (setq cat value))
  4005. ((member key '("SEQ_TODO" "TODO"))
  4006. (push (cons 'sequence (org-split-string value splitre)) kwds))
  4007. ((equal key "TYP_TODO")
  4008. (push (cons 'type (org-split-string value splitre)) kwds))
  4009. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  4010. ;; general TODO-like setup
  4011. (push (cons (intern (downcase (match-string 1 key)))
  4012. (org-split-string value splitre)) kwds))
  4013. ((equal key "TAGS")
  4014. (setq tags (append tags (if tags '("\\n") nil)
  4015. (org-split-string value splitre))))
  4016. ((equal key "COLUMNS")
  4017. (org-set-local 'org-columns-default-format value))
  4018. ((equal key "LINK")
  4019. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4020. (push (cons (match-string 1 value)
  4021. (org-trim (match-string 2 value)))
  4022. links)))
  4023. ((equal key "PRIORITIES")
  4024. (setq prio (org-split-string value " +")))
  4025. ((equal key "PROPERTY")
  4026. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4027. (setq props (org-update-property-plist (match-string 1 value)
  4028. (match-string 2 value)
  4029. props))))
  4030. ((equal key "FILETAGS")
  4031. (when (string-match "\\S-" value)
  4032. (setq ftags
  4033. (append
  4034. ftags
  4035. (apply 'append
  4036. (mapcar (lambda (x) (org-split-string x ":"))
  4037. (org-split-string value)))))))
  4038. ((equal key "DRAWERS")
  4039. (setq drawers (delete-dups (append org-drawers (org-split-string value splitre)))))
  4040. ((equal key "CONSTANTS")
  4041. (setq const (append const (org-split-string value splitre))))
  4042. ((equal key "STARTUP")
  4043. (let ((opts (org-split-string value splitre))
  4044. l var val)
  4045. (while (setq l (pop opts))
  4046. (when (setq l (assoc l org-startup-options))
  4047. (setq var (nth 1 l) val (nth 2 l))
  4048. (if (not (nth 3 l))
  4049. (set (make-local-variable var) val)
  4050. (if (not (listp (symbol-value var)))
  4051. (set (make-local-variable var) nil))
  4052. (set (make-local-variable var) (symbol-value var))
  4053. (add-to-list var val))))))
  4054. ((equal key "ARCHIVE")
  4055. (setq arch value)
  4056. (remove-text-properties 0 (length arch)
  4057. '(face t fontified t) arch))
  4058. ((equal key "LATEX_CLASS")
  4059. (setq beamer-p (equal value "beamer")))
  4060. ((equal key "OPTIONS")
  4061. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  4062. (setq scripts (read (match-string 2 value)))))
  4063. ((equal key "SETUPFILE")
  4064. (setq setup-contents (org-file-contents
  4065. (expand-file-name
  4066. (org-remove-double-quotes value))
  4067. 'noerror))
  4068. (if (not ext-setup-or-nil)
  4069. (setq ext-setup-or-nil setup-contents start 0)
  4070. (setq ext-setup-or-nil
  4071. (concat (substring ext-setup-or-nil 0 start)
  4072. "\n" setup-contents "\n"
  4073. (substring ext-setup-or-nil start)))))))
  4074. ;; search for property blocks
  4075. (goto-char (point-min))
  4076. (while (re-search-forward org-block-regexp nil t)
  4077. (when (equal "PROPERTY" (upcase (match-string 1)))
  4078. (setq value (replace-regexp-in-string
  4079. "[\n\r]" " " (match-string 4)))
  4080. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  4081. (setq props (org-update-property-plist (match-string 1 value)
  4082. (match-string 2 value)
  4083. props)))))))
  4084. (org-set-local 'org-use-sub-superscripts scripts)
  4085. (when cat
  4086. (org-set-local 'org-category (intern cat))
  4087. (push (cons "CATEGORY" cat) props))
  4088. (when prio
  4089. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  4090. (setq prio (mapcar 'string-to-char prio))
  4091. (org-set-local 'org-highest-priority (nth 0 prio))
  4092. (org-set-local 'org-lowest-priority (nth 1 prio))
  4093. (org-set-local 'org-default-priority (nth 2 prio)))
  4094. (and props (org-set-local 'org-file-properties (nreverse props)))
  4095. (and ftags (org-set-local 'org-file-tags
  4096. (mapcar 'org-add-prop-inherited ftags)))
  4097. (and drawers (org-set-local 'org-drawers drawers))
  4098. (and arch (org-set-local 'org-archive-location arch))
  4099. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  4100. ;; Process the TODO keywords
  4101. (unless kwds
  4102. ;; Use the global values as if they had been given locally.
  4103. (setq kwds (default-value 'org-todo-keywords))
  4104. (if (stringp (car kwds))
  4105. (setq kwds (list (cons org-todo-interpretation
  4106. (default-value 'org-todo-keywords)))))
  4107. (setq kwds (reverse kwds)))
  4108. (setq kwds (nreverse kwds))
  4109. (let (inter kws kw)
  4110. (while (setq kws (pop kwds))
  4111. (let ((kws (or
  4112. (run-hook-with-args-until-success
  4113. 'org-todo-setup-filter-hook kws)
  4114. kws)))
  4115. (setq inter (pop kws) sep (member "|" kws)
  4116. kws0 (delete "|" (copy-sequence kws))
  4117. kwsa nil
  4118. kws1 (mapcar
  4119. (lambda (x)
  4120. ;; 1 2
  4121. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  4122. (progn
  4123. (setq kw (match-string 1 x)
  4124. key (and (match-end 2) (match-string 2 x))
  4125. log (org-extract-log-state-settings x))
  4126. (push (cons kw (and key (string-to-char key))) kwsa)
  4127. (and log (push log org-todo-log-states))
  4128. kw)
  4129. (error "Invalid TODO keyword %s" x)))
  4130. kws0)
  4131. kwsa (if kwsa (append '((:startgroup))
  4132. (nreverse kwsa)
  4133. '((:endgroup))))
  4134. hw (car kws1)
  4135. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  4136. tail (list inter hw (car dws) (org-last dws))))
  4137. (add-to-list 'org-todo-heads hw 'append)
  4138. (push kws1 org-todo-sets)
  4139. (setq org-done-keywords (append org-done-keywords dws nil))
  4140. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  4141. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  4142. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  4143. (setq org-todo-sets (nreverse org-todo-sets)
  4144. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4145. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  4146. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  4147. ;; Process the constants
  4148. (when const
  4149. (let (e cst)
  4150. (while (setq e (pop const))
  4151. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  4152. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  4153. (setq org-table-formula-constants-local cst)))
  4154. ;; Process the tags.
  4155. (when tags
  4156. (let (e tgs)
  4157. (while (setq e (pop tags))
  4158. (cond
  4159. ((equal e "{") (push '(:startgroup) tgs))
  4160. ((equal e "}") (push '(:endgroup) tgs))
  4161. ((equal e "\\n") (push '(:newline) tgs))
  4162. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  4163. (push (cons (match-string 1 e)
  4164. (string-to-char (match-string 2 e)))
  4165. tgs))
  4166. (t (push (list e) tgs))))
  4167. (org-set-local 'org-tag-alist nil)
  4168. (while (setq e (pop tgs))
  4169. (or (and (stringp (car e))
  4170. (assoc (car e) org-tag-alist))
  4171. (push e org-tag-alist)))))
  4172. ;; Compute the regular expressions and other local variables.
  4173. ;; Using `org-outline-regexp-bol' would complicate them much,
  4174. ;; because of the fixed white space at the end of that string.
  4175. (if (not org-done-keywords)
  4176. (setq org-done-keywords (and org-todo-keywords-1
  4177. (list (org-last org-todo-keywords-1)))))
  4178. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  4179. (length org-scheduled-string)
  4180. (length org-clock-string)
  4181. (length org-closed-string)))
  4182. org-drawer-regexp
  4183. (concat "^[ \t]*:\\("
  4184. (mapconcat 'regexp-quote org-drawers "\\|")
  4185. "\\):[ \t]*$")
  4186. org-not-done-keywords
  4187. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  4188. org-todo-regexp
  4189. (concat "\\("
  4190. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  4191. "\\)")
  4192. org-not-done-regexp
  4193. (concat "\\("
  4194. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  4195. "\\)")
  4196. org-not-done-heading-regexp
  4197. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4198. org-todo-line-regexp
  4199. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4200. org-complex-heading-regexp
  4201. (concat "^\\(\\*+\\)"
  4202. "\\(?: +" org-todo-regexp "\\)?"
  4203. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4204. "\\(?: +\\(.*?\\)\\)?"
  4205. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4206. "[ \t]*$")
  4207. org-complex-heading-regexp-format
  4208. (concat "^\\(\\*+\\)"
  4209. "\\(?: +" org-todo-regexp "\\)?"
  4210. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4211. "\\(?: +"
  4212. ;; Stats cookies can be stuck to body.
  4213. "\\(?:\\[[0-9%%/]+\\] *\\)?"
  4214. "\\(%s\\)"
  4215. "\\(?: *\\[[0-9%%/]+\\]\\)?"
  4216. "\\)"
  4217. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4218. "[ \t]*$")
  4219. org-todo-line-tags-regexp
  4220. (concat "^\\(\\*+\\)"
  4221. "\\(?: +" org-todo-regexp "\\)?"
  4222. "\\(?: +\\(.*?\\)\\)?"
  4223. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4224. "[ \t]*$")
  4225. org-deadline-regexp (concat "\\<" org-deadline-string)
  4226. org-deadline-time-regexp
  4227. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4228. org-deadline-line-regexp
  4229. (concat "\\<\\(" org-deadline-string "\\).*")
  4230. org-scheduled-regexp
  4231. (concat "\\<" org-scheduled-string)
  4232. org-scheduled-time-regexp
  4233. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4234. org-closed-time-regexp
  4235. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4236. org-keyword-time-regexp
  4237. (concat "\\<\\(" org-scheduled-string
  4238. "\\|" org-deadline-string
  4239. "\\|" org-closed-string
  4240. "\\|" org-clock-string "\\)"
  4241. " *[[<]\\([^]>]+\\)[]>]")
  4242. org-keyword-time-not-clock-regexp
  4243. (concat "\\<\\(" org-scheduled-string
  4244. "\\|" org-deadline-string
  4245. "\\|" org-closed-string
  4246. "\\)"
  4247. " *[[<]\\([^]>]+\\)[]>]")
  4248. org-maybe-keyword-time-regexp
  4249. (concat "\\(\\<\\(" org-scheduled-string
  4250. "\\|" org-deadline-string
  4251. "\\|" org-closed-string
  4252. "\\|" org-clock-string "\\)\\)?"
  4253. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4254. org-planning-or-clock-line-re
  4255. (concat "^[ \t]*\\("
  4256. org-scheduled-string "\\|"
  4257. org-deadline-string "\\|"
  4258. org-closed-string "\\|"
  4259. org-clock-string "\\)")
  4260. org-all-time-keywords
  4261. (mapcar (lambda (w) (substring w 0 -1))
  4262. (list org-scheduled-string org-deadline-string
  4263. org-clock-string org-closed-string))
  4264. )
  4265. (org-compute-latex-and-specials-regexp)
  4266. (org-set-font-lock-defaults))))
  4267. (defun org-file-contents (file &optional noerror)
  4268. "Return the contents of FILE, as a string."
  4269. (if (or (not file)
  4270. (not (file-readable-p file)))
  4271. (if noerror
  4272. (progn
  4273. (message "Cannot read file \"%s\"" file)
  4274. (ding) (sit-for 2)
  4275. "")
  4276. (error "Cannot read file \"%s\"" file))
  4277. (with-temp-buffer
  4278. (insert-file-contents file)
  4279. (buffer-string))))
  4280. (defun org-extract-log-state-settings (x)
  4281. "Extract the log state setting from a TODO keyword string.
  4282. This will extract info from a string like \"WAIT(w@/!)\"."
  4283. (let (kw key log1 log2)
  4284. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4285. (setq kw (match-string 1 x)
  4286. key (and (match-end 2) (match-string 2 x))
  4287. log1 (and (match-end 3) (match-string 3 x))
  4288. log2 (and (match-end 4) (match-string 4 x)))
  4289. (and (or log1 log2)
  4290. (list kw
  4291. (and log1 (if (equal log1 "!") 'time 'note))
  4292. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4293. (defun org-remove-keyword-keys (list)
  4294. "Remove a pair of parenthesis at the end of each string in LIST."
  4295. (mapcar (lambda (x)
  4296. (if (string-match "(.*)$" x)
  4297. (substring x 0 (match-beginning 0))
  4298. x))
  4299. list))
  4300. (defun org-assign-fast-keys (alist)
  4301. "Assign fast keys to a keyword-key alist.
  4302. Respect keys that are already there."
  4303. (let (new e (alt ?0))
  4304. (while (setq e (pop alist))
  4305. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4306. (cdr e)) ;; Key already assigned.
  4307. (push e new)
  4308. (let ((clist (string-to-list (downcase (car e))))
  4309. (used (append new alist)))
  4310. (when (= (car clist) ?@)
  4311. (pop clist))
  4312. (while (and clist (rassoc (car clist) used))
  4313. (pop clist))
  4314. (unless clist
  4315. (while (rassoc alt used)
  4316. (incf alt)))
  4317. (push (cons (car e) (or (car clist) alt)) new))))
  4318. (nreverse new)))
  4319. ;;; Some variables used in various places
  4320. (defvar org-window-configuration nil
  4321. "Used in various places to store a window configuration.")
  4322. (defvar org-selected-window nil
  4323. "Used in various places to store a window configuration.")
  4324. (defvar org-finish-function nil
  4325. "Function to be called when `C-c C-c' is used.
  4326. This is for getting out of special buffers like remember.")
  4327. ;; FIXME: Occasionally check by commenting these, to make sure
  4328. ;; no other functions uses these, forgetting to let-bind them.
  4329. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4330. (defvar org-last-state)
  4331. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4332. ;; Defined somewhere in this file, but used before definition.
  4333. (defvar org-entities) ;; defined in org-entities.el
  4334. (defvar org-struct-menu)
  4335. (defvar org-org-menu)
  4336. (defvar org-tbl-menu)
  4337. ;;;; Define the Org-mode
  4338. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4339. (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"))
  4340. ;; We use a before-change function to check if a table might need
  4341. ;; an update.
  4342. (defvar org-table-may-need-update t
  4343. "Indicates that a table might need an update.
  4344. This variable is set by `org-before-change-function'.
  4345. `org-table-align' sets it back to nil.")
  4346. (defun org-before-change-function (beg end)
  4347. "Every change indicates that a table might need an update."
  4348. (setq org-table-may-need-update t))
  4349. (defvar org-mode-map)
  4350. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4351. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4352. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4353. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4354. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4355. (defvar org-table-buffer-is-an nil)
  4356. (defvar bidi-paragraph-direction)
  4357. (defvar buffer-face-mode-face)
  4358. (require 'outline)
  4359. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4360. ;; Other stuff we need.
  4361. (require 'time-date)
  4362. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4363. (require 'easymenu)
  4364. (require 'overlay)
  4365. (require 'org-macs)
  4366. (require 'org-entities)
  4367. (require 'org-compat)
  4368. (require 'org-faces)
  4369. (require 'org-list)
  4370. (require 'org-pcomplete)
  4371. (require 'org-src)
  4372. (require 'org-footnote)
  4373. (require 'org-element)
  4374. ;; babel
  4375. (require 'ob)
  4376. (require 'ob-table)
  4377. (require 'ob-lob)
  4378. (require 'ob-ref)
  4379. (require 'ob-tangle)
  4380. (require 'ob-comint)
  4381. (require 'ob-keys)
  4382. ;;;###autoload
  4383. (define-derived-mode org-mode outline-mode "Org"
  4384. "Outline-based notes management and organizer, alias
  4385. \"Carsten's outline-mode for keeping track of everything.\"
  4386. Org-mode develops organizational tasks around a NOTES file which
  4387. contains information about projects as plain text. Org-mode is
  4388. implemented on top of outline-mode, which is ideal to keep the content
  4389. of large files well structured. It supports ToDo items, deadlines and
  4390. time stamps, which magically appear in the diary listing of the Emacs
  4391. calendar. Tables are easily created with a built-in table editor.
  4392. Plain text URL-like links connect to websites, emails (VM), Usenet
  4393. messages (Gnus), BBDB entries, and any files related to the project.
  4394. For printing and sharing of notes, an Org-mode file (or a part of it)
  4395. can be exported as a structured ASCII or HTML file.
  4396. The following commands are available:
  4397. \\{org-mode-map}"
  4398. ;; Get rid of Outline menus, they are not needed
  4399. ;; Need to do this here because define-derived-mode sets up
  4400. ;; the keymap so late. Still, it is a waste to call this each time
  4401. ;; we switch another buffer into org-mode.
  4402. (if (featurep 'xemacs)
  4403. (when (boundp 'outline-mode-menu-heading)
  4404. ;; Assume this is Greg's port, it uses easymenu
  4405. (easy-menu-remove outline-mode-menu-heading)
  4406. (easy-menu-remove outline-mode-menu-show)
  4407. (easy-menu-remove outline-mode-menu-hide))
  4408. (define-key org-mode-map [menu-bar headings] 'undefined)
  4409. (define-key org-mode-map [menu-bar hide] 'undefined)
  4410. (define-key org-mode-map [menu-bar show] 'undefined))
  4411. (org-load-modules-maybe)
  4412. (easy-menu-add org-org-menu)
  4413. (easy-menu-add org-tbl-menu)
  4414. (org-install-agenda-files-menu)
  4415. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4416. (add-to-invisibility-spec '(org-cwidth))
  4417. (add-to-invisibility-spec '(org-hide-block . t))
  4418. (when (featurep 'xemacs)
  4419. (org-set-local 'line-move-ignore-invisible t))
  4420. (org-set-local 'outline-regexp org-outline-regexp)
  4421. (org-set-local 'outline-level 'org-outline-level)
  4422. (setq bidi-paragraph-direction 'left-to-right)
  4423. (when (and org-ellipsis
  4424. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4425. (fboundp 'make-glyph-code))
  4426. (unless org-display-table
  4427. (setq org-display-table (make-display-table)))
  4428. (set-display-table-slot
  4429. org-display-table 4
  4430. (vconcat (mapcar
  4431. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4432. org-ellipsis)))
  4433. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4434. (setq buffer-display-table org-display-table))
  4435. (org-set-regexps-and-options)
  4436. (when (and org-tag-faces (not org-tags-special-faces-re))
  4437. ;; tag faces set outside customize.... force initialization.
  4438. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4439. ;; Calc embedded
  4440. (org-set-local 'calc-embedded-open-mode "# ")
  4441. (modify-syntax-entry ?@ "w")
  4442. (if org-startup-truncated (setq truncate-lines t))
  4443. (org-set-local 'font-lock-unfontify-region-function
  4444. 'org-unfontify-region)
  4445. ;; Activate before-change-function
  4446. (org-set-local 'org-table-may-need-update t)
  4447. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4448. 'local)
  4449. ;; Check for running clock before killing a buffer
  4450. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4451. ;; Paragraphs and auto-filling
  4452. (org-set-autofill-regexps)
  4453. (org-set-local 'indent-line-function 'org-indent-line)
  4454. (org-set-local 'indent-region-function 'org-indent-region)
  4455. (org-update-radio-target-regexp)
  4456. ;; Beginning/end of defun
  4457. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4458. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4459. ;; Next error for sparse trees
  4460. (org-set-local 'next-error-function 'org-occur-next-match)
  4461. ;; Make sure dependence stuff works reliably, even for users who set it
  4462. ;; too late :-(
  4463. (if org-enforce-todo-dependencies
  4464. (add-hook 'org-blocker-hook
  4465. 'org-block-todo-from-children-or-siblings-or-parent)
  4466. (remove-hook 'org-blocker-hook
  4467. 'org-block-todo-from-children-or-siblings-or-parent))
  4468. (if org-enforce-todo-checkbox-dependencies
  4469. (add-hook 'org-blocker-hook
  4470. 'org-block-todo-from-checkboxes)
  4471. (remove-hook 'org-blocker-hook
  4472. 'org-block-todo-from-checkboxes))
  4473. ;; Comment characters
  4474. (org-set-local 'comment-start "#")
  4475. (org-set-local 'comment-padding " ")
  4476. ;; Align options lines
  4477. (org-set-local
  4478. 'align-mode-rules-list
  4479. '((org-in-buffer-settings
  4480. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4481. (modes . '(org-mode)))))
  4482. ;; Imenu
  4483. (org-set-local 'imenu-create-index-function
  4484. 'org-imenu-get-tree)
  4485. ;; Make isearch reveal context
  4486. (if (or (featurep 'xemacs)
  4487. (not (boundp 'outline-isearch-open-invisible-function)))
  4488. ;; Emacs 21 and XEmacs make use of the hook
  4489. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4490. ;; Emacs 22 deals with this through a special variable
  4491. (org-set-local 'outline-isearch-open-invisible-function
  4492. (lambda (&rest ignore) (org-show-context 'isearch))))
  4493. ;; Turn on org-beamer-mode?
  4494. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4495. ;; Setup the pcomplete hooks
  4496. (set (make-local-variable 'pcomplete-command-completion-function)
  4497. 'org-pcomplete-initial)
  4498. (set (make-local-variable 'pcomplete-command-name-function)
  4499. 'org-command-at-point)
  4500. (set (make-local-variable 'pcomplete-default-completion-function)
  4501. 'ignore)
  4502. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4503. 'org-parse-arguments)
  4504. (set (make-local-variable 'pcomplete-termination-string) "")
  4505. (when (>= emacs-major-version 23)
  4506. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4507. ;; If empty file that did not turn on org-mode automatically, make it to.
  4508. (if (and org-insert-mode-line-in-empty-file
  4509. (org-called-interactively-p 'any)
  4510. (= (point-min) (point-max)))
  4511. (insert "# -*- mode: org -*-\n\n"))
  4512. (unless org-inhibit-startup
  4513. (when org-startup-align-all-tables
  4514. (let ((bmp (buffer-modified-p)))
  4515. (org-table-map-tables 'org-table-align 'quietly)
  4516. (set-buffer-modified-p bmp)))
  4517. (when org-startup-with-inline-images
  4518. (org-display-inline-images))
  4519. (when org-startup-indented
  4520. (require 'org-indent)
  4521. (org-indent-mode 1))
  4522. (unless org-inhibit-startup-visibility-stuff
  4523. (org-set-startup-visibility))))
  4524. (when (fboundp 'abbrev-table-put)
  4525. (abbrev-table-put org-mode-abbrev-table
  4526. :parents (list text-mode-abbrev-table)))
  4527. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4528. (defun org-current-time ()
  4529. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4530. (if (> (car org-time-stamp-rounding-minutes) 1)
  4531. (let ((r (car org-time-stamp-rounding-minutes))
  4532. (time (decode-time)))
  4533. (apply 'encode-time
  4534. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4535. (nthcdr 2 time))))
  4536. (current-time)))
  4537. (defun org-today ()
  4538. "Return today date, considering `org-extend-today-until'."
  4539. (time-to-days
  4540. (time-subtract (current-time)
  4541. (list 0 (* 3600 org-extend-today-until) 0))))
  4542. ;;;; Font-Lock stuff, including the activators
  4543. (defvar org-mouse-map (make-sparse-keymap))
  4544. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4545. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4546. (when org-mouse-1-follows-link
  4547. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4548. (when org-tab-follows-link
  4549. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4550. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4551. (require 'font-lock)
  4552. (defconst org-non-link-chars "]\t\n\r<>")
  4553. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4554. "shell" "elisp" "doi" "message"))
  4555. (defvar org-link-types-re nil
  4556. "Matches a link that has a url-like prefix like \"http:\"")
  4557. (defvar org-link-re-with-space nil
  4558. "Matches a link with spaces, optional angular brackets around it.")
  4559. (defvar org-link-re-with-space2 nil
  4560. "Matches a link with spaces, optional angular brackets around it.")
  4561. (defvar org-link-re-with-space3 nil
  4562. "Matches a link with spaces, only for internal part in bracket links.")
  4563. (defvar org-angle-link-re nil
  4564. "Matches link with angular brackets, spaces are allowed.")
  4565. (defvar org-plain-link-re nil
  4566. "Matches plain link, without spaces.")
  4567. (defvar org-bracket-link-regexp nil
  4568. "Matches a link in double brackets.")
  4569. (defvar org-bracket-link-analytic-regexp nil
  4570. "Regular expression used to analyze links.
  4571. Here is what the match groups contain after a match:
  4572. 1: http:
  4573. 2: http
  4574. 3: path
  4575. 4: [desc]
  4576. 5: desc")
  4577. (defvar org-bracket-link-analytic-regexp++ nil
  4578. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4579. (defvar org-any-link-re nil
  4580. "Regular expression matching any link.")
  4581. (defcustom org-match-sexp-depth 3
  4582. "Number of stacked braces for sub/superscript matching.
  4583. This has to be set before loading org.el to be effective."
  4584. :group 'org-export-translation ; ??????????????????????????/
  4585. :type 'integer)
  4586. (defun org-create-multibrace-regexp (left right n)
  4587. "Create a regular expression which will match a balanced sexp.
  4588. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4589. as single character strings.
  4590. The regexp returned will match the entire expression including the
  4591. delimiters. It will also define a single group which contains the
  4592. match except for the outermost delimiters. The maximum depth of
  4593. stacked delimiters is N. Escaping delimiters is not possible."
  4594. (let* ((nothing (concat "[^" left right "]*?"))
  4595. (or "\\|")
  4596. (re nothing)
  4597. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4598. (while (> n 1)
  4599. (setq n (1- n)
  4600. re (concat re or next)
  4601. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4602. (concat left "\\(" re "\\)" right)))
  4603. (defvar org-match-substring-regexp
  4604. (concat
  4605. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4606. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4607. "\\|"
  4608. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4609. "\\|"
  4610. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4611. "The regular expression matching a sub- or superscript.")
  4612. (defvar org-match-substring-with-braces-regexp
  4613. (concat
  4614. "\\([^\\]\\|^\\)\\([_^]\\)\\("
  4615. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4616. "\\)")
  4617. "The regular expression matching a sub- or superscript, forcing braces.")
  4618. (defun org-make-link-regexps ()
  4619. "Update the link regular expressions.
  4620. This should be called after the variable `org-link-types' has changed."
  4621. (setq org-link-types-re
  4622. (concat
  4623. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4624. org-link-re-with-space
  4625. (concat
  4626. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4627. "\\([^" org-non-link-chars " ]"
  4628. "[^" org-non-link-chars "]*"
  4629. "[^" org-non-link-chars " ]\\)>?")
  4630. org-link-re-with-space2
  4631. (concat
  4632. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4633. "\\([^" org-non-link-chars " ]"
  4634. "[^\t\n\r]*"
  4635. "[^" org-non-link-chars " ]\\)>?")
  4636. org-link-re-with-space3
  4637. (concat
  4638. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4639. "\\([^" org-non-link-chars " ]"
  4640. "[^\t\n\r]*\\)")
  4641. org-angle-link-re
  4642. (concat
  4643. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4644. "\\([^" org-non-link-chars " ]"
  4645. "[^" org-non-link-chars "]*"
  4646. "\\)>")
  4647. org-plain-link-re
  4648. (concat
  4649. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4650. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4651. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4652. org-bracket-link-regexp
  4653. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4654. org-bracket-link-analytic-regexp
  4655. (concat
  4656. "\\[\\["
  4657. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4658. "\\([^]]+\\)"
  4659. "\\]"
  4660. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4661. "\\]")
  4662. org-bracket-link-analytic-regexp++
  4663. (concat
  4664. "\\[\\["
  4665. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4666. "\\([^]]+\\)"
  4667. "\\]"
  4668. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4669. "\\]")
  4670. org-any-link-re
  4671. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4672. org-angle-link-re "\\)\\|\\("
  4673. org-plain-link-re "\\)")))
  4674. (org-make-link-regexps)
  4675. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  4676. "Regular expression for fast time stamp matching.")
  4677. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  4678. "Regular expression for fast time stamp matching.")
  4679. (defconst org-ts-regexp0
  4680. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4681. "Regular expression matching time strings for analysis.
  4682. This one does not require the space after the date, so it can be used
  4683. on a string that terminates immediately after the date.")
  4684. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4685. "Regular expression matching time strings for analysis.")
  4686. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4687. "Regular expression matching time stamps, with groups.")
  4688. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4689. "Regular expression matching time stamps (also [..]), with groups.")
  4690. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4691. "Regular expression matching a time stamp range.")
  4692. (defconst org-tr-regexp-both
  4693. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4694. "Regular expression matching a time stamp range.")
  4695. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4696. org-ts-regexp "\\)?")
  4697. "Regular expression matching a time stamp or time stamp range.")
  4698. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4699. org-ts-regexp-both "\\)?")
  4700. "Regular expression matching a time stamp or time stamp range.
  4701. The time stamps may be either active or inactive.")
  4702. (defvar org-emph-face nil)
  4703. (defun org-do-emphasis-faces (limit)
  4704. "Run through the buffer and add overlays to emphasized strings."
  4705. (let (rtn a)
  4706. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4707. (if (not (= (char-after (match-beginning 3))
  4708. (char-after (match-beginning 4))))
  4709. (progn
  4710. (setq rtn t)
  4711. (setq a (assoc (match-string 3) org-emphasis-alist))
  4712. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4713. 'face
  4714. (nth 1 a))
  4715. (and (nth 4 a)
  4716. (org-remove-flyspell-overlays-in
  4717. (match-beginning 0) (match-end 0)))
  4718. (add-text-properties (match-beginning 2) (match-end 2)
  4719. '(font-lock-multiline t org-emphasis t))
  4720. (when org-hide-emphasis-markers
  4721. (add-text-properties (match-end 4) (match-beginning 5)
  4722. '(invisible org-link))
  4723. (add-text-properties (match-beginning 3) (match-end 3)
  4724. '(invisible org-link)))))
  4725. (backward-char 1))
  4726. rtn))
  4727. (defun org-emphasize (&optional char)
  4728. "Insert or change an emphasis, i.e. a font like bold or italic.
  4729. If there is an active region, change that region to a new emphasis.
  4730. If there is no region, just insert the marker characters and position
  4731. the cursor between them.
  4732. CHAR should be either the marker character, or the first character of the
  4733. HTML tag associated with that emphasis. If CHAR is a space, the means
  4734. to remove the emphasis of the selected region.
  4735. If char is not given (for example in an interactive call) it
  4736. will be prompted for."
  4737. (interactive)
  4738. (let ((eal org-emphasis-alist) e det
  4739. (erc org-emphasis-regexp-components)
  4740. (prompt "")
  4741. (string "") beg end move tag c s)
  4742. (if (org-region-active-p)
  4743. (setq beg (region-beginning) end (region-end)
  4744. string (buffer-substring beg end))
  4745. (setq move t))
  4746. (while (setq e (pop eal))
  4747. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4748. c (aref tag 0))
  4749. (push (cons c (string-to-char (car e))) det)
  4750. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4751. (substring tag 1)))))
  4752. (setq det (nreverse det))
  4753. (unless char
  4754. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4755. (setq char (read-char-exclusive)))
  4756. (setq char (or (cdr (assoc char det)) char))
  4757. (if (equal char ?\ )
  4758. (setq s "" move nil)
  4759. (unless (assoc (char-to-string char) org-emphasis-alist)
  4760. (error "No such emphasis marker: \"%c\"" char))
  4761. (setq s (char-to-string char)))
  4762. (while (and (> (length string) 1)
  4763. (equal (substring string 0 1) (substring string -1))
  4764. (assoc (substring string 0 1) org-emphasis-alist))
  4765. (setq string (substring string 1 -1)))
  4766. (setq string (concat s string s))
  4767. (if beg (delete-region beg end))
  4768. (unless (or (bolp)
  4769. (string-match (concat "[" (nth 0 erc) "\n]")
  4770. (char-to-string (char-before (point)))))
  4771. (insert " "))
  4772. (unless (or (eobp)
  4773. (string-match (concat "[" (nth 1 erc) "\n]")
  4774. (char-to-string (char-after (point)))))
  4775. (insert " ") (backward-char 1))
  4776. (insert string)
  4777. (and move (backward-char 1))))
  4778. (defconst org-nonsticky-props
  4779. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4780. (defsubst org-rear-nonsticky-at (pos)
  4781. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4782. (defun org-activate-plain-links (limit)
  4783. "Run through the buffer and add overlays to links."
  4784. (catch 'exit
  4785. (let (f)
  4786. (if (re-search-forward org-plain-link-re limit t)
  4787. (progn
  4788. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4789. (setq f (get-text-property (match-beginning 0) 'face))
  4790. (if (or (eq f 'org-tag)
  4791. (and (listp f) (memq 'org-tag f)))
  4792. nil
  4793. (add-text-properties (match-beginning 0) (match-end 0)
  4794. (list 'mouse-face 'highlight
  4795. 'face 'org-link
  4796. 'keymap org-mouse-map))
  4797. (org-rear-nonsticky-at (match-end 0)))
  4798. t)))))
  4799. (defun org-activate-code (limit)
  4800. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  4801. (progn
  4802. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4803. (remove-text-properties (match-beginning 0) (match-end 0)
  4804. '(display t invisible t intangible t))
  4805. t)))
  4806. (defcustom org-src-fontify-natively nil
  4807. "When non-nil, fontify code in code blocks."
  4808. :type 'boolean
  4809. :version "24.1"
  4810. :group 'org-appearance
  4811. :group 'org-babel)
  4812. (defcustom org-src-prevent-auto-filling nil
  4813. "When non-nil, prevent auto-filling in src blocks."
  4814. :type 'boolean
  4815. :version "24.1"
  4816. :group 'org-appearance
  4817. :group 'org-babel)
  4818. (defcustom org-allow-promoting-top-level-subtree nil
  4819. "When non-nil, allow promoting a top level subtree.
  4820. The leading star of the top level headline will be replaced
  4821. by a #."
  4822. :type 'boolean
  4823. :version "24.1"
  4824. :group 'org-appearance)
  4825. (defun org-fontify-meta-lines-and-blocks (limit)
  4826. (condition-case nil
  4827. (org-fontify-meta-lines-and-blocks-1 limit)
  4828. (error (message "org-mode fontification error"))))
  4829. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  4830. "Fontify #+ lines and blocks, in the correct ways."
  4831. (let ((case-fold-search t))
  4832. (if (re-search-forward
  4833. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4834. limit t)
  4835. (let ((beg (match-beginning 0))
  4836. (block-start (match-end 0))
  4837. (block-end nil)
  4838. (lang (match-string 7))
  4839. (beg1 (line-beginning-position 2))
  4840. (dc1 (downcase (match-string 2)))
  4841. (dc3 (downcase (match-string 3)))
  4842. end end1 quoting block-type ovl)
  4843. (cond
  4844. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4845. ;; a single line of backend-specific content
  4846. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4847. (remove-text-properties (match-beginning 0) (match-end 0)
  4848. '(display t invisible t intangible t))
  4849. (add-text-properties (match-beginning 1) (match-end 3)
  4850. '(font-lock-fontified t face org-meta-line))
  4851. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4852. '(font-lock-fontified t face org-block))
  4853. ; for backend-specific code
  4854. t)
  4855. ((and (match-end 4) (equal dc3 "begin"))
  4856. ;; Truly a block
  4857. (setq block-type (downcase (match-string 5))
  4858. quoting (member block-type org-protecting-blocks))
  4859. (when (re-search-forward
  4860. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4861. nil t) ;; on purpose, we look further than LIMIT
  4862. (setq end (min (point-max) (match-end 0))
  4863. end1 (min (point-max) (1- (match-beginning 0))))
  4864. (setq block-end (match-beginning 0))
  4865. (when quoting
  4866. (remove-text-properties beg end
  4867. '(display t invisible t intangible t)))
  4868. (add-text-properties
  4869. beg end
  4870. '(font-lock-fontified t font-lock-multiline t))
  4871. (add-text-properties beg beg1 '(face org-meta-line))
  4872. (add-text-properties end1 (min (point-max) (1+ end))
  4873. '(face org-meta-line)) ; for end_src
  4874. (cond
  4875. ((and lang (not (string= lang "")) org-src-fontify-natively)
  4876. (org-src-font-lock-fontify-block lang block-start block-end)
  4877. ;; remove old background overlays
  4878. (mapc (lambda (ov)
  4879. (if (eq (overlay-get ov 'face) 'org-block-background)
  4880. (delete-overlay ov)))
  4881. (overlays-at (/ (+ beg1 block-end) 2)))
  4882. ;; add a background overlay
  4883. (setq ovl (make-overlay beg1 block-end))
  4884. (overlay-put ovl 'face 'org-block-background)
  4885. (overlay-put ovl 'evaporate t)) ;; make it go away when empty
  4886. (quoting
  4887. (add-text-properties beg1 (min (point-max) (1+ end1))
  4888. '(face org-block))) ; end of source block
  4889. ((not org-fontify-quote-and-verse-blocks))
  4890. ((string= block-type "quote")
  4891. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  4892. ((string= block-type "verse")
  4893. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  4894. (add-text-properties beg beg1 '(face org-block-begin-line))
  4895. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  4896. '(face org-block-end-line))
  4897. t))
  4898. ((member dc1 '("title:" "author:" "email:" "date:"))
  4899. (add-text-properties
  4900. beg (match-end 3)
  4901. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4902. '(font-lock-fontified t invisible t)
  4903. '(font-lock-fontified t face org-document-info-keyword)))
  4904. (add-text-properties
  4905. (match-beginning 6) (match-end 6)
  4906. (if (string-equal dc1 "title:")
  4907. '(font-lock-fontified t face org-document-title)
  4908. '(font-lock-fontified t face org-document-info))))
  4909. ((not (member (char-after beg) '(?\ ?\t)))
  4910. ;; just any other in-buffer setting, but not indented
  4911. (add-text-properties
  4912. beg (match-end 0)
  4913. '(font-lock-fontified t face org-meta-line))
  4914. t)
  4915. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4916. "orgtbl:" "tblfm:" "tblname:" "results:"
  4917. "call:" "header:" "headers:" "name:"))
  4918. (and (match-end 4) (equal dc3 "attr")))
  4919. (add-text-properties
  4920. beg (match-end 0)
  4921. '(font-lock-fontified t face org-meta-line))
  4922. t)
  4923. ((member dc3 '(" " ""))
  4924. (add-text-properties
  4925. beg (match-end 0)
  4926. '(font-lock-fontified t face font-lock-comment-face)))
  4927. (t nil))))))
  4928. (defun org-strip-protective-commas (beg end)
  4929. "Strip protective commas between BEG and END in the current buffer."
  4930. (interactive "r")
  4931. (save-excursion
  4932. (save-match-data
  4933. (goto-char beg)
  4934. (let ((front-line (save-excursion
  4935. (re-search-forward
  4936. "[^[:space:]]" end t)
  4937. (goto-char (match-beginning 0))
  4938. (current-column))))
  4939. (while (re-search-forward "^[ \t]*\\(,\\)\\([*]\\|#\\+\\)" end t)
  4940. (goto-char (match-beginning 1))
  4941. (when (= (current-column) front-line)
  4942. (replace-match "" nil nil nil 1)))))))
  4943. (defun org-activate-angle-links (limit)
  4944. "Run through the buffer and add overlays to links."
  4945. (if (re-search-forward org-angle-link-re limit t)
  4946. (progn
  4947. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4948. (add-text-properties (match-beginning 0) (match-end 0)
  4949. (list 'mouse-face 'highlight
  4950. 'keymap org-mouse-map))
  4951. (org-rear-nonsticky-at (match-end 0))
  4952. t)))
  4953. (defun org-activate-footnote-links (limit)
  4954. "Run through the buffer and add overlays to footnotes."
  4955. (let ((fn (org-footnote-next-reference-or-definition limit)))
  4956. (when fn
  4957. (let ((beg (nth 1 fn)) (end (nth 2 fn)))
  4958. (org-remove-flyspell-overlays-in beg end)
  4959. (add-text-properties beg end
  4960. (list 'mouse-face 'highlight
  4961. 'keymap org-mouse-map
  4962. 'help-echo
  4963. (if (= (point-at-bol) beg)
  4964. "Footnote definition"
  4965. "Footnote reference")
  4966. 'font-lock-fontified t
  4967. 'font-lock-multiline t
  4968. 'face 'org-footnote))))))
  4969. (defun org-activate-bracket-links (limit)
  4970. "Run through the buffer and add overlays to bracketed links."
  4971. (if (re-search-forward org-bracket-link-regexp limit t)
  4972. (let* ((help (concat "LINK: "
  4973. (org-match-string-no-properties 1)))
  4974. ;; FIXME: above we should remove the escapes.
  4975. ;; but that requires another match, protecting match data,
  4976. ;; a lot of overhead for font-lock.
  4977. (ip (org-maybe-intangible
  4978. (list 'invisible 'org-link
  4979. 'keymap org-mouse-map 'mouse-face 'highlight
  4980. 'font-lock-multiline t 'help-echo help)))
  4981. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4982. 'font-lock-multiline t 'help-echo help)))
  4983. ;; We need to remove the invisible property here. Table narrowing
  4984. ;; may have made some of this invisible.
  4985. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4986. (remove-text-properties (match-beginning 0) (match-end 0)
  4987. '(invisible nil))
  4988. (if (match-end 3)
  4989. (progn
  4990. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4991. (org-rear-nonsticky-at (match-beginning 3))
  4992. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4993. (org-rear-nonsticky-at (match-end 3))
  4994. (add-text-properties (match-end 3) (match-end 0) ip)
  4995. (org-rear-nonsticky-at (match-end 0)))
  4996. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4997. (org-rear-nonsticky-at (match-beginning 1))
  4998. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4999. (org-rear-nonsticky-at (match-end 1))
  5000. (add-text-properties (match-end 1) (match-end 0) ip)
  5001. (org-rear-nonsticky-at (match-end 0)))
  5002. t)))
  5003. (defun org-activate-dates (limit)
  5004. "Run through the buffer and add overlays to dates."
  5005. (if (re-search-forward org-tsr-regexp-both limit t)
  5006. (progn
  5007. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5008. (add-text-properties (match-beginning 0) (match-end 0)
  5009. (list 'mouse-face 'highlight
  5010. 'keymap org-mouse-map))
  5011. (org-rear-nonsticky-at (match-end 0))
  5012. (when org-display-custom-times
  5013. (if (match-end 3)
  5014. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5015. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5016. t)))
  5017. (defvar org-target-link-regexp nil
  5018. "Regular expression matching radio targets in plain text.")
  5019. (make-variable-buffer-local 'org-target-link-regexp)
  5020. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  5021. "Regular expression matching a link target.")
  5022. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  5023. "Regular expression matching a radio target.")
  5024. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  5025. "Regular expression matching any target.")
  5026. (defun org-activate-target-links (limit)
  5027. "Run through the buffer and add overlays to target matches."
  5028. (when org-target-link-regexp
  5029. (let ((case-fold-search t))
  5030. (if (re-search-forward org-target-link-regexp limit t)
  5031. (progn
  5032. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5033. (add-text-properties (match-beginning 0) (match-end 0)
  5034. (list 'mouse-face 'highlight
  5035. 'keymap org-mouse-map
  5036. 'help-echo "Radio target link"
  5037. 'org-linked-text t))
  5038. (org-rear-nonsticky-at (match-end 0))
  5039. t)))))
  5040. (defun org-update-radio-target-regexp ()
  5041. "Find all radio targets in this file and update the regular expression."
  5042. (interactive)
  5043. (when (memq 'radio org-activate-links)
  5044. (setq org-target-link-regexp
  5045. (org-make-target-link-regexp (org-all-targets 'radio)))
  5046. (org-restart-font-lock)))
  5047. (defun org-hide-wide-columns (limit)
  5048. (let (s e)
  5049. (setq s (text-property-any (point) (or limit (point-max))
  5050. 'org-cwidth t))
  5051. (when s
  5052. (setq e (next-single-property-change s 'org-cwidth))
  5053. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5054. (goto-char e)
  5055. t)))
  5056. (defvar org-latex-and-specials-regexp nil
  5057. "Regular expression for highlighting export special stuff.")
  5058. (defvar org-match-substring-regexp)
  5059. (defvar org-match-substring-with-braces-regexp)
  5060. ;; This should be with the exporter code, but we also use if for font-locking
  5061. (defconst org-export-html-special-string-regexps
  5062. '(("\\\\-" . "&shy;")
  5063. ("---\\([^-]\\)" . "&mdash;\\1")
  5064. ("--\\([^-]\\)" . "&ndash;\\1")
  5065. ("\\.\\.\\." . "&hellip;"))
  5066. "Regular expressions for special string conversion.")
  5067. (defun org-compute-latex-and-specials-regexp ()
  5068. "Compute regular expression for stuff treated specially by exporters."
  5069. (if (not org-highlight-latex-fragments-and-specials)
  5070. (org-set-local 'org-latex-and-specials-regexp nil)
  5071. (require 'org-exp)
  5072. (let*
  5073. ((matchers (plist-get org-format-latex-options :matchers))
  5074. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  5075. org-latex-regexps)))
  5076. (org-export-allow-BIND nil)
  5077. (options (org-combine-plists (org-default-export-plist)
  5078. (org-infile-export-plist)))
  5079. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  5080. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  5081. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  5082. (org-export-html-expand (plist-get options :expand-quoted-html))
  5083. (org-export-with-special-strings (plist-get options :special-strings))
  5084. (re-sub
  5085. (cond
  5086. ((equal org-export-with-sub-superscripts '{})
  5087. (list org-match-substring-with-braces-regexp))
  5088. (org-export-with-sub-superscripts
  5089. (list org-match-substring-regexp))
  5090. (t nil)))
  5091. (re-latex
  5092. (if org-export-with-LaTeX-fragments
  5093. (mapcar (lambda (x) (nth 1 x)) latexs)))
  5094. (re-macros
  5095. (if org-export-with-TeX-macros
  5096. (list (concat "\\\\"
  5097. (regexp-opt
  5098. (append
  5099. (delq nil
  5100. (mapcar 'car-safe
  5101. (append org-entities-user
  5102. org-entities)))
  5103. (if (boundp 'org-latex-entities)
  5104. (mapcar (lambda (x)
  5105. (or (car-safe x) x))
  5106. org-latex-entities)
  5107. nil))
  5108. 'words))) ; FIXME
  5109. ))
  5110. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  5111. (re-special (if org-export-with-special-strings
  5112. (mapcar (lambda (x) (car x))
  5113. org-export-html-special-string-regexps)))
  5114. (re-rest
  5115. (delq nil
  5116. (list
  5117. (if org-export-html-expand "@<[^>\n]+>")
  5118. ))))
  5119. (org-set-local
  5120. 'org-latex-and-specials-regexp
  5121. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  5122. re-rest) "\\|")))))
  5123. (defun org-do-latex-and-special-faces (limit)
  5124. "Run through the buffer and add overlays to links."
  5125. (when org-latex-and-specials-regexp
  5126. (let (rtn d)
  5127. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  5128. limit t))
  5129. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5130. 'face))
  5131. '(org-code org-verbatim underline)))
  5132. (progn
  5133. (setq rtn t
  5134. d (cond ((member (char-after (1+ (match-beginning 0)))
  5135. '(?_ ?^)) 1)
  5136. (t 0)))
  5137. (font-lock-prepend-text-property
  5138. (+ d (match-beginning 0)) (match-end 0)
  5139. 'face 'org-latex-and-export-specials)
  5140. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  5141. '(font-lock-multiline t)))))
  5142. rtn)))
  5143. (defun org-restart-font-lock ()
  5144. "Restart `font-lock-mode', to force refontification."
  5145. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5146. (font-lock-mode -1)
  5147. (font-lock-mode 1)))
  5148. (defun org-all-targets (&optional radio)
  5149. "Return a list of all targets in this file.
  5150. With optional argument RADIO, only find radio targets."
  5151. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  5152. rtn)
  5153. (save-excursion
  5154. (goto-char (point-min))
  5155. (while (re-search-forward re nil t)
  5156. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  5157. rtn)))
  5158. (defun org-make-target-link-regexp (targets)
  5159. "Make regular expression matching all strings in TARGETS.
  5160. The regular expression finds the targets also if there is a line break
  5161. between words."
  5162. (and targets
  5163. (concat
  5164. "\\<\\("
  5165. (mapconcat
  5166. (lambda (x)
  5167. (setq x (regexp-quote x))
  5168. (while (string-match " +" x)
  5169. (setq x (replace-match "\\s-+" t t x)))
  5170. x)
  5171. targets
  5172. "\\|")
  5173. "\\)\\>")))
  5174. (defun org-activate-tags (limit)
  5175. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5176. (progn
  5177. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5178. (add-text-properties (match-beginning 1) (match-end 1)
  5179. (list 'mouse-face 'highlight
  5180. 'keymap org-mouse-map))
  5181. (org-rear-nonsticky-at (match-end 1))
  5182. t)))
  5183. (defun org-outline-level ()
  5184. "Compute the outline level of the heading at point.
  5185. This function assumes that the cursor is at the beginning of a line matched
  5186. by `outline-regexp'. Otherwise it returns garbage.
  5187. If this is called at a normal headline, the level is the number of stars.
  5188. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  5189. (save-excursion
  5190. (looking-at org-outline-regexp)
  5191. (1- (- (match-end 0) (match-beginning 0)))))
  5192. (defvar org-font-lock-keywords nil)
  5193. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\+?\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  5194. "Regular expression matching a property line.")
  5195. (defvar org-font-lock-hook nil
  5196. "Functions to be called for special font lock stuff.")
  5197. (defvar org-font-lock-set-keywords-hook nil
  5198. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5199. This is called after `org-font-lock-extra-keywords' is defined, but before
  5200. it is installed to be used by font lock. This can be useful if something
  5201. needs to be inserted at a specific position in the font-lock sequence.")
  5202. (defun org-font-lock-hook (limit)
  5203. (run-hook-with-args 'org-font-lock-hook limit))
  5204. (defun org-set-font-lock-defaults ()
  5205. (let* ((em org-fontify-emphasized-text)
  5206. (lk org-activate-links)
  5207. (org-font-lock-extra-keywords
  5208. (list
  5209. ;; Call the hook
  5210. '(org-font-lock-hook)
  5211. ;; Headlines
  5212. `(,(if org-fontify-whole-heading-line
  5213. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5214. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5215. (1 (org-get-level-face 1))
  5216. (2 (org-get-level-face 2))
  5217. (3 (org-get-level-face 3)))
  5218. ;; Table lines
  5219. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5220. (1 'org-table t))
  5221. ;; Table internals
  5222. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5223. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5224. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5225. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5226. ;; Drawers
  5227. (list org-drawer-regexp '(0 'org-special-keyword t))
  5228. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  5229. ;; Properties
  5230. (list org-property-re
  5231. '(1 'org-special-keyword t)
  5232. '(3 'org-property-value t))
  5233. ;; Links
  5234. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5235. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5236. (if (memq 'plain lk) '(org-activate-plain-links))
  5237. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5238. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  5239. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5240. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5241. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5242. '(org-hide-wide-columns (0 nil append))
  5243. ;; TODO keyword
  5244. (list (format org-heading-keyword-regexp-format
  5245. org-todo-regexp)
  5246. '(2 (org-get-todo-face 2) t))
  5247. ;; DONE
  5248. (if org-fontify-done-headline
  5249. (list (format org-heading-keyword-regexp-format
  5250. (concat
  5251. "\\(?:"
  5252. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5253. "\\)"))
  5254. '(2 'org-headline-done t))
  5255. nil)
  5256. ;; Priorities
  5257. '(org-font-lock-add-priority-faces)
  5258. ;; Tags
  5259. '(org-font-lock-add-tag-faces)
  5260. ;; Special keywords
  5261. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5262. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5263. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5264. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5265. ;; Emphasis
  5266. (if em
  5267. (if (featurep 'xemacs)
  5268. '(org-do-emphasis-faces (0 nil append))
  5269. '(org-do-emphasis-faces)))
  5270. ;; Checkboxes
  5271. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5272. 1 'org-checkbox prepend)
  5273. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5274. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5275. (0 (org-get-checkbox-statistics-face) t)))
  5276. ;; Description list items
  5277. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5278. 1 'org-list-dt prepend)
  5279. ;; ARCHIVEd headings
  5280. (list (concat
  5281. org-outline-regexp-bol
  5282. "\\(.*:" org-archive-tag ":.*\\)")
  5283. '(1 'org-archived prepend))
  5284. ;; Specials
  5285. '(org-do-latex-and-special-faces)
  5286. '(org-fontify-entities)
  5287. '(org-raise-scripts)
  5288. ;; Code
  5289. '(org-activate-code (1 'org-code t))
  5290. ;; COMMENT
  5291. (list (format org-heading-keyword-regexp-format
  5292. (concat "\\("
  5293. org-comment-string "\\|" org-quote-string
  5294. "\\)"))
  5295. '(2 'org-special-keyword t))
  5296. '("^#.*" (0 'font-lock-comment-face t))
  5297. ;; Blocks and meta lines
  5298. '(org-fontify-meta-lines-and-blocks)
  5299. )))
  5300. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5301. (run-hooks 'org-font-lock-set-keywords-hook)
  5302. ;; Now set the full font-lock-keywords
  5303. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5304. (org-set-local 'font-lock-defaults
  5305. '(org-font-lock-keywords t nil nil backward-paragraph))
  5306. (kill-local-variable 'font-lock-keywords) nil))
  5307. (defun org-toggle-pretty-entities ()
  5308. "Toggle the composition display of entities as UTF8 characters."
  5309. (interactive)
  5310. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5311. (org-restart-font-lock)
  5312. (if org-pretty-entities
  5313. (message "Entities are displayed as UTF8 characters")
  5314. (save-restriction
  5315. (widen)
  5316. (org-decompose-region (point-min) (point-max))
  5317. (message "Entities are displayed plain"))))
  5318. (defun org-fontify-entities (limit)
  5319. "Find an entity to fontify."
  5320. (let (ee)
  5321. (when org-pretty-entities
  5322. (catch 'match
  5323. (while (re-search-forward
  5324. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5325. limit t)
  5326. (if (and (not (org-in-indented-comment-line))
  5327. (setq ee (org-entity-get (match-string 1)))
  5328. (= (length (nth 6 ee)) 1))
  5329. (let*
  5330. ((end (if (equal (match-string 2) "{}")
  5331. (match-end 2)
  5332. (match-end 1))))
  5333. (add-text-properties
  5334. (match-beginning 0) end
  5335. (list 'font-lock-fontified t))
  5336. (compose-region (match-beginning 0) end
  5337. (nth 6 ee) nil)
  5338. (backward-char 1)
  5339. (throw 'match t))))
  5340. nil))))
  5341. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5342. "Fontify string S like in Org-mode."
  5343. (with-temp-buffer
  5344. (insert s)
  5345. (let ((org-odd-levels-only odd-levels))
  5346. (org-mode)
  5347. (font-lock-fontify-buffer)
  5348. (buffer-string))))
  5349. (defvar org-m nil)
  5350. (defvar org-l nil)
  5351. (defvar org-f nil)
  5352. (defun org-get-level-face (n)
  5353. "Get the right face for match N in font-lock matching of headlines."
  5354. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5355. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5356. (if org-cycle-level-faces
  5357. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5358. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5359. (cond
  5360. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5361. ((eq n 2) org-f)
  5362. (t (if org-level-color-stars-only nil org-f))))
  5363. (defun org-get-todo-face (kwd)
  5364. "Get the right face for a TODO keyword KWD.
  5365. If KWD is a number, get the corresponding match group."
  5366. (if (numberp kwd) (setq kwd (match-string kwd)))
  5367. (or (org-face-from-face-or-color
  5368. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5369. (and (member kwd org-done-keywords) 'org-done)
  5370. 'org-todo))
  5371. (defun org-face-from-face-or-color (context inherit face-or-color)
  5372. "Create a face list that inherits INHERIT, but sets the foreground color.
  5373. When FACE-OR-COLOR is not a string, just return it."
  5374. (if (stringp face-or-color)
  5375. (list :inherit inherit
  5376. (cdr (assoc context org-faces-easy-properties))
  5377. face-or-color)
  5378. face-or-color))
  5379. (defun org-font-lock-add-tag-faces (limit)
  5380. "Add the special tag faces."
  5381. (when (and org-tag-faces org-tags-special-faces-re)
  5382. (while (re-search-forward org-tags-special-faces-re limit t)
  5383. (add-text-properties (match-beginning 1) (match-end 1)
  5384. (list 'face (org-get-tag-face 1)
  5385. 'font-lock-fontified t))
  5386. (backward-char 1))))
  5387. (defun org-font-lock-add-priority-faces (limit)
  5388. "Add the special priority faces."
  5389. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5390. (when (save-match-data (org-at-heading-p))
  5391. (add-text-properties
  5392. (match-beginning 0) (match-end 0)
  5393. (list 'face (or (org-face-from-face-or-color
  5394. 'priority 'org-special-keyword
  5395. (cdr (assoc (char-after (match-beginning 1))
  5396. org-priority-faces)))
  5397. 'org-special-keyword)
  5398. 'font-lock-fontified t)))))
  5399. (defun org-get-tag-face (kwd)
  5400. "Get the right face for a TODO keyword KWD.
  5401. If KWD is a number, get the corresponding match group."
  5402. (if (numberp kwd) (setq kwd (match-string kwd)))
  5403. (or (org-face-from-face-or-color
  5404. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5405. 'org-tag))
  5406. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5407. "Remove fontification and activation overlays from links."
  5408. (font-lock-default-unfontify-region beg end)
  5409. (let* ((buffer-undo-list t)
  5410. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5411. (inhibit-modification-hooks t)
  5412. deactivate-mark buffer-file-name buffer-file-truename)
  5413. (org-decompose-region beg end)
  5414. (remove-text-properties beg end
  5415. '(mouse-face t keymap t org-linked-text t
  5416. invisible t intangible t
  5417. org-no-flyspell t org-emphasis t))
  5418. (org-remove-font-lock-display-properties beg end)))
  5419. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5420. ((raise 0.3) (height 0.7))
  5421. ((raise -0.5))
  5422. ((raise 0.5)))
  5423. "Display properties for showing superscripts and subscripts.")
  5424. (defun org-remove-font-lock-display-properties (beg end)
  5425. "Remove specific display properties that have been added by font lock.
  5426. The will remove the raise properties that are used to show superscripts
  5427. and subscripts."
  5428. (let (next prop)
  5429. (while (< beg end)
  5430. (setq next (next-single-property-change beg 'display nil end)
  5431. prop (get-text-property beg 'display))
  5432. (if (member prop org-script-display)
  5433. (put-text-property beg next 'display nil))
  5434. (setq beg next))))
  5435. (defun org-raise-scripts (limit)
  5436. "Add raise properties to sub/superscripts."
  5437. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5438. (if (re-search-forward
  5439. (if (eq org-use-sub-superscripts t)
  5440. org-match-substring-regexp
  5441. org-match-substring-with-braces-regexp)
  5442. limit t)
  5443. (let* ((pos (point)) table-p comment-p
  5444. (mpos (match-beginning 3))
  5445. (emph-p (get-text-property mpos 'org-emphasis))
  5446. (link-p (get-text-property mpos 'mouse-face))
  5447. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5448. (goto-char (point-at-bol))
  5449. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5450. comment-p (org-looking-at-p "[ \t]*#"))
  5451. (goto-char pos)
  5452. ;; FIXME: Should we go back one character here, for a_b^c
  5453. ;; (goto-char (1- pos)) ;????????????????????
  5454. (if (or comment-p emph-p link-p keyw-p)
  5455. t
  5456. (put-text-property (match-beginning 3) (match-end 0)
  5457. 'display
  5458. (if (equal (char-after (match-beginning 2)) ?^)
  5459. (nth (if table-p 3 1) org-script-display)
  5460. (nth (if table-p 2 0) org-script-display)))
  5461. (add-text-properties (match-beginning 2) (match-end 2)
  5462. (list 'invisible t
  5463. 'org-dwidth t 'org-dwidth-n 1))
  5464. (if (and (eq (char-after (match-beginning 3)) ?{)
  5465. (eq (char-before (match-end 3)) ?}))
  5466. (progn
  5467. (add-text-properties
  5468. (match-beginning 3) (1+ (match-beginning 3))
  5469. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5470. (add-text-properties
  5471. (1- (match-end 3)) (match-end 3)
  5472. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5473. t)))))
  5474. ;;;; Visibility cycling, including org-goto and indirect buffer
  5475. ;;; Cycling
  5476. (defvar org-cycle-global-status nil)
  5477. (make-variable-buffer-local 'org-cycle-global-status)
  5478. (defvar org-cycle-subtree-status nil)
  5479. (make-variable-buffer-local 'org-cycle-subtree-status)
  5480. ;;;###autoload
  5481. (defvar org-inlinetask-min-level)
  5482. (defun org-cycle (&optional arg)
  5483. "TAB-action and visibility cycling for Org-mode.
  5484. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5485. is outline visibility cycling, but it also invokes other actions
  5486. in special contexts.
  5487. - When this function is called with a prefix argument, rotate the entire
  5488. buffer through 3 states (global cycling)
  5489. 1. OVERVIEW: Show only top-level headlines.
  5490. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5491. 3. SHOW ALL: Show everything.
  5492. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5493. determined by the variable `org-startup-folded', and by any VISIBILITY
  5494. properties in the buffer.
  5495. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5496. including any drawers.
  5497. - When inside a table, re-align the table and move to the next field.
  5498. - When point is at the beginning of a headline, rotate the subtree started
  5499. by this line through 3 different states (local cycling)
  5500. 1. FOLDED: Only the main headline is shown.
  5501. 2. CHILDREN: The main headline and the direct children are shown.
  5502. From this state, you can move to one of the children
  5503. and zoom in further.
  5504. 3. SUBTREE: Show the entire subtree, including body text.
  5505. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5506. - When point is at the beginning of an empty headline and the variable
  5507. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5508. of the headline by demoting and promoting it to likely levels. This
  5509. speeds up creation document structure by pressing TAB once or several
  5510. times right after creating a new headline.
  5511. - When there is a numeric prefix, go up to a heading with level ARG, do
  5512. a `show-subtree' and return to the previous cursor position. If ARG
  5513. is negative, go up that many levels.
  5514. - When point is not at the beginning of a headline, execute the global
  5515. binding for TAB, which is re-indenting the line. See the option
  5516. `org-cycle-emulate-tab' for details.
  5517. - Special case: if point is at the beginning of the buffer and there is
  5518. no headline in line 1, this function will act as if called with prefix arg
  5519. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5520. But only if also the variable `org-cycle-global-at-bob' is t."
  5521. (interactive "P")
  5522. (org-load-modules-maybe)
  5523. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5524. (and org-cycle-level-after-item/entry-creation
  5525. (or (org-cycle-level)
  5526. (org-cycle-item-indentation))))
  5527. (let* ((limit-level
  5528. (or org-cycle-max-level
  5529. (and (boundp 'org-inlinetask-min-level)
  5530. org-inlinetask-min-level
  5531. (1- org-inlinetask-min-level))))
  5532. (nstars (and limit-level
  5533. (if org-odd-levels-only
  5534. (and limit-level (1- (* limit-level 2)))
  5535. limit-level)))
  5536. (org-outline-regexp
  5537. (if (not (derived-mode-p 'org-mode))
  5538. outline-regexp
  5539. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5540. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5541. (not (looking-at org-outline-regexp))))
  5542. (org-cycle-hook
  5543. (if bob-special
  5544. (delq 'org-optimize-window-after-visibility-change
  5545. (copy-sequence org-cycle-hook))
  5546. org-cycle-hook))
  5547. (pos (point)))
  5548. (if (or bob-special (equal arg '(4)))
  5549. ;; special case: use global cycling
  5550. (setq arg t))
  5551. (cond
  5552. ((equal arg '(16))
  5553. (setq last-command 'dummy)
  5554. (org-set-startup-visibility)
  5555. (message "Startup visibility, plus VISIBILITY properties"))
  5556. ((equal arg '(64))
  5557. (show-all)
  5558. (message "Entire buffer visible, including drawers"))
  5559. ;; Table: enter it or move to the next field.
  5560. ((org-at-table-p 'any)
  5561. (if (org-at-table.el-p)
  5562. (message "Use C-c ' to edit table.el tables")
  5563. (if arg (org-table-edit-field t)
  5564. (org-table-justify-field-maybe)
  5565. (call-interactively 'org-table-next-field))))
  5566. ((run-hook-with-args-until-success
  5567. 'org-tab-after-check-for-table-hook))
  5568. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5569. ((eq arg t) (org-cycle-internal-global))
  5570. ;; Drawers: delegate to `org-flag-drawer'.
  5571. ((and org-drawers org-drawer-regexp
  5572. (save-excursion
  5573. (beginning-of-line 1)
  5574. (looking-at org-drawer-regexp)))
  5575. (org-flag-drawer ; toggle block visibility
  5576. (not (get-char-property (match-end 0) 'invisible))))
  5577. ;; Show-subtree, ARG levels up from here.
  5578. ((integerp arg)
  5579. (save-excursion
  5580. (org-back-to-heading)
  5581. (outline-up-heading (if (< arg 0) (- arg)
  5582. (- (funcall outline-level) arg)))
  5583. (org-show-subtree)))
  5584. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5585. ((and (featurep 'org-inlinetask)
  5586. (org-inlinetask-at-task-p)
  5587. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5588. (org-inlinetask-toggle-visibility))
  5589. ((org-try-cdlatex-tab))
  5590. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5591. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5592. (save-excursion (beginning-of-line 1)
  5593. (looking-at org-outline-regexp)))
  5594. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5595. (org-cycle-internal-local))
  5596. ;; From there: TAB emulation and template completion.
  5597. (buffer-read-only (org-back-to-heading))
  5598. ((run-hook-with-args-until-success
  5599. 'org-tab-after-check-for-cycling-hook))
  5600. ((org-try-structure-completion))
  5601. ((run-hook-with-args-until-success
  5602. 'org-tab-before-tab-emulation-hook))
  5603. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5604. (or (not (bolp))
  5605. (not (looking-at org-outline-regexp))))
  5606. (call-interactively (global-key-binding "\t")))
  5607. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5608. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5609. (or (and (eq org-cycle-emulate-tab 'white)
  5610. (= (match-end 0) (point-at-eol)))
  5611. (and (eq org-cycle-emulate-tab 'whitestart)
  5612. (>= (match-end 0) pos))))
  5613. t
  5614. (eq org-cycle-emulate-tab t))
  5615. (call-interactively (global-key-binding "\t")))
  5616. (t (save-excursion
  5617. (org-back-to-heading)
  5618. (org-cycle)))))))
  5619. (defun org-cycle-internal-global ()
  5620. "Do the global cycling action."
  5621. ;; Hack to avoid display of messages for .org attachments in Gnus
  5622. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5623. (cond
  5624. ((and (eq last-command this-command)
  5625. (eq org-cycle-global-status 'overview))
  5626. ;; We just created the overview - now do table of contents
  5627. ;; This can be slow in very large buffers, so indicate action
  5628. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5629. (unless ga (message "CONTENTS..."))
  5630. (org-content)
  5631. (unless ga (message "CONTENTS...done"))
  5632. (setq org-cycle-global-status 'contents)
  5633. (run-hook-with-args 'org-cycle-hook 'contents))
  5634. ((and (eq last-command this-command)
  5635. (eq org-cycle-global-status 'contents))
  5636. ;; We just showed the table of contents - now show everything
  5637. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5638. (show-all)
  5639. (unless ga (message "SHOW ALL"))
  5640. (setq org-cycle-global-status 'all)
  5641. (run-hook-with-args 'org-cycle-hook 'all))
  5642. (t
  5643. ;; Default action: go to overview
  5644. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5645. (org-overview)
  5646. (unless ga (message "OVERVIEW"))
  5647. (setq org-cycle-global-status 'overview)
  5648. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5649. (defun org-cycle-internal-local ()
  5650. "Do the local cycling action."
  5651. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5652. ;; First, determine end of headline (EOH), end of subtree or item
  5653. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5654. (save-excursion
  5655. (if (org-at-item-p)
  5656. (progn
  5657. (beginning-of-line)
  5658. (setq struct (org-list-struct))
  5659. (setq eoh (point-at-eol))
  5660. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5661. (setq has-children (org-list-has-child-p (point) struct)))
  5662. (org-back-to-heading)
  5663. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5664. (setq eos (save-excursion
  5665. (org-end-of-subtree t)
  5666. (unless (eobp)
  5667. (skip-chars-forward " \t\n"))
  5668. (if (eobp) (point) (1- (point)))))
  5669. (setq has-children
  5670. (or (save-excursion
  5671. (let ((level (funcall outline-level)))
  5672. (outline-next-heading)
  5673. (and (org-at-heading-p t)
  5674. (> (funcall outline-level) level))))
  5675. (save-excursion
  5676. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5677. ;; Determine end invisible part of buffer (EOL)
  5678. (beginning-of-line 2)
  5679. ;; XEmacs doesn't have `next-single-char-property-change'
  5680. (if (featurep 'xemacs)
  5681. (while (and (not (eobp)) ;; this is like `next-line'
  5682. (get-char-property (1- (point)) 'invisible))
  5683. (beginning-of-line 2))
  5684. (while (and (not (eobp)) ;; this is like `next-line'
  5685. (get-char-property (1- (point)) 'invisible))
  5686. (goto-char (next-single-char-property-change (point) 'invisible))
  5687. (and (eolp) (beginning-of-line 2))))
  5688. (setq eol (point)))
  5689. ;; Find out what to do next and set `this-command'
  5690. (cond
  5691. ((= eos eoh)
  5692. ;; Nothing is hidden behind this heading
  5693. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5694. (message "EMPTY ENTRY")
  5695. (setq org-cycle-subtree-status nil)
  5696. (save-excursion
  5697. (goto-char eos)
  5698. (outline-next-heading)
  5699. (if (outline-invisible-p) (org-flag-heading nil))))
  5700. ((and (or (>= eol eos)
  5701. (not (string-match "\\S-" (buffer-substring eol eos))))
  5702. (or has-children
  5703. (not (setq children-skipped
  5704. org-cycle-skip-children-state-if-no-children))))
  5705. ;; Entire subtree is hidden in one line: children view
  5706. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5707. (if (org-at-item-p)
  5708. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5709. (org-show-entry)
  5710. (org-with-limited-levels (show-children))
  5711. ;; FIXME: This slows down the func way too much.
  5712. ;; How keep drawers hidden in subtree anyway?
  5713. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  5714. ;; (org-cycle-hide-drawers 'subtree))
  5715. ;; Fold every list in subtree to top-level items.
  5716. (when (eq org-cycle-include-plain-lists 'integrate)
  5717. (save-excursion
  5718. (org-back-to-heading)
  5719. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5720. (beginning-of-line 1)
  5721. (let* ((struct (org-list-struct))
  5722. (prevs (org-list-prevs-alist struct))
  5723. (end (org-list-get-bottom-point struct)))
  5724. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5725. (org-list-get-all-items (point) struct prevs))
  5726. (goto-char end))))))
  5727. (message "CHILDREN")
  5728. (save-excursion
  5729. (goto-char eos)
  5730. (outline-next-heading)
  5731. (if (outline-invisible-p) (org-flag-heading nil)))
  5732. (setq org-cycle-subtree-status 'children)
  5733. (run-hook-with-args 'org-cycle-hook 'children))
  5734. ((or children-skipped
  5735. (and (eq last-command this-command)
  5736. (eq org-cycle-subtree-status 'children)))
  5737. ;; We just showed the children, or no children are there,
  5738. ;; now show everything.
  5739. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5740. (outline-flag-region eoh eos nil)
  5741. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5742. (setq org-cycle-subtree-status 'subtree)
  5743. (run-hook-with-args 'org-cycle-hook 'subtree))
  5744. (t
  5745. ;; Default action: hide the subtree.
  5746. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5747. (outline-flag-region eoh eos t)
  5748. (message "FOLDED")
  5749. (setq org-cycle-subtree-status 'folded)
  5750. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5751. ;;;###autoload
  5752. (defun org-global-cycle (&optional arg)
  5753. "Cycle the global visibility. For details see `org-cycle'.
  5754. With \\[universal-argument] prefix arg, switch to startup visibility.
  5755. With a numeric prefix, show all headlines up to that level."
  5756. (interactive "P")
  5757. (let ((org-cycle-include-plain-lists
  5758. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  5759. (cond
  5760. ((integerp arg)
  5761. (show-all)
  5762. (hide-sublevels arg)
  5763. (setq org-cycle-global-status 'contents))
  5764. ((equal arg '(4))
  5765. (org-set-startup-visibility)
  5766. (message "Startup visibility, plus VISIBILITY properties."))
  5767. (t
  5768. (org-cycle '(4))))))
  5769. (defun org-set-startup-visibility ()
  5770. "Set the visibility required by startup options and properties."
  5771. (cond
  5772. ((eq org-startup-folded t)
  5773. (org-cycle '(4)))
  5774. ((eq org-startup-folded 'content)
  5775. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5776. (org-cycle '(4)) (org-cycle '(4)))))
  5777. (unless (eq org-startup-folded 'showeverything)
  5778. (if org-hide-block-startup (org-hide-block-all))
  5779. (org-set-visibility-according-to-property 'no-cleanup)
  5780. (org-cycle-hide-archived-subtrees 'all)
  5781. (org-cycle-hide-drawers 'all)
  5782. (org-cycle-show-empty-lines t)))
  5783. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5784. "Switch subtree visibilities according to :VISIBILITY: property."
  5785. (interactive)
  5786. (let (org-show-entry-below state)
  5787. (save-excursion
  5788. (goto-char (point-min))
  5789. (while (re-search-forward
  5790. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5791. nil t)
  5792. (setq state (match-string 1))
  5793. (save-excursion
  5794. (org-back-to-heading t)
  5795. (hide-subtree)
  5796. (org-reveal)
  5797. (cond
  5798. ((equal state '("fold" "folded"))
  5799. (hide-subtree))
  5800. ((equal state "children")
  5801. (org-show-hidden-entry)
  5802. (show-children))
  5803. ((equal state "content")
  5804. (save-excursion
  5805. (save-restriction
  5806. (org-narrow-to-subtree)
  5807. (org-content))))
  5808. ((member state '("all" "showall"))
  5809. (show-subtree)))))
  5810. (unless no-cleanup
  5811. (org-cycle-hide-archived-subtrees 'all)
  5812. (org-cycle-hide-drawers 'all)
  5813. (org-cycle-show-empty-lines 'all)))))
  5814. ;; This function uses outline-regexp instead of the more fundamental
  5815. ;; org-outline-regexp so that org-cycle-global works outside of Org
  5816. ;; buffers, where outline-regexp is needed.
  5817. (defun org-overview ()
  5818. "Switch to overview mode, showing only top-level headlines.
  5819. Really, this shows all headlines with level equal or greater than the level
  5820. of the first headline in the buffer. This is important, because if the
  5821. first headline is not level one, then (hide-sublevels 1) gives confusing
  5822. results."
  5823. (interactive)
  5824. (let ((level (save-excursion
  5825. (goto-char (point-min))
  5826. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5827. (progn
  5828. (goto-char (match-beginning 0))
  5829. (funcall outline-level))))))
  5830. (and level (hide-sublevels level))))
  5831. (defun org-content (&optional arg)
  5832. "Show all headlines in the buffer, like a table of contents.
  5833. With numerical argument N, show content up to level N."
  5834. (interactive "P")
  5835. (save-excursion
  5836. ;; Visit all headings and show their offspring
  5837. (and (integerp arg) (org-overview))
  5838. (goto-char (point-max))
  5839. (catch 'exit
  5840. (while (and (progn (condition-case nil
  5841. (outline-previous-visible-heading 1)
  5842. (error (goto-char (point-min))))
  5843. t)
  5844. (looking-at org-outline-regexp))
  5845. (if (integerp arg)
  5846. (show-children (1- arg))
  5847. (show-branches))
  5848. (if (bobp) (throw 'exit nil))))))
  5849. (defun org-optimize-window-after-visibility-change (state)
  5850. "Adjust the window after a change in outline visibility.
  5851. This function is the default value of the hook `org-cycle-hook'."
  5852. (when (get-buffer-window (current-buffer))
  5853. (cond
  5854. ((eq state 'content) nil)
  5855. ((eq state 'all) nil)
  5856. ((eq state 'folded) nil)
  5857. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5858. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5859. (defun org-remove-empty-overlays-at (pos)
  5860. "Remove outline overlays that do not contain non-white stuff."
  5861. (mapc
  5862. (lambda (o)
  5863. (and (eq 'outline (overlay-get o 'invisible))
  5864. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5865. (overlay-end o))))
  5866. (delete-overlay o)))
  5867. (overlays-at pos)))
  5868. (defun org-clean-visibility-after-subtree-move ()
  5869. "Fix visibility issues after moving a subtree."
  5870. ;; First, find a reasonable region to look at:
  5871. ;; Start two siblings above, end three below
  5872. (let* ((beg (save-excursion
  5873. (and (org-get-last-sibling)
  5874. (org-get-last-sibling))
  5875. (point)))
  5876. (end (save-excursion
  5877. (and (org-get-next-sibling)
  5878. (org-get-next-sibling)
  5879. (org-get-next-sibling))
  5880. (if (org-at-heading-p)
  5881. (point-at-eol)
  5882. (point))))
  5883. (level (looking-at "\\*+"))
  5884. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5885. (save-excursion
  5886. (save-restriction
  5887. (narrow-to-region beg end)
  5888. (when re
  5889. ;; Properly fold already folded siblings
  5890. (goto-char (point-min))
  5891. (while (re-search-forward re nil t)
  5892. (if (and (not (outline-invisible-p))
  5893. (save-excursion
  5894. (goto-char (point-at-eol)) (outline-invisible-p)))
  5895. (hide-entry))))
  5896. (org-cycle-show-empty-lines 'overview)
  5897. (org-cycle-hide-drawers 'overview)))))
  5898. (defun org-cycle-show-empty-lines (state)
  5899. "Show empty lines above all visible headlines.
  5900. The region to be covered depends on STATE when called through
  5901. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5902. entire buffer covered. Note that an empty line is only shown if there
  5903. are at least `org-cycle-separator-lines' empty lines before the headline."
  5904. (when (not (= org-cycle-separator-lines 0))
  5905. (save-excursion
  5906. (let* ((n (abs org-cycle-separator-lines))
  5907. (re (cond
  5908. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5909. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5910. (t (let ((ns (number-to-string (- n 2))))
  5911. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5912. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5913. beg end b e)
  5914. (cond
  5915. ((memq state '(overview contents t))
  5916. (setq beg (point-min) end (point-max)))
  5917. ((memq state '(children folded))
  5918. (setq beg (point) end (progn (org-end-of-subtree t t)
  5919. (beginning-of-line 2)
  5920. (point)))))
  5921. (when beg
  5922. (goto-char beg)
  5923. (while (re-search-forward re end t)
  5924. (unless (get-char-property (match-end 1) 'invisible)
  5925. (setq e (match-end 1))
  5926. (if (< org-cycle-separator-lines 0)
  5927. (setq b (save-excursion
  5928. (goto-char (match-beginning 0))
  5929. (org-back-over-empty-lines)
  5930. (if (save-excursion
  5931. (goto-char (max (point-min) (1- (point))))
  5932. (org-at-heading-p))
  5933. (1- (point))
  5934. (point))))
  5935. (setq b (match-beginning 1)))
  5936. (outline-flag-region b e nil)))))))
  5937. ;; Never hide empty lines at the end of the file.
  5938. (save-excursion
  5939. (goto-char (point-max))
  5940. (outline-previous-heading)
  5941. (outline-end-of-heading)
  5942. (if (and (looking-at "[ \t\n]+")
  5943. (= (match-end 0) (point-max)))
  5944. (outline-flag-region (point) (match-end 0) nil))))
  5945. (defun org-show-empty-lines-in-parent ()
  5946. "Move to the parent and re-show empty lines before visible headlines."
  5947. (save-excursion
  5948. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5949. (org-cycle-show-empty-lines context))))
  5950. (defun org-files-list ()
  5951. "Return `org-agenda-files' list, plus all open org-mode files.
  5952. This is useful for operations that need to scan all of a user's
  5953. open and agenda-wise Org files."
  5954. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5955. (dolist (buf (buffer-list))
  5956. (with-current-buffer buf
  5957. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  5958. (let ((file (expand-file-name (buffer-file-name))))
  5959. (unless (member file files)
  5960. (push file files))))))
  5961. files))
  5962. (defsubst org-entry-beginning-position ()
  5963. "Return the beginning position of the current entry."
  5964. (save-excursion (outline-back-to-heading t) (point)))
  5965. (defsubst org-entry-end-position ()
  5966. "Return the end position of the current entry."
  5967. (save-excursion (outline-next-heading) (point)))
  5968. (defun org-cycle-hide-drawers (state)
  5969. "Re-hide all drawers after a visibility state change."
  5970. (when (and (derived-mode-p 'org-mode)
  5971. (not (memq state '(overview folded contents))))
  5972. (save-excursion
  5973. (let* ((globalp (memq state '(contents all)))
  5974. (beg (if globalp (point-min) (point)))
  5975. (end (if globalp (point-max)
  5976. (if (eq state 'children)
  5977. (save-excursion (outline-next-heading) (point))
  5978. (org-end-of-subtree t)))))
  5979. (goto-char beg)
  5980. (while (re-search-forward org-drawer-regexp end t)
  5981. (org-flag-drawer t))))))
  5982. (defun org-flag-drawer (flag)
  5983. "When FLAG is non-nil, hide the drawer we are within.
  5984. Otherwise make it visible."
  5985. (save-excursion
  5986. (beginning-of-line 1)
  5987. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5988. (let ((b (match-end 0)))
  5989. (if (re-search-forward
  5990. "^[ \t]*:END:"
  5991. (save-excursion (outline-next-heading) (point)) t)
  5992. (outline-flag-region b (point-at-eol) flag)
  5993. (error ":END: line missing at position %s" b))))))
  5994. (defun org-subtree-end-visible-p ()
  5995. "Is the end of the current subtree visible?"
  5996. (pos-visible-in-window-p
  5997. (save-excursion (org-end-of-subtree t) (point))))
  5998. (defun org-first-headline-recenter (&optional N)
  5999. "Move cursor to the first headline and recenter the headline.
  6000. Optional argument N means put the headline into the Nth line of the window."
  6001. (goto-char (point-min))
  6002. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6003. (beginning-of-line)
  6004. (recenter (prefix-numeric-value N))))
  6005. ;;; Saving and restoring visibility
  6006. (defun org-outline-overlay-data (&optional use-markers)
  6007. "Return a list of the locations of all outline overlays.
  6008. These are overlays with the `invisible' property value `outline'.
  6009. The return value is a list of cons cells, with start and stop
  6010. positions for each overlay.
  6011. If USE-MARKERS is set, return the positions as markers."
  6012. (let (beg end)
  6013. (save-excursion
  6014. (save-restriction
  6015. (widen)
  6016. (delq nil
  6017. (mapcar (lambda (o)
  6018. (when (eq (overlay-get o 'invisible) 'outline)
  6019. (setq beg (overlay-start o)
  6020. end (overlay-end o))
  6021. (and beg end (> end beg)
  6022. (if use-markers
  6023. (cons (move-marker (make-marker) beg)
  6024. (move-marker (make-marker) end))
  6025. (cons beg end)))))
  6026. (overlays-in (point-min) (point-max))))))))
  6027. (defun org-set-outline-overlay-data (data)
  6028. "Create visibility overlays for all positions in DATA.
  6029. DATA should have been made by `org-outline-overlay-data'."
  6030. (let (o)
  6031. (save-excursion
  6032. (save-restriction
  6033. (widen)
  6034. (show-all)
  6035. (mapc (lambda (c)
  6036. (outline-flag-region (car c) (cdr c) t))
  6037. data)))))
  6038. ;;; Folding of blocks
  6039. (defvar org-hide-block-overlays nil
  6040. "Overlays hiding blocks.")
  6041. (make-variable-buffer-local 'org-hide-block-overlays)
  6042. (defun org-block-map (function &optional start end)
  6043. "Call FUNCTION at the head of all source blocks in the current buffer.
  6044. Optional arguments START and END can be used to limit the range."
  6045. (let ((start (or start (point-min)))
  6046. (end (or end (point-max))))
  6047. (save-excursion
  6048. (goto-char start)
  6049. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6050. (save-excursion
  6051. (save-match-data
  6052. (goto-char (match-beginning 0))
  6053. (funcall function)))))))
  6054. (defun org-hide-block-toggle-all ()
  6055. "Toggle the visibility of all blocks in the current buffer."
  6056. (org-block-map #'org-hide-block-toggle))
  6057. (defun org-hide-block-all ()
  6058. "Fold all blocks in the current buffer."
  6059. (interactive)
  6060. (org-show-block-all)
  6061. (org-block-map #'org-hide-block-toggle-maybe))
  6062. (defun org-show-block-all ()
  6063. "Unfold all blocks in the current buffer."
  6064. (interactive)
  6065. (mapc 'delete-overlay org-hide-block-overlays)
  6066. (setq org-hide-block-overlays nil))
  6067. (defun org-hide-block-toggle-maybe ()
  6068. "Toggle visibility of block at point."
  6069. (interactive)
  6070. (let ((case-fold-search t))
  6071. (if (save-excursion
  6072. (beginning-of-line 1)
  6073. (looking-at org-block-regexp))
  6074. (progn (org-hide-block-toggle)
  6075. t) ;; to signal that we took action
  6076. nil))) ;; to signal that we did not
  6077. (defun org-hide-block-toggle (&optional force)
  6078. "Toggle the visibility of the current block."
  6079. (interactive)
  6080. (save-excursion
  6081. (beginning-of-line)
  6082. (if (re-search-forward org-block-regexp nil t)
  6083. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  6084. (end (match-end 0)) ;; end of entire body
  6085. ov)
  6086. (if (memq t (mapcar (lambda (overlay)
  6087. (eq (overlay-get overlay 'invisible)
  6088. 'org-hide-block))
  6089. (overlays-at start)))
  6090. (if (or (not force) (eq force 'off))
  6091. (mapc (lambda (ov)
  6092. (when (member ov org-hide-block-overlays)
  6093. (setq org-hide-block-overlays
  6094. (delq ov org-hide-block-overlays)))
  6095. (when (eq (overlay-get ov 'invisible)
  6096. 'org-hide-block)
  6097. (delete-overlay ov)))
  6098. (overlays-at start)))
  6099. (setq ov (make-overlay start end))
  6100. (overlay-put ov 'invisible 'org-hide-block)
  6101. ;; make the block accessible to isearch
  6102. (overlay-put
  6103. ov 'isearch-open-invisible
  6104. (lambda (ov)
  6105. (when (member ov org-hide-block-overlays)
  6106. (setq org-hide-block-overlays
  6107. (delq ov org-hide-block-overlays)))
  6108. (when (eq (overlay-get ov 'invisible)
  6109. 'org-hide-block)
  6110. (delete-overlay ov))))
  6111. (push ov org-hide-block-overlays)))
  6112. (error "Not looking at a source block"))))
  6113. ;; org-tab-after-check-for-cycling-hook
  6114. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6115. ;; Remove overlays when changing major mode
  6116. (add-hook 'org-mode-hook
  6117. (lambda () (org-add-hook 'change-major-mode-hook
  6118. 'org-show-block-all 'append 'local)))
  6119. ;;; Org-goto
  6120. (defvar org-goto-window-configuration nil)
  6121. (defvar org-goto-marker nil)
  6122. (defvar org-goto-map
  6123. (let ((map (make-sparse-keymap)))
  6124. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  6125. (while (setq cmd (pop cmds))
  6126. (substitute-key-definition cmd cmd map global-map)))
  6127. (suppress-keymap map)
  6128. (org-defkey map "\C-m" 'org-goto-ret)
  6129. (org-defkey map [(return)] 'org-goto-ret)
  6130. (org-defkey map [(left)] 'org-goto-left)
  6131. (org-defkey map [(right)] 'org-goto-right)
  6132. (org-defkey map [(control ?g)] 'org-goto-quit)
  6133. (org-defkey map "\C-i" 'org-cycle)
  6134. (org-defkey map [(tab)] 'org-cycle)
  6135. (org-defkey map [(down)] 'outline-next-visible-heading)
  6136. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6137. (if org-goto-auto-isearch
  6138. (if (fboundp 'define-key-after)
  6139. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6140. nil)
  6141. (org-defkey map "q" 'org-goto-quit)
  6142. (org-defkey map "n" 'outline-next-visible-heading)
  6143. (org-defkey map "p" 'outline-previous-visible-heading)
  6144. (org-defkey map "f" 'outline-forward-same-level)
  6145. (org-defkey map "b" 'outline-backward-same-level)
  6146. (org-defkey map "u" 'outline-up-heading))
  6147. (org-defkey map "/" 'org-occur)
  6148. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6149. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6150. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6151. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6152. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6153. map))
  6154. (defconst org-goto-help
  6155. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  6156. RET=jump to location [Q]uit and return to previous location
  6157. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6158. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6159. ;; FIXME: Docstring does not mention both interfaces
  6160. (defun org-goto (&optional alternative-interface)
  6161. "Look up a different location in the current file, keeping current visibility.
  6162. When you want look-up or go to a different location in a
  6163. document, the fastest way is often to fold the entire buffer and
  6164. then dive into the tree. This method has the disadvantage, that
  6165. the previous location will be folded, which may not be what you
  6166. want.
  6167. This command works around this by showing a copy of the current
  6168. buffer in an indirect buffer, in overview mode. You can dive
  6169. into the tree in that copy, use org-occur and incremental search
  6170. to find a location. When pressing RET or `Q', the command
  6171. returns to the original buffer in which the visibility is still
  6172. unchanged. After RET it will also jump to the location selected
  6173. in the indirect buffer and expose the headline hierarchy above.
  6174. With a prefix argument, use the alternative interface: e.g. if
  6175. `org-goto-interface' is 'outline use 'outline-path-completion."
  6176. (interactive "P")
  6177. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6178. (org-refile-use-outline-path t)
  6179. (org-refile-target-verify-function nil)
  6180. (interface
  6181. (if (not alternative-interface)
  6182. org-goto-interface
  6183. (if (eq org-goto-interface 'outline)
  6184. 'outline-path-completion
  6185. 'outline)))
  6186. (org-goto-start-pos (point))
  6187. (selected-point
  6188. (if (eq interface 'outline)
  6189. (car (org-get-location (current-buffer) org-goto-help))
  6190. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6191. (org-refile-check-position pa)
  6192. (nth 3 pa)))))
  6193. (if selected-point
  6194. (progn
  6195. (org-mark-ring-push org-goto-start-pos)
  6196. (goto-char selected-point)
  6197. (if (or (outline-invisible-p) (org-invisible-p2))
  6198. (org-show-context 'org-goto)))
  6199. (message "Quit"))))
  6200. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6201. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6202. (defvar org-goto-local-auto-isearch-map) ; defined below
  6203. (defun org-get-location (buf help)
  6204. "Let the user select a location in the Org-mode buffer BUF.
  6205. This function uses a recursive edit. It returns the selected position
  6206. or nil."
  6207. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6208. (isearch-hide-immediately nil)
  6209. (isearch-search-fun-function
  6210. (lambda () 'org-goto-local-search-headings))
  6211. (org-goto-selected-point org-goto-exit-command)
  6212. (pop-up-frames nil)
  6213. (special-display-buffer-names nil)
  6214. (special-display-regexps nil)
  6215. (special-display-function nil))
  6216. (save-excursion
  6217. (save-window-excursion
  6218. (delete-other-windows)
  6219. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6220. (org-pop-to-buffer-same-window
  6221. (condition-case nil
  6222. (make-indirect-buffer (current-buffer) "*org-goto*")
  6223. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6224. (with-output-to-temp-buffer "*Help*"
  6225. (princ help))
  6226. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  6227. (setq buffer-read-only nil)
  6228. (let ((org-startup-truncated t)
  6229. (org-startup-folded nil)
  6230. (org-startup-align-all-tables nil))
  6231. (org-mode)
  6232. (org-overview))
  6233. (setq buffer-read-only t)
  6234. (if (and (boundp 'org-goto-start-pos)
  6235. (integer-or-marker-p org-goto-start-pos))
  6236. (let ((org-show-hierarchy-above t)
  6237. (org-show-siblings t)
  6238. (org-show-following-heading t))
  6239. (goto-char org-goto-start-pos)
  6240. (and (outline-invisible-p) (org-show-context)))
  6241. (goto-char (point-min)))
  6242. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6243. (message "Select location and press RET")
  6244. (use-local-map org-goto-map)
  6245. (recursive-edit)
  6246. ))
  6247. (kill-buffer "*org-goto*")
  6248. (cons org-goto-selected-point org-goto-exit-command)))
  6249. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6250. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6251. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6252. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  6253. (defun org-goto-local-search-headings (string bound noerror)
  6254. "Search and make sure that any matches are in headlines."
  6255. (catch 'return
  6256. (while (if isearch-forward
  6257. (search-forward string bound noerror)
  6258. (search-backward string bound noerror))
  6259. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  6260. (and (member :headline context)
  6261. (not (member :tags context))))
  6262. (throw 'return (point))))))
  6263. (defun org-goto-local-auto-isearch ()
  6264. "Start isearch."
  6265. (interactive)
  6266. (goto-char (point-min))
  6267. (let ((keys (this-command-keys)))
  6268. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6269. (isearch-mode t)
  6270. (isearch-process-search-char (string-to-char keys)))))
  6271. (defun org-goto-ret (&optional arg)
  6272. "Finish `org-goto' by going to the new location."
  6273. (interactive "P")
  6274. (setq org-goto-selected-point (point)
  6275. org-goto-exit-command 'return)
  6276. (throw 'exit nil))
  6277. (defun org-goto-left ()
  6278. "Finish `org-goto' by going to the new location."
  6279. (interactive)
  6280. (if (org-at-heading-p)
  6281. (progn
  6282. (beginning-of-line 1)
  6283. (setq org-goto-selected-point (point)
  6284. org-goto-exit-command 'left)
  6285. (throw 'exit nil))
  6286. (error "Not on a heading")))
  6287. (defun org-goto-right ()
  6288. "Finish `org-goto' by going to the new location."
  6289. (interactive)
  6290. (if (org-at-heading-p)
  6291. (progn
  6292. (setq org-goto-selected-point (point)
  6293. org-goto-exit-command 'right)
  6294. (throw 'exit nil))
  6295. (error "Not on a heading")))
  6296. (defun org-goto-quit ()
  6297. "Finish `org-goto' without cursor motion."
  6298. (interactive)
  6299. (setq org-goto-selected-point nil)
  6300. (setq org-goto-exit-command 'quit)
  6301. (throw 'exit nil))
  6302. ;;; Indirect buffer display of subtrees
  6303. (defvar org-indirect-dedicated-frame nil
  6304. "This is the frame being used for indirect tree display.")
  6305. (defvar org-last-indirect-buffer nil)
  6306. (defun org-tree-to-indirect-buffer (&optional arg)
  6307. "Create indirect buffer and narrow it to current subtree.
  6308. With numerical prefix ARG, go up to this level and then take that tree.
  6309. If ARG is negative, go up that many levels.
  6310. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6311. indirect buffer previously made with this command, to avoid proliferation of
  6312. indirect buffers. However, when you call the command with a \
  6313. \\[universal-argument] prefix, or
  6314. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6315. is kept so that you can work with several indirect buffers at the same time.
  6316. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6317. \\[universal-argument] prefix also
  6318. requests that a new frame be made for the new buffer, so that the dedicated
  6319. frame is not changed."
  6320. (interactive "P")
  6321. (let ((cbuf (current-buffer))
  6322. (cwin (selected-window))
  6323. (pos (point))
  6324. beg end level heading ibuf)
  6325. (save-excursion
  6326. (org-back-to-heading t)
  6327. (when (numberp arg)
  6328. (setq level (org-outline-level))
  6329. (if (< arg 0) (setq arg (+ level arg)))
  6330. (while (> (setq level (org-outline-level)) arg)
  6331. (outline-up-heading 1 t)))
  6332. (setq beg (point)
  6333. heading (org-get-heading))
  6334. (org-end-of-subtree t t)
  6335. (if (org-at-heading-p) (backward-char 1))
  6336. (setq end (point)))
  6337. (if (and (buffer-live-p org-last-indirect-buffer)
  6338. (not (eq org-indirect-buffer-display 'new-frame))
  6339. (not arg))
  6340. (kill-buffer org-last-indirect-buffer))
  6341. (setq ibuf (org-get-indirect-buffer cbuf)
  6342. org-last-indirect-buffer ibuf)
  6343. (cond
  6344. ((or (eq org-indirect-buffer-display 'new-frame)
  6345. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6346. (select-frame (make-frame))
  6347. (delete-other-windows)
  6348. (org-pop-to-buffer-same-window ibuf)
  6349. (org-set-frame-title heading))
  6350. ((eq org-indirect-buffer-display 'dedicated-frame)
  6351. (raise-frame
  6352. (select-frame (or (and org-indirect-dedicated-frame
  6353. (frame-live-p org-indirect-dedicated-frame)
  6354. org-indirect-dedicated-frame)
  6355. (setq org-indirect-dedicated-frame (make-frame)))))
  6356. (delete-other-windows)
  6357. (org-pop-to-buffer-same-window ibuf)
  6358. (org-set-frame-title (concat "Indirect: " heading)))
  6359. ((eq org-indirect-buffer-display 'current-window)
  6360. (org-pop-to-buffer-same-window ibuf))
  6361. ((eq org-indirect-buffer-display 'other-window)
  6362. (pop-to-buffer ibuf))
  6363. (t (error "Invalid value")))
  6364. (if (featurep 'xemacs)
  6365. (save-excursion (org-mode) (turn-on-font-lock)))
  6366. (narrow-to-region beg end)
  6367. (show-all)
  6368. (goto-char pos)
  6369. (run-hook-with-args 'org-cycle-hook 'all)
  6370. (and (window-live-p cwin) (select-window cwin))))
  6371. (defun org-get-indirect-buffer (&optional buffer)
  6372. (setq buffer (or buffer (current-buffer)))
  6373. (let ((n 1) (base (buffer-name buffer)) bname)
  6374. (while (buffer-live-p
  6375. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6376. (setq n (1+ n)))
  6377. (condition-case nil
  6378. (make-indirect-buffer buffer bname 'clone)
  6379. (error (make-indirect-buffer buffer bname)))))
  6380. (defun org-set-frame-title (title)
  6381. "Set the title of the current frame to the string TITLE."
  6382. ;; FIXME: how to name a single frame in XEmacs???
  6383. (unless (featurep 'xemacs)
  6384. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6385. ;;;; Structure editing
  6386. ;;; Inserting headlines
  6387. (defun org-previous-line-empty-p ()
  6388. (save-excursion
  6389. (and (not (bobp))
  6390. (or (beginning-of-line 0) t)
  6391. (save-match-data
  6392. (looking-at "[ \t]*$")))))
  6393. (defun org-insert-heading (&optional force-heading invisible-ok)
  6394. "Insert a new heading or item with same depth at point.
  6395. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6396. If point is at the beginning of a headline, insert a sibling before the
  6397. current headline. If point is not at the beginning, split the line,
  6398. create the new headline with the text in the current line after point
  6399. \(but see also the variable `org-M-RET-may-split-line').
  6400. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6401. This is important for non-interactive uses of the command."
  6402. (interactive "P")
  6403. (if (or (= (buffer-size) 0)
  6404. (and (not (save-excursion
  6405. (and (ignore-errors (org-back-to-heading invisible-ok))
  6406. (org-at-heading-p))))
  6407. (or force-heading (not (org-in-item-p)))))
  6408. (progn
  6409. (insert "\n* ")
  6410. (run-hooks 'org-insert-heading-hook))
  6411. (when (or force-heading (not (org-insert-item)))
  6412. (let* ((empty-line-p nil)
  6413. (level nil)
  6414. (on-heading (org-at-heading-p))
  6415. (head (save-excursion
  6416. (condition-case nil
  6417. (progn
  6418. (org-back-to-heading invisible-ok)
  6419. (when (and (not on-heading)
  6420. (featurep 'org-inlinetask)
  6421. (integerp org-inlinetask-min-level)
  6422. (>= (length (match-string 0))
  6423. org-inlinetask-min-level))
  6424. ;; Find a heading level before the inline task
  6425. (while (and (setq level (org-up-heading-safe))
  6426. (>= level org-inlinetask-min-level)))
  6427. (if (org-at-heading-p)
  6428. (org-back-to-heading invisible-ok)
  6429. (error "This should not happen")))
  6430. (setq empty-line-p (org-previous-line-empty-p))
  6431. (match-string 0))
  6432. (error "*"))))
  6433. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6434. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6435. pos hide-previous previous-pos)
  6436. (cond
  6437. ((and (org-at-heading-p) (bolp)
  6438. (or (bobp)
  6439. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6440. ;; insert before the current line
  6441. (open-line (if blank 2 1)))
  6442. ((and (bolp)
  6443. (not org-insert-heading-respect-content)
  6444. (or (bobp)
  6445. (save-excursion
  6446. (backward-char 1) (not (outline-invisible-p)))))
  6447. ;; insert right here
  6448. nil)
  6449. (t
  6450. ;; somewhere in the line
  6451. (save-excursion
  6452. (setq previous-pos (point-at-bol))
  6453. (end-of-line)
  6454. (setq hide-previous (outline-invisible-p)))
  6455. (and org-insert-heading-respect-content (org-show-subtree))
  6456. (let ((split
  6457. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6458. (save-excursion
  6459. (let ((p (point)))
  6460. (goto-char (point-at-bol))
  6461. (and (looking-at org-complex-heading-regexp)
  6462. (match-beginning 4)
  6463. (> p (match-beginning 4)))))))
  6464. tags pos)
  6465. (cond
  6466. (org-insert-heading-respect-content
  6467. (org-end-of-subtree nil t)
  6468. (when (featurep 'org-inlinetask)
  6469. (while (and (not (eobp))
  6470. (looking-at "\\(\\*+\\)[ \t]+")
  6471. (>= (length (match-string 1))
  6472. org-inlinetask-min-level))
  6473. (org-end-of-subtree nil t)))
  6474. (or (bolp) (newline))
  6475. (or (org-previous-line-empty-p)
  6476. (and blank (newline)))
  6477. (open-line 1))
  6478. ((org-at-heading-p)
  6479. (when hide-previous
  6480. (show-children)
  6481. (org-show-entry))
  6482. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6483. (setq tags (and (match-end 2) (match-string 2)))
  6484. (and (match-end 1)
  6485. (delete-region (match-beginning 1) (match-end 1)))
  6486. (setq pos (point-at-bol))
  6487. (or split (end-of-line 1))
  6488. (delete-horizontal-space)
  6489. (if (string-match "\\`\\*+\\'"
  6490. (buffer-substring (point-at-bol) (point)))
  6491. (insert " "))
  6492. (newline (if blank 2 1))
  6493. (when tags
  6494. (save-excursion
  6495. (goto-char pos)
  6496. (end-of-line 1)
  6497. (insert " " tags)
  6498. (org-set-tags nil 'align))))
  6499. (t
  6500. (or split (end-of-line 1))
  6501. (newline (if blank 2 1)))))))
  6502. (insert head) (just-one-space)
  6503. (setq pos (point))
  6504. (end-of-line 1)
  6505. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6506. (when (and org-insert-heading-respect-content hide-previous)
  6507. (save-excursion
  6508. (goto-char previous-pos)
  6509. (hide-subtree)))
  6510. (run-hooks 'org-insert-heading-hook)))))
  6511. (defun org-get-heading (&optional no-tags no-todo)
  6512. "Return the heading of the current entry, without the stars.
  6513. When NO-TAGS is non-nil, don't include tags.
  6514. When NO-TODO is non-nil, don't include TODO keywords."
  6515. (save-excursion
  6516. (org-back-to-heading t)
  6517. (cond
  6518. ((and no-tags no-todo)
  6519. (looking-at org-complex-heading-regexp)
  6520. (match-string 4))
  6521. (no-tags
  6522. (looking-at (concat org-outline-regexp
  6523. "\\(.*?\\)"
  6524. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6525. (match-string 1))
  6526. (no-todo
  6527. (looking-at org-todo-line-regexp)
  6528. (match-string 3))
  6529. (t (looking-at org-heading-regexp)
  6530. (match-string 2)))))
  6531. (defun org-heading-components ()
  6532. "Return the components of the current heading.
  6533. This is a list with the following elements:
  6534. - the level as an integer
  6535. - the reduced level, different if `org-odd-levels-only' is set.
  6536. - the TODO keyword, or nil
  6537. - the priority character, like ?A, or nil if no priority is given
  6538. - the headline text itself, or the tags string if no headline text
  6539. - the tags string, or nil."
  6540. (save-excursion
  6541. (org-back-to-heading t)
  6542. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6543. (list (length (match-string 1))
  6544. (org-reduced-level (length (match-string 1)))
  6545. (org-match-string-no-properties 2)
  6546. (and (match-end 3) (aref (match-string 3) 2))
  6547. (org-match-string-no-properties 4)
  6548. (org-match-string-no-properties 5)))))
  6549. (defun org-get-entry ()
  6550. "Get the entry text, after heading, entire subtree."
  6551. (save-excursion
  6552. (org-back-to-heading t)
  6553. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6554. (defun org-insert-heading-after-current ()
  6555. "Insert a new heading with same level as current, after current subtree."
  6556. (interactive)
  6557. (org-back-to-heading)
  6558. (org-insert-heading)
  6559. (org-move-subtree-down)
  6560. (end-of-line 1))
  6561. (defun org-insert-heading-respect-content ()
  6562. (interactive)
  6563. (let ((org-insert-heading-respect-content t))
  6564. (org-insert-heading t)))
  6565. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6566. (interactive "P")
  6567. (let ((org-insert-heading-respect-content t))
  6568. (org-insert-todo-heading force-state t)))
  6569. (defun org-insert-todo-heading (arg &optional force-heading)
  6570. "Insert a new heading with the same level and TODO state as current heading.
  6571. If the heading has no TODO state, or if the state is DONE, use the first
  6572. state (TODO by default). Also with prefix arg, force first state."
  6573. (interactive "P")
  6574. (when (or force-heading (not (org-insert-item 'checkbox)))
  6575. (org-insert-heading force-heading)
  6576. (save-excursion
  6577. (org-back-to-heading)
  6578. (outline-previous-heading)
  6579. (looking-at org-todo-line-regexp))
  6580. (let*
  6581. ((new-mark-x
  6582. (if (or arg
  6583. (not (match-beginning 2))
  6584. (member (match-string 2) org-done-keywords))
  6585. (car org-todo-keywords-1)
  6586. (match-string 2)))
  6587. (new-mark
  6588. (or
  6589. (run-hook-with-args-until-success
  6590. 'org-todo-get-default-hook new-mark-x nil)
  6591. new-mark-x)))
  6592. (beginning-of-line 1)
  6593. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  6594. (if org-treat-insert-todo-heading-as-state-change
  6595. (org-todo new-mark)
  6596. (insert new-mark " "))))
  6597. (when org-provide-todo-statistics
  6598. (org-update-parent-todo-statistics))))
  6599. (defun org-insert-subheading (arg)
  6600. "Insert a new subheading and demote it.
  6601. Works for outline headings and for plain lists alike."
  6602. (interactive "P")
  6603. (org-insert-heading arg)
  6604. (cond
  6605. ((org-at-heading-p) (org-do-demote))
  6606. ((org-at-item-p) (org-indent-item))))
  6607. (defun org-insert-todo-subheading (arg)
  6608. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6609. Works for outline headings and for plain lists alike."
  6610. (interactive "P")
  6611. (org-insert-todo-heading arg)
  6612. (cond
  6613. ((org-at-heading-p) (org-do-demote))
  6614. ((org-at-item-p) (org-indent-item))))
  6615. ;;; Promotion and Demotion
  6616. (defvar org-after-demote-entry-hook nil
  6617. "Hook run after an entry has been demoted.
  6618. The cursor will be at the beginning of the entry.
  6619. When a subtree is being demoted, the hook will be called for each node.")
  6620. (defvar org-after-promote-entry-hook nil
  6621. "Hook run after an entry has been promoted.
  6622. The cursor will be at the beginning of the entry.
  6623. When a subtree is being promoted, the hook will be called for each node.")
  6624. (defun org-promote-subtree ()
  6625. "Promote the entire subtree.
  6626. See also `org-promote'."
  6627. (interactive)
  6628. (save-excursion
  6629. (org-with-limited-levels (org-map-tree 'org-promote)))
  6630. (org-fix-position-after-promote))
  6631. (defun org-demote-subtree ()
  6632. "Demote the entire subtree. See `org-demote'.
  6633. See also `org-promote'."
  6634. (interactive)
  6635. (save-excursion
  6636. (org-with-limited-levels (org-map-tree 'org-demote)))
  6637. (org-fix-position-after-promote))
  6638. (defun org-do-promote ()
  6639. "Promote the current heading higher up the tree.
  6640. If the region is active in `transient-mark-mode', promote all headings
  6641. in the region."
  6642. (interactive)
  6643. (save-excursion
  6644. (if (org-region-active-p)
  6645. (org-map-region 'org-promote (region-beginning) (region-end))
  6646. (org-promote)))
  6647. (org-fix-position-after-promote))
  6648. (defun org-do-demote ()
  6649. "Demote the current heading lower down the tree.
  6650. If the region is active in `transient-mark-mode', demote all headings
  6651. in the region."
  6652. (interactive)
  6653. (save-excursion
  6654. (if (org-region-active-p)
  6655. (org-map-region 'org-demote (region-beginning) (region-end))
  6656. (org-demote)))
  6657. (org-fix-position-after-promote))
  6658. (defun org-fix-position-after-promote ()
  6659. "Make sure that after pro/demotion cursor position is right."
  6660. (let ((pos (point)))
  6661. (when (save-excursion
  6662. (beginning-of-line 1)
  6663. (looking-at org-todo-line-regexp)
  6664. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6665. (cond ((eobp) (insert " "))
  6666. ((eolp) (insert " "))
  6667. ((equal (char-after) ?\ ) (forward-char 1))))))
  6668. (defun org-current-level ()
  6669. "Return the level of the current entry, or nil if before the first headline.
  6670. The level is the number of stars at the beginning of the headline."
  6671. (save-excursion
  6672. (org-with-limited-levels
  6673. (if (ignore-errors (org-back-to-heading t))
  6674. (funcall outline-level)))))
  6675. (defun org-get-previous-line-level ()
  6676. "Return the outline depth of the last headline before the current line.
  6677. Returns 0 for the first headline in the buffer, and nil if before the
  6678. first headline."
  6679. (let ((current-level (org-current-level))
  6680. (prev-level (when (> (line-number-at-pos) 1)
  6681. (save-excursion
  6682. (beginning-of-line 0)
  6683. (org-current-level)))))
  6684. (cond ((null current-level) nil) ; Before first headline
  6685. ((null prev-level) 0) ; At first headline
  6686. (prev-level))))
  6687. (defun org-reduced-level (l)
  6688. "Compute the effective level of a heading.
  6689. This takes into account the setting of `org-odd-levels-only'."
  6690. (cond
  6691. ((zerop l) 0)
  6692. (org-odd-levels-only (1+ (floor (/ l 2))))
  6693. (t l)))
  6694. (defun org-level-increment ()
  6695. "Return the number of stars that will be added or removed at a
  6696. time to headlines when structure editing, based on the value of
  6697. `org-odd-levels-only'."
  6698. (if org-odd-levels-only 2 1))
  6699. (defun org-get-valid-level (level &optional change)
  6700. "Rectify a level change under the influence of `org-odd-levels-only'
  6701. LEVEL is a current level, CHANGE is by how much the level should be
  6702. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6703. even level numbers will become the next higher odd number."
  6704. (if org-odd-levels-only
  6705. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6706. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6707. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6708. (max 1 (+ level (or change 0)))))
  6709. (if (boundp 'define-obsolete-function-alias)
  6710. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6711. (define-obsolete-function-alias 'org-get-legal-level
  6712. 'org-get-valid-level)
  6713. (define-obsolete-function-alias 'org-get-legal-level
  6714. 'org-get-valid-level "23.1")))
  6715. (defvar org-called-with-limited-levels nil) ;; Dynamically bound in
  6716. ;; ̀org-with-limited-levels'
  6717. (defun org-promote ()
  6718. "Promote the current heading higher up the tree.
  6719. If the region is active in `transient-mark-mode', promote all headings
  6720. in the region."
  6721. (org-back-to-heading t)
  6722. (let* ((level (save-match-data (funcall outline-level)))
  6723. (after-change-functions (remove 'flyspell-after-change-function
  6724. after-change-functions))
  6725. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6726. (diff (abs (- level (length up-head) -1))))
  6727. (cond ((and (= level 1) org-called-with-limited-levels
  6728. org-allow-promoting-top-level-subtree)
  6729. (replace-match "# " nil t))
  6730. ((= level 1)
  6731. (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6732. (t (replace-match up-head nil t)))
  6733. ;; Fixup tag positioning
  6734. (unless (= level 1)
  6735. (and org-auto-align-tags (org-set-tags nil t))
  6736. (if org-adapt-indentation (org-fixup-indentation (- diff))))
  6737. (run-hooks 'org-after-promote-entry-hook)))
  6738. (defun org-demote ()
  6739. "Demote the current heading lower down the tree.
  6740. If the region is active in `transient-mark-mode', demote all headings
  6741. in the region."
  6742. (org-back-to-heading t)
  6743. (let* ((level (save-match-data (funcall outline-level)))
  6744. (after-change-functions (remove 'flyspell-after-change-function
  6745. after-change-functions))
  6746. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6747. (diff (abs (- level (length down-head) -1))))
  6748. (replace-match down-head nil t)
  6749. ;; Fixup tag positioning
  6750. (and org-auto-align-tags (org-set-tags nil t))
  6751. (if org-adapt-indentation (org-fixup-indentation diff))
  6752. (run-hooks 'org-after-demote-entry-hook)))
  6753. (defun org-cycle-level ()
  6754. "Cycle the level of an empty headline through possible states.
  6755. This goes first to child, then to parent, level, then up the hierarchy.
  6756. After top level, it switches back to sibling level."
  6757. (interactive)
  6758. (let ((org-adapt-indentation nil))
  6759. (when (org-point-at-end-of-empty-headline)
  6760. (setq this-command 'org-cycle-level) ; Only needed for caching
  6761. (let ((cur-level (org-current-level))
  6762. (prev-level (org-get-previous-line-level)))
  6763. (cond
  6764. ;; If first headline in file, promote to top-level.
  6765. ((= prev-level 0)
  6766. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6767. do (org-do-promote)))
  6768. ;; If same level as prev, demote one.
  6769. ((= prev-level cur-level)
  6770. (org-do-demote))
  6771. ;; If parent is top-level, promote to top level if not already.
  6772. ((= prev-level 1)
  6773. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6774. do (org-do-promote)))
  6775. ;; If top-level, return to prev-level.
  6776. ((= cur-level 1)
  6777. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6778. do (org-do-demote)))
  6779. ;; If less than prev-level, promote one.
  6780. ((< cur-level prev-level)
  6781. (org-do-promote))
  6782. ;; If deeper than prev-level, promote until higher than
  6783. ;; prev-level.
  6784. ((> cur-level prev-level)
  6785. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6786. do (org-do-promote))))
  6787. t))))
  6788. (defun org-map-tree (fun)
  6789. "Call FUN for every heading underneath the current one."
  6790. (org-back-to-heading)
  6791. (let ((level (funcall outline-level)))
  6792. (save-excursion
  6793. (funcall fun)
  6794. (while (and (progn
  6795. (outline-next-heading)
  6796. (> (funcall outline-level) level))
  6797. (not (eobp)))
  6798. (funcall fun)))))
  6799. (defun org-map-region (fun beg end)
  6800. "Call FUN for every heading between BEG and END."
  6801. (let ((org-ignore-region t))
  6802. (save-excursion
  6803. (setq end (copy-marker end))
  6804. (goto-char beg)
  6805. (if (and (re-search-forward org-outline-regexp-bol nil t)
  6806. (< (point) end))
  6807. (funcall fun))
  6808. (while (and (progn
  6809. (outline-next-heading)
  6810. (< (point) end))
  6811. (not (eobp)))
  6812. (funcall fun)))))
  6813. (defvar org-property-end-re) ; silence byte-compiler
  6814. (defun org-fixup-indentation (diff)
  6815. "Change the indentation in the current entry by DIFF.
  6816. However, if any line in the current entry has no indentation, or if it
  6817. would end up with no indentation after the change, nothing at all is done."
  6818. (save-excursion
  6819. (let ((end (save-excursion (outline-next-heading)
  6820. (point-marker)))
  6821. (prohibit (if (> diff 0)
  6822. "^\\S-"
  6823. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6824. col)
  6825. (unless (save-excursion (end-of-line 1)
  6826. (re-search-forward prohibit end t))
  6827. (while (and (< (point) end)
  6828. (re-search-forward "^[ \t]+" end t))
  6829. (goto-char (match-end 0))
  6830. (setq col (current-column))
  6831. (if (< diff 0) (replace-match ""))
  6832. (org-indent-to-column (+ diff col))))
  6833. (move-marker end nil))))
  6834. (defun org-convert-to-odd-levels ()
  6835. "Convert an org-mode file with all levels allowed to one with odd levels.
  6836. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6837. level 5 etc."
  6838. (interactive)
  6839. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6840. (let ((outline-level 'org-outline-level)
  6841. (org-odd-levels-only nil) n)
  6842. (save-excursion
  6843. (goto-char (point-min))
  6844. (while (re-search-forward "^\\*\\*+ " nil t)
  6845. (setq n (- (length (match-string 0)) 2))
  6846. (while (>= (setq n (1- n)) 0)
  6847. (org-demote))
  6848. (end-of-line 1))))))
  6849. (defun org-convert-to-oddeven-levels ()
  6850. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6851. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6852. file contains a section with an even level, conversion would
  6853. destroy the structure of the file. An error is signaled in this
  6854. case."
  6855. (interactive)
  6856. (goto-char (point-min))
  6857. ;; First check if there are no even levels
  6858. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6859. (org-show-context t)
  6860. (error "Not all levels are odd in this file. Conversion not possible"))
  6861. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6862. (let ((outline-regexp org-outline-regexp)
  6863. (outline-level 'org-outline-level)
  6864. (org-odd-levels-only nil) n)
  6865. (save-excursion
  6866. (goto-char (point-min))
  6867. (while (re-search-forward "^\\*\\*+ " nil t)
  6868. (setq n (/ (1- (length (match-string 0))) 2))
  6869. (while (>= (setq n (1- n)) 0)
  6870. (org-promote))
  6871. (end-of-line 1))))))
  6872. (defun org-tr-level (n)
  6873. "Make N odd if required."
  6874. (if org-odd-levels-only (1+ (/ n 2)) n))
  6875. ;;; Vertical tree motion, cutting and pasting of subtrees
  6876. (defun org-move-subtree-up (&optional arg)
  6877. "Move the current subtree up past ARG headlines of the same level."
  6878. (interactive "p")
  6879. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6880. (defun org-move-subtree-down (&optional arg)
  6881. "Move the current subtree down past ARG headlines of the same level."
  6882. (interactive "p")
  6883. (setq arg (prefix-numeric-value arg))
  6884. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6885. 'org-get-last-sibling))
  6886. (ins-point (make-marker))
  6887. (cnt (abs arg))
  6888. (col (current-column))
  6889. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6890. ;; Select the tree
  6891. (org-back-to-heading)
  6892. (setq beg0 (point))
  6893. (save-excursion
  6894. (setq ne-beg (org-back-over-empty-lines))
  6895. (setq beg (point)))
  6896. (save-match-data
  6897. (save-excursion (outline-end-of-heading)
  6898. (setq folded (outline-invisible-p)))
  6899. (outline-end-of-subtree))
  6900. (outline-next-heading)
  6901. (setq ne-end (org-back-over-empty-lines))
  6902. (setq end (point))
  6903. (goto-char beg0)
  6904. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6905. ;; include less whitespace
  6906. (save-excursion
  6907. (goto-char beg)
  6908. (forward-line (- ne-beg ne-end))
  6909. (setq beg (point))))
  6910. ;; Find insertion point, with error handling
  6911. (while (> cnt 0)
  6912. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  6913. (progn (goto-char beg0)
  6914. (error "Cannot move past superior level or buffer limit")))
  6915. (setq cnt (1- cnt)))
  6916. (if (> arg 0)
  6917. ;; Moving forward - still need to move over subtree
  6918. (progn (org-end-of-subtree t t)
  6919. (save-excursion
  6920. (org-back-over-empty-lines)
  6921. (or (bolp) (newline)))))
  6922. (setq ne-ins (org-back-over-empty-lines))
  6923. (move-marker ins-point (point))
  6924. (setq txt (buffer-substring beg end))
  6925. (org-save-markers-in-region beg end)
  6926. (delete-region beg end)
  6927. (org-remove-empty-overlays-at beg)
  6928. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6929. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6930. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6931. (let ((bbb (point)))
  6932. (insert-before-markers txt)
  6933. (org-reinstall-markers-in-region bbb)
  6934. (move-marker ins-point bbb))
  6935. (or (bolp) (insert "\n"))
  6936. (setq ins-end (point))
  6937. (goto-char ins-point)
  6938. (org-skip-whitespace)
  6939. (when (and (< arg 0)
  6940. (org-first-sibling-p)
  6941. (> ne-ins ne-beg))
  6942. ;; Move whitespace back to beginning
  6943. (save-excursion
  6944. (goto-char ins-end)
  6945. (let ((kill-whole-line t))
  6946. (kill-line (- ne-ins ne-beg)) (point)))
  6947. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6948. (move-marker ins-point nil)
  6949. (if folded
  6950. (hide-subtree)
  6951. (org-show-entry)
  6952. (show-children)
  6953. (org-cycle-hide-drawers 'children))
  6954. (org-clean-visibility-after-subtree-move)
  6955. ;; move back to the initial column we were at
  6956. (move-to-column col)))
  6957. (defvar org-subtree-clip ""
  6958. "Clipboard for cut and paste of subtrees.
  6959. This is actually only a copy of the kill, because we use the normal kill
  6960. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6961. (defvar org-subtree-clip-folded nil
  6962. "Was the last copied subtree folded?
  6963. This is used to fold the tree back after pasting.")
  6964. (defun org-cut-subtree (&optional n)
  6965. "Cut the current subtree into the clipboard.
  6966. With prefix arg N, cut this many sequential subtrees.
  6967. This is a short-hand for marking the subtree and then cutting it."
  6968. (interactive "p")
  6969. (org-copy-subtree n 'cut))
  6970. (defun org-copy-subtree (&optional n cut force-store-markers)
  6971. "Cut the current subtree into the clipboard.
  6972. With prefix arg N, cut this many sequential subtrees.
  6973. This is a short-hand for marking the subtree and then copying it.
  6974. If CUT is non-nil, actually cut the subtree.
  6975. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6976. of some markers in the region, even if CUT is non-nil. This is
  6977. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6978. (interactive "p")
  6979. (let (beg end folded (beg0 (point)))
  6980. (if (org-called-interactively-p 'any)
  6981. (org-back-to-heading nil) ; take what looks like a subtree
  6982. (org-back-to-heading t)) ; take what is really there
  6983. (org-back-over-empty-lines)
  6984. (setq beg (point))
  6985. (skip-chars-forward " \t\r\n")
  6986. (save-match-data
  6987. (save-excursion (outline-end-of-heading)
  6988. (setq folded (outline-invisible-p)))
  6989. (condition-case nil
  6990. (org-forward-same-level (1- n) t)
  6991. (error nil))
  6992. (org-end-of-subtree t t))
  6993. (org-back-over-empty-lines)
  6994. (setq end (point))
  6995. (goto-char beg0)
  6996. (when (> end beg)
  6997. (setq org-subtree-clip-folded folded)
  6998. (when (or cut force-store-markers)
  6999. (org-save-markers-in-region beg end))
  7000. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7001. (setq org-subtree-clip (current-kill 0))
  7002. (message "%s: Subtree(s) with %d characters"
  7003. (if cut "Cut" "Copied")
  7004. (length org-subtree-clip)))))
  7005. (defun org-paste-subtree (&optional level tree for-yank)
  7006. "Paste the clipboard as a subtree, with modification of headline level.
  7007. The entire subtree is promoted or demoted in order to match a new headline
  7008. level.
  7009. If the cursor is at the beginning of a headline, the same level as
  7010. that headline is used to paste the tree
  7011. If not, the new level is derived from the *visible* headings
  7012. before and after the insertion point, and taken to be the inferior headline
  7013. level of the two. So if the previous visible heading is level 3 and the
  7014. next is level 4 (or vice versa), level 4 will be used for insertion.
  7015. This makes sure that the subtree remains an independent subtree and does
  7016. not swallow low level entries.
  7017. You can also force a different level, either by using a numeric prefix
  7018. argument, or by inserting the heading marker by hand. For example, if the
  7019. cursor is after \"*****\", then the tree will be shifted to level 5.
  7020. If optional TREE is given, use this text instead of the kill ring.
  7021. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7022. move back over whitespace before inserting, and move point to the end of
  7023. the inserted text when done."
  7024. (interactive "P")
  7025. (setq tree (or tree (and kill-ring (current-kill 0))))
  7026. (unless (org-kill-is-subtree-p tree)
  7027. (error "%s"
  7028. (substitute-command-keys
  7029. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7030. (org-with-limited-levels
  7031. (let* ((visp (not (outline-invisible-p)))
  7032. (txt tree)
  7033. (^re_ "\\(\\*+\\)[ \t]*")
  7034. (old-level (if (string-match org-outline-regexp-bol txt)
  7035. (- (match-end 0) (match-beginning 0) 1)
  7036. -1))
  7037. (force-level (cond (level (prefix-numeric-value level))
  7038. ((and (looking-at "[ \t]*$")
  7039. (string-match
  7040. "^\\*+$" (buffer-substring
  7041. (point-at-bol) (point))))
  7042. (- (match-end 1) (match-beginning 1)))
  7043. ((and (bolp)
  7044. (looking-at org-outline-regexp))
  7045. (- (match-end 0) (point) 1))
  7046. (t nil)))
  7047. (previous-level (save-excursion
  7048. (condition-case nil
  7049. (progn
  7050. (outline-previous-visible-heading 1)
  7051. (if (looking-at ^re_)
  7052. (- (match-end 0) (match-beginning 0) 1)
  7053. 1))
  7054. (error 1))))
  7055. (next-level (save-excursion
  7056. (condition-case nil
  7057. (progn
  7058. (or (looking-at org-outline-regexp)
  7059. (outline-next-visible-heading 1))
  7060. (if (looking-at ^re_)
  7061. (- (match-end 0) (match-beginning 0) 1)
  7062. 1))
  7063. (error 1))))
  7064. (new-level (or force-level (max previous-level next-level)))
  7065. (shift (if (or (= old-level -1)
  7066. (= new-level -1)
  7067. (= old-level new-level))
  7068. 0
  7069. (- new-level old-level)))
  7070. (delta (if (> shift 0) -1 1))
  7071. (func (if (> shift 0) 'org-demote 'org-promote))
  7072. (org-odd-levels-only nil)
  7073. beg end newend)
  7074. ;; Remove the forced level indicator
  7075. (if force-level
  7076. (delete-region (point-at-bol) (point)))
  7077. ;; Paste
  7078. (beginning-of-line (if (bolp) 1 2))
  7079. (unless for-yank (org-back-over-empty-lines))
  7080. (setq beg (point))
  7081. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7082. (insert-before-markers txt)
  7083. (unless (string-match "\n\\'" txt) (insert "\n"))
  7084. (setq newend (point))
  7085. (org-reinstall-markers-in-region beg)
  7086. (setq end (point))
  7087. (goto-char beg)
  7088. (skip-chars-forward " \t\n\r")
  7089. (setq beg (point))
  7090. (if (and (outline-invisible-p) visp)
  7091. (save-excursion (outline-show-heading)))
  7092. ;; Shift if necessary
  7093. (unless (= shift 0)
  7094. (save-restriction
  7095. (narrow-to-region beg end)
  7096. (while (not (= shift 0))
  7097. (org-map-region func (point-min) (point-max))
  7098. (setq shift (+ delta shift)))
  7099. (goto-char (point-min))
  7100. (setq newend (point-max))))
  7101. (when (or (org-called-interactively-p 'interactive) for-yank)
  7102. (message "Clipboard pasted as level %d subtree" new-level))
  7103. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7104. kill-ring
  7105. (eq org-subtree-clip (current-kill 0))
  7106. org-subtree-clip-folded)
  7107. ;; The tree was folded before it was killed/copied
  7108. (hide-subtree))
  7109. (and for-yank (goto-char newend)))))
  7110. (defun org-kill-is-subtree-p (&optional txt)
  7111. "Check if the current kill is an outline subtree, or a set of trees.
  7112. Returns nil if kill does not start with a headline, or if the first
  7113. headline level is not the largest headline level in the tree.
  7114. So this will actually accept several entries of equal levels as well,
  7115. which is OK for `org-paste-subtree'.
  7116. If optional TXT is given, check this string instead of the current kill."
  7117. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7118. (re (org-get-limited-outline-regexp))
  7119. (^re (concat "^" re))
  7120. (start-level (and kill
  7121. (string-match
  7122. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7123. kill)
  7124. (- (match-end 2) (match-beginning 2) 1)))
  7125. (start (1+ (or (match-beginning 2) -1))))
  7126. (if (not start-level)
  7127. (progn
  7128. nil) ;; does not even start with a heading
  7129. (catch 'exit
  7130. (while (setq start (string-match ^re kill (1+ start)))
  7131. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7132. (throw 'exit nil)))
  7133. t))))
  7134. (defvar org-markers-to-move nil
  7135. "Markers that should be moved with a cut-and-paste operation.
  7136. Those markers are stored together with their positions relative to
  7137. the start of the region.")
  7138. (defun org-save-markers-in-region (beg end)
  7139. "Check markers in region.
  7140. If these markers are between BEG and END, record their position relative
  7141. to BEG, so that after moving the block of text, we can put the markers back
  7142. into place.
  7143. This function gets called just before an entry or tree gets cut from the
  7144. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7145. called immediately, to move the markers with the entries."
  7146. (setq org-markers-to-move nil)
  7147. (when (featurep 'org-clock)
  7148. (org-clock-save-markers-for-cut-and-paste beg end))
  7149. (when (featurep 'org-agenda)
  7150. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7151. (defun org-check-and-save-marker (marker beg end)
  7152. "Check if MARKER is between BEG and END.
  7153. If yes, remember the marker and the distance to BEG."
  7154. (when (and (marker-buffer marker)
  7155. (equal (marker-buffer marker) (current-buffer)))
  7156. (if (and (>= marker beg) (< marker end))
  7157. (push (cons marker (- marker beg)) org-markers-to-move))))
  7158. (defun org-reinstall-markers-in-region (beg)
  7159. "Move all remembered markers to their position relative to BEG."
  7160. (mapc (lambda (x)
  7161. (move-marker (car x) (+ beg (cdr x))))
  7162. org-markers-to-move)
  7163. (setq org-markers-to-move nil))
  7164. (defun org-narrow-to-subtree ()
  7165. "Narrow buffer to the current subtree."
  7166. (interactive)
  7167. (save-excursion
  7168. (save-match-data
  7169. (org-with-limited-levels
  7170. (narrow-to-region
  7171. (progn (org-back-to-heading t) (point))
  7172. (progn (org-end-of-subtree t t)
  7173. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7174. (point)))))))
  7175. (defun org-narrow-to-block ()
  7176. "Narrow buffer to the current block."
  7177. (interactive)
  7178. (let* ((case-fold-search t)
  7179. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7180. "^[ \t]*#\\+end_.*")))
  7181. (if blockp
  7182. (narrow-to-region (car blockp) (cdr blockp))
  7183. (error "Not in a block"))))
  7184. (eval-when-compile
  7185. (defvar org-property-drawer-re))
  7186. (defvar org-property-start-re) ;; defined below
  7187. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7188. "Clone the task (subtree) at point N times.
  7189. The clones will be inserted as siblings.
  7190. In interactive use, the user will be prompted for the number of
  7191. clones to be produced, and for a time SHIFT, which may be a
  7192. repeater as used in time stamps, for example `+3d'.
  7193. When a valid repeater is given and the entry contains any time
  7194. stamps, the clones will become a sequence in time, with time
  7195. stamps in the subtree shifted for each clone produced. If SHIFT
  7196. is nil or the empty string, time stamps will be left alone. The
  7197. ID property of the original subtree is removed.
  7198. If the original subtree did contain time stamps with a repeater,
  7199. the following will happen:
  7200. - the repeater will be removed in each clone
  7201. - an additional clone will be produced, with the current, unshifted
  7202. date(s) in the entry.
  7203. - the original entry will be placed *after* all the clones, with
  7204. repeater intact.
  7205. - the start days in the repeater in the original entry will be shifted
  7206. to past the last clone.
  7207. In this way you can spell out a number of instances of a repeating task,
  7208. and still retain the repeater to cover future instances of the task."
  7209. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  7210. (let (beg end template task idprop
  7211. shift-n shift-what doshift nmin nmax (n-no-remove -1)
  7212. (drawer-re org-drawer-regexp))
  7213. (if (not (and (integerp n) (> n 0)))
  7214. (error "Invalid number of replications %s" n))
  7215. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7216. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7217. shift)))
  7218. (error "Invalid shift specification %s" shift))
  7219. (when doshift
  7220. (setq shift-n (string-to-number (match-string 1 shift))
  7221. shift-what (cdr (assoc (match-string 2 shift)
  7222. '(("d" . day) ("w" . week)
  7223. ("m" . month) ("y" . year))))))
  7224. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7225. (setq nmin 1 nmax n)
  7226. (org-back-to-heading t)
  7227. (setq beg (point))
  7228. (setq idprop (org-entry-get nil "ID"))
  7229. (org-end-of-subtree t t)
  7230. (or (bolp) (insert "\n"))
  7231. (setq end (point))
  7232. (setq template (buffer-substring beg end))
  7233. (when (and doshift
  7234. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7235. (delete-region beg end)
  7236. (setq end beg)
  7237. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7238. (goto-char end)
  7239. (loop for n from nmin to nmax do
  7240. ;; prepare clone
  7241. (with-temp-buffer
  7242. (insert template)
  7243. (org-mode)
  7244. (goto-char (point-min))
  7245. (org-show-subtree)
  7246. (and idprop (if org-clone-delete-id
  7247. (org-entry-delete nil "ID")
  7248. (org-id-get-create t)))
  7249. (unless (= n 0)
  7250. (while (re-search-forward "^[ \t]*CLOCK:.*$" nil t)
  7251. (kill-whole-line))
  7252. (goto-char (point-min))
  7253. (while (re-search-forward drawer-re nil t)
  7254. (mapc (lambda (d)
  7255. (org-remove-empty-drawer-at d (point))) org-drawers)))
  7256. (goto-char (point-min))
  7257. (when doshift
  7258. (while (re-search-forward org-ts-regexp-both nil t)
  7259. (org-timestamp-change (* n shift-n) shift-what))
  7260. (unless (= n n-no-remove)
  7261. (goto-char (point-min))
  7262. (while (re-search-forward org-ts-regexp nil t)
  7263. (save-excursion
  7264. (goto-char (match-beginning 0))
  7265. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7266. (delete-region (match-beginning 1) (match-end 1)))))))
  7267. (setq task (buffer-string)))
  7268. (insert task))
  7269. (goto-char beg)))
  7270. ;;; Outline Sorting
  7271. (defun org-sort (with-case)
  7272. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7273. Optional argument WITH-CASE means sort case-sensitively."
  7274. (interactive "P")
  7275. (cond
  7276. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7277. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7278. (t
  7279. (org-call-with-arg 'org-sort-entries with-case))))
  7280. (defun org-sort-remove-invisible (s)
  7281. (remove-text-properties 0 (length s) org-rm-props s)
  7282. (while (string-match org-bracket-link-regexp s)
  7283. (setq s (replace-match (if (match-end 2)
  7284. (match-string 3 s)
  7285. (match-string 1 s)) t t s)))
  7286. s)
  7287. (defvar org-priority-regexp) ; defined later in the file
  7288. (defvar org-after-sorting-entries-or-items-hook nil
  7289. "Hook that is run after a bunch of entries or items have been sorted.
  7290. When children are sorted, the cursor is in the parent line when this
  7291. hook gets called. When a region or a plain list is sorted, the cursor
  7292. will be in the first entry of the sorted region/list.")
  7293. (defun org-sort-entries
  7294. (&optional with-case sorting-type getkey-func compare-func property)
  7295. "Sort entries on a certain level of an outline tree.
  7296. If there is an active region, the entries in the region are sorted.
  7297. Else, if the cursor is before the first entry, sort the top-level items.
  7298. Else, the children of the entry at point are sorted.
  7299. Sorting can be alphabetically, numerically, by date/time as given by
  7300. a time stamp, by a property or by priority.
  7301. The command prompts for the sorting type unless it has been given to the
  7302. function through the SORTING-TYPE argument, which needs to be a character,
  7303. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  7304. precise meaning of each character:
  7305. n Numerically, by converting the beginning of the entry/item to a number.
  7306. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7307. t By date/time, either the first active time stamp in the entry, or, if
  7308. none exist, by the first inactive one.
  7309. s By the scheduled date/time.
  7310. d By deadline date/time.
  7311. c By creation time, which is assumed to be the first inactive time stamp
  7312. at the beginning of a line.
  7313. p By priority according to the cookie.
  7314. r By the value of a property.
  7315. Capital letters will reverse the sort order.
  7316. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7317. called with point at the beginning of the record. It must return either
  7318. a string or a number that should serve as the sorting key for that record.
  7319. Comparing entries ignores case by default. However, with an optional argument
  7320. WITH-CASE, the sorting considers case as well."
  7321. (interactive "P")
  7322. (let ((case-func (if with-case 'identity 'downcase))
  7323. start beg end stars re re2
  7324. txt what tmp)
  7325. ;; Find beginning and end of region to sort
  7326. (cond
  7327. ((org-region-active-p)
  7328. ;; we will sort the region
  7329. (setq end (region-end)
  7330. what "region")
  7331. (goto-char (region-beginning))
  7332. (if (not (org-at-heading-p)) (outline-next-heading))
  7333. (setq start (point)))
  7334. ((or (org-at-heading-p)
  7335. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7336. ;; we will sort the children of the current headline
  7337. (org-back-to-heading)
  7338. (setq start (point)
  7339. end (progn (org-end-of-subtree t t)
  7340. (or (bolp) (insert "\n"))
  7341. (org-back-over-empty-lines)
  7342. (point))
  7343. what "children")
  7344. (goto-char start)
  7345. (show-subtree)
  7346. (outline-next-heading))
  7347. (t
  7348. ;; we will sort the top-level entries in this file
  7349. (goto-char (point-min))
  7350. (or (org-at-heading-p) (outline-next-heading))
  7351. (setq start (point))
  7352. (goto-char (point-max))
  7353. (beginning-of-line 1)
  7354. (when (looking-at ".*?\\S-")
  7355. ;; File ends in a non-white line
  7356. (end-of-line 1)
  7357. (insert "\n"))
  7358. (setq end (point-max))
  7359. (setq what "top-level")
  7360. (goto-char start)
  7361. (show-all)))
  7362. (setq beg (point))
  7363. (if (>= beg end) (error "Nothing to sort"))
  7364. (looking-at "\\(\\*+\\)")
  7365. (setq stars (match-string 1)
  7366. re (concat "^" (regexp-quote stars) " +")
  7367. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7368. txt (buffer-substring beg end))
  7369. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7370. (if (and (not (equal stars "*")) (string-match re2 txt))
  7371. (error "Region to sort contains a level above the first entry"))
  7372. (unless sorting-type
  7373. (message
  7374. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7375. [t]ime [s]cheduled [d]eadline [c]reated
  7376. A/N/T/S/D/C/P/O/F means reversed:"
  7377. what)
  7378. (setq sorting-type (read-char-exclusive))
  7379. (and (= (downcase sorting-type) ?f)
  7380. (setq getkey-func
  7381. (org-icompleting-read "Sort using function: "
  7382. obarray 'fboundp t nil nil))
  7383. (setq getkey-func (intern getkey-func)))
  7384. (and (= (downcase sorting-type) ?r)
  7385. (setq property
  7386. (org-icompleting-read "Property: "
  7387. (mapcar 'list (org-buffer-property-keys t))
  7388. nil t))))
  7389. (message "Sorting entries...")
  7390. (save-restriction
  7391. (narrow-to-region start end)
  7392. (let ((dcst (downcase sorting-type))
  7393. (case-fold-search nil)
  7394. (now (current-time)))
  7395. (sort-subr
  7396. (/= dcst sorting-type)
  7397. ;; This function moves to the beginning character of the "record" to
  7398. ;; be sorted.
  7399. (lambda nil
  7400. (if (re-search-forward re nil t)
  7401. (goto-char (match-beginning 0))
  7402. (goto-char (point-max))))
  7403. ;; This function moves to the last character of the "record" being
  7404. ;; sorted.
  7405. (lambda nil
  7406. (save-match-data
  7407. (condition-case nil
  7408. (outline-forward-same-level 1)
  7409. (error
  7410. (goto-char (point-max))))))
  7411. ;; This function returns the value that gets sorted against.
  7412. (lambda nil
  7413. (cond
  7414. ((= dcst ?n)
  7415. (if (looking-at org-complex-heading-regexp)
  7416. (string-to-number (match-string 4))
  7417. nil))
  7418. ((= dcst ?a)
  7419. (if (looking-at org-complex-heading-regexp)
  7420. (funcall case-func (match-string 4))
  7421. nil))
  7422. ((= dcst ?t)
  7423. (let ((end (save-excursion (outline-next-heading) (point))))
  7424. (if (or (re-search-forward org-ts-regexp end t)
  7425. (re-search-forward org-ts-regexp-both end t))
  7426. (org-time-string-to-seconds (match-string 0))
  7427. (org-float-time now))))
  7428. ((= dcst ?c)
  7429. (let ((end (save-excursion (outline-next-heading) (point))))
  7430. (if (re-search-forward
  7431. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7432. end t)
  7433. (org-time-string-to-seconds (match-string 0))
  7434. (org-float-time now))))
  7435. ((= dcst ?s)
  7436. (let ((end (save-excursion (outline-next-heading) (point))))
  7437. (if (re-search-forward org-scheduled-time-regexp end t)
  7438. (org-time-string-to-seconds (match-string 1))
  7439. (org-float-time now))))
  7440. ((= dcst ?d)
  7441. (let ((end (save-excursion (outline-next-heading) (point))))
  7442. (if (re-search-forward org-deadline-time-regexp end t)
  7443. (org-time-string-to-seconds (match-string 1))
  7444. (org-float-time now))))
  7445. ((= dcst ?p)
  7446. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7447. (string-to-char (match-string 2))
  7448. org-default-priority))
  7449. ((= dcst ?r)
  7450. (or (org-entry-get nil property) ""))
  7451. ((= dcst ?o)
  7452. (if (looking-at org-complex-heading-regexp)
  7453. (- 9999 (length (member (match-string 2)
  7454. org-todo-keywords-1)))))
  7455. ((= dcst ?f)
  7456. (if getkey-func
  7457. (progn
  7458. (setq tmp (funcall getkey-func))
  7459. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7460. tmp)
  7461. (error "Invalid key function `%s'" getkey-func)))
  7462. (t (error "Invalid sorting type `%c'" sorting-type))))
  7463. nil
  7464. (cond
  7465. ((= dcst ?a) 'string<)
  7466. ((= dcst ?f) compare-func)
  7467. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7468. (t nil)))))
  7469. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7470. (message "Sorting entries...done")))
  7471. (defun org-do-sort (table what &optional with-case sorting-type)
  7472. "Sort TABLE of WHAT according to SORTING-TYPE.
  7473. The user will be prompted for the SORTING-TYPE if the call to this
  7474. function does not specify it. WHAT is only for the prompt, to indicate
  7475. what is being sorted. The sorting key will be extracted from
  7476. the car of the elements of the table.
  7477. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7478. (unless sorting-type
  7479. (message
  7480. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7481. what)
  7482. (setq sorting-type (read-char-exclusive)))
  7483. (let ((dcst (downcase sorting-type))
  7484. extractfun comparefun)
  7485. ;; Define the appropriate functions
  7486. (cond
  7487. ((= dcst ?n)
  7488. (setq extractfun 'string-to-number
  7489. comparefun (if (= dcst sorting-type) '< '>)))
  7490. ((= dcst ?a)
  7491. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7492. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7493. comparefun (if (= dcst sorting-type)
  7494. 'string<
  7495. (lambda (a b) (and (not (string< a b))
  7496. (not (string= a b)))))))
  7497. ((= dcst ?t)
  7498. (setq extractfun
  7499. (lambda (x)
  7500. (if (or (string-match org-ts-regexp x)
  7501. (string-match org-ts-regexp-both x))
  7502. (org-float-time
  7503. (org-time-string-to-time (match-string 0 x)))
  7504. 0))
  7505. comparefun (if (= dcst sorting-type) '< '>)))
  7506. (t (error "Invalid sorting type `%c'" sorting-type)))
  7507. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7508. table)
  7509. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7510. ;;; The orgstruct minor mode
  7511. ;; Define a minor mode which can be used in other modes in order to
  7512. ;; integrate the org-mode structure editing commands.
  7513. ;; This is really a hack, because the org-mode structure commands use
  7514. ;; keys which normally belong to the major mode. Here is how it
  7515. ;; works: The minor mode defines all the keys necessary to operate the
  7516. ;; structure commands, but wraps the commands into a function which
  7517. ;; tests if the cursor is currently at a headline or a plain list
  7518. ;; item. If that is the case, the structure command is used,
  7519. ;; temporarily setting many Org-mode variables like regular
  7520. ;; expressions for filling etc. However, when any of those keys is
  7521. ;; used at a different location, function uses `key-binding' to look
  7522. ;; up if the key has an associated command in another currently active
  7523. ;; keymap (minor modes, major mode, global), and executes that
  7524. ;; command. There might be problems if any of the keys is otherwise
  7525. ;; used as a prefix key.
  7526. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7527. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7528. ;; addresses this by checking explicitly for both bindings.
  7529. (defvar orgstruct-mode-map (make-sparse-keymap)
  7530. "Keymap for the minor `orgstruct-mode'.")
  7531. (defvar org-local-vars nil
  7532. "List of local variables, for use by `orgstruct-mode'.")
  7533. ;;;###autoload
  7534. (define-minor-mode orgstruct-mode
  7535. "Toggle the minor mode `orgstruct-mode'.
  7536. This mode is for using Org-mode structure commands in other
  7537. modes. The following keys behave as if Org-mode were active, if
  7538. the cursor is on a headline, or on a plain list item (both as
  7539. defined by Org-mode).
  7540. M-up Move entry/item up
  7541. M-down Move entry/item down
  7542. M-left Promote
  7543. M-right Demote
  7544. M-S-up Move entry/item up
  7545. M-S-down Move entry/item down
  7546. M-S-left Promote subtree
  7547. M-S-right Demote subtree
  7548. M-q Fill paragraph and items like in Org-mode
  7549. C-c ^ Sort entries
  7550. C-c - Cycle list bullet
  7551. TAB Cycle item visibility
  7552. M-RET Insert new heading/item
  7553. S-M-RET Insert new TODO heading / Checkbox item
  7554. C-c C-c Set tags / toggle checkbox"
  7555. nil " OrgStruct" nil
  7556. (org-load-modules-maybe)
  7557. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7558. ;;;###autoload
  7559. (defun turn-on-orgstruct ()
  7560. "Unconditionally turn on `orgstruct-mode'."
  7561. (orgstruct-mode 1))
  7562. (defvar org-fb-vars nil)
  7563. (make-variable-buffer-local 'org-fb-vars)
  7564. (defun orgstruct++-mode (&optional arg)
  7565. "Toggle `orgstruct-mode', the enhanced version of it.
  7566. In addition to setting orgstruct-mode, this also exports all
  7567. indentation and autofilling variables from org-mode into the
  7568. buffer. It will also recognize item context in multiline items."
  7569. (interactive "P")
  7570. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7571. (if (< arg 1)
  7572. (progn (orgstruct-mode -1)
  7573. (mapc (lambda(v)
  7574. (org-set-local (car v)
  7575. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  7576. org-fb-vars))
  7577. (orgstruct-mode 1)
  7578. (setq org-fb-vars nil)
  7579. (let (var val)
  7580. (mapc
  7581. (lambda (x)
  7582. (when (string-match
  7583. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7584. (symbol-name (car x)))
  7585. (setq var (car x) val (nth 1 x))
  7586. (push (list var `(quote ,(eval var))) org-fb-vars)
  7587. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7588. org-local-vars)
  7589. (org-set-local 'orgstruct-is-++ t))))
  7590. (defvar orgstruct-is-++ nil
  7591. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7592. (make-variable-buffer-local 'orgstruct-is-++)
  7593. ;;;###autoload
  7594. (defun turn-on-orgstruct++ ()
  7595. "Unconditionally turn on `orgstruct++-mode'."
  7596. (orgstruct++-mode 1))
  7597. (defun orgstruct-error ()
  7598. "Error when there is no default binding for a structure key."
  7599. (interactive)
  7600. (error "This key has no function outside structure elements"))
  7601. (defun orgstruct-setup ()
  7602. "Setup orgstruct keymaps."
  7603. (let ((nfunc 0)
  7604. (bindings
  7605. (list
  7606. '([(meta up)] org-metaup)
  7607. '([(meta down)] org-metadown)
  7608. '([(meta left)] org-metaleft)
  7609. '([(meta right)] org-metaright)
  7610. '([(meta shift up)] org-shiftmetaup)
  7611. '([(meta shift down)] org-shiftmetadown)
  7612. '([(meta shift left)] org-shiftmetaleft)
  7613. '([(meta shift right)] org-shiftmetaright)
  7614. '([?\e (up)] org-metaup)
  7615. '([?\e (down)] org-metadown)
  7616. '([?\e (left)] org-metaleft)
  7617. '([?\e (right)] org-metaright)
  7618. '([?\e (shift up)] org-shiftmetaup)
  7619. '([?\e (shift down)] org-shiftmetadown)
  7620. '([?\e (shift left)] org-shiftmetaleft)
  7621. '([?\e (shift right)] org-shiftmetaright)
  7622. '([(shift up)] org-shiftup)
  7623. '([(shift down)] org-shiftdown)
  7624. '([(shift left)] org-shiftleft)
  7625. '([(shift right)] org-shiftright)
  7626. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7627. '("\M-q" fill-paragraph)
  7628. '("\C-c^" org-sort)
  7629. '("\C-c-" org-cycle-list-bullet)))
  7630. elt key fun cmd)
  7631. (while (setq elt (pop bindings))
  7632. (setq nfunc (1+ nfunc))
  7633. (setq key (org-key (car elt))
  7634. fun (nth 1 elt)
  7635. cmd (orgstruct-make-binding fun nfunc key))
  7636. (org-defkey orgstruct-mode-map key cmd))
  7637. ;; Special treatment needed for TAB and RET
  7638. (org-defkey orgstruct-mode-map [(tab)]
  7639. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7640. (org-defkey orgstruct-mode-map "\C-i"
  7641. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7642. (org-defkey orgstruct-mode-map "\M-\C-m"
  7643. (orgstruct-make-binding 'org-insert-heading 105
  7644. "\M-\C-m" [(meta return)]))
  7645. (org-defkey orgstruct-mode-map [(meta return)]
  7646. (orgstruct-make-binding 'org-insert-heading 106
  7647. [(meta return)] "\M-\C-m"))
  7648. (org-defkey orgstruct-mode-map [(shift meta return)]
  7649. (orgstruct-make-binding 'org-insert-todo-heading 107
  7650. [(meta return)] "\M-\C-m"))
  7651. (org-defkey orgstruct-mode-map "\e\C-m"
  7652. (orgstruct-make-binding 'org-insert-heading 108
  7653. "\e\C-m" [?\e (return)]))
  7654. (org-defkey orgstruct-mode-map [?\e (return)]
  7655. (orgstruct-make-binding 'org-insert-heading 109
  7656. [?\e (return)] "\e\C-m"))
  7657. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7658. (orgstruct-make-binding 'org-insert-todo-heading 110
  7659. [?\e (return)] "\e\C-m"))
  7660. (unless org-local-vars
  7661. (setq org-local-vars (org-get-local-variables)))
  7662. t))
  7663. (defun orgstruct-make-binding (fun n &rest keys)
  7664. "Create a function for binding in the structure minor mode.
  7665. FUN is the command to call inside a table. N is used to create a unique
  7666. command name. KEYS are keys that should be checked in for a command
  7667. to execute outside of tables."
  7668. (eval
  7669. (list 'defun
  7670. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7671. '(arg)
  7672. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7673. "Outside of structure, run the binding of `"
  7674. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7675. "'.")
  7676. '(interactive "p")
  7677. (list 'if
  7678. `(org-context-p 'headline 'item
  7679. (and orgstruct-is-++
  7680. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7681. 'item-body))
  7682. (list 'org-run-like-in-org-mode (list 'quote fun))
  7683. (list 'let '(orgstruct-mode)
  7684. (list 'call-interactively
  7685. (append '(or)
  7686. (mapcar (lambda (k)
  7687. (list 'key-binding k))
  7688. keys)
  7689. '('orgstruct-error))))))))
  7690. (defun org-context-p (&rest contexts)
  7691. "Check if local context is any of CONTEXTS.
  7692. Possible values in the list of contexts are `table', `headline', and `item'."
  7693. (let ((pos (point)))
  7694. (goto-char (point-at-bol))
  7695. (prog1 (or (and (memq 'table contexts)
  7696. (looking-at "[ \t]*|"))
  7697. (and (memq 'headline contexts)
  7698. (looking-at org-outline-regexp))
  7699. (and (memq 'item contexts)
  7700. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7701. (and (memq 'item-body contexts)
  7702. (org-in-item-p)))
  7703. (goto-char pos))))
  7704. (defun org-get-local-variables ()
  7705. "Return a list of all local variables in an org-mode buffer."
  7706. (let (varlist)
  7707. (with-current-buffer (get-buffer-create "*Org tmp*")
  7708. (erase-buffer)
  7709. (org-mode)
  7710. (setq varlist (buffer-local-variables)))
  7711. (kill-buffer "*Org tmp*")
  7712. (delq nil
  7713. (mapcar
  7714. (lambda (x)
  7715. (setq x
  7716. (if (symbolp x)
  7717. (list x)
  7718. (list (car x) (list 'quote (cdr x)))))
  7719. (if (string-match
  7720. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7721. (symbol-name (car x)))
  7722. x nil))
  7723. varlist))))
  7724. (defun org-clone-local-variables (from-buffer &optional regexp)
  7725. "Clone local variables from FROM-BUFFER.
  7726. Optional argument REGEXP selects variables to clone."
  7727. (mapc
  7728. (lambda (pair)
  7729. (and (symbolp (car pair))
  7730. (or (null regexp)
  7731. (string-match regexp (symbol-name (car pair))))
  7732. (set (make-local-variable (car pair))
  7733. (cdr pair))))
  7734. (buffer-local-variables from-buffer)))
  7735. ;;;###autoload
  7736. (defun org-run-like-in-org-mode (cmd)
  7737. "Run a command, pretending that the current buffer is in Org-mode.
  7738. This will temporarily bind local variables that are typically bound in
  7739. Org-mode to the values they have in Org-mode, and then interactively
  7740. call CMD."
  7741. (org-load-modules-maybe)
  7742. (unless org-local-vars
  7743. (setq org-local-vars (org-get-local-variables)))
  7744. (eval (list 'let org-local-vars
  7745. (list 'call-interactively (list 'quote cmd)))))
  7746. ;;;; Archiving
  7747. (defun org-get-category (&optional pos force-refresh)
  7748. "Get the category applying to position POS."
  7749. (save-match-data
  7750. (if force-refresh (org-refresh-category-properties))
  7751. (let ((pos (or pos (point))))
  7752. (or (get-text-property pos 'org-category)
  7753. (progn (org-refresh-category-properties)
  7754. (get-text-property pos 'org-category))))))
  7755. (defun org-refresh-category-properties ()
  7756. "Refresh category text properties in the buffer."
  7757. (let ((inhibit-read-only t)
  7758. (def-cat (cond
  7759. ((null org-category)
  7760. (if buffer-file-name
  7761. (file-name-sans-extension
  7762. (file-name-nondirectory buffer-file-name))
  7763. "???"))
  7764. ((symbolp org-category) (symbol-name org-category))
  7765. (t org-category)))
  7766. beg end cat pos optionp)
  7767. (org-unmodified
  7768. (save-excursion
  7769. (save-restriction
  7770. (widen)
  7771. (goto-char (point-min))
  7772. (put-text-property (point) (point-max) 'org-category def-cat)
  7773. (while (re-search-forward
  7774. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7775. (setq pos (match-end 0)
  7776. optionp (equal (char-after (match-beginning 0)) ?#)
  7777. cat (org-trim (match-string 2)))
  7778. (if optionp
  7779. (setq beg (point-at-bol) end (point-max))
  7780. (org-back-to-heading t)
  7781. (setq beg (point) end (org-end-of-subtree t t)))
  7782. (put-text-property beg end 'org-category cat)
  7783. (put-text-property beg end 'org-category-position beg)
  7784. (goto-char pos)))))))
  7785. ;;;; Link Stuff
  7786. ;;; Link abbreviations
  7787. (defun org-link-expand-abbrev (link)
  7788. "Apply replacements as defined in `org-link-abbrev-alist'."
  7789. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7790. (let* ((key (match-string 1 link))
  7791. (as (or (assoc key org-link-abbrev-alist-local)
  7792. (assoc key org-link-abbrev-alist)))
  7793. (tag (and (match-end 2) (match-string 3 link)))
  7794. rpl)
  7795. (if (not as)
  7796. link
  7797. (setq rpl (cdr as))
  7798. (cond
  7799. ((symbolp rpl) (funcall rpl tag))
  7800. ((string-match "%(\\([^)]+\\))" rpl)
  7801. (replace-match (funcall (intern-soft (match-string 1 rpl)) tag) t t rpl))
  7802. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7803. ((string-match "%h" rpl)
  7804. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7805. (t (concat rpl tag)))))
  7806. link))
  7807. ;;; Storing and inserting links
  7808. (defvar org-insert-link-history nil
  7809. "Minibuffer history for links inserted with `org-insert-link'.")
  7810. (defvar org-stored-links nil
  7811. "Contains the links stored with `org-store-link'.")
  7812. (defvar org-store-link-plist nil
  7813. "Plist with info about the most recently link created with `org-store-link'.")
  7814. (defvar org-link-protocols nil
  7815. "Link protocols added to Org-mode using `org-add-link-type'.")
  7816. (defvar org-store-link-functions nil
  7817. "List of functions that are called to create and store a link.
  7818. Each function will be called in turn until one returns a non-nil
  7819. value. Each function should check if it is responsible for creating
  7820. this link (for example by looking at the major mode).
  7821. If not, it must exit and return nil.
  7822. If yes, it should return a non-nil value after a calling
  7823. `org-store-link-props' with a list of properties and values.
  7824. Special properties are:
  7825. :type The link prefix, like \"http\". This must be given.
  7826. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7827. This is obligatory as well.
  7828. :description Optional default description for the second pair
  7829. of brackets in an Org-mode link. The user can still change
  7830. this when inserting this link into an Org-mode buffer.
  7831. In addition to these, any additional properties can be specified
  7832. and then used in remember templates.")
  7833. (defun org-add-link-type (type &optional follow export)
  7834. "Add TYPE to the list of `org-link-types'.
  7835. Re-compute all regular expressions depending on `org-link-types'
  7836. FOLLOW and EXPORT are two functions.
  7837. FOLLOW should take the link path as the single argument and do whatever
  7838. is necessary to follow the link, for example find a file or display
  7839. a mail message.
  7840. EXPORT should format the link path for export to one of the export formats.
  7841. It should be a function accepting three arguments:
  7842. path the path of the link, the text after the prefix (like \"http:\")
  7843. desc the description of the link, if any, or a description added by
  7844. org-export-normalize-links if there is none
  7845. format the export format, a symbol like `html' or `latex' or `ascii'..
  7846. The function may use the FORMAT information to return different values
  7847. depending on the format. The return value will be put literally into
  7848. the exported file. If the return value is nil, this means Org should
  7849. do what it normally does with links which do not have EXPORT defined.
  7850. Org-mode has a built-in default for exporting links. If you are happy with
  7851. this default, there is no need to define an export function for the link
  7852. type. For a simple example of an export function, see `org-bbdb.el'."
  7853. (add-to-list 'org-link-types type t)
  7854. (org-make-link-regexps)
  7855. (if (assoc type org-link-protocols)
  7856. (setcdr (assoc type org-link-protocols) (list follow export))
  7857. (push (list type follow export) org-link-protocols)))
  7858. (defvar org-agenda-buffer-name)
  7859. ;;;###autoload
  7860. (defun org-store-link (arg)
  7861. "\\<org-mode-map>Store an org-link to the current location.
  7862. This link is added to `org-stored-links' and can later be inserted
  7863. into an org-buffer with \\[org-insert-link].
  7864. For some link types, a prefix arg is interpreted:
  7865. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7866. For file links, arg negates `org-context-in-file-links'."
  7867. (interactive "P")
  7868. (org-load-modules-maybe)
  7869. (setq org-store-link-plist nil) ; reset
  7870. (org-with-limited-levels
  7871. (let (link cpltxt desc description search txt custom-id agenda-link)
  7872. (cond
  7873. ((run-hook-with-args-until-success 'org-store-link-functions)
  7874. (setq link (plist-get org-store-link-plist :link)
  7875. desc (or (plist-get org-store-link-plist :description) link)))
  7876. ((org-src-edit-buffer-p)
  7877. (let (label gc)
  7878. (while (or (not label)
  7879. (save-excursion
  7880. (save-restriction
  7881. (widen)
  7882. (goto-char (point-min))
  7883. (re-search-forward
  7884. (regexp-quote (format org-coderef-label-format label))
  7885. nil t))))
  7886. (when label (message "Label exists already") (sit-for 2))
  7887. (setq label (read-string "Code line label: " label)))
  7888. (end-of-line 1)
  7889. (setq link (format org-coderef-label-format label))
  7890. (setq gc (- 79 (length link)))
  7891. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7892. (insert link)
  7893. (setq link (concat "(" label ")") desc nil)))
  7894. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7895. ;; We are in the agenda, link to referenced location
  7896. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7897. (get-text-property (point) 'org-marker))))
  7898. (when m
  7899. (org-with-point-at m
  7900. (setq agenda-link
  7901. (if (org-called-interactively-p 'any)
  7902. (call-interactively 'org-store-link)
  7903. (org-store-link nil)))))))
  7904. ((eq major-mode 'calendar-mode)
  7905. (let ((cd (calendar-cursor-to-date)))
  7906. (setq link
  7907. (format-time-string
  7908. (car org-time-stamp-formats)
  7909. (apply 'encode-time
  7910. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7911. nil nil nil))))
  7912. (org-store-link-props :type "calendar" :date cd)))
  7913. ((eq major-mode 'w3-mode)
  7914. (setq cpltxt (if (and (buffer-name)
  7915. (not (string-match "Untitled" (buffer-name))))
  7916. (buffer-name)
  7917. (url-view-url t))
  7918. link (org-make-link (url-view-url t)))
  7919. (org-store-link-props :type "w3" :url (url-view-url t)))
  7920. ((eq major-mode 'w3m-mode)
  7921. (setq cpltxt (or w3m-current-title w3m-current-url)
  7922. link (org-make-link w3m-current-url))
  7923. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7924. ((setq search (run-hook-with-args-until-success
  7925. 'org-create-file-search-functions))
  7926. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7927. "::" search))
  7928. (setq cpltxt (or description link)))
  7929. ((eq major-mode 'image-mode)
  7930. (setq cpltxt (concat "file:"
  7931. (abbreviate-file-name buffer-file-name))
  7932. link (org-make-link cpltxt))
  7933. (org-store-link-props :type "image" :file buffer-file-name))
  7934. ((eq major-mode 'dired-mode)
  7935. ;; link to the file in the current line
  7936. (let ((file (dired-get-filename nil t)))
  7937. (setq file (if file
  7938. (abbreviate-file-name
  7939. (expand-file-name (dired-get-filename nil t)))
  7940. ;; otherwise, no file so use current directory.
  7941. default-directory))
  7942. (setq cpltxt (concat "file:" file)
  7943. link (org-make-link cpltxt))))
  7944. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  7945. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7946. (cond
  7947. ((org-in-regexp "<<\\(.*?\\)>>")
  7948. (setq cpltxt
  7949. (concat "file:"
  7950. (abbreviate-file-name
  7951. (buffer-file-name (buffer-base-buffer)))
  7952. "::" (match-string 1))
  7953. link (org-make-link cpltxt)))
  7954. ((and (featurep 'org-id)
  7955. (or (eq org-link-to-org-use-id t)
  7956. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7957. (org-called-interactively-p 'any))
  7958. (and (eq org-link-to-org-use-id
  7959. 'create-if-interactive-and-no-custom-id)
  7960. (org-called-interactively-p 'any)
  7961. (not custom-id))
  7962. (and org-link-to-org-use-id
  7963. (org-entry-get nil "ID"))))
  7964. ;; We can make a link using the ID.
  7965. (setq link (condition-case nil
  7966. (prog1 (org-id-store-link)
  7967. (setq desc (plist-get org-store-link-plist
  7968. :description)))
  7969. (error
  7970. ;; probably before first headline, link to file only
  7971. (concat "file:"
  7972. (abbreviate-file-name
  7973. (buffer-file-name (buffer-base-buffer))))))))
  7974. (t
  7975. ;; Just link to current headline
  7976. (setq cpltxt (concat "file:"
  7977. (abbreviate-file-name
  7978. (buffer-file-name (buffer-base-buffer)))))
  7979. ;; Add a context search string
  7980. (when (org-xor org-context-in-file-links arg)
  7981. (setq txt (cond
  7982. ((org-at-heading-p) nil)
  7983. ((org-region-active-p)
  7984. (buffer-substring (region-beginning) (region-end)))
  7985. (t nil)))
  7986. (when (or (null txt) (string-match "\\S-" txt))
  7987. (setq cpltxt
  7988. (concat cpltxt "::"
  7989. (condition-case nil
  7990. (org-make-org-heading-search-string txt)
  7991. (error "")))
  7992. desc (or (nth 4 (ignore-errors
  7993. (org-heading-components))) "NONE"))))
  7994. (if (string-match "::\\'" cpltxt)
  7995. (setq cpltxt (substring cpltxt 0 -2)))
  7996. (setq link (org-make-link cpltxt)))))
  7997. ((buffer-file-name (buffer-base-buffer))
  7998. ;; Just link to this file here.
  7999. (setq cpltxt (concat "file:"
  8000. (abbreviate-file-name
  8001. (buffer-file-name (buffer-base-buffer)))))
  8002. ;; Add a context string
  8003. (when (org-xor org-context-in-file-links arg)
  8004. (setq txt (if (org-region-active-p)
  8005. (buffer-substring (region-beginning) (region-end))
  8006. (buffer-substring (point-at-bol) (point-at-eol))))
  8007. ;; Only use search option if there is some text.
  8008. (when (string-match "\\S-" txt)
  8009. (setq cpltxt
  8010. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8011. desc "NONE")))
  8012. (setq link (org-make-link cpltxt)))
  8013. ((org-called-interactively-p 'interactive)
  8014. (error "Cannot link to a buffer which is not visiting a file"))
  8015. (t (setq link nil)))
  8016. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8017. (setq link (or link cpltxt)
  8018. desc (or desc cpltxt))
  8019. (if (equal desc "NONE") (setq desc nil))
  8020. (if (and (or (org-called-interactively-p 'any) executing-kbd-macro) link)
  8021. (progn
  8022. (setq org-stored-links
  8023. (cons (list link desc) org-stored-links))
  8024. (message "Stored: %s" (or desc link))
  8025. (when custom-id
  8026. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  8027. "::#" custom-id))
  8028. (setq org-stored-links
  8029. (cons (list link desc) org-stored-links))))
  8030. (or agenda-link (and link (org-make-link-string link desc)))))))
  8031. (defun org-store-link-props (&rest plist)
  8032. "Store link properties, extract names and addresses."
  8033. (let (x adr)
  8034. (when (setq x (plist-get plist :from))
  8035. (setq adr (mail-extract-address-components x))
  8036. (setq plist (plist-put plist :fromname (car adr)))
  8037. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8038. (when (setq x (plist-get plist :to))
  8039. (setq adr (mail-extract-address-components x))
  8040. (setq plist (plist-put plist :toname (car adr)))
  8041. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8042. (let ((from (plist-get plist :from))
  8043. (to (plist-get plist :to)))
  8044. (when (and from to org-from-is-user-regexp)
  8045. (setq plist
  8046. (plist-put plist :fromto
  8047. (if (string-match org-from-is-user-regexp from)
  8048. (concat "to %t")
  8049. (concat "from %f"))))))
  8050. (setq org-store-link-plist plist))
  8051. (defun org-add-link-props (&rest plist)
  8052. "Add these properties to the link property list."
  8053. (let (key value)
  8054. (while plist
  8055. (setq key (pop plist) value (pop plist))
  8056. (setq org-store-link-plist
  8057. (plist-put org-store-link-plist key value)))))
  8058. (defun org-email-link-description (&optional fmt)
  8059. "Return the description part of an email link.
  8060. This takes information from `org-store-link-plist' and formats it
  8061. according to FMT (default from `org-email-link-description-format')."
  8062. (setq fmt (or fmt org-email-link-description-format))
  8063. (let* ((p org-store-link-plist)
  8064. (to (plist-get p :toaddress))
  8065. (from (plist-get p :fromaddress))
  8066. (table
  8067. (list
  8068. (cons "%c" (plist-get p :fromto))
  8069. (cons "%F" (plist-get p :from))
  8070. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8071. (cons "%T" (plist-get p :to))
  8072. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8073. (cons "%s" (plist-get p :subject))
  8074. (cons "%d" (plist-get p :date))
  8075. (cons "%m" (plist-get p :message-id)))))
  8076. (when (string-match "%c" fmt)
  8077. ;; Check if the user wrote this message
  8078. (if (and org-from-is-user-regexp from to
  8079. (save-match-data (string-match org-from-is-user-regexp from)))
  8080. (setq fmt (replace-match "to %t" t t fmt))
  8081. (setq fmt (replace-match "from %f" t t fmt))))
  8082. (org-replace-escapes fmt table)))
  8083. (defun org-make-org-heading-search-string (&optional string heading)
  8084. "Make search string for STRING or current headline."
  8085. (interactive)
  8086. (let ((s (or string (org-get-heading)))
  8087. (lines org-context-in-file-links))
  8088. (unless (and string (not heading))
  8089. ;; We are using a headline, clean up garbage in there.
  8090. (if (string-match org-todo-regexp s)
  8091. (setq s (replace-match "" t t s)))
  8092. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  8093. (setq s (replace-match "" t t s)))
  8094. (setq s (org-trim s))
  8095. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8096. org-comment-string "\\)") s)
  8097. (setq s (replace-match "" t t s)))
  8098. (while (string-match org-ts-regexp s)
  8099. (setq s (replace-match "" t t s))))
  8100. (or string (setq s (concat "*" s))) ; Add * for headlines
  8101. (when (and string (integerp lines) (> lines 0))
  8102. (let ((slines (org-split-string s "\n")))
  8103. (when (< lines (length slines))
  8104. (setq s (mapconcat
  8105. 'identity
  8106. (reverse (nthcdr (- (length slines) lines)
  8107. (reverse slines))) "\n")))))
  8108. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8109. (defun org-make-link (&rest strings)
  8110. "Concatenate STRINGS."
  8111. (apply 'concat strings))
  8112. (defun org-make-link-string (link &optional description)
  8113. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8114. (unless (string-match "\\S-" link)
  8115. (error "Empty link"))
  8116. (when (and description
  8117. (stringp description)
  8118. (not (string-match "\\S-" description)))
  8119. (setq description nil))
  8120. (when (stringp description)
  8121. ;; Remove brackets from the description, they are fatal.
  8122. (while (string-match "\\[" description)
  8123. (setq description (replace-match "{" t t description)))
  8124. (while (string-match "\\]" description)
  8125. (setq description (replace-match "}" t t description))))
  8126. (when (equal link description)
  8127. ;; No description needed, it is identical
  8128. (setq description nil))
  8129. (when (and (not description)
  8130. (not (string-match (org-image-file-name-regexp) link))
  8131. (not (equal link (org-link-escape link))))
  8132. (setq description (org-extract-attributes link)))
  8133. (setq link
  8134. (cond ((string-match (org-image-file-name-regexp) link) link)
  8135. ((string-match org-link-types-re link)
  8136. (concat (match-string 1 link)
  8137. (org-link-escape (substring link (match-end 1)))))
  8138. (t (org-link-escape link))))
  8139. (concat "[[" link "]"
  8140. (if description (concat "[" description "]") "")
  8141. "]"))
  8142. (defconst org-link-escape-chars
  8143. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  8144. "List of characters that should be escaped in link.
  8145. This is the list that is used for internal purposes.")
  8146. (defvar org-url-encoding-use-url-hexify nil)
  8147. (defconst org-link-escape-chars-browser
  8148. '(?\ )
  8149. "List of escapes for characters that are problematic in links.
  8150. This is the list that is used before handing over to the browser.")
  8151. (defun org-link-escape (text &optional table merge)
  8152. "Return percent escaped representation of TEXT.
  8153. TEXT is a string with the text to escape.
  8154. Optional argument TABLE is a list with characters that should be
  8155. escaped. When nil, `org-link-escape-chars' is used.
  8156. If optional argument MERGE is set, merge TABLE into
  8157. `org-link-escape-chars'."
  8158. (if (and org-url-encoding-use-url-hexify (not table))
  8159. (url-hexify-string text)
  8160. (cond
  8161. ((and table merge)
  8162. (mapc (lambda (defchr)
  8163. (unless (member defchr table)
  8164. (setq table (cons defchr table)))) org-link-escape-chars))
  8165. ((null table)
  8166. (setq table org-link-escape-chars)))
  8167. (mapconcat
  8168. (lambda (char)
  8169. (if (or (member char table)
  8170. (< char 32) (= char 37) (> char 126))
  8171. (mapconcat (lambda (sequence-element)
  8172. (format "%%%.2X" sequence-element))
  8173. (or (encode-coding-char char 'utf-8)
  8174. (error "Unable to percent escape character: %s"
  8175. (char-to-string char))) "")
  8176. (char-to-string char))) text "")))
  8177. (defun org-link-unescape (str)
  8178. "Unhex hexified Unicode strings as returned from the JavaScript function
  8179. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  8180. (unless (and (null str) (string= "" str))
  8181. (let ((pos 0) (case-fold-search t) unhexed)
  8182. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  8183. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  8184. (setq str (replace-match unhexed t t str))
  8185. (setq pos (+ pos (length unhexed))))))
  8186. str)
  8187. (defun org-link-unescape-compound (hex)
  8188. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  8189. Note: this function also decodes single byte encodings like
  8190. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  8191. (save-match-data
  8192. (let* ((bytes (cdr (split-string hex "%")))
  8193. (ret "")
  8194. (eat 0)
  8195. (sum 0))
  8196. (while bytes
  8197. (let* ((val (string-to-number (pop bytes) 16))
  8198. (shift-xor
  8199. (if (= 0 eat)
  8200. (cond
  8201. ((>= val 252) (cons 6 252))
  8202. ((>= val 248) (cons 5 248))
  8203. ((>= val 240) (cons 4 240))
  8204. ((>= val 224) (cons 3 224))
  8205. ((>= val 192) (cons 2 192))
  8206. (t (cons 0 0)))
  8207. (cons 6 128))))
  8208. (if (>= val 192) (setq eat (car shift-xor)))
  8209. (setq val (logxor val (cdr shift-xor)))
  8210. (setq sum (+ (lsh sum (car shift-xor)) val))
  8211. (if (> eat 0) (setq eat (- eat 1)))
  8212. (cond
  8213. ((= 0 eat) ;multi byte
  8214. (setq ret (concat ret (org-char-to-string sum)))
  8215. (setq sum 0))
  8216. ((not bytes) ; single byte(s)
  8217. (setq ret (org-link-unescape-single-byte-sequence hex))))
  8218. )) ;; end (while bytes
  8219. ret )))
  8220. (defun org-link-unescape-single-byte-sequence (hex)
  8221. "Unhexify hex-encoded single byte character sequences."
  8222. (mapconcat (lambda (byte)
  8223. (char-to-string (string-to-number byte 16)))
  8224. (cdr (split-string hex "%")) ""))
  8225. (defun org-xor (a b)
  8226. "Exclusive or."
  8227. (if a (not b) b))
  8228. (defun org-fixup-message-id-for-http (s)
  8229. "Replace special characters in a message id, so it can be used in an http query."
  8230. (when (string-match "%" s)
  8231. (setq s (mapconcat (lambda (c)
  8232. (if (eq c ?%)
  8233. "%25"
  8234. (char-to-string c)))
  8235. s "")))
  8236. (while (string-match "<" s)
  8237. (setq s (replace-match "%3C" t t s)))
  8238. (while (string-match ">" s)
  8239. (setq s (replace-match "%3E" t t s)))
  8240. (while (string-match "@" s)
  8241. (setq s (replace-match "%40" t t s)))
  8242. s)
  8243. (defun org-link-prettify (link)
  8244. "Return a human-readable representation of LINK.
  8245. The car of LINK must be a raw link the cdr of LINK must be either
  8246. a link description or nil."
  8247. (let ((desc (or (cadr link) "<no description>")))
  8248. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  8249. "[[" (car link) "]]")))
  8250. ;;;###autoload
  8251. (defun org-insert-link-global ()
  8252. "Insert a link like Org-mode does.
  8253. This command can be called in any mode to insert a link in Org-mode syntax."
  8254. (interactive)
  8255. (org-load-modules-maybe)
  8256. (org-run-like-in-org-mode 'org-insert-link))
  8257. (defun org-insert-link (&optional complete-file link-location default-description)
  8258. "Insert a link. At the prompt, enter the link.
  8259. Completion can be used to insert any of the link protocol prefixes like
  8260. http or ftp in use.
  8261. The history can be used to select a link previously stored with
  8262. `org-store-link'. When the empty string is entered (i.e. if you just
  8263. press RET at the prompt), the link defaults to the most recently
  8264. stored link. As SPC triggers completion in the minibuffer, you need to
  8265. use M-SPC or C-q SPC to force the insertion of a space character.
  8266. You will also be prompted for a description, and if one is given, it will
  8267. be displayed in the buffer instead of the link.
  8268. If there is already a link at point, this command will allow you to edit link
  8269. and description parts.
  8270. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  8271. be selected using completion. The path to the file will be relative to the
  8272. current directory if the file is in the current directory or a subdirectory.
  8273. Otherwise, the link will be the absolute path as completed in the minibuffer
  8274. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  8275. option `org-link-file-path-type'.
  8276. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  8277. the current directory or below.
  8278. With three \\[universal-argument] prefixes, negate the meaning of
  8279. `org-keep-stored-link-after-insertion'.
  8280. If `org-make-link-description-function' is non-nil, this function will be
  8281. called with the link target, and the result will be the default
  8282. link description.
  8283. If the LINK-LOCATION parameter is non-nil, this value will be
  8284. used as the link location instead of reading one interactively.
  8285. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  8286. be used as the default description."
  8287. (interactive "P")
  8288. (let* ((wcf (current-window-configuration))
  8289. (region (if (org-region-active-p)
  8290. (buffer-substring (region-beginning) (region-end))))
  8291. (remove (and region (list (region-beginning) (region-end))))
  8292. (desc region)
  8293. tmphist ; byte-compile incorrectly complains about this
  8294. (link link-location)
  8295. entry file all-prefixes)
  8296. (cond
  8297. (link-location) ; specified by arg, just use it.
  8298. ((org-in-regexp org-bracket-link-regexp 1)
  8299. ;; We do have a link at point, and we are going to edit it.
  8300. (setq remove (list (match-beginning 0) (match-end 0)))
  8301. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8302. (setq link (read-string "Link: "
  8303. (org-link-unescape
  8304. (org-match-string-no-properties 1)))))
  8305. ((or (org-in-regexp org-angle-link-re)
  8306. (org-in-regexp org-plain-link-re))
  8307. ;; Convert to bracket link
  8308. (setq remove (list (match-beginning 0) (match-end 0))
  8309. link (read-string "Link: "
  8310. (org-remove-angle-brackets (match-string 0)))))
  8311. ((member complete-file '((4) (16)))
  8312. ;; Completing read for file names.
  8313. (setq link (org-file-complete-link complete-file)))
  8314. (t
  8315. ;; Read link, with completion for stored links.
  8316. (with-output-to-temp-buffer "*Org Links*"
  8317. (princ "Insert a link.
  8318. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  8319. (when org-stored-links
  8320. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  8321. (princ (mapconcat 'org-link-prettify
  8322. (reverse org-stored-links) "\n"))))
  8323. (let ((cw (selected-window)))
  8324. (select-window (get-buffer-window "*Org Links*" 'visible))
  8325. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  8326. (unless (pos-visible-in-window-p (point-max))
  8327. (org-fit-window-to-buffer))
  8328. (and (window-live-p cw) (select-window cw)))
  8329. ;; Fake a link history, containing the stored links.
  8330. (setq tmphist (append (mapcar 'car org-stored-links)
  8331. org-insert-link-history))
  8332. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  8333. (mapcar 'car org-link-abbrev-alist)
  8334. org-link-types))
  8335. (unwind-protect
  8336. (progn
  8337. (setq link
  8338. (let ((org-completion-use-ido nil)
  8339. (org-completion-use-iswitchb nil))
  8340. (org-completing-read
  8341. "Link: "
  8342. (append
  8343. (mapcar (lambda (x) (list (concat x ":")))
  8344. all-prefixes)
  8345. (mapcar 'car org-stored-links))
  8346. nil nil nil
  8347. 'tmphist
  8348. (car (car org-stored-links)))))
  8349. (if (not (string-match "\\S-" link))
  8350. (error "No link selected"))
  8351. (if (or (member link all-prefixes)
  8352. (and (equal ":" (substring link -1))
  8353. (member (substring link 0 -1) all-prefixes)
  8354. (setq link (substring link 0 -1))))
  8355. (setq link (org-link-try-special-completion link))))
  8356. (set-window-configuration wcf)
  8357. (kill-buffer "*Org Links*"))
  8358. (setq entry (assoc link org-stored-links))
  8359. (or entry (push link org-insert-link-history))
  8360. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8361. (not org-keep-stored-link-after-insertion))
  8362. (setq org-stored-links (delq (assoc link org-stored-links)
  8363. org-stored-links)))
  8364. (setq desc (or desc (nth 1 entry)))))
  8365. (if (string-match org-plain-link-re link)
  8366. ;; URL-like link, normalize the use of angular brackets.
  8367. (setq link (org-make-link (org-remove-angle-brackets link))))
  8368. ;; Check if we are linking to the current file with a search option
  8369. ;; If yes, simplify the link by using only the search option.
  8370. (when (and buffer-file-name
  8371. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8372. (let* ((path (match-string 1 link))
  8373. (case-fold-search nil)
  8374. (search (match-string 2 link)))
  8375. (save-match-data
  8376. (if (equal (file-truename buffer-file-name) (file-truename path))
  8377. ;; We are linking to this same file, with a search option
  8378. (setq link search)))))
  8379. ;; Check if we can/should use a relative path. If yes, simplify the link
  8380. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8381. (let* ((type (match-string 1 link))
  8382. (path (match-string 2 link))
  8383. (origpath path)
  8384. (case-fold-search nil))
  8385. (cond
  8386. ((or (eq org-link-file-path-type 'absolute)
  8387. (equal complete-file '(16)))
  8388. (setq path (abbreviate-file-name (expand-file-name path))))
  8389. ((eq org-link-file-path-type 'noabbrev)
  8390. (setq path (expand-file-name path)))
  8391. ((eq org-link-file-path-type 'relative)
  8392. (setq path (file-relative-name path)))
  8393. (t
  8394. (save-match-data
  8395. (if (string-match (concat "^" (regexp-quote
  8396. (expand-file-name
  8397. (file-name-as-directory
  8398. default-directory))))
  8399. (expand-file-name path))
  8400. ;; We are linking a file with relative path name.
  8401. (setq path (substring (expand-file-name path)
  8402. (match-end 0)))
  8403. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8404. (setq link (concat type path))
  8405. (if (equal desc origpath)
  8406. (setq desc path))))
  8407. (if org-make-link-description-function
  8408. (setq desc (funcall org-make-link-description-function link desc))
  8409. (if default-description (setq desc default-description)))
  8410. (setq desc (read-string "Description: " desc))
  8411. (unless (string-match "\\S-" desc) (setq desc nil))
  8412. (if remove (apply 'delete-region remove))
  8413. (insert (org-make-link-string link desc))))
  8414. (defun org-link-try-special-completion (type)
  8415. "If there is completion support for link type TYPE, offer it."
  8416. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8417. (if (functionp fun)
  8418. (funcall fun)
  8419. (read-string "Link (no completion support): " (concat type ":")))))
  8420. (defun org-file-complete-link (&optional arg)
  8421. "Create a file link using completion."
  8422. (let (file link)
  8423. (setq file (read-file-name "File: "))
  8424. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8425. (pwd1 (file-name-as-directory (abbreviate-file-name
  8426. (expand-file-name ".")))))
  8427. (cond
  8428. ((equal arg '(16))
  8429. (setq link (org-make-link
  8430. "file:"
  8431. (abbreviate-file-name (expand-file-name file)))))
  8432. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8433. (setq link (org-make-link "file:" (match-string 1 file))))
  8434. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8435. (expand-file-name file))
  8436. (setq link (org-make-link
  8437. "file:" (match-string 1 (expand-file-name file)))))
  8438. (t (setq link (org-make-link "file:" file)))))
  8439. link))
  8440. (defun org-completing-read (&rest args)
  8441. "Completing-read with SPACE being a normal character."
  8442. (let ((enable-recursive-minibuffers t)
  8443. (minibuffer-local-completion-map
  8444. (copy-keymap minibuffer-local-completion-map)))
  8445. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8446. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8447. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  8448. (apply 'org-icompleting-read args)))
  8449. (defun org-completing-read-no-i (&rest args)
  8450. (let (org-completion-use-ido org-completion-use-iswitchb)
  8451. (apply 'org-completing-read args)))
  8452. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8453. "Use iswitch as a completing-read replacement to choose from choices.
  8454. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8455. from."
  8456. (let* ((iswitchb-use-virtual-buffers nil)
  8457. (iswitchb-make-buflist-hook
  8458. (lambda ()
  8459. (setq iswitchb-temp-buflist choices))))
  8460. (iswitchb-read-buffer prompt)))
  8461. (defun org-icompleting-read (&rest args)
  8462. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8463. (org-without-partial-completion
  8464. (if (and org-completion-use-ido
  8465. (fboundp 'ido-completing-read)
  8466. (boundp 'ido-mode) ido-mode
  8467. (listp (second args)))
  8468. (let ((ido-enter-matching-directory nil))
  8469. (apply 'ido-completing-read (concat (car args))
  8470. (if (consp (car (nth 1 args)))
  8471. (mapcar 'car (nth 1 args))
  8472. (nth 1 args))
  8473. (cddr args)))
  8474. (if (and org-completion-use-iswitchb
  8475. (boundp 'iswitchb-mode) iswitchb-mode
  8476. (listp (second args)))
  8477. (apply 'org-iswitchb-completing-read (concat (car args))
  8478. (if (consp (car (nth 1 args)))
  8479. (mapcar 'car (nth 1 args))
  8480. (nth 1 args))
  8481. (cddr args))
  8482. (apply 'completing-read args)))))
  8483. (defun org-extract-attributes (s)
  8484. "Extract the attributes cookie from a string and set as text property."
  8485. (let (a attr (start 0) key value)
  8486. (save-match-data
  8487. (when (string-match "{{\\([^}]+\\)}}$" s)
  8488. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8489. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8490. (setq key (match-string 1 a) value (match-string 2 a)
  8491. start (match-end 0)
  8492. attr (plist-put attr (intern key) value))))
  8493. (org-add-props s nil 'org-attr attr))
  8494. s))
  8495. (defun org-extract-attributes-from-string (tag)
  8496. (let (key value attr)
  8497. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8498. (setq key (match-string 1 tag) value (match-string 2 tag)
  8499. tag (replace-match "" t t tag)
  8500. attr (plist-put attr (intern key) value)))
  8501. (cons tag attr)))
  8502. (defun org-attributes-to-string (plist)
  8503. "Format a property list into an HTML attribute list."
  8504. (let ((s "") key value)
  8505. (while plist
  8506. (setq key (pop plist) value (pop plist))
  8507. (and value
  8508. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8509. s))
  8510. ;;; Opening/following a link
  8511. (defvar org-link-search-failed nil)
  8512. (defvar org-open-link-functions nil
  8513. "Hook for functions finding a plain text link.
  8514. These functions must take a single argument, the link content.
  8515. They will be called for links that look like [[link text][description]]
  8516. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8517. like a filename (e.g. \"./blue.png\").
  8518. These functions will be called *before* Org attempts to resolve the
  8519. link by doing text searches in the current buffer - so if you want a
  8520. link \"[[target]]\" to still find \"<<target>>\", your function should
  8521. handle this as a special case.
  8522. When the function does handle the link, it must return a non-nil value.
  8523. If it decides that it is not responsible for this link, it must return
  8524. nil to indicate that that Org-mode can continue with other options
  8525. like exact and fuzzy text search.")
  8526. (defun org-next-link ()
  8527. "Move forward to the next link.
  8528. If the link is in hidden text, expose it."
  8529. (interactive)
  8530. (when (and org-link-search-failed (eq this-command last-command))
  8531. (goto-char (point-min))
  8532. (message "Link search wrapped back to beginning of buffer"))
  8533. (setq org-link-search-failed nil)
  8534. (let* ((pos (point))
  8535. (ct (org-context))
  8536. (a (assoc :link ct)))
  8537. (if a (goto-char (nth 2 a)))
  8538. (if (re-search-forward org-any-link-re nil t)
  8539. (progn
  8540. (goto-char (match-beginning 0))
  8541. (if (outline-invisible-p) (org-show-context)))
  8542. (goto-char pos)
  8543. (setq org-link-search-failed t)
  8544. (error "No further link found"))))
  8545. (defun org-previous-link ()
  8546. "Move backward to the previous link.
  8547. If the link is in hidden text, expose it."
  8548. (interactive)
  8549. (when (and org-link-search-failed (eq this-command last-command))
  8550. (goto-char (point-max))
  8551. (message "Link search wrapped back to end of buffer"))
  8552. (setq org-link-search-failed nil)
  8553. (let* ((pos (point))
  8554. (ct (org-context))
  8555. (a (assoc :link ct)))
  8556. (if a (goto-char (nth 1 a)))
  8557. (if (re-search-backward org-any-link-re nil t)
  8558. (progn
  8559. (goto-char (match-beginning 0))
  8560. (if (outline-invisible-p) (org-show-context)))
  8561. (goto-char pos)
  8562. (setq org-link-search-failed t)
  8563. (error "No further link found"))))
  8564. (defun org-translate-link (s)
  8565. "Translate a link string if a translation function has been defined."
  8566. (if (and org-link-translation-function
  8567. (fboundp org-link-translation-function)
  8568. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8569. (progn
  8570. (setq s (funcall org-link-translation-function
  8571. (match-string 1 s) (match-string 2 s)))
  8572. (concat (car s) ":" (cdr s)))
  8573. s))
  8574. (defun org-translate-link-from-planner (type path)
  8575. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8576. This is still an experimental function, your mileage may vary."
  8577. (cond
  8578. ((member type '("http" "https" "news" "ftp"))
  8579. ;; standard Internet links are the same.
  8580. nil)
  8581. ((and (equal type "irc") (string-match "^//" path))
  8582. ;; Planner has two / at the beginning of an irc link, we have 1.
  8583. ;; We should have zero, actually....
  8584. (setq path (substring path 1)))
  8585. ((and (equal type "lisp") (string-match "^/" path))
  8586. ;; Planner has a slash, we do not.
  8587. (setq type "elisp" path (substring path 1)))
  8588. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8589. ;; A typical message link. Planner has the id after the final slash,
  8590. ;; we separate it with a hash mark
  8591. (setq path (concat (match-string 1 path) "#"
  8592. (org-remove-angle-brackets (match-string 2 path)))))
  8593. )
  8594. (cons type path))
  8595. (defun org-find-file-at-mouse (ev)
  8596. "Open file link or URL at mouse."
  8597. (interactive "e")
  8598. (mouse-set-point ev)
  8599. (org-open-at-point 'in-emacs))
  8600. (defun org-open-at-mouse (ev)
  8601. "Open file link or URL at mouse.
  8602. See the docstring of `org-open-file' for details."
  8603. (interactive "e")
  8604. (mouse-set-point ev)
  8605. (if (eq major-mode 'org-agenda-mode)
  8606. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8607. (org-open-at-point))
  8608. (defvar org-window-config-before-follow-link nil
  8609. "The window configuration before following a link.
  8610. This is saved in case the need arises to restore it.")
  8611. (defvar org-open-link-marker (make-marker)
  8612. "Marker pointing to the location where `org-open-at-point; was called.")
  8613. ;;;###autoload
  8614. (defun org-open-at-point-global ()
  8615. "Follow a link like Org-mode does.
  8616. This command can be called in any mode to follow a link that has
  8617. Org-mode syntax."
  8618. (interactive)
  8619. (org-run-like-in-org-mode 'org-open-at-point))
  8620. ;;;###autoload
  8621. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8622. "Open a link in the string S, as if it was in Org-mode."
  8623. (interactive "sLink: \nP")
  8624. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8625. (with-temp-buffer
  8626. (let ((org-inhibit-startup (not reference-buffer)))
  8627. (org-mode)
  8628. (insert s)
  8629. (goto-char (point-min))
  8630. (when reference-buffer
  8631. (setq org-link-abbrev-alist-local
  8632. (with-current-buffer reference-buffer
  8633. org-link-abbrev-alist-local)))
  8634. (org-open-at-point arg reference-buffer)))))
  8635. (defvar org-open-at-point-functions nil
  8636. "Hook that is run when following a link at point.
  8637. Functions in this hook must return t if they identify and follow
  8638. a link at point. If they don't find anything interesting at point,
  8639. they must return nil.")
  8640. (defun org-open-at-point (&optional arg reference-buffer)
  8641. "Open link at or after point.
  8642. If there is no link at point, this function will search forward up to
  8643. the end of the current line.
  8644. Normally, files will be opened by an appropriate application. If the
  8645. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8646. With a double prefix argument, try to open outside of Emacs, in the
  8647. application the system uses for this file type."
  8648. (interactive "P")
  8649. ;; if in a code block, then open the block's results
  8650. (unless (call-interactively #'org-babel-open-src-block-result)
  8651. (org-load-modules-maybe)
  8652. (move-marker org-open-link-marker (point))
  8653. (setq org-window-config-before-follow-link (current-window-configuration))
  8654. (org-remove-occur-highlights nil nil t)
  8655. (cond
  8656. ((and (org-at-heading-p)
  8657. (not (org-at-timestamp-p t))
  8658. (not (org-in-regexp
  8659. (concat org-plain-link-re "\\|"
  8660. org-bracket-link-regexp "\\|"
  8661. org-angle-link-re "\\|"
  8662. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8663. (not (get-text-property (point) 'org-linked-text)))
  8664. (or (org-offer-links-in-entry arg)
  8665. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8666. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8667. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8668. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8669. (not (org-in-regexp org-bracket-link-regexp)))
  8670. (org-footnote-action))
  8671. (t
  8672. (let (type path link line search (pos (point)))
  8673. (catch 'match
  8674. (save-excursion
  8675. (skip-chars-forward "^]\n\r")
  8676. (when (org-in-regexp org-bracket-link-regexp 1)
  8677. (setq link (org-extract-attributes
  8678. (org-link-unescape (org-match-string-no-properties 1))))
  8679. (while (string-match " *\n *" link)
  8680. (setq link (replace-match " " t t link)))
  8681. (setq link (org-link-expand-abbrev link))
  8682. (cond
  8683. ((or (file-name-absolute-p link)
  8684. (string-match "^\\.\\.?/" link))
  8685. (setq type "file" path link))
  8686. ((string-match org-link-re-with-space3 link)
  8687. (setq type (match-string 1 link) path (match-string 2 link)))
  8688. (t (setq type "thisfile" path link)))
  8689. (throw 'match t)))
  8690. (when (get-text-property (point) 'org-linked-text)
  8691. (setq type "thisfile"
  8692. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8693. (1+ (point)) (point))
  8694. path (buffer-substring
  8695. (or (previous-single-property-change pos 'org-linked-text)
  8696. (point-min))
  8697. (or (next-single-property-change pos 'org-linked-text)
  8698. (point-max))))
  8699. (throw 'match t))
  8700. (save-excursion
  8701. (when (or (org-in-regexp org-angle-link-re)
  8702. (org-in-regexp org-plain-link-re))
  8703. (setq type (match-string 1)
  8704. path (org-link-unescape (match-string 2)))
  8705. (throw 'match t)))
  8706. (save-excursion
  8707. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8708. (setq type "tags"
  8709. path (match-string 1))
  8710. (while (string-match ":" path)
  8711. (setq path (replace-match "+" t t path)))
  8712. (throw 'match t)))
  8713. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8714. (setq type "tree-match"
  8715. path (match-string 1))
  8716. (throw 'match t)))
  8717. (unless path
  8718. (error "No link found"))
  8719. ;; switch back to reference buffer
  8720. ;; needed when if called in a temporary buffer through
  8721. ;; org-open-link-from-string
  8722. (with-current-buffer (or reference-buffer (current-buffer))
  8723. ;; Remove any trailing spaces in path
  8724. (if (string-match " +\\'" path)
  8725. (setq path (replace-match "" t t path)))
  8726. (if (and org-link-translation-function
  8727. (fboundp org-link-translation-function))
  8728. ;; Check if we need to translate the link
  8729. (let ((tmp (funcall org-link-translation-function type path)))
  8730. (setq type (car tmp) path (cdr tmp))))
  8731. (cond
  8732. ((assoc type org-link-protocols)
  8733. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8734. ((equal type "mailto")
  8735. (let ((cmd (car org-link-mailto-program))
  8736. (args (cdr org-link-mailto-program)) args1
  8737. (address path) (subject "") a)
  8738. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8739. (setq address (match-string 1 path)
  8740. subject (org-link-escape (match-string 2 path))))
  8741. (while args
  8742. (cond
  8743. ((not (stringp (car args))) (push (pop args) args1))
  8744. (t (setq a (pop args))
  8745. (if (string-match "%a" a)
  8746. (setq a (replace-match address t t a)))
  8747. (if (string-match "%s" a)
  8748. (setq a (replace-match subject t t a)))
  8749. (push a args1))))
  8750. (apply cmd (nreverse args1))))
  8751. ((member type '("http" "https" "ftp" "news"))
  8752. (browse-url (concat type ":" (if (org-string-match-p "[[:nonascii:] ]" path)
  8753. (org-link-escape
  8754. path org-link-escape-chars-browser)
  8755. path))))
  8756. ((string= type "doi")
  8757. (browse-url (concat org-doi-server-url (if (org-string-match-p "[[:nonascii:] ]" path)
  8758. (org-link-escape
  8759. path org-link-escape-chars-browser)
  8760. path))))
  8761. ((member type '("message"))
  8762. (browse-url (concat type ":" path)))
  8763. ((string= type "tags")
  8764. (org-tags-view arg path))
  8765. ((string= type "tree-match")
  8766. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8767. ((string= type "file")
  8768. (if (string-match "::\\([0-9]+\\)\\'" path)
  8769. (setq line (string-to-number (match-string 1 path))
  8770. path (substring path 0 (match-beginning 0)))
  8771. (if (string-match "::\\(.+\\)\\'" path)
  8772. (setq search (match-string 1 path)
  8773. path (substring path 0 (match-beginning 0)))))
  8774. (if (string-match "[*?{]" (file-name-nondirectory path))
  8775. (dired path)
  8776. (org-open-file path arg line search)))
  8777. ((string= type "shell")
  8778. (let ((cmd path))
  8779. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8780. (string-match org-confirm-shell-link-not-regexp cmd))
  8781. (not org-confirm-shell-link-function)
  8782. (funcall org-confirm-shell-link-function
  8783. (format "Execute \"%s\" in shell? "
  8784. (org-add-props cmd nil
  8785. 'face 'org-warning))))
  8786. (progn
  8787. (message "Executing %s" cmd)
  8788. (shell-command cmd))
  8789. (error "Abort"))))
  8790. ((string= type "elisp")
  8791. (let ((cmd path))
  8792. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8793. (string-match org-confirm-elisp-link-not-regexp cmd))
  8794. (not org-confirm-elisp-link-function)
  8795. (funcall org-confirm-elisp-link-function
  8796. (format "Execute \"%s\" as elisp? "
  8797. (org-add-props cmd nil
  8798. 'face 'org-warning))))
  8799. (message "%s => %s" cmd
  8800. (if (equal (string-to-char cmd) ?\()
  8801. (eval (read cmd))
  8802. (call-interactively (read cmd))))
  8803. (error "Abort"))))
  8804. ((and (string= type "thisfile")
  8805. (run-hook-with-args-until-success
  8806. 'org-open-link-functions path)))
  8807. ((string= type "thisfile")
  8808. (if arg
  8809. (switch-to-buffer-other-window
  8810. (org-get-buffer-for-internal-link (current-buffer)))
  8811. (org-mark-ring-push))
  8812. (let ((cmd `(org-link-search
  8813. ,path
  8814. ,(cond ((equal arg '(4)) ''occur)
  8815. ((equal arg '(16)) ''org-occur)
  8816. (t nil))
  8817. ,pos)))
  8818. (condition-case nil (let ((org-link-search-inhibit-query t))
  8819. (eval cmd))
  8820. (error (progn (widen) (eval cmd))))))
  8821. (t
  8822. (browse-url-at-point)))))))
  8823. (move-marker org-open-link-marker nil)
  8824. (run-hook-with-args 'org-follow-link-hook)))
  8825. (defun org-offer-links-in-entry (&optional nth zero)
  8826. "Offer links in the current entry and follow the selected link.
  8827. If there is only one link, follow it immediately as well.
  8828. If NTH is an integer, immediately pick the NTH link found.
  8829. If ZERO is a string, check also this string for a link, and if
  8830. there is one, offer it as link number zero."
  8831. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8832. "\\(" org-angle-link-re "\\)\\|"
  8833. "\\(" org-plain-link-re "\\)"))
  8834. (cnt ?0)
  8835. (in-emacs (if (integerp nth) nil nth))
  8836. have-zero end links link c)
  8837. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8838. (push (match-string 0 zero) links)
  8839. (setq cnt (1- cnt) have-zero t))
  8840. (save-excursion
  8841. (org-back-to-heading t)
  8842. (setq end (save-excursion (outline-next-heading) (point)))
  8843. (while (re-search-forward re end t)
  8844. (push (match-string 0) links))
  8845. (setq links (org-uniquify (reverse links))))
  8846. (cond
  8847. ((null links)
  8848. (message "No links"))
  8849. ((equal (length links) 1)
  8850. (setq link (list (car links))))
  8851. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8852. (setq link (list (nth (if have-zero nth (1- nth)) links))))
  8853. (t ; we have to select a link
  8854. (save-excursion
  8855. (save-window-excursion
  8856. (delete-other-windows)
  8857. (with-output-to-temp-buffer "*Select Link*"
  8858. (mapc (lambda (l)
  8859. (if (not (string-match org-bracket-link-regexp l))
  8860. (princ (format "[%c] %s\n" (incf cnt)
  8861. (org-remove-angle-brackets l)))
  8862. (if (match-end 3)
  8863. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8864. (match-string 3 l) (match-string 1 l)))
  8865. (princ (format "[%c] %s\n" (incf cnt)
  8866. (match-string 1 l))))))
  8867. links))
  8868. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8869. (message "Select link to open, RET to open all:")
  8870. (setq c (read-char-exclusive))
  8871. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8872. (when (equal c ?q) (error "Abort"))
  8873. (if (equal c ?\C-m)
  8874. (setq link links)
  8875. (setq nth (- c ?0))
  8876. (if have-zero (setq nth (1+ nth)))
  8877. (unless (and (integerp nth) (>= (length links) nth))
  8878. (error "Invalid link selection"))
  8879. (setq link (list (nth (1- nth) links))))))
  8880. (if link
  8881. (let ((buf (current-buffer)))
  8882. (dolist (l link)
  8883. (org-open-link-from-string l in-emacs buf))
  8884. t)
  8885. nil)))
  8886. ;; Add special file links that specify the way of opening
  8887. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8888. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8889. (defun org-open-file-with-system (path)
  8890. "Open file at PATH using the system way of opening it."
  8891. (org-open-file path 'system))
  8892. (defun org-open-file-with-emacs (path)
  8893. "Open file at PATH in Emacs."
  8894. (org-open-file path 'emacs))
  8895. (defun org-remove-file-link-modifiers ()
  8896. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8897. (goto-char (point-min))
  8898. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8899. (org-if-unprotected
  8900. (replace-match "file:" t t))))
  8901. (eval-after-load "org-exp"
  8902. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8903. 'org-remove-file-link-modifiers))
  8904. ;;;; Time estimates
  8905. (defun org-get-effort (&optional pom)
  8906. "Get the effort estimate for the current entry."
  8907. (org-entry-get pom org-effort-property))
  8908. ;;; File search
  8909. (defvar org-create-file-search-functions nil
  8910. "List of functions to construct the right search string for a file link.
  8911. These functions are called in turn with point at the location to
  8912. which the link should point.
  8913. A function in the hook should first test if it would like to
  8914. handle this file type, for example by checking the `major-mode'
  8915. or the file extension. If it decides not to handle this file, it
  8916. should just return nil to give other functions a chance. If it
  8917. does handle the file, it must return the search string to be used
  8918. when following the link. The search string will be part of the
  8919. file link, given after a double colon, and `org-open-at-point'
  8920. will automatically search for it. If special measures must be
  8921. taken to make the search successful, another function should be
  8922. added to the companion hook `org-execute-file-search-functions',
  8923. which see.
  8924. A function in this hook may also use `setq' to set the variable
  8925. `description' to provide a suggestion for the descriptive text to
  8926. be used for this link when it gets inserted into an Org-mode
  8927. buffer with \\[org-insert-link].")
  8928. (defvar org-execute-file-search-functions nil
  8929. "List of functions to execute a file search triggered by a link.
  8930. Functions added to this hook must accept a single argument, the
  8931. search string that was part of the file link, the part after the
  8932. double colon. The function must first check if it would like to
  8933. handle this search, for example by checking the `major-mode' or
  8934. the file extension. If it decides not to handle this search, it
  8935. should just return nil to give other functions a chance. If it
  8936. does handle the search, it must return a non-nil value to keep
  8937. other functions from trying.
  8938. Each function can access the current prefix argument through the
  8939. variable `current-prefix-argument'. Note that a single prefix is
  8940. used to force opening a link in Emacs, so it may be good to only
  8941. use a numeric or double prefix to guide the search function.
  8942. In case this is needed, a function in this hook can also restore
  8943. the window configuration before `org-open-at-point' was called using:
  8944. (set-window-configuration org-window-config-before-follow-link)")
  8945. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8946. (defun org-link-search (s &optional type avoid-pos stealth)
  8947. "Search for a link search option.
  8948. If S is surrounded by forward slashes, it is interpreted as a
  8949. regular expression. In org-mode files, this will create an `org-occur'
  8950. sparse tree. In ordinary files, `occur' will be used to list matches.
  8951. If the current buffer is in `dired-mode', grep will be used to search
  8952. in all files. If AVOID-POS is given, ignore matches near that position.
  8953. When optional argument STEALTH is non-nil, do not modify
  8954. visibility around point, thus ignoring
  8955. `org-show-hierarchy-above', `org-show-following-heading' and
  8956. `org-show-siblings' variables."
  8957. (let ((case-fold-search t)
  8958. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8959. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8960. (append '(("") (" ") ("\t") ("\n"))
  8961. org-emphasis-alist)
  8962. "\\|") "\\)"))
  8963. (pos (point))
  8964. (pre nil) (post nil)
  8965. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8966. (cond
  8967. ;; First check if there are any special search functions
  8968. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8969. ;; Now try the builtin stuff
  8970. ((and (equal (string-to-char s0) ?#)
  8971. (> (length s0) 1)
  8972. (save-excursion
  8973. (goto-char (point-min))
  8974. (and
  8975. (re-search-forward
  8976. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8977. (setq type 'dedicated
  8978. pos (match-beginning 0))))
  8979. ;; There is an exact target for this
  8980. (goto-char pos)
  8981. (org-back-to-heading t)))
  8982. ((save-excursion
  8983. (goto-char (point-min))
  8984. (and
  8985. (re-search-forward
  8986. (concat "<<" (regexp-quote s0) ">>") nil t)
  8987. (setq type 'dedicated
  8988. pos (match-beginning 0))))
  8989. ;; There is an exact target for this
  8990. (goto-char pos))
  8991. ((save-excursion
  8992. (goto-char (point-min))
  8993. (and
  8994. (re-search-forward
  8995. (format "^[ \t]*#\\+TARGET: %s" (regexp-quote s0)) nil t)
  8996. (setq type 'dedicated pos (match-beginning 0))))
  8997. ;; Found an invisible target.
  8998. (goto-char pos))
  8999. ((save-excursion
  9000. (goto-char (point-min))
  9001. (and
  9002. (re-search-forward
  9003. (format "^[ \t]*#\\+NAME: %s" (regexp-quote s0)) nil t)
  9004. (setq type 'dedicated pos (match-beginning 0))))
  9005. ;; Found an element with a matching #+name affiliated keyword.
  9006. (goto-char pos))
  9007. ((and (string-match "^(\\(.*\\))$" s0)
  9008. (save-excursion
  9009. (goto-char (point-min))
  9010. (and
  9011. (re-search-forward
  9012. (concat "[^[]" (regexp-quote
  9013. (format org-coderef-label-format
  9014. (match-string 1 s0))))
  9015. nil t)
  9016. (setq type 'dedicated
  9017. pos (1+ (match-beginning 0))))))
  9018. ;; There is a coderef target for this
  9019. (goto-char pos))
  9020. ((string-match "^/\\(.*\\)/$" s)
  9021. ;; A regular expression
  9022. (cond
  9023. ((derived-mode-p 'org-mode)
  9024. (org-occur (match-string 1 s)))
  9025. ;;((eq major-mode 'dired-mode)
  9026. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  9027. (t (org-do-occur (match-string 1 s)))))
  9028. ((and (derived-mode-p 'org-mode) org-link-search-must-match-exact-headline)
  9029. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  9030. (goto-char (point-min))
  9031. (cond
  9032. ((let (case-fold-search)
  9033. (re-search-forward (format org-complex-heading-regexp-format
  9034. (regexp-quote s))
  9035. nil t))
  9036. ;; OK, found a match
  9037. (setq type 'dedicated)
  9038. (goto-char (match-beginning 0)))
  9039. ((and (not org-link-search-inhibit-query)
  9040. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9041. (y-or-n-p "No match - create this as a new heading? "))
  9042. (goto-char (point-max))
  9043. (or (bolp) (newline))
  9044. (insert "* " s "\n")
  9045. (beginning-of-line 0))
  9046. (t
  9047. (goto-char pos)
  9048. (error "No match"))))
  9049. (t
  9050. ;; A normal search string
  9051. (when (equal (string-to-char s) ?*)
  9052. ;; Anchor on headlines, post may include tags.
  9053. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  9054. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  9055. s (substring s 1)))
  9056. (remove-text-properties
  9057. 0 (length s)
  9058. '(face nil mouse-face nil keymap nil fontified nil) s)
  9059. ;; Make a series of regular expressions to find a match
  9060. (setq words (org-split-string s "[ \n\r\t]+")
  9061. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  9062. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  9063. "\\)" markers)
  9064. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  9065. re2a (concat "[ \t\r\n]" re2a_)
  9066. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  9067. re4 (concat "[^a-zA-Z_]" re4_)
  9068. re1 (concat pre re2 post)
  9069. re3 (concat pre (if pre re4_ re4) post)
  9070. re5 (concat pre ".*" re4)
  9071. re2 (concat pre re2)
  9072. re2a (concat pre (if pre re2a_ re2a))
  9073. re4 (concat pre (if pre re4_ re4))
  9074. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  9075. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  9076. re5 "\\)"
  9077. ))
  9078. (cond
  9079. ((eq type 'org-occur) (org-occur reall))
  9080. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  9081. (t (goto-char (point-min))
  9082. (setq type 'fuzzy)
  9083. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  9084. (org-search-not-self 1 re1 nil t)
  9085. (org-search-not-self 1 re2 nil t)
  9086. (org-search-not-self 1 re2a nil t)
  9087. (org-search-not-self 1 re3 nil t)
  9088. (org-search-not-self 1 re4 nil t)
  9089. (org-search-not-self 1 re5 nil t)
  9090. )
  9091. (goto-char (match-beginning 1))
  9092. (goto-char pos)
  9093. (error "No match"))))))
  9094. (and (derived-mode-p 'org-mode)
  9095. (not stealth)
  9096. (org-show-context 'link-search))
  9097. type))
  9098. (defun org-search-not-self (group &rest args)
  9099. "Execute `re-search-forward', but only accept matches that do not
  9100. enclose the position of `org-open-link-marker'."
  9101. (let ((m org-open-link-marker))
  9102. (catch 'exit
  9103. (while (apply 're-search-forward args)
  9104. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  9105. (goto-char (match-end group))
  9106. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  9107. (> (match-beginning 0) (marker-position m))
  9108. (< (match-end 0) (marker-position m)))
  9109. (save-match-data
  9110. (or (not (org-in-regexp
  9111. org-bracket-link-analytic-regexp 1))
  9112. (not (match-end 4)) ; no description
  9113. (and (<= (match-beginning 4) (point))
  9114. (>= (match-end 4) (point))))))
  9115. (throw 'exit (point))))))))
  9116. (defun org-get-buffer-for-internal-link (buffer)
  9117. "Return a buffer to be used for displaying the link target of internal links."
  9118. (cond
  9119. ((not org-display-internal-link-with-indirect-buffer)
  9120. buffer)
  9121. ((string-match "(Clone)$" (buffer-name buffer))
  9122. (message "Buffer is already a clone, not making another one")
  9123. ;; we also do not modify visibility in this case
  9124. buffer)
  9125. (t ; make a new indirect buffer for displaying the link
  9126. (let* ((bn (buffer-name buffer))
  9127. (ibn (concat bn "(Clone)"))
  9128. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9129. (with-current-buffer ib (org-overview))
  9130. ib))))
  9131. (defun org-do-occur (regexp &optional cleanup)
  9132. "Call the Emacs command `occur'.
  9133. If CLEANUP is non-nil, remove the printout of the regular expression
  9134. in the *Occur* buffer. This is useful if the regex is long and not useful
  9135. to read."
  9136. (occur regexp)
  9137. (when cleanup
  9138. (let ((cwin (selected-window)) win beg end)
  9139. (when (setq win (get-buffer-window "*Occur*"))
  9140. (select-window win))
  9141. (goto-char (point-min))
  9142. (when (re-search-forward "match[a-z]+" nil t)
  9143. (setq beg (match-end 0))
  9144. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  9145. (setq end (1- (match-beginning 0)))))
  9146. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  9147. (goto-char (point-min))
  9148. (select-window cwin))))
  9149. ;;; The mark ring for links jumps
  9150. (defvar org-mark-ring nil
  9151. "Mark ring for positions before jumps in Org-mode.")
  9152. (defvar org-mark-ring-last-goto nil
  9153. "Last position in the mark ring used to go back.")
  9154. ;; Fill and close the ring
  9155. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  9156. (loop for i from 1 to org-mark-ring-length do
  9157. (push (make-marker) org-mark-ring))
  9158. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  9159. org-mark-ring)
  9160. (defun org-mark-ring-push (&optional pos buffer)
  9161. "Put the current position or POS into the mark ring and rotate it."
  9162. (interactive)
  9163. (setq pos (or pos (point)))
  9164. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  9165. (move-marker (car org-mark-ring)
  9166. (or pos (point))
  9167. (or buffer (current-buffer)))
  9168. (message "%s"
  9169. (substitute-command-keys
  9170. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  9171. (defun org-mark-ring-goto (&optional n)
  9172. "Jump to the previous position in the mark ring.
  9173. With prefix arg N, jump back that many stored positions. When
  9174. called several times in succession, walk through the entire ring.
  9175. Org-mode commands jumping to a different position in the current file,
  9176. or to another Org-mode file, automatically push the old position
  9177. onto the ring."
  9178. (interactive "p")
  9179. (let (p m)
  9180. (if (eq last-command this-command)
  9181. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  9182. (setq p org-mark-ring))
  9183. (setq org-mark-ring-last-goto p)
  9184. (setq m (car p))
  9185. (org-pop-to-buffer-same-window (marker-buffer m))
  9186. (goto-char m)
  9187. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  9188. (defun org-remove-angle-brackets (s)
  9189. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  9190. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  9191. s)
  9192. (defun org-add-angle-brackets (s)
  9193. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  9194. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  9195. s)
  9196. (defun org-remove-double-quotes (s)
  9197. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  9198. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  9199. s)
  9200. ;;; Following specific links
  9201. (defun org-follow-timestamp-link ()
  9202. (cond
  9203. ((org-at-date-range-p t)
  9204. (let ((org-agenda-start-on-weekday)
  9205. (t1 (match-string 1))
  9206. (t2 (match-string 2)))
  9207. (setq t1 (time-to-days (org-time-string-to-time t1))
  9208. t2 (time-to-days (org-time-string-to-time t2)))
  9209. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  9210. ((org-at-timestamp-p t)
  9211. (org-agenda-list nil (time-to-days (org-time-string-to-time
  9212. (substring (match-string 1) 0 10)))
  9213. 1))
  9214. (t (error "This should not happen"))))
  9215. ;;; Following file links
  9216. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  9217. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  9218. (declare-function mailcap-mime-info
  9219. "mailcap" (string &optional request no-decode))
  9220. (defvar org-wait nil)
  9221. (defun org-open-file (path &optional in-emacs line search)
  9222. "Open the file at PATH.
  9223. First, this expands any special file name abbreviations. Then the
  9224. configuration variable `org-file-apps' is checked if it contains an
  9225. entry for this file type, and if yes, the corresponding command is launched.
  9226. If no application is found, Emacs simply visits the file.
  9227. With optional prefix argument IN-EMACS, Emacs will visit the file.
  9228. With a double \\[universal-argument] \\[universal-argument] \
  9229. prefix arg, Org tries to avoid opening in Emacs
  9230. and to use an external application to visit the file.
  9231. Optional LINE specifies a line to go to, optional SEARCH a string
  9232. to search for. If LINE or SEARCH is given, the file will be
  9233. opened in Emacs, unless an entry from org-file-apps that makes
  9234. use of groups in a regexp matches.
  9235. If you want to change the way frames are used when following a
  9236. link, please customize `org-link-frame-setup'.
  9237. If the file does not exist, an error is thrown."
  9238. (let* ((file (if (equal path "")
  9239. buffer-file-name
  9240. (substitute-in-file-name (expand-file-name path))))
  9241. (file-apps (append org-file-apps (org-default-apps)))
  9242. (apps (org-remove-if
  9243. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9244. (apps-dlink (org-remove-if-not
  9245. 'org-file-apps-entry-match-against-dlink-p file-apps))
  9246. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  9247. (dirp (if remp nil (file-directory-p file)))
  9248. (file (if (and dirp org-open-directory-means-index-dot-org)
  9249. (concat (file-name-as-directory file) "index.org")
  9250. file))
  9251. (a-m-a-p (assq 'auto-mode apps))
  9252. (dfile (downcase file))
  9253. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  9254. (link (cond ((and (eq line nil)
  9255. (eq search nil))
  9256. file)
  9257. (line
  9258. (concat file "::" (number-to-string line)))
  9259. (search
  9260. (concat file "::" search))))
  9261. (dlink (downcase link))
  9262. (old-buffer (current-buffer))
  9263. (old-pos (point))
  9264. (old-mode major-mode)
  9265. ext cmd link-match-data)
  9266. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  9267. (setq ext (match-string 1 dfile))
  9268. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  9269. (setq ext (match-string 1 dfile))))
  9270. (cond
  9271. ((member in-emacs '((16) system))
  9272. (setq cmd (cdr (assoc 'system apps))))
  9273. (in-emacs (setq cmd 'emacs))
  9274. (t
  9275. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  9276. (and dirp (cdr (assoc 'directory apps)))
  9277. ; first, try matching against apps-dlink
  9278. ; if we get a match here, store the match data for later
  9279. (let ((match (assoc-default dlink apps-dlink
  9280. 'string-match)))
  9281. (if match
  9282. (progn (setq link-match-data (match-data))
  9283. match)
  9284. (progn (setq in-emacs (or in-emacs line search))
  9285. nil))) ; if we have no match in apps-dlink,
  9286. ; always open the file in emacs if line or search
  9287. ; is given (for backwards compatibility)
  9288. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  9289. 'string-match)
  9290. (cdr (assoc ext apps))
  9291. (cdr (assoc t apps))))))
  9292. (when (eq cmd 'system)
  9293. (setq cmd (cdr (assoc 'system apps))))
  9294. (when (eq cmd 'default)
  9295. (setq cmd (cdr (assoc t apps))))
  9296. (when (eq cmd 'mailcap)
  9297. (require 'mailcap)
  9298. (mailcap-parse-mailcaps)
  9299. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  9300. (command (mailcap-mime-info mime-type)))
  9301. (if (stringp command)
  9302. (setq cmd command)
  9303. (setq cmd 'emacs))))
  9304. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  9305. (not (file-exists-p file))
  9306. (not org-open-non-existing-files))
  9307. (error "No such file: %s" file))
  9308. (cond
  9309. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  9310. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  9311. (while (string-match "['\"]%s['\"]" cmd)
  9312. (setq cmd (replace-match "%s" t t cmd)))
  9313. (while (string-match "%s" cmd)
  9314. (setq cmd (replace-match
  9315. (save-match-data
  9316. (shell-quote-argument
  9317. (convert-standard-filename file)))
  9318. t t cmd)))
  9319. ;; Replace "%1", "%2" etc. in command with group matches from regex
  9320. (save-match-data
  9321. (let ((match-index 1)
  9322. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  9323. (set-match-data link-match-data)
  9324. (while (<= match-index number-of-groups)
  9325. (let ((regex (concat "%" (number-to-string match-index)))
  9326. (replace-with (match-string match-index dlink)))
  9327. (while (string-match regex cmd)
  9328. (setq cmd (replace-match replace-with t t cmd))))
  9329. (setq match-index (+ match-index 1)))))
  9330. (save-window-excursion
  9331. (start-process-shell-command cmd nil cmd)
  9332. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  9333. ))
  9334. ((or (stringp cmd)
  9335. (eq cmd 'emacs))
  9336. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  9337. (widen)
  9338. (if line (org-goto-line line)
  9339. (if search (org-link-search search))))
  9340. ((consp cmd)
  9341. (let ((file (convert-standard-filename file)))
  9342. (save-match-data
  9343. (set-match-data link-match-data)
  9344. (eval cmd))))
  9345. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  9346. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  9347. (or (not (equal old-buffer (current-buffer)))
  9348. (not (equal old-pos (point))))
  9349. (org-mark-ring-push old-pos old-buffer))))
  9350. (defun org-file-apps-entry-match-against-dlink-p (entry)
  9351. "This function returns non-nil if `entry' uses a regular
  9352. expression which should be matched against the whole link by
  9353. org-open-file.
  9354. It assumes that is the case when the entry uses a regular
  9355. expression which has at least one grouping construct and the
  9356. action is either a lisp form or a command string containing
  9357. '%1', i.e. using at least one subexpression match as a
  9358. parameter."
  9359. (let ((selector (car entry))
  9360. (action (cdr entry)))
  9361. (if (stringp selector)
  9362. (and (> (regexp-opt-depth selector) 0)
  9363. (or (and (stringp action)
  9364. (string-match "%[0-9]" action))
  9365. (consp action)))
  9366. nil)))
  9367. (defun org-default-apps ()
  9368. "Return the default applications for this operating system."
  9369. (cond
  9370. ((eq system-type 'darwin)
  9371. org-file-apps-defaults-macosx)
  9372. ((eq system-type 'windows-nt)
  9373. org-file-apps-defaults-windowsnt)
  9374. (t org-file-apps-defaults-gnu)))
  9375. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  9376. "Convert extensions to regular expressions in the cars of LIST.
  9377. Also, weed out any non-string entries, because the return value is used
  9378. only for regexp matching.
  9379. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  9380. point to the symbol `emacs', indicating that the file should
  9381. be opened in Emacs."
  9382. (append
  9383. (delq nil
  9384. (mapcar (lambda (x)
  9385. (if (not (stringp (car x)))
  9386. nil
  9387. (if (string-match "\\W" (car x))
  9388. x
  9389. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9390. list))
  9391. (if add-auto-mode
  9392. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9393. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9394. (defun org-file-remote-p (file)
  9395. "Test whether FILE specifies a location on a remote system.
  9396. Return non-nil if the location is indeed remote.
  9397. For example, the filename \"/user@host:/foo\" specifies a location
  9398. on the system \"/user@host:\"."
  9399. (cond ((fboundp 'file-remote-p)
  9400. (file-remote-p file))
  9401. ((fboundp 'tramp-handle-file-remote-p)
  9402. (tramp-handle-file-remote-p file))
  9403. ((and (boundp 'ange-ftp-name-format)
  9404. (string-match (car ange-ftp-name-format) file))
  9405. t)
  9406. (t nil)))
  9407. ;;;; Refiling
  9408. (defun org-get-org-file ()
  9409. "Read a filename, with default directory `org-directory'."
  9410. (let ((default (or org-default-notes-file remember-data-file)))
  9411. (read-file-name (format "File name [%s]: " default)
  9412. (file-name-as-directory org-directory)
  9413. default)))
  9414. (defun org-notes-order-reversed-p ()
  9415. "Check if the current file should receive notes in reversed order."
  9416. (cond
  9417. ((not org-reverse-note-order) nil)
  9418. ((eq t org-reverse-note-order) t)
  9419. ((not (listp org-reverse-note-order)) nil)
  9420. (t (catch 'exit
  9421. (let ((all org-reverse-note-order)
  9422. entry)
  9423. (while (setq entry (pop all))
  9424. (if (string-match (car entry) buffer-file-name)
  9425. (throw 'exit (cdr entry))))
  9426. nil)))))
  9427. (defvar org-refile-target-table nil
  9428. "The list of refile targets, created by `org-refile'.")
  9429. (defvar org-agenda-new-buffers nil
  9430. "Buffers created to visit agenda files.")
  9431. (defvar org-refile-cache nil
  9432. "Cache for refile targets.")
  9433. (defvar org-refile-markers nil
  9434. "All the markers used for caching refile locations.")
  9435. (defun org-refile-marker (pos)
  9436. "Get a new refile marker, but only if caching is in use."
  9437. (if (not org-refile-use-cache)
  9438. pos
  9439. (let ((m (make-marker)))
  9440. (move-marker m pos)
  9441. (push m org-refile-markers)
  9442. m)))
  9443. (defun org-refile-cache-clear ()
  9444. "Clear the refile cache and disable all the markers."
  9445. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9446. (setq org-refile-markers nil)
  9447. (setq org-refile-cache nil)
  9448. (message "Refile cache has been cleared"))
  9449. (defun org-refile-cache-check-set (set)
  9450. "Check if all the markers in the cache still have live buffers."
  9451. (let (marker)
  9452. (catch 'exit
  9453. (while (and set (setq marker (nth 3 (pop set))))
  9454. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9455. (when (and marker (null (marker-buffer marker)))
  9456. (message "not found") (sit-for 3)
  9457. (throw 'exit nil)))
  9458. t)))
  9459. (defun org-refile-cache-put (set &rest identifiers)
  9460. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9461. (let* ((key (sha1 (prin1-to-string identifiers)))
  9462. (entry (assoc key org-refile-cache)))
  9463. (if entry
  9464. (setcdr entry set)
  9465. (push (cons key set) org-refile-cache))))
  9466. (defun org-refile-cache-get (&rest identifiers)
  9467. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9468. (cond
  9469. ((not org-refile-cache) nil)
  9470. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9471. (t
  9472. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9473. org-refile-cache))))
  9474. (and set (org-refile-cache-check-set set) set)))))
  9475. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  9476. "Produce a table with refile targets."
  9477. (let ((case-fold-search nil)
  9478. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9479. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9480. targets tgs txt re files f desc descre fast-path-p level pos0)
  9481. (message "Getting targets...")
  9482. (with-current-buffer (or default-buffer (current-buffer))
  9483. (while (setq entry (pop entries))
  9484. (setq files (car entry) desc (cdr entry))
  9485. (setq fast-path-p nil)
  9486. (cond
  9487. ((null files) (setq files (list (current-buffer))))
  9488. ((eq files 'org-agenda-files)
  9489. (setq files (org-agenda-files 'unrestricted)))
  9490. ((and (symbolp files) (fboundp files))
  9491. (setq files (funcall files)))
  9492. ((and (symbolp files) (boundp files))
  9493. (setq files (symbol-value files))))
  9494. (if (stringp files) (setq files (list files)))
  9495. (cond
  9496. ((eq (car desc) :tag)
  9497. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9498. ((eq (car desc) :todo)
  9499. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9500. ((eq (car desc) :regexp)
  9501. (setq descre (cdr desc)))
  9502. ((eq (car desc) :level)
  9503. (setq descre (concat "^\\*\\{" (number-to-string
  9504. (if org-odd-levels-only
  9505. (1- (* 2 (cdr desc)))
  9506. (cdr desc)))
  9507. "\\}[ \t]")))
  9508. ((eq (car desc) :maxlevel)
  9509. (setq fast-path-p t)
  9510. (setq descre (concat "^\\*\\{1," (number-to-string
  9511. (if org-odd-levels-only
  9512. (1- (* 2 (cdr desc)))
  9513. (cdr desc)))
  9514. "\\}[ \t]")))
  9515. (t (error "Bad refiling target description %s" desc)))
  9516. (while (setq f (pop files))
  9517. (with-current-buffer
  9518. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9519. (or
  9520. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9521. (progn
  9522. (if (bufferp f) (setq f (buffer-file-name
  9523. (buffer-base-buffer f))))
  9524. (setq f (and f (expand-file-name f)))
  9525. (if (eq org-refile-use-outline-path 'file)
  9526. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9527. (save-excursion
  9528. (save-restriction
  9529. (widen)
  9530. (goto-char (point-min))
  9531. (while (re-search-forward descre nil t)
  9532. (goto-char (setq pos0 (point-at-bol)))
  9533. (catch 'next
  9534. (when org-refile-target-verify-function
  9535. (save-match-data
  9536. (or (funcall org-refile-target-verify-function)
  9537. (throw 'next t))))
  9538. (when (and (looking-at org-complex-heading-regexp)
  9539. (not (member (match-string 4) excluded-entries))
  9540. (match-string 4))
  9541. (setq level (org-reduced-level
  9542. (- (match-end 1) (match-beginning 1)))
  9543. txt (org-link-display-format (match-string 4))
  9544. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9545. re (format org-complex-heading-regexp-format
  9546. (regexp-quote (match-string 4))))
  9547. (when org-refile-use-outline-path
  9548. (setq txt (mapconcat
  9549. 'org-protect-slash
  9550. (append
  9551. (if (eq org-refile-use-outline-path
  9552. 'file)
  9553. (list (file-name-nondirectory
  9554. (buffer-file-name
  9555. (buffer-base-buffer))))
  9556. (if (eq org-refile-use-outline-path
  9557. 'full-file-path)
  9558. (list (buffer-file-name
  9559. (buffer-base-buffer)))))
  9560. (org-get-outline-path fast-path-p
  9561. level txt)
  9562. (list txt))
  9563. "/")))
  9564. (push (list txt f re (org-refile-marker (point)))
  9565. tgs)))
  9566. (when (= (point) pos0)
  9567. ;; verification function has not moved point
  9568. (goto-char (point-at-eol))))))))
  9569. (when org-refile-use-cache
  9570. (org-refile-cache-put tgs (buffer-file-name) descre))
  9571. (setq targets (append tgs targets))
  9572. ))))
  9573. (message "Getting targets...done")
  9574. (nreverse targets)))
  9575. (defun org-protect-slash (s)
  9576. (while (string-match "/" s)
  9577. (setq s (replace-match "\\" t t s)))
  9578. s)
  9579. (defvar org-olpa (make-vector 20 nil))
  9580. (defun org-get-outline-path (&optional fastp level heading)
  9581. "Return the outline path to the current entry, as a list.
  9582. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9583. routine which makes outline path derivations for an entire file,
  9584. avoiding backtracing. Refile target collection makes use of that."
  9585. (if fastp
  9586. (progn
  9587. (if (> level 19)
  9588. (error "Outline path failure, more than 19 levels"))
  9589. (loop for i from level upto 19 do
  9590. (aset org-olpa i nil))
  9591. (prog1
  9592. (delq nil (append org-olpa nil))
  9593. (aset org-olpa level heading)))
  9594. (let (rtn case-fold-search)
  9595. (save-excursion
  9596. (save-restriction
  9597. (widen)
  9598. (while (org-up-heading-safe)
  9599. (when (looking-at org-complex-heading-regexp)
  9600. (push (org-match-string-no-properties 4) rtn)))
  9601. rtn)))))
  9602. (defun org-format-outline-path (path &optional width prefix)
  9603. "Format the outline path PATH for display.
  9604. Width is the maximum number of characters that is available.
  9605. Prefix is a prefix to be included in the returned string,
  9606. such as the file name."
  9607. (setq width (or width 79))
  9608. (if prefix (setq width (- width (length prefix))))
  9609. (if (not path)
  9610. (or prefix "")
  9611. (let* ((nsteps (length path))
  9612. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9613. (maxwidth (if (<= total-width width)
  9614. 10000 ;; everything fits
  9615. ;; we need to shorten the level headings
  9616. (/ (- width nsteps) nsteps)))
  9617. (org-odd-levels-only nil)
  9618. (n 0)
  9619. (total (1+ (length prefix))))
  9620. (setq maxwidth (max maxwidth 10))
  9621. (concat prefix
  9622. (mapconcat
  9623. (lambda (h)
  9624. (setq n (1+ n))
  9625. (if (and (= n nsteps) (< maxwidth 10000))
  9626. (setq maxwidth (- total-width total)))
  9627. (if (< (length h) maxwidth)
  9628. (progn (setq total (+ total (length h) 1)) h)
  9629. (setq h (substring h 0 (- maxwidth 2))
  9630. total (+ total maxwidth 1))
  9631. (if (string-match "[ \t]+\\'" h)
  9632. (setq h (substring h 0 (match-beginning 0))))
  9633. (setq h (concat h "..")))
  9634. (org-add-props h nil 'face
  9635. (nth (% (1- n) org-n-level-faces)
  9636. org-level-faces))
  9637. h)
  9638. path "/")))))
  9639. (defun org-display-outline-path (&optional file current)
  9640. "Display the current outline path in the echo area."
  9641. (interactive "P")
  9642. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9643. (case-fold-search nil)
  9644. (path (and (derived-mode-p 'org-mode) (org-get-outline-path))))
  9645. (if current (setq path (append path
  9646. (save-excursion
  9647. (org-back-to-heading t)
  9648. (if (looking-at org-complex-heading-regexp)
  9649. (list (match-string 4)))))))
  9650. (message "%s"
  9651. (org-format-outline-path
  9652. path
  9653. (1- (frame-width))
  9654. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9655. (defvar org-refile-history nil
  9656. "History for refiling operations.")
  9657. (defvar org-after-refile-insert-hook nil
  9658. "Hook run after `org-refile' has inserted its stuff at the new location.
  9659. Note that this is still *before* the stuff will be removed from
  9660. the *old* location.")
  9661. (defvar org-capture-last-stored-marker)
  9662. (defun org-refile (&optional goto default-buffer rfloc)
  9663. "Move the entry or entries at point to another heading.
  9664. The list of target headings is compiled using the information in
  9665. `org-refile-targets', which see.
  9666. At the target location, the entry is filed as a subitem of the target
  9667. heading. Depending on `org-reverse-note-order', the new subitem will
  9668. either be the first or the last subitem.
  9669. If there is an active region, all entries in that region will be moved.
  9670. However, the region must fulfill the requirement that the first heading
  9671. is the first one sets the top-level of the moved text - at most siblings
  9672. below it are allowed.
  9673. With prefix arg GOTO, the command will only visit the target location
  9674. and not actually move anything.
  9675. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9676. go to the location where the last refiling operation has put the subtree.
  9677. With a prefix argument of `2', refile to the running clock.
  9678. RFLOC can be a refile location obtained in a different way.
  9679. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9680. If you are using target caching (see `org-refile-use-cache'),
  9681. you have to clear the target cache in order to find new targets.
  9682. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  9683. prefix argument (`C-u C-u C-u C-c C-w')."
  9684. (interactive "P")
  9685. (if (member goto '(0 (64)))
  9686. (org-refile-cache-clear)
  9687. (let* ((cbuf (current-buffer))
  9688. (regionp (org-region-active-p))
  9689. (region-start (and regionp (region-beginning)))
  9690. (region-end (and regionp (region-end)))
  9691. (region-length (and regionp (- region-end region-start)))
  9692. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9693. pos it nbuf file re level reversed)
  9694. (setq last-command nil)
  9695. (when regionp
  9696. (goto-char region-start)
  9697. (or (bolp) (goto-char (point-at-bol)))
  9698. (setq region-start (point))
  9699. (unless (or (org-kill-is-subtree-p
  9700. (buffer-substring region-start region-end))
  9701. (prog1 org-refile-active-region-within-subtree
  9702. (org-toggle-heading)))
  9703. (error "The region is not a (sequence of) subtree(s)")))
  9704. (if (equal goto '(16))
  9705. (org-refile-goto-last-stored)
  9706. (when (or
  9707. (and (equal goto 2)
  9708. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9709. (prog1
  9710. (setq it (list (or org-clock-heading "running clock")
  9711. (buffer-file-name
  9712. (marker-buffer org-clock-hd-marker))
  9713. ""
  9714. (marker-position org-clock-hd-marker)))
  9715. (setq goto nil)))
  9716. (setq it (or rfloc
  9717. (let (heading-text)
  9718. (save-excursion
  9719. (unless goto
  9720. (org-back-to-heading t)
  9721. (setq heading-text
  9722. (nth 4 (org-heading-components))))
  9723. (org-refile-get-location
  9724. (cond (goto "Goto")
  9725. (regionp "Refile region to")
  9726. (t (concat "Refile subtree \""
  9727. heading-text "\" to")))
  9728. default-buffer
  9729. (and (not (equal '(4) goto))
  9730. org-refile-allow-creating-parent-nodes)
  9731. goto))))))
  9732. (setq file (nth 1 it)
  9733. re (nth 2 it)
  9734. pos (nth 3 it))
  9735. (if (and (not goto)
  9736. pos
  9737. (equal (buffer-file-name) file)
  9738. (if regionp
  9739. (and (>= pos region-start)
  9740. (<= pos region-end))
  9741. (and (>= pos (point))
  9742. (< pos (save-excursion
  9743. (org-end-of-subtree t t))))))
  9744. (error "Cannot refile to position inside the tree or region"))
  9745. (setq nbuf (or (find-buffer-visiting file)
  9746. (find-file-noselect file)))
  9747. (if goto
  9748. (progn
  9749. (org-pop-to-buffer-same-window nbuf)
  9750. (goto-char pos)
  9751. (org-show-context 'org-goto))
  9752. (if regionp
  9753. (progn
  9754. (org-kill-new (buffer-substring region-start region-end))
  9755. (org-save-markers-in-region region-start region-end))
  9756. (org-copy-subtree 1 nil t))
  9757. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9758. (find-file-noselect file)))
  9759. (setq reversed (org-notes-order-reversed-p))
  9760. (save-excursion
  9761. (save-restriction
  9762. (widen)
  9763. (if pos
  9764. (progn
  9765. (goto-char pos)
  9766. (looking-at org-outline-regexp)
  9767. (setq level (org-get-valid-level (funcall outline-level) 1))
  9768. (goto-char
  9769. (if reversed
  9770. (or (outline-next-heading) (point-max))
  9771. (or (save-excursion (org-get-next-sibling))
  9772. (org-end-of-subtree t t)
  9773. (point-max)))))
  9774. (setq level 1)
  9775. (if (not reversed)
  9776. (goto-char (point-max))
  9777. (goto-char (point-min))
  9778. (or (outline-next-heading) (goto-char (point-max)))))
  9779. (if (not (bolp)) (newline))
  9780. (org-paste-subtree level)
  9781. (when org-log-refile
  9782. (org-add-log-setup 'refile nil nil 'findpos
  9783. org-log-refile)
  9784. (unless (eq org-log-refile 'note)
  9785. (save-excursion (org-add-log-note))))
  9786. (and org-auto-align-tags
  9787. (let ((org-loop-over-headlines-in-active-region nil))
  9788. (org-set-tags nil t)))
  9789. (bookmark-set "org-refile-last-stored")
  9790. ;; If we are refiling for capture, make sure that the
  9791. ;; last-capture pointers point here
  9792. (when (org-bound-and-true-p org-refile-for-capture)
  9793. (bookmark-set "org-capture-last-stored-marker")
  9794. (move-marker org-capture-last-stored-marker (point)))
  9795. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9796. (run-hooks 'org-after-refile-insert-hook))))
  9797. (if regionp
  9798. (delete-region (point) (+ (point) region-length))
  9799. (org-cut-subtree))
  9800. (when (featurep 'org-inlinetask)
  9801. (org-inlinetask-remove-END-maybe))
  9802. (setq org-markers-to-move nil)
  9803. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9804. (defun org-refile-goto-last-stored ()
  9805. "Go to the location where the last refile was stored."
  9806. (interactive)
  9807. (bookmark-jump "org-refile-last-stored")
  9808. (message "This is the location of the last refile"))
  9809. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  9810. no-exclude)
  9811. "Prompt the user for a refile location, using PROMPT.
  9812. PROMPT should not be suffixed with a colon and a space, because
  9813. this function appends the default value from
  9814. `org-refile-history' automatically, if that is not empty.
  9815. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  9816. this is used for the GOTO interface."
  9817. (let ((org-refile-targets org-refile-targets)
  9818. (org-refile-use-outline-path org-refile-use-outline-path)
  9819. excluded-entries)
  9820. (when (and (derived-mode-p 'org-mode)
  9821. (not org-refile-use-cache)
  9822. (not no-exclude))
  9823. (org-map-tree
  9824. (lambda()
  9825. (setq excluded-entries
  9826. (append excluded-entries (list (org-get-heading t t)))))))
  9827. (setq org-refile-target-table
  9828. (org-refile-get-targets default-buffer excluded-entries)))
  9829. (unless org-refile-target-table
  9830. (error "No refile targets"))
  9831. (let* ((prompt (concat prompt
  9832. (and (car org-refile-history)
  9833. (concat " (default " (car org-refile-history) ")"))
  9834. ": "))
  9835. (cbuf (current-buffer))
  9836. (partial-completion-mode nil)
  9837. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9838. (cfunc (if (and org-refile-use-outline-path
  9839. org-outline-path-complete-in-steps)
  9840. 'org-olpath-completing-read
  9841. 'org-icompleting-read))
  9842. (extra (if org-refile-use-outline-path "/" ""))
  9843. (filename (and cfn (expand-file-name cfn)))
  9844. (tbl (mapcar
  9845. (lambda (x)
  9846. (if (and (not (member org-refile-use-outline-path
  9847. '(file full-file-path)))
  9848. (not (equal filename (nth 1 x))))
  9849. (cons (concat (car x) extra " ("
  9850. (file-name-nondirectory (nth 1 x)) ")")
  9851. (cdr x))
  9852. (cons (concat (car x) extra) (cdr x))))
  9853. org-refile-target-table))
  9854. (completion-ignore-case t)
  9855. pa answ parent-target child parent old-hist)
  9856. (setq old-hist org-refile-history)
  9857. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9858. nil 'org-refile-history (car org-refile-history)))
  9859. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9860. (org-refile-check-position pa)
  9861. (if pa
  9862. (progn
  9863. (when (or (not org-refile-history)
  9864. (not (eq old-hist org-refile-history))
  9865. (not (equal (car pa) (car org-refile-history))))
  9866. (setq org-refile-history
  9867. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9868. org-refile-history
  9869. (cdr org-refile-history))))
  9870. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9871. (pop org-refile-history)))
  9872. pa)
  9873. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9874. (progn
  9875. (setq parent (match-string 1 answ)
  9876. child (match-string 2 answ))
  9877. (setq parent-target (or (assoc parent tbl)
  9878. (assoc (concat parent "/") tbl)))
  9879. (when (and parent-target
  9880. (or (eq new-nodes t)
  9881. (and (eq new-nodes 'confirm)
  9882. (y-or-n-p (format "Create new node \"%s\"? "
  9883. child)))))
  9884. (org-refile-new-child parent-target child)))
  9885. (error "Invalid target location")))))
  9886. (declare-function org-string-nw-p "org-macs.el" (s))
  9887. (defun org-refile-check-position (refile-pointer)
  9888. "Check if the refile pointer matches the readline to which it points."
  9889. (let* ((file (nth 1 refile-pointer))
  9890. (re (nth 2 refile-pointer))
  9891. (pos (nth 3 refile-pointer))
  9892. buffer)
  9893. (when (org-string-nw-p re)
  9894. (setq buffer (if (markerp pos)
  9895. (marker-buffer pos)
  9896. (or (find-buffer-visiting file)
  9897. (find-file-noselect file))))
  9898. (with-current-buffer buffer
  9899. (save-excursion
  9900. (save-restriction
  9901. (widen)
  9902. (goto-char pos)
  9903. (beginning-of-line 1)
  9904. (unless (org-looking-at-p re)
  9905. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9906. (defun org-refile-new-child (parent-target child)
  9907. "Use refile target PARENT-TARGET to add new CHILD below it."
  9908. (unless parent-target
  9909. (error "Cannot find parent for new node"))
  9910. (let ((file (nth 1 parent-target))
  9911. (pos (nth 3 parent-target))
  9912. level)
  9913. (with-current-buffer (or (find-buffer-visiting file)
  9914. (find-file-noselect file))
  9915. (save-excursion
  9916. (save-restriction
  9917. (widen)
  9918. (if pos
  9919. (goto-char pos)
  9920. (goto-char (point-max))
  9921. (if (not (bolp)) (newline)))
  9922. (when (looking-at org-outline-regexp)
  9923. (setq level (funcall outline-level))
  9924. (org-end-of-subtree t t))
  9925. (org-back-over-empty-lines)
  9926. (insert "\n" (make-string
  9927. (if pos (org-get-valid-level level 1) 1) ?*)
  9928. " " child "\n")
  9929. (beginning-of-line 0)
  9930. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9931. (defun org-olpath-completing-read (prompt collection &rest args)
  9932. "Read an outline path like a file name."
  9933. (let ((thetable collection)
  9934. (org-completion-use-ido nil) ; does not work with ido.
  9935. (org-completion-use-iswitchb nil)) ; or iswitchb
  9936. (apply
  9937. 'org-icompleting-read prompt
  9938. (lambda (string predicate &optional flag)
  9939. (let (rtn r f (l (length string)))
  9940. (cond
  9941. ((eq flag nil)
  9942. ;; try completion
  9943. (try-completion string thetable))
  9944. ((eq flag t)
  9945. ;; all-completions
  9946. (setq rtn (all-completions string thetable predicate))
  9947. (mapcar
  9948. (lambda (x)
  9949. (setq r (substring x l))
  9950. (if (string-match " ([^)]*)$" x)
  9951. (setq f (match-string 0 x))
  9952. (setq f ""))
  9953. (if (string-match "/" r)
  9954. (concat string (substring r 0 (match-end 0)) f)
  9955. x))
  9956. rtn))
  9957. ((eq flag 'lambda)
  9958. ;; exact match?
  9959. (assoc string thetable)))))
  9960. args)))
  9961. ;;;; Dynamic blocks
  9962. (defun org-find-dblock (name)
  9963. "Find the first dynamic block with name NAME in the buffer.
  9964. If not found, stay at current position and return nil."
  9965. (let (pos)
  9966. (save-excursion
  9967. (goto-char (point-min))
  9968. (setq pos (and (re-search-forward (concat "^[ \t]*#\\+BEGIN:[ \t]+" name "\\>")
  9969. nil t)
  9970. (match-beginning 0))))
  9971. (if pos (goto-char pos))
  9972. pos))
  9973. (defconst org-dblock-start-re
  9974. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9975. "Matches the start line of a dynamic block, with parameters.")
  9976. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9977. "Matches the end of a dynamic block.")
  9978. (defun org-create-dblock (plist)
  9979. "Create a dynamic block section, with parameters taken from PLIST.
  9980. PLIST must contain a :name entry which is used as name of the block."
  9981. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9982. (end-of-line 1)
  9983. (newline))
  9984. (let ((col (current-column))
  9985. (name (plist-get plist :name)))
  9986. (insert "#+BEGIN: " name)
  9987. (while plist
  9988. (if (eq (car plist) :name)
  9989. (setq plist (cddr plist))
  9990. (insert " " (prin1-to-string (pop plist)))))
  9991. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9992. (beginning-of-line -2)))
  9993. (defun org-prepare-dblock ()
  9994. "Prepare dynamic block for refresh.
  9995. This empties the block, puts the cursor at the insert position and returns
  9996. the property list including an extra property :name with the block name."
  9997. (unless (looking-at org-dblock-start-re)
  9998. (error "Not at a dynamic block"))
  9999. (let* ((begdel (1+ (match-end 0)))
  10000. (name (org-no-properties (match-string 1)))
  10001. (params (append (list :name name)
  10002. (read (concat "(" (match-string 3) ")")))))
  10003. (save-excursion
  10004. (beginning-of-line 1)
  10005. (skip-chars-forward " \t")
  10006. (setq params (plist-put params :indentation-column (current-column))))
  10007. (unless (re-search-forward org-dblock-end-re nil t)
  10008. (error "Dynamic block not terminated"))
  10009. (setq params
  10010. (append params
  10011. (list :content (buffer-substring
  10012. begdel (match-beginning 0)))))
  10013. (delete-region begdel (match-beginning 0))
  10014. (goto-char begdel)
  10015. (open-line 1)
  10016. params))
  10017. (defun org-map-dblocks (&optional command)
  10018. "Apply COMMAND to all dynamic blocks in the current buffer.
  10019. If COMMAND is not given, use `org-update-dblock'."
  10020. (let ((cmd (or command 'org-update-dblock)))
  10021. (save-excursion
  10022. (goto-char (point-min))
  10023. (while (re-search-forward org-dblock-start-re nil t)
  10024. (goto-char (match-beginning 0))
  10025. (save-excursion
  10026. (condition-case nil
  10027. (funcall cmd)
  10028. (error (message "Error during update of dynamic block"))))
  10029. (unless (re-search-forward org-dblock-end-re nil t)
  10030. (error "Dynamic block not terminated"))))))
  10031. (defun org-dblock-update (&optional arg)
  10032. "User command for updating dynamic blocks.
  10033. Update the dynamic block at point. With prefix ARG, update all dynamic
  10034. blocks in the buffer."
  10035. (interactive "P")
  10036. (if arg
  10037. (org-update-all-dblocks)
  10038. (or (looking-at org-dblock-start-re)
  10039. (org-beginning-of-dblock))
  10040. (org-update-dblock)))
  10041. (defun org-update-dblock ()
  10042. "Update the dynamic block at point.
  10043. This means to empty the block, parse for parameters and then call
  10044. the correct writing function."
  10045. (interactive)
  10046. (save-window-excursion
  10047. (let* ((pos (point))
  10048. (line (org-current-line))
  10049. (params (org-prepare-dblock))
  10050. (name (plist-get params :name))
  10051. (indent (plist-get params :indentation-column))
  10052. (cmd (intern (concat "org-dblock-write:" name))))
  10053. (message "Updating dynamic block `%s' at line %d..." name line)
  10054. (funcall cmd params)
  10055. (message "Updating dynamic block `%s' at line %d...done" name line)
  10056. (goto-char pos)
  10057. (when (and indent (> indent 0))
  10058. (setq indent (make-string indent ?\ ))
  10059. (save-excursion
  10060. (org-beginning-of-dblock)
  10061. (forward-line 1)
  10062. (while (not (looking-at org-dblock-end-re))
  10063. (insert indent)
  10064. (beginning-of-line 2))
  10065. (when (looking-at org-dblock-end-re)
  10066. (and (looking-at "[ \t]+")
  10067. (replace-match ""))
  10068. (insert indent)))))))
  10069. (defun org-beginning-of-dblock ()
  10070. "Find the beginning of the dynamic block at point.
  10071. Error if there is no such block at point."
  10072. (let ((pos (point))
  10073. beg)
  10074. (end-of-line 1)
  10075. (if (and (re-search-backward org-dblock-start-re nil t)
  10076. (setq beg (match-beginning 0))
  10077. (re-search-forward org-dblock-end-re nil t)
  10078. (> (match-end 0) pos))
  10079. (goto-char beg)
  10080. (goto-char pos)
  10081. (error "Not in a dynamic block"))))
  10082. ;;;###autoload
  10083. (defun org-update-all-dblocks ()
  10084. "Update all dynamic blocks in the buffer.
  10085. This function can be used in a hook."
  10086. (interactive)
  10087. (when (derived-mode-p 'org-mode)
  10088. (org-map-dblocks 'org-update-dblock)))
  10089. ;;;; Completion
  10090. (defconst org-additional-option-like-keywords
  10091. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  10092. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  10093. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  10094. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  10095. "BEGIN:" "END:"
  10096. "ORGTBL" "TBLFM:" "TBLNAME:"
  10097. "BEGIN_EXAMPLE" "END_EXAMPLE"
  10098. "BEGIN_QUOTE" "END_QUOTE"
  10099. "BEGIN_VERSE" "END_VERSE"
  10100. "BEGIN_CENTER" "END_CENTER"
  10101. "BEGIN_SRC" "END_SRC"
  10102. "BEGIN_RESULT" "END_RESULT"
  10103. "NAME:" "RESULTS:"
  10104. "HEADER:" "HEADERS:"
  10105. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  10106. "CAPTION:" "LABEL:"
  10107. "SETUPFILE:"
  10108. "INCLUDE:"
  10109. "BIND:"
  10110. "MACRO:"))
  10111. (defcustom org-structure-template-alist
  10112. '(
  10113. ("s" "#+BEGIN_SRC ?\n\n#+END_SRC"
  10114. "<src lang=\"?\">\n\n</src>")
  10115. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE"
  10116. "<example>\n?\n</example>")
  10117. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE"
  10118. "<quote>\n?\n</quote>")
  10119. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE"
  10120. "<verse>\n?\n</verse>")
  10121. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER"
  10122. "<center>\n?\n</center>")
  10123. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX"
  10124. "<literal style=\"latex\">\n?\n</literal>")
  10125. ("L" "#+LaTeX: "
  10126. "<literal style=\"latex\">?</literal>")
  10127. ("h" "#+BEGIN_HTML\n?\n#+END_HTML"
  10128. "<literal style=\"html\">\n?\n</literal>")
  10129. ("H" "#+HTML: "
  10130. "<literal style=\"html\">?</literal>")
  10131. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  10132. ("A" "#+ASCII: ")
  10133. ("i" "#+INDEX: ?"
  10134. "#+INDEX: ?")
  10135. ("I" "#+INCLUDE %file ?"
  10136. "<include file=%file markup=\"?\">")
  10137. )
  10138. "Structure completion elements.
  10139. This is a list of abbreviation keys and values. The value gets inserted
  10140. if you type `<' followed by the key and then press the completion key,
  10141. usually `M-TAB'. %file will be replaced by a file name after prompting
  10142. for the file using completion. The cursor will be placed at the position
  10143. of the `?` in the template.
  10144. There are two templates for each key, the first uses the original Org syntax,
  10145. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  10146. the default when the /org-mtags.el/ module has been loaded. See also the
  10147. variable `org-mtags-prefer-muse-templates'."
  10148. :group 'org-completion
  10149. :type '(repeat
  10150. (string :tag "Key")
  10151. (string :tag "Template")
  10152. (string :tag "Muse Template")))
  10153. (defun org-try-structure-completion ()
  10154. "Try to complete a structure template before point.
  10155. This looks for strings like \"<e\" on an otherwise empty line and
  10156. expands them."
  10157. (let ((l (buffer-substring (point-at-bol) (point)))
  10158. a)
  10159. (when (and (looking-at "[ \t]*$")
  10160. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  10161. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  10162. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  10163. (match-beginning 1)) a)
  10164. t)))
  10165. (defun org-complete-expand-structure-template (start cell)
  10166. "Expand a structure template."
  10167. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  10168. (rpl (nth (if musep 2 1) cell))
  10169. (ind ""))
  10170. (delete-region start (point))
  10171. (when (string-match "\\`#\\+" rpl)
  10172. (cond
  10173. ((bolp))
  10174. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  10175. (setq ind (buffer-substring (point-at-bol) (point))))
  10176. (t (newline))))
  10177. (setq start (point))
  10178. (if (string-match "%file" rpl)
  10179. (setq rpl (replace-match
  10180. (concat
  10181. "\""
  10182. (save-match-data
  10183. (abbreviate-file-name (read-file-name "Include file: ")))
  10184. "\"")
  10185. t t rpl)))
  10186. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  10187. (concat "\n" ind)))
  10188. (insert rpl)
  10189. (if (re-search-backward "\\?" start t) (delete-char 1))))
  10190. ;;;; TODO, DEADLINE, Comments
  10191. (defun org-toggle-comment ()
  10192. "Change the COMMENT state of an entry."
  10193. (interactive)
  10194. (save-excursion
  10195. (org-back-to-heading)
  10196. (let (case-fold-search)
  10197. (cond
  10198. ((looking-at (format org-heading-keyword-regexp-format
  10199. org-comment-string))
  10200. (goto-char (match-end 1))
  10201. (looking-at (concat " +" org-comment-string))
  10202. (replace-match "" t t)
  10203. (when (eolp) (insert " ")))
  10204. ((looking-at org-outline-regexp)
  10205. (goto-char (match-end 0))
  10206. (insert org-comment-string " "))))))
  10207. (defvar org-last-todo-state-is-todo nil
  10208. "This is non-nil when the last TODO state change led to a TODO state.
  10209. If the last change removed the TODO tag or switched to DONE, then
  10210. this is nil.")
  10211. (defvar org-setting-tags nil) ; dynamically skipped
  10212. (defvar org-todo-setup-filter-hook nil
  10213. "Hook for functions that pre-filter todo specs.
  10214. Each function takes a todo spec and returns either nil or the spec
  10215. transformed into canonical form." )
  10216. (defvar org-todo-get-default-hook nil
  10217. "Hook for functions that get a default item for todo.
  10218. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  10219. nil or a string to be used for the todo mark." )
  10220. (defvar org-agenda-headline-snapshot-before-repeat)
  10221. (defun org-current-effective-time ()
  10222. "Return current time adjusted for `org-extend-today-until' variable"
  10223. (let* ((ct (org-current-time))
  10224. (dct (decode-time ct))
  10225. (ct1
  10226. (if (and org-use-effective-time
  10227. (< (nth 2 dct) org-extend-today-until))
  10228. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  10229. ct)))
  10230. ct1))
  10231. (defun org-todo-yesterday (&optional arg)
  10232. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  10233. (interactive "P")
  10234. (if (eq major-mode 'org-agenda-mode)
  10235. (apply 'org-agenda-todo-yesterday arg)
  10236. (let* ((hour (third (decode-time
  10237. (org-current-time))))
  10238. (org-extend-today-until (1+ hour)))
  10239. (org-todo arg))))
  10240. (defun org-todo (&optional arg)
  10241. "Change the TODO state of an item.
  10242. The state of an item is given by a keyword at the start of the heading,
  10243. like
  10244. *** TODO Write paper
  10245. *** DONE Call mom
  10246. The different keywords are specified in the variable `org-todo-keywords'.
  10247. By default the available states are \"TODO\" and \"DONE\".
  10248. So for this example: when the item starts with TODO, it is changed to DONE.
  10249. When it starts with DONE, the DONE is removed. And when neither TODO nor
  10250. DONE are present, add TODO at the beginning of the heading.
  10251. With \\[universal-argument] prefix arg, use completion to determine the new \
  10252. state.
  10253. With numeric prefix arg, switch to that state.
  10254. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  10255. keywords (nextset).
  10256. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  10257. With a numeric prefix arg of 0, inhibit note taking for the change.
  10258. For calling through lisp, arg is also interpreted in the following way:
  10259. 'none -> empty state
  10260. \"\"(empty string) -> switch to empty state
  10261. 'done -> switch to DONE
  10262. 'nextset -> switch to the next set of keywords
  10263. 'previousset -> switch to the previous set of keywords
  10264. \"WAITING\" -> switch to the specified keyword, but only if it
  10265. really is a member of `org-todo-keywords'."
  10266. (interactive "P")
  10267. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10268. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10269. 'region-start-level 'region))
  10270. org-loop-over-headlines-in-active-region)
  10271. (org-map-entries
  10272. `(org-todo ,arg)
  10273. org-loop-over-headlines-in-active-region
  10274. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  10275. (if (equal arg '(16)) (setq arg 'nextset))
  10276. (let ((org-blocker-hook org-blocker-hook)
  10277. (case-fold-search nil))
  10278. (when (equal arg '(64))
  10279. (setq arg nil org-blocker-hook nil))
  10280. (when (and org-blocker-hook
  10281. (or org-inhibit-blocking
  10282. (org-entry-get nil "NOBLOCKING")))
  10283. (setq org-blocker-hook nil))
  10284. (save-excursion
  10285. (catch 'exit
  10286. (org-back-to-heading t)
  10287. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  10288. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  10289. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  10290. (let* ((match-data (match-data))
  10291. (startpos (point-at-bol))
  10292. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  10293. (org-log-done org-log-done)
  10294. (org-log-repeat org-log-repeat)
  10295. (org-todo-log-states org-todo-log-states)
  10296. (org-inhibit-logging
  10297. (if (equal arg 0)
  10298. (progn (setq arg nil) 'note) org-inhibit-logging))
  10299. (this (match-string 1))
  10300. (hl-pos (match-beginning 0))
  10301. (head (org-get-todo-sequence-head this))
  10302. (ass (assoc head org-todo-kwd-alist))
  10303. (interpret (nth 1 ass))
  10304. (done-word (nth 3 ass))
  10305. (final-done-word (nth 4 ass))
  10306. (org-last-state (or this ""))
  10307. (completion-ignore-case t)
  10308. (member (member this org-todo-keywords-1))
  10309. (tail (cdr member))
  10310. (org-state (cond
  10311. ((and org-todo-key-trigger
  10312. (or (and (equal arg '(4))
  10313. (eq org-use-fast-todo-selection 'prefix))
  10314. (and (not arg) org-use-fast-todo-selection
  10315. (not (eq org-use-fast-todo-selection
  10316. 'prefix)))))
  10317. ;; Use fast selection
  10318. (org-fast-todo-selection))
  10319. ((and (equal arg '(4))
  10320. (or (not org-use-fast-todo-selection)
  10321. (not org-todo-key-trigger)))
  10322. ;; Read a state with completion
  10323. (org-icompleting-read
  10324. "State: " (mapcar (lambda(x) (list x))
  10325. org-todo-keywords-1)
  10326. nil t))
  10327. ((eq arg 'right)
  10328. (if this
  10329. (if tail (car tail) nil)
  10330. (car org-todo-keywords-1)))
  10331. ((eq arg 'left)
  10332. (if (equal member org-todo-keywords-1)
  10333. nil
  10334. (if this
  10335. (nth (- (length org-todo-keywords-1)
  10336. (length tail) 2)
  10337. org-todo-keywords-1)
  10338. (org-last org-todo-keywords-1))))
  10339. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  10340. (setq arg nil))) ; hack to fall back to cycling
  10341. (arg
  10342. ;; user or caller requests a specific state
  10343. (cond
  10344. ((equal arg "") nil)
  10345. ((eq arg 'none) nil)
  10346. ((eq arg 'done) (or done-word (car org-done-keywords)))
  10347. ((eq arg 'nextset)
  10348. (or (car (cdr (member head org-todo-heads)))
  10349. (car org-todo-heads)))
  10350. ((eq arg 'previousset)
  10351. (let ((org-todo-heads (reverse org-todo-heads)))
  10352. (or (car (cdr (member head org-todo-heads)))
  10353. (car org-todo-heads))))
  10354. ((car (member arg org-todo-keywords-1)))
  10355. ((stringp arg)
  10356. (error "State `%s' not valid in this file" arg))
  10357. ((nth (1- (prefix-numeric-value arg))
  10358. org-todo-keywords-1))))
  10359. ((null member) (or head (car org-todo-keywords-1)))
  10360. ((equal this final-done-word) nil) ;; -> make empty
  10361. ((null tail) nil) ;; -> first entry
  10362. ((memq interpret '(type priority))
  10363. (if (eq this-command last-command)
  10364. (car tail)
  10365. (if (> (length tail) 0)
  10366. (or done-word (car org-done-keywords))
  10367. nil)))
  10368. (t
  10369. (car tail))))
  10370. (org-state (or
  10371. (run-hook-with-args-until-success
  10372. 'org-todo-get-default-hook org-state org-last-state)
  10373. org-state))
  10374. (next (if org-state (concat " " org-state " ") " "))
  10375. (change-plist (list :type 'todo-state-change :from this :to org-state
  10376. :position startpos))
  10377. dolog now-done-p)
  10378. (when org-blocker-hook
  10379. (setq org-last-todo-state-is-todo
  10380. (not (member this org-done-keywords)))
  10381. (unless (save-excursion
  10382. (save-match-data
  10383. (org-with-wide-buffer
  10384. (run-hook-with-args-until-failure
  10385. 'org-blocker-hook change-plist))))
  10386. (if (org-called-interactively-p 'interactive)
  10387. (error "TODO state change from %s to %s blocked" this org-state)
  10388. ;; fail silently
  10389. (message "TODO state change from %s to %s blocked" this org-state)
  10390. (throw 'exit nil))))
  10391. (store-match-data match-data)
  10392. (replace-match next t t)
  10393. (unless (pos-visible-in-window-p hl-pos)
  10394. (message "TODO state changed to %s" (org-trim next)))
  10395. (unless head
  10396. (setq head (org-get-todo-sequence-head org-state)
  10397. ass (assoc head org-todo-kwd-alist)
  10398. interpret (nth 1 ass)
  10399. done-word (nth 3 ass)
  10400. final-done-word (nth 4 ass)))
  10401. (when (memq arg '(nextset previousset))
  10402. (message "Keyword-Set %d/%d: %s"
  10403. (- (length org-todo-sets) -1
  10404. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  10405. (length org-todo-sets)
  10406. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  10407. (setq org-last-todo-state-is-todo
  10408. (not (member org-state org-done-keywords)))
  10409. (setq now-done-p (and (member org-state org-done-keywords)
  10410. (not (member this org-done-keywords))))
  10411. (and logging (org-local-logging logging))
  10412. (when (and (or org-todo-log-states org-log-done)
  10413. (not (eq org-inhibit-logging t))
  10414. (not (memq arg '(nextset previousset))))
  10415. ;; we need to look at recording a time and note
  10416. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  10417. (nth 2 (assoc this org-todo-log-states))))
  10418. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  10419. (setq dolog 'time))
  10420. (when (and org-state
  10421. (member org-state org-not-done-keywords)
  10422. (not (member this org-not-done-keywords)))
  10423. ;; This is now a todo state and was not one before
  10424. ;; If there was a CLOSED time stamp, get rid of it.
  10425. (org-add-planning-info nil nil 'closed))
  10426. (when (and now-done-p org-log-done)
  10427. ;; It is now done, and it was not done before
  10428. (org-add-planning-info 'closed (org-current-effective-time))
  10429. (if (and (not dolog) (eq 'note org-log-done))
  10430. (org-add-log-setup 'done org-state this 'findpos 'note)))
  10431. (when (and org-state dolog)
  10432. ;; This is a non-nil state, and we need to log it
  10433. (org-add-log-setup 'state org-state this 'findpos dolog)))
  10434. ;; Fixup tag positioning
  10435. (org-todo-trigger-tag-changes org-state)
  10436. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  10437. (when org-provide-todo-statistics
  10438. (org-update-parent-todo-statistics))
  10439. (run-hooks 'org-after-todo-state-change-hook)
  10440. (if (and arg (not (member org-state org-done-keywords)))
  10441. (setq head (org-get-todo-sequence-head org-state)))
  10442. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  10443. ;; Do we need to trigger a repeat?
  10444. (when now-done-p
  10445. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  10446. ;; This is for the agenda, take a snapshot of the headline.
  10447. (save-match-data
  10448. (setq org-agenda-headline-snapshot-before-repeat
  10449. (org-get-heading))))
  10450. (org-auto-repeat-maybe org-state))
  10451. ;; Fixup cursor location if close to the keyword
  10452. (if (and (outline-on-heading-p)
  10453. (not (bolp))
  10454. (save-excursion (beginning-of-line 1)
  10455. (looking-at org-todo-line-regexp))
  10456. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10457. (progn
  10458. (goto-char (or (match-end 2) (match-end 1)))
  10459. (and (looking-at " ") (just-one-space))))
  10460. (when org-trigger-hook
  10461. (save-excursion
  10462. (run-hook-with-args 'org-trigger-hook change-plist)))))))))
  10463. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10464. "Block turning an entry into a TODO, using the hierarchy.
  10465. This checks whether the current task should be blocked from state
  10466. changes. Such blocking occurs when:
  10467. 1. The task has children which are not all in a completed state.
  10468. 2. A task has a parent with the property :ORDERED:, and there
  10469. are siblings prior to the current task with incomplete
  10470. status.
  10471. 3. The parent of the task is blocked because it has siblings that should
  10472. be done first, or is child of a block grandparent TODO entry."
  10473. (if (not org-enforce-todo-dependencies)
  10474. t ; if locally turned off don't block
  10475. (catch 'dont-block
  10476. ;; If this is not a todo state change, or if this entry is already DONE,
  10477. ;; do not block
  10478. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10479. (member (plist-get change-plist :from)
  10480. (cons 'done org-done-keywords))
  10481. (member (plist-get change-plist :to)
  10482. (cons 'todo org-not-done-keywords))
  10483. (not (plist-get change-plist :to)))
  10484. (throw 'dont-block t))
  10485. ;; If this task has children, and any are undone, it's blocked
  10486. (save-excursion
  10487. (org-back-to-heading t)
  10488. (let ((this-level (funcall outline-level)))
  10489. (outline-next-heading)
  10490. (let ((child-level (funcall outline-level)))
  10491. (while (and (not (eobp))
  10492. (> child-level this-level))
  10493. ;; this todo has children, check whether they are all
  10494. ;; completed
  10495. (if (and (not (org-entry-is-done-p))
  10496. (org-entry-is-todo-p))
  10497. (throw 'dont-block nil))
  10498. (outline-next-heading)
  10499. (setq child-level (funcall outline-level))))))
  10500. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10501. ;; any previous siblings are undone, it's blocked
  10502. (save-excursion
  10503. (org-back-to-heading t)
  10504. (let* ((pos (point))
  10505. (parent-pos (and (org-up-heading-safe) (point))))
  10506. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10507. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10508. (forward-line 1)
  10509. (re-search-forward org-not-done-heading-regexp pos t))
  10510. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10511. ;; Search further up the hierarchy, to see if an ancestor is blocked
  10512. (while t
  10513. (goto-char parent-pos)
  10514. (if (not (looking-at org-not-done-heading-regexp))
  10515. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10516. (setq pos (point))
  10517. (setq parent-pos (and (org-up-heading-safe) (point)))
  10518. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10519. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10520. (forward-line 1)
  10521. (re-search-forward org-not-done-heading-regexp pos t))
  10522. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10523. (defcustom org-track-ordered-property-with-tag nil
  10524. "Should the ORDERED property also be shown as a tag?
  10525. The ORDERED property decides if an entry should require subtasks to be
  10526. completed in sequence. Since a property is not very visible, setting
  10527. this option means that toggling the ORDERED property with the command
  10528. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10529. not relevant for the behavior, but it makes things more visible.
  10530. Note that toggling the tag with tags commands will not change the property
  10531. and therefore not influence behavior!
  10532. This can be t, meaning the tag ORDERED should be used, It can also be a
  10533. string to select a different tag for this task."
  10534. :group 'org-todo
  10535. :type '(choice
  10536. (const :tag "No tracking" nil)
  10537. (const :tag "Track with ORDERED tag" t)
  10538. (string :tag "Use other tag")))
  10539. (defun org-toggle-ordered-property ()
  10540. "Toggle the ORDERED property of the current entry.
  10541. For better visibility, you can track the value of this property with a tag.
  10542. See variable `org-track-ordered-property-with-tag'."
  10543. (interactive)
  10544. (let* ((t1 org-track-ordered-property-with-tag)
  10545. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10546. (save-excursion
  10547. (org-back-to-heading)
  10548. (if (org-entry-get nil "ORDERED")
  10549. (progn
  10550. (org-delete-property "ORDERED")
  10551. (and tag (org-toggle-tag tag 'off))
  10552. (message "Subtasks can be completed in arbitrary order"))
  10553. (org-entry-put nil "ORDERED" "t")
  10554. (and tag (org-toggle-tag tag 'on))
  10555. (message "Subtasks must be completed in sequence")))))
  10556. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10557. (defun org-block-todo-from-checkboxes (change-plist)
  10558. "Block turning an entry into a TODO, using checkboxes.
  10559. This checks whether the current task should be blocked from state
  10560. changes because there are unchecked boxes in this entry."
  10561. (if (not org-enforce-todo-checkbox-dependencies)
  10562. t ; if locally turned off don't block
  10563. (catch 'dont-block
  10564. ;; If this is not a todo state change, or if this entry is already DONE,
  10565. ;; do not block
  10566. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10567. (member (plist-get change-plist :from)
  10568. (cons 'done org-done-keywords))
  10569. (member (plist-get change-plist :to)
  10570. (cons 'todo org-not-done-keywords))
  10571. (not (plist-get change-plist :to)))
  10572. (throw 'dont-block t))
  10573. ;; If this task has checkboxes that are not checked, it's blocked
  10574. (save-excursion
  10575. (org-back-to-heading t)
  10576. (let ((beg (point)) end)
  10577. (outline-next-heading)
  10578. (setq end (point))
  10579. (goto-char beg)
  10580. (if (org-list-search-forward
  10581. (concat (org-item-beginning-re)
  10582. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  10583. "\\[[- ]\\]")
  10584. end t)
  10585. (progn
  10586. (if (boundp 'org-blocked-by-checkboxes)
  10587. (setq org-blocked-by-checkboxes t))
  10588. (throw 'dont-block nil)))))
  10589. t))) ; do not block
  10590. (defun org-entry-blocked-p ()
  10591. "Is the current entry blocked?"
  10592. (if (org-entry-get nil "NOBLOCKING")
  10593. nil ;; Never block this entry
  10594. (not
  10595. (run-hook-with-args-until-failure
  10596. 'org-blocker-hook
  10597. (list :type 'todo-state-change
  10598. :position (point)
  10599. :from 'todo
  10600. :to 'done)))))
  10601. (defun org-update-statistics-cookies (all)
  10602. "Update the statistics cookie, either from TODO or from checkboxes.
  10603. This should be called with the cursor in a line with a statistics cookie."
  10604. (interactive "P")
  10605. (if all
  10606. (progn
  10607. (org-update-checkbox-count 'all)
  10608. (org-map-entries 'org-update-parent-todo-statistics))
  10609. (if (not (org-at-heading-p))
  10610. (org-update-checkbox-count)
  10611. (let ((pos (move-marker (make-marker) (point)))
  10612. end l1 l2)
  10613. (ignore-errors (org-back-to-heading t))
  10614. (if (not (org-at-heading-p))
  10615. (org-update-checkbox-count)
  10616. (setq l1 (org-outline-level))
  10617. (setq end (save-excursion
  10618. (outline-next-heading)
  10619. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  10620. (point)))
  10621. (if (and (save-excursion
  10622. (re-search-forward
  10623. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10624. (not (save-excursion (re-search-forward
  10625. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10626. (org-update-checkbox-count)
  10627. (if (and l2 (> l2 l1))
  10628. (progn
  10629. (goto-char end)
  10630. (org-update-parent-todo-statistics))
  10631. (goto-char pos)
  10632. (beginning-of-line 1)
  10633. (while (re-search-forward
  10634. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10635. (point-at-eol) t)
  10636. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10637. (goto-char pos)
  10638. (move-marker pos nil)))))
  10639. (defvar org-entry-property-inherited-from) ;; defined below
  10640. (defun org-update-parent-todo-statistics ()
  10641. "Update any statistics cookie in the parent of the current headline.
  10642. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10643. the entire subtree and this will travel up the hierarchy and update
  10644. statistics everywhere."
  10645. (let* ((prop (save-excursion (org-up-heading-safe)
  10646. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  10647. (recursive (or (not org-hierarchical-todo-statistics)
  10648. (and prop (string-match "\\<recursive\\>" prop))))
  10649. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  10650. 0))
  10651. (first t)
  10652. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10653. level ltoggle l1 new ndel
  10654. (cnt-all 0) (cnt-done 0) is-percent kwd
  10655. checkbox-beg ov ovs ove cookie-present)
  10656. (catch 'exit
  10657. (save-excursion
  10658. (beginning-of-line 1)
  10659. (setq ltoggle (funcall outline-level))
  10660. ;; Three situations are to consider:
  10661. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  10662. ;; to the top-level ancestor on the headline;
  10663. ;; 2. If parent has "recursive" property, repeat up to the
  10664. ;; headline setting that property, taking inheritance into
  10665. ;; account;
  10666. ;; 3. Else, move up to direct parent and proceed only once.
  10667. (while (and (setq level (org-up-heading-safe))
  10668. (or recursive first)
  10669. (>= (point) lim))
  10670. (setq first nil cookie-present nil)
  10671. (unless (and level
  10672. (not (string-match
  10673. "\\<checkbox\\>"
  10674. (downcase (or (org-entry-get nil "COOKIE_DATA")
  10675. "")))))
  10676. (throw 'exit nil))
  10677. (while (re-search-forward box-re (point-at-eol) t)
  10678. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10679. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10680. (save-match-data
  10681. (unless (outline-next-heading) (throw 'exit nil))
  10682. (while (and (looking-at org-complex-heading-regexp)
  10683. (> (setq l1 (length (match-string 1))) level))
  10684. (setq kwd (and (or recursive (= l1 ltoggle))
  10685. (match-string 2)))
  10686. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10687. (and (listp org-provide-todo-statistics)
  10688. (or (member kwd org-provide-todo-statistics)
  10689. (member kwd org-done-keywords))))
  10690. (setq cnt-all (1+ cnt-all))
  10691. (if (eq org-provide-todo-statistics t)
  10692. (and kwd (setq cnt-all (1+ cnt-all)))))
  10693. (and (member kwd org-done-keywords)
  10694. (setq cnt-done (1+ cnt-done)))
  10695. (outline-next-heading)))
  10696. (setq new
  10697. (if is-percent
  10698. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10699. (format "[%d/%d]" cnt-done cnt-all))
  10700. ndel (- (match-end 0) checkbox-beg))
  10701. ;; handle overlays when updating cookie from column view
  10702. (when (setq ov (car (overlays-at checkbox-beg)))
  10703. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10704. (delete-overlay ov))
  10705. (goto-char checkbox-beg)
  10706. (insert new)
  10707. (delete-region (point) (+ (point) ndel))
  10708. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  10709. (when ov (move-overlay ov ovs ove)))
  10710. (when cookie-present
  10711. (run-hook-with-args 'org-after-todo-statistics-hook
  10712. cnt-done (- cnt-all cnt-done))))))
  10713. (run-hooks 'org-todo-statistics-hook)))
  10714. (defvar org-after-todo-statistics-hook nil
  10715. "Hook that is called after a TODO statistics cookie has been updated.
  10716. Each function is called with two arguments: the number of not-done entries
  10717. and the number of done entries.
  10718. For example, the following function, when added to this hook, will switch
  10719. an entry to DONE when all children are done, and back to TODO when new
  10720. entries are set to a TODO status. Note that this hook is only called
  10721. when there is a statistics cookie in the headline!
  10722. (defun org-summary-todo (n-done n-not-done)
  10723. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10724. (let (org-log-done org-log-states) ; turn off logging
  10725. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10726. ")
  10727. (defvar org-todo-statistics-hook nil
  10728. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10729. This hook runs even if there is no statistics cookie present, in which case
  10730. `org-after-todo-statistics-hook' would not run.")
  10731. (defun org-todo-trigger-tag-changes (state)
  10732. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10733. (let ((l org-todo-state-tags-triggers)
  10734. changes)
  10735. (when (or (not state) (equal state ""))
  10736. (setq changes (append changes (cdr (assoc "" l)))))
  10737. (when (and (stringp state) (> (length state) 0))
  10738. (setq changes (append changes (cdr (assoc state l)))))
  10739. (when (member state org-not-done-keywords)
  10740. (setq changes (append changes (cdr (assoc 'todo l)))))
  10741. (when (member state org-done-keywords)
  10742. (setq changes (append changes (cdr (assoc 'done l)))))
  10743. (dolist (c changes)
  10744. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10745. (defun org-local-logging (value)
  10746. "Get logging settings from a property VALUE."
  10747. (let* (words w a)
  10748. ;; directly set the variables, they are already local.
  10749. (setq org-log-done nil
  10750. org-log-repeat nil
  10751. org-todo-log-states nil)
  10752. (setq words (org-split-string value))
  10753. (while (setq w (pop words))
  10754. (cond
  10755. ((setq a (assoc w org-startup-options))
  10756. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10757. (set (nth 1 a) (nth 2 a))))
  10758. ((setq a (org-extract-log-state-settings w))
  10759. (and (member (car a) org-todo-keywords-1)
  10760. (push a org-todo-log-states)))))))
  10761. (defun org-get-todo-sequence-head (kwd)
  10762. "Return the head of the TODO sequence to which KWD belongs.
  10763. If KWD is not set, check if there is a text property remembering the
  10764. right sequence."
  10765. (let (p)
  10766. (cond
  10767. ((not kwd)
  10768. (or (get-text-property (point-at-bol) 'org-todo-head)
  10769. (progn
  10770. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10771. nil (point-at-eol)))
  10772. (get-text-property p 'org-todo-head))))
  10773. ((not (member kwd org-todo-keywords-1))
  10774. (car org-todo-keywords-1))
  10775. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10776. (defun org-fast-todo-selection ()
  10777. "Fast TODO keyword selection with single keys.
  10778. Returns the new TODO keyword, or nil if no state change should occur."
  10779. (let* ((fulltable org-todo-key-alist)
  10780. (done-keywords org-done-keywords) ;; needed for the faces.
  10781. (maxlen (apply 'max (mapcar
  10782. (lambda (x)
  10783. (if (stringp (car x)) (string-width (car x)) 0))
  10784. fulltable)))
  10785. (expert nil)
  10786. (fwidth (+ maxlen 3 1 3))
  10787. (ncol (/ (- (window-width) 4) fwidth))
  10788. tg cnt e c tbl
  10789. groups ingroup)
  10790. (save-excursion
  10791. (save-window-excursion
  10792. (if expert
  10793. (set-buffer (get-buffer-create " *Org todo*"))
  10794. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10795. (erase-buffer)
  10796. (org-set-local 'org-done-keywords done-keywords)
  10797. (setq tbl fulltable cnt 0)
  10798. (while (setq e (pop tbl))
  10799. (cond
  10800. ((equal e '(:startgroup))
  10801. (push '() groups) (setq ingroup t)
  10802. (when (not (= cnt 0))
  10803. (setq cnt 0)
  10804. (insert "\n"))
  10805. (insert "{ "))
  10806. ((equal e '(:endgroup))
  10807. (setq ingroup nil cnt 0)
  10808. (insert "}\n"))
  10809. ((equal e '(:newline))
  10810. (when (not (= cnt 0))
  10811. (setq cnt 0)
  10812. (insert "\n")
  10813. (setq e (car tbl))
  10814. (while (equal (car tbl) '(:newline))
  10815. (insert "\n")
  10816. (setq tbl (cdr tbl)))))
  10817. (t
  10818. (setq tg (car e) c (cdr e))
  10819. (if ingroup (push tg (car groups)))
  10820. (setq tg (org-add-props tg nil 'face
  10821. (org-get-todo-face tg)))
  10822. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10823. (insert "[" c "] " tg (make-string
  10824. (- fwidth 4 (length tg)) ?\ ))
  10825. (when (= (setq cnt (1+ cnt)) ncol)
  10826. (insert "\n")
  10827. (if ingroup (insert " "))
  10828. (setq cnt 0)))))
  10829. (insert "\n")
  10830. (goto-char (point-min))
  10831. (if (not expert) (org-fit-window-to-buffer))
  10832. (message "[a-z..]:Set [SPC]:clear")
  10833. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10834. (cond
  10835. ((or (= c ?\C-g)
  10836. (and (= c ?q) (not (rassoc c fulltable))))
  10837. (setq quit-flag t))
  10838. ((= c ?\ ) nil)
  10839. ((setq e (rassoc c fulltable) tg (car e))
  10840. tg)
  10841. (t (setq quit-flag t)))))))
  10842. (defun org-entry-is-todo-p ()
  10843. (member (org-get-todo-state) org-not-done-keywords))
  10844. (defun org-entry-is-done-p ()
  10845. (member (org-get-todo-state) org-done-keywords))
  10846. (defun org-get-todo-state ()
  10847. (save-excursion
  10848. (org-back-to-heading t)
  10849. (and (looking-at org-todo-line-regexp)
  10850. (match-end 2)
  10851. (match-string 2))))
  10852. (defun org-at-date-range-p (&optional inactive-ok)
  10853. "Is the cursor inside a date range?"
  10854. (interactive)
  10855. (save-excursion
  10856. (catch 'exit
  10857. (let ((pos (point)))
  10858. (skip-chars-backward "^[<\r\n")
  10859. (skip-chars-backward "<[")
  10860. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10861. (>= (match-end 0) pos)
  10862. (throw 'exit t))
  10863. (skip-chars-backward "^<[\r\n")
  10864. (skip-chars-backward "<[")
  10865. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10866. (>= (match-end 0) pos)
  10867. (throw 'exit t)))
  10868. nil)))
  10869. (defun org-get-repeat (&optional tagline)
  10870. "Check if there is a deadline/schedule with repeater in this entry."
  10871. (save-match-data
  10872. (save-excursion
  10873. (org-back-to-heading t)
  10874. (and (re-search-forward (if tagline
  10875. (concat tagline "\\s-*" org-repeat-re)
  10876. org-repeat-re)
  10877. (org-entry-end-position) t)
  10878. (match-string-no-properties 1)))))
  10879. (defvar org-last-changed-timestamp)
  10880. (defvar org-last-inserted-timestamp)
  10881. (defvar org-log-post-message)
  10882. (defvar org-log-note-purpose)
  10883. (defvar org-log-note-how)
  10884. (defvar org-log-note-extra)
  10885. (defun org-auto-repeat-maybe (done-word)
  10886. "Check if the current headline contains a repeated deadline/schedule.
  10887. If yes, set TODO state back to what it was and change the base date
  10888. of repeating deadline/scheduled time stamps to new date.
  10889. This function is run automatically after each state change to a DONE state."
  10890. ;; last-state is dynamically scoped into this function
  10891. (let* ((repeat (org-get-repeat))
  10892. (aa (assoc org-last-state org-todo-kwd-alist))
  10893. (interpret (nth 1 aa))
  10894. (head (nth 2 aa))
  10895. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  10896. (msg "Entry repeats: ")
  10897. (org-log-done nil)
  10898. (org-todo-log-states nil)
  10899. re type n what ts time to-state)
  10900. (when repeat
  10901. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10902. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10903. org-todo-repeat-to-state))
  10904. (unless (and to-state (member to-state org-todo-keywords-1))
  10905. (setq to-state (if (eq interpret 'type) org-last-state head)))
  10906. (org-todo to-state)
  10907. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10908. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10909. (org-time-stamp-format t t))))
  10910. (when org-log-repeat
  10911. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10912. (memq 'org-add-log-note post-command-hook))
  10913. ;; OK, we are already setup for some record
  10914. (if (eq org-log-repeat 'note)
  10915. ;; make sure we take a note, not only a time stamp
  10916. (setq org-log-note-how 'note))
  10917. ;; Set up for taking a record
  10918. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10919. org-last-state
  10920. 'findpos org-log-repeat)))
  10921. (org-back-to-heading t)
  10922. (org-add-planning-info nil nil 'closed)
  10923. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10924. org-deadline-time-regexp "\\)\\|\\("
  10925. org-ts-regexp "\\)"))
  10926. (while (re-search-forward
  10927. re (save-excursion (outline-next-heading) (point)) t)
  10928. (setq type (if (match-end 1) org-scheduled-string
  10929. (if (match-end 3) org-deadline-string "Plain:"))
  10930. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10931. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  10932. (setq n (string-to-number (match-string 2 ts))
  10933. what (match-string 3 ts))
  10934. (if (equal what "w") (setq n (* n 7) what "d"))
  10935. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  10936. (error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  10937. ;; Preparation, see if we need to modify the start date for the change
  10938. (when (match-end 1)
  10939. (setq time (save-match-data (org-time-string-to-time ts)))
  10940. (cond
  10941. ((equal (match-string 1 ts) ".")
  10942. ;; Shift starting date to today
  10943. (org-timestamp-change
  10944. (- (org-today) (time-to-days time))
  10945. 'day))
  10946. ((equal (match-string 1 ts) "+")
  10947. (let ((nshiftmax 10) (nshift 0))
  10948. (while (or (= nshift 0)
  10949. (<= (time-to-days time)
  10950. (time-to-days (current-time))))
  10951. (when (= (incf nshift) nshiftmax)
  10952. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10953. (error "Abort")))
  10954. (org-timestamp-change n (cdr (assoc what whata)))
  10955. (org-at-timestamp-p t)
  10956. (setq ts (match-string 1))
  10957. (setq time (save-match-data (org-time-string-to-time ts)))))
  10958. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10959. ;; rematch, so that we have everything in place for the real shift
  10960. (org-at-timestamp-p t)
  10961. (setq ts (match-string 1))
  10962. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  10963. (org-timestamp-change n (cdr (assoc what whata)))
  10964. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10965. (setq org-log-post-message msg)
  10966. (message "%s" msg))))
  10967. (defun org-show-todo-tree (arg)
  10968. "Make a compact tree which shows all headlines marked with TODO.
  10969. The tree will show the lines where the regexp matches, and all higher
  10970. headlines above the match.
  10971. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10972. With a numeric prefix N, construct a sparse tree for the Nth element
  10973. of `org-todo-keywords-1'."
  10974. (interactive "P")
  10975. (let ((case-fold-search nil)
  10976. (kwd-re
  10977. (cond ((null arg) org-not-done-regexp)
  10978. ((equal arg '(4))
  10979. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10980. (mapcar 'list org-todo-keywords-1))))
  10981. (concat "\\("
  10982. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10983. "\\)\\>")))
  10984. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10985. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10986. org-todo-keywords-1)))
  10987. (t (error "Invalid prefix argument: %s" arg)))))
  10988. (message "%d TODO entries found"
  10989. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  10990. (defun org-deadline (&optional remove time)
  10991. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10992. With argument REMOVE, remove any deadline from the item.
  10993. With argument TIME, set the deadline at the corresponding date. TIME
  10994. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  10995. (interactive "P")
  10996. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  10997. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  10998. 'region-start-level 'region))
  10999. org-loop-over-headlines-in-active-region)
  11000. (org-map-entries
  11001. `(org-deadline ',remove ,time)
  11002. org-loop-over-headlines-in-active-region
  11003. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11004. (let* ((old-date (org-entry-get nil "DEADLINE"))
  11005. (repeater (and old-date
  11006. (string-match
  11007. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11008. old-date)
  11009. (match-string 1 old-date))))
  11010. (if remove
  11011. (progn
  11012. (when (and old-date org-log-redeadline)
  11013. (org-add-log-setup 'deldeadline nil old-date 'findpos
  11014. org-log-redeadline))
  11015. (org-remove-timestamp-with-keyword org-deadline-string)
  11016. (message "Item no longer has a deadline."))
  11017. (org-add-planning-info 'deadline time 'closed)
  11018. (when (and old-date org-log-redeadline
  11019. (not (equal old-date
  11020. (substring org-last-inserted-timestamp 1 -1))))
  11021. (org-add-log-setup 'redeadline nil old-date 'findpos
  11022. org-log-redeadline))
  11023. (when repeater
  11024. (save-excursion
  11025. (org-back-to-heading t)
  11026. (when (re-search-forward (concat org-deadline-string " "
  11027. org-last-inserted-timestamp)
  11028. (save-excursion
  11029. (outline-next-heading) (point)) t)
  11030. (goto-char (1- (match-end 0)))
  11031. (insert " " repeater)
  11032. (setq org-last-inserted-timestamp
  11033. (concat (substring org-last-inserted-timestamp 0 -1)
  11034. " " repeater
  11035. (substring org-last-inserted-timestamp -1))))))
  11036. (message "Deadline on %s" org-last-inserted-timestamp)))))
  11037. (defun org-schedule (&optional remove time)
  11038. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  11039. With argument REMOVE, remove any scheduling date from the item.
  11040. With argument TIME, scheduled at the corresponding date. TIME can
  11041. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11042. (interactive "P")
  11043. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11044. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11045. 'region-start-level 'region))
  11046. org-loop-over-headlines-in-active-region)
  11047. (org-map-entries
  11048. `(org-schedule ',remove ,time)
  11049. org-loop-over-headlines-in-active-region
  11050. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11051. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  11052. (repeater (and old-date
  11053. (string-match
  11054. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  11055. old-date)
  11056. (match-string 1 old-date))))
  11057. (if remove
  11058. (progn
  11059. (when (and old-date org-log-reschedule)
  11060. (org-add-log-setup 'delschedule nil old-date 'findpos
  11061. org-log-reschedule))
  11062. (org-remove-timestamp-with-keyword org-scheduled-string)
  11063. (message "Item is no longer scheduled."))
  11064. (org-add-planning-info 'scheduled time 'closed)
  11065. (when (and old-date org-log-reschedule
  11066. (not (equal old-date
  11067. (substring org-last-inserted-timestamp 1 -1))))
  11068. (org-add-log-setup 'reschedule nil old-date 'findpos
  11069. org-log-reschedule))
  11070. (when repeater
  11071. (save-excursion
  11072. (org-back-to-heading t)
  11073. (when (re-search-forward (concat org-scheduled-string " "
  11074. org-last-inserted-timestamp)
  11075. (save-excursion
  11076. (outline-next-heading) (point)) t)
  11077. (goto-char (1- (match-end 0)))
  11078. (insert " " repeater)
  11079. (setq org-last-inserted-timestamp
  11080. (concat (substring org-last-inserted-timestamp 0 -1)
  11081. " " repeater
  11082. (substring org-last-inserted-timestamp -1))))))
  11083. (message "Scheduled to %s" org-last-inserted-timestamp)))))
  11084. (defun org-get-scheduled-time (pom &optional inherit)
  11085. "Get the scheduled time as a time tuple, of a format suitable
  11086. for calling org-schedule with, or if there is no scheduling,
  11087. returns nil."
  11088. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  11089. (when time
  11090. (apply 'encode-time (org-parse-time-string time)))))
  11091. (defun org-get-deadline-time (pom &optional inherit)
  11092. "Get the deadline as a time tuple, of a format suitable for
  11093. calling org-deadline with, or if there is no scheduling, returns
  11094. nil."
  11095. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  11096. (when time
  11097. (apply 'encode-time (org-parse-time-string time)))))
  11098. (defun org-remove-timestamp-with-keyword (keyword)
  11099. "Remove all time stamps with KEYWORD in the current entry."
  11100. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  11101. beg)
  11102. (save-excursion
  11103. (org-back-to-heading t)
  11104. (setq beg (point))
  11105. (outline-next-heading)
  11106. (while (re-search-backward re beg t)
  11107. (replace-match "")
  11108. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  11109. (equal (char-before) ?\ ))
  11110. (backward-delete-char 1)
  11111. (if (string-match "^[ \t]*$" (buffer-substring
  11112. (point-at-bol) (point-at-eol)))
  11113. (delete-region (point-at-bol)
  11114. (min (point-max) (1+ (point-at-eol))))))))))
  11115. (defun org-add-planning-info (what &optional time &rest remove)
  11116. "Insert new timestamp with keyword in the line directly after the headline.
  11117. WHAT indicates what kind of time stamp to add. TIME indicates the time to use.
  11118. If non is given, the user is prompted for a date.
  11119. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  11120. be removed."
  11121. (interactive)
  11122. (let (org-time-was-given org-end-time-was-given ts
  11123. end default-time default-input)
  11124. (catch 'exit
  11125. (when (and (memq what '(scheduled deadline))
  11126. (or (not time)
  11127. (and (stringp time)
  11128. (string-match "^[-+]+[0-9]" time))))
  11129. ;; Try to get a default date/time from existing timestamp
  11130. (save-excursion
  11131. (org-back-to-heading t)
  11132. (setq end (save-excursion (outline-next-heading) (point)))
  11133. (when (re-search-forward (if (eq what 'scheduled)
  11134. org-scheduled-time-regexp
  11135. org-deadline-time-regexp)
  11136. end t)
  11137. (setq ts (match-string 1)
  11138. default-time
  11139. (apply 'encode-time (org-parse-time-string ts))
  11140. default-input (and ts (org-get-compact-tod ts))))))
  11141. (when what
  11142. (setq time
  11143. (if (stringp time)
  11144. ;; This is a string (relative or absolute), set proper date
  11145. (apply 'encode-time
  11146. (org-read-date-analyze
  11147. time default-time (decode-time default-time)))
  11148. ;; If necessary, get the time from the user
  11149. (or time (org-read-date nil 'to-time nil nil
  11150. default-time default-input)))))
  11151. (when (and org-insert-labeled-timestamps-at-point
  11152. (member what '(scheduled deadline)))
  11153. (insert
  11154. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  11155. (org-insert-time-stamp time org-time-was-given
  11156. nil nil nil (list org-end-time-was-given))
  11157. (setq what nil))
  11158. (save-excursion
  11159. (save-restriction
  11160. (let (col list elt ts buffer-invisibility-spec)
  11161. (org-back-to-heading t)
  11162. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"))
  11163. (goto-char (match-end 1))
  11164. (setq col (current-column))
  11165. (goto-char (match-end 0))
  11166. (if (eobp) (insert "\n") (forward-char 1))
  11167. (when (and (not what)
  11168. (not (looking-at
  11169. (concat "[ \t]*"
  11170. org-keyword-time-not-clock-regexp))))
  11171. ;; Nothing to add, nothing to remove...... :-)
  11172. (throw 'exit nil))
  11173. (if (and (not (looking-at org-outline-regexp))
  11174. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  11175. "[^\r\n]*"))
  11176. (not (equal (match-string 1) org-clock-string)))
  11177. (narrow-to-region (match-beginning 0) (match-end 0))
  11178. (insert-before-markers "\n")
  11179. (backward-char 1)
  11180. (narrow-to-region (point) (point))
  11181. (and org-adapt-indentation (org-indent-to-column col)))
  11182. ;; Check if we have to remove something.
  11183. (setq list (cons what remove))
  11184. (while list
  11185. (setq elt (pop list))
  11186. (when (or (and (eq elt 'scheduled)
  11187. (re-search-forward org-scheduled-time-regexp nil t))
  11188. (and (eq elt 'deadline)
  11189. (re-search-forward org-deadline-time-regexp nil t))
  11190. (and (eq elt 'closed)
  11191. (re-search-forward org-closed-time-regexp nil t)))
  11192. (replace-match "")
  11193. (if (looking-at "--+<[^>]+>") (replace-match ""))))
  11194. (and (looking-at "[ \t]+") (replace-match ""))
  11195. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  11196. (when what
  11197. (insert
  11198. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  11199. (cond ((eq what 'scheduled) org-scheduled-string)
  11200. ((eq what 'deadline) org-deadline-string)
  11201. ((eq what 'closed) org-closed-string))
  11202. " ")
  11203. (setq ts (org-insert-time-stamp
  11204. time
  11205. (or org-time-was-given
  11206. (and (eq what 'closed) org-log-done-with-time))
  11207. (eq what 'closed)
  11208. nil nil (list org-end-time-was-given)))
  11209. (insert
  11210. (if (not (or (bolp) (eq (char-before) ?\ )
  11211. (memq (char-after) '(32 10))
  11212. (eobp))) " " ""))
  11213. (end-of-line 1))
  11214. (goto-char (point-min))
  11215. (widen)
  11216. (if (and (looking-at "[ \t]*\n")
  11217. (equal (char-before) ?\n))
  11218. (delete-region (1- (point)) (point-at-eol)))
  11219. ts))))))
  11220. (defvar org-log-note-marker (make-marker))
  11221. (defvar org-log-note-purpose nil)
  11222. (defvar org-log-note-state nil)
  11223. (defvar org-log-note-previous-state nil)
  11224. (defvar org-log-note-how nil)
  11225. (defvar org-log-note-extra nil)
  11226. (defvar org-log-note-window-configuration nil)
  11227. (defvar org-log-note-return-to (make-marker))
  11228. (defvar org-log-note-effective-time nil
  11229. "Remembered current time so that dynamically scoped
  11230. `org-extend-today-until' affects tha timestamps in state change
  11231. log")
  11232. (defvar org-log-post-message nil
  11233. "Message to be displayed after a log note has been stored.
  11234. The auto-repeater uses this.")
  11235. (defun org-add-note ()
  11236. "Add a note to the current entry.
  11237. This is done in the same way as adding a state change note."
  11238. (interactive)
  11239. (org-add-log-setup 'note nil nil 'findpos nil))
  11240. (defvar org-property-end-re)
  11241. (defun org-add-log-setup (&optional purpose state prev-state
  11242. findpos how extra)
  11243. "Set up the post command hook to take a note.
  11244. If this is about to TODO state change, the new state is expected in STATE.
  11245. When FINDPOS is non-nil, find the correct position for the note in
  11246. the current entry. If not, assume that it can be inserted at point.
  11247. HOW is an indicator what kind of note should be created.
  11248. EXTRA is additional text that will be inserted into the notes buffer."
  11249. (let* ((org-log-into-drawer (org-log-into-drawer))
  11250. (drawer (cond ((stringp org-log-into-drawer)
  11251. org-log-into-drawer)
  11252. (org-log-into-drawer "LOGBOOK")
  11253. (t nil))))
  11254. (save-restriction
  11255. (save-excursion
  11256. (when findpos
  11257. (org-back-to-heading t)
  11258. (narrow-to-region (point) (save-excursion
  11259. (outline-next-heading) (point)))
  11260. (looking-at (concat org-outline-regexp "\\( *\\)[^\r\n]*"
  11261. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  11262. "[^\r\n]*\\)?"))
  11263. (goto-char (match-end 0))
  11264. (cond
  11265. (drawer
  11266. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  11267. nil t)
  11268. (progn
  11269. (goto-char (match-end 0))
  11270. (or org-log-states-order-reversed
  11271. (and (re-search-forward org-property-end-re nil t)
  11272. (goto-char (1- (match-beginning 0))))))
  11273. (insert "\n:" drawer ":\n:END:")
  11274. (beginning-of-line 0)
  11275. (org-indent-line)
  11276. (beginning-of-line 2)
  11277. (org-indent-line)
  11278. (end-of-line 0)))
  11279. ((and org-log-state-notes-insert-after-drawers
  11280. (save-excursion
  11281. (forward-line) (looking-at org-drawer-regexp)))
  11282. (forward-line)
  11283. (while (looking-at org-drawer-regexp)
  11284. (goto-char (match-end 0))
  11285. (re-search-forward org-property-end-re (point-max) t)
  11286. (forward-line))
  11287. (forward-line -1)))
  11288. (unless org-log-states-order-reversed
  11289. (and (= (char-after) ?\n) (forward-char 1))
  11290. (org-skip-over-state-notes)
  11291. (skip-chars-backward " \t\n\r")))
  11292. (move-marker org-log-note-marker (point))
  11293. (setq org-log-note-purpose purpose
  11294. org-log-note-state state
  11295. org-log-note-previous-state prev-state
  11296. org-log-note-how how
  11297. org-log-note-extra extra
  11298. org-log-note-effective-time (org-current-effective-time))
  11299. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  11300. (defun org-skip-over-state-notes ()
  11301. "Skip past the list of State notes in an entry."
  11302. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  11303. (when (ignore-errors (goto-char (org-in-item-p)))
  11304. (let* ((struct (org-list-struct))
  11305. (prevs (org-list-prevs-alist struct)))
  11306. (while (looking-at "[ \t]*- State")
  11307. (goto-char (or (org-list-get-next-item (point) struct prevs)
  11308. (org-list-get-item-end (point) struct)))))))
  11309. (defun org-add-log-note (&optional purpose)
  11310. "Pop up a window for taking a note, and add this note later at point."
  11311. (remove-hook 'post-command-hook 'org-add-log-note)
  11312. (setq org-log-note-window-configuration (current-window-configuration))
  11313. (delete-other-windows)
  11314. (move-marker org-log-note-return-to (point))
  11315. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  11316. (goto-char org-log-note-marker)
  11317. (org-switch-to-buffer-other-window "*Org Note*")
  11318. (erase-buffer)
  11319. (if (memq org-log-note-how '(time state))
  11320. (let (current-prefix-arg) (org-store-log-note))
  11321. (let ((org-inhibit-startup t)) (org-mode))
  11322. (insert (format "# Insert note for %s.
  11323. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  11324. (cond
  11325. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  11326. ((eq org-log-note-purpose 'done) "closed todo item")
  11327. ((eq org-log-note-purpose 'state)
  11328. (format "state change from \"%s\" to \"%s\""
  11329. (or org-log-note-previous-state "")
  11330. (or org-log-note-state "")))
  11331. ((eq org-log-note-purpose 'reschedule)
  11332. "rescheduling")
  11333. ((eq org-log-note-purpose 'delschedule)
  11334. "no longer scheduled")
  11335. ((eq org-log-note-purpose 'redeadline)
  11336. "changing deadline")
  11337. ((eq org-log-note-purpose 'deldeadline)
  11338. "removing deadline")
  11339. ((eq org-log-note-purpose 'refile)
  11340. "refiling")
  11341. ((eq org-log-note-purpose 'note)
  11342. "this entry")
  11343. (t (error "This should not happen")))))
  11344. (if org-log-note-extra (insert org-log-note-extra))
  11345. (org-set-local 'org-finish-function 'org-store-log-note)
  11346. (run-hooks 'org-log-buffer-setup-hook)))
  11347. (defvar org-note-abort nil) ; dynamically scoped
  11348. (defun org-store-log-note ()
  11349. "Finish taking a log note, and insert it to where it belongs."
  11350. (let ((txt (buffer-string))
  11351. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  11352. lines ind bul)
  11353. (kill-buffer (current-buffer))
  11354. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  11355. (setq txt (replace-match "" t t txt)))
  11356. (if (string-match "\\s-+\\'" txt)
  11357. (setq txt (replace-match "" t t txt)))
  11358. (setq lines (org-split-string txt "\n"))
  11359. (when (and note (string-match "\\S-" note))
  11360. (setq note
  11361. (org-replace-escapes
  11362. note
  11363. (list (cons "%u" (user-login-name))
  11364. (cons "%U" user-full-name)
  11365. (cons "%t" (format-time-string
  11366. (org-time-stamp-format 'long 'inactive)
  11367. org-log-note-effective-time))
  11368. (cons "%T" (format-time-string
  11369. (org-time-stamp-format 'long nil)
  11370. org-log-note-effective-time))
  11371. (cons "%d" (format-time-string
  11372. (org-time-stamp-format nil 'inactive)
  11373. org-log-note-effective-time))
  11374. (cons "%D" (format-time-string
  11375. (org-time-stamp-format nil nil)
  11376. org-log-note-effective-time))
  11377. (cons "%s" (if org-log-note-state
  11378. (concat "\"" org-log-note-state "\"")
  11379. ""))
  11380. (cons "%S" (if org-log-note-previous-state
  11381. (concat "\"" org-log-note-previous-state "\"")
  11382. "\"\"")))))
  11383. (if lines (setq note (concat note " \\\\")))
  11384. (push note lines))
  11385. (when (or current-prefix-arg org-note-abort)
  11386. (when org-log-into-drawer
  11387. (org-remove-empty-drawer-at
  11388. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  11389. org-log-note-marker))
  11390. (setq lines nil))
  11391. (when lines
  11392. (with-current-buffer (marker-buffer org-log-note-marker)
  11393. (save-excursion
  11394. (goto-char org-log-note-marker)
  11395. (move-marker org-log-note-marker nil)
  11396. (end-of-line 1)
  11397. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  11398. (setq ind (save-excursion
  11399. (if (ignore-errors (goto-char (org-in-item-p)))
  11400. (let ((struct (org-list-struct)))
  11401. (org-list-get-ind
  11402. (org-list-get-top-point struct) struct))
  11403. (skip-chars-backward " \r\t\n")
  11404. (cond
  11405. ((and (org-at-heading-p)
  11406. org-adapt-indentation)
  11407. (1+ (org-current-level)))
  11408. ((org-at-heading-p) 0)
  11409. (t (org-get-indentation))))))
  11410. (setq bul (org-list-bullet-string "-"))
  11411. (org-indent-line-to ind)
  11412. (insert bul (pop lines))
  11413. (let ((ind-body (+ (length bul) ind)))
  11414. (while lines
  11415. (insert "\n")
  11416. (org-indent-line-to ind-body)
  11417. (insert (pop lines))))
  11418. (message "Note stored")
  11419. (org-back-to-heading t)
  11420. (org-cycle-hide-drawers 'children)))))
  11421. (set-window-configuration org-log-note-window-configuration)
  11422. (with-current-buffer (marker-buffer org-log-note-return-to)
  11423. (goto-char org-log-note-return-to))
  11424. (move-marker org-log-note-return-to nil)
  11425. (and org-log-post-message (message "%s" org-log-post-message)))
  11426. (defun org-remove-empty-drawer-at (drawer pos)
  11427. "Remove an empty drawer DRAWER at position POS.
  11428. POS may also be a marker."
  11429. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  11430. (save-excursion
  11431. (save-restriction
  11432. (widen)
  11433. (goto-char pos)
  11434. (if (org-in-regexp
  11435. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  11436. (replace-match ""))))))
  11437. (defun org-sparse-tree (&optional arg)
  11438. "Create a sparse tree, prompt for the details.
  11439. This command can create sparse trees. You first need to select the type
  11440. of match used to create the tree:
  11441. t Show all TODO entries.
  11442. T Show entries with a specific TODO keyword.
  11443. m Show entries selected by a tags/property match.
  11444. p Enter a property name and its value (both with completion on existing
  11445. names/values) and show entries with that property.
  11446. r Show entries matching a regular expression (`/' can be used as well)
  11447. d Show deadlines due within `org-deadline-warning-days'.
  11448. b Show deadlines and scheduled items before a date.
  11449. a Show deadlines and scheduled items after a date."
  11450. (interactive "P")
  11451. (let (ans kwd value)
  11452. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date [D]ates range")
  11453. (setq ans (read-char-exclusive))
  11454. (cond
  11455. ((equal ans ?d)
  11456. (call-interactively 'org-check-deadlines))
  11457. ((equal ans ?b)
  11458. (call-interactively 'org-check-before-date))
  11459. ((equal ans ?a)
  11460. (call-interactively 'org-check-after-date))
  11461. ((equal ans ?D)
  11462. (call-interactively 'org-check-dates-range))
  11463. ((equal ans ?t)
  11464. (org-show-todo-tree nil))
  11465. ((equal ans ?T)
  11466. (org-show-todo-tree '(4)))
  11467. ((member ans '(?T ?m))
  11468. (call-interactively 'org-match-sparse-tree))
  11469. ((member ans '(?p ?P))
  11470. (setq kwd (org-icompleting-read "Property: "
  11471. (mapcar 'list (org-buffer-property-keys))))
  11472. (setq value (org-icompleting-read "Value: "
  11473. (mapcar 'list (org-property-values kwd))))
  11474. (unless (string-match "\\`{.*}\\'" value)
  11475. (setq value (concat "\"" value "\"")))
  11476. (org-match-sparse-tree arg (concat kwd "=" value)))
  11477. ((member ans '(?r ?R ?/))
  11478. (call-interactively 'org-occur))
  11479. (t (error "No such sparse tree command \"%c\"" ans)))))
  11480. (defvar org-occur-highlights nil
  11481. "List of overlays used for occur matches.")
  11482. (make-variable-buffer-local 'org-occur-highlights)
  11483. (defvar org-occur-parameters nil
  11484. "Parameters of the active org-occur calls.
  11485. This is a list, each call to org-occur pushes as cons cell,
  11486. containing the regular expression and the callback, onto the list.
  11487. The list can contain several entries if `org-occur' has been called
  11488. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  11489. will only contain one set of parameters. When the highlights are
  11490. removed (for example with `C-c C-c', or with the next edit (depending
  11491. on `org-remove-highlights-with-change'), this variable is emptied
  11492. as well.")
  11493. (make-variable-buffer-local 'org-occur-parameters)
  11494. (defun org-occur (regexp &optional keep-previous callback)
  11495. "Make a compact tree which shows all matches of REGEXP.
  11496. The tree will show the lines where the regexp matches, and all higher
  11497. headlines above the match. It will also show the heading after the match,
  11498. to make sure editing the matching entry is easy.
  11499. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11500. call to `org-occur' will be kept, to allow stacking of calls to this
  11501. command.
  11502. If CALLBACK is non-nil, it is a function which is called to confirm
  11503. that the match should indeed be shown."
  11504. (interactive "sRegexp: \nP")
  11505. (when (equal regexp "")
  11506. (error "Regexp cannot be empty"))
  11507. (unless keep-previous
  11508. (org-remove-occur-highlights nil nil t))
  11509. (push (cons regexp callback) org-occur-parameters)
  11510. (let ((cnt 0))
  11511. (save-excursion
  11512. (goto-char (point-min))
  11513. (if (or (not keep-previous) ; do not want to keep
  11514. (not org-occur-highlights)) ; no previous matches
  11515. ;; hide everything
  11516. (org-overview))
  11517. (while (re-search-forward regexp nil t)
  11518. (when (or (not callback)
  11519. (save-match-data (funcall callback)))
  11520. (setq cnt (1+ cnt))
  11521. (when org-highlight-sparse-tree-matches
  11522. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11523. (org-show-context 'occur-tree))))
  11524. (when org-remove-highlights-with-change
  11525. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11526. nil 'local))
  11527. (unless org-sparse-tree-open-archived-trees
  11528. (org-hide-archived-subtrees (point-min) (point-max)))
  11529. (run-hooks 'org-occur-hook)
  11530. (if (org-called-interactively-p 'interactive)
  11531. (message "%d match(es) for regexp %s" cnt regexp))
  11532. cnt))
  11533. (defun org-occur-next-match (&optional n reset)
  11534. "Function for `next-error-function' to find sparse tree matches.
  11535. N is the number of matches to move, when negative move backwards.
  11536. RESET is entirely ignored - this function always goes back to the
  11537. starting point when no match is found."
  11538. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11539. (search-func (if (< n 0)
  11540. 'previous-single-char-property-change
  11541. 'next-single-char-property-change))
  11542. (n (abs n))
  11543. (pos (point))
  11544. p1)
  11545. (catch 'exit
  11546. (while (setq p1 (funcall search-func (point) 'org-type))
  11547. (when (equal p1 limit)
  11548. (goto-char pos)
  11549. (error "No more matches"))
  11550. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11551. (setq n (1- n))
  11552. (when (= n 0)
  11553. (goto-char p1)
  11554. (throw 'exit (point))))
  11555. (goto-char p1))
  11556. (goto-char p1)
  11557. (error "No more matches"))))
  11558. (defun org-show-context (&optional key)
  11559. "Make sure point and context are visible.
  11560. How much context is shown depends upon the variables
  11561. `org-show-hierarchy-above', `org-show-following-heading',
  11562. `org-show-entry-below' and `org-show-siblings'."
  11563. (let ((heading-p (org-at-heading-p t))
  11564. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11565. (following-p (org-get-alist-option org-show-following-heading key))
  11566. (entry-p (org-get-alist-option org-show-entry-below key))
  11567. (siblings-p (org-get-alist-option org-show-siblings key)))
  11568. (catch 'exit
  11569. ;; Show heading or entry text
  11570. (if (and heading-p (not entry-p))
  11571. (org-flag-heading nil) ; only show the heading
  11572. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11573. (org-show-hidden-entry))) ; show entire entry
  11574. (when following-p
  11575. ;; Show next sibling, or heading below text
  11576. (save-excursion
  11577. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11578. (org-flag-heading nil))))
  11579. (when siblings-p (org-show-siblings))
  11580. (when hierarchy-p
  11581. ;; show all higher headings, possibly with siblings
  11582. (save-excursion
  11583. (while (and (condition-case nil
  11584. (progn (org-up-heading-all 1) t)
  11585. (error nil))
  11586. (not (bobp)))
  11587. (org-flag-heading nil)
  11588. (when siblings-p (org-show-siblings))))))))
  11589. (defvar org-reveal-start-hook nil
  11590. "Hook run before revealing a location.")
  11591. (defun org-reveal (&optional siblings)
  11592. "Show current entry, hierarchy above it, and the following headline.
  11593. This can be used to show a consistent set of context around locations
  11594. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11595. not t for the search context.
  11596. With optional argument SIBLINGS, on each level of the hierarchy all
  11597. siblings are shown. This repairs the tree structure to what it would
  11598. look like when opened with hierarchical calls to `org-cycle'.
  11599. With double optional argument \\[universal-argument] \\[universal-argument], \
  11600. go to the parent and show the
  11601. entire tree."
  11602. (interactive "P")
  11603. (run-hooks 'org-reveal-start-hook)
  11604. (let ((org-show-hierarchy-above t)
  11605. (org-show-following-heading t)
  11606. (org-show-siblings (if siblings t org-show-siblings)))
  11607. (org-show-context nil))
  11608. (when (equal siblings '(16))
  11609. (save-excursion
  11610. (when (org-up-heading-safe)
  11611. (org-show-subtree)
  11612. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11613. (defun org-highlight-new-match (beg end)
  11614. "Highlight from BEG to END and mark the highlight is an occur headline."
  11615. (let ((ov (make-overlay beg end)))
  11616. (overlay-put ov 'face 'secondary-selection)
  11617. (overlay-put ov 'org-type 'org-occur)
  11618. (push ov org-occur-highlights)))
  11619. (defun org-remove-occur-highlights (&optional beg end noremove)
  11620. "Remove the occur highlights from the buffer.
  11621. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11622. from the `before-change-functions' in the current buffer."
  11623. (interactive)
  11624. (unless org-inhibit-highlight-removal
  11625. (mapc 'delete-overlay org-occur-highlights)
  11626. (setq org-occur-highlights nil)
  11627. (setq org-occur-parameters nil)
  11628. (unless noremove
  11629. (remove-hook 'before-change-functions
  11630. 'org-remove-occur-highlights 'local))))
  11631. ;;;; Priorities
  11632. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11633. "Regular expression matching the priority indicator.")
  11634. (defvar org-remove-priority-next-time nil)
  11635. (defun org-priority-up ()
  11636. "Increase the priority of the current item."
  11637. (interactive)
  11638. (org-priority 'up))
  11639. (defun org-priority-down ()
  11640. "Decrease the priority of the current item."
  11641. (interactive)
  11642. (org-priority 'down))
  11643. (defun org-priority (&optional action)
  11644. "Change the priority of an item by ARG.
  11645. ACTION can be `set', `up', `down', or a character."
  11646. (interactive)
  11647. (unless org-enable-priority-commands
  11648. (error "Priority commands are disabled"))
  11649. (setq action (or action 'set))
  11650. (let (current new news have remove)
  11651. (save-excursion
  11652. (org-back-to-heading t)
  11653. (if (looking-at org-priority-regexp)
  11654. (setq current (string-to-char (match-string 2))
  11655. have t))
  11656. (cond
  11657. ((eq action 'remove)
  11658. (setq remove t new ?\ ))
  11659. ((or (eq action 'set)
  11660. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11661. (if (not (eq action 'set))
  11662. (setq new action)
  11663. (message "Priority %c-%c, SPC to remove: "
  11664. org-highest-priority org-lowest-priority)
  11665. (save-match-data
  11666. (setq new (read-char-exclusive))))
  11667. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11668. (= (upcase org-lowest-priority) org-lowest-priority))
  11669. (setq new (upcase new)))
  11670. (cond ((equal new ?\ ) (setq remove t))
  11671. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11672. (error "Priority must be between `%c' and `%c'"
  11673. org-highest-priority org-lowest-priority))))
  11674. ((eq action 'up)
  11675. (setq new (if have
  11676. (1- current) ; normal cycling
  11677. ;; last priority was empty
  11678. (if (eq last-command this-command)
  11679. org-lowest-priority ; wrap around empty to lowest
  11680. ;; default
  11681. (if org-priority-start-cycle-with-default
  11682. org-default-priority
  11683. (1- org-default-priority))))))
  11684. ((eq action 'down)
  11685. (setq new (if have
  11686. (1+ current) ; normal cycling
  11687. ;; last priority was empty
  11688. (if (eq last-command this-command)
  11689. org-highest-priority ; wrap around empty to highest
  11690. ;; default
  11691. (if org-priority-start-cycle-with-default
  11692. org-default-priority
  11693. (1+ org-default-priority))))))
  11694. (t (error "Invalid action")))
  11695. (if (or (< (upcase new) org-highest-priority)
  11696. (> (upcase new) org-lowest-priority))
  11697. (if (and (memq action '(up down))
  11698. (not have) (not (eq last-command this-command)))
  11699. ;; `new' is from default priority
  11700. (error
  11701. "The default can not be set, see `org-default-priority' why")
  11702. ;; normal cycling: `new' is beyond highest/lowest priority
  11703. ;; and is wrapped around to the empty priority
  11704. (setq remove t)))
  11705. (setq news (format "%c" new))
  11706. (if have
  11707. (if remove
  11708. (replace-match "" t t nil 1)
  11709. (replace-match news t t nil 2))
  11710. (if remove
  11711. (error "No priority cookie found in line")
  11712. (let ((case-fold-search nil))
  11713. (looking-at org-todo-line-regexp))
  11714. (if (match-end 2)
  11715. (progn
  11716. (goto-char (match-end 2))
  11717. (insert " [#" news "]"))
  11718. (goto-char (match-beginning 3))
  11719. (insert "[#" news "] "))))
  11720. (org-preserve-lc (org-set-tags nil 'align)))
  11721. (if remove
  11722. (message "Priority removed")
  11723. (message "Priority of current item set to %s" news))))
  11724. (defun org-get-priority (s)
  11725. "Find priority cookie and return priority."
  11726. (if (functionp org-get-priority-function)
  11727. (funcall org-get-priority-function)
  11728. (save-match-data
  11729. (if (not (string-match org-priority-regexp s))
  11730. (* 1000 (- org-lowest-priority org-default-priority))
  11731. (* 1000 (- org-lowest-priority
  11732. (string-to-char (match-string 2 s))))))))
  11733. ;;;; Tags
  11734. (defvar org-agenda-archives-mode)
  11735. (defvar org-map-continue-from nil
  11736. "Position from where mapping should continue.
  11737. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  11738. (defvar org-scanner-tags nil
  11739. "The current tag list while the tags scanner is running.")
  11740. (defvar org-trust-scanner-tags nil
  11741. "Should `org-get-tags-at' use the tags for the scanner.
  11742. This is for internal dynamical scoping only.
  11743. When this is non-nil, the function `org-get-tags-at' will return the value
  11744. of `org-scanner-tags' instead of building the list by itself. This
  11745. can lead to large speed-ups when the tags scanner is used in a file with
  11746. many entries, and when the list of tags is retrieved, for example to
  11747. obtain a list of properties. Building the tags list for each entry in such
  11748. a file becomes an N^2 operation - but with this variable set, it scales
  11749. as N.")
  11750. (defun org-scan-tags (action matcher todo-only &optional start-level)
  11751. "Scan headline tags with inheritance and produce output ACTION.
  11752. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11753. or `agenda' to produce an entry list for an agenda view. It can also be
  11754. a Lisp form or a function that should be called at each matched headline, in
  11755. this case the return value is a list of all return values from these calls.
  11756. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11757. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11758. only lines with a not-done TODO keyword are included in the output.
  11759. This should be the same variable that was scoped into
  11760. and set by `org-make-tags-matcher' when it constructed MATCHER.
  11761. START-LEVEL can be a string with asterisks, reducing the scope to
  11762. headlines matching this string."
  11763. (require 'org-agenda)
  11764. (let* ((re (concat "^"
  11765. (if start-level
  11766. ;; Get the correct level to match
  11767. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  11768. org-outline-regexp)
  11769. " *\\(\\<\\("
  11770. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11771. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11772. (props (list 'face 'default
  11773. 'done-face 'org-agenda-done
  11774. 'undone-face 'default
  11775. 'mouse-face 'highlight
  11776. 'org-not-done-regexp org-not-done-regexp
  11777. 'org-todo-regexp org-todo-regexp
  11778. 'org-complex-heading-regexp org-complex-heading-regexp
  11779. 'help-echo
  11780. (format "mouse-2 or RET jump to org file %s"
  11781. (abbreviate-file-name
  11782. (or (buffer-file-name (buffer-base-buffer))
  11783. (buffer-name (buffer-base-buffer)))))))
  11784. (case-fold-search nil)
  11785. (org-map-continue-from nil)
  11786. lspos tags tags-list
  11787. (tags-alist (list (cons 0 org-file-tags)))
  11788. (llast 0) rtn rtn1 level category i txt
  11789. todo marker entry priority)
  11790. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11791. (setq action (list 'lambda nil action)))
  11792. (save-excursion
  11793. (goto-char (point-min))
  11794. (when (eq action 'sparse-tree)
  11795. (org-overview)
  11796. (org-remove-occur-highlights))
  11797. (while (re-search-forward re nil t)
  11798. (setq org-map-continue-from nil)
  11799. (catch :skip
  11800. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11801. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11802. (goto-char (setq lspos (match-beginning 0)))
  11803. (setq level (org-reduced-level (funcall outline-level))
  11804. category (org-get-category))
  11805. (setq i llast llast level)
  11806. ;; remove tag lists from same and sublevels
  11807. (while (>= i level)
  11808. (when (setq entry (assoc i tags-alist))
  11809. (setq tags-alist (delete entry tags-alist)))
  11810. (setq i (1- i)))
  11811. ;; add the next tags
  11812. (when tags
  11813. (setq tags (org-split-string tags ":")
  11814. tags-alist
  11815. (cons (cons level tags) tags-alist)))
  11816. ;; compile tags for current headline
  11817. (setq tags-list
  11818. (if org-use-tag-inheritance
  11819. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11820. tags)
  11821. org-scanner-tags tags-list)
  11822. (when org-use-tag-inheritance
  11823. (setcdr (car tags-alist)
  11824. (mapcar (lambda (x)
  11825. (setq x (copy-sequence x))
  11826. (org-add-prop-inherited x))
  11827. (cdar tags-alist))))
  11828. (when (and tags org-use-tag-inheritance
  11829. (or (not (eq t org-use-tag-inheritance))
  11830. org-tags-exclude-from-inheritance))
  11831. ;; selective inheritance, remove uninherited ones
  11832. (setcdr (car tags-alist)
  11833. (org-remove-uninherited-tags (cdar tags-alist))))
  11834. (when (and
  11835. ;; eval matcher only when the todo condition is OK
  11836. (and (or (not todo-only) (member todo org-not-done-keywords))
  11837. (let ((case-fold-search t) (org-trust-scanner-tags t))
  11838. (eval matcher)))
  11839. ;; Call the skipper, but return t if it does not skip,
  11840. ;; so that the `and' form continues evaluating
  11841. (progn
  11842. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11843. t)
  11844. ;; Check if timestamps are deselecting this entry
  11845. (or (not todo-only)
  11846. (and (member todo org-not-done-keywords)
  11847. (or (not org-agenda-tags-todo-honor-ignore-options)
  11848. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11849. ;; Extra check for the archive tag
  11850. ;; FIXME: Does the skipper already do this????
  11851. (or
  11852. (not (member org-archive-tag tags-list))
  11853. ;; we have an archive tag, should we use this anyway?
  11854. (or (not org-agenda-skip-archived-trees)
  11855. (and (eq action 'agenda) org-agenda-archives-mode))))
  11856. ;; select this headline
  11857. (cond
  11858. ((eq action 'sparse-tree)
  11859. (and org-highlight-sparse-tree-matches
  11860. (org-get-heading) (match-end 0)
  11861. (org-highlight-new-match
  11862. (match-beginning 1) (match-end 1)))
  11863. (org-show-context 'tags-tree))
  11864. ((eq action 'agenda)
  11865. (setq txt (org-agenda-format-item
  11866. ""
  11867. (concat
  11868. (if (eq org-tags-match-list-sublevels 'indented)
  11869. (make-string (1- level) ?.) "")
  11870. (org-get-heading))
  11871. category
  11872. tags-list)
  11873. priority (org-get-priority txt))
  11874. (goto-char lspos)
  11875. (setq marker (org-agenda-new-marker))
  11876. (org-add-props txt props
  11877. 'org-marker marker 'org-hd-marker marker 'org-category category
  11878. 'todo-state todo
  11879. 'priority priority 'type "tagsmatch")
  11880. (push txt rtn))
  11881. ((functionp action)
  11882. (setq org-map-continue-from nil)
  11883. (save-excursion
  11884. (setq rtn1 (funcall action))
  11885. (push rtn1 rtn)))
  11886. (t (error "Invalid action")))
  11887. ;; if we are to skip sublevels, jump to end of subtree
  11888. (unless org-tags-match-list-sublevels
  11889. (org-end-of-subtree t)
  11890. (backward-char 1))))
  11891. ;; Get the correct position from where to continue
  11892. (if org-map-continue-from
  11893. (goto-char org-map-continue-from)
  11894. (and (= (point) lspos) (end-of-line 1)))))
  11895. (when (and (eq action 'sparse-tree)
  11896. (not org-sparse-tree-open-archived-trees))
  11897. (org-hide-archived-subtrees (point-min) (point-max)))
  11898. (nreverse rtn)))
  11899. (defun org-remove-uninherited-tags (tags)
  11900. "Remove all tags that are not inherited from the list TAGS."
  11901. (cond
  11902. ((eq org-use-tag-inheritance t)
  11903. (if org-tags-exclude-from-inheritance
  11904. (org-delete-all org-tags-exclude-from-inheritance tags)
  11905. tags))
  11906. ((not org-use-tag-inheritance) nil)
  11907. ((stringp org-use-tag-inheritance)
  11908. (delq nil (mapcar
  11909. (lambda (x)
  11910. (if (and (string-match org-use-tag-inheritance x)
  11911. (not (member x org-tags-exclude-from-inheritance)))
  11912. x nil))
  11913. tags)))
  11914. ((listp org-use-tag-inheritance)
  11915. (delq nil (mapcar
  11916. (lambda (x)
  11917. (if (member x org-use-tag-inheritance) x nil))
  11918. tags)))))
  11919. (defun org-match-sparse-tree (&optional todo-only match)
  11920. "Create a sparse tree according to tags string MATCH.
  11921. MATCH can contain positive and negative selection of tags, like
  11922. \"+WORK+URGENT-WITHBOSS\".
  11923. If optional argument TODO-ONLY is non-nil, only select lines that are
  11924. also TODO lines."
  11925. (interactive "P")
  11926. (org-prepare-agenda-buffers (list (current-buffer)))
  11927. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11928. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11929. (defvar org-cached-props nil)
  11930. (defun org-cached-entry-get (pom property)
  11931. (if (or (eq t org-use-property-inheritance)
  11932. (and (stringp org-use-property-inheritance)
  11933. (string-match org-use-property-inheritance property))
  11934. (and (listp org-use-property-inheritance)
  11935. (member property org-use-property-inheritance)))
  11936. ;; Caching is not possible, check it directly
  11937. (org-entry-get pom property 'inherit)
  11938. ;; Get all properties, so that we can do complicated checks easily
  11939. (cdr (assoc property (or org-cached-props
  11940. (setq org-cached-props
  11941. (org-entry-properties pom)))))))
  11942. (defun org-global-tags-completion-table (&optional files)
  11943. "Return the list of all tags in all agenda buffer/files.
  11944. Optional FILES argument is a list of files to which can be used
  11945. instead of the agenda files."
  11946. (save-excursion
  11947. (org-uniquify
  11948. (delq nil
  11949. (apply 'append
  11950. (mapcar
  11951. (lambda (file)
  11952. (set-buffer (find-file-noselect file))
  11953. (append (org-get-buffer-tags)
  11954. (mapcar (lambda (x) (if (stringp (car-safe x))
  11955. (list (car-safe x)) nil))
  11956. org-tag-alist)))
  11957. (if (and files (car files))
  11958. files
  11959. (org-agenda-files))))))))
  11960. (defun org-make-tags-matcher (match)
  11961. "Create the TAGS/TODO matcher form for the selection string MATCH.
  11962. The variable `todo-only' is scoped dynamically into this function; it will be
  11963. set to t if the matcher restricts matching to TODO entries,
  11964. otherwise will not be touched.
  11965. Returns a cons of the selection string MATCH and the constructed
  11966. lisp form implementing the matcher. The matcher is to be
  11967. evaluated at an Org entry, with point on the headline,
  11968. and returns t if the entry matches the
  11969. selection string MATCH. The returned lisp form references
  11970. two variables with information about the entry, which must be
  11971. bound around the form's evaluation: todo, the TODO keyword at the
  11972. entry (or nil of none); and tags-list, the list of all tags at the
  11973. entry including inherited ones. Additionally, the category
  11974. of the entry (if any) must be specified as the text property
  11975. 'org-category on the headline.
  11976. See also `org-scan-tags'.
  11977. "
  11978. (declare (special todo-only))
  11979. (unless (boundp 'todo-only)
  11980. (error "org-make-tags-matcher expects todo-only to be scoped in"))
  11981. (unless match
  11982. ;; Get a new match request, with completion
  11983. (let ((org-last-tags-completion-table
  11984. (org-global-tags-completion-table)))
  11985. (setq match (org-completing-read-no-i
  11986. "Match: " 'org-tags-completion-function nil nil nil
  11987. 'org-tags-history))))
  11988. ;; Parse the string and create a lisp form
  11989. (let ((match0 match)
  11990. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11991. minus tag mm
  11992. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11993. orterms term orlist re-p str-p level-p level-op time-p
  11994. prop-p pn pv po gv rest)
  11995. (if (string-match "/+" match)
  11996. ;; match contains also a todo-matching request
  11997. (progn
  11998. (setq tagsmatch (substring match 0 (match-beginning 0))
  11999. todomatch (substring match (match-end 0)))
  12000. (if (string-match "^!" todomatch)
  12001. (setq todo-only t todomatch (substring todomatch 1)))
  12002. (if (string-match "^\\s-*$" todomatch)
  12003. (setq todomatch nil)))
  12004. ;; only matching tags
  12005. (setq tagsmatch match todomatch nil))
  12006. ;; Make the tags matcher
  12007. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  12008. (setq tagsmatcher t)
  12009. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  12010. (while (setq term (pop orterms))
  12011. (while (and (equal (substring term -1) "\\") orterms)
  12012. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  12013. (while (string-match re term)
  12014. (setq rest (substring term (match-end 0))
  12015. minus (and (match-end 1)
  12016. (equal (match-string 1 term) "-"))
  12017. tag (save-match-data (replace-regexp-in-string
  12018. "\\\\-" "-"
  12019. (match-string 2 term)))
  12020. re-p (equal (string-to-char tag) ?{)
  12021. level-p (match-end 4)
  12022. prop-p (match-end 5)
  12023. mm (cond
  12024. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  12025. (level-p
  12026. (setq level-op (org-op-to-function (match-string 3 term)))
  12027. `(,level-op level ,(string-to-number
  12028. (match-string 4 term))))
  12029. (prop-p
  12030. (setq pn (match-string 5 term)
  12031. po (match-string 6 term)
  12032. pv (match-string 7 term)
  12033. re-p (equal (string-to-char pv) ?{)
  12034. str-p (equal (string-to-char pv) ?\")
  12035. time-p (save-match-data
  12036. (string-match "^\"[[<].*[]>]\"$" pv))
  12037. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  12038. (if time-p (setq pv (org-matcher-time pv)))
  12039. (setq po (org-op-to-function po (if time-p 'time str-p)))
  12040. (cond
  12041. ((equal pn "CATEGORY")
  12042. (setq gv '(get-text-property (point) 'org-category)))
  12043. ((equal pn "TODO")
  12044. (setq gv 'todo))
  12045. (t
  12046. (setq gv `(org-cached-entry-get nil ,pn))))
  12047. (if re-p
  12048. (if (eq po 'org<>)
  12049. `(not (string-match ,pv (or ,gv "")))
  12050. `(string-match ,pv (or ,gv "")))
  12051. (if str-p
  12052. `(,po (or ,gv "") ,pv)
  12053. `(,po (string-to-number (or ,gv ""))
  12054. ,(string-to-number pv) ))))
  12055. (t `(member ,tag tags-list)))
  12056. mm (if minus (list 'not mm) mm)
  12057. term rest)
  12058. (push mm tagsmatcher))
  12059. (push (if (> (length tagsmatcher) 1)
  12060. (cons 'and tagsmatcher)
  12061. (car tagsmatcher))
  12062. orlist)
  12063. (setq tagsmatcher nil))
  12064. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  12065. (setq tagsmatcher
  12066. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  12067. ;; Make the todo matcher
  12068. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  12069. (setq todomatcher t)
  12070. (setq orterms (org-split-string todomatch "|") orlist nil)
  12071. (while (setq term (pop orterms))
  12072. (while (string-match re term)
  12073. (setq minus (and (match-end 1)
  12074. (equal (match-string 1 term) "-"))
  12075. kwd (match-string 2 term)
  12076. re-p (equal (string-to-char kwd) ?{)
  12077. term (substring term (match-end 0))
  12078. mm (if re-p
  12079. `(string-match ,(substring kwd 1 -1) todo)
  12080. (list 'equal 'todo kwd))
  12081. mm (if minus (list 'not mm) mm))
  12082. (push mm todomatcher))
  12083. (push (if (> (length todomatcher) 1)
  12084. (cons 'and todomatcher)
  12085. (car todomatcher))
  12086. orlist)
  12087. (setq todomatcher nil))
  12088. (setq todomatcher (if (> (length orlist) 1)
  12089. (cons 'or orlist) (car orlist))))
  12090. ;; Return the string and lisp forms of the matcher
  12091. (setq matcher (if todomatcher
  12092. (list 'and tagsmatcher todomatcher)
  12093. tagsmatcher))
  12094. (when todo-only
  12095. (setq matcher (list 'and '(member todo org-not-done-keywords)
  12096. matcher)))
  12097. (cons match0 matcher)))
  12098. (defun org-op-to-function (op &optional stringp)
  12099. "Turn an operator into the appropriate function."
  12100. (setq op
  12101. (cond
  12102. ((equal op "<" ) '(< string< org-time<))
  12103. ((equal op ">" ) '(> org-string> org-time>))
  12104. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  12105. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  12106. ((member op '("=" "==")) '(= string= org-time=))
  12107. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  12108. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  12109. (defun org<> (a b) (not (= a b)))
  12110. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  12111. (defun org-string>= (a b) (not (string< a b)))
  12112. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  12113. (defun org-string<> (a b) (not (string= a b)))
  12114. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  12115. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  12116. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  12117. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  12118. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  12119. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  12120. (defun org-2ft (s)
  12121. "Convert S to a floating point time.
  12122. If S is already a number, just return it. If it is a string, parse
  12123. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  12124. (cond
  12125. ((numberp s) s)
  12126. ((stringp s)
  12127. (condition-case nil
  12128. (float-time (apply 'encode-time (org-parse-time-string s)))
  12129. (error 0.)))
  12130. (t 0.)))
  12131. (defun org-time-today ()
  12132. "Time in seconds today at 0:00.
  12133. Returns the float number of seconds since the beginning of the
  12134. epoch to the beginning of today (00:00)."
  12135. (float-time (apply 'encode-time
  12136. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  12137. (defun org-matcher-time (s)
  12138. "Interpret a time comparison value."
  12139. (save-match-data
  12140. (cond
  12141. ((string= s "<now>") (float-time))
  12142. ((string= s "<today>") (org-time-today))
  12143. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  12144. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  12145. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  12146. (+ (org-time-today)
  12147. (* (string-to-number (match-string 1 s))
  12148. (cdr (assoc (match-string 2 s)
  12149. '(("d" . 86400.0) ("w" . 604800.0)
  12150. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  12151. (t (org-2ft s)))))
  12152. (defun org-match-any-p (re list)
  12153. "Does re match any element of list?"
  12154. (setq list (mapcar (lambda (x) (string-match re x)) list))
  12155. (delq nil list))
  12156. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  12157. (defvar org-tags-overlay (make-overlay 1 1))
  12158. (org-detach-overlay org-tags-overlay)
  12159. (defun org-get-local-tags-at (&optional pos)
  12160. "Get a list of tags defined in the current headline."
  12161. (org-get-tags-at pos 'local))
  12162. (defun org-get-local-tags ()
  12163. "Get a list of tags defined in the current headline."
  12164. (org-get-tags-at nil 'local))
  12165. (defun org-get-tags-at (&optional pos local)
  12166. "Get a list of all headline tags applicable at POS.
  12167. POS defaults to point. If tags are inherited, the list contains
  12168. the targets in the same sequence as the headlines appear, i.e.
  12169. the tags of the current headline come last.
  12170. When LOCAL is non-nil, only return tags from the current headline,
  12171. ignore inherited ones."
  12172. (interactive)
  12173. (if (and org-trust-scanner-tags
  12174. (or (not pos) (equal pos (point)))
  12175. (not local))
  12176. org-scanner-tags
  12177. (let (tags ltags lastpos parent)
  12178. (save-excursion
  12179. (save-restriction
  12180. (widen)
  12181. (goto-char (or pos (point)))
  12182. (save-match-data
  12183. (catch 'done
  12184. (condition-case nil
  12185. (progn
  12186. (org-back-to-heading t)
  12187. (while (not (equal lastpos (point)))
  12188. (setq lastpos (point))
  12189. (when (looking-at
  12190. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  12191. (setq ltags (org-split-string
  12192. (org-match-string-no-properties 1) ":"))
  12193. (when parent
  12194. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  12195. (setq tags (append
  12196. (if parent
  12197. (org-remove-uninherited-tags ltags)
  12198. ltags)
  12199. tags)))
  12200. (or org-use-tag-inheritance (throw 'done t))
  12201. (if local (throw 'done t))
  12202. (or (org-up-heading-safe) (error nil))
  12203. (setq parent t)))
  12204. (error nil)))))
  12205. (if local
  12206. tags
  12207. (append (org-remove-uninherited-tags org-file-tags) tags))))))
  12208. (defun org-add-prop-inherited (s)
  12209. (add-text-properties 0 (length s) '(inherited t) s)
  12210. s)
  12211. (defun org-toggle-tag (tag &optional onoff)
  12212. "Toggle the tag TAG for the current line.
  12213. If ONOFF is `on' or `off', don't toggle but set to this state."
  12214. (let (res current)
  12215. (save-excursion
  12216. (org-back-to-heading t)
  12217. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  12218. (point-at-eol) t)
  12219. (progn
  12220. (setq current (match-string 1))
  12221. (replace-match ""))
  12222. (setq current ""))
  12223. (setq current (nreverse (org-split-string current ":")))
  12224. (cond
  12225. ((eq onoff 'on)
  12226. (setq res t)
  12227. (or (member tag current) (push tag current)))
  12228. ((eq onoff 'off)
  12229. (or (not (member tag current)) (setq current (delete tag current))))
  12230. (t (if (member tag current)
  12231. (setq current (delete tag current))
  12232. (setq res t)
  12233. (push tag current))))
  12234. (end-of-line 1)
  12235. (if current
  12236. (progn
  12237. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  12238. (org-set-tags nil t))
  12239. (delete-horizontal-space))
  12240. (run-hooks 'org-after-tags-change-hook))
  12241. res))
  12242. (defun org-align-tags-here (to-col)
  12243. ;; Assumes that this is a headline
  12244. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  12245. (beginning-of-line 1)
  12246. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12247. (< pos (match-beginning 2)))
  12248. (progn
  12249. (setq tags-l (- (match-end 2) (match-beginning 2)))
  12250. (goto-char (match-beginning 1))
  12251. (insert " ")
  12252. (delete-region (point) (1+ (match-beginning 2)))
  12253. (setq ncol (max (current-column)
  12254. (1+ col)
  12255. (if (> to-col 0)
  12256. to-col
  12257. (- (abs to-col) tags-l))))
  12258. (setq p (point))
  12259. (insert (make-string (- ncol (current-column)) ?\ ))
  12260. (setq ncol (current-column))
  12261. (when indent-tabs-mode (tabify p (point-at-eol)))
  12262. (org-move-to-column (min ncol col) t))
  12263. (goto-char pos))))
  12264. (defun org-set-tags-command (&optional arg just-align)
  12265. "Call the set-tags command for the current entry."
  12266. (interactive "P")
  12267. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  12268. (org-set-tags arg just-align)
  12269. (save-excursion
  12270. (org-back-to-heading t)
  12271. (org-set-tags arg just-align))))
  12272. (defun org-set-tags-to (data)
  12273. "Set the tags of the current entry to DATA, replacing the current tags.
  12274. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  12275. If DATA is nil or the empty string, any tags will be removed."
  12276. (interactive "sTags: ")
  12277. (setq data
  12278. (cond
  12279. ((eq data nil) "")
  12280. ((equal data "") "")
  12281. ((stringp data)
  12282. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  12283. ":"))
  12284. ((listp data)
  12285. (concat ":" (mapconcat 'identity data ":") ":"))
  12286. (t nil)))
  12287. (when data
  12288. (save-excursion
  12289. (org-back-to-heading t)
  12290. (when (looking-at org-complex-heading-regexp)
  12291. (if (match-end 5)
  12292. (progn
  12293. (goto-char (match-beginning 5))
  12294. (insert data)
  12295. (delete-region (point) (point-at-eol))
  12296. (org-set-tags nil 'align))
  12297. (goto-char (point-at-eol))
  12298. (insert " " data)
  12299. (org-set-tags nil 'align)))
  12300. (beginning-of-line 1)
  12301. (if (looking-at ".*?\\([ \t]+\\)$")
  12302. (delete-region (match-beginning 1) (match-end 1))))))
  12303. (defun org-align-all-tags ()
  12304. "Align the tags i all headings."
  12305. (interactive)
  12306. (save-excursion
  12307. (or (ignore-errors (org-back-to-heading t))
  12308. (outline-next-heading))
  12309. (if (org-at-heading-p)
  12310. (org-set-tags t)
  12311. (message "No headings"))))
  12312. (defvar org-indent-indentation-per-level)
  12313. (defun org-set-tags (&optional arg just-align)
  12314. "Set the tags for the current headline.
  12315. With prefix ARG, realign all tags in headings in the current buffer."
  12316. (interactive "P")
  12317. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12318. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12319. 'region-start-level 'region))
  12320. org-loop-over-headlines-in-active-region)
  12321. (org-map-entries
  12322. ;; We don't use ARG and JUST-ALIGN here these args are not
  12323. ;; useful when looping over headlines
  12324. `(org-set-tags)
  12325. org-loop-over-headlines-in-active-region
  12326. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12327. (let* ((re org-outline-regexp-bol)
  12328. (current (unless arg (org-get-tags-string)))
  12329. (col (current-column))
  12330. (org-setting-tags t)
  12331. table current-tags inherited-tags ; computed below when needed
  12332. tags p0 c0 c1 rpl di tc level)
  12333. (if arg
  12334. (save-excursion
  12335. (goto-char (point-min))
  12336. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  12337. (while (re-search-forward re nil t)
  12338. (org-set-tags nil t)
  12339. (end-of-line 1)))
  12340. (message "All tags realigned to column %d" org-tags-column))
  12341. (if just-align
  12342. (setq tags current)
  12343. ;; Get a new set of tags from the user
  12344. (save-excursion
  12345. (setq table (append org-tag-persistent-alist
  12346. (or org-tag-alist (org-get-buffer-tags))
  12347. (and
  12348. org-complete-tags-always-offer-all-agenda-tags
  12349. (org-global-tags-completion-table
  12350. (org-agenda-files))))
  12351. org-last-tags-completion-table table
  12352. current-tags (org-split-string current ":")
  12353. inherited-tags (nreverse
  12354. (nthcdr (length current-tags)
  12355. (nreverse (org-get-tags-at))))
  12356. tags
  12357. (if (or (eq t org-use-fast-tag-selection)
  12358. (and org-use-fast-tag-selection
  12359. (delq nil (mapcar 'cdr table))))
  12360. (org-fast-tag-selection
  12361. current-tags inherited-tags table
  12362. (if org-fast-tag-selection-include-todo
  12363. org-todo-key-alist))
  12364. (let ((org-add-colon-after-tag-completion (< 1 (length table))))
  12365. (org-trim
  12366. (org-icompleting-read "Tags: "
  12367. 'org-tags-completion-function
  12368. nil nil current 'org-tags-history))))))
  12369. (while (string-match "[-+&]+" tags)
  12370. ;; No boolean logic, just a list
  12371. (setq tags (replace-match ":" t t tags))))
  12372. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  12373. (if org-tags-sort-function
  12374. (setq tags (mapconcat 'identity
  12375. (sort (org-split-string
  12376. tags (org-re "[^[:alnum:]_@#%]+"))
  12377. org-tags-sort-function) ":")))
  12378. (if (string-match "\\`[\t ]*\\'" tags)
  12379. (setq tags "")
  12380. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  12381. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  12382. ;; Insert new tags at the correct column
  12383. (beginning-of-line 1)
  12384. (setq level (or (and (looking-at org-outline-regexp)
  12385. (- (match-end 0) (point) 1))
  12386. 1))
  12387. (cond
  12388. ((and (equal current "") (equal tags "")))
  12389. ((re-search-forward
  12390. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  12391. (point-at-eol) t)
  12392. (if (equal tags "")
  12393. (setq rpl "")
  12394. (goto-char (match-beginning 0))
  12395. (setq c0 (current-column)
  12396. ;; compute offset for the case of org-indent-mode active
  12397. di (if org-indent-mode
  12398. (* (1- org-indent-indentation-per-level) (1- level))
  12399. 0)
  12400. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  12401. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  12402. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  12403. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  12404. (replace-match rpl t t)
  12405. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  12406. tags)
  12407. (t (error "Tags alignment failed")))
  12408. (org-move-to-column col)
  12409. (unless just-align
  12410. (run-hooks 'org-after-tags-change-hook))))))
  12411. (defun org-change-tag-in-region (beg end tag off)
  12412. "Add or remove TAG for each entry in the region.
  12413. This works in the agenda, and also in an org-mode buffer."
  12414. (interactive
  12415. (list (region-beginning) (region-end)
  12416. (let ((org-last-tags-completion-table
  12417. (if (derived-mode-p 'org-mode)
  12418. (org-get-buffer-tags)
  12419. (org-global-tags-completion-table))))
  12420. (org-icompleting-read
  12421. "Tag: " 'org-tags-completion-function nil nil nil
  12422. 'org-tags-history))
  12423. (progn
  12424. (message "[s]et or [r]emove? ")
  12425. (equal (read-char-exclusive) ?r))))
  12426. (if (fboundp 'deactivate-mark) (deactivate-mark))
  12427. (let ((agendap (equal major-mode 'org-agenda-mode))
  12428. l1 l2 m buf pos newhead (cnt 0))
  12429. (goto-char end)
  12430. (setq l2 (1- (org-current-line)))
  12431. (goto-char beg)
  12432. (setq l1 (org-current-line))
  12433. (loop for l from l1 to l2 do
  12434. (org-goto-line l)
  12435. (setq m (get-text-property (point) 'org-hd-marker))
  12436. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  12437. (and agendap m))
  12438. (setq buf (if agendap (marker-buffer m) (current-buffer))
  12439. pos (if agendap m (point)))
  12440. (with-current-buffer buf
  12441. (save-excursion
  12442. (save-restriction
  12443. (goto-char pos)
  12444. (setq cnt (1+ cnt))
  12445. (org-toggle-tag tag (if off 'off 'on))
  12446. (setq newhead (org-get-heading)))))
  12447. (and agendap (org-agenda-change-all-lines newhead m))))
  12448. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  12449. (defun org-tags-completion-function (string predicate &optional flag)
  12450. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  12451. (confirm (lambda (x) (stringp (car x)))))
  12452. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  12453. (setq s1 (match-string 1 string)
  12454. s2 (match-string 2 string))
  12455. (setq s1 "" s2 string))
  12456. (cond
  12457. ((eq flag nil)
  12458. ;; try completion
  12459. (setq rtn (try-completion s2 ctable confirm))
  12460. (if (stringp rtn)
  12461. (setq rtn
  12462. (concat s1 s2 (substring rtn (length s2))
  12463. (if (and org-add-colon-after-tag-completion
  12464. (assoc rtn ctable))
  12465. ":" ""))))
  12466. rtn)
  12467. ((eq flag t)
  12468. ;; all-completions
  12469. (all-completions s2 ctable confirm)
  12470. )
  12471. ((eq flag 'lambda)
  12472. ;; exact match?
  12473. (assoc s2 ctable)))
  12474. ))
  12475. (defun org-fast-tag-insert (kwd tags face &optional end)
  12476. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  12477. (insert (format "%-12s" (concat kwd ":"))
  12478. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  12479. (or end "")))
  12480. (defun org-fast-tag-show-exit (flag)
  12481. (save-excursion
  12482. (org-goto-line 3)
  12483. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  12484. (replace-match ""))
  12485. (when flag
  12486. (end-of-line 1)
  12487. (org-move-to-column (- (window-width) 19) t)
  12488. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  12489. (defun org-set-current-tags-overlay (current prefix)
  12490. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  12491. (if (featurep 'xemacs)
  12492. (org-overlay-display org-tags-overlay (concat prefix s)
  12493. 'secondary-selection)
  12494. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  12495. (org-overlay-display org-tags-overlay (concat prefix s)))))
  12496. (defvar org-last-tag-selection-key nil)
  12497. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  12498. "Fast tag selection with single keys.
  12499. CURRENT is the current list of tags in the headline, INHERITED is the
  12500. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  12501. possibly with grouping information. TODO-TABLE is a similar table with
  12502. TODO keywords, should these have keys assigned to them.
  12503. If the keys are nil, a-z are automatically assigned.
  12504. Returns the new tags string, or nil to not change the current settings."
  12505. (let* ((fulltable (append table todo-table))
  12506. (maxlen (apply 'max (mapcar
  12507. (lambda (x)
  12508. (if (stringp (car x)) (string-width (car x)) 0))
  12509. fulltable)))
  12510. (buf (current-buffer))
  12511. (expert (eq org-fast-tag-selection-single-key 'expert))
  12512. (buffer-tags nil)
  12513. (fwidth (+ maxlen 3 1 3))
  12514. (ncol (/ (- (window-width) 4) fwidth))
  12515. (i-face 'org-done)
  12516. (c-face 'org-todo)
  12517. tg cnt e c char c1 c2 ntable tbl rtn
  12518. ov-start ov-end ov-prefix
  12519. (exit-after-next org-fast-tag-selection-single-key)
  12520. (done-keywords org-done-keywords)
  12521. groups ingroup)
  12522. (save-excursion
  12523. (beginning-of-line 1)
  12524. (if (looking-at
  12525. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12526. (setq ov-start (match-beginning 1)
  12527. ov-end (match-end 1)
  12528. ov-prefix "")
  12529. (setq ov-start (1- (point-at-eol))
  12530. ov-end (1+ ov-start))
  12531. (skip-chars-forward "^\n\r")
  12532. (setq ov-prefix
  12533. (concat
  12534. (buffer-substring (1- (point)) (point))
  12535. (if (> (current-column) org-tags-column)
  12536. " "
  12537. (make-string (- org-tags-column (current-column)) ?\ ))))))
  12538. (move-overlay org-tags-overlay ov-start ov-end)
  12539. (save-window-excursion
  12540. (if expert
  12541. (set-buffer (get-buffer-create " *Org tags*"))
  12542. (delete-other-windows)
  12543. (split-window-vertically)
  12544. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  12545. (erase-buffer)
  12546. (org-set-local 'org-done-keywords done-keywords)
  12547. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  12548. (org-fast-tag-insert "Current" current c-face "\n\n")
  12549. (org-fast-tag-show-exit exit-after-next)
  12550. (org-set-current-tags-overlay current ov-prefix)
  12551. (setq tbl fulltable char ?a cnt 0)
  12552. (while (setq e (pop tbl))
  12553. (cond
  12554. ((equal (car e) :startgroup)
  12555. (push '() groups) (setq ingroup t)
  12556. (when (not (= cnt 0))
  12557. (setq cnt 0)
  12558. (insert "\n"))
  12559. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  12560. ((equal (car e) :endgroup)
  12561. (setq ingroup nil cnt 0)
  12562. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12563. ((equal e '(:newline))
  12564. (when (not (= cnt 0))
  12565. (setq cnt 0)
  12566. (insert "\n")
  12567. (setq e (car tbl))
  12568. (while (equal (car tbl) '(:newline))
  12569. (insert "\n")
  12570. (setq tbl (cdr tbl)))))
  12571. (t
  12572. (setq tg (copy-sequence (car e)) c2 nil)
  12573. (if (cdr e)
  12574. (setq c (cdr e))
  12575. ;; automatically assign a character.
  12576. (setq c1 (string-to-char
  12577. (downcase (substring
  12578. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12579. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12580. (while (or (rassoc char ntable) (rassoc char table))
  12581. (setq char (1+ char)))
  12582. (setq c2 c1))
  12583. (setq c (or c2 char)))
  12584. (if ingroup (push tg (car groups)))
  12585. (setq tg (org-add-props tg nil 'face
  12586. (cond
  12587. ((not (assoc tg table))
  12588. (org-get-todo-face tg))
  12589. ((member tg current) c-face)
  12590. ((member tg inherited) i-face)
  12591. (t nil))))
  12592. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12593. (insert "[" c "] " tg (make-string
  12594. (- fwidth 4 (length tg)) ?\ ))
  12595. (push (cons tg c) ntable)
  12596. (when (= (setq cnt (1+ cnt)) ncol)
  12597. (insert "\n")
  12598. (if ingroup (insert " "))
  12599. (setq cnt 0)))))
  12600. (setq ntable (nreverse ntable))
  12601. (insert "\n")
  12602. (goto-char (point-min))
  12603. (if (not expert) (org-fit-window-to-buffer))
  12604. (setq rtn
  12605. (catch 'exit
  12606. (while t
  12607. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12608. (if (not groups) "no " "")
  12609. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12610. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12611. (setq org-last-tag-selection-key c)
  12612. (cond
  12613. ((= c ?\r) (throw 'exit t))
  12614. ((= c ?!)
  12615. (setq groups (not groups))
  12616. (goto-char (point-min))
  12617. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12618. ((= c ?\C-c)
  12619. (if (not expert)
  12620. (org-fast-tag-show-exit
  12621. (setq exit-after-next (not exit-after-next)))
  12622. (setq expert nil)
  12623. (delete-other-windows)
  12624. (set-window-buffer (split-window-vertically) " *Org tags*")
  12625. (org-switch-to-buffer-other-window " *Org tags*")
  12626. (org-fit-window-to-buffer)))
  12627. ((or (= c ?\C-g)
  12628. (and (= c ?q) (not (rassoc c ntable))))
  12629. (org-detach-overlay org-tags-overlay)
  12630. (setq quit-flag t))
  12631. ((= c ?\ )
  12632. (setq current nil)
  12633. (if exit-after-next (setq exit-after-next 'now)))
  12634. ((= c ?\t)
  12635. (condition-case nil
  12636. (setq tg (org-icompleting-read
  12637. "Tag: "
  12638. (or buffer-tags
  12639. (with-current-buffer buf
  12640. (org-get-buffer-tags)))))
  12641. (quit (setq tg "")))
  12642. (when (string-match "\\S-" tg)
  12643. (add-to-list 'buffer-tags (list tg))
  12644. (if (member tg current)
  12645. (setq current (delete tg current))
  12646. (push tg current)))
  12647. (if exit-after-next (setq exit-after-next 'now)))
  12648. ((setq e (rassoc c todo-table) tg (car e))
  12649. (with-current-buffer buf
  12650. (save-excursion (org-todo tg)))
  12651. (if exit-after-next (setq exit-after-next 'now)))
  12652. ((setq e (rassoc c ntable) tg (car e))
  12653. (if (member tg current)
  12654. (setq current (delete tg current))
  12655. (loop for g in groups do
  12656. (if (member tg g)
  12657. (mapc (lambda (x)
  12658. (setq current (delete x current)))
  12659. g)))
  12660. (push tg current))
  12661. (if exit-after-next (setq exit-after-next 'now))))
  12662. ;; Create a sorted list
  12663. (setq current
  12664. (sort current
  12665. (lambda (a b)
  12666. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12667. (if (eq exit-after-next 'now) (throw 'exit t))
  12668. (goto-char (point-min))
  12669. (beginning-of-line 2)
  12670. (delete-region (point) (point-at-eol))
  12671. (org-fast-tag-insert "Current" current c-face)
  12672. (org-set-current-tags-overlay current ov-prefix)
  12673. (while (re-search-forward
  12674. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12675. (setq tg (match-string 1))
  12676. (add-text-properties
  12677. (match-beginning 1) (match-end 1)
  12678. (list 'face
  12679. (cond
  12680. ((member tg current) c-face)
  12681. ((member tg inherited) i-face)
  12682. (t (get-text-property (match-beginning 1) 'face))))))
  12683. (goto-char (point-min)))))
  12684. (org-detach-overlay org-tags-overlay)
  12685. (if rtn
  12686. (mapconcat 'identity current ":")
  12687. nil))))
  12688. (defun org-get-tags-string ()
  12689. "Get the TAGS string in the current headline."
  12690. (unless (org-at-heading-p t)
  12691. (error "Not on a heading"))
  12692. (save-excursion
  12693. (beginning-of-line 1)
  12694. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12695. (org-match-string-no-properties 1)
  12696. "")))
  12697. (defun org-get-tags ()
  12698. "Get the list of tags specified in the current headline."
  12699. (org-split-string (org-get-tags-string) ":"))
  12700. (defun org-get-buffer-tags ()
  12701. "Get a table of all tags used in the buffer, for completion."
  12702. (let (tags)
  12703. (save-excursion
  12704. (goto-char (point-min))
  12705. (while (re-search-forward
  12706. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12707. (when (equal (char-after (point-at-bol 0)) ?*)
  12708. (mapc (lambda (x) (add-to-list 'tags x))
  12709. (org-split-string (org-match-string-no-properties 1) ":")))))
  12710. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12711. (mapcar 'list tags)))
  12712. ;;;; The mapping API
  12713. ;;;###autoload
  12714. (defun org-map-entries (func &optional match scope &rest skip)
  12715. "Call FUNC at each headline selected by MATCH in SCOPE.
  12716. FUNC is a function or a lisp form. The function will be called without
  12717. arguments, with the cursor positioned at the beginning of the headline.
  12718. The return values of all calls to the function will be collected and
  12719. returned as a list.
  12720. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12721. does not need to preserve point. After evaluation, the cursor will be
  12722. moved to the end of the line (presumably of the headline of the
  12723. processed entry) and search continues from there. Under some
  12724. circumstances, this may not produce the wanted results. For example,
  12725. if you have removed (e.g. archived) the current (sub)tree it could
  12726. mean that the next entry will be skipped entirely. In such cases, you
  12727. can specify the position from where search should continue by making
  12728. FUNC set the variable `org-map-continue-from' to the desired buffer
  12729. position.
  12730. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12731. Only headlines that are matched by this query will be considered during
  12732. the iteration. When MATCH is nil or t, all headlines will be
  12733. visited by the iteration.
  12734. SCOPE determines the scope of this command. It can be any of:
  12735. nil The current buffer, respecting the restriction if any
  12736. tree The subtree started with the entry at point
  12737. region The entries within the active region, if any
  12738. region-start-level
  12739. The entries within the active region, but only those at
  12740. the same level than the first one.
  12741. file The current buffer, without restriction
  12742. file-with-archives
  12743. The current buffer, and any archives associated with it
  12744. agenda All agenda files
  12745. agenda-with-archives
  12746. All agenda files with any archive files associated with them
  12747. \(file1 file2 ...)
  12748. If this is a list, all files in the list will be scanned
  12749. The remaining args are treated as settings for the skipping facilities of
  12750. the scanner. The following items can be given here:
  12751. archive skip trees with the archive tag.
  12752. comment skip trees with the COMMENT keyword
  12753. function or Emacs Lisp form:
  12754. will be used as value for `org-agenda-skip-function', so whenever
  12755. the function returns t, FUNC will not be called for that
  12756. entry and search will continue from the point where the
  12757. function leaves it.
  12758. If your function needs to retrieve the tags including inherited tags
  12759. at the *current* entry, you can use the value of the variable
  12760. `org-scanner-tags' which will be much faster than getting the value
  12761. with `org-get-tags-at'. If your function gets properties with
  12762. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12763. to t around the call to `org-entry-properties' to get the same speedup.
  12764. Note that if your function moves around to retrieve tags and properties at
  12765. a *different* entry, you cannot use these techniques."
  12766. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  12767. (not (org-region-active-p)))
  12768. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12769. (org-agenda-skip-archived-trees (memq 'archive skip))
  12770. (org-agenda-skip-comment-trees (memq 'comment skip))
  12771. (org-agenda-skip-function
  12772. (car (org-delete-all '(comment archive) skip)))
  12773. (org-tags-match-list-sublevels t)
  12774. (start-level (eq scope 'region-start-level))
  12775. matcher file res
  12776. org-todo-keywords-for-agenda
  12777. org-done-keywords-for-agenda
  12778. org-todo-keyword-alist-for-agenda
  12779. org-drawers-for-agenda
  12780. org-tag-alist-for-agenda
  12781. todo-only)
  12782. (cond
  12783. ((eq match t) (setq matcher t))
  12784. ((eq match nil) (setq matcher t))
  12785. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12786. (save-excursion
  12787. (save-restriction
  12788. (cond ((eq scope 'tree)
  12789. (org-back-to-heading t)
  12790. (org-narrow-to-subtree)
  12791. (setq scope nil))
  12792. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  12793. (org-region-active-p))
  12794. ;; If needed, set start-level to a string like "2"
  12795. (when start-level
  12796. (save-excursion
  12797. (goto-char (region-beginning))
  12798. (unless (org-at-heading-p) (outline-next-heading))
  12799. (setq start-level (org-current-level))))
  12800. (narrow-to-region (region-beginning)
  12801. (save-excursion
  12802. (goto-char (region-end))
  12803. (unless (and (bolp) (org-at-heading-p))
  12804. (outline-next-heading))
  12805. (point)))
  12806. (setq scope nil)))
  12807. (if (not scope)
  12808. (progn
  12809. (org-prepare-agenda-buffers
  12810. (list (buffer-file-name (current-buffer))))
  12811. (setq res (org-scan-tags func matcher todo-only start-level)))
  12812. ;; Get the right scope
  12813. (cond
  12814. ((and scope (listp scope) (symbolp (car scope)))
  12815. (setq scope (eval scope)))
  12816. ((eq scope 'agenda)
  12817. (setq scope (org-agenda-files t)))
  12818. ((eq scope 'agenda-with-archives)
  12819. (setq scope (org-agenda-files t))
  12820. (setq scope (org-add-archive-files scope)))
  12821. ((eq scope 'file)
  12822. (setq scope (list (buffer-file-name))))
  12823. ((eq scope 'file-with-archives)
  12824. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12825. (org-prepare-agenda-buffers scope)
  12826. (while (setq file (pop scope))
  12827. (with-current-buffer (org-find-base-buffer-visiting file)
  12828. (save-excursion
  12829. (save-restriction
  12830. (widen)
  12831. (goto-char (point-min))
  12832. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  12833. res)))
  12834. ;;;; Properties
  12835. ;;; Setting and retrieving properties
  12836. (defconst org-special-properties
  12837. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12838. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE" "CLOCKSUM")
  12839. "The special properties valid in Org-mode.
  12840. These are properties that are not defined in the property drawer,
  12841. but in some other way.")
  12842. (defconst org-default-properties
  12843. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12844. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12845. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12846. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12847. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12848. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12849. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12850. "Some properties that are used by Org-mode for various purposes.
  12851. Being in this list makes sure that they are offered for completion.")
  12852. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12853. "Regular expression matching the first line of a property drawer.")
  12854. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12855. "Regular expression matching the last line of a property drawer.")
  12856. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12857. "Regular expression matching the first line of a property drawer.")
  12858. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12859. "Regular expression matching the first line of a property drawer.")
  12860. (defconst org-property-drawer-re
  12861. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12862. org-property-end-re "\\)\n?")
  12863. "Matches an entire property drawer.")
  12864. (defconst org-clock-drawer-re
  12865. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12866. org-property-end-re "\\)\n?")
  12867. "Matches an entire clock drawer.")
  12868. (defsubst org-re-property (property)
  12869. "Return a regexp matching PROPERTY.
  12870. Match group 1 will be set to the value "
  12871. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12872. (defun org-property-action ()
  12873. "Do an action on properties."
  12874. (interactive)
  12875. (let (c)
  12876. (org-at-property-p)
  12877. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12878. (setq c (read-char-exclusive))
  12879. (cond
  12880. ((equal c ?s)
  12881. (call-interactively 'org-set-property))
  12882. ((equal c ?d)
  12883. (call-interactively 'org-delete-property))
  12884. ((equal c ?D)
  12885. (call-interactively 'org-delete-property-globally))
  12886. ((equal c ?c)
  12887. (call-interactively 'org-compute-property-at-point))
  12888. (t (error "No such property action %c" c)))))
  12889. (defun org-set-effort (&optional value)
  12890. "Set the effort property of the current entry.
  12891. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12892. allowed value."
  12893. (interactive "P")
  12894. (if (equal value 0) (setq value 10))
  12895. (let* ((completion-ignore-case t)
  12896. (prop org-effort-property)
  12897. (cur (org-entry-get nil prop))
  12898. (allowed (org-property-get-allowed-values nil prop 'table))
  12899. (existing (mapcar 'list (org-property-values prop)))
  12900. rpl
  12901. (val (cond
  12902. ((stringp value) value)
  12903. ((and allowed (integerp value))
  12904. (or (car (nth (1- value) allowed))
  12905. (car (org-last allowed))))
  12906. (allowed
  12907. (message "Select 1-9,0, [RET%s]: %s"
  12908. (if cur (concat "=" cur) "")
  12909. (mapconcat 'car allowed " "))
  12910. (setq rpl (read-char-exclusive))
  12911. (if (equal rpl ?\r)
  12912. cur
  12913. (setq rpl (- rpl ?0))
  12914. (if (equal rpl 0) (setq rpl 10))
  12915. (if (and (> rpl 0) (<= rpl (length allowed)))
  12916. (car (nth (1- rpl) allowed))
  12917. (org-completing-read "Effort: " allowed nil))))
  12918. (t
  12919. (let (org-completion-use-ido org-completion-use-iswitchb)
  12920. (org-completing-read
  12921. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12922. (concat "[" cur "]") "")
  12923. ": ")
  12924. existing nil nil "" nil cur))))))
  12925. (unless (equal (org-entry-get nil prop) val)
  12926. (org-entry-put nil prop val))
  12927. (message "%s is now %s" prop val)))
  12928. (defun org-at-property-p ()
  12929. "Is cursor inside a property drawer?"
  12930. (save-excursion
  12931. (beginning-of-line 1)
  12932. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12933. (save-match-data ;; Used by calling procedures
  12934. (let ((p (point))
  12935. (range (unless (org-before-first-heading-p)
  12936. (org-get-property-block))))
  12937. (and range (<= (car range) p) (< p (cdr range))))))))
  12938. (defun org-get-property-block (&optional beg end force)
  12939. "Return the (beg . end) range of the body of the property drawer.
  12940. BEG and END can be beginning and end of subtree, if not given
  12941. they will be found.
  12942. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12943. (catch 'exit
  12944. (save-excursion
  12945. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12946. (end (or end (progn (outline-next-heading) (point)))))
  12947. (goto-char beg)
  12948. (if (re-search-forward org-property-start-re end t)
  12949. (setq beg (1+ (match-end 0)))
  12950. (if force
  12951. (save-excursion
  12952. (org-insert-property-drawer)
  12953. (setq end (progn (outline-next-heading) (point))))
  12954. (throw 'exit nil))
  12955. (goto-char beg)
  12956. (if (re-search-forward org-property-start-re end t)
  12957. (setq beg (1+ (match-end 0)))))
  12958. (if (re-search-forward org-property-end-re end t)
  12959. (setq end (match-beginning 0))
  12960. (or force (throw 'exit nil))
  12961. (goto-char beg)
  12962. (setq end beg)
  12963. (org-indent-line)
  12964. (insert ":END:\n"))
  12965. (cons beg end)))))
  12966. (defun org-entry-properties (&optional pom which specific)
  12967. "Get all properties of the entry at point-or-marker POM.
  12968. This includes the TODO keyword, the tags, time strings for deadline,
  12969. scheduled, and clocking, and any additional properties defined in the
  12970. entry. The return value is an alist, keys may occur multiple times
  12971. if the property key was used several times.
  12972. POM may also be nil, in which case the current entry is used.
  12973. If WHICH is nil or `all', get all properties. If WHICH is
  12974. `special' or `standard', only get that subclass. If WHICH
  12975. is a string only get exactly this property. SPECIFIC can be a string, the
  12976. specific property we are interested in. Specifying it can speed
  12977. things up because then unnecessary parsing is avoided."
  12978. (setq which (or which 'all))
  12979. (org-with-point-at pom
  12980. (let ((clockstr (substring org-clock-string 0 -1))
  12981. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12982. (case-fold-search nil)
  12983. beg end range props sum-props key key1 value string clocksum)
  12984. (save-excursion
  12985. (when (condition-case nil
  12986. (and (derived-mode-p 'org-mode) (org-back-to-heading t))
  12987. (error nil))
  12988. (setq beg (point))
  12989. (setq sum-props (get-text-property (point) 'org-summaries))
  12990. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12991. (outline-next-heading)
  12992. (setq end (point))
  12993. (when (memq which '(all special))
  12994. ;; Get the special properties, like TODO and tags
  12995. (goto-char beg)
  12996. (when (and (or (not specific) (string= specific "TODO"))
  12997. (looking-at org-todo-line-regexp) (match-end 2))
  12998. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12999. (when (and (or (not specific) (string= specific "PRIORITY"))
  13000. (looking-at org-priority-regexp))
  13001. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  13002. (when (or (not specific) (string= specific "FILE"))
  13003. (push (cons "FILE" buffer-file-name) props))
  13004. (when (and (or (not specific) (string= specific "TAGS"))
  13005. (setq value (org-get-tags-string))
  13006. (string-match "\\S-" value))
  13007. (push (cons "TAGS" value) props))
  13008. (when (and (or (not specific) (string= specific "ALLTAGS"))
  13009. (setq value (org-get-tags-at)))
  13010. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  13011. ":"))
  13012. props))
  13013. (when (or (not specific) (string= specific "BLOCKED"))
  13014. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  13015. (when (or (not specific)
  13016. (member specific
  13017. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  13018. "TIMESTAMP" "TIMESTAMP_IA")))
  13019. (catch 'match
  13020. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  13021. (setq key (if (match-end 1)
  13022. (substring (org-match-string-no-properties 1)
  13023. 0 -1))
  13024. string (if (equal key clockstr)
  13025. (org-no-properties
  13026. (org-trim
  13027. (buffer-substring
  13028. (match-beginning 3) (goto-char
  13029. (point-at-eol)))))
  13030. (substring (org-match-string-no-properties 3)
  13031. 1 -1)))
  13032. ;; Get the correct property name from the key. This is
  13033. ;; necessary if the user has configured time keywords.
  13034. (setq key1 (concat key ":"))
  13035. (cond
  13036. ((not key)
  13037. (setq key
  13038. (if (= (char-after (match-beginning 3)) ?\[)
  13039. "TIMESTAMP_IA" "TIMESTAMP")))
  13040. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  13041. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  13042. ((equal key1 org-closed-string) (setq key "CLOSED"))
  13043. ((equal key1 org-clock-string) (setq key "CLOCK")))
  13044. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  13045. (progn
  13046. (push (cons key string) props)
  13047. ;; no need to search further if match is found
  13048. (throw 'match t))
  13049. (when (or (equal key "CLOCK") (not (assoc key props)))
  13050. (push (cons key string) props))))))
  13051. )
  13052. (when (memq which '(all standard))
  13053. ;; Get the standard properties, like :PROP: ...
  13054. (setq range (org-get-property-block beg end))
  13055. (when range
  13056. (goto-char (car range))
  13057. (while (re-search-forward
  13058. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  13059. (cdr range) t)
  13060. (setq key (org-match-string-no-properties 1)
  13061. value (org-trim (or (org-match-string-no-properties 2) "")))
  13062. (unless (member key excluded)
  13063. (push (cons key (or value "")) props)))))
  13064. (if clocksum
  13065. (push (cons "CLOCKSUM"
  13066. (org-columns-number-to-string (/ (float clocksum) 60.)
  13067. 'add_times))
  13068. props))
  13069. (unless (assoc "CATEGORY" props)
  13070. (push (cons "CATEGORY" (org-get-category)) props))
  13071. (append sum-props (nreverse props)))))))
  13072. (defun org-entry-get (pom property &optional inherit literal-nil)
  13073. "Get value of PROPERTY for entry at point-or-marker POM.
  13074. If INHERIT is non-nil and the entry does not have the property,
  13075. then also check higher levels of the hierarchy.
  13076. If INHERIT is the symbol `selective', use inheritance only if the setting
  13077. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  13078. If the property is present but empty, the return value is the empty string.
  13079. If the property is not present at all, nil is returned.
  13080. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  13081. do not interpret it as the list atom nil. This is used for inheritance
  13082. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  13083. (org-with-point-at pom
  13084. (if (and inherit (if (eq inherit 'selective)
  13085. (org-property-inherit-p property)
  13086. t))
  13087. (org-entry-get-with-inheritance property literal-nil)
  13088. (if (member property org-special-properties)
  13089. ;; We need a special property. Use `org-entry-properties' to
  13090. ;; retrieve it, but specify the wanted property
  13091. (cdr (assoc property (org-entry-properties nil 'special property)))
  13092. (let ((range (unless (org-before-first-heading-p)
  13093. (org-get-property-block)))
  13094. (props (list (or (assoc property org-file-properties)
  13095. (assoc property org-global-properties)
  13096. (assoc property org-global-properties-fixed))))
  13097. val)
  13098. (org-flet ((ap (key)
  13099. (when (re-search-forward
  13100. (org-re-property key) (cdr range) t)
  13101. (setq props
  13102. (org-update-property-plist
  13103. key
  13104. (if (match-end 1)
  13105. (org-match-string-no-properties 1) "")
  13106. props)))))
  13107. (when (and range (goto-char (car range)))
  13108. (ap property)
  13109. (goto-char (car range))
  13110. (while (ap (concat property "+")))
  13111. (setq val (cdr (assoc property props)))
  13112. (when val (if literal-nil val (org-not-nil val))))))))))
  13113. (defun org-property-or-variable-value (var &optional inherit)
  13114. "Check if there is a property fixing the value of VAR.
  13115. If yes, return this value. If not, return the current value of the variable."
  13116. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  13117. (if (and prop (stringp prop) (string-match "\\S-" prop))
  13118. (read prop)
  13119. (symbol-value var))))
  13120. (defun org-entry-delete (pom property)
  13121. "Delete the property PROPERTY from entry at point-or-marker POM."
  13122. (org-with-point-at pom
  13123. (if (member property org-special-properties)
  13124. nil ; cannot delete these properties.
  13125. (let ((range (org-get-property-block)))
  13126. (if (and range
  13127. (goto-char (car range))
  13128. (re-search-forward
  13129. (org-re-property property)
  13130. (cdr range) t))
  13131. (progn
  13132. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  13133. t)
  13134. nil)))))
  13135. ;; Multi-values properties are properties that contain multiple values
  13136. ;; These values are assumed to be single words, separated by whitespace.
  13137. (defun org-entry-add-to-multivalued-property (pom property value)
  13138. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  13139. (let* ((old (org-entry-get pom property))
  13140. (values (and old (org-split-string old "[ \t]"))))
  13141. (setq value (org-entry-protect-space value))
  13142. (unless (member value values)
  13143. (setq values (cons value values))
  13144. (org-entry-put pom property
  13145. (mapconcat 'identity values " ")))))
  13146. (defun org-entry-remove-from-multivalued-property (pom property value)
  13147. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  13148. (let* ((old (org-entry-get pom property))
  13149. (values (and old (org-split-string old "[ \t]"))))
  13150. (setq value (org-entry-protect-space value))
  13151. (when (member value values)
  13152. (setq values (delete value values))
  13153. (org-entry-put pom property
  13154. (mapconcat 'identity values " ")))))
  13155. (defun org-entry-member-in-multivalued-property (pom property value)
  13156. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  13157. (let* ((old (org-entry-get pom property))
  13158. (values (and old (org-split-string old "[ \t]"))))
  13159. (setq value (org-entry-protect-space value))
  13160. (member value values)))
  13161. (defun org-entry-get-multivalued-property (pom property)
  13162. "Return a list of values in a multivalued property."
  13163. (let* ((value (org-entry-get pom property))
  13164. (values (and value (org-split-string value "[ \t]"))))
  13165. (mapcar 'org-entry-restore-space values)))
  13166. (defun org-entry-put-multivalued-property (pom property &rest values)
  13167. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  13168. VALUES should be a list of strings. Spaces will be protected."
  13169. (org-entry-put pom property
  13170. (mapconcat 'org-entry-protect-space values " "))
  13171. (let* ((value (org-entry-get pom property))
  13172. (values (and value (org-split-string value "[ \t]"))))
  13173. (mapcar 'org-entry-restore-space values)))
  13174. (defun org-entry-protect-space (s)
  13175. "Protect spaces and newline in string S."
  13176. (while (string-match " " s)
  13177. (setq s (replace-match "%20" t t s)))
  13178. (while (string-match "\n" s)
  13179. (setq s (replace-match "%0A" t t s)))
  13180. s)
  13181. (defun org-entry-restore-space (s)
  13182. "Restore spaces and newline in string S."
  13183. (while (string-match "%20" s)
  13184. (setq s (replace-match " " t t s)))
  13185. (while (string-match "%0A" s)
  13186. (setq s (replace-match "\n" t t s)))
  13187. s)
  13188. (defvar org-entry-property-inherited-from (make-marker)
  13189. "Marker pointing to the entry from where a property was inherited.
  13190. Each call to `org-entry-get-with-inheritance' will set this marker to the
  13191. location of the entry where the inheritance search matched. If there was
  13192. no match, the marker will point nowhere.
  13193. Note that also `org-entry-get' calls this function, if the INHERIT flag
  13194. is set.")
  13195. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  13196. "Get entry property, and search higher levels if not present.
  13197. The search will stop at the first ancestor which has the property defined.
  13198. If the value found is \"nil\", return nil to show that the property
  13199. should be considered as undefined (this is the meaning of nil here).
  13200. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  13201. (move-marker org-entry-property-inherited-from nil)
  13202. (let (tmp)
  13203. (unless (org-before-first-heading-p)
  13204. (save-excursion
  13205. (save-restriction
  13206. (widen)
  13207. (catch 'ex
  13208. (while t
  13209. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  13210. (org-back-to-heading t)
  13211. (move-marker org-entry-property-inherited-from (point))
  13212. (throw 'ex tmp))
  13213. (or (org-up-heading-safe) (throw 'ex nil)))))))
  13214. (setq tmp (or tmp
  13215. (cdr (assoc property org-file-properties))
  13216. (cdr (assoc property org-global-properties))
  13217. (cdr (assoc property org-global-properties-fixed))))
  13218. (if literal-nil tmp (org-not-nil tmp))))
  13219. (defvar org-property-changed-functions nil
  13220. "Hook called when the value of a property has changed.
  13221. Each hook function should accept two arguments, the name of the property
  13222. and the new value.")
  13223. (defun org-entry-put (pom property value)
  13224. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  13225. (org-with-point-at pom
  13226. (org-back-to-heading t)
  13227. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  13228. range)
  13229. (cond
  13230. ((equal property "TODO")
  13231. (when (and (stringp value) (string-match "\\S-" value)
  13232. (not (member value org-todo-keywords-1)))
  13233. (error "\"%s\" is not a valid TODO state" value))
  13234. (if (or (not value)
  13235. (not (string-match "\\S-" value)))
  13236. (setq value 'none))
  13237. (org-todo value)
  13238. (org-set-tags nil 'align))
  13239. ((equal property "PRIORITY")
  13240. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  13241. (string-to-char value) ?\ ))
  13242. (org-set-tags nil 'align))
  13243. ((equal property "SCHEDULED")
  13244. (if (re-search-forward org-scheduled-time-regexp end t)
  13245. (cond
  13246. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13247. ((eq value 'later) (org-timestamp-change 1 'day))
  13248. (t (call-interactively 'org-schedule)))
  13249. (call-interactively 'org-schedule)))
  13250. ((equal property "DEADLINE")
  13251. (if (re-search-forward org-deadline-time-regexp end t)
  13252. (cond
  13253. ((eq value 'earlier) (org-timestamp-change -1 'day))
  13254. ((eq value 'later) (org-timestamp-change 1 'day))
  13255. (t (call-interactively 'org-deadline)))
  13256. (call-interactively 'org-deadline)))
  13257. ((member property org-special-properties)
  13258. (error "The %s property can not yet be set with `org-entry-put'"
  13259. property))
  13260. (t ; a non-special property
  13261. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  13262. (setq range (org-get-property-block beg end 'force))
  13263. (goto-char (car range))
  13264. (if (re-search-forward
  13265. (org-re-property property) (cdr range) t)
  13266. (progn
  13267. (delete-region (match-beginning 0) (match-end 0))
  13268. (goto-char (match-beginning 0)))
  13269. (goto-char (cdr range))
  13270. (insert "\n")
  13271. (backward-char 1)
  13272. (org-indent-line))
  13273. (insert ":" property ":")
  13274. (and value (insert " " value))
  13275. (org-indent-line)))))
  13276. (run-hook-with-args 'org-property-changed-functions property value)))
  13277. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  13278. "Get all property keys in the current buffer.
  13279. With INCLUDE-SPECIALS, also list the special properties that reflect things
  13280. like tags and TODO state.
  13281. With INCLUDE-DEFAULTS, also include properties that has special meaning
  13282. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  13283. and others.
  13284. With INCLUDE-COLUMNS, also include property names given in COLUMN
  13285. formats in the current buffer."
  13286. (let (rtn range cfmt s p)
  13287. (save-excursion
  13288. (save-restriction
  13289. (widen)
  13290. (goto-char (point-min))
  13291. (while (re-search-forward org-property-start-re nil t)
  13292. (setq range (org-get-property-block))
  13293. (goto-char (car range))
  13294. (while (re-search-forward
  13295. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  13296. (cdr range) t)
  13297. (add-to-list 'rtn (org-match-string-no-properties 1)))
  13298. (outline-next-heading))))
  13299. (when include-specials
  13300. (setq rtn (append org-special-properties rtn)))
  13301. (when include-defaults
  13302. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  13303. (add-to-list 'rtn org-effort-property))
  13304. (when include-columns
  13305. (save-excursion
  13306. (save-restriction
  13307. (widen)
  13308. (goto-char (point-min))
  13309. (while (re-search-forward
  13310. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  13311. nil t)
  13312. (setq cfmt (match-string 2) s 0)
  13313. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  13314. cfmt s)
  13315. (setq s (match-end 0)
  13316. p (match-string 1 cfmt))
  13317. (unless (or (equal p "ITEM")
  13318. (member p org-special-properties))
  13319. (add-to-list 'rtn (match-string 1 cfmt))))))))
  13320. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  13321. (defun org-property-values (key)
  13322. "Return a list of all values of property KEY in the current buffer."
  13323. (save-excursion
  13324. (save-restriction
  13325. (widen)
  13326. (goto-char (point-min))
  13327. (let ((re (org-re-property key))
  13328. values)
  13329. (while (re-search-forward re nil t)
  13330. (add-to-list 'values (org-trim (match-string 1))))
  13331. (delete "" values)))))
  13332. (defun org-insert-property-drawer ()
  13333. "Insert a property drawer into the current entry."
  13334. (org-back-to-heading t)
  13335. (looking-at org-outline-regexp)
  13336. (let ((indent (if org-adapt-indentation
  13337. (- (match-end 0) (match-beginning 0))
  13338. 0))
  13339. (beg (point))
  13340. (re (concat "^[ \t]*" org-keyword-time-regexp))
  13341. end hiddenp)
  13342. (outline-next-heading)
  13343. (setq end (point))
  13344. (goto-char beg)
  13345. (while (re-search-forward re end t))
  13346. (setq hiddenp (outline-invisible-p))
  13347. (end-of-line 1)
  13348. (and (equal (char-after) ?\n) (forward-char 1))
  13349. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  13350. (if (member (match-string 1) '("CLOCK:" ":END:"))
  13351. ;; just skip this line
  13352. (beginning-of-line 2)
  13353. ;; Drawer start, find the end
  13354. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  13355. (beginning-of-line 1)))
  13356. (org-skip-over-state-notes)
  13357. (skip-chars-backward " \t\n\r")
  13358. (if (eq (char-before) ?*) (forward-char 1))
  13359. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  13360. (beginning-of-line 0)
  13361. (org-indent-to-column indent)
  13362. (beginning-of-line 2)
  13363. (org-indent-to-column indent)
  13364. (beginning-of-line 0)
  13365. (if hiddenp
  13366. (save-excursion
  13367. (org-back-to-heading t)
  13368. (hide-entry))
  13369. (org-flag-drawer t))))
  13370. (defun org-insert-drawer (&optional arg drawer)
  13371. "Insert a drawer at point.
  13372. Optional argument DRAWER, when non-nil, is a string representing
  13373. drawer's name. Otherwise, the user is prompted for a name.
  13374. If a region is active, insert the drawer around that region
  13375. instead.
  13376. Point is left between drawer's boundaries."
  13377. (interactive "P")
  13378. (let* ((logbook (if (stringp org-log-into-drawer) org-log-into-drawer
  13379. "LOGBOOK"))
  13380. ;; SYSTEM-DRAWERS is a list of drawer names that are used
  13381. ;; internally by Org. They are meant to be inserted
  13382. ;; automatically.
  13383. (system-drawers `("CLOCK" ,logbook "PROPERTIES"))
  13384. ;; Remove system drawers from list. Note: For some reason,
  13385. ;; `org-completing-read' ignores the predicate while
  13386. ;; `completing-read' handles it fine.
  13387. (drawer (if arg "PROPERTIES"
  13388. (or drawer
  13389. (completing-read
  13390. "Drawer: " org-drawers
  13391. (lambda (d) (not (member d system-drawers))))))))
  13392. (cond
  13393. ;; With C-u, fall back on `org-insert-property-drawer'
  13394. (arg (org-insert-property-drawer))
  13395. ;; With an active region, insert a drawer at point.
  13396. ((not (org-region-active-p))
  13397. (progn
  13398. (unless (bolp) (insert "\n"))
  13399. (insert (format ":%s:\n\n:END:\n" drawer))
  13400. (forward-line -2)))
  13401. ;; Otherwise, insert the drawer at point
  13402. (t
  13403. (let ((rbeg (region-beginning))
  13404. (rend (copy-marker (region-end))))
  13405. (unwind-protect
  13406. (progn
  13407. (goto-char rbeg)
  13408. (beginning-of-line)
  13409. (when (save-excursion
  13410. (re-search-forward org-outline-regexp-bol rend t))
  13411. (error "Drawers cannot contain headlines"))
  13412. ;; Position point at the beginning of the first
  13413. ;; non-blank line in region. Insert drawer's opening
  13414. ;; there, then indent it.
  13415. (org-skip-whitespace)
  13416. (beginning-of-line)
  13417. (insert ":" drawer ":\n")
  13418. (forward-line -1)
  13419. (indent-for-tab-command)
  13420. ;; Move point to the beginning of the first blank line
  13421. ;; after the last non-blank line in region. Insert
  13422. ;; drawer's closing, then indent it.
  13423. (goto-char rend)
  13424. (skip-chars-backward " \r\t\n")
  13425. (insert "\n:END:")
  13426. (indent-for-tab-command)
  13427. (unless (eolp) (insert "\n")))
  13428. ;; Clear marker, whatever the outcome of insertion is.
  13429. (set-marker rend nil)))))))
  13430. (defvar org-property-set-functions-alist nil
  13431. "Property set function alist.
  13432. Each entry should have the following format:
  13433. (PROPERTY . READ-FUNCTION)
  13434. The read function will be called with the same argument as
  13435. `org-completing-read'.")
  13436. (defun org-set-property-function (property)
  13437. "Get the function that should be used to set PROPERTY.
  13438. This is computed according to `org-property-set-functions-alist'."
  13439. (or (cdr (assoc property org-property-set-functions-alist))
  13440. 'org-completing-read))
  13441. (defun org-read-property-value (property)
  13442. "Read PROPERTY value from user."
  13443. (let* ((completion-ignore-case t)
  13444. (allowed (org-property-get-allowed-values nil property 'table))
  13445. (cur (org-entry-get nil property))
  13446. (prompt (concat property " value"
  13447. (if (and cur (string-match "\\S-" cur))
  13448. (concat " [" cur "]") "") ": "))
  13449. (set-function (org-set-property-function property))
  13450. (val (if allowed
  13451. (funcall set-function prompt allowed nil
  13452. (not (get-text-property 0 'org-unrestricted
  13453. (caar allowed))))
  13454. (let (org-completion-use-ido org-completion-use-iswitchb)
  13455. (funcall set-function prompt
  13456. (mapcar 'list (org-property-values property))
  13457. nil nil "" nil cur)))))
  13458. (if (equal val "")
  13459. cur
  13460. val)))
  13461. (defvar org-last-set-property nil)
  13462. (defun org-read-property-name ()
  13463. "Read a property name."
  13464. (let* ((completion-ignore-case t)
  13465. (keys (org-buffer-property-keys nil t t))
  13466. (default-prop (or (save-excursion
  13467. (save-match-data
  13468. (beginning-of-line)
  13469. (and (looking-at "^\\s-*:\\([^:\n]+\\):")
  13470. (null (string= (match-string 1) "END"))
  13471. (match-string 1))))
  13472. org-last-set-property))
  13473. (property (org-icompleting-read
  13474. (concat "Property"
  13475. (if default-prop (concat " [" default-prop "]") "")
  13476. ": ")
  13477. (mapcar 'list keys)
  13478. nil nil nil nil
  13479. default-prop
  13480. )))
  13481. (if (member property keys)
  13482. property
  13483. (or (cdr (assoc (downcase property)
  13484. (mapcar (lambda (x) (cons (downcase x) x))
  13485. keys)))
  13486. property))))
  13487. (defun org-set-property (property value)
  13488. "In the current entry, set PROPERTY to VALUE.
  13489. When called interactively, this will prompt for a property name, offering
  13490. completion on existing and default properties. And then it will prompt
  13491. for a value, offering completion either on allowed values (via an inherited
  13492. xxx_ALL property) or on existing values in other instances of this property
  13493. in the current file."
  13494. (interactive (list nil nil))
  13495. (let* ((property (or property (org-read-property-name)))
  13496. (value (or value (org-read-property-value property)))
  13497. (fn (cdr (assoc property org-properties-postprocess-alist))))
  13498. (setq org-last-set-property property)
  13499. ;; Possibly postprocess the inserted value:
  13500. (when fn (setq value (funcall fn value)))
  13501. (unless (equal (org-entry-get nil property) value)
  13502. (org-entry-put nil property value))))
  13503. (defun org-delete-property (property)
  13504. "In the current entry, delete PROPERTY."
  13505. (interactive
  13506. (let* ((completion-ignore-case t)
  13507. (prop (org-icompleting-read "Property: "
  13508. (org-entry-properties nil 'standard))))
  13509. (list prop)))
  13510. (message "Property %s %s" property
  13511. (if (org-entry-delete nil property)
  13512. "deleted"
  13513. "was not present in the entry")))
  13514. (defun org-delete-property-globally (property)
  13515. "Remove PROPERTY globally, from all entries."
  13516. (interactive
  13517. (let* ((completion-ignore-case t)
  13518. (prop (org-icompleting-read
  13519. "Globally remove property: "
  13520. (mapcar 'list (org-buffer-property-keys)))))
  13521. (list prop)))
  13522. (save-excursion
  13523. (save-restriction
  13524. (widen)
  13525. (goto-char (point-min))
  13526. (let ((cnt 0))
  13527. (while (re-search-forward
  13528. (org-re-property property)
  13529. nil t)
  13530. (setq cnt (1+ cnt))
  13531. (delete-region (match-beginning 0) (1+ (point-at-eol))))
  13532. (message "Property \"%s\" removed from %d entries" property cnt)))))
  13533. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  13534. (defun org-compute-property-at-point ()
  13535. "Compute the property at point.
  13536. This looks for an enclosing column format, extracts the operator and
  13537. then applies it to the property in the column format's scope."
  13538. (interactive)
  13539. (unless (org-at-property-p)
  13540. (error "Not at a property"))
  13541. (let ((prop (org-match-string-no-properties 2)))
  13542. (org-columns-get-format-and-top-level)
  13543. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  13544. (error "No operator defined for property %s" prop))
  13545. (org-columns-compute prop)))
  13546. (defvar org-property-allowed-value-functions nil
  13547. "Hook for functions supplying allowed values for a specific property.
  13548. The functions must take a single argument, the name of the property, and
  13549. return a flat list of allowed values. If \":ETC\" is one of
  13550. the values, this means that these values are intended as defaults for
  13551. completion, but that other values should be allowed too.
  13552. The functions must return nil if they are not responsible for this
  13553. property.")
  13554. (defun org-property-get-allowed-values (pom property &optional table)
  13555. "Get allowed values for the property PROPERTY.
  13556. When TABLE is non-nil, return an alist that can directly be used for
  13557. completion."
  13558. (let (vals)
  13559. (cond
  13560. ((equal property "TODO")
  13561. (setq vals (org-with-point-at pom
  13562. (append org-todo-keywords-1 '("")))))
  13563. ((equal property "PRIORITY")
  13564. (let ((n org-lowest-priority))
  13565. (while (>= n org-highest-priority)
  13566. (push (char-to-string n) vals)
  13567. (setq n (1- n)))))
  13568. ((member property org-special-properties))
  13569. ((setq vals (run-hook-with-args-until-success
  13570. 'org-property-allowed-value-functions property)))
  13571. (t
  13572. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  13573. (when (and vals (string-match "\\S-" vals))
  13574. (setq vals (car (read-from-string (concat "(" vals ")"))))
  13575. (setq vals (mapcar (lambda (x)
  13576. (cond ((stringp x) x)
  13577. ((numberp x) (number-to-string x))
  13578. ((symbolp x) (symbol-name x))
  13579. (t "???")))
  13580. vals)))))
  13581. (when (member ":ETC" vals)
  13582. (setq vals (remove ":ETC" vals))
  13583. (org-add-props (car vals) '(org-unrestricted t)))
  13584. (if table (mapcar 'list vals) vals)))
  13585. (defun org-property-previous-allowed-value (&optional previous)
  13586. "Switch to the next allowed value for this property."
  13587. (interactive)
  13588. (org-property-next-allowed-value t))
  13589. (defun org-property-next-allowed-value (&optional previous)
  13590. "Switch to the next allowed value for this property."
  13591. (interactive)
  13592. (unless (org-at-property-p)
  13593. (error "Not at a property"))
  13594. (let* ((key (match-string 2))
  13595. (value (match-string 3))
  13596. (allowed (or (org-property-get-allowed-values (point) key)
  13597. (and (member value '("[ ]" "[-]" "[X]"))
  13598. '("[ ]" "[X]"))))
  13599. nval)
  13600. (unless allowed
  13601. (error "Allowed values for this property have not been defined"))
  13602. (if previous (setq allowed (reverse allowed)))
  13603. (if (member value allowed)
  13604. (setq nval (car (cdr (member value allowed)))))
  13605. (setq nval (or nval (car allowed)))
  13606. (if (equal nval value)
  13607. (error "Only one allowed value for this property"))
  13608. (org-at-property-p)
  13609. (replace-match (concat " :" key ": " nval) t t)
  13610. (org-indent-line)
  13611. (beginning-of-line 1)
  13612. (skip-chars-forward " \t")
  13613. (run-hook-with-args 'org-property-changed-functions key nval)))
  13614. (defun org-find-olp (path &optional this-buffer)
  13615. "Return a marker pointing to the entry at outline path OLP.
  13616. If anything goes wrong, throw an error.
  13617. You can wrap this call to catch the error like this:
  13618. (condition-case msg
  13619. (org-mobile-locate-entry (match-string 4))
  13620. (error (nth 1 msg)))
  13621. The return value will then be either a string with the error message,
  13622. or a marker if everything is OK.
  13623. If THIS-BUFFER is set, the outline path does not contain a file,
  13624. only headings."
  13625. (let* ((file (if this-buffer buffer-file-name (pop path)))
  13626. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  13627. (level 1)
  13628. (lmin 1)
  13629. (lmax 1)
  13630. limit re end found pos heading cnt flevel)
  13631. (unless buffer (error "File not found :%s" file))
  13632. (with-current-buffer buffer
  13633. (save-excursion
  13634. (save-restriction
  13635. (widen)
  13636. (setq limit (point-max))
  13637. (goto-char (point-min))
  13638. (while (setq heading (pop path))
  13639. (setq re (format org-complex-heading-regexp-format
  13640. (regexp-quote heading)))
  13641. (setq cnt 0 pos (point))
  13642. (while (re-search-forward re end t)
  13643. (setq level (- (match-end 1) (match-beginning 1)))
  13644. (if (and (>= level lmin) (<= level lmax))
  13645. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  13646. (when (= cnt 0) (error "Heading not found on level %d: %s"
  13647. lmax heading))
  13648. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  13649. lmax heading))
  13650. (goto-char found)
  13651. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  13652. (setq end (save-excursion (org-end-of-subtree t t))))
  13653. (when (org-at-heading-p)
  13654. (move-marker (make-marker) (point))))))))
  13655. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  13656. "Find node HEADING in BUFFER.
  13657. Return a marker to the heading if it was found, or nil if not.
  13658. If POS-ONLY is set, return just the position instead of a marker.
  13659. The heading text must match exact, but it may have a TODO keyword,
  13660. a priority cookie and tags in the standard locations."
  13661. (with-current-buffer (or buffer (current-buffer))
  13662. (save-excursion
  13663. (save-restriction
  13664. (widen)
  13665. (goto-char (point-min))
  13666. (let (case-fold-search)
  13667. (if (re-search-forward
  13668. (format org-complex-heading-regexp-format
  13669. (regexp-quote heading)) nil t)
  13670. (if pos-only
  13671. (match-beginning 0)
  13672. (move-marker (make-marker) (match-beginning 0)))))))))
  13673. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13674. "Find Org node headline HEADING in all .org files in directory DIR.
  13675. When the target headline is found, return a marker to this location."
  13676. (let ((files (directory-files (or dir default-directory)
  13677. nil "\\`[^.#].*\\.org\\'"))
  13678. file visiting m buffer)
  13679. (catch 'found
  13680. (while (setq file (pop files))
  13681. (message "trying %s" file)
  13682. (setq visiting (org-find-base-buffer-visiting file))
  13683. (setq buffer (or visiting (find-file-noselect file)))
  13684. (setq m (org-find-exact-headline-in-buffer
  13685. heading buffer))
  13686. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13687. (and m (throw 'found m))))))
  13688. (defun org-find-entry-with-id (ident)
  13689. "Locate the entry that contains the ID property with exact value IDENT.
  13690. IDENT can be a string, a symbol or a number, this function will search for
  13691. the string representation of it.
  13692. Return the position where this entry starts, or nil if there is no such entry."
  13693. (interactive "sID: ")
  13694. (let ((id (cond
  13695. ((stringp ident) ident)
  13696. ((symbol-name ident) (symbol-name ident))
  13697. ((numberp ident) (number-to-string ident))
  13698. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13699. (case-fold-search nil))
  13700. (save-excursion
  13701. (save-restriction
  13702. (widen)
  13703. (goto-char (point-min))
  13704. (when (re-search-forward
  13705. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13706. nil t)
  13707. (org-back-to-heading t)
  13708. (point))))))
  13709. ;;;; Timestamps
  13710. (defvar org-last-changed-timestamp nil)
  13711. (defvar org-last-inserted-timestamp nil
  13712. "The last time stamp inserted with `org-insert-time-stamp'.")
  13713. (defvar org-time-was-given) ; dynamically scoped parameter
  13714. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13715. (defvar org-ts-what) ; dynamically scoped parameter
  13716. (defun org-time-stamp (arg &optional inactive)
  13717. "Prompt for a date/time and insert a time stamp.
  13718. If the user specifies a time like HH:MM or if this command is
  13719. called with at least one prefix argument, the time stamp contains
  13720. the date and the time. Otherwise, only the date is be included.
  13721. All parts of a date not specified by the user is filled in from
  13722. the current date/time. So if you just press return without
  13723. typing anything, the time stamp will represent the current
  13724. date/time.
  13725. If there is already a timestamp at the cursor, it will be
  13726. modified.
  13727. With two universal prefix arguments, insert an active timestamp
  13728. with the current time without prompting the user."
  13729. (interactive "P")
  13730. (let* ((ts nil)
  13731. (default-time
  13732. ;; Default time is either today, or, when entering a range,
  13733. ;; the range start.
  13734. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13735. (save-excursion
  13736. (re-search-backward
  13737. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13738. (- (point) 20) t)))
  13739. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13740. (current-time)))
  13741. (default-input (and ts (org-get-compact-tod ts)))
  13742. (repeater (save-excursion
  13743. (save-match-data
  13744. (beginning-of-line)
  13745. (when (re-search-forward
  13746. "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ;;\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  13747. (save-excursion (progn (end-of-line) (point))) t)
  13748. (match-string 0)))))
  13749. org-time-was-given org-end-time-was-given time)
  13750. (cond
  13751. ((and (org-at-timestamp-p t)
  13752. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13753. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13754. (insert "--")
  13755. (setq time (let ((this-command this-command))
  13756. (org-read-date arg 'totime nil nil
  13757. default-time default-input inactive)))
  13758. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13759. ((org-at-timestamp-p t)
  13760. (setq time (let ((this-command this-command))
  13761. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13762. (when (org-at-timestamp-p t) ; just to get the match data
  13763. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13764. (replace-match "")
  13765. (setq org-last-changed-timestamp
  13766. (org-insert-time-stamp
  13767. time (or org-time-was-given arg)
  13768. inactive nil nil (list org-end-time-was-given)))
  13769. (when repeater (goto-char (1- (point))) (insert " " repeater)
  13770. (setq org-last-changed-timestamp
  13771. (concat (substring org-last-inserted-timestamp 0 -1)
  13772. " " repeater ">"))))
  13773. (message "Timestamp updated"))
  13774. ((equal arg '(16))
  13775. (org-insert-time-stamp (current-time) t))
  13776. (t
  13777. (setq time (let ((this-command this-command))
  13778. (org-read-date arg 'totime nil nil default-time default-input inactive)))
  13779. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13780. nil nil (list org-end-time-was-given))))))
  13781. ;; FIXME: can we use this for something else, like computing time differences?
  13782. (defun org-get-compact-tod (s)
  13783. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13784. (let* ((t1 (match-string 1 s))
  13785. (h1 (string-to-number (match-string 2 s)))
  13786. (m1 (string-to-number (match-string 3 s)))
  13787. (t2 (and (match-end 4) (match-string 5 s)))
  13788. (h2 (and t2 (string-to-number (match-string 6 s))))
  13789. (m2 (and t2 (string-to-number (match-string 7 s))))
  13790. dh dm)
  13791. (if (not t2)
  13792. t1
  13793. (setq dh (- h2 h1) dm (- m2 m1))
  13794. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13795. (concat t1 "+" (number-to-string dh)
  13796. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13797. (defun org-time-stamp-inactive (&optional arg)
  13798. "Insert an inactive time stamp.
  13799. An inactive time stamp is enclosed in square brackets instead of angle
  13800. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13801. does not link to the calendar and cannot be changed with the S-cursor keys.
  13802. So these are more for recording a certain time/date."
  13803. (interactive "P")
  13804. (org-time-stamp arg 'inactive))
  13805. (defvar org-date-ovl (make-overlay 1 1))
  13806. (overlay-put org-date-ovl 'face 'org-date-selected)
  13807. (org-detach-overlay org-date-ovl)
  13808. (defvar org-ans1) ; dynamically scoped parameter
  13809. (defvar org-ans2) ; dynamically scoped parameter
  13810. (defvar org-plain-time-of-day-regexp) ; defined below
  13811. (defvar org-overriding-default-time nil) ; dynamically scoped
  13812. (defvar org-read-date-overlay nil)
  13813. (defvar org-dcst nil) ; dynamically scoped
  13814. (defvar org-read-date-history nil)
  13815. (defvar org-read-date-final-answer nil)
  13816. (defvar org-read-date-analyze-futurep nil)
  13817. (defvar org-read-date-analyze-forced-year nil)
  13818. (defvar org-read-date-inactive)
  13819. (defun org-read-date (&optional org-with-time to-time from-string prompt
  13820. default-time default-input inactive)
  13821. "Read a date, possibly a time, and make things smooth for the user.
  13822. The prompt will suggest to enter an ISO date, but you can also enter anything
  13823. which will at least partially be understood by `parse-time-string'.
  13824. Unrecognized parts of the date will default to the current day, month, year,
  13825. hour and minute. If this command is called to replace a timestamp at point,
  13826. or to enter the second timestamp of a range, the default time is taken
  13827. from the existing stamp. Furthermore, the command prefers the future,
  13828. so if you are giving a date where the year is not given, and the day-month
  13829. combination is already past in the current year, it will assume you
  13830. mean next year. For details, see the manual. A few examples:
  13831. 3-2-5 --> 2003-02-05
  13832. feb 15 --> currentyear-02-15
  13833. 2/15 --> currentyear-02-15
  13834. sep 12 9 --> 2009-09-12
  13835. 12:45 --> today 12:45
  13836. 22 sept 0:34 --> currentyear-09-22 0:34
  13837. 12 --> currentyear-currentmonth-12
  13838. Fri --> nearest Friday (today or later)
  13839. etc.
  13840. Furthermore you can specify a relative date by giving, as the *first* thing
  13841. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  13842. change in days weeks, months, years.
  13843. With a single plus or minus, the date is relative to today. With a double
  13844. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13845. +4d --> four days from today
  13846. +4 --> same as above
  13847. +2w --> two weeks from today
  13848. ++5 --> five days from default date
  13849. The function understands only English month and weekday abbreviations.
  13850. While prompting, a calendar is popped up - you can also select the
  13851. date with the mouse (button 1). The calendar shows a period of three
  13852. months. To scroll it to other months, use the keys `>' and `<'.
  13853. If you don't like the calendar, turn it off with
  13854. \(setq org-read-date-popup-calendar nil)
  13855. With optional argument TO-TIME, the date will immediately be converted
  13856. to an internal time.
  13857. With an optional argument WITH-TIME, the prompt will suggest to also
  13858. insert a time. Note that when WITH-TIME is not set, you can still
  13859. enter a time, and this function will inform the calling routine about
  13860. this change. The calling routine may then choose to change the format
  13861. used to insert the time stamp into the buffer to include the time.
  13862. With optional argument FROM-STRING, read from this string instead from
  13863. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13864. the time/date that is used for everything that is not specified by the
  13865. user."
  13866. (require 'parse-time)
  13867. (let* ((org-time-stamp-rounding-minutes
  13868. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13869. (org-dcst org-display-custom-times)
  13870. (ct (org-current-time))
  13871. (org-def (or org-overriding-default-time default-time ct))
  13872. (org-defdecode (decode-time org-def))
  13873. (dummy (progn
  13874. (when (< (nth 2 org-defdecode) org-extend-today-until)
  13875. (setcar (nthcdr 2 org-defdecode) -1)
  13876. (setcar (nthcdr 1 org-defdecode) 59)
  13877. (setq org-def (apply 'encode-time org-defdecode)
  13878. org-defdecode (decode-time org-def)))))
  13879. (calendar-frame-setup nil)
  13880. (calendar-setup nil)
  13881. (calendar-move-hook nil)
  13882. (calendar-view-diary-initially-flag nil)
  13883. (calendar-view-holidays-initially-flag nil)
  13884. (timestr (format-time-string
  13885. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  13886. (prompt (concat (if prompt (concat prompt " ") "")
  13887. (format "Date+time [%s]: " timestr)))
  13888. ans (org-ans0 "") org-ans1 org-ans2 final)
  13889. (cond
  13890. (from-string (setq ans from-string))
  13891. (org-read-date-popup-calendar
  13892. (save-excursion
  13893. (save-window-excursion
  13894. (calendar)
  13895. (org-eval-in-calendar '(setq cursor-type nil) t)
  13896. (unwind-protect
  13897. (progn
  13898. (calendar-forward-day (- (time-to-days org-def)
  13899. (calendar-absolute-from-gregorian
  13900. (calendar-current-date))))
  13901. (org-eval-in-calendar nil t)
  13902. (let* ((old-map (current-local-map))
  13903. (map (copy-keymap calendar-mode-map))
  13904. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13905. (org-defkey map (kbd "RET") 'org-calendar-select)
  13906. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13907. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13908. (org-defkey minibuffer-local-map [(meta shift left)]
  13909. (lambda () (interactive)
  13910. (org-eval-in-calendar '(calendar-backward-month 1))))
  13911. (org-defkey minibuffer-local-map [(meta shift right)]
  13912. (lambda () (interactive)
  13913. (org-eval-in-calendar '(calendar-forward-month 1))))
  13914. (org-defkey minibuffer-local-map [(meta shift up)]
  13915. (lambda () (interactive)
  13916. (org-eval-in-calendar '(calendar-backward-year 1))))
  13917. (org-defkey minibuffer-local-map [(meta shift down)]
  13918. (lambda () (interactive)
  13919. (org-eval-in-calendar '(calendar-forward-year 1))))
  13920. (org-defkey minibuffer-local-map [?\e (shift left)]
  13921. (lambda () (interactive)
  13922. (org-eval-in-calendar '(calendar-backward-month 1))))
  13923. (org-defkey minibuffer-local-map [?\e (shift right)]
  13924. (lambda () (interactive)
  13925. (org-eval-in-calendar '(calendar-forward-month 1))))
  13926. (org-defkey minibuffer-local-map [?\e (shift up)]
  13927. (lambda () (interactive)
  13928. (org-eval-in-calendar '(calendar-backward-year 1))))
  13929. (org-defkey minibuffer-local-map [?\e (shift down)]
  13930. (lambda () (interactive)
  13931. (org-eval-in-calendar '(calendar-forward-year 1))))
  13932. (org-defkey minibuffer-local-map [(shift up)]
  13933. (lambda () (interactive)
  13934. (org-eval-in-calendar '(calendar-backward-week 1))))
  13935. (org-defkey minibuffer-local-map [(shift down)]
  13936. (lambda () (interactive)
  13937. (org-eval-in-calendar '(calendar-forward-week 1))))
  13938. (org-defkey minibuffer-local-map [(shift left)]
  13939. (lambda () (interactive)
  13940. (org-eval-in-calendar '(calendar-backward-day 1))))
  13941. (org-defkey minibuffer-local-map [(shift right)]
  13942. (lambda () (interactive)
  13943. (org-eval-in-calendar '(calendar-forward-day 1))))
  13944. (org-defkey minibuffer-local-map ">"
  13945. (lambda () (interactive)
  13946. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13947. (org-defkey minibuffer-local-map "<"
  13948. (lambda () (interactive)
  13949. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13950. (org-defkey minibuffer-local-map "\C-v"
  13951. (lambda () (interactive)
  13952. (org-eval-in-calendar
  13953. '(calendar-scroll-left-three-months 1))))
  13954. (org-defkey minibuffer-local-map "\M-v"
  13955. (lambda () (interactive)
  13956. (org-eval-in-calendar
  13957. '(calendar-scroll-right-three-months 1))))
  13958. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13959. (unwind-protect
  13960. (progn
  13961. (use-local-map map)
  13962. (setq org-read-date-inactive inactive)
  13963. (add-hook 'post-command-hook 'org-read-date-display)
  13964. (setq org-ans0 (read-string prompt default-input
  13965. 'org-read-date-history nil))
  13966. ;; org-ans0: from prompt
  13967. ;; org-ans1: from mouse click
  13968. ;; org-ans2: from calendar motion
  13969. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13970. (remove-hook 'post-command-hook 'org-read-date-display)
  13971. (use-local-map old-map)
  13972. (when org-read-date-overlay
  13973. (delete-overlay org-read-date-overlay)
  13974. (setq org-read-date-overlay nil)))))
  13975. (bury-buffer "*Calendar*")))))
  13976. (t ; Naked prompt only
  13977. (unwind-protect
  13978. (setq ans (read-string prompt default-input
  13979. 'org-read-date-history timestr))
  13980. (when org-read-date-overlay
  13981. (delete-overlay org-read-date-overlay)
  13982. (setq org-read-date-overlay nil)))))
  13983. (setq final (org-read-date-analyze ans org-def org-defdecode))
  13984. (when org-read-date-analyze-forced-year
  13985. (message "Year was forced into %s"
  13986. (if org-read-date-force-compatible-dates
  13987. "compatible range (1970-2037)"
  13988. "range representable on this machine"))
  13989. (ding))
  13990. ;; One round trip to get rid of 34th of August and stuff like that....
  13991. (setq final (decode-time (apply 'encode-time final)))
  13992. (setq org-read-date-final-answer ans)
  13993. (if to-time
  13994. (apply 'encode-time final)
  13995. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13996. (format "%04d-%02d-%02d %02d:%02d"
  13997. (nth 5 final) (nth 4 final) (nth 3 final)
  13998. (nth 2 final) (nth 1 final))
  13999. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  14000. (defvar org-def)
  14001. (defvar org-defdecode)
  14002. (defvar org-with-time)
  14003. (defun org-read-date-display ()
  14004. "Display the current date prompt interpretation in the minibuffer."
  14005. (when org-read-date-display-live
  14006. (when org-read-date-overlay
  14007. (delete-overlay org-read-date-overlay))
  14008. (when (minibufferp (current-buffer))
  14009. (save-excursion
  14010. (end-of-line 1)
  14011. (while (not (equal (buffer-substring
  14012. (max (point-min) (- (point) 4)) (point))
  14013. " "))
  14014. (insert " ")))
  14015. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  14016. " " (or org-ans1 org-ans2)))
  14017. (org-end-time-was-given nil)
  14018. (f (org-read-date-analyze ans org-def org-defdecode))
  14019. (fmts (if org-dcst
  14020. org-time-stamp-custom-formats
  14021. org-time-stamp-formats))
  14022. (fmt (if (or org-with-time
  14023. (and (boundp 'org-time-was-given) org-time-was-given))
  14024. (cdr fmts)
  14025. (car fmts)))
  14026. (txt (format-time-string fmt (apply 'encode-time f)))
  14027. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  14028. (txt (concat "=> " txt)))
  14029. (when (and org-end-time-was-given
  14030. (string-match org-plain-time-of-day-regexp txt))
  14031. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  14032. org-end-time-was-given
  14033. (substring txt (match-end 0)))))
  14034. (when org-read-date-analyze-futurep
  14035. (setq txt (concat txt " (=>F)")))
  14036. (setq org-read-date-overlay
  14037. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  14038. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  14039. (defun org-read-date-analyze (ans org-def org-defdecode)
  14040. "Analyze the combined answer of the date prompt."
  14041. ;; FIXME: cleanup and comment
  14042. (let ((nowdecode (decode-time (current-time)))
  14043. delta deltan deltaw deltadef year month day
  14044. hour minute second wday pm h2 m2 tl wday1
  14045. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  14046. (setq org-read-date-analyze-futurep nil
  14047. org-read-date-analyze-forced-year nil)
  14048. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  14049. (setq ans "+0"))
  14050. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  14051. (setq ans (replace-match "" t t ans)
  14052. deltan (car delta)
  14053. deltaw (nth 1 delta)
  14054. deltadef (nth 2 delta)))
  14055. ;; Check if there is an iso week date in there
  14056. ;; If yes, store the info and postpone interpreting it until the rest
  14057. ;; of the parsing is done
  14058. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  14059. (setq iso-year (if (match-end 1)
  14060. (org-small-year-to-year
  14061. (string-to-number (match-string 1 ans))))
  14062. iso-weekday (if (match-end 3)
  14063. (string-to-number (match-string 3 ans)))
  14064. iso-week (string-to-number (match-string 2 ans)))
  14065. (setq ans (replace-match "" t t ans)))
  14066. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  14067. (when (string-match
  14068. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  14069. (setq year (if (match-end 2)
  14070. (string-to-number (match-string 2 ans))
  14071. (progn (setq kill-year t)
  14072. (string-to-number (format-time-string "%Y"))))
  14073. month (string-to-number (match-string 3 ans))
  14074. day (string-to-number (match-string 4 ans)))
  14075. (if (< year 100) (setq year (+ 2000 year)))
  14076. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14077. t nil ans)))
  14078. ;; Help matching dotted european dates
  14079. (when (string-match
  14080. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  14081. (setq year (if (match-end 3)
  14082. (string-to-number (match-string 3 ans))
  14083. (progn (setq kill-year t)
  14084. (string-to-number (format-time-string "%Y"))))
  14085. day (string-to-number (match-string 1 ans))
  14086. month (string-to-number (match-string 2 ans))
  14087. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14088. t nil ans)))
  14089. ;; Help matching american dates, like 5/30 or 5/30/7
  14090. (when (string-match
  14091. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  14092. (setq year (if (match-end 4)
  14093. (string-to-number (match-string 4 ans))
  14094. (progn (setq kill-year t)
  14095. (string-to-number (format-time-string "%Y"))))
  14096. month (string-to-number (match-string 1 ans))
  14097. day (string-to-number (match-string 2 ans)))
  14098. (if (< year 100) (setq year (+ 2000 year)))
  14099. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  14100. t nil ans)))
  14101. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  14102. ;; If there is a time with am/pm, and *no* time without it, we convert
  14103. ;; so that matching will be successful.
  14104. (loop for i from 1 to 2 do ; twice, for end time as well
  14105. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  14106. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  14107. (setq hour (string-to-number (match-string 1 ans))
  14108. minute (if (match-end 3)
  14109. (string-to-number (match-string 3 ans))
  14110. 0)
  14111. pm (equal ?p
  14112. (string-to-char (downcase (match-string 4 ans)))))
  14113. (if (and (= hour 12) (not pm))
  14114. (setq hour 0)
  14115. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  14116. (setq ans (replace-match (format "%02d:%02d" hour minute)
  14117. t t ans))))
  14118. ;; Check if a time range is given as a duration
  14119. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  14120. (setq hour (string-to-number (match-string 1 ans))
  14121. h2 (+ hour (string-to-number (match-string 3 ans)))
  14122. minute (string-to-number (match-string 2 ans))
  14123. m2 (+ minute (if (match-end 5) (string-to-number
  14124. (match-string 5 ans))0)))
  14125. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  14126. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  14127. t t ans)))
  14128. ;; Check if there is a time range
  14129. (when (boundp 'org-end-time-was-given)
  14130. (setq org-time-was-given nil)
  14131. (when (and (string-match org-plain-time-of-day-regexp ans)
  14132. (match-end 8))
  14133. (setq org-end-time-was-given (match-string 8 ans))
  14134. (setq ans (concat (substring ans 0 (match-beginning 7))
  14135. (substring ans (match-end 7))))))
  14136. (setq tl (parse-time-string ans)
  14137. day (or (nth 3 tl) (nth 3 org-defdecode))
  14138. month (or (nth 4 tl)
  14139. (if (and org-read-date-prefer-future
  14140. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  14141. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  14142. (nth 4 org-defdecode)))
  14143. year (or (and (not kill-year) (nth 5 tl))
  14144. (if (and org-read-date-prefer-future
  14145. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  14146. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  14147. (nth 5 org-defdecode)))
  14148. hour (or (nth 2 tl) (nth 2 org-defdecode))
  14149. minute (or (nth 1 tl) (nth 1 org-defdecode))
  14150. second (or (nth 0 tl) 0)
  14151. wday (nth 6 tl))
  14152. (when (and (eq org-read-date-prefer-future 'time)
  14153. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  14154. (equal day (nth 3 nowdecode))
  14155. (equal month (nth 4 nowdecode))
  14156. (equal year (nth 5 nowdecode))
  14157. (nth 2 tl)
  14158. (or (< (nth 2 tl) (nth 2 nowdecode))
  14159. (and (= (nth 2 tl) (nth 2 nowdecode))
  14160. (nth 1 tl)
  14161. (< (nth 1 tl) (nth 1 nowdecode)))))
  14162. (setq day (1+ day)
  14163. futurep t))
  14164. ;; Special date definitions below
  14165. (cond
  14166. (iso-week
  14167. ;; There was an iso week
  14168. (require 'cal-iso)
  14169. (setq futurep nil)
  14170. (setq year (or iso-year year)
  14171. day (or iso-weekday wday 1)
  14172. wday nil ; to make sure that the trigger below does not match
  14173. iso-date (calendar-gregorian-from-absolute
  14174. (calendar-absolute-from-iso
  14175. (list iso-week day year))))
  14176. ; FIXME: Should we also push ISO weeks into the future?
  14177. ; (when (and org-read-date-prefer-future
  14178. ; (not iso-year)
  14179. ; (< (calendar-absolute-from-gregorian iso-date)
  14180. ; (time-to-days (current-time))))
  14181. ; (setq year (1+ year)
  14182. ; iso-date (calendar-gregorian-from-absolute
  14183. ; (calendar-absolute-from-iso
  14184. ; (list iso-week day year)))))
  14185. (setq month (car iso-date)
  14186. year (nth 2 iso-date)
  14187. day (nth 1 iso-date)))
  14188. (deltan
  14189. (setq futurep nil)
  14190. (unless deltadef
  14191. (let ((now (decode-time (current-time))))
  14192. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  14193. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  14194. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  14195. ((equal deltaw "m") (setq month (+ month deltan)))
  14196. ((equal deltaw "y") (setq year (+ year deltan)))))
  14197. ((and wday (not (nth 3 tl)))
  14198. (setq futurep nil)
  14199. ;; Weekday was given, but no day, so pick that day in the week
  14200. ;; on or after the derived date.
  14201. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  14202. (unless (equal wday wday1)
  14203. (setq day (+ day (% (- wday wday1 -7) 7))))))
  14204. (if (and (boundp 'org-time-was-given)
  14205. (nth 2 tl))
  14206. (setq org-time-was-given t))
  14207. (if (< year 100) (setq year (+ 2000 year)))
  14208. ;; Check of the date is representable
  14209. (if org-read-date-force-compatible-dates
  14210. (progn
  14211. (if (< year 1970)
  14212. (setq year 1970 org-read-date-analyze-forced-year t))
  14213. (if (> year 2037)
  14214. (setq year 2037 org-read-date-analyze-forced-year t)))
  14215. (condition-case nil
  14216. (ignore (encode-time second minute hour day month year))
  14217. (error
  14218. (setq year (nth 5 org-defdecode))
  14219. (setq org-read-date-analyze-forced-year t))))
  14220. (setq org-read-date-analyze-futurep futurep)
  14221. (list second minute hour day month year)))
  14222. (defvar parse-time-weekdays)
  14223. (defun org-read-date-get-relative (s today default)
  14224. "Check string S for special relative date string.
  14225. TODAY and DEFAULT are internal times, for today and for a default.
  14226. Return shift list (N what def-flag)
  14227. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  14228. N is the number of WHATs to shift.
  14229. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  14230. the DEFAULT date rather than TODAY."
  14231. (require 'parse-time)
  14232. (when (and
  14233. (string-match
  14234. (concat
  14235. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  14236. "\\([0-9]+\\)?"
  14237. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  14238. "\\([ \t]\\|$\\)") s)
  14239. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  14240. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  14241. (string-to-char (substring (match-string 1 s) -1))
  14242. ?+))
  14243. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  14244. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  14245. (what (if (match-end 3) (match-string 3 s) "d"))
  14246. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  14247. (date (if rel default today))
  14248. (wday (nth 6 (decode-time date)))
  14249. delta)
  14250. (if wday1
  14251. (progn
  14252. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  14253. (if (= dir ?-) (setq delta (- delta 7)))
  14254. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  14255. (list delta "d" rel))
  14256. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  14257. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  14258. "Turn a user-specified date into the internal representation.
  14259. The internal representation needed by the calendar is (month day year).
  14260. This is a wrapper to handle the brain-dead convention in calendar that
  14261. user function argument order change dependent on argument order."
  14262. (if (boundp 'calendar-date-style)
  14263. (cond
  14264. ((eq calendar-date-style 'american)
  14265. (list arg1 arg2 arg3))
  14266. ((eq calendar-date-style 'european)
  14267. (list arg2 arg1 arg3))
  14268. ((eq calendar-date-style 'iso)
  14269. (list arg2 arg3 arg1)))
  14270. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  14271. (if (org-bound-and-true-p european-calendar-style)
  14272. (list arg2 arg1 arg3)
  14273. (list arg1 arg2 arg3)))))
  14274. (defun org-eval-in-calendar (form &optional keepdate)
  14275. "Eval FORM in the calendar window and return to current window.
  14276. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  14277. otherwise stick to the current value of `org-ans2'."
  14278. (let ((sf (selected-frame))
  14279. (sw (selected-window)))
  14280. (select-window (get-buffer-window "*Calendar*" t))
  14281. (eval form)
  14282. (when (and (not keepdate) (calendar-cursor-to-date))
  14283. (let* ((date (calendar-cursor-to-date))
  14284. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14285. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  14286. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  14287. (select-window sw)
  14288. (org-select-frame-set-input-focus sf)))
  14289. (defun org-calendar-select ()
  14290. "Return to `org-read-date' with the date currently selected.
  14291. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14292. (interactive)
  14293. (when (calendar-cursor-to-date)
  14294. (let* ((date (calendar-cursor-to-date))
  14295. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14296. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14297. (if (active-minibuffer-window) (exit-minibuffer))))
  14298. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  14299. "Insert a date stamp for the date given by the internal TIME.
  14300. WITH-HM means use the stamp format that includes the time of the day.
  14301. INACTIVE means use square brackets instead of angular ones, so that the
  14302. stamp will not contribute to the agenda.
  14303. PRE and POST are optional strings to be inserted before and after the
  14304. stamp.
  14305. The command returns the inserted time stamp."
  14306. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  14307. stamp)
  14308. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  14309. (insert-before-markers (or pre ""))
  14310. (when (listp extra)
  14311. (setq extra (car extra))
  14312. (if (and (stringp extra)
  14313. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  14314. (setq extra (format "-%02d:%02d"
  14315. (string-to-number (match-string 1 extra))
  14316. (string-to-number (match-string 2 extra))))
  14317. (setq extra nil)))
  14318. (when extra
  14319. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  14320. (insert-before-markers (setq stamp (format-time-string fmt time)))
  14321. (insert-before-markers (or post ""))
  14322. (setq org-last-inserted-timestamp stamp)))
  14323. (defun org-toggle-time-stamp-overlays ()
  14324. "Toggle the use of custom time stamp formats."
  14325. (interactive)
  14326. (setq org-display-custom-times (not org-display-custom-times))
  14327. (unless org-display-custom-times
  14328. (let ((p (point-min)) (bmp (buffer-modified-p)))
  14329. (while (setq p (next-single-property-change p 'display))
  14330. (if (and (get-text-property p 'display)
  14331. (eq (get-text-property p 'face) 'org-date))
  14332. (remove-text-properties
  14333. p (setq p (next-single-property-change p 'display))
  14334. '(display t))))
  14335. (set-buffer-modified-p bmp)))
  14336. (if (featurep 'xemacs)
  14337. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  14338. (org-restart-font-lock)
  14339. (setq org-table-may-need-update t)
  14340. (if org-display-custom-times
  14341. (message "Time stamps are overlaid with custom format")
  14342. (message "Time stamp overlays removed")))
  14343. (defun org-display-custom-time (beg end)
  14344. "Overlay modified time stamp format over timestamp between BEG and END."
  14345. (let* ((ts (buffer-substring beg end))
  14346. t1 w1 with-hm tf time str w2 (off 0))
  14347. (save-match-data
  14348. (setq t1 (org-parse-time-string ts t))
  14349. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  14350. (setq off (- (match-end 0) (match-beginning 0)))))
  14351. (setq end (- end off))
  14352. (setq w1 (- end beg)
  14353. with-hm (and (nth 1 t1) (nth 2 t1))
  14354. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  14355. time (org-fix-decoded-time t1)
  14356. str (org-add-props
  14357. (format-time-string
  14358. (substring tf 1 -1) (apply 'encode-time time))
  14359. nil 'mouse-face 'highlight)
  14360. w2 (length str))
  14361. (if (not (= w2 w1))
  14362. (add-text-properties (1+ beg) (+ 2 beg)
  14363. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  14364. (if (featurep 'xemacs)
  14365. (progn
  14366. (put-text-property beg end 'invisible t)
  14367. (put-text-property beg end 'end-glyph (make-glyph str)))
  14368. (put-text-property beg end 'display str))))
  14369. (defun org-translate-time (string)
  14370. "Translate all timestamps in STRING to custom format.
  14371. But do this only if the variable `org-display-custom-times' is set."
  14372. (when org-display-custom-times
  14373. (save-match-data
  14374. (let* ((start 0)
  14375. (re org-ts-regexp-both)
  14376. t1 with-hm inactive tf time str beg end)
  14377. (while (setq start (string-match re string start))
  14378. (setq beg (match-beginning 0)
  14379. end (match-end 0)
  14380. t1 (save-match-data
  14381. (org-parse-time-string (substring string beg end) t))
  14382. with-hm (and (nth 1 t1) (nth 2 t1))
  14383. inactive (equal (substring string beg (1+ beg)) "[")
  14384. tf (funcall (if with-hm 'cdr 'car)
  14385. org-time-stamp-custom-formats)
  14386. time (org-fix-decoded-time t1)
  14387. str (format-time-string
  14388. (concat
  14389. (if inactive "[" "<") (substring tf 1 -1)
  14390. (if inactive "]" ">"))
  14391. (apply 'encode-time time))
  14392. string (replace-match str t t string)
  14393. start (+ start (length str)))))))
  14394. string)
  14395. (defun org-fix-decoded-time (time)
  14396. "Set 0 instead of nil for the first 6 elements of time.
  14397. Don't touch the rest."
  14398. (let ((n 0))
  14399. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  14400. (defun org-days-to-time (timestamp-string)
  14401. "Difference between TIMESTAMP-STRING and now in days."
  14402. (- (time-to-days (org-time-string-to-time timestamp-string))
  14403. (time-to-days (current-time))))
  14404. (defun org-deadline-close (timestamp-string &optional ndays)
  14405. "Is the time in TIMESTAMP-STRING close to the current date?"
  14406. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  14407. (and (< (org-days-to-time timestamp-string) ndays)
  14408. (not (org-entry-is-done-p))))
  14409. (defun org-get-wdays (ts)
  14410. "Get the deadline lead time appropriate for timestring TS."
  14411. (cond
  14412. ((<= org-deadline-warning-days 0)
  14413. ;; 0 or negative, enforce this value no matter what
  14414. (- org-deadline-warning-days))
  14415. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  14416. ;; lead time is specified.
  14417. (floor (* (string-to-number (match-string 1 ts))
  14418. (cdr (assoc (match-string 2 ts)
  14419. '(("d" . 1) ("w" . 7)
  14420. ("m" . 30.4) ("y" . 365.25)))))))
  14421. ;; go for the default.
  14422. (t org-deadline-warning-days)))
  14423. (defun org-calendar-select-mouse (ev)
  14424. "Return to `org-read-date' with the date currently selected.
  14425. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  14426. (interactive "e")
  14427. (mouse-set-point ev)
  14428. (when (calendar-cursor-to-date)
  14429. (let* ((date (calendar-cursor-to-date))
  14430. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  14431. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  14432. (if (active-minibuffer-window) (exit-minibuffer))))
  14433. (defun org-check-deadlines (ndays)
  14434. "Check if there are any deadlines due or past due.
  14435. A deadline is considered due if it happens within `org-deadline-warning-days'
  14436. days from today's date. If the deadline appears in an entry marked DONE,
  14437. it is not shown. The prefix arg NDAYS can be used to test that many
  14438. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  14439. (interactive "P")
  14440. (let* ((org-warn-days
  14441. (cond
  14442. ((equal ndays '(4)) 100000)
  14443. (ndays (prefix-numeric-value ndays))
  14444. (t (abs org-deadline-warning-days))))
  14445. (case-fold-search nil)
  14446. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  14447. (callback
  14448. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  14449. (message "%d deadlines past-due or due within %d days"
  14450. (org-occur regexp nil callback)
  14451. org-warn-days)))
  14452. (defun org-check-before-date (date)
  14453. "Check if there are deadlines or scheduled entries before DATE."
  14454. (interactive (list (org-read-date)))
  14455. (let ((case-fold-search nil)
  14456. (regexp (concat "\\<\\(" org-deadline-string
  14457. "\\|" org-scheduled-string
  14458. "\\) *<\\([^>]+\\)>"))
  14459. (callback
  14460. (lambda () (time-less-p
  14461. (org-time-string-to-time (match-string 2))
  14462. (org-time-string-to-time date)))))
  14463. (message "%d entries before %s"
  14464. (org-occur regexp nil callback) date)))
  14465. (defun org-check-after-date (date)
  14466. "Check if there are deadlines or scheduled entries after DATE."
  14467. (interactive (list (org-read-date)))
  14468. (let ((case-fold-search nil)
  14469. (regexp (concat "\\<\\(" org-deadline-string
  14470. "\\|" org-scheduled-string
  14471. "\\) *<\\([^>]+\\)>"))
  14472. (callback
  14473. (lambda () (not
  14474. (time-less-p
  14475. (org-time-string-to-time (match-string 2))
  14476. (org-time-string-to-time date))))))
  14477. (message "%d entries after %s"
  14478. (org-occur regexp nil callback) date)))
  14479. (defun org-check-dates-range (start-date end-date)
  14480. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  14481. (interactive (list (org-read-date nil nil nil "Range starts")
  14482. (org-read-date nil nil nil "Range end")))
  14483. (let ((case-fold-search nil)
  14484. (regexp (concat "\\<\\(" org-deadline-string
  14485. "\\|" org-scheduled-string
  14486. "\\) *<\\([^>]+\\)>"))
  14487. (callback
  14488. (lambda ()
  14489. (let ((match (match-string 2)))
  14490. (and
  14491. (not (time-less-p
  14492. (org-time-string-to-time match)
  14493. (org-time-string-to-time start-date)))
  14494. (time-less-p
  14495. (org-time-string-to-time match)
  14496. (org-time-string-to-time end-date)))))))
  14497. (message "%d entries between %s and %s"
  14498. (org-occur regexp nil callback) start-date end-date)))
  14499. (defun org-evaluate-time-range (&optional to-buffer)
  14500. "Evaluate a time range by computing the difference between start and end.
  14501. Normally the result is just printed in the echo area, but with prefix arg
  14502. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  14503. If the time range is actually in a table, the result is inserted into the
  14504. next column.
  14505. For time difference computation, a year is assumed to be exactly 365
  14506. days in order to avoid rounding problems."
  14507. (interactive "P")
  14508. (or
  14509. (org-clock-update-time-maybe)
  14510. (save-excursion
  14511. (unless (org-at-date-range-p t)
  14512. (goto-char (point-at-bol))
  14513. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  14514. (if (not (org-at-date-range-p t))
  14515. (error "Not at a time-stamp range, and none found in current line")))
  14516. (let* ((ts1 (match-string 1))
  14517. (ts2 (match-string 2))
  14518. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  14519. (match-end (match-end 0))
  14520. (time1 (org-time-string-to-time ts1))
  14521. (time2 (org-time-string-to-time ts2))
  14522. (t1 (org-float-time time1))
  14523. (t2 (org-float-time time2))
  14524. (diff (abs (- t2 t1)))
  14525. (negative (< (- t2 t1) 0))
  14526. ;; (ys (floor (* 365 24 60 60)))
  14527. (ds (* 24 60 60))
  14528. (hs (* 60 60))
  14529. (fy "%dy %dd %02d:%02d")
  14530. (fy1 "%dy %dd")
  14531. (fd "%dd %02d:%02d")
  14532. (fd1 "%dd")
  14533. (fh "%02d:%02d")
  14534. y d h m align)
  14535. (if havetime
  14536. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14537. y 0
  14538. d (floor (/ diff ds)) diff (mod diff ds)
  14539. h (floor (/ diff hs)) diff (mod diff hs)
  14540. m (floor (/ diff 60)))
  14541. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  14542. y 0
  14543. d (floor (+ (/ diff ds) 0.5))
  14544. h 0 m 0))
  14545. (if (not to-buffer)
  14546. (message "%s" (org-make-tdiff-string y d h m))
  14547. (if (org-at-table-p)
  14548. (progn
  14549. (goto-char match-end)
  14550. (setq align t)
  14551. (and (looking-at " *|") (goto-char (match-end 0))))
  14552. (goto-char match-end))
  14553. (if (looking-at
  14554. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  14555. (replace-match ""))
  14556. (if negative (insert " -"))
  14557. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  14558. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  14559. (insert " " (format fh h m))))
  14560. (if align (org-table-align))
  14561. (message "Time difference inserted")))))
  14562. (defun org-make-tdiff-string (y d h m)
  14563. (let ((fmt "")
  14564. (l nil))
  14565. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  14566. l (push y l)))
  14567. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  14568. l (push d l)))
  14569. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  14570. l (push h l)))
  14571. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  14572. l (push m l)))
  14573. (apply 'format fmt (nreverse l))))
  14574. (defun org-time-string-to-time (s &optional buffer pos)
  14575. (condition-case errdata
  14576. (apply 'encode-time (org-parse-time-string s))
  14577. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14578. s (if (not (and buffer pos))
  14579. ""
  14580. (format " at %d in buffer `%s'" pos buffer))
  14581. (cdr errdata)))))
  14582. (defun org-time-string-to-seconds (s)
  14583. (org-float-time (org-time-string-to-time s)))
  14584. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  14585. "Convert a time stamp to an absolute day number.
  14586. If there is a specifier for a cyclic time stamp, get the closest date to
  14587. DAYNR.
  14588. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  14589. The variable date is bound by the calendar when this is called."
  14590. (cond
  14591. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  14592. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  14593. daynr
  14594. (+ daynr 1000)))
  14595. ((and daynr (string-match "\\+[0-9]+[hdwmy]" s))
  14596. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  14597. (time-to-days (current-time))) (match-string 0 s)
  14598. prefer show-all))
  14599. (t (time-to-days
  14600. (condition-case errdata
  14601. (apply 'encode-time (org-parse-time-string s))
  14602. (error (error "Bad timestamp `%s'%s\nError was: %s"
  14603. s (if (not (and buffer pos))
  14604. ""
  14605. (format " at %d in buffer `%s'" pos buffer))
  14606. (cdr errdata))))))))
  14607. (defun org-days-to-iso-week (days)
  14608. "Return the iso week number."
  14609. (require 'cal-iso)
  14610. (car (calendar-iso-from-absolute days)))
  14611. (defun org-small-year-to-year (year)
  14612. "Convert 2-digit years into 4-digit years.
  14613. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  14614. The year 2000 cannot be abbreviated. Any year larger than 99
  14615. is returned unchanged."
  14616. (if (< year 38)
  14617. (setq year (+ 2000 year))
  14618. (if (< year 100)
  14619. (setq year (+ 1900 year))))
  14620. year)
  14621. (defun org-time-from-absolute (d)
  14622. "Return the time corresponding to date D.
  14623. D may be an absolute day number, or a calendar-type list (month day year)."
  14624. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  14625. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  14626. (defun org-calendar-holiday ()
  14627. "List of holidays, for Diary display in Org-mode."
  14628. (require 'holidays)
  14629. (let ((hl (funcall
  14630. (if (fboundp 'calendar-check-holidays)
  14631. 'calendar-check-holidays 'check-calendar-holidays) date)))
  14632. (if hl (mapconcat 'identity hl "; "))))
  14633. (defun org-diary-sexp-entry (sexp entry date)
  14634. "Process a SEXP diary ENTRY for DATE."
  14635. (require 'diary-lib)
  14636. (let ((result (if calendar-debug-sexp
  14637. (let ((stack-trace-on-error t))
  14638. (eval (car (read-from-string sexp))))
  14639. (condition-case nil
  14640. (eval (car (read-from-string sexp)))
  14641. (error
  14642. (beep)
  14643. (message "Bad sexp at line %d in %s: %s"
  14644. (org-current-line)
  14645. (buffer-file-name) sexp)
  14646. (sleep-for 2))))))
  14647. (cond ((stringp result) (split-string result "; "))
  14648. ((and (consp result)
  14649. (not (consp (cdr result)))
  14650. (stringp (cdr result))) (cdr result))
  14651. ((and (consp result)
  14652. (stringp (car result))) result)
  14653. (result entry)
  14654. (t nil))))
  14655. (defun org-diary-to-ical-string (frombuf)
  14656. "Get iCalendar entries from diary entries in buffer FROMBUF.
  14657. This uses the icalendar.el library."
  14658. (let* ((tmpdir (if (featurep 'xemacs)
  14659. (temp-directory)
  14660. temporary-file-directory))
  14661. (tmpfile (make-temp-name
  14662. (expand-file-name "orgics" tmpdir)))
  14663. buf rtn b e)
  14664. (with-current-buffer frombuf
  14665. (icalendar-export-region (point-min) (point-max) tmpfile)
  14666. (setq buf (find-buffer-visiting tmpfile))
  14667. (set-buffer buf)
  14668. (goto-char (point-min))
  14669. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  14670. (setq b (match-beginning 0)))
  14671. (goto-char (point-max))
  14672. (if (re-search-backward "^END:VEVENT" nil t)
  14673. (setq e (match-end 0)))
  14674. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  14675. (kill-buffer buf)
  14676. (delete-file tmpfile)
  14677. rtn))
  14678. (defun org-closest-date (start current change prefer show-all)
  14679. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  14680. When PREFER is `past', return a date that is either CURRENT or past.
  14681. When PREFER is `future', return a date that is either CURRENT or future.
  14682. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  14683. ;; Make the proper lists from the dates
  14684. (catch 'exit
  14685. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  14686. dn dw sday cday n1 n2 n0
  14687. d m y y1 y2 date1 date2 nmonths nm ny m2)
  14688. (setq start (org-date-to-gregorian start)
  14689. current (org-date-to-gregorian
  14690. (if show-all
  14691. current
  14692. (time-to-days (current-time))))
  14693. sday (calendar-absolute-from-gregorian start)
  14694. cday (calendar-absolute-from-gregorian current))
  14695. (if (<= cday sday) (throw 'exit sday))
  14696. (if (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  14697. (setq dn (string-to-number (match-string 1 change))
  14698. dw (cdr (assoc (match-string 2 change) a1)))
  14699. (error "Invalid change specifier: %s" change))
  14700. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  14701. (cond
  14702. ((eq dw 'day)
  14703. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  14704. n2 (+ n1 dn)))
  14705. ((eq dw 'year)
  14706. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  14707. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  14708. (setq date1 (list m d y1)
  14709. n1 (calendar-absolute-from-gregorian date1)
  14710. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  14711. n2 (calendar-absolute-from-gregorian date2)))
  14712. ((eq dw 'month)
  14713. ;; approx number of month between the two dates
  14714. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  14715. ;; How often does dn fit in there?
  14716. (setq d (nth 1 start) m (car start) y (nth 2 start)
  14717. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  14718. m (+ m nm)
  14719. ny (floor (/ m 12))
  14720. y (+ y ny)
  14721. m (- m (* ny 12)))
  14722. (while (> m 12) (setq m (- m 12) y (1+ y)))
  14723. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  14724. (setq m2 (+ m dn) y2 y)
  14725. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14726. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  14727. (while (<= n2 cday)
  14728. (setq n1 n2 m m2 y y2)
  14729. (setq m2 (+ m dn) y2 y)
  14730. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14731. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14732. ;; Make sure n1 is the earlier date
  14733. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14734. (if show-all
  14735. (cond
  14736. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14737. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14738. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14739. (cond
  14740. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14741. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14742. (t (if (= cday n1) n1 n2)))))))
  14743. (defun org-date-to-gregorian (date)
  14744. "Turn any specification of DATE into a Gregorian date for the calendar."
  14745. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14746. ((and (listp date) (= (length date) 3)) date)
  14747. ((stringp date)
  14748. (setq date (org-parse-time-string date))
  14749. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14750. ((listp date)
  14751. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14752. (defun org-parse-time-string (s &optional nodefault)
  14753. "Parse the standard Org-mode time string.
  14754. This should be a lot faster than the normal `parse-time-string'.
  14755. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14756. hour and minute fields will be nil if not given."
  14757. (if (string-match org-ts-regexp0 s)
  14758. (list 0
  14759. (if (or (match-beginning 8) (not nodefault))
  14760. (string-to-number (or (match-string 8 s) "0")))
  14761. (if (or (match-beginning 7) (not nodefault))
  14762. (string-to-number (or (match-string 7 s) "0")))
  14763. (string-to-number (match-string 4 s))
  14764. (string-to-number (match-string 3 s))
  14765. (string-to-number (match-string 2 s))
  14766. nil nil nil)
  14767. (error "Not a standard Org-mode time string: %s" s)))
  14768. (defun org-timestamp-up (&optional arg)
  14769. "Increase the date item at the cursor by one.
  14770. If the cursor is on the year, change the year. If it is on the month,
  14771. the day or the time, change that.
  14772. With prefix ARG, change by that many units."
  14773. (interactive "p")
  14774. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14775. (defun org-timestamp-down (&optional arg)
  14776. "Decrease the date item at the cursor by one.
  14777. If the cursor is on the year, change the year. If it is on the month,
  14778. the day or the time, change that.
  14779. With prefix ARG, change by that many units."
  14780. (interactive "p")
  14781. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14782. (defun org-timestamp-up-day (&optional arg)
  14783. "Increase the date in the time stamp by one day.
  14784. With prefix ARG, change that many days."
  14785. (interactive "p")
  14786. (if (and (not (org-at-timestamp-p t))
  14787. (org-at-heading-p))
  14788. (org-todo 'up)
  14789. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14790. (defun org-timestamp-down-day (&optional arg)
  14791. "Decrease the date in the time stamp by one day.
  14792. With prefix ARG, change that many days."
  14793. (interactive "p")
  14794. (if (and (not (org-at-timestamp-p t))
  14795. (org-at-heading-p))
  14796. (org-todo 'down)
  14797. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14798. (defun org-at-timestamp-p (&optional inactive-ok)
  14799. "Determine if the cursor is in or at a timestamp."
  14800. (interactive)
  14801. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14802. (pos (point))
  14803. (ans (or (looking-at tsr)
  14804. (save-excursion
  14805. (skip-chars-backward "^[<\n\r\t")
  14806. (if (> (point) (point-min)) (backward-char 1))
  14807. (and (looking-at tsr)
  14808. (> (- (match-end 0) pos) -1))))))
  14809. (and ans
  14810. (boundp 'org-ts-what)
  14811. (setq org-ts-what
  14812. (cond
  14813. ((= pos (match-beginning 0)) 'bracket)
  14814. ;; Point is considered to be "on the bracket" whether
  14815. ;; it's really on it or right after it.
  14816. ((= pos (1- (match-end 0))) 'bracket)
  14817. ((= pos (match-end 0)) 'after)
  14818. ((org-pos-in-match-range pos 2) 'year)
  14819. ((org-pos-in-match-range pos 3) 'month)
  14820. ((org-pos-in-match-range pos 7) 'hour)
  14821. ((org-pos-in-match-range pos 8) 'minute)
  14822. ((or (org-pos-in-match-range pos 4)
  14823. (org-pos-in-match-range pos 5)) 'day)
  14824. ((and (> pos (or (match-end 8) (match-end 5)))
  14825. (< pos (match-end 0)))
  14826. (- pos (or (match-end 8) (match-end 5))))
  14827. (t 'day))))
  14828. ans))
  14829. (defun org-toggle-timestamp-type ()
  14830. "Toggle the type (<active> or [inactive]) of a time stamp."
  14831. (interactive)
  14832. (when (org-at-timestamp-p t)
  14833. (let ((beg (match-beginning 0)) (end (match-end 0))
  14834. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14835. (save-excursion
  14836. (goto-char beg)
  14837. (while (re-search-forward "[][<>]" end t)
  14838. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14839. t t)))
  14840. (message "Timestamp is now %sactive"
  14841. (if (equal (char-after beg) ?<) "" "in")))))
  14842. (defvar org-clock-history) ; defined in org-clock.el
  14843. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  14844. (defun org-timestamp-change (n &optional what updown)
  14845. "Change the date in the time stamp at point.
  14846. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14847. `year', `minute', `second'. If WHAT is not given, the cursor position
  14848. in the timestamp determines what will be changed."
  14849. (let ((origin (point)) origin-cat
  14850. with-hm inactive
  14851. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14852. org-ts-what
  14853. extra rem
  14854. ts time time0 fixnext clrgx)
  14855. (if (not (org-at-timestamp-p t))
  14856. (error "Not at a timestamp"))
  14857. (if (and (not what) (eq org-ts-what 'bracket))
  14858. (org-toggle-timestamp-type)
  14859. ;; Point isn't on brackets. Remember the part of the time-stamp
  14860. ;; the point was in. Indeed, size of time-stamps may change,
  14861. ;; but point must be kept in the same category nonetheless.
  14862. (setq origin-cat org-ts-what)
  14863. (if (and (not what) (not (eq org-ts-what 'day))
  14864. org-display-custom-times
  14865. (get-text-property (point) 'display)
  14866. (not (get-text-property (1- (point)) 'display)))
  14867. (setq org-ts-what 'day))
  14868. (setq org-ts-what (or what org-ts-what)
  14869. inactive (= (char-after (match-beginning 0)) ?\[)
  14870. ts (match-string 0))
  14871. (replace-match "")
  14872. (if (string-match
  14873. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  14874. ts)
  14875. (setq extra (match-string 1 ts)))
  14876. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14877. (setq with-hm t))
  14878. (setq time0 (org-parse-time-string ts))
  14879. (when (and updown
  14880. (eq org-ts-what 'minute)
  14881. (not current-prefix-arg))
  14882. ;; This looks like s-up and s-down. Change by one rounding step.
  14883. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14884. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14885. (setcar (cdr time0) (+ (nth 1 time0)
  14886. (if (> n 0) (- rem) (- dm rem))))))
  14887. (setq time
  14888. (encode-time (or (car time0) 0)
  14889. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14890. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14891. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14892. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14893. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14894. (nthcdr 6 time0)))
  14895. (when (and (member org-ts-what '(hour minute))
  14896. extra
  14897. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14898. (setq extra (org-modify-ts-extra
  14899. extra
  14900. (if (eq org-ts-what 'hour) 2 5)
  14901. n dm)))
  14902. (when (integerp org-ts-what)
  14903. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14904. (if (eq what 'calendar)
  14905. (let ((cal-date (org-get-date-from-calendar)))
  14906. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14907. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14908. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14909. (setcar time0 (or (car time0) 0))
  14910. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14911. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14912. (setq time (apply 'encode-time time0))))
  14913. ;; Insert the new time-stamp, and ensure point stays in the same
  14914. ;; category as before (i.e. not after the last position in that
  14915. ;; category).
  14916. (let ((pos (point)))
  14917. ;; Stay before inserted string. `save-excursion' is of no use.
  14918. (setq org-last-changed-timestamp
  14919. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14920. (goto-char pos))
  14921. (save-match-data
  14922. (looking-at org-ts-regexp3)
  14923. (goto-char (cond
  14924. ;; `day' category ends before `hour' if any, or at
  14925. ;; the end of the day name.
  14926. ((eq origin-cat 'day)
  14927. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  14928. ((eq origin-cat 'hour) (min (match-end 7) origin))
  14929. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  14930. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  14931. ;; `year' and `month' have both fixed size: point
  14932. ;; couldn't have moved into another part.
  14933. (t origin))))
  14934. ;; Update clock if on a CLOCK line.
  14935. (org-clock-update-time-maybe)
  14936. ;; Maybe adjust the closest clock in `org-clock-history'
  14937. (if (not (and org-clock-adjust-closest
  14938. (org-at-clock-log-p)
  14939. (< 1 (length (delq nil (mapcar (lambda(m) (marker-position m))
  14940. org-clock-history))))))
  14941. (message "No clock to adjust")
  14942. (cond ((save-excursion ; fix previous clock?
  14943. (re-search-backward org-ts-regexp0 nil t)
  14944. (looking-back (concat org-clock-string " \\[")))
  14945. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  14946. ((save-excursion ; fix next clock?
  14947. (re-search-backward org-ts-regexp0 nil t)
  14948. (looking-at (concat org-ts-regexp0 "\\] =>")))
  14949. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  14950. (save-excursion
  14951. ;; Find closest clock to point, adjust the previous/next one
  14952. (org-back-to-heading t)
  14953. (let* ((cl (mapcar (lambda(c) (abs (- (marker-position c) (point))))
  14954. org-clock-history))
  14955. (clfixnth
  14956. (+ fixnext (- (length cl) (or (length (member (apply #'min cl) cl)) 100))))
  14957. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  14958. (if (not clfixpos)
  14959. (message "No clock to adjust")
  14960. (goto-char clfixpos)
  14961. (org-show-subtree)
  14962. (when (re-search-forward clrgx nil t)
  14963. (goto-char (match-beginning 1))
  14964. (let (org-clock-adjust-closest)
  14965. (org-timestamp-change n org-ts-what updown))
  14966. (message "Clock adjusted for heading: %s" (org-get-heading t t)))))))
  14967. ;; Try to recenter the calendar window, if any.
  14968. (if (and org-calendar-follow-timestamp-change
  14969. (get-buffer-window "*Calendar*" t)
  14970. (memq org-ts-what '(day month year)))
  14971. (org-recenter-calendar (time-to-days time))))))
  14972. (defun org-modify-ts-extra (s pos n dm)
  14973. "Change the different parts of the lead-time and repeat fields in timestamp."
  14974. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14975. ng h m new rem)
  14976. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14977. (cond
  14978. ((or (org-pos-in-match-range pos 2)
  14979. (org-pos-in-match-range pos 3))
  14980. (setq m (string-to-number (match-string 3 s))
  14981. h (string-to-number (match-string 2 s)))
  14982. (if (org-pos-in-match-range pos 2)
  14983. (setq h (+ h n))
  14984. (setq n (* dm (org-no-warnings (signum n))))
  14985. (when (not (= 0 (setq rem (% m dm))))
  14986. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14987. (setq m (+ m n)))
  14988. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14989. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14990. (setq h (min 24 (max 0 h)))
  14991. (setq ng 1 new (format "-%02d:%02d" h m)))
  14992. ((org-pos-in-match-range pos 6)
  14993. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14994. ((org-pos-in-match-range pos 5)
  14995. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14996. ((org-pos-in-match-range pos 9)
  14997. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14998. ((org-pos-in-match-range pos 8)
  14999. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  15000. (when ng
  15001. (setq s (concat
  15002. (substring s 0 (match-beginning ng))
  15003. new
  15004. (substring s (match-end ng))))))
  15005. s))
  15006. (defun org-recenter-calendar (date)
  15007. "If the calendar is visible, recenter it to DATE."
  15008. (let ((cwin (get-buffer-window "*Calendar*" t)))
  15009. (when cwin
  15010. (let ((calendar-move-hook nil))
  15011. (with-selected-window cwin
  15012. (calendar-goto-date (if (listp date) date
  15013. (calendar-gregorian-from-absolute date))))))))
  15014. (defun org-goto-calendar (&optional arg)
  15015. "Go to the Emacs calendar at the current date.
  15016. If there is a time stamp in the current line, go to that date.
  15017. A prefix ARG can be used to force the current date."
  15018. (interactive "P")
  15019. (let ((tsr org-ts-regexp) diff
  15020. (calendar-move-hook nil)
  15021. (calendar-view-holidays-initially-flag nil)
  15022. (calendar-view-diary-initially-flag nil))
  15023. (if (or (org-at-timestamp-p)
  15024. (save-excursion
  15025. (beginning-of-line 1)
  15026. (looking-at (concat ".*" tsr))))
  15027. (let ((d1 (time-to-days (current-time)))
  15028. (d2 (time-to-days
  15029. (org-time-string-to-time (match-string 1)))))
  15030. (setq diff (- d2 d1))))
  15031. (calendar)
  15032. (calendar-goto-today)
  15033. (if (and diff (not arg)) (calendar-forward-day diff))))
  15034. (defun org-get-date-from-calendar ()
  15035. "Return a list (month day year) of date at point in calendar."
  15036. (with-current-buffer "*Calendar*"
  15037. (save-match-data
  15038. (calendar-cursor-to-date))))
  15039. (defun org-date-from-calendar ()
  15040. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  15041. If there is already a time stamp at the cursor position, update it."
  15042. (interactive)
  15043. (if (org-at-timestamp-p t)
  15044. (org-timestamp-change 0 'calendar)
  15045. (let ((cal-date (org-get-date-from-calendar)))
  15046. (org-insert-time-stamp
  15047. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  15048. (defun org-minutes-to-hh:mm-string (m)
  15049. "Compute H:MM from a number of minutes."
  15050. (let ((h (/ m 60)))
  15051. (setq m (- m (* 60 h)))
  15052. (format org-time-clocksum-format h m)))
  15053. (defun org-hh:mm-string-to-minutes (s)
  15054. "Convert a string H:MM to a number of minutes.
  15055. If the string is just a number, interpret it as minutes.
  15056. In fact, the first hh:mm or number in the string will be taken,
  15057. there can be extra stuff in the string.
  15058. If no number is found, the return value is 0."
  15059. (cond
  15060. ((integerp s) s)
  15061. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  15062. (+ (* (string-to-number (match-string 1 s)) 60)
  15063. (string-to-number (match-string 2 s))))
  15064. ((string-match "\\([0-9]+\\)" s)
  15065. (string-to-number (match-string 1 s)))
  15066. (t 0)))
  15067. (defcustom org-effort-durations
  15068. `(("h" . 60)
  15069. ("d" . ,(* 60 8))
  15070. ("w" . ,(* 60 8 5))
  15071. ("m" . ,(* 60 8 5 4))
  15072. ("y" . ,(* 60 8 5 40)))
  15073. "Conversion factor to minutes for an effort modifier.
  15074. Each entry has the form (MODIFIER . MINUTES).
  15075. In an effort string, a number followed by MODIFIER is multiplied
  15076. by the specified number of MINUTES to obtain an effort in
  15077. minutes.
  15078. For example, if the value of this variable is ((\"hours\" . 60)), then an
  15079. effort string \"2hours\" is equivalent to 120 minutes."
  15080. :group 'org-agenda
  15081. :version "24.1"
  15082. :type '(alist :key-type (string :tag "Modifier")
  15083. :value-type (number :tag "Minutes")))
  15084. (defun org-duration-string-to-minutes (s &optional output-to-string)
  15085. "Convert a duration string S to minutes.
  15086. A bare number is interpreted as minutes, modifiers can be set by
  15087. customizing `org-effort-durations' (which see).
  15088. Entries containing a colon are interpreted as H:MM by
  15089. `org-hh:mm-string-to-minutes'."
  15090. (let ((result 0)
  15091. (re (concat "\\([0-9.]+\\) *\\("
  15092. (regexp-opt (mapcar 'car org-effort-durations))
  15093. "\\)")))
  15094. (while (string-match re s)
  15095. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  15096. (string-to-number (match-string 1 s))))
  15097. (setq s (replace-match "" nil t s)))
  15098. (setq result (floor result))
  15099. (incf result (org-hh:mm-string-to-minutes s))
  15100. (if output-to-string (number-to-string result) result)))
  15101. ;;;; Files
  15102. (defun org-save-all-org-buffers ()
  15103. "Save all Org-mode buffers without user confirmation."
  15104. (interactive)
  15105. (message "Saving all Org-mode buffers...")
  15106. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  15107. (when (featurep 'org-id) (org-id-locations-save))
  15108. (message "Saving all Org-mode buffers... done"))
  15109. (defun org-revert-all-org-buffers ()
  15110. "Revert all Org-mode buffers.
  15111. Prompt for confirmation when there are unsaved changes.
  15112. Be sure you know what you are doing before letting this function
  15113. overwrite your changes.
  15114. This function is useful in a setup where one tracks org files
  15115. with a version control system, to revert on one machine after pulling
  15116. changes from another. I believe the procedure must be like this:
  15117. 1. M-x org-save-all-org-buffers
  15118. 2. Pull changes from the other machine, resolve conflicts
  15119. 3. M-x org-revert-all-org-buffers"
  15120. (interactive)
  15121. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  15122. (error "Abort"))
  15123. (save-excursion
  15124. (save-window-excursion
  15125. (mapc
  15126. (lambda (b)
  15127. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  15128. (with-current-buffer b buffer-file-name))
  15129. (org-pop-to-buffer-same-window b)
  15130. (revert-buffer t 'no-confirm)))
  15131. (buffer-list))
  15132. (when (and (featurep 'org-id) org-id-track-globally)
  15133. (org-id-locations-load)))))
  15134. ;;;; Agenda files
  15135. ;;;###autoload
  15136. (defun org-switchb (&optional arg)
  15137. "Switch between Org buffers.
  15138. With one prefix argument, restrict available buffers to files.
  15139. With two prefix arguments, restrict available buffers to agenda files.
  15140. Defaults to `iswitchb' for buffer name completion.
  15141. Set `org-completion-use-ido' to make it use ido instead."
  15142. (interactive "P")
  15143. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  15144. ((equal arg '(16)) (org-buffer-list 'agenda))
  15145. (t (org-buffer-list))))
  15146. (org-completion-use-iswitchb org-completion-use-iswitchb)
  15147. (org-completion-use-ido org-completion-use-ido))
  15148. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  15149. (setq org-completion-use-iswitchb t))
  15150. (org-pop-to-buffer-same-window
  15151. (org-icompleting-read "Org buffer: "
  15152. (mapcar 'list (mapcar 'buffer-name blist))
  15153. nil t))))
  15154. ;;; Define some older names previously used for this functionality
  15155. ;;;###autoload
  15156. (defalias 'org-ido-switchb 'org-switchb)
  15157. ;;;###autoload
  15158. (defalias 'org-iswitchb 'org-switchb)
  15159. (defun org-buffer-list (&optional predicate exclude-tmp)
  15160. "Return a list of Org buffers.
  15161. PREDICATE can be `export', `files' or `agenda'.
  15162. export restrict the list to Export buffers.
  15163. files restrict the list to buffers visiting Org files.
  15164. agenda restrict the list to buffers visiting agenda files.
  15165. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  15166. (let* ((bfn nil)
  15167. (agenda-files (and (eq predicate 'agenda)
  15168. (mapcar 'file-truename (org-agenda-files t))))
  15169. (filter
  15170. (cond
  15171. ((eq predicate 'files)
  15172. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  15173. ((eq predicate 'export)
  15174. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  15175. ((eq predicate 'agenda)
  15176. (lambda (b)
  15177. (with-current-buffer b
  15178. (and (derived-mode-p 'org-mode)
  15179. (setq bfn (buffer-file-name b))
  15180. (member (file-truename bfn) agenda-files)))))
  15181. (t (lambda (b) (with-current-buffer b
  15182. (or (derived-mode-p 'org-mode)
  15183. (string-match "\*Org .*Export"
  15184. (buffer-name b)))))))))
  15185. (delq nil
  15186. (mapcar
  15187. (lambda(b)
  15188. (if (and (funcall filter b)
  15189. (or (not exclude-tmp)
  15190. (not (string-match "tmp" (buffer-name b)))))
  15191. b
  15192. nil))
  15193. (buffer-list)))))
  15194. (defun org-agenda-files (&optional unrestricted archives)
  15195. "Get the list of agenda files.
  15196. Optional UNRESTRICTED means return the full list even if a restriction
  15197. is currently in place.
  15198. When ARCHIVES is t, include all archive files that are really being
  15199. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  15200. `org-agenda-archives-mode' is t."
  15201. (let ((files
  15202. (cond
  15203. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  15204. ((stringp org-agenda-files) (org-read-agenda-file-list))
  15205. ((listp org-agenda-files) org-agenda-files)
  15206. (t (error "Invalid value of `org-agenda-files'")))))
  15207. (setq files (apply 'append
  15208. (mapcar (lambda (f)
  15209. (if (file-directory-p f)
  15210. (directory-files
  15211. f t org-agenda-file-regexp)
  15212. (list f)))
  15213. files)))
  15214. (when org-agenda-skip-unavailable-files
  15215. (setq files (delq nil
  15216. (mapcar (function
  15217. (lambda (file)
  15218. (and (file-readable-p file) file)))
  15219. files))))
  15220. (when (or (eq archives t)
  15221. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  15222. (setq files (org-add-archive-files files)))
  15223. files))
  15224. (defun org-agenda-file-p (&optional file)
  15225. "Return non-nil, if FILE is an agenda file.
  15226. If FILE is omitted, use the file associated with the current
  15227. buffer."
  15228. (member (or file (buffer-file-name))
  15229. (org-agenda-files t)))
  15230. (defun org-edit-agenda-file-list ()
  15231. "Edit the list of agenda files.
  15232. Depending on setup, this either uses customize to edit the variable
  15233. `org-agenda-files', or it visits the file that is holding the list. In the
  15234. latter case, the buffer is set up in a way that saving it automatically kills
  15235. the buffer and restores the previous window configuration."
  15236. (interactive)
  15237. (if (stringp org-agenda-files)
  15238. (let ((cw (current-window-configuration)))
  15239. (find-file org-agenda-files)
  15240. (org-set-local 'org-window-configuration cw)
  15241. (org-add-hook 'after-save-hook
  15242. (lambda ()
  15243. (set-window-configuration
  15244. (prog1 org-window-configuration
  15245. (kill-buffer (current-buffer))))
  15246. (org-install-agenda-files-menu)
  15247. (message "New agenda file list installed"))
  15248. nil 'local)
  15249. (message "%s" (substitute-command-keys
  15250. "Edit list and finish with \\[save-buffer]")))
  15251. (customize-variable 'org-agenda-files)))
  15252. (defun org-store-new-agenda-file-list (list)
  15253. "Set new value for the agenda file list and save it correctly."
  15254. (if (stringp org-agenda-files)
  15255. (let ((fe (org-read-agenda-file-list t)) b u)
  15256. (while (setq b (find-buffer-visiting org-agenda-files))
  15257. (kill-buffer b))
  15258. (with-temp-file org-agenda-files
  15259. (insert
  15260. (mapconcat
  15261. (lambda (f) ;; Keep un-expanded entries.
  15262. (if (setq u (assoc f fe))
  15263. (cdr u)
  15264. f))
  15265. list "\n")
  15266. "\n")))
  15267. (let ((org-mode-hook nil) (org-inhibit-startup t)
  15268. (org-insert-mode-line-in-empty-file nil))
  15269. (setq org-agenda-files list)
  15270. (customize-save-variable 'org-agenda-files org-agenda-files))))
  15271. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  15272. "Read the list of agenda files from a file.
  15273. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  15274. filenames, used by `org-store-new-agenda-file-list' to write back
  15275. un-expanded file names."
  15276. (when (file-directory-p org-agenda-files)
  15277. (error "`org-agenda-files' cannot be a single directory"))
  15278. (when (stringp org-agenda-files)
  15279. (with-temp-buffer
  15280. (insert-file-contents org-agenda-files)
  15281. (mapcar
  15282. (lambda (f)
  15283. (let ((e (expand-file-name (substitute-in-file-name f)
  15284. org-directory)))
  15285. (if pair-with-expansion
  15286. (cons e f)
  15287. e)))
  15288. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  15289. ;;;###autoload
  15290. (defun org-cycle-agenda-files ()
  15291. "Cycle through the files in `org-agenda-files'.
  15292. If the current buffer visits an agenda file, find the next one in the list.
  15293. If the current buffer does not, find the first agenda file."
  15294. (interactive)
  15295. (let* ((fs (org-agenda-files t))
  15296. (files (append fs (list (car fs))))
  15297. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  15298. file)
  15299. (unless files (error "No agenda files"))
  15300. (catch 'exit
  15301. (while (setq file (pop files))
  15302. (if (equal (file-truename file) tcf)
  15303. (when (car files)
  15304. (find-file (car files))
  15305. (throw 'exit t))))
  15306. (find-file (car fs)))
  15307. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  15308. (defun org-agenda-file-to-front (&optional to-end)
  15309. "Move/add the current file to the top of the agenda file list.
  15310. If the file is not present in the list, it is added to the front. If it is
  15311. present, it is moved there. With optional argument TO-END, add/move to the
  15312. end of the list."
  15313. (interactive "P")
  15314. (let ((org-agenda-skip-unavailable-files nil)
  15315. (file-alist (mapcar (lambda (x)
  15316. (cons (file-truename x) x))
  15317. (org-agenda-files t)))
  15318. (ctf (file-truename buffer-file-name))
  15319. x had)
  15320. (setq x (assoc ctf file-alist) had x)
  15321. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  15322. (if to-end
  15323. (setq file-alist (append (delq x file-alist) (list x)))
  15324. (setq file-alist (cons x (delq x file-alist))))
  15325. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  15326. (org-install-agenda-files-menu)
  15327. (message "File %s to %s of agenda file list"
  15328. (if had "moved" "added") (if to-end "end" "front"))))
  15329. (defun org-remove-file (&optional file)
  15330. "Remove current file from the list of files in variable `org-agenda-files'.
  15331. These are the files which are being checked for agenda entries.
  15332. Optional argument FILE means use this file instead of the current."
  15333. (interactive)
  15334. (let* ((org-agenda-skip-unavailable-files nil)
  15335. (file (or file buffer-file-name))
  15336. (true-file (file-truename file))
  15337. (afile (abbreviate-file-name file))
  15338. (files (delq nil (mapcar
  15339. (lambda (x)
  15340. (if (equal true-file
  15341. (file-truename x))
  15342. nil x))
  15343. (org-agenda-files t)))))
  15344. (if (not (= (length files) (length (org-agenda-files t))))
  15345. (progn
  15346. (org-store-new-agenda-file-list files)
  15347. (org-install-agenda-files-menu)
  15348. (message "Removed file: %s" afile))
  15349. (message "File was not in list: %s (not removed)" afile))))
  15350. (defun org-file-menu-entry (file)
  15351. (vector file (list 'find-file file) t))
  15352. (defun org-check-agenda-file (file)
  15353. "Make sure FILE exists. If not, ask user what to do."
  15354. (when (not (file-exists-p file))
  15355. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  15356. (abbreviate-file-name file))
  15357. (let ((r (downcase (read-char-exclusive))))
  15358. (cond
  15359. ((equal r ?r)
  15360. (org-remove-file file)
  15361. (throw 'nextfile t))
  15362. (t (error "Abort"))))))
  15363. (defun org-get-agenda-file-buffer (file)
  15364. "Get a buffer visiting FILE. If the buffer needs to be created, add
  15365. it to the list of buffers which might be released later."
  15366. (let ((buf (org-find-base-buffer-visiting file)))
  15367. (if buf
  15368. buf ; just return it
  15369. ;; Make a new buffer and remember it
  15370. (setq buf (find-file-noselect file))
  15371. (if buf (push buf org-agenda-new-buffers))
  15372. buf)))
  15373. (defun org-release-buffers (blist)
  15374. "Release all buffers in list, asking the user for confirmation when needed.
  15375. When a buffer is unmodified, it is just killed. When modified, it is saved
  15376. \(if the user agrees) and then killed."
  15377. (let (buf file)
  15378. (while (setq buf (pop blist))
  15379. (setq file (buffer-file-name buf))
  15380. (when (and (buffer-modified-p buf)
  15381. file
  15382. (y-or-n-p (format "Save file %s? " file)))
  15383. (with-current-buffer buf (save-buffer)))
  15384. (kill-buffer buf))))
  15385. (defun org-prepare-agenda-buffers (files)
  15386. "Create buffers for all agenda files, protect archived trees and comments."
  15387. (interactive)
  15388. (let ((pa '(:org-archived t))
  15389. (pc '(:org-comment t))
  15390. (pall '(:org-archived t :org-comment t))
  15391. (inhibit-read-only t)
  15392. (rea (concat ":" org-archive-tag ":"))
  15393. bmp file re)
  15394. (save-excursion
  15395. (save-restriction
  15396. (while (setq file (pop files))
  15397. (catch 'nextfile
  15398. (if (bufferp file)
  15399. (set-buffer file)
  15400. (org-check-agenda-file file)
  15401. (set-buffer (org-get-agenda-file-buffer file)))
  15402. (widen)
  15403. (setq bmp (buffer-modified-p))
  15404. (org-refresh-category-properties)
  15405. (setq org-todo-keywords-for-agenda
  15406. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  15407. (setq org-done-keywords-for-agenda
  15408. (append org-done-keywords-for-agenda org-done-keywords))
  15409. (setq org-todo-keyword-alist-for-agenda
  15410. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  15411. (setq org-drawers-for-agenda
  15412. (append org-drawers-for-agenda org-drawers))
  15413. (setq org-tag-alist-for-agenda
  15414. (append org-tag-alist-for-agenda org-tag-alist))
  15415. (save-excursion
  15416. (remove-text-properties (point-min) (point-max) pall)
  15417. (when org-agenda-skip-archived-trees
  15418. (goto-char (point-min))
  15419. (while (re-search-forward rea nil t)
  15420. (if (org-at-heading-p t)
  15421. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  15422. (goto-char (point-min))
  15423. (setq re (format org-heading-keyword-regexp-format
  15424. org-comment-string))
  15425. (while (re-search-forward re nil t)
  15426. (add-text-properties
  15427. (match-beginning 0) (org-end-of-subtree t) pc)))
  15428. (set-buffer-modified-p bmp)))))
  15429. (setq org-todo-keywords-for-agenda
  15430. (org-uniquify org-todo-keywords-for-agenda))
  15431. (setq org-todo-keyword-alist-for-agenda
  15432. (org-uniquify org-todo-keyword-alist-for-agenda)
  15433. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  15434. ;;;; Embedded LaTeX
  15435. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  15436. "Keymap for the minor `org-cdlatex-mode'.")
  15437. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  15438. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  15439. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  15440. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  15441. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  15442. (defvar org-cdlatex-texmathp-advice-is-done nil
  15443. "Flag remembering if we have applied the advice to texmathp already.")
  15444. (define-minor-mode org-cdlatex-mode
  15445. "Toggle the minor `org-cdlatex-mode'.
  15446. This mode supports entering LaTeX environment and math in LaTeX fragments
  15447. in Org-mode.
  15448. \\{org-cdlatex-mode-map}"
  15449. nil " OCDL" nil
  15450. (when org-cdlatex-mode
  15451. (require 'cdlatex)
  15452. (run-hooks 'cdlatex-mode-hook)
  15453. (cdlatex-compute-tables))
  15454. (unless org-cdlatex-texmathp-advice-is-done
  15455. (setq org-cdlatex-texmathp-advice-is-done t)
  15456. (defadvice texmathp (around org-math-always-on activate)
  15457. "Always return t in org-mode buffers.
  15458. This is because we want to insert math symbols without dollars even outside
  15459. the LaTeX math segments. If Orgmode thinks that point is actually inside
  15460. an embedded LaTeX fragment, let texmathp do its job.
  15461. \\[org-cdlatex-mode-map]"
  15462. (interactive)
  15463. (let (p)
  15464. (cond
  15465. ((not (derived-mode-p 'org-mode)) ad-do-it)
  15466. ((eq this-command 'cdlatex-math-symbol)
  15467. (setq ad-return-value t
  15468. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  15469. (t
  15470. (let ((p (org-inside-LaTeX-fragment-p)))
  15471. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  15472. (setq ad-return-value t
  15473. texmathp-why '("Org-mode embedded math" . 0))
  15474. (if p ad-do-it)))))))))
  15475. (defun turn-on-org-cdlatex ()
  15476. "Unconditionally turn on `org-cdlatex-mode'."
  15477. (org-cdlatex-mode 1))
  15478. (defun org-inside-LaTeX-fragment-p ()
  15479. "Test if point is inside a LaTeX fragment.
  15480. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  15481. sequence appearing also before point.
  15482. Even though the matchers for math are configurable, this function assumes
  15483. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  15484. delimiters are skipped when they have been removed by customization.
  15485. The return value is nil, or a cons cell with the delimiter and the
  15486. position of this delimiter.
  15487. This function does a reasonably good job, but can locally be fooled by
  15488. for example currency specifications. For example it will assume being in
  15489. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  15490. fragments that are properly closed, but during editing, we have to live
  15491. with the uncertainty caused by missing closing delimiters. This function
  15492. looks only before point, not after."
  15493. (catch 'exit
  15494. (let ((pos (point))
  15495. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  15496. (lim (progn
  15497. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  15498. (point)))
  15499. dd-on str (start 0) m re)
  15500. (goto-char pos)
  15501. (when dodollar
  15502. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  15503. re (nth 1 (assoc "$" org-latex-regexps)))
  15504. (while (string-match re str start)
  15505. (cond
  15506. ((= (match-end 0) (length str))
  15507. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  15508. ((= (match-end 0) (- (length str) 5))
  15509. (throw 'exit nil))
  15510. (t (setq start (match-end 0))))))
  15511. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  15512. (goto-char pos)
  15513. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  15514. (and (match-beginning 2) (throw 'exit nil))
  15515. ;; count $$
  15516. (while (re-search-backward "\\$\\$" lim t)
  15517. (setq dd-on (not dd-on)))
  15518. (goto-char pos)
  15519. (if dd-on (cons "$$" m))))))
  15520. (defun org-inside-latex-macro-p ()
  15521. "Is point inside a LaTeX macro or its arguments?"
  15522. (save-match-data
  15523. (org-in-regexp
  15524. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  15525. (defun org-try-cdlatex-tab ()
  15526. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  15527. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  15528. - inside a LaTeX fragment, or
  15529. - after the first word in a line, where an abbreviation expansion could
  15530. insert a LaTeX environment."
  15531. (when org-cdlatex-mode
  15532. (cond
  15533. ;; Before any word on the line: No expansion possible.
  15534. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  15535. ;; Just after first word on the line: Expand it. Make sure it
  15536. ;; cannot happen on headlines, though.
  15537. ((save-excursion
  15538. (skip-chars-backward "a-zA-Z0-9*")
  15539. (skip-chars-backward " \t")
  15540. (and (bolp) (not (org-at-heading-p))))
  15541. (cdlatex-tab) t)
  15542. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  15543. (defun org-cdlatex-underscore-caret (&optional arg)
  15544. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  15545. Revert to the normal definition outside of these fragments."
  15546. (interactive "P")
  15547. (if (org-inside-LaTeX-fragment-p)
  15548. (call-interactively 'cdlatex-sub-superscript)
  15549. (let (org-cdlatex-mode)
  15550. (call-interactively (key-binding (vector last-input-event))))))
  15551. (defun org-cdlatex-math-modify (&optional arg)
  15552. "Execute `cdlatex-math-modify' in LaTeX fragments.
  15553. Revert to the normal definition outside of these fragments."
  15554. (interactive "P")
  15555. (if (org-inside-LaTeX-fragment-p)
  15556. (call-interactively 'cdlatex-math-modify)
  15557. (let (org-cdlatex-mode)
  15558. (call-interactively (key-binding (vector last-input-event))))))
  15559. (defvar org-latex-fragment-image-overlays nil
  15560. "List of overlays carrying the images of latex fragments.")
  15561. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  15562. (defun org-remove-latex-fragment-image-overlays ()
  15563. "Remove all overlays with LaTeX fragment images in current buffer."
  15564. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  15565. (setq org-latex-fragment-image-overlays nil))
  15566. (defun org-preview-latex-fragment (&optional subtree)
  15567. "Preview the LaTeX fragment at point, or all locally or globally.
  15568. If the cursor is in a LaTeX fragment, create the image and overlay
  15569. it over the source code. If there is no fragment at point, display
  15570. all fragments in the current text, from one headline to the next. With
  15571. prefix SUBTREE, display all fragments in the current subtree. With a
  15572. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  15573. the cursor is before the first headline,
  15574. display all fragments in the buffer.
  15575. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  15576. (interactive "P")
  15577. (unless buffer-file-name
  15578. (error "Can't preview LaTeX fragment in a non-file buffer"))
  15579. (org-remove-latex-fragment-image-overlays)
  15580. (save-excursion
  15581. (save-restriction
  15582. (let (beg end at msg)
  15583. (cond
  15584. ((or (equal subtree '(16))
  15585. (not (save-excursion
  15586. (re-search-backward org-outline-regexp-bol nil t))))
  15587. (setq beg (point-min) end (point-max)
  15588. msg "Creating images for buffer...%s"))
  15589. ((equal subtree '(4))
  15590. (org-back-to-heading)
  15591. (setq beg (point) end (org-end-of-subtree t)
  15592. msg "Creating images for subtree...%s"))
  15593. (t
  15594. (if (setq at (org-inside-LaTeX-fragment-p))
  15595. (goto-char (max (point-min) (- (cdr at) 2)))
  15596. (org-back-to-heading))
  15597. (setq beg (point) end (progn (outline-next-heading) (point))
  15598. msg (if at "Creating image...%s"
  15599. "Creating images for entry...%s"))))
  15600. (message msg "")
  15601. (narrow-to-region beg end)
  15602. (goto-char beg)
  15603. (org-format-latex
  15604. (concat "ltxpng/" (file-name-sans-extension
  15605. (file-name-nondirectory
  15606. buffer-file-name)))
  15607. default-directory 'overlays msg at 'forbuffer
  15608. org-latex-create-formula-image-program)
  15609. (message msg "done. Use `C-c C-c' to remove images.")))))
  15610. (defvar org-latex-regexps
  15611. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  15612. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  15613. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  15614. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15615. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  15616. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  15617. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  15618. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  15619. "Regular expressions for matching embedded LaTeX.")
  15620. (defvar org-export-have-math nil) ;; dynamic scoping
  15621. (defun org-format-latex (prefix &optional dir overlays msg at
  15622. forbuffer processing-type)
  15623. "Replace LaTeX fragments with links to an image, and produce images.
  15624. Some of the options can be changed using the variable
  15625. `org-format-latex-options'."
  15626. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  15627. (let* ((prefixnodir (file-name-nondirectory prefix))
  15628. (absprefix (expand-file-name prefix dir))
  15629. (todir (file-name-directory absprefix))
  15630. (opt org-format-latex-options)
  15631. (matchers (plist-get opt :matchers))
  15632. (re-list org-latex-regexps)
  15633. (org-format-latex-header-extra
  15634. (plist-get (org-infile-export-plist) :latex-header-extra))
  15635. (cnt 0) txt hash link beg end re e checkdir
  15636. executables-checked string
  15637. m n block-type block linkfile movefile ov)
  15638. ;; Check the different regular expressions
  15639. (while (setq e (pop re-list))
  15640. (setq m (car e) re (nth 1 e) n (nth 2 e) block-type (nth 3 e)
  15641. block (if block-type "\n\n" ""))
  15642. (when (member m matchers)
  15643. (goto-char (point-min))
  15644. (while (re-search-forward re nil t)
  15645. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  15646. (not (get-text-property (match-beginning n)
  15647. 'org-protected))
  15648. (or (not overlays)
  15649. (not (eq (get-char-property (match-beginning n)
  15650. 'org-overlay-type)
  15651. 'org-latex-overlay))))
  15652. (setq org-export-have-math t)
  15653. (cond
  15654. ((eq processing-type 'verbatim)
  15655. ;; Leave the text verbatim, just protect it
  15656. (add-text-properties (match-beginning n) (match-end n)
  15657. '(org-protected t)))
  15658. ((eq processing-type 'mathjax)
  15659. ;; Prepare for MathJax processing
  15660. (setq string (match-string n))
  15661. (if (member m '("$" "$1"))
  15662. (save-excursion
  15663. (delete-region (match-beginning n) (match-end n))
  15664. (goto-char (match-beginning n))
  15665. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  15666. "\\)")
  15667. '(org-protected t))))
  15668. (add-text-properties (match-beginning n) (match-end n)
  15669. '(org-protected t))))
  15670. ((or (eq processing-type 'dvipng)
  15671. (eq processing-type 'imagemagick))
  15672. ;; Process to an image
  15673. (setq txt (match-string n)
  15674. beg (match-beginning n) end (match-end n)
  15675. cnt (1+ cnt))
  15676. (let (print-length print-level) ; make sure full list is printed
  15677. (setq hash (sha1 (prin1-to-string
  15678. (list org-format-latex-header
  15679. org-format-latex-header-extra
  15680. org-export-latex-default-packages-alist
  15681. org-export-latex-packages-alist
  15682. org-format-latex-options
  15683. forbuffer txt)))
  15684. linkfile (format "%s_%s.png" prefix hash)
  15685. movefile (format "%s_%s.png" absprefix hash)))
  15686. (setq link (concat block "[[file:" linkfile "]]" block))
  15687. (if msg (message msg cnt))
  15688. (goto-char beg)
  15689. (unless checkdir ; make sure the directory exists
  15690. (setq checkdir t)
  15691. (or (file-directory-p todir) (make-directory todir t)))
  15692. (cond
  15693. ((eq processing-type 'dvipng)
  15694. (unless executables-checked
  15695. (org-check-external-command
  15696. "latex" "needed to convert LaTeX fragments to images")
  15697. (org-check-external-command
  15698. "dvipng" "needed to convert LaTeX fragments to images")
  15699. (setq executables-checked t))
  15700. (unless (file-exists-p movefile)
  15701. (org-create-formula-image-with-dvipng
  15702. txt movefile opt forbuffer)))
  15703. ((eq processing-type 'imagemagick)
  15704. (unless executables-checked
  15705. (org-check-external-command
  15706. "convert" "you need to install imagemagick")
  15707. (setq executables-checked t))
  15708. (unless (file-exists-p movefile)
  15709. (org-create-formula-image-with-imagemagick
  15710. txt movefile opt forbuffer))))
  15711. (if overlays
  15712. (progn
  15713. (mapc (lambda (o)
  15714. (if (eq (overlay-get o 'org-overlay-type)
  15715. 'org-latex-overlay)
  15716. (delete-overlay o)))
  15717. (overlays-in beg end))
  15718. (setq ov (make-overlay beg end))
  15719. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  15720. (if (featurep 'xemacs)
  15721. (progn
  15722. (overlay-put ov 'invisible t)
  15723. (overlay-put
  15724. ov 'end-glyph
  15725. (make-glyph (vector 'png :file movefile))))
  15726. (overlay-put
  15727. ov 'display
  15728. (list 'image :type 'png :file movefile :ascent 'center)))
  15729. (push ov org-latex-fragment-image-overlays)
  15730. (goto-char end))
  15731. (delete-region beg end)
  15732. (insert (org-add-props link
  15733. (list 'org-latex-src
  15734. (replace-regexp-in-string
  15735. "\"" "" txt)
  15736. 'org-latex-src-embed-type
  15737. (if block-type 'paragraph 'character))))))
  15738. ((eq processing-type 'mathml)
  15739. ;; Process to MathML
  15740. (unless executables-checked
  15741. (unless (save-match-data (org-format-latex-mathml-available-p))
  15742. (error "LaTeX to MathML converter not configured"))
  15743. (setq executables-checked t))
  15744. (setq txt (match-string n)
  15745. beg (match-beginning n) end (match-end n)
  15746. cnt (1+ cnt))
  15747. (if msg (message msg cnt))
  15748. (goto-char beg)
  15749. (delete-region beg end)
  15750. (insert (org-format-latex-as-mathml
  15751. txt block-type prefix dir)))
  15752. (t
  15753. (error "Unknown conversion type %s for latex fragments"
  15754. processing-type)))))))))
  15755. (defun org-create-math-formula (latex-frag &optional mathml-file)
  15756. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  15757. Use `org-latex-to-mathml-convert-command'. If the conversion is
  15758. sucessful, return the portion between \"<math...> </math>\"
  15759. elements otherwise return nil. When MATHML-FILE is specified,
  15760. write the results in to that file. When invoked as an
  15761. interactive command, prompt for LATEX-FRAG, with initial value
  15762. set to the current active region and echo the results for user
  15763. inspection."
  15764. (interactive (list (let ((frag (when (region-active-p)
  15765. (buffer-substring-no-properties
  15766. (region-beginning) (region-end)))))
  15767. (read-string "LaTeX Fragment: " frag nil frag))))
  15768. (unless latex-frag (error "Invalid latex-frag"))
  15769. (let* ((tmp-in-file (file-relative-name
  15770. (make-temp-name (expand-file-name "ltxmathml-in"))))
  15771. (ignore (write-region latex-frag nil tmp-in-file))
  15772. (tmp-out-file (file-relative-name
  15773. (make-temp-name (expand-file-name "ltxmathml-out"))))
  15774. (cmd (format-spec
  15775. org-latex-to-mathml-convert-command
  15776. `((?j . ,(shell-quote-argument
  15777. (expand-file-name org-latex-to-mathml-jar-file)))
  15778. (?I . ,(shell-quote-argument tmp-in-file))
  15779. (?o . ,(shell-quote-argument tmp-out-file)))))
  15780. mathml shell-command-output)
  15781. (when (org-called-interactively-p 'any)
  15782. (unless (org-format-latex-mathml-available-p)
  15783. (error "LaTeX to MathML converter not configured")))
  15784. (message "Running %s" cmd)
  15785. (setq shell-command-output (shell-command-to-string cmd))
  15786. (setq mathml
  15787. (when (file-readable-p tmp-out-file)
  15788. (with-current-buffer (find-file-noselect tmp-out-file t)
  15789. (goto-char (point-min))
  15790. (when (re-search-forward
  15791. (concat
  15792. (regexp-quote
  15793. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\">")
  15794. "\\(.\\|\n\\)*"
  15795. (regexp-quote "</math>")) nil t)
  15796. (prog1 (match-string 0) (kill-buffer))))))
  15797. (cond
  15798. (mathml
  15799. (setq mathml
  15800. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  15801. (when mathml-file
  15802. (write-region mathml nil mathml-file))
  15803. (when (org-called-interactively-p 'any)
  15804. (message mathml)))
  15805. ((message "LaTeX to MathML conversion failed")
  15806. (message shell-command-output)))
  15807. (delete-file tmp-in-file)
  15808. (when (file-exists-p tmp-out-file)
  15809. (delete-file tmp-out-file))
  15810. mathml))
  15811. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  15812. prefix &optional dir)
  15813. "Use `org-create-math-formula' but check local cache first."
  15814. (let* ((absprefix (expand-file-name prefix dir))
  15815. (print-length nil) (print-level nil)
  15816. (formula-id (concat
  15817. "formula-"
  15818. (sha1
  15819. (prin1-to-string
  15820. (list latex-frag
  15821. org-latex-to-mathml-convert-command)))))
  15822. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  15823. (formula-cache-dir (file-name-directory formula-cache)))
  15824. (unless (file-directory-p formula-cache-dir)
  15825. (make-directory formula-cache-dir t))
  15826. (unless (file-exists-p formula-cache)
  15827. (org-create-math-formula latex-frag formula-cache))
  15828. (if (file-exists-p formula-cache)
  15829. ;; Successful conversion. Return the link to MathML file.
  15830. (org-add-props
  15831. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  15832. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  15833. 'org-latex-src-embed-type (if latex-frag-type
  15834. 'paragraph 'character)))
  15835. ;; Failed conversion. Return the LaTeX fragment verbatim
  15836. (add-text-properties
  15837. 0 (1- (length latex-frag)) '(org-protected t) latex-frag)
  15838. latex-frag)))
  15839. ;; This function borrows from Ganesh Swami's latex2png.el
  15840. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  15841. "This calls dvipng."
  15842. (require 'org-latex)
  15843. (let* ((tmpdir (if (featurep 'xemacs)
  15844. (temp-directory)
  15845. temporary-file-directory))
  15846. (texfilebase (make-temp-name
  15847. (expand-file-name "orgtex" tmpdir)))
  15848. (texfile (concat texfilebase ".tex"))
  15849. (dvifile (concat texfilebase ".dvi"))
  15850. (pngfile (concat texfilebase ".png"))
  15851. (fnh (if (featurep 'xemacs)
  15852. (font-height (face-font 'default))
  15853. (face-attribute 'default :height nil)))
  15854. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15855. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15856. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15857. "Black"))
  15858. (bg (or (plist-get options (if buffer :background :html-background))
  15859. "Transparent")))
  15860. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  15861. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  15862. (with-temp-file texfile
  15863. (insert (org-splice-latex-header
  15864. org-format-latex-header
  15865. org-export-latex-default-packages-alist
  15866. org-export-latex-packages-alist t
  15867. org-format-latex-header-extra))
  15868. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  15869. (require 'org-latex)
  15870. (org-export-latex-fix-inputenc))
  15871. (let ((dir default-directory))
  15872. (condition-case nil
  15873. (progn
  15874. (cd tmpdir)
  15875. (call-process "latex" nil nil nil texfile))
  15876. (error nil))
  15877. (cd dir))
  15878. (if (not (file-exists-p dvifile))
  15879. (progn (message "Failed to create dvi file from %s" texfile) nil)
  15880. (condition-case nil
  15881. (if (featurep 'xemacs)
  15882. (call-process "dvipng" nil nil nil
  15883. "-fg" fg "-bg" bg
  15884. "-T" "tight"
  15885. "-o" pngfile
  15886. dvifile)
  15887. (call-process "dvipng" nil nil nil
  15888. "-fg" fg "-bg" bg
  15889. "-D" dpi
  15890. ;;"-x" scale "-y" scale
  15891. "-T" "tight"
  15892. "-o" pngfile
  15893. dvifile))
  15894. (error nil))
  15895. (if (not (file-exists-p pngfile))
  15896. (if org-format-latex-signal-error
  15897. (error "Failed to create png file from %s" texfile)
  15898. (message "Failed to create png file from %s" texfile)
  15899. nil)
  15900. ;; Use the requested file name and clean up
  15901. (copy-file pngfile tofile 'replace)
  15902. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  15903. (if (file-exists-p (concat texfilebase e))
  15904. (delete-file (concat texfilebase e))))
  15905. pngfile))))
  15906. (defvar org-latex-to-pdf-process) ;; Defined in org-latex.el
  15907. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  15908. "This calls convert, which is included into imagemagick."
  15909. (require 'org-latex)
  15910. (let* ((tmpdir (if (featurep 'xemacs)
  15911. (temp-directory)
  15912. temporary-file-directory))
  15913. (texfilebase (make-temp-name
  15914. (expand-file-name "orgtex" tmpdir)))
  15915. (texfile (concat texfilebase ".tex"))
  15916. (pdffile (concat texfilebase ".pdf"))
  15917. (pngfile (concat texfilebase ".png"))
  15918. (fnh (if (featurep 'xemacs)
  15919. (font-height (face-font 'default))
  15920. (face-attribute 'default :height nil)))
  15921. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  15922. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  15923. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  15924. "black"))
  15925. (bg (or (plist-get options (if buffer :background :html-background))
  15926. "white")))
  15927. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  15928. (setq fg (org-latex-color-format fg)))
  15929. (if (eq bg 'default) (setq bg (org-latex-color :background))
  15930. (setq bg (org-latex-color-format
  15931. (if (string= bg "Transparent")(setq bg "white")))))
  15932. (with-temp-file texfile
  15933. (insert (org-splice-latex-header
  15934. org-format-latex-header
  15935. org-export-latex-default-packages-alist
  15936. org-export-latex-packages-alist t
  15937. org-format-latex-header-extra))
  15938. (insert "\n\\begin{document}\n"
  15939. "\\definecolor{fg}{rgb}{" fg "}\n"
  15940. "\\definecolor{bg}{rgb}{" bg "}\n"
  15941. "\n\\pagecolor{bg}\n"
  15942. "\n{\\color{fg}\n"
  15943. string
  15944. "\n}\n"
  15945. "\n\\end{document}\n" )
  15946. (require 'org-latex)
  15947. (org-export-latex-fix-inputenc))
  15948. (let ((dir default-directory) cmd cmds latex-frags-cmds)
  15949. (condition-case nil
  15950. (progn
  15951. (cd tmpdir)
  15952. (setq cmds org-latex-to-pdf-process)
  15953. (while cmds
  15954. (setq latex-frags-cmds (pop cmds))
  15955. (if (listp latex-frags-cmds)
  15956. (setq cmds nil)
  15957. (setq latex-frags-cmds (list (car org-latex-to-pdf-process)))))
  15958. (while latex-frags-cmds
  15959. (setq cmd (pop latex-frags-cmds))
  15960. (while (string-match "%b" cmd)
  15961. (setq cmd (replace-match
  15962. (save-match-data
  15963. (shell-quote-argument texfile))
  15964. t t cmd)))
  15965. (while (string-match "%f" cmd)
  15966. (setq cmd (replace-match
  15967. (save-match-data
  15968. (shell-quote-argument (file-name-nondirectory texfile)))
  15969. t t cmd)))
  15970. (while (string-match "%o" cmd)
  15971. (setq cmd (replace-match
  15972. (save-match-data
  15973. (shell-quote-argument (file-name-directory texfile)))
  15974. t t cmd)))
  15975. (setq cmd (split-string cmd))
  15976. (eval (append (list 'call-process (pop cmd) nil nil nil) cmd))))
  15977. (error nil))
  15978. (cd dir))
  15979. (if (not (file-exists-p pdffile))
  15980. (progn (message "Failed to create pdf file from %s" texfile) nil)
  15981. (condition-case nil
  15982. (if (featurep 'xemacs)
  15983. (call-process "convert" nil nil nil
  15984. "-density" "96"
  15985. "-trim"
  15986. "-antialias"
  15987. pdffile
  15988. "-quality" "100"
  15989. ;; "-sharpen" "0x1.0"
  15990. pngfile)
  15991. (call-process "convert" nil nil nil
  15992. "-density" dpi
  15993. "-trim"
  15994. "-antialias"
  15995. pdffile
  15996. "-quality" "100"
  15997. ; "-sharpen" "0x1.0"
  15998. pngfile))
  15999. (error nil))
  16000. (if (not (file-exists-p pngfile))
  16001. (if org-format-latex-signal-error
  16002. (error "Failed to create png file from %s" texfile)
  16003. (message "Failed to create png file from %s" texfile)
  16004. nil)
  16005. ;; Use the requested file name and clean up
  16006. (copy-file pngfile tofile 'replace)
  16007. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  16008. (if (file-exists-p (concat texfilebase e))
  16009. (delete-file (concat texfilebase e))))
  16010. pngfile))))
  16011. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  16012. "Fill a LaTeX header template TPL.
  16013. In the template, the following place holders will be recognized:
  16014. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  16015. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  16016. [PACKAGES] \\usepackage statements for PKG
  16017. [NO-PACKAGES] do not include PKG
  16018. [EXTRA] the string EXTRA
  16019. [NO-EXTRA] do not include EXTRA
  16020. For backward compatibility, if both the positive and the negative place
  16021. holder is missing, the positive one (without the \"NO-\") will be
  16022. assumed to be present at the end of the template.
  16023. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  16024. EXTRA is a string.
  16025. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  16026. (let (rpl (end ""))
  16027. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  16028. (setq rpl (if (or (match-end 1) (not def-pkg))
  16029. "" (org-latex-packages-to-string def-pkg snippets-p t))
  16030. tpl (replace-match rpl t t tpl))
  16031. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  16032. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  16033. (setq rpl (if (or (match-end 1) (not pkg))
  16034. "" (org-latex-packages-to-string pkg snippets-p t))
  16035. tpl (replace-match rpl t t tpl))
  16036. (if pkg (setq end
  16037. (concat end "\n"
  16038. (org-latex-packages-to-string pkg snippets-p)))))
  16039. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  16040. (setq rpl (if (or (match-end 1) (not extra))
  16041. "" (concat extra "\n"))
  16042. tpl (replace-match rpl t t tpl))
  16043. (if (and extra (string-match "\\S-" extra))
  16044. (setq end (concat end "\n" extra))))
  16045. (if (string-match "\\S-" end)
  16046. (concat tpl "\n" end)
  16047. tpl)))
  16048. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  16049. "Turn an alist of packages into a string with the \\usepackage macros."
  16050. (setq pkg (mapconcat (lambda(p)
  16051. (cond
  16052. ((stringp p) p)
  16053. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  16054. (format "%% Package %s omitted" (cadr p)))
  16055. ((equal "" (car p))
  16056. (format "\\usepackage{%s}" (cadr p)))
  16057. (t
  16058. (format "\\usepackage[%s]{%s}"
  16059. (car p) (cadr p)))))
  16060. pkg
  16061. "\n"))
  16062. (if newline (concat pkg "\n") pkg))
  16063. (defun org-dvipng-color (attr)
  16064. "Return a RGB color specification for dvipng."
  16065. (apply 'format "rgb %s %s %s"
  16066. (mapcar 'org-normalize-color
  16067. (if (featurep 'xemacs)
  16068. (color-rgb-components
  16069. (face-property 'default
  16070. (cond ((eq attr :foreground) 'foreground)
  16071. ((eq attr :background) 'background))))
  16072. (color-values (face-attribute 'default attr nil))))))
  16073. (defun org-latex-color (attr)
  16074. "Return a RGB color for the LaTeX color package."
  16075. (apply 'format "%s,%s,%s"
  16076. (mapcar 'org-normalize-color
  16077. (if (featurep 'xemacs)
  16078. (color-rgb-components
  16079. (face-property 'default
  16080. (cond ((eq attr :foreground) 'foreground)
  16081. ((eq attr :background) 'background))))
  16082. (color-values (face-attribute 'default attr nil))))))
  16083. (defun org-latex-color-format (color-name)
  16084. "Convert COLOR-NAME to a RGB color value."
  16085. (apply 'format "%s,%s,%s"
  16086. (mapcar 'org-normalize-color
  16087. (color-values color-name))))
  16088. (defun org-normalize-color (value)
  16089. "Return string to be used as color value for an RGB component."
  16090. (format "%g" (/ value 65535.0)))
  16091. ;; Image display
  16092. (defvar org-inline-image-overlays nil)
  16093. (make-variable-buffer-local 'org-inline-image-overlays)
  16094. (defun org-toggle-inline-images (&optional include-linked)
  16095. "Toggle the display of inline images.
  16096. INCLUDE-LINKED is passed to `org-display-inline-images'."
  16097. (interactive "P")
  16098. (if org-inline-image-overlays
  16099. (progn
  16100. (org-remove-inline-images)
  16101. (message "Inline image display turned off"))
  16102. (org-display-inline-images include-linked)
  16103. (if org-inline-image-overlays
  16104. (message "%d images displayed inline"
  16105. (length org-inline-image-overlays))
  16106. (message "No images to display inline"))))
  16107. (defun org-display-inline-images (&optional include-linked refresh beg end)
  16108. "Display inline images.
  16109. Normally only links without a description part are inlined, because this
  16110. is how it will work for export. When INCLUDE-LINKED is set, also links
  16111. with a description part will be inlined. This can be nice for a quick
  16112. look at those images, but it does not reflect what exported files will look
  16113. like.
  16114. When REFRESH is set, refresh existing images between BEG and END.
  16115. This will create new image displays only if necessary.
  16116. BEG and END default to the buffer boundaries."
  16117. (interactive "P")
  16118. (unless refresh
  16119. (org-remove-inline-images)
  16120. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  16121. (save-excursion
  16122. (save-restriction
  16123. (widen)
  16124. (setq beg (or beg (point-min)) end (or end (point-max)))
  16125. (goto-char beg)
  16126. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  16127. (substring (org-image-file-name-regexp) 0 -2)
  16128. "\\)\\]" (if include-linked "" "\\]")))
  16129. old file ov img)
  16130. (while (re-search-forward re end t)
  16131. (setq old (get-char-property-and-overlay (match-beginning 1)
  16132. 'org-image-overlay))
  16133. (setq file (expand-file-name
  16134. (concat (or (match-string 3) "") (match-string 4))))
  16135. (when (file-exists-p file)
  16136. (if (and (car-safe old) refresh)
  16137. (image-refresh (overlay-get (cdr old) 'display))
  16138. (setq img (save-match-data (create-image file)))
  16139. (when img
  16140. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  16141. (overlay-put ov 'display img)
  16142. (overlay-put ov 'face 'default)
  16143. (overlay-put ov 'org-image-overlay t)
  16144. (overlay-put ov 'modification-hooks
  16145. (list 'org-display-inline-modification-hook))
  16146. (push ov org-inline-image-overlays)))))))))
  16147. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  16148. "Remove inline-display overlay if a corresponding region is modified."
  16149. (let ((inhibit-modification-hooks t))
  16150. (when (and ov after)
  16151. (delete ov org-inline-image-overlays)
  16152. (delete-overlay ov))))
  16153. (defun org-remove-inline-images ()
  16154. "Remove inline display of images."
  16155. (interactive)
  16156. (mapc 'delete-overlay org-inline-image-overlays)
  16157. (setq org-inline-image-overlays nil))
  16158. ;;;; Key bindings
  16159. ;; Outline functions from `outline-mode-prefix-map'
  16160. ;; that can be remapped in Org:
  16161. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  16162. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  16163. (define-key org-mode-map [remap outline-forward-same-level]
  16164. 'org-forward-same-level)
  16165. (define-key org-mode-map [remap outline-backward-same-level]
  16166. 'org-backward-same-level)
  16167. (define-key org-mode-map [remap show-branches]
  16168. 'org-kill-note-or-show-branches)
  16169. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  16170. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  16171. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  16172. ;; Outline functions from `outline-mode-prefix-map' that can not
  16173. ;; be remapped in Org:
  16174. ;;
  16175. ;; - the column "key binding" shows whether the Outline function is still
  16176. ;; available in Org mode on the same key that it has been bound to in
  16177. ;; Outline mode:
  16178. ;; - "overridden": key used for a different functionality in Org mode
  16179. ;; - else: key still bound to the same Outline function in Org mode
  16180. ;;
  16181. ;; | Outline function | key binding | Org replacement |
  16182. ;; |------------------------------------+-------------+-----------------------|
  16183. ;; | `outline-next-visible-heading' | `C-c C-n' | still same function |
  16184. ;; | `outline-previous-visible-heading' | `C-c C-p' | still same function |
  16185. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  16186. ;; | `outline-move-subtree-up' | `C-c C-^' | better: org-shiftup |
  16187. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  16188. ;; | `show-entry' | overridden | no replacement |
  16189. ;; | `show-children' | `C-c C-i' | visibility cycling |
  16190. ;; | `show-branches' | `C-c C-k' | still same function |
  16191. ;; | `show-subtree' | overridden | visibility cycling |
  16192. ;; | `show-all' | overridden | no replacement |
  16193. ;; | `hide-subtree' | overridden | visibility cycling |
  16194. ;; | `hide-body' | overridden | no replacement |
  16195. ;; | `hide-entry' | overridden | visibility cycling |
  16196. ;; | `hide-leaves' | overridden | no replacement |
  16197. ;; | `hide-sublevels' | overridden | no replacement |
  16198. ;; | `hide-other' | overridden | no replacement |
  16199. ;; Make `C-c C-x' a prefix key
  16200. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  16201. ;; TAB key with modifiers
  16202. (org-defkey org-mode-map "\C-i" 'org-cycle)
  16203. (org-defkey org-mode-map [(tab)] 'org-cycle)
  16204. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  16205. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  16206. ;; The following line is necessary under Suse GNU/Linux
  16207. (unless (featurep 'xemacs)
  16208. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  16209. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  16210. (define-key org-mode-map [backtab] 'org-shifttab)
  16211. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  16212. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  16213. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  16214. ;; Cursor keys with modifiers
  16215. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  16216. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  16217. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  16218. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  16219. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  16220. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  16221. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  16222. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  16223. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  16224. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  16225. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  16226. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  16227. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  16228. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  16229. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  16230. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  16231. ;; Babel keys
  16232. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  16233. (mapc (lambda (pair)
  16234. (define-key org-babel-map (car pair) (cdr pair)))
  16235. org-babel-key-bindings)
  16236. ;;; Extra keys for tty access.
  16237. ;; We only set them when really needed because otherwise the
  16238. ;; menus don't show the simple keys
  16239. (when (or org-use-extra-keys
  16240. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  16241. (not window-system))
  16242. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  16243. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  16244. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  16245. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  16246. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  16247. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  16248. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  16249. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  16250. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  16251. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  16252. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  16253. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  16254. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  16255. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  16256. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  16257. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  16258. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  16259. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  16260. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  16261. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  16262. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  16263. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  16264. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  16265. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  16266. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  16267. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  16268. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  16269. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  16270. ;; All the other keys
  16271. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  16272. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  16273. (if (boundp 'narrow-map)
  16274. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  16275. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  16276. (if (boundp 'narrow-map)
  16277. (org-defkey narrow-map "b" 'org-narrow-to-block)
  16278. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  16279. (if (boundp 'narrow-map)
  16280. (org-defkey narrow-map "e" 'org-narrow-to-element)
  16281. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  16282. (org-defkey org-mode-map "\C-\M-t" 'org-element-transpose)
  16283. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  16284. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  16285. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  16286. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  16287. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  16288. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  16289. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  16290. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  16291. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  16292. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  16293. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  16294. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  16295. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  16296. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  16297. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  16298. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  16299. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  16300. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  16301. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  16302. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  16303. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  16304. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  16305. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  16306. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  16307. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  16308. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  16309. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  16310. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  16311. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  16312. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  16313. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  16314. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  16315. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  16316. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  16317. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  16318. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  16319. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  16320. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  16321. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  16322. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  16323. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  16324. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  16325. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  16326. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  16327. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  16328. (org-defkey org-mode-map "\C-c^" 'org-sort)
  16329. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  16330. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  16331. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  16332. (org-defkey org-mode-map "\C-m" 'org-return)
  16333. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  16334. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  16335. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  16336. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  16337. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  16338. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  16339. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  16340. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  16341. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  16342. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  16343. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  16344. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  16345. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  16346. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  16347. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  16348. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  16349. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  16350. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  16351. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  16352. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  16353. (org-defkey org-mode-map "\C-c\C-@" 'org-mark-list)
  16354. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  16355. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  16356. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  16357. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  16358. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  16359. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  16360. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  16361. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  16362. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  16363. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  16364. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  16365. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  16366. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  16367. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  16368. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  16369. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  16370. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  16371. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  16372. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  16373. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  16374. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  16375. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  16376. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  16377. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  16378. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  16379. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  16380. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  16381. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  16382. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  16383. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  16384. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  16385. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  16386. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  16387. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  16388. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  16389. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  16390. (when (featurep 'xemacs)
  16391. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  16392. (defconst org-speed-commands-default
  16393. '(
  16394. ("Outline Navigation")
  16395. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  16396. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  16397. ("f" . (org-speed-move-safe 'org-forward-same-level))
  16398. ("b" . (org-speed-move-safe 'org-backward-same-level))
  16399. ("u" . (org-speed-move-safe 'outline-up-heading))
  16400. ("j" . org-goto)
  16401. ("g" . (org-refile t))
  16402. ("Outline Visibility")
  16403. ("c" . org-cycle)
  16404. ("C" . org-shifttab)
  16405. (" " . org-display-outline-path)
  16406. (":" . org-columns)
  16407. ("Outline Structure Editing")
  16408. ("U" . org-shiftmetaup)
  16409. ("D" . org-shiftmetadown)
  16410. ("r" . org-metaright)
  16411. ("l" . org-metaleft)
  16412. ("R" . org-shiftmetaright)
  16413. ("L" . org-shiftmetaleft)
  16414. ("i" . (progn (forward-char 1) (call-interactively
  16415. 'org-insert-heading-respect-content)))
  16416. ("^" . org-sort)
  16417. ("w" . org-refile)
  16418. ("a" . org-archive-subtree-default-with-confirmation)
  16419. ("." . org-mark-subtree)
  16420. ("#" . org-toggle-comment)
  16421. ("Clock Commands")
  16422. ("I" . org-clock-in)
  16423. ("O" . org-clock-out)
  16424. ("Meta Data Editing")
  16425. ("t" . org-todo)
  16426. ("0" . (org-priority ?\ ))
  16427. ("1" . (org-priority ?A))
  16428. ("2" . (org-priority ?B))
  16429. ("3" . (org-priority ?C))
  16430. (";" . org-set-tags-command)
  16431. ("e" . org-set-effort)
  16432. ("Agenda Views etc")
  16433. ("v" . org-agenda)
  16434. ("/" . org-sparse-tree)
  16435. ("Misc")
  16436. ("o" . org-open-at-point)
  16437. ("?" . org-speed-command-help)
  16438. ("<" . (org-agenda-set-restriction-lock 'subtree))
  16439. (">" . (org-agenda-remove-restriction-lock))
  16440. )
  16441. "The default speed commands.")
  16442. (defun org-print-speed-command (e)
  16443. (if (> (length (car e)) 1)
  16444. (progn
  16445. (princ "\n")
  16446. (princ (car e))
  16447. (princ "\n")
  16448. (princ (make-string (length (car e)) ?-))
  16449. (princ "\n"))
  16450. (princ (car e))
  16451. (princ " ")
  16452. (if (symbolp (cdr e))
  16453. (princ (symbol-name (cdr e)))
  16454. (prin1 (cdr e)))
  16455. (princ "\n")))
  16456. (defun org-speed-command-help ()
  16457. "Show the available speed commands."
  16458. (interactive)
  16459. (if (not org-use-speed-commands)
  16460. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  16461. (with-output-to-temp-buffer "*Help*"
  16462. (princ "User-defined Speed commands\n===========================\n")
  16463. (mapc 'org-print-speed-command org-speed-commands-user)
  16464. (princ "\n")
  16465. (princ "Built-in Speed commands\n=======================\n")
  16466. (mapc 'org-print-speed-command org-speed-commands-default))
  16467. (with-current-buffer "*Help*"
  16468. (setq truncate-lines t))))
  16469. (defun org-speed-move-safe (cmd)
  16470. "Execute CMD, but make sure that the cursor always ends up in a headline.
  16471. If not, return to the original position and throw an error."
  16472. (interactive)
  16473. (let ((pos (point)))
  16474. (call-interactively cmd)
  16475. (unless (and (bolp) (org-at-heading-p))
  16476. (goto-char pos)
  16477. (error "Boundary reached while executing %s" cmd))))
  16478. (defvar org-self-insert-command-undo-counter 0)
  16479. (defvar org-table-auto-blank-field) ; defined in org-table.el
  16480. (defvar org-speed-command nil)
  16481. (defun org-speed-command-default-hook (keys)
  16482. "Hook for activating single-letter speed commands.
  16483. `org-speed-commands-default' specifies a minimal command set.
  16484. Use `org-speed-commands-user' for further customization."
  16485. (when (or (and (bolp) (looking-at org-outline-regexp))
  16486. (and (functionp org-use-speed-commands)
  16487. (funcall org-use-speed-commands)))
  16488. (cdr (assoc keys (append org-speed-commands-user
  16489. org-speed-commands-default)))))
  16490. (defun org-babel-speed-command-hook (keys)
  16491. "Hook for activating single-letter code block commands."
  16492. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  16493. (cdr (assoc keys org-babel-key-bindings))))
  16494. (defcustom org-speed-command-hook
  16495. '(org-speed-command-default-hook org-babel-speed-command-hook)
  16496. "Hook for activating speed commands at strategic locations.
  16497. Hook functions are called in sequence until a valid handler is
  16498. found.
  16499. Each hook takes a single argument, a user-pressed command key
  16500. which is also a `self-insert-command' from the global map.
  16501. Within the hook, examine the cursor position and the command key
  16502. and return nil or a valid handler as appropriate. Handler could
  16503. be one of an interactive command, a function, or a form.
  16504. Set `org-use-speed-commands' to non-nil value to enable this
  16505. hook. The default setting is `org-speed-command-default-hook'."
  16506. :group 'org-structure
  16507. :version "24.1"
  16508. :type 'hook)
  16509. (defun org-self-insert-command (N)
  16510. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  16511. If the cursor is in a table looking at whitespace, the whitespace is
  16512. overwritten, and the table is not marked as requiring realignment."
  16513. (interactive "p")
  16514. (org-check-before-invisible-edit 'insert)
  16515. (cond
  16516. ((and org-use-speed-commands
  16517. (setq org-speed-command
  16518. (run-hook-with-args-until-success
  16519. 'org-speed-command-hook (this-command-keys))))
  16520. (cond
  16521. ((commandp org-speed-command)
  16522. (setq this-command org-speed-command)
  16523. (call-interactively org-speed-command))
  16524. ((functionp org-speed-command)
  16525. (funcall org-speed-command))
  16526. ((and org-speed-command (listp org-speed-command))
  16527. (eval org-speed-command))
  16528. (t (let (org-use-speed-commands)
  16529. (call-interactively 'org-self-insert-command)))))
  16530. ((and
  16531. (org-table-p)
  16532. (progn
  16533. ;; check if we blank the field, and if that triggers align
  16534. (and (featurep 'org-table) org-table-auto-blank-field
  16535. (member last-command
  16536. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  16537. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  16538. ;; got extra space, this field does not determine column width
  16539. (let (org-table-may-need-update) (org-table-blank-field))
  16540. ;; no extra space, this field may determine column width
  16541. (org-table-blank-field)))
  16542. t)
  16543. (eq N 1)
  16544. (looking-at "[^|\n]* |"))
  16545. (let (org-table-may-need-update)
  16546. (goto-char (1- (match-end 0)))
  16547. (backward-delete-char 1)
  16548. (goto-char (match-beginning 0))
  16549. (self-insert-command N)))
  16550. (t
  16551. (setq org-table-may-need-update t)
  16552. (self-insert-command N)
  16553. (org-fix-tags-on-the-fly)
  16554. (if org-self-insert-cluster-for-undo
  16555. (if (not (eq last-command 'org-self-insert-command))
  16556. (setq org-self-insert-command-undo-counter 1)
  16557. (if (>= org-self-insert-command-undo-counter 20)
  16558. (setq org-self-insert-command-undo-counter 1)
  16559. (and (> org-self-insert-command-undo-counter 0)
  16560. buffer-undo-list (listp buffer-undo-list)
  16561. (not (cadr buffer-undo-list)) ; remove nil entry
  16562. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  16563. (setq org-self-insert-command-undo-counter
  16564. (1+ org-self-insert-command-undo-counter))))))))
  16565. (defun org-check-before-invisible-edit (kind)
  16566. "Check is editing if kind KIND would be dangerous with invisible text around.
  16567. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  16568. ;; First, try to get out of here as quickly as possible, to reduce overhead
  16569. (if (and org-catch-invisible-edits
  16570. (or (not (boundp 'visible-mode)) (not visible-mode))
  16571. (or (get-char-property (point) 'invisible)
  16572. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  16573. ;; OK, we need to take a closer look
  16574. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  16575. (invisible-before-point (if (bobp) nil (get-char-property
  16576. (1- (point)) 'invisible)))
  16577. (border-and-ok-direction
  16578. (or
  16579. ;; Check if we are acting predictably before invisible text
  16580. (and invisible-at-point (not invisible-before-point)
  16581. (memq kind '(insert delete-backward)))
  16582. ;; Check if we are acting predictably after invisible text
  16583. ;; This works not well, and I have turned it off. It seems
  16584. ;; better to always show and stop after invisible text.
  16585. ;; (and (not invisible-at-point) invisible-before-point
  16586. ;; (memq kind '(insert delete)))
  16587. )))
  16588. (when (or (memq invisible-at-point '(outline org-hide-block))
  16589. (memq invisible-before-point '(outline org-hide-block)))
  16590. (if (eq org-catch-invisible-edits 'error)
  16591. (error "Editing in invisible areas is prohibited - make visible first"))
  16592. ;; Make the area visible
  16593. (save-excursion
  16594. (if invisible-before-point
  16595. (goto-char (previous-single-char-property-change
  16596. (point) 'invisible)))
  16597. (org-cycle))
  16598. (cond
  16599. ((eq org-catch-invisible-edits 'show)
  16600. ;; That's it, we do the edit after showing
  16601. (message
  16602. "Unfolding invisible region around point before editing")
  16603. (sit-for 1))
  16604. ((and (eq org-catch-invisible-edits 'smart)
  16605. border-and-ok-direction)
  16606. (message "Unfolding invisible region around point before editing"))
  16607. (t
  16608. ;; Don't do the edit, make the user repeat it in full visibility
  16609. (error "Edit in invisible region aborted, repeat to confirm with text visible")))))))
  16610. (defun org-fix-tags-on-the-fly ()
  16611. (when (and (equal (char-after (point-at-bol)) ?*)
  16612. (org-at-heading-p))
  16613. (org-align-tags-here org-tags-column)))
  16614. (defun org-delete-backward-char (N)
  16615. "Like `delete-backward-char', insert whitespace at field end in tables.
  16616. When deleting backwards, in tables this function will insert whitespace in
  16617. front of the next \"|\" separator, to keep the table aligned. The table will
  16618. still be marked for re-alignment if the field did fill the entire column,
  16619. because, in this case the deletion might narrow the column."
  16620. (interactive "p")
  16621. (org-check-before-invisible-edit 'delete-backward)
  16622. (if (and (org-table-p)
  16623. (eq N 1)
  16624. (string-match "|" (buffer-substring (point-at-bol) (point)))
  16625. (looking-at ".*?|"))
  16626. (let ((pos (point))
  16627. (noalign (looking-at "[^|\n\r]* |"))
  16628. (c org-table-may-need-update))
  16629. (backward-delete-char N)
  16630. (if (not overwrite-mode)
  16631. (progn
  16632. (skip-chars-forward "^|")
  16633. (insert " ")
  16634. (goto-char (1- pos))))
  16635. ;; noalign: if there were two spaces at the end, this field
  16636. ;; does not determine the width of the column.
  16637. (if noalign (setq org-table-may-need-update c)))
  16638. (backward-delete-char N)
  16639. (org-fix-tags-on-the-fly)))
  16640. (defun org-delete-char (N)
  16641. "Like `delete-char', but insert whitespace at field end in tables.
  16642. When deleting characters, in tables this function will insert whitespace in
  16643. front of the next \"|\" separator, to keep the table aligned. The table will
  16644. still be marked for re-alignment if the field did fill the entire column,
  16645. because, in this case the deletion might narrow the column."
  16646. (interactive "p")
  16647. (org-check-before-invisible-edit 'delete)
  16648. (if (and (org-table-p)
  16649. (not (bolp))
  16650. (not (= (char-after) ?|))
  16651. (eq N 1))
  16652. (if (looking-at ".*?|")
  16653. (let ((pos (point))
  16654. (noalign (looking-at "[^|\n\r]* |"))
  16655. (c org-table-may-need-update))
  16656. (replace-match (concat
  16657. (substring (match-string 0) 1 -1)
  16658. " |"))
  16659. (goto-char pos)
  16660. ;; noalign: if there were two spaces at the end, this field
  16661. ;; does not determine the width of the column.
  16662. (if noalign (setq org-table-may-need-update c)))
  16663. (delete-char N))
  16664. (delete-char N)
  16665. (org-fix-tags-on-the-fly)))
  16666. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  16667. (put 'org-self-insert-command 'delete-selection t)
  16668. (put 'orgtbl-self-insert-command 'delete-selection t)
  16669. (put 'org-delete-char 'delete-selection 'supersede)
  16670. (put 'org-delete-backward-char 'delete-selection 'supersede)
  16671. (put 'org-yank 'delete-selection 'yank)
  16672. ;; Make `flyspell-mode' delay after some commands
  16673. (put 'org-self-insert-command 'flyspell-delayed t)
  16674. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  16675. (put 'org-delete-char 'flyspell-delayed t)
  16676. (put 'org-delete-backward-char 'flyspell-delayed t)
  16677. ;; Make pabbrev-mode expand after org-mode commands
  16678. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  16679. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  16680. ;; How to do this: Measure non-white length of current string
  16681. ;; If equal to column width, we should realign.
  16682. (defun org-remap (map &rest commands)
  16683. "In MAP, remap the functions given in COMMANDS.
  16684. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  16685. (let (new old)
  16686. (while commands
  16687. (setq old (pop commands) new (pop commands))
  16688. (if (fboundp 'command-remapping)
  16689. (org-defkey map (vector 'remap old) new)
  16690. (substitute-key-definition old new map global-map)))))
  16691. (when (eq org-enable-table-editor 'optimized)
  16692. ;; If the user wants maximum table support, we need to hijack
  16693. ;; some standard editing functions
  16694. (org-remap org-mode-map
  16695. 'self-insert-command 'org-self-insert-command
  16696. 'delete-char 'org-delete-char
  16697. 'delete-backward-char 'org-delete-backward-char)
  16698. (org-defkey org-mode-map "|" 'org-force-self-insert))
  16699. (defvar org-ctrl-c-ctrl-c-hook nil
  16700. "Hook for functions attaching themselves to `C-c C-c'.
  16701. This can be used to add additional functionality to the C-c C-c
  16702. key which executes context-dependent commands. This hook is run
  16703. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  16704. run after the last test.
  16705. Each function will be called with no arguments. The function
  16706. must check if the context is appropriate for it to act. If yes,
  16707. it should do its thing and then return a non-nil value. If the
  16708. context is wrong, just do nothing and return nil.")
  16709. (defvar org-ctrl-c-ctrl-c-final-hook nil
  16710. "Hook for functions attaching themselves to `C-c C-c'.
  16711. This can be used to add additional functionality to the C-c C-c
  16712. key which executes context-dependent commands. This hook is run
  16713. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  16714. before the first test.
  16715. Each function will be called with no arguments. The function
  16716. must check if the context is appropriate for it to act. If yes,
  16717. it should do its thing and then return a non-nil value. If the
  16718. context is wrong, just do nothing and return nil.")
  16719. (defvar org-tab-first-hook nil
  16720. "Hook for functions to attach themselves to TAB.
  16721. See `org-ctrl-c-ctrl-c-hook' for more information.
  16722. This hook runs as the first action when TAB is pressed, even before
  16723. `org-cycle' messes around with the `outline-regexp' to cater for
  16724. inline tasks and plain list item folding.
  16725. If any function in this hook returns t, any other actions that
  16726. would have been caused by TAB (such as table field motion or visibility
  16727. cycling) will not occur.")
  16728. (defvar org-tab-after-check-for-table-hook nil
  16729. "Hook for functions to attach themselves to TAB.
  16730. See `org-ctrl-c-ctrl-c-hook' for more information.
  16731. This hook runs after it has been established that the cursor is not in a
  16732. table, but before checking if the cursor is in a headline or if global cycling
  16733. should be done.
  16734. If any function in this hook returns t, not other actions like visibility
  16735. cycling will be done.")
  16736. (defvar org-tab-after-check-for-cycling-hook nil
  16737. "Hook for functions to attach themselves to TAB.
  16738. See `org-ctrl-c-ctrl-c-hook' for more information.
  16739. This hook runs after it has been established that not table field motion and
  16740. not visibility should be done because of current context. This is probably
  16741. the place where a package like yasnippets can hook in.")
  16742. (defvar org-tab-before-tab-emulation-hook nil
  16743. "Hook for functions to attach themselves to TAB.
  16744. See `org-ctrl-c-ctrl-c-hook' for more information.
  16745. This hook runs after every other options for TAB have been exhausted, but
  16746. before indentation and \t insertion takes place.")
  16747. (defvar org-metaleft-hook nil
  16748. "Hook for functions attaching themselves to `M-left'.
  16749. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16750. (defvar org-metaright-hook nil
  16751. "Hook for functions attaching themselves to `M-right'.
  16752. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16753. (defvar org-metaup-hook nil
  16754. "Hook for functions attaching themselves to `M-up'.
  16755. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16756. (defvar org-metadown-hook nil
  16757. "Hook for functions attaching themselves to `M-down'.
  16758. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16759. (defvar org-shiftmetaleft-hook nil
  16760. "Hook for functions attaching themselves to `M-S-left'.
  16761. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16762. (defvar org-shiftmetaright-hook nil
  16763. "Hook for functions attaching themselves to `M-S-right'.
  16764. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16765. (defvar org-shiftmetaup-hook nil
  16766. "Hook for functions attaching themselves to `M-S-up'.
  16767. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16768. (defvar org-shiftmetadown-hook nil
  16769. "Hook for functions attaching themselves to `M-S-down'.
  16770. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16771. (defvar org-metareturn-hook nil
  16772. "Hook for functions attaching themselves to `M-RET'.
  16773. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16774. (defvar org-shiftup-hook nil
  16775. "Hook for functions attaching themselves to `S-up'.
  16776. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16777. (defvar org-shiftup-final-hook nil
  16778. "Hook for functions attaching themselves to `S-up'.
  16779. This one runs after all other options except shift-select have been excluded.
  16780. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16781. (defvar org-shiftdown-hook nil
  16782. "Hook for functions attaching themselves to `S-down'.
  16783. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16784. (defvar org-shiftdown-final-hook nil
  16785. "Hook for functions attaching themselves to `S-down'.
  16786. This one runs after all other options except shift-select have been excluded.
  16787. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16788. (defvar org-shiftleft-hook nil
  16789. "Hook for functions attaching themselves to `S-left'.
  16790. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16791. (defvar org-shiftleft-final-hook nil
  16792. "Hook for functions attaching themselves to `S-left'.
  16793. This one runs after all other options except shift-select have been excluded.
  16794. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16795. (defvar org-shiftright-hook nil
  16796. "Hook for functions attaching themselves to `S-right'.
  16797. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16798. (defvar org-shiftright-final-hook nil
  16799. "Hook for functions attaching themselves to `S-right'.
  16800. This one runs after all other options except shift-select have been excluded.
  16801. See `org-ctrl-c-ctrl-c-hook' for more information.")
  16802. (defun org-modifier-cursor-error ()
  16803. "Throw an error, a modified cursor command was applied in wrong context."
  16804. (error "This command is active in special context like tables, headlines or items"))
  16805. (defun org-shiftselect-error ()
  16806. "Throw an error because Shift-Cursor command was applied in wrong context."
  16807. (if (and (boundp 'shift-select-mode) shift-select-mode)
  16808. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  16809. (error "This command works only in special context like headlines or timestamps")))
  16810. (defun org-call-for-shift-select (cmd)
  16811. (let ((this-command-keys-shift-translated t))
  16812. (call-interactively cmd)))
  16813. (defun org-shifttab (&optional arg)
  16814. "Global visibility cycling or move to previous table field.
  16815. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  16816. on context.
  16817. See the individual commands for more information."
  16818. (interactive "P")
  16819. (cond
  16820. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  16821. ((integerp arg)
  16822. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  16823. (message "Content view to level: %d" arg)
  16824. (org-content (prefix-numeric-value arg2))
  16825. (setq org-cycle-global-status 'overview)))
  16826. (t (call-interactively 'org-global-cycle))))
  16827. (defun org-shiftmetaleft ()
  16828. "Promote subtree or delete table column.
  16829. Calls `org-promote-subtree', `org-outdent-item-tree', or
  16830. `org-table-delete-column', depending on context. See the
  16831. individual commands for more information."
  16832. (interactive)
  16833. (cond
  16834. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  16835. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  16836. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  16837. ((if (not (org-region-active-p)) (org-at-item-p)
  16838. (save-excursion (goto-char (region-beginning))
  16839. (org-at-item-p)))
  16840. (call-interactively 'org-outdent-item-tree))
  16841. (t (org-modifier-cursor-error))))
  16842. (defun org-shiftmetaright ()
  16843. "Demote subtree or insert table column.
  16844. Calls `org-demote-subtree', `org-indent-item-tree', or
  16845. `org-table-insert-column', depending on context. See the
  16846. individual commands for more information."
  16847. (interactive)
  16848. (cond
  16849. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  16850. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  16851. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  16852. ((if (not (org-region-active-p)) (org-at-item-p)
  16853. (save-excursion (goto-char (region-beginning))
  16854. (org-at-item-p)))
  16855. (call-interactively 'org-indent-item-tree))
  16856. (t (org-modifier-cursor-error))))
  16857. (defun org-shiftmetaup (&optional arg)
  16858. "Move subtree up or kill table row.
  16859. Calls `org-move-subtree-up' or `org-table-kill-row' or
  16860. `org-move-item-up' or `org-timestamp-up', depending on context.
  16861. See the individual commands for more information."
  16862. (interactive "P")
  16863. (cond
  16864. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  16865. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  16866. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16867. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16868. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  16869. (call-interactively 'org-timestamp-up)))
  16870. (t (org-modifier-cursor-error))))
  16871. (defun org-shiftmetadown (&optional arg)
  16872. "Move subtree down or insert table row.
  16873. Calls `org-move-subtree-down' or `org-table-insert-row' or
  16874. `org-move-item-down' or `org-timestamp-up', depending on context.
  16875. See the individual commands for more information."
  16876. (interactive "P")
  16877. (cond
  16878. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  16879. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  16880. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  16881. ((org-at-item-p) (call-interactively 'org-move-item-down))
  16882. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  16883. (call-interactively 'org-timestamp-down)))
  16884. (t (org-modifier-cursor-error))))
  16885. (defsubst org-hidden-tree-error ()
  16886. (error
  16887. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  16888. (defun org-metaleft (&optional arg)
  16889. "Promote heading or move table column to left.
  16890. Calls `org-do-promote' or `org-table-move-column', depending on context.
  16891. With no specific context, calls the Emacs default `backward-word'.
  16892. See the individual commands for more information."
  16893. (interactive "P")
  16894. (cond
  16895. ((run-hook-with-args-until-success 'org-metaleft-hook))
  16896. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  16897. ((org-with-limited-levels
  16898. (or (org-at-heading-p)
  16899. (and (org-region-active-p)
  16900. (save-excursion
  16901. (goto-char (region-beginning))
  16902. (org-at-heading-p)))))
  16903. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16904. (call-interactively 'org-do-promote))
  16905. ;; At an inline task.
  16906. ((org-at-heading-p)
  16907. (call-interactively 'org-inlinetask-promote))
  16908. ((or (org-at-item-p)
  16909. (and (org-region-active-p)
  16910. (save-excursion
  16911. (goto-char (region-beginning))
  16912. (org-at-item-p))))
  16913. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16914. (call-interactively 'org-outdent-item))
  16915. (t (call-interactively 'backward-word))))
  16916. (defun org-metaright (&optional arg)
  16917. "Demote a subtree, a list item or move table column to right.
  16918. In front of a drawer or a block keyword, indent it correctly.
  16919. With no specific context, calls the Emacs default `forward-word'.
  16920. See the individual commands for more information."
  16921. (interactive "P")
  16922. (cond
  16923. ((run-hook-with-args-until-success 'org-metaright-hook))
  16924. ((org-at-table-p) (call-interactively 'org-table-move-column))
  16925. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  16926. ((org-at-block-p) (call-interactively 'org-indent-block))
  16927. ((org-with-limited-levels
  16928. (or (org-at-heading-p)
  16929. (and (org-region-active-p)
  16930. (save-excursion
  16931. (goto-char (region-beginning))
  16932. (org-at-heading-p)))))
  16933. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  16934. (call-interactively 'org-do-demote))
  16935. ;; At an inline task.
  16936. ((org-at-heading-p)
  16937. (call-interactively 'org-inlinetask-demote))
  16938. ((or (org-at-item-p)
  16939. (and (org-region-active-p)
  16940. (save-excursion
  16941. (goto-char (region-beginning))
  16942. (org-at-item-p))))
  16943. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  16944. (call-interactively 'org-indent-item))
  16945. (t (call-interactively 'forward-word))))
  16946. (defun org-check-for-hidden (what)
  16947. "Check if there are hidden headlines/items in the current visual line.
  16948. WHAT can be either `headlines' or `items'. If the current line is
  16949. an outline or item heading and it has a folded subtree below it,
  16950. this function returns t, nil otherwise."
  16951. (let ((re (cond
  16952. ((eq what 'headlines) org-outline-regexp-bol)
  16953. ((eq what 'items) (org-item-beginning-re))
  16954. (t (error "This should not happen"))))
  16955. beg end)
  16956. (save-excursion
  16957. (catch 'exit
  16958. (unless (org-region-active-p)
  16959. (setq beg (point-at-bol))
  16960. (beginning-of-line 2)
  16961. (while (and (not (eobp)) ;; this is like `next-line'
  16962. (get-char-property (1- (point)) 'invisible))
  16963. (beginning-of-line 2))
  16964. (setq end (point))
  16965. (goto-char beg)
  16966. (goto-char (point-at-eol))
  16967. (setq end (max end (point)))
  16968. (while (re-search-forward re end t)
  16969. (if (get-char-property (match-beginning 0) 'invisible)
  16970. (throw 'exit t))))
  16971. nil))))
  16972. (defun org-metaup (&optional arg)
  16973. "Move subtree up or move table row up.
  16974. Calls `org-move-subtree-up' or `org-table-move-row' or
  16975. `org-move-item-up', depending on context. See the individual commands
  16976. for more information."
  16977. (interactive "P")
  16978. (cond
  16979. ((run-hook-with-args-until-success 'org-metaup-hook))
  16980. ((org-region-active-p)
  16981. (let* ((a (min (region-beginning) (region-end)))
  16982. (b (1- (max (region-beginning) (region-end))))
  16983. (c (save-excursion (goto-char a)
  16984. (move-beginning-of-line 0)))
  16985. (d (save-excursion (goto-char a)
  16986. (move-end-of-line 0) (point))))
  16987. (transpose-regions a b c d)
  16988. (goto-char c)))
  16989. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  16990. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  16991. ((org-at-item-p) (call-interactively 'org-move-item-up))
  16992. (t (org-element-drag-backward))))
  16993. (defun org-metadown (&optional arg)
  16994. "Move subtree down or move table row down.
  16995. Calls `org-move-subtree-down' or `org-table-move-row' or
  16996. `org-move-item-down', depending on context. See the individual
  16997. commands for more information."
  16998. (interactive "P")
  16999. (cond
  17000. ((run-hook-with-args-until-success 'org-metadown-hook))
  17001. ((org-region-active-p)
  17002. (let* ((a (min (region-beginning) (region-end)))
  17003. (b (max (region-beginning) (region-end)))
  17004. (c (save-excursion (goto-char b)
  17005. (move-beginning-of-line 1)))
  17006. (d (save-excursion (goto-char b)
  17007. (move-end-of-line 1) (1+ (point)))))
  17008. (transpose-regions a b c d)
  17009. (goto-char d)))
  17010. ((org-at-table-p) (call-interactively 'org-table-move-row))
  17011. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  17012. ((org-at-item-p) (call-interactively 'org-move-item-down))
  17013. (t (org-element-drag-forward))))
  17014. (defun org-shiftup (&optional arg)
  17015. "Increase item in timestamp or increase priority of current headline.
  17016. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  17017. depending on context. See the individual commands for more information."
  17018. (interactive "P")
  17019. (cond
  17020. ((run-hook-with-args-until-success 'org-shiftup-hook))
  17021. ((and org-support-shift-select (org-region-active-p))
  17022. (org-call-for-shift-select 'previous-line))
  17023. ((org-at-timestamp-p t)
  17024. (call-interactively (if org-edit-timestamp-down-means-later
  17025. 'org-timestamp-down 'org-timestamp-up)))
  17026. ((and (not (eq org-support-shift-select 'always))
  17027. org-enable-priority-commands
  17028. (org-at-heading-p))
  17029. (call-interactively 'org-priority-up))
  17030. ((and (not org-support-shift-select) (org-at-item-p))
  17031. (call-interactively 'org-previous-item))
  17032. ((org-clocktable-try-shift 'up arg))
  17033. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  17034. (org-support-shift-select
  17035. (org-call-for-shift-select 'previous-line))
  17036. (t (org-shiftselect-error))))
  17037. (defun org-shiftdown (&optional arg)
  17038. "Decrease item in timestamp or decrease priority of current headline.
  17039. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  17040. depending on context. See the individual commands for more information."
  17041. (interactive "P")
  17042. (cond
  17043. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  17044. ((and org-support-shift-select (org-region-active-p))
  17045. (org-call-for-shift-select 'next-line))
  17046. ((org-at-timestamp-p t)
  17047. (call-interactively (if org-edit-timestamp-down-means-later
  17048. 'org-timestamp-up 'org-timestamp-down)))
  17049. ((and (not (eq org-support-shift-select 'always))
  17050. org-enable-priority-commands
  17051. (org-at-heading-p))
  17052. (call-interactively 'org-priority-down))
  17053. ((and (not org-support-shift-select) (org-at-item-p))
  17054. (call-interactively 'org-next-item))
  17055. ((org-clocktable-try-shift 'down arg))
  17056. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  17057. (org-support-shift-select
  17058. (org-call-for-shift-select 'next-line))
  17059. (t (org-shiftselect-error))))
  17060. (defun org-shiftright (&optional arg)
  17061. "Cycle the thing at point or in the current line, depending on context.
  17062. Depending on context, this does one of the following:
  17063. - switch a timestamp at point one day into the future
  17064. - on a headline, switch to the next TODO keyword.
  17065. - on an item, switch entire list to the next bullet type
  17066. - on a property line, switch to the next allowed value
  17067. - on a clocktable definition line, move time block into the future"
  17068. (interactive "P")
  17069. (cond
  17070. ((run-hook-with-args-until-success 'org-shiftright-hook))
  17071. ((and org-support-shift-select (org-region-active-p))
  17072. (org-call-for-shift-select 'forward-char))
  17073. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  17074. ((and (not (eq org-support-shift-select 'always))
  17075. (org-at-heading-p))
  17076. (let ((org-inhibit-logging
  17077. (not org-treat-S-cursor-todo-selection-as-state-change))
  17078. (org-inhibit-blocking
  17079. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17080. (org-call-with-arg 'org-todo 'right)))
  17081. ((or (and org-support-shift-select
  17082. (not (eq org-support-shift-select 'always))
  17083. (org-at-item-bullet-p))
  17084. (and (not org-support-shift-select) (org-at-item-p)))
  17085. (org-call-with-arg 'org-cycle-list-bullet nil))
  17086. ((and (not (eq org-support-shift-select 'always))
  17087. (org-at-property-p))
  17088. (call-interactively 'org-property-next-allowed-value))
  17089. ((org-clocktable-try-shift 'right arg))
  17090. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  17091. (org-support-shift-select
  17092. (org-call-for-shift-select 'forward-char))
  17093. (t (org-shiftselect-error))))
  17094. (defun org-shiftleft (&optional arg)
  17095. "Cycle the thing at point or in the current line, depending on context.
  17096. Depending on context, this does one of the following:
  17097. - switch a timestamp at point one day into the past
  17098. - on a headline, switch to the previous TODO keyword.
  17099. - on an item, switch entire list to the previous bullet type
  17100. - on a property line, switch to the previous allowed value
  17101. - on a clocktable definition line, move time block into the past"
  17102. (interactive "P")
  17103. (cond
  17104. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  17105. ((and org-support-shift-select (org-region-active-p))
  17106. (org-call-for-shift-select 'backward-char))
  17107. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  17108. ((and (not (eq org-support-shift-select 'always))
  17109. (org-at-heading-p))
  17110. (let ((org-inhibit-logging
  17111. (not org-treat-S-cursor-todo-selection-as-state-change))
  17112. (org-inhibit-blocking
  17113. (not org-treat-S-cursor-todo-selection-as-state-change)))
  17114. (org-call-with-arg 'org-todo 'left)))
  17115. ((or (and org-support-shift-select
  17116. (not (eq org-support-shift-select 'always))
  17117. (org-at-item-bullet-p))
  17118. (and (not org-support-shift-select) (org-at-item-p)))
  17119. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  17120. ((and (not (eq org-support-shift-select 'always))
  17121. (org-at-property-p))
  17122. (call-interactively 'org-property-previous-allowed-value))
  17123. ((org-clocktable-try-shift 'left arg))
  17124. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  17125. (org-support-shift-select
  17126. (org-call-for-shift-select 'backward-char))
  17127. (t (org-shiftselect-error))))
  17128. (defun org-shiftcontrolright ()
  17129. "Switch to next TODO set."
  17130. (interactive)
  17131. (cond
  17132. ((and org-support-shift-select (org-region-active-p))
  17133. (org-call-for-shift-select 'forward-word))
  17134. ((and (not (eq org-support-shift-select 'always))
  17135. (org-at-heading-p))
  17136. (org-call-with-arg 'org-todo 'nextset))
  17137. (org-support-shift-select
  17138. (org-call-for-shift-select 'forward-word))
  17139. (t (org-shiftselect-error))))
  17140. (defun org-shiftcontrolleft ()
  17141. "Switch to previous TODO set."
  17142. (interactive)
  17143. (cond
  17144. ((and org-support-shift-select (org-region-active-p))
  17145. (org-call-for-shift-select 'backward-word))
  17146. ((and (not (eq org-support-shift-select 'always))
  17147. (org-at-heading-p))
  17148. (org-call-with-arg 'org-todo 'previousset))
  17149. (org-support-shift-select
  17150. (org-call-for-shift-select 'backward-word))
  17151. (t (org-shiftselect-error))))
  17152. (defun org-shiftcontrolup ()
  17153. "Change timestamps synchronously up in CLOCK log lines."
  17154. (interactive)
  17155. (cond ((and (not org-support-shift-select)
  17156. (org-at-clock-log-p)
  17157. (org-at-timestamp-p t))
  17158. (org-clock-timestamps-up))
  17159. (t (org-shiftselect-error))))
  17160. (defun org-shiftcontroldown ()
  17161. "Change timestamps synchronously down in CLOCK log lines."
  17162. (interactive)
  17163. (cond ((and (not org-support-shift-select)
  17164. (org-at-clock-log-p)
  17165. (org-at-timestamp-p t))
  17166. (org-clock-timestamps-down))
  17167. (t (org-shiftselect-error))))
  17168. (defun org-ctrl-c-ret ()
  17169. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  17170. (interactive)
  17171. (cond
  17172. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  17173. (t (call-interactively 'org-insert-heading))))
  17174. (defun org-find-visible ()
  17175. (let ((s (point)))
  17176. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17177. (get-char-property s 'invisible)))
  17178. s))
  17179. (defun org-find-invisible ()
  17180. (let ((s (point)))
  17181. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  17182. (not (get-char-property s 'invisible))))
  17183. s))
  17184. (defun org-copy-visible (beg end)
  17185. "Copy the visible parts of the region."
  17186. (interactive "r")
  17187. (let (snippets s)
  17188. (save-excursion
  17189. (save-restriction
  17190. (narrow-to-region beg end)
  17191. (setq s (goto-char (point-min)))
  17192. (while (not (= (point) (point-max)))
  17193. (goto-char (org-find-invisible))
  17194. (push (buffer-substring s (point)) snippets)
  17195. (setq s (goto-char (org-find-visible))))))
  17196. (kill-new (apply 'concat (nreverse snippets)))))
  17197. (defun org-copy-special ()
  17198. "Copy region in table or copy current subtree.
  17199. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  17200. See the individual commands for more information."
  17201. (interactive)
  17202. (call-interactively
  17203. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  17204. (defun org-cut-special ()
  17205. "Cut region in table or cut current subtree.
  17206. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  17207. See the individual commands for more information."
  17208. (interactive)
  17209. (call-interactively
  17210. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  17211. (defun org-paste-special (arg)
  17212. "Paste rectangular region into table, or past subtree relative to level.
  17213. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  17214. See the individual commands for more information."
  17215. (interactive "P")
  17216. (if (org-at-table-p)
  17217. (org-table-paste-rectangle)
  17218. (org-paste-subtree arg)))
  17219. (defun org-edit-special (&optional arg)
  17220. "Call a special editor for the stuff at point.
  17221. When at a table, call the formula editor with `org-table-edit-formulas'.
  17222. When at the first line of an src example, call `org-edit-src-code'.
  17223. When in an #+include line, visit the include file. Otherwise call
  17224. `ffap' to visit the file at point."
  17225. (interactive)
  17226. ;; possibly prep session before editing source
  17227. (when arg
  17228. (let* ((info (org-babel-get-src-block-info))
  17229. (lang (nth 0 info))
  17230. (params (nth 2 info))
  17231. (session (cdr (assoc :session params))))
  17232. (when (and info session) ;; we are in a source-code block with a session
  17233. (funcall
  17234. (intern (concat "org-babel-prep-session:" lang)) session params))))
  17235. (cond ;; proceed with `org-edit-special'
  17236. ((save-excursion
  17237. (beginning-of-line 1)
  17238. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  17239. (find-file (org-trim (match-string 1))))
  17240. ((org-edit-src-code))
  17241. ((org-edit-fixed-width-region))
  17242. ((org-at-table.el-p)
  17243. (org-edit-src-code))
  17244. ((or (org-at-table-p)
  17245. (save-excursion
  17246. (beginning-of-line 1)
  17247. (let ((case-fold-search )) (looking-at "[ \t]*#\\+tblfm:"))))
  17248. (call-interactively 'org-table-edit-formulas))
  17249. (t (call-interactively 'ffap))))
  17250. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  17251. (defun org-ctrl-c-ctrl-c (&optional arg)
  17252. "Set tags in headline, or update according to changed information at point.
  17253. This command does many different things, depending on context:
  17254. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  17255. this is what we do.
  17256. - If the cursor is on a statistics cookie, update it.
  17257. - If the cursor is in a headline, prompt for tags and insert them
  17258. into the current line, aligned to `org-tags-column'. When called
  17259. with prefix arg, realign all tags in the current buffer.
  17260. - If the cursor is in one of the special #+KEYWORD lines, this
  17261. triggers scanning the buffer for these lines and updating the
  17262. information.
  17263. - If the cursor is inside a table, realign the table. This command
  17264. works even if the automatic table editor has been turned off.
  17265. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  17266. the entire table.
  17267. - If the cursor is at a footnote reference or definition, jump to
  17268. the corresponding definition or references, respectively.
  17269. - If the cursor is a the beginning of a dynamic block, update it.
  17270. - If the current buffer is a capture buffer, close note and file it.
  17271. - If the cursor is on a <<<target>>>, update radio targets and
  17272. corresponding links in this buffer.
  17273. - If the cursor is on a numbered item in a plain list, renumber the
  17274. ordered list.
  17275. - If the cursor is on a checkbox, toggle it.
  17276. - If the cursor is on a code block, evaluate it. The variable
  17277. `org-confirm-babel-evaluate' can be used to control prompting
  17278. before code block evaluation, by default every code block
  17279. evaluation requires confirmation. Code block evaluation can be
  17280. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  17281. (interactive "P")
  17282. (let ((org-enable-table-editor t))
  17283. (cond
  17284. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  17285. org-occur-highlights
  17286. org-latex-fragment-image-overlays)
  17287. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  17288. (org-remove-occur-highlights)
  17289. (org-remove-latex-fragment-image-overlays)
  17290. (message "Temporary highlights/overlays removed from current buffer"))
  17291. ((and (local-variable-p 'org-finish-function (current-buffer))
  17292. (fboundp org-finish-function))
  17293. (funcall org-finish-function))
  17294. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  17295. ((org-in-regexp org-ts-regexp-both)
  17296. (org-timestamp-change 0 'day))
  17297. ((or (looking-at org-property-start-re)
  17298. (org-at-property-p))
  17299. (call-interactively 'org-property-action))
  17300. ((org-at-target-p) (call-interactively 'org-update-radio-target-regexp))
  17301. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  17302. (or (org-at-heading-p) (org-at-item-p)))
  17303. (call-interactively 'org-update-statistics-cookies))
  17304. ((org-at-heading-p) (call-interactively 'org-set-tags))
  17305. ((org-at-table.el-p)
  17306. (message "Use C-c ' to edit table.el tables"))
  17307. ((org-at-table-p)
  17308. (org-table-maybe-eval-formula)
  17309. (if arg
  17310. (call-interactively 'org-table-recalculate)
  17311. (org-table-maybe-recalculate-line))
  17312. (call-interactively 'org-table-align)
  17313. (orgtbl-send-table 'maybe))
  17314. ((or (org-footnote-at-reference-p)
  17315. (org-footnote-at-definition-p))
  17316. (call-interactively 'org-footnote-action))
  17317. ((org-at-item-checkbox-p)
  17318. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  17319. ;; list only if at top item.
  17320. (let* ((cbox (match-string 1))
  17321. (struct (org-list-struct))
  17322. (old-struct (copy-tree struct))
  17323. (parents (org-list-parents-alist struct))
  17324. (orderedp (org-entry-get nil "ORDERED"))
  17325. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17326. block-item)
  17327. ;; Use a light version of `org-toggle-checkbox' to avoid
  17328. ;; computing list structure twice.
  17329. (let ((new-box (cond
  17330. ((equal arg '(16)) "[-]")
  17331. ((equal arg '(4)) nil)
  17332. ((equal "[X]" cbox) "[ ]")
  17333. (t "[X]"))))
  17334. (if (and firstp arg)
  17335. ;; If at first item of sub-list, remove check-box from
  17336. ;; every item at the same level.
  17337. (mapc
  17338. (lambda (pos) (org-list-set-checkbox pos struct new-box))
  17339. (org-list-get-all-items
  17340. (point-at-bol) struct (org-list-prevs-alist struct)))
  17341. (org-list-set-checkbox (point-at-bol) struct new-box)))
  17342. ;; Replicate `org-list-write-struct', while grabbing a return
  17343. ;; value from `org-list-struct-fix-box'.
  17344. (org-list-struct-fix-ind struct parents 2)
  17345. (org-list-struct-fix-item-end struct)
  17346. (let ((prevs (org-list-prevs-alist struct)))
  17347. (org-list-struct-fix-bul struct prevs)
  17348. (org-list-struct-fix-ind struct parents)
  17349. (setq block-item
  17350. (org-list-struct-fix-box struct parents prevs orderedp)))
  17351. (org-list-struct-apply-struct struct old-struct)
  17352. (org-update-checkbox-count-maybe)
  17353. (when block-item
  17354. (message
  17355. "Checkboxes were removed due to unchecked box at line %d"
  17356. (org-current-line block-item)))
  17357. (when firstp (org-list-send-list 'maybe))))
  17358. ((org-at-item-p)
  17359. ;; Cursor at an item: repair list. Do checkbox related actions
  17360. ;; only if function was called with an argument. Send list only
  17361. ;; if at top item.
  17362. (let* ((struct (org-list-struct))
  17363. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  17364. old-struct)
  17365. (when arg
  17366. (setq old-struct (copy-tree struct))
  17367. (if firstp
  17368. ;; If at first item of sub-list, add check-box to every
  17369. ;; item at the same level.
  17370. (mapc
  17371. (lambda (pos)
  17372. (unless (org-list-get-checkbox pos struct)
  17373. (org-list-set-checkbox pos struct "[ ]")))
  17374. (org-list-get-all-items
  17375. (point-at-bol) struct (org-list-prevs-alist struct)))
  17376. (org-list-set-checkbox (point-at-bol) struct "[ ]")))
  17377. (org-list-write-struct
  17378. struct (org-list-parents-alist struct) old-struct)
  17379. (when arg (org-update-checkbox-count-maybe))
  17380. (when firstp (org-list-send-list 'maybe))))
  17381. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  17382. ;; Dynamic block
  17383. (beginning-of-line 1)
  17384. (save-excursion (org-update-dblock)))
  17385. ((save-excursion
  17386. (let ((case-fold-search t))
  17387. (beginning-of-line 1)
  17388. (looking-at "[ \t]*#\\+\\([a-z]+\\)")))
  17389. (cond
  17390. ((or (equal (match-string 1) "TBLFM")
  17391. (equal (match-string 1) "tblfm"))
  17392. ;; Recalculate the table before this line
  17393. (save-excursion
  17394. (beginning-of-line 1)
  17395. (skip-chars-backward " \r\n\t")
  17396. (if (org-at-table-p)
  17397. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  17398. (t
  17399. (let ((org-inhibit-startup-visibility-stuff t)
  17400. (org-startup-align-all-tables nil))
  17401. (when (boundp 'org-table-coordinate-overlays)
  17402. (mapc 'delete-overlay org-table-coordinate-overlays)
  17403. (setq org-table-coordinate-overlays nil))
  17404. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  17405. (message "Local setup has been refreshed"))))
  17406. ((org-clock-update-time-maybe))
  17407. (t
  17408. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  17409. (error "C-c C-c can do nothing useful at this location"))))))
  17410. (defun org-mode-restart ()
  17411. "Restart Org-mode, to scan again for special lines.
  17412. Also updates the keyword regular expressions."
  17413. (interactive)
  17414. (org-mode)
  17415. (message "Org-mode restarted"))
  17416. (defun org-kill-note-or-show-branches ()
  17417. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  17418. (interactive)
  17419. (if (not org-finish-function)
  17420. (progn
  17421. (hide-subtree)
  17422. (call-interactively 'show-branches))
  17423. (let ((org-note-abort t))
  17424. (funcall org-finish-function))))
  17425. (defun org-return (&optional indent)
  17426. "Goto next table row or insert a newline.
  17427. Calls `org-table-next-row' or `newline', depending on context.
  17428. See the individual commands for more information."
  17429. (interactive)
  17430. (let (org-ts-what)
  17431. (cond
  17432. ((or (bobp) (org-in-src-block-p))
  17433. (if indent (newline-and-indent) (newline)))
  17434. ((org-at-table-p)
  17435. (org-table-justify-field-maybe)
  17436. (call-interactively 'org-table-next-row))
  17437. ;; when `newline-and-indent' is called within a list, make sure
  17438. ;; text moved stays inside the item.
  17439. ((and (org-in-item-p) indent)
  17440. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  17441. (progn
  17442. (save-match-data (newline))
  17443. (org-indent-line-to (length (match-string 0))))
  17444. (let ((ind (org-get-indentation)))
  17445. (newline)
  17446. (if (org-looking-back org-list-end-re)
  17447. (org-indent-line)
  17448. (org-indent-line-to ind)))))
  17449. ((and org-return-follows-link
  17450. (org-at-timestamp-p t)
  17451. (not (eq org-ts-what 'after)))
  17452. (org-follow-timestamp-link))
  17453. ((and org-return-follows-link
  17454. (let ((tprop (get-text-property (point) 'face)))
  17455. (or (eq tprop 'org-link)
  17456. (and (listp tprop) (memq 'org-link tprop)))))
  17457. (call-interactively 'org-open-at-point))
  17458. ((and (org-at-heading-p)
  17459. (looking-at
  17460. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  17461. (org-show-entry)
  17462. (end-of-line 1)
  17463. (newline))
  17464. (t (if indent (newline-and-indent) (newline))))))
  17465. (defun org-return-indent ()
  17466. "Goto next table row or insert a newline and indent.
  17467. Calls `org-table-next-row' or `newline-and-indent', depending on
  17468. context. See the individual commands for more information."
  17469. (interactive)
  17470. (org-return t))
  17471. (defun org-ctrl-c-star ()
  17472. "Compute table, or change heading status of lines.
  17473. Calls `org-table-recalculate' or `org-toggle-heading',
  17474. depending on context."
  17475. (interactive)
  17476. (cond
  17477. ((org-at-table-p)
  17478. (call-interactively 'org-table-recalculate))
  17479. (t
  17480. ;; Convert all lines in region to list items
  17481. (call-interactively 'org-toggle-heading))))
  17482. (defun org-ctrl-c-minus ()
  17483. "Insert separator line in table or modify bullet status of line.
  17484. Also turns a plain line or a region of lines into list items.
  17485. Calls `org-table-insert-hline', `org-toggle-item', or
  17486. `org-cycle-list-bullet', depending on context."
  17487. (interactive)
  17488. (cond
  17489. ((org-at-table-p)
  17490. (call-interactively 'org-table-insert-hline))
  17491. ((org-region-active-p)
  17492. (call-interactively 'org-toggle-item))
  17493. ((org-in-item-p)
  17494. (call-interactively 'org-cycle-list-bullet))
  17495. (t
  17496. (call-interactively 'org-toggle-item))))
  17497. (defun org-toggle-item (arg)
  17498. "Convert headings or normal lines to items, items to normal lines.
  17499. If there is no active region, only the current line is considered.
  17500. If the first non blank line in the region is an headline, convert
  17501. all headlines to items, shifting text accordingly.
  17502. If it is an item, convert all items to normal lines.
  17503. If it is normal text, change region into an item. With a prefix
  17504. argument ARG, change each line in region into an item."
  17505. (interactive "P")
  17506. (let ((shift-text
  17507. (function
  17508. ;; Shift text in current section to IND, from point to END.
  17509. ;; The function leaves point to END line.
  17510. (lambda (ind end)
  17511. (let ((min-i 1000) (end (copy-marker end)))
  17512. ;; First determine the minimum indentation (MIN-I) of
  17513. ;; the text.
  17514. (save-excursion
  17515. (catch 'exit
  17516. (while (< (point) end)
  17517. (let ((i (org-get-indentation)))
  17518. (cond
  17519. ;; Skip blank lines and inline tasks.
  17520. ((looking-at "^[ \t]*$"))
  17521. ((looking-at org-outline-regexp-bol))
  17522. ;; We can't find less than 0 indentation.
  17523. ((zerop i) (throw 'exit (setq min-i 0)))
  17524. ((< i min-i) (setq min-i i))))
  17525. (forward-line))))
  17526. ;; Then indent each line so that a line indented to
  17527. ;; MIN-I becomes indented to IND. Ignore blank lines
  17528. ;; and inline tasks in the process.
  17529. (let ((delta (- ind min-i)))
  17530. (while (< (point) end)
  17531. (unless (or (looking-at "^[ \t]*$")
  17532. (looking-at org-outline-regexp-bol))
  17533. (org-indent-line-to (+ (org-get-indentation) delta)))
  17534. (forward-line)))))))
  17535. (skip-blanks
  17536. (function
  17537. ;; Return beginning of first non-blank line, starting from
  17538. ;; line at POS.
  17539. (lambda (pos)
  17540. (save-excursion
  17541. (goto-char pos)
  17542. (skip-chars-forward " \r\t\n")
  17543. (point-at-bol)))))
  17544. beg end)
  17545. ;; Determine boundaries of changes.
  17546. (if (org-region-active-p)
  17547. (setq beg (funcall skip-blanks (region-beginning))
  17548. end (copy-marker (region-end)))
  17549. (setq beg (funcall skip-blanks (point-at-bol))
  17550. end (copy-marker (point-at-eol))))
  17551. ;; Depending on the starting line, choose an action on the text
  17552. ;; between BEG and END.
  17553. (org-with-limited-levels
  17554. (save-excursion
  17555. (goto-char beg)
  17556. (cond
  17557. ;; Case 1. Start at an item: de-itemize. Note that it only
  17558. ;; happens when a region is active: `org-ctrl-c-minus'
  17559. ;; would call `org-cycle-list-bullet' otherwise.
  17560. ((org-at-item-p)
  17561. (while (< (point) end)
  17562. (when (org-at-item-p)
  17563. (skip-chars-forward " \t")
  17564. (delete-region (point) (match-end 0)))
  17565. (forward-line)))
  17566. ;; Case 2. Start at an heading: convert to items.
  17567. ((org-at-heading-p)
  17568. (let* ((bul (org-list-bullet-string "-"))
  17569. (bul-len (length bul))
  17570. ;; Indentation of the first heading. It should be
  17571. ;; relative to the indentation of its parent, if any.
  17572. (start-ind (save-excursion
  17573. (cond
  17574. ((not org-adapt-indentation) 0)
  17575. ((not (outline-previous-heading)) 0)
  17576. (t (length (match-string 0))))))
  17577. ;; Level of first heading. Further headings will be
  17578. ;; compared to it to determine hierarchy in the list.
  17579. (ref-level (org-reduced-level (org-outline-level))))
  17580. (while (< (point) end)
  17581. (let* ((level (org-reduced-level (org-outline-level)))
  17582. (delta (max 0 (- level ref-level))))
  17583. ;; If current headline is less indented than the first
  17584. ;; one, set it as reference, in order to preserve
  17585. ;; subtrees.
  17586. (when (< level ref-level) (setq ref-level level))
  17587. (replace-match bul t t)
  17588. (org-indent-line-to (+ start-ind (* delta bul-len)))
  17589. ;; Ensure all text down to END (or SECTION-END) belongs
  17590. ;; to the newly created item.
  17591. (let ((section-end (save-excursion
  17592. (or (outline-next-heading) (point)))))
  17593. (forward-line)
  17594. (funcall shift-text
  17595. (+ start-ind (* (1+ delta) bul-len))
  17596. (min end section-end)))))))
  17597. ;; Case 3. Normal line with ARG: turn each non-item line into
  17598. ;; an item.
  17599. (arg
  17600. (while (< (point) end)
  17601. (unless (or (org-at-heading-p) (org-at-item-p))
  17602. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  17603. (replace-match
  17604. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  17605. (forward-line)))
  17606. ;; Case 4. Normal line without ARG: make the first line of
  17607. ;; region an item, and shift indentation of others
  17608. ;; lines to set them as item's body.
  17609. (t (let* ((bul (org-list-bullet-string "-"))
  17610. (bul-len (length bul))
  17611. (ref-ind (org-get-indentation)))
  17612. (skip-chars-forward " \t")
  17613. (insert bul)
  17614. (forward-line)
  17615. (while (< (point) end)
  17616. ;; Ensure that lines less indented than first one
  17617. ;; still get included in item body.
  17618. (funcall shift-text
  17619. (+ ref-ind bul-len)
  17620. (min end (save-excursion (or (outline-next-heading)
  17621. (point)))))
  17622. (forward-line)))))))))
  17623. (defun org-toggle-heading (&optional nstars)
  17624. "Convert headings to normal text, or items or text to headings.
  17625. If there is no active region, only the current line is considered.
  17626. With a \\[universal-argument] prefix, convert the whole list at
  17627. point into heading.
  17628. In a region:
  17629. - If the first non blank line is an headline, remove the stars
  17630. from all headlines in the region.
  17631. - If it is a normal line turn each and every normal line (i.e. not an
  17632. heading or an item) in the region into a heading.
  17633. - If it is a plain list item, turn all plain list items into headings.
  17634. When converting a line into a heading, the number of stars is chosen
  17635. such that the lines become children of the current entry. However,
  17636. when a prefix argument is given, its value determines the number of
  17637. stars to add."
  17638. (interactive "P")
  17639. (let ((skip-blanks
  17640. (function
  17641. ;; Return beginning of first non-blank line, starting from
  17642. ;; line at POS.
  17643. (lambda (pos)
  17644. (save-excursion
  17645. (goto-char pos)
  17646. (while (org-at-comment-p) (forward-line))
  17647. (skip-chars-forward " \r\t\n")
  17648. (point-at-bol)))))
  17649. beg end toggled)
  17650. ;; Determine boundaries of changes. If a universal prefix has
  17651. ;; been given, put the list in a region. If region ends at a bol,
  17652. ;; do not consider the last line to be in the region.
  17653. (when (and current-prefix-arg (org-at-item-p))
  17654. (if (equal current-prefix-arg '(4)) (setq current-prefix-arg 1))
  17655. (org-mark-list))
  17656. (if (org-region-active-p)
  17657. (setq beg (funcall skip-blanks (region-beginning))
  17658. end (copy-marker (save-excursion
  17659. (goto-char (region-end))
  17660. (if (bolp) (point) (point-at-eol)))))
  17661. (setq beg (funcall skip-blanks (point-at-bol))
  17662. end (copy-marker (point-at-eol))))
  17663. ;; Ensure inline tasks don't count as headings.
  17664. (org-with-limited-levels
  17665. (save-excursion
  17666. (goto-char beg)
  17667. (cond
  17668. ;; Case 1. Started at an heading: de-star headings.
  17669. ((org-at-heading-p)
  17670. (while (< (point) end)
  17671. (when (org-at-heading-p t)
  17672. (looking-at org-outline-regexp) (replace-match "")
  17673. (setq toggled t))
  17674. (forward-line)))
  17675. ;; Case 2. Started at an item: change items into headlines.
  17676. ;; One star will be added by `org-list-to-subtree'.
  17677. ((org-at-item-p)
  17678. (let* ((stars (make-string
  17679. (if nstars
  17680. ;; subtract the star that will be added again by
  17681. ;; `org-list-to-subtree'
  17682. (1- (prefix-numeric-value current-prefix-arg))
  17683. (or (org-current-level) 0))
  17684. ?*))
  17685. (add-stars
  17686. (cond (nstars "") ; stars from prefix only
  17687. ((equal stars "") "") ; before first heading
  17688. (org-odd-levels-only "*") ; inside heading, odd
  17689. (t "")))) ; inside heading, oddeven
  17690. (while (< (point) end)
  17691. (when (org-at-item-p)
  17692. ;; Pay attention to cases when region ends before list.
  17693. (let* ((struct (org-list-struct))
  17694. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  17695. (save-restriction
  17696. (narrow-to-region (point) list-end)
  17697. (insert
  17698. (org-list-to-subtree
  17699. (org-list-parse-list t)
  17700. '(:istart (concat stars add-stars (funcall get-stars depth))
  17701. :icount (concat stars add-stars (funcall get-stars depth)))))))
  17702. (setq toggled t))
  17703. (forward-line))))
  17704. ;; Case 3. Started at normal text: make every line an heading,
  17705. ;; skipping headlines and items.
  17706. (t (let* ((stars (make-string
  17707. (if nstars
  17708. (prefix-numeric-value current-prefix-arg)
  17709. (or (org-current-level) 0))
  17710. ?*))
  17711. (add-stars
  17712. (cond (nstars "") ; stars from prefix only
  17713. ((equal stars "") "*") ; before first heading
  17714. (org-odd-levels-only "**") ; inside heading, odd
  17715. (t "*"))) ; inside heading, oddeven
  17716. (rpl (concat stars add-stars " ")))
  17717. (while (< (point) end)
  17718. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  17719. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  17720. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  17721. (forward-line)))))))
  17722. (unless toggled (message "Cannot toggle heading from here"))))
  17723. (defun org-meta-return (&optional arg)
  17724. "Insert a new heading or wrap a region in a table.
  17725. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  17726. See the individual commands for more information."
  17727. (interactive "P")
  17728. (cond
  17729. ((run-hook-with-args-until-success 'org-metareturn-hook))
  17730. ((or (org-at-drawer-p) (org-at-property-p))
  17731. (newline-and-indent))
  17732. ((org-at-table-p)
  17733. (call-interactively 'org-table-wrap-region))
  17734. (t (call-interactively 'org-insert-heading))))
  17735. ;;; Menu entries
  17736. ;; Define the Org-mode menus
  17737. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  17738. '("Tbl"
  17739. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  17740. ["Next Field" org-cycle (org-at-table-p)]
  17741. ["Previous Field" org-shifttab (org-at-table-p)]
  17742. ["Next Row" org-return (org-at-table-p)]
  17743. "--"
  17744. ["Blank Field" org-table-blank-field (org-at-table-p)]
  17745. ["Edit Field" org-table-edit-field (org-at-table-p)]
  17746. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  17747. "--"
  17748. ("Column"
  17749. ["Move Column Left" org-metaleft (org-at-table-p)]
  17750. ["Move Column Right" org-metaright (org-at-table-p)]
  17751. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  17752. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  17753. ("Row"
  17754. ["Move Row Up" org-metaup (org-at-table-p)]
  17755. ["Move Row Down" org-metadown (org-at-table-p)]
  17756. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  17757. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  17758. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  17759. "--"
  17760. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  17761. ("Rectangle"
  17762. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  17763. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  17764. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  17765. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  17766. "--"
  17767. ("Calculate"
  17768. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  17769. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  17770. ["Edit Formulas" org-edit-special (org-at-table-p)]
  17771. "--"
  17772. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  17773. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  17774. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  17775. "--"
  17776. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  17777. "--"
  17778. ["Sum Column/Rectangle" org-table-sum
  17779. (or (org-at-table-p) (org-region-active-p))]
  17780. ["Which Column?" org-table-current-column (org-at-table-p)])
  17781. ["Debug Formulas"
  17782. org-table-toggle-formula-debugger
  17783. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  17784. ["Show Col/Row Numbers"
  17785. org-table-toggle-coordinate-overlays
  17786. :style toggle
  17787. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  17788. "--"
  17789. ["Create" org-table-create (and (not (org-at-table-p))
  17790. org-enable-table-editor)]
  17791. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  17792. ["Import from File" org-table-import (not (org-at-table-p))]
  17793. ["Export to File" org-table-export (org-at-table-p)]
  17794. "--"
  17795. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  17796. (easy-menu-define org-org-menu org-mode-map "Org menu"
  17797. '("Org"
  17798. ("Show/Hide"
  17799. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  17800. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  17801. ["Sparse Tree..." org-sparse-tree t]
  17802. ["Reveal Context" org-reveal t]
  17803. ["Show All" show-all t]
  17804. "--"
  17805. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  17806. "--"
  17807. ["New Heading" org-insert-heading t]
  17808. ("Navigate Headings"
  17809. ["Up" outline-up-heading t]
  17810. ["Next" outline-next-visible-heading t]
  17811. ["Previous" outline-previous-visible-heading t]
  17812. ["Next Same Level" outline-forward-same-level t]
  17813. ["Previous Same Level" outline-backward-same-level t]
  17814. "--"
  17815. ["Jump" org-goto t])
  17816. ("Edit Structure"
  17817. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  17818. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  17819. "--"
  17820. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  17821. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  17822. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  17823. "--"
  17824. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  17825. "--"
  17826. ["Copy visible text" org-copy-visible t]
  17827. "--"
  17828. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  17829. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  17830. ["Demote Heading" org-metaright (not (org-at-table-p))]
  17831. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  17832. "--"
  17833. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  17834. "--"
  17835. ["Convert to odd levels" org-convert-to-odd-levels t]
  17836. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  17837. ("Editing"
  17838. ["Emphasis..." org-emphasize t]
  17839. ["Edit Source Example" org-edit-special t]
  17840. "--"
  17841. ["Footnote new/jump" org-footnote-action t]
  17842. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  17843. ("Archive"
  17844. ["Archive (default method)" org-archive-subtree-default t]
  17845. "--"
  17846. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  17847. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  17848. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  17849. )
  17850. "--"
  17851. ("Hyperlinks"
  17852. ["Store Link (Global)" org-store-link t]
  17853. ["Find existing link to here" org-occur-link-in-agenda-files t]
  17854. ["Insert Link" org-insert-link t]
  17855. ["Follow Link" org-open-at-point t]
  17856. "--"
  17857. ["Next link" org-next-link t]
  17858. ["Previous link" org-previous-link t]
  17859. "--"
  17860. ["Descriptive Links"
  17861. org-toggle-link-display
  17862. :style radio
  17863. :selected org-descriptive-links
  17864. ]
  17865. ["Literal Links"
  17866. org-toggle-link-display
  17867. :style radio
  17868. :selected (not org-descriptive-links)])
  17869. "--"
  17870. ("TODO Lists"
  17871. ["TODO/DONE/-" org-todo t]
  17872. ("Select keyword"
  17873. ["Next keyword" org-shiftright (org-at-heading-p)]
  17874. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  17875. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  17876. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  17877. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  17878. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  17879. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  17880. "--"
  17881. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  17882. :selected org-enforce-todo-dependencies :style toggle :active t]
  17883. "Settings for tree at point"
  17884. ["Do Children sequentially" org-toggle-ordered-property :style radio
  17885. :selected (org-entry-get nil "ORDERED")
  17886. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17887. ["Do Children parallel" org-toggle-ordered-property :style radio
  17888. :selected (not (org-entry-get nil "ORDERED"))
  17889. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  17890. "--"
  17891. ["Set Priority" org-priority t]
  17892. ["Priority Up" org-shiftup t]
  17893. ["Priority Down" org-shiftdown t]
  17894. "--"
  17895. ["Get news from all feeds" org-feed-update-all t]
  17896. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  17897. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  17898. ("TAGS and Properties"
  17899. ["Set Tags" org-set-tags-command t]
  17900. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  17901. "--"
  17902. ["Set property" org-set-property t]
  17903. ["Column view of properties" org-columns t]
  17904. ["Insert Column View DBlock" org-insert-columns-dblock t])
  17905. ("Dates and Scheduling"
  17906. ["Timestamp" org-time-stamp t]
  17907. ["Timestamp (inactive)" org-time-stamp-inactive t]
  17908. ("Change Date"
  17909. ["1 Day Later" org-shiftright t]
  17910. ["1 Day Earlier" org-shiftleft t]
  17911. ["1 ... Later" org-shiftup t]
  17912. ["1 ... Earlier" org-shiftdown t])
  17913. ["Compute Time Range" org-evaluate-time-range t]
  17914. ["Schedule Item" org-schedule t]
  17915. ["Deadline" org-deadline t]
  17916. "--"
  17917. ["Custom time format" org-toggle-time-stamp-overlays
  17918. :style radio :selected org-display-custom-times]
  17919. "--"
  17920. ["Goto Calendar" org-goto-calendar t]
  17921. ["Date from Calendar" org-date-from-calendar t]
  17922. "--"
  17923. ["Start/Restart Timer" org-timer-start t]
  17924. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  17925. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  17926. ["Insert Timer String" org-timer t]
  17927. ["Insert Timer Item" org-timer-item t])
  17928. ("Logging work"
  17929. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  17930. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  17931. ["Clock out" org-clock-out t]
  17932. ["Clock cancel" org-clock-cancel t]
  17933. "--"
  17934. ["Mark as default task" org-clock-mark-default-task t]
  17935. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  17936. ["Goto running clock" org-clock-goto t]
  17937. "--"
  17938. ["Display times" org-clock-display t]
  17939. ["Create clock table" org-clock-report t]
  17940. "--"
  17941. ["Record DONE time"
  17942. (progn (setq org-log-done (not org-log-done))
  17943. (message "Switching to %s will %s record a timestamp"
  17944. (car org-done-keywords)
  17945. (if org-log-done "automatically" "not")))
  17946. :style toggle :selected org-log-done])
  17947. "--"
  17948. ["Agenda Command..." org-agenda t]
  17949. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  17950. ("File List for Agenda")
  17951. ("Special views current file"
  17952. ["TODO Tree" org-show-todo-tree t]
  17953. ["Check Deadlines" org-check-deadlines t]
  17954. ["Timeline" org-timeline t]
  17955. ["Tags/Property tree" org-match-sparse-tree t])
  17956. "--"
  17957. ["Export/Publish..." org-export t]
  17958. ("LaTeX"
  17959. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  17960. :selected org-cdlatex-mode]
  17961. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  17962. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  17963. ["Modify math symbol" org-cdlatex-math-modify
  17964. (org-inside-LaTeX-fragment-p)]
  17965. ["Insert citation" org-reftex-citation t]
  17966. "--"
  17967. ["Template for BEAMER" (progn (require 'org-beamer)
  17968. (org-insert-beamer-options-template)) t])
  17969. "--"
  17970. ("MobileOrg"
  17971. ["Push Files and Views" org-mobile-push t]
  17972. ["Get Captured and Flagged" org-mobile-pull t]
  17973. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  17974. "--"
  17975. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  17976. "--"
  17977. ("Documentation"
  17978. ["Show Version" org-version t]
  17979. ["Info Documentation" org-info t])
  17980. ("Customize"
  17981. ["Browse Org Group" org-customize t]
  17982. "--"
  17983. ["Expand This Menu" org-create-customize-menu
  17984. (fboundp 'customize-menu-create)])
  17985. ["Send bug report" org-submit-bug-report t]
  17986. "--"
  17987. ("Refresh/Reload"
  17988. ["Refresh setup current buffer" org-mode-restart t]
  17989. ["Reload Org (after update)" org-reload t]
  17990. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  17991. ))
  17992. (defun org-info (&optional node)
  17993. "Read documentation for Org-mode in the info system.
  17994. With optional NODE, go directly to that node."
  17995. (interactive)
  17996. (info (format "(org)%s" (or node ""))))
  17997. ;;;###autoload
  17998. (defun org-submit-bug-report ()
  17999. "Submit a bug report on Org-mode via mail.
  18000. Don't hesitate to report any problems or inaccurate documentation.
  18001. If you don't have setup sending mail from (X)Emacs, please copy the
  18002. output buffer into your mail program, as it gives us important
  18003. information about your Org-mode version and configuration."
  18004. (interactive)
  18005. (require 'reporter)
  18006. (org-load-modules-maybe)
  18007. (org-require-autoloaded-modules)
  18008. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  18009. (reporter-submit-bug-report
  18010. "emacs-orgmode@gnu.org"
  18011. (org-version nil 'full)
  18012. (let (list)
  18013. (save-window-excursion
  18014. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  18015. (delete-other-windows)
  18016. (erase-buffer)
  18017. (insert "You are about to submit a bug report to the Org-mode mailing list.
  18018. We would like to add your full Org-mode and Outline configuration to the
  18019. bug report. This greatly simplifies the work of the maintainer and
  18020. other experts on the mailing list.
  18021. HOWEVER, some variables you have customized may contain private
  18022. information. The names of customers, colleagues, or friends, might
  18023. appear in the form of file names, tags, todo states, or search strings.
  18024. If you answer yes to the prompt, you might want to check and remove
  18025. such private information before sending the email.")
  18026. (add-text-properties (point-min) (point-max) '(face org-warning))
  18027. (when (yes-or-no-p "Include your Org-mode configuration ")
  18028. (mapatoms
  18029. (lambda (v)
  18030. (and (boundp v)
  18031. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  18032. (or (and (symbol-value v)
  18033. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  18034. (and
  18035. (get v 'custom-type) (get v 'standard-value)
  18036. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  18037. (push v list)))))
  18038. (kill-buffer (get-buffer "*Warn about privacy*"))
  18039. list))
  18040. nil nil
  18041. "Remember to cover the basics, that is, what you expected to happen and
  18042. what in fact did happen. You don't know how to make a good report? See
  18043. http://orgmode.org/manual/Feedback.html#Feedback
  18044. Your bug report will be posted to the Org-mode mailing list.
  18045. ------------------------------------------------------------------------")
  18046. (save-excursion
  18047. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  18048. (replace-match "\\1Bug: \\3 [\\2]")))))
  18049. (defun org-install-agenda-files-menu ()
  18050. (let ((bl (buffer-list)))
  18051. (save-excursion
  18052. (while bl
  18053. (set-buffer (pop bl))
  18054. (if (derived-mode-p 'org-mode) (setq bl nil)))
  18055. (when (derived-mode-p 'org-mode)
  18056. (easy-menu-change
  18057. '("Org") "File List for Agenda"
  18058. (append
  18059. (list
  18060. ["Edit File List" (org-edit-agenda-file-list) t]
  18061. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  18062. ["Remove Current File from List" org-remove-file t]
  18063. ["Cycle through agenda files" org-cycle-agenda-files t]
  18064. ["Occur in all agenda files" org-occur-in-agenda-files t]
  18065. "--")
  18066. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  18067. ;;;; Documentation
  18068. ;;;###autoload
  18069. (defun org-require-autoloaded-modules ()
  18070. (interactive)
  18071. (mapc 'require
  18072. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  18073. org-docbook org-exp org-html org-icalendar
  18074. org-id org-latex
  18075. org-publish org-remember org-table
  18076. org-timer org-xoxo)))
  18077. ;;;###autoload
  18078. (defun org-reload (&optional uncompiled)
  18079. "Reload all org lisp files.
  18080. With prefix arg UNCOMPILED, load the uncompiled versions."
  18081. (interactive "P")
  18082. (require 'find-func)
  18083. (let* ((file-re "^org\\(-.*\\)?\\.el")
  18084. (dir-org (file-name-directory (org-find-library-dir "org")))
  18085. (dir-org-contrib (ignore-errors
  18086. (file-name-directory
  18087. (org-find-library-dir "org-contribdir"))))
  18088. (babel-files
  18089. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  18090. (append (list nil "comint" "eval" "exp" "keys"
  18091. "lob" "ref" "table" "tangle")
  18092. (delq nil
  18093. (mapcar
  18094. (lambda (lang)
  18095. (when (cdr lang) (symbol-name (car lang))))
  18096. org-babel-load-languages)))))
  18097. (files
  18098. (append babel-files
  18099. (and dir-org-contrib
  18100. (directory-files dir-org-contrib t file-re))
  18101. (directory-files dir-org t file-re)))
  18102. (remove-re (concat (if (featurep 'xemacs)
  18103. "org-colview" "org-colview-xemacs")
  18104. "\\'")))
  18105. (setq files (mapcar 'file-name-sans-extension files))
  18106. (setq files (mapcar
  18107. (lambda (x) (if (string-match remove-re x) nil x))
  18108. files))
  18109. (setq files (delq nil files))
  18110. (mapc
  18111. (lambda (f)
  18112. (when (featurep (intern (file-name-nondirectory f)))
  18113. (if (and (not uncompiled)
  18114. (file-exists-p (concat f ".elc")))
  18115. (load (concat f ".elc") nil nil 'nosuffix)
  18116. (load (concat f ".el") nil nil 'nosuffix))))
  18117. files)
  18118. (load (concat dir-org "org-version.el") 'noerror nil 'nosuffix))
  18119. (org-version nil 'full 'message))
  18120. ;;;###autoload
  18121. (defun org-customize ()
  18122. "Call the customize function with org as argument."
  18123. (interactive)
  18124. (org-load-modules-maybe)
  18125. (org-require-autoloaded-modules)
  18126. (customize-browse 'org))
  18127. (defun org-create-customize-menu ()
  18128. "Create a full customization menu for Org-mode, insert it into the menu."
  18129. (interactive)
  18130. (org-load-modules-maybe)
  18131. (org-require-autoloaded-modules)
  18132. (if (fboundp 'customize-menu-create)
  18133. (progn
  18134. (easy-menu-change
  18135. '("Org") "Customize"
  18136. `(["Browse Org group" org-customize t]
  18137. "--"
  18138. ,(customize-menu-create 'org)
  18139. ["Set" Custom-set t]
  18140. ["Save" Custom-save t]
  18141. ["Reset to Current" Custom-reset-current t]
  18142. ["Reset to Saved" Custom-reset-saved t]
  18143. ["Reset to Standard Settings" Custom-reset-standard t]))
  18144. (message "\"Org\"-menu now contains full customization menu"))
  18145. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  18146. ;;;; Miscellaneous stuff
  18147. ;;; Generally useful functions
  18148. (defun org-get-at-bol (property)
  18149. "Get text property PROPERTY at beginning of line."
  18150. (get-text-property (point-at-bol) property))
  18151. (defun org-find-text-property-in-string (prop s)
  18152. "Return the first non-nil value of property PROP in string S."
  18153. (or (get-text-property 0 prop s)
  18154. (get-text-property (or (next-single-property-change 0 prop s) 0)
  18155. prop s)))
  18156. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  18157. "Display the given MESSAGE as a warning."
  18158. (if (fboundp 'display-warning)
  18159. (display-warning 'org message
  18160. (if (featurep 'xemacs) 'warning :warning))
  18161. (let ((buf (get-buffer-create "*Org warnings*")))
  18162. (with-current-buffer buf
  18163. (goto-char (point-max))
  18164. (insert "Warning (Org): " message)
  18165. (unless (bolp)
  18166. (newline)))
  18167. (display-buffer buf)
  18168. (sit-for 0))))
  18169. (defun org-eval (form)
  18170. "Eval FORM and return result."
  18171. (condition-case error
  18172. (eval form)
  18173. (error (format "%%![Error: %s]" error))))
  18174. (defun org-in-clocktable-p ()
  18175. "Check if the cursor is in a clocktable."
  18176. (let ((pos (point)) start)
  18177. (save-excursion
  18178. (end-of-line 1)
  18179. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  18180. (setq start (match-beginning 0))
  18181. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  18182. (>= (match-end 0) pos)
  18183. start))))
  18184. (defun org-in-commented-line ()
  18185. "Is point in a line starting with `#'?"
  18186. (equal (char-after (point-at-bol)) ?#))
  18187. (defun org-in-indented-comment-line ()
  18188. "Is point in a line starting with `#' after some white space?"
  18189. (save-excursion
  18190. (save-match-data
  18191. (goto-char (point-at-bol))
  18192. (looking-at "[ \t]*#"))))
  18193. (defun org-in-verbatim-emphasis ()
  18194. (save-match-data
  18195. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  18196. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  18197. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  18198. (if (and marker (marker-buffer marker)
  18199. (buffer-live-p (marker-buffer marker)))
  18200. (progn
  18201. (org-pop-to-buffer-same-window (marker-buffer marker))
  18202. (if (or (> marker (point-max)) (< marker (point-min)))
  18203. (widen))
  18204. (goto-char marker)
  18205. (org-show-context 'org-goto))
  18206. (if bookmark
  18207. (bookmark-jump bookmark)
  18208. (error "Cannot find location"))))
  18209. (defun org-quote-csv-field (s)
  18210. "Quote field for inclusion in CSV material."
  18211. (if (string-match "[\",]" s)
  18212. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  18213. s))
  18214. (defun org-force-self-insert (N)
  18215. "Needed to enforce self-insert under remapping."
  18216. (interactive "p")
  18217. (self-insert-command N))
  18218. (defun org-string-width (s)
  18219. "Compute width of string, ignoring invisible characters.
  18220. This ignores character with invisibility property `org-link', and also
  18221. characters with property `org-cwidth', because these will become invisible
  18222. upon the next fontification round."
  18223. (let (b l)
  18224. (when (or (eq t buffer-invisibility-spec)
  18225. (assq 'org-link buffer-invisibility-spec))
  18226. (while (setq b (text-property-any 0 (length s)
  18227. 'invisible 'org-link s))
  18228. (setq s (concat (substring s 0 b)
  18229. (substring s (or (next-single-property-change
  18230. b 'invisible s) (length s)))))))
  18231. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  18232. (setq s (concat (substring s 0 b)
  18233. (substring s (or (next-single-property-change
  18234. b 'org-cwidth s) (length s))))))
  18235. (setq l (string-width s) b -1)
  18236. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  18237. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  18238. l))
  18239. (defun org-shorten-string (s maxlength)
  18240. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  18241. If the string is shorter or has length MAXLENGTH, just return the
  18242. original string. If it is longer, the functions finds a space in the
  18243. string, breaks this string off at that locations and adds three dots
  18244. as ellipsis. Including the ellipsis, the string will not be longer
  18245. than MAXLENGTH. If finding a good breaking point in the string does
  18246. not work, the string is just chopped off in the middle of a word
  18247. if necessary."
  18248. (if (<= (length s) maxlength)
  18249. s
  18250. (let* ((n (max (- maxlength 4) 1))
  18251. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  18252. (if (string-match re s)
  18253. (concat (match-string 1 s) "...")
  18254. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  18255. (defun org-get-indentation (&optional line)
  18256. "Get the indentation of the current line, interpreting tabs.
  18257. When LINE is given, assume it represents a line and compute its indentation."
  18258. (if line
  18259. (if (string-match "^ *" (org-remove-tabs line))
  18260. (match-end 0))
  18261. (save-excursion
  18262. (beginning-of-line 1)
  18263. (skip-chars-forward " \t")
  18264. (current-column))))
  18265. (defun org-get-string-indentation (s)
  18266. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  18267. (let ((n -1) (i 0) (w tab-width) c)
  18268. (catch 'exit
  18269. (while (< (setq n (1+ n)) (length s))
  18270. (setq c (aref s n))
  18271. (cond ((= c ?\ ) (setq i (1+ i)))
  18272. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  18273. (t (throw 'exit t)))))
  18274. i))
  18275. (defun org-remove-tabs (s &optional width)
  18276. "Replace tabulators in S with spaces.
  18277. Assumes that s is a single line, starting in column 0."
  18278. (setq width (or width tab-width))
  18279. (while (string-match "\t" s)
  18280. (setq s (replace-match
  18281. (make-string
  18282. (- (* width (/ (+ (match-beginning 0) width) width))
  18283. (match-beginning 0)) ?\ )
  18284. t t s)))
  18285. s)
  18286. (defun org-fix-indentation (line ind)
  18287. "Fix indentation in LINE.
  18288. IND is a cons cell with target and minimum indentation.
  18289. If the current indentation in LINE is smaller than the minimum,
  18290. leave it alone. If it is larger than ind, set it to the target."
  18291. (let* ((l (org-remove-tabs line))
  18292. (i (org-get-indentation l))
  18293. (i1 (car ind)) (i2 (cdr ind)))
  18294. (if (>= i i2) (setq l (substring line i2)))
  18295. (if (> i1 0)
  18296. (concat (make-string i1 ?\ ) l)
  18297. l)))
  18298. (defun org-remove-indentation (code &optional n)
  18299. "Remove the maximum common indentation from the lines in CODE.
  18300. N may optionally be the number of spaces to remove."
  18301. (with-temp-buffer
  18302. (insert code)
  18303. (org-do-remove-indentation n)
  18304. (buffer-string)))
  18305. (defun org-do-remove-indentation (&optional n)
  18306. "Remove the maximum common indentation from the buffer."
  18307. (untabify (point-min) (point-max))
  18308. (let ((min 10000) re)
  18309. (if n
  18310. (setq min n)
  18311. (goto-char (point-min))
  18312. (while (re-search-forward "^ *[^ \n]" nil t)
  18313. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  18314. (unless (or (= min 0) (= min 10000))
  18315. (setq re (format "^ \\{%d\\}" min))
  18316. (goto-char (point-min))
  18317. (while (re-search-forward re nil t)
  18318. (replace-match "")
  18319. (end-of-line 1))
  18320. min)))
  18321. (defun org-fill-template (template alist)
  18322. "Find each %key of ALIST in TEMPLATE and replace it."
  18323. (let ((case-fold-search nil)
  18324. entry key value)
  18325. (setq alist (sort (copy-sequence alist)
  18326. (lambda (a b) (< (length (car a)) (length (car b))))))
  18327. (while (setq entry (pop alist))
  18328. (setq template
  18329. (replace-regexp-in-string
  18330. (concat "%" (regexp-quote (car entry)))
  18331. (cdr entry) template t t)))
  18332. template))
  18333. (defun org-base-buffer (buffer)
  18334. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  18335. (if (not buffer)
  18336. buffer
  18337. (or (buffer-base-buffer buffer)
  18338. buffer)))
  18339. (defun org-trim (s)
  18340. "Remove whitespace at beginning and end of string."
  18341. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  18342. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  18343. s)
  18344. (defun org-wrap (string &optional width lines)
  18345. "Wrap string to either a number of lines, or a width in characters.
  18346. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  18347. that costs. If there is a word longer than WIDTH, the text is actually
  18348. wrapped to the length of that word.
  18349. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  18350. many lines, whatever width that takes.
  18351. The return value is a list of lines, without newlines at the end."
  18352. (let* ((words (org-split-string string "[ \t\n]+"))
  18353. (maxword (apply 'max (mapcar 'org-string-width words)))
  18354. w ll)
  18355. (cond (width
  18356. (org-do-wrap words (max maxword width)))
  18357. (lines
  18358. (setq w maxword)
  18359. (setq ll (org-do-wrap words maxword))
  18360. (if (<= (length ll) lines)
  18361. ll
  18362. (setq ll words)
  18363. (while (> (length ll) lines)
  18364. (setq w (1+ w))
  18365. (setq ll (org-do-wrap words w)))
  18366. ll))
  18367. (t (error "Cannot wrap this")))))
  18368. (defun org-do-wrap (words width)
  18369. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  18370. (let (lines line)
  18371. (while words
  18372. (setq line (pop words))
  18373. (while (and words (< (+ (length line) (length (car words))) width))
  18374. (setq line (concat line " " (pop words))))
  18375. (setq lines (push line lines)))
  18376. (nreverse lines)))
  18377. (defun org-split-string (string &optional separators)
  18378. "Splits STRING into substrings at SEPARATORS.
  18379. No empty strings are returned if there are matches at the beginning
  18380. and end of string."
  18381. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  18382. (start 0)
  18383. notfirst
  18384. (list nil))
  18385. (while (and (string-match rexp string
  18386. (if (and notfirst
  18387. (= start (match-beginning 0))
  18388. (< start (length string)))
  18389. (1+ start) start))
  18390. (< (match-beginning 0) (length string)))
  18391. (setq notfirst t)
  18392. (or (eq (match-beginning 0) 0)
  18393. (and (eq (match-beginning 0) (match-end 0))
  18394. (eq (match-beginning 0) start))
  18395. (setq list
  18396. (cons (substring string start (match-beginning 0))
  18397. list)))
  18398. (setq start (match-end 0)))
  18399. (or (eq start (length string))
  18400. (setq list
  18401. (cons (substring string start)
  18402. list)))
  18403. (nreverse list)))
  18404. (defun org-quote-vert (s)
  18405. "Replace \"|\" with \"\\vert\"."
  18406. (while (string-match "|" s)
  18407. (setq s (replace-match "\\vert" t t s)))
  18408. s)
  18409. (defun org-uuidgen-p (s)
  18410. "Is S an ID created by UUIDGEN?"
  18411. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  18412. (defun org-in-src-block-p nil
  18413. "Whether point is in a code source block."
  18414. (let (ov)
  18415. (when (setq ov (overlays-at (point)))
  18416. (memq 'org-block-background
  18417. (overlay-properties
  18418. (car ov))))))
  18419. (defun org-context ()
  18420. "Return a list of contexts of the current cursor position.
  18421. If several contexts apply, all are returned.
  18422. Each context entry is a list with a symbol naming the context, and
  18423. two positions indicating start and end of the context. Possible
  18424. contexts are:
  18425. :headline anywhere in a headline
  18426. :headline-stars on the leading stars in a headline
  18427. :todo-keyword on a TODO keyword (including DONE) in a headline
  18428. :tags on the TAGS in a headline
  18429. :priority on the priority cookie in a headline
  18430. :item on the first line of a plain list item
  18431. :item-bullet on the bullet/number of a plain list item
  18432. :checkbox on the checkbox in a plain list item
  18433. :table in an org-mode table
  18434. :table-special on a special filed in a table
  18435. :table-table in a table.el table
  18436. :clocktable in a clocktable
  18437. :src-block in a source block
  18438. :link on a hyperlink
  18439. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT, QUOTE.
  18440. :target on a <<target>>
  18441. :radio-target on a <<<radio-target>>>
  18442. :latex-fragment on a LaTeX fragment
  18443. :latex-preview on a LaTeX fragment with overlaid preview image
  18444. This function expects the position to be visible because it uses font-lock
  18445. faces as a help to recognize the following contexts: :table-special, :link,
  18446. and :keyword."
  18447. (let* ((f (get-text-property (point) 'face))
  18448. (faces (if (listp f) f (list f)))
  18449. (case-fold-search t)
  18450. (p (point)) clist o)
  18451. ;; First the large context
  18452. (cond
  18453. ((org-at-heading-p t)
  18454. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  18455. (when (progn
  18456. (beginning-of-line 1)
  18457. (looking-at org-todo-line-tags-regexp))
  18458. (push (org-point-in-group p 1 :headline-stars) clist)
  18459. (push (org-point-in-group p 2 :todo-keyword) clist)
  18460. (push (org-point-in-group p 4 :tags) clist))
  18461. (goto-char p)
  18462. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  18463. (if (looking-at "\\[#[A-Z0-9]\\]")
  18464. (push (org-point-in-group p 0 :priority) clist)))
  18465. ((org-at-item-p)
  18466. (push (org-point-in-group p 2 :item-bullet) clist)
  18467. (push (list :item (point-at-bol)
  18468. (save-excursion (org-end-of-item) (point)))
  18469. clist)
  18470. (and (org-at-item-checkbox-p)
  18471. (push (org-point-in-group p 0 :checkbox) clist)))
  18472. ((org-at-table-p)
  18473. (push (list :table (org-table-begin) (org-table-end)) clist)
  18474. (if (memq 'org-formula faces)
  18475. (push (list :table-special
  18476. (previous-single-property-change p 'face)
  18477. (next-single-property-change p 'face)) clist)))
  18478. ((org-at-table-p 'any)
  18479. (push (list :table-table) clist)))
  18480. (goto-char p)
  18481. ;; New the "medium" contexts: clocktables, source blocks
  18482. (cond ((org-in-clocktable-p)
  18483. (push (list :clocktable
  18484. (and (or (looking-at "#\\+BEGIN: clocktable")
  18485. (search-backward "#+BEGIN: clocktable" nil t))
  18486. (match-beginning 0))
  18487. (and (re-search-forward "#\\+END:?" nil t)
  18488. (match-end 0))) clist))
  18489. ((org-in-src-block-p)
  18490. (push (list :src-block
  18491. (and (or (looking-at "#\\+BEGIN_SRC")
  18492. (search-backward "#+BEGIN_SRC" nil t))
  18493. (match-beginning 0))
  18494. (and (search-forward "#+END_SRC" nil t)
  18495. (match-beginning 0))) clist)))
  18496. (goto-char p)
  18497. ;; Now the small context
  18498. (cond
  18499. ((org-at-timestamp-p)
  18500. (push (org-point-in-group p 0 :timestamp) clist))
  18501. ((memq 'org-link faces)
  18502. (push (list :link
  18503. (previous-single-property-change p 'face)
  18504. (next-single-property-change p 'face)) clist))
  18505. ((memq 'org-special-keyword faces)
  18506. (push (list :keyword
  18507. (previous-single-property-change p 'face)
  18508. (next-single-property-change p 'face)) clist))
  18509. ((org-at-target-p)
  18510. (push (org-point-in-group p 0 :target) clist)
  18511. (goto-char (1- (match-beginning 0)))
  18512. (if (looking-at org-radio-target-regexp)
  18513. (push (org-point-in-group p 0 :radio-target) clist))
  18514. (goto-char p))
  18515. ((setq o (car (delq nil
  18516. (mapcar
  18517. (lambda (x)
  18518. (if (memq x org-latex-fragment-image-overlays) x))
  18519. (overlays-at (point))))))
  18520. (push (list :latex-fragment
  18521. (overlay-start o) (overlay-end o)) clist)
  18522. (push (list :latex-preview
  18523. (overlay-start o) (overlay-end o)) clist))
  18524. ((org-inside-LaTeX-fragment-p)
  18525. ;; FIXME: positions wrong.
  18526. (push (list :latex-fragment (point) (point)) clist)))
  18527. (setq clist (nreverse (delq nil clist)))
  18528. clist))
  18529. ;; FIXME: Compare with at-regexp-p Do we need both?
  18530. (defun org-in-regexp (re &optional nlines visually)
  18531. "Check if point is inside a match of regexp.
  18532. Normally only the current line is checked, but you can include NLINES extra
  18533. lines both before and after point into the search.
  18534. If VISUALLY is set, require that the cursor is not after the match but
  18535. really on, so that the block visually is on the match."
  18536. (catch 'exit
  18537. (let ((pos (point))
  18538. (eol (point-at-eol (+ 1 (or nlines 0))))
  18539. (inc (if visually 1 0)))
  18540. (save-excursion
  18541. (beginning-of-line (- 1 (or nlines 0)))
  18542. (while (re-search-forward re eol t)
  18543. (if (and (<= (match-beginning 0) pos)
  18544. (>= (+ inc (match-end 0)) pos))
  18545. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  18546. (defun org-at-regexp-p (regexp)
  18547. "Is point inside a match of REGEXP in the current line?"
  18548. (catch 'exit
  18549. (save-excursion
  18550. (let ((pos (point)) (end (point-at-eol)))
  18551. (beginning-of-line 1)
  18552. (while (re-search-forward regexp end t)
  18553. (if (and (<= (match-beginning 0) pos)
  18554. (>= (match-end 0) pos))
  18555. (throw 'exit t)))
  18556. nil))))
  18557. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  18558. "Non-nil when point is between matches of START-RE and END-RE.
  18559. Also return a non-nil value when point is on one of the matches.
  18560. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  18561. buffer positions. Default values are the positions of headlines
  18562. surrounding the point.
  18563. The functions returns a cons cell whose car (resp. cdr) is the
  18564. position before START-RE (resp. after END-RE)."
  18565. (save-match-data
  18566. (let ((pos (point))
  18567. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  18568. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  18569. beg end)
  18570. (save-excursion
  18571. ;; Point is on a block when on START-RE or if START-RE can be
  18572. ;; found before it...
  18573. (and (or (org-at-regexp-p start-re)
  18574. (re-search-backward start-re limit-up t))
  18575. (setq beg (match-beginning 0))
  18576. ;; ... and END-RE after it...
  18577. (goto-char (match-end 0))
  18578. (re-search-forward end-re limit-down t)
  18579. (> (setq end (match-end 0)) pos)
  18580. ;; ... without another START-RE in-between.
  18581. (goto-char (match-beginning 0))
  18582. (not (re-search-backward start-re (1+ beg) t))
  18583. ;; Return value.
  18584. (cons beg end))))))
  18585. (defun org-in-block-p (names)
  18586. "Non-nil when point belongs to a block whose name belongs to NAMES.
  18587. NAMES is a list of strings containing names of blocks.
  18588. Return first block name matched, or nil. Beware that in case of
  18589. nested blocks, the returned name may not belong to the closest
  18590. block from point."
  18591. (save-match-data
  18592. (catch 'exit
  18593. (let ((case-fold-search t)
  18594. (lim-up (save-excursion (outline-previous-heading)))
  18595. (lim-down (save-excursion (outline-next-heading))))
  18596. (mapc (lambda (name)
  18597. (let ((n (regexp-quote name)))
  18598. (when (org-between-regexps-p
  18599. (concat "^[ \t]*#\\+begin_" n)
  18600. (concat "^[ \t]*#\\+end_" n)
  18601. lim-up lim-down)
  18602. (throw 'exit n))))
  18603. names))
  18604. nil)))
  18605. (defun org-occur-in-agenda-files (regexp &optional nlines)
  18606. "Call `multi-occur' with buffers for all agenda files."
  18607. (interactive "sOrg-files matching: \np")
  18608. (let* ((files (org-agenda-files))
  18609. (tnames (mapcar 'file-truename files))
  18610. (extra org-agenda-text-search-extra-files)
  18611. f)
  18612. (when (eq (car extra) 'agenda-archives)
  18613. (setq extra (cdr extra))
  18614. (setq files (org-add-archive-files files)))
  18615. (while (setq f (pop extra))
  18616. (unless (member (file-truename f) tnames)
  18617. (add-to-list 'files f 'append)
  18618. (add-to-list 'tnames (file-truename f) 'append)))
  18619. (multi-occur
  18620. (mapcar (lambda (x)
  18621. (with-current-buffer
  18622. (or (get-file-buffer x) (find-file-noselect x))
  18623. (widen)
  18624. (current-buffer)))
  18625. files)
  18626. regexp)))
  18627. (if (boundp 'occur-mode-find-occurrence-hook)
  18628. ;; Emacs 23
  18629. (add-hook 'occur-mode-find-occurrence-hook
  18630. (lambda ()
  18631. (when (derived-mode-p 'org-mode)
  18632. (org-reveal))))
  18633. ;; Emacs 22
  18634. (defadvice occur-mode-goto-occurrence
  18635. (after org-occur-reveal activate)
  18636. (and (derived-mode-p 'org-mode) (org-reveal)))
  18637. (defadvice occur-mode-goto-occurrence-other-window
  18638. (after org-occur-reveal activate)
  18639. (and (derived-mode-p 'org-mode) (org-reveal)))
  18640. (defadvice occur-mode-display-occurrence
  18641. (after org-occur-reveal activate)
  18642. (when (derived-mode-p 'org-mode)
  18643. (let ((pos (occur-mode-find-occurrence)))
  18644. (with-current-buffer (marker-buffer pos)
  18645. (save-excursion
  18646. (goto-char pos)
  18647. (org-reveal)))))))
  18648. (defun org-occur-link-in-agenda-files ()
  18649. "Create a link and search for it in the agendas.
  18650. The link is not stored in `org-stored-links', it is just created
  18651. for the search purpose."
  18652. (interactive)
  18653. (let ((link (condition-case nil
  18654. (org-store-link nil)
  18655. (error "Unable to create a link to here"))))
  18656. (org-occur-in-agenda-files (regexp-quote link))))
  18657. (defun org-uniquify (list)
  18658. "Remove duplicate elements from LIST."
  18659. (let (res)
  18660. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  18661. res))
  18662. (defun org-delete-all (elts list)
  18663. "Remove all elements in ELTS from LIST."
  18664. (while elts
  18665. (setq list (delete (pop elts) list)))
  18666. list)
  18667. (defun org-count (cl-item cl-seq)
  18668. "Count the number of occurrences of ITEM in SEQ.
  18669. Taken from `count' in cl-seq.el with all keyword arguments removed."
  18670. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  18671. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  18672. (while (< cl-start cl-end)
  18673. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  18674. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  18675. (setq cl-start (1+ cl-start)))
  18676. cl-count))
  18677. (defun org-remove-if (predicate seq)
  18678. "Remove everything from SEQ that fulfills PREDICATE."
  18679. (let (res e)
  18680. (while seq
  18681. (setq e (pop seq))
  18682. (if (not (funcall predicate e)) (push e res)))
  18683. (nreverse res)))
  18684. (defun org-remove-if-not (predicate seq)
  18685. "Remove everything from SEQ that does not fulfill PREDICATE."
  18686. (let (res e)
  18687. (while seq
  18688. (setq e (pop seq))
  18689. (if (funcall predicate e) (push e res)))
  18690. (nreverse res)))
  18691. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  18692. "Reduce two-argument FUNCTION across SEQ.
  18693. Taken from `reduce' in cl-seq.el with all keyword arguments but
  18694. \":initial-value\" removed."
  18695. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  18696. (cadr (memq :initial-value cl-keys)))
  18697. (cl-seq (pop cl-seq))
  18698. (t (funcall cl-func)))))
  18699. (while cl-seq
  18700. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  18701. cl-accum))
  18702. (defun org-back-over-empty-lines ()
  18703. "Move backwards over whitespace, to the beginning of the first empty line.
  18704. Returns the number of empty lines passed."
  18705. (let ((pos (point)))
  18706. (if (cdr (assoc 'heading org-blank-before-new-entry))
  18707. (skip-chars-backward " \t\n\r")
  18708. (unless (eobp)
  18709. (forward-line -1)))
  18710. (beginning-of-line 2)
  18711. (goto-char (min (point) pos))
  18712. (count-lines (point) pos)))
  18713. (defun org-skip-whitespace ()
  18714. (skip-chars-forward " \t\n\r"))
  18715. (defun org-point-in-group (point group &optional context)
  18716. "Check if POINT is in match-group GROUP.
  18717. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  18718. match. If the match group does not exist or point is not inside it,
  18719. return nil."
  18720. (and (match-beginning group)
  18721. (>= point (match-beginning group))
  18722. (<= point (match-end group))
  18723. (if context
  18724. (list context (match-beginning group) (match-end group))
  18725. t)))
  18726. (defun org-switch-to-buffer-other-window (&rest args)
  18727. "Switch to buffer in a second window on the current frame.
  18728. In particular, do not allow pop-up frames.
  18729. Returns the newly created buffer."
  18730. (let (pop-up-frames special-display-buffer-names special-display-regexps
  18731. special-display-function)
  18732. (apply 'switch-to-buffer-other-window args)))
  18733. (defun org-combine-plists (&rest plists)
  18734. "Create a single property list from all plists in PLISTS.
  18735. The process starts by copying the first list, and then setting properties
  18736. from the other lists. Settings in the last list are the most significant
  18737. ones and overrule settings in the other lists."
  18738. (let ((rtn (copy-sequence (pop plists)))
  18739. p v ls)
  18740. (while plists
  18741. (setq ls (pop plists))
  18742. (while ls
  18743. (setq p (pop ls) v (pop ls))
  18744. (setq rtn (plist-put rtn p v))))
  18745. rtn))
  18746. (defun org-replace-escapes (string table)
  18747. "Replace %-escapes in STRING with values in TABLE.
  18748. TABLE is an association list with keys like \"%a\" and string values.
  18749. The sequences in STRING may contain normal field width and padding information,
  18750. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  18751. so values can contain further %-escapes if they are define later in TABLE."
  18752. (let ((tbl (copy-alist table))
  18753. (case-fold-search nil)
  18754. (pchg 0)
  18755. e re rpl)
  18756. (while (setq e (pop tbl))
  18757. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  18758. (when (and (cdr e) (string-match re (cdr e)))
  18759. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  18760. (safe "SREF"))
  18761. (add-text-properties 0 3 (list 'sref sref) safe)
  18762. (setcdr e (replace-match safe t t (cdr e)))))
  18763. (while (string-match re string)
  18764. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  18765. (cdr e)))
  18766. (setq string (replace-match rpl t t string))))
  18767. (while (setq pchg (next-property-change pchg string))
  18768. (let ((sref (get-text-property pchg 'sref string)))
  18769. (when (and sref (string-match "SREF" string pchg))
  18770. (setq string (replace-match sref t t string)))))
  18771. string))
  18772. (defun org-sublist (list start end)
  18773. "Return a section of LIST, from START to END.
  18774. Counting starts at 1."
  18775. (let (rtn (c start))
  18776. (setq list (nthcdr (1- start) list))
  18777. (while (and list (<= c end))
  18778. (push (pop list) rtn)
  18779. (setq c (1+ c)))
  18780. (nreverse rtn)))
  18781. (defun org-find-base-buffer-visiting (file)
  18782. "Like `find-buffer-visiting' but always return the base buffer and
  18783. not an indirect buffer."
  18784. (let ((buf (or (get-file-buffer file)
  18785. (find-buffer-visiting file))))
  18786. (if buf
  18787. (or (buffer-base-buffer buf) buf)
  18788. nil)))
  18789. (defun org-image-file-name-regexp (&optional extensions)
  18790. "Return regexp matching the file names of images.
  18791. If EXTENSIONS is given, only match these."
  18792. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  18793. (image-file-name-regexp)
  18794. (let ((image-file-name-extensions
  18795. (or extensions
  18796. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  18797. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  18798. (concat "\\."
  18799. (regexp-opt (nconc (mapcar 'upcase
  18800. image-file-name-extensions)
  18801. image-file-name-extensions)
  18802. t)
  18803. "\\'"))))
  18804. (defun org-file-image-p (file &optional extensions)
  18805. "Return non-nil if FILE is an image."
  18806. (save-match-data
  18807. (string-match (org-image-file-name-regexp extensions) file)))
  18808. (defun org-get-cursor-date ()
  18809. "Return the date at cursor in as a time.
  18810. This works in the calendar and in the agenda, anywhere else it just
  18811. returns the current time."
  18812. (let (date day defd)
  18813. (cond
  18814. ((eq major-mode 'calendar-mode)
  18815. (setq date (calendar-cursor-to-date)
  18816. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  18817. ((eq major-mode 'org-agenda-mode)
  18818. (setq day (get-text-property (point) 'day))
  18819. (if day
  18820. (setq date (calendar-gregorian-from-absolute day)
  18821. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  18822. (nth 2 date))))))
  18823. (or defd (current-time))))
  18824. (defvar org-agenda-action-marker (make-marker)
  18825. "Marker pointing to the entry for the next agenda action.")
  18826. (defun org-mark-entry-for-agenda-action ()
  18827. "Mark the current entry as target of an agenda action.
  18828. Agenda actions are actions executed from the agenda with the key `k',
  18829. which make use of the date at the cursor."
  18830. (interactive)
  18831. (move-marker org-agenda-action-marker
  18832. (save-excursion (org-back-to-heading t) (point))
  18833. (current-buffer))
  18834. (message
  18835. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  18836. (defun org-mark-subtree ()
  18837. "Mark the current subtree.
  18838. This puts point at the start of the current subtree, and mark at the end.
  18839. If point is in an inline task, mark that task instead."
  18840. (interactive)
  18841. (let ((inline-task-p
  18842. (and (featurep 'org-inlinetask)
  18843. (org-inlinetask-in-task-p)))
  18844. (beg))
  18845. ;; Get beginning of subtree
  18846. (cond
  18847. (inline-task-p (org-inlinetask-goto-beginning))
  18848. ((org-at-heading-p) (beginning-of-line))
  18849. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  18850. (setq beg (point))
  18851. ;; Get end of it
  18852. (if inline-task-p
  18853. (org-inlinetask-goto-end)
  18854. (org-end-of-subtree))
  18855. ;; Mark zone
  18856. (push-mark (point) nil t)
  18857. (goto-char beg)))
  18858. ;;; Paragraph filling stuff.
  18859. ;; We want this to be just right, so use the full arsenal.
  18860. (defun org-indent-line ()
  18861. "Indent line depending on context."
  18862. (interactive)
  18863. (let* ((pos (point))
  18864. (itemp (org-at-item-p))
  18865. (case-fold-search t)
  18866. (org-drawer-regexp (or org-drawer-regexp "\000"))
  18867. (inline-task-p (and (featurep 'org-inlinetask)
  18868. (org-inlinetask-in-task-p)))
  18869. (inline-re (and inline-task-p
  18870. (org-inlinetask-outline-regexp)))
  18871. column)
  18872. (beginning-of-line 1)
  18873. (cond
  18874. ;; Comments
  18875. ((looking-at "# ") (setq column 0))
  18876. ;; Headings
  18877. ((looking-at org-outline-regexp) (setq column 0))
  18878. ;; Included files
  18879. ((looking-at "#\\+include:") (setq column 0))
  18880. ;; Footnote definition
  18881. ((looking-at org-footnote-definition-re) (setq column 0))
  18882. ;; Literal examples
  18883. ((looking-at "[ \t]*:\\( \\|$\\)")
  18884. (setq column (org-get-indentation))) ; do nothing
  18885. ;; Lists
  18886. ((ignore-errors (goto-char (org-in-item-p)))
  18887. (setq column (if itemp
  18888. (org-get-indentation)
  18889. (org-list-item-body-column (point))))
  18890. (goto-char pos))
  18891. ;; Drawers
  18892. ((and (looking-at "[ \t]*:END:")
  18893. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  18894. (save-excursion
  18895. (goto-char (1- (match-beginning 1)))
  18896. (setq column (current-column))))
  18897. ;; Special blocks
  18898. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  18899. (save-excursion
  18900. (re-search-backward
  18901. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  18902. (setq column (org-get-indentation (match-string 0))))
  18903. ((and (not (looking-at "[ \t]*#\\+begin_"))
  18904. (org-between-regexps-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  18905. (save-excursion
  18906. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  18907. (setq column
  18908. (cond ((equal (downcase (match-string 1)) "src")
  18909. ;; src blocks: let `org-edit-src-exit' handle them
  18910. (org-get-indentation))
  18911. ((equal (downcase (match-string 1)) "example")
  18912. (max (org-get-indentation)
  18913. (org-get-indentation (match-string 0))))
  18914. (t
  18915. (org-get-indentation (match-string 0))))))
  18916. ;; This line has nothing special, look at the previous relevant
  18917. ;; line to compute indentation
  18918. (t
  18919. (beginning-of-line 0)
  18920. (while (and (not (bobp))
  18921. (not (looking-at org-drawer-regexp))
  18922. ;; When point started in an inline task, do not move
  18923. ;; above task starting line.
  18924. (not (and inline-task-p (looking-at inline-re)))
  18925. ;; Skip drawers, blocks, empty lines, verbatim,
  18926. ;; comments, tables, footnotes definitions, lists,
  18927. ;; inline tasks.
  18928. (or (and (looking-at "[ \t]*:END:")
  18929. (re-search-backward org-drawer-regexp nil t))
  18930. (and (looking-at "[ \t]*#\\+end_")
  18931. (re-search-backward "[ \t]*#\\+begin_"nil t))
  18932. (looking-at "[ \t]*[\n:#|]")
  18933. (looking-at org-footnote-definition-re)
  18934. (and (ignore-errors (goto-char (org-in-item-p)))
  18935. (goto-char
  18936. (org-list-get-top-point (org-list-struct))))
  18937. (and (not inline-task-p)
  18938. (featurep 'org-inlinetask)
  18939. (org-inlinetask-in-task-p)
  18940. (or (org-inlinetask-goto-beginning) t))))
  18941. (beginning-of-line 0))
  18942. (cond
  18943. ;; There was an heading above.
  18944. ((looking-at "\\*+[ \t]+")
  18945. (if (not org-adapt-indentation)
  18946. (setq column 0)
  18947. (goto-char (match-end 0))
  18948. (setq column (current-column))))
  18949. ;; A drawer had started and is unfinished
  18950. ((looking-at org-drawer-regexp)
  18951. (goto-char (1- (match-beginning 1)))
  18952. (setq column (current-column)))
  18953. ;; Else, nothing noticeable found: get indentation and go on.
  18954. (t (setq column (org-get-indentation))))))
  18955. ;; Now apply indentation and move cursor accordingly
  18956. (goto-char pos)
  18957. (if (<= (current-column) (current-indentation))
  18958. (org-indent-line-to column)
  18959. (save-excursion (org-indent-line-to column)))
  18960. ;; Special polishing for properties, see `org-property-format'
  18961. (setq column (current-column))
  18962. (beginning-of-line 1)
  18963. (if (looking-at
  18964. "\\([ \t]*\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  18965. (replace-match (concat (match-string 1)
  18966. (format org-property-format
  18967. (match-string 2) (match-string 3)))
  18968. t t))
  18969. (org-move-to-column column)
  18970. (when (and orgstruct-is-++ (eq pos (point)))
  18971. (org-let org-fb-vars
  18972. '(indent-according-to-mode)))))
  18973. (defun org-indent-drawer ()
  18974. "Indent the drawer at point."
  18975. (interactive)
  18976. (let ((p (point))
  18977. (e (and (save-excursion (re-search-forward ":END:" nil t))
  18978. (match-end 0)))
  18979. (folded
  18980. (save-excursion
  18981. (end-of-line)
  18982. (when (overlays-at (point))
  18983. (member 'invisible (overlay-properties
  18984. (car (overlays-at (point)))))))))
  18985. (when folded (org-cycle))
  18986. (indent-for-tab-command)
  18987. (while (and (move-beginning-of-line 2) (< (point) e))
  18988. (indent-for-tab-command))
  18989. (goto-char p)
  18990. (when folded (org-cycle)))
  18991. (message "Drawer at point indented"))
  18992. (defun org-indent-block ()
  18993. "Indent the block at point."
  18994. (interactive)
  18995. (let ((p (point))
  18996. (case-fold-search t)
  18997. (e (and (save-excursion (re-search-forward "#\\+end_?\\(?:[a-z]+\\)?" nil t))
  18998. (match-end 0)))
  18999. (folded
  19000. (save-excursion
  19001. (end-of-line)
  19002. (when (overlays-at (point))
  19003. (member 'invisible (overlay-properties
  19004. (car (overlays-at (point)))))))))
  19005. (when folded (org-cycle))
  19006. (indent-for-tab-command)
  19007. (while (and (move-beginning-of-line 2) (< (point) e))
  19008. (indent-for-tab-command))
  19009. (goto-char p)
  19010. (when folded (org-cycle)))
  19011. (message "Block at point indented"))
  19012. (defun org-indent-region (start end)
  19013. "Indent region."
  19014. (interactive "r")
  19015. (save-excursion
  19016. (let ((line-end (org-current-line end)))
  19017. (goto-char start)
  19018. (while (< (org-current-line) line-end)
  19019. (cond ((org-in-src-block-p) (org-src-native-tab-command-maybe))
  19020. (t (call-interactively 'org-indent-line)))
  19021. (move-beginning-of-line 2)))))
  19022. ;; For reference, this is the default value of adaptive-fill-regexp
  19023. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  19024. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  19025. "Variable to store copy of `adaptive-fill-regexp'.
  19026. Since `adaptive-fill-regexp' is set to never match, we need to
  19027. store a backup of its value before entering `org-mode' so that
  19028. the functionality can be provided as a fall-back.")
  19029. (defun org-set-autofill-regexps ()
  19030. (interactive)
  19031. ;; In the paragraph separator we include headlines, because filling
  19032. ;; text in a line directly attached to a headline would otherwise
  19033. ;; fill the headline as well.
  19034. (org-set-local 'comment-start-skip "^#+[ \t]*")
  19035. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  19036. ;; The paragraph starter includes hand-formatted lists.
  19037. (org-set-local
  19038. 'paragraph-start
  19039. (concat
  19040. "\f" "\\|"
  19041. "[ ]*$" "\\|"
  19042. org-outline-regexp "\\|"
  19043. "[ \t]*#" "\\|"
  19044. (org-item-re) "\\|"
  19045. "[ \t]*[:|]" "\\|"
  19046. "\\$\\$" "\\|"
  19047. "\\\\\\(begin\\|end\\|[][]\\)"))
  19048. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  19049. ;; But only if the user has not turned off tables or fixed-width regions
  19050. (org-set-local
  19051. 'auto-fill-inhibit-regexp
  19052. (concat org-outline-regexp
  19053. "\\|#\\+"
  19054. "\\|[ \t]*" org-keyword-time-regexp
  19055. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  19056. (concat
  19057. "\\|[ \t]*["
  19058. (if org-enable-table-editor "|" "")
  19059. (if org-enable-fixed-width-editor ":" "")
  19060. "]"))))
  19061. ;; We use our own fill-paragraph function, to make sure that tables
  19062. ;; and fixed-width regions are not wrapped. That function will pass
  19063. ;; through to `fill-paragraph' when appropriate.
  19064. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  19065. ;; Prevent auto-fill from inserting unwanted new items.
  19066. (if (boundp 'fill-nobreak-predicate)
  19067. (org-set-local
  19068. 'fill-nobreak-predicate
  19069. (org-uniquify
  19070. (append fill-nobreak-predicate
  19071. '(org-fill-item-nobreak-p org-fill-line-break-nobreak-p)))))
  19072. ;; Adaptive filling: To get full control, first make sure that
  19073. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  19074. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  19075. (org-set-local 'org-adaptive-fill-regexp-backup
  19076. adaptive-fill-regexp))
  19077. (org-set-local 'adaptive-fill-regexp "\000")
  19078. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  19079. (org-set-local 'adaptive-fill-function
  19080. 'org-adaptive-fill-function)
  19081. (org-set-local
  19082. 'align-mode-rules-list
  19083. '((org-in-buffer-settings
  19084. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  19085. (modes . '(org-mode))))))
  19086. (defun org-fill-item-nobreak-p ()
  19087. "Non-nil when a line break at point would insert a new item."
  19088. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  19089. (defun org-fill-line-break-nobreak-p ()
  19090. "Non-nil when a line break at point would create an Org line break."
  19091. (save-excursion
  19092. (skip-chars-backward "[ \t]")
  19093. (skip-chars-backward "\\\\")
  19094. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  19095. (defun org-fill-paragraph (&optional justify)
  19096. "Re-align a table, pass through to fill-paragraph if no table."
  19097. (let ((table-p (org-at-table-p))
  19098. (table.el-p (org-at-table.el-p))
  19099. (itemp (org-in-item-p))
  19100. (srcp (org-in-src-block-p)))
  19101. (cond ((and (equal (char-after (point-at-bol)) ?*)
  19102. (save-excursion (goto-char (point-at-bol))
  19103. (looking-at org-outline-regexp)))
  19104. t) ; skip headlines
  19105. (table.el-p t) ; skip table.el tables
  19106. (table-p (org-table-align) t) ; align Org tables
  19107. (srcp ; indent entire src block to prevent
  19108. (save-excursion ; fill-paragraph-as-region to mess up
  19109. (org-babel-do-in-edit-buffer
  19110. (indent-region (point-min) (point-max)))))
  19111. (itemp ; align text in items
  19112. (let* ((struct (save-excursion (goto-char itemp)
  19113. (org-list-struct)))
  19114. (parents (org-list-parents-alist struct))
  19115. (children (org-list-get-children itemp struct parents))
  19116. beg end prev next prefix)
  19117. ;; Determine in which part of item point is: before
  19118. ;; first child, after last child, between two
  19119. ;; sub-lists, or simply in item if there's no child.
  19120. (cond
  19121. ((not children)
  19122. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  19123. beg itemp
  19124. end (org-list-get-item-end itemp struct)))
  19125. ((< (point) (setq next (car children)))
  19126. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  19127. beg itemp
  19128. end next))
  19129. ((> (point) (setq prev (car (last children))))
  19130. (setq beg (org-list-get-item-end prev struct)
  19131. end (org-list-get-item-end itemp struct)
  19132. prefix (save-excursion
  19133. (goto-char beg)
  19134. (skip-chars-forward " \t")
  19135. (make-string (current-column) ?\ ))))
  19136. (t (catch 'exit
  19137. (while (setq next (pop children))
  19138. (if (> (point) next)
  19139. (setq prev next)
  19140. (setq beg (org-list-get-item-end prev struct)
  19141. end next
  19142. prefix (save-excursion
  19143. (goto-char beg)
  19144. (skip-chars-forward " \t")
  19145. (make-string (current-column) ?\ )))
  19146. (throw 'exit nil))))))
  19147. ;; Use `fill-paragraph' with buffer narrowed to item
  19148. ;; without any child, and with our computed PREFIX.
  19149. (org-flet ((fill-context-prefix (from to &optional flr) prefix))
  19150. (save-restriction
  19151. (narrow-to-region beg end)
  19152. (save-excursion (fill-paragraph justify)))) t))
  19153. ;; Special case where point is not in a list but is on
  19154. ;; a paragraph adjacent to a list: make sure this paragraph
  19155. ;; doesn't get merged with the end of the list by narrowing
  19156. ;; buffer first.
  19157. ((and (derived-mode-p 'org-mode)
  19158. (save-excursion (forward-paragraph -1)
  19159. (setq itemp (org-in-item-p))))
  19160. (let ((struct (save-excursion (goto-char itemp)
  19161. (org-list-struct))))
  19162. (save-restriction
  19163. (narrow-to-region (org-list-get-bottom-point struct)
  19164. (save-excursion (forward-paragraph 1)
  19165. (point)))
  19166. (fill-paragraph justify) t)))
  19167. ;; Don't fill schedule/deadline line before a paragraph
  19168. ;; This only makes sense in real org-mode buffers
  19169. ((and (eq major-mode 'org-mode)
  19170. (save-excursion (forward-paragraph -1)
  19171. (or (looking-at (concat "^[^\n]*" org-scheduled-regexp ".*$"))
  19172. (looking-at (concat "^[^\n]*" org-deadline-regexp ".*$")))))
  19173. (save-restriction
  19174. (narrow-to-region (1+ (match-end 0))
  19175. (save-excursion (forward-paragraph 1) (point)))
  19176. (fill-paragraph justify) t))
  19177. ;; Else fall back on fill-paragraph-function as possibly
  19178. ;; defined in `org-fb-vars'
  19179. (orgstruct-is-++
  19180. (org-let org-fb-vars
  19181. '(fill-paragraph justify)))
  19182. (t nil))))
  19183. (defun org-adaptive-fill-function ()
  19184. "Return a fill prefix for org-mode files."
  19185. (let (itemp)
  19186. (save-excursion
  19187. (cond
  19188. ;; Comment line
  19189. ((looking-at "#[ \t]+")
  19190. (match-string-no-properties 0))
  19191. ;; Plain list item
  19192. ((org-at-item-p)
  19193. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  19194. ;; Point is in a list after `backward-paragraph': original
  19195. ;; point wasn't in the list, or filling would have been taken
  19196. ;; care of by `org-auto-fill-function', but the list and the
  19197. ;; real paragraph are not separated by a blank line. Thus, move
  19198. ;; point after the list to go back to real paragraph and
  19199. ;; determine fill-prefix.
  19200. ((setq itemp (org-in-item-p))
  19201. (goto-char itemp)
  19202. (let* ((struct (org-list-struct))
  19203. (bottom (org-list-get-bottom-point struct)))
  19204. (goto-char bottom)
  19205. (make-string (org-get-indentation) ?\ )))
  19206. ;; Other text
  19207. ((looking-at org-adaptive-fill-regexp-backup)
  19208. (match-string-no-properties 0))))))
  19209. (defun org-auto-fill-function ()
  19210. "Auto-fill function."
  19211. (unless (and org-src-prevent-auto-filling (org-in-src-block-p))
  19212. (let (itemp prefix)
  19213. ;; When in a list, compute an appropriate fill-prefix and make
  19214. ;; sure it will be used by `do-auto-fill'.
  19215. (cond ((setq itemp (org-in-item-p))
  19216. (progn
  19217. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  19218. (org-flet ((fill-context-prefix (from to &optional flr) prefix))
  19219. (do-auto-fill))))
  19220. (orgstruct-is-++
  19221. (org-let org-fb-vars
  19222. '(do-auto-fill)))
  19223. (t (do-auto-fill))))))
  19224. ;;; Other stuff.
  19225. (defun org-toggle-fixed-width-section (arg)
  19226. "Toggle the fixed-width export.
  19227. If there is no active region, the QUOTE keyword at the current headline is
  19228. inserted or removed. When present, it causes the text between this headline
  19229. and the next to be exported as fixed-width text, and unmodified.
  19230. If there is an active region, this command adds or removes a colon as the
  19231. first character of this line. If the first character of a line is a colon,
  19232. this line is also exported in fixed-width font."
  19233. (interactive "P")
  19234. (let* ((cc 0)
  19235. (regionp (org-region-active-p))
  19236. (beg (if regionp (region-beginning) (point)))
  19237. (end (if regionp (region-end)))
  19238. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  19239. (case-fold-search nil)
  19240. (re "[ \t]*\\(:\\(?: \\|$\\)\\)")
  19241. off)
  19242. (if regionp
  19243. (save-excursion
  19244. (goto-char beg)
  19245. (setq cc (current-column))
  19246. (beginning-of-line 1)
  19247. (setq off (looking-at re))
  19248. (while (> nlines 0)
  19249. (setq nlines (1- nlines))
  19250. (beginning-of-line 1)
  19251. (cond
  19252. (arg
  19253. (org-move-to-column cc t)
  19254. (insert ": \n")
  19255. (forward-line -1))
  19256. ((and off (looking-at re))
  19257. (replace-match "" t t nil 1))
  19258. ((not off) (org-move-to-column cc t) (insert ": ")))
  19259. (forward-line 1)))
  19260. (save-excursion
  19261. (org-back-to-heading)
  19262. (cond
  19263. ((looking-at (format org-heading-keyword-regexp-format
  19264. org-quote-string))
  19265. (goto-char (match-end 1))
  19266. (looking-at (concat " +" org-quote-string))
  19267. (replace-match "" t t)
  19268. (when (eolp) (insert " ")))
  19269. ((looking-at org-outline-regexp)
  19270. (goto-char (match-end 0))
  19271. (insert org-quote-string " ")))))))
  19272. (defun org-reftex-citation ()
  19273. "Use reftex-citation to insert a citation into the buffer.
  19274. This looks for a line like
  19275. #+BIBLIOGRAPHY: foo plain option:-d
  19276. and derives from it that foo.bib is the bibliography file relevant
  19277. for this document. It then installs the necessary environment for RefTeX
  19278. to work in this buffer and calls `reftex-citation' to insert a citation
  19279. into the buffer.
  19280. Export of such citations to both LaTeX and HTML is handled by the contributed
  19281. package org-exp-bibtex by Taru Karttunen."
  19282. (interactive)
  19283. (let ((reftex-docstruct-symbol 'rds)
  19284. (reftex-cite-format "\\cite{%l}")
  19285. rds bib)
  19286. (save-excursion
  19287. (save-restriction
  19288. (widen)
  19289. (let ((case-fold-search t)
  19290. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  19291. (if (not (save-excursion
  19292. (or (re-search-forward re nil t)
  19293. (re-search-backward re nil t))))
  19294. (error "No bibliography defined in file")
  19295. (setq bib (concat (match-string 1) ".bib")
  19296. rds (list (list 'bib bib)))))))
  19297. (call-interactively 'reftex-citation)))
  19298. ;;;; Functions extending outline functionality
  19299. (defun org-beginning-of-line (&optional arg)
  19300. "Go to the beginning of the current line. If that is invisible, continue
  19301. to a visible line beginning. This makes the function of C-a more intuitive.
  19302. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19303. first attempt, and only move to after the tags when the cursor is already
  19304. beyond the end of the headline."
  19305. (interactive "P")
  19306. (let ((pos (point))
  19307. (special (if (consp org-special-ctrl-a/e)
  19308. (car org-special-ctrl-a/e)
  19309. org-special-ctrl-a/e))
  19310. refpos)
  19311. (if (org-bound-and-true-p line-move-visual)
  19312. (beginning-of-visual-line 1)
  19313. (beginning-of-line 1))
  19314. (if (and arg (fboundp 'move-beginning-of-line))
  19315. (call-interactively 'move-beginning-of-line)
  19316. (if (bobp)
  19317. nil
  19318. (backward-char 1)
  19319. (if (org-truely-invisible-p)
  19320. (while (and (not (bobp)) (org-truely-invisible-p))
  19321. (backward-char 1)
  19322. (beginning-of-line 1))
  19323. (forward-char 1))))
  19324. (when special
  19325. (cond
  19326. ((and (looking-at org-complex-heading-regexp)
  19327. (= (char-after (match-end 1)) ?\ ))
  19328. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  19329. (point-at-eol)))
  19330. (goto-char
  19331. (if (eq special t)
  19332. (cond ((> pos refpos) refpos)
  19333. ((= pos (point)) refpos)
  19334. (t (point)))
  19335. (cond ((> pos (point)) (point))
  19336. ((not (eq last-command this-command)) (point))
  19337. (t refpos)))))
  19338. ((org-at-item-p)
  19339. ;; Being at an item and not looking at an the item means point
  19340. ;; was previously moved to beginning of a visual line, which
  19341. ;; doesn't contain the item. Therefore, do nothing special,
  19342. ;; just stay here.
  19343. (when (looking-at org-list-full-item-re)
  19344. ;; Set special position at first white space character after
  19345. ;; bullet, and check-box, if any.
  19346. (let ((after-bullet
  19347. (let ((box (match-end 3)))
  19348. (if (not box) (match-end 1)
  19349. (let ((after (char-after box)))
  19350. (if (and after (= after ? )) (1+ box) box))))))
  19351. ;; Special case: Move point to special position when
  19352. ;; currently after it or at beginning of line.
  19353. (if (eq special t)
  19354. (when (or (> pos after-bullet) (= (point) pos))
  19355. (goto-char after-bullet))
  19356. ;; Reversed case: Move point to special position when
  19357. ;; point was already at beginning of line and command is
  19358. ;; repeated.
  19359. (when (and (= (point) pos) (eq last-command this-command))
  19360. (goto-char after-bullet))))))))
  19361. (org-no-warnings
  19362. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19363. (defun org-end-of-line (&optional arg)
  19364. "Go to the end of the line.
  19365. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  19366. first attempt, and only move to after the tags when the cursor is already
  19367. beyond the end of the headline."
  19368. (interactive "P")
  19369. (let ((special (if (consp org-special-ctrl-a/e)
  19370. (cdr org-special-ctrl-a/e)
  19371. org-special-ctrl-a/e)))
  19372. (cond
  19373. ((or (not special) arg
  19374. (not (or (org-at-heading-p) (org-at-item-p) (org-at-drawer-p))))
  19375. (call-interactively
  19376. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19377. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19378. (t 'end-of-line))))
  19379. ((org-at-heading-p)
  19380. (let ((pos (point)))
  19381. (beginning-of-line 1)
  19382. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  19383. (if (eq special t)
  19384. (if (or (< pos (match-beginning 1))
  19385. (= pos (match-end 0)))
  19386. (goto-char (match-beginning 1))
  19387. (goto-char (match-end 0)))
  19388. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  19389. (goto-char (match-end 0))
  19390. (goto-char (match-beginning 1))))
  19391. (call-interactively (if (fboundp 'move-end-of-line)
  19392. 'move-end-of-line
  19393. 'end-of-line)))))
  19394. ((org-at-drawer-p)
  19395. (move-end-of-line 1)
  19396. (when (overlays-at (1- (point))) (backward-char 1)))
  19397. ;; At an item: Move before any hidden text.
  19398. (t (call-interactively
  19399. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  19400. ((fboundp 'move-end-of-line) 'move-end-of-line)
  19401. (t 'end-of-line)))))
  19402. (org-no-warnings
  19403. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  19404. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  19405. (define-key org-mode-map "\C-e" 'org-end-of-line)
  19406. (defun org-backward-sentence (&optional arg)
  19407. "Go to beginning of sentence, or beginning of table field.
  19408. This will call `backward-sentence' or `org-table-beginning-of-field',
  19409. depending on context."
  19410. (interactive "P")
  19411. (cond
  19412. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  19413. (t (call-interactively 'backward-sentence))))
  19414. (defun org-forward-sentence (&optional arg)
  19415. "Go to end of sentence, or end of table field.
  19416. This will call `forward-sentence' or `org-table-end-of-field',
  19417. depending on context."
  19418. (interactive "P")
  19419. (cond
  19420. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  19421. (t (call-interactively 'forward-sentence))))
  19422. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  19423. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  19424. (defun org-kill-line (&optional arg)
  19425. "Kill line, to tags or end of line."
  19426. (interactive "P")
  19427. (cond
  19428. ((or (not org-special-ctrl-k)
  19429. (bolp)
  19430. (not (org-at-heading-p)))
  19431. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  19432. org-ctrl-k-protect-subtree)
  19433. (if (or (eq org-ctrl-k-protect-subtree 'error)
  19434. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  19435. (error "C-k aborted - would kill hidden subtree")))
  19436. (call-interactively
  19437. (if (and (boundp 'visual-line-mode) visual-line-mode) 'kill-visual-line 'kill-line)))
  19438. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  19439. (kill-region (point) (match-beginning 1))
  19440. (org-set-tags nil t))
  19441. (t (kill-region (point) (point-at-eol)))))
  19442. (define-key org-mode-map "\C-k" 'org-kill-line)
  19443. (defun org-yank (&optional arg)
  19444. "Yank. If the kill is a subtree, treat it specially.
  19445. This command will look at the current kill and check if is a single
  19446. subtree, or a series of subtrees[1]. If it passes the test, and if the
  19447. cursor is at the beginning of a line or after the stars of a currently
  19448. empty headline, then the yank is handled specially. How exactly depends
  19449. on the value of the following variables, both set by default.
  19450. org-yank-folded-subtrees
  19451. When set, the subtree(s) will be folded after insertion, but only
  19452. if doing so would now swallow text after the yanked text.
  19453. org-yank-adjusted-subtrees
  19454. When set, the subtree will be promoted or demoted in order to
  19455. fit into the local outline tree structure, which means that the level
  19456. will be adjusted so that it becomes the smaller one of the two
  19457. *visible* surrounding headings.
  19458. Any prefix to this command will cause `yank' to be called directly with
  19459. no special treatment. In particular, a simple \\[universal-argument] prefix \
  19460. will just
  19461. plainly yank the text as it is.
  19462. \[1] The test checks if the first non-white line is a heading
  19463. and if there are no other headings with fewer stars."
  19464. (interactive "P")
  19465. (org-yank-generic 'yank arg))
  19466. (defun org-yank-generic (command arg)
  19467. "Perform some yank-like command.
  19468. This function implements the behavior described in the `org-yank'
  19469. documentation. However, it has been generalized to work for any
  19470. interactive command with similar behavior."
  19471. ;; pretend to be command COMMAND
  19472. (setq this-command command)
  19473. (if arg
  19474. (call-interactively command)
  19475. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  19476. (and (org-kill-is-subtree-p)
  19477. (or (bolp)
  19478. (and (looking-at "[ \t]*$")
  19479. (string-match
  19480. "\\`\\*+\\'"
  19481. (buffer-substring (point-at-bol) (point)))))))
  19482. swallowp)
  19483. (cond
  19484. ((and subtreep org-yank-folded-subtrees)
  19485. (let ((beg (point))
  19486. end)
  19487. (if (and subtreep org-yank-adjusted-subtrees)
  19488. (org-paste-subtree nil nil 'for-yank)
  19489. (call-interactively command))
  19490. (setq end (point))
  19491. (goto-char beg)
  19492. (when (and (bolp) subtreep
  19493. (not (setq swallowp
  19494. (org-yank-folding-would-swallow-text beg end))))
  19495. (org-with-limited-levels
  19496. (or (looking-at org-outline-regexp)
  19497. (re-search-forward org-outline-regexp-bol end t))
  19498. (while (and (< (point) end) (looking-at org-outline-regexp))
  19499. (hide-subtree)
  19500. (org-cycle-show-empty-lines 'folded)
  19501. (condition-case nil
  19502. (outline-forward-same-level 1)
  19503. (error (goto-char end))))))
  19504. (when swallowp
  19505. (message
  19506. "Inserted text not folded because that would swallow text"))
  19507. (goto-char end)
  19508. (skip-chars-forward " \t\n\r")
  19509. (beginning-of-line 1)
  19510. (push-mark beg 'nomsg)))
  19511. ((and subtreep org-yank-adjusted-subtrees)
  19512. (let ((beg (point-at-bol)))
  19513. (org-paste-subtree nil nil 'for-yank)
  19514. (push-mark beg 'nomsg)))
  19515. (t
  19516. (call-interactively command))))))
  19517. (defun org-yank-folding-would-swallow-text (beg end)
  19518. "Would hide-subtree at BEG swallow any text after END?"
  19519. (let (level)
  19520. (org-with-limited-levels
  19521. (save-excursion
  19522. (goto-char beg)
  19523. (when (or (looking-at org-outline-regexp)
  19524. (re-search-forward org-outline-regexp-bol end t))
  19525. (setq level (org-outline-level)))
  19526. (goto-char end)
  19527. (skip-chars-forward " \t\r\n\v\f")
  19528. (if (or (eobp)
  19529. (and (bolp) (looking-at org-outline-regexp)
  19530. (<= (org-outline-level) level)))
  19531. nil ; Nothing would be swallowed
  19532. t))))) ; something would swallow
  19533. (define-key org-mode-map "\C-y" 'org-yank)
  19534. (defun org-truely-invisible-p ()
  19535. "Check if point is at a character currently not visible.
  19536. This version does not only check the character property, but also
  19537. `visible-mode'."
  19538. ;; Early versions of noutline don't have `outline-invisible-p'.
  19539. (if (org-bound-and-true-p visible-mode)
  19540. nil
  19541. (outline-invisible-p)))
  19542. (defun org-invisible-p2 ()
  19543. "Check if point is at a character currently not visible."
  19544. (save-excursion
  19545. (if (and (eolp) (not (bobp))) (backward-char 1))
  19546. ;; Early versions of noutline don't have `outline-invisible-p'.
  19547. (outline-invisible-p)))
  19548. (defun org-back-to-heading (&optional invisible-ok)
  19549. "Call `outline-back-to-heading', but provide a better error message."
  19550. (condition-case nil
  19551. (outline-back-to-heading invisible-ok)
  19552. (error (error "Before first headline at position %d in buffer %s"
  19553. (point) (current-buffer)))))
  19554. (defun org-beginning-of-defun ()
  19555. "Go to the beginning of the subtree, i.e. back to the heading."
  19556. (org-back-to-heading))
  19557. (defun org-end-of-defun ()
  19558. "Go to the end of the subtree."
  19559. (org-end-of-subtree nil t))
  19560. (defun org-before-first-heading-p ()
  19561. "Before first heading?"
  19562. (save-excursion
  19563. (end-of-line)
  19564. (null (re-search-backward org-outline-regexp-bol nil t))))
  19565. (defun org-at-heading-p (&optional ignored)
  19566. (outline-on-heading-p t))
  19567. ;; Compatibility alias with Org versions < 7.8.03
  19568. (defalias 'org-on-heading-p 'org-at-heading-p)
  19569. (defun org-at-comment-p nil
  19570. "Is cursor in a line starting with a # character?"
  19571. (save-excursion
  19572. (beginning-of-line)
  19573. (looking-at "^#")))
  19574. (defun org-at-drawer-p nil
  19575. "Is cursor at a drawer keyword?"
  19576. (save-excursion
  19577. (move-beginning-of-line 1)
  19578. (looking-at org-drawer-regexp)))
  19579. (defun org-at-block-p nil
  19580. "Is cursor at a block keyword?"
  19581. (save-excursion
  19582. (move-beginning-of-line 1)
  19583. (looking-at org-block-regexp)))
  19584. (defun org-point-at-end-of-empty-headline ()
  19585. "If point is at the end of an empty headline, return t, else nil.
  19586. If the heading only contains a TODO keyword, it is still still considered
  19587. empty."
  19588. (and (looking-at "[ \t]*$")
  19589. (when org-todo-line-regexp
  19590. (save-excursion
  19591. (beginning-of-line 1)
  19592. (let ((case-fold-search nil))
  19593. (looking-at org-todo-line-regexp)
  19594. (string= (match-string 3) ""))))))
  19595. (defun org-at-heading-or-item-p ()
  19596. (or (org-at-heading-p) (org-at-item-p)))
  19597. (defun org-at-target-p ()
  19598. (or (org-in-regexp org-radio-target-regexp)
  19599. (org-in-regexp org-target-regexp)))
  19600. ;; Compatibility alias with Org versions < 7.8.03
  19601. (defalias 'org-on-target-p 'org-at-target-p)
  19602. (defun org-up-heading-all (arg)
  19603. "Move to the heading line of which the present line is a subheading.
  19604. This function considers both visible and invisible heading lines.
  19605. With argument, move up ARG levels."
  19606. (if (fboundp 'outline-up-heading-all)
  19607. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  19608. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  19609. (defun org-up-heading-safe ()
  19610. "Move to the heading line of which the present line is a subheading.
  19611. This version will not throw an error. It will return the level of the
  19612. headline found, or nil if no higher level is found.
  19613. Also, this function will be a lot faster than `outline-up-heading',
  19614. because it relies on stars being the outline starters. This can really
  19615. make a significant difference in outlines with very many siblings."
  19616. (let (start-level re)
  19617. (org-back-to-heading t)
  19618. (setq start-level (funcall outline-level))
  19619. (if (equal start-level 1)
  19620. nil
  19621. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  19622. (if (re-search-backward re nil t)
  19623. (funcall outline-level)))))
  19624. (defun org-first-sibling-p ()
  19625. "Is this heading the first child of its parents?"
  19626. (interactive)
  19627. (let ((re org-outline-regexp-bol)
  19628. level l)
  19629. (unless (org-at-heading-p t)
  19630. (error "Not at a heading"))
  19631. (setq level (funcall outline-level))
  19632. (save-excursion
  19633. (if (not (re-search-backward re nil t))
  19634. t
  19635. (setq l (funcall outline-level))
  19636. (< l level)))))
  19637. (defun org-goto-sibling (&optional previous)
  19638. "Goto the next sibling, even if it is invisible.
  19639. When PREVIOUS is set, go to the previous sibling instead. Returns t
  19640. when a sibling was found. When none is found, return nil and don't
  19641. move point."
  19642. (let ((fun (if previous 're-search-backward 're-search-forward))
  19643. (pos (point))
  19644. (re org-outline-regexp-bol)
  19645. level l)
  19646. (when (condition-case nil (org-back-to-heading t) (error nil))
  19647. (setq level (funcall outline-level))
  19648. (catch 'exit
  19649. (or previous (forward-char 1))
  19650. (while (funcall fun re nil t)
  19651. (setq l (funcall outline-level))
  19652. (when (< l level) (goto-char pos) (throw 'exit nil))
  19653. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  19654. (goto-char pos)
  19655. nil))))
  19656. (defun org-show-siblings ()
  19657. "Show all siblings of the current headline."
  19658. (save-excursion
  19659. (while (org-goto-sibling) (org-flag-heading nil)))
  19660. (save-excursion
  19661. (while (org-goto-sibling 'previous)
  19662. (org-flag-heading nil))))
  19663. (defun org-goto-first-child ()
  19664. "Goto the first child, even if it is invisible.
  19665. Return t when a child was found. Otherwise don't move point and
  19666. return nil."
  19667. (let (level (pos (point)) (re org-outline-regexp-bol))
  19668. (when (condition-case nil (org-back-to-heading t) (error nil))
  19669. (setq level (outline-level))
  19670. (forward-char 1)
  19671. (if (and (re-search-forward re nil t) (> (outline-level) level))
  19672. (progn (goto-char (match-beginning 0)) t)
  19673. (goto-char pos) nil))))
  19674. (defun org-show-hidden-entry ()
  19675. "Show an entry where even the heading is hidden."
  19676. (save-excursion
  19677. (org-show-entry)))
  19678. (defun org-flag-heading (flag &optional entry)
  19679. "Flag the current heading. FLAG non-nil means make invisible.
  19680. When ENTRY is non-nil, show the entire entry."
  19681. (save-excursion
  19682. (org-back-to-heading t)
  19683. ;; Check if we should show the entire entry
  19684. (if entry
  19685. (progn
  19686. (org-show-entry)
  19687. (save-excursion
  19688. (and (outline-next-heading)
  19689. (org-flag-heading nil))))
  19690. (outline-flag-region (max (point-min) (1- (point)))
  19691. (save-excursion (outline-end-of-heading) (point))
  19692. flag))))
  19693. (defun org-get-next-sibling ()
  19694. "Move to next heading of the same level, and return point.
  19695. If there is no such heading, return nil.
  19696. This is like outline-next-sibling, but invisible headings are ok."
  19697. (let ((level (funcall outline-level)))
  19698. (outline-next-heading)
  19699. (while (and (not (eobp)) (> (funcall outline-level) level))
  19700. (outline-next-heading))
  19701. (if (or (eobp) (< (funcall outline-level) level))
  19702. nil
  19703. (point))))
  19704. (defun org-get-last-sibling ()
  19705. "Move to previous heading of the same level, and return point.
  19706. If there is no such heading, return nil."
  19707. (let ((opoint (point))
  19708. (level (funcall outline-level)))
  19709. (outline-previous-heading)
  19710. (when (and (/= (point) opoint) (outline-on-heading-p t))
  19711. (while (and (> (funcall outline-level) level)
  19712. (not (bobp)))
  19713. (outline-previous-heading))
  19714. (if (< (funcall outline-level) level)
  19715. nil
  19716. (point)))))
  19717. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  19718. ;; This contains an exact copy of the original function, but it uses
  19719. ;; `org-back-to-heading', to make it work also in invisible
  19720. ;; trees. And is uses an invisible-OK argument.
  19721. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  19722. ;; Furthermore, when used inside Org, finding the end of a large subtree
  19723. ;; with many children and grandchildren etc, this can be much faster
  19724. ;; than the outline version.
  19725. (org-back-to-heading invisible-OK)
  19726. (let ((first t)
  19727. (level (funcall outline-level)))
  19728. (if (and (derived-mode-p 'org-mode) (< level 1000))
  19729. ;; A true heading (not a plain list item), in Org-mode
  19730. ;; This means we can easily find the end by looking
  19731. ;; only for the right number of stars. Using a regexp to do
  19732. ;; this is so much faster than using a Lisp loop.
  19733. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  19734. (forward-char 1)
  19735. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  19736. ;; something else, do it the slow way
  19737. (while (and (not (eobp))
  19738. (or first (> (funcall outline-level) level)))
  19739. (setq first nil)
  19740. (outline-next-heading)))
  19741. (unless to-heading
  19742. (if (memq (preceding-char) '(?\n ?\^M))
  19743. (progn
  19744. ;; Go to end of line before heading
  19745. (forward-char -1)
  19746. (if (memq (preceding-char) '(?\n ?\^M))
  19747. ;; leave blank line before heading
  19748. (forward-char -1))))))
  19749. (point))
  19750. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  19751. "Use Org version in org-mode, for dramatic speed-up."
  19752. (if (derived-mode-p 'org-mode)
  19753. (progn
  19754. (org-end-of-subtree nil t)
  19755. (unless (eobp) (backward-char 1)))
  19756. ad-do-it))
  19757. (defun org-end-of-meta-data-and-drawers ()
  19758. "Jump to the first text after meta data and drawers in the current entry.
  19759. This will move over empty lines, lines with planning time stamps,
  19760. clocking lines, and drawers."
  19761. (org-back-to-heading t)
  19762. (let ((end (save-excursion (outline-next-heading) (point)))
  19763. (re (concat "\\(" org-drawer-regexp "\\)"
  19764. "\\|" "[ \t]*" org-keyword-time-regexp)))
  19765. (forward-line 1)
  19766. (while (re-search-forward re end t)
  19767. (if (not (match-end 1))
  19768. ;; empty or planning line
  19769. (forward-line 1)
  19770. ;; a drawer, find the end
  19771. (re-search-forward "^[ \t]*:END:" end 'move)
  19772. (forward-line 1)))
  19773. (and (re-search-forward "[^\n]" nil t) (backward-char 1))
  19774. (point)))
  19775. (defun org-forward-same-level (arg &optional invisible-ok)
  19776. "Move forward to the arg'th subheading at same level as this one.
  19777. Stop at the first and last subheadings of a superior heading.
  19778. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  19779. it wil also look at invisible ones."
  19780. (interactive "p")
  19781. (org-back-to-heading invisible-ok)
  19782. (org-at-heading-p)
  19783. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19784. (re (format "^\\*\\{1,%d\\} " level))
  19785. l)
  19786. (forward-char 1)
  19787. (while (> arg 0)
  19788. (while (and (re-search-forward re nil 'move)
  19789. (setq l (- (match-end 0) (match-beginning 0) 1))
  19790. (= l level)
  19791. (not invisible-ok)
  19792. (progn (backward-char 1) (outline-invisible-p)))
  19793. (if (< l level) (setq arg 1)))
  19794. (setq arg (1- arg)))
  19795. (beginning-of-line 1)))
  19796. (defun org-backward-same-level (arg &optional invisible-ok)
  19797. "Move backward to the arg'th subheading at same level as this one.
  19798. Stop at the first and last subheadings of a superior heading."
  19799. (interactive "p")
  19800. (org-back-to-heading)
  19801. (org-at-heading-p)
  19802. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  19803. (re (format "^\\*\\{1,%d\\} " level))
  19804. l)
  19805. (while (> arg 0)
  19806. (while (and (re-search-backward re nil 'move)
  19807. (setq l (- (match-end 0) (match-beginning 0) 1))
  19808. (= l level)
  19809. (not invisible-ok)
  19810. (outline-invisible-p))
  19811. (if (< l level) (setq arg 1)))
  19812. (setq arg (1- arg)))))
  19813. (defun org-show-subtree ()
  19814. "Show everything after this heading at deeper levels."
  19815. (interactive)
  19816. (outline-flag-region
  19817. (point)
  19818. (save-excursion
  19819. (org-end-of-subtree t t))
  19820. nil))
  19821. (defun org-show-entry ()
  19822. "Show the body directly following this heading.
  19823. Show the heading too, if it is currently invisible."
  19824. (interactive)
  19825. (save-excursion
  19826. (condition-case nil
  19827. (progn
  19828. (org-back-to-heading t)
  19829. (outline-flag-region
  19830. (max (point-min) (1- (point)))
  19831. (save-excursion
  19832. (if (re-search-forward
  19833. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  19834. (match-beginning 1)
  19835. (point-max)))
  19836. nil)
  19837. (org-cycle-hide-drawers 'children))
  19838. (error nil))))
  19839. (defun org-make-options-regexp (kwds &optional extra)
  19840. "Make a regular expression for keyword lines."
  19841. (concat
  19842. "^"
  19843. "#?[ \t]*\\+\\("
  19844. (mapconcat 'regexp-quote kwds "\\|")
  19845. (if extra (concat "\\|" extra))
  19846. "\\):[ \t]*"
  19847. "\\(.*\\)"))
  19848. ;; Make isearch reveal the necessary context
  19849. (defun org-isearch-end ()
  19850. "Reveal context after isearch exits."
  19851. (when isearch-success ; only if search was successful
  19852. (if (featurep 'xemacs)
  19853. ;; Under XEmacs, the hook is run in the correct place,
  19854. ;; we directly show the context.
  19855. (org-show-context 'isearch)
  19856. ;; In Emacs the hook runs *before* restoring the overlays.
  19857. ;; So we have to use a one-time post-command-hook to do this.
  19858. ;; (Emacs 22 has a special variable, see function `org-mode')
  19859. (unless (and (boundp 'isearch-mode-end-hook-quit)
  19860. isearch-mode-end-hook-quit)
  19861. ;; Only when the isearch was not quitted.
  19862. (org-add-hook 'post-command-hook 'org-isearch-post-command
  19863. 'append 'local)))))
  19864. (defun org-isearch-post-command ()
  19865. "Remove self from hook, and show context."
  19866. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  19867. (org-show-context 'isearch))
  19868. ;;;; Integration with and fixes for other packages
  19869. ;;; Imenu support
  19870. (defvar org-imenu-markers nil
  19871. "All markers currently used by Imenu.")
  19872. (make-variable-buffer-local 'org-imenu-markers)
  19873. (defun org-imenu-new-marker (&optional pos)
  19874. "Return a new marker for use by Imenu, and remember the marker."
  19875. (let ((m (make-marker)))
  19876. (move-marker m (or pos (point)))
  19877. (push m org-imenu-markers)
  19878. m))
  19879. (defun org-imenu-get-tree ()
  19880. "Produce the index for Imenu."
  19881. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  19882. (setq org-imenu-markers nil)
  19883. (let* ((n org-imenu-depth)
  19884. (re (concat "^" (org-get-limited-outline-regexp)))
  19885. (subs (make-vector (1+ n) nil))
  19886. (last-level 0)
  19887. m level head)
  19888. (save-excursion
  19889. (save-restriction
  19890. (widen)
  19891. (goto-char (point-max))
  19892. (while (re-search-backward re nil t)
  19893. (setq level (org-reduced-level (funcall outline-level)))
  19894. (when (and (<= level n)
  19895. (looking-at org-complex-heading-regexp))
  19896. (setq head (org-link-display-format
  19897. (org-match-string-no-properties 4))
  19898. m (org-imenu-new-marker))
  19899. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  19900. (if (>= level last-level)
  19901. (push (cons head m) (aref subs level))
  19902. (push (cons head (aref subs (1+ level))) (aref subs level))
  19903. (loop for i from (1+ level) to n do (aset subs i nil)))
  19904. (setq last-level level)))))
  19905. (aref subs 1)))
  19906. (eval-after-load "imenu"
  19907. '(progn
  19908. (add-hook 'imenu-after-jump-hook
  19909. (lambda ()
  19910. (if (derived-mode-p 'org-mode)
  19911. (org-show-context 'org-goto))))))
  19912. (defun org-link-display-format (link)
  19913. "Replace a link with either the description, or the link target
  19914. if no description is present"
  19915. (save-match-data
  19916. (if (string-match org-bracket-link-analytic-regexp link)
  19917. (replace-match (if (match-end 5)
  19918. (match-string 5 link)
  19919. (concat (match-string 1 link)
  19920. (match-string 3 link)))
  19921. nil t link)
  19922. link)))
  19923. (defun org-toggle-link-display ()
  19924. "Toggle the literal or descriptive display of links."
  19925. (interactive)
  19926. (if org-descriptive-links
  19927. (progn (org-remove-from-invisibility-spec '(org-link))
  19928. (org-restart-font-lock)
  19929. (setq org-descriptive-links nil))
  19930. (progn (add-to-invisibility-spec '(org-link))
  19931. (org-restart-font-lock)
  19932. (setq org-descriptive-links t))))
  19933. ;; Speedbar support
  19934. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  19935. "Overlay marking the agenda restriction line in speedbar.")
  19936. (overlay-put org-speedbar-restriction-lock-overlay
  19937. 'face 'org-agenda-restriction-lock)
  19938. (overlay-put org-speedbar-restriction-lock-overlay
  19939. 'help-echo "Agendas are currently limited to this item.")
  19940. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  19941. (defun org-speedbar-set-agenda-restriction ()
  19942. "Restrict future agenda commands to the location at point in speedbar.
  19943. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  19944. (interactive)
  19945. (require 'org-agenda)
  19946. (let (p m tp np dir txt)
  19947. (cond
  19948. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19949. 'org-imenu t))
  19950. (setq m (get-text-property p 'org-imenu-marker))
  19951. (with-current-buffer (marker-buffer m)
  19952. (goto-char m)
  19953. (org-agenda-set-restriction-lock 'subtree)))
  19954. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  19955. 'speedbar-function 'speedbar-find-file))
  19956. (setq tp (previous-single-property-change
  19957. (1+ p) 'speedbar-function)
  19958. np (next-single-property-change
  19959. tp 'speedbar-function)
  19960. dir (speedbar-line-directory)
  19961. txt (buffer-substring-no-properties (or tp (point-min))
  19962. (or np (point-max))))
  19963. (with-current-buffer (find-file-noselect
  19964. (let ((default-directory dir))
  19965. (expand-file-name txt)))
  19966. (unless (derived-mode-p 'org-mode)
  19967. (error "Cannot restrict to non-Org-mode file"))
  19968. (org-agenda-set-restriction-lock 'file)))
  19969. (t (error "Don't know how to restrict Org-mode's agenda")))
  19970. (move-overlay org-speedbar-restriction-lock-overlay
  19971. (point-at-bol) (point-at-eol))
  19972. (setq current-prefix-arg nil)
  19973. (org-agenda-maybe-redo)))
  19974. (eval-after-load "speedbar"
  19975. '(progn
  19976. (speedbar-add-supported-extension ".org")
  19977. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  19978. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  19979. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  19980. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  19981. (add-hook 'speedbar-visiting-tag-hook
  19982. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  19983. ;;; Fixes and Hacks for problems with other packages
  19984. ;; Make flyspell not check words in links, to not mess up our keymap
  19985. (defun org-mode-flyspell-verify ()
  19986. "Don't let flyspell put overlays at active buttons, or on
  19987. {todo,all-time,additional-option-like}-keywords."
  19988. (let ((pos (max (1- (point)) (point-min)))
  19989. (word (thing-at-point 'word)))
  19990. (and (not (get-text-property pos 'keymap))
  19991. (not (get-text-property pos 'org-no-flyspell))
  19992. (not (member word org-todo-keywords-1))
  19993. (not (member word org-all-time-keywords))
  19994. (not (member word org-additional-option-like-keywords)))))
  19995. (defun org-remove-flyspell-overlays-in (beg end)
  19996. "Remove flyspell overlays in region."
  19997. (and (org-bound-and-true-p flyspell-mode)
  19998. (fboundp 'flyspell-delete-region-overlays)
  19999. (flyspell-delete-region-overlays beg end))
  20000. (add-text-properties beg end '(org-no-flyspell t)))
  20001. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  20002. (eval-after-load "bookmark"
  20003. '(if (boundp 'bookmark-after-jump-hook)
  20004. ;; We can use the hook
  20005. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  20006. ;; Hook not available, use advice
  20007. (defadvice bookmark-jump (after org-make-visible activate)
  20008. "Make the position visible."
  20009. (org-bookmark-jump-unhide))))
  20010. ;; Make sure saveplace shows the location if it was hidden
  20011. (eval-after-load "saveplace"
  20012. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  20013. "Make the position visible."
  20014. (org-bookmark-jump-unhide)))
  20015. ;; Make sure ecb shows the location if it was hidden
  20016. (eval-after-load "ecb"
  20017. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  20018. "Make hierarchy visible when jumping into location from ECB tree buffer."
  20019. (if (derived-mode-p 'org-mode)
  20020. (org-show-context))))
  20021. (defun org-bookmark-jump-unhide ()
  20022. "Unhide the current position, to show the bookmark location."
  20023. (and (derived-mode-p 'org-mode)
  20024. (or (outline-invisible-p)
  20025. (save-excursion (goto-char (max (point-min) (1- (point))))
  20026. (outline-invisible-p)))
  20027. (org-show-context 'bookmark-jump)))
  20028. ;; Make session.el ignore our circular variable
  20029. (eval-after-load "session"
  20030. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  20031. ;;;; Experimental code
  20032. (defun org-closed-in-range ()
  20033. "Sparse tree of items closed in a certain time range.
  20034. Still experimental, may disappear in the future."
  20035. (interactive)
  20036. ;; Get the time interval from the user.
  20037. (let* ((time1 (org-float-time
  20038. (org-read-date nil 'to-time nil "Starting date: ")))
  20039. (time2 (org-float-time
  20040. (org-read-date nil 'to-time nil "End date:")))
  20041. ;; callback function
  20042. (callback (lambda ()
  20043. (let ((time
  20044. (org-float-time
  20045. (apply 'encode-time
  20046. (org-parse-time-string
  20047. (match-string 1))))))
  20048. ;; check if time in interval
  20049. (and (>= time time1) (<= time time2))))))
  20050. ;; make tree, check each match with the callback
  20051. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  20052. ;;;; Finish up
  20053. (provide 'org)
  20054. (run-hooks 'org-load-hook)
  20055. ;;; org.el ends here