org.el 756 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
  4. ;; Free Software Foundation, Inc.
  5. ;;
  6. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;; Version: 7.5
  10. ;;
  11. ;; This file is part of GNU Emacs.
  12. ;;
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  73. (when (fboundp 'defvaralias)
  74. (unless (boundp 'calendar-view-holidays-initially-flag)
  75. (defvaralias 'calendar-view-holidays-initially-flag
  76. 'view-calendar-holidays-initially))
  77. (unless (boundp 'calendar-view-diary-initially-flag)
  78. (defvaralias 'calendar-view-diary-initially-flag
  79. 'view-diary-entries-initially))
  80. (unless (boundp 'diary-fancy-buffer)
  81. (defvaralias 'diary-fancy-buffer 'fancy-diary-buffer)))
  82. (require 'outline) (require 'noutline)
  83. ;; Other stuff we need.
  84. (require 'time-date)
  85. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  86. (require 'easymenu)
  87. (require 'overlay)
  88. (require 'org-macs)
  89. (require 'org-entities)
  90. (require 'org-compat)
  91. (require 'org-faces)
  92. (require 'org-list)
  93. (require 'org-pcomplete)
  94. (require 'org-src)
  95. (require 'org-footnote)
  96. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  97. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  98. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  99. ;; babel
  100. (require 'ob)
  101. (require 'ob-table)
  102. (require 'ob-lob)
  103. (require 'ob-ref)
  104. (require 'ob-tangle)
  105. (require 'ob-comint)
  106. (require 'ob-keys)
  107. ;; load languages based on value of `org-babel-load-languages'
  108. (defvar org-babel-load-languages)
  109. ;;;###autoload
  110. (defun org-babel-do-load-languages (sym value)
  111. "Load the languages defined in `org-babel-load-languages'."
  112. (set-default sym value)
  113. (mapc (lambda (pair)
  114. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  115. (if active
  116. (progn
  117. (require (intern (concat "ob-" lang))))
  118. (progn
  119. (funcall 'fmakunbound
  120. (intern (concat "org-babel-execute:" lang)))
  121. (funcall 'fmakunbound
  122. (intern (concat "org-babel-expand-body:" lang)))))))
  123. org-babel-load-languages))
  124. (defcustom org-babel-load-languages '((emacs-lisp . t))
  125. "Languages which can be evaluated in Org-mode buffers.
  126. This list can be used to load support for any of the languages
  127. below, note that each language will depend on a different set of
  128. system executables and/or Emacs modes. When a language is
  129. \"loaded\", then code blocks in that language can be evaluated
  130. with `org-babel-execute-src-block' bound by default to C-c
  131. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  132. be set to remove code block evaluation from the C-c C-c
  133. keybinding. By default only Emacs Lisp (which has no
  134. requirements) is loaded."
  135. :group 'org-babel
  136. :set 'org-babel-do-load-languages
  137. :type '(alist :tag "Babel Languages"
  138. :key-type
  139. (choice
  140. (const :tag "C" C)
  141. (const :tag "R" R)
  142. (const :tag "Asymptote" asymptote)
  143. (const :tag "Calc" calc)
  144. (const :tag "Clojure" clojure)
  145. (const :tag "CSS" css)
  146. (const :tag "Ditaa" ditaa)
  147. (const :tag "Dot" dot)
  148. (const :tag "Emacs Lisp" emacs-lisp)
  149. (const :tag "Gnuplot" gnuplot)
  150. (const :tag "Haskell" haskell)
  151. (const :tag "Javascript" js)
  152. (const :tag "Latex" latex)
  153. (const :tag "Ledger" ledger)
  154. (const :tag "Maxima" maxima)
  155. (const :tag "Matlab" matlab)
  156. (const :tag "Mscgen" mscgen)
  157. (const :tag "Ocaml" ocaml)
  158. (const :tag "Octave" octave)
  159. (const :tag "Org" org)
  160. (const :tag "Perl" perl)
  161. (const :tag "PlantUML" plantuml)
  162. (const :tag "Python" python)
  163. (const :tag "Ruby" ruby)
  164. (const :tag "Sass" sass)
  165. (const :tag "Scheme" scheme)
  166. (const :tag "Screen" screen)
  167. (const :tag "Shell Script" sh)
  168. (const :tag "Sql" sql)
  169. (const :tag "Sqlite" sqlite))
  170. :value-type (boolean :tag "Activate" :value t)))
  171. ;;;; Customization variables
  172. (defcustom org-clone-delete-id nil
  173. "Remove ID property of clones of a subtree.
  174. When non-nil, clones of a subtree don't inherit the ID property.
  175. Otherwise they inherit the ID property with a new unique
  176. identifier."
  177. :type 'boolean
  178. :group 'org-id)
  179. ;;; Version
  180. (defconst org-version "7.5"
  181. "The version number of the file org.el.")
  182. (defun org-version (&optional here)
  183. "Show the org-mode version in the echo area.
  184. With prefix arg HERE, insert it at point."
  185. (interactive "P")
  186. (let* ((origin default-directory)
  187. (version org-version)
  188. (git-version)
  189. (dir (concat (file-name-directory (locate-library "org")) "../" )))
  190. (when (and (file-exists-p (expand-file-name ".git" dir))
  191. (executable-find "git"))
  192. (unwind-protect
  193. (progn
  194. (cd dir)
  195. (when (eql 0 (shell-command "git describe --abbrev=4 HEAD"))
  196. (with-current-buffer "*Shell Command Output*"
  197. (goto-char (point-min))
  198. (setq git-version (buffer-substring (point) (point-at-eol))))
  199. (subst-char-in-string ?- ?. git-version t)
  200. (when (string-match "\\S-"
  201. (shell-command-to-string
  202. "git diff-index --name-only HEAD --"))
  203. (setq git-version (concat git-version ".dirty")))
  204. (setq version (concat version " (" git-version ")"))))
  205. (cd origin)))
  206. (setq version (format "Org-mode version %s" version))
  207. (if here (insert version))
  208. (message version)))
  209. ;;; Compatibility constants
  210. ;;; The custom variables
  211. (defgroup org nil
  212. "Outline-based notes management and organizer."
  213. :tag "Org"
  214. :group 'outlines
  215. :group 'calendar)
  216. (defcustom org-mode-hook nil
  217. "Mode hook for Org-mode, run after the mode was turned on."
  218. :group 'org
  219. :type 'hook)
  220. (defcustom org-load-hook nil
  221. "Hook that is run after org.el has been loaded."
  222. :group 'org
  223. :type 'hook)
  224. (defvar org-modules) ; defined below
  225. (defvar org-modules-loaded nil
  226. "Have the modules been loaded already?")
  227. (defun org-load-modules-maybe (&optional force)
  228. "Load all extensions listed in `org-modules'."
  229. (when (or force (not org-modules-loaded))
  230. (mapc (lambda (ext)
  231. (condition-case nil (require ext)
  232. (error (message "Problems while trying to load feature `%s'" ext))))
  233. org-modules)
  234. (setq org-modules-loaded t)))
  235. (defun org-set-modules (var value)
  236. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  237. (set var value)
  238. (when (featurep 'org)
  239. (org-load-modules-maybe 'force)))
  240. (when (org-bound-and-true-p org-modules)
  241. (let ((a (member 'org-infojs org-modules)))
  242. (and a (setcar a 'org-jsinfo))))
  243. (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)
  244. "Modules that should always be loaded together with org.el.
  245. If a description starts with <C>, the file is not part of Emacs
  246. and loading it will require that you have downloaded and properly installed
  247. the org-mode distribution.
  248. You can also use this system to load external packages (i.e. neither Org
  249. core modules, nor modules from the CONTRIB directory). Just add symbols
  250. to the end of the list. If the package is called org-xyz.el, then you need
  251. to add the symbol `xyz', and the package must have a call to
  252. (provide 'org-xyz)"
  253. :group 'org
  254. :set 'org-set-modules
  255. :type
  256. '(set :greedy t
  257. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  258. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  259. (const :tag " crypt: Encryption of subtrees" org-crypt)
  260. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  261. (const :tag " docview: Links to doc-view buffers" org-docview)
  262. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  263. (const :tag " id: Global IDs for identifying entries" org-id)
  264. (const :tag " info: Links to Info nodes" org-info)
  265. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  266. (const :tag " habit: Track your consistency with habits" org-habit)
  267. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  268. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  269. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  270. (const :tag " mew Links to Mew folders/messages" org-mew)
  271. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  272. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  273. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  274. (const :tag " special-blocks: Turn blocks into LaTeX envs and HTML divs" org-special-blocks)
  275. (const :tag " vm: Links to VM folders/messages" org-vm)
  276. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  277. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  278. (const :tag " mouse: Additional mouse support" org-mouse)
  279. (const :tag " TaskJuggler: Export tasks to a TaskJuggler project" org-taskjuggler)
  280. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  281. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  282. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  283. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  284. (const :tag "C collector: Collect properties into tables" org-collector)
  285. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  286. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  287. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  288. (const :tag "C eval: Include command output as text" org-eval)
  289. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  290. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  291. (const :tag "C exp-bibtex: Export citations using BibTeX" org-exp-bibtex)
  292. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  293. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  294. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  295. (const :tag "C jira: Add a jira:ticket protocol to Org-mode" org-jira)
  296. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  297. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  298. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  299. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  300. (const :tag "C mac-link-grabber Grab links and URLs from various Mac applications" org-mac-link-grabber)
  301. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  302. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  303. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  304. (const :tag "C registry: A registry for Org-mode links" org-registry)
  305. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  306. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  307. (const :tag "C secretary: Team management with org-mode" org-secretary)
  308. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  309. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  310. (const :tag "C track: Keep up with Org-mode development" org-track)
  311. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  312. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  313. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  314. (defcustom org-support-shift-select nil
  315. "Non-nil means make shift-cursor commands select text when possible.
  316. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  317. selecting a region, or enlarge regions started in this way.
  318. In Org-mode, in special contexts, these same keys are used for other
  319. purposes, important enough to compete with shift selection. Org tries
  320. to balance these needs by supporting `shift-select-mode' outside these
  321. special contexts, under control of this variable.
  322. The default of this variable is nil, to avoid confusing behavior. Shifted
  323. cursor keys will then execute Org commands in the following contexts:
  324. - on a headline, changing TODO state (left/right) and priority (up/down)
  325. - on a time stamp, changing the time
  326. - in a plain list item, changing the bullet type
  327. - in a property definition line, switching between allowed values
  328. - in the BEGIN line of a clock table (changing the time block).
  329. Outside these contexts, the commands will throw an error.
  330. When this variable is t and the cursor is not in a special context,
  331. Org-mode will support shift-selection for making and enlarging regions.
  332. To make this more effective, the bullet cycling will no longer happen
  333. anywhere in an item line, but only if the cursor is exactly on the bullet.
  334. If you set this variable to the symbol `always', then the keys
  335. will not be special in headlines, property lines, and item lines, to make
  336. shift selection work there as well. If this is what you want, you can
  337. use the following alternative commands: `C-c C-t' and `C-c ,' to
  338. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  339. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  340. edited by hand or in column view.
  341. However, when the cursor is on a timestamp, shift-cursor commands
  342. will still edit the time stamp - this is just too good to give up.
  343. XEmacs user should have this variable set to nil, because shift-select-mode
  344. is Emacs 23 only."
  345. :group 'org
  346. :type '(choice
  347. (const :tag "Never" nil)
  348. (const :tag "When outside special context" t)
  349. (const :tag "Everywhere except timestamps" always)))
  350. (defgroup org-startup nil
  351. "Options concerning startup of Org-mode."
  352. :tag "Org Startup"
  353. :group 'org)
  354. (defcustom org-startup-folded t
  355. "Non-nil means entering Org-mode will switch to OVERVIEW.
  356. This can also be configured on a per-file basis by adding one of
  357. the following lines anywhere in the buffer:
  358. #+STARTUP: fold (or `overview', this is equivalent)
  359. #+STARTUP: nofold (or `showall', this is equivalent)
  360. #+STARTUP: content
  361. #+STARTUP: showeverything"
  362. :group 'org-startup
  363. :type '(choice
  364. (const :tag "nofold: show all" nil)
  365. (const :tag "fold: overview" t)
  366. (const :tag "content: all headlines" content)
  367. (const :tag "show everything, even drawers" showeverything)))
  368. (defcustom org-startup-truncated t
  369. "Non-nil means entering Org-mode will set `truncate-lines'.
  370. This is useful since some lines containing links can be very long and
  371. uninteresting. Also tables look terrible when wrapped."
  372. :group 'org-startup
  373. :type 'boolean)
  374. (defcustom org-startup-indented nil
  375. "Non-nil means turn on `org-indent-mode' on startup.
  376. This can also be configured on a per-file basis by adding one of
  377. the following lines anywhere in the buffer:
  378. #+STARTUP: indent
  379. #+STARTUP: noindent"
  380. :group 'org-structure
  381. :type '(choice
  382. (const :tag "Not" nil)
  383. (const :tag "Globally (slow on startup in large files)" t)))
  384. (defcustom org-use-sub-superscripts t
  385. "Non-nil means interpret \"_\" and \"^\" for export.
  386. When this option is turned on, you can use TeX-like syntax for sub- and
  387. superscripts. Several characters after \"_\" or \"^\" will be
  388. considered as a single item - so grouping with {} is normally not
  389. needed. For example, the following things will be parsed as single
  390. sub- or superscripts.
  391. 10^24 or 10^tau several digits will be considered 1 item.
  392. 10^-12 or 10^-tau a leading sign with digits or a word
  393. x^2-y^3 will be read as x^2 - y^3, because items are
  394. terminated by almost any nonword/nondigit char.
  395. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  396. Still, ambiguity is possible - so when in doubt use {} to enclose the
  397. sub/superscript. If you set this variable to the symbol `{}',
  398. the braces are *required* in order to trigger interpretations as
  399. sub/superscript. This can be helpful in documents that need \"_\"
  400. frequently in plain text.
  401. Not all export backends support this, but HTML does.
  402. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  403. :group 'org-startup
  404. :group 'org-export-translation
  405. :type '(choice
  406. (const :tag "Always interpret" t)
  407. (const :tag "Only with braces" {})
  408. (const :tag "Never interpret" nil)))
  409. (if (fboundp 'defvaralias)
  410. (defvaralias 'org-export-with-sub-superscripts 'org-use-sub-superscripts))
  411. (defcustom org-startup-with-beamer-mode nil
  412. "Non-nil means turn on `org-beamer-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: beamer"
  416. :group 'org-startup
  417. :type 'boolean)
  418. (defcustom org-startup-align-all-tables nil
  419. "Non-nil means align all tables when visiting a file.
  420. This is useful when the column width in tables is forced with <N> cookies
  421. in table fields. Such tables will look correct only after the first re-align.
  422. This can also be configured on a per-file basis by adding one of
  423. the following lines anywhere in the buffer:
  424. #+STARTUP: align
  425. #+STARTUP: noalign"
  426. :group 'org-startup
  427. :type 'boolean)
  428. (defcustom org-startup-with-inline-images nil
  429. "Non-nil means show inline images when loading a new Org file.
  430. This can also be configured on a per-file basis by adding one of
  431. the following lines anywhere in the buffer:
  432. #+STARTUP: inlineimages
  433. #+STARTUP: noinlineimages"
  434. :group 'org-startup
  435. :type 'boolean)
  436. (defcustom org-insert-mode-line-in-empty-file nil
  437. "Non-nil means insert the first line setting Org-mode in empty files.
  438. When the function `org-mode' is called interactively in an empty file, this
  439. normally means that the file name does not automatically trigger Org-mode.
  440. To ensure that the file will always be in Org-mode in the future, a
  441. line enforcing Org-mode will be inserted into the buffer, if this option
  442. has been set."
  443. :group 'org-startup
  444. :type 'boolean)
  445. (defcustom org-replace-disputed-keys nil
  446. "Non-nil means use alternative key bindings for some keys.
  447. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  448. These keys are also used by other packages like shift-selection-mode'
  449. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  450. If you want to use Org-mode together with one of these other modes,
  451. or more generally if you would like to move some Org-mode commands to
  452. other keys, set this variable and configure the keys with the variable
  453. `org-disputed-keys'.
  454. This option is only relevant at load-time of Org-mode, and must be set
  455. *before* org.el is loaded. Changing it requires a restart of Emacs to
  456. become effective."
  457. :group 'org-startup
  458. :type 'boolean)
  459. (defcustom org-use-extra-keys nil
  460. "Non-nil means use extra key sequence definitions for certain commands.
  461. This happens automatically if you run XEmacs or if `window-system'
  462. is nil. This variable lets you do the same manually. You must
  463. set it before loading org.
  464. Example: on Carbon Emacs 22 running graphically, with an external
  465. keyboard on a Powerbook, the default way of setting M-left might
  466. not work for either Alt or ESC. Setting this variable will make
  467. it work for ESC."
  468. :group 'org-startup
  469. :type 'boolean)
  470. (if (fboundp 'defvaralias)
  471. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  472. (defcustom org-disputed-keys
  473. '(([(shift up)] . [(meta p)])
  474. ([(shift down)] . [(meta n)])
  475. ([(shift left)] . [(meta -)])
  476. ([(shift right)] . [(meta +)])
  477. ([(control shift right)] . [(meta shift +)])
  478. ([(control shift left)] . [(meta shift -)]))
  479. "Keys for which Org-mode and other modes compete.
  480. This is an alist, cars are the default keys, second element specifies
  481. the alternative to use when `org-replace-disputed-keys' is t.
  482. Keys can be specified in any syntax supported by `define-key'.
  483. The value of this option takes effect only at Org-mode's startup,
  484. therefore you'll have to restart Emacs to apply it after changing."
  485. :group 'org-startup
  486. :type 'alist)
  487. (defun org-key (key)
  488. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  489. Or return the original if not disputed.
  490. Also apply the translations defined in `org-xemacs-key-equivalents'."
  491. (when org-replace-disputed-keys
  492. (let* ((nkey (key-description key))
  493. (x (org-find-if (lambda (x)
  494. (equal (key-description (car x)) nkey))
  495. org-disputed-keys)))
  496. (setq key (if x (cdr x) key))))
  497. (when (featurep 'xemacs)
  498. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  499. key)
  500. (defun org-find-if (predicate seq)
  501. (catch 'exit
  502. (while seq
  503. (if (funcall predicate (car seq))
  504. (throw 'exit (car seq))
  505. (pop seq)))))
  506. (defun org-defkey (keymap key def)
  507. "Define a key, possibly translated, as returned by `org-key'."
  508. (define-key keymap (org-key key) def))
  509. (defcustom org-ellipsis nil
  510. "The ellipsis to use in the Org-mode outline.
  511. When nil, just use the standard three dots. When a string, use that instead,
  512. When a face, use the standard 3 dots, but with the specified face.
  513. The change affects only Org-mode (which will then use its own display table).
  514. Changing this requires executing `M-x org-mode' in a buffer to become
  515. effective."
  516. :group 'org-startup
  517. :type '(choice (const :tag "Default" nil)
  518. (face :tag "Face" :value org-warning)
  519. (string :tag "String" :value "...#")))
  520. (defvar org-display-table nil
  521. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  522. (defgroup org-keywords nil
  523. "Keywords in Org-mode."
  524. :tag "Org Keywords"
  525. :group 'org)
  526. (defcustom org-deadline-string "DEADLINE:"
  527. "String to mark deadline entries.
  528. A deadline is this string, followed by a time stamp. Should be a word,
  529. terminated by a colon. You can insert a schedule keyword and
  530. a timestamp with \\[org-deadline].
  531. Changes become only effective after restarting Emacs."
  532. :group 'org-keywords
  533. :type 'string)
  534. (defcustom org-scheduled-string "SCHEDULED:"
  535. "String to mark scheduled TODO entries.
  536. A schedule is this string, followed by a time stamp. Should be a word,
  537. terminated by a colon. You can insert a schedule keyword and
  538. a timestamp with \\[org-schedule].
  539. Changes become only effective after restarting Emacs."
  540. :group 'org-keywords
  541. :type 'string)
  542. (defcustom org-closed-string "CLOSED:"
  543. "String used as the prefix for timestamps logging closing a TODO entry."
  544. :group 'org-keywords
  545. :type 'string)
  546. (defcustom org-clock-string "CLOCK:"
  547. "String used as prefix for timestamps clocking work hours on an item."
  548. :group 'org-keywords
  549. :type 'string)
  550. (defcustom org-comment-string "COMMENT"
  551. "Entries starting with this keyword will never be exported.
  552. An entry can be toggled between COMMENT and normal with
  553. \\[org-toggle-comment].
  554. Changes become only effective after restarting Emacs."
  555. :group 'org-keywords
  556. :type 'string)
  557. (defcustom org-quote-string "QUOTE"
  558. "Entries starting with this keyword will be exported in fixed-width font.
  559. Quoting applies only to the text in the entry following the headline, and does
  560. not extend beyond the next headline, even if that is lower level.
  561. An entry can be toggled between QUOTE and normal with
  562. \\[org-toggle-fixed-width-section]."
  563. :group 'org-keywords
  564. :type 'string)
  565. (defconst org-repeat-re
  566. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)"
  567. "Regular expression for specifying repeated events.
  568. After a match, group 1 contains the repeat expression.")
  569. (defgroup org-structure nil
  570. "Options concerning the general structure of Org-mode files."
  571. :tag "Org Structure"
  572. :group 'org)
  573. (defgroup org-reveal-location nil
  574. "Options about how to make context of a location visible."
  575. :tag "Org Reveal Location"
  576. :group 'org-structure)
  577. (defconst org-context-choice
  578. '(choice
  579. (const :tag "Always" t)
  580. (const :tag "Never" nil)
  581. (repeat :greedy t :tag "Individual contexts"
  582. (cons
  583. (choice :tag "Context"
  584. (const agenda)
  585. (const org-goto)
  586. (const occur-tree)
  587. (const tags-tree)
  588. (const link-search)
  589. (const mark-goto)
  590. (const bookmark-jump)
  591. (const isearch)
  592. (const default))
  593. (boolean))))
  594. "Contexts for the reveal options.")
  595. (defcustom org-show-hierarchy-above '((default . t))
  596. "Non-nil means show full hierarchy when revealing a location.
  597. Org-mode often shows locations in an org-mode file which might have
  598. been invisible before. When this is set, the hierarchy of headings
  599. above the exposed location is shown.
  600. Turning this off for example for sparse trees makes them very compact.
  601. Instead of t, this can also be an alist specifying this option for different
  602. contexts. Valid contexts are
  603. agenda when exposing an entry from the agenda
  604. org-goto when using the command `org-goto' on key C-c C-j
  605. occur-tree when using the command `org-occur' on key C-c /
  606. tags-tree when constructing a sparse tree based on tags matches
  607. link-search when exposing search matches associated with a link
  608. mark-goto when exposing the jump goal of a mark
  609. bookmark-jump when exposing a bookmark location
  610. isearch when exiting from an incremental search
  611. default default for all contexts not set explicitly"
  612. :group 'org-reveal-location
  613. :type org-context-choice)
  614. (defcustom org-show-following-heading '((default . nil))
  615. "Non-nil means show following heading when revealing a location.
  616. Org-mode often shows locations in an org-mode file which might have
  617. been invisible before. When this is set, the heading following the
  618. match is shown.
  619. Turning this off for example for sparse trees makes them very compact,
  620. but makes it harder to edit the location of the match. In such a case,
  621. use the command \\[org-reveal] to show more context.
  622. Instead of t, this can also be an alist specifying this option for different
  623. contexts. See `org-show-hierarchy-above' for valid contexts."
  624. :group 'org-reveal-location
  625. :type org-context-choice)
  626. (defcustom org-show-siblings '((default . nil) (isearch t))
  627. "Non-nil means show all sibling heading when revealing a location.
  628. Org-mode often shows locations in an org-mode file which might have
  629. been invisible before. When this is set, the sibling of the current entry
  630. heading are all made visible. If `org-show-hierarchy-above' is t,
  631. the same happens on each level of the hierarchy above the current entry.
  632. By default this is on for the isearch context, off for all other contexts.
  633. Turning this off for example for sparse trees makes them very compact,
  634. but makes it harder to edit the location of the match. In such a case,
  635. use the command \\[org-reveal] to show more context.
  636. Instead of t, this can also be an alist specifying this option for different
  637. contexts. See `org-show-hierarchy-above' for valid contexts."
  638. :group 'org-reveal-location
  639. :type org-context-choice)
  640. (defcustom org-show-entry-below '((default . nil))
  641. "Non-nil means show the entry below a headline when revealing a location.
  642. Org-mode often shows locations in an org-mode file which might have
  643. been invisible before. When this is set, the text below the headline that is
  644. exposed is also shown.
  645. By default this is off for all contexts.
  646. Instead of t, this can also be an alist specifying this option for different
  647. contexts. See `org-show-hierarchy-above' for valid contexts."
  648. :group 'org-reveal-location
  649. :type org-context-choice)
  650. (defcustom org-indirect-buffer-display 'other-window
  651. "How should indirect tree buffers be displayed?
  652. This applies to indirect buffers created with the commands
  653. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  654. Valid values are:
  655. current-window Display in the current window
  656. other-window Just display in another window.
  657. dedicated-frame Create one new frame, and re-use it each time.
  658. new-frame Make a new frame each time. Note that in this case
  659. previously-made indirect buffers are kept, and you need to
  660. kill these buffers yourself."
  661. :group 'org-structure
  662. :group 'org-agenda-windows
  663. :type '(choice
  664. (const :tag "In current window" current-window)
  665. (const :tag "In current frame, other window" other-window)
  666. (const :tag "Each time a new frame" new-frame)
  667. (const :tag "One dedicated frame" dedicated-frame)))
  668. (defcustom org-use-speed-commands nil
  669. "Non-nil means activate single letter commands at beginning of a headline.
  670. This may also be a function to test for appropriate locations where speed
  671. commands should be active."
  672. :group 'org-structure
  673. :type '(choice
  674. (const :tag "Never" nil)
  675. (const :tag "At beginning of headline stars" t)
  676. (function)))
  677. (defcustom org-speed-commands-user nil
  678. "Alist of additional speed commands.
  679. This list will be checked before `org-speed-commands-default'
  680. when the variable `org-use-speed-commands' is non-nil
  681. and when the cursor is at the beginning of a headline.
  682. The car if each entry is a string with a single letter, which must
  683. be assigned to `self-insert-command' in the global map.
  684. The cdr is either a command to be called interactively, a function
  685. to be called, or a form to be evaluated.
  686. An entry that is just a list with a single string will be interpreted
  687. as a descriptive headline that will be added when listing the speed
  688. commands in the Help buffer using the `?' speed command."
  689. :group 'org-structure
  690. :type '(repeat :value ("k" . ignore)
  691. (choice :value ("k" . ignore)
  692. (list :tag "Descriptive Headline" (string :tag "Headline"))
  693. (cons :tag "Letter and Command"
  694. (string :tag "Command letter")
  695. (choice
  696. (function)
  697. (sexp))))))
  698. (defgroup org-cycle nil
  699. "Options concerning visibility cycling in Org-mode."
  700. :tag "Org Cycle"
  701. :group 'org-structure)
  702. (defcustom org-cycle-skip-children-state-if-no-children t
  703. "Non-nil means skip CHILDREN state in entries that don't have any."
  704. :group 'org-cycle
  705. :type 'boolean)
  706. (defcustom org-cycle-max-level nil
  707. "Maximum level which should still be subject to visibility cycling.
  708. Levels higher than this will, for cycling, be treated as text, not a headline.
  709. When `org-odd-levels-only' is set, a value of N in this variable actually
  710. means 2N-1 stars as the limiting headline.
  711. When nil, cycle all levels.
  712. Note that the limiting level of cycling is also influenced by
  713. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  714. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  715. than its value."
  716. :group 'org-cycle
  717. :type '(choice
  718. (const :tag "No limit" nil)
  719. (integer :tag "Maximum level")))
  720. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  721. "Names of drawers. Drawers are not opened by cycling on the headline above.
  722. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  723. this:
  724. :DRAWERNAME:
  725. .....
  726. :END:
  727. The drawer \"PROPERTIES\" is special for capturing properties through
  728. the property API.
  729. Drawers can be defined on the per-file basis with a line like:
  730. #+DRAWERS: HIDDEN STATE PROPERTIES"
  731. :group 'org-structure
  732. :group 'org-cycle
  733. :type '(repeat (string :tag "Drawer Name")))
  734. (defcustom org-hide-block-startup nil
  735. "Non-nil means entering Org-mode will fold all blocks.
  736. This can also be set in on a per-file basis with
  737. #+STARTUP: hideblocks
  738. #+STARTUP: showblocks"
  739. :group 'org-startup
  740. :group 'org-cycle
  741. :type 'boolean)
  742. (defcustom org-cycle-global-at-bob nil
  743. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  744. This makes it possible to do global cycling without having to use S-TAB or
  745. \\[universal-argument] TAB. For this special case to work, the first line \
  746. of the buffer
  747. must not be a headline - it may be empty or some other text. When used in
  748. this way, `org-cycle-hook' is disables temporarily, to make sure the
  749. cursor stays at the beginning of the buffer.
  750. When this option is nil, don't do anything special at the beginning
  751. of the buffer."
  752. :group 'org-cycle
  753. :type 'boolean)
  754. (defcustom org-cycle-level-after-item/entry-creation t
  755. "Non-nil means cycle entry level or item indentation in new empty entries.
  756. When the cursor is at the end of an empty headline, i.e with only stars
  757. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  758. and then all possible ancestor states, before returning to the original state.
  759. This makes data entry extremely fast: M-RET to create a new headline,
  760. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  761. When the cursor is at the end of an empty plain list item, one TAB will
  762. make it a subitem, two or more tabs will back up to make this an item
  763. higher up in the item hierarchy."
  764. :group 'org-cycle
  765. :type 'boolean)
  766. (defcustom org-cycle-emulate-tab t
  767. "Where should `org-cycle' emulate TAB.
  768. nil Never
  769. white Only in completely white lines
  770. whitestart Only at the beginning of lines, before the first non-white char
  771. t Everywhere except in headlines
  772. exc-hl-bol Everywhere except at the start of a headline
  773. If TAB is used in a place where it does not emulate TAB, the current subtree
  774. visibility is cycled."
  775. :group 'org-cycle
  776. :type '(choice (const :tag "Never" nil)
  777. (const :tag "Only in completely white lines" white)
  778. (const :tag "Before first char in a line" whitestart)
  779. (const :tag "Everywhere except in headlines" t)
  780. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  781. ))
  782. (defcustom org-cycle-separator-lines 2
  783. "Number of empty lines needed to keep an empty line between collapsed trees.
  784. If you leave an empty line between the end of a subtree and the following
  785. headline, this empty line is hidden when the subtree is folded.
  786. Org-mode will leave (exactly) one empty line visible if the number of
  787. empty lines is equal or larger to the number given in this variable.
  788. So the default 2 means at least 2 empty lines after the end of a subtree
  789. are needed to produce free space between a collapsed subtree and the
  790. following headline.
  791. If the number is negative, and the number of empty lines is at least -N,
  792. all empty lines are shown.
  793. Special case: when 0, never leave empty lines in collapsed view."
  794. :group 'org-cycle
  795. :type 'integer)
  796. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  797. (defcustom org-pre-cycle-hook nil
  798. "Hook that is run before visibility cycling is happening.
  799. The function(s) in this hook must accept a single argument which indicates
  800. the new state that will be set right after running this hook. The
  801. argument is a symbol. Before a global state change, it can have the values
  802. `overview', `content', or `all'. Before a local state change, it can have
  803. the values `folded', `children', or `subtree'."
  804. :group 'org-cycle
  805. :type 'hook)
  806. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  807. org-cycle-hide-drawers
  808. org-cycle-show-empty-lines
  809. org-optimize-window-after-visibility-change)
  810. "Hook that is run after `org-cycle' has changed the buffer visibility.
  811. The function(s) in this hook must accept a single argument which indicates
  812. the new state that was set by the most recent `org-cycle' command. The
  813. argument is a symbol. After a global state change, it can have the values
  814. `overview', `content', or `all'. After a local state change, it can have
  815. the values `folded', `children', or `subtree'."
  816. :group 'org-cycle
  817. :type 'hook)
  818. (defgroup org-edit-structure nil
  819. "Options concerning structure editing in Org-mode."
  820. :tag "Org Edit Structure"
  821. :group 'org-structure)
  822. (defcustom org-odd-levels-only nil
  823. "Non-nil means skip even levels and only use odd levels for the outline.
  824. This has the effect that two stars are being added/taken away in
  825. promotion/demotion commands. It also influences how levels are
  826. handled by the exporters.
  827. Changing it requires restart of `font-lock-mode' to become effective
  828. for fontification also in regions already fontified.
  829. You may also set this on a per-file basis by adding one of the following
  830. lines to the buffer:
  831. #+STARTUP: odd
  832. #+STARTUP: oddeven"
  833. :group 'org-edit-structure
  834. :group 'org-appearance
  835. :type 'boolean)
  836. (defcustom org-adapt-indentation t
  837. "Non-nil means adapt indentation to outline node level.
  838. When this variable is set, Org assumes that you write outlines by
  839. indenting text in each node to align with the headline (after the stars).
  840. The following issues are influenced by this variable:
  841. - When this is set and the *entire* text in an entry is indented, the
  842. indentation is increased by one space in a demotion command, and
  843. decreased by one in a promotion command. If any line in the entry
  844. body starts with text at column 0, indentation is not changed at all.
  845. - Property drawers and planning information is inserted indented when
  846. this variable s set. When nil, they will not be indented.
  847. - TAB indents a line relative to context. The lines below a headline
  848. will be indented when this variable is set.
  849. Note that this is all about true indentation, by adding and removing
  850. space characters. See also `org-indent.el' which does level-dependent
  851. indentation in a virtual way, i.e. at display time in Emacs."
  852. :group 'org-edit-structure
  853. :type 'boolean)
  854. (defcustom org-special-ctrl-a/e nil
  855. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  856. When t, `C-a' will bring back the cursor to the beginning of the
  857. headline text, i.e. after the stars and after a possible TODO keyword.
  858. In an item, this will be the position after the bullet.
  859. When the cursor is already at that position, another `C-a' will bring
  860. it to the beginning of the line.
  861. `C-e' will jump to the end of the headline, ignoring the presence of tags
  862. in the headline. A second `C-e' will then jump to the true end of the
  863. line, after any tags. This also means that, when this variable is
  864. non-nil, `C-e' also will never jump beyond the end of the heading of a
  865. folded section, i.e. not after the ellipses.
  866. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  867. going to the true line boundary first. Only a directly following, identical
  868. keypress will bring the cursor to the special positions.
  869. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  870. set separately."
  871. :group 'org-edit-structure
  872. :type '(choice
  873. (const :tag "off" nil)
  874. (const :tag "on: after stars/bullet and before tags first" t)
  875. (const :tag "reversed: true line boundary first" reversed)
  876. (cons :tag "Set C-a and C-e separately"
  877. (choice :tag "Special C-a"
  878. (const :tag "off" nil)
  879. (const :tag "on: after stars/bullet first" t)
  880. (const :tag "reversed: before stars/bullet first" reversed))
  881. (choice :tag "Special C-e"
  882. (const :tag "off" nil)
  883. (const :tag "on: before tags first" t)
  884. (const :tag "reversed: after tags first" reversed)))))
  885. (if (fboundp 'defvaralias)
  886. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  887. (defcustom org-special-ctrl-k nil
  888. "Non-nil means `C-k' will behave specially in headlines.
  889. When nil, `C-k' will call the default `kill-line' command.
  890. When t, the following will happen while the cursor is in the headline:
  891. - When the cursor is at the beginning of a headline, kill the entire
  892. line and possible the folded subtree below the line.
  893. - When in the middle of the headline text, kill the headline up to the tags.
  894. - When after the headline text, kill the tags."
  895. :group 'org-edit-structure
  896. :type 'boolean)
  897. (defcustom org-ctrl-k-protect-subtree nil
  898. "Non-nil means, do not delete a hidden subtree with C-k.
  899. When set to the symbol `error', simply throw an error when C-k is
  900. used to kill (part-of) a headline that has hidden text behind it.
  901. Any other non-nil value will result in a query to the user, if it is
  902. OK to kill that hidden subtree. When nil, kill without remorse."
  903. :group 'org-edit-structure
  904. :type '(choice
  905. (const :tag "Do not protect hidden subtrees" nil)
  906. (const :tag "Protect hidden subtrees with a security query" t)
  907. (const :tag "Never kill a hidden subtree with C-k" error)))
  908. (defcustom org-yank-folded-subtrees t
  909. "Non-nil means when yanking subtrees, fold them.
  910. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  911. it starts with a heading and all other headings in it are either children
  912. or siblings, then fold all the subtrees. However, do this only if no
  913. text after the yank would be swallowed into a folded tree by this action."
  914. :group 'org-edit-structure
  915. :type 'boolean)
  916. (defcustom org-yank-adjusted-subtrees nil
  917. "Non-nil means when yanking subtrees, adjust the level.
  918. With this setting, `org-paste-subtree' is used to insert the subtree, see
  919. this function for details."
  920. :group 'org-edit-structure
  921. :type 'boolean)
  922. (defcustom org-M-RET-may-split-line '((default . t))
  923. "Non-nil means M-RET will split the line at the cursor position.
  924. When nil, it will go to the end of the line before making a
  925. new line.
  926. You may also set this option in a different way for different
  927. contexts. Valid contexts are:
  928. headline when creating a new headline
  929. item when creating a new item
  930. table in a table field
  931. default the value to be used for all contexts not explicitly
  932. customized"
  933. :group 'org-structure
  934. :group 'org-table
  935. :type '(choice
  936. (const :tag "Always" t)
  937. (const :tag "Never" nil)
  938. (repeat :greedy t :tag "Individual contexts"
  939. (cons
  940. (choice :tag "Context"
  941. (const headline)
  942. (const item)
  943. (const table)
  944. (const default))
  945. (boolean)))))
  946. (defcustom org-insert-heading-respect-content nil
  947. "Non-nil means insert new headings after the current subtree.
  948. When nil, the new heading is created directly after the current line.
  949. The commands \\[org-insert-heading-respect-content] and
  950. \\[org-insert-todo-heading-respect-content] turn this variable on
  951. for the duration of the command."
  952. :group 'org-structure
  953. :type 'boolean)
  954. (defcustom org-blank-before-new-entry '((heading . auto)
  955. (plain-list-item . auto))
  956. "Should `org-insert-heading' leave a blank line before new heading/item?
  957. The value is an alist, with `heading' and `plain-list-item' as car,
  958. and a boolean flag as cdr. The cdr may also be the symbol `auto', and then
  959. Org will look at the surrounding headings/items and try to make an
  960. intelligent decision whether to insert a blank line or not.
  961. For plain lists, if the variable `org-empty-line-terminates-plain-lists' is
  962. set, the setting here is ignored and no empty line is inserted, to avoid
  963. breaking the list structure."
  964. :group 'org-edit-structure
  965. :type '(list
  966. (cons (const heading)
  967. (choice (const :tag "Never" nil)
  968. (const :tag "Always" t)
  969. (const :tag "Auto" auto)))
  970. (cons (const plain-list-item)
  971. (choice (const :tag "Never" nil)
  972. (const :tag "Always" t)
  973. (const :tag "Auto" auto)))))
  974. (defcustom org-insert-heading-hook nil
  975. "Hook being run after inserting a new heading."
  976. :group 'org-edit-structure
  977. :type 'hook)
  978. (defcustom org-enable-fixed-width-editor t
  979. "Non-nil means lines starting with \":\" are treated as fixed-width.
  980. This currently only means they are never auto-wrapped.
  981. When nil, such lines will be treated like ordinary lines.
  982. See also the QUOTE keyword."
  983. :group 'org-edit-structure
  984. :type 'boolean)
  985. (defcustom org-goto-auto-isearch t
  986. "Non-nil means typing characters in `org-goto' starts incremental search."
  987. :group 'org-edit-structure
  988. :type 'boolean)
  989. (defgroup org-sparse-trees nil
  990. "Options concerning sparse trees in Org-mode."
  991. :tag "Org Sparse Trees"
  992. :group 'org-structure)
  993. (defcustom org-highlight-sparse-tree-matches t
  994. "Non-nil means highlight all matches that define a sparse tree.
  995. The highlights will automatically disappear the next time the buffer is
  996. changed by an edit command."
  997. :group 'org-sparse-trees
  998. :type 'boolean)
  999. (defcustom org-remove-highlights-with-change t
  1000. "Non-nil means any change to the buffer will remove temporary highlights.
  1001. Such highlights are created by `org-occur' and `org-clock-display'.
  1002. When nil, `C-c C-c needs to be used to get rid of the highlights.
  1003. The highlights created by `org-preview-latex-fragment' always need
  1004. `C-c C-c' to be removed."
  1005. :group 'org-sparse-trees
  1006. :group 'org-time
  1007. :type 'boolean)
  1008. (defcustom org-occur-hook '(org-first-headline-recenter)
  1009. "Hook that is run after `org-occur' has constructed a sparse tree.
  1010. This can be used to recenter the window to show as much of the structure
  1011. as possible."
  1012. :group 'org-sparse-trees
  1013. :type 'hook)
  1014. (defgroup org-imenu-and-speedbar nil
  1015. "Options concerning imenu and speedbar in Org-mode."
  1016. :tag "Org Imenu and Speedbar"
  1017. :group 'org-structure)
  1018. (defcustom org-imenu-depth 2
  1019. "The maximum level for Imenu access to Org-mode headlines.
  1020. This also applied for speedbar access."
  1021. :group 'org-imenu-and-speedbar
  1022. :type 'integer)
  1023. (defgroup org-table nil
  1024. "Options concerning tables in Org-mode."
  1025. :tag "Org Table"
  1026. :group 'org)
  1027. (defcustom org-enable-table-editor 'optimized
  1028. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1029. When nil, such lines will be treated like ordinary lines.
  1030. When equal to the symbol `optimized', the table editor will be optimized to
  1031. do the following:
  1032. - Automatic overwrite mode in front of whitespace in table fields.
  1033. This makes the structure of the table stay in tact as long as the edited
  1034. field does not exceed the column width.
  1035. - Minimize the number of realigns. Normally, the table is aligned each time
  1036. TAB or RET are pressed to move to another field. With optimization this
  1037. happens only if changes to a field might have changed the column width.
  1038. Optimization requires replacing the functions `self-insert-command',
  1039. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1040. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1041. very good at guessing when a re-align will be necessary, but you can always
  1042. force one with \\[org-ctrl-c-ctrl-c].
  1043. If you would like to use the optimized version in Org-mode, but the
  1044. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1045. This variable can be used to turn on and off the table editor during a session,
  1046. but in order to toggle optimization, a restart is required.
  1047. See also the variable `org-table-auto-blank-field'."
  1048. :group 'org-table
  1049. :type '(choice
  1050. (const :tag "off" nil)
  1051. (const :tag "on" t)
  1052. (const :tag "on, optimized" optimized)))
  1053. (defcustom org-self-insert-cluster-for-undo t
  1054. "Non-nil means cluster self-insert commands for undo when possible.
  1055. If this is set, then, like in the Emacs command loop, 20 consecutive
  1056. characters will be undone together.
  1057. This is configurable, because there is some impact on typing performance."
  1058. :group 'org-table
  1059. :type 'boolean)
  1060. (defcustom org-table-tab-recognizes-table.el t
  1061. "Non-nil means TAB will automatically notice a table.el table.
  1062. When it sees such a table, it moves point into it and - if necessary -
  1063. calls `table-recognize-table'."
  1064. :group 'org-table-editing
  1065. :type 'boolean)
  1066. (defgroup org-link nil
  1067. "Options concerning links in Org-mode."
  1068. :tag "Org Link"
  1069. :group 'org)
  1070. (defvar org-link-abbrev-alist-local nil
  1071. "Buffer-local version of `org-link-abbrev-alist', which see.
  1072. The value of this is taken from the #+LINK lines.")
  1073. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1074. (defcustom org-link-abbrev-alist nil
  1075. "Alist of link abbreviations.
  1076. The car of each element is a string, to be replaced at the start of a link.
  1077. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1078. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1079. [[linkkey:tag][description]]
  1080. The 'linkkey' must be a word word, starting with a letter, followed
  1081. by letters, numbers, '-' or '_'.
  1082. If REPLACE is a string, the tag will simply be appended to create the link.
  1083. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  1084. the placeholder \"%h\" will cause a url-encoded version of the tag to
  1085. be inserted at that point (see the function `url-hexify-string').
  1086. REPLACE may also be a function that will be called with the tag as the
  1087. only argument to create the link, which should be returned as a string.
  1088. See the manual for examples."
  1089. :group 'org-link
  1090. :type '(repeat
  1091. (cons
  1092. (string :tag "Protocol")
  1093. (choice
  1094. (string :tag "Format")
  1095. (function)))))
  1096. (defcustom org-descriptive-links t
  1097. "Non-nil means hide link part and only show description of bracket links.
  1098. Bracket links are like [[link][description]]. This variable sets the initial
  1099. state in new org-mode buffers. The setting can then be toggled on a
  1100. per-buffer basis from the Org->Hyperlinks menu."
  1101. :group 'org-link
  1102. :type 'boolean)
  1103. (defcustom org-link-file-path-type 'adaptive
  1104. "How the path name in file links should be stored.
  1105. Valid values are:
  1106. relative Relative to the current directory, i.e. the directory of the file
  1107. into which the link is being inserted.
  1108. absolute Absolute path, if possible with ~ for home directory.
  1109. noabbrev Absolute path, no abbreviation of home directory.
  1110. adaptive Use relative path for files in the current directory and sub-
  1111. directories of it. For other files, use an absolute path."
  1112. :group 'org-link
  1113. :type '(choice
  1114. (const relative)
  1115. (const absolute)
  1116. (const noabbrev)
  1117. (const adaptive)))
  1118. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  1119. "Types of links that should be activated in Org-mode files.
  1120. This is a list of symbols, each leading to the activation of a certain link
  1121. type. In principle, it does not hurt to turn on most link types - there may
  1122. be a small gain when turning off unused link types. The types are:
  1123. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1124. angle Links in angular brackets that may contain whitespace like
  1125. <bbdb:Carsten Dominik>.
  1126. plain Plain links in normal text, no whitespace, like http://google.com.
  1127. radio Text that is matched by a radio target, see manual for details.
  1128. tag Tag settings in a headline (link to tag search).
  1129. date Time stamps (link to calendar).
  1130. footnote Footnote labels.
  1131. Changing this variable requires a restart of Emacs to become effective."
  1132. :group 'org-link
  1133. :type '(set :greedy t
  1134. (const :tag "Double bracket links" bracket)
  1135. (const :tag "Angular bracket links" angle)
  1136. (const :tag "Plain text links" plain)
  1137. (const :tag "Radio target matches" radio)
  1138. (const :tag "Tags" tag)
  1139. (const :tag "Timestamps" date)
  1140. (const :tag "Footnotes" footnote)))
  1141. (defcustom org-make-link-description-function nil
  1142. "Function to use to generate link descriptions from links.
  1143. If nil the link location will be used. This function must take
  1144. two parameters; the first is the link and the second the
  1145. description `org-insert-link' has generated, and should return the
  1146. description to use."
  1147. :group 'org-link
  1148. :type 'function)
  1149. (defgroup org-link-store nil
  1150. "Options concerning storing links in Org-mode."
  1151. :tag "Org Store Link"
  1152. :group 'org-link)
  1153. (defcustom org-email-link-description-format "Email %c: %.30s"
  1154. "Format of the description part of a link to an email or usenet message.
  1155. The following %-escapes will be replaced by corresponding information:
  1156. %F full \"From\" field
  1157. %f name, taken from \"From\" field, address if no name
  1158. %T full \"To\" field
  1159. %t first name in \"To\" field, address if no name
  1160. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1161. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1162. %s subject
  1163. %d date
  1164. %m message-id.
  1165. You may use normal field width specification between the % and the letter.
  1166. This is for example useful to limit the length of the subject.
  1167. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1168. :group 'org-link-store
  1169. :type 'string)
  1170. (defcustom org-from-is-user-regexp
  1171. (let (r1 r2)
  1172. (when (and user-mail-address (not (string= user-mail-address "")))
  1173. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1174. (when (and user-full-name (not (string= user-full-name "")))
  1175. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1176. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1177. "Regexp matched against the \"From:\" header of an email or usenet message.
  1178. It should match if the message is from the user him/herself."
  1179. :group 'org-link-store
  1180. :type 'regexp)
  1181. (defcustom org-link-to-org-use-id 'create-if-interactive-and-no-custom-id
  1182. "Non-nil means storing a link to an Org file will use entry IDs.
  1183. Note that before this variable is even considered, org-id must be loaded,
  1184. so please customize `org-modules' and turn it on.
  1185. The variable can have the following values:
  1186. t Create an ID if needed to make a link to the current entry.
  1187. create-if-interactive
  1188. If `org-store-link' is called directly (interactively, as a user
  1189. command), do create an ID to support the link. But when doing the
  1190. job for remember, only use the ID if it already exists. The
  1191. purpose of this setting is to avoid proliferation of unwanted
  1192. IDs, just because you happen to be in an Org file when you
  1193. call `org-remember' that automatically and preemptively
  1194. creates a link. If you do want to get an ID link in a remember
  1195. template to an entry not having an ID, create it first by
  1196. explicitly creating a link to it, using `C-c C-l' first.
  1197. create-if-interactive-and-no-custom-id
  1198. Like create-if-interactive, but do not create an ID if there is
  1199. a CUSTOM_ID property defined in the entry. This is the default.
  1200. use-existing
  1201. Use existing ID, do not create one.
  1202. nil Never use an ID to make a link, instead link using a text search for
  1203. the headline text."
  1204. :group 'org-link-store
  1205. :type '(choice
  1206. (const :tag "Create ID to make link" t)
  1207. (const :tag "Create if storing link interactively"
  1208. create-if-interactive)
  1209. (const :tag "Create if storing link interactively and no CUSTOM_ID is present"
  1210. create-if-interactive-and-no-custom-id)
  1211. (const :tag "Only use existing" use-existing)
  1212. (const :tag "Do not use ID to create link" nil)))
  1213. (defcustom org-context-in-file-links t
  1214. "Non-nil means file links from `org-store-link' contain context.
  1215. A search string will be added to the file name with :: as separator and
  1216. used to find the context when the link is activated by the command
  1217. `org-open-at-point'. When this option is t, the entire active region
  1218. will be placed in the search string of the file link. If set to a
  1219. positive integer, only the first n lines of context will be stored.
  1220. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1221. negates this setting for the duration of the command."
  1222. :group 'org-link-store
  1223. :type '(choice boolean integer))
  1224. (defcustom org-keep-stored-link-after-insertion nil
  1225. "Non-nil means keep link in list for entire session.
  1226. The command `org-store-link' adds a link pointing to the current
  1227. location to an internal list. These links accumulate during a session.
  1228. The command `org-insert-link' can be used to insert links into any
  1229. Org-mode file (offering completion for all stored links). When this
  1230. option is nil, every link which has been inserted once using \\[org-insert-link]
  1231. will be removed from the list, to make completing the unused links
  1232. more efficient."
  1233. :group 'org-link-store
  1234. :type 'boolean)
  1235. (defgroup org-link-follow nil
  1236. "Options concerning following links in Org-mode."
  1237. :tag "Org Follow Link"
  1238. :group 'org-link)
  1239. (defcustom org-link-translation-function nil
  1240. "Function to translate links with different syntax to Org syntax.
  1241. This can be used to translate links created for example by the Planner
  1242. or emacs-wiki packages to Org syntax.
  1243. The function must accept two parameters, a TYPE containing the link
  1244. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1245. which is everything after the link protocol. It should return a cons
  1246. with possibly modified values of type and path.
  1247. Org contains a function for this, so if you set this variable to
  1248. `org-translate-link-from-planner', you should be able follow many
  1249. links created by planner."
  1250. :group 'org-link-follow
  1251. :type 'function)
  1252. (defcustom org-follow-link-hook nil
  1253. "Hook that is run after a link has been followed."
  1254. :group 'org-link-follow
  1255. :type 'hook)
  1256. (defcustom org-tab-follows-link nil
  1257. "Non-nil means on links TAB will follow the link.
  1258. Needs to be set before org.el is loaded.
  1259. This really should not be used, it does not make sense, and the
  1260. implementation is bad."
  1261. :group 'org-link-follow
  1262. :type 'boolean)
  1263. (defcustom org-return-follows-link nil
  1264. "Non-nil means on links RET will follow the link."
  1265. :group 'org-link-follow
  1266. :type 'boolean)
  1267. (defcustom org-mouse-1-follows-link
  1268. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1269. "Non-nil means mouse-1 on a link will follow the link.
  1270. A longer mouse click will still set point. Does not work on XEmacs.
  1271. Needs to be set before org.el is loaded."
  1272. :group 'org-link-follow
  1273. :type 'boolean)
  1274. (defcustom org-mark-ring-length 4
  1275. "Number of different positions to be recorded in the ring.
  1276. Changing this requires a restart of Emacs to work correctly."
  1277. :group 'org-link-follow
  1278. :type 'integer)
  1279. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1280. "Non-nil means internal links in Org files must exactly match a headline.
  1281. When nil, the link search tries to match a phrase with all words
  1282. in the search text."
  1283. :group 'org-link-follow
  1284. :type '(choice
  1285. (const :tag "Use fuzy text search" nil)
  1286. (const :tag "Match only exact headline" t)
  1287. (const :tag "Match extact headline or query to create it"
  1288. query-to-create)))
  1289. (defcustom org-link-frame-setup
  1290. '((vm . vm-visit-folder-other-frame)
  1291. (gnus . org-gnus-no-new-news)
  1292. (file . find-file-other-window)
  1293. (wl . wl-other-frame))
  1294. "Setup the frame configuration for following links.
  1295. When following a link with Emacs, it may often be useful to display
  1296. this link in another window or frame. This variable can be used to
  1297. set this up for the different types of links.
  1298. For VM, use any of
  1299. `vm-visit-folder'
  1300. `vm-visit-folder-other-window'
  1301. `vm-visit-folder-other-frame'
  1302. For Gnus, use any of
  1303. `gnus'
  1304. `gnus-other-frame'
  1305. `org-gnus-no-new-news'
  1306. For FILE, use any of
  1307. `find-file'
  1308. `find-file-other-window'
  1309. `find-file-other-frame'
  1310. For Wanderlust use any of
  1311. `wl'
  1312. `wl-other-frame'
  1313. For the calendar, use the variable `calendar-setup'.
  1314. For BBDB, it is currently only possible to display the matches in
  1315. another window."
  1316. :group 'org-link-follow
  1317. :type '(list
  1318. (cons (const vm)
  1319. (choice
  1320. (const vm-visit-folder)
  1321. (const vm-visit-folder-other-window)
  1322. (const vm-visit-folder-other-frame)))
  1323. (cons (const gnus)
  1324. (choice
  1325. (const gnus)
  1326. (const gnus-other-frame)
  1327. (const org-gnus-no-new-news)))
  1328. (cons (const file)
  1329. (choice
  1330. (const find-file)
  1331. (const find-file-other-window)
  1332. (const find-file-other-frame)))
  1333. (cons (const wl)
  1334. (choice
  1335. (const wl)
  1336. (const wl-other-frame)))))
  1337. (defcustom org-display-internal-link-with-indirect-buffer nil
  1338. "Non-nil means use indirect buffer to display infile links.
  1339. Activating internal links (from one location in a file to another location
  1340. in the same file) normally just jumps to the location. When the link is
  1341. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1342. is displayed in
  1343. another window. When this option is set, the other window actually displays
  1344. an indirect buffer clone of the current buffer, to avoid any visibility
  1345. changes to the current buffer."
  1346. :group 'org-link-follow
  1347. :type 'boolean)
  1348. (defcustom org-open-non-existing-files nil
  1349. "Non-nil means `org-open-file' will open non-existing files.
  1350. When nil, an error will be generated.
  1351. This variable applies only to external applications because they
  1352. might choke on non-existing files. If the link is to a file that
  1353. will be opened in Emacs, the variable is ignored."
  1354. :group 'org-link-follow
  1355. :type 'boolean)
  1356. (defcustom org-open-directory-means-index-dot-org nil
  1357. "Non-nil means a link to a directory really means to index.org.
  1358. When nil, following a directory link will run dired or open a finder/explorer
  1359. window on that directory."
  1360. :group 'org-link-follow
  1361. :type 'boolean)
  1362. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1363. "Function and arguments to call for following mailto links.
  1364. This is a list with the first element being a Lisp function, and the
  1365. remaining elements being arguments to the function. In string arguments,
  1366. %a will be replaced by the address, and %s will be replaced by the subject
  1367. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1368. :group 'org-link-follow
  1369. :type '(choice
  1370. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1371. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1372. (const :tag "message-mail" (message-mail "%a" "%s"))
  1373. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1374. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1375. "Non-nil means ask for confirmation before executing shell links.
  1376. Shell links can be dangerous: just think about a link
  1377. [[shell:rm -rf ~/*][Google Search]]
  1378. This link would show up in your Org-mode document as \"Google Search\",
  1379. but really it would remove your entire home directory.
  1380. Therefore we advise against setting this variable to nil.
  1381. Just change it to `y-or-n-p' if you want to confirm with a
  1382. single keystroke rather than having to type \"yes\"."
  1383. :group 'org-link-follow
  1384. :type '(choice
  1385. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1386. (const :tag "with y-or-n (faster)" y-or-n-p)
  1387. (const :tag "no confirmation (dangerous)" nil)))
  1388. (put 'org-confirm-shell-link-function
  1389. 'safe-local-variable
  1390. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1391. (defcustom org-confirm-shell-link-not-regexp ""
  1392. "A regexp to skip confirmation for shell links."
  1393. :group 'org-link-follow
  1394. :type 'regexp)
  1395. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1396. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1397. Elisp links can be dangerous: just think about a link
  1398. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1399. This link would show up in your Org-mode document as \"Google Search\",
  1400. but really it would remove your entire home directory.
  1401. Therefore we advise against setting this variable to nil.
  1402. Just change it to `y-or-n-p' if you want to confirm with a
  1403. single keystroke rather than having to type \"yes\"."
  1404. :group 'org-link-follow
  1405. :type '(choice
  1406. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1407. (const :tag "with y-or-n (faster)" y-or-n-p)
  1408. (const :tag "no confirmation (dangerous)" nil)))
  1409. (put 'org-confirm-shell-link-function
  1410. 'safe-local-variable
  1411. '(lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1412. (defcustom org-confirm-elisp-link-not-regexp ""
  1413. "A regexp to skip confirmation for Elisp links."
  1414. :group 'org-link-follow
  1415. :type 'regexp)
  1416. (defconst org-file-apps-defaults-gnu
  1417. '((remote . emacs)
  1418. (system . mailcap)
  1419. (t . mailcap))
  1420. "Default file applications on a UNIX or GNU/Linux system.
  1421. See `org-file-apps'.")
  1422. (defconst org-file-apps-defaults-macosx
  1423. '((remote . emacs)
  1424. (t . "open %s")
  1425. (system . "open %s")
  1426. ("ps.gz" . "gv %s")
  1427. ("eps.gz" . "gv %s")
  1428. ("dvi" . "xdvi %s")
  1429. ("fig" . "xfig %s"))
  1430. "Default file applications on a MacOS X system.
  1431. The system \"open\" is known as a default, but we use X11 applications
  1432. for some files for which the OS does not have a good default.
  1433. See `org-file-apps'.")
  1434. (defconst org-file-apps-defaults-windowsnt
  1435. (list
  1436. '(remote . emacs)
  1437. (cons t
  1438. (list (if (featurep 'xemacs)
  1439. 'mswindows-shell-execute
  1440. 'w32-shell-execute)
  1441. "open" 'file))
  1442. (cons 'system
  1443. (list (if (featurep 'xemacs)
  1444. 'mswindows-shell-execute
  1445. 'w32-shell-execute)
  1446. "open" 'file)))
  1447. "Default file applications on a Windows NT system.
  1448. The system \"open\" is used for most files.
  1449. See `org-file-apps'.")
  1450. (defcustom org-file-apps
  1451. '(
  1452. (auto-mode . emacs)
  1453. ("\\.mm\\'" . default)
  1454. ("\\.x?html?\\'" . default)
  1455. ("\\.pdf\\'" . default)
  1456. )
  1457. "External applications for opening `file:path' items in a document.
  1458. Org-mode uses system defaults for different file types, but
  1459. you can use this variable to set the application for a given file
  1460. extension. The entries in this list are cons cells where the car identifies
  1461. files and the cdr the corresponding command. Possible values for the
  1462. file identifier are
  1463. \"string\" A string as a file identifier can be interpreted in different
  1464. ways, depending on its contents:
  1465. - Alphanumeric characters only:
  1466. Match links with this file extension.
  1467. Example: (\"pdf\" . \"evince %s\")
  1468. to open PDFs with evince.
  1469. - Regular expression: Match links where the
  1470. filename matches the regexp. If you want to
  1471. use groups here, use shy groups.
  1472. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1473. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1474. to open *.html and *.xhtml with firefox.
  1475. - Regular expression which contains (non-shy) groups:
  1476. Match links where the whole link, including \"::\", and
  1477. anything after that, matches the regexp.
  1478. In a custom command string, %1, %2, etc. are replaced with
  1479. the parts of the link that were matched by the groups.
  1480. For backwards compatibility, if a command string is given
  1481. that does not use any of the group matches, this case is
  1482. handled identically to the second one (i.e. match against
  1483. file name only).
  1484. In a custom lisp form, you can access the group matches with
  1485. (match-string n link).
  1486. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1487. to open [[file:document.pdf::5]] with evince at page 5.
  1488. `directory' Matches a directory
  1489. `remote' Matches a remote file, accessible through tramp or efs.
  1490. Remote files most likely should be visited through Emacs
  1491. because external applications cannot handle such paths.
  1492. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1493. so all files Emacs knows how to handle. Using this with
  1494. command `emacs' will open most files in Emacs. Beware that this
  1495. will also open html files inside Emacs, unless you add
  1496. (\"html\" . default) to the list as well.
  1497. t Default for files not matched by any of the other options.
  1498. `system' The system command to open files, like `open' on Windows
  1499. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1500. that will be selected if you call `C-c C-o' with a double
  1501. \\[universal-argument] \\[universal-argument] prefix.
  1502. Possible values for the command are:
  1503. `emacs' The file will be visited by the current Emacs process.
  1504. `default' Use the default application for this file type, which is the
  1505. association for t in the list, most likely in the system-specific
  1506. part.
  1507. This can be used to overrule an unwanted setting in the
  1508. system-specific variable.
  1509. `system' Use the system command for opening files, like \"open\".
  1510. This command is specified by the entry whose car is `system'.
  1511. Most likely, the system-specific version of this variable
  1512. does define this command, but you can overrule/replace it
  1513. here.
  1514. string A command to be executed by a shell; %s will be replaced
  1515. by the path to the file.
  1516. sexp A Lisp form which will be evaluated. The file path will
  1517. be available in the Lisp variable `file'.
  1518. For more examples, see the system specific constants
  1519. `org-file-apps-defaults-macosx'
  1520. `org-file-apps-defaults-windowsnt'
  1521. `org-file-apps-defaults-gnu'."
  1522. :group 'org-link-follow
  1523. :type '(repeat
  1524. (cons (choice :value ""
  1525. (string :tag "Extension")
  1526. (const :tag "System command to open files" system)
  1527. (const :tag "Default for unrecognized files" t)
  1528. (const :tag "Remote file" remote)
  1529. (const :tag "Links to a directory" directory)
  1530. (const :tag "Any files that have Emacs modes"
  1531. auto-mode))
  1532. (choice :value ""
  1533. (const :tag "Visit with Emacs" emacs)
  1534. (const :tag "Use default" default)
  1535. (const :tag "Use the system command" system)
  1536. (string :tag "Command")
  1537. (sexp :tag "Lisp form")))))
  1538. (defgroup org-refile nil
  1539. "Options concerning refiling entries in Org-mode."
  1540. :tag "Org Refile"
  1541. :group 'org)
  1542. (defcustom org-directory "~/org"
  1543. "Directory with org files.
  1544. This is just a default location to look for Org files. There is no need
  1545. at all to put your files into this directory. It is only used in the
  1546. following situations:
  1547. 1. When a remember template specifies a target file that is not an
  1548. absolute path. The path will then be interpreted relative to
  1549. `org-directory'
  1550. 2. When a remember note is filed away in an interactive way (when exiting the
  1551. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1552. with `org-directory' as the default path."
  1553. :group 'org-refile
  1554. :group 'org-remember
  1555. :type 'directory)
  1556. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1557. "Default target for storing notes.
  1558. Used as a fall back file for org-remember.el and org-capture.el, for
  1559. templates that do not specify a target file."
  1560. :group 'org-refile
  1561. :group 'org-remember
  1562. :type '(choice
  1563. (const :tag "Default from remember-data-file" nil)
  1564. file))
  1565. (defcustom org-goto-interface 'outline
  1566. "The default interface to be used for `org-goto'.
  1567. Allowed values are:
  1568. outline The interface shows an outline of the relevant file
  1569. and the correct heading is found by moving through
  1570. the outline or by searching with incremental search.
  1571. outline-path-completion Headlines in the current buffer are offered via
  1572. completion. This is the interface also used by
  1573. the refile command."
  1574. :group 'org-refile
  1575. :type '(choice
  1576. (const :tag "Outline" outline)
  1577. (const :tag "Outline-path-completion" outline-path-completion)))
  1578. (defcustom org-goto-max-level 5
  1579. "Maximum target level when running `org-goto' with refile interface."
  1580. :group 'org-refile
  1581. :type 'integer)
  1582. (defcustom org-reverse-note-order nil
  1583. "Non-nil means store new notes at the beginning of a file or entry.
  1584. When nil, new notes will be filed to the end of a file or entry.
  1585. This can also be a list with cons cells of regular expressions that
  1586. are matched against file names, and values."
  1587. :group 'org-remember
  1588. :group 'org-refile
  1589. :type '(choice
  1590. (const :tag "Reverse always" t)
  1591. (const :tag "Reverse never" nil)
  1592. (repeat :tag "By file name regexp"
  1593. (cons regexp boolean))))
  1594. (defcustom org-log-refile nil
  1595. "Information to record when a task is refiled.
  1596. Possible values are:
  1597. nil Don't add anything
  1598. time Add a time stamp to the task
  1599. note Prompt for a note and add it with template `org-log-note-headings'
  1600. This option can also be set with on a per-file-basis with
  1601. #+STARTUP: nologrefile
  1602. #+STARTUP: logrefile
  1603. #+STARTUP: lognoterefile
  1604. You can have local logging settings for a subtree by setting the LOGGING
  1605. property to one or more of these keywords.
  1606. When bulk-refiling from the agenda, the value `note' is forbidden and
  1607. will temporarily be changed to `time'."
  1608. :group 'org-refile
  1609. :group 'org-progress
  1610. :type '(choice
  1611. (const :tag "No logging" nil)
  1612. (const :tag "Record timestamp" time)
  1613. (const :tag "Record timestamp with note." note)))
  1614. (defcustom org-refile-targets nil
  1615. "Targets for refiling entries with \\[org-refile].
  1616. This is list of cons cells. Each cell contains:
  1617. - a specification of the files to be considered, either a list of files,
  1618. or a symbol whose function or variable value will be used to retrieve
  1619. a file name or a list of file names. If you use `org-agenda-files' for
  1620. that, all agenda files will be scanned for targets. Nil means consider
  1621. headings in the current buffer.
  1622. - A specification of how to find candidate refile targets. This may be
  1623. any of:
  1624. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1625. This tag has to be present in all target headlines, inheritance will
  1626. not be considered.
  1627. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1628. todo keyword.
  1629. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1630. headlines that are refiling targets.
  1631. - a cons cell (:level . N). Any headline of level N is considered a target.
  1632. Note that, when `org-odd-levels-only' is set, level corresponds to
  1633. order in hierarchy, not to the number of stars.
  1634. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1635. Note that, when `org-odd-levels-only' is set, level corresponds to
  1636. order in hierarchy, not to the number of stars.
  1637. You can set the variable `org-refile-target-verify-function' to a function
  1638. to verify each headline found by the simple criteria above.
  1639. When this variable is nil, all top-level headlines in the current buffer
  1640. are used, equivalent to the value `((nil . (:level . 1))'."
  1641. :group 'org-refile
  1642. :type '(repeat
  1643. (cons
  1644. (choice :value org-agenda-files
  1645. (const :tag "All agenda files" org-agenda-files)
  1646. (const :tag "Current buffer" nil)
  1647. (function) (variable) (file))
  1648. (choice :tag "Identify target headline by"
  1649. (cons :tag "Specific tag" (const :value :tag) (string))
  1650. (cons :tag "TODO keyword" (const :value :todo) (string))
  1651. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1652. (cons :tag "Level number" (const :value :level) (integer))
  1653. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1654. (defcustom org-refile-target-verify-function nil
  1655. "Function to verify if the headline at point should be a refile target.
  1656. The function will be called without arguments, with point at the
  1657. beginning of the headline. It should return t and leave point
  1658. where it is if the headline is a valid target for refiling.
  1659. If the target should not be selected, the function must return nil.
  1660. In addition to this, it may move point to a place from where the search
  1661. should be continued. For example, the function may decide that the entire
  1662. subtree of the current entry should be excluded and move point to the end
  1663. of the subtree."
  1664. :group 'org-refile
  1665. :type 'function)
  1666. (defcustom org-refile-use-cache nil
  1667. "Non-nil means cache refile targets to speed up the process.
  1668. The cache for a particular file will be updated automatically when
  1669. the buffer has been killed, or when any of the marker used for flagging
  1670. refile targets no longer points at a live buffer.
  1671. If you have added new entries to a buffer that might themselves be targets,
  1672. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  1673. find that easier, `C-u C-u C-u C-c C-w'."
  1674. :group 'org-refile
  1675. :type 'boolean)
  1676. (defcustom org-refile-use-outline-path nil
  1677. "Non-nil means provide refile targets as paths.
  1678. So a level 3 headline will be available as level1/level2/level3.
  1679. When the value is `file', also include the file name (without directory)
  1680. into the path. In this case, you can also stop the completion after
  1681. the file name, to get entries inserted as top level in the file.
  1682. When `full-file-path', include the full file path."
  1683. :group 'org-refile
  1684. :type '(choice
  1685. (const :tag "Not" nil)
  1686. (const :tag "Yes" t)
  1687. (const :tag "Start with file name" file)
  1688. (const :tag "Start with full file path" full-file-path)))
  1689. (defcustom org-outline-path-complete-in-steps t
  1690. "Non-nil means complete the outline path in hierarchical steps.
  1691. When Org-mode uses the refile interface to select an outline path
  1692. \(see variable `org-refile-use-outline-path'), the completion of
  1693. the path can be done is a single go, or if can be done in steps down
  1694. the headline hierarchy. Going in steps is probably the best if you
  1695. do not use a special completion package like `ido' or `icicles'.
  1696. However, when using these packages, going in one step can be very
  1697. fast, while still showing the whole path to the entry."
  1698. :group 'org-refile
  1699. :type 'boolean)
  1700. (defcustom org-refile-allow-creating-parent-nodes nil
  1701. "Non-nil means allow to create new nodes as refile targets.
  1702. New nodes are then created by adding \"/new node name\" to the completion
  1703. of an existing node. When the value of this variable is `confirm',
  1704. new node creation must be confirmed by the user (recommended)
  1705. When nil, the completion must match an existing entry.
  1706. Note that, if the new heading is not seen by the criteria
  1707. listed in `org-refile-targets', multiple instances of the same
  1708. heading would be created by trying again to file under the new
  1709. heading."
  1710. :group 'org-refile
  1711. :type '(choice
  1712. (const :tag "Never" nil)
  1713. (const :tag "Always" t)
  1714. (const :tag "Prompt for confirmation" confirm)))
  1715. (defgroup org-todo nil
  1716. "Options concerning TODO items in Org-mode."
  1717. :tag "Org TODO"
  1718. :group 'org)
  1719. (defgroup org-progress nil
  1720. "Options concerning Progress logging in Org-mode."
  1721. :tag "Org Progress"
  1722. :group 'org-time)
  1723. (defvar org-todo-interpretation-widgets
  1724. '(
  1725. (:tag "Sequence (cycling hits every state)" sequence)
  1726. (:tag "Type (cycling directly to DONE)" type))
  1727. "The available interpretation symbols for customizing `org-todo-keywords'.
  1728. Interested libraries should add to this list.")
  1729. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1730. "List of TODO entry keyword sequences and their interpretation.
  1731. \\<org-mode-map>This is a list of sequences.
  1732. Each sequence starts with a symbol, either `sequence' or `type',
  1733. indicating if the keywords should be interpreted as a sequence of
  1734. action steps, or as different types of TODO items. The first
  1735. keywords are states requiring action - these states will select a headline
  1736. for inclusion into the global TODO list Org-mode produces. If one of
  1737. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  1738. signify that no further action is necessary. If \"|\" is not found,
  1739. the last keyword is treated as the only DONE state of the sequence.
  1740. The command \\[org-todo] cycles an entry through these states, and one
  1741. additional state where no keyword is present. For details about this
  1742. cycling, see the manual.
  1743. TODO keywords and interpretation can also be set on a per-file basis with
  1744. the special #+SEQ_TODO and #+TYP_TODO lines.
  1745. Each keyword can optionally specify a character for fast state selection
  1746. \(in combination with the variable `org-use-fast-todo-selection')
  1747. and specifiers for state change logging, using the same syntax
  1748. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1749. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1750. indicates to record a time stamp each time this state is selected.
  1751. Each keyword may also specify if a timestamp or a note should be
  1752. recorded when entering or leaving the state, by adding additional
  1753. characters in the parenthesis after the keyword. This looks like this:
  1754. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1755. record only the time of the state change. With X and Y being either
  1756. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1757. Y when leaving the state if and only if the *target* state does not
  1758. define X. You may omit any of the fast-selection key or X or /Y,
  1759. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1760. For backward compatibility, this variable may also be just a list
  1761. of keywords - in this case the interpretation (sequence or type) will be
  1762. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1763. :group 'org-todo
  1764. :group 'org-keywords
  1765. :type '(choice
  1766. (repeat :tag "Old syntax, just keywords"
  1767. (string :tag "Keyword"))
  1768. (repeat :tag "New syntax"
  1769. (cons
  1770. (choice
  1771. :tag "Interpretation"
  1772. ;;Quick and dirty way to see
  1773. ;;`org-todo-interpretations'. This takes the
  1774. ;;place of item arguments
  1775. :convert-widget
  1776. (lambda (widget)
  1777. (widget-put widget
  1778. :args (mapcar
  1779. #'(lambda (x)
  1780. (widget-convert
  1781. (cons 'const x)))
  1782. org-todo-interpretation-widgets))
  1783. widget))
  1784. (repeat
  1785. (string :tag "Keyword"))))))
  1786. (defvar org-todo-keywords-1 nil
  1787. "All TODO and DONE keywords active in a buffer.")
  1788. (make-variable-buffer-local 'org-todo-keywords-1)
  1789. (defvar org-todo-keywords-for-agenda nil)
  1790. (defvar org-done-keywords-for-agenda nil)
  1791. (defvar org-drawers-for-agenda nil)
  1792. (defvar org-todo-keyword-alist-for-agenda nil)
  1793. (defvar org-tag-alist-for-agenda nil)
  1794. (defvar org-agenda-contributing-files nil)
  1795. (defvar org-not-done-keywords nil)
  1796. (make-variable-buffer-local 'org-not-done-keywords)
  1797. (defvar org-done-keywords nil)
  1798. (make-variable-buffer-local 'org-done-keywords)
  1799. (defvar org-todo-heads nil)
  1800. (make-variable-buffer-local 'org-todo-heads)
  1801. (defvar org-todo-sets nil)
  1802. (make-variable-buffer-local 'org-todo-sets)
  1803. (defvar org-todo-log-states nil)
  1804. (make-variable-buffer-local 'org-todo-log-states)
  1805. (defvar org-todo-kwd-alist nil)
  1806. (make-variable-buffer-local 'org-todo-kwd-alist)
  1807. (defvar org-todo-key-alist nil)
  1808. (make-variable-buffer-local 'org-todo-key-alist)
  1809. (defvar org-todo-key-trigger nil)
  1810. (make-variable-buffer-local 'org-todo-key-trigger)
  1811. (defcustom org-todo-interpretation 'sequence
  1812. "Controls how TODO keywords are interpreted.
  1813. This variable is in principle obsolete and is only used for
  1814. backward compatibility, if the interpretation of todo keywords is
  1815. not given already in `org-todo-keywords'. See that variable for
  1816. more information."
  1817. :group 'org-todo
  1818. :group 'org-keywords
  1819. :type '(choice (const sequence)
  1820. (const type)))
  1821. (defcustom org-use-fast-todo-selection t
  1822. "Non-nil means use the fast todo selection scheme with C-c C-t.
  1823. This variable describes if and under what circumstances the cycling
  1824. mechanism for TODO keywords will be replaced by a single-key, direct
  1825. selection scheme.
  1826. When nil, fast selection is never used.
  1827. When the symbol `prefix', it will be used when `org-todo' is called with
  1828. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1829. in an agenda buffer.
  1830. When t, fast selection is used by default. In this case, the prefix
  1831. argument forces cycling instead.
  1832. In all cases, the special interface is only used if access keys have actually
  1833. been assigned by the user, i.e. if keywords in the configuration are followed
  1834. by a letter in parenthesis, like TODO(t)."
  1835. :group 'org-todo
  1836. :type '(choice
  1837. (const :tag "Never" nil)
  1838. (const :tag "By default" t)
  1839. (const :tag "Only with C-u C-c C-t" prefix)))
  1840. (defcustom org-provide-todo-statistics t
  1841. "Non-nil means update todo statistics after insert and toggle.
  1842. ALL-HEADLINES means update todo statistics by including headlines
  1843. with no TODO keyword as well, counting them as not done.
  1844. A list of TODO keywords means the same, but skip keywords that are
  1845. not in this list.
  1846. When this is set, todo statistics is updated in the parent of the
  1847. current entry each time a todo state is changed."
  1848. :group 'org-todo
  1849. :type '(choice
  1850. (const :tag "Yes, only for TODO entries" t)
  1851. (const :tag "Yes, including all entries" 'all-headlines)
  1852. (repeat :tag "Yes, for TODOs in this list"
  1853. (string :tag "TODO keyword"))
  1854. (other :tag "No TODO statistics" nil)))
  1855. (defcustom org-hierarchical-todo-statistics t
  1856. "Non-nil means TODO statistics covers just direct children.
  1857. When nil, all entries in the subtree are considered.
  1858. This has only an effect if `org-provide-todo-statistics' is set.
  1859. To set this to nil for only a single subtree, use a COOKIE_DATA
  1860. property and include the word \"recursive\" into the value."
  1861. :group 'org-todo
  1862. :type 'boolean)
  1863. (defcustom org-after-todo-state-change-hook nil
  1864. "Hook which is run after the state of a TODO item was changed.
  1865. The new state (a string with a TODO keyword, or nil) is available in the
  1866. Lisp variable `state'."
  1867. :group 'org-todo
  1868. :type 'hook)
  1869. (defvar org-blocker-hook nil
  1870. "Hook for functions that are allowed to block a state change.
  1871. Each function gets as its single argument a property list, see
  1872. `org-trigger-hook' for more information about this list.
  1873. If any of the functions in this hook returns nil, the state change
  1874. is blocked.")
  1875. (defvar org-trigger-hook nil
  1876. "Hook for functions that are triggered by a state change.
  1877. Each function gets as its single argument a property list with at least
  1878. the following elements:
  1879. (:type type-of-change :position pos-at-entry-start
  1880. :from old-state :to new-state)
  1881. Depending on the type, more properties may be present.
  1882. This mechanism is currently implemented for:
  1883. TODO state changes
  1884. ------------------
  1885. :type todo-state-change
  1886. :from previous state (keyword as a string), or nil, or a symbol
  1887. 'todo' or 'done', to indicate the general type of state.
  1888. :to new state, like in :from")
  1889. (defcustom org-enforce-todo-dependencies nil
  1890. "Non-nil means undone TODO entries will block switching the parent to DONE.
  1891. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1892. be blocked if any prior sibling is not yet done.
  1893. Finally, if the parent is blocked because of ordered siblings of its own,
  1894. the child will also be blocked.
  1895. This variable needs to be set before org.el is loaded, and you need to
  1896. restart Emacs after a change to make the change effective. The only way
  1897. to change is while Emacs is running is through the customize interface."
  1898. :set (lambda (var val)
  1899. (set var val)
  1900. (if val
  1901. (add-hook 'org-blocker-hook
  1902. 'org-block-todo-from-children-or-siblings-or-parent)
  1903. (remove-hook 'org-blocker-hook
  1904. 'org-block-todo-from-children-or-siblings-or-parent)))
  1905. :group 'org-todo
  1906. :type 'boolean)
  1907. (defcustom org-enforce-todo-checkbox-dependencies nil
  1908. "Non-nil means unchecked boxes will block switching the parent to DONE.
  1909. When this is nil, checkboxes have no influence on switching TODO states.
  1910. When non-nil, you first need to check off all check boxes before the TODO
  1911. entry can be switched to DONE.
  1912. This variable needs to be set before org.el is loaded, and you need to
  1913. restart Emacs after a change to make the change effective. The only way
  1914. to change is while Emacs is running is through the customize interface."
  1915. :set (lambda (var val)
  1916. (set var val)
  1917. (if val
  1918. (add-hook 'org-blocker-hook
  1919. 'org-block-todo-from-checkboxes)
  1920. (remove-hook 'org-blocker-hook
  1921. 'org-block-todo-from-checkboxes)))
  1922. :group 'org-todo
  1923. :type 'boolean)
  1924. (defcustom org-treat-insert-todo-heading-as-state-change nil
  1925. "Non-nil means inserting a TODO heading is treated as state change.
  1926. So when the command \\[org-insert-todo-heading] is used, state change
  1927. logging will apply if appropriate. When nil, the new TODO item will
  1928. be inserted directly, and no logging will take place."
  1929. :group 'org-todo
  1930. :type 'boolean)
  1931. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  1932. "Non-nil means switching TODO states with S-cursor counts as state change.
  1933. This is the default behavior. However, setting this to nil allows a
  1934. convenient way to select a TODO state and bypass any logging associated
  1935. with that."
  1936. :group 'org-todo
  1937. :type 'boolean)
  1938. (defcustom org-todo-state-tags-triggers nil
  1939. "Tag changes that should be triggered by TODO state changes.
  1940. This is a list. Each entry is
  1941. (state-change (tag . flag) .......)
  1942. State-change can be a string with a state, and empty string to indicate the
  1943. state that has no TODO keyword, or it can be one of the symbols `todo'
  1944. or `done', meaning any not-done or done state, respectively."
  1945. :group 'org-todo
  1946. :group 'org-tags
  1947. :type '(repeat
  1948. (cons (choice :tag "When changing to"
  1949. (const :tag "Not-done state" todo)
  1950. (const :tag "Done state" done)
  1951. (string :tag "State"))
  1952. (repeat
  1953. (cons :tag "Tag action"
  1954. (string :tag "Tag")
  1955. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1956. (defcustom org-log-done nil
  1957. "Information to record when a task moves to the DONE state.
  1958. Possible values are:
  1959. nil Don't add anything, just change the keyword
  1960. time Add a time stamp to the task
  1961. note Prompt for a note and add it with template `org-log-note-headings'
  1962. This option can also be set with on a per-file-basis with
  1963. #+STARTUP: nologdone
  1964. #+STARTUP: logdone
  1965. #+STARTUP: lognotedone
  1966. You can have local logging settings for a subtree by setting the LOGGING
  1967. property to one or more of these keywords."
  1968. :group 'org-todo
  1969. :group 'org-progress
  1970. :type '(choice
  1971. (const :tag "No logging" nil)
  1972. (const :tag "Record CLOSED timestamp" time)
  1973. (const :tag "Record CLOSED timestamp with note." note)))
  1974. ;; Normalize old uses of org-log-done.
  1975. (cond
  1976. ((eq org-log-done t) (setq org-log-done 'time))
  1977. ((and (listp org-log-done) (memq 'done org-log-done))
  1978. (setq org-log-done 'note)))
  1979. (defcustom org-log-reschedule nil
  1980. "Information to record when the scheduling date of a tasks is modified.
  1981. Possible values are:
  1982. nil Don't add anything, just change the date
  1983. time Add a time stamp to the task
  1984. note Prompt for a note and add it with template `org-log-note-headings'
  1985. This option can also be set with on a per-file-basis with
  1986. #+STARTUP: nologreschedule
  1987. #+STARTUP: logreschedule
  1988. #+STARTUP: lognotereschedule"
  1989. :group 'org-todo
  1990. :group 'org-progress
  1991. :type '(choice
  1992. (const :tag "No logging" nil)
  1993. (const :tag "Record timestamp" time)
  1994. (const :tag "Record timestamp with note." note)))
  1995. (defcustom org-log-redeadline nil
  1996. "Information to record when the deadline date of a tasks is modified.
  1997. Possible values are:
  1998. nil Don't add anything, just change the date
  1999. time Add a time stamp to the task
  2000. note Prompt for a note and add it with template `org-log-note-headings'
  2001. This option can also be set with on a per-file-basis with
  2002. #+STARTUP: nologredeadline
  2003. #+STARTUP: logredeadline
  2004. #+STARTUP: lognoteredeadline
  2005. You can have local logging settings for a subtree by setting the LOGGING
  2006. property to one or more of these keywords."
  2007. :group 'org-todo
  2008. :group 'org-progress
  2009. :type '(choice
  2010. (const :tag "No logging" nil)
  2011. (const :tag "Record timestamp" time)
  2012. (const :tag "Record timestamp with note." note)))
  2013. (defcustom org-log-note-clock-out nil
  2014. "Non-nil means record a note when clocking out of an item.
  2015. This can also be configured on a per-file basis by adding one of
  2016. the following lines anywhere in the buffer:
  2017. #+STARTUP: lognoteclock-out
  2018. #+STARTUP: nolognoteclock-out"
  2019. :group 'org-todo
  2020. :group 'org-progress
  2021. :type 'boolean)
  2022. (defcustom org-log-done-with-time t
  2023. "Non-nil means the CLOSED time stamp will contain date and time.
  2024. When nil, only the date will be recorded."
  2025. :group 'org-progress
  2026. :type 'boolean)
  2027. (defcustom org-log-note-headings
  2028. '((done . "CLOSING NOTE %t")
  2029. (state . "State %-12s from %-12S %t")
  2030. (note . "Note taken on %t")
  2031. (reschedule . "Rescheduled from %S on %t")
  2032. (delschedule . "Not scheduled, was %S on %t")
  2033. (redeadline . "New deadline from %S on %t")
  2034. (deldeadline . "Removed deadline, was %S on %t")
  2035. (refile . "Refiled on %t")
  2036. (clock-out . ""))
  2037. "Headings for notes added to entries.
  2038. The value is an alist, with the car being a symbol indicating the note
  2039. context, and the cdr is the heading to be used. The heading may also be the
  2040. empty string.
  2041. %t in the heading will be replaced by a time stamp.
  2042. %T will be an active time stamp instead the default inactive one
  2043. %s will be replaced by the new TODO state, in double quotes.
  2044. %S will be replaced by the old TODO state, in double quotes.
  2045. %u will be replaced by the user name.
  2046. %U will be replaced by the full user name.
  2047. In fact, it is not a good idea to change the `state' entry, because
  2048. agenda log mode depends on the format of these entries."
  2049. :group 'org-todo
  2050. :group 'org-progress
  2051. :type '(list :greedy t
  2052. (cons (const :tag "Heading when closing an item" done) string)
  2053. (cons (const :tag
  2054. "Heading when changing todo state (todo sequence only)"
  2055. state) string)
  2056. (cons (const :tag "Heading when just taking a note" note) string)
  2057. (cons (const :tag "Heading when clocking out" clock-out) string)
  2058. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2059. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2060. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2061. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2062. (cons (const :tag "Heading when refiling" refile) string)))
  2063. (unless (assq 'note org-log-note-headings)
  2064. (push '(note . "%t") org-log-note-headings))
  2065. (defcustom org-log-into-drawer nil
  2066. "Non-nil means insert state change notes and time stamps into a drawer.
  2067. When nil, state changes notes will be inserted after the headline and
  2068. any scheduling and clock lines, but not inside a drawer.
  2069. The value of this variable should be the name of the drawer to use.
  2070. LOGBOOK is proposed at the default drawer for this purpose, you can
  2071. also set this to a string to define the drawer of your choice.
  2072. A value of t is also allowed, representing \"LOGBOOK\".
  2073. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2074. will be ignored.
  2075. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2076. a subtree."
  2077. :group 'org-todo
  2078. :group 'org-progress
  2079. :type '(choice
  2080. (const :tag "Not into a drawer" nil)
  2081. (const :tag "LOGBOOK" t)
  2082. (string :tag "Other")))
  2083. (if (fboundp 'defvaralias)
  2084. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  2085. (defun org-log-into-drawer ()
  2086. "Return the value of `org-log-into-drawer', but let properties overrule.
  2087. If the current entry has or inherits a LOG_INTO_DRAWER property, it will be
  2088. used instead of the default value."
  2089. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit)))
  2090. (cond
  2091. ((or (not p) (equal p "nil")) org-log-into-drawer)
  2092. ((equal p "t") "LOGBOOK")
  2093. (t p))))
  2094. (defcustom org-log-state-notes-insert-after-drawers nil
  2095. "Non-nil means insert state change notes after any drawers in entry.
  2096. Only the drawers that *immediately* follow the headline and the
  2097. deadline/scheduled line are skipped.
  2098. When nil, insert notes right after the heading and perhaps the line
  2099. with deadline/scheduling if present.
  2100. This variable will have no effect if `org-log-into-drawer' is
  2101. set."
  2102. :group 'org-todo
  2103. :group 'org-progress
  2104. :type 'boolean)
  2105. (defcustom org-log-states-order-reversed t
  2106. "Non-nil means the latest state note will be directly after heading.
  2107. When nil, the state change notes will be ordered according to time."
  2108. :group 'org-todo
  2109. :group 'org-progress
  2110. :type 'boolean)
  2111. (defcustom org-todo-repeat-to-state nil
  2112. "The TODO state to which a repeater should return the repeating task.
  2113. By default this is the first task in a TODO sequence, or the previous state
  2114. in a TODO_TYP set. But you can specify another task here.
  2115. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2116. :group 'org-todo
  2117. :type '(choice (const :tag "Head of sequence" nil)
  2118. (string :tag "Specific state")))
  2119. (defcustom org-log-repeat 'time
  2120. "Non-nil means record moving through the DONE state when triggering repeat.
  2121. An auto-repeating task is immediately switched back to TODO when
  2122. marked DONE. If you are not logging state changes (by adding \"@\"
  2123. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2124. record a closing note, there will be no record of the task moving
  2125. through DONE. This variable forces taking a note anyway.
  2126. nil Don't force a record
  2127. time Record a time stamp
  2128. note Record a note
  2129. This option can also be set with on a per-file-basis with
  2130. #+STARTUP: logrepeat
  2131. #+STARTUP: lognoterepeat
  2132. #+STARTUP: nologrepeat
  2133. You can have local logging settings for a subtree by setting the LOGGING
  2134. property to one or more of these keywords."
  2135. :group 'org-todo
  2136. :group 'org-progress
  2137. :type '(choice
  2138. (const :tag "Don't force a record" nil)
  2139. (const :tag "Force recording the DONE state" time)
  2140. (const :tag "Force recording a note with the DONE state" note)))
  2141. (defgroup org-priorities nil
  2142. "Priorities in Org-mode."
  2143. :tag "Org Priorities"
  2144. :group 'org-todo)
  2145. (defcustom org-enable-priority-commands t
  2146. "Non-nil means priority commands are active.
  2147. When nil, these commands will be disabled, so that you never accidentally
  2148. set a priority."
  2149. :group 'org-priorities
  2150. :type 'boolean)
  2151. (defcustom org-highest-priority ?A
  2152. "The highest priority of TODO items. A character like ?A, ?B etc.
  2153. Must have a smaller ASCII number than `org-lowest-priority'."
  2154. :group 'org-priorities
  2155. :type 'character)
  2156. (defcustom org-lowest-priority ?C
  2157. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2158. Must have a larger ASCII number than `org-highest-priority'."
  2159. :group 'org-priorities
  2160. :type 'character)
  2161. (defcustom org-default-priority ?B
  2162. "The default priority of TODO items.
  2163. This is the priority an item get if no explicit priority is given."
  2164. :group 'org-priorities
  2165. :type 'character)
  2166. (defcustom org-priority-start-cycle-with-default t
  2167. "Non-nil means start with default priority when starting to cycle.
  2168. When this is nil, the first step in the cycle will be (depending on the
  2169. command used) one higher or lower that the default priority."
  2170. :group 'org-priorities
  2171. :type 'boolean)
  2172. (defcustom org-get-priority-function nil
  2173. "Function to extract the priority from a string.
  2174. The string is normally the headline. If this is nil Org computes the
  2175. priority from the priority cookie like [#A] in the headline. It returns
  2176. an integer, increasing by 1000 for each priority level.
  2177. The user can set a different function here, which should take a string
  2178. as an argument and return the numeric priority."
  2179. :group 'org-priorities
  2180. :type 'function)
  2181. (defgroup org-time nil
  2182. "Options concerning time stamps and deadlines in Org-mode."
  2183. :tag "Org Time"
  2184. :group 'org)
  2185. (defcustom org-insert-labeled-timestamps-at-point nil
  2186. "Non-nil means SCHEDULED and DEADLINE timestamps are inserted at point.
  2187. When nil, these labeled time stamps are forces into the second line of an
  2188. entry, just after the headline. When scheduling from the global TODO list,
  2189. the time stamp will always be forced into the second line."
  2190. :group 'org-time
  2191. :type 'boolean)
  2192. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  2193. "Formats for `format-time-string' which are used for time stamps.
  2194. It is not recommended to change this constant.")
  2195. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2196. "Number of minutes to round time stamps to.
  2197. These are two values, the first applies when first creating a time stamp.
  2198. The second applies when changing it with the commands `S-up' and `S-down'.
  2199. When changing the time stamp, this means that it will change in steps
  2200. of N minutes, as given by the second value.
  2201. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2202. numbers should be factors of 60, so for example 5, 10, 15.
  2203. When this is larger than 1, you can still force an exact time stamp by using
  2204. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2205. and by using a prefix arg to `S-up/down' to specify the exact number
  2206. of minutes to shift."
  2207. :group 'org-time
  2208. :get '(lambda (var) ; Make sure both elements are there
  2209. (if (integerp (default-value var))
  2210. (list (default-value var) 5)
  2211. (default-value var)))
  2212. :type '(list
  2213. (integer :tag "when inserting times")
  2214. (integer :tag "when modifying times")))
  2215. ;; Normalize old customizations of this variable.
  2216. (when (integerp org-time-stamp-rounding-minutes)
  2217. (setq org-time-stamp-rounding-minutes
  2218. (list org-time-stamp-rounding-minutes
  2219. org-time-stamp-rounding-minutes)))
  2220. (defcustom org-display-custom-times nil
  2221. "Non-nil means overlay custom formats over all time stamps.
  2222. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2223. To turn this on on a per-file basis, insert anywhere in the file:
  2224. #+STARTUP: customtime"
  2225. :group 'org-time
  2226. :set 'set-default
  2227. :type 'sexp)
  2228. (make-variable-buffer-local 'org-display-custom-times)
  2229. (defcustom org-time-stamp-custom-formats
  2230. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2231. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2232. These are overlayed over the default ISO format if the variable
  2233. `org-display-custom-times' is set. Time like %H:%M should be at the
  2234. end of the second format. The custom formats are also honored by export
  2235. commands, if custom time display is turned on at the time of export."
  2236. :group 'org-time
  2237. :type 'sexp)
  2238. (defun org-time-stamp-format (&optional long inactive)
  2239. "Get the right format for a time string."
  2240. (let ((f (if long (cdr org-time-stamp-formats)
  2241. (car org-time-stamp-formats))))
  2242. (if inactive
  2243. (concat "[" (substring f 1 -1) "]")
  2244. f)))
  2245. (defcustom org-time-clocksum-format "%d:%02d"
  2246. "The format string used when creating CLOCKSUM lines.
  2247. This is also used when org-mode generates a time duration."
  2248. :group 'org-time
  2249. :type 'string)
  2250. (defcustom org-time-clocksum-use-fractional nil
  2251. "If non-nil, \\[org-clock-display] uses fractional times.
  2252. org-mode generates a time duration."
  2253. :group 'org-time
  2254. :type 'boolean)
  2255. (defcustom org-time-clocksum-fractional-format "%.2f"
  2256. "The format string used when creating CLOCKSUM lines, or when
  2257. org-mode generates a time duration."
  2258. :group 'org-time
  2259. :type 'string)
  2260. (defcustom org-deadline-warning-days 14
  2261. "No. of days before expiration during which a deadline becomes active.
  2262. This variable governs the display in sparse trees and in the agenda.
  2263. When 0 or negative, it means use this number (the absolute value of it)
  2264. even if a deadline has a different individual lead time specified.
  2265. Custom commands can set this variable in the options section."
  2266. :group 'org-time
  2267. :group 'org-agenda-daily/weekly
  2268. :type 'integer)
  2269. (defcustom org-read-date-prefer-future t
  2270. "Non-nil means assume future for incomplete date input from user.
  2271. This affects the following situations:
  2272. 1. The user gives a month but not a year.
  2273. For example, if it is April and you enter \"feb 2\", this will be read
  2274. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2275. 2. The user gives a day, but no month.
  2276. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2277. read this as the third of *next* month. However, if you enter \"17\",
  2278. it will be considered as *this* month.
  2279. If you set this variable to the symbol `time', then also the following
  2280. will work:
  2281. 3. If the user gives a time, but no day. If the time is before now,
  2282. to will be interpreted as tomorrow.
  2283. Currently none of this works for ISO week specifications.
  2284. When this option is nil, the current day, month and year will always be
  2285. used as defaults.
  2286. See also `org-agenda-jump-prefer-future'."
  2287. :group 'org-time
  2288. :type '(choice
  2289. (const :tag "Never" nil)
  2290. (const :tag "Check month and day" t)
  2291. (const :tag "Check month, day, and time" time)))
  2292. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2293. "Should the agenda jump command prefer the future for incomplete dates?
  2294. The default is to do the same as configured in `org-read-date-prefer-future'.
  2295. But you can also set a deviating value here.
  2296. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2297. :group 'org-agenda
  2298. :group 'org-time
  2299. :type '(choice
  2300. (const :tag "Use org-read-date-prefer-future"
  2301. org-read-date-prefer-future)
  2302. (const :tag "Never" nil)
  2303. (const :tag "Always" t)))
  2304. (defcustom org-read-date-force-compatible-dates t
  2305. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2306. Depending on the system Emacs is running on, certain dates cannot
  2307. be represented with the type used internally to represent time.
  2308. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2309. correctly. Some systems allow for earlier dates, some for later,
  2310. some for both. One way to find out it to insert any date into an
  2311. Org buffer, putting the cursor on the year and hitting S-up and
  2312. S-down to test the range.
  2313. When this variable is set to t, the date/time prompt will not let
  2314. you specify dates outside the 1970-2037 range, so it is certain that
  2315. these dates will work in whatever version of Emacs you are
  2316. running, and also that you can move a file from one Emacs implementation
  2317. to another. WHenever Org is forcing the year for you, it will display
  2318. a message and beep.
  2319. When this variable is nil, Org will check if the date is
  2320. representable in the specific Emacs implementation you are using.
  2321. If not, it will force a year, usually the current year, and beep
  2322. to remind you. Currently this setting is not recommended because
  2323. the likelihood that you will open your Org files in an Emacs that
  2324. has limited date range is not negligible.
  2325. A workaround for this problem is to use diary sexp dates for time
  2326. stamps outside of this range."
  2327. :group 'org-time
  2328. :type 'boolean)
  2329. (defcustom org-read-date-display-live t
  2330. "Non-nil means display current interpretation of date prompt live.
  2331. This display will be in an overlay, in the minibuffer."
  2332. :group 'org-time
  2333. :type 'boolean)
  2334. (defcustom org-read-date-popup-calendar t
  2335. "Non-nil means pop up a calendar when prompting for a date.
  2336. In the calendar, the date can be selected with mouse-1. However, the
  2337. minibuffer will also be active, and you can simply enter the date as well.
  2338. When nil, only the minibuffer will be available."
  2339. :group 'org-time
  2340. :type 'boolean)
  2341. (if (fboundp 'defvaralias)
  2342. (defvaralias 'org-popup-calendar-for-date-prompt
  2343. 'org-read-date-popup-calendar))
  2344. (defcustom org-read-date-minibuffer-setup-hook nil
  2345. "Hook to be used to set up keys for the date/time interface.
  2346. Add key definitions to `minibuffer-local-map', which will be a temporary
  2347. copy."
  2348. :group 'org-time
  2349. :type 'hook)
  2350. (defcustom org-extend-today-until 0
  2351. "The hour when your day really ends. Must be an integer.
  2352. This has influence for the following applications:
  2353. - When switching the agenda to \"today\". It it is still earlier than
  2354. the time given here, the day recognized as TODAY is actually yesterday.
  2355. - When a date is read from the user and it is still before the time given
  2356. here, the current date and time will be assumed to be yesterday, 23:59.
  2357. Also, timestamps inserted in remember templates follow this rule.
  2358. IMPORTANT: This is a feature whose implementation is and likely will
  2359. remain incomplete. Really, it is only here because past midnight seems to
  2360. be the favorite working time of John Wiegley :-)"
  2361. :group 'org-time
  2362. :type 'integer)
  2363. (defcustom org-edit-timestamp-down-means-later nil
  2364. "Non-nil means S-down will increase the time in a time stamp.
  2365. When nil, S-up will increase."
  2366. :group 'org-time
  2367. :type 'boolean)
  2368. (defcustom org-calendar-follow-timestamp-change t
  2369. "Non-nil means make the calendar window follow timestamp changes.
  2370. When a timestamp is modified and the calendar window is visible, it will be
  2371. moved to the new date."
  2372. :group 'org-time
  2373. :type 'boolean)
  2374. (defgroup org-tags nil
  2375. "Options concerning tags in Org-mode."
  2376. :tag "Org Tags"
  2377. :group 'org)
  2378. (defcustom org-tag-alist nil
  2379. "List of tags allowed in Org-mode files.
  2380. When this list is nil, Org-mode will base TAG input on what is already in the
  2381. buffer.
  2382. The value of this variable is an alist, the car of each entry must be a
  2383. keyword as a string, the cdr may be a character that is used to select
  2384. that tag through the fast-tag-selection interface.
  2385. See the manual for details."
  2386. :group 'org-tags
  2387. :type '(repeat
  2388. (choice
  2389. (cons (string :tag "Tag name")
  2390. (character :tag "Access char"))
  2391. (list :tag "Start radio group"
  2392. (const :startgroup)
  2393. (option (string :tag "Group description")))
  2394. (list :tag "End radio group"
  2395. (const :endgroup)
  2396. (option (string :tag "Group description")))
  2397. (const :tag "New line" (:newline)))))
  2398. (defcustom org-tag-persistent-alist nil
  2399. "List of tags that will always appear in all Org-mode files.
  2400. This is in addition to any in buffer settings or customizations
  2401. of `org-tag-alist'.
  2402. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  2403. The value of this variable is an alist, the car of each entry must be a
  2404. keyword as a string, the cdr may be a character that is used to select
  2405. that tag through the fast-tag-selection interface.
  2406. See the manual for details.
  2407. To disable these tags on a per-file basis, insert anywhere in the file:
  2408. #+STARTUP: noptag"
  2409. :group 'org-tags
  2410. :type '(repeat
  2411. (choice
  2412. (cons (string :tag "Tag name")
  2413. (character :tag "Access char"))
  2414. (const :tag "Start radio group" (:startgroup))
  2415. (const :tag "End radio group" (:endgroup))
  2416. (const :tag "New line" (:newline)))))
  2417. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  2418. "If non-nil, always offer completion for all tags of all agenda files.
  2419. Instead of customizing this variable directly, you might want to
  2420. set it locally for capture buffers, because there no list of
  2421. tags in that file can be created dynamically (there are none).
  2422. (add-hook 'org-capture-mode-hook
  2423. (lambda ()
  2424. (set (make-local-variable
  2425. 'org-complete-tags-always-offer-all-agenda-tags)
  2426. t)))"
  2427. :group 'org-tags
  2428. :type 'boolean)
  2429. (defvar org-file-tags nil
  2430. "List of tags that can be inherited by all entries in the file.
  2431. The tags will be inherited if the variable `org-use-tag-inheritance'
  2432. says they should be.
  2433. This variable is populated from #+FILETAGS lines.")
  2434. (defcustom org-use-fast-tag-selection 'auto
  2435. "Non-nil means use fast tag selection scheme.
  2436. This is a special interface to select and deselect tags with single keys.
  2437. When nil, fast selection is never used.
  2438. When the symbol `auto', fast selection is used if and only if selection
  2439. characters for tags have been configured, either through the variable
  2440. `org-tag-alist' or through a #+TAGS line in the buffer.
  2441. When t, fast selection is always used and selection keys are assigned
  2442. automatically if necessary."
  2443. :group 'org-tags
  2444. :type '(choice
  2445. (const :tag "Always" t)
  2446. (const :tag "Never" nil)
  2447. (const :tag "When selection characters are configured" 'auto)))
  2448. (defcustom org-fast-tag-selection-single-key nil
  2449. "Non-nil means fast tag selection exits after first change.
  2450. When nil, you have to press RET to exit it.
  2451. During fast tag selection, you can toggle this flag with `C-c'.
  2452. This variable can also have the value `expert'. In this case, the window
  2453. displaying the tags menu is not even shown, until you press C-c again."
  2454. :group 'org-tags
  2455. :type '(choice
  2456. (const :tag "No" nil)
  2457. (const :tag "Yes" t)
  2458. (const :tag "Expert" expert)))
  2459. (defvar org-fast-tag-selection-include-todo nil
  2460. "Non-nil means fast tags selection interface will also offer TODO states.
  2461. This is an undocumented feature, you should not rely on it.")
  2462. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  2463. "The column to which tags should be indented in a headline.
  2464. If this number is positive, it specifies the column. If it is negative,
  2465. it means that the tags should be flushright to that column. For example,
  2466. -80 works well for a normal 80 character screen."
  2467. :group 'org-tags
  2468. :type 'integer)
  2469. (defcustom org-auto-align-tags t
  2470. "Non-nil means realign tags after pro/demotion of TODO state change.
  2471. These operations change the length of a headline and therefore shift
  2472. the tags around. With this options turned on, after each such operation
  2473. the tags are again aligned to `org-tags-column'."
  2474. :group 'org-tags
  2475. :type 'boolean)
  2476. (defcustom org-use-tag-inheritance t
  2477. "Non-nil means tags in levels apply also for sublevels.
  2478. When nil, only the tags directly given in a specific line apply there.
  2479. This may also be a list of tags that should be inherited, or a regexp that
  2480. matches tags that should be inherited. Additional control is possible
  2481. with the variable `org-tags-exclude-from-inheritance' which gives an
  2482. explicit list of tags to be excluded from inheritance., even if the value of
  2483. `org-use-tag-inheritance' would select it for inheritance.
  2484. If this option is t, a match early-on in a tree can lead to a large
  2485. number of matches in the subtree when constructing the agenda or creating
  2486. a sparse tree. If you only want to see the first match in a tree during
  2487. a search, check out the variable `org-tags-match-list-sublevels'."
  2488. :group 'org-tags
  2489. :type '(choice
  2490. (const :tag "Not" nil)
  2491. (const :tag "Always" t)
  2492. (repeat :tag "Specific tags" (string :tag "Tag"))
  2493. (regexp :tag "Tags matched by regexp")))
  2494. (defcustom org-tags-exclude-from-inheritance nil
  2495. "List of tags that should never be inherited.
  2496. This is a way to exclude a few tags from inheritance. For way to do
  2497. the opposite, to actively allow inheritance for selected tags,
  2498. see the variable `org-use-tag-inheritance'."
  2499. :group 'org-tags
  2500. :type '(repeat (string :tag "Tag")))
  2501. (defun org-tag-inherit-p (tag)
  2502. "Check if TAG is one that should be inherited."
  2503. (cond
  2504. ((member tag org-tags-exclude-from-inheritance) nil)
  2505. ((eq org-use-tag-inheritance t) t)
  2506. ((not org-use-tag-inheritance) nil)
  2507. ((stringp org-use-tag-inheritance)
  2508. (string-match org-use-tag-inheritance tag))
  2509. ((listp org-use-tag-inheritance)
  2510. (member tag org-use-tag-inheritance))
  2511. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  2512. (defcustom org-tags-match-list-sublevels t
  2513. "Non-nil means list also sublevels of headlines matching a search.
  2514. This variable applies to tags/property searches, and also to stuck
  2515. projects because this search is based on a tags match as well.
  2516. When set to the symbol `indented', sublevels are indented with
  2517. leading dots.
  2518. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  2519. the sublevels of a headline matching a tag search often also match
  2520. the same search. Listing all of them can create very long lists.
  2521. Setting this variable to nil causes subtrees of a match to be skipped.
  2522. This variable is semi-obsolete and probably should always be true. It
  2523. is better to limit inheritance to certain tags using the variables
  2524. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  2525. :group 'org-tags
  2526. :type '(choice
  2527. (const :tag "No, don't list them" nil)
  2528. (const :tag "Yes, do list them" t)
  2529. (const :tag "List them, indented with leading dots" indented)))
  2530. (defcustom org-tags-sort-function nil
  2531. "When set, tags are sorted using this function as a comparator."
  2532. :group 'org-tags
  2533. :type '(choice
  2534. (const :tag "No sorting" nil)
  2535. (const :tag "Alphabetical" string<)
  2536. (const :tag "Reverse alphabetical" string>)
  2537. (function :tag "Custom function" nil)))
  2538. (defvar org-tags-history nil
  2539. "History of minibuffer reads for tags.")
  2540. (defvar org-last-tags-completion-table nil
  2541. "The last used completion table for tags.")
  2542. (defvar org-after-tags-change-hook nil
  2543. "Hook that is run after the tags in a line have changed.")
  2544. (defgroup org-properties nil
  2545. "Options concerning properties in Org-mode."
  2546. :tag "Org Properties"
  2547. :group 'org)
  2548. (defcustom org-property-format "%-10s %s"
  2549. "How property key/value pairs should be formatted by `indent-line'.
  2550. When `indent-line' hits a property definition, it will format the line
  2551. according to this format, mainly to make sure that the values are
  2552. lined-up with respect to each other."
  2553. :group 'org-properties
  2554. :type 'string)
  2555. (defcustom org-use-property-inheritance nil
  2556. "Non-nil means properties apply also for sublevels.
  2557. This setting is chiefly used during property searches. Turning it on can
  2558. cause significant overhead when doing a search, which is why it is not
  2559. on by default.
  2560. When nil, only the properties directly given in the current entry count.
  2561. When t, every property is inherited. The value may also be a list of
  2562. properties that should have inheritance, or a regular expression matching
  2563. properties that should be inherited.
  2564. However, note that some special properties use inheritance under special
  2565. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  2566. and the properties ending in \"_ALL\" when they are used as descriptor
  2567. for valid values of a property.
  2568. Note for programmers:
  2569. When querying an entry with `org-entry-get', you can control if inheritance
  2570. should be used. By default, `org-entry-get' looks only at the local
  2571. properties. You can request inheritance by setting the inherit argument
  2572. to t (to force inheritance) or to `selective' (to respect the setting
  2573. in this variable)."
  2574. :group 'org-properties
  2575. :type '(choice
  2576. (const :tag "Not" nil)
  2577. (const :tag "Always" t)
  2578. (repeat :tag "Specific properties" (string :tag "Property"))
  2579. (regexp :tag "Properties matched by regexp")))
  2580. (defun org-property-inherit-p (property)
  2581. "Check if PROPERTY is one that should be inherited."
  2582. (cond
  2583. ((eq org-use-property-inheritance t) t)
  2584. ((not org-use-property-inheritance) nil)
  2585. ((stringp org-use-property-inheritance)
  2586. (string-match org-use-property-inheritance property))
  2587. ((listp org-use-property-inheritance)
  2588. (member property org-use-property-inheritance))
  2589. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  2590. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  2591. "The default column format, if no other format has been defined.
  2592. This variable can be set on the per-file basis by inserting a line
  2593. #+COLUMNS: %25ITEM ....."
  2594. :group 'org-properties
  2595. :type 'string)
  2596. (defcustom org-columns-ellipses ".."
  2597. "The ellipses to be used when a field in column view is truncated.
  2598. When this is the empty string, as many characters as possible are shown,
  2599. but then there will be no visual indication that the field has been truncated.
  2600. When this is a string of length N, the last N characters of a truncated
  2601. field are replaced by this string. If the column is narrower than the
  2602. ellipses string, only part of the ellipses string will be shown."
  2603. :group 'org-properties
  2604. :type 'string)
  2605. (defcustom org-columns-modify-value-for-display-function nil
  2606. "Function that modifies values for display in column view.
  2607. For example, it can be used to cut out a certain part from a time stamp.
  2608. The function must take 2 arguments:
  2609. column-title The title of the column (*not* the property name)
  2610. value The value that should be modified.
  2611. The function should return the value that should be displayed,
  2612. or nil if the normal value should be used."
  2613. :group 'org-properties
  2614. :type 'function)
  2615. (defcustom org-effort-property "Effort"
  2616. "The property that is being used to keep track of effort estimates.
  2617. Effort estimates given in this property need to have the format H:MM."
  2618. :group 'org-properties
  2619. :group 'org-progress
  2620. :type '(string :tag "Property"))
  2621. (defconst org-global-properties-fixed
  2622. '(("VISIBILITY_ALL" . "folded children content all")
  2623. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  2624. "List of property/value pairs that can be inherited by any entry.
  2625. These are fixed values, for the preset properties. The user variable
  2626. that can be used to add to this list is `org-global-properties'.
  2627. The entries in this list are cons cells where the car is a property
  2628. name and cdr is a string with the value. If the value represents
  2629. multiple items like an \"_ALL\" property, separate the items by
  2630. spaces.")
  2631. (defcustom org-global-properties nil
  2632. "List of property/value pairs that can be inherited by any entry.
  2633. This list will be combined with the constant `org-global-properties-fixed'.
  2634. The entries in this list are cons cells where the car is a property
  2635. name and cdr is a string with the value.
  2636. You can set buffer-local values for the same purpose in the variable
  2637. `org-file-properties' this by adding lines like
  2638. #+PROPERTY: NAME VALUE"
  2639. :group 'org-properties
  2640. :type '(repeat
  2641. (cons (string :tag "Property")
  2642. (string :tag "Value"))))
  2643. (defvar org-file-properties nil
  2644. "List of property/value pairs that can be inherited by any entry.
  2645. Valid for the current buffer.
  2646. This variable is populated from #+PROPERTY lines.")
  2647. (make-variable-buffer-local 'org-file-properties)
  2648. (defgroup org-agenda nil
  2649. "Options concerning agenda views in Org-mode."
  2650. :tag "Org Agenda"
  2651. :group 'org)
  2652. (defvar org-category nil
  2653. "Variable used by org files to set a category for agenda display.
  2654. Such files should use a file variable to set it, for example
  2655. # -*- mode: org; org-category: \"ELisp\"
  2656. or contain a special line
  2657. #+CATEGORY: ELisp
  2658. If the file does not specify a category, then file's base name
  2659. is used instead.")
  2660. (make-variable-buffer-local 'org-category)
  2661. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2662. (defcustom org-agenda-files nil
  2663. "The files to be used for agenda display.
  2664. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2665. \\[org-remove-file]. You can also use customize to edit the list.
  2666. If an entry is a directory, all files in that directory that are matched by
  2667. `org-agenda-file-regexp' will be part of the file list.
  2668. If the value of the variable is not a list but a single file name, then
  2669. the list of agenda files is actually stored and maintained in that file, one
  2670. agenda file per line. In this file paths can be given relative to
  2671. `org-directory'. Tilde expansion and environment variable substitution
  2672. are also made."
  2673. :group 'org-agenda
  2674. :type '(choice
  2675. (repeat :tag "List of files and directories" file)
  2676. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2677. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2678. "Regular expression to match files for `org-agenda-files'.
  2679. If any element in the list in that variable contains a directory instead
  2680. of a normal file, all files in that directory that are matched by this
  2681. regular expression will be included."
  2682. :group 'org-agenda
  2683. :type 'regexp)
  2684. (defcustom org-agenda-text-search-extra-files nil
  2685. "List of extra files to be searched by text search commands.
  2686. These files will be search in addition to the agenda files by the
  2687. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2688. Note that these files will only be searched for text search commands,
  2689. not for the other agenda views like todo lists, tag searches or the weekly
  2690. agenda. This variable is intended to list notes and possibly archive files
  2691. that should also be searched by these two commands.
  2692. In fact, if the first element in the list is the symbol `agenda-archives',
  2693. than all archive files of all agenda files will be added to the search
  2694. scope."
  2695. :group 'org-agenda
  2696. :type '(set :greedy t
  2697. (const :tag "Agenda Archives" agenda-archives)
  2698. (repeat :inline t (file))))
  2699. (if (fboundp 'defvaralias)
  2700. (defvaralias 'org-agenda-multi-occur-extra-files
  2701. 'org-agenda-text-search-extra-files))
  2702. (defcustom org-agenda-skip-unavailable-files nil
  2703. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2704. A nil value means to remove them, after a query, from the list."
  2705. :group 'org-agenda
  2706. :type 'boolean)
  2707. (defcustom org-calendar-to-agenda-key [?c]
  2708. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2709. The command `org-calendar-goto-agenda' will be bound to this key. The
  2710. default is the character `c' because then `c' can be used to switch back and
  2711. forth between agenda and calendar."
  2712. :group 'org-agenda
  2713. :type 'sexp)
  2714. (defcustom org-calendar-agenda-action-key [?k]
  2715. "The key to be installed in `calendar-mode-map' for agenda-action.
  2716. The command `org-agenda-action' will be bound to this key. The
  2717. default is the character `k' because we use the same key in the agenda."
  2718. :group 'org-agenda
  2719. :type 'sexp)
  2720. (defcustom org-calendar-insert-diary-entry-key [?i]
  2721. "The key to be installed in `calendar-mode-map' for adding diary entries.
  2722. This option is irrelevant until `org-agenda-diary-file' has been configured
  2723. to point to an Org-mode file. When that is the case, the command
  2724. `org-agenda-diary-entry' will be bound to the key given here, by default
  2725. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  2726. if you want to continue doing this, you need to change this to a different
  2727. key."
  2728. :group 'org-agenda
  2729. :type 'sexp)
  2730. (defcustom org-agenda-diary-file 'diary-file
  2731. "File to which to add new entries with the `i' key in agenda and calendar.
  2732. When this is the symbol `diary-file', the functionality in the Emacs
  2733. calendar will be used to add entries to the `diary-file'. But when this
  2734. points to a file, `org-agenda-diary-entry' will be used instead."
  2735. :group 'org-agenda
  2736. :type '(choice
  2737. (const :tag "The standard Emacs diary file" diary-file)
  2738. (file :tag "Special Org file diary entries")))
  2739. (eval-after-load "calendar"
  2740. '(progn
  2741. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2742. 'org-calendar-goto-agenda)
  2743. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2744. 'org-agenda-action)
  2745. (add-hook 'calendar-mode-hook
  2746. (lambda ()
  2747. (unless (eq org-agenda-diary-file 'diary-file)
  2748. (define-key calendar-mode-map
  2749. org-calendar-insert-diary-entry-key
  2750. 'org-agenda-diary-entry))))))
  2751. (defgroup org-latex nil
  2752. "Options for embedding LaTeX code into Org-mode."
  2753. :tag "Org LaTeX"
  2754. :group 'org)
  2755. (defcustom org-format-latex-options
  2756. '(:foreground default :background default :scale 1.0
  2757. :html-foreground "Black" :html-background "Transparent"
  2758. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2759. "Options for creating images from LaTeX fragments.
  2760. This is a property list with the following properties:
  2761. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2762. `default' means use the foreground of the default face.
  2763. :background the background color, or \"Transparent\".
  2764. `default' means use the background of the default face.
  2765. :scale a scaling factor for the size of the images, to get more pixels
  2766. :html-foreground, :html-background, :html-scale
  2767. the same numbers for HTML export.
  2768. :matchers a list indicating which matchers should be used to
  2769. find LaTeX fragments. Valid members of this list are:
  2770. \"begin\" find environments
  2771. \"$1\" find single characters surrounded by $.$
  2772. \"$\" find math expressions surrounded by $...$
  2773. \"$$\" find math expressions surrounded by $$....$$
  2774. \"\\(\" find math expressions surrounded by \\(...\\)
  2775. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2776. :group 'org-latex
  2777. :type 'plist)
  2778. (defcustom org-format-latex-signal-error t
  2779. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  2780. When nil, just push out a message."
  2781. :group 'org-latex
  2782. :type 'boolean)
  2783. (defcustom org-format-latex-header "\\documentclass{article}
  2784. \\usepackage[usenames]{color}
  2785. \\usepackage{amsmath}
  2786. \\usepackage[mathscr]{eucal}
  2787. \\pagestyle{empty} % do not remove
  2788. \[PACKAGES]
  2789. \[DEFAULT-PACKAGES]
  2790. % The settings below are copied from fullpage.sty
  2791. \\setlength{\\textwidth}{\\paperwidth}
  2792. \\addtolength{\\textwidth}{-3cm}
  2793. \\setlength{\\oddsidemargin}{1.5cm}
  2794. \\addtolength{\\oddsidemargin}{-2.54cm}
  2795. \\setlength{\\evensidemargin}{\\oddsidemargin}
  2796. \\setlength{\\textheight}{\\paperheight}
  2797. \\addtolength{\\textheight}{-\\headheight}
  2798. \\addtolength{\\textheight}{-\\headsep}
  2799. \\addtolength{\\textheight}{-\\footskip}
  2800. \\addtolength{\\textheight}{-3cm}
  2801. \\setlength{\\topmargin}{1.5cm}
  2802. \\addtolength{\\topmargin}{-2.54cm}"
  2803. "The document header used for processing LaTeX fragments.
  2804. It is imperative that this header make sure that no page number
  2805. appears on the page. The package defined in the variables
  2806. `org-export-latex-default-packages-alist' and `org-export-latex-packages-alist'
  2807. will either replace the placeholder \"[PACKAGES]\" in this header, or they
  2808. will be appended."
  2809. :group 'org-latex
  2810. :type 'string)
  2811. (defvar org-format-latex-header-extra nil)
  2812. (defun org-set-packages-alist (var val)
  2813. "Set the packages alist and make sure it has 3 elements per entry."
  2814. (set var (mapcar (lambda (x)
  2815. (if (and (consp x) (= (length x) 2))
  2816. (list (car x) (nth 1 x) t)
  2817. x))
  2818. val)))
  2819. (defun org-get-packages-alist (var)
  2820. "Get the packages alist and make sure it has 3 elements per entry."
  2821. (mapcar (lambda (x)
  2822. (if (and (consp x) (= (length x) 2))
  2823. (list (car x) (nth 1 x) t)
  2824. x))
  2825. (default-value var)))
  2826. ;; The following variables are defined here because is it also used
  2827. ;; when formatting latex fragments. Originally it was part of the
  2828. ;; LaTeX exporter, which is why the name includes "export".
  2829. (defcustom org-export-latex-default-packages-alist
  2830. '(("AUTO" "inputenc" t)
  2831. ("T1" "fontenc" t)
  2832. ("" "fixltx2e" nil)
  2833. ("" "graphicx" t)
  2834. ("" "longtable" nil)
  2835. ("" "float" nil)
  2836. ("" "wrapfig" nil)
  2837. ("" "soul" t)
  2838. ("" "textcomp" t)
  2839. ("" "marvosym" t)
  2840. ("" "wasysym" t)
  2841. ("" "latexsym" t)
  2842. ("" "amssymb" t)
  2843. ("" "hyperref" nil)
  2844. "\\tolerance=1000"
  2845. )
  2846. "Alist of default packages to be inserted in the header.
  2847. Change this only if one of the packages here causes an incompatibility
  2848. with another package you are using.
  2849. The packages in this list are needed by one part or another of Org-mode
  2850. to function properly.
  2851. - inputenc, fontenc: for basic font and character selection
  2852. - textcomp, marvosymb, wasysym, latexsym, amssym: for various symbols used
  2853. for interpreting the entities in `org-entities'. You can skip some of these
  2854. packages if you don't use any of the symbols in it.
  2855. - graphicx: for including images
  2856. - float, wrapfig: for figure placement
  2857. - longtable: for long tables
  2858. - hyperref: for cross references
  2859. Therefore you should not modify this variable unless you know what you
  2860. are doing. The one reason to change it anyway is that you might be loading
  2861. some other package that conflicts with one of the default packages.
  2862. Each cell is of the format \( \"options\" \"package\" snippet-flag\).
  2863. If SNIPPET-FLAG is t, the package also needs to be included when
  2864. compiling LaTeX snippets into images for inclusion into HTML."
  2865. :group 'org-export-latex
  2866. :set 'org-set-packages-alist
  2867. :get 'org-get-packages-alist
  2868. :type '(repeat
  2869. (choice
  2870. (list :tag "options/package pair"
  2871. (string :tag "options")
  2872. (string :tag "package")
  2873. (boolean :tag "Snippet"))
  2874. (string :tag "A line of LaTeX"))))
  2875. (defcustom org-export-latex-packages-alist nil
  2876. "Alist of packages to be inserted in every LaTeX header.
  2877. These will be inserted after `org-export-latex-default-packages-alist'.
  2878. Each cell is of the format \( \"options\" \"package\" snippet-flag \).
  2879. SNIPPET-FLAG, when t, indicates that this package is also needed when
  2880. turning LaTeX snippets into images for inclusion into HTML.
  2881. Make sure that you only list packages here which:
  2882. - you want in every file
  2883. - do not conflict with the default packages in
  2884. `org-export-latex-default-packages-alist'
  2885. - do not conflict with the setup in `org-format-latex-header'."
  2886. :group 'org-export-latex
  2887. :set 'org-set-packages-alist
  2888. :get 'org-get-packages-alist
  2889. :type '(repeat
  2890. (choice
  2891. (list :tag "options/package pair"
  2892. (string :tag "options")
  2893. (string :tag "package")
  2894. (boolean :tag "Snippet"))
  2895. (string :tag "A line of LaTeX"))))
  2896. (defgroup org-appearance nil
  2897. "Settings for Org-mode appearance."
  2898. :tag "Org Appearance"
  2899. :group 'org)
  2900. (defcustom org-level-color-stars-only nil
  2901. "Non-nil means fontify only the stars in each headline.
  2902. When nil, the entire headline is fontified.
  2903. Changing it requires restart of `font-lock-mode' to become effective
  2904. also in regions already fontified."
  2905. :group 'org-appearance
  2906. :type 'boolean)
  2907. (defcustom org-hide-leading-stars nil
  2908. "Non-nil means hide the first N-1 stars in a headline.
  2909. This works by using the face `org-hide' for these stars. This
  2910. face is white for a light background, and black for a dark
  2911. background. You may have to customize the face `org-hide' to
  2912. make this work.
  2913. Changing it requires restart of `font-lock-mode' to become effective
  2914. also in regions already fontified.
  2915. You may also set this on a per-file basis by adding one of the following
  2916. lines to the buffer:
  2917. #+STARTUP: hidestars
  2918. #+STARTUP: showstars"
  2919. :group 'org-appearance
  2920. :type 'boolean)
  2921. (defcustom org-hidden-keywords nil
  2922. "List of symbols corresponding to keywords to be hidden the org buffer.
  2923. For example, a value '(title) for this list will make the document's title
  2924. appear in the buffer without the initial #+TITLE: keyword."
  2925. :group 'org-appearance
  2926. :type '(set (const :tag "#+AUTHOR" author)
  2927. (const :tag "#+DATE" date)
  2928. (const :tag "#+EMAIL" email)
  2929. (const :tag "#+TITLE" title)))
  2930. (defcustom org-fontify-done-headline nil
  2931. "Non-nil means change the face of a headline if it is marked DONE.
  2932. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2933. When this is non-nil, the headline after the keyword is set to the
  2934. `org-headline-done' as an additional indication."
  2935. :group 'org-appearance
  2936. :type 'boolean)
  2937. (defcustom org-fontify-emphasized-text t
  2938. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2939. Changing this variable requires a restart of Emacs to take effect."
  2940. :group 'org-appearance
  2941. :type 'boolean)
  2942. (defcustom org-fontify-whole-heading-line nil
  2943. "Non-nil means fontify the whole line for headings.
  2944. This is useful when setting a background color for the
  2945. org-level-* faces."
  2946. :group 'org-appearance
  2947. :type 'boolean)
  2948. (defcustom org-highlight-latex-fragments-and-specials nil
  2949. "Non-nil means fontify what is treated specially by the exporters."
  2950. :group 'org-appearance
  2951. :type 'boolean)
  2952. (defcustom org-hide-emphasis-markers nil
  2953. "Non-nil mean font-lock should hide the emphasis marker characters."
  2954. :group 'org-appearance
  2955. :type 'boolean)
  2956. (defcustom org-pretty-entities nil
  2957. "Non-nil means show entities as UTF8 characters.
  2958. When nil, the \\name form remains in the buffer."
  2959. :group 'org-appearance
  2960. :type 'boolean)
  2961. (defcustom org-pretty-entities-include-sub-superscripts t
  2962. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  2963. :group 'org-appearance
  2964. :type 'boolean)
  2965. (defvar org-emph-re nil
  2966. "Regular expression for matching emphasis.
  2967. After a match, the match groups contain these elements:
  2968. 0 The match of the full regular expression, including the characters
  2969. before and after the proper match
  2970. 1 The character before the proper match, or empty at beginning of line
  2971. 2 The proper match, including the leading and trailing markers
  2972. 3 The leading marker like * or /, indicating the type of highlighting
  2973. 4 The text between the emphasis markers, not including the markers
  2974. 5 The character after the match, empty at the end of a line")
  2975. (defvar org-verbatim-re nil
  2976. "Regular expression for matching verbatim text.")
  2977. (defvar org-emphasis-regexp-components) ; defined just below
  2978. (defvar org-emphasis-alist) ; defined just below
  2979. (defun org-set-emph-re (var val)
  2980. "Set variable and compute the emphasis regular expression."
  2981. (set var val)
  2982. (when (and (boundp 'org-emphasis-alist)
  2983. (boundp 'org-emphasis-regexp-components)
  2984. org-emphasis-alist org-emphasis-regexp-components)
  2985. (let* ((e org-emphasis-regexp-components)
  2986. (pre (car e))
  2987. (post (nth 1 e))
  2988. (border (nth 2 e))
  2989. (body (nth 3 e))
  2990. (nl (nth 4 e))
  2991. (body1 (concat body "*?"))
  2992. (markers (mapconcat 'car org-emphasis-alist ""))
  2993. (vmarkers (mapconcat
  2994. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2995. org-emphasis-alist "")))
  2996. ;; make sure special characters appear at the right position in the class
  2997. (if (string-match "\\^" markers)
  2998. (setq markers (concat (replace-match "" t t markers) "^")))
  2999. (if (string-match "-" markers)
  3000. (setq markers (concat (replace-match "" t t markers) "-")))
  3001. (if (string-match "\\^" vmarkers)
  3002. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3003. (if (string-match "-" vmarkers)
  3004. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3005. (if (> nl 0)
  3006. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3007. (int-to-string nl) "\\}")))
  3008. ;; Make the regexp
  3009. (setq org-emph-re
  3010. (concat "\\([" pre "]\\|^\\)"
  3011. "\\("
  3012. "\\([" markers "]\\)"
  3013. "\\("
  3014. "[^" border "]\\|"
  3015. "[^" border "]"
  3016. body1
  3017. "[^" border "]"
  3018. "\\)"
  3019. "\\3\\)"
  3020. "\\([" post "]\\|$\\)"))
  3021. (setq org-verbatim-re
  3022. (concat "\\([" pre "]\\|^\\)"
  3023. "\\("
  3024. "\\([" vmarkers "]\\)"
  3025. "\\("
  3026. "[^" border "]\\|"
  3027. "[^" border "]"
  3028. body1
  3029. "[^" border "]"
  3030. "\\)"
  3031. "\\3\\)"
  3032. "\\([" post "]\\|$\\)")))))
  3033. (defcustom org-emphasis-regexp-components
  3034. '(" \t('\"{" "- \t.,:!?;'\")}\\" " \t\r\n,\"'" "." 1)
  3035. "Components used to build the regular expression for emphasis.
  3036. This is a list with five entries. Terminology: In an emphasis string
  3037. like \" *strong word* \", we call the initial space PREMATCH, the final
  3038. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3039. and \"trong wor\" is the body. The different components in this variable
  3040. specify what is allowed/forbidden in each part:
  3041. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3042. post Chars allowed as postmatch. End of line will be allowed too.
  3043. border The chars *forbidden* as border characters.
  3044. body-regexp A regexp like \".\" to match a body character. Don't use
  3045. non-shy groups here, and don't allow newline here.
  3046. newline The maximum number of newlines allowed in an emphasis exp.
  3047. Use customize to modify this, or restart Emacs after changing it."
  3048. :group 'org-appearance
  3049. :set 'org-set-emph-re
  3050. :type '(list
  3051. (sexp :tag "Allowed chars in pre ")
  3052. (sexp :tag "Allowed chars in post ")
  3053. (sexp :tag "Forbidden chars in border ")
  3054. (sexp :tag "Regexp for body ")
  3055. (integer :tag "number of newlines allowed")
  3056. (option (boolean :tag "Please ignore this button"))))
  3057. (defcustom org-emphasis-alist
  3058. `(("*" bold "<b>" "</b>")
  3059. ("/" italic "<i>" "</i>")
  3060. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  3061. ("=" org-code "<code>" "</code>" verbatim)
  3062. ("~" org-verbatim "<code>" "</code>" verbatim)
  3063. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  3064. "<del>" "</del>")
  3065. )
  3066. "Special syntax for emphasized text.
  3067. Text starting and ending with a special character will be emphasized, for
  3068. example *bold*, _underlined_ and /italic/. This variable sets the marker
  3069. characters, the face to be used by font-lock for highlighting in Org-mode
  3070. Emacs buffers, and the HTML tags to be used for this.
  3071. For LaTeX export, see the variable `org-export-latex-emphasis-alist'.
  3072. For DocBook export, see the variable `org-export-docbook-emphasis-alist'.
  3073. Use customize to modify this, or restart Emacs after changing it."
  3074. :group 'org-appearance
  3075. :set 'org-set-emph-re
  3076. :type '(repeat
  3077. (list
  3078. (string :tag "Marker character")
  3079. (choice
  3080. (face :tag "Font-lock-face")
  3081. (plist :tag "Face property list"))
  3082. (string :tag "HTML start tag")
  3083. (string :tag "HTML end tag")
  3084. (option (const verbatim)))))
  3085. (defvar org-protecting-blocks
  3086. '("src" "example" "latex" "ascii" "html" "docbook" "ditaa" "dot" "r" "R")
  3087. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3088. This is needed for font-lock setup.")
  3089. ;;; Miscellaneous options
  3090. (defgroup org-completion nil
  3091. "Completion in Org-mode."
  3092. :tag "Org Completion"
  3093. :group 'org)
  3094. (defcustom org-completion-use-ido nil
  3095. "Non-nil means use ido completion wherever possible.
  3096. Note that `ido-mode' must be active for this variable to be relevant.
  3097. If you decide to turn this variable on, you might well want to turn off
  3098. `org-outline-path-complete-in-steps'.
  3099. See also `org-completion-use-iswitchb'."
  3100. :group 'org-completion
  3101. :type 'boolean)
  3102. (defcustom org-completion-use-iswitchb nil
  3103. "Non-nil means use iswitchb completion wherever possible.
  3104. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3105. If you decide to turn this variable on, you might well want to turn off
  3106. `org-outline-path-complete-in-steps'.
  3107. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3108. :group 'org-completion
  3109. :type 'boolean)
  3110. (defcustom org-completion-fallback-command 'hippie-expand
  3111. "The expansion command called by \\[pcomplete] in normal context.
  3112. Normal means, no org-mode-specific context."
  3113. :group 'org-completion
  3114. :type 'function)
  3115. ;;; Functions and variables from their packages
  3116. ;; Declared here to avoid compiler warnings
  3117. ;; XEmacs only
  3118. (defvar outline-mode-menu-heading)
  3119. (defvar outline-mode-menu-show)
  3120. (defvar outline-mode-menu-hide)
  3121. (defvar zmacs-regions) ; XEmacs regions
  3122. ;; Emacs only
  3123. (defvar mark-active)
  3124. ;; Various packages
  3125. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  3126. (declare-function calendar-forward-day "cal-move" (arg))
  3127. (declare-function calendar-goto-date "cal-move" (date))
  3128. (declare-function calendar-goto-today "cal-move" ())
  3129. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3130. (defvar calc-embedded-close-formula)
  3131. (defvar calc-embedded-open-formula)
  3132. (declare-function cdlatex-tab "ext:cdlatex" ())
  3133. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3134. (defvar font-lock-unfontify-region-function)
  3135. (declare-function iswitchb-read-buffer "iswitchb"
  3136. (prompt &optional default require-match start matches-set))
  3137. (defvar iswitchb-temp-buflist)
  3138. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3139. (defvar org-agenda-tags-todo-honor-ignore-options)
  3140. (declare-function org-agenda-skip "org-agenda" ())
  3141. (declare-function
  3142. org-format-agenda-item "org-agenda"
  3143. (extra txt &optional category tags dotime noprefix remove-re habitp))
  3144. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3145. (declare-function org-agenda-change-all-lines "org-agenda"
  3146. (newhead hdmarker &optional fixface just-this))
  3147. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3148. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3149. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3150. (beg end))
  3151. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3152. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3153. "org-agenda" (&optional end))
  3154. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3155. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3156. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3157. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3158. (declare-function org-indent-mode "org-indent" (&optional arg))
  3159. (declare-function parse-time-string "parse-time" (string))
  3160. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3161. (declare-function org-export-latex-fix-inputenc "org-latex" ())
  3162. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3163. (defvar remember-data-file)
  3164. (defvar texmathp-why)
  3165. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3166. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3167. (defvar w3m-current-url)
  3168. (defvar w3m-current-title)
  3169. (defvar org-latex-regexps)
  3170. ;;; Autoload and prepare some org modules
  3171. ;; Some table stuff that needs to be defined here, because it is used
  3172. ;; by the functions setting up org-mode or checking for table context.
  3173. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  3174. "Detect an org-type or table-type table.")
  3175. (defconst org-table-line-regexp "^[ \t]*|"
  3176. "Detect an org-type table line.")
  3177. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  3178. "Detect an org-type table line.")
  3179. (defconst org-table-hline-regexp "^[ \t]*|-"
  3180. "Detect an org-type table hline.")
  3181. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  3182. "Detect a table-type table hline.")
  3183. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  3184. "Detect the first line outside a table when searching from within it.
  3185. This works for both table types.")
  3186. ;; Autoload the functions in org-table.el that are needed by functions here.
  3187. (eval-and-compile
  3188. (org-autoload "org-table"
  3189. '(org-table-align org-table-begin org-table-blank-field
  3190. org-table-convert org-table-convert-region org-table-copy-down
  3191. org-table-copy-region org-table-create
  3192. org-table-create-or-convert-from-region
  3193. org-table-create-with-table.el org-table-current-dline
  3194. org-table-cut-region org-table-delete-column org-table-edit-field
  3195. org-table-edit-formulas org-table-end org-table-eval-formula
  3196. org-table-export org-table-field-info
  3197. org-table-get-stored-formulas org-table-goto-column
  3198. org-table-hline-and-move org-table-import org-table-insert-column
  3199. org-table-insert-hline org-table-insert-row org-table-iterate
  3200. org-table-justify-field-maybe org-table-kill-row
  3201. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  3202. org-table-move-column org-table-move-column-left
  3203. org-table-move-column-right org-table-move-row
  3204. org-table-move-row-down org-table-move-row-up
  3205. org-table-next-field org-table-next-row org-table-paste-rectangle
  3206. org-table-previous-field org-table-recalculate
  3207. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  3208. org-table-toggle-coordinate-overlays
  3209. org-table-toggle-formula-debugger org-table-wrap-region
  3210. orgtbl-mode turn-on-orgtbl org-table-to-lisp
  3211. orgtbl-to-generic orgtbl-to-tsv orgtbl-to-csv orgtbl-to-latex
  3212. orgtbl-to-orgtbl orgtbl-to-html orgtbl-to-texinfo)))
  3213. (defun org-at-table-p (&optional table-type)
  3214. "Return t if the cursor is inside an org-type table.
  3215. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  3216. (if org-enable-table-editor
  3217. (save-excursion
  3218. (beginning-of-line 1)
  3219. (looking-at (if table-type org-table-any-line-regexp
  3220. org-table-line-regexp)))
  3221. nil))
  3222. (defsubst org-table-p () (org-at-table-p))
  3223. (defun org-at-table.el-p ()
  3224. "Return t if and only if we are at a table.el table."
  3225. (and (org-at-table-p 'any)
  3226. (save-excursion
  3227. (goto-char (org-table-begin 'any))
  3228. (looking-at org-table1-hline-regexp))))
  3229. (defun org-table-recognize-table.el ()
  3230. "If there is a table.el table nearby, recognize it and move into it."
  3231. (if org-table-tab-recognizes-table.el
  3232. (if (org-at-table.el-p)
  3233. (progn
  3234. (beginning-of-line 1)
  3235. (if (looking-at org-table-dataline-regexp)
  3236. nil
  3237. (if (looking-at org-table1-hline-regexp)
  3238. (progn
  3239. (beginning-of-line 2)
  3240. (if (looking-at org-table-any-border-regexp)
  3241. (beginning-of-line -1)))))
  3242. (if (re-search-forward "|" (org-table-end t) t)
  3243. (progn
  3244. (require 'table)
  3245. (if (table--at-cell-p (point))
  3246. t
  3247. (message "recognizing table.el table...")
  3248. (table-recognize-table)
  3249. (message "recognizing table.el table...done")))
  3250. (error "This should not happen"))
  3251. t)
  3252. nil)
  3253. nil))
  3254. (defun org-at-table-hline-p ()
  3255. "Return t if the cursor is inside a hline in a table."
  3256. (if org-enable-table-editor
  3257. (save-excursion
  3258. (beginning-of-line 1)
  3259. (looking-at org-table-hline-regexp))
  3260. nil))
  3261. (defvar org-table-clean-did-remove-column nil)
  3262. (defun org-table-map-tables (function &optional quietly)
  3263. "Apply FUNCTION to the start of all tables in the buffer."
  3264. (save-excursion
  3265. (save-restriction
  3266. (widen)
  3267. (goto-char (point-min))
  3268. (while (re-search-forward org-table-any-line-regexp nil t)
  3269. (unless quietly
  3270. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size))))
  3271. (beginning-of-line 1)
  3272. (when (looking-at org-table-line-regexp)
  3273. (save-excursion (funcall function))
  3274. (or (looking-at org-table-line-regexp)
  3275. (forward-char 1)))
  3276. (re-search-forward org-table-any-border-regexp nil 1))))
  3277. (unless quietly (message "Mapping tables: done")))
  3278. ;; Declare and autoload functions from org-exp.el & Co
  3279. (declare-function org-default-export-plist "org-exp")
  3280. (declare-function org-infile-export-plist "org-exp")
  3281. (declare-function org-get-current-options "org-exp")
  3282. (eval-and-compile
  3283. (org-autoload "org-exp"
  3284. '(org-export org-export-visible
  3285. org-insert-export-options-template
  3286. org-table-clean-before-export))
  3287. (org-autoload "org-ascii"
  3288. '(org-export-as-ascii org-export-ascii-preprocess
  3289. org-export-as-ascii-to-buffer org-replace-region-by-ascii
  3290. org-export-region-as-ascii))
  3291. (org-autoload "org-latex"
  3292. '(org-export-as-latex-batch org-export-as-latex-to-buffer
  3293. org-replace-region-by-latex org-export-region-as-latex
  3294. org-export-as-latex org-export-as-pdf
  3295. org-export-as-pdf-and-open))
  3296. (org-autoload "org-html"
  3297. '(org-export-as-html-and-open
  3298. org-export-as-html-batch org-export-as-html-to-buffer
  3299. org-replace-region-by-html org-export-region-as-html
  3300. org-export-as-html))
  3301. (org-autoload "org-docbook"
  3302. '(org-export-as-docbook-batch org-export-as-docbook-to-buffer
  3303. org-replace-region-by-docbook org-export-region-as-docbook
  3304. org-export-as-docbook-pdf org-export-as-docbook-pdf-and-open
  3305. org-export-as-docbook))
  3306. (org-autoload "org-icalendar"
  3307. '(org-export-icalendar-this-file
  3308. org-export-icalendar-all-agenda-files
  3309. org-export-icalendar-combine-agenda-files))
  3310. (org-autoload "org-xoxo" '(org-export-as-xoxo))
  3311. (org-autoload "org-beamer" '(org-beamer-mode org-beamer-sectioning)))
  3312. ;; Declare and autoload functions from org-agenda.el
  3313. (eval-and-compile
  3314. (org-autoload "org-agenda"
  3315. '(org-agenda org-agenda-list org-search-view
  3316. org-todo-list org-tags-view org-agenda-list-stuck-projects
  3317. org-diary org-agenda-to-appt
  3318. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  3319. ;; Autoload org-remember
  3320. (eval-and-compile
  3321. (org-autoload "org-remember"
  3322. '(org-remember-insinuate org-remember-annotation
  3323. org-remember-apply-template org-remember org-remember-handler)))
  3324. (eval-and-compile
  3325. (org-autoload "org-capture"
  3326. '(org-capture org-capture-insert-template-here
  3327. org-capture-import-remember-templates)))
  3328. ;; Autoload org-clock.el
  3329. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  3330. (beg end))
  3331. (declare-function org-clock-update-mode-line "org-clock" ())
  3332. (declare-function org-resolve-clocks "org-clock"
  3333. (&optional also-non-dangling-p prompt last-valid))
  3334. (defvar org-clock-start-time)
  3335. (defvar org-clock-marker (make-marker)
  3336. "Marker recording the last clock-in.")
  3337. (defvar org-clock-hd-marker (make-marker)
  3338. "Marker recording the last clock-in, but the headline position.")
  3339. (defvar org-clock-heading ""
  3340. "The heading of the current clock entry.")
  3341. (defun org-clock-is-active ()
  3342. "Return non-nil if clock is currently running.
  3343. The return value is actually the clock marker."
  3344. (marker-buffer org-clock-marker))
  3345. (eval-and-compile
  3346. (org-autoload
  3347. "org-clock"
  3348. '(org-clock-in org-clock-out org-clock-cancel
  3349. org-clock-goto org-clock-sum org-clock-display
  3350. org-clock-remove-overlays org-clock-report
  3351. org-clocktable-shift org-dblock-write:clocktable
  3352. org-get-clocktable org-resolve-clocks)))
  3353. (defun org-clock-update-time-maybe ()
  3354. "If this is a CLOCK line, update it and return t.
  3355. Otherwise, return nil."
  3356. (interactive)
  3357. (save-excursion
  3358. (beginning-of-line 1)
  3359. (skip-chars-forward " \t")
  3360. (when (looking-at org-clock-string)
  3361. (let ((re (concat "[ \t]*" org-clock-string
  3362. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  3363. "\\([ \t]*=>.*\\)?\\)?"))
  3364. ts te h m s neg)
  3365. (cond
  3366. ((not (looking-at re))
  3367. nil)
  3368. ((not (match-end 2))
  3369. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3370. (> org-clock-marker (point))
  3371. (<= org-clock-marker (point-at-eol)))
  3372. ;; The clock is running here
  3373. (setq org-clock-start-time
  3374. (apply 'encode-time
  3375. (org-parse-time-string (match-string 1))))
  3376. (org-clock-update-mode-line)))
  3377. (t
  3378. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  3379. (end-of-line 1)
  3380. (setq ts (match-string 1)
  3381. te (match-string 3))
  3382. (setq s (- (org-float-time
  3383. (apply 'encode-time (org-parse-time-string te)))
  3384. (org-float-time
  3385. (apply 'encode-time (org-parse-time-string ts))))
  3386. neg (< s 0)
  3387. s (abs s)
  3388. h (floor (/ s 3600))
  3389. s (- s (* 3600 h))
  3390. m (floor (/ s 60))
  3391. s (- s (* 60 s)))
  3392. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  3393. t))))))
  3394. (defun org-check-running-clock ()
  3395. "Check if the current buffer contains the running clock.
  3396. If yes, offer to stop it and to save the buffer with the changes."
  3397. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3398. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3399. (buffer-name))))
  3400. (org-clock-out)
  3401. (when (y-or-n-p "Save changed buffer?")
  3402. (save-buffer))))
  3403. (defun org-clocktable-try-shift (dir n)
  3404. "Check if this line starts a clock table, if yes, shift the time block."
  3405. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  3406. (org-clocktable-shift dir n)))
  3407. ;; Autoload org-timer.el
  3408. (eval-and-compile
  3409. (org-autoload
  3410. "org-timer"
  3411. '(org-timer-start org-timer org-timer-item
  3412. org-timer-change-times-in-region
  3413. org-timer-set-timer
  3414. org-timer-reset-timers
  3415. org-timer-show-remaining-time)))
  3416. ;; Autoload org-feed.el
  3417. (eval-and-compile
  3418. (org-autoload
  3419. "org-feed"
  3420. '(org-feed-update org-feed-update-all org-feed-goto-inbox)))
  3421. ;; Autoload org-indent.el
  3422. ;; Define the variable already here, to make sure we have it.
  3423. (defvar org-indent-mode nil
  3424. "Non-nil if Org-Indent mode is enabled.
  3425. Use the command `org-indent-mode' to change this variable.")
  3426. (eval-and-compile
  3427. (org-autoload
  3428. "org-indent"
  3429. '(org-indent-mode)))
  3430. ;; Autoload org-mobile.el
  3431. (eval-and-compile
  3432. (org-autoload
  3433. "org-mobile"
  3434. '(org-mobile-push org-mobile-pull org-mobile-create-sumo-agenda)))
  3435. ;; Autoload archiving code
  3436. ;; The stuff that is needed for cycling and tags has to be defined here.
  3437. (defgroup org-archive nil
  3438. "Options concerning archiving in Org-mode."
  3439. :tag "Org Archive"
  3440. :group 'org-structure)
  3441. (defcustom org-archive-location "%s_archive::"
  3442. "The location where subtrees should be archived.
  3443. The value of this variable is a string, consisting of two parts,
  3444. separated by a double-colon. The first part is a filename and
  3445. the second part is a headline.
  3446. When the filename is omitted, archiving happens in the same file.
  3447. %s in the filename will be replaced by the current file
  3448. name (without the directory part). Archiving to a different file
  3449. is useful to keep archived entries from contributing to the
  3450. Org-mode Agenda.
  3451. The archived entries will be filed as subtrees of the specified
  3452. headline. When the headline is omitted, the subtrees are simply
  3453. filed away at the end of the file, as top-level entries. Also in
  3454. the heading you can use %s to represent the file name, this can be
  3455. useful when using the same archive for a number of different files.
  3456. Here are a few examples:
  3457. \"%s_archive::\"
  3458. If the current file is Projects.org, archive in file
  3459. Projects.org_archive, as top-level trees. This is the default.
  3460. \"::* Archived Tasks\"
  3461. Archive in the current file, under the top-level headline
  3462. \"* Archived Tasks\".
  3463. \"~/org/archive.org::\"
  3464. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3465. \"~/org/archive.org::From %s\"
  3466. Archive in file ~/org/archive.org (absolute path), under headlines
  3467. \"From FILENAME\" where file name is the current file name.
  3468. \"basement::** Finished Tasks\"
  3469. Archive in file ./basement (relative path), as level 3 trees
  3470. below the level 2 heading \"** Finished Tasks\".
  3471. You may set this option on a per-file basis by adding to the buffer a
  3472. line like
  3473. #+ARCHIVE: basement::** Finished Tasks
  3474. You may also define it locally for a subtree by setting an ARCHIVE property
  3475. in the entry. If such a property is found in an entry, or anywhere up
  3476. the hierarchy, it will be used."
  3477. :group 'org-archive
  3478. :type 'string)
  3479. (defcustom org-archive-tag "ARCHIVE"
  3480. "The tag that marks a subtree as archived.
  3481. An archived subtree does not open during visibility cycling, and does
  3482. not contribute to the agenda listings.
  3483. After changing this, font-lock must be restarted in the relevant buffers to
  3484. get the proper fontification."
  3485. :group 'org-archive
  3486. :group 'org-keywords
  3487. :type 'string)
  3488. (defcustom org-agenda-skip-archived-trees t
  3489. "Non-nil means the agenda will skip any items located in archived trees.
  3490. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  3491. variable is no longer recommended, you should leave it at the value t.
  3492. Instead, use the key `v' to cycle the archives-mode in the agenda."
  3493. :group 'org-archive
  3494. :group 'org-agenda-skip
  3495. :type 'boolean)
  3496. (defcustom org-columns-skip-archived-trees t
  3497. "Non-nil means ignore archived trees when creating column view."
  3498. :group 'org-archive
  3499. :group 'org-properties
  3500. :type 'boolean)
  3501. (defcustom org-cycle-open-archived-trees nil
  3502. "Non-nil means `org-cycle' will open archived trees.
  3503. An archived tree is a tree marked with the tag ARCHIVE.
  3504. When nil, archived trees will stay folded. You can still open them with
  3505. normal outline commands like `show-all', but not with the cycling commands."
  3506. :group 'org-archive
  3507. :group 'org-cycle
  3508. :type 'boolean)
  3509. (defcustom org-sparse-tree-open-archived-trees nil
  3510. "Non-nil means sparse tree construction shows matches in archived trees.
  3511. When nil, matches in these trees are highlighted, but the trees are kept in
  3512. collapsed state."
  3513. :group 'org-archive
  3514. :group 'org-sparse-trees
  3515. :type 'boolean)
  3516. (defun org-cycle-hide-archived-subtrees (state)
  3517. "Re-hide all archived subtrees after a visibility state change."
  3518. (when (and (not org-cycle-open-archived-trees)
  3519. (not (memq state '(overview folded))))
  3520. (save-excursion
  3521. (let* ((globalp (memq state '(contents all)))
  3522. (beg (if globalp (point-min) (point)))
  3523. (end (if globalp (point-max) (org-end-of-subtree t))))
  3524. (org-hide-archived-subtrees beg end)
  3525. (goto-char beg)
  3526. (if (looking-at (concat ".*:" org-archive-tag ":"))
  3527. (message "%s" (substitute-command-keys
  3528. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  3529. (defun org-force-cycle-archived ()
  3530. "Cycle subtree even if it is archived."
  3531. (interactive)
  3532. (setq this-command 'org-cycle)
  3533. (let ((org-cycle-open-archived-trees t))
  3534. (call-interactively 'org-cycle)))
  3535. (defun org-hide-archived-subtrees (beg end)
  3536. "Re-hide all archived subtrees after a visibility state change."
  3537. (save-excursion
  3538. (let* ((re (concat ":" org-archive-tag ":")))
  3539. (goto-char beg)
  3540. (while (re-search-forward re end t)
  3541. (when (org-on-heading-p)
  3542. (org-flag-subtree t)
  3543. (org-end-of-subtree t))))))
  3544. (defun org-flag-subtree (flag)
  3545. (save-excursion
  3546. (org-back-to-heading t)
  3547. (outline-end-of-heading)
  3548. (outline-flag-region (point)
  3549. (progn (org-end-of-subtree t) (point))
  3550. flag)))
  3551. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  3552. (eval-and-compile
  3553. (org-autoload "org-archive"
  3554. '(org-add-archive-files org-archive-subtree
  3555. org-archive-to-archive-sibling org-toggle-archive-tag
  3556. org-archive-subtree-default
  3557. org-archive-subtree-default-with-confirmation)))
  3558. ;; Autoload Column View Code
  3559. (declare-function org-columns-number-to-string "org-colview")
  3560. (declare-function org-columns-get-format-and-top-level "org-colview")
  3561. (declare-function org-columns-compute "org-colview")
  3562. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  3563. '(org-columns-number-to-string org-columns-get-format-and-top-level
  3564. org-columns-compute org-agenda-columns org-columns-remove-overlays
  3565. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  3566. ;; Autoload ID code
  3567. (declare-function org-id-store-link "org-id")
  3568. (declare-function org-id-locations-load "org-id")
  3569. (declare-function org-id-locations-save "org-id")
  3570. (defvar org-id-track-globally)
  3571. (org-autoload "org-id"
  3572. '(org-id-get-create org-id-new org-id-copy org-id-get
  3573. org-id-get-with-outline-path-completion
  3574. org-id-get-with-outline-drilling org-id-store-link
  3575. org-id-goto org-id-find org-id-store-link))
  3576. ;; Autoload Plotting Code
  3577. (org-autoload "org-plot"
  3578. '(org-plot/gnuplot))
  3579. ;;; Variables for pre-computed regular expressions, all buffer local
  3580. (defvar org-drawer-regexp nil
  3581. "Matches first line of a hidden block.")
  3582. (make-variable-buffer-local 'org-drawer-regexp)
  3583. (defvar org-todo-regexp nil
  3584. "Matches any of the TODO state keywords.")
  3585. (make-variable-buffer-local 'org-todo-regexp)
  3586. (defvar org-not-done-regexp nil
  3587. "Matches any of the TODO state keywords except the last one.")
  3588. (make-variable-buffer-local 'org-not-done-regexp)
  3589. (defvar org-not-done-heading-regexp nil
  3590. "Matches a TODO headline that is not done.")
  3591. (make-variable-buffer-local 'org-not-done-regexp)
  3592. (defvar org-todo-line-regexp nil
  3593. "Matches a headline and puts TODO state into group 2 if present.")
  3594. (make-variable-buffer-local 'org-todo-line-regexp)
  3595. (defvar org-complex-heading-regexp nil
  3596. "Matches a headline and puts everything into groups:
  3597. group 1: the stars
  3598. group 2: The todo keyword, maybe
  3599. group 3: Priority cookie
  3600. group 4: True headline
  3601. group 5: Tags")
  3602. (make-variable-buffer-local 'org-complex-heading-regexp)
  3603. (defvar org-complex-heading-regexp-format nil
  3604. "Printf format to make regexp to match an exact headline.
  3605. This regexp will match the headline of any node which hase the exact
  3606. headline text that is put into the format, but may have any TODO state,
  3607. priority and tags.")
  3608. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  3609. (defvar org-todo-line-tags-regexp nil
  3610. "Matches a headline and puts TODO state into group 2 if present.
  3611. Also put tags into group 4 if tags are present.")
  3612. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  3613. (defvar org-nl-done-regexp nil
  3614. "Matches newline followed by a headline with the DONE keyword.")
  3615. (make-variable-buffer-local 'org-nl-done-regexp)
  3616. (defvar org-looking-at-done-regexp nil
  3617. "Matches the DONE keyword a point.")
  3618. (make-variable-buffer-local 'org-looking-at-done-regexp)
  3619. (defvar org-ds-keyword-length 12
  3620. "Maximum length of the Deadline and SCHEDULED keywords.")
  3621. (make-variable-buffer-local 'org-ds-keyword-length)
  3622. (defvar org-deadline-regexp nil
  3623. "Matches the DEADLINE keyword.")
  3624. (make-variable-buffer-local 'org-deadline-regexp)
  3625. (defvar org-deadline-time-regexp nil
  3626. "Matches the DEADLINE keyword together with a time stamp.")
  3627. (make-variable-buffer-local 'org-deadline-time-regexp)
  3628. (defvar org-deadline-line-regexp nil
  3629. "Matches the DEADLINE keyword and the rest of the line.")
  3630. (make-variable-buffer-local 'org-deadline-line-regexp)
  3631. (defvar org-scheduled-regexp nil
  3632. "Matches the SCHEDULED keyword.")
  3633. (make-variable-buffer-local 'org-scheduled-regexp)
  3634. (defvar org-scheduled-time-regexp nil
  3635. "Matches the SCHEDULED keyword together with a time stamp.")
  3636. (make-variable-buffer-local 'org-scheduled-time-regexp)
  3637. (defvar org-closed-time-regexp nil
  3638. "Matches the CLOSED keyword together with a time stamp.")
  3639. (make-variable-buffer-local 'org-closed-time-regexp)
  3640. (defvar org-keyword-time-regexp nil
  3641. "Matches any of the 4 keywords, together with the time stamp.")
  3642. (make-variable-buffer-local 'org-keyword-time-regexp)
  3643. (defvar org-keyword-time-not-clock-regexp nil
  3644. "Matches any of the 3 keywords, together with the time stamp.")
  3645. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  3646. (defvar org-maybe-keyword-time-regexp nil
  3647. "Matches a timestamp, possibly preceded by a keyword.")
  3648. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  3649. (defvar org-planning-or-clock-line-re nil
  3650. "Matches a line with planning or clock info.")
  3651. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  3652. (defvar org-all-time-keywords nil
  3653. "List of time keywords.")
  3654. (make-variable-buffer-local 'org-all-time-keywords)
  3655. (defconst org-plain-time-of-day-regexp
  3656. (concat
  3657. "\\(\\<[012]?[0-9]"
  3658. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3659. "\\(--?"
  3660. "\\(\\<[012]?[0-9]"
  3661. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3662. "\\)?")
  3663. "Regular expression to match a plain time or time range.
  3664. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3665. groups carry important information:
  3666. 0 the full match
  3667. 1 the first time, range or not
  3668. 8 the second time, if it is a range.")
  3669. (defconst org-plain-time-extension-regexp
  3670. (concat
  3671. "\\(\\<[012]?[0-9]"
  3672. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  3673. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  3674. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  3675. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  3676. groups carry important information:
  3677. 0 the full match
  3678. 7 hours of duration
  3679. 9 minutes of duration")
  3680. (defconst org-stamp-time-of-day-regexp
  3681. (concat
  3682. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  3683. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  3684. "\\(--?"
  3685. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  3686. "Regular expression to match a timestamp time or time range.
  3687. After a match, the following groups carry important information:
  3688. 0 the full match
  3689. 1 date plus weekday, for back referencing to make sure both times are on the same day
  3690. 2 the first time, range or not
  3691. 4 the second time, if it is a range.")
  3692. (defconst org-startup-options
  3693. '(("fold" org-startup-folded t)
  3694. ("overview" org-startup-folded t)
  3695. ("nofold" org-startup-folded nil)
  3696. ("showall" org-startup-folded nil)
  3697. ("showeverything" org-startup-folded showeverything)
  3698. ("content" org-startup-folded content)
  3699. ("indent" org-startup-indented t)
  3700. ("noindent" org-startup-indented nil)
  3701. ("hidestars" org-hide-leading-stars t)
  3702. ("showstars" org-hide-leading-stars nil)
  3703. ("odd" org-odd-levels-only t)
  3704. ("oddeven" org-odd-levels-only nil)
  3705. ("align" org-startup-align-all-tables t)
  3706. ("noalign" org-startup-align-all-tables nil)
  3707. ("inlineimages" org-startup-with-inline-images t)
  3708. ("noinlineimages" org-startup-with-inline-images nil)
  3709. ("customtime" org-display-custom-times t)
  3710. ("logdone" org-log-done time)
  3711. ("lognotedone" org-log-done note)
  3712. ("nologdone" org-log-done nil)
  3713. ("lognoteclock-out" org-log-note-clock-out t)
  3714. ("nolognoteclock-out" org-log-note-clock-out nil)
  3715. ("logrepeat" org-log-repeat state)
  3716. ("lognoterepeat" org-log-repeat note)
  3717. ("nologrepeat" org-log-repeat nil)
  3718. ("logreschedule" org-log-reschedule time)
  3719. ("lognotereschedule" org-log-reschedule note)
  3720. ("nologreschedule" org-log-reschedule nil)
  3721. ("logredeadline" org-log-redeadline time)
  3722. ("lognoteredeadline" org-log-redeadline note)
  3723. ("nologredeadline" org-log-redeadline nil)
  3724. ("logrefile" org-log-refile time)
  3725. ("lognoterefile" org-log-refile note)
  3726. ("nologrefile" org-log-refile nil)
  3727. ("fninline" org-footnote-define-inline t)
  3728. ("nofninline" org-footnote-define-inline nil)
  3729. ("fnlocal" org-footnote-section nil)
  3730. ("fnauto" org-footnote-auto-label t)
  3731. ("fnprompt" org-footnote-auto-label nil)
  3732. ("fnconfirm" org-footnote-auto-label confirm)
  3733. ("fnplain" org-footnote-auto-label plain)
  3734. ("fnadjust" org-footnote-auto-adjust t)
  3735. ("nofnadjust" org-footnote-auto-adjust nil)
  3736. ("constcgs" constants-unit-system cgs)
  3737. ("constSI" constants-unit-system SI)
  3738. ("noptag" org-tag-persistent-alist nil)
  3739. ("hideblocks" org-hide-block-startup t)
  3740. ("nohideblocks" org-hide-block-startup nil)
  3741. ("beamer" org-startup-with-beamer-mode t)
  3742. ("entitiespretty" org-pretty-entities t)
  3743. ("entitiesplain" org-pretty-entities nil))
  3744. "Variable associated with STARTUP options for org-mode.
  3745. Each element is a list of three items: The startup options as written
  3746. in the #+STARTUP line, the corresponding variable, and the value to
  3747. set this variable to if the option is found. An optional forth element PUSH
  3748. means to push this value onto the list in the variable.")
  3749. (defun org-set-regexps-and-options ()
  3750. "Precompute regular expressions for current buffer."
  3751. (when (org-mode-p)
  3752. (org-set-local 'org-todo-kwd-alist nil)
  3753. (org-set-local 'org-todo-key-alist nil)
  3754. (org-set-local 'org-todo-key-trigger nil)
  3755. (org-set-local 'org-todo-keywords-1 nil)
  3756. (org-set-local 'org-done-keywords nil)
  3757. (org-set-local 'org-todo-heads nil)
  3758. (org-set-local 'org-todo-sets nil)
  3759. (org-set-local 'org-todo-log-states nil)
  3760. (org-set-local 'org-file-properties nil)
  3761. (org-set-local 'org-file-tags nil)
  3762. (let ((re (org-make-options-regexp
  3763. '("CATEGORY" "TODO" "COLUMNS"
  3764. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  3765. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE" "LATEX_CLASS"
  3766. "OPTIONS")
  3767. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  3768. (splitre "[ \t]+")
  3769. (scripts org-use-sub-superscripts)
  3770. kwds kws0 kwsa key log value cat arch tags const links hw dws
  3771. tail sep kws1 prio props ftags drawers beamer-p
  3772. ext-setup-or-nil setup-contents (start 0))
  3773. (save-excursion
  3774. (save-restriction
  3775. (widen)
  3776. (goto-char (point-min))
  3777. (while (or (and ext-setup-or-nil
  3778. (string-match re ext-setup-or-nil start)
  3779. (setq start (match-end 0)))
  3780. (and (setq ext-setup-or-nil nil start 0)
  3781. (re-search-forward re nil t)))
  3782. (setq key (upcase (match-string 1 ext-setup-or-nil))
  3783. value (org-match-string-no-properties 2 ext-setup-or-nil))
  3784. (if (stringp value) (setq value (org-trim value)))
  3785. (cond
  3786. ((equal key "CATEGORY")
  3787. (setq cat value))
  3788. ((member key '("SEQ_TODO" "TODO"))
  3789. (push (cons 'sequence (org-split-string value splitre)) kwds))
  3790. ((equal key "TYP_TODO")
  3791. (push (cons 'type (org-split-string value splitre)) kwds))
  3792. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  3793. ;; general TODO-like setup
  3794. (push (cons (intern (downcase (match-string 1 key)))
  3795. (org-split-string value splitre)) kwds))
  3796. ((equal key "TAGS")
  3797. (setq tags (append tags (if tags '("\\n") nil)
  3798. (org-split-string value splitre))))
  3799. ((equal key "COLUMNS")
  3800. (org-set-local 'org-columns-default-format value))
  3801. ((equal key "LINK")
  3802. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  3803. (push (cons (match-string 1 value)
  3804. (org-trim (match-string 2 value)))
  3805. links)))
  3806. ((equal key "PRIORITIES")
  3807. (setq prio (org-split-string value " +")))
  3808. ((equal key "PROPERTY")
  3809. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  3810. (push (cons (match-string 1 value) (match-string 2 value))
  3811. props)))
  3812. ((equal key "FILETAGS")
  3813. (when (string-match "\\S-" value)
  3814. (setq ftags
  3815. (append
  3816. ftags
  3817. (apply 'append
  3818. (mapcar (lambda (x) (org-split-string x ":"))
  3819. (org-split-string value)))))))
  3820. ((equal key "DRAWERS")
  3821. (setq drawers (org-split-string value splitre)))
  3822. ((equal key "CONSTANTS")
  3823. (setq const (append const (org-split-string value splitre))))
  3824. ((equal key "STARTUP")
  3825. (let ((opts (org-split-string value splitre))
  3826. l var val)
  3827. (while (setq l (pop opts))
  3828. (when (setq l (assoc l org-startup-options))
  3829. (setq var (nth 1 l) val (nth 2 l))
  3830. (if (not (nth 3 l))
  3831. (set (make-local-variable var) val)
  3832. (if (not (listp (symbol-value var)))
  3833. (set (make-local-variable var) nil))
  3834. (set (make-local-variable var) (symbol-value var))
  3835. (add-to-list var val))))))
  3836. ((equal key "ARCHIVE")
  3837. (setq arch value)
  3838. (remove-text-properties 0 (length arch)
  3839. '(face t fontified t) arch))
  3840. ((equal key "LATEX_CLASS")
  3841. (setq beamer-p (equal value "beamer")))
  3842. ((equal key "OPTIONS")
  3843. (if (string-match "\\([ \t]\\|\\`\\)\\^:\\(t\\|nil\\|{}\\)" value)
  3844. (setq scripts (read (match-string 2 value)))))
  3845. ((equal key "SETUPFILE")
  3846. (setq setup-contents (org-file-contents
  3847. (expand-file-name
  3848. (org-remove-double-quotes value))
  3849. 'noerror))
  3850. (if (not ext-setup-or-nil)
  3851. (setq ext-setup-or-nil setup-contents start 0)
  3852. (setq ext-setup-or-nil
  3853. (concat (substring ext-setup-or-nil 0 start)
  3854. "\n" setup-contents "\n"
  3855. (substring ext-setup-or-nil start)))))
  3856. ))))
  3857. (org-set-local 'org-use-sub-superscripts scripts)
  3858. (when cat
  3859. (org-set-local 'org-category (intern cat))
  3860. (push (cons "CATEGORY" cat) props))
  3861. (when prio
  3862. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  3863. (setq prio (mapcar 'string-to-char prio))
  3864. (org-set-local 'org-highest-priority (nth 0 prio))
  3865. (org-set-local 'org-lowest-priority (nth 1 prio))
  3866. (org-set-local 'org-default-priority (nth 2 prio)))
  3867. (and props (org-set-local 'org-file-properties (nreverse props)))
  3868. (and ftags (org-set-local 'org-file-tags
  3869. (mapcar 'org-add-prop-inherited ftags)))
  3870. (and drawers (org-set-local 'org-drawers drawers))
  3871. (and arch (org-set-local 'org-archive-location arch))
  3872. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  3873. ;; Process the TODO keywords
  3874. (unless kwds
  3875. ;; Use the global values as if they had been given locally.
  3876. (setq kwds (default-value 'org-todo-keywords))
  3877. (if (stringp (car kwds))
  3878. (setq kwds (list (cons org-todo-interpretation
  3879. (default-value 'org-todo-keywords)))))
  3880. (setq kwds (reverse kwds)))
  3881. (setq kwds (nreverse kwds))
  3882. (let (inter kws kw)
  3883. (while (setq kws (pop kwds))
  3884. (let ((kws (or
  3885. (run-hook-with-args-until-success
  3886. 'org-todo-setup-filter-hook kws)
  3887. kws)))
  3888. (setq inter (pop kws) sep (member "|" kws)
  3889. kws0 (delete "|" (copy-sequence kws))
  3890. kwsa nil
  3891. kws1 (mapcar
  3892. (lambda (x)
  3893. ;; 1 2
  3894. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  3895. (progn
  3896. (setq kw (match-string 1 x)
  3897. key (and (match-end 2) (match-string 2 x))
  3898. log (org-extract-log-state-settings x))
  3899. (push (cons kw (and key (string-to-char key))) kwsa)
  3900. (and log (push log org-todo-log-states))
  3901. kw)
  3902. (error "Invalid TODO keyword %s" x)))
  3903. kws0)
  3904. kwsa (if kwsa (append '((:startgroup))
  3905. (nreverse kwsa)
  3906. '((:endgroup))))
  3907. hw (car kws1)
  3908. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  3909. tail (list inter hw (car dws) (org-last dws))))
  3910. (add-to-list 'org-todo-heads hw 'append)
  3911. (push kws1 org-todo-sets)
  3912. (setq org-done-keywords (append org-done-keywords dws nil))
  3913. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  3914. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  3915. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  3916. (setq org-todo-sets (nreverse org-todo-sets)
  3917. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  3918. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  3919. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  3920. ;; Process the constants
  3921. (when const
  3922. (let (e cst)
  3923. (while (setq e (pop const))
  3924. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3925. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3926. (setq org-table-formula-constants-local cst)))
  3927. ;; Process the tags.
  3928. (when tags
  3929. (let (e tgs)
  3930. (while (setq e (pop tags))
  3931. (cond
  3932. ((equal e "{") (push '(:startgroup) tgs))
  3933. ((equal e "}") (push '(:endgroup) tgs))
  3934. ((equal e "\\n") (push '(:newline) tgs))
  3935. ((string-match (org-re "^\\([[:alnum:]_@#%]+\\)(\\(.\\))$") e)
  3936. (push (cons (match-string 1 e)
  3937. (string-to-char (match-string 2 e)))
  3938. tgs))
  3939. (t (push (list e) tgs))))
  3940. (org-set-local 'org-tag-alist nil)
  3941. (while (setq e (pop tgs))
  3942. (or (and (stringp (car e))
  3943. (assoc (car e) org-tag-alist))
  3944. (push e org-tag-alist)))))
  3945. ;; Compute the regular expressions and other local variables
  3946. (if (not org-done-keywords)
  3947. (setq org-done-keywords (and org-todo-keywords-1
  3948. (list (org-last org-todo-keywords-1)))))
  3949. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3950. (length org-scheduled-string)
  3951. (length org-clock-string)
  3952. (length org-closed-string)))
  3953. org-drawer-regexp
  3954. (concat "^[ \t]*:\\("
  3955. (mapconcat 'regexp-quote org-drawers "\\|")
  3956. "\\):[ \t]*$")
  3957. org-not-done-keywords
  3958. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3959. org-todo-regexp
  3960. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3961. "\\|") "\\)\\>")
  3962. org-not-done-regexp
  3963. (concat "\\<\\("
  3964. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3965. "\\)\\>")
  3966. org-not-done-heading-regexp
  3967. (concat "^\\(\\*+\\)[ \t]+\\("
  3968. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3969. "\\)\\>")
  3970. org-todo-line-regexp
  3971. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3972. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3973. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3974. org-complex-heading-regexp
  3975. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3976. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3977. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3978. "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  3979. org-complex-heading-regexp-format
  3980. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3981. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3982. "\\)\\>\\)?"
  3983. "\\(?:[ \t]*\\(\\[#.\\]\\)\\)?"
  3984. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3985. "[ \t]*\\(%s\\)"
  3986. "\\(?:[ \t]*\\(?:\\[[0-9%%/]+\\]\\)\\)?" ;; stats cookie
  3987. "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?[ \t]*$")
  3988. org-nl-done-regexp
  3989. (concat "\n\\*+[ \t]+"
  3990. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3991. "\\)" "\\>")
  3992. org-todo-line-tags-regexp
  3993. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3994. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3995. (org-re
  3996. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@#%]+:[ \t]*\\)?$\\)"))
  3997. org-looking-at-done-regexp
  3998. (concat "^" "\\(?:"
  3999. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  4000. "\\>")
  4001. org-deadline-regexp (concat "\\<" org-deadline-string)
  4002. org-deadline-time-regexp
  4003. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  4004. org-deadline-line-regexp
  4005. (concat "\\<\\(" org-deadline-string "\\).*")
  4006. org-scheduled-regexp
  4007. (concat "\\<" org-scheduled-string)
  4008. org-scheduled-time-regexp
  4009. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  4010. org-closed-time-regexp
  4011. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  4012. org-keyword-time-regexp
  4013. (concat "\\<\\(" org-scheduled-string
  4014. "\\|" org-deadline-string
  4015. "\\|" org-closed-string
  4016. "\\|" org-clock-string "\\)"
  4017. " *[[<]\\([^]>]+\\)[]>]")
  4018. org-keyword-time-not-clock-regexp
  4019. (concat "\\<\\(" org-scheduled-string
  4020. "\\|" org-deadline-string
  4021. "\\|" org-closed-string
  4022. "\\)"
  4023. " *[[<]\\([^]>]+\\)[]>]")
  4024. org-maybe-keyword-time-regexp
  4025. (concat "\\(\\<\\(" org-scheduled-string
  4026. "\\|" org-deadline-string
  4027. "\\|" org-closed-string
  4028. "\\|" org-clock-string "\\)\\)?"
  4029. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  4030. org-planning-or-clock-line-re
  4031. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  4032. "\\|" org-deadline-string
  4033. "\\|" org-closed-string "\\|" org-clock-string
  4034. "\\)\\>\\)")
  4035. org-all-time-keywords
  4036. (mapcar (lambda (w) (substring w 0 -1))
  4037. (list org-scheduled-string org-deadline-string
  4038. org-clock-string org-closed-string))
  4039. )
  4040. (org-compute-latex-and-specials-regexp)
  4041. (org-set-font-lock-defaults))))
  4042. (defun org-file-contents (file &optional noerror)
  4043. "Return the contents of FILE, as a string."
  4044. (if (or (not file)
  4045. (not (file-readable-p file)))
  4046. (if noerror
  4047. (progn
  4048. (message "Cannot read file \"%s\"" file)
  4049. (ding) (sit-for 2)
  4050. "")
  4051. (error "Cannot read file \"%s\"" file))
  4052. (with-temp-buffer
  4053. (insert-file-contents file)
  4054. (buffer-string))))
  4055. (defun org-extract-log-state-settings (x)
  4056. "Extract the log state setting from a TODO keyword string.
  4057. This will extract info from a string like \"WAIT(w@/!)\"."
  4058. (let (kw key log1 log2)
  4059. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4060. (setq kw (match-string 1 x)
  4061. key (and (match-end 2) (match-string 2 x))
  4062. log1 (and (match-end 3) (match-string 3 x))
  4063. log2 (and (match-end 4) (match-string 4 x)))
  4064. (and (or log1 log2)
  4065. (list kw
  4066. (and log1 (if (equal log1 "!") 'time 'note))
  4067. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4068. (defun org-remove-keyword-keys (list)
  4069. "Remove a pair of parenthesis at the end of each string in LIST."
  4070. (mapcar (lambda (x)
  4071. (if (string-match "(.*)$" x)
  4072. (substring x 0 (match-beginning 0))
  4073. x))
  4074. list))
  4075. (defun org-assign-fast-keys (alist)
  4076. "Assign fast keys to a keyword-key alist.
  4077. Respect keys that are already there."
  4078. (let (new e (alt ?0))
  4079. (while (setq e (pop alist))
  4080. (if (or (memq (car e) '(:newline :endgroup :startgroup))
  4081. (cdr e)) ;; Key already assigned.
  4082. (push e new)
  4083. (let ((clist (string-to-list (downcase (car e))))
  4084. (used (append new alist)))
  4085. (when (= (car clist) ?@)
  4086. (pop clist))
  4087. (while (and clist (rassoc (car clist) used))
  4088. (pop clist))
  4089. (unless clist
  4090. (while (rassoc alt used)
  4091. (incf alt)))
  4092. (push (cons (car e) (or (car clist) alt)) new))))
  4093. (nreverse new)))
  4094. ;;; Some variables used in various places
  4095. (defvar org-window-configuration nil
  4096. "Used in various places to store a window configuration.")
  4097. (defvar org-selected-window nil
  4098. "Used in various places to store a window configuration.")
  4099. (defvar org-finish-function nil
  4100. "Function to be called when `C-c C-c' is used.
  4101. This is for getting out of special buffers like remember.")
  4102. ;; FIXME: Occasionally check by commenting these, to make sure
  4103. ;; no other functions uses these, forgetting to let-bind them.
  4104. (defvar entry)
  4105. (defvar last-state)
  4106. (defvar date)
  4107. ;; Defined somewhere in this file, but used before definition.
  4108. (defvar org-entities) ;; defined in org-entities.el
  4109. (defvar org-struct-menu)
  4110. (defvar org-org-menu)
  4111. (defvar org-tbl-menu)
  4112. ;;;; Define the Org-mode
  4113. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4114. (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"))
  4115. ;; We use a before-change function to check if a table might need
  4116. ;; an update.
  4117. (defvar org-table-may-need-update t
  4118. "Indicates that a table might need an update.
  4119. This variable is set by `org-before-change-function'.
  4120. `org-table-align' sets it back to nil.")
  4121. (defun org-before-change-function (beg end)
  4122. "Every change indicates that a table might need an update."
  4123. (setq org-table-may-need-update t))
  4124. (defvar org-mode-map)
  4125. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4126. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4127. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4128. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4129. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4130. (defvar org-table-buffer-is-an nil)
  4131. (defconst org-outline-regexp "\\*+ ")
  4132. ;;;###autoload
  4133. (define-derived-mode org-mode outline-mode "Org"
  4134. "Outline-based notes management and organizer, alias
  4135. \"Carsten's outline-mode for keeping track of everything.\"
  4136. Org-mode develops organizational tasks around a NOTES file which
  4137. contains information about projects as plain text. Org-mode is
  4138. implemented on top of outline-mode, which is ideal to keep the content
  4139. of large files well structured. It supports ToDo items, deadlines and
  4140. time stamps, which magically appear in the diary listing of the Emacs
  4141. calendar. Tables are easily created with a built-in table editor.
  4142. Plain text URL-like links connect to websites, emails (VM), Usenet
  4143. messages (Gnus), BBDB entries, and any files related to the project.
  4144. For printing and sharing of notes, an Org-mode file (or a part of it)
  4145. can be exported as a structured ASCII or HTML file.
  4146. The following commands are available:
  4147. \\{org-mode-map}"
  4148. ;; Get rid of Outline menus, they are not needed
  4149. ;; Need to do this here because define-derived-mode sets up
  4150. ;; the keymap so late. Still, it is a waste to call this each time
  4151. ;; we switch another buffer into org-mode.
  4152. (if (featurep 'xemacs)
  4153. (when (boundp 'outline-mode-menu-heading)
  4154. ;; Assume this is Greg's port, it uses easymenu
  4155. (easy-menu-remove outline-mode-menu-heading)
  4156. (easy-menu-remove outline-mode-menu-show)
  4157. (easy-menu-remove outline-mode-menu-hide))
  4158. (define-key org-mode-map [menu-bar headings] 'undefined)
  4159. (define-key org-mode-map [menu-bar hide] 'undefined)
  4160. (define-key org-mode-map [menu-bar show] 'undefined))
  4161. (org-load-modules-maybe)
  4162. (easy-menu-add org-org-menu)
  4163. (easy-menu-add org-tbl-menu)
  4164. (org-install-agenda-files-menu)
  4165. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4166. (add-to-invisibility-spec '(org-cwidth))
  4167. (add-to-invisibility-spec '(org-hide-block . t))
  4168. (when (featurep 'xemacs)
  4169. (org-set-local 'line-move-ignore-invisible t))
  4170. (org-set-local 'outline-regexp org-outline-regexp)
  4171. (org-set-local 'outline-level 'org-outline-level)
  4172. (when (and org-ellipsis
  4173. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4174. (fboundp 'make-glyph-code))
  4175. (unless org-display-table
  4176. (setq org-display-table (make-display-table)))
  4177. (set-display-table-slot
  4178. org-display-table 4
  4179. (vconcat (mapcar
  4180. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4181. org-ellipsis)))
  4182. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4183. (setq buffer-display-table org-display-table))
  4184. (org-set-regexps-and-options)
  4185. (when (and org-tag-faces (not org-tags-special-faces-re))
  4186. ;; tag faces set outside customize.... force initialization.
  4187. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4188. ;; Calc embedded
  4189. (org-set-local 'calc-embedded-open-mode "# ")
  4190. (modify-syntax-entry ?@ "w")
  4191. (if org-startup-truncated (setq truncate-lines t))
  4192. (org-set-local 'font-lock-unfontify-region-function
  4193. 'org-unfontify-region)
  4194. ;; Activate before-change-function
  4195. (org-set-local 'org-table-may-need-update t)
  4196. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4197. 'local)
  4198. ;; Check for running clock before killing a buffer
  4199. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4200. ;; Paragraphs and auto-filling
  4201. (org-set-autofill-regexps)
  4202. (setq indent-line-function 'org-indent-line-function)
  4203. (org-update-radio-target-regexp)
  4204. ;; Beginning/end of defun
  4205. (org-set-local 'beginning-of-defun-function 'org-beginning-of-defun)
  4206. (org-set-local 'end-of-defun-function 'org-end-of-defun)
  4207. ;; Next error for sparse trees
  4208. (org-set-local 'next-error-function 'org-occur-next-match)
  4209. ;; Make sure dependence stuff works reliably, even for users who set it
  4210. ;; too late :-(
  4211. (if org-enforce-todo-dependencies
  4212. (add-hook 'org-blocker-hook
  4213. 'org-block-todo-from-children-or-siblings-or-parent)
  4214. (remove-hook 'org-blocker-hook
  4215. 'org-block-todo-from-children-or-siblings-or-parent))
  4216. (if org-enforce-todo-checkbox-dependencies
  4217. (add-hook 'org-blocker-hook
  4218. 'org-block-todo-from-checkboxes)
  4219. (remove-hook 'org-blocker-hook
  4220. 'org-block-todo-from-checkboxes))
  4221. ;; Comment characters
  4222. (org-set-local 'comment-start "#")
  4223. (org-set-local 'comment-padding " ")
  4224. ;; Align options lines
  4225. (org-set-local
  4226. 'align-mode-rules-list
  4227. '((org-in-buffer-settings
  4228. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4229. (modes . '(org-mode)))))
  4230. ;; Imenu
  4231. (org-set-local 'imenu-create-index-function
  4232. 'org-imenu-get-tree)
  4233. ;; Make isearch reveal context
  4234. (if (or (featurep 'xemacs)
  4235. (not (boundp 'outline-isearch-open-invisible-function)))
  4236. ;; Emacs 21 and XEmacs make use of the hook
  4237. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4238. ;; Emacs 22 deals with this through a special variable
  4239. (org-set-local 'outline-isearch-open-invisible-function
  4240. (lambda (&rest ignore) (org-show-context 'isearch))))
  4241. ;; Turn on org-beamer-mode?
  4242. (and org-startup-with-beamer-mode (org-beamer-mode 1))
  4243. ;; Setup the pcomplete hooks
  4244. (set (make-local-variable 'pcomplete-command-completion-function)
  4245. 'org-pcomplete-initial)
  4246. (set (make-local-variable 'pcomplete-command-name-function)
  4247. 'org-command-at-point)
  4248. (set (make-local-variable 'pcomplete-default-completion-function)
  4249. 'ignore)
  4250. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4251. 'org-parse-arguments)
  4252. (set (make-local-variable 'pcomplete-termination-string) "")
  4253. ;; If empty file that did not turn on org-mode automatically, make it to.
  4254. (if (and org-insert-mode-line-in-empty-file
  4255. (interactive-p)
  4256. (= (point-min) (point-max)))
  4257. (insert "# -*- mode: org -*-\n\n"))
  4258. (unless org-inhibit-startup
  4259. (when org-startup-align-all-tables
  4260. (let ((bmp (buffer-modified-p)))
  4261. (org-table-map-tables 'org-table-align 'quietly)
  4262. (set-buffer-modified-p bmp)))
  4263. (when org-startup-with-inline-images
  4264. (org-display-inline-images))
  4265. (when org-startup-indented
  4266. (require 'org-indent)
  4267. (org-indent-mode 1))
  4268. (unless org-inhibit-startup-visibility-stuff
  4269. (org-set-startup-visibility))))
  4270. (when (fboundp 'abbrev-table-put)
  4271. (abbrev-table-put org-mode-abbrev-table
  4272. :parents (list text-mode-abbrev-table)))
  4273. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  4274. (defun org-current-time ()
  4275. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  4276. (if (> (car org-time-stamp-rounding-minutes) 1)
  4277. (let ((r (car org-time-stamp-rounding-minutes))
  4278. (time (decode-time)))
  4279. (apply 'encode-time
  4280. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4281. (nthcdr 2 time))))
  4282. (current-time)))
  4283. (defun org-today ()
  4284. "Return today date, considering `org-extend-today-until'."
  4285. (time-to-days
  4286. (time-subtract (current-time)
  4287. (list 0 (* 3600 org-extend-today-until) 0))))
  4288. ;;;; Font-Lock stuff, including the activators
  4289. (defvar org-mouse-map (make-sparse-keymap))
  4290. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4291. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4292. (when org-mouse-1-follows-link
  4293. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4294. (when org-tab-follows-link
  4295. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4296. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4297. (require 'font-lock)
  4298. (defconst org-non-link-chars "]\t\n\r<>")
  4299. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  4300. "shell" "elisp" "doi" "message"))
  4301. (defvar org-link-types-re nil
  4302. "Matches a link that has a url-like prefix like \"http:\"")
  4303. (defvar org-link-re-with-space nil
  4304. "Matches a link with spaces, optional angular brackets around it.")
  4305. (defvar org-link-re-with-space2 nil
  4306. "Matches a link with spaces, optional angular brackets around it.")
  4307. (defvar org-link-re-with-space3 nil
  4308. "Matches a link with spaces, only for internal part in bracket links.")
  4309. (defvar org-angle-link-re nil
  4310. "Matches link with angular brackets, spaces are allowed.")
  4311. (defvar org-plain-link-re nil
  4312. "Matches plain link, without spaces.")
  4313. (defvar org-bracket-link-regexp nil
  4314. "Matches a link in double brackets.")
  4315. (defvar org-bracket-link-analytic-regexp nil
  4316. "Regular expression used to analyze links.
  4317. Here is what the match groups contain after a match:
  4318. 1: http:
  4319. 2: http
  4320. 3: path
  4321. 4: [desc]
  4322. 5: desc")
  4323. (defvar org-bracket-link-analytic-regexp++ nil
  4324. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4325. (defvar org-any-link-re nil
  4326. "Regular expression matching any link.")
  4327. (defcustom org-match-sexp-depth 3
  4328. "Number of stacked braces for sub/superscript matching.
  4329. This has to be set before loading org.el to be effective."
  4330. :group 'org-export-translation ; ??????????????????????????/
  4331. :type 'integer)
  4332. (defun org-create-multibrace-regexp (left right n)
  4333. "Create a regular expression which will match a balanced sexp.
  4334. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4335. as single character strings.
  4336. The regexp returned will match the entire expression including the
  4337. delimiters. It will also define a single group which contains the
  4338. match except for the outermost delimiters. The maximum depth of
  4339. stacked delimiters is N. Escaping delimiters is not possible."
  4340. (let* ((nothing (concat "[^" left right "]*?"))
  4341. (or "\\|")
  4342. (re nothing)
  4343. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4344. (while (> n 1)
  4345. (setq n (1- n)
  4346. re (concat re or next)
  4347. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4348. (concat left "\\(" re "\\)" right)))
  4349. (defvar org-match-substring-regexp
  4350. (concat
  4351. "\\([^\\]\\)\\([_^]\\)\\("
  4352. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4353. "\\|"
  4354. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4355. "\\|"
  4356. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  4357. "The regular expression matching a sub- or superscript.")
  4358. (defvar org-match-substring-with-braces-regexp
  4359. (concat
  4360. "\\([^\\]\\)\\([_^]\\)\\("
  4361. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4362. "\\)")
  4363. "The regular expression matching a sub- or superscript, forcing braces.")
  4364. (defun org-make-link-regexps ()
  4365. "Update the link regular expressions.
  4366. This should be called after the variable `org-link-types' has changed."
  4367. (setq org-link-types-re
  4368. (concat
  4369. "\\`\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):")
  4370. org-link-re-with-space
  4371. (concat
  4372. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4373. "\\([^" org-non-link-chars " ]"
  4374. "[^" org-non-link-chars "]*"
  4375. "[^" org-non-link-chars " ]\\)>?")
  4376. org-link-re-with-space2
  4377. (concat
  4378. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4379. "\\([^" org-non-link-chars " ]"
  4380. "[^\t\n\r]*"
  4381. "[^" org-non-link-chars " ]\\)>?")
  4382. org-link-re-with-space3
  4383. (concat
  4384. "<?\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4385. "\\([^" org-non-link-chars " ]"
  4386. "[^\t\n\r]*\\)")
  4387. org-angle-link-re
  4388. (concat
  4389. "<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4390. "\\([^" org-non-link-chars " ]"
  4391. "[^" org-non-link-chars "]*"
  4392. "\\)>")
  4393. org-plain-link-re
  4394. (concat
  4395. "\\<\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):"
  4396. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4397. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4398. org-bracket-link-regexp
  4399. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4400. org-bracket-link-analytic-regexp
  4401. (concat
  4402. "\\[\\["
  4403. "\\(\\(" (mapconcat 'regexp-quote org-link-types "\\|") "\\):\\)?"
  4404. "\\([^]]+\\)"
  4405. "\\]"
  4406. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4407. "\\]")
  4408. org-bracket-link-analytic-regexp++
  4409. (concat
  4410. "\\[\\["
  4411. "\\(\\(" (mapconcat 'regexp-quote (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  4412. "\\([^]]+\\)"
  4413. "\\]"
  4414. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4415. "\\]")
  4416. org-any-link-re
  4417. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4418. org-angle-link-re "\\)\\|\\("
  4419. org-plain-link-re "\\)")))
  4420. (org-make-link-regexps)
  4421. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  4422. "Regular expression for fast time stamp matching.")
  4423. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?\\)[]>]"
  4424. "Regular expression for fast time stamp matching.")
  4425. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4426. "Regular expression matching time strings for analysis.
  4427. This one does not require the space after the date, so it can be used
  4428. on a string that terminates immediately after the date.")
  4429. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  4430. "Regular expression matching time strings for analysis.")
  4431. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  4432. "Regular expression matching time stamps, with groups.")
  4433. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  4434. "Regular expression matching time stamps (also [..]), with groups.")
  4435. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  4436. "Regular expression matching a time stamp range.")
  4437. (defconst org-tr-regexp-both
  4438. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  4439. "Regular expression matching a time stamp range.")
  4440. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  4441. org-ts-regexp "\\)?")
  4442. "Regular expression matching a time stamp or time stamp range.")
  4443. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  4444. org-ts-regexp-both "\\)?")
  4445. "Regular expression matching a time stamp or time stamp range.
  4446. The time stamps may be either active or inactive.")
  4447. (defvar org-emph-face nil)
  4448. (defun org-do-emphasis-faces (limit)
  4449. "Run through the buffer and add overlays to links."
  4450. (let (rtn a)
  4451. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4452. (if (not (= (char-after (match-beginning 3))
  4453. (char-after (match-beginning 4))))
  4454. (progn
  4455. (setq rtn t)
  4456. (setq a (assoc (match-string 3) org-emphasis-alist))
  4457. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4458. 'face
  4459. (nth 1 a))
  4460. (and (nth 4 a)
  4461. (org-remove-flyspell-overlays-in
  4462. (match-beginning 0) (match-end 0)))
  4463. (add-text-properties (match-beginning 2) (match-end 2)
  4464. '(font-lock-multiline t org-emphasis t))
  4465. (when org-hide-emphasis-markers
  4466. (add-text-properties (match-end 4) (match-beginning 5)
  4467. '(invisible org-link))
  4468. (add-text-properties (match-beginning 3) (match-end 3)
  4469. '(invisible org-link)))))
  4470. (backward-char 1))
  4471. rtn))
  4472. (defun org-emphasize (&optional char)
  4473. "Insert or change an emphasis, i.e. a font like bold or italic.
  4474. If there is an active region, change that region to a new emphasis.
  4475. If there is no region, just insert the marker characters and position
  4476. the cursor between them.
  4477. CHAR should be either the marker character, or the first character of the
  4478. HTML tag associated with that emphasis. If CHAR is a space, the means
  4479. to remove the emphasis of the selected region.
  4480. If char is not given (for example in an interactive call) it
  4481. will be prompted for."
  4482. (interactive)
  4483. (let ((eal org-emphasis-alist) e det
  4484. (erc org-emphasis-regexp-components)
  4485. (prompt "")
  4486. (string "") beg end move tag c s)
  4487. (if (org-region-active-p)
  4488. (setq beg (region-beginning) end (region-end)
  4489. string (buffer-substring beg end))
  4490. (setq move t))
  4491. (while (setq e (pop eal))
  4492. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  4493. c (aref tag 0))
  4494. (push (cons c (string-to-char (car e))) det)
  4495. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  4496. (substring tag 1)))))
  4497. (setq det (nreverse det))
  4498. (unless char
  4499. (message "%s" (concat "Emphasis marker or tag:" prompt))
  4500. (setq char (read-char-exclusive)))
  4501. (setq char (or (cdr (assoc char det)) char))
  4502. (if (equal char ?\ )
  4503. (setq s "" move nil)
  4504. (unless (assoc (char-to-string char) org-emphasis-alist)
  4505. (error "No such emphasis marker: \"%c\"" char))
  4506. (setq s (char-to-string char)))
  4507. (while (and (> (length string) 1)
  4508. (equal (substring string 0 1) (substring string -1))
  4509. (assoc (substring string 0 1) org-emphasis-alist))
  4510. (setq string (substring string 1 -1)))
  4511. (setq string (concat s string s))
  4512. (if beg (delete-region beg end))
  4513. (unless (or (bolp)
  4514. (string-match (concat "[" (nth 0 erc) "\n]")
  4515. (char-to-string (char-before (point)))))
  4516. (insert " "))
  4517. (unless (or (eobp)
  4518. (string-match (concat "[" (nth 1 erc) "\n]")
  4519. (char-to-string (char-after (point)))))
  4520. (insert " ") (backward-char 1))
  4521. (insert string)
  4522. (and move (backward-char 1))))
  4523. (defconst org-nonsticky-props
  4524. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  4525. (defsubst org-rear-nonsticky-at (pos)
  4526. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  4527. (defun org-activate-plain-links (limit)
  4528. "Run through the buffer and add overlays to links."
  4529. (catch 'exit
  4530. (let (f)
  4531. (if (re-search-forward org-plain-link-re limit t)
  4532. (progn
  4533. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4534. (setq f (get-text-property (match-beginning 0) 'face))
  4535. (if (or (eq f 'org-tag)
  4536. (and (listp f) (memq 'org-tag f)))
  4537. nil
  4538. (add-text-properties (match-beginning 0) (match-end 0)
  4539. (list 'mouse-face 'highlight
  4540. 'face 'org-link
  4541. 'keymap org-mouse-map))
  4542. (org-rear-nonsticky-at (match-end 0)))
  4543. t)))))
  4544. (defun org-activate-code (limit)
  4545. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  4546. (progn
  4547. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4548. (remove-text-properties (match-beginning 0) (match-end 0)
  4549. '(display t invisible t intangible t))
  4550. t)))
  4551. (defcustom org-src-fontify-natively nil
  4552. "When non-nil, fontify code in code blocks."
  4553. :type 'boolean
  4554. :group 'org-appearance
  4555. :group 'org-babel)
  4556. (defun org-fontify-meta-lines-and-blocks (limit)
  4557. "Fontify #+ lines and blocks, in the correct ways."
  4558. (let ((case-fold-search t))
  4559. (if (re-search-forward
  4560. "^\\([ \t]*#\\+\\(\\([a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  4561. limit t)
  4562. (let ((beg (match-beginning 0))
  4563. (block-start (match-end 0))
  4564. (block-end nil)
  4565. (lang (match-string 7))
  4566. (beg1 (line-beginning-position 2))
  4567. (dc1 (downcase (match-string 2)))
  4568. (dc3 (downcase (match-string 3)))
  4569. end end1 quoting block-type)
  4570. (cond
  4571. ((member dc1 '("html:" "ascii:" "latex:" "docbook:"))
  4572. ;; a single line of backend-specific content
  4573. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4574. (remove-text-properties (match-beginning 0) (match-end 0)
  4575. '(display t invisible t intangible t))
  4576. (add-text-properties (match-beginning 1) (match-end 3)
  4577. '(font-lock-fontified t face org-meta-line))
  4578. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  4579. '(font-lock-fontified t face org-block))
  4580. ; for backend-specific code
  4581. t)
  4582. ((and (match-end 4) (equal dc3 "begin"))
  4583. ;; Truly a block
  4584. (setq block-type (downcase (match-string 5))
  4585. quoting (member block-type org-protecting-blocks))
  4586. (when (re-search-forward
  4587. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  4588. nil t) ;; on purpose, we look further than LIMIT
  4589. (setq end (match-end 0) end1 (1- (match-beginning 0)))
  4590. (setq block-end (match-beginning 0))
  4591. (when quoting
  4592. (remove-text-properties beg end
  4593. '(display t invisible t intangible t)))
  4594. (add-text-properties
  4595. beg end
  4596. '(font-lock-fontified t font-lock-multiline t))
  4597. (add-text-properties beg beg1 '(face org-meta-line))
  4598. (add-text-properties end1 (+ end 1) '(face org-meta-line))
  4599. ; for end_src
  4600. (cond
  4601. ((and lang org-src-fontify-natively)
  4602. (org-src-font-lock-fontify-block lang block-start block-end)
  4603. (overlay-put (make-overlay beg1 block-end)
  4604. 'face 'org-block-background))
  4605. (quoting
  4606. (add-text-properties beg1 (+ end1 1) '(face org-block)))
  4607. ; end of source block
  4608. ((not org-fontify-quote-and-verse-blocks))
  4609. ((string= block-type "quote")
  4610. (add-text-properties beg1 (1+ end1) '(face org-quote)))
  4611. ((string= block-type "verse")
  4612. (add-text-properties beg1 (1+ end1) '(face org-verse))))
  4613. (add-text-properties beg beg1 '(face org-block-begin-line))
  4614. (add-text-properties (1+ end) (1+ end1) '(face org-block-end-line))
  4615. t))
  4616. ((member dc1 '("title:" "author:" "email:" "date:"))
  4617. (add-text-properties
  4618. beg (match-end 3)
  4619. (if (member (intern (substring dc1 0 -1)) org-hidden-keywords)
  4620. '(font-lock-fontified t invisible t)
  4621. '(font-lock-fontified t face org-document-info-keyword)))
  4622. (add-text-properties
  4623. (match-beginning 6) (match-end 6)
  4624. (if (string-equal dc1 "title:")
  4625. '(font-lock-fontified t face org-document-title)
  4626. '(font-lock-fontified t face org-document-info))))
  4627. ((not (member (char-after beg) '(?\ ?\t)))
  4628. ;; just any other in-buffer setting, but not indented
  4629. (add-text-properties
  4630. beg (1+ (match-end 0))
  4631. '(font-lock-fontified t face org-meta-line))
  4632. t)
  4633. ((or (member dc1 '("begin:" "end:" "caption:" "label:"
  4634. "orgtbl:" "tblfm:" "tblname:" "result:"
  4635. "results:" "source:" "srcname:" "call:"))
  4636. (and (match-end 4) (equal dc3 "attr")))
  4637. (add-text-properties
  4638. beg (match-end 0)
  4639. '(font-lock-fontified t face org-meta-line))
  4640. t)
  4641. ((member dc3 '(" " ""))
  4642. (add-text-properties
  4643. beg (match-end 0)
  4644. '(font-lock-fontified t face font-lock-comment-face)))
  4645. (t nil))))))
  4646. (defun org-activate-angle-links (limit)
  4647. "Run through the buffer and add overlays to links."
  4648. (if (re-search-forward org-angle-link-re limit t)
  4649. (progn
  4650. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4651. (add-text-properties (match-beginning 0) (match-end 0)
  4652. (list 'mouse-face 'highlight
  4653. 'keymap org-mouse-map))
  4654. (org-rear-nonsticky-at (match-end 0))
  4655. t)))
  4656. (defun org-activate-footnote-links (limit)
  4657. "Run through the buffer and add overlays to links."
  4658. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  4659. limit t)
  4660. (progn
  4661. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4662. (add-text-properties (match-beginning 2) (match-end 2)
  4663. (list 'mouse-face 'highlight
  4664. 'keymap org-mouse-map
  4665. 'help-echo
  4666. (if (= (point-at-bol) (match-beginning 2))
  4667. "Footnote definition"
  4668. "Footnote reference")
  4669. ))
  4670. (org-rear-nonsticky-at (match-end 2))
  4671. t)))
  4672. (defun org-activate-bracket-links (limit)
  4673. "Run through the buffer and add overlays to bracketed links."
  4674. (if (re-search-forward org-bracket-link-regexp limit t)
  4675. (let* ((help (concat "LINK: "
  4676. (org-match-string-no-properties 1)))
  4677. ;; FIXME: above we should remove the escapes.
  4678. ;; but that requires another match, protecting match data,
  4679. ;; a lot of overhead for font-lock.
  4680. (ip (org-maybe-intangible
  4681. (list 'invisible 'org-link
  4682. 'keymap org-mouse-map 'mouse-face 'highlight
  4683. 'font-lock-multiline t 'help-echo help)))
  4684. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  4685. 'font-lock-multiline t 'help-echo help)))
  4686. ;; We need to remove the invisible property here. Table narrowing
  4687. ;; may have made some of this invisible.
  4688. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4689. (remove-text-properties (match-beginning 0) (match-end 0)
  4690. '(invisible nil))
  4691. (if (match-end 3)
  4692. (progn
  4693. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  4694. (org-rear-nonsticky-at (match-beginning 3))
  4695. (add-text-properties (match-beginning 3) (match-end 3) vp)
  4696. (org-rear-nonsticky-at (match-end 3))
  4697. (add-text-properties (match-end 3) (match-end 0) ip)
  4698. (org-rear-nonsticky-at (match-end 0)))
  4699. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  4700. (org-rear-nonsticky-at (match-beginning 1))
  4701. (add-text-properties (match-beginning 1) (match-end 1) vp)
  4702. (org-rear-nonsticky-at (match-end 1))
  4703. (add-text-properties (match-end 1) (match-end 0) ip)
  4704. (org-rear-nonsticky-at (match-end 0)))
  4705. t)))
  4706. (defun org-activate-dates (limit)
  4707. "Run through the buffer and add overlays to dates."
  4708. (if (re-search-forward org-tsr-regexp-both limit t)
  4709. (progn
  4710. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4711. (add-text-properties (match-beginning 0) (match-end 0)
  4712. (list 'mouse-face 'highlight
  4713. 'keymap org-mouse-map))
  4714. (org-rear-nonsticky-at (match-end 0))
  4715. (when org-display-custom-times
  4716. (if (match-end 3)
  4717. (org-display-custom-time (match-beginning 3) (match-end 3)))
  4718. (org-display-custom-time (match-beginning 1) (match-end 1)))
  4719. t)))
  4720. (defvar org-target-link-regexp nil
  4721. "Regular expression matching radio targets in plain text.")
  4722. (make-variable-buffer-local 'org-target-link-regexp)
  4723. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  4724. "Regular expression matching a link target.")
  4725. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  4726. "Regular expression matching a radio target.")
  4727. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  4728. "Regular expression matching any target.")
  4729. (defun org-activate-target-links (limit)
  4730. "Run through the buffer and add overlays to target matches."
  4731. (when org-target-link-regexp
  4732. (let ((case-fold-search t))
  4733. (if (re-search-forward org-target-link-regexp limit t)
  4734. (progn
  4735. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  4736. (add-text-properties (match-beginning 0) (match-end 0)
  4737. (list 'mouse-face 'highlight
  4738. 'keymap org-mouse-map
  4739. 'help-echo "Radio target link"
  4740. 'org-linked-text t))
  4741. (org-rear-nonsticky-at (match-end 0))
  4742. t)))))
  4743. (defun org-update-radio-target-regexp ()
  4744. "Find all radio targets in this file and update the regular expression."
  4745. (interactive)
  4746. (when (memq 'radio org-activate-links)
  4747. (setq org-target-link-regexp
  4748. (org-make-target-link-regexp (org-all-targets 'radio)))
  4749. (org-restart-font-lock)))
  4750. (defun org-hide-wide-columns (limit)
  4751. (let (s e)
  4752. (setq s (text-property-any (point) (or limit (point-max))
  4753. 'org-cwidth t))
  4754. (when s
  4755. (setq e (next-single-property-change s 'org-cwidth))
  4756. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  4757. (goto-char e)
  4758. t)))
  4759. (defvar org-latex-and-specials-regexp nil
  4760. "Regular expression for highlighting export special stuff.")
  4761. (defvar org-match-substring-regexp)
  4762. (defvar org-match-substring-with-braces-regexp)
  4763. ;; This should be with the exporter code, but we also use if for font-locking
  4764. (defconst org-export-html-special-string-regexps
  4765. '(("\\\\-" . "&shy;")
  4766. ("---\\([^-]\\)" . "&mdash;\\1")
  4767. ("--\\([^-]\\)" . "&ndash;\\1")
  4768. ("\\.\\.\\." . "&hellip;"))
  4769. "Regular expressions for special string conversion.")
  4770. (defun org-compute-latex-and-specials-regexp ()
  4771. "Compute regular expression for stuff treated specially by exporters."
  4772. (if (not org-highlight-latex-fragments-and-specials)
  4773. (org-set-local 'org-latex-and-specials-regexp nil)
  4774. (require 'org-exp)
  4775. (let*
  4776. ((matchers (plist-get org-format-latex-options :matchers))
  4777. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  4778. org-latex-regexps)))
  4779. (org-export-allow-BIND nil)
  4780. (options (org-combine-plists (org-default-export-plist)
  4781. (org-infile-export-plist)))
  4782. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  4783. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  4784. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  4785. (org-export-html-expand (plist-get options :expand-quoted-html))
  4786. (org-export-with-special-strings (plist-get options :special-strings))
  4787. (re-sub
  4788. (cond
  4789. ((equal org-export-with-sub-superscripts '{})
  4790. (list org-match-substring-with-braces-regexp))
  4791. (org-export-with-sub-superscripts
  4792. (list org-match-substring-regexp))
  4793. (t nil)))
  4794. (re-latex
  4795. (if org-export-with-LaTeX-fragments
  4796. (mapcar (lambda (x) (nth 1 x)) latexs)))
  4797. (re-macros
  4798. (if org-export-with-TeX-macros
  4799. (list (concat "\\\\"
  4800. (regexp-opt
  4801. (append
  4802. (delq nil
  4803. (mapcar 'car-safe
  4804. (append org-entities-user
  4805. org-entities)))
  4806. (if (boundp 'org-latex-entities)
  4807. (mapcar (lambda (x)
  4808. (or (car-safe x) x))
  4809. org-latex-entities)
  4810. nil))
  4811. 'words))) ; FIXME
  4812. ))
  4813. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  4814. (re-special (if org-export-with-special-strings
  4815. (mapcar (lambda (x) (car x))
  4816. org-export-html-special-string-regexps)))
  4817. (re-rest
  4818. (delq nil
  4819. (list
  4820. (if org-export-html-expand "@<[^>\n]+>")
  4821. ))))
  4822. (org-set-local
  4823. 'org-latex-and-specials-regexp
  4824. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  4825. re-rest) "\\|")))))
  4826. (defun org-do-latex-and-special-faces (limit)
  4827. "Run through the buffer and add overlays to links."
  4828. (when org-latex-and-specials-regexp
  4829. (let (rtn d)
  4830. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  4831. limit t))
  4832. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  4833. 'face))
  4834. '(org-code org-verbatim underline)))
  4835. (progn
  4836. (setq rtn t
  4837. d (cond ((member (char-after (1+ (match-beginning 0)))
  4838. '(?_ ?^)) 1)
  4839. (t 0)))
  4840. (font-lock-prepend-text-property
  4841. (+ d (match-beginning 0)) (match-end 0)
  4842. 'face 'org-latex-and-export-specials)
  4843. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  4844. '(font-lock-multiline t)))))
  4845. rtn)))
  4846. (defun org-restart-font-lock ()
  4847. "Restart `font-lock-mode', to force refontification."
  4848. (when (and (boundp 'font-lock-mode) font-lock-mode)
  4849. (font-lock-mode -1)
  4850. (font-lock-mode 1)))
  4851. (defun org-all-targets (&optional radio)
  4852. "Return a list of all targets in this file.
  4853. With optional argument RADIO, only find radio targets."
  4854. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  4855. rtn)
  4856. (save-excursion
  4857. (goto-char (point-min))
  4858. (while (re-search-forward re nil t)
  4859. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  4860. rtn)))
  4861. (defun org-make-target-link-regexp (targets)
  4862. "Make regular expression matching all strings in TARGETS.
  4863. The regular expression finds the targets also if there is a line break
  4864. between words."
  4865. (and targets
  4866. (concat
  4867. "\\<\\("
  4868. (mapconcat
  4869. (lambda (x)
  4870. (setq x (regexp-quote x))
  4871. (while (string-match " +" x)
  4872. (setq x (replace-match "\\s-+" t t x)))
  4873. x)
  4874. targets
  4875. "\\|")
  4876. "\\)\\>")))
  4877. (defun org-activate-tags (limit)
  4878. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  4879. (progn
  4880. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  4881. (add-text-properties (match-beginning 1) (match-end 1)
  4882. (list 'mouse-face 'highlight
  4883. 'keymap org-mouse-map))
  4884. (org-rear-nonsticky-at (match-end 1))
  4885. t)))
  4886. (defun org-outline-level ()
  4887. "Compute the outline level of the heading at point.
  4888. This function assumes that the cursor is at the beginning of a line matched
  4889. by `outline-regexp'. Otherwise it returns garbage.
  4890. If this is called at a normal headline, the level is the number of stars.
  4891. Use `org-reduced-level' to remove the effect of `org-odd-levels'."
  4892. (save-excursion
  4893. (looking-at outline-regexp)
  4894. (1- (- (match-end 0) (match-beginning 0)))))
  4895. (defvar org-font-lock-keywords nil)
  4896. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  4897. "Regular expression matching a property line.")
  4898. (defvar org-font-lock-hook nil
  4899. "Functions to be called for special font lock stuff.")
  4900. (defvar org-font-lock-set-keywords-hook nil
  4901. "Functions that can manipulate `org-font-lock-extra-keywords'.
  4902. This is calles after `org-font-lock-extra-keywords' is defined, but before
  4903. it is installed to be used by font lock. This can be useful if something
  4904. needs to be inserted at a specific position in the font-lock sequence.")
  4905. (defun org-font-lock-hook (limit)
  4906. (run-hook-with-args 'org-font-lock-hook limit))
  4907. (defun org-set-font-lock-defaults ()
  4908. (let* ((em org-fontify-emphasized-text)
  4909. (lk org-activate-links)
  4910. (org-font-lock-extra-keywords
  4911. (list
  4912. ;; Call the hook
  4913. '(org-font-lock-hook)
  4914. ;; Headlines
  4915. `(,(if org-fontify-whole-heading-line
  4916. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  4917. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  4918. (1 (org-get-level-face 1))
  4919. (2 (org-get-level-face 2))
  4920. (3 (org-get-level-face 3)))
  4921. ;; Table lines
  4922. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  4923. (1 'org-table t))
  4924. ;; Table internals
  4925. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  4926. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  4927. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  4928. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  4929. ;; Drawers
  4930. (list org-drawer-regexp '(0 'org-special-keyword t))
  4931. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  4932. ;; Properties
  4933. (list org-property-re
  4934. '(1 'org-special-keyword t)
  4935. '(3 'org-property-value t))
  4936. ;; Links
  4937. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  4938. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  4939. (if (memq 'plain lk) '(org-activate-plain-links))
  4940. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  4941. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  4942. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  4943. (if (memq 'footnote lk) '(org-activate-footnote-links
  4944. (2 'org-footnote t)))
  4945. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  4946. '(org-hide-wide-columns (0 nil append))
  4947. ;; TODO lines
  4948. (list (concat "^\\*+[ \t]+" org-todo-regexp "\\([ \t]\\|$\\)")
  4949. '(1 (org-get-todo-face 1) t))
  4950. ;; DONE
  4951. (if org-fontify-done-headline
  4952. (list (concat "^[*]+ +\\<\\("
  4953. (mapconcat 'regexp-quote org-done-keywords "\\|")
  4954. "\\)\\(.*\\)")
  4955. '(2 'org-headline-done t))
  4956. nil)
  4957. ;; Priorities
  4958. '(org-font-lock-add-priority-faces)
  4959. ;; Tags
  4960. '(org-font-lock-add-tag-faces)
  4961. ;; Special keywords
  4962. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  4963. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  4964. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  4965. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  4966. ;; Emphasis
  4967. (if em
  4968. (if (featurep 'xemacs)
  4969. '(org-do-emphasis-faces (0 nil append))
  4970. '(org-do-emphasis-faces)))
  4971. ;; Checkboxes
  4972. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  4973. 1 'org-checkbox prepend)
  4974. (if (cdr (assq 'checkbox org-list-automatic-rules))
  4975. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  4976. (0 (org-get-checkbox-statistics-face) t)))
  4977. ;; Description list items
  4978. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  4979. 1 'bold prepend)
  4980. ;; ARCHIVEd headings
  4981. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  4982. '(1 'org-archived prepend))
  4983. ;; Specials
  4984. '(org-do-latex-and-special-faces)
  4985. '(org-fontify-entities)
  4986. '(org-raise-scripts)
  4987. ;; Code
  4988. '(org-activate-code (1 'org-code t))
  4989. ;; COMMENT
  4990. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  4991. "\\|" org-quote-string "\\)\\>")
  4992. '(1 'org-special-keyword t))
  4993. '("^#.*" (0 'font-lock-comment-face t))
  4994. ;; Blocks and meta lines
  4995. '(org-fontify-meta-lines-and-blocks)
  4996. )))
  4997. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  4998. (run-hooks 'org-font-lock-set-keywords-hook)
  4999. ;; Now set the full font-lock-keywords
  5000. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5001. (org-set-local 'font-lock-defaults
  5002. '(org-font-lock-keywords t nil nil backward-paragraph))
  5003. (kill-local-variable 'font-lock-keywords) nil))
  5004. (defun org-toggle-pretty-entities ()
  5005. "Toggle the composition display of entities as UTF8 characters."
  5006. (interactive)
  5007. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5008. (org-restart-font-lock)
  5009. (if org-pretty-entities
  5010. (message "Entities are displayed as UTF8 characers")
  5011. (save-restriction
  5012. (widen)
  5013. (org-decompose-region (point-min) (point-max))
  5014. (message "Entities are displayed plain"))))
  5015. (defun org-fontify-entities (limit)
  5016. "Find an entity to fontify."
  5017. (let (ee)
  5018. (when org-pretty-entities
  5019. (catch 'match
  5020. (while (re-search-forward
  5021. "\\\\\\([a-zA-Z][a-zA-Z0-9]*\\)\\($\\|[^[:alnum:]\n]\\)"
  5022. limit t)
  5023. (if (and (not (org-in-indented-comment-line))
  5024. (setq ee (org-entity-get (match-string 1)))
  5025. (= (length (nth 6 ee)) 1))
  5026. (progn
  5027. (add-text-properties
  5028. (match-beginning 0) (match-end 1)
  5029. (list 'font-lock-fontified t))
  5030. (compose-region (match-beginning 0) (match-end 1)
  5031. (nth 6 ee) nil)
  5032. (backward-char 1)
  5033. (throw 'match t))))
  5034. nil))))
  5035. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5036. "Fontify string S like in Org-mode."
  5037. (with-temp-buffer
  5038. (insert s)
  5039. (let ((org-odd-levels-only odd-levels))
  5040. (org-mode)
  5041. (font-lock-fontify-buffer)
  5042. (buffer-string))))
  5043. (defvar org-m nil)
  5044. (defvar org-l nil)
  5045. (defvar org-f nil)
  5046. (defun org-get-level-face (n)
  5047. "Get the right face for match N in font-lock matching of headlines."
  5048. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5049. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5050. (if org-cycle-level-faces
  5051. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5052. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5053. (cond
  5054. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5055. ((eq n 2) org-f)
  5056. (t (if org-level-color-stars-only nil org-f))))
  5057. (defun org-get-todo-face (kwd)
  5058. "Get the right face for a TODO keyword KWD.
  5059. If KWD is a number, get the corresponding match group."
  5060. (if (numberp kwd) (setq kwd (match-string kwd)))
  5061. (or (org-face-from-face-or-color
  5062. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5063. (and (member kwd org-done-keywords) 'org-done)
  5064. 'org-todo))
  5065. (defun org-face-from-face-or-color (context inherit face-or-color)
  5066. "Create a face list that inherits INHERIT, but sets the foreground color.
  5067. When FACE-OR-COLOR is not a string, just return it."
  5068. (if (stringp face-or-color)
  5069. (list :inherit inherit
  5070. (cdr (assoc context org-faces-easy-properties))
  5071. face-or-color)
  5072. face-or-color))
  5073. (defun org-font-lock-add-tag-faces (limit)
  5074. "Add the special tag faces."
  5075. (when (and org-tag-faces org-tags-special-faces-re)
  5076. (while (re-search-forward org-tags-special-faces-re limit t)
  5077. (add-text-properties (match-beginning 1) (match-end 1)
  5078. (list 'face (org-get-tag-face 1)
  5079. 'font-lock-fontified t))
  5080. (backward-char 1))))
  5081. (defun org-font-lock-add-priority-faces (limit)
  5082. "Add the special priority faces."
  5083. (while (re-search-forward "\\[#\\([A-Z0-9]\\)\\]" limit t)
  5084. (add-text-properties
  5085. (match-beginning 0) (match-end 0)
  5086. (list 'face (or (org-face-from-face-or-color
  5087. 'priority 'org-special-keyword
  5088. (cdr (assoc (char-after (match-beginning 1))
  5089. org-priority-faces)))
  5090. 'org-special-keyword)
  5091. 'font-lock-fontified t))))
  5092. (defun org-get-tag-face (kwd)
  5093. "Get the right face for a TODO keyword KWD.
  5094. If KWD is a number, get the corresponding match group."
  5095. (if (numberp kwd) (setq kwd (match-string kwd)))
  5096. (or (org-face-from-face-or-color
  5097. 'tag 'org-tag (cdr (assoc kwd org-tag-faces)))
  5098. 'org-tag))
  5099. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5100. "Remove fontification and activation overlays from links."
  5101. (font-lock-default-unfontify-region beg end)
  5102. (let* ((buffer-undo-list t)
  5103. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5104. (inhibit-modification-hooks t)
  5105. deactivate-mark buffer-file-name buffer-file-truename)
  5106. (org-decompose-region beg end)
  5107. (remove-text-properties
  5108. beg end
  5109. (if org-indent-mode
  5110. ;; also remove line-prefix and wrap-prefix properties
  5111. '(mouse-face t keymap t org-linked-text t
  5112. invisible t intangible t
  5113. line-prefix t wrap-prefix t
  5114. org-no-flyspell t org-emphasis t)
  5115. '(mouse-face t keymap t org-linked-text t
  5116. invisible t intangible t
  5117. org-no-flyspell t org-emphasis t)))
  5118. (org-remove-font-lock-display-properties beg end)))
  5119. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5120. ((raise 0.3) (height 0.7))
  5121. ((raise -0.5))
  5122. ((raise 0.5)))
  5123. "Display properties for showing superscripts and subscripts.")
  5124. (defun org-remove-font-lock-display-properties (beg end)
  5125. "Remove specific display properties that have been added by font lock.
  5126. The will remove the raise properties that are used to show superscripts
  5127. and subscripts."
  5128. (let (next prop)
  5129. (while (< beg end)
  5130. (setq next (next-single-property-change beg 'display nil end)
  5131. prop (get-text-property beg 'display))
  5132. (if (member prop org-script-display)
  5133. (put-text-property beg next 'display nil))
  5134. (setq beg next))))
  5135. (defun org-raise-scripts (limit)
  5136. "Add raise properties to sub/superscripts."
  5137. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5138. (if (re-search-forward
  5139. (if (eq org-use-sub-superscripts t)
  5140. org-match-substring-regexp
  5141. org-match-substring-with-braces-regexp)
  5142. limit t)
  5143. (let* ((pos (point)) table-p comment-p
  5144. (mpos (match-beginning 3))
  5145. (emph-p (get-text-property mpos 'org-emphasis))
  5146. (link-p (get-text-property mpos 'mouse-face))
  5147. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5148. (goto-char (point-at-bol))
  5149. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5150. comment-p (org-looking-at-p "[ \t]*#"))
  5151. (goto-char pos)
  5152. ;; FIXME: Should we go back one character here, for a_b^c
  5153. ;; (goto-char (1- pos)) ;????????????????????
  5154. (if (or comment-p emph-p link-p keyw-p)
  5155. t
  5156. (put-text-property (match-beginning 3) (match-end 0)
  5157. 'display
  5158. (if (equal (char-after (match-beginning 2)) ?^)
  5159. (nth (if table-p 3 1) org-script-display)
  5160. (nth (if table-p 2 0) org-script-display)))
  5161. (add-text-properties (match-beginning 2) (match-end 2)
  5162. (list 'invisible t
  5163. 'org-dwidth t 'org-dwidth-n 1))
  5164. (if (and (eq (char-after (match-beginning 3)) ?{)
  5165. (eq (char-before (match-end 3)) ?}))
  5166. (progn
  5167. (add-text-properties
  5168. (match-beginning 3) (1+ (match-beginning 3))
  5169. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5170. (add-text-properties
  5171. (1- (match-end 3)) (match-end 3)
  5172. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5173. t)))))
  5174. ;;;; Visibility cycling, including org-goto and indirect buffer
  5175. ;;; Cycling
  5176. (defvar org-cycle-global-status nil)
  5177. (make-variable-buffer-local 'org-cycle-global-status)
  5178. (defvar org-cycle-subtree-status nil)
  5179. (make-variable-buffer-local 'org-cycle-subtree-status)
  5180. ;;;###autoload
  5181. (defvar org-inlinetask-min-level)
  5182. (defun org-cycle (&optional arg)
  5183. "TAB-action and visibility cycling for Org-mode.
  5184. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5185. is outline visibility cycling, but it also invokes other actions
  5186. in special contexts.
  5187. - When this function is called with a prefix argument, rotate the entire
  5188. buffer through 3 states (global cycling)
  5189. 1. OVERVIEW: Show only top-level headlines.
  5190. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5191. 3. SHOW ALL: Show everything.
  5192. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5193. determined by the variable `org-startup-folded', and by any VISIBILITY
  5194. properties in the buffer.
  5195. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5196. including any drawers.
  5197. - When inside a table, re-align the table and move to the next field.
  5198. - When point is at the beginning of a headline, rotate the subtree started
  5199. by this line through 3 different states (local cycling)
  5200. 1. FOLDED: Only the main headline is shown.
  5201. 2. CHILDREN: The main headline and the direct children are shown.
  5202. From this state, you can move to one of the children
  5203. and zoom in further.
  5204. 3. SUBTREE: Show the entire subtree, including body text.
  5205. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5206. - When point is at the beginning of an empty headline and the variable
  5207. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5208. of the headline by demoting and promoting it to likely levels. This
  5209. speeds up creation document structure by pressing TAB once or several
  5210. times right after creating a new headline.
  5211. - When there is a numeric prefix, go up to a heading with level ARG, do
  5212. a `show-subtree' and return to the previous cursor position. If ARG
  5213. is negative, go up that many levels.
  5214. - When point is not at the beginning of a headline, execute the global
  5215. binding for TAB, which is re-indenting the line. See the option
  5216. `org-cycle-emulate-tab' for details.
  5217. - Special case: if point is at the beginning of the buffer and there is
  5218. no headline in line 1, this function will act as if called with prefix arg
  5219. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5220. But only if also the variable `org-cycle-global-at-bob' is t."
  5221. (interactive "P")
  5222. (org-load-modules-maybe)
  5223. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5224. (and org-cycle-level-after-item/entry-creation
  5225. (or (org-cycle-level)
  5226. (org-cycle-item-indentation))))
  5227. (let* ((limit-level
  5228. (or org-cycle-max-level
  5229. (and (boundp 'org-inlinetask-min-level)
  5230. org-inlinetask-min-level
  5231. (1- org-inlinetask-min-level))))
  5232. (nstars (and limit-level
  5233. (if org-odd-levels-only
  5234. (and limit-level (1- (* limit-level 2)))
  5235. limit-level)))
  5236. (outline-regexp
  5237. (if (not (org-mode-p))
  5238. outline-regexp
  5239. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5240. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5241. (not (looking-at outline-regexp))))
  5242. (org-cycle-hook
  5243. (if bob-special
  5244. (delq 'org-optimize-window-after-visibility-change
  5245. (copy-sequence org-cycle-hook))
  5246. org-cycle-hook))
  5247. (pos (point)))
  5248. (if (or bob-special (equal arg '(4)))
  5249. ;; special case: use global cycling
  5250. (setq arg t))
  5251. (cond
  5252. ((equal arg '(16))
  5253. (setq last-command 'dummy)
  5254. (org-set-startup-visibility)
  5255. (message "Startup visibility, plus VISIBILITY properties"))
  5256. ((equal arg '(64))
  5257. (show-all)
  5258. (message "Entire buffer visible, including drawers"))
  5259. ;; Table: enter it or move to the next field.
  5260. ((org-at-table-p 'any)
  5261. (if (org-at-table.el-p)
  5262. (message "Use C-c ' to edit table.el tables")
  5263. (if arg (org-table-edit-field t)
  5264. (org-table-justify-field-maybe)
  5265. (call-interactively 'org-table-next-field))))
  5266. ((run-hook-with-args-until-success
  5267. 'org-tab-after-check-for-table-hook))
  5268. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5269. ((eq arg t) (org-cycle-internal-global))
  5270. ;; Drawers: delegate to `org-flag-drawer'.
  5271. ((and org-drawers org-drawer-regexp
  5272. (save-excursion
  5273. (beginning-of-line 1)
  5274. (looking-at org-drawer-regexp)))
  5275. (org-flag-drawer ; toggle block visibility
  5276. (not (get-char-property (match-end 0) 'invisible))))
  5277. ;; Show-subtree, ARG levels up from here.
  5278. ((integerp arg)
  5279. (save-excursion
  5280. (org-back-to-heading)
  5281. (outline-up-heading (if (< arg 0) (- arg)
  5282. (- (funcall outline-level) arg)))
  5283. (org-show-subtree)))
  5284. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5285. ((and (featurep 'org-inlinetask)
  5286. (org-inlinetask-at-task-p)
  5287. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5288. (org-inlinetask-toggle-visibility))
  5289. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5290. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5291. (save-excursion (beginning-of-line 1)
  5292. (looking-at outline-regexp)))
  5293. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5294. (org-cycle-internal-local))
  5295. ;; From there: TAB emulation and template completion.
  5296. (buffer-read-only (org-back-to-heading))
  5297. ((run-hook-with-args-until-success
  5298. 'org-tab-after-check-for-cycling-hook))
  5299. ((org-try-structure-completion))
  5300. ((org-try-cdlatex-tab))
  5301. ((run-hook-with-args-until-success
  5302. 'org-tab-before-tab-emulation-hook))
  5303. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5304. (or (not (bolp))
  5305. (not (looking-at outline-regexp))))
  5306. (call-interactively (global-key-binding "\t")))
  5307. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5308. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5309. (or (and (eq org-cycle-emulate-tab 'white)
  5310. (= (match-end 0) (point-at-eol)))
  5311. (and (eq org-cycle-emulate-tab 'whitestart)
  5312. (>= (match-end 0) pos))))
  5313. t
  5314. (eq org-cycle-emulate-tab t))
  5315. (call-interactively (global-key-binding "\t")))
  5316. (t (save-excursion
  5317. (org-back-to-heading)
  5318. (org-cycle)))))))
  5319. (defun org-cycle-internal-global ()
  5320. "Do the global cycling action."
  5321. (cond
  5322. ((and (eq last-command this-command)
  5323. (eq org-cycle-global-status 'overview))
  5324. ;; We just created the overview - now do table of contents
  5325. ;; This can be slow in very large buffers, so indicate action
  5326. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5327. (message "CONTENTS...")
  5328. (org-content)
  5329. (message "CONTENTS...done")
  5330. (setq org-cycle-global-status 'contents)
  5331. (run-hook-with-args 'org-cycle-hook 'contents))
  5332. ((and (eq last-command this-command)
  5333. (eq org-cycle-global-status 'contents))
  5334. ;; We just showed the table of contents - now show everything
  5335. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5336. (show-all)
  5337. (message "SHOW ALL")
  5338. (setq org-cycle-global-status 'all)
  5339. (run-hook-with-args 'org-cycle-hook 'all))
  5340. (t
  5341. ;; Default action: go to overview
  5342. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5343. (org-overview)
  5344. (message "OVERVIEW")
  5345. (setq org-cycle-global-status 'overview)
  5346. (run-hook-with-args 'org-cycle-hook 'overview))))
  5347. (defun org-cycle-internal-local ()
  5348. "Do the local cycling action."
  5349. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5350. ;; First, determine end of headline (EOH), end of subtree or item
  5351. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5352. (save-excursion
  5353. (if (org-at-item-p)
  5354. (progn
  5355. (beginning-of-line)
  5356. (setq struct (org-list-struct))
  5357. (setq eoh (point-at-eol))
  5358. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5359. (setq has-children (org-list-has-child-p (point) struct)))
  5360. (org-back-to-heading)
  5361. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5362. (setq eos (save-excursion
  5363. (org-end-of-subtree t)
  5364. (unless (eobp)
  5365. (skip-chars-forward " \t\n"))
  5366. (if (eobp) (point) (1- (point)))))
  5367. (setq has-children
  5368. (or (save-excursion
  5369. (let ((level (funcall outline-level)))
  5370. (outline-next-heading)
  5371. (and (org-at-heading-p t)
  5372. (> (funcall outline-level) level))))
  5373. (save-excursion
  5374. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5375. ;; Determine end invisible part of buffer (EOL)
  5376. (beginning-of-line 2)
  5377. ;; XEmacs doesn't have `next-single-char-property-change'
  5378. (if (featurep 'xemacs)
  5379. (while (and (not (eobp)) ;; this is like `next-line'
  5380. (get-char-property (1- (point)) 'invisible))
  5381. (beginning-of-line 2))
  5382. (while (and (not (eobp)) ;; this is like `next-line'
  5383. (get-char-property (1- (point)) 'invisible))
  5384. (goto-char (next-single-char-property-change (point) 'invisible))
  5385. (and (eolp) (beginning-of-line 2))))
  5386. (setq eol (point)))
  5387. ;; Find out what to do next and set `this-command'
  5388. (cond
  5389. ((= eos eoh)
  5390. ;; Nothing is hidden behind this heading
  5391. (run-hook-with-args 'org-pre-cycle-hook 'empty)
  5392. (message "EMPTY ENTRY")
  5393. (setq org-cycle-subtree-status nil)
  5394. (save-excursion
  5395. (goto-char eos)
  5396. (outline-next-heading)
  5397. (if (outline-invisible-p) (org-flag-heading nil))))
  5398. ((and (or (>= eol eos)
  5399. (not (string-match "\\S-" (buffer-substring eol eos))))
  5400. (or has-children
  5401. (not (setq children-skipped
  5402. org-cycle-skip-children-state-if-no-children))))
  5403. ;; Entire subtree is hidden in one line: children view
  5404. (run-hook-with-args 'org-pre-cycle-hook 'children)
  5405. (if (org-at-item-p)
  5406. (org-list-set-item-visibility (point-at-bol) struct 'children)
  5407. (org-show-entry)
  5408. (show-children)
  5409. ;; Fold every list in subtree to top-level items.
  5410. (when (eq org-cycle-include-plain-lists 'integrate)
  5411. (save-excursion
  5412. (org-back-to-heading)
  5413. (while (org-list-search-forward (org-item-beginning-re) eos t)
  5414. (beginning-of-line 1)
  5415. (let* ((struct (org-list-struct))
  5416. (prevs (org-list-prevs-alist struct))
  5417. (end (org-list-get-bottom-point struct)))
  5418. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  5419. (org-list-get-all-items (point) struct prevs))
  5420. (goto-char end))))))
  5421. (message "CHILDREN")
  5422. (save-excursion
  5423. (goto-char eos)
  5424. (outline-next-heading)
  5425. (if (outline-invisible-p) (org-flag-heading nil)))
  5426. (setq org-cycle-subtree-status 'children)
  5427. (run-hook-with-args 'org-cycle-hook 'children))
  5428. ((or children-skipped
  5429. (and (eq last-command this-command)
  5430. (eq org-cycle-subtree-status 'children)))
  5431. ;; We just showed the children, or no children are there,
  5432. ;; now show everything.
  5433. (run-hook-with-args 'org-pre-cycle-hook 'subtree)
  5434. (outline-flag-region eoh eos nil)
  5435. (message (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  5436. (setq org-cycle-subtree-status 'subtree)
  5437. (run-hook-with-args 'org-cycle-hook 'subtree))
  5438. (t
  5439. ;; Default action: hide the subtree.
  5440. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  5441. (outline-flag-region eoh eos t)
  5442. (message "FOLDED")
  5443. (setq org-cycle-subtree-status 'folded)
  5444. (run-hook-with-args 'org-cycle-hook 'folded)))))
  5445. ;;;###autoload
  5446. (defun org-global-cycle (&optional arg)
  5447. "Cycle the global visibility. For details see `org-cycle'.
  5448. With \\[universal-argument] prefix arg, switch to startup visibility.
  5449. With a numeric prefix, show all headlines up to that level."
  5450. (interactive "P")
  5451. (let ((org-cycle-include-plain-lists
  5452. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  5453. (cond
  5454. ((integerp arg)
  5455. (show-all)
  5456. (hide-sublevels arg)
  5457. (setq org-cycle-global-status 'contents))
  5458. ((equal arg '(4))
  5459. (org-set-startup-visibility)
  5460. (message "Startup visibility, plus VISIBILITY properties."))
  5461. (t
  5462. (org-cycle '(4))))))
  5463. (defun org-set-startup-visibility ()
  5464. "Set the visibility required by startup options and properties."
  5465. (cond
  5466. ((eq org-startup-folded t)
  5467. (org-cycle '(4)))
  5468. ((eq org-startup-folded 'content)
  5469. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  5470. (org-cycle '(4)) (org-cycle '(4)))))
  5471. (unless (eq org-startup-folded 'showeverything)
  5472. (if org-hide-block-startup (org-hide-block-all))
  5473. (org-set-visibility-according-to-property 'no-cleanup)
  5474. (org-cycle-hide-archived-subtrees 'all)
  5475. (org-cycle-hide-drawers 'all)
  5476. (org-cycle-show-empty-lines t)))
  5477. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  5478. "Switch subtree visibilities according to :VISIBILITY: property."
  5479. (interactive)
  5480. (let (org-show-entry-below state)
  5481. (save-excursion
  5482. (goto-char (point-min))
  5483. (while (re-search-forward
  5484. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  5485. nil t)
  5486. (setq state (match-string 1))
  5487. (save-excursion
  5488. (org-back-to-heading t)
  5489. (hide-subtree)
  5490. (org-reveal)
  5491. (cond
  5492. ((equal state '("fold" "folded"))
  5493. (hide-subtree))
  5494. ((equal state "children")
  5495. (org-show-hidden-entry)
  5496. (show-children))
  5497. ((equal state "content")
  5498. (save-excursion
  5499. (save-restriction
  5500. (org-narrow-to-subtree)
  5501. (org-content))))
  5502. ((member state '("all" "showall"))
  5503. (show-subtree)))))
  5504. (unless no-cleanup
  5505. (org-cycle-hide-archived-subtrees 'all)
  5506. (org-cycle-hide-drawers 'all)
  5507. (org-cycle-show-empty-lines 'all)))))
  5508. (defun org-overview ()
  5509. "Switch to overview mode, showing only top-level headlines.
  5510. Really, this shows all headlines with level equal or greater than the level
  5511. of the first headline in the buffer. This is important, because if the
  5512. first headline is not level one, then (hide-sublevels 1) gives confusing
  5513. results."
  5514. (interactive)
  5515. (let ((level (save-excursion
  5516. (goto-char (point-min))
  5517. (if (re-search-forward (concat "^" outline-regexp) nil t)
  5518. (progn
  5519. (goto-char (match-beginning 0))
  5520. (funcall outline-level))))))
  5521. (and level (hide-sublevels level))))
  5522. (defun org-content (&optional arg)
  5523. "Show all headlines in the buffer, like a table of contents.
  5524. With numerical argument N, show content up to level N."
  5525. (interactive "P")
  5526. (save-excursion
  5527. ;; Visit all headings and show their offspring
  5528. (and (integerp arg) (org-overview))
  5529. (goto-char (point-max))
  5530. (catch 'exit
  5531. (while (and (progn (condition-case nil
  5532. (outline-previous-visible-heading 1)
  5533. (error (goto-char (point-min))))
  5534. t)
  5535. (looking-at outline-regexp))
  5536. (if (integerp arg)
  5537. (show-children (1- arg))
  5538. (show-branches))
  5539. (if (bobp) (throw 'exit nil))))))
  5540. (defun org-optimize-window-after-visibility-change (state)
  5541. "Adjust the window after a change in outline visibility.
  5542. This function is the default value of the hook `org-cycle-hook'."
  5543. (when (get-buffer-window (current-buffer))
  5544. (cond
  5545. ((eq state 'content) nil)
  5546. ((eq state 'all) nil)
  5547. ((eq state 'folded) nil)
  5548. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  5549. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  5550. (defun org-remove-empty-overlays-at (pos)
  5551. "Remove outline overlays that do not contain non-white stuff."
  5552. (mapc
  5553. (lambda (o)
  5554. (and (eq 'outline (overlay-get o 'invisible))
  5555. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  5556. (overlay-end o))))
  5557. (delete-overlay o)))
  5558. (overlays-at pos)))
  5559. (defun org-clean-visibility-after-subtree-move ()
  5560. "Fix visibility issues after moving a subtree."
  5561. ;; First, find a reasonable region to look at:
  5562. ;; Start two siblings above, end three below
  5563. (let* ((beg (save-excursion
  5564. (and (org-get-last-sibling)
  5565. (org-get-last-sibling))
  5566. (point)))
  5567. (end (save-excursion
  5568. (and (org-get-next-sibling)
  5569. (org-get-next-sibling)
  5570. (org-get-next-sibling))
  5571. (if (org-at-heading-p)
  5572. (point-at-eol)
  5573. (point))))
  5574. (level (looking-at "\\*+"))
  5575. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  5576. (save-excursion
  5577. (save-restriction
  5578. (narrow-to-region beg end)
  5579. (when re
  5580. ;; Properly fold already folded siblings
  5581. (goto-char (point-min))
  5582. (while (re-search-forward re nil t)
  5583. (if (and (not (outline-invisible-p))
  5584. (save-excursion
  5585. (goto-char (point-at-eol)) (outline-invisible-p)))
  5586. (hide-entry))))
  5587. (org-cycle-show-empty-lines 'overview)
  5588. (org-cycle-hide-drawers 'overview)))))
  5589. (defun org-cycle-show-empty-lines (state)
  5590. "Show empty lines above all visible headlines.
  5591. The region to be covered depends on STATE when called through
  5592. `org-cycle-hook'. Lisp program can use t for STATE to get the
  5593. entire buffer covered. Note that an empty line is only shown if there
  5594. are at least `org-cycle-separator-lines' empty lines before the headline."
  5595. (when (not (= org-cycle-separator-lines 0))
  5596. (save-excursion
  5597. (let* ((n (abs org-cycle-separator-lines))
  5598. (re (cond
  5599. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  5600. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  5601. (t (let ((ns (number-to-string (- n 2))))
  5602. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  5603. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  5604. beg end b e)
  5605. (cond
  5606. ((memq state '(overview contents t))
  5607. (setq beg (point-min) end (point-max)))
  5608. ((memq state '(children folded))
  5609. (setq beg (point) end (progn (org-end-of-subtree t t)
  5610. (beginning-of-line 2)
  5611. (point)))))
  5612. (when beg
  5613. (goto-char beg)
  5614. (while (re-search-forward re end t)
  5615. (unless (get-char-property (match-end 1) 'invisible)
  5616. (setq e (match-end 1))
  5617. (if (< org-cycle-separator-lines 0)
  5618. (setq b (save-excursion
  5619. (goto-char (match-beginning 0))
  5620. (org-back-over-empty-lines)
  5621. (if (save-excursion
  5622. (goto-char (max (point-min) (1- (point))))
  5623. (org-on-heading-p))
  5624. (1- (point))
  5625. (point))))
  5626. (setq b (match-beginning 1)))
  5627. (outline-flag-region b e nil)))))))
  5628. ;; Never hide empty lines at the end of the file.
  5629. (save-excursion
  5630. (goto-char (point-max))
  5631. (outline-previous-heading)
  5632. (outline-end-of-heading)
  5633. (if (and (looking-at "[ \t\n]+")
  5634. (= (match-end 0) (point-max)))
  5635. (outline-flag-region (point) (match-end 0) nil))))
  5636. (defun org-show-empty-lines-in-parent ()
  5637. "Move to the parent and re-show empty lines before visible headlines."
  5638. (save-excursion
  5639. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  5640. (org-cycle-show-empty-lines context))))
  5641. (defun org-files-list ()
  5642. "Return `org-agenda-files' list, plus all open org-mode files.
  5643. This is useful for operations that need to scan all of a user's
  5644. open and agenda-wise Org files."
  5645. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  5646. (dolist (buf (buffer-list))
  5647. (with-current-buffer buf
  5648. (if (and (org-mode-p) (buffer-file-name))
  5649. (let ((file (expand-file-name (buffer-file-name))))
  5650. (unless (member file files)
  5651. (push file files))))))
  5652. files))
  5653. (defsubst org-entry-beginning-position ()
  5654. "Return the beginning position of the current entry."
  5655. (save-excursion (outline-back-to-heading t) (point)))
  5656. (defsubst org-entry-end-position ()
  5657. "Return the end position of the current entry."
  5658. (save-excursion (outline-next-heading) (point)))
  5659. (defun org-cycle-hide-drawers (state)
  5660. "Re-hide all drawers after a visibility state change."
  5661. (when (and (org-mode-p)
  5662. (not (memq state '(overview folded contents))))
  5663. (save-excursion
  5664. (let* ((globalp (memq state '(contents all)))
  5665. (beg (if globalp (point-min) (point)))
  5666. (end (if globalp (point-max)
  5667. (if (eq state 'children)
  5668. (save-excursion (outline-next-heading) (point))
  5669. (org-end-of-subtree t)))))
  5670. (goto-char beg)
  5671. (while (re-search-forward org-drawer-regexp end t)
  5672. (org-flag-drawer t))))))
  5673. (defun org-flag-drawer (flag)
  5674. (save-excursion
  5675. (beginning-of-line 1)
  5676. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  5677. (let ((b (match-end 0))
  5678. (outline-regexp org-outline-regexp))
  5679. (if (re-search-forward
  5680. "^[ \t]*:END:"
  5681. (save-excursion (outline-next-heading) (point)) t)
  5682. (outline-flag-region b (point-at-eol) flag)
  5683. (error ":END: line missing at position %s" b))))))
  5684. (defun org-subtree-end-visible-p ()
  5685. "Is the end of the current subtree visible?"
  5686. (pos-visible-in-window-p
  5687. (save-excursion (org-end-of-subtree t) (point))))
  5688. (defun org-first-headline-recenter (&optional N)
  5689. "Move cursor to the first headline and recenter the headline.
  5690. Optional argument N means put the headline into the Nth line of the window."
  5691. (goto-char (point-min))
  5692. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  5693. (beginning-of-line)
  5694. (recenter (prefix-numeric-value N))))
  5695. ;;; Saving and restoring visibility
  5696. (defun org-outline-overlay-data (&optional use-markers)
  5697. "Return a list of the locations of all outline overlays.
  5698. These are overlays with the `invisible' property value `outline'.
  5699. The return value is a list of cons cells, with start and stop
  5700. positions for each overlay.
  5701. If USE-MARKERS is set, return the positions as markers."
  5702. (let (beg end)
  5703. (save-excursion
  5704. (save-restriction
  5705. (widen)
  5706. (delq nil
  5707. (mapcar (lambda (o)
  5708. (when (eq (overlay-get o 'invisible) 'outline)
  5709. (setq beg (overlay-start o)
  5710. end (overlay-end o))
  5711. (and beg end (> end beg)
  5712. (if use-markers
  5713. (cons (move-marker (make-marker) beg)
  5714. (move-marker (make-marker) end))
  5715. (cons beg end)))))
  5716. (overlays-in (point-min) (point-max))))))))
  5717. (defun org-set-outline-overlay-data (data)
  5718. "Create visibility overlays for all positions in DATA.
  5719. DATA should have been made by `org-outline-overlay-data'."
  5720. (let (o)
  5721. (save-excursion
  5722. (save-restriction
  5723. (widen)
  5724. (show-all)
  5725. (mapc (lambda (c)
  5726. (setq o (make-overlay (car c) (cdr c)))
  5727. (overlay-put o 'invisible 'outline))
  5728. data)))))
  5729. ;;; Folding of blocks
  5730. (defconst org-block-regexp
  5731. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  5732. "Regular expression for hiding blocks.")
  5733. (defvar org-hide-block-overlays nil
  5734. "Overlays hiding blocks.")
  5735. (make-variable-buffer-local 'org-hide-block-overlays)
  5736. (defun org-block-map (function &optional start end)
  5737. "Call FUNCTION at the head of all source blocks in the current buffer.
  5738. Optional arguments START and END can be used to limit the range."
  5739. (let ((start (or start (point-min)))
  5740. (end (or end (point-max))))
  5741. (save-excursion
  5742. (goto-char start)
  5743. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  5744. (save-excursion
  5745. (save-match-data
  5746. (goto-char (match-beginning 0))
  5747. (funcall function)))))))
  5748. (defun org-hide-block-toggle-all ()
  5749. "Toggle the visibility of all blocks in the current buffer."
  5750. (org-block-map #'org-hide-block-toggle))
  5751. (defun org-hide-block-all ()
  5752. "Fold all blocks in the current buffer."
  5753. (interactive)
  5754. (org-show-block-all)
  5755. (org-block-map #'org-hide-block-toggle-maybe))
  5756. (defun org-show-block-all ()
  5757. "Unfold all blocks in the current buffer."
  5758. (interactive)
  5759. (mapc 'delete-overlay org-hide-block-overlays)
  5760. (setq org-hide-block-overlays nil))
  5761. (defun org-hide-block-toggle-maybe ()
  5762. "Toggle visibility of block at point."
  5763. (interactive)
  5764. (let ((case-fold-search t))
  5765. (if (save-excursion
  5766. (beginning-of-line 1)
  5767. (looking-at org-block-regexp))
  5768. (progn (org-hide-block-toggle)
  5769. t) ;; to signal that we took action
  5770. nil))) ;; to signal that we did not
  5771. (defun org-hide-block-toggle (&optional force)
  5772. "Toggle the visibility of the current block."
  5773. (interactive)
  5774. (save-excursion
  5775. (beginning-of-line)
  5776. (if (re-search-forward org-block-regexp nil t)
  5777. (let ((start (- (match-beginning 4) 1)) ;; beginning of body
  5778. (end (match-end 0)) ;; end of entire body
  5779. ov)
  5780. (if (memq t (mapcar (lambda (overlay)
  5781. (eq (overlay-get overlay 'invisible)
  5782. 'org-hide-block))
  5783. (overlays-at start)))
  5784. (if (or (not force) (eq force 'off))
  5785. (mapc (lambda (ov)
  5786. (when (member ov org-hide-block-overlays)
  5787. (setq org-hide-block-overlays
  5788. (delq ov org-hide-block-overlays)))
  5789. (when (eq (overlay-get ov 'invisible)
  5790. 'org-hide-block)
  5791. (delete-overlay ov)))
  5792. (overlays-at start)))
  5793. (setq ov (make-overlay start end))
  5794. (overlay-put ov 'invisible 'org-hide-block)
  5795. ;; make the block accessible to isearch
  5796. (overlay-put
  5797. ov 'isearch-open-invisible
  5798. (lambda (ov)
  5799. (when (member ov org-hide-block-overlays)
  5800. (setq org-hide-block-overlays
  5801. (delq ov org-hide-block-overlays)))
  5802. (when (eq (overlay-get ov 'invisible)
  5803. 'org-hide-block)
  5804. (delete-overlay ov))))
  5805. (push ov org-hide-block-overlays)))
  5806. (error "Not looking at a source block"))))
  5807. ;; org-tab-after-check-for-cycling-hook
  5808. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  5809. ;; Remove overlays when changing major mode
  5810. (add-hook 'org-mode-hook
  5811. (lambda () (org-add-hook 'change-major-mode-hook
  5812. 'org-show-block-all 'append 'local)))
  5813. ;;; Org-goto
  5814. (defvar org-goto-window-configuration nil)
  5815. (defvar org-goto-marker nil)
  5816. (defvar org-goto-map
  5817. (let ((map (make-sparse-keymap)))
  5818. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  5819. (while (setq cmd (pop cmds))
  5820. (substitute-key-definition cmd cmd map global-map)))
  5821. (suppress-keymap map)
  5822. (org-defkey map "\C-m" 'org-goto-ret)
  5823. (org-defkey map [(return)] 'org-goto-ret)
  5824. (org-defkey map [(left)] 'org-goto-left)
  5825. (org-defkey map [(right)] 'org-goto-right)
  5826. (org-defkey map [(control ?g)] 'org-goto-quit)
  5827. (org-defkey map "\C-i" 'org-cycle)
  5828. (org-defkey map [(tab)] 'org-cycle)
  5829. (org-defkey map [(down)] 'outline-next-visible-heading)
  5830. (org-defkey map [(up)] 'outline-previous-visible-heading)
  5831. (if org-goto-auto-isearch
  5832. (if (fboundp 'define-key-after)
  5833. (define-key-after map [t] 'org-goto-local-auto-isearch)
  5834. nil)
  5835. (org-defkey map "q" 'org-goto-quit)
  5836. (org-defkey map "n" 'outline-next-visible-heading)
  5837. (org-defkey map "p" 'outline-previous-visible-heading)
  5838. (org-defkey map "f" 'outline-forward-same-level)
  5839. (org-defkey map "b" 'outline-backward-same-level)
  5840. (org-defkey map "u" 'outline-up-heading))
  5841. (org-defkey map "/" 'org-occur)
  5842. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  5843. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  5844. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  5845. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  5846. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  5847. map))
  5848. (defconst org-goto-help
  5849. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  5850. RET=jump to location [Q]uit and return to previous location
  5851. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  5852. (defvar org-goto-start-pos) ; dynamically scoped parameter
  5853. ;; FIXME: Docstring does not mention both interfaces
  5854. (defun org-goto (&optional alternative-interface)
  5855. "Look up a different location in the current file, keeping current visibility.
  5856. When you want look-up or go to a different location in a document, the
  5857. fastest way is often to fold the entire buffer and then dive into the tree.
  5858. This method has the disadvantage, that the previous location will be folded,
  5859. which may not be what you want.
  5860. This command works around this by showing a copy of the current buffer
  5861. in an indirect buffer, in overview mode. You can dive into the tree in
  5862. that copy, use org-occur and incremental search to find a location.
  5863. When pressing RET or `Q', the command returns to the original buffer in
  5864. which the visibility is still unchanged. After RET is will also jump to
  5865. the location selected in the indirect buffer and expose the
  5866. the headline hierarchy above."
  5867. (interactive "P")
  5868. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  5869. (org-refile-use-outline-path t)
  5870. (org-refile-target-verify-function nil)
  5871. (interface
  5872. (if (not alternative-interface)
  5873. org-goto-interface
  5874. (if (eq org-goto-interface 'outline)
  5875. 'outline-path-completion
  5876. 'outline)))
  5877. (org-goto-start-pos (point))
  5878. (selected-point
  5879. (if (eq interface 'outline)
  5880. (car (org-get-location (current-buffer) org-goto-help))
  5881. (let ((pa (org-refile-get-location "Goto")))
  5882. (org-refile-check-position pa)
  5883. (nth 3 pa)))))
  5884. (if selected-point
  5885. (progn
  5886. (org-mark-ring-push org-goto-start-pos)
  5887. (goto-char selected-point)
  5888. (if (or (outline-invisible-p) (org-invisible-p2))
  5889. (org-show-context 'org-goto)))
  5890. (message "Quit"))))
  5891. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  5892. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  5893. (defvar org-goto-local-auto-isearch-map) ; defined below
  5894. (defun org-get-location (buf help)
  5895. "Let the user select a location in the Org-mode buffer BUF.
  5896. This function uses a recursive edit. It returns the selected position
  5897. or nil."
  5898. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  5899. (isearch-hide-immediately nil)
  5900. (isearch-search-fun-function
  5901. (lambda () 'org-goto-local-search-headings))
  5902. (org-goto-selected-point org-goto-exit-command)
  5903. (pop-up-frames nil)
  5904. (special-display-buffer-names nil)
  5905. (special-display-regexps nil)
  5906. (special-display-function nil))
  5907. (save-excursion
  5908. (save-window-excursion
  5909. (delete-other-windows)
  5910. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  5911. (switch-to-buffer
  5912. (condition-case nil
  5913. (make-indirect-buffer (current-buffer) "*org-goto*")
  5914. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  5915. (with-output-to-temp-buffer "*Help*"
  5916. (princ help))
  5917. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  5918. (setq buffer-read-only nil)
  5919. (let ((org-startup-truncated t)
  5920. (org-startup-folded nil)
  5921. (org-startup-align-all-tables nil))
  5922. (org-mode)
  5923. (org-overview))
  5924. (setq buffer-read-only t)
  5925. (if (and (boundp 'org-goto-start-pos)
  5926. (integer-or-marker-p org-goto-start-pos))
  5927. (let ((org-show-hierarchy-above t)
  5928. (org-show-siblings t)
  5929. (org-show-following-heading t))
  5930. (goto-char org-goto-start-pos)
  5931. (and (outline-invisible-p) (org-show-context)))
  5932. (goto-char (point-min)))
  5933. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  5934. (message "Select location and press RET")
  5935. (use-local-map org-goto-map)
  5936. (recursive-edit)
  5937. ))
  5938. (kill-buffer "*org-goto*")
  5939. (cons org-goto-selected-point org-goto-exit-command)))
  5940. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  5941. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  5942. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  5943. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  5944. (defun org-goto-local-search-headings (string bound noerror)
  5945. "Search and make sure that any matches are in headlines."
  5946. (catch 'return
  5947. (while (if isearch-forward
  5948. (search-forward string bound noerror)
  5949. (search-backward string bound noerror))
  5950. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  5951. (and (member :headline context)
  5952. (not (member :tags context))))
  5953. (throw 'return (point))))))
  5954. (defun org-goto-local-auto-isearch ()
  5955. "Start isearch."
  5956. (interactive)
  5957. (goto-char (point-min))
  5958. (let ((keys (this-command-keys)))
  5959. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  5960. (isearch-mode t)
  5961. (isearch-process-search-char (string-to-char keys)))))
  5962. (defun org-goto-ret (&optional arg)
  5963. "Finish `org-goto' by going to the new location."
  5964. (interactive "P")
  5965. (setq org-goto-selected-point (point)
  5966. org-goto-exit-command 'return)
  5967. (throw 'exit nil))
  5968. (defun org-goto-left ()
  5969. "Finish `org-goto' by going to the new location."
  5970. (interactive)
  5971. (if (org-on-heading-p)
  5972. (progn
  5973. (beginning-of-line 1)
  5974. (setq org-goto-selected-point (point)
  5975. org-goto-exit-command 'left)
  5976. (throw 'exit nil))
  5977. (error "Not on a heading")))
  5978. (defun org-goto-right ()
  5979. "Finish `org-goto' by going to the new location."
  5980. (interactive)
  5981. (if (org-on-heading-p)
  5982. (progn
  5983. (setq org-goto-selected-point (point)
  5984. org-goto-exit-command 'right)
  5985. (throw 'exit nil))
  5986. (error "Not on a heading")))
  5987. (defun org-goto-quit ()
  5988. "Finish `org-goto' without cursor motion."
  5989. (interactive)
  5990. (setq org-goto-selected-point nil)
  5991. (setq org-goto-exit-command 'quit)
  5992. (throw 'exit nil))
  5993. ;;; Indirect buffer display of subtrees
  5994. (defvar org-indirect-dedicated-frame nil
  5995. "This is the frame being used for indirect tree display.")
  5996. (defvar org-last-indirect-buffer nil)
  5997. (defun org-tree-to-indirect-buffer (&optional arg)
  5998. "Create indirect buffer and narrow it to current subtree.
  5999. With numerical prefix ARG, go up to this level and then take that tree.
  6000. If ARG is negative, go up that many levels.
  6001. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6002. indirect buffer previously made with this command, to avoid proliferation of
  6003. indirect buffers. However, when you call the command with a \
  6004. \\[universal-argument] prefix, or
  6005. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6006. is kept so that you can work with several indirect buffers at the same time.
  6007. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6008. \\[universal-argument] prefix also
  6009. requests that a new frame be made for the new buffer, so that the dedicated
  6010. frame is not changed."
  6011. (interactive "P")
  6012. (let ((cbuf (current-buffer))
  6013. (cwin (selected-window))
  6014. (pos (point))
  6015. beg end level heading ibuf)
  6016. (save-excursion
  6017. (org-back-to-heading t)
  6018. (when (numberp arg)
  6019. (setq level (org-outline-level))
  6020. (if (< arg 0) (setq arg (+ level arg)))
  6021. (while (> (setq level (org-outline-level)) arg)
  6022. (outline-up-heading 1 t)))
  6023. (setq beg (point)
  6024. heading (org-get-heading))
  6025. (org-end-of-subtree t t)
  6026. (if (org-on-heading-p) (backward-char 1))
  6027. (setq end (point)))
  6028. (if (and (buffer-live-p org-last-indirect-buffer)
  6029. (not (eq org-indirect-buffer-display 'new-frame))
  6030. (not arg))
  6031. (kill-buffer org-last-indirect-buffer))
  6032. (setq ibuf (org-get-indirect-buffer cbuf)
  6033. org-last-indirect-buffer ibuf)
  6034. (cond
  6035. ((or (eq org-indirect-buffer-display 'new-frame)
  6036. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6037. (select-frame (make-frame))
  6038. (delete-other-windows)
  6039. (switch-to-buffer ibuf)
  6040. (org-set-frame-title heading))
  6041. ((eq org-indirect-buffer-display 'dedicated-frame)
  6042. (raise-frame
  6043. (select-frame (or (and org-indirect-dedicated-frame
  6044. (frame-live-p org-indirect-dedicated-frame)
  6045. org-indirect-dedicated-frame)
  6046. (setq org-indirect-dedicated-frame (make-frame)))))
  6047. (delete-other-windows)
  6048. (switch-to-buffer ibuf)
  6049. (org-set-frame-title (concat "Indirect: " heading)))
  6050. ((eq org-indirect-buffer-display 'current-window)
  6051. (switch-to-buffer ibuf))
  6052. ((eq org-indirect-buffer-display 'other-window)
  6053. (pop-to-buffer ibuf))
  6054. (t (error "Invalid value")))
  6055. (if (featurep 'xemacs)
  6056. (save-excursion (org-mode) (turn-on-font-lock)))
  6057. (narrow-to-region beg end)
  6058. (show-all)
  6059. (goto-char pos)
  6060. (and (window-live-p cwin) (select-window cwin))))
  6061. (defun org-get-indirect-buffer (&optional buffer)
  6062. (setq buffer (or buffer (current-buffer)))
  6063. (let ((n 1) (base (buffer-name buffer)) bname)
  6064. (while (buffer-live-p
  6065. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  6066. (setq n (1+ n)))
  6067. (condition-case nil
  6068. (make-indirect-buffer buffer bname 'clone)
  6069. (error (make-indirect-buffer buffer bname)))))
  6070. (defun org-set-frame-title (title)
  6071. "Set the title of the current frame to the string TITLE."
  6072. ;; FIXME: how to name a single frame in XEmacs???
  6073. (unless (featurep 'xemacs)
  6074. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6075. ;;;; Structure editing
  6076. ;;; Inserting headlines
  6077. (defun org-previous-line-empty-p ()
  6078. (save-excursion
  6079. (and (not (bobp))
  6080. (or (beginning-of-line 0) t)
  6081. (save-match-data
  6082. (looking-at "[ \t]*$")))))
  6083. (defun org-insert-heading (&optional force-heading invisible-ok)
  6084. "Insert a new heading or item with same depth at point.
  6085. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  6086. If point is at the beginning of a headline, insert a sibling before the
  6087. current headline. If point is not at the beginning, split the line,
  6088. create the new headline with the text in the current line after point
  6089. \(but see also the variable `org-M-RET-may-split-line').
  6090. When INVISIBLE-OK is set, stop at invisible headlines when going back.
  6091. This is important for non-interactive uses of the command."
  6092. (interactive "P")
  6093. (if (or (= (buffer-size) 0)
  6094. (and (not (save-excursion
  6095. (and (ignore-errors (org-back-to-heading invisible-ok))
  6096. (org-on-heading-p))))
  6097. (not (org-in-item-p))))
  6098. (progn
  6099. (insert "\n* ")
  6100. (run-hooks 'org-insert-heading-hook))
  6101. (when (or force-heading (not (org-insert-item)))
  6102. (let* ((empty-line-p nil)
  6103. (level nil)
  6104. (on-heading (org-on-heading-p))
  6105. (head (save-excursion
  6106. (condition-case nil
  6107. (progn
  6108. (org-back-to-heading invisible-ok)
  6109. (when (and (not on-heading)
  6110. (featurep 'org-inlinetask)
  6111. (integerp org-inlinetask-min-level)
  6112. (>= (length (match-string 0))
  6113. org-inlinetask-min-level))
  6114. ;; Find a heading level before the inline task
  6115. (while (and (setq level (org-up-heading-safe))
  6116. (>= level org-inlinetask-min-level)))
  6117. (if (org-on-heading-p)
  6118. (org-back-to-heading invisible-ok)
  6119. (error "This should not happen")))
  6120. (setq empty-line-p (org-previous-line-empty-p))
  6121. (match-string 0))
  6122. (error "*"))))
  6123. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6124. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6125. pos hide-previous previous-pos)
  6126. (cond
  6127. ((and (org-on-heading-p) (bolp)
  6128. (or (bobp)
  6129. (save-excursion (backward-char 1) (not (outline-invisible-p)))))
  6130. ;; insert before the current line
  6131. (open-line (if blank 2 1)))
  6132. ((and (bolp)
  6133. (not org-insert-heading-respect-content)
  6134. (or (bobp)
  6135. (save-excursion
  6136. (backward-char 1) (not (outline-invisible-p)))))
  6137. ;; insert right here
  6138. nil)
  6139. (t
  6140. ;; somewhere in the line
  6141. (save-excursion
  6142. (setq previous-pos (point-at-bol))
  6143. (end-of-line)
  6144. (setq hide-previous (outline-invisible-p)))
  6145. (and org-insert-heading-respect-content (org-show-subtree))
  6146. (let ((split
  6147. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  6148. (save-excursion
  6149. (let ((p (point)))
  6150. (goto-char (point-at-bol))
  6151. (and (looking-at org-complex-heading-regexp)
  6152. (> p (match-beginning 4)))))))
  6153. tags pos)
  6154. (cond
  6155. (org-insert-heading-respect-content
  6156. (org-end-of-subtree nil t)
  6157. (when (featurep 'org-inlinetask)
  6158. (while (and (not (eobp))
  6159. (looking-at "\\(\\*+\\)[ \t]+")
  6160. (>= (length (match-string 1))
  6161. org-inlinetask-min-level))
  6162. (org-end-of-subtree nil t)))
  6163. (or (bolp) (newline))
  6164. (or (org-previous-line-empty-p)
  6165. (and blank (newline)))
  6166. (open-line 1))
  6167. ((org-on-heading-p)
  6168. (when hide-previous
  6169. (show-children)
  6170. (org-show-entry))
  6171. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?[ \t]*$")
  6172. (setq tags (and (match-end 2) (match-string 2)))
  6173. (and (match-end 1)
  6174. (delete-region (match-beginning 1) (match-end 1)))
  6175. (setq pos (point-at-bol))
  6176. (or split (end-of-line 1))
  6177. (delete-horizontal-space)
  6178. (if (string-match "\\`\\*+\\'"
  6179. (buffer-substring (point-at-bol) (point)))
  6180. (insert " "))
  6181. (newline (if blank 2 1))
  6182. (when tags
  6183. (save-excursion
  6184. (goto-char pos)
  6185. (end-of-line 1)
  6186. (insert " " tags)
  6187. (org-set-tags nil 'align))))
  6188. (t
  6189. (or split (end-of-line 1))
  6190. (newline (if blank 2 1)))))))
  6191. (insert head) (just-one-space)
  6192. (setq pos (point))
  6193. (end-of-line 1)
  6194. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  6195. (when (and org-insert-heading-respect-content hide-previous)
  6196. (save-excursion
  6197. (goto-char previous-pos)
  6198. (hide-subtree)))
  6199. (run-hooks 'org-insert-heading-hook)))))
  6200. (defun org-get-heading (&optional no-tags)
  6201. "Return the heading of the current entry, without the stars."
  6202. (save-excursion
  6203. (org-back-to-heading t)
  6204. (if (looking-at
  6205. (if no-tags
  6206. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@#%]+:[ \t]*\\)?$")
  6207. "\\*+[ \t]+\\([^\r\n]*\\)"))
  6208. (match-string 1) "")))
  6209. (defun org-heading-components ()
  6210. "Return the components of the current heading.
  6211. This is a list with the following elements:
  6212. - the level as an integer
  6213. - the reduced level, different if `org-odd-levels-only' is set.
  6214. - the TODO keyword, or nil
  6215. - the priority character, like ?A, or nil if no priority is given
  6216. - the headline text itself, or the tags string if no headline text
  6217. - the tags string, or nil."
  6218. (save-excursion
  6219. (org-back-to-heading t)
  6220. (if (let (case-fold-search) (looking-at org-complex-heading-regexp))
  6221. (list (length (match-string 1))
  6222. (org-reduced-level (length (match-string 1)))
  6223. (org-match-string-no-properties 2)
  6224. (and (match-end 3) (aref (match-string 3) 2))
  6225. (org-match-string-no-properties 4)
  6226. (org-match-string-no-properties 5)))))
  6227. (defun org-get-entry ()
  6228. "Get the entry text, after heading, entire subtree."
  6229. (save-excursion
  6230. (org-back-to-heading t)
  6231. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6232. (defun org-insert-heading-after-current ()
  6233. "Insert a new heading with same level as current, after current subtree."
  6234. (interactive)
  6235. (org-back-to-heading)
  6236. (org-insert-heading)
  6237. (org-move-subtree-down)
  6238. (end-of-line 1))
  6239. (defun org-insert-heading-respect-content ()
  6240. (interactive)
  6241. (let ((org-insert-heading-respect-content t))
  6242. (org-insert-heading t)))
  6243. (defun org-insert-todo-heading-respect-content (&optional force-state)
  6244. (interactive "P")
  6245. (let ((org-insert-heading-respect-content t))
  6246. (org-insert-todo-heading force-state t)))
  6247. (defun org-insert-todo-heading (arg &optional force-heading)
  6248. "Insert a new heading with the same level and TODO state as current heading.
  6249. If the heading has no TODO state, or if the state is DONE, use the first
  6250. state (TODO by default). Also with prefix arg, force first state."
  6251. (interactive "P")
  6252. (when (or force-heading (not (org-insert-item 'checkbox)))
  6253. (org-insert-heading force-heading)
  6254. (save-excursion
  6255. (org-back-to-heading)
  6256. (outline-previous-heading)
  6257. (looking-at org-todo-line-regexp))
  6258. (let*
  6259. ((new-mark-x
  6260. (if (or arg
  6261. (not (match-beginning 2))
  6262. (member (match-string 2) org-done-keywords))
  6263. (car org-todo-keywords-1)
  6264. (match-string 2)))
  6265. (new-mark
  6266. (or
  6267. (run-hook-with-args-until-success
  6268. 'org-todo-get-default-hook new-mark-x nil)
  6269. new-mark-x)))
  6270. (beginning-of-line 1)
  6271. (and (looking-at "\\*+ ") (goto-char (match-end 0))
  6272. (if org-treat-insert-todo-heading-as-state-change
  6273. (org-todo new-mark)
  6274. (insert new-mark " "))))
  6275. (when org-provide-todo-statistics
  6276. (org-update-parent-todo-statistics))))
  6277. (defun org-insert-subheading (arg)
  6278. "Insert a new subheading and demote it.
  6279. Works for outline headings and for plain lists alike."
  6280. (interactive "P")
  6281. (org-insert-heading arg)
  6282. (cond
  6283. ((org-on-heading-p) (org-do-demote))
  6284. ((org-at-item-p) (org-indent-item))))
  6285. (defun org-insert-todo-subheading (arg)
  6286. "Insert a new subheading with TODO keyword or checkbox and demote it.
  6287. Works for outline headings and for plain lists alike."
  6288. (interactive "P")
  6289. (org-insert-todo-heading arg)
  6290. (cond
  6291. ((org-on-heading-p) (org-do-demote))
  6292. ((org-at-item-p) (org-indent-item))))
  6293. ;;; Promotion and Demotion
  6294. (defvar org-after-demote-entry-hook nil
  6295. "Hook run after an entry has been demoted.
  6296. The cursor will be at the beginning of the entry.
  6297. When a subtree is being demoted, the hook will be called for each node.")
  6298. (defvar org-after-promote-entry-hook nil
  6299. "Hook run after an entry has been promoted.
  6300. The cursor will be at the beginning of the entry.
  6301. When a subtree is being promoted, the hook will be called for each node.")
  6302. (defun org-promote-subtree ()
  6303. "Promote the entire subtree.
  6304. See also `org-promote'."
  6305. (interactive)
  6306. (save-excursion
  6307. (org-with-limited-levels (org-map-tree 'org-promote)))
  6308. (org-fix-position-after-promote))
  6309. (defun org-demote-subtree ()
  6310. "Demote the entire subtree. See `org-demote'.
  6311. See also `org-promote'."
  6312. (interactive)
  6313. (save-excursion
  6314. (org-with-limited-levels (org-map-tree 'org-demote)))
  6315. (org-fix-position-after-promote))
  6316. (defun org-do-promote ()
  6317. "Promote the current heading higher up the tree.
  6318. If the region is active in `transient-mark-mode', promote all headings
  6319. in the region."
  6320. (interactive)
  6321. (save-excursion
  6322. (if (org-region-active-p)
  6323. (org-map-region 'org-promote (region-beginning) (region-end))
  6324. (org-promote)))
  6325. (org-fix-position-after-promote))
  6326. (defun org-do-demote ()
  6327. "Demote the current heading lower down the tree.
  6328. If the region is active in `transient-mark-mode', demote all headings
  6329. in the region."
  6330. (interactive)
  6331. (save-excursion
  6332. (if (org-region-active-p)
  6333. (org-map-region 'org-demote (region-beginning) (region-end))
  6334. (org-demote)))
  6335. (org-fix-position-after-promote))
  6336. (defun org-fix-position-after-promote ()
  6337. "Make sure that after pro/demotion cursor position is right."
  6338. (let ((pos (point)))
  6339. (when (save-excursion
  6340. (beginning-of-line 1)
  6341. (looking-at org-todo-line-regexp)
  6342. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  6343. (cond ((eobp) (insert " "))
  6344. ((eolp) (insert " "))
  6345. ((equal (char-after) ?\ ) (forward-char 1))))))
  6346. (defun org-current-level ()
  6347. "Return the level of the current entry, or nil if before the first headline.
  6348. The level is the number of stars at the beginning of the headline."
  6349. (save-excursion
  6350. (org-with-limited-levels
  6351. (ignore-errors
  6352. (org-back-to-heading t)
  6353. (funcall outline-level)))))
  6354. (defun org-get-previous-line-level ()
  6355. "Return the outline depth of the last headline before the current line.
  6356. Returns 0 for the first headline in the buffer, and nil if before the
  6357. first headline."
  6358. (let ((current-level (org-current-level))
  6359. (prev-level (when (> (line-number-at-pos) 1)
  6360. (save-excursion
  6361. (beginning-of-line 0)
  6362. (org-current-level)))))
  6363. (cond ((null current-level) nil) ; Before first headline
  6364. ((null prev-level) 0) ; At first headline
  6365. (prev-level))))
  6366. (defun org-reduced-level (l)
  6367. "Compute the effective level of a heading.
  6368. This takes into account the setting of `org-odd-levels-only'."
  6369. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  6370. (defun org-level-increment ()
  6371. "Return the number of stars that will be added or removed at a
  6372. time to headlines when structure editing, based on the value of
  6373. `org-odd-levels-only'."
  6374. (if org-odd-levels-only 2 1))
  6375. (defun org-get-valid-level (level &optional change)
  6376. "Rectify a level change under the influence of `org-odd-levels-only'
  6377. LEVEL is a current level, CHANGE is by how much the level should be
  6378. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  6379. even level numbers will become the next higher odd number."
  6380. (if org-odd-levels-only
  6381. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  6382. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  6383. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  6384. (max 1 (+ level (or change 0)))))
  6385. (if (boundp 'define-obsolete-function-alias)
  6386. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  6387. (define-obsolete-function-alias 'org-get-legal-level
  6388. 'org-get-valid-level)
  6389. (define-obsolete-function-alias 'org-get-legal-level
  6390. 'org-get-valid-level "23.1")))
  6391. (defun org-promote ()
  6392. "Promote the current heading higher up the tree.
  6393. If the region is active in `transient-mark-mode', promote all headings
  6394. in the region."
  6395. (org-back-to-heading t)
  6396. (let* ((level (save-match-data (funcall outline-level)))
  6397. (after-change-functions (remove 'flyspell-after-change-function
  6398. after-change-functions))
  6399. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  6400. (diff (abs (- level (length up-head) -1))))
  6401. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  6402. (replace-match up-head nil t)
  6403. ;; Fixup tag positioning
  6404. (and org-auto-align-tags (org-set-tags nil t))
  6405. (if org-adapt-indentation (org-fixup-indentation (- diff)))
  6406. (run-hooks 'org-after-promote-entry-hook)))
  6407. (defun org-demote ()
  6408. "Demote the current heading lower down the tree.
  6409. If the region is active in `transient-mark-mode', demote all headings
  6410. in the region."
  6411. (org-back-to-heading t)
  6412. (let* ((level (save-match-data (funcall outline-level)))
  6413. (after-change-functions (remove 'flyspell-after-change-function
  6414. after-change-functions))
  6415. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  6416. (diff (abs (- level (length down-head) -1))))
  6417. (replace-match down-head nil t)
  6418. ;; Fixup tag positioning
  6419. (and org-auto-align-tags (org-set-tags nil t))
  6420. (if org-adapt-indentation (org-fixup-indentation diff))
  6421. (run-hooks 'org-after-demote-entry-hook)))
  6422. (defun org-cycle-level ()
  6423. "Cycle the level of an empty headline through possible states.
  6424. This goes first to child, then to parent, level, then up the hierarchy.
  6425. After top level, it switches back to sibling level."
  6426. (interactive)
  6427. (let ((org-adapt-indentation nil))
  6428. (when (org-point-at-end-of-empty-headline)
  6429. (setq this-command 'org-cycle-level) ; Only needed for caching
  6430. (let ((cur-level (org-current-level))
  6431. (prev-level (org-get-previous-line-level)))
  6432. (cond
  6433. ;; If first headline in file, promote to top-level.
  6434. ((= prev-level 0)
  6435. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6436. do (org-do-promote)))
  6437. ;; If same level as prev, demote one.
  6438. ((= prev-level cur-level)
  6439. (org-do-demote))
  6440. ;; If parent is top-level, promote to top level if not already.
  6441. ((= prev-level 1)
  6442. (loop repeat (/ (- cur-level 1) (org-level-increment))
  6443. do (org-do-promote)))
  6444. ;; If top-level, return to prev-level.
  6445. ((= cur-level 1)
  6446. (loop repeat (/ (- prev-level 1) (org-level-increment))
  6447. do (org-do-demote)))
  6448. ;; If less than prev-level, promote one.
  6449. ((< cur-level prev-level)
  6450. (org-do-promote))
  6451. ;; If deeper than prev-level, promote until higher than
  6452. ;; prev-level.
  6453. ((> cur-level prev-level)
  6454. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  6455. do (org-do-promote))))
  6456. t))))
  6457. (defun org-map-tree (fun)
  6458. "Call FUN for every heading underneath the current one."
  6459. (org-back-to-heading)
  6460. (let ((level (funcall outline-level)))
  6461. (save-excursion
  6462. (funcall fun)
  6463. (while (and (progn
  6464. (outline-next-heading)
  6465. (> (funcall outline-level) level))
  6466. (not (eobp)))
  6467. (funcall fun)))))
  6468. (defun org-map-region (fun beg end)
  6469. "Call FUN for every heading between BEG and END."
  6470. (let ((org-ignore-region t))
  6471. (save-excursion
  6472. (setq end (copy-marker end))
  6473. (goto-char beg)
  6474. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  6475. (< (point) end))
  6476. (funcall fun))
  6477. (while (and (progn
  6478. (outline-next-heading)
  6479. (< (point) end))
  6480. (not (eobp)))
  6481. (funcall fun)))))
  6482. (defun org-fixup-indentation (diff)
  6483. "Change the indentation in the current entry by DIFF.
  6484. However, if any line in the current entry has no indentation, or if it
  6485. would end up with no indentation after the change, nothing at all is done."
  6486. (save-excursion
  6487. (let ((end (save-excursion (outline-next-heading)
  6488. (point-marker)))
  6489. (prohibit (if (> diff 0)
  6490. "^\\S-"
  6491. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  6492. col)
  6493. (unless (save-excursion (end-of-line 1)
  6494. (re-search-forward prohibit end t))
  6495. (while (and (< (point) end)
  6496. (re-search-forward "^[ \t]+" end t))
  6497. (goto-char (match-end 0))
  6498. (setq col (current-column))
  6499. (if (< diff 0) (replace-match ""))
  6500. (org-indent-to-column (+ diff col))))
  6501. (move-marker end nil))))
  6502. (defun org-convert-to-odd-levels ()
  6503. "Convert an org-mode file with all levels allowed to one with odd levels.
  6504. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  6505. level 5 etc."
  6506. (interactive)
  6507. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  6508. (let ((outline-regexp org-outline-regexp)
  6509. (outline-level 'org-outline-level)
  6510. (org-odd-levels-only nil) n)
  6511. (save-excursion
  6512. (goto-char (point-min))
  6513. (while (re-search-forward "^\\*\\*+ " nil t)
  6514. (setq n (- (length (match-string 0)) 2))
  6515. (while (>= (setq n (1- n)) 0)
  6516. (org-demote))
  6517. (end-of-line 1))))))
  6518. (defun org-convert-to-oddeven-levels ()
  6519. "Convert an org-mode file with only odd levels to one with odd/even levels.
  6520. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  6521. file contains a section with an even level, conversion would
  6522. destroy the structure of the file. An error is signaled in this
  6523. case."
  6524. (interactive)
  6525. (goto-char (point-min))
  6526. ;; First check if there are no even levels
  6527. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  6528. (org-show-context t)
  6529. (error "Not all levels are odd in this file. Conversion not possible"))
  6530. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  6531. (let ((outline-regexp org-outline-regexp)
  6532. (outline-level 'org-outline-level)
  6533. (org-odd-levels-only nil) n)
  6534. (save-excursion
  6535. (goto-char (point-min))
  6536. (while (re-search-forward "^\\*\\*+ " nil t)
  6537. (setq n (/ (1- (length (match-string 0))) 2))
  6538. (while (>= (setq n (1- n)) 0)
  6539. (org-promote))
  6540. (end-of-line 1))))))
  6541. (defun org-tr-level (n)
  6542. "Make N odd if required."
  6543. (if org-odd-levels-only (1+ (/ n 2)) n))
  6544. ;;; Vertical tree motion, cutting and pasting of subtrees
  6545. (defun org-move-subtree-up (&optional arg)
  6546. "Move the current subtree up past ARG headlines of the same level."
  6547. (interactive "p")
  6548. (org-move-subtree-down (- (prefix-numeric-value arg))))
  6549. (defun org-move-subtree-down (&optional arg)
  6550. "Move the current subtree down past ARG headlines of the same level."
  6551. (interactive "p")
  6552. (setq arg (prefix-numeric-value arg))
  6553. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  6554. 'org-get-last-sibling))
  6555. (ins-point (make-marker))
  6556. (cnt (abs arg))
  6557. (col (current-column))
  6558. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  6559. ;; Select the tree
  6560. (org-back-to-heading)
  6561. (setq beg0 (point))
  6562. (save-excursion
  6563. (setq ne-beg (org-back-over-empty-lines))
  6564. (setq beg (point)))
  6565. (save-match-data
  6566. (save-excursion (outline-end-of-heading)
  6567. (setq folded (outline-invisible-p)))
  6568. (outline-end-of-subtree))
  6569. (outline-next-heading)
  6570. (setq ne-end (org-back-over-empty-lines))
  6571. (setq end (point))
  6572. (goto-char beg0)
  6573. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  6574. ;; include less whitespace
  6575. (save-excursion
  6576. (goto-char beg)
  6577. (forward-line (- ne-beg ne-end))
  6578. (setq beg (point))))
  6579. ;; Find insertion point, with error handling
  6580. (while (> cnt 0)
  6581. (or (and (funcall movfunc) (looking-at outline-regexp))
  6582. (progn (goto-char beg0)
  6583. (error "Cannot move past superior level or buffer limit")))
  6584. (setq cnt (1- cnt)))
  6585. (if (> arg 0)
  6586. ;; Moving forward - still need to move over subtree
  6587. (progn (org-end-of-subtree t t)
  6588. (save-excursion
  6589. (org-back-over-empty-lines)
  6590. (or (bolp) (newline)))))
  6591. (setq ne-ins (org-back-over-empty-lines))
  6592. (move-marker ins-point (point))
  6593. (setq txt (buffer-substring beg end))
  6594. (org-save-markers-in-region beg end)
  6595. (delete-region beg end)
  6596. (org-remove-empty-overlays-at beg)
  6597. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  6598. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  6599. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  6600. (let ((bbb (point)))
  6601. (insert-before-markers txt)
  6602. (org-reinstall-markers-in-region bbb)
  6603. (move-marker ins-point bbb))
  6604. (or (bolp) (insert "\n"))
  6605. (setq ins-end (point))
  6606. (goto-char ins-point)
  6607. (org-skip-whitespace)
  6608. (when (and (< arg 0)
  6609. (org-first-sibling-p)
  6610. (> ne-ins ne-beg))
  6611. ;; Move whitespace back to beginning
  6612. (save-excursion
  6613. (goto-char ins-end)
  6614. (let ((kill-whole-line t))
  6615. (kill-line (- ne-ins ne-beg)) (point)))
  6616. (insert (make-string (- ne-ins ne-beg) ?\n)))
  6617. (move-marker ins-point nil)
  6618. (if folded
  6619. (hide-subtree)
  6620. (org-show-entry)
  6621. (show-children)
  6622. (org-cycle-hide-drawers 'children))
  6623. (org-clean-visibility-after-subtree-move)
  6624. ;; move back to the initial column we were at
  6625. (move-to-column col)))
  6626. (defvar org-subtree-clip ""
  6627. "Clipboard for cut and paste of subtrees.
  6628. This is actually only a copy of the kill, because we use the normal kill
  6629. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  6630. (defvar org-subtree-clip-folded nil
  6631. "Was the last copied subtree folded?
  6632. This is used to fold the tree back after pasting.")
  6633. (defun org-cut-subtree (&optional n)
  6634. "Cut the current subtree into the clipboard.
  6635. With prefix arg N, cut this many sequential subtrees.
  6636. This is a short-hand for marking the subtree and then cutting it."
  6637. (interactive "p")
  6638. (org-copy-subtree n 'cut))
  6639. (defun org-copy-subtree (&optional n cut force-store-markers)
  6640. "Cut the current subtree into the clipboard.
  6641. With prefix arg N, cut this many sequential subtrees.
  6642. This is a short-hand for marking the subtree and then copying it.
  6643. If CUT is non-nil, actually cut the subtree.
  6644. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  6645. of some markers in the region, even if CUT is non-nil. This is
  6646. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  6647. (interactive "p")
  6648. (let (beg end folded (beg0 (point)))
  6649. (if (interactive-p)
  6650. (org-back-to-heading nil) ; take what looks like a subtree
  6651. (org-back-to-heading t)) ; take what is really there
  6652. (org-back-over-empty-lines)
  6653. (setq beg (point))
  6654. (skip-chars-forward " \t\r\n")
  6655. (save-match-data
  6656. (save-excursion (outline-end-of-heading)
  6657. (setq folded (outline-invisible-p)))
  6658. (condition-case nil
  6659. (org-forward-same-level (1- n) t)
  6660. (error nil))
  6661. (org-end-of-subtree t t))
  6662. (org-back-over-empty-lines)
  6663. (setq end (point))
  6664. (goto-char beg0)
  6665. (when (> end beg)
  6666. (setq org-subtree-clip-folded folded)
  6667. (when (or cut force-store-markers)
  6668. (org-save-markers-in-region beg end))
  6669. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  6670. (setq org-subtree-clip (current-kill 0))
  6671. (message "%s: Subtree(s) with %d characters"
  6672. (if cut "Cut" "Copied")
  6673. (length org-subtree-clip)))))
  6674. (defun org-paste-subtree (&optional level tree for-yank)
  6675. "Paste the clipboard as a subtree, with modification of headline level.
  6676. The entire subtree is promoted or demoted in order to match a new headline
  6677. level.
  6678. If the cursor is at the beginning of a headline, the same level as
  6679. that headline is used to paste the tree
  6680. If not, the new level is derived from the *visible* headings
  6681. before and after the insertion point, and taken to be the inferior headline
  6682. level of the two. So if the previous visible heading is level 3 and the
  6683. next is level 4 (or vice versa), level 4 will be used for insertion.
  6684. This makes sure that the subtree remains an independent subtree and does
  6685. not swallow low level entries.
  6686. You can also force a different level, either by using a numeric prefix
  6687. argument, or by inserting the heading marker by hand. For example, if the
  6688. cursor is after \"*****\", then the tree will be shifted to level 5.
  6689. If optional TREE is given, use this text instead of the kill ring.
  6690. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  6691. move back over whitespace before inserting, and move point to the end of
  6692. the inserted text when done."
  6693. (interactive "P")
  6694. (setq tree (or tree (and kill-ring (current-kill 0))))
  6695. (unless (org-kill-is-subtree-p tree)
  6696. (error "%s"
  6697. (substitute-command-keys
  6698. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  6699. (let* ((visp (not (outline-invisible-p)))
  6700. (txt tree)
  6701. (^re (concat "^\\(" outline-regexp "\\)"))
  6702. (re (concat "\\(" outline-regexp "\\)"))
  6703. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  6704. (old-level (if (string-match ^re txt)
  6705. (- (match-end 0) (match-beginning 0) 1)
  6706. -1))
  6707. (force-level (cond (level (prefix-numeric-value level))
  6708. ((and (looking-at "[ \t]*$")
  6709. (string-match
  6710. ^re_ (buffer-substring
  6711. (point-at-bol) (point))))
  6712. (- (match-end 1) (match-beginning 1)))
  6713. ((and (bolp)
  6714. (looking-at org-outline-regexp))
  6715. (- (match-end 0) (point) 1))
  6716. (t nil)))
  6717. (previous-level (save-excursion
  6718. (condition-case nil
  6719. (progn
  6720. (outline-previous-visible-heading 1)
  6721. (if (looking-at re)
  6722. (- (match-end 0) (match-beginning 0) 1)
  6723. 1))
  6724. (error 1))))
  6725. (next-level (save-excursion
  6726. (condition-case nil
  6727. (progn
  6728. (or (looking-at outline-regexp)
  6729. (outline-next-visible-heading 1))
  6730. (if (looking-at re)
  6731. (- (match-end 0) (match-beginning 0) 1)
  6732. 1))
  6733. (error 1))))
  6734. (new-level (or force-level (max previous-level next-level)))
  6735. (shift (if (or (= old-level -1)
  6736. (= new-level -1)
  6737. (= old-level new-level))
  6738. 0
  6739. (- new-level old-level)))
  6740. (delta (if (> shift 0) -1 1))
  6741. (func (if (> shift 0) 'org-demote 'org-promote))
  6742. (org-odd-levels-only nil)
  6743. beg end newend)
  6744. ;; Remove the forced level indicator
  6745. (if force-level
  6746. (delete-region (point-at-bol) (point)))
  6747. ;; Paste
  6748. (beginning-of-line 1)
  6749. (unless for-yank (org-back-over-empty-lines))
  6750. (setq beg (point))
  6751. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  6752. (insert-before-markers txt)
  6753. (unless (string-match "\n\\'" txt) (insert "\n"))
  6754. (setq newend (point))
  6755. (org-reinstall-markers-in-region beg)
  6756. (setq end (point))
  6757. (goto-char beg)
  6758. (skip-chars-forward " \t\n\r")
  6759. (setq beg (point))
  6760. (if (and (outline-invisible-p) visp)
  6761. (save-excursion (outline-show-heading)))
  6762. ;; Shift if necessary
  6763. (unless (= shift 0)
  6764. (save-restriction
  6765. (narrow-to-region beg end)
  6766. (while (not (= shift 0))
  6767. (org-map-region func (point-min) (point-max))
  6768. (setq shift (+ delta shift)))
  6769. (goto-char (point-min))
  6770. (setq newend (point-max))))
  6771. (when (or (interactive-p) for-yank)
  6772. (message "Clipboard pasted as level %d subtree" new-level))
  6773. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  6774. kill-ring
  6775. (eq org-subtree-clip (current-kill 0))
  6776. org-subtree-clip-folded)
  6777. ;; The tree was folded before it was killed/copied
  6778. (hide-subtree))
  6779. (and for-yank (goto-char newend))))
  6780. (defun org-kill-is-subtree-p (&optional txt)
  6781. "Check if the current kill is an outline subtree, or a set of trees.
  6782. Returns nil if kill does not start with a headline, or if the first
  6783. headline level is not the largest headline level in the tree.
  6784. So this will actually accept several entries of equal levels as well,
  6785. which is OK for `org-paste-subtree'.
  6786. If optional TXT is given, check this string instead of the current kill."
  6787. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  6788. (start-level (and kill
  6789. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  6790. org-outline-regexp "\\)")
  6791. kill)
  6792. (- (match-end 2) (match-beginning 2) 1)))
  6793. (re (concat "^" org-outline-regexp))
  6794. (start (1+ (or (match-beginning 2) -1))))
  6795. (if (not start-level)
  6796. (progn
  6797. nil) ;; does not even start with a heading
  6798. (catch 'exit
  6799. (while (setq start (string-match re kill (1+ start)))
  6800. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  6801. (throw 'exit nil)))
  6802. t))))
  6803. (defvar org-markers-to-move nil
  6804. "Markers that should be moved with a cut-and-paste operation.
  6805. Those markers are stored together with their positions relative to
  6806. the start of the region.")
  6807. (defun org-save-markers-in-region (beg end)
  6808. "Check markers in region.
  6809. If these markers are between BEG and END, record their position relative
  6810. to BEG, so that after moving the block of text, we can put the markers back
  6811. into place.
  6812. This function gets called just before an entry or tree gets cut from the
  6813. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  6814. called immediately, to move the markers with the entries."
  6815. (setq org-markers-to-move nil)
  6816. (when (featurep 'org-clock)
  6817. (org-clock-save-markers-for-cut-and-paste beg end))
  6818. (when (featurep 'org-agenda)
  6819. (org-agenda-save-markers-for-cut-and-paste beg end)))
  6820. (defun org-check-and-save-marker (marker beg end)
  6821. "Check if MARKER is between BEG and END.
  6822. If yes, remember the marker and the distance to BEG."
  6823. (when (and (marker-buffer marker)
  6824. (equal (marker-buffer marker) (current-buffer)))
  6825. (if (and (>= marker beg) (< marker end))
  6826. (push (cons marker (- marker beg)) org-markers-to-move))))
  6827. (defun org-reinstall-markers-in-region (beg)
  6828. "Move all remembered markers to their position relative to BEG."
  6829. (mapc (lambda (x)
  6830. (move-marker (car x) (+ beg (cdr x))))
  6831. org-markers-to-move)
  6832. (setq org-markers-to-move nil))
  6833. (defun org-narrow-to-subtree ()
  6834. "Narrow buffer to the current subtree."
  6835. (interactive)
  6836. (save-excursion
  6837. (save-match-data
  6838. (org-with-limited-levels
  6839. (narrow-to-region
  6840. (progn (org-back-to-heading t) (point))
  6841. (progn (org-end-of-subtree t t)
  6842. (if (and (org-on-heading-p) (not (eobp))) (backward-char 1))
  6843. (point)))))))
  6844. (defun org-narrow-to-block ()
  6845. "Narrow buffer to the current block."
  6846. (interactive)
  6847. (let ((bstart "^[ \t]*#\\+begin")
  6848. (bend "[ \t]*#\\+end")
  6849. (case-fold-search t) ;; allow #+BEGIN
  6850. b_start b_end)
  6851. (if (org-in-regexps-block-p bstart bend)
  6852. (progn
  6853. (save-excursion (re-search-backward bstart nil t)
  6854. (setq b_start (match-beginning 0)))
  6855. (save-excursion (re-search-forward bend nil t)
  6856. (setq b_end (match-end 0)))
  6857. (narrow-to-region b_start b_end))
  6858. (error "Not in a block"))))
  6859. (eval-when-compile
  6860. (defvar org-property-drawer-re))
  6861. (defvar org-property-start-re) ;; defined below
  6862. (defun org-clone-subtree-with-time-shift (n &optional shift)
  6863. "Clone the task (subtree) at point N times.
  6864. The clones will be inserted as siblings.
  6865. In interactive use, the user will be prompted for the number of
  6866. clones to be produced, and for a time SHIFT, which may be a
  6867. repeater as used in time stamps, for example `+3d'.
  6868. When a valid repeater is given and the entry contains any time
  6869. stamps, the clones will become a sequence in time, with time
  6870. stamps in the subtree shifted for each clone produced. If SHIFT
  6871. is nil or the empty string, time stamps will be left alone. The
  6872. ID property of the original subtree is removed.
  6873. If the original subtree did contain time stamps with a repeater,
  6874. the following will happen:
  6875. - the repeater will be removed in each clone
  6876. - an additional clone will be produced, with the current, unshifted
  6877. date(s) in the entry.
  6878. - the original entry will be placed *after* all the clones, with
  6879. repeater intact.
  6880. - the start days in the repeater in the original entry will be shifted
  6881. to past the last clone.
  6882. I this way you can spell out a number of instances of a repeating task,
  6883. and still retain the repeater to cover future instances of the task."
  6884. (interactive "nNumber of clones to produce: \nsDate shift per clone (e.g. +1w, empty to copy unchanged): ")
  6885. (let (beg end template task idprop
  6886. shift-n shift-what doshift nmin nmax (n-no-remove -1))
  6887. (if (not (and (integerp n) (> n 0)))
  6888. (error "Invalid number of replications %s" n))
  6889. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  6890. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([dwmy]\\)[ \t]*\\'"
  6891. shift)))
  6892. (error "Invalid shift specification %s" shift))
  6893. (when doshift
  6894. (setq shift-n (string-to-number (match-string 1 shift))
  6895. shift-what (cdr (assoc (match-string 2 shift)
  6896. '(("d" . day) ("w" . week)
  6897. ("m" . month) ("y" . year))))))
  6898. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  6899. (setq nmin 1 nmax n)
  6900. (org-back-to-heading t)
  6901. (setq beg (point))
  6902. (setq idprop (org-entry-get nil "ID"))
  6903. (org-end-of-subtree t t)
  6904. (or (bolp) (insert "\n"))
  6905. (setq end (point))
  6906. (setq template (buffer-substring beg end))
  6907. (when (and doshift
  6908. (string-match "<[^<>\n]+ \\+[0-9]+[dwmy][^<>\n]*>" template))
  6909. (delete-region beg end)
  6910. (setq end beg)
  6911. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  6912. (goto-char end)
  6913. (loop for n from nmin to nmax do
  6914. ;; prepare clone
  6915. (with-temp-buffer
  6916. (insert template)
  6917. (org-mode)
  6918. (goto-char (point-min))
  6919. (and idprop (if org-clone-delete-id
  6920. (org-entry-delete nil "ID")
  6921. (org-id-get-create t)))
  6922. (while (re-search-forward org-property-start-re nil t)
  6923. (org-remove-empty-drawer-at "PROPERTIES" (point)))
  6924. (goto-char (point-min))
  6925. (when doshift
  6926. (while (re-search-forward org-ts-regexp-both nil t)
  6927. (org-timestamp-change (* n shift-n) shift-what))
  6928. (unless (= n n-no-remove)
  6929. (goto-char (point-min))
  6930. (while (re-search-forward org-ts-regexp nil t)
  6931. (save-excursion
  6932. (goto-char (match-beginning 0))
  6933. (if (looking-at "<[^<>\n]+\\( +\\+[0-9]+[dwmy]\\)")
  6934. (delete-region (match-beginning 1) (match-end 1)))))))
  6935. (setq task (buffer-string)))
  6936. (insert task))
  6937. (goto-char beg)))
  6938. ;;; Outline Sorting
  6939. (defun org-sort (with-case)
  6940. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  6941. Optional argument WITH-CASE means sort case-sensitively.
  6942. With a double prefix argument, also remove duplicate entries."
  6943. (interactive "P")
  6944. (cond
  6945. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  6946. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  6947. (t
  6948. (org-call-with-arg 'org-sort-entries with-case))))
  6949. (defun org-sort-remove-invisible (s)
  6950. (remove-text-properties 0 (length s) org-rm-props s)
  6951. (while (string-match org-bracket-link-regexp s)
  6952. (setq s (replace-match (if (match-end 2)
  6953. (match-string 3 s)
  6954. (match-string 1 s)) t t s)))
  6955. s)
  6956. (defvar org-priority-regexp) ; defined later in the file
  6957. (defvar org-after-sorting-entries-or-items-hook nil
  6958. "Hook that is run after a bunch of entries or items have been sorted.
  6959. When children are sorted, the cursor is in the parent line when this
  6960. hook gets called. When a region or a plain list is sorted, the cursor
  6961. will be in the first entry of the sorted region/list.")
  6962. (defun org-sort-entries
  6963. (&optional with-case sorting-type getkey-func compare-func property)
  6964. "Sort entries on a certain level of an outline tree.
  6965. If there is an active region, the entries in the region are sorted.
  6966. Else, if the cursor is before the first entry, sort the top-level items.
  6967. Else, the children of the entry at point are sorted.
  6968. Sorting can be alphabetically, numerically, by date/time as given by
  6969. a time stamp, by a property or by priority.
  6970. The command prompts for the sorting type unless it has been given to the
  6971. function through the SORTING-TYPE argument, which needs to be a character,
  6972. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?r ?R ?f ?F). Here is the
  6973. precise meaning of each character:
  6974. n Numerically, by converting the beginning of the entry/item to a number.
  6975. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  6976. t By date/time, either the first active time stamp in the entry, or, if
  6977. none exist, by the first inactive one.
  6978. s By the scheduled date/time.
  6979. d By deadline date/time.
  6980. c By creation time, which is assumed to be the first inactive time stamp
  6981. at the beginning of a line.
  6982. p By priority according to the cookie.
  6983. r By the value of a property.
  6984. Capital letters will reverse the sort order.
  6985. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  6986. called with point at the beginning of the record. It must return either
  6987. a string or a number that should serve as the sorting key for that record.
  6988. Comparing entries ignores case by default. However, with an optional argument
  6989. WITH-CASE, the sorting considers case as well."
  6990. (interactive "P")
  6991. (let ((case-func (if with-case 'identity 'downcase))
  6992. start beg end stars re re2
  6993. txt what tmp)
  6994. ;; Find beginning and end of region to sort
  6995. (cond
  6996. ((org-region-active-p)
  6997. ;; we will sort the region
  6998. (setq end (region-end)
  6999. what "region")
  7000. (goto-char (region-beginning))
  7001. (if (not (org-on-heading-p)) (outline-next-heading))
  7002. (setq start (point)))
  7003. ((or (org-on-heading-p)
  7004. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  7005. ;; we will sort the children of the current headline
  7006. (org-back-to-heading)
  7007. (setq start (point)
  7008. end (progn (org-end-of-subtree t t)
  7009. (or (bolp) (insert "\n"))
  7010. (org-back-over-empty-lines)
  7011. (point))
  7012. what "children")
  7013. (goto-char start)
  7014. (show-subtree)
  7015. (outline-next-heading))
  7016. (t
  7017. ;; we will sort the top-level entries in this file
  7018. (goto-char (point-min))
  7019. (or (org-on-heading-p) (outline-next-heading))
  7020. (setq start (point))
  7021. (goto-char (point-max))
  7022. (beginning-of-line 1)
  7023. (when (looking-at ".*?\\S-")
  7024. ;; File ends in a non-white line
  7025. (end-of-line 1)
  7026. (insert "\n"))
  7027. (setq end (point-max))
  7028. (setq what "top-level")
  7029. (goto-char start)
  7030. (show-all)))
  7031. (setq beg (point))
  7032. (if (>= beg end) (error "Nothing to sort"))
  7033. (looking-at "\\(\\*+\\)")
  7034. (setq stars (match-string 1)
  7035. re (concat "^" (regexp-quote stars) " +")
  7036. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7037. txt (buffer-substring beg end))
  7038. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  7039. (if (and (not (equal stars "*")) (string-match re2 txt))
  7040. (error "Region to sort contains a level above the first entry"))
  7041. (unless sorting-type
  7042. (message
  7043. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7044. [t]ime [s]cheduled [d]eadline [c]reated
  7045. A/N/T/S/D/C/P/O/F means reversed:"
  7046. what)
  7047. (setq sorting-type (read-char-exclusive))
  7048. (and (= (downcase sorting-type) ?f)
  7049. (setq getkey-func
  7050. (org-icompleting-read "Sort using function: "
  7051. obarray 'fboundp t nil nil))
  7052. (setq getkey-func (intern getkey-func)))
  7053. (and (= (downcase sorting-type) ?r)
  7054. (setq property
  7055. (org-icompleting-read "Property: "
  7056. (mapcar 'list (org-buffer-property-keys t))
  7057. nil t))))
  7058. (message "Sorting entries...")
  7059. (save-restriction
  7060. (narrow-to-region start end)
  7061. (let ((dcst (downcase sorting-type))
  7062. (case-fold-search nil)
  7063. (now (current-time)))
  7064. (sort-subr
  7065. (/= dcst sorting-type)
  7066. ;; This function moves to the beginning character of the "record" to
  7067. ;; be sorted.
  7068. (lambda nil
  7069. (if (re-search-forward re nil t)
  7070. (goto-char (match-beginning 0))
  7071. (goto-char (point-max))))
  7072. ;; This function moves to the last character of the "record" being
  7073. ;; sorted.
  7074. (lambda nil
  7075. (save-match-data
  7076. (condition-case nil
  7077. (outline-forward-same-level 1)
  7078. (error
  7079. (goto-char (point-max))))))
  7080. ;; This function returns the value that gets sorted against.
  7081. (lambda nil
  7082. (cond
  7083. ((= dcst ?n)
  7084. (if (looking-at org-complex-heading-regexp)
  7085. (string-to-number (match-string 4))
  7086. nil))
  7087. ((= dcst ?a)
  7088. (if (looking-at org-complex-heading-regexp)
  7089. (funcall case-func (match-string 4))
  7090. nil))
  7091. ((= dcst ?t)
  7092. (let ((end (save-excursion (outline-next-heading) (point))))
  7093. (if (or (re-search-forward org-ts-regexp end t)
  7094. (re-search-forward org-ts-regexp-both end t))
  7095. (org-time-string-to-seconds (match-string 0))
  7096. (org-float-time now))))
  7097. ((= dcst ?c)
  7098. (let ((end (save-excursion (outline-next-heading) (point))))
  7099. (if (re-search-forward
  7100. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7101. end t)
  7102. (org-time-string-to-seconds (match-string 0))
  7103. (org-float-time now))))
  7104. ((= dcst ?s)
  7105. (let ((end (save-excursion (outline-next-heading) (point))))
  7106. (if (re-search-forward org-scheduled-time-regexp end t)
  7107. (org-time-string-to-seconds (match-string 1))
  7108. (org-float-time now))))
  7109. ((= dcst ?d)
  7110. (let ((end (save-excursion (outline-next-heading) (point))))
  7111. (if (re-search-forward org-deadline-time-regexp end t)
  7112. (org-time-string-to-seconds (match-string 1))
  7113. (org-float-time now))))
  7114. ((= dcst ?p)
  7115. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  7116. (string-to-char (match-string 2))
  7117. org-default-priority))
  7118. ((= dcst ?r)
  7119. (or (org-entry-get nil property) ""))
  7120. ((= dcst ?o)
  7121. (if (looking-at org-complex-heading-regexp)
  7122. (- 9999 (length (member (match-string 2)
  7123. org-todo-keywords-1)))))
  7124. ((= dcst ?f)
  7125. (if getkey-func
  7126. (progn
  7127. (setq tmp (funcall getkey-func))
  7128. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  7129. tmp)
  7130. (error "Invalid key function `%s'" getkey-func)))
  7131. (t (error "Invalid sorting type `%c'" sorting-type))))
  7132. nil
  7133. (cond
  7134. ((= dcst ?a) 'string<)
  7135. ((= dcst ?f) compare-func)
  7136. ((member dcst '(?p ?t ?s ?d ?c)) '<)
  7137. (t nil)))))
  7138. (run-hooks 'org-after-sorting-entries-or-items-hook)
  7139. (message "Sorting entries...done")))
  7140. (defun org-do-sort (table what &optional with-case sorting-type)
  7141. "Sort TABLE of WHAT according to SORTING-TYPE.
  7142. The user will be prompted for the SORTING-TYPE if the call to this
  7143. function does not specify it. WHAT is only for the prompt, to indicate
  7144. what is being sorted. The sorting key will be extracted from
  7145. the car of the elements of the table.
  7146. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  7147. (unless sorting-type
  7148. (message
  7149. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  7150. what)
  7151. (setq sorting-type (read-char-exclusive)))
  7152. (let ((dcst (downcase sorting-type))
  7153. extractfun comparefun)
  7154. ;; Define the appropriate functions
  7155. (cond
  7156. ((= dcst ?n)
  7157. (setq extractfun 'string-to-number
  7158. comparefun (if (= dcst sorting-type) '< '>)))
  7159. ((= dcst ?a)
  7160. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  7161. (lambda(x) (downcase (org-sort-remove-invisible x))))
  7162. comparefun (if (= dcst sorting-type)
  7163. 'string<
  7164. (lambda (a b) (and (not (string< a b))
  7165. (not (string= a b)))))))
  7166. ((= dcst ?t)
  7167. (setq extractfun
  7168. (lambda (x)
  7169. (if (or (string-match org-ts-regexp x)
  7170. (string-match org-ts-regexp-both x))
  7171. (org-float-time
  7172. (org-time-string-to-time (match-string 0 x)))
  7173. 0))
  7174. comparefun (if (= dcst sorting-type) '< '>)))
  7175. (t (error "Invalid sorting type `%c'" sorting-type)))
  7176. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  7177. table)
  7178. (lambda (a b) (funcall comparefun (car a) (car b))))))
  7179. ;;; The orgstruct minor mode
  7180. ;; Define a minor mode which can be used in other modes in order to
  7181. ;; integrate the org-mode structure editing commands.
  7182. ;; This is really a hack, because the org-mode structure commands use
  7183. ;; keys which normally belong to the major mode. Here is how it
  7184. ;; works: The minor mode defines all the keys necessary to operate the
  7185. ;; structure commands, but wraps the commands into a function which
  7186. ;; tests if the cursor is currently at a headline or a plain list
  7187. ;; item. If that is the case, the structure command is used,
  7188. ;; temporarily setting many Org-mode variables like regular
  7189. ;; expressions for filling etc. However, when any of those keys is
  7190. ;; used at a different location, function uses `key-binding' to look
  7191. ;; up if the key has an associated command in another currently active
  7192. ;; keymap (minor modes, major mode, global), and executes that
  7193. ;; command. There might be problems if any of the keys is otherwise
  7194. ;; used as a prefix key.
  7195. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  7196. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  7197. ;; addresses this by checking explicitly for both bindings.
  7198. (defvar orgstruct-mode-map (make-sparse-keymap)
  7199. "Keymap for the minor `orgstruct-mode'.")
  7200. (defvar org-local-vars nil
  7201. "List of local variables, for use by `orgstruct-mode'.")
  7202. ;;;###autoload
  7203. (define-minor-mode orgstruct-mode
  7204. "Toggle the minor mode `orgstruct-mode'.
  7205. This mode is for using Org-mode structure commands in other
  7206. modes. The following keys behave as if Org-mode were active, if
  7207. the cursor is on a headline, or on a plain list item (both as
  7208. defined by Org-mode).
  7209. M-up Move entry/item up
  7210. M-down Move entry/item down
  7211. M-left Promote
  7212. M-right Demote
  7213. M-S-up Move entry/item up
  7214. M-S-down Move entry/item down
  7215. M-S-left Promote subtree
  7216. M-S-right Demote subtree
  7217. M-q Fill paragraph and items like in Org-mode
  7218. C-c ^ Sort entries
  7219. C-c - Cycle list bullet
  7220. TAB Cycle item visibility
  7221. M-RET Insert new heading/item
  7222. S-M-RET Insert new TODO heading / Checkbox item
  7223. C-c C-c Set tags / toggle checkbox"
  7224. nil " OrgStruct" nil
  7225. (org-load-modules-maybe)
  7226. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  7227. ;;;###autoload
  7228. (defun turn-on-orgstruct ()
  7229. "Unconditionally turn on `orgstruct-mode'."
  7230. (orgstruct-mode 1))
  7231. (defun orgstruct++-mode (&optional arg)
  7232. "Toggle `orgstruct-mode', the enhanced version of it.
  7233. In addition to setting orgstruct-mode, this also exports all indentation
  7234. and autofilling variables from org-mode into the buffer. It will also
  7235. recognize item context in multiline items.
  7236. Note that turning off orgstruct-mode will *not* remove the
  7237. indentation/paragraph settings. This can only be done by refreshing the
  7238. major mode, for example with \\[normal-mode]."
  7239. (interactive "P")
  7240. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  7241. (if (< arg 1)
  7242. (orgstruct-mode -1)
  7243. (orgstruct-mode 1)
  7244. (let (var val)
  7245. (mapc
  7246. (lambda (x)
  7247. (when (string-match
  7248. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7249. (symbol-name (car x)))
  7250. (setq var (car x) val (nth 1 x))
  7251. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  7252. org-local-vars)
  7253. (org-set-local 'orgstruct-is-++ t))))
  7254. (defvar orgstruct-is-++ nil
  7255. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  7256. (make-variable-buffer-local 'orgstruct-is-++)
  7257. ;;;###autoload
  7258. (defun turn-on-orgstruct++ ()
  7259. "Unconditionally turn on `orgstruct++-mode'."
  7260. (orgstruct++-mode 1))
  7261. (defun orgstruct-error ()
  7262. "Error when there is no default binding for a structure key."
  7263. (interactive)
  7264. (error "This key has no function outside structure elements"))
  7265. (defun orgstruct-setup ()
  7266. "Setup orgstruct keymaps."
  7267. (let ((nfunc 0)
  7268. (bindings
  7269. (list
  7270. '([(meta up)] org-metaup)
  7271. '([(meta down)] org-metadown)
  7272. '([(meta left)] org-metaleft)
  7273. '([(meta right)] org-metaright)
  7274. '([(meta shift up)] org-shiftmetaup)
  7275. '([(meta shift down)] org-shiftmetadown)
  7276. '([(meta shift left)] org-shiftmetaleft)
  7277. '([(meta shift right)] org-shiftmetaright)
  7278. '([?\e (up)] org-metaup)
  7279. '([?\e (down)] org-metadown)
  7280. '([?\e (left)] org-metaleft)
  7281. '([?\e (right)] org-metaright)
  7282. '([?\e (shift up)] org-shiftmetaup)
  7283. '([?\e (shift down)] org-shiftmetadown)
  7284. '([?\e (shift left)] org-shiftmetaleft)
  7285. '([?\e (shift right)] org-shiftmetaright)
  7286. '([(shift up)] org-shiftup)
  7287. '([(shift down)] org-shiftdown)
  7288. '([(shift left)] org-shiftleft)
  7289. '([(shift right)] org-shiftright)
  7290. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  7291. '("\M-q" fill-paragraph)
  7292. '("\C-c^" org-sort)
  7293. '("\C-c-" org-cycle-list-bullet)))
  7294. elt key fun cmd)
  7295. (while (setq elt (pop bindings))
  7296. (setq nfunc (1+ nfunc))
  7297. (setq key (org-key (car elt))
  7298. fun (nth 1 elt)
  7299. cmd (orgstruct-make-binding fun nfunc key))
  7300. (org-defkey orgstruct-mode-map key cmd))
  7301. ;; Special treatment needed for TAB and RET
  7302. (org-defkey orgstruct-mode-map [(tab)]
  7303. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  7304. (org-defkey orgstruct-mode-map "\C-i"
  7305. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  7306. (org-defkey orgstruct-mode-map "\M-\C-m"
  7307. (orgstruct-make-binding 'org-insert-heading 105
  7308. "\M-\C-m" [(meta return)]))
  7309. (org-defkey orgstruct-mode-map [(meta return)]
  7310. (orgstruct-make-binding 'org-insert-heading 106
  7311. [(meta return)] "\M-\C-m"))
  7312. (org-defkey orgstruct-mode-map [(shift meta return)]
  7313. (orgstruct-make-binding 'org-insert-todo-heading 107
  7314. [(meta return)] "\M-\C-m"))
  7315. (org-defkey orgstruct-mode-map "\e\C-m"
  7316. (orgstruct-make-binding 'org-insert-heading 108
  7317. "\e\C-m" [?\e (return)]))
  7318. (org-defkey orgstruct-mode-map [?\e (return)]
  7319. (orgstruct-make-binding 'org-insert-heading 109
  7320. [?\e (return)] "\e\C-m"))
  7321. (org-defkey orgstruct-mode-map [?\e (shift return)]
  7322. (orgstruct-make-binding 'org-insert-todo-heading 110
  7323. [?\e (return)] "\e\C-m"))
  7324. (unless org-local-vars
  7325. (setq org-local-vars (org-get-local-variables)))
  7326. t))
  7327. (defun orgstruct-make-binding (fun n &rest keys)
  7328. "Create a function for binding in the structure minor mode.
  7329. FUN is the command to call inside a table. N is used to create a unique
  7330. command name. KEYS are keys that should be checked in for a command
  7331. to execute outside of tables."
  7332. (eval
  7333. (list 'defun
  7334. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  7335. '(arg)
  7336. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  7337. "Outside of structure, run the binding of `"
  7338. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  7339. "'.")
  7340. '(interactive "p")
  7341. (list 'if
  7342. `(org-context-p 'headline 'item
  7343. (and orgstruct-is-++
  7344. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  7345. 'item-body))
  7346. (list 'org-run-like-in-org-mode (list 'quote fun))
  7347. (list 'let '(orgstruct-mode)
  7348. (list 'call-interactively
  7349. (append '(or)
  7350. (mapcar (lambda (k)
  7351. (list 'key-binding k))
  7352. keys)
  7353. '('orgstruct-error))))))))
  7354. (defun org-context-p (&rest contexts)
  7355. "Check if local context is any of CONTEXTS.
  7356. Possible values in the list of contexts are `table', `headline', and `item'."
  7357. (let ((pos (point)))
  7358. (goto-char (point-at-bol))
  7359. (prog1 (or (and (memq 'table contexts)
  7360. (looking-at "[ \t]*|"))
  7361. (and (memq 'headline contexts)
  7362. ;;????????? (looking-at "\\*+"))
  7363. (looking-at outline-regexp))
  7364. (and (memq 'item contexts)
  7365. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  7366. (and (memq 'item-body contexts)
  7367. (org-in-item-p)))
  7368. (goto-char pos))))
  7369. (defun org-get-local-variables ()
  7370. "Return a list of all local variables in an org-mode buffer."
  7371. (let (varlist)
  7372. (with-current-buffer (get-buffer-create "*Org tmp*")
  7373. (erase-buffer)
  7374. (org-mode)
  7375. (setq varlist (buffer-local-variables)))
  7376. (kill-buffer "*Org tmp*")
  7377. (delq nil
  7378. (mapcar
  7379. (lambda (x)
  7380. (setq x
  7381. (if (symbolp x)
  7382. (list x)
  7383. (list (car x) (list 'quote (cdr x)))))
  7384. (if (string-match
  7385. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  7386. (symbol-name (car x)))
  7387. x nil))
  7388. varlist))))
  7389. (defun org-clone-local-variables (from-buffer &optional regexp)
  7390. "Clone local variables from FROM-BUFFER.
  7391. Optional argument REGEXP selects variables to clone."
  7392. (mapc
  7393. (lambda (pair)
  7394. (and (symbolp (car pair))
  7395. (or (null regexp)
  7396. (string-match regexp (symbol-name (car pair))))
  7397. (set (make-local-variable (car pair))
  7398. (cdr pair))))
  7399. (buffer-local-variables from-buffer)))
  7400. ;;;###autoload
  7401. (defun org-run-like-in-org-mode (cmd)
  7402. "Run a command, pretending that the current buffer is in Org-mode.
  7403. This will temporarily bind local variables that are typically bound in
  7404. Org-mode to the values they have in Org-mode, and then interactively
  7405. call CMD."
  7406. (org-load-modules-maybe)
  7407. (unless org-local-vars
  7408. (setq org-local-vars (org-get-local-variables)))
  7409. (eval (list 'let org-local-vars
  7410. (list 'call-interactively (list 'quote cmd)))))
  7411. ;;;; Archiving
  7412. (defun org-get-category (&optional pos force-refresh)
  7413. "Get the category applying to position POS."
  7414. (if force-refresh (org-refresh-category-properties))
  7415. (let ((pos (or pos (point))))
  7416. (or (get-text-property pos 'org-category)
  7417. (progn (org-refresh-category-properties)
  7418. (get-text-property pos 'org-category)))))
  7419. (defun org-refresh-category-properties ()
  7420. "Refresh category text properties in the buffer."
  7421. (let ((def-cat (cond
  7422. ((null org-category)
  7423. (if buffer-file-name
  7424. (file-name-sans-extension
  7425. (file-name-nondirectory buffer-file-name))
  7426. "???"))
  7427. ((symbolp org-category) (symbol-name org-category))
  7428. (t org-category)))
  7429. beg end cat pos optionp)
  7430. (org-unmodified
  7431. (save-excursion
  7432. (save-restriction
  7433. (widen)
  7434. (goto-char (point-min))
  7435. (put-text-property (point) (point-max) 'org-category def-cat)
  7436. (while (re-search-forward
  7437. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  7438. (setq pos (match-end 0)
  7439. optionp (equal (char-after (match-beginning 0)) ?#)
  7440. cat (org-trim (match-string 2)))
  7441. (if optionp
  7442. (setq beg (point-at-bol) end (point-max))
  7443. (org-back-to-heading t)
  7444. (setq beg (point) end (org-end-of-subtree t t)))
  7445. (put-text-property beg end 'org-category cat)
  7446. (goto-char pos)))))))
  7447. ;;;; Link Stuff
  7448. ;;; Link abbreviations
  7449. (defun org-link-expand-abbrev (link)
  7450. "Apply replacements as defined in `org-link-abbrev-alist."
  7451. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  7452. (let* ((key (match-string 1 link))
  7453. (as (or (assoc key org-link-abbrev-alist-local)
  7454. (assoc key org-link-abbrev-alist)))
  7455. (tag (and (match-end 2) (match-string 3 link)))
  7456. rpl)
  7457. (if (not as)
  7458. link
  7459. (setq rpl (cdr as))
  7460. (cond
  7461. ((symbolp rpl) (funcall rpl tag))
  7462. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  7463. ((string-match "%h" rpl)
  7464. (replace-match (url-hexify-string (or tag "")) t t rpl))
  7465. (t (concat rpl tag)))))
  7466. link))
  7467. ;;; Storing and inserting links
  7468. (defvar org-insert-link-history nil
  7469. "Minibuffer history for links inserted with `org-insert-link'.")
  7470. (defvar org-stored-links nil
  7471. "Contains the links stored with `org-store-link'.")
  7472. (defvar org-store-link-plist nil
  7473. "Plist with info about the most recently link created with `org-store-link'.")
  7474. (defvar org-link-protocols nil
  7475. "Link protocols added to Org-mode using `org-add-link-type'.")
  7476. (defvar org-store-link-functions nil
  7477. "List of functions that are called to create and store a link.
  7478. Each function will be called in turn until one returns a non-nil
  7479. value. Each function should check if it is responsible for creating
  7480. this link (for example by looking at the major mode).
  7481. If not, it must exit and return nil.
  7482. If yes, it should return a non-nil value after a calling
  7483. `org-store-link-props' with a list of properties and values.
  7484. Special properties are:
  7485. :type The link prefix, like \"http\". This must be given.
  7486. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  7487. This is obligatory as well.
  7488. :description Optional default description for the second pair
  7489. of brackets in an Org-mode link. The user can still change
  7490. this when inserting this link into an Org-mode buffer.
  7491. In addition to these, any additional properties can be specified
  7492. and then used in remember templates.")
  7493. (defun org-add-link-type (type &optional follow export)
  7494. "Add TYPE to the list of `org-link-types'.
  7495. Re-compute all regular expressions depending on `org-link-types'
  7496. FOLLOW and EXPORT are two functions.
  7497. FOLLOW should take the link path as the single argument and do whatever
  7498. is necessary to follow the link, for example find a file or display
  7499. a mail message.
  7500. EXPORT should format the link path for export to one of the export formats.
  7501. It should be a function accepting three arguments:
  7502. path the path of the link, the text after the prefix (like \"http:\")
  7503. desc the description of the link, if any, or a description added by
  7504. org-export-normalize-links if there is none
  7505. format the export format, a symbol like `html' or `latex' or `ascii'..
  7506. The function may use the FORMAT information to return different values
  7507. depending on the format. The return value will be put literally into
  7508. the exported file. If the return value is nil, this means Org should
  7509. do what it normally does with links which do not have EXPORT defined.
  7510. Org-mode has a built-in default for exporting links. If you are happy with
  7511. this default, there is no need to define an export function for the link
  7512. type. For a simple example of an export function, see `org-bbdb.el'."
  7513. (add-to-list 'org-link-types type t)
  7514. (org-make-link-regexps)
  7515. (if (assoc type org-link-protocols)
  7516. (setcdr (assoc type org-link-protocols) (list follow export))
  7517. (push (list type follow export) org-link-protocols)))
  7518. (defvar org-agenda-buffer-name)
  7519. ;;;###autoload
  7520. (defun org-store-link (arg)
  7521. "\\<org-mode-map>Store an org-link to the current location.
  7522. This link is added to `org-stored-links' and can later be inserted
  7523. into an org-buffer with \\[org-insert-link].
  7524. For some link types, a prefix arg is interpreted:
  7525. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  7526. For file links, arg negates `org-context-in-file-links'."
  7527. (interactive "P")
  7528. (org-load-modules-maybe)
  7529. (setq org-store-link-plist nil) ; reset
  7530. (org-with-limited-levels
  7531. (let (link cpltxt desc description search txt custom-id agenda-link)
  7532. (cond
  7533. ((run-hook-with-args-until-success 'org-store-link-functions)
  7534. (setq link (plist-get org-store-link-plist :link)
  7535. desc (or (plist-get org-store-link-plist :description) link)))
  7536. ((equal (buffer-name) "*Org Edit Src Example*")
  7537. (let (label gc)
  7538. (while (or (not label)
  7539. (save-excursion
  7540. (save-restriction
  7541. (widen)
  7542. (goto-char (point-min))
  7543. (re-search-forward
  7544. (regexp-quote (format org-coderef-label-format label))
  7545. nil t))))
  7546. (when label (message "Label exists already") (sit-for 2))
  7547. (setq label (read-string "Code line label: " label)))
  7548. (end-of-line 1)
  7549. (setq link (format org-coderef-label-format label))
  7550. (setq gc (- 79 (length link)))
  7551. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  7552. (insert link)
  7553. (setq link (concat "(" label ")") desc nil)))
  7554. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  7555. ;; We are in the agenda, link to referenced location
  7556. (let ((m (or (get-text-property (point) 'org-hd-marker)
  7557. (get-text-property (point) 'org-marker))))
  7558. (when m
  7559. (org-with-point-at m
  7560. (setq agenda-link
  7561. (if (interactive-p)
  7562. (call-interactively 'org-store-link)
  7563. (org-store-link nil)))))))
  7564. ((eq major-mode 'calendar-mode)
  7565. (let ((cd (calendar-cursor-to-date)))
  7566. (setq link
  7567. (format-time-string
  7568. (car org-time-stamp-formats)
  7569. (apply 'encode-time
  7570. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  7571. nil nil nil))))
  7572. (org-store-link-props :type "calendar" :date cd)))
  7573. ((eq major-mode 'w3-mode)
  7574. (setq cpltxt (if (and (buffer-name)
  7575. (not (string-match "Untitled" (buffer-name))))
  7576. (buffer-name)
  7577. (url-view-url t))
  7578. link (org-make-link (url-view-url t)))
  7579. (org-store-link-props :type "w3" :url (url-view-url t)))
  7580. ((eq major-mode 'w3m-mode)
  7581. (setq cpltxt (or w3m-current-title w3m-current-url)
  7582. link (org-make-link w3m-current-url))
  7583. (org-store-link-props :type "w3m" :url (url-view-url t)))
  7584. ((setq search (run-hook-with-args-until-success
  7585. 'org-create-file-search-functions))
  7586. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  7587. "::" search))
  7588. (setq cpltxt (or description link)))
  7589. ((eq major-mode 'image-mode)
  7590. (setq cpltxt (concat "file:"
  7591. (abbreviate-file-name buffer-file-name))
  7592. link (org-make-link cpltxt))
  7593. (org-store-link-props :type "image" :file buffer-file-name))
  7594. ((eq major-mode 'dired-mode)
  7595. ;; link to the file in the current line
  7596. (let ((file (dired-get-filename nil t)))
  7597. (setq file (if file
  7598. (abbreviate-file-name
  7599. (expand-file-name (dired-get-filename nil t)))
  7600. ;; otherwise, no file so use current directory.
  7601. default-directory))
  7602. (setq cpltxt (concat "file:" file)
  7603. link (org-make-link cpltxt))))
  7604. ((and (buffer-file-name (buffer-base-buffer)) (org-mode-p))
  7605. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  7606. (cond
  7607. ((org-in-regexp "<<\\(.*?\\)>>")
  7608. (setq cpltxt
  7609. (concat "file:"
  7610. (abbreviate-file-name
  7611. (buffer-file-name (buffer-base-buffer)))
  7612. "::" (match-string 1))
  7613. link (org-make-link cpltxt)))
  7614. ((and (featurep 'org-id)
  7615. (or (eq org-link-to-org-use-id t)
  7616. (and (eq org-link-to-org-use-id 'create-if-interactive)
  7617. (interactive-p))
  7618. (and (eq org-link-to-org-use-id 'create-if-interactive-and-no-custom-id)
  7619. (interactive-p)
  7620. (not custom-id))
  7621. (and org-link-to-org-use-id
  7622. (org-entry-get nil "ID"))))
  7623. ;; We can make a link using the ID.
  7624. (setq link (condition-case nil
  7625. (prog1 (org-id-store-link)
  7626. (setq desc (plist-get org-store-link-plist
  7627. :description)))
  7628. (error
  7629. ;; probably before first headline, link to file only
  7630. (concat "file:"
  7631. (abbreviate-file-name
  7632. (buffer-file-name (buffer-base-buffer))))))))
  7633. (t
  7634. ;; Just link to current headline
  7635. (setq cpltxt (concat "file:"
  7636. (abbreviate-file-name
  7637. (buffer-file-name (buffer-base-buffer)))))
  7638. ;; Add a context search string
  7639. (when (org-xor org-context-in-file-links arg)
  7640. (setq txt (cond
  7641. ((org-on-heading-p) nil)
  7642. ((org-region-active-p)
  7643. (buffer-substring (region-beginning) (region-end)))
  7644. (t nil)))
  7645. (when (or (null txt) (string-match "\\S-" txt))
  7646. (setq cpltxt
  7647. (concat cpltxt "::"
  7648. (condition-case nil
  7649. (org-make-org-heading-search-string txt)
  7650. (error "")))
  7651. desc (or (nth 4 (ignore-errors
  7652. (org-heading-components))) "NONE"))))
  7653. (if (string-match "::\\'" cpltxt)
  7654. (setq cpltxt (substring cpltxt 0 -2)))
  7655. (setq link (org-make-link cpltxt)))))
  7656. ((buffer-file-name (buffer-base-buffer))
  7657. ;; Just link to this file here.
  7658. (setq cpltxt (concat "file:"
  7659. (abbreviate-file-name
  7660. (buffer-file-name (buffer-base-buffer)))))
  7661. ;; Add a context string
  7662. (when (org-xor org-context-in-file-links arg)
  7663. (setq txt (if (org-region-active-p)
  7664. (buffer-substring (region-beginning) (region-end))
  7665. (buffer-substring (point-at-bol) (point-at-eol))))
  7666. ;; Only use search option if there is some text.
  7667. (when (string-match "\\S-" txt)
  7668. (setq cpltxt
  7669. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  7670. desc "NONE")))
  7671. (setq link (org-make-link cpltxt)))
  7672. ((interactive-p)
  7673. (error "Cannot link to a buffer which is not visiting a file"))
  7674. (t (setq link nil)))
  7675. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  7676. (setq link (or link cpltxt)
  7677. desc (or desc cpltxt))
  7678. (if (equal desc "NONE") (setq desc nil))
  7679. (if (and (or (interactive-p) executing-kbd-macro) link)
  7680. (progn
  7681. (setq org-stored-links
  7682. (cons (list link desc) org-stored-links))
  7683. (message "Stored: %s" (or desc link))
  7684. (when custom-id
  7685. (setq link (concat "file:" (abbreviate-file-name (buffer-file-name))
  7686. "::#" custom-id))
  7687. (setq org-stored-links
  7688. (cons (list link desc) org-stored-links))))
  7689. (or agenda-link (and link (org-make-link-string link desc)))))))
  7690. (defun org-store-link-props (&rest plist)
  7691. "Store link properties, extract names and addresses."
  7692. (let (x adr)
  7693. (when (setq x (plist-get plist :from))
  7694. (setq adr (mail-extract-address-components x))
  7695. (setq plist (plist-put plist :fromname (car adr)))
  7696. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  7697. (when (setq x (plist-get plist :to))
  7698. (setq adr (mail-extract-address-components x))
  7699. (setq plist (plist-put plist :toname (car adr)))
  7700. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  7701. (let ((from (plist-get plist :from))
  7702. (to (plist-get plist :to)))
  7703. (when (and from to org-from-is-user-regexp)
  7704. (setq plist
  7705. (plist-put plist :fromto
  7706. (if (string-match org-from-is-user-regexp from)
  7707. (concat "to %t")
  7708. (concat "from %f"))))))
  7709. (setq org-store-link-plist plist))
  7710. (defun org-add-link-props (&rest plist)
  7711. "Add these properties to the link property list."
  7712. (let (key value)
  7713. (while plist
  7714. (setq key (pop plist) value (pop plist))
  7715. (setq org-store-link-plist
  7716. (plist-put org-store-link-plist key value)))))
  7717. (defun org-email-link-description (&optional fmt)
  7718. "Return the description part of an email link.
  7719. This takes information from `org-store-link-plist' and formats it
  7720. according to FMT (default from `org-email-link-description-format')."
  7721. (setq fmt (or fmt org-email-link-description-format))
  7722. (let* ((p org-store-link-plist)
  7723. (to (plist-get p :toaddress))
  7724. (from (plist-get p :fromaddress))
  7725. (table
  7726. (list
  7727. (cons "%c" (plist-get p :fromto))
  7728. (cons "%F" (plist-get p :from))
  7729. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  7730. (cons "%T" (plist-get p :to))
  7731. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  7732. (cons "%s" (plist-get p :subject))
  7733. (cons "%d" (plist-get p :date))
  7734. (cons "%m" (plist-get p :message-id)))))
  7735. (when (string-match "%c" fmt)
  7736. ;; Check if the user wrote this message
  7737. (if (and org-from-is-user-regexp from to
  7738. (save-match-data (string-match org-from-is-user-regexp from)))
  7739. (setq fmt (replace-match "to %t" t t fmt))
  7740. (setq fmt (replace-match "from %f" t t fmt))))
  7741. (org-replace-escapes fmt table)))
  7742. (defun org-make-org-heading-search-string (&optional string heading)
  7743. "Make search string for STRING or current headline."
  7744. (interactive)
  7745. (let ((s (or string (org-get-heading)))
  7746. (lines org-context-in-file-links))
  7747. (unless (and string (not heading))
  7748. ;; We are using a headline, clean up garbage in there.
  7749. (if (string-match org-todo-regexp s)
  7750. (setq s (replace-match "" t t s)))
  7751. (if (string-match (org-re ":[[:alnum:]_@#%:]+:[ \t]*$") s)
  7752. (setq s (replace-match "" t t s)))
  7753. (setq s (org-trim s))
  7754. (if (string-match (concat "^\\(" org-quote-string "\\|"
  7755. org-comment-string "\\)") s)
  7756. (setq s (replace-match "" t t s)))
  7757. (while (string-match org-ts-regexp s)
  7758. (setq s (replace-match "" t t s))))
  7759. (or string (setq s (concat "*" s))) ; Add * for headlines
  7760. (when (and string (integerp lines) (> lines 0))
  7761. (let ((slines (org-split-string s "\n")))
  7762. (when (< lines (length slines))
  7763. (setq s (mapconcat
  7764. 'identity
  7765. (reverse (nthcdr (- (length slines) lines)
  7766. (reverse slines))) "\n")))))
  7767. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  7768. (defun org-make-link (&rest strings)
  7769. "Concatenate STRINGS."
  7770. (apply 'concat strings))
  7771. (defun org-make-link-string (link &optional description)
  7772. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  7773. (unless (string-match "\\S-" link)
  7774. (error "Empty link"))
  7775. (when (and description
  7776. (stringp description)
  7777. (not (string-match "\\S-" description)))
  7778. (setq description nil))
  7779. (when (stringp description)
  7780. ;; Remove brackets from the description, they are fatal.
  7781. (while (string-match "\\[" description)
  7782. (setq description (replace-match "{" t t description)))
  7783. (while (string-match "\\]" description)
  7784. (setq description (replace-match "}" t t description))))
  7785. (when (equal (org-link-escape link) description)
  7786. ;; No description needed, it is identical
  7787. (setq description nil))
  7788. (when (and (not description)
  7789. (not (equal link (org-link-escape link))))
  7790. (setq description (org-extract-attributes link)))
  7791. (setq link (if (string-match org-link-types-re link)
  7792. (concat (match-string 1 link)
  7793. (org-link-escape (substring link (match-end 1))))
  7794. (org-link-escape link)))
  7795. (concat "[[" link "]"
  7796. (if description (concat "[" description "]") "")
  7797. "]"))
  7798. (defconst org-link-escape-chars
  7799. '(?\ ?\[ ?\] ?\; ?\= ?\+)
  7800. "List of characters that should be escaped in link.
  7801. This is the list that is used for internal purposes.")
  7802. (defvar org-url-encoding-use-url-hexify nil)
  7803. (defconst org-link-escape-chars-browser
  7804. '(?\ )
  7805. "List of escapes for characters that are problematic in links.
  7806. This is the list that is used before handing over to the browser.")
  7807. (defun org-link-escape (text &optional table merge)
  7808. "Return percent escaped representation of TEXT.
  7809. TEXT is a string with the text to escape.
  7810. Optional argument TABLE is a list with characters that should be
  7811. escaped. When nil, `org-link-escape-chars' is used.
  7812. If optional argument MERGE is set, merge TABLE into
  7813. `org-link-escape-chars'."
  7814. (if (and org-url-encoding-use-url-hexify (not table))
  7815. (url-hexify-string text)
  7816. (cond
  7817. ((and table merge)
  7818. (mapc (lambda (defchr)
  7819. (unless (member defchr table)
  7820. (setq table (cons defchr table)))) org-link-escape-chars))
  7821. ((null table)
  7822. (setq table org-link-escape-chars)))
  7823. (mapconcat
  7824. (lambda (char)
  7825. (if (or (member char table)
  7826. (< char 32) (= char 37) (> char 126))
  7827. (mapconcat (lambda (sequence-element)
  7828. (format "%%%.2X" sequence-element))
  7829. (or (encode-coding-char char 'utf-8)
  7830. (error "Unable to percent escape character: %s"
  7831. (char-to-string char))) "")
  7832. (char-to-string char))) text "")))
  7833. (defun org-link-unescape (str)
  7834. "Unhex hexified unicode strings as returned from the JavaScript function
  7835. encodeURIComponent. E.g. `%C3%B6' is the german Umlaut `ö'."
  7836. (unless (and (null str) (string= "" str))
  7837. (let ((pos 0) (case-fold-search t) unhexed)
  7838. (while (setq pos (string-match "\\(%[0-9a-f][0-9a-f]\\)+" str pos))
  7839. (setq unhexed (org-link-unescape-compound (match-string 0 str)))
  7840. (setq str (replace-match unhexed t t str))
  7841. (setq pos (+ pos (length unhexed))))))
  7842. str)
  7843. (defun org-link-unescape-compound (hex)
  7844. "Unhexify unicode hex-chars. E.g. `%C3%B6' is the German Umlaut `ö'.
  7845. Note: this function also decodes single byte encodings like
  7846. `%E1' (\"á\") if not followed by another `%[A-F0-9]{2}' group."
  7847. (save-match-data
  7848. (let* ((bytes (cdr (split-string hex "%")))
  7849. (ret "")
  7850. (eat 0)
  7851. (sum 0))
  7852. (while bytes
  7853. (let* ((val (string-to-number (pop bytes) 16))
  7854. (shift-xor
  7855. (if (= 0 eat)
  7856. (cond
  7857. ((>= val 252) (cons 6 252))
  7858. ((>= val 248) (cons 5 248))
  7859. ((>= val 240) (cons 4 240))
  7860. ((>= val 224) (cons 3 224))
  7861. ((>= val 192) (cons 2 192))
  7862. (t (cons 0 0)))
  7863. (cons 6 128))))
  7864. (if (>= val 192) (setq eat (car shift-xor)))
  7865. (setq val (logxor val (cdr shift-xor)))
  7866. (setq sum (+ (lsh sum (car shift-xor)) val))
  7867. (if (> eat 0) (setq eat (- eat 1)))
  7868. (cond
  7869. ((= 0 eat) ;multi byte
  7870. (setq ret (concat ret (org-char-to-string sum)))
  7871. (setq sum 0))
  7872. ((not bytes) ; single byte(s)
  7873. (setq ret (org-link-unescape-single-byte-sequence hex))))
  7874. )) ;; end (while bytes
  7875. ret )))
  7876. (defun org-link-unescape-single-byte-sequence (hex)
  7877. "Unhexify hex-encoded single byte character sequences."
  7878. (mapconcat (lambda (byte)
  7879. (char-to-string (string-to-number byte 16)))
  7880. (cdr (split-string hex "%")) ""))
  7881. (defun org-xor (a b)
  7882. "Exclusive or."
  7883. (if a (not b) b))
  7884. (defun org-fixup-message-id-for-http (s)
  7885. "Replace special characters in a message id, so it can be used in an http query."
  7886. (when (string-match "%" s)
  7887. (setq s (mapconcat (lambda (c)
  7888. (if (eq c ?%)
  7889. "%25"
  7890. (char-to-string c)))
  7891. s "")))
  7892. (while (string-match "<" s)
  7893. (setq s (replace-match "%3C" t t s)))
  7894. (while (string-match ">" s)
  7895. (setq s (replace-match "%3E" t t s)))
  7896. (while (string-match "@" s)
  7897. (setq s (replace-match "%40" t t s)))
  7898. s)
  7899. ;;;###autoload
  7900. (defun org-insert-link-global ()
  7901. "Insert a link like Org-mode does.
  7902. This command can be called in any mode to insert a link in Org-mode syntax."
  7903. (interactive)
  7904. (org-load-modules-maybe)
  7905. (org-run-like-in-org-mode 'org-insert-link))
  7906. (defun org-insert-link (&optional complete-file link-location)
  7907. "Insert a link. At the prompt, enter the link.
  7908. Completion can be used to insert any of the link protocol prefixes like
  7909. http or ftp in use.
  7910. The history can be used to select a link previously stored with
  7911. `org-store-link'. When the empty string is entered (i.e. if you just
  7912. press RET at the prompt), the link defaults to the most recently
  7913. stored link. As SPC triggers completion in the minibuffer, you need to
  7914. use M-SPC or C-q SPC to force the insertion of a space character.
  7915. You will also be prompted for a description, and if one is given, it will
  7916. be displayed in the buffer instead of the link.
  7917. If there is already a link at point, this command will allow you to edit link
  7918. and description parts.
  7919. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  7920. be selected using completion. The path to the file will be relative to the
  7921. current directory if the file is in the current directory or a subdirectory.
  7922. Otherwise, the link will be the absolute path as completed in the minibuffer
  7923. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  7924. option `org-link-file-path-type'.
  7925. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  7926. the current directory or below.
  7927. With three \\[universal-argument] prefixes, negate the meaning of
  7928. `org-keep-stored-link-after-insertion'.
  7929. If `org-make-link-description-function' is non-nil, this function will be
  7930. called with the link target, and the result will be the default
  7931. link description.
  7932. If the LINK-LOCATION parameter is non-nil, this value will be
  7933. used as the link location instead of reading one interactively."
  7934. (interactive "P")
  7935. (let* ((wcf (current-window-configuration))
  7936. (region (if (org-region-active-p)
  7937. (buffer-substring (region-beginning) (region-end))))
  7938. (remove (and region (list (region-beginning) (region-end))))
  7939. (desc region)
  7940. tmphist ; byte-compile incorrectly complains about this
  7941. (link link-location)
  7942. entry file all-prefixes)
  7943. (cond
  7944. (link-location) ; specified by arg, just use it.
  7945. ((org-in-regexp org-bracket-link-regexp 1)
  7946. ;; We do have a link at point, and we are going to edit it.
  7947. (setq remove (list (match-beginning 0) (match-end 0)))
  7948. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  7949. (setq link (read-string "Link: "
  7950. (org-link-unescape
  7951. (org-match-string-no-properties 1)))))
  7952. ((or (org-in-regexp org-angle-link-re)
  7953. (org-in-regexp org-plain-link-re))
  7954. ;; Convert to bracket link
  7955. (setq remove (list (match-beginning 0) (match-end 0))
  7956. link (read-string "Link: "
  7957. (org-remove-angle-brackets (match-string 0)))))
  7958. ((member complete-file '((4) (16)))
  7959. ;; Completing read for file names.
  7960. (setq link (org-file-complete-link complete-file)))
  7961. (t
  7962. ;; Read link, with completion for stored links.
  7963. (with-output-to-temp-buffer "*Org Links*"
  7964. (princ "Insert a link.
  7965. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  7966. (when org-stored-links
  7967. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  7968. (princ (mapconcat
  7969. (lambda (x)
  7970. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  7971. (reverse org-stored-links) "\n"))))
  7972. (let ((cw (selected-window)))
  7973. (select-window (get-buffer-window "*Org Links*" 'visible))
  7974. (setq truncate-lines t)
  7975. (unless (pos-visible-in-window-p (point-max))
  7976. (org-fit-window-to-buffer))
  7977. (and (window-live-p cw) (select-window cw)))
  7978. ;; Fake a link history, containing the stored links.
  7979. (setq tmphist (append (mapcar 'car org-stored-links)
  7980. org-insert-link-history))
  7981. (setq all-prefixes (append (mapcar 'car org-link-abbrev-alist-local)
  7982. (mapcar 'car org-link-abbrev-alist)
  7983. org-link-types))
  7984. (unwind-protect
  7985. (progn
  7986. (setq link
  7987. (let ((org-completion-use-ido nil)
  7988. (org-completion-use-iswitchb nil))
  7989. (org-completing-read
  7990. "Link: "
  7991. (append
  7992. (mapcar (lambda (x) (list (concat x ":")))
  7993. all-prefixes)
  7994. (mapcar 'car org-stored-links))
  7995. nil nil nil
  7996. 'tmphist
  7997. (car (car org-stored-links)))))
  7998. (if (not (string-match "\\S-" link))
  7999. (error "No link selected"))
  8000. (if (or (member link all-prefixes)
  8001. (and (equal ":" (substring link -1))
  8002. (member (substring link 0 -1) all-prefixes)
  8003. (setq link (substring link 0 -1))))
  8004. (setq link (org-link-try-special-completion link))))
  8005. (set-window-configuration wcf)
  8006. (kill-buffer "*Org Links*"))
  8007. (setq entry (assoc link org-stored-links))
  8008. (or entry (push link org-insert-link-history))
  8009. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  8010. (not org-keep-stored-link-after-insertion))
  8011. (setq org-stored-links (delq (assoc link org-stored-links)
  8012. org-stored-links)))
  8013. (setq desc (or desc (nth 1 entry)))))
  8014. (if (string-match org-plain-link-re link)
  8015. ;; URL-like link, normalize the use of angular brackets.
  8016. (setq link (org-make-link (org-remove-angle-brackets link))))
  8017. ;; Check if we are linking to the current file with a search option
  8018. ;; If yes, simplify the link by using only the search option.
  8019. (when (and buffer-file-name
  8020. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  8021. (let* ((path (match-string 1 link))
  8022. (case-fold-search nil)
  8023. (search (match-string 2 link)))
  8024. (save-match-data
  8025. (if (equal (file-truename buffer-file-name) (file-truename path))
  8026. ;; We are linking to this same file, with a search option
  8027. (setq link search)))))
  8028. ;; Check if we can/should use a relative path. If yes, simplify the link
  8029. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  8030. (let* ((type (match-string 1 link))
  8031. (path (match-string 2 link))
  8032. (origpath path)
  8033. (case-fold-search nil))
  8034. (cond
  8035. ((or (eq org-link-file-path-type 'absolute)
  8036. (equal complete-file '(16)))
  8037. (setq path (abbreviate-file-name (expand-file-name path))))
  8038. ((eq org-link-file-path-type 'noabbrev)
  8039. (setq path (expand-file-name path)))
  8040. ((eq org-link-file-path-type 'relative)
  8041. (setq path (file-relative-name path)))
  8042. (t
  8043. (save-match-data
  8044. (if (string-match (concat "^" (regexp-quote
  8045. (expand-file-name
  8046. (file-name-as-directory
  8047. default-directory))))
  8048. (expand-file-name path))
  8049. ;; We are linking a file with relative path name.
  8050. (setq path (substring (expand-file-name path)
  8051. (match-end 0)))
  8052. (setq path (abbreviate-file-name (expand-file-name path)))))))
  8053. (setq link (concat type path))
  8054. (if (equal desc origpath)
  8055. (setq desc path))))
  8056. (if org-make-link-description-function
  8057. (setq desc (funcall org-make-link-description-function link desc)))
  8058. (setq desc (read-string "Description: " desc))
  8059. (unless (string-match "\\S-" desc) (setq desc nil))
  8060. (if remove (apply 'delete-region remove))
  8061. (insert (org-make-link-string link desc))))
  8062. (defun org-link-try-special-completion (type)
  8063. "If there is completion support for link type TYPE, offer it."
  8064. (let ((fun (intern (concat "org-" type "-complete-link"))))
  8065. (if (functionp fun)
  8066. (funcall fun)
  8067. (read-string "Link (no completion support): " (concat type ":")))))
  8068. (defun org-file-complete-link (&optional arg)
  8069. "Create a file link using completion."
  8070. (let (file link)
  8071. (setq file (read-file-name "File: "))
  8072. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8073. (pwd1 (file-name-as-directory (abbreviate-file-name
  8074. (expand-file-name ".")))))
  8075. (cond
  8076. ((equal arg '(16))
  8077. (setq link (org-make-link
  8078. "file:"
  8079. (abbreviate-file-name (expand-file-name file)))))
  8080. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8081. (setq link (org-make-link "file:" (match-string 1 file))))
  8082. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8083. (expand-file-name file))
  8084. (setq link (org-make-link
  8085. "file:" (match-string 1 (expand-file-name file)))))
  8086. (t (setq link (org-make-link "file:" file)))))
  8087. link))
  8088. (defun org-completing-read (&rest args)
  8089. "Completing-read with SPACE being a normal character."
  8090. (let ((minibuffer-local-completion-map
  8091. (copy-keymap minibuffer-local-completion-map)))
  8092. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  8093. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  8094. (apply 'org-icompleting-read args)))
  8095. (defun org-completing-read-no-i (&rest args)
  8096. (let (org-completion-use-ido org-completion-use-iswitchb)
  8097. (apply 'org-completing-read args)))
  8098. (defun org-iswitchb-completing-read (prompt choices &rest args)
  8099. "Use iswitch as a completing-read replacement to choose from choices.
  8100. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  8101. from."
  8102. (let* ((iswitchb-use-virtual-buffers nil)
  8103. (iswitchb-make-buflist-hook
  8104. (lambda ()
  8105. (setq iswitchb-temp-buflist choices))))
  8106. (iswitchb-read-buffer prompt)))
  8107. (defun org-icompleting-read (&rest args)
  8108. "Completing-read using `ido-mode' or `iswitchb' speedups if available."
  8109. (org-without-partial-completion
  8110. (if (and org-completion-use-ido
  8111. (fboundp 'ido-completing-read)
  8112. (boundp 'ido-mode) ido-mode
  8113. (listp (second args)))
  8114. (let ((ido-enter-matching-directory nil))
  8115. (apply 'ido-completing-read (concat (car args))
  8116. (if (consp (car (nth 1 args)))
  8117. (mapcar 'car (nth 1 args))
  8118. (nth 1 args))
  8119. (cddr args)))
  8120. (if (and org-completion-use-iswitchb
  8121. (boundp 'iswitchb-mode) iswitchb-mode
  8122. (listp (second args)))
  8123. (apply 'org-iswitchb-completing-read (concat (car args))
  8124. (if (consp (car (nth 1 args)))
  8125. (mapcar 'car (nth 1 args))
  8126. (nth 1 args))
  8127. (cddr args))
  8128. (apply 'completing-read args)))))
  8129. (defun org-extract-attributes (s)
  8130. "Extract the attributes cookie from a string and set as text property."
  8131. (let (a attr (start 0) key value)
  8132. (save-match-data
  8133. (when (string-match "{{\\([^}]+\\)}}$" s)
  8134. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  8135. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  8136. (setq key (match-string 1 a) value (match-string 2 a)
  8137. start (match-end 0)
  8138. attr (plist-put attr (intern key) value))))
  8139. (org-add-props s nil 'org-attr attr))
  8140. s))
  8141. (defun org-extract-attributes-from-string (tag)
  8142. (let (key value attr)
  8143. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"\\s-?" tag)
  8144. (setq key (match-string 1 tag) value (match-string 2 tag)
  8145. tag (replace-match "" t t tag)
  8146. attr (plist-put attr (intern key) value)))
  8147. (cons tag attr)))
  8148. (defun org-attributes-to-string (plist)
  8149. "Format a property list into an HTML attribute list."
  8150. (let ((s "") key value)
  8151. (while plist
  8152. (setq key (pop plist) value (pop plist))
  8153. (and value
  8154. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  8155. s))
  8156. ;;; Opening/following a link
  8157. (defvar org-link-search-failed nil)
  8158. (defvar org-open-link-functions nil
  8159. "Hook for functions finding a plain text link.
  8160. These functions must take a single argument, the link content.
  8161. They will be called for links that look like [[link text][description]]
  8162. when LINK TEXT does not have a protocol like \"http:\" and does not look
  8163. like a filename (e.g. \"./blue.png\").
  8164. These functions will be called *before* Org attempts to resolve the
  8165. link by doing text searches in the current buffer - so if you want a
  8166. link \"[[target]]\" to still find \"<<target>>\", your function should
  8167. handle this as a special case.
  8168. When the function does handle the link, it must return a non-nil value.
  8169. If it decides that it is not responsible for this link, it must return
  8170. nil to indicate that that Org-mode can continue with other options
  8171. like exact and fuzzy text search.")
  8172. (defun org-next-link ()
  8173. "Move forward to the next link.
  8174. If the link is in hidden text, expose it."
  8175. (interactive)
  8176. (when (and org-link-search-failed (eq this-command last-command))
  8177. (goto-char (point-min))
  8178. (message "Link search wrapped back to beginning of buffer"))
  8179. (setq org-link-search-failed nil)
  8180. (let* ((pos (point))
  8181. (ct (org-context))
  8182. (a (assoc :link ct)))
  8183. (if a (goto-char (nth 2 a)))
  8184. (if (re-search-forward org-any-link-re nil t)
  8185. (progn
  8186. (goto-char (match-beginning 0))
  8187. (if (outline-invisible-p) (org-show-context)))
  8188. (goto-char pos)
  8189. (setq org-link-search-failed t)
  8190. (error "No further link found"))))
  8191. (defun org-previous-link ()
  8192. "Move backward to the previous link.
  8193. If the link is in hidden text, expose it."
  8194. (interactive)
  8195. (when (and org-link-search-failed (eq this-command last-command))
  8196. (goto-char (point-max))
  8197. (message "Link search wrapped back to end of buffer"))
  8198. (setq org-link-search-failed nil)
  8199. (let* ((pos (point))
  8200. (ct (org-context))
  8201. (a (assoc :link ct)))
  8202. (if a (goto-char (nth 1 a)))
  8203. (if (re-search-backward org-any-link-re nil t)
  8204. (progn
  8205. (goto-char (match-beginning 0))
  8206. (if (outline-invisible-p) (org-show-context)))
  8207. (goto-char pos)
  8208. (setq org-link-search-failed t)
  8209. (error "No further link found"))))
  8210. (defun org-translate-link (s)
  8211. "Translate a link string if a translation function has been defined."
  8212. (if (and org-link-translation-function
  8213. (fboundp org-link-translation-function)
  8214. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  8215. (progn
  8216. (setq s (funcall org-link-translation-function
  8217. (match-string 1) (match-string 2)))
  8218. (concat (car s) ":" (cdr s)))
  8219. s))
  8220. (defun org-translate-link-from-planner (type path)
  8221. "Translate a link from Emacs Planner syntax so that Org can follow it.
  8222. This is still an experimental function, your mileage may vary."
  8223. (cond
  8224. ((member type '("http" "https" "news" "ftp"))
  8225. ;; standard Internet links are the same.
  8226. nil)
  8227. ((and (equal type "irc") (string-match "^//" path))
  8228. ;; Planner has two / at the beginning of an irc link, we have 1.
  8229. ;; We should have zero, actually....
  8230. (setq path (substring path 1)))
  8231. ((and (equal type "lisp") (string-match "^/" path))
  8232. ;; Planner has a slash, we do not.
  8233. (setq type "elisp" path (substring path 1)))
  8234. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  8235. ;; A typical message link. Planner has the id after the final slash,
  8236. ;; we separate it with a hash mark
  8237. (setq path (concat (match-string 1 path) "#"
  8238. (org-remove-angle-brackets (match-string 2 path)))))
  8239. )
  8240. (cons type path))
  8241. (defun org-find-file-at-mouse (ev)
  8242. "Open file link or URL at mouse."
  8243. (interactive "e")
  8244. (mouse-set-point ev)
  8245. (org-open-at-point 'in-emacs))
  8246. (defun org-open-at-mouse (ev)
  8247. "Open file link or URL at mouse."
  8248. (interactive "e")
  8249. (mouse-set-point ev)
  8250. (if (eq major-mode 'org-agenda-mode)
  8251. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  8252. (org-open-at-point))
  8253. (defvar org-window-config-before-follow-link nil
  8254. "The window configuration before following a link.
  8255. This is saved in case the need arises to restore it.")
  8256. (defvar org-open-link-marker (make-marker)
  8257. "Marker pointing to the location where `org-open-at-point; was called.")
  8258. ;;;###autoload
  8259. (defun org-open-at-point-global ()
  8260. "Follow a link like Org-mode does.
  8261. This command can be called in any mode to follow a link that has
  8262. Org-mode syntax."
  8263. (interactive)
  8264. (org-run-like-in-org-mode 'org-open-at-point))
  8265. ;;;###autoload
  8266. (defun org-open-link-from-string (s &optional arg reference-buffer)
  8267. "Open a link in the string S, as if it was in Org-mode."
  8268. (interactive "sLink: \nP")
  8269. (let ((reference-buffer (or reference-buffer (current-buffer))))
  8270. (with-temp-buffer
  8271. (let ((org-inhibit-startup t))
  8272. (org-mode)
  8273. (insert s)
  8274. (goto-char (point-min))
  8275. (when reference-buffer
  8276. (setq org-link-abbrev-alist-local
  8277. (with-current-buffer reference-buffer
  8278. org-link-abbrev-alist-local)))
  8279. (org-open-at-point arg reference-buffer)))))
  8280. (defvar org-open-at-point-functions nil
  8281. "Hook that is run when following a link at point.
  8282. Functions in this hook must return t if they identify and follow
  8283. a link at point. If they don't find anything interesting at point,
  8284. they must return nil.")
  8285. (defun org-open-at-point (&optional arg reference-buffer)
  8286. "Open link at or after point.
  8287. If there is no link at point, this function will search forward up to
  8288. the end of the current line.
  8289. Normally, files will be opened by an appropriate application. If the
  8290. optional prefix argument ARG is non-nil, Emacs will visit the file.
  8291. With a double prefix argument, try to open outside of Emacs, in the
  8292. application the system uses for this file type."
  8293. (interactive "P")
  8294. ;; if in a code block, then open the block's results
  8295. (unless (call-interactively #'org-babel-open-src-block-result)
  8296. (org-load-modules-maybe)
  8297. (move-marker org-open-link-marker (point))
  8298. (setq org-window-config-before-follow-link (current-window-configuration))
  8299. (org-remove-occur-highlights nil nil t)
  8300. (cond
  8301. ((and (org-on-heading-p)
  8302. (not (org-in-regexp
  8303. (concat org-plain-link-re "\\|"
  8304. org-bracket-link-regexp "\\|"
  8305. org-angle-link-re "\\|"
  8306. "[ \t]:[^ \t\n]+:[ \t]*$")))
  8307. (not (get-text-property (point) 'org-linked-text)))
  8308. (or (org-offer-links-in-entry arg)
  8309. (progn (require 'org-attach) (org-attach-reveal 'if-exists))))
  8310. ((run-hook-with-args-until-success 'org-open-at-point-functions))
  8311. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  8312. ((and (or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  8313. (not (org-in-regexp org-bracket-link-regexp)))
  8314. (org-footnote-action))
  8315. (t
  8316. (let (type path link line search (pos (point)))
  8317. (catch 'match
  8318. (save-excursion
  8319. (skip-chars-forward "^]\n\r")
  8320. (when (org-in-regexp org-bracket-link-regexp 1)
  8321. (setq link (org-extract-attributes
  8322. (org-link-unescape (org-match-string-no-properties 1))))
  8323. (while (string-match " *\n *" link)
  8324. (setq link (replace-match " " t t link)))
  8325. (setq link (org-link-expand-abbrev link))
  8326. (cond
  8327. ((or (file-name-absolute-p link)
  8328. (string-match "^\\.\\.?/" link))
  8329. (setq type "file" path link))
  8330. ((string-match org-link-re-with-space3 link)
  8331. (setq type (match-string 1 link) path (match-string 2 link)))
  8332. (t (setq type "thisfile" path link)))
  8333. (throw 'match t)))
  8334. (when (get-text-property (point) 'org-linked-text)
  8335. (setq type "thisfile"
  8336. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  8337. (1+ (point)) (point))
  8338. path (buffer-substring
  8339. (or (previous-single-property-change pos 'org-linked-text)
  8340. (point-min))
  8341. (or (next-single-property-change pos 'org-linked-text)
  8342. (point-max))))
  8343. (throw 'match t))
  8344. (save-excursion
  8345. (when (or (org-in-regexp org-angle-link-re)
  8346. (org-in-regexp org-plain-link-re))
  8347. (setq type (match-string 1) path (match-string 2))
  8348. (throw 'match t)))
  8349. (save-excursion
  8350. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@#%:]+\\):[ \t]*$"))
  8351. (setq type "tags"
  8352. path (match-string 1))
  8353. (while (string-match ":" path)
  8354. (setq path (replace-match "+" t t path)))
  8355. (throw 'match t)))
  8356. (when (org-in-regexp "<\\([^><\n]+\\)>")
  8357. (setq type "tree-match"
  8358. path (match-string 1))
  8359. (throw 'match t)))
  8360. (unless path
  8361. (error "No link found"))
  8362. ;; switch back to reference buffer
  8363. ;; needed when if called in a temporary buffer through
  8364. ;; org-open-link-from-string
  8365. (with-current-buffer (or reference-buffer (current-buffer))
  8366. ;; Remove any trailing spaces in path
  8367. (if (string-match " +\\'" path)
  8368. (setq path (replace-match "" t t path)))
  8369. (if (and org-link-translation-function
  8370. (fboundp org-link-translation-function))
  8371. ;; Check if we need to translate the link
  8372. (let ((tmp (funcall org-link-translation-function type path)))
  8373. (setq type (car tmp) path (cdr tmp))))
  8374. (cond
  8375. ((assoc type org-link-protocols)
  8376. (funcall (nth 1 (assoc type org-link-protocols)) path))
  8377. ((equal type "mailto")
  8378. (let ((cmd (car org-link-mailto-program))
  8379. (args (cdr org-link-mailto-program)) args1
  8380. (address path) (subject "") a)
  8381. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  8382. (setq address (match-string 1 path)
  8383. subject (org-link-escape (match-string 2 path))))
  8384. (while args
  8385. (cond
  8386. ((not (stringp (car args))) (push (pop args) args1))
  8387. (t (setq a (pop args))
  8388. (if (string-match "%a" a)
  8389. (setq a (replace-match address t t a)))
  8390. (if (string-match "%s" a)
  8391. (setq a (replace-match subject t t a)))
  8392. (push a args1))))
  8393. (apply cmd (nreverse args1))))
  8394. ((member type '("http" "https" "ftp" "news"))
  8395. (browse-url (concat type ":" (org-link-escape
  8396. path org-link-escape-chars-browser))))
  8397. ((string= type "doi")
  8398. (browse-url (concat "http://dx.doi.org/"
  8399. (org-link-escape
  8400. path org-link-escape-chars-browser))))
  8401. ((member type '("message"))
  8402. (browse-url (concat type ":" path)))
  8403. ((string= type "tags")
  8404. (org-tags-view arg path))
  8405. ((string= type "tree-match")
  8406. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  8407. ((string= type "file")
  8408. (if (string-match "::\\([0-9]+\\)\\'" path)
  8409. (setq line (string-to-number (match-string 1 path))
  8410. path (substring path 0 (match-beginning 0)))
  8411. (if (string-match "::\\(.+\\)\\'" path)
  8412. (setq search (match-string 1 path)
  8413. path (substring path 0 (match-beginning 0)))))
  8414. (if (string-match "[*?{]" (file-name-nondirectory path))
  8415. (dired path)
  8416. (org-open-file path arg line search)))
  8417. ((string= type "shell")
  8418. (let ((cmd path))
  8419. (if (or (and (not (string= org-confirm-shell-link-not-regexp ""))
  8420. (string-match org-confirm-shell-link-not-regexp cmd))
  8421. (not org-confirm-shell-link-function)
  8422. (funcall org-confirm-shell-link-function
  8423. (format "Execute \"%s\" in shell? "
  8424. (org-add-props cmd nil
  8425. 'face 'org-warning))))
  8426. (progn
  8427. (message "Executing %s" cmd)
  8428. (shell-command cmd))
  8429. (error "Abort"))))
  8430. ((string= type "elisp")
  8431. (let ((cmd path))
  8432. (if (or (and (not (string= org-confirm-elisp-link-not-regexp ""))
  8433. (string-match org-confirm-elisp-link-not-regexp cmd))
  8434. (not org-confirm-elisp-link-function)
  8435. (funcall org-confirm-elisp-link-function
  8436. (format "Execute \"%s\" as elisp? "
  8437. (org-add-props cmd nil
  8438. 'face 'org-warning))))
  8439. (message "%s => %s" cmd
  8440. (if (equal (string-to-char cmd) ?\()
  8441. (eval (read cmd))
  8442. (call-interactively (read cmd))))
  8443. (error "Abort"))))
  8444. ((and (string= type "thisfile")
  8445. (run-hook-with-args-until-success
  8446. 'org-open-link-functions path)))
  8447. ((string= type "thisfile")
  8448. (if arg
  8449. (switch-to-buffer-other-window
  8450. (org-get-buffer-for-internal-link (current-buffer)))
  8451. (org-mark-ring-push))
  8452. (let ((cmd `(org-link-search
  8453. ,path
  8454. ,(cond ((equal arg '(4)) ''occur)
  8455. ((equal arg '(16)) ''org-occur)
  8456. (t nil))
  8457. ,pos)))
  8458. (condition-case nil (eval cmd)
  8459. (error (progn (widen) (eval cmd))))))
  8460. (t
  8461. (browse-url-at-point)))))))
  8462. (move-marker org-open-link-marker nil)
  8463. (run-hook-with-args 'org-follow-link-hook)))
  8464. (defun org-offer-links-in-entry (&optional nth zero)
  8465. "Offer links in the current entry and follow the selected link.
  8466. If there is only one link, follow it immediately as well.
  8467. If NTH is an integer, immediately pick the NTH link found.
  8468. If ZERO is a string, check also this string for a link, and if
  8469. there is one, offer it as link number zero."
  8470. (let ((re (concat "\\(" org-bracket-link-regexp "\\)\\|"
  8471. "\\(" org-angle-link-re "\\)\\|"
  8472. "\\(" org-plain-link-re "\\)"))
  8473. (cnt ?0)
  8474. (in-emacs (if (integerp nth) nil nth))
  8475. have-zero end links link c)
  8476. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  8477. (push (match-string 0 zero) links)
  8478. (setq cnt (1- cnt) have-zero t))
  8479. (save-excursion
  8480. (org-back-to-heading t)
  8481. (setq end (save-excursion (outline-next-heading) (point)))
  8482. (while (re-search-forward re end t)
  8483. (push (match-string 0) links))
  8484. (setq links (org-uniquify (reverse links))))
  8485. (cond
  8486. ((null links)
  8487. (message "No links"))
  8488. ((equal (length links) 1)
  8489. (setq link (list (car links))))
  8490. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  8491. (setq link (nth (if have-zero nth (1- nth)) links)))
  8492. (t ; we have to select a link
  8493. (save-excursion
  8494. (save-window-excursion
  8495. (delete-other-windows)
  8496. (with-output-to-temp-buffer "*Select Link*"
  8497. (mapc (lambda (l)
  8498. (if (not (string-match org-bracket-link-regexp l))
  8499. (princ (format "[%c] %s\n" (incf cnt)
  8500. (org-remove-angle-brackets l)))
  8501. (if (match-end 3)
  8502. (princ (format "[%c] %s (%s)\n" (incf cnt)
  8503. (match-string 3 l) (match-string 1 l)))
  8504. (princ (format "[%c] %s\n" (incf cnt)
  8505. (match-string 1 l))))))
  8506. links))
  8507. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  8508. (message "Select link to open, RET to open all:")
  8509. (setq c (read-char-exclusive))
  8510. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  8511. (when (equal c ?q) (error "Abort"))
  8512. (if (equal c ?\C-m)
  8513. (setq link links)
  8514. (setq nth (- c ?0))
  8515. (if have-zero (setq nth (1+ nth)))
  8516. (unless (and (integerp nth) (>= (length links) nth))
  8517. (error "Invalid link selection"))
  8518. (setq link (list (nth (1- nth) links))))))
  8519. (if link
  8520. (let ((buf (current-buffer)))
  8521. (dolist (l link)
  8522. (org-open-link-from-string l in-emacs buf))
  8523. t)
  8524. nil)))
  8525. ;; Add special file links that specify the way of opening
  8526. (org-add-link-type "file+sys" 'org-open-file-with-system)
  8527. (org-add-link-type "file+emacs" 'org-open-file-with-emacs)
  8528. (defun org-open-file-with-system (path)
  8529. "Open file at PATH using the system way of opening it."
  8530. (org-open-file path 'system))
  8531. (defun org-open-file-with-emacs (path)
  8532. "Open file at PATH in Emacs."
  8533. (org-open-file path 'emacs))
  8534. (defun org-remove-file-link-modifiers ()
  8535. "Remove the file link modifiers in `file+sys:' and `file+emacs:' links."
  8536. (goto-char (point-min))
  8537. (while (re-search-forward "\\<file\\+\\(sys\\|emacs\\):" nil t)
  8538. (org-if-unprotected
  8539. (replace-match "file:" t t))))
  8540. (eval-after-load "org-exp"
  8541. '(add-hook 'org-export-preprocess-before-normalizing-links-hook
  8542. 'org-remove-file-link-modifiers))
  8543. ;;;; Time estimates
  8544. (defun org-get-effort (&optional pom)
  8545. "Get the effort estimate for the current entry."
  8546. (org-entry-get pom org-effort-property))
  8547. ;;; File search
  8548. (defvar org-create-file-search-functions nil
  8549. "List of functions to construct the right search string for a file link.
  8550. These functions are called in turn with point at the location to
  8551. which the link should point.
  8552. A function in the hook should first test if it would like to
  8553. handle this file type, for example by checking the `major-mode'
  8554. or the file extension. If it decides not to handle this file, it
  8555. should just return nil to give other functions a chance. If it
  8556. does handle the file, it must return the search string to be used
  8557. when following the link. The search string will be part of the
  8558. file link, given after a double colon, and `org-open-at-point'
  8559. will automatically search for it. If special measures must be
  8560. taken to make the search successful, another function should be
  8561. added to the companion hook `org-execute-file-search-functions',
  8562. which see.
  8563. A function in this hook may also use `setq' to set the variable
  8564. `description' to provide a suggestion for the descriptive text to
  8565. be used for this link when it gets inserted into an Org-mode
  8566. buffer with \\[org-insert-link].")
  8567. (defvar org-execute-file-search-functions nil
  8568. "List of functions to execute a file search triggered by a link.
  8569. Functions added to this hook must accept a single argument, the
  8570. search string that was part of the file link, the part after the
  8571. double colon. The function must first check if it would like to
  8572. handle this search, for example by checking the `major-mode' or
  8573. the file extension. If it decides not to handle this search, it
  8574. should just return nil to give other functions a chance. If it
  8575. does handle the search, it must return a non-nil value to keep
  8576. other functions from trying.
  8577. Each function can access the current prefix argument through the
  8578. variable `current-prefix-argument'. Note that a single prefix is
  8579. used to force opening a link in Emacs, so it may be good to only
  8580. use a numeric or double prefix to guide the search function.
  8581. In case this is needed, a function in this hook can also restore
  8582. the window configuration before `org-open-at-point' was called using:
  8583. (set-window-configuration org-window-config-before-follow-link)")
  8584. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  8585. (defun org-link-search (s &optional type avoid-pos)
  8586. "Search for a link search option.
  8587. If S is surrounded by forward slashes, it is interpreted as a
  8588. regular expression. In org-mode files, this will create an `org-occur'
  8589. sparse tree. In ordinary files, `occur' will be used to list matches.
  8590. If the current buffer is in `dired-mode', grep will be used to search
  8591. in all files. If AVOID-POS is given, ignore matches near that position."
  8592. (let ((case-fold-search t)
  8593. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  8594. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  8595. (append '(("") (" ") ("\t") ("\n"))
  8596. org-emphasis-alist)
  8597. "\\|") "\\)"))
  8598. (pos (point))
  8599. (pre nil) (post nil)
  8600. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  8601. (cond
  8602. ;; First check if there are any special search functions
  8603. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  8604. ;; Now try the builtin stuff
  8605. ((and (equal (string-to-char s0) ?#)
  8606. (> (length s0) 1)
  8607. (save-excursion
  8608. (goto-char (point-min))
  8609. (and
  8610. (re-search-forward
  8611. (concat "^[ \t]*:CUSTOM_ID:[ \t]+" (regexp-quote (substring s0 1)) "[ \t]*$") nil t)
  8612. (setq type 'dedicated
  8613. pos (match-beginning 0))))
  8614. ;; There is an exact target for this
  8615. (goto-char pos)
  8616. (org-back-to-heading t)))
  8617. ((save-excursion
  8618. (goto-char (point-min))
  8619. (and
  8620. (re-search-forward
  8621. (concat "<<" (regexp-quote s0) ">>") nil t)
  8622. (setq type 'dedicated
  8623. pos (match-beginning 0))))
  8624. ;; There is an exact target for this
  8625. (goto-char pos))
  8626. ((and (string-match "^(\\(.*\\))$" s0)
  8627. (save-excursion
  8628. (goto-char (point-min))
  8629. (and
  8630. (re-search-forward
  8631. (concat "[^[]" (regexp-quote
  8632. (format org-coderef-label-format
  8633. (match-string 1 s0))))
  8634. nil t)
  8635. (setq type 'dedicated
  8636. pos (1+ (match-beginning 0))))))
  8637. ;; There is a coderef target for this
  8638. (goto-char pos))
  8639. ((string-match "^/\\(.*\\)/$" s)
  8640. ;; A regular expression
  8641. (cond
  8642. ((org-mode-p)
  8643. (org-occur (match-string 1 s)))
  8644. ;;((eq major-mode 'dired-mode)
  8645. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  8646. (t (org-do-occur (match-string 1 s)))))
  8647. ((and (org-mode-p) org-link-search-must-match-exact-headline)
  8648. (and (equal (string-to-char s) ?*) (setq s (substring s 1)))
  8649. (goto-char (point-min))
  8650. (cond
  8651. ((let (case-fold-search)
  8652. (re-search-forward (format org-complex-heading-regexp-format
  8653. (regexp-quote s))
  8654. nil t))
  8655. ;; OK, found a match
  8656. (setq type 'dedicated)
  8657. (goto-char (match-beginning 0)))
  8658. ((and (not org-link-search-inhibit-query)
  8659. (eq org-link-search-must-match-exact-headline 'query-to-create)
  8660. (y-or-n-p "No match - create this as a new heading? "))
  8661. (goto-char (point-max))
  8662. (or (bolp) (newline))
  8663. (insert "* " s "\n")
  8664. (beginning-of-line 0))
  8665. (t
  8666. (goto-char pos)
  8667. (error "No match"))))
  8668. (t
  8669. ;; A normal search string
  8670. (when (equal (string-to-char s) ?*)
  8671. ;; Anchor on headlines, post may include tags.
  8672. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  8673. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@#%:+]:[ \t]*\\)?$")
  8674. s (substring s 1)))
  8675. (remove-text-properties
  8676. 0 (length s)
  8677. '(face nil mouse-face nil keymap nil fontified nil) s)
  8678. ;; Make a series of regular expressions to find a match
  8679. (setq words (org-split-string s "[ \n\r\t]+")
  8680. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  8681. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  8682. "\\)" markers)
  8683. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  8684. re2a (concat "[ \t\r\n]" re2a_)
  8685. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  8686. re4 (concat "[^a-zA-Z_]" re4_)
  8687. re1 (concat pre re2 post)
  8688. re3 (concat pre (if pre re4_ re4) post)
  8689. re5 (concat pre ".*" re4)
  8690. re2 (concat pre re2)
  8691. re2a (concat pre (if pre re2a_ re2a))
  8692. re4 (concat pre (if pre re4_ re4))
  8693. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  8694. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  8695. re5 "\\)"
  8696. ))
  8697. (cond
  8698. ((eq type 'org-occur) (org-occur reall))
  8699. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  8700. (t (goto-char (point-min))
  8701. (setq type 'fuzzy)
  8702. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  8703. (org-search-not-self 1 re1 nil t)
  8704. (org-search-not-self 1 re2 nil t)
  8705. (org-search-not-self 1 re2a nil t)
  8706. (org-search-not-self 1 re3 nil t)
  8707. (org-search-not-self 1 re4 nil t)
  8708. (org-search-not-self 1 re5 nil t)
  8709. )
  8710. (goto-char (match-beginning 1))
  8711. (goto-char pos)
  8712. (error "No match"))))))
  8713. (and (org-mode-p) (org-show-context 'link-search))
  8714. type))
  8715. (defun org-search-not-self (group &rest args)
  8716. "Execute `re-search-forward', but only accept matches that do not
  8717. enclose the position of `org-open-link-marker'."
  8718. (let ((m org-open-link-marker))
  8719. (catch 'exit
  8720. (while (apply 're-search-forward args)
  8721. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  8722. (goto-char (match-end group))
  8723. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  8724. (> (match-beginning 0) (marker-position m))
  8725. (< (match-end 0) (marker-position m)))
  8726. (save-match-data
  8727. (or (not (org-in-regexp
  8728. org-bracket-link-analytic-regexp 1))
  8729. (not (match-end 4)) ; no description
  8730. (and (<= (match-beginning 4) (point))
  8731. (>= (match-end 4) (point))))))
  8732. (throw 'exit (point))))))))
  8733. (defun org-get-buffer-for-internal-link (buffer)
  8734. "Return a buffer to be used for displaying the link target of internal links."
  8735. (cond
  8736. ((not org-display-internal-link-with-indirect-buffer)
  8737. buffer)
  8738. ((string-match "(Clone)$" (buffer-name buffer))
  8739. (message "Buffer is already a clone, not making another one")
  8740. ;; we also do not modify visibility in this case
  8741. buffer)
  8742. (t ; make a new indirect buffer for displaying the link
  8743. (let* ((bn (buffer-name buffer))
  8744. (ibn (concat bn "(Clone)"))
  8745. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  8746. (with-current-buffer ib (org-overview))
  8747. ib))))
  8748. (defun org-do-occur (regexp &optional cleanup)
  8749. "Call the Emacs command `occur'.
  8750. If CLEANUP is non-nil, remove the printout of the regular expression
  8751. in the *Occur* buffer. This is useful if the regex is long and not useful
  8752. to read."
  8753. (occur regexp)
  8754. (when cleanup
  8755. (let ((cwin (selected-window)) win beg end)
  8756. (when (setq win (get-buffer-window "*Occur*"))
  8757. (select-window win))
  8758. (goto-char (point-min))
  8759. (when (re-search-forward "match[a-z]+" nil t)
  8760. (setq beg (match-end 0))
  8761. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  8762. (setq end (1- (match-beginning 0)))))
  8763. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  8764. (goto-char (point-min))
  8765. (select-window cwin))))
  8766. ;;; The mark ring for links jumps
  8767. (defvar org-mark-ring nil
  8768. "Mark ring for positions before jumps in Org-mode.")
  8769. (defvar org-mark-ring-last-goto nil
  8770. "Last position in the mark ring used to go back.")
  8771. ;; Fill and close the ring
  8772. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  8773. (loop for i from 1 to org-mark-ring-length do
  8774. (push (make-marker) org-mark-ring))
  8775. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  8776. org-mark-ring)
  8777. (defun org-mark-ring-push (&optional pos buffer)
  8778. "Put the current position or POS into the mark ring and rotate it."
  8779. (interactive)
  8780. (setq pos (or pos (point)))
  8781. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  8782. (move-marker (car org-mark-ring)
  8783. (or pos (point))
  8784. (or buffer (current-buffer)))
  8785. (message "%s"
  8786. (substitute-command-keys
  8787. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  8788. (defun org-mark-ring-goto (&optional n)
  8789. "Jump to the previous position in the mark ring.
  8790. With prefix arg N, jump back that many stored positions. When
  8791. called several times in succession, walk through the entire ring.
  8792. Org-mode commands jumping to a different position in the current file,
  8793. or to another Org-mode file, automatically push the old position
  8794. onto the ring."
  8795. (interactive "p")
  8796. (let (p m)
  8797. (if (eq last-command this-command)
  8798. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  8799. (setq p org-mark-ring))
  8800. (setq org-mark-ring-last-goto p)
  8801. (setq m (car p))
  8802. (switch-to-buffer (marker-buffer m))
  8803. (goto-char m)
  8804. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  8805. (defun org-remove-angle-brackets (s)
  8806. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  8807. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  8808. s)
  8809. (defun org-add-angle-brackets (s)
  8810. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  8811. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  8812. s)
  8813. (defun org-remove-double-quotes (s)
  8814. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  8815. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  8816. s)
  8817. ;;; Following specific links
  8818. (defun org-follow-timestamp-link ()
  8819. (cond
  8820. ((org-at-date-range-p t)
  8821. (let ((org-agenda-start-on-weekday)
  8822. (t1 (match-string 1))
  8823. (t2 (match-string 2)))
  8824. (setq t1 (time-to-days (org-time-string-to-time t1))
  8825. t2 (time-to-days (org-time-string-to-time t2)))
  8826. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  8827. ((org-at-timestamp-p t)
  8828. (org-agenda-list nil (time-to-days (org-time-string-to-time
  8829. (substring (match-string 1) 0 10)))
  8830. 1))
  8831. (t (error "This should not happen"))))
  8832. ;;; Following file links
  8833. (defvar org-wait nil)
  8834. (defun org-open-file (path &optional in-emacs line search)
  8835. "Open the file at PATH.
  8836. First, this expands any special file name abbreviations. Then the
  8837. configuration variable `org-file-apps' is checked if it contains an
  8838. entry for this file type, and if yes, the corresponding command is launched.
  8839. If no application is found, Emacs simply visits the file.
  8840. With optional prefix argument IN-EMACS, Emacs will visit the file.
  8841. With a double \\[universal-argument] \\[universal-argument] \
  8842. prefix arg, Org tries to avoid opening in Emacs
  8843. and to use an external application to visit the file.
  8844. Optional LINE specifies a line to go to, optional SEARCH a string
  8845. to search for. If LINE or SEARCH is given, the file will be
  8846. opened in Emacs, unless an entry from org-file-apps that makes
  8847. use of groups in a regexp matches.
  8848. If the file does not exist, an error is thrown."
  8849. (let* ((file (if (equal path "")
  8850. buffer-file-name
  8851. (substitute-in-file-name (expand-file-name path))))
  8852. (file-apps (append org-file-apps (org-default-apps)))
  8853. (apps (org-remove-if
  8854. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8855. (apps-dlink (org-remove-if-not
  8856. 'org-file-apps-entry-match-against-dlink-p file-apps))
  8857. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  8858. (dirp (if remp nil (file-directory-p file)))
  8859. (file (if (and dirp org-open-directory-means-index-dot-org)
  8860. (concat (file-name-as-directory file) "index.org")
  8861. file))
  8862. (a-m-a-p (assq 'auto-mode apps))
  8863. (dfile (downcase file))
  8864. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  8865. (link (cond ((and (eq line nil)
  8866. (eq search nil))
  8867. file)
  8868. (line
  8869. (concat file "::" (number-to-string line)))
  8870. (search
  8871. (concat file "::" search))))
  8872. (dlink (downcase link))
  8873. (old-buffer (current-buffer))
  8874. (old-pos (point))
  8875. (old-mode major-mode)
  8876. ext cmd link-match-data)
  8877. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  8878. (setq ext (match-string 1 dfile))
  8879. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  8880. (setq ext (match-string 1 dfile))))
  8881. (cond
  8882. ((member in-emacs '((16) system))
  8883. (setq cmd (cdr (assoc 'system apps))))
  8884. (in-emacs (setq cmd 'emacs))
  8885. (t
  8886. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  8887. (and dirp (cdr (assoc 'directory apps)))
  8888. ; first, try matching against apps-dlink
  8889. ; if we get a match here, store the match data for later
  8890. (let ((match (assoc-default dlink apps-dlink
  8891. 'string-match)))
  8892. (if match
  8893. (progn (setq link-match-data (match-data))
  8894. match)
  8895. (progn (setq in-emacs (or in-emacs line search))
  8896. nil))) ; if we have no match in apps-dlink,
  8897. ; always open the file in emacs if line or search
  8898. ; is given (for backwards compatibility)
  8899. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  8900. 'string-match)
  8901. (cdr (assoc ext apps))
  8902. (cdr (assoc t apps))))))
  8903. (when (eq cmd 'system)
  8904. (setq cmd (cdr (assoc 'system apps))))
  8905. (when (eq cmd 'default)
  8906. (setq cmd (cdr (assoc t apps))))
  8907. (when (eq cmd 'mailcap)
  8908. (require 'mailcap)
  8909. (mailcap-parse-mailcaps)
  8910. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  8911. (command (mailcap-mime-info mime-type)))
  8912. (if (stringp command)
  8913. (setq cmd command)
  8914. (setq cmd 'emacs))))
  8915. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  8916. (not (file-exists-p file))
  8917. (not org-open-non-existing-files))
  8918. (error "No such file: %s" file))
  8919. (cond
  8920. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  8921. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  8922. (while (string-match "['\"]%s['\"]" cmd)
  8923. (setq cmd (replace-match "%s" t t cmd)))
  8924. (while (string-match "%s" cmd)
  8925. (setq cmd (replace-match
  8926. (save-match-data
  8927. (shell-quote-argument
  8928. (convert-standard-filename file)))
  8929. t t cmd)))
  8930. ;; Replace "%1", "%2" etc. in command with group matches from regex
  8931. (save-match-data
  8932. (let ((match-index 1)
  8933. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  8934. (set-match-data link-match-data)
  8935. (while (<= match-index number-of-groups)
  8936. (let ((regex (concat "%" (number-to-string match-index)))
  8937. (replace-with (match-string match-index dlink)))
  8938. (while (string-match regex cmd)
  8939. (setq cmd (replace-match replace-with t t cmd))))
  8940. (setq match-index (+ match-index 1)))))
  8941. (save-window-excursion
  8942. (start-process-shell-command cmd nil cmd)
  8943. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  8944. ))
  8945. ((or (stringp cmd)
  8946. (eq cmd 'emacs))
  8947. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  8948. (widen)
  8949. (if line (org-goto-line line)
  8950. (if search (org-link-search search))))
  8951. ((consp cmd)
  8952. (let ((file (convert-standard-filename file)))
  8953. (save-match-data
  8954. (set-match-data link-match-data)
  8955. (eval cmd))))
  8956. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8957. (and (org-mode-p) (eq old-mode 'org-mode)
  8958. (or (not (equal old-buffer (current-buffer)))
  8959. (not (equal old-pos (point))))
  8960. (org-mark-ring-push old-pos old-buffer))))
  8961. (defun org-file-apps-entry-match-against-dlink-p (entry)
  8962. "This function returns non-nil if `entry' uses a regular
  8963. expression which should be matched against the whole link by
  8964. org-open-file.
  8965. It assumes that is the case when the entry uses a regular
  8966. expression which has at least one grouping construct and the
  8967. action is either a lisp form or a command string containing
  8968. '%1', i.e. using at least one subexpression match as a
  8969. parameter."
  8970. (let ((selector (car entry))
  8971. (action (cdr entry)))
  8972. (if (stringp selector)
  8973. (and (> (regexp-opt-depth selector) 0)
  8974. (or (and (stringp action)
  8975. (string-match "%[0-9]" action))
  8976. (consp action)))
  8977. nil)))
  8978. (defun org-default-apps ()
  8979. "Return the default applications for this operating system."
  8980. (cond
  8981. ((eq system-type 'darwin)
  8982. org-file-apps-defaults-macosx)
  8983. ((eq system-type 'windows-nt)
  8984. org-file-apps-defaults-windowsnt)
  8985. (t org-file-apps-defaults-gnu)))
  8986. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  8987. "Convert extensions to regular expressions in the cars of LIST.
  8988. Also, weed out any non-string entries, because the return value is used
  8989. only for regexp matching.
  8990. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  8991. point to the symbol `emacs', indicating that the file should
  8992. be opened in Emacs."
  8993. (append
  8994. (delq nil
  8995. (mapcar (lambda (x)
  8996. (if (not (stringp (car x)))
  8997. nil
  8998. (if (string-match "\\W" (car x))
  8999. x
  9000. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  9001. list))
  9002. (if add-auto-mode
  9003. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  9004. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  9005. (defun org-file-remote-p (file)
  9006. "Test whether FILE specifies a location on a remote system.
  9007. Return non-nil if the location is indeed remote.
  9008. For example, the filename \"/user@host:/foo\" specifies a location
  9009. on the system \"/user@host:\"."
  9010. (cond ((fboundp 'file-remote-p)
  9011. (file-remote-p file))
  9012. ((fboundp 'tramp-handle-file-remote-p)
  9013. (tramp-handle-file-remote-p file))
  9014. ((and (boundp 'ange-ftp-name-format)
  9015. (string-match (car ange-ftp-name-format) file))
  9016. t)
  9017. (t nil)))
  9018. ;;;; Refiling
  9019. (defun org-get-org-file ()
  9020. "Read a filename, with default directory `org-directory'."
  9021. (let ((default (or org-default-notes-file remember-data-file)))
  9022. (read-file-name (format "File name [%s]: " default)
  9023. (file-name-as-directory org-directory)
  9024. default)))
  9025. (defun org-notes-order-reversed-p ()
  9026. "Check if the current file should receive notes in reversed order."
  9027. (cond
  9028. ((not org-reverse-note-order) nil)
  9029. ((eq t org-reverse-note-order) t)
  9030. ((not (listp org-reverse-note-order)) nil)
  9031. (t (catch 'exit
  9032. (let ((all org-reverse-note-order)
  9033. entry)
  9034. (while (setq entry (pop all))
  9035. (if (string-match (car entry) buffer-file-name)
  9036. (throw 'exit (cdr entry))))
  9037. nil)))))
  9038. (defvar org-refile-target-table nil
  9039. "The list of refile targets, created by `org-refile'.")
  9040. (defvar org-agenda-new-buffers nil
  9041. "Buffers created to visit agenda files.")
  9042. (defvar org-refile-cache nil
  9043. "Cache for refile targets.")
  9044. (defvar org-refile-markers nil
  9045. "All the markers used for caching refile locations.")
  9046. (defun org-refile-marker (pos)
  9047. "Get a new refile marker, but only if caching is in use."
  9048. (if (not org-refile-use-cache)
  9049. pos
  9050. (let ((m (make-marker)))
  9051. (move-marker m pos)
  9052. (push m org-refile-markers)
  9053. m)))
  9054. (defun org-refile-cache-clear ()
  9055. "Clear the refile cache and disable all the markers."
  9056. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  9057. (setq org-refile-markers nil)
  9058. (setq org-refile-cache nil)
  9059. (message "Refile cache has been cleared"))
  9060. (defun org-refile-cache-check-set (set)
  9061. "Check if all the markers in the cache still have live buffers."
  9062. (let (marker)
  9063. (catch 'exit
  9064. (while (and set (setq marker (nth 3 (pop set))))
  9065. ;; if org-refile-use-outline-path is 'file, marker may be nil
  9066. (when (and marker (null (marker-buffer marker)))
  9067. (message "not found") (sit-for 3)
  9068. (throw 'exit nil)))
  9069. t)))
  9070. (defun org-refile-cache-put (set &rest identifiers)
  9071. "Push the refile targets SET into the cache, under IDENTIFIERS."
  9072. (let* ((key (sha1 (prin1-to-string identifiers)))
  9073. (entry (assoc key org-refile-cache)))
  9074. (if entry
  9075. (setcdr entry set)
  9076. (push (cons key set) org-refile-cache))))
  9077. (defun org-refile-cache-get (&rest identifiers)
  9078. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  9079. (cond
  9080. ((not org-refile-cache) nil)
  9081. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  9082. (t
  9083. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  9084. org-refile-cache))))
  9085. (and set (org-refile-cache-check-set set) set)))))
  9086. (defun org-refile-get-targets (&optional default-buffer)
  9087. "Produce a table with refile targets."
  9088. (let ((case-fold-search nil)
  9089. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  9090. (entries (or org-refile-targets '((nil . (:level . 1)))))
  9091. targets tgs txt re files f desc descre fast-path-p level pos0)
  9092. (message "Getting targets...")
  9093. (with-current-buffer (or default-buffer (current-buffer))
  9094. (while (setq entry (pop entries))
  9095. (setq files (car entry) desc (cdr entry))
  9096. (setq fast-path-p nil)
  9097. (cond
  9098. ((null files) (setq files (list (current-buffer))))
  9099. ((eq files 'org-agenda-files)
  9100. (setq files (org-agenda-files 'unrestricted)))
  9101. ((and (symbolp files) (fboundp files))
  9102. (setq files (funcall files)))
  9103. ((and (symbolp files) (boundp files))
  9104. (setq files (symbol-value files))))
  9105. (if (stringp files) (setq files (list files)))
  9106. (cond
  9107. ((eq (car desc) :tag)
  9108. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  9109. ((eq (car desc) :todo)
  9110. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  9111. ((eq (car desc) :regexp)
  9112. (setq descre (cdr desc)))
  9113. ((eq (car desc) :level)
  9114. (setq descre (concat "^\\*\\{" (number-to-string
  9115. (if org-odd-levels-only
  9116. (1- (* 2 (cdr desc)))
  9117. (cdr desc)))
  9118. "\\}[ \t]")))
  9119. ((eq (car desc) :maxlevel)
  9120. (setq fast-path-p t)
  9121. (setq descre (concat "^\\*\\{1," (number-to-string
  9122. (if org-odd-levels-only
  9123. (1- (* 2 (cdr desc)))
  9124. (cdr desc)))
  9125. "\\}[ \t]")))
  9126. (t (error "Bad refiling target description %s" desc)))
  9127. (while (setq f (pop files))
  9128. (with-current-buffer
  9129. (if (bufferp f) f (org-get-agenda-file-buffer f))
  9130. (or
  9131. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  9132. (progn
  9133. (if (bufferp f) (setq f (buffer-file-name
  9134. (buffer-base-buffer f))))
  9135. (setq f (and f (expand-file-name f)))
  9136. (if (eq org-refile-use-outline-path 'file)
  9137. (push (list (file-name-nondirectory f) f nil nil) tgs))
  9138. (save-excursion
  9139. (save-restriction
  9140. (widen)
  9141. (goto-char (point-min))
  9142. (while (re-search-forward descre nil t)
  9143. (goto-char (setq pos0 (point-at-bol)))
  9144. (catch 'next
  9145. (when org-refile-target-verify-function
  9146. (save-match-data
  9147. (or (funcall org-refile-target-verify-function)
  9148. (throw 'next t))))
  9149. (when (looking-at org-complex-heading-regexp)
  9150. (setq level (org-reduced-level
  9151. (- (match-end 1) (match-beginning 1)))
  9152. txt (org-link-display-format (match-string 4))
  9153. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  9154. re (format org-complex-heading-regexp-format
  9155. (regexp-quote (match-string 4))))
  9156. (when org-refile-use-outline-path
  9157. (setq txt (mapconcat
  9158. 'org-protect-slash
  9159. (append
  9160. (if (eq org-refile-use-outline-path
  9161. 'file)
  9162. (list (file-name-nondirectory
  9163. (buffer-file-name
  9164. (buffer-base-buffer))))
  9165. (if (eq org-refile-use-outline-path
  9166. 'full-file-path)
  9167. (list (buffer-file-name
  9168. (buffer-base-buffer)))))
  9169. (org-get-outline-path fast-path-p
  9170. level txt)
  9171. (list txt))
  9172. "/")))
  9173. (push (list txt f re (org-refile-marker (point)))
  9174. tgs)))
  9175. (when (= (point) pos0)
  9176. ;; verification function has not moved point
  9177. (goto-char (point-at-eol))))))))
  9178. (when org-refile-use-cache
  9179. (org-refile-cache-put tgs (buffer-file-name) descre))
  9180. (setq targets (append tgs targets))
  9181. ))))
  9182. (message "Getting targets...done")
  9183. (nreverse targets)))
  9184. (defun org-protect-slash (s)
  9185. (while (string-match "/" s)
  9186. (setq s (replace-match "\\" t t s)))
  9187. s)
  9188. (defvar org-olpa (make-vector 20 nil))
  9189. (defun org-get-outline-path (&optional fastp level heading)
  9190. "Return the outline path to the current entry, as a list.
  9191. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  9192. routine which makes outline path derivations for an entire file,
  9193. avoiding backtracing. Refile target collection makes use of that."
  9194. (if fastp
  9195. (progn
  9196. (if (> level 19)
  9197. (error "Outline path failure, more than 19 levels"))
  9198. (loop for i from level upto 19 do
  9199. (aset org-olpa i nil))
  9200. (prog1
  9201. (delq nil (append org-olpa nil))
  9202. (aset org-olpa level heading)))
  9203. (let (rtn case-fold-search)
  9204. (save-excursion
  9205. (save-restriction
  9206. (widen)
  9207. (while (org-up-heading-safe)
  9208. (when (looking-at org-complex-heading-regexp)
  9209. (push (org-match-string-no-properties 4) rtn)))
  9210. rtn)))))
  9211. (defun org-format-outline-path (path &optional width prefix)
  9212. "Format the outline path PATH for display.
  9213. Width is the maximum number of characters that is available.
  9214. Prefix is a prefix to be included in the returned string,
  9215. such as the file name."
  9216. (setq width (or width 79))
  9217. (if prefix (setq width (- width (length prefix))))
  9218. (if (not path)
  9219. (or prefix "")
  9220. (let* ((nsteps (length path))
  9221. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  9222. (maxwidth (if (<= total-width width)
  9223. 10000 ;; everything fits
  9224. ;; we need to shorten the level headings
  9225. (/ (- width nsteps) nsteps)))
  9226. (org-odd-levels-only nil)
  9227. (n 0)
  9228. (total (1+ (length prefix))))
  9229. (setq maxwidth (max maxwidth 10))
  9230. (concat prefix
  9231. (mapconcat
  9232. (lambda (h)
  9233. (setq n (1+ n))
  9234. (if (and (= n nsteps) (< maxwidth 10000))
  9235. (setq maxwidth (- total-width total)))
  9236. (if (< (length h) maxwidth)
  9237. (progn (setq total (+ total (length h) 1)) h)
  9238. (setq h (substring h 0 (- maxwidth 2))
  9239. total (+ total maxwidth 1))
  9240. (if (string-match "[ \t]+\\'" h)
  9241. (setq h (substring h 0 (match-beginning 0))))
  9242. (setq h (concat h "..")))
  9243. (org-add-props h nil 'face
  9244. (nth (% (1- n) org-n-level-faces)
  9245. org-level-faces))
  9246. h)
  9247. path "/")))))
  9248. (defun org-display-outline-path (&optional file current)
  9249. "Display the current outline path in the echo area."
  9250. (interactive "P")
  9251. (let* ((bfn (buffer-file-name (buffer-base-buffer)))
  9252. (case-fold-search nil)
  9253. (path (and (org-mode-p) (org-get-outline-path))))
  9254. (if current (setq path (append path
  9255. (save-excursion
  9256. (org-back-to-heading t)
  9257. (if (looking-at org-complex-heading-regexp)
  9258. (list (match-string 4)))))))
  9259. (message "%s"
  9260. (org-format-outline-path
  9261. path
  9262. (1- (frame-width))
  9263. (and file bfn (concat (file-name-nondirectory bfn) "/"))))))
  9264. (defvar org-refile-history nil
  9265. "History for refiling operations.")
  9266. (defvar org-after-refile-insert-hook nil
  9267. "Hook run after `org-refile' has inserted its stuff at the new location.
  9268. Note that this is still *before* the stuff will be removed from
  9269. the *old* location.")
  9270. (defvar org-capture-last-stored-marker)
  9271. (defun org-refile (&optional goto default-buffer rfloc)
  9272. "Move the entry at point to another heading.
  9273. The list of target headings is compiled using the information in
  9274. `org-refile-targets', which see. This list is created before each use
  9275. and will therefore always be up-to-date.
  9276. At the target location, the entry is filed as a subitem of the target heading.
  9277. Depending on `org-reverse-note-order', the new subitem will either be the
  9278. first or the last subitem.
  9279. If there is an active region, all entries in that region will be moved.
  9280. However, the region must fulfill the requirement that the first heading
  9281. is the first one sets the top-level of the moved text - at most siblings
  9282. below it are allowed.
  9283. With prefix arg GOTO, the command will only visit the target location,
  9284. not actually move anything.
  9285. With a double prefix arg \\[universal-argument] \\[universal-argument], \
  9286. go to the location where the last refiling
  9287. operation has put the subtree.
  9288. With a prefix argument of `2', refile to the running clock.
  9289. RFLOC can be a refile location obtained in a different way.
  9290. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  9291. If you are using target caching (see `org-refile-use-cache'),
  9292. You have to clear the target cache in order to find new targets.
  9293. This can be done with a 0 prefix: `C-0 C-c C-w'"
  9294. (interactive "P")
  9295. (if (member goto '(0 (64)))
  9296. (org-refile-cache-clear)
  9297. (let* ((cbuf (current-buffer))
  9298. (regionp (org-region-active-p))
  9299. (region-start (and regionp (region-beginning)))
  9300. (region-end (and regionp (region-end)))
  9301. (region-length (and regionp (- region-end region-start)))
  9302. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  9303. pos it nbuf file re level reversed)
  9304. (setq last-command nil)
  9305. (when regionp
  9306. (goto-char region-start)
  9307. (or (bolp) (goto-char (point-at-bol)))
  9308. (setq region-start (point))
  9309. (unless (org-kill-is-subtree-p
  9310. (buffer-substring region-start region-end))
  9311. (error "The region is not a (sequence of) subtree(s)")))
  9312. (if (equal goto '(16))
  9313. (org-refile-goto-last-stored)
  9314. (when (or
  9315. (and (equal goto 2)
  9316. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  9317. (prog1
  9318. (setq it (list (or org-clock-heading "running clock")
  9319. (buffer-file-name
  9320. (marker-buffer org-clock-hd-marker))
  9321. ""
  9322. (marker-position org-clock-hd-marker)))
  9323. (setq goto nil)))
  9324. (setq it (or rfloc
  9325. (save-excursion
  9326. (org-refile-get-location
  9327. (if goto "Goto" "Refile to") default-buffer
  9328. org-refile-allow-creating-parent-nodes)))))
  9329. (setq file (nth 1 it)
  9330. re (nth 2 it)
  9331. pos (nth 3 it))
  9332. (if (and (not goto)
  9333. pos
  9334. (equal (buffer-file-name) file)
  9335. (if regionp
  9336. (and (>= pos region-start)
  9337. (<= pos region-end))
  9338. (and (>= pos (point))
  9339. (< pos (save-excursion
  9340. (org-end-of-subtree t t))))))
  9341. (error "Cannot refile to position inside the tree or region"))
  9342. (setq nbuf (or (find-buffer-visiting file)
  9343. (find-file-noselect file)))
  9344. (if goto
  9345. (progn
  9346. (switch-to-buffer nbuf)
  9347. (goto-char pos)
  9348. (org-show-context 'org-goto))
  9349. (if regionp
  9350. (progn
  9351. (org-kill-new (buffer-substring region-start region-end))
  9352. (org-save-markers-in-region region-start region-end))
  9353. (org-copy-subtree 1 nil t))
  9354. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  9355. (find-file-noselect file)))
  9356. (setq reversed (org-notes-order-reversed-p))
  9357. (save-excursion
  9358. (save-restriction
  9359. (widen)
  9360. (if pos
  9361. (progn
  9362. (goto-char pos)
  9363. (looking-at outline-regexp)
  9364. (setq level (org-get-valid-level (funcall outline-level) 1))
  9365. (goto-char
  9366. (if reversed
  9367. (or (outline-next-heading) (point-max))
  9368. (or (save-excursion (org-get-next-sibling))
  9369. (org-end-of-subtree t t)
  9370. (point-max)))))
  9371. (setq level 1)
  9372. (if (not reversed)
  9373. (goto-char (point-max))
  9374. (goto-char (point-min))
  9375. (or (outline-next-heading) (goto-char (point-max)))))
  9376. (if (not (bolp)) (newline))
  9377. (org-paste-subtree level)
  9378. (when org-log-refile
  9379. (org-add-log-setup 'refile nil nil 'findpos
  9380. org-log-refile)
  9381. (unless (eq org-log-refile 'note)
  9382. (save-excursion (org-add-log-note))))
  9383. (and org-auto-align-tags (org-set-tags nil t))
  9384. (bookmark-set "org-refile-last-stored")
  9385. ;; If we are refiling for capture, make sure that the
  9386. ;; last-capture pointers point here
  9387. (when (org-bound-and-true-p org-refile-for-capture)
  9388. (bookmark-set "org-capture-last-stored-marker")
  9389. (move-marker org-capture-last-stored-marker (point)))
  9390. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9391. (run-hooks 'org-after-refile-insert-hook))))
  9392. (if regionp
  9393. (delete-region (point) (+ (point) region-length))
  9394. (org-cut-subtree))
  9395. (when (featurep 'org-inlinetask)
  9396. (org-inlinetask-remove-END-maybe))
  9397. (setq org-markers-to-move nil)
  9398. (message "Refiled to \"%s\" in file %s" (car it) file)))))))
  9399. (defun org-refile-goto-last-stored ()
  9400. "Go to the location where the last refile was stored."
  9401. (interactive)
  9402. (bookmark-jump "org-refile-last-stored")
  9403. (message "This is the location of the last refile"))
  9404. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  9405. "Prompt the user for a refile location, using PROMPT.
  9406. PROMPT should not be suffixed with a colon and a space, because
  9407. this function appends the default value from
  9408. `org-refile-history' automatically, if that is not empty."
  9409. (let ((org-refile-targets org-refile-targets)
  9410. (org-refile-use-outline-path org-refile-use-outline-path))
  9411. (setq org-refile-target-table (org-refile-get-targets default-buffer)))
  9412. (unless org-refile-target-table
  9413. (error "No refile targets"))
  9414. (let* ((prompt (concat prompt
  9415. (and (car org-refile-history)
  9416. (concat " (default " (car org-refile-history) ")"))
  9417. ": "))
  9418. (cbuf (current-buffer))
  9419. (partial-completion-mode nil)
  9420. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  9421. (cfunc (if (and org-refile-use-outline-path
  9422. org-outline-path-complete-in-steps)
  9423. 'org-olpath-completing-read
  9424. 'org-icompleting-read))
  9425. (extra (if org-refile-use-outline-path "/" ""))
  9426. (filename (and cfn (expand-file-name cfn)))
  9427. (tbl (mapcar
  9428. (lambda (x)
  9429. (if (and (not (member org-refile-use-outline-path
  9430. '(file full-file-path)))
  9431. (not (equal filename (nth 1 x))))
  9432. (cons (concat (car x) extra " ("
  9433. (file-name-nondirectory (nth 1 x)) ")")
  9434. (cdr x))
  9435. (cons (concat (car x) extra) (cdr x))))
  9436. org-refile-target-table))
  9437. (completion-ignore-case t)
  9438. pa answ parent-target child parent old-hist)
  9439. (setq old-hist org-refile-history)
  9440. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  9441. nil 'org-refile-history (car org-refile-history)))
  9442. (setq pa (or (assoc answ tbl) (assoc (concat answ "/") tbl)))
  9443. (org-refile-check-position pa)
  9444. (if pa
  9445. (progn
  9446. (when (or (not org-refile-history)
  9447. (not (eq old-hist org-refile-history))
  9448. (not (equal (car pa) (car org-refile-history))))
  9449. (setq org-refile-history
  9450. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  9451. org-refile-history
  9452. (cdr org-refile-history))))
  9453. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  9454. (pop org-refile-history)))
  9455. pa)
  9456. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  9457. (progn
  9458. (setq parent (match-string 1 answ)
  9459. child (match-string 2 answ))
  9460. (setq parent-target (or (assoc parent tbl)
  9461. (assoc (concat parent "/") tbl)))
  9462. (when (and parent-target
  9463. (or (eq new-nodes t)
  9464. (and (eq new-nodes 'confirm)
  9465. (y-or-n-p (format "Create new node \"%s\"? "
  9466. child)))))
  9467. (org-refile-new-child parent-target child)))
  9468. (error "Invalid target location")))))
  9469. (defun org-refile-check-position (refile-pointer)
  9470. "Check if the refile pointer matches the readline to which it points."
  9471. (let* ((file (nth 1 refile-pointer))
  9472. (re (nth 2 refile-pointer))
  9473. (pos (nth 3 refile-pointer))
  9474. buffer)
  9475. (when (org-string-nw-p re)
  9476. (setq buffer (if (markerp pos)
  9477. (marker-buffer pos)
  9478. (or (find-buffer-visiting file)
  9479. (find-file-noselect file))))
  9480. (with-current-buffer buffer
  9481. (save-excursion
  9482. (save-restriction
  9483. (widen)
  9484. (goto-char pos)
  9485. (beginning-of-line 1)
  9486. (unless (org-looking-at-p re)
  9487. (error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling"))))))))
  9488. (defun org-refile-new-child (parent-target child)
  9489. "Use refile target PARENT-TARGET to add new CHILD below it."
  9490. (unless parent-target
  9491. (error "Cannot find parent for new node"))
  9492. (let ((file (nth 1 parent-target))
  9493. (pos (nth 3 parent-target))
  9494. level)
  9495. (with-current-buffer (or (find-buffer-visiting file)
  9496. (find-file-noselect file))
  9497. (save-excursion
  9498. (save-restriction
  9499. (widen)
  9500. (if pos
  9501. (goto-char pos)
  9502. (goto-char (point-max))
  9503. (if (not (bolp)) (newline)))
  9504. (when (looking-at outline-regexp)
  9505. (setq level (funcall outline-level))
  9506. (org-end-of-subtree t t))
  9507. (org-back-over-empty-lines)
  9508. (insert "\n" (make-string
  9509. (if pos (org-get-valid-level level 1) 1) ?*)
  9510. " " child "\n")
  9511. (beginning-of-line 0)
  9512. (list (concat (car parent-target) "/" child) file "" (point)))))))
  9513. (defun org-olpath-completing-read (prompt collection &rest args)
  9514. "Read an outline path like a file name."
  9515. (let ((thetable collection)
  9516. (org-completion-use-ido nil) ; does not work with ido.
  9517. (org-completion-use-iswitchb nil)) ; or iswitchb
  9518. (apply
  9519. 'org-icompleting-read prompt
  9520. (lambda (string predicate &optional flag)
  9521. (let (rtn r f (l (length string)))
  9522. (cond
  9523. ((eq flag nil)
  9524. ;; try completion
  9525. (try-completion string thetable))
  9526. ((eq flag t)
  9527. ;; all-completions
  9528. (setq rtn (all-completions string thetable predicate))
  9529. (mapcar
  9530. (lambda (x)
  9531. (setq r (substring x l))
  9532. (if (string-match " ([^)]*)$" x)
  9533. (setq f (match-string 0 x))
  9534. (setq f ""))
  9535. (if (string-match "/" r)
  9536. (concat string (substring r 0 (match-end 0)) f)
  9537. x))
  9538. rtn))
  9539. ((eq flag 'lambda)
  9540. ;; exact match?
  9541. (assoc string thetable)))
  9542. ))
  9543. args)))
  9544. ;;;; Dynamic blocks
  9545. (defun org-find-dblock (name)
  9546. "Find the first dynamic block with name NAME in the buffer.
  9547. If not found, stay at current position and return nil."
  9548. (let (pos)
  9549. (save-excursion
  9550. (goto-char (point-min))
  9551. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  9552. nil t)
  9553. (match-beginning 0))))
  9554. (if pos (goto-char pos))
  9555. pos))
  9556. (defconst org-dblock-start-re
  9557. "^[ \t]*#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  9558. "Matches the start line of a dynamic block, with parameters.")
  9559. (defconst org-dblock-end-re "^[ \t]*#\\+END\\([: \t\r\n]\\|$\\)"
  9560. "Matches the end of a dynamic block.")
  9561. (defun org-create-dblock (plist)
  9562. "Create a dynamic block section, with parameters taken from PLIST.
  9563. PLIST must contain a :name entry which is used as name of the block."
  9564. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  9565. (end-of-line 1)
  9566. (newline))
  9567. (let ((col (current-column))
  9568. (name (plist-get plist :name)))
  9569. (insert "#+BEGIN: " name)
  9570. (while plist
  9571. (if (eq (car plist) :name)
  9572. (setq plist (cddr plist))
  9573. (insert " " (prin1-to-string (pop plist)))))
  9574. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  9575. (beginning-of-line -2)))
  9576. (defun org-prepare-dblock ()
  9577. "Prepare dynamic block for refresh.
  9578. This empties the block, puts the cursor at the insert position and returns
  9579. the property list including an extra property :name with the block name."
  9580. (unless (looking-at org-dblock-start-re)
  9581. (error "Not at a dynamic block"))
  9582. (let* ((begdel (1+ (match-end 0)))
  9583. (name (org-no-properties (match-string 1)))
  9584. (params (append (list :name name)
  9585. (read (concat "(" (match-string 3) ")")))))
  9586. (save-excursion
  9587. (beginning-of-line 1)
  9588. (skip-chars-forward " \t")
  9589. (setq params (plist-put params :indentation-column (current-column))))
  9590. (unless (re-search-forward org-dblock-end-re nil t)
  9591. (error "Dynamic block not terminated"))
  9592. (setq params
  9593. (append params
  9594. (list :content (buffer-substring
  9595. begdel (match-beginning 0)))))
  9596. (delete-region begdel (match-beginning 0))
  9597. (goto-char begdel)
  9598. (open-line 1)
  9599. params))
  9600. (defun org-map-dblocks (&optional command)
  9601. "Apply COMMAND to all dynamic blocks in the current buffer.
  9602. If COMMAND is not given, use `org-update-dblock'."
  9603. (let ((cmd (or command 'org-update-dblock)))
  9604. (save-excursion
  9605. (goto-char (point-min))
  9606. (while (re-search-forward org-dblock-start-re nil t)
  9607. (goto-char (match-beginning 0))
  9608. (save-excursion
  9609. (condition-case nil
  9610. (funcall cmd)
  9611. (error (message "Error during update of dynamic block"))))
  9612. (unless (re-search-forward org-dblock-end-re nil t)
  9613. (error "Dynamic block not terminated"))))))
  9614. (defun org-dblock-update (&optional arg)
  9615. "User command for updating dynamic blocks.
  9616. Update the dynamic block at point. With prefix ARG, update all dynamic
  9617. blocks in the buffer."
  9618. (interactive "P")
  9619. (if arg
  9620. (org-update-all-dblocks)
  9621. (or (looking-at org-dblock-start-re)
  9622. (org-beginning-of-dblock))
  9623. (org-update-dblock)))
  9624. (defun org-update-dblock ()
  9625. "Update the dynamic block at point.
  9626. This means to empty the block, parse for parameters and then call
  9627. the correct writing function."
  9628. (interactive)
  9629. (save-window-excursion
  9630. (let* ((pos (point))
  9631. (line (org-current-line))
  9632. (params (org-prepare-dblock))
  9633. (name (plist-get params :name))
  9634. (indent (plist-get params :indentation-column))
  9635. (cmd (intern (concat "org-dblock-write:" name))))
  9636. (message "Updating dynamic block `%s' at line %d..." name line)
  9637. (funcall cmd params)
  9638. (message "Updating dynamic block `%s' at line %d...done" name line)
  9639. (goto-char pos)
  9640. (when (and indent (> indent 0))
  9641. (setq indent (make-string indent ?\ ))
  9642. (save-excursion
  9643. (org-beginning-of-dblock)
  9644. (forward-line 1)
  9645. (while (not (looking-at org-dblock-end-re))
  9646. (insert indent)
  9647. (beginning-of-line 2))
  9648. (when (looking-at org-dblock-end-re)
  9649. (and (looking-at "[ \t]+")
  9650. (replace-match ""))
  9651. (insert indent)))))))
  9652. (defun org-beginning-of-dblock ()
  9653. "Find the beginning of the dynamic block at point.
  9654. Error if there is no such block at point."
  9655. (let ((pos (point))
  9656. beg)
  9657. (end-of-line 1)
  9658. (if (and (re-search-backward org-dblock-start-re nil t)
  9659. (setq beg (match-beginning 0))
  9660. (re-search-forward org-dblock-end-re nil t)
  9661. (> (match-end 0) pos))
  9662. (goto-char beg)
  9663. (goto-char pos)
  9664. (error "Not in a dynamic block"))))
  9665. (defun org-update-all-dblocks ()
  9666. "Update all dynamic blocks in the buffer.
  9667. This function can be used in a hook."
  9668. (interactive)
  9669. (when (org-mode-p)
  9670. (org-map-dblocks 'org-update-dblock)))
  9671. ;;;; Completion
  9672. (defconst org-additional-option-like-keywords
  9673. '("BEGIN_HTML" "END_HTML" "HTML:" "ATTR_HTML:"
  9674. "BEGIN_DocBook" "END_DocBook" "DocBook:" "ATTR_DocBook:"
  9675. "BEGIN_LaTeX" "END_LaTeX" "LaTeX:" "LATEX_HEADER:"
  9676. "LATEX_CLASS:" "LATEX_CLASS_OPTIONS:" "ATTR_LaTeX:"
  9677. "BEGIN:" "END:"
  9678. "ORGTBL" "TBLFM:" "TBLNAME:"
  9679. "BEGIN_EXAMPLE" "END_EXAMPLE"
  9680. "BEGIN_QUOTE" "END_QUOTE"
  9681. "BEGIN_VERSE" "END_VERSE"
  9682. "BEGIN_CENTER" "END_CENTER"
  9683. "BEGIN_SRC" "END_SRC"
  9684. "BEGIN_RESULT" "END_RESULT"
  9685. "SOURCE:" "SRCNAME:" "FUNCTION:"
  9686. "RESULTS:"
  9687. "HEADER:" "HEADERS:"
  9688. "BABEL:"
  9689. "CATEGORY:" "COLUMNS:" "PROPERTY:"
  9690. "CAPTION:" "LABEL:"
  9691. "SETUPFILE:"
  9692. "INCLUDE:"
  9693. "BIND:"
  9694. "MACRO:"))
  9695. (defcustom org-structure-template-alist
  9696. '(
  9697. ("s" "#+begin_src ?\n\n#+end_src"
  9698. "<src lang=\"?\">\n\n</src>")
  9699. ("e" "#+begin_example\n?\n#+end_example"
  9700. "<example>\n?\n</example>")
  9701. ("q" "#+begin_quote\n?\n#+end_quote"
  9702. "<quote>\n?\n</quote>")
  9703. ("v" "#+begin_verse\n?\n#+end_verse"
  9704. "<verse>\n?\n/verse>")
  9705. ("c" "#+begin_center\n?\n#+end_center"
  9706. "<center>\n?\n/center>")
  9707. ("l" "#+begin_latex\n?\n#+end_latex"
  9708. "<literal style=\"latex\">\n?\n</literal>")
  9709. ("L" "#+latex: "
  9710. "<literal style=\"latex\">?</literal>")
  9711. ("h" "#+begin_html\n?\n#+end_html"
  9712. "<literal style=\"html\">\n?\n</literal>")
  9713. ("H" "#+html: "
  9714. "<literal style=\"html\">?</literal>")
  9715. ("a" "#+begin_ascii\n?\n#+end_ascii")
  9716. ("A" "#+ascii: ")
  9717. ("i" "#+include %file ?"
  9718. "<include file=%file markup=\"?\">")
  9719. )
  9720. "Structure completion elements.
  9721. This is a list of abbreviation keys and values. The value gets inserted
  9722. if you type `<' followed by the key and then press the completion key,
  9723. usually `M-TAB'. %file will be replaced by a file name after prompting
  9724. for the file using completion.
  9725. There are two templates for each key, the first uses the original Org syntax,
  9726. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  9727. the default when the /org-mtags.el/ module has been loaded. See also the
  9728. variable `org-mtags-prefer-muse-templates'.
  9729. This is an experimental feature, it is undecided if it is going to stay in."
  9730. :group 'org-completion
  9731. :type '(repeat
  9732. (string :tag "Key")
  9733. (string :tag "Template")
  9734. (string :tag "Muse Template")))
  9735. (defun org-try-structure-completion ()
  9736. "Try to complete a structure template before point.
  9737. This looks for strings like \"<e\" on an otherwise empty line and
  9738. expands them."
  9739. (let ((l (buffer-substring (point-at-bol) (point)))
  9740. a)
  9741. (when (and (looking-at "[ \t]*$")
  9742. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  9743. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  9744. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  9745. (match-beginning 1)) a)
  9746. t)))
  9747. (defun org-complete-expand-structure-template (start cell)
  9748. "Expand a structure template."
  9749. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  9750. (rpl (nth (if musep 2 1) cell))
  9751. (ind ""))
  9752. (delete-region start (point))
  9753. (when (string-match "\\`#\\+" rpl)
  9754. (cond
  9755. ((bolp))
  9756. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  9757. (setq ind (buffer-substring (point-at-bol) (point))))
  9758. (t (newline))))
  9759. (setq start (point))
  9760. (if (string-match "%file" rpl)
  9761. (setq rpl (replace-match
  9762. (concat
  9763. "\""
  9764. (save-match-data
  9765. (abbreviate-file-name (read-file-name "Include file: ")))
  9766. "\"")
  9767. t t rpl)))
  9768. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  9769. (concat "\n" ind)))
  9770. (insert rpl)
  9771. (if (re-search-backward "\\?" start t) (delete-char 1))))
  9772. ;;;; TODO, DEADLINE, Comments
  9773. (defun org-toggle-comment ()
  9774. "Change the COMMENT state of an entry."
  9775. (interactive)
  9776. (save-excursion
  9777. (org-back-to-heading)
  9778. (let (case-fold-search)
  9779. (if (looking-at (concat outline-regexp
  9780. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  9781. (replace-match "" t t nil 1)
  9782. (if (looking-at outline-regexp)
  9783. (progn
  9784. (goto-char (match-end 0))
  9785. (insert org-comment-string " ")))))))
  9786. (defvar org-last-todo-state-is-todo nil
  9787. "This is non-nil when the last TODO state change led to a TODO state.
  9788. If the last change removed the TODO tag or switched to DONE, then
  9789. this is nil.")
  9790. (defvar org-setting-tags nil) ; dynamically skipped
  9791. (defvar org-todo-setup-filter-hook nil
  9792. "Hook for functions that pre-filter todo specs.
  9793. Each function takes a todo spec and returns either nil or the spec
  9794. transformed into canonical form." )
  9795. (defvar org-todo-get-default-hook nil
  9796. "Hook for functions that get a default item for todo.
  9797. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  9798. nil or a string to be used for the todo mark." )
  9799. (defvar org-agenda-headline-snapshot-before-repeat)
  9800. (defun org-todo (&optional arg)
  9801. "Change the TODO state of an item.
  9802. The state of an item is given by a keyword at the start of the heading,
  9803. like
  9804. *** TODO Write paper
  9805. *** DONE Call mom
  9806. The different keywords are specified in the variable `org-todo-keywords'.
  9807. By default the available states are \"TODO\" and \"DONE\".
  9808. So for this example: when the item starts with TODO, it is changed to DONE.
  9809. When it starts with DONE, the DONE is removed. And when neither TODO nor
  9810. DONE are present, add TODO at the beginning of the heading.
  9811. With \\[universal-argument] prefix arg, use completion to determine the new \
  9812. state.
  9813. With numeric prefix arg, switch to that state.
  9814. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  9815. keywords (nextset).
  9816. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  9817. For calling through lisp, arg is also interpreted in the following way:
  9818. 'none -> empty state
  9819. \"\"(empty string) -> switch to empty state
  9820. 'done -> switch to DONE
  9821. 'nextset -> switch to the next set of keywords
  9822. 'previousset -> switch to the previous set of keywords
  9823. \"WAITING\" -> switch to the specified keyword, but only if it
  9824. really is a member of `org-todo-keywords'."
  9825. (interactive "P")
  9826. (if (equal arg '(16)) (setq arg 'nextset))
  9827. (let ((org-blocker-hook org-blocker-hook)
  9828. (case-fold-search nil))
  9829. (when (equal arg '(64))
  9830. (setq arg nil org-blocker-hook nil))
  9831. (when (and org-blocker-hook
  9832. (or org-inhibit-blocking
  9833. (org-entry-get nil "NOBLOCKING")))
  9834. (setq org-blocker-hook nil))
  9835. (save-excursion
  9836. (catch 'exit
  9837. (org-back-to-heading t)
  9838. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  9839. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|$\\)"))
  9840. (looking-at " *"))
  9841. (let* ((match-data (match-data))
  9842. (startpos (point-at-bol))
  9843. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  9844. (org-log-done org-log-done)
  9845. (org-log-repeat org-log-repeat)
  9846. (org-todo-log-states org-todo-log-states)
  9847. (this (match-string 1))
  9848. (hl-pos (match-beginning 0))
  9849. (head (org-get-todo-sequence-head this))
  9850. (ass (assoc head org-todo-kwd-alist))
  9851. (interpret (nth 1 ass))
  9852. (done-word (nth 3 ass))
  9853. (final-done-word (nth 4 ass))
  9854. (last-state (or this ""))
  9855. (completion-ignore-case t)
  9856. (member (member this org-todo-keywords-1))
  9857. (tail (cdr member))
  9858. (state (cond
  9859. ((and org-todo-key-trigger
  9860. (or (and (equal arg '(4))
  9861. (eq org-use-fast-todo-selection 'prefix))
  9862. (and (not arg) org-use-fast-todo-selection
  9863. (not (eq org-use-fast-todo-selection
  9864. 'prefix)))))
  9865. ;; Use fast selection
  9866. (org-fast-todo-selection))
  9867. ((and (equal arg '(4))
  9868. (or (not org-use-fast-todo-selection)
  9869. (not org-todo-key-trigger)))
  9870. ;; Read a state with completion
  9871. (org-icompleting-read
  9872. "State: " (mapcar (lambda(x) (list x))
  9873. org-todo-keywords-1)
  9874. nil t))
  9875. ((eq arg 'right)
  9876. (if this
  9877. (if tail (car tail) nil)
  9878. (car org-todo-keywords-1)))
  9879. ((eq arg 'left)
  9880. (if (equal member org-todo-keywords-1)
  9881. nil
  9882. (if this
  9883. (nth (- (length org-todo-keywords-1)
  9884. (length tail) 2)
  9885. org-todo-keywords-1)
  9886. (org-last org-todo-keywords-1))))
  9887. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  9888. (setq arg nil))) ; hack to fall back to cycling
  9889. (arg
  9890. ;; user or caller requests a specific state
  9891. (cond
  9892. ((equal arg "") nil)
  9893. ((eq arg 'none) nil)
  9894. ((eq arg 'done) (or done-word (car org-done-keywords)))
  9895. ((eq arg 'nextset)
  9896. (or (car (cdr (member head org-todo-heads)))
  9897. (car org-todo-heads)))
  9898. ((eq arg 'previousset)
  9899. (let ((org-todo-heads (reverse org-todo-heads)))
  9900. (or (car (cdr (member head org-todo-heads)))
  9901. (car org-todo-heads))))
  9902. ((car (member arg org-todo-keywords-1)))
  9903. ((stringp arg)
  9904. (error "State `%s' not valid in this file" arg))
  9905. ((nth (1- (prefix-numeric-value arg))
  9906. org-todo-keywords-1))))
  9907. ((null member) (or head (car org-todo-keywords-1)))
  9908. ((equal this final-done-word) nil) ;; -> make empty
  9909. ((null tail) nil) ;; -> first entry
  9910. ((memq interpret '(type priority))
  9911. (if (eq this-command last-command)
  9912. (car tail)
  9913. (if (> (length tail) 0)
  9914. (or done-word (car org-done-keywords))
  9915. nil)))
  9916. (t
  9917. (car tail))))
  9918. (state (or
  9919. (run-hook-with-args-until-success
  9920. 'org-todo-get-default-hook state last-state)
  9921. state))
  9922. (next (if state (concat " " state " ") " "))
  9923. (change-plist (list :type 'todo-state-change :from this :to state
  9924. :position startpos))
  9925. dolog now-done-p)
  9926. (when org-blocker-hook
  9927. (setq org-last-todo-state-is-todo
  9928. (not (member this org-done-keywords)))
  9929. (unless (save-excursion
  9930. (save-match-data
  9931. (org-with-wide-buffer
  9932. (run-hook-with-args-until-failure
  9933. 'org-blocker-hook change-plist))))
  9934. (if (interactive-p)
  9935. (error "TODO state change from %s to %s blocked" this state)
  9936. ;; fail silently
  9937. (message "TODO state change from %s to %s blocked" this state)
  9938. (throw 'exit nil))))
  9939. (store-match-data match-data)
  9940. (replace-match next t t)
  9941. (unless (pos-visible-in-window-p hl-pos)
  9942. (message "TODO state changed to %s" (org-trim next)))
  9943. (unless head
  9944. (setq head (org-get-todo-sequence-head state)
  9945. ass (assoc head org-todo-kwd-alist)
  9946. interpret (nth 1 ass)
  9947. done-word (nth 3 ass)
  9948. final-done-word (nth 4 ass)))
  9949. (when (memq arg '(nextset previousset))
  9950. (message "Keyword-Set %d/%d: %s"
  9951. (- (length org-todo-sets) -1
  9952. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  9953. (length org-todo-sets)
  9954. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  9955. (setq org-last-todo-state-is-todo
  9956. (not (member state org-done-keywords)))
  9957. (setq now-done-p (and (member state org-done-keywords)
  9958. (not (member this org-done-keywords))))
  9959. (and logging (org-local-logging logging))
  9960. (when (and (or org-todo-log-states org-log-done)
  9961. (not (eq org-inhibit-logging t))
  9962. (not (memq arg '(nextset previousset))))
  9963. ;; we need to look at recording a time and note
  9964. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  9965. (nth 2 (assoc this org-todo-log-states))))
  9966. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  9967. (setq dolog 'time))
  9968. (when (and state
  9969. (member state org-not-done-keywords)
  9970. (not (member this org-not-done-keywords)))
  9971. ;; This is now a todo state and was not one before
  9972. ;; If there was a CLOSED time stamp, get rid of it.
  9973. (org-add-planning-info nil nil 'closed))
  9974. (when (and now-done-p org-log-done)
  9975. ;; It is now done, and it was not done before
  9976. (org-add-planning-info 'closed (org-current-time))
  9977. (if (and (not dolog) (eq 'note org-log-done))
  9978. (org-add-log-setup 'done state this 'findpos 'note)))
  9979. (when (and state dolog)
  9980. ;; This is a non-nil state, and we need to log it
  9981. (org-add-log-setup 'state state this 'findpos dolog)))
  9982. ;; Fixup tag positioning
  9983. (org-todo-trigger-tag-changes state)
  9984. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  9985. (when org-provide-todo-statistics
  9986. (org-update-parent-todo-statistics))
  9987. (run-hooks 'org-after-todo-state-change-hook)
  9988. (if (and arg (not (member state org-done-keywords)))
  9989. (setq head (org-get-todo-sequence-head state)))
  9990. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  9991. ;; Do we need to trigger a repeat?
  9992. (when now-done-p
  9993. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  9994. ;; This is for the agenda, take a snapshot of the headline.
  9995. (save-match-data
  9996. (setq org-agenda-headline-snapshot-before-repeat
  9997. (org-get-heading))))
  9998. (org-auto-repeat-maybe state))
  9999. ;; Fixup cursor location if close to the keyword
  10000. (if (and (outline-on-heading-p)
  10001. (not (bolp))
  10002. (save-excursion (beginning-of-line 1)
  10003. (looking-at org-todo-line-regexp))
  10004. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  10005. (progn
  10006. (goto-char (or (match-end 2) (match-end 1)))
  10007. (and (looking-at " ") (just-one-space))))
  10008. (when org-trigger-hook
  10009. (save-excursion
  10010. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  10011. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  10012. "Block turning an entry into a TODO, using the hierarchy.
  10013. This checks whether the current task should be blocked from state
  10014. changes. Such blocking occurs when:
  10015. 1. The task has children which are not all in a completed state.
  10016. 2. A task has a parent with the property :ORDERED:, and there
  10017. are siblings prior to the current task with incomplete
  10018. status.
  10019. 3. The parent of the task is blocked because it has siblings that should
  10020. be done first, or is child of a block grandparent TODO entry."
  10021. (if (not org-enforce-todo-dependencies)
  10022. t ; if locally turned off don't block
  10023. (catch 'dont-block
  10024. ;; If this is not a todo state change, or if this entry is already DONE,
  10025. ;; do not block
  10026. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10027. (member (plist-get change-plist :from)
  10028. (cons 'done org-done-keywords))
  10029. (member (plist-get change-plist :to)
  10030. (cons 'todo org-not-done-keywords))
  10031. (not (plist-get change-plist :to)))
  10032. (throw 'dont-block t))
  10033. ;; If this task has children, and any are undone, it's blocked
  10034. (save-excursion
  10035. (org-back-to-heading t)
  10036. (let ((this-level (funcall outline-level)))
  10037. (outline-next-heading)
  10038. (let ((child-level (funcall outline-level)))
  10039. (while (and (not (eobp))
  10040. (> child-level this-level))
  10041. ;; this todo has children, check whether they are all
  10042. ;; completed
  10043. (if (and (not (org-entry-is-done-p))
  10044. (org-entry-is-todo-p))
  10045. (throw 'dont-block nil))
  10046. (outline-next-heading)
  10047. (setq child-level (funcall outline-level))))))
  10048. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  10049. ;; any previous siblings are undone, it's blocked
  10050. (save-excursion
  10051. (org-back-to-heading t)
  10052. (let* ((pos (point))
  10053. (parent-pos (and (org-up-heading-safe) (point))))
  10054. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10055. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10056. (forward-line 1)
  10057. (re-search-forward org-not-done-heading-regexp pos t))
  10058. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  10059. ;; Search further up the hierarchy, to see if an anchestor is blocked
  10060. (while t
  10061. (goto-char parent-pos)
  10062. (if (not (looking-at org-not-done-heading-regexp))
  10063. (throw 'dont-block t)) ; do not block, parent is not a TODO
  10064. (setq pos (point))
  10065. (setq parent-pos (and (org-up-heading-safe) (point)))
  10066. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  10067. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  10068. (forward-line 1)
  10069. (re-search-forward org-not-done-heading-regexp pos t))
  10070. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  10071. (defcustom org-track-ordered-property-with-tag nil
  10072. "Should the ORDERED property also be shown as a tag?
  10073. The ORDERED property decides if an entry should require subtasks to be
  10074. completed in sequence. Since a property is not very visible, setting
  10075. this option means that toggling the ORDERED property with the command
  10076. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  10077. not relevant for the behavior, but it makes things more visible.
  10078. Note that toggling the tag with tags commands will not change the property
  10079. and therefore not influence behavior!
  10080. This can be t, meaning the tag ORDERED should be used, It can also be a
  10081. string to select a different tag for this task."
  10082. :group 'org-todo
  10083. :type '(choice
  10084. (const :tag "No tracking" nil)
  10085. (const :tag "Track with ORDERED tag" t)
  10086. (string :tag "Use other tag")))
  10087. (defun org-toggle-ordered-property ()
  10088. "Toggle the ORDERED property of the current entry.
  10089. For better visibility, you can track the value of this property with a tag.
  10090. See variable `org-track-ordered-property-with-tag'."
  10091. (interactive)
  10092. (let* ((t1 org-track-ordered-property-with-tag)
  10093. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  10094. (save-excursion
  10095. (org-back-to-heading)
  10096. (if (org-entry-get nil "ORDERED")
  10097. (progn
  10098. (org-delete-property "ORDERED")
  10099. (and tag (org-toggle-tag tag 'off))
  10100. (message "Subtasks can be completed in arbitrary order"))
  10101. (org-entry-put nil "ORDERED" "t")
  10102. (and tag (org-toggle-tag tag 'on))
  10103. (message "Subtasks must be completed in sequence")))))
  10104. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  10105. (defun org-block-todo-from-checkboxes (change-plist)
  10106. "Block turning an entry into a TODO, using checkboxes.
  10107. This checks whether the current task should be blocked from state
  10108. changes because there are unchecked boxes in this entry."
  10109. (if (not org-enforce-todo-checkbox-dependencies)
  10110. t ; if locally turned off don't block
  10111. (catch 'dont-block
  10112. ;; If this is not a todo state change, or if this entry is already DONE,
  10113. ;; do not block
  10114. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  10115. (member (plist-get change-plist :from)
  10116. (cons 'done org-done-keywords))
  10117. (member (plist-get change-plist :to)
  10118. (cons 'todo org-not-done-keywords))
  10119. (not (plist-get change-plist :to)))
  10120. (throw 'dont-block t))
  10121. ;; If this task has checkboxes that are not checked, it's blocked
  10122. (save-excursion
  10123. (org-back-to-heading t)
  10124. (let ((beg (point)) end)
  10125. (outline-next-heading)
  10126. (setq end (point))
  10127. (goto-char beg)
  10128. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  10129. end t)
  10130. (progn
  10131. (if (boundp 'org-blocked-by-checkboxes)
  10132. (setq org-blocked-by-checkboxes t))
  10133. (throw 'dont-block nil)))))
  10134. t))) ; do not block
  10135. (defun org-entry-blocked-p ()
  10136. "Is the current entry blocked?"
  10137. (if (org-entry-get nil "NOBLOCKING")
  10138. nil ;; Never block this entry
  10139. (not
  10140. (run-hook-with-args-until-failure
  10141. 'org-blocker-hook
  10142. (list :type 'todo-state-change
  10143. :position (point)
  10144. :from 'todo
  10145. :to 'done)))))
  10146. (defun org-update-statistics-cookies (all)
  10147. "Update the statistics cookie, either from TODO or from checkboxes.
  10148. This should be called with the cursor in a line with a statistics cookie."
  10149. (interactive "P")
  10150. (if all
  10151. (progn
  10152. (org-update-checkbox-count 'all)
  10153. (org-map-entries 'org-update-parent-todo-statistics))
  10154. (if (not (org-on-heading-p))
  10155. (org-update-checkbox-count)
  10156. (let ((pos (move-marker (make-marker) (point)))
  10157. end l1 l2)
  10158. (ignore-errors (org-back-to-heading t))
  10159. (if (not (org-on-heading-p))
  10160. (org-update-checkbox-count)
  10161. (setq l1 (org-outline-level))
  10162. (setq end (save-excursion
  10163. (outline-next-heading)
  10164. (if (org-on-heading-p) (setq l2 (org-outline-level)))
  10165. (point)))
  10166. (if (and (save-excursion
  10167. (re-search-forward
  10168. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  10169. (not (save-excursion (re-search-forward
  10170. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  10171. (org-update-checkbox-count)
  10172. (if (and l2 (> l2 l1))
  10173. (progn
  10174. (goto-char end)
  10175. (org-update-parent-todo-statistics))
  10176. (goto-char pos)
  10177. (beginning-of-line 1)
  10178. (while (re-search-forward
  10179. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  10180. (point-at-eol) t)
  10181. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  10182. (goto-char pos)
  10183. (move-marker pos nil)))))
  10184. (defvar org-entry-property-inherited-from) ;; defined below
  10185. (defun org-update-parent-todo-statistics ()
  10186. "Update any statistics cookie in the parent of the current headline.
  10187. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  10188. the entire subtree and this will travel up the hierarchy and update
  10189. statistics everywhere."
  10190. (interactive)
  10191. (let* ((lim 0) prop
  10192. (recursive (or (not org-hierarchical-todo-statistics)
  10193. (string-match
  10194. "\\<recursive\\>"
  10195. (or (setq prop (org-entry-get
  10196. nil "COOKIE_DATA" 'inherit)) ""))))
  10197. (lim (or (and prop (marker-position
  10198. org-entry-property-inherited-from))
  10199. lim))
  10200. (first t)
  10201. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  10202. level ltoggle l1 new ndel
  10203. (cnt-all 0) (cnt-done 0) is-percent kwd
  10204. checkbox-beg ov ovs ove cookie-present)
  10205. (catch 'exit
  10206. (save-excursion
  10207. (beginning-of-line 1)
  10208. (if (org-at-heading-p)
  10209. (setq ltoggle (funcall outline-level))
  10210. (error "This should not happen"))
  10211. (while (and (setq level (org-up-heading-safe))
  10212. (or recursive first)
  10213. (>= (point) lim))
  10214. (setq first nil cookie-present nil)
  10215. (unless (and level
  10216. (not (string-match
  10217. "\\<checkbox\\>"
  10218. (downcase
  10219. (or (org-entry-get
  10220. nil "COOKIE_DATA")
  10221. "")))))
  10222. (throw 'exit nil))
  10223. (while (re-search-forward box-re (point-at-eol) t)
  10224. (setq cnt-all 0 cnt-done 0 cookie-present t)
  10225. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  10226. (save-match-data
  10227. (unless (outline-next-heading) (throw 'exit nil))
  10228. (while (and (looking-at org-complex-heading-regexp)
  10229. (> (setq l1 (length (match-string 1))) level))
  10230. (setq kwd (and (or recursive (= l1 ltoggle))
  10231. (match-string 2)))
  10232. (if (or (eq org-provide-todo-statistics 'all-headlines)
  10233. (and (listp org-provide-todo-statistics)
  10234. (or (member kwd org-provide-todo-statistics)
  10235. (member kwd org-done-keywords))))
  10236. (setq cnt-all (1+ cnt-all))
  10237. (if (eq org-provide-todo-statistics t)
  10238. (and kwd (setq cnt-all (1+ cnt-all)))))
  10239. (and (member kwd org-done-keywords)
  10240. (setq cnt-done (1+ cnt-done)))
  10241. (outline-next-heading)))
  10242. (setq new
  10243. (if is-percent
  10244. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  10245. (format "[%d/%d]" cnt-done cnt-all))
  10246. ndel (- (match-end 0) checkbox-beg))
  10247. ;; handle overlays when updating cookie from column view
  10248. (when (setq ov (car (overlays-at checkbox-beg)))
  10249. (setq ovs (overlay-start ov) ove (overlay-end ov))
  10250. (delete-overlay ov))
  10251. (goto-char checkbox-beg)
  10252. (insert new)
  10253. (delete-region (point) (+ (point) ndel))
  10254. (when ov (move-overlay ov ovs ove)))
  10255. (when cookie-present
  10256. (run-hook-with-args 'org-after-todo-statistics-hook
  10257. cnt-done (- cnt-all cnt-done))))))
  10258. (run-hooks 'org-todo-statistics-hook)))
  10259. (defvar org-after-todo-statistics-hook nil
  10260. "Hook that is called after a TODO statistics cookie has been updated.
  10261. Each function is called with two arguments: the number of not-done entries
  10262. and the number of done entries.
  10263. For example, the following function, when added to this hook, will switch
  10264. an entry to DONE when all children are done, and back to TODO when new
  10265. entries are set to a TODO status. Note that this hook is only called
  10266. when there is a statistics cookie in the headline!
  10267. (defun org-summary-todo (n-done n-not-done)
  10268. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  10269. (let (org-log-done org-log-states) ; turn off logging
  10270. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  10271. ")
  10272. (defvar org-todo-statistics-hook nil
  10273. "Hook that is run whenever Org thinks TODO statistics should be updated.
  10274. This hook runs even if there is no statistics cookie present, in which case
  10275. `org-after-todo-statistics-hook' would not run.")
  10276. (defun org-todo-trigger-tag-changes (state)
  10277. "Apply the changes defined in `org-todo-state-tags-triggers'."
  10278. (let ((l org-todo-state-tags-triggers)
  10279. changes)
  10280. (when (or (not state) (equal state ""))
  10281. (setq changes (append changes (cdr (assoc "" l)))))
  10282. (when (and (stringp state) (> (length state) 0))
  10283. (setq changes (append changes (cdr (assoc state l)))))
  10284. (when (member state org-not-done-keywords)
  10285. (setq changes (append changes (cdr (assoc 'todo l)))))
  10286. (when (member state org-done-keywords)
  10287. (setq changes (append changes (cdr (assoc 'done l)))))
  10288. (dolist (c changes)
  10289. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  10290. (defun org-local-logging (value)
  10291. "Get logging settings from a property VALUE."
  10292. (let* (words w a)
  10293. ;; directly set the variables, they are already local.
  10294. (setq org-log-done nil
  10295. org-log-repeat nil
  10296. org-todo-log-states nil)
  10297. (setq words (org-split-string value))
  10298. (while (setq w (pop words))
  10299. (cond
  10300. ((setq a (assoc w org-startup-options))
  10301. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  10302. (set (nth 1 a) (nth 2 a))))
  10303. ((setq a (org-extract-log-state-settings w))
  10304. (and (member (car a) org-todo-keywords-1)
  10305. (push a org-todo-log-states)))))))
  10306. (defun org-get-todo-sequence-head (kwd)
  10307. "Return the head of the TODO sequence to which KWD belongs.
  10308. If KWD is not set, check if there is a text property remembering the
  10309. right sequence."
  10310. (let (p)
  10311. (cond
  10312. ((not kwd)
  10313. (or (get-text-property (point-at-bol) 'org-todo-head)
  10314. (progn
  10315. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  10316. nil (point-at-eol)))
  10317. (get-text-property p 'org-todo-head))))
  10318. ((not (member kwd org-todo-keywords-1))
  10319. (car org-todo-keywords-1))
  10320. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  10321. (defun org-fast-todo-selection ()
  10322. "Fast TODO keyword selection with single keys.
  10323. Returns the new TODO keyword, or nil if no state change should occur."
  10324. (let* ((fulltable org-todo-key-alist)
  10325. (done-keywords org-done-keywords) ;; needed for the faces.
  10326. (maxlen (apply 'max (mapcar
  10327. (lambda (x)
  10328. (if (stringp (car x)) (string-width (car x)) 0))
  10329. fulltable)))
  10330. (expert nil)
  10331. (fwidth (+ maxlen 3 1 3))
  10332. (ncol (/ (- (window-width) 4) fwidth))
  10333. tg cnt e c tbl
  10334. groups ingroup)
  10335. (save-excursion
  10336. (save-window-excursion
  10337. (if expert
  10338. (set-buffer (get-buffer-create " *Org todo*"))
  10339. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  10340. (erase-buffer)
  10341. (org-set-local 'org-done-keywords done-keywords)
  10342. (setq tbl fulltable cnt 0)
  10343. (while (setq e (pop tbl))
  10344. (cond
  10345. ((equal e '(:startgroup))
  10346. (push '() groups) (setq ingroup t)
  10347. (when (not (= cnt 0))
  10348. (setq cnt 0)
  10349. (insert "\n"))
  10350. (insert "{ "))
  10351. ((equal e '(:endgroup))
  10352. (setq ingroup nil cnt 0)
  10353. (insert "}\n"))
  10354. ((equal e '(:newline))
  10355. (when (not (= cnt 0))
  10356. (setq cnt 0)
  10357. (insert "\n")
  10358. (setq e (car tbl))
  10359. (while (equal (car tbl) '(:newline))
  10360. (insert "\n")
  10361. (setq tbl (cdr tbl)))))
  10362. (t
  10363. (setq tg (car e) c (cdr e))
  10364. (if ingroup (push tg (car groups)))
  10365. (setq tg (org-add-props tg nil 'face
  10366. (org-get-todo-face tg)))
  10367. (if (and (= cnt 0) (not ingroup)) (insert " "))
  10368. (insert "[" c "] " tg (make-string
  10369. (- fwidth 4 (length tg)) ?\ ))
  10370. (when (= (setq cnt (1+ cnt)) ncol)
  10371. (insert "\n")
  10372. (if ingroup (insert " "))
  10373. (setq cnt 0)))))
  10374. (insert "\n")
  10375. (goto-char (point-min))
  10376. (if (not expert) (org-fit-window-to-buffer))
  10377. (message "[a-z..]:Set [SPC]:clear")
  10378. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  10379. (cond
  10380. ((or (= c ?\C-g)
  10381. (and (= c ?q) (not (rassoc c fulltable))))
  10382. (setq quit-flag t))
  10383. ((= c ?\ ) nil)
  10384. ((setq e (rassoc c fulltable) tg (car e))
  10385. tg)
  10386. (t (setq quit-flag t)))))))
  10387. (defun org-entry-is-todo-p ()
  10388. (member (org-get-todo-state) org-not-done-keywords))
  10389. (defun org-entry-is-done-p ()
  10390. (member (org-get-todo-state) org-done-keywords))
  10391. (defun org-get-todo-state ()
  10392. (save-excursion
  10393. (org-back-to-heading t)
  10394. (and (looking-at org-todo-line-regexp)
  10395. (match-end 2)
  10396. (match-string 2))))
  10397. (defun org-at-date-range-p (&optional inactive-ok)
  10398. "Is the cursor inside a date range?"
  10399. (interactive)
  10400. (save-excursion
  10401. (catch 'exit
  10402. (let ((pos (point)))
  10403. (skip-chars-backward "^[<\r\n")
  10404. (skip-chars-backward "<[")
  10405. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10406. (>= (match-end 0) pos)
  10407. (throw 'exit t))
  10408. (skip-chars-backward "^<[\r\n")
  10409. (skip-chars-backward "<[")
  10410. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  10411. (>= (match-end 0) pos)
  10412. (throw 'exit t)))
  10413. nil)))
  10414. (defun org-get-repeat (&optional tagline)
  10415. "Check if there is a deadline/schedule with repeater in this entry."
  10416. (save-match-data
  10417. (save-excursion
  10418. (org-back-to-heading t)
  10419. (and (re-search-forward (if tagline
  10420. (concat tagline "\\s-*" org-repeat-re)
  10421. org-repeat-re)
  10422. (org-entry-end-position) t)
  10423. (match-string-no-properties 1)))))
  10424. (defvar org-last-changed-timestamp)
  10425. (defvar org-last-inserted-timestamp)
  10426. (defvar org-log-post-message)
  10427. (defvar org-log-note-purpose)
  10428. (defvar org-log-note-how)
  10429. (defvar org-log-note-extra)
  10430. (defun org-auto-repeat-maybe (done-word)
  10431. "Check if the current headline contains a repeated deadline/schedule.
  10432. If yes, set TODO state back to what it was and change the base date
  10433. of repeating deadline/scheduled time stamps to new date.
  10434. This function is run automatically after each state change to a DONE state."
  10435. ;; last-state is dynamically scoped into this function
  10436. (let* ((repeat (org-get-repeat))
  10437. (aa (assoc last-state org-todo-kwd-alist))
  10438. (interpret (nth 1 aa))
  10439. (head (nth 2 aa))
  10440. (whata '(("d" . day) ("m" . month) ("y" . year)))
  10441. (msg "Entry repeats: ")
  10442. (org-log-done nil)
  10443. (org-todo-log-states nil)
  10444. re type n what ts time to-state)
  10445. (when repeat
  10446. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  10447. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  10448. org-todo-repeat-to-state))
  10449. (unless (and to-state (member to-state org-todo-keywords-1))
  10450. (setq to-state (if (eq interpret 'type) last-state head)))
  10451. (org-todo to-state)
  10452. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  10453. (org-entry-put nil "LAST_REPEAT" (format-time-string
  10454. (org-time-stamp-format t t))))
  10455. (when org-log-repeat
  10456. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  10457. (memq 'org-add-log-note post-command-hook))
  10458. ;; OK, we are already setup for some record
  10459. (if (eq org-log-repeat 'note)
  10460. ;; make sure we take a note, not only a time stamp
  10461. (setq org-log-note-how 'note))
  10462. ;; Set up for taking a record
  10463. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  10464. last-state
  10465. 'findpos org-log-repeat)))
  10466. (org-back-to-heading t)
  10467. (org-add-planning-info nil nil 'closed)
  10468. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  10469. org-deadline-time-regexp "\\)\\|\\("
  10470. org-ts-regexp "\\)"))
  10471. (while (re-search-forward
  10472. re (save-excursion (outline-next-heading) (point)) t)
  10473. (setq type (if (match-end 1) org-scheduled-string
  10474. (if (match-end 3) org-deadline-string "Plain:"))
  10475. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  10476. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  10477. (setq n (string-to-number (match-string 2 ts))
  10478. what (match-string 3 ts))
  10479. (if (equal what "w") (setq n (* n 7) what "d"))
  10480. ;; Preparation, see if we need to modify the start date for the change
  10481. (when (match-end 1)
  10482. (setq time (save-match-data (org-time-string-to-time ts)))
  10483. (cond
  10484. ((equal (match-string 1 ts) ".")
  10485. ;; Shift starting date to today
  10486. (org-timestamp-change
  10487. (- (time-to-days (current-time)) (time-to-days time))
  10488. 'day))
  10489. ((equal (match-string 1 ts) "+")
  10490. (let ((nshiftmax 10) (nshift 0))
  10491. (while (or (= nshift 0)
  10492. (<= (time-to-days time)
  10493. (time-to-days (current-time))))
  10494. (when (= (incf nshift) nshiftmax)
  10495. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  10496. (error "Abort")))
  10497. (org-timestamp-change n (cdr (assoc what whata)))
  10498. (org-at-timestamp-p t)
  10499. (setq ts (match-string 1))
  10500. (setq time (save-match-data (org-time-string-to-time ts)))))
  10501. (org-timestamp-change (- n) (cdr (assoc what whata)))
  10502. ;; rematch, so that we have everything in place for the real shift
  10503. (org-at-timestamp-p t)
  10504. (setq ts (match-string 1))
  10505. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  10506. (org-timestamp-change n (cdr (assoc what whata)))
  10507. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  10508. (setq org-log-post-message msg)
  10509. (message "%s" msg))))
  10510. (defun org-show-todo-tree (arg)
  10511. "Make a compact tree which shows all headlines marked with TODO.
  10512. The tree will show the lines where the regexp matches, and all higher
  10513. headlines above the match.
  10514. With a \\[universal-argument] prefix, prompt for a regexp to match.
  10515. With a numeric prefix N, construct a sparse tree for the Nth element
  10516. of `org-todo-keywords-1'."
  10517. (interactive "P")
  10518. (let ((case-fold-search nil)
  10519. (kwd-re
  10520. (cond ((null arg) org-not-done-regexp)
  10521. ((equal arg '(4))
  10522. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  10523. (mapcar 'list org-todo-keywords-1))))
  10524. (concat "\\("
  10525. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  10526. "\\)\\>")))
  10527. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  10528. (regexp-quote (nth (1- (prefix-numeric-value arg))
  10529. org-todo-keywords-1)))
  10530. (t (error "Invalid prefix argument: %s" arg)))))
  10531. (message "%d TODO entries found"
  10532. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  10533. (defun org-deadline (&optional remove time)
  10534. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  10535. With argument REMOVE, remove any deadline from the item.
  10536. When TIME is set, it should be an internal time specification, and the
  10537. scheduling will use the corresponding date."
  10538. (interactive "P")
  10539. (let* ((old-date (org-entry-get nil "DEADLINE"))
  10540. (repeater (and old-date
  10541. (string-match
  10542. "\\([.+]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10543. old-date)
  10544. (match-string 1 old-date))))
  10545. (if remove
  10546. (progn
  10547. (when (and old-date org-log-redeadline)
  10548. (org-add-log-setup 'deldeadline nil old-date 'findpos
  10549. org-log-redeadline))
  10550. (org-remove-timestamp-with-keyword org-deadline-string)
  10551. (message "Item no longer has a deadline."))
  10552. (org-add-planning-info 'deadline time 'closed)
  10553. (when (and old-date org-log-redeadline
  10554. (not (equal old-date
  10555. (substring org-last-inserted-timestamp 1 -1))))
  10556. (org-add-log-setup 'redeadline nil old-date 'findpos
  10557. org-log-redeadline))
  10558. (when repeater
  10559. (save-excursion
  10560. (org-back-to-heading t)
  10561. (when (re-search-forward (concat org-deadline-string " "
  10562. org-last-inserted-timestamp)
  10563. (save-excursion
  10564. (outline-next-heading) (point)) t)
  10565. (goto-char (1- (match-end 0)))
  10566. (insert " " repeater)
  10567. (setq org-last-inserted-timestamp
  10568. (concat (substring org-last-inserted-timestamp 0 -1)
  10569. " " repeater
  10570. (substring org-last-inserted-timestamp -1))))))
  10571. (message "Deadline on %s" org-last-inserted-timestamp))))
  10572. (defun org-schedule (&optional remove time)
  10573. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  10574. With argument REMOVE, remove any scheduling date from the item.
  10575. When TIME is set, it should be an internal time specification, and the
  10576. scheduling will use the corresponding date."
  10577. (interactive "P")
  10578. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  10579. (repeater (and old-date
  10580. (string-match
  10581. "\\([.+]+[0-9]+[dwmy]\\(?:[/ ][-+]?[0-9]+[dwmy]\\)?\\) ?"
  10582. old-date)
  10583. (match-string 1 old-date))))
  10584. (if remove
  10585. (progn
  10586. (when (and old-date org-log-reschedule)
  10587. (org-add-log-setup 'delschedule nil old-date 'findpos
  10588. org-log-reschedule))
  10589. (org-remove-timestamp-with-keyword org-scheduled-string)
  10590. (message "Item is no longer scheduled."))
  10591. (org-add-planning-info 'scheduled time 'closed)
  10592. (when (and old-date org-log-reschedule
  10593. (not (equal old-date
  10594. (substring org-last-inserted-timestamp 1 -1))))
  10595. (org-add-log-setup 'reschedule nil old-date 'findpos
  10596. org-log-reschedule))
  10597. (when repeater
  10598. (save-excursion
  10599. (org-back-to-heading t)
  10600. (when (re-search-forward (concat org-scheduled-string " "
  10601. org-last-inserted-timestamp)
  10602. (save-excursion
  10603. (outline-next-heading) (point)) t)
  10604. (goto-char (1- (match-end 0)))
  10605. (insert " " repeater)
  10606. (setq org-last-inserted-timestamp
  10607. (concat (substring org-last-inserted-timestamp 0 -1)
  10608. " " repeater
  10609. (substring org-last-inserted-timestamp -1))))))
  10610. (message "Scheduled to %s" org-last-inserted-timestamp))))
  10611. (defun org-get-scheduled-time (pom &optional inherit)
  10612. "Get the scheduled time as a time tuple, of a format suitable
  10613. for calling org-schedule with, or if there is no scheduling,
  10614. returns nil."
  10615. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  10616. (when time
  10617. (apply 'encode-time (org-parse-time-string time)))))
  10618. (defun org-get-deadline-time (pom &optional inherit)
  10619. "Get the deadline as a time tuple, of a format suitable for
  10620. calling org-deadline with, or if there is no scheduling, returns
  10621. nil."
  10622. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  10623. (when time
  10624. (apply 'encode-time (org-parse-time-string time)))))
  10625. (defun org-remove-timestamp-with-keyword (keyword)
  10626. "Remove all time stamps with KEYWORD in the current entry."
  10627. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  10628. beg)
  10629. (save-excursion
  10630. (org-back-to-heading t)
  10631. (setq beg (point))
  10632. (outline-next-heading)
  10633. (while (re-search-backward re beg t)
  10634. (replace-match "")
  10635. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  10636. (equal (char-before) ?\ ))
  10637. (backward-delete-char 1)
  10638. (if (string-match "^[ \t]*$" (buffer-substring
  10639. (point-at-bol) (point-at-eol)))
  10640. (delete-region (point-at-bol)
  10641. (min (point-max) (1+ (point-at-eol))))))))))
  10642. (defun org-add-planning-info (what &optional time &rest remove)
  10643. "Insert new timestamp with keyword in the line directly after the headline.
  10644. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  10645. If non is given, the user is prompted for a date.
  10646. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  10647. be removed."
  10648. (interactive)
  10649. (let (org-time-was-given org-end-time-was-given ts
  10650. end default-time default-input)
  10651. (catch 'exit
  10652. (when (and (not time) (memq what '(scheduled deadline)))
  10653. ;; Try to get a default date/time from existing timestamp
  10654. (save-excursion
  10655. (org-back-to-heading t)
  10656. (setq end (save-excursion (outline-next-heading) (point)))
  10657. (when (re-search-forward (if (eq what 'scheduled)
  10658. org-scheduled-time-regexp
  10659. org-deadline-time-regexp)
  10660. end t)
  10661. (setq ts (match-string 1)
  10662. default-time
  10663. (apply 'encode-time (org-parse-time-string ts))
  10664. default-input (and ts (org-get-compact-tod ts))))))
  10665. (when what
  10666. ;; If necessary, get the time from the user
  10667. (setq time (or time (org-read-date nil 'to-time nil nil
  10668. default-time default-input))))
  10669. (when (and org-insert-labeled-timestamps-at-point
  10670. (member what '(scheduled deadline)))
  10671. (insert
  10672. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  10673. (org-insert-time-stamp time org-time-was-given
  10674. nil nil nil (list org-end-time-was-given))
  10675. (setq what nil))
  10676. (save-excursion
  10677. (save-restriction
  10678. (let (col list elt ts buffer-invisibility-spec)
  10679. (org-back-to-heading t)
  10680. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  10681. (goto-char (match-end 1))
  10682. (setq col (current-column))
  10683. (goto-char (match-end 0))
  10684. (if (eobp) (insert "\n") (forward-char 1))
  10685. (when (and (not what)
  10686. (not (looking-at
  10687. (concat "[ \t]*"
  10688. org-keyword-time-not-clock-regexp))))
  10689. ;; Nothing to add, nothing to remove...... :-)
  10690. (throw 'exit nil))
  10691. (if (and (not (looking-at outline-regexp))
  10692. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  10693. "[^\r\n]*"))
  10694. (not (equal (match-string 1) org-clock-string)))
  10695. (narrow-to-region (match-beginning 0) (match-end 0))
  10696. (insert-before-markers "\n")
  10697. (backward-char 1)
  10698. (narrow-to-region (point) (point))
  10699. (and org-adapt-indentation (org-indent-to-column col)))
  10700. ;; Check if we have to remove something.
  10701. (setq list (cons what remove))
  10702. (while list
  10703. (setq elt (pop list))
  10704. (goto-char (point-min))
  10705. (when (or (and (eq elt 'scheduled)
  10706. (re-search-forward org-scheduled-time-regexp nil t))
  10707. (and (eq elt 'deadline)
  10708. (re-search-forward org-deadline-time-regexp nil t))
  10709. (and (eq elt 'closed)
  10710. (re-search-forward org-closed-time-regexp nil t)))
  10711. (replace-match "")
  10712. (if (looking-at "--+<[^>]+>") (replace-match ""))
  10713. (skip-chars-backward " ")
  10714. (if (looking-at " +") (replace-match ""))))
  10715. (goto-char (point-max))
  10716. (and org-adapt-indentation (bolp) (org-indent-to-column col))
  10717. (when what
  10718. (insert
  10719. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  10720. (cond ((eq what 'scheduled) org-scheduled-string)
  10721. ((eq what 'deadline) org-deadline-string)
  10722. ((eq what 'closed) org-closed-string))
  10723. " ")
  10724. (setq ts (org-insert-time-stamp
  10725. time
  10726. (or org-time-was-given
  10727. (and (eq what 'closed) org-log-done-with-time))
  10728. (eq what 'closed)
  10729. nil nil (list org-end-time-was-given)))
  10730. (end-of-line 1))
  10731. (goto-char (point-min))
  10732. (widen)
  10733. (if (and (looking-at "[ \t]*\n")
  10734. (equal (char-before) ?\n))
  10735. (delete-region (1- (point)) (point-at-eol)))
  10736. ts))))))
  10737. (defvar org-log-note-marker (make-marker))
  10738. (defvar org-log-note-purpose nil)
  10739. (defvar org-log-note-state nil)
  10740. (defvar org-log-note-previous-state nil)
  10741. (defvar org-log-note-how nil)
  10742. (defvar org-log-note-extra nil)
  10743. (defvar org-log-note-window-configuration nil)
  10744. (defvar org-log-note-return-to (make-marker))
  10745. (defvar org-log-post-message nil
  10746. "Message to be displayed after a log note has been stored.
  10747. The auto-repeater uses this.")
  10748. (defun org-add-note ()
  10749. "Add a note to the current entry.
  10750. This is done in the same way as adding a state change note."
  10751. (interactive)
  10752. (org-add-log-setup 'note nil nil 'findpos nil))
  10753. (defvar org-property-end-re)
  10754. (defun org-add-log-setup (&optional purpose state prev-state
  10755. findpos how extra)
  10756. "Set up the post command hook to take a note.
  10757. If this is about to TODO state change, the new state is expected in STATE.
  10758. When FINDPOS is non-nil, find the correct position for the note in
  10759. the current entry. If not, assume that it can be inserted at point.
  10760. HOW is an indicator what kind of note should be created.
  10761. EXTRA is additional text that will be inserted into the notes buffer."
  10762. (let* ((org-log-into-drawer (org-log-into-drawer))
  10763. (drawer (cond ((stringp org-log-into-drawer)
  10764. org-log-into-drawer)
  10765. (org-log-into-drawer "LOGBOOK")
  10766. (t nil))))
  10767. (save-restriction
  10768. (save-excursion
  10769. (when findpos
  10770. (org-back-to-heading t)
  10771. (narrow-to-region (point) (save-excursion
  10772. (outline-next-heading) (point)))
  10773. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  10774. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  10775. "[^\r\n]*\\)?"))
  10776. (goto-char (match-end 0))
  10777. (cond
  10778. (drawer
  10779. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  10780. nil t)
  10781. (progn
  10782. (goto-char (match-end 0))
  10783. (or org-log-states-order-reversed
  10784. (and (re-search-forward org-property-end-re nil t)
  10785. (goto-char (1- (match-beginning 0))))))
  10786. (insert "\n:" drawer ":\n:END:")
  10787. (beginning-of-line 0)
  10788. (org-indent-line-function)
  10789. (beginning-of-line 2)
  10790. (org-indent-line-function)
  10791. (end-of-line 0)))
  10792. ((and org-log-state-notes-insert-after-drawers
  10793. (save-excursion
  10794. (forward-line) (looking-at org-drawer-regexp)))
  10795. (forward-line)
  10796. (while (looking-at org-drawer-regexp)
  10797. (goto-char (match-end 0))
  10798. (re-search-forward org-property-end-re (point-max) t)
  10799. (forward-line))
  10800. (forward-line -1)))
  10801. (unless org-log-states-order-reversed
  10802. (and (= (char-after) ?\n) (forward-char 1))
  10803. (org-skip-over-state-notes)
  10804. (skip-chars-backward " \t\n\r")))
  10805. (move-marker org-log-note-marker (point))
  10806. (setq org-log-note-purpose purpose
  10807. org-log-note-state state
  10808. org-log-note-previous-state prev-state
  10809. org-log-note-how how
  10810. org-log-note-extra extra)
  10811. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  10812. (defun org-skip-over-state-notes ()
  10813. "Skip past the list of State notes in an entry."
  10814. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  10815. (when (ignore-errors (goto-char (org-in-item-p)))
  10816. (let* ((struct (org-list-struct))
  10817. (prevs (org-list-prevs-alist struct)))
  10818. (while (looking-at "[ \t]*- State")
  10819. (goto-char (or (org-list-get-next-item (point) struct prevs)
  10820. (org-list-get-item-end (point) struct)))))))
  10821. (defun org-add-log-note (&optional purpose)
  10822. "Pop up a window for taking a note, and add this note later at point."
  10823. (remove-hook 'post-command-hook 'org-add-log-note)
  10824. (setq org-log-note-window-configuration (current-window-configuration))
  10825. (delete-other-windows)
  10826. (move-marker org-log-note-return-to (point))
  10827. (switch-to-buffer (marker-buffer org-log-note-marker))
  10828. (goto-char org-log-note-marker)
  10829. (org-switch-to-buffer-other-window "*Org Note*")
  10830. (erase-buffer)
  10831. (if (memq org-log-note-how '(time state))
  10832. (let (current-prefix-arg) (org-store-log-note))
  10833. (let ((org-inhibit-startup t)) (org-mode))
  10834. (insert (format "# Insert note for %s.
  10835. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  10836. (cond
  10837. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  10838. ((eq org-log-note-purpose 'done) "closed todo item")
  10839. ((eq org-log-note-purpose 'state)
  10840. (format "state change from \"%s\" to \"%s\""
  10841. (or org-log-note-previous-state "")
  10842. (or org-log-note-state "")))
  10843. ((eq org-log-note-purpose 'reschedule)
  10844. "rescheduling")
  10845. ((eq org-log-note-purpose 'delschedule)
  10846. "no longer scheduled")
  10847. ((eq org-log-note-purpose 'redeadline)
  10848. "changing deadline")
  10849. ((eq org-log-note-purpose 'deldeadline)
  10850. "removing deadline")
  10851. ((eq org-log-note-purpose 'refile)
  10852. "refiling")
  10853. ((eq org-log-note-purpose 'note)
  10854. "this entry")
  10855. (t (error "This should not happen")))))
  10856. (if org-log-note-extra (insert org-log-note-extra))
  10857. (org-set-local 'org-finish-function 'org-store-log-note)))
  10858. (defvar org-note-abort nil) ; dynamically scoped
  10859. (defun org-store-log-note ()
  10860. "Finish taking a log note, and insert it to where it belongs."
  10861. (let ((txt (buffer-string))
  10862. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  10863. lines ind bul)
  10864. (kill-buffer (current-buffer))
  10865. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  10866. (setq txt (replace-match "" t t txt)))
  10867. (if (string-match "\\s-+\\'" txt)
  10868. (setq txt (replace-match "" t t txt)))
  10869. (setq lines (org-split-string txt "\n"))
  10870. (when (and note (string-match "\\S-" note))
  10871. (setq note
  10872. (org-replace-escapes
  10873. note
  10874. (list (cons "%u" (user-login-name))
  10875. (cons "%U" user-full-name)
  10876. (cons "%t" (format-time-string
  10877. (org-time-stamp-format 'long 'inactive)
  10878. (current-time)))
  10879. (cons "%T" (format-time-string
  10880. (org-time-stamp-format 'long nil)
  10881. (current-time)))
  10882. (cons "%s" (if org-log-note-state
  10883. (concat "\"" org-log-note-state "\"")
  10884. ""))
  10885. (cons "%S" (if org-log-note-previous-state
  10886. (concat "\"" org-log-note-previous-state "\"")
  10887. "\"\"")))))
  10888. (if lines (setq note (concat note " \\\\")))
  10889. (push note lines))
  10890. (when (or current-prefix-arg org-note-abort)
  10891. (when org-log-into-drawer
  10892. (org-remove-empty-drawer-at
  10893. (if (stringp org-log-into-drawer) org-log-into-drawer "LOGBOOK")
  10894. org-log-note-marker))
  10895. (setq lines nil))
  10896. (when lines
  10897. (with-current-buffer (marker-buffer org-log-note-marker)
  10898. (save-excursion
  10899. (goto-char org-log-note-marker)
  10900. (move-marker org-log-note-marker nil)
  10901. (end-of-line 1)
  10902. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  10903. (setq ind (save-excursion
  10904. (if (ignore-errors (goto-char (org-in-item-p)))
  10905. (let ((struct (org-list-struct)))
  10906. (org-list-get-ind
  10907. (org-list-get-top-point struct) struct))
  10908. (skip-chars-backward " \r\t\n")
  10909. (cond
  10910. ((and (org-at-heading-p)
  10911. org-adapt-indentation)
  10912. (1+ (org-current-level)))
  10913. ((org-at-heading-p) 0)
  10914. (t (org-get-indentation))))))
  10915. (setq bul (org-list-bullet-string "-"))
  10916. (org-indent-line-to ind)
  10917. (insert bul (pop lines))
  10918. (let ((ind-body (+ (length bul) ind)))
  10919. (while lines
  10920. (insert "\n")
  10921. (org-indent-line-to ind-body)
  10922. (insert (pop lines))))
  10923. (message "Note stored")
  10924. (org-back-to-heading t)
  10925. (org-cycle-hide-drawers 'children)))))
  10926. (set-window-configuration org-log-note-window-configuration)
  10927. (with-current-buffer (marker-buffer org-log-note-return-to)
  10928. (goto-char org-log-note-return-to))
  10929. (move-marker org-log-note-return-to nil)
  10930. (and org-log-post-message (message "%s" org-log-post-message)))
  10931. (defun org-remove-empty-drawer-at (drawer pos)
  10932. "Remove an empty drawer DRAWER at position POS.
  10933. POS may also be a marker."
  10934. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  10935. (save-excursion
  10936. (save-restriction
  10937. (widen)
  10938. (goto-char pos)
  10939. (if (org-in-regexp
  10940. (concat "^[ \t]*:" drawer ":[ \t]*\n[ \t]*:END:[ \t]*\n?") 2)
  10941. (replace-match ""))))))
  10942. (defun org-sparse-tree (&optional arg)
  10943. "Create a sparse tree, prompt for the details.
  10944. This command can create sparse trees. You first need to select the type
  10945. of match used to create the tree:
  10946. t Show all TODO entries.
  10947. T Show entries with a specific TODO keyword.
  10948. m Show entries selected by a tags/property match.
  10949. p Enter a property name and its value (both with completion on existing
  10950. names/values) and show entries with that property.
  10951. r Show entries matching a regular expression (`/' can be used as well)
  10952. d Show deadlines due within `org-deadline-warning-days'.
  10953. b Show deadlines and scheduled items before a date.
  10954. a Show deadlines and scheduled items after a date."
  10955. (interactive "P")
  10956. (let (ans kwd value)
  10957. (message "Sparse tree: [r]egexp [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty\n [d]eadlines [b]efore-date [a]fter-date")
  10958. (setq ans (read-char-exclusive))
  10959. (cond
  10960. ((equal ans ?d)
  10961. (call-interactively 'org-check-deadlines))
  10962. ((equal ans ?b)
  10963. (call-interactively 'org-check-before-date))
  10964. ((equal ans ?a)
  10965. (call-interactively 'org-check-after-date))
  10966. ((equal ans ?t)
  10967. (org-show-todo-tree nil))
  10968. ((equal ans ?T)
  10969. (org-show-todo-tree '(4)))
  10970. ((member ans '(?T ?m))
  10971. (call-interactively 'org-match-sparse-tree))
  10972. ((member ans '(?p ?P))
  10973. (setq kwd (org-icompleting-read "Property: "
  10974. (mapcar 'list (org-buffer-property-keys))))
  10975. (setq value (org-icompleting-read "Value: "
  10976. (mapcar 'list (org-property-values kwd))))
  10977. (unless (string-match "\\`{.*}\\'" value)
  10978. (setq value (concat "\"" value "\"")))
  10979. (org-match-sparse-tree arg (concat kwd "=" value)))
  10980. ((member ans '(?r ?R ?/))
  10981. (call-interactively 'org-occur))
  10982. (t (error "No such sparse tree command \"%c\"" ans)))))
  10983. (defvar org-occur-highlights nil
  10984. "List of overlays used for occur matches.")
  10985. (make-variable-buffer-local 'org-occur-highlights)
  10986. (defvar org-occur-parameters nil
  10987. "Parameters of the active org-occur calls.
  10988. This is a list, each call to org-occur pushes as cons cell,
  10989. containing the regular expression and the callback, onto the list.
  10990. The list can contain several entries if `org-occur' has been called
  10991. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  10992. will only contain one set of parameters. When the highlights are
  10993. removed (for example with `C-c C-c', or with the next edit (depending
  10994. on `org-remove-highlights-with-change'), this variable is emptied
  10995. as well.")
  10996. (make-variable-buffer-local 'org-occur-parameters)
  10997. (defun org-occur (regexp &optional keep-previous callback)
  10998. "Make a compact tree which shows all matches of REGEXP.
  10999. The tree will show the lines where the regexp matches, and all higher
  11000. headlines above the match. It will also show the heading after the match,
  11001. to make sure editing the matching entry is easy.
  11002. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  11003. call to `org-occur' will be kept, to allow stacking of calls to this
  11004. command.
  11005. If CALLBACK is non-nil, it is a function which is called to confirm
  11006. that the match should indeed be shown."
  11007. (interactive "sRegexp: \nP")
  11008. (when (equal regexp "")
  11009. (error "Regexp cannot be empty"))
  11010. (unless keep-previous
  11011. (org-remove-occur-highlights nil nil t))
  11012. (push (cons regexp callback) org-occur-parameters)
  11013. (let ((cnt 0))
  11014. (save-excursion
  11015. (goto-char (point-min))
  11016. (if (or (not keep-previous) ; do not want to keep
  11017. (not org-occur-highlights)) ; no previous matches
  11018. ;; hide everything
  11019. (org-overview))
  11020. (while (re-search-forward regexp nil t)
  11021. (when (or (not callback)
  11022. (save-match-data (funcall callback)))
  11023. (setq cnt (1+ cnt))
  11024. (when org-highlight-sparse-tree-matches
  11025. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  11026. (org-show-context 'occur-tree))))
  11027. (when org-remove-highlights-with-change
  11028. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  11029. nil 'local))
  11030. (unless org-sparse-tree-open-archived-trees
  11031. (org-hide-archived-subtrees (point-min) (point-max)))
  11032. (run-hooks 'org-occur-hook)
  11033. (if (interactive-p)
  11034. (message "%d match(es) for regexp %s" cnt regexp))
  11035. cnt))
  11036. (defun org-occur-next-match (&optional n reset)
  11037. "Function for `next-error-function' to find sparse tree matches.
  11038. N is the number of matches to move, when negative move backwards.
  11039. RESET is entirely ignored - this function always goes back to the
  11040. starting point when no match is found."
  11041. (let* ((limit (if (< n 0) (point-min) (point-max)))
  11042. (search-func (if (< n 0)
  11043. 'previous-single-char-property-change
  11044. 'next-single-char-property-change))
  11045. (n (abs n))
  11046. (pos (point))
  11047. p1)
  11048. (catch 'exit
  11049. (while (setq p1 (funcall search-func (point) 'org-type))
  11050. (when (equal p1 limit)
  11051. (goto-char pos)
  11052. (error "No more matches"))
  11053. (when (equal (get-char-property p1 'org-type) 'org-occur)
  11054. (setq n (1- n))
  11055. (when (= n 0)
  11056. (goto-char p1)
  11057. (throw 'exit (point))))
  11058. (goto-char p1))
  11059. (goto-char p1)
  11060. (error "No more matches"))))
  11061. (defun org-show-context (&optional key)
  11062. "Make sure point and context are visible.
  11063. How much context is shown depends upon the variables
  11064. `org-show-hierarchy-above', `org-show-following-heading'. and
  11065. `org-show-siblings'."
  11066. (let ((heading-p (org-on-heading-p t))
  11067. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  11068. (following-p (org-get-alist-option org-show-following-heading key))
  11069. (entry-p (org-get-alist-option org-show-entry-below key))
  11070. (siblings-p (org-get-alist-option org-show-siblings key)))
  11071. (catch 'exit
  11072. ;; Show heading or entry text
  11073. (if (and heading-p (not entry-p))
  11074. (org-flag-heading nil) ; only show the heading
  11075. (and (or entry-p (outline-invisible-p) (org-invisible-p2))
  11076. (org-show-hidden-entry))) ; show entire entry
  11077. (when following-p
  11078. ;; Show next sibling, or heading below text
  11079. (save-excursion
  11080. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  11081. (org-flag-heading nil))))
  11082. (when siblings-p (org-show-siblings))
  11083. (when hierarchy-p
  11084. ;; show all higher headings, possibly with siblings
  11085. (save-excursion
  11086. (while (and (condition-case nil
  11087. (progn (org-up-heading-all 1) t)
  11088. (error nil))
  11089. (not (bobp)))
  11090. (org-flag-heading nil)
  11091. (when siblings-p (org-show-siblings))))))))
  11092. (defvar org-reveal-start-hook nil
  11093. "Hook run before revealing a location.")
  11094. (defun org-reveal (&optional siblings)
  11095. "Show current entry, hierarchy above it, and the following headline.
  11096. This can be used to show a consistent set of context around locations
  11097. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  11098. not t for the search context.
  11099. With optional argument SIBLINGS, on each level of the hierarchy all
  11100. siblings are shown. This repairs the tree structure to what it would
  11101. look like when opened with hierarchical calls to `org-cycle'.
  11102. With double optional argument \\[universal-argument] \\[universal-argument], \
  11103. go to the parent and show the
  11104. entire tree."
  11105. (interactive "P")
  11106. (run-hooks 'org-reveal-start-hook)
  11107. (let ((org-show-hierarchy-above t)
  11108. (org-show-following-heading t)
  11109. (org-show-siblings (if siblings t org-show-siblings)))
  11110. (org-show-context nil))
  11111. (when (equal siblings '(16))
  11112. (save-excursion
  11113. (when (org-up-heading-safe)
  11114. (org-show-subtree)
  11115. (run-hook-with-args 'org-cycle-hook 'subtree)))))
  11116. (defun org-highlight-new-match (beg end)
  11117. "Highlight from BEG to END and mark the highlight is an occur headline."
  11118. (let ((ov (make-overlay beg end)))
  11119. (overlay-put ov 'face 'secondary-selection)
  11120. (overlay-put ov 'org-type 'org-occur)
  11121. (push ov org-occur-highlights)))
  11122. (defun org-remove-occur-highlights (&optional beg end noremove)
  11123. "Remove the occur highlights from the buffer.
  11124. BEG and END are ignored. If NOREMOVE is nil, remove this function
  11125. from the `before-change-functions' in the current buffer."
  11126. (interactive)
  11127. (unless org-inhibit-highlight-removal
  11128. (mapc 'delete-overlay org-occur-highlights)
  11129. (setq org-occur-highlights nil)
  11130. (setq org-occur-parameters nil)
  11131. (unless noremove
  11132. (remove-hook 'before-change-functions
  11133. 'org-remove-occur-highlights 'local))))
  11134. ;;;; Priorities
  11135. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  11136. "Regular expression matching the priority indicator.")
  11137. (defvar org-remove-priority-next-time nil)
  11138. (defun org-priority-up ()
  11139. "Increase the priority of the current item."
  11140. (interactive)
  11141. (org-priority 'up))
  11142. (defun org-priority-down ()
  11143. "Decrease the priority of the current item."
  11144. (interactive)
  11145. (org-priority 'down))
  11146. (defun org-priority (&optional action)
  11147. "Change the priority of an item by ARG.
  11148. ACTION can be `set', `up', `down', or a character."
  11149. (interactive)
  11150. (unless org-enable-priority-commands
  11151. (error "Priority commands are disabled"))
  11152. (setq action (or action 'set))
  11153. (let (current new news have remove)
  11154. (save-excursion
  11155. (org-back-to-heading t)
  11156. (if (looking-at org-priority-regexp)
  11157. (setq current (string-to-char (match-string 2))
  11158. have t)
  11159. (setq current org-default-priority))
  11160. (cond
  11161. ((eq action 'remove)
  11162. (setq remove t new ?\ ))
  11163. ((or (eq action 'set)
  11164. (if (featurep 'xemacs) (characterp action) (integerp action)))
  11165. (if (not (eq action 'set))
  11166. (setq new action)
  11167. (message "Priority %c-%c, SPC to remove: "
  11168. org-highest-priority org-lowest-priority)
  11169. (save-match-data
  11170. (setq new (read-char-exclusive))))
  11171. (if (and (= (upcase org-highest-priority) org-highest-priority)
  11172. (= (upcase org-lowest-priority) org-lowest-priority))
  11173. (setq new (upcase new)))
  11174. (cond ((equal new ?\ ) (setq remove t))
  11175. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  11176. (error "Priority must be between `%c' and `%c'"
  11177. org-highest-priority org-lowest-priority))))
  11178. ((eq action 'up)
  11179. (if (and (not have) (eq last-command this-command))
  11180. (setq new org-lowest-priority)
  11181. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11182. org-default-priority (1- current)))))
  11183. ((eq action 'down)
  11184. (if (and (not have) (eq last-command this-command))
  11185. (setq new org-highest-priority)
  11186. (setq new (if (and org-priority-start-cycle-with-default (not have))
  11187. org-default-priority (1+ current)))))
  11188. (t (error "Invalid action")))
  11189. (if (or (< (upcase new) org-highest-priority)
  11190. (> (upcase new) org-lowest-priority))
  11191. (setq remove t))
  11192. (setq news (format "%c" new))
  11193. (if have
  11194. (if remove
  11195. (replace-match "" t t nil 1)
  11196. (replace-match news t t nil 2))
  11197. (if remove
  11198. (error "No priority cookie found in line")
  11199. (let ((case-fold-search nil))
  11200. (looking-at org-todo-line-regexp))
  11201. (if (match-end 2)
  11202. (progn
  11203. (goto-char (match-end 2))
  11204. (insert " [#" news "]"))
  11205. (goto-char (match-beginning 3))
  11206. (insert "[#" news "] "))))
  11207. (org-preserve-lc (org-set-tags nil 'align)))
  11208. (if remove
  11209. (message "Priority removed")
  11210. (message "Priority of current item set to %s" news))))
  11211. (defun org-get-priority (s)
  11212. "Find priority cookie and return priority."
  11213. (if (functionp org-get-priority-function)
  11214. (funcall org-get-priority-function)
  11215. (save-match-data
  11216. (if (not (string-match org-priority-regexp s))
  11217. (* 1000 (- org-lowest-priority org-default-priority))
  11218. (* 1000 (- org-lowest-priority
  11219. (string-to-char (match-string 2 s))))))))
  11220. ;;;; Tags
  11221. (defvar org-agenda-archives-mode)
  11222. (defvar org-map-continue-from nil
  11223. "Position from where mapping should continue.
  11224. Can be set by the action argument to `org-scan-tag's and `org-map-entries'.")
  11225. (defvar org-scanner-tags nil
  11226. "The current tag list while the tags scanner is running.")
  11227. (defvar org-trust-scanner-tags nil
  11228. "Should `org-get-tags-at' use the tags for the scanner.
  11229. This is for internal dynamical scoping only.
  11230. When this is non-nil, the function `org-get-tags-at' will return the value
  11231. of `org-scanner-tags' instead of building the list by itself. This
  11232. can lead to large speed-ups when the tags scanner is used in a file with
  11233. many entries, and when the list of tags is retrieved, for example to
  11234. obtain a list of properties. Building the tags list for each entry in such
  11235. a file becomes an N^2 operation - but with this variable set, it scales
  11236. as N.")
  11237. (defun org-scan-tags (action matcher &optional todo-only)
  11238. "Scan headline tags with inheritance and produce output ACTION.
  11239. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  11240. or `agenda' to produce an entry list for an agenda view. It can also be
  11241. a Lisp form or a function that should be called at each matched headline, in
  11242. this case the return value is a list of all return values from these calls.
  11243. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  11244. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  11245. only lines with a TODO keyword are included in the output."
  11246. (require 'org-agenda)
  11247. (let* ((re (concat "^" outline-regexp " *\\(\\<\\("
  11248. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  11249. (org-re
  11250. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  11251. (props (list 'face 'default
  11252. 'done-face 'org-agenda-done
  11253. 'undone-face 'default
  11254. 'mouse-face 'highlight
  11255. 'org-not-done-regexp org-not-done-regexp
  11256. 'org-todo-regexp org-todo-regexp
  11257. 'help-echo
  11258. (format "mouse-2 or RET jump to org file %s"
  11259. (abbreviate-file-name
  11260. (or (buffer-file-name (buffer-base-buffer))
  11261. (buffer-name (buffer-base-buffer)))))))
  11262. (case-fold-search nil)
  11263. (org-map-continue-from nil)
  11264. lspos tags tags-list
  11265. (tags-alist (list (cons 0 org-file-tags)))
  11266. (llast 0) rtn rtn1 level category i txt
  11267. todo marker entry priority)
  11268. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  11269. (setq action (list 'lambda nil action)))
  11270. (save-excursion
  11271. (goto-char (point-min))
  11272. (when (eq action 'sparse-tree)
  11273. (org-overview)
  11274. (org-remove-occur-highlights))
  11275. (while (re-search-forward re nil t)
  11276. (catch :skip
  11277. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  11278. tags (if (match-end 4) (org-match-string-no-properties 4)))
  11279. (goto-char (setq lspos (match-beginning 0)))
  11280. (setq level (org-reduced-level (funcall outline-level))
  11281. category (org-get-category))
  11282. (setq i llast llast level)
  11283. ;; remove tag lists from same and sublevels
  11284. (while (>= i level)
  11285. (when (setq entry (assoc i tags-alist))
  11286. (setq tags-alist (delete entry tags-alist)))
  11287. (setq i (1- i)))
  11288. ;; add the next tags
  11289. (when tags
  11290. (setq tags (org-split-string tags ":")
  11291. tags-alist
  11292. (cons (cons level tags) tags-alist)))
  11293. ;; compile tags for current headline
  11294. (setq tags-list
  11295. (if org-use-tag-inheritance
  11296. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  11297. tags)
  11298. org-scanner-tags tags-list)
  11299. (when org-use-tag-inheritance
  11300. (setcdr (car tags-alist)
  11301. (mapcar (lambda (x)
  11302. (setq x (copy-sequence x))
  11303. (org-add-prop-inherited x))
  11304. (cdar tags-alist))))
  11305. (when (and tags org-use-tag-inheritance
  11306. (or (not (eq t org-use-tag-inheritance))
  11307. org-tags-exclude-from-inheritance))
  11308. ;; selective inheritance, remove uninherited ones
  11309. (setcdr (car tags-alist)
  11310. (org-remove-uniherited-tags (cdar tags-alist))))
  11311. (when (and (or (not todo-only)
  11312. (and (member todo org-not-done-keywords)
  11313. (or (not org-agenda-tags-todo-honor-ignore-options)
  11314. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  11315. (let ((case-fold-search t)) (eval matcher))
  11316. (or
  11317. (not (member org-archive-tag tags-list))
  11318. ;; we have an archive tag, should we use this anyway?
  11319. (or (not org-agenda-skip-archived-trees)
  11320. (and (eq action 'agenda) org-agenda-archives-mode))))
  11321. (unless (eq action 'sparse-tree) (org-agenda-skip))
  11322. ;; select this headline
  11323. (cond
  11324. ((eq action 'sparse-tree)
  11325. (and org-highlight-sparse-tree-matches
  11326. (org-get-heading) (match-end 0)
  11327. (org-highlight-new-match
  11328. (match-beginning 0) (match-beginning 1)))
  11329. (org-show-context 'tags-tree))
  11330. ((eq action 'agenda)
  11331. (setq txt (org-format-agenda-item
  11332. ""
  11333. (concat
  11334. (if (eq org-tags-match-list-sublevels 'indented)
  11335. (make-string (1- level) ?.) "")
  11336. (org-get-heading))
  11337. category
  11338. tags-list
  11339. )
  11340. priority (org-get-priority txt))
  11341. (goto-char lspos)
  11342. (setq marker (org-agenda-new-marker))
  11343. (org-add-props txt props
  11344. 'org-marker marker 'org-hd-marker marker 'org-category category
  11345. 'todo-state todo
  11346. 'priority priority 'type "tagsmatch")
  11347. (push txt rtn))
  11348. ((functionp action)
  11349. (setq org-map-continue-from nil)
  11350. (save-excursion
  11351. (setq rtn1 (funcall action))
  11352. (push rtn1 rtn)))
  11353. (t (error "Invalid action")))
  11354. ;; if we are to skip sublevels, jump to end of subtree
  11355. (unless org-tags-match-list-sublevels
  11356. (org-end-of-subtree t)
  11357. (backward-char 1))))
  11358. ;; Get the correct position from where to continue
  11359. (if org-map-continue-from
  11360. (goto-char org-map-continue-from)
  11361. (and (= (point) lspos) (end-of-line 1)))))
  11362. (when (and (eq action 'sparse-tree)
  11363. (not org-sparse-tree-open-archived-trees))
  11364. (org-hide-archived-subtrees (point-min) (point-max)))
  11365. (nreverse rtn)))
  11366. (defun org-remove-uniherited-tags (tags)
  11367. "Remove all tags that are not inherited from the list TAGS."
  11368. (cond
  11369. ((eq org-use-tag-inheritance t)
  11370. (if org-tags-exclude-from-inheritance
  11371. (org-delete-all org-tags-exclude-from-inheritance tags)
  11372. tags))
  11373. ((not org-use-tag-inheritance) nil)
  11374. ((stringp org-use-tag-inheritance)
  11375. (delq nil (mapcar
  11376. (lambda (x)
  11377. (if (and (string-match org-use-tag-inheritance x)
  11378. (not (member x org-tags-exclude-from-inheritance)))
  11379. x nil))
  11380. tags)))
  11381. ((listp org-use-tag-inheritance)
  11382. (delq nil (mapcar
  11383. (lambda (x)
  11384. (if (member x org-use-tag-inheritance) x nil))
  11385. tags)))))
  11386. (defvar todo-only) ;; dynamically scoped
  11387. (defun org-match-sparse-tree (&optional todo-only match)
  11388. "Create a sparse tree according to tags string MATCH.
  11389. MATCH can contain positive and negative selection of tags, like
  11390. \"+WORK+URGENT-WITHBOSS\".
  11391. If optional argument TODO-ONLY is non-nil, only select lines that are
  11392. also TODO lines."
  11393. (interactive "P")
  11394. (org-prepare-agenda-buffers (list (current-buffer)))
  11395. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  11396. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  11397. (defvar org-cached-props nil)
  11398. (defun org-cached-entry-get (pom property)
  11399. (if (or (eq t org-use-property-inheritance)
  11400. (and (stringp org-use-property-inheritance)
  11401. (string-match org-use-property-inheritance property))
  11402. (and (listp org-use-property-inheritance)
  11403. (member property org-use-property-inheritance)))
  11404. ;; Caching is not possible, check it directly
  11405. (org-entry-get pom property 'inherit)
  11406. ;; Get all properties, so that we can do complicated checks easily
  11407. (cdr (assoc property (or org-cached-props
  11408. (setq org-cached-props
  11409. (org-entry-properties pom)))))))
  11410. (defun org-global-tags-completion-table (&optional files)
  11411. "Return the list of all tags in all agenda buffer/files.
  11412. Optional FILES argument is a list of files to which can be used
  11413. instead of the agenda files."
  11414. (save-excursion
  11415. (org-uniquify
  11416. (delq nil
  11417. (apply 'append
  11418. (mapcar
  11419. (lambda (file)
  11420. (set-buffer (find-file-noselect file))
  11421. (append (org-get-buffer-tags)
  11422. (mapcar (lambda (x) (if (stringp (car-safe x))
  11423. (list (car-safe x)) nil))
  11424. org-tag-alist)))
  11425. (if (and files (car files))
  11426. files
  11427. (org-agenda-files))))))))
  11428. (defun org-make-tags-matcher (match)
  11429. "Create the TAGS/TODO matcher form for the selection string MATCH."
  11430. ;; todo-only is scoped dynamically into this function, and the function
  11431. ;; may change it if the matcher asks for it.
  11432. (unless match
  11433. ;; Get a new match request, with completion
  11434. (let ((org-last-tags-completion-table
  11435. (org-global-tags-completion-table)))
  11436. (setq match (org-completing-read-no-i
  11437. "Match: " 'org-tags-completion-function nil nil nil
  11438. 'org-tags-history))))
  11439. ;; Parse the string and create a lisp form
  11440. (let ((match0 match)
  11441. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  11442. minus tag mm
  11443. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  11444. orterms term orlist re-p str-p level-p level-op time-p
  11445. prop-p pn pv po gv rest)
  11446. (if (string-match "/+" match)
  11447. ;; match contains also a todo-matching request
  11448. (progn
  11449. (setq tagsmatch (substring match 0 (match-beginning 0))
  11450. todomatch (substring match (match-end 0)))
  11451. (if (string-match "^!" todomatch)
  11452. (setq todo-only t todomatch (substring todomatch 1)))
  11453. (if (string-match "^\\s-*$" todomatch)
  11454. (setq todomatch nil)))
  11455. ;; only matching tags
  11456. (setq tagsmatch match todomatch nil))
  11457. ;; Make the tags matcher
  11458. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  11459. (setq tagsmatcher t)
  11460. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  11461. (while (setq term (pop orterms))
  11462. (while (and (equal (substring term -1) "\\") orterms)
  11463. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  11464. (while (string-match re term)
  11465. (setq rest (substring term (match-end 0))
  11466. minus (and (match-end 1)
  11467. (equal (match-string 1 term) "-"))
  11468. tag (save-match-data (replace-regexp-in-string
  11469. "\\\\-" "-"
  11470. (match-string 2 term)))
  11471. re-p (equal (string-to-char tag) ?{)
  11472. level-p (match-end 4)
  11473. prop-p (match-end 5)
  11474. mm (cond
  11475. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  11476. (level-p
  11477. (setq level-op (org-op-to-function (match-string 3 term)))
  11478. `(,level-op level ,(string-to-number
  11479. (match-string 4 term))))
  11480. (prop-p
  11481. (setq pn (match-string 5 term)
  11482. po (match-string 6 term)
  11483. pv (match-string 7 term)
  11484. re-p (equal (string-to-char pv) ?{)
  11485. str-p (equal (string-to-char pv) ?\")
  11486. time-p (save-match-data
  11487. (string-match "^\"[[<].*[]>]\"$" pv))
  11488. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  11489. (if time-p (setq pv (org-matcher-time pv)))
  11490. (setq po (org-op-to-function po (if time-p 'time str-p)))
  11491. (cond
  11492. ((equal pn "CATEGORY")
  11493. (setq gv '(get-text-property (point) 'org-category)))
  11494. ((equal pn "TODO")
  11495. (setq gv 'todo))
  11496. (t
  11497. (setq gv `(org-cached-entry-get nil ,pn))))
  11498. (if re-p
  11499. (if (eq po 'org<>)
  11500. `(not (string-match ,pv (or ,gv "")))
  11501. `(string-match ,pv (or ,gv "")))
  11502. (if str-p
  11503. `(,po (or ,gv "") ,pv)
  11504. `(,po (string-to-number (or ,gv ""))
  11505. ,(string-to-number pv) ))))
  11506. (t `(member ,tag tags-list)))
  11507. mm (if minus (list 'not mm) mm)
  11508. term rest)
  11509. (push mm tagsmatcher))
  11510. (push (if (> (length tagsmatcher) 1)
  11511. (cons 'and tagsmatcher)
  11512. (car tagsmatcher))
  11513. orlist)
  11514. (setq tagsmatcher nil))
  11515. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  11516. (setq tagsmatcher
  11517. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  11518. ;; Make the todo matcher
  11519. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  11520. (setq todomatcher t)
  11521. (setq orterms (org-split-string todomatch "|") orlist nil)
  11522. (while (setq term (pop orterms))
  11523. (while (string-match re term)
  11524. (setq minus (and (match-end 1)
  11525. (equal (match-string 1 term) "-"))
  11526. kwd (match-string 2 term)
  11527. re-p (equal (string-to-char kwd) ?{)
  11528. term (substring term (match-end 0))
  11529. mm (if re-p
  11530. `(string-match ,(substring kwd 1 -1) todo)
  11531. (list 'equal 'todo kwd))
  11532. mm (if minus (list 'not mm) mm))
  11533. (push mm todomatcher))
  11534. (push (if (> (length todomatcher) 1)
  11535. (cons 'and todomatcher)
  11536. (car todomatcher))
  11537. orlist)
  11538. (setq todomatcher nil))
  11539. (setq todomatcher (if (> (length orlist) 1)
  11540. (cons 'or orlist) (car orlist))))
  11541. ;; Return the string and lisp forms of the matcher
  11542. (setq matcher (if todomatcher
  11543. (list 'and tagsmatcher todomatcher)
  11544. tagsmatcher))
  11545. (cons match0 matcher)))
  11546. (defun org-op-to-function (op &optional stringp)
  11547. "Turn an operator into the appropriate function."
  11548. (setq op
  11549. (cond
  11550. ((equal op "<" ) '(< string< org-time<))
  11551. ((equal op ">" ) '(> org-string> org-time>))
  11552. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  11553. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  11554. ((member op '("=" "==")) '(= string= org-time=))
  11555. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  11556. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  11557. (defun org<> (a b) (not (= a b)))
  11558. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  11559. (defun org-string>= (a b) (not (string< a b)))
  11560. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  11561. (defun org-string<> (a b) (not (string= a b)))
  11562. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  11563. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  11564. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  11565. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  11566. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  11567. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  11568. (defun org-2ft (s)
  11569. "Convert S to a floating point time.
  11570. If S is already a number, just return it. If it is a string, parse
  11571. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  11572. (cond
  11573. ((numberp s) s)
  11574. ((stringp s)
  11575. (condition-case nil
  11576. (float-time (apply 'encode-time (org-parse-time-string s)))
  11577. (error 0.)))
  11578. (t 0.)))
  11579. (defun org-time-today ()
  11580. "Time in seconds today at 0:00.
  11581. Returns the float number of seconds since the beginning of the
  11582. epoch to the beginning of today (00:00)."
  11583. (float-time (apply 'encode-time
  11584. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  11585. (defun org-matcher-time (s)
  11586. "Interpret a time comparison value."
  11587. (save-match-data
  11588. (cond
  11589. ((string= s "<now>") (float-time))
  11590. ((string= s "<today>") (org-time-today))
  11591. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  11592. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  11593. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  11594. (+ (org-time-today)
  11595. (* (string-to-number (match-string 1 s))
  11596. (cdr (assoc (match-string 2 s)
  11597. '(("d" . 86400.0) ("w" . 604800.0)
  11598. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  11599. (t (org-2ft s)))))
  11600. (defun org-match-any-p (re list)
  11601. "Does re match any element of list?"
  11602. (setq list (mapcar (lambda (x) (string-match re x)) list))
  11603. (delq nil list))
  11604. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  11605. (defvar org-tags-overlay (make-overlay 1 1))
  11606. (org-detach-overlay org-tags-overlay)
  11607. (defun org-get-local-tags-at (&optional pos)
  11608. "Get a list of tags defined in the current headline."
  11609. (org-get-tags-at pos 'local))
  11610. (defun org-get-local-tags ()
  11611. "Get a list of tags defined in the current headline."
  11612. (org-get-tags-at nil 'local))
  11613. (defun org-get-tags-at (&optional pos local)
  11614. "Get a list of all headline tags applicable at POS.
  11615. POS defaults to point. If tags are inherited, the list contains
  11616. the targets in the same sequence as the headlines appear, i.e.
  11617. the tags of the current headline come last.
  11618. When LOCAL is non-nil, only return tags from the current headline,
  11619. ignore inherited ones."
  11620. (interactive)
  11621. (if (and org-trust-scanner-tags
  11622. (or (not pos) (equal pos (point)))
  11623. (not local))
  11624. org-scanner-tags
  11625. (let (tags ltags lastpos parent)
  11626. (save-excursion
  11627. (save-restriction
  11628. (widen)
  11629. (goto-char (or pos (point)))
  11630. (save-match-data
  11631. (catch 'done
  11632. (condition-case nil
  11633. (progn
  11634. (org-back-to-heading t)
  11635. (while (not (equal lastpos (point)))
  11636. (setq lastpos (point))
  11637. (when (looking-at
  11638. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  11639. (setq ltags (org-split-string
  11640. (org-match-string-no-properties 1) ":"))
  11641. (when parent
  11642. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  11643. (setq tags (append
  11644. (if parent
  11645. (org-remove-uniherited-tags ltags)
  11646. ltags)
  11647. tags)))
  11648. (or org-use-tag-inheritance (throw 'done t))
  11649. (if local (throw 'done t))
  11650. (or (org-up-heading-safe) (error nil))
  11651. (setq parent t)))
  11652. (error nil)))))
  11653. (append (org-remove-uniherited-tags org-file-tags) tags)))))
  11654. (defun org-add-prop-inherited (s)
  11655. (add-text-properties 0 (length s) '(inherited t) s)
  11656. s)
  11657. (defun org-toggle-tag (tag &optional onoff)
  11658. "Toggle the tag TAG for the current line.
  11659. If ONOFF is `on' or `off', don't toggle but set to this state."
  11660. (let (res current)
  11661. (save-excursion
  11662. (org-back-to-heading t)
  11663. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  11664. (point-at-eol) t)
  11665. (progn
  11666. (setq current (match-string 1))
  11667. (replace-match ""))
  11668. (setq current ""))
  11669. (setq current (nreverse (org-split-string current ":")))
  11670. (cond
  11671. ((eq onoff 'on)
  11672. (setq res t)
  11673. (or (member tag current) (push tag current)))
  11674. ((eq onoff 'off)
  11675. (or (not (member tag current)) (setq current (delete tag current))))
  11676. (t (if (member tag current)
  11677. (setq current (delete tag current))
  11678. (setq res t)
  11679. (push tag current))))
  11680. (end-of-line 1)
  11681. (if current
  11682. (progn
  11683. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  11684. (org-set-tags nil t))
  11685. (delete-horizontal-space))
  11686. (run-hooks 'org-after-tags-change-hook))
  11687. res))
  11688. (defun org-align-tags-here (to-col)
  11689. ;; Assumes that this is a headline
  11690. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  11691. (beginning-of-line 1)
  11692. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11693. (< pos (match-beginning 2)))
  11694. (progn
  11695. (setq tags-l (- (match-end 2) (match-beginning 2)))
  11696. (goto-char (match-beginning 1))
  11697. (insert " ")
  11698. (delete-region (point) (1+ (match-beginning 2)))
  11699. (setq ncol (max (1+ (current-column))
  11700. (1+ col)
  11701. (if (> to-col 0)
  11702. to-col
  11703. (- (abs to-col) tags-l))))
  11704. (setq p (point))
  11705. (insert (make-string (- ncol (current-column)) ?\ ))
  11706. (setq ncol (current-column))
  11707. (when indent-tabs-mode (tabify p (point-at-eol)))
  11708. (org-move-to-column (min ncol col) t))
  11709. (goto-char pos))))
  11710. (defun org-set-tags-command (&optional arg just-align)
  11711. "Call the set-tags command for the current entry."
  11712. (interactive "P")
  11713. (if (org-on-heading-p)
  11714. (org-set-tags arg just-align)
  11715. (save-excursion
  11716. (org-back-to-heading t)
  11717. (org-set-tags arg just-align))))
  11718. (defun org-set-tags-to (data)
  11719. "Set the tags of the current entry to DATA, replacing the current tags.
  11720. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  11721. If DATA is nil or the empty string, any tags will be removed."
  11722. (interactive "sTags: ")
  11723. (setq data
  11724. (cond
  11725. ((eq data nil) "")
  11726. ((equal data "") "")
  11727. ((stringp data)
  11728. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  11729. ":"))
  11730. ((listp data)
  11731. (concat ":" (mapconcat 'identity data ":") ":"))
  11732. (t nil)))
  11733. (when data
  11734. (save-excursion
  11735. (org-back-to-heading t)
  11736. (when (looking-at org-complex-heading-regexp)
  11737. (if (match-end 5)
  11738. (progn
  11739. (goto-char (match-beginning 5))
  11740. (insert data)
  11741. (delete-region (point) (point-at-eol))
  11742. (org-set-tags nil 'align))
  11743. (goto-char (point-at-eol))
  11744. (insert " " data)
  11745. (org-set-tags nil 'align)))
  11746. (beginning-of-line 1)
  11747. (if (looking-at ".*?\\([ \t]+\\)$")
  11748. (delete-region (match-beginning 1) (match-end 1))))))
  11749. (defun org-align-all-tags ()
  11750. "Align the tags i all headings."
  11751. (interactive)
  11752. (save-excursion
  11753. (or (ignore-errors (org-back-to-heading t))
  11754. (outline-next-heading))
  11755. (if (org-on-heading-p)
  11756. (org-set-tags t)
  11757. (message "No headings"))))
  11758. (defvar org-indent-indentation-per-level)
  11759. (defun org-set-tags (&optional arg just-align)
  11760. "Set the tags for the current headline.
  11761. With prefix ARG, realign all tags in headings in the current buffer."
  11762. (interactive "P")
  11763. (let* ((re (concat "^" outline-regexp))
  11764. (current (org-get-tags-string))
  11765. (col (current-column))
  11766. (org-setting-tags t)
  11767. table current-tags inherited-tags ; computed below when needed
  11768. tags p0 c0 c1 rpl di tc level)
  11769. (if arg
  11770. (save-excursion
  11771. (goto-char (point-min))
  11772. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  11773. (while (re-search-forward re nil t)
  11774. (org-set-tags nil t)
  11775. (end-of-line 1)))
  11776. (message "All tags realigned to column %d" org-tags-column))
  11777. (if just-align
  11778. (setq tags current)
  11779. ;; Get a new set of tags from the user
  11780. (save-excursion
  11781. (setq table (append org-tag-persistent-alist
  11782. (or org-tag-alist (org-get-buffer-tags))
  11783. (and
  11784. org-complete-tags-always-offer-all-agenda-tags
  11785. (org-global-tags-completion-table
  11786. (org-agenda-files))))
  11787. org-last-tags-completion-table table
  11788. current-tags (org-split-string current ":")
  11789. inherited-tags (nreverse
  11790. (nthcdr (length current-tags)
  11791. (nreverse (org-get-tags-at))))
  11792. tags
  11793. (if (or (eq t org-use-fast-tag-selection)
  11794. (and org-use-fast-tag-selection
  11795. (delq nil (mapcar 'cdr table))))
  11796. (org-fast-tag-selection
  11797. current-tags inherited-tags table
  11798. (if org-fast-tag-selection-include-todo
  11799. org-todo-key-alist))
  11800. (let ((org-add-colon-after-tag-completion t))
  11801. (org-trim
  11802. (org-without-partial-completion
  11803. (org-icompleting-read "Tags: "
  11804. 'org-tags-completion-function
  11805. nil nil current 'org-tags-history)))))))
  11806. (while (string-match "[-+&]+" tags)
  11807. ;; No boolean logic, just a list
  11808. (setq tags (replace-match ":" t t tags))))
  11809. (setq tags (replace-regexp-in-string "[,]" ":" tags))
  11810. (if org-tags-sort-function
  11811. (setq tags (mapconcat 'identity
  11812. (sort (org-split-string
  11813. tags (org-re "[^[:alnum:]_@#%]+"))
  11814. org-tags-sort-function) ":")))
  11815. (if (string-match "\\`[\t ]*\\'" tags)
  11816. (setq tags "")
  11817. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  11818. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  11819. ;; Insert new tags at the correct column
  11820. (beginning-of-line 1)
  11821. (setq level (or (and (looking-at org-outline-regexp)
  11822. (- (match-end 0) (point) 1))
  11823. 1))
  11824. (cond
  11825. ((and (equal current "") (equal tags "")))
  11826. ((re-search-forward
  11827. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  11828. (point-at-eol) t)
  11829. (if (equal tags "")
  11830. (setq rpl "")
  11831. (goto-char (match-beginning 0))
  11832. (setq c0 (current-column)
  11833. ;; compute offset for the case of org-indent-mode active
  11834. di (if org-indent-mode
  11835. (* (1- org-indent-indentation-per-level) (1- level))
  11836. 0)
  11837. p0 (if (equal (char-before) ?*) (1+ (point)) (point))
  11838. tc (+ org-tags-column (if (> org-tags-column 0) (- di) di))
  11839. c1 (max (1+ c0) (if (> tc 0) tc (- (- tc) (length tags))))
  11840. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  11841. (replace-match rpl t t)
  11842. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  11843. tags)
  11844. (t (error "Tags alignment failed")))
  11845. (org-move-to-column col)
  11846. (unless just-align
  11847. (run-hooks 'org-after-tags-change-hook)))))
  11848. (defun org-change-tag-in-region (beg end tag off)
  11849. "Add or remove TAG for each entry in the region.
  11850. This works in the agenda, and also in an org-mode buffer."
  11851. (interactive
  11852. (list (region-beginning) (region-end)
  11853. (let ((org-last-tags-completion-table
  11854. (if (org-mode-p)
  11855. (org-get-buffer-tags)
  11856. (org-global-tags-completion-table))))
  11857. (org-icompleting-read
  11858. "Tag: " 'org-tags-completion-function nil nil nil
  11859. 'org-tags-history))
  11860. (progn
  11861. (message "[s]et or [r]emove? ")
  11862. (equal (read-char-exclusive) ?r))))
  11863. (if (fboundp 'deactivate-mark) (deactivate-mark))
  11864. (let ((agendap (equal major-mode 'org-agenda-mode))
  11865. l1 l2 m buf pos newhead (cnt 0))
  11866. (goto-char end)
  11867. (setq l2 (1- (org-current-line)))
  11868. (goto-char beg)
  11869. (setq l1 (org-current-line))
  11870. (loop for l from l1 to l2 do
  11871. (org-goto-line l)
  11872. (setq m (get-text-property (point) 'org-hd-marker))
  11873. (when (or (and (org-mode-p) (org-on-heading-p))
  11874. (and agendap m))
  11875. (setq buf (if agendap (marker-buffer m) (current-buffer))
  11876. pos (if agendap m (point)))
  11877. (with-current-buffer buf
  11878. (save-excursion
  11879. (save-restriction
  11880. (goto-char pos)
  11881. (setq cnt (1+ cnt))
  11882. (org-toggle-tag tag (if off 'off 'on))
  11883. (setq newhead (org-get-heading)))))
  11884. (and agendap (org-agenda-change-all-lines newhead m))))
  11885. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  11886. (defun org-tags-completion-function (string predicate &optional flag)
  11887. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  11888. (confirm (lambda (x) (stringp (car x)))))
  11889. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  11890. (setq s1 (match-string 1 string)
  11891. s2 (match-string 2 string))
  11892. (setq s1 "" s2 string))
  11893. (cond
  11894. ((eq flag nil)
  11895. ;; try completion
  11896. (setq rtn (try-completion s2 ctable confirm))
  11897. (if (stringp rtn)
  11898. (setq rtn
  11899. (concat s1 s2 (substring rtn (length s2))
  11900. (if (and org-add-colon-after-tag-completion
  11901. (assoc rtn ctable))
  11902. ":" ""))))
  11903. rtn)
  11904. ((eq flag t)
  11905. ;; all-completions
  11906. (all-completions s2 ctable confirm)
  11907. )
  11908. ((eq flag 'lambda)
  11909. ;; exact match?
  11910. (assoc s2 ctable)))
  11911. ))
  11912. (defun org-fast-tag-insert (kwd tags face &optional end)
  11913. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  11914. (insert (format "%-12s" (concat kwd ":"))
  11915. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  11916. (or end "")))
  11917. (defun org-fast-tag-show-exit (flag)
  11918. (save-excursion
  11919. (org-goto-line 3)
  11920. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  11921. (replace-match ""))
  11922. (when flag
  11923. (end-of-line 1)
  11924. (org-move-to-column (- (window-width) 19) t)
  11925. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  11926. (defun org-set-current-tags-overlay (current prefix)
  11927. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  11928. (if (featurep 'xemacs)
  11929. (org-overlay-display org-tags-overlay (concat prefix s)
  11930. 'secondary-selection)
  11931. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  11932. (org-overlay-display org-tags-overlay (concat prefix s)))))
  11933. (defvar org-last-tag-selection-key nil)
  11934. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  11935. "Fast tag selection with single keys.
  11936. CURRENT is the current list of tags in the headline, INHERITED is the
  11937. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  11938. possibly with grouping information. TODO-TABLE is a similar table with
  11939. TODO keywords, should these have keys assigned to them.
  11940. If the keys are nil, a-z are automatically assigned.
  11941. Returns the new tags string, or nil to not change the current settings."
  11942. (let* ((fulltable (append table todo-table))
  11943. (maxlen (apply 'max (mapcar
  11944. (lambda (x)
  11945. (if (stringp (car x)) (string-width (car x)) 0))
  11946. fulltable)))
  11947. (buf (current-buffer))
  11948. (expert (eq org-fast-tag-selection-single-key 'expert))
  11949. (buffer-tags nil)
  11950. (fwidth (+ maxlen 3 1 3))
  11951. (ncol (/ (- (window-width) 4) fwidth))
  11952. (i-face 'org-done)
  11953. (c-face 'org-todo)
  11954. tg cnt e c char c1 c2 ntable tbl rtn
  11955. ov-start ov-end ov-prefix
  11956. (exit-after-next org-fast-tag-selection-single-key)
  11957. (done-keywords org-done-keywords)
  11958. groups ingroup)
  11959. (save-excursion
  11960. (beginning-of-line 1)
  11961. (if (looking-at
  11962. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  11963. (setq ov-start (match-beginning 1)
  11964. ov-end (match-end 1)
  11965. ov-prefix "")
  11966. (setq ov-start (1- (point-at-eol))
  11967. ov-end (1+ ov-start))
  11968. (skip-chars-forward "^\n\r")
  11969. (setq ov-prefix
  11970. (concat
  11971. (buffer-substring (1- (point)) (point))
  11972. (if (> (current-column) org-tags-column)
  11973. " "
  11974. (make-string (- org-tags-column (current-column)) ?\ ))))))
  11975. (move-overlay org-tags-overlay ov-start ov-end)
  11976. (save-window-excursion
  11977. (if expert
  11978. (set-buffer (get-buffer-create " *Org tags*"))
  11979. (delete-other-windows)
  11980. (split-window-vertically)
  11981. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  11982. (erase-buffer)
  11983. (org-set-local 'org-done-keywords done-keywords)
  11984. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  11985. (org-fast-tag-insert "Current" current c-face "\n\n")
  11986. (org-fast-tag-show-exit exit-after-next)
  11987. (org-set-current-tags-overlay current ov-prefix)
  11988. (setq tbl fulltable char ?a cnt 0)
  11989. (while (setq e (pop tbl))
  11990. (cond
  11991. ((equal (car e) :startgroup)
  11992. (push '() groups) (setq ingroup t)
  11993. (when (not (= cnt 0))
  11994. (setq cnt 0)
  11995. (insert "\n"))
  11996. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  11997. ((equal (car e) :endgroup)
  11998. (setq ingroup nil cnt 0)
  11999. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  12000. ((equal e '(:newline))
  12001. (when (not (= cnt 0))
  12002. (setq cnt 0)
  12003. (insert "\n")
  12004. (setq e (car tbl))
  12005. (while (equal (car tbl) '(:newline))
  12006. (insert "\n")
  12007. (setq tbl (cdr tbl)))))
  12008. (t
  12009. (setq tg (copy-sequence (car e)) c2 nil)
  12010. (if (cdr e)
  12011. (setq c (cdr e))
  12012. ;; automatically assign a character.
  12013. (setq c1 (string-to-char
  12014. (downcase (substring
  12015. tg (if (= (string-to-char tg) ?@) 1 0)))))
  12016. (if (or (rassoc c1 ntable) (rassoc c1 table))
  12017. (while (or (rassoc char ntable) (rassoc char table))
  12018. (setq char (1+ char)))
  12019. (setq c2 c1))
  12020. (setq c (or c2 char)))
  12021. (if ingroup (push tg (car groups)))
  12022. (setq tg (org-add-props tg nil 'face
  12023. (cond
  12024. ((not (assoc tg table))
  12025. (org-get-todo-face tg))
  12026. ((member tg current) c-face)
  12027. ((member tg inherited) i-face)
  12028. (t nil))))
  12029. (if (and (= cnt 0) (not ingroup)) (insert " "))
  12030. (insert "[" c "] " tg (make-string
  12031. (- fwidth 4 (length tg)) ?\ ))
  12032. (push (cons tg c) ntable)
  12033. (when (= (setq cnt (1+ cnt)) ncol)
  12034. (insert "\n")
  12035. (if ingroup (insert " "))
  12036. (setq cnt 0)))))
  12037. (setq ntable (nreverse ntable))
  12038. (insert "\n")
  12039. (goto-char (point-min))
  12040. (if (not expert) (org-fit-window-to-buffer))
  12041. (setq rtn
  12042. (catch 'exit
  12043. (while t
  12044. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  12045. (if (not groups) "no " "")
  12046. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  12047. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  12048. (setq org-last-tag-selection-key c)
  12049. (cond
  12050. ((= c ?\r) (throw 'exit t))
  12051. ((= c ?!)
  12052. (setq groups (not groups))
  12053. (goto-char (point-min))
  12054. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  12055. ((= c ?\C-c)
  12056. (if (not expert)
  12057. (org-fast-tag-show-exit
  12058. (setq exit-after-next (not exit-after-next)))
  12059. (setq expert nil)
  12060. (delete-other-windows)
  12061. (set-window-buffer (split-window-vertically) " *Org tags*")
  12062. (org-switch-to-buffer-other-window " *Org tags*")
  12063. (org-fit-window-to-buffer)))
  12064. ((or (= c ?\C-g)
  12065. (and (= c ?q) (not (rassoc c ntable))))
  12066. (org-detach-overlay org-tags-overlay)
  12067. (setq quit-flag t))
  12068. ((= c ?\ )
  12069. (setq current nil)
  12070. (if exit-after-next (setq exit-after-next 'now)))
  12071. ((= c ?\t)
  12072. (condition-case nil
  12073. (setq tg (org-icompleting-read
  12074. "Tag: "
  12075. (or buffer-tags
  12076. (with-current-buffer buf
  12077. (org-get-buffer-tags)))))
  12078. (quit (setq tg "")))
  12079. (when (string-match "\\S-" tg)
  12080. (add-to-list 'buffer-tags (list tg))
  12081. (if (member tg current)
  12082. (setq current (delete tg current))
  12083. (push tg current)))
  12084. (if exit-after-next (setq exit-after-next 'now)))
  12085. ((setq e (rassoc c todo-table) tg (car e))
  12086. (with-current-buffer buf
  12087. (save-excursion (org-todo tg)))
  12088. (if exit-after-next (setq exit-after-next 'now)))
  12089. ((setq e (rassoc c ntable) tg (car e))
  12090. (if (member tg current)
  12091. (setq current (delete tg current))
  12092. (loop for g in groups do
  12093. (if (member tg g)
  12094. (mapc (lambda (x)
  12095. (setq current (delete x current)))
  12096. g)))
  12097. (push tg current))
  12098. (if exit-after-next (setq exit-after-next 'now))))
  12099. ;; Create a sorted list
  12100. (setq current
  12101. (sort current
  12102. (lambda (a b)
  12103. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  12104. (if (eq exit-after-next 'now) (throw 'exit t))
  12105. (goto-char (point-min))
  12106. (beginning-of-line 2)
  12107. (delete-region (point) (point-at-eol))
  12108. (org-fast-tag-insert "Current" current c-face)
  12109. (org-set-current-tags-overlay current ov-prefix)
  12110. (while (re-search-forward
  12111. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  12112. (setq tg (match-string 1))
  12113. (add-text-properties
  12114. (match-beginning 1) (match-end 1)
  12115. (list 'face
  12116. (cond
  12117. ((member tg current) c-face)
  12118. ((member tg inherited) i-face)
  12119. (t (get-text-property (match-beginning 1) 'face))))))
  12120. (goto-char (point-min)))))
  12121. (org-detach-overlay org-tags-overlay)
  12122. (if rtn
  12123. (mapconcat 'identity current ":")
  12124. nil))))
  12125. (defun org-get-tags-string ()
  12126. "Get the TAGS string in the current headline."
  12127. (unless (org-on-heading-p t)
  12128. (error "Not on a heading"))
  12129. (save-excursion
  12130. (beginning-of-line 1)
  12131. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  12132. (org-match-string-no-properties 1)
  12133. "")))
  12134. (defun org-get-tags ()
  12135. "Get the list of tags specified in the current headline."
  12136. (org-split-string (org-get-tags-string) ":"))
  12137. (defun org-get-buffer-tags ()
  12138. "Get a table of all tags used in the buffer, for completion."
  12139. (let (tags)
  12140. (save-excursion
  12141. (goto-char (point-min))
  12142. (while (re-search-forward
  12143. (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t\r\n]") nil t)
  12144. (when (equal (char-after (point-at-bol 0)) ?*)
  12145. (mapc (lambda (x) (add-to-list 'tags x))
  12146. (org-split-string (org-match-string-no-properties 1) ":")))))
  12147. (mapc (lambda (s) (add-to-list 'tags s)) org-file-tags)
  12148. (mapcar 'list tags)))
  12149. ;;;; The mapping API
  12150. ;;;###autoload
  12151. (defun org-map-entries (func &optional match scope &rest skip)
  12152. "Call FUNC at each headline selected by MATCH in SCOPE.
  12153. FUNC is a function or a lisp form. The function will be called without
  12154. arguments, with the cursor positioned at the beginning of the headline.
  12155. The return values of all calls to the function will be collected and
  12156. returned as a list.
  12157. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  12158. does not need to preserve point. After evaluation, the cursor will be
  12159. moved to the end of the line (presumably of the headline of the
  12160. processed entry) and search continues from there. Under some
  12161. circumstances, this may not produce the wanted results. For example,
  12162. if you have removed (e.g. archived) the current (sub)tree it could
  12163. mean that the next entry will be skipped entirely. In such cases, you
  12164. can specify the position from where search should continue by making
  12165. FUNC set the variable `org-map-continue-from' to the desired buffer
  12166. position.
  12167. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  12168. Only headlines that are matched by this query will be considered during
  12169. the iteration. When MATCH is nil or t, all headlines will be
  12170. visited by the iteration.
  12171. SCOPE determines the scope of this command. It can be any of:
  12172. nil The current buffer, respecting the restriction if any
  12173. tree The subtree started with the entry at point
  12174. file The current buffer, without restriction
  12175. file-with-archives
  12176. The current buffer, and any archives associated with it
  12177. agenda All agenda files
  12178. agenda-with-archives
  12179. All agenda files with any archive files associated with them
  12180. \(file1 file2 ...)
  12181. If this is a list, all files in the list will be scanned
  12182. The remaining args are treated as settings for the skipping facilities of
  12183. the scanner. The following items can be given here:
  12184. archive skip trees with the archive tag.
  12185. comment skip trees with the COMMENT keyword
  12186. function or Emacs Lisp form:
  12187. will be used as value for `org-agenda-skip-function', so whenever
  12188. the function returns t, FUNC will not be called for that
  12189. entry and search will continue from the point where the
  12190. function leaves it.
  12191. If your function needs to retrieve the tags including inherited tags
  12192. at the *current* entry, you can use the value of the variable
  12193. `org-scanner-tags' which will be much faster than getting the value
  12194. with `org-get-tags-at'. If your function gets properties with
  12195. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  12196. to t around the call to `org-entry-properties' to get the same speedup.
  12197. Note that if your function moves around to retrieve tags and properties at
  12198. a *different* entry, you cannot use these techniques."
  12199. (let* ((org-agenda-archives-mode nil) ; just to make sure
  12200. (org-agenda-skip-archived-trees (memq 'archive skip))
  12201. (org-agenda-skip-comment-trees (memq 'comment skip))
  12202. (org-agenda-skip-function
  12203. (car (org-delete-all '(comment archive) skip)))
  12204. (org-tags-match-list-sublevels t)
  12205. matcher file res
  12206. org-todo-keywords-for-agenda
  12207. org-done-keywords-for-agenda
  12208. org-todo-keyword-alist-for-agenda
  12209. org-drawers-for-agenda
  12210. org-tag-alist-for-agenda)
  12211. (cond
  12212. ((eq match t) (setq matcher t))
  12213. ((eq match nil) (setq matcher t))
  12214. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  12215. (save-excursion
  12216. (save-restriction
  12217. (when (eq scope 'tree)
  12218. (org-back-to-heading t)
  12219. (org-narrow-to-subtree)
  12220. (setq scope nil))
  12221. (if (not scope)
  12222. (progn
  12223. (org-prepare-agenda-buffers
  12224. (list (buffer-file-name (current-buffer))))
  12225. (setq res (org-scan-tags func matcher)))
  12226. ;; Get the right scope
  12227. (cond
  12228. ((and scope (listp scope) (symbolp (car scope)))
  12229. (setq scope (eval scope)))
  12230. ((eq scope 'agenda)
  12231. (setq scope (org-agenda-files t)))
  12232. ((eq scope 'agenda-with-archives)
  12233. (setq scope (org-agenda-files t))
  12234. (setq scope (org-add-archive-files scope)))
  12235. ((eq scope 'file)
  12236. (setq scope (list (buffer-file-name))))
  12237. ((eq scope 'file-with-archives)
  12238. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  12239. (org-prepare-agenda-buffers scope)
  12240. (while (setq file (pop scope))
  12241. (with-current-buffer (org-find-base-buffer-visiting file)
  12242. (save-excursion
  12243. (save-restriction
  12244. (widen)
  12245. (goto-char (point-min))
  12246. (setq res (append res (org-scan-tags func matcher))))))))))
  12247. res))
  12248. ;;;; Properties
  12249. ;;; Setting and retrieving properties
  12250. (defconst org-special-properties
  12251. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  12252. "TIMESTAMP" "TIMESTAMP_IA" "BLOCKED" "FILE")
  12253. "The special properties valid in Org-mode.
  12254. These are properties that are not defined in the property drawer,
  12255. but in some other way.")
  12256. (defconst org-default-properties
  12257. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  12258. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  12259. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  12260. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  12261. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE"
  12262. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  12263. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  12264. "Some properties that are used by Org-mode for various purposes.
  12265. Being in this list makes sure that they are offered for completion.")
  12266. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  12267. "Regular expression matching the first line of a property drawer.")
  12268. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  12269. "Regular expression matching the last line of a property drawer.")
  12270. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  12271. "Regular expression matching the first line of a property drawer.")
  12272. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  12273. "Regular expression matching the first line of a property drawer.")
  12274. (defconst org-property-drawer-re
  12275. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  12276. org-property-end-re "\\)\n?")
  12277. "Matches an entire property drawer.")
  12278. (defconst org-clock-drawer-re
  12279. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  12280. org-property-end-re "\\)\n?")
  12281. "Matches an entire clock drawer.")
  12282. (defun org-property-action ()
  12283. "Do an action on properties."
  12284. (interactive)
  12285. (let (c)
  12286. (org-at-property-p)
  12287. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  12288. (setq c (read-char-exclusive))
  12289. (cond
  12290. ((equal c ?s)
  12291. (call-interactively 'org-set-property))
  12292. ((equal c ?d)
  12293. (call-interactively 'org-delete-property))
  12294. ((equal c ?D)
  12295. (call-interactively 'org-delete-property-globally))
  12296. ((equal c ?c)
  12297. (call-interactively 'org-compute-property-at-point))
  12298. (t (error "No such property action %c" c)))))
  12299. (defun org-set-effort (&optional value)
  12300. "Set the effort property of the current entry.
  12301. With numerical prefix arg, use the nth allowed value, 0 stands for the 10th
  12302. allowed value."
  12303. (interactive "P")
  12304. (if (equal value 0) (setq value 10))
  12305. (let* ((completion-ignore-case t)
  12306. (prop org-effort-property)
  12307. (cur (org-entry-get nil prop))
  12308. (allowed (org-property-get-allowed-values nil prop 'table))
  12309. (existing (mapcar 'list (org-property-values prop)))
  12310. rpl
  12311. (val (cond
  12312. ((stringp value) value)
  12313. ((and allowed (integerp value))
  12314. (or (car (nth (1- value) allowed))
  12315. (car (org-last allowed))))
  12316. (allowed
  12317. (message "Select 1-9,0, [RET%s]: %s"
  12318. (if cur (concat "=" cur) "")
  12319. (mapconcat 'car allowed " "))
  12320. (setq rpl (read-char-exclusive))
  12321. (if (equal rpl ?\r)
  12322. cur
  12323. (setq rpl (- rpl ?0))
  12324. (if (equal rpl 0) (setq rpl 10))
  12325. (if (and (> rpl 0) (<= rpl (length allowed)))
  12326. (car (nth (1- rpl) allowed))
  12327. (org-completing-read "Effort: " allowed nil))))
  12328. (t
  12329. (let (org-completion-use-ido org-completion-use-iswitchb)
  12330. (org-completing-read
  12331. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  12332. (concat "[" cur "]") "")
  12333. ": ")
  12334. existing nil nil "" nil cur))))))
  12335. (unless (equal (org-entry-get nil prop) val)
  12336. (org-entry-put nil prop val))
  12337. (message "%s is now %s" prop val)))
  12338. (defun org-at-property-p ()
  12339. "Is cursor inside a property drawer?"
  12340. (save-excursion
  12341. (beginning-of-line 1)
  12342. (when (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))
  12343. (save-match-data ;; Used by calling procedures
  12344. (let ((p (point))
  12345. (range (unless (org-before-first-heading-p)
  12346. (org-get-property-block))))
  12347. (and range (<= (car range) p) (< p (cdr range))))))))
  12348. (defun org-get-property-block (&optional beg end force)
  12349. "Return the (beg . end) range of the body of the property drawer.
  12350. BEG and END can be beginning and end of subtree, if not given
  12351. they will be found.
  12352. If the drawer does not exist and FORCE is non-nil, create the drawer."
  12353. (catch 'exit
  12354. (save-excursion
  12355. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  12356. (end (or end (progn (outline-next-heading) (point)))))
  12357. (goto-char beg)
  12358. (if (re-search-forward org-property-start-re end t)
  12359. (setq beg (1+ (match-end 0)))
  12360. (if force
  12361. (save-excursion
  12362. (org-insert-property-drawer)
  12363. (setq end (progn (outline-next-heading) (point))))
  12364. (throw 'exit nil))
  12365. (goto-char beg)
  12366. (if (re-search-forward org-property-start-re end t)
  12367. (setq beg (1+ (match-end 0)))))
  12368. (if (re-search-forward org-property-end-re end t)
  12369. (setq end (match-beginning 0))
  12370. (or force (throw 'exit nil))
  12371. (goto-char beg)
  12372. (setq end beg)
  12373. (org-indent-line-function)
  12374. (insert ":END:\n"))
  12375. (cons beg end)))))
  12376. (defun org-entry-properties (&optional pom which specific)
  12377. "Get all properties of the entry at point-or-marker POM.
  12378. This includes the TODO keyword, the tags, time strings for deadline,
  12379. scheduled, and clocking, and any additional properties defined in the
  12380. entry. The return value is an alist, keys may occur multiple times
  12381. if the property key was used several times.
  12382. POM may also be nil, in which case the current entry is used.
  12383. If WHICH is nil or `all', get all properties. If WHICH is
  12384. `special' or `standard', only get that subclass. If WHICH
  12385. is a string only get exactly this property. SPECIFIC can be a string, the
  12386. specific property we are interested in. Specifying it can speed
  12387. things up because then unnecessary parsing is avoided."
  12388. (setq which (or which 'all))
  12389. (org-with-point-at pom
  12390. (let ((clockstr (substring org-clock-string 0 -1))
  12391. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY" "BLOCKED"))
  12392. (case-fold-search nil)
  12393. beg end range props sum-props key key1 value string clocksum)
  12394. (save-excursion
  12395. (when (condition-case nil
  12396. (and (org-mode-p) (org-back-to-heading t))
  12397. (error nil))
  12398. (setq beg (point))
  12399. (setq sum-props (get-text-property (point) 'org-summaries))
  12400. (setq clocksum (get-text-property (point) :org-clock-minutes))
  12401. (outline-next-heading)
  12402. (setq end (point))
  12403. (when (memq which '(all special))
  12404. ;; Get the special properties, like TODO and tags
  12405. (goto-char beg)
  12406. (when (and (or (not specific) (string= specific "TODO"))
  12407. (looking-at org-todo-line-regexp) (match-end 2))
  12408. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  12409. (when (and (or (not specific) (string= specific "PRIORITY"))
  12410. (looking-at org-priority-regexp))
  12411. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  12412. (when (or (not specific) (string= specific "FILE"))
  12413. (push (cons "FILE" buffer-file-name) props))
  12414. (when (and (or (not specific) (string= specific "TAGS"))
  12415. (setq value (org-get-tags-string))
  12416. (string-match "\\S-" value))
  12417. (push (cons "TAGS" value) props))
  12418. (when (and (or (not specific) (string= specific "ALLTAGS"))
  12419. (setq value (org-get-tags-at)))
  12420. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":")
  12421. ":"))
  12422. props))
  12423. (when (or (not specific) (string= specific "BLOCKED"))
  12424. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props))
  12425. (when (or (not specific)
  12426. (member specific
  12427. '("SCHEDULED" "DEADLINE" "CLOCK" "CLOSED"
  12428. "TIMESTAMP" "TIMESTAMP_IA")))
  12429. (catch 'match
  12430. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  12431. (setq key (if (match-end 1)
  12432. (substring (org-match-string-no-properties 1)
  12433. 0 -1))
  12434. string (if (equal key clockstr)
  12435. (org-no-properties
  12436. (org-trim
  12437. (buffer-substring
  12438. (match-beginning 3) (goto-char
  12439. (point-at-eol)))))
  12440. (substring (org-match-string-no-properties 3)
  12441. 1 -1)))
  12442. ;; Get the correct property name from the key. This is
  12443. ;; necessary if the user has configured time keywords.
  12444. (setq key1 (concat key ":"))
  12445. (cond
  12446. ((not key)
  12447. (setq key
  12448. (if (= (char-after (match-beginning 3)) ?\[)
  12449. "TIMESTAMP_IA" "TIMESTAMP")))
  12450. ((equal key1 org-scheduled-string) (setq key "SCHEDULED"))
  12451. ((equal key1 org-deadline-string) (setq key "DEADLINE"))
  12452. ((equal key1 org-closed-string) (setq key "CLOSED"))
  12453. ((equal key1 org-clock-string) (setq key "CLOCK")))
  12454. (if (and specific (equal key specific) (not (equal key "CLOCK")))
  12455. (progn
  12456. (push (cons key string) props)
  12457. ;; no need to search further if match is found
  12458. (throw 'match t))
  12459. (when (or (equal key "CLOCK") (not (assoc key props)))
  12460. (push (cons key string) props))))))
  12461. )
  12462. (when (memq which '(all standard))
  12463. ;; Get the standard properties, like :PROP: ...
  12464. (setq range (org-get-property-block beg end))
  12465. (when range
  12466. (goto-char (car range))
  12467. (while (re-search-forward
  12468. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  12469. (cdr range) t)
  12470. (setq key (org-match-string-no-properties 1)
  12471. value (org-trim (or (org-match-string-no-properties 2) "")))
  12472. (unless (member key excluded)
  12473. (push (cons key (or value "")) props)))))
  12474. (if clocksum
  12475. (push (cons "CLOCKSUM"
  12476. (org-columns-number-to-string (/ (float clocksum) 60.)
  12477. 'add_times))
  12478. props))
  12479. (unless (assoc "CATEGORY" props)
  12480. (push (cons "CATEGORY" (org-get-category)) props))
  12481. (append sum-props (nreverse props)))))))
  12482. (defun org-entry-get (pom property &optional inherit literal-nil)
  12483. "Get value of PROPERTY for entry at point-or-marker POM.
  12484. If INHERIT is non-nil and the entry does not have the property,
  12485. then also check higher levels of the hierarchy.
  12486. If INHERIT is the symbol `selective', use inheritance only if the setting
  12487. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  12488. If the property is present but empty, the return value is the empty string.
  12489. If the property is not present at all, nil is returned.
  12490. If LITERAL-NIL is set, return the string value \"nil\" as a string,
  12491. do not interpret it as the list atom nil. This is used for inheritance
  12492. when a \"nil\" value can supersede a non-nil value higher up the hierarchy."
  12493. (org-with-point-at pom
  12494. (if (and inherit (if (eq inherit 'selective)
  12495. (org-property-inherit-p property)
  12496. t))
  12497. (org-entry-get-with-inheritance property literal-nil)
  12498. (if (member property org-special-properties)
  12499. ;; We need a special property. Use `org-entry-properties' to
  12500. ;; retrieve it, but specify the wanted property
  12501. (cdr (assoc property (org-entry-properties nil 'special property)))
  12502. (let ((range (unless (org-before-first-heading-p)
  12503. (org-get-property-block))))
  12504. (if (and range
  12505. (goto-char (car range))
  12506. (re-search-forward
  12507. (org-re-property property)
  12508. (cdr range) t))
  12509. ;; Found the property, return it.
  12510. (if (match-end 1)
  12511. (if literal-nil
  12512. (org-match-string-no-properties 1)
  12513. (org-not-nil (org-match-string-no-properties 1)))
  12514. "")))))))
  12515. (defun org-property-or-variable-value (var &optional inherit)
  12516. "Check if there is a property fixing the value of VAR.
  12517. If yes, return this value. If not, return the current value of the variable."
  12518. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  12519. (if (and prop (stringp prop) (string-match "\\S-" prop))
  12520. (read prop)
  12521. (symbol-value var))))
  12522. (defun org-entry-delete (pom property)
  12523. "Delete the property PROPERTY from entry at point-or-marker POM."
  12524. (org-with-point-at pom
  12525. (if (member property org-special-properties)
  12526. nil ; cannot delete these properties.
  12527. (let ((range (org-get-property-block)))
  12528. (if (and range
  12529. (goto-char (car range))
  12530. (re-search-forward
  12531. (org-re-property property)
  12532. (cdr range) t))
  12533. (progn
  12534. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  12535. t)
  12536. nil)))))
  12537. ;; Multi-values properties are properties that contain multiple values
  12538. ;; These values are assumed to be single words, separated by whitespace.
  12539. (defun org-entry-add-to-multivalued-property (pom property value)
  12540. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  12541. (let* ((old (org-entry-get pom property))
  12542. (values (and old (org-split-string old "[ \t]"))))
  12543. (setq value (org-entry-protect-space value))
  12544. (unless (member value values)
  12545. (setq values (cons value values))
  12546. (org-entry-put pom property
  12547. (mapconcat 'identity values " ")))))
  12548. (defun org-entry-remove-from-multivalued-property (pom property value)
  12549. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  12550. (let* ((old (org-entry-get pom property))
  12551. (values (and old (org-split-string old "[ \t]"))))
  12552. (setq value (org-entry-protect-space value))
  12553. (when (member value values)
  12554. (setq values (delete value values))
  12555. (org-entry-put pom property
  12556. (mapconcat 'identity values " ")))))
  12557. (defun org-entry-member-in-multivalued-property (pom property value)
  12558. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  12559. (let* ((old (org-entry-get pom property))
  12560. (values (and old (org-split-string old "[ \t]"))))
  12561. (setq value (org-entry-protect-space value))
  12562. (member value values)))
  12563. (defun org-entry-get-multivalued-property (pom property)
  12564. "Return a list of values in a multivalued property."
  12565. (let* ((value (org-entry-get pom property))
  12566. (values (and value (org-split-string value "[ \t]"))))
  12567. (mapcar 'org-entry-restore-space values)))
  12568. (defun org-entry-put-multivalued-property (pom property &rest values)
  12569. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  12570. VALUES should be a list of strings. Spaces will be protected."
  12571. (org-entry-put pom property
  12572. (mapconcat 'org-entry-protect-space values " "))
  12573. (let* ((value (org-entry-get pom property))
  12574. (values (and value (org-split-string value "[ \t]"))))
  12575. (mapcar 'org-entry-restore-space values)))
  12576. (defun org-entry-protect-space (s)
  12577. "Protect spaces and newline in string S."
  12578. (while (string-match " " s)
  12579. (setq s (replace-match "%20" t t s)))
  12580. (while (string-match "\n" s)
  12581. (setq s (replace-match "%0A" t t s)))
  12582. s)
  12583. (defun org-entry-restore-space (s)
  12584. "Restore spaces and newline in string S."
  12585. (while (string-match "%20" s)
  12586. (setq s (replace-match " " t t s)))
  12587. (while (string-match "%0A" s)
  12588. (setq s (replace-match "\n" t t s)))
  12589. s)
  12590. (defvar org-entry-property-inherited-from (make-marker)
  12591. "Marker pointing to the entry from where a property was inherited.
  12592. Each call to `org-entry-get-with-inheritance' will set this marker to the
  12593. location of the entry where the inheritance search matched. If there was
  12594. no match, the marker will point nowhere.
  12595. Note that also `org-entry-get' calls this function, if the INHERIT flag
  12596. is set.")
  12597. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  12598. "Get entry property, and search higher levels if not present.
  12599. The search will stop at the first ancestor which has the property defined.
  12600. If the value found is \"nil\", return nil to show that the property
  12601. should be considered as undefined (this is the meaning of nil here).
  12602. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  12603. (move-marker org-entry-property-inherited-from nil)
  12604. (let (tmp)
  12605. (unless (org-before-first-heading-p)
  12606. (save-excursion
  12607. (save-restriction
  12608. (widen)
  12609. (catch 'ex
  12610. (while t
  12611. (when (setq tmp (org-entry-get nil property nil 'literal-nil))
  12612. (org-back-to-heading t)
  12613. (move-marker org-entry-property-inherited-from (point))
  12614. (throw 'ex tmp))
  12615. (or (org-up-heading-safe) (throw 'ex nil)))))))
  12616. (setq tmp (or tmp
  12617. (cdr (assoc property org-file-properties))
  12618. (cdr (assoc property org-global-properties))
  12619. (cdr (assoc property org-global-properties-fixed))))
  12620. (if literal-nil tmp (org-not-nil tmp))))
  12621. (defvar org-property-changed-functions nil
  12622. "Hook called when the value of a property has changed.
  12623. Each hook function should accept two arguments, the name of the property
  12624. and the new value.")
  12625. (defun org-entry-put (pom property value)
  12626. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  12627. (org-with-point-at pom
  12628. (org-back-to-heading t)
  12629. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  12630. range)
  12631. (cond
  12632. ((equal property "TODO")
  12633. (when (and (stringp value) (string-match "\\S-" value)
  12634. (not (member value org-todo-keywords-1)))
  12635. (error "\"%s\" is not a valid TODO state" value))
  12636. (if (or (not value)
  12637. (not (string-match "\\S-" value)))
  12638. (setq value 'none))
  12639. (org-todo value)
  12640. (org-set-tags nil 'align))
  12641. ((equal property "PRIORITY")
  12642. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  12643. (string-to-char value) ?\ ))
  12644. (org-set-tags nil 'align))
  12645. ((equal property "SCHEDULED")
  12646. (if (re-search-forward org-scheduled-time-regexp end t)
  12647. (cond
  12648. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12649. ((eq value 'later) (org-timestamp-change 1 'day))
  12650. (t (call-interactively 'org-schedule)))
  12651. (call-interactively 'org-schedule)))
  12652. ((equal property "DEADLINE")
  12653. (if (re-search-forward org-deadline-time-regexp end t)
  12654. (cond
  12655. ((eq value 'earlier) (org-timestamp-change -1 'day))
  12656. ((eq value 'later) (org-timestamp-change 1 'day))
  12657. (t (call-interactively 'org-deadline)))
  12658. (call-interactively 'org-deadline)))
  12659. ((member property org-special-properties)
  12660. (error "The %s property can not yet be set with `org-entry-put'"
  12661. property))
  12662. (t ; a non-special property
  12663. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  12664. (setq range (org-get-property-block beg end 'force))
  12665. (goto-char (car range))
  12666. (if (re-search-forward
  12667. (org-re-property property) (cdr range) t)
  12668. (progn
  12669. (delete-region (match-beginning 1) (match-end 1))
  12670. (goto-char (match-beginning 1)))
  12671. (goto-char (cdr range))
  12672. (insert "\n")
  12673. (backward-char 1)
  12674. (org-indent-line-function)
  12675. (insert ":" property ":"))
  12676. (and value (insert " " value))
  12677. (org-indent-line-function)))))
  12678. (run-hook-with-args 'org-property-changed-functions property value)))
  12679. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  12680. "Get all property keys in the current buffer.
  12681. With INCLUDE-SPECIALS, also list the special properties that reflect things
  12682. like tags and TODO state.
  12683. With INCLUDE-DEFAULTS, also include properties that has special meaning
  12684. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING
  12685. and others.
  12686. With INCLUDE-COLUMNS, also include property names given in COLUMN
  12687. formats in the current buffer."
  12688. (let (rtn range cfmt s p)
  12689. (save-excursion
  12690. (save-restriction
  12691. (widen)
  12692. (goto-char (point-min))
  12693. (while (re-search-forward org-property-start-re nil t)
  12694. (setq range (org-get-property-block))
  12695. (goto-char (car range))
  12696. (while (re-search-forward
  12697. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  12698. (cdr range) t)
  12699. (add-to-list 'rtn (org-match-string-no-properties 1)))
  12700. (outline-next-heading))))
  12701. (when include-specials
  12702. (setq rtn (append org-special-properties rtn)))
  12703. (when include-defaults
  12704. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties)
  12705. (add-to-list 'rtn org-effort-property))
  12706. (when include-columns
  12707. (save-excursion
  12708. (save-restriction
  12709. (widen)
  12710. (goto-char (point-min))
  12711. (while (re-search-forward
  12712. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  12713. nil t)
  12714. (setq cfmt (match-string 2) s 0)
  12715. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  12716. cfmt s)
  12717. (setq s (match-end 0)
  12718. p (match-string 1 cfmt))
  12719. (unless (or (equal p "ITEM")
  12720. (member p org-special-properties))
  12721. (add-to-list 'rtn (match-string 1 cfmt))))))))
  12722. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  12723. (defsubst org-re-property (property)
  12724. "Return a regexp matching PROPERTY.
  12725. Match group 1 will be set to the value "
  12726. (concat "^[ \t]*:" (regexp-quote property) ":[ \t]*\\(\\S-.*\\)"))
  12727. (defun org-property-values (key)
  12728. "Return a list of all values of property KEY in the current buffer."
  12729. (save-excursion
  12730. (save-restriction
  12731. (widen)
  12732. (goto-char (point-min))
  12733. (let ((re (org-re-property key))
  12734. values)
  12735. (while (re-search-forward re nil t)
  12736. (add-to-list 'values (org-trim (match-string 1))))
  12737. (delete "" values)))))
  12738. (defun org-insert-property-drawer ()
  12739. "Insert a property drawer into the current entry."
  12740. (interactive)
  12741. (org-back-to-heading t)
  12742. (looking-at outline-regexp)
  12743. (let ((indent (if org-adapt-indentation
  12744. (- (match-end 0)(match-beginning 0))
  12745. 0))
  12746. (beg (point))
  12747. (re (concat "^[ \t]*" org-keyword-time-regexp))
  12748. end hiddenp)
  12749. (outline-next-heading)
  12750. (setq end (point))
  12751. (goto-char beg)
  12752. (while (re-search-forward re end t))
  12753. (setq hiddenp (outline-invisible-p))
  12754. (end-of-line 1)
  12755. (and (equal (char-after) ?\n) (forward-char 1))
  12756. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  12757. (if (member (match-string 1) '("CLOCK:" ":END:"))
  12758. ;; just skip this line
  12759. (beginning-of-line 2)
  12760. ;; Drawer start, find the end
  12761. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  12762. (beginning-of-line 1)))
  12763. (org-skip-over-state-notes)
  12764. (skip-chars-backward " \t\n\r")
  12765. (if (eq (char-before) ?*) (forward-char 1))
  12766. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  12767. (beginning-of-line 0)
  12768. (org-indent-to-column indent)
  12769. (beginning-of-line 2)
  12770. (org-indent-to-column indent)
  12771. (beginning-of-line 0)
  12772. (if hiddenp
  12773. (save-excursion
  12774. (org-back-to-heading t)
  12775. (hide-entry))
  12776. (org-flag-drawer t))))
  12777. (defvar org-property-set-functions-alist nil
  12778. "Property set function alist.
  12779. Each entry should have the following format:
  12780. (PROPERTY . READ-FUNCTION)
  12781. The read function will be called with the same argument as
  12782. `org-completing-read'.")
  12783. (defun org-set-property-function (property)
  12784. "Get the function that should be used to set PROPERTY.
  12785. This is computed according to `org-property-set-functions-alist'."
  12786. (or (cdr (assoc property org-property-set-functions-alist))
  12787. 'org-completing-read))
  12788. (defun org-read-property-value (property)
  12789. "Read PROPERTY value from user."
  12790. (let* ((completion-ignore-case t)
  12791. (allowed (org-property-get-allowed-values nil property 'table))
  12792. (cur (org-entry-get nil property))
  12793. (prompt (concat property " value"
  12794. (if (and cur (string-match "\\S-" cur))
  12795. (concat " [" cur "]") "") ": "))
  12796. (set-function (org-set-property-function property))
  12797. (val (if allowed
  12798. (funcall set-function prompt allowed nil
  12799. (not (get-text-property 0 'org-unrestricted
  12800. (caar allowed))))
  12801. (let (org-completion-use-ido org-completion-use-iswitchb)
  12802. (funcall set-function prompt
  12803. (mapcar 'list (org-property-values property))
  12804. nil nil "" nil cur)))))
  12805. (if (equal val "")
  12806. cur
  12807. val)))
  12808. (defun org-read-property-name ()
  12809. "Read a property name."
  12810. (let* ((completion-ignore-case t)
  12811. (keys (org-buffer-property-keys nil t t))
  12812. (property (org-icompleting-read "Property: " (mapcar 'list keys))))
  12813. (if (member property keys)
  12814. property
  12815. (or (cdr (assoc (downcase property)
  12816. (mapcar (lambda (x) (cons (downcase x) x))
  12817. keys)))
  12818. property))))
  12819. (defun org-set-property (property value)
  12820. "In the current entry, set PROPERTY to VALUE.
  12821. When called interactively, this will prompt for a property name, offering
  12822. completion on existing and default properties. And then it will prompt
  12823. for a value, offering completion either on allowed values (via an inherited
  12824. xxx_ALL property) or on existing values in other instances of this property
  12825. in the current file."
  12826. (interactive (list nil nil))
  12827. (let* ((property (or property (org-read-property-name)))
  12828. (value (or value (org-read-property-value property))))
  12829. (unless (equal (org-entry-get nil property) value)
  12830. (org-entry-put nil property value))))
  12831. (defun org-delete-property (property)
  12832. "In the current entry, delete PROPERTY."
  12833. (interactive
  12834. (let* ((completion-ignore-case t)
  12835. (prop (org-icompleting-read "Property: "
  12836. (org-entry-properties nil 'standard))))
  12837. (list prop)))
  12838. (message "Property %s %s" property
  12839. (if (org-entry-delete nil property)
  12840. "deleted"
  12841. "was not present in the entry")))
  12842. (defun org-delete-property-globally (property)
  12843. "Remove PROPERTY globally, from all entries."
  12844. (interactive
  12845. (let* ((completion-ignore-case t)
  12846. (prop (org-icompleting-read
  12847. "Globally remove property: "
  12848. (mapcar 'list (org-buffer-property-keys)))))
  12849. (list prop)))
  12850. (save-excursion
  12851. (save-restriction
  12852. (widen)
  12853. (goto-char (point-min))
  12854. (let ((cnt 0))
  12855. (while (re-search-forward
  12856. (org-re-property property)
  12857. nil t)
  12858. (setq cnt (1+ cnt))
  12859. (replace-match ""))
  12860. (message "Property \"%s\" removed from %d entries" property cnt)))))
  12861. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  12862. (defun org-compute-property-at-point ()
  12863. "Compute the property at point.
  12864. This looks for an enclosing column format, extracts the operator and
  12865. then applies it to the property in the column format's scope."
  12866. (interactive)
  12867. (unless (org-at-property-p)
  12868. (error "Not at a property"))
  12869. (let ((prop (org-match-string-no-properties 2)))
  12870. (org-columns-get-format-and-top-level)
  12871. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  12872. (error "No operator defined for property %s" prop))
  12873. (org-columns-compute prop)))
  12874. (defvar org-property-allowed-value-functions nil
  12875. "Hook for functions supplying allowed values for a specific property.
  12876. The functions must take a single argument, the name of the property, and
  12877. return a flat list of allowed values. If \":ETC\" is one of
  12878. the values, this means that these values are intended as defaults for
  12879. completion, but that other values should be allowed too.
  12880. The functions must return nil if they are not responsible for this
  12881. property.")
  12882. (defun org-property-get-allowed-values (pom property &optional table)
  12883. "Get allowed values for the property PROPERTY.
  12884. When TABLE is non-nil, return an alist that can directly be used for
  12885. completion."
  12886. (let (vals)
  12887. (cond
  12888. ((equal property "TODO")
  12889. (setq vals (org-with-point-at pom
  12890. (append org-todo-keywords-1 '("")))))
  12891. ((equal property "PRIORITY")
  12892. (let ((n org-lowest-priority))
  12893. (while (>= n org-highest-priority)
  12894. (push (char-to-string n) vals)
  12895. (setq n (1- n)))))
  12896. ((member property org-special-properties))
  12897. ((setq vals (run-hook-with-args-until-success
  12898. 'org-property-allowed-value-functions property)))
  12899. (t
  12900. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  12901. (when (and vals (string-match "\\S-" vals))
  12902. (setq vals (car (read-from-string (concat "(" vals ")"))))
  12903. (setq vals (mapcar (lambda (x)
  12904. (cond ((stringp x) x)
  12905. ((numberp x) (number-to-string x))
  12906. ((symbolp x) (symbol-name x))
  12907. (t "???")))
  12908. vals)))))
  12909. (when (member ":ETC" vals)
  12910. (setq vals (remove ":ETC" vals))
  12911. (org-add-props (car vals) '(org-unrestricted t)))
  12912. (if table (mapcar 'list vals) vals)))
  12913. (defun org-property-previous-allowed-value (&optional previous)
  12914. "Switch to the next allowed value for this property."
  12915. (interactive)
  12916. (org-property-next-allowed-value t))
  12917. (defun org-property-next-allowed-value (&optional previous)
  12918. "Switch to the next allowed value for this property."
  12919. (interactive)
  12920. (unless (org-at-property-p)
  12921. (error "Not at a property"))
  12922. (let* ((key (match-string 2))
  12923. (value (match-string 3))
  12924. (allowed (or (org-property-get-allowed-values (point) key)
  12925. (and (member value '("[ ]" "[-]" "[X]"))
  12926. '("[ ]" "[X]"))))
  12927. nval)
  12928. (unless allowed
  12929. (error "Allowed values for this property have not been defined"))
  12930. (if previous (setq allowed (reverse allowed)))
  12931. (if (member value allowed)
  12932. (setq nval (car (cdr (member value allowed)))))
  12933. (setq nval (or nval (car allowed)))
  12934. (if (equal nval value)
  12935. (error "Only one allowed value for this property"))
  12936. (org-at-property-p)
  12937. (replace-match (concat " :" key ": " nval) t t)
  12938. (org-indent-line-function)
  12939. (beginning-of-line 1)
  12940. (skip-chars-forward " \t")
  12941. (run-hook-with-args 'org-property-changed-functions key nval)))
  12942. (defun org-find-olp (path &optional this-buffer)
  12943. "Return a marker pointing to the entry at outline path OLP.
  12944. If anything goes wrong, throw an error.
  12945. You can wrap this call to catch the error like this:
  12946. (condition-case msg
  12947. (org-mobile-locate-entry (match-string 4))
  12948. (error (nth 1 msg)))
  12949. The return value will then be either a string with the error message,
  12950. or a marker if everything is OK.
  12951. If THIS-BUFFER is set, the outline path does not contain a file,
  12952. only headings."
  12953. (let* ((file (if this-buffer buffer-file-name (pop path)))
  12954. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  12955. (level 1)
  12956. (lmin 1)
  12957. (lmax 1)
  12958. limit re end found pos heading cnt flevel)
  12959. (unless buffer (error "File not found :%s" file))
  12960. (with-current-buffer buffer
  12961. (save-excursion
  12962. (save-restriction
  12963. (widen)
  12964. (setq limit (point-max))
  12965. (goto-char (point-min))
  12966. (while (setq heading (pop path))
  12967. (setq re (format org-complex-heading-regexp-format
  12968. (regexp-quote heading)))
  12969. (setq cnt 0 pos (point))
  12970. (while (re-search-forward re end t)
  12971. (setq level (- (match-end 1) (match-beginning 1)))
  12972. (if (and (>= level lmin) (<= level lmax))
  12973. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  12974. (when (= cnt 0) (error "Heading not found on level %d: %s"
  12975. lmax heading))
  12976. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  12977. lmax heading))
  12978. (goto-char found)
  12979. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  12980. (setq end (save-excursion (org-end-of-subtree t t))))
  12981. (when (org-on-heading-p)
  12982. (move-marker (make-marker) (point))))))))
  12983. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  12984. "Find node HEADING in BUFFER.
  12985. Return a marker to the heading if it was found, or nil if not.
  12986. If POS-ONLY is set, return just the position instead of a marker.
  12987. The heading text must match exact, but it may have a TODO keyword,
  12988. a priority cookie and tags in the standard locations."
  12989. (with-current-buffer (or buffer (current-buffer))
  12990. (save-excursion
  12991. (save-restriction
  12992. (widen)
  12993. (goto-char (point-min))
  12994. (let (case-fold-search)
  12995. (if (re-search-forward
  12996. (format org-complex-heading-regexp-format
  12997. (regexp-quote heading)) nil t)
  12998. (if pos-only
  12999. (match-beginning 0)
  13000. (move-marker (make-marker) (match-beginning 0)))))))))
  13001. (defun org-find-exact-heading-in-directory (heading &optional dir)
  13002. "Find Org node headline HEADING in all .org files in directory DIR.
  13003. When the target headline is found, return a marker to this location."
  13004. (let ((files (directory-files (or dir default-directory)
  13005. nil "\\`[^.#].*\\.org\\'"))
  13006. file visiting m buffer)
  13007. (catch 'found
  13008. (while (setq file (pop files))
  13009. (message "trying %s" file)
  13010. (setq visiting (org-find-base-buffer-visiting file))
  13011. (setq buffer (or visiting (find-file-noselect file)))
  13012. (setq m (org-find-exact-headline-in-buffer
  13013. heading buffer))
  13014. (when (and (not m) (not visiting)) (kill-buffer buffer))
  13015. (and m (throw 'found m))))))
  13016. (defun org-find-entry-with-id (ident)
  13017. "Locate the entry that contains the ID property with exact value IDENT.
  13018. IDENT can be a string, a symbol or a number, this function will search for
  13019. the string representation of it.
  13020. Return the position where this entry starts, or nil if there is no such entry."
  13021. (interactive "sID: ")
  13022. (let ((id (cond
  13023. ((stringp ident) ident)
  13024. ((symbol-name ident) (symbol-name ident))
  13025. ((numberp ident) (number-to-string ident))
  13026. (t (error "IDENT %s must be a string, symbol or number" ident))))
  13027. (case-fold-search nil))
  13028. (save-excursion
  13029. (save-restriction
  13030. (widen)
  13031. (goto-char (point-min))
  13032. (when (re-search-forward
  13033. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  13034. nil t)
  13035. (org-back-to-heading t)
  13036. (point))))))
  13037. ;;;; Timestamps
  13038. (defvar org-last-changed-timestamp nil)
  13039. (defvar org-last-inserted-timestamp nil
  13040. "The last time stamp inserted with `org-insert-time-stamp'.")
  13041. (defvar org-time-was-given) ; dynamically scoped parameter
  13042. (defvar org-end-time-was-given) ; dynamically scoped parameter
  13043. (defvar org-ts-what) ; dynamically scoped parameter
  13044. (defun org-time-stamp (arg &optional inactive)
  13045. "Prompt for a date/time and insert a time stamp.
  13046. If the user specifies a time like HH:MM, or if this command is called
  13047. with a prefix argument, the time stamp will contain date and time.
  13048. Otherwise, only the date will be included. All parts of a date not
  13049. specified by the user will be filled in from the current date/time.
  13050. So if you press just return without typing anything, the time stamp
  13051. will represent the current date/time. If there is already a timestamp
  13052. at the cursor, it will be modified."
  13053. (interactive "P")
  13054. (let* ((ts nil)
  13055. (default-time
  13056. ;; Default time is either today, or, when entering a range,
  13057. ;; the range start.
  13058. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  13059. (save-excursion
  13060. (re-search-backward
  13061. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  13062. (- (point) 20) t)))
  13063. (apply 'encode-time (org-parse-time-string (match-string 1)))
  13064. (current-time)))
  13065. (default-input (and ts (org-get-compact-tod ts)))
  13066. org-time-was-given org-end-time-was-given time)
  13067. (cond
  13068. ((and (org-at-timestamp-p t)
  13069. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  13070. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  13071. (insert "--")
  13072. (setq time (let ((this-command this-command))
  13073. (org-read-date arg 'totime nil nil
  13074. default-time default-input)))
  13075. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  13076. ((org-at-timestamp-p t)
  13077. (setq time (let ((this-command this-command))
  13078. (org-read-date arg 'totime nil nil default-time default-input)))
  13079. (when (org-at-timestamp-p t) ; just to get the match data
  13080. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  13081. (replace-match "")
  13082. (setq org-last-changed-timestamp
  13083. (org-insert-time-stamp
  13084. time (or org-time-was-given arg)
  13085. inactive nil nil (list org-end-time-was-given))))
  13086. (message "Timestamp updated"))
  13087. (t
  13088. (setq time (let ((this-command this-command))
  13089. (org-read-date arg 'totime nil nil default-time default-input)))
  13090. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  13091. nil nil (list org-end-time-was-given))))))
  13092. ;; FIXME: can we use this for something else, like computing time differences?
  13093. (defun org-get-compact-tod (s)
  13094. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  13095. (let* ((t1 (match-string 1 s))
  13096. (h1 (string-to-number (match-string 2 s)))
  13097. (m1 (string-to-number (match-string 3 s)))
  13098. (t2 (and (match-end 4) (match-string 5 s)))
  13099. (h2 (and t2 (string-to-number (match-string 6 s))))
  13100. (m2 (and t2 (string-to-number (match-string 7 s))))
  13101. dh dm)
  13102. (if (not t2)
  13103. t1
  13104. (setq dh (- h2 h1) dm (- m2 m1))
  13105. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  13106. (concat t1 "+" (number-to-string dh)
  13107. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  13108. (defun org-time-stamp-inactive (&optional arg)
  13109. "Insert an inactive time stamp.
  13110. An inactive time stamp is enclosed in square brackets instead of angle
  13111. brackets. It is inactive in the sense that it does not trigger agenda entries,
  13112. does not link to the calendar and cannot be changed with the S-cursor keys.
  13113. So these are more for recording a certain time/date."
  13114. (interactive "P")
  13115. (org-time-stamp arg 'inactive))
  13116. (defvar org-date-ovl (make-overlay 1 1))
  13117. (overlay-put org-date-ovl 'face 'org-warning)
  13118. (org-detach-overlay org-date-ovl)
  13119. (defvar org-ans1) ; dynamically scoped parameter
  13120. (defvar org-ans2) ; dynamically scoped parameter
  13121. (defvar org-plain-time-of-day-regexp) ; defined below
  13122. (defvar org-overriding-default-time nil) ; dynamically scoped
  13123. (defvar org-read-date-overlay nil)
  13124. (defvar org-dcst nil) ; dynamically scoped
  13125. (defvar org-read-date-history nil)
  13126. (defvar org-read-date-final-answer nil)
  13127. (defvar org-read-date-analyze-futurep nil)
  13128. (defvar org-read-date-analyze-forced-year nil)
  13129. (defun org-read-date (&optional with-time to-time from-string prompt
  13130. default-time default-input)
  13131. "Read a date, possibly a time, and make things smooth for the user.
  13132. The prompt will suggest to enter an ISO date, but you can also enter anything
  13133. which will at least partially be understood by `parse-time-string'.
  13134. Unrecognized parts of the date will default to the current day, month, year,
  13135. hour and minute. If this command is called to replace a timestamp at point,
  13136. of to enter the second timestamp of a range, the default time is taken
  13137. from the existing stamp. Furthermore, the command prefers the future,
  13138. so if you are giving a date where the year is not given, and the day-month
  13139. combination is already past in the current year, it will assume you
  13140. mean next year. For details, see the manual. A few examples:
  13141. 3-2-5 --> 2003-02-05
  13142. feb 15 --> currentyear-02-15
  13143. 2/15 --> currentyear-02-15
  13144. sep 12 9 --> 2009-09-12
  13145. 12:45 --> today 12:45
  13146. 22 sept 0:34 --> currentyear-09-22 0:34
  13147. 12 --> currentyear-currentmonth-12
  13148. Fri --> nearest Friday (today or later)
  13149. etc.
  13150. Furthermore you can specify a relative date by giving, as the *first* thing
  13151. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  13152. change in days weeks, months, years.
  13153. With a single plus or minus, the date is relative to today. With a double
  13154. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  13155. +4d --> four days from today
  13156. +4 --> same as above
  13157. +2w --> two weeks from today
  13158. ++5 --> five days from default date
  13159. The function understands only English month and weekday abbreviations,
  13160. but this can be configured with the variables `parse-time-months' and
  13161. `parse-time-weekdays'.
  13162. While prompting, a calendar is popped up - you can also select the
  13163. date with the mouse (button 1). The calendar shows a period of three
  13164. months. To scroll it to other months, use the keys `>' and `<'.
  13165. If you don't like the calendar, turn it off with
  13166. \(setq org-read-date-popup-calendar nil)
  13167. With optional argument TO-TIME, the date will immediately be converted
  13168. to an internal time.
  13169. With an optional argument WITH-TIME, the prompt will suggest to also
  13170. insert a time. Note that when WITH-TIME is not set, you can still
  13171. enter a time, and this function will inform the calling routine about
  13172. this change. The calling routine may then choose to change the format
  13173. used to insert the time stamp into the buffer to include the time.
  13174. With optional argument FROM-STRING, read from this string instead from
  13175. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  13176. the time/date that is used for everything that is not specified by the
  13177. user."
  13178. (require 'parse-time)
  13179. (let* ((org-time-stamp-rounding-minutes
  13180. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  13181. (org-dcst org-display-custom-times)
  13182. (ct (org-current-time))
  13183. (def (or org-overriding-default-time default-time ct))
  13184. (defdecode (decode-time def))
  13185. (dummy (progn
  13186. (when (< (nth 2 defdecode) org-extend-today-until)
  13187. (setcar (nthcdr 2 defdecode) -1)
  13188. (setcar (nthcdr 1 defdecode) 59)
  13189. (setq def (apply 'encode-time defdecode)
  13190. defdecode (decode-time def)))))
  13191. (calendar-frame-setup nil)
  13192. (calendar-setup nil)
  13193. (calendar-move-hook nil)
  13194. (calendar-view-diary-initially-flag nil)
  13195. (calendar-view-holidays-initially-flag nil)
  13196. (timestr (format-time-string
  13197. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  13198. (prompt (concat (if prompt (concat prompt " ") "")
  13199. (format "Date+time [%s]: " timestr)))
  13200. ans (org-ans0 "") org-ans1 org-ans2 final)
  13201. (cond
  13202. (from-string (setq ans from-string))
  13203. (org-read-date-popup-calendar
  13204. (save-excursion
  13205. (save-window-excursion
  13206. (calendar)
  13207. (calendar-forward-day (- (time-to-days def)
  13208. (calendar-absolute-from-gregorian
  13209. (calendar-current-date))))
  13210. (org-eval-in-calendar nil t)
  13211. (let* ((old-map (current-local-map))
  13212. (map (copy-keymap calendar-mode-map))
  13213. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  13214. (org-defkey map (kbd "RET") 'org-calendar-select)
  13215. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  13216. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  13217. (org-defkey minibuffer-local-map [(meta shift left)]
  13218. (lambda () (interactive)
  13219. (org-eval-in-calendar '(calendar-backward-month 1))))
  13220. (org-defkey minibuffer-local-map [(meta shift right)]
  13221. (lambda () (interactive)
  13222. (org-eval-in-calendar '(calendar-forward-month 1))))
  13223. (org-defkey minibuffer-local-map [(meta shift up)]
  13224. (lambda () (interactive)
  13225. (org-eval-in-calendar '(calendar-backward-year 1))))
  13226. (org-defkey minibuffer-local-map [(meta shift down)]
  13227. (lambda () (interactive)
  13228. (org-eval-in-calendar '(calendar-forward-year 1))))
  13229. (org-defkey minibuffer-local-map [?\e (shift left)]
  13230. (lambda () (interactive)
  13231. (org-eval-in-calendar '(calendar-backward-month 1))))
  13232. (org-defkey minibuffer-local-map [?\e (shift right)]
  13233. (lambda () (interactive)
  13234. (org-eval-in-calendar '(calendar-forward-month 1))))
  13235. (org-defkey minibuffer-local-map [?\e (shift up)]
  13236. (lambda () (interactive)
  13237. (org-eval-in-calendar '(calendar-backward-year 1))))
  13238. (org-defkey minibuffer-local-map [?\e (shift down)]
  13239. (lambda () (interactive)
  13240. (org-eval-in-calendar '(calendar-forward-year 1))))
  13241. (org-defkey minibuffer-local-map [(shift up)]
  13242. (lambda () (interactive)
  13243. (org-eval-in-calendar '(calendar-backward-week 1))))
  13244. (org-defkey minibuffer-local-map [(shift down)]
  13245. (lambda () (interactive)
  13246. (org-eval-in-calendar '(calendar-forward-week 1))))
  13247. (org-defkey minibuffer-local-map [(shift left)]
  13248. (lambda () (interactive)
  13249. (org-eval-in-calendar '(calendar-backward-day 1))))
  13250. (org-defkey minibuffer-local-map [(shift right)]
  13251. (lambda () (interactive)
  13252. (org-eval-in-calendar '(calendar-forward-day 1))))
  13253. (org-defkey minibuffer-local-map ">"
  13254. (lambda () (interactive)
  13255. (org-eval-in-calendar '(scroll-calendar-left 1))))
  13256. (org-defkey minibuffer-local-map "<"
  13257. (lambda () (interactive)
  13258. (org-eval-in-calendar '(scroll-calendar-right 1))))
  13259. (org-defkey minibuffer-local-map "\C-v"
  13260. (lambda () (interactive)
  13261. (org-eval-in-calendar
  13262. '(calendar-scroll-left-three-months 1))))
  13263. (org-defkey minibuffer-local-map "\M-v"
  13264. (lambda () (interactive)
  13265. (org-eval-in-calendar
  13266. '(calendar-scroll-right-three-months 1))))
  13267. (run-hooks 'org-read-date-minibuffer-setup-hook)
  13268. (unwind-protect
  13269. (progn
  13270. (use-local-map map)
  13271. (add-hook 'post-command-hook 'org-read-date-display)
  13272. (setq org-ans0 (read-string prompt default-input
  13273. 'org-read-date-history nil))
  13274. ;; org-ans0: from prompt
  13275. ;; org-ans1: from mouse click
  13276. ;; org-ans2: from calendar motion
  13277. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  13278. (remove-hook 'post-command-hook 'org-read-date-display)
  13279. (use-local-map old-map)
  13280. (when org-read-date-overlay
  13281. (delete-overlay org-read-date-overlay)
  13282. (setq org-read-date-overlay nil)))))))
  13283. (t ; Naked prompt only
  13284. (unwind-protect
  13285. (setq ans (read-string prompt default-input
  13286. 'org-read-date-history timestr))
  13287. (when org-read-date-overlay
  13288. (delete-overlay org-read-date-overlay)
  13289. (setq org-read-date-overlay nil)))))
  13290. (setq final (org-read-date-analyze ans def defdecode))
  13291. (when org-read-date-analyze-forced-year
  13292. (message "Year was forced into %s"
  13293. (if org-read-date-force-compatible-dates
  13294. "compatible range (1970-2037)"
  13295. "range representable on this machine"))
  13296. (ding))
  13297. ;; One round trip to get rid of 34th of August and stuff like that....
  13298. (setq final (decode-time (apply 'encode-time final)))
  13299. (setq org-read-date-final-answer ans)
  13300. (if to-time
  13301. (apply 'encode-time final)
  13302. (if (and (boundp 'org-time-was-given) org-time-was-given)
  13303. (format "%04d-%02d-%02d %02d:%02d"
  13304. (nth 5 final) (nth 4 final) (nth 3 final)
  13305. (nth 2 final) (nth 1 final))
  13306. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  13307. (defvar def)
  13308. (defvar defdecode)
  13309. (defvar with-time)
  13310. (defun org-read-date-display ()
  13311. "Display the current date prompt interpretation in the minibuffer."
  13312. (when org-read-date-display-live
  13313. (when org-read-date-overlay
  13314. (delete-overlay org-read-date-overlay))
  13315. (let ((p (point)))
  13316. (end-of-line 1)
  13317. (while (not (equal (buffer-substring
  13318. (max (point-min) (- (point) 4)) (point))
  13319. " "))
  13320. (insert " "))
  13321. (goto-char p))
  13322. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  13323. " " (or org-ans1 org-ans2)))
  13324. (org-end-time-was-given nil)
  13325. (f (org-read-date-analyze ans def defdecode))
  13326. (fmts (if org-dcst
  13327. org-time-stamp-custom-formats
  13328. org-time-stamp-formats))
  13329. (fmt (if (or with-time
  13330. (and (boundp 'org-time-was-given) org-time-was-given))
  13331. (cdr fmts)
  13332. (car fmts)))
  13333. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  13334. (when (and org-end-time-was-given
  13335. (string-match org-plain-time-of-day-regexp txt))
  13336. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  13337. org-end-time-was-given
  13338. (substring txt (match-end 0)))))
  13339. (when org-read-date-analyze-futurep
  13340. (setq txt (concat txt " (=>F)")))
  13341. (setq org-read-date-overlay
  13342. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  13343. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  13344. (defun org-read-date-analyze (ans def defdecode)
  13345. "Analyze the combined answer of the date prompt."
  13346. ;; FIXME: cleanup and comment
  13347. (let ((nowdecode (decode-time (current-time)))
  13348. delta deltan deltaw deltadef year month day
  13349. hour minute second wday pm h2 m2 tl wday1
  13350. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  13351. (setq org-read-date-analyze-futurep nil
  13352. org-read-date-analyze-forced-year nil)
  13353. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  13354. (setq ans "+0"))
  13355. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  13356. (setq ans (replace-match "" t t ans)
  13357. deltan (car delta)
  13358. deltaw (nth 1 delta)
  13359. deltadef (nth 2 delta)))
  13360. ;; Check if there is an iso week date in there
  13361. ;; If yes, store the info and postpone interpreting it until the rest
  13362. ;; of the parsing is done
  13363. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  13364. (setq iso-year (if (match-end 1)
  13365. (org-small-year-to-year
  13366. (string-to-number (match-string 1 ans))))
  13367. iso-weekday (if (match-end 3)
  13368. (string-to-number (match-string 3 ans)))
  13369. iso-week (string-to-number (match-string 2 ans)))
  13370. (setq ans (replace-match "" t t ans)))
  13371. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  13372. (when (string-match
  13373. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  13374. (setq year (if (match-end 2)
  13375. (string-to-number (match-string 2 ans))
  13376. (progn (setq kill-year t)
  13377. (string-to-number (format-time-string "%Y"))))
  13378. month (string-to-number (match-string 3 ans))
  13379. day (string-to-number (match-string 4 ans)))
  13380. (if (< year 100) (setq year (+ 2000 year)))
  13381. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13382. t nil ans)))
  13383. ;; Help matching dottet european dates
  13384. (when (string-match
  13385. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\. ?\\([1-9][0-9][0-9][0-9]\\)?" ans)
  13386. (setq year (if (match-end 3)
  13387. (string-to-number (match-string 3 ans))
  13388. (progn (setq kill-year t)
  13389. (string-to-number (format-time-string "%Y"))))
  13390. day (string-to-number (match-string 1 ans))
  13391. month (string-to-number (match-string 2 ans))
  13392. ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13393. t nil ans)))
  13394. ;; Help matching american dates, like 5/30 or 5/30/7
  13395. (when (string-match
  13396. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  13397. (setq year (if (match-end 4)
  13398. (string-to-number (match-string 4 ans))
  13399. (progn (setq kill-year t)
  13400. (string-to-number (format-time-string "%Y"))))
  13401. month (string-to-number (match-string 1 ans))
  13402. day (string-to-number (match-string 2 ans)))
  13403. (if (< year 100) (setq year (+ 2000 year)))
  13404. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  13405. t nil ans)))
  13406. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  13407. ;; If there is a time with am/pm, and *no* time without it, we convert
  13408. ;; so that matching will be successful.
  13409. (loop for i from 1 to 2 do ; twice, for end time as well
  13410. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  13411. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  13412. (setq hour (string-to-number (match-string 1 ans))
  13413. minute (if (match-end 3)
  13414. (string-to-number (match-string 3 ans))
  13415. 0)
  13416. pm (equal ?p
  13417. (string-to-char (downcase (match-string 4 ans)))))
  13418. (if (and (= hour 12) (not pm))
  13419. (setq hour 0)
  13420. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  13421. (setq ans (replace-match (format "%02d:%02d" hour minute)
  13422. t t ans))))
  13423. ;; Check if a time range is given as a duration
  13424. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  13425. (setq hour (string-to-number (match-string 1 ans))
  13426. h2 (+ hour (string-to-number (match-string 3 ans)))
  13427. minute (string-to-number (match-string 2 ans))
  13428. m2 (+ minute (if (match-end 5) (string-to-number
  13429. (match-string 5 ans))0)))
  13430. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  13431. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  13432. t t ans)))
  13433. ;; Check if there is a time range
  13434. (when (boundp 'org-end-time-was-given)
  13435. (setq org-time-was-given nil)
  13436. (when (and (string-match org-plain-time-of-day-regexp ans)
  13437. (match-end 8))
  13438. (setq org-end-time-was-given (match-string 8 ans))
  13439. (setq ans (concat (substring ans 0 (match-beginning 7))
  13440. (substring ans (match-end 7))))))
  13441. (setq tl (parse-time-string ans)
  13442. day (or (nth 3 tl) (nth 3 defdecode))
  13443. month (or (nth 4 tl)
  13444. (if (and org-read-date-prefer-future
  13445. (nth 3 tl) (< (nth 3 tl) (nth 3 nowdecode)))
  13446. (prog1 (1+ (nth 4 nowdecode)) (setq futurep t))
  13447. (nth 4 defdecode)))
  13448. year (or (and (not kill-year) (nth 5 tl))
  13449. (if (and org-read-date-prefer-future
  13450. (nth 4 tl) (< (nth 4 tl) (nth 4 nowdecode)))
  13451. (prog1 (1+ (nth 5 nowdecode)) (setq futurep t))
  13452. (nth 5 defdecode)))
  13453. hour (or (nth 2 tl) (nth 2 defdecode))
  13454. minute (or (nth 1 tl) (nth 1 defdecode))
  13455. second (or (nth 0 tl) 0)
  13456. wday (nth 6 tl))
  13457. (when (and (eq org-read-date-prefer-future 'time)
  13458. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  13459. (equal day (nth 3 nowdecode))
  13460. (equal month (nth 4 nowdecode))
  13461. (equal year (nth 5 nowdecode))
  13462. (nth 2 tl)
  13463. (or (< (nth 2 tl) (nth 2 nowdecode))
  13464. (and (= (nth 2 tl) (nth 2 nowdecode))
  13465. (nth 1 tl)
  13466. (< (nth 1 tl) (nth 1 nowdecode)))))
  13467. (setq day (1+ day)
  13468. futurep t))
  13469. ;; Special date definitions below
  13470. (cond
  13471. (iso-week
  13472. ;; There was an iso week
  13473. (require 'cal-iso)
  13474. (setq futurep nil)
  13475. (setq year (or iso-year year)
  13476. day (or iso-weekday wday 1)
  13477. wday nil ; to make sure that the trigger below does not match
  13478. iso-date (calendar-gregorian-from-absolute
  13479. (calendar-absolute-from-iso
  13480. (list iso-week day year))))
  13481. ; FIXME: Should we also push ISO weeks into the future?
  13482. ; (when (and org-read-date-prefer-future
  13483. ; (not iso-year)
  13484. ; (< (calendar-absolute-from-gregorian iso-date)
  13485. ; (time-to-days (current-time))))
  13486. ; (setq year (1+ year)
  13487. ; iso-date (calendar-gregorian-from-absolute
  13488. ; (calendar-absolute-from-iso
  13489. ; (list iso-week day year)))))
  13490. (setq month (car iso-date)
  13491. year (nth 2 iso-date)
  13492. day (nth 1 iso-date)))
  13493. (deltan
  13494. (setq futurep nil)
  13495. (unless deltadef
  13496. (let ((now (decode-time (current-time))))
  13497. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  13498. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  13499. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  13500. ((equal deltaw "m") (setq month (+ month deltan)))
  13501. ((equal deltaw "y") (setq year (+ year deltan)))))
  13502. ((and wday (not (nth 3 tl)))
  13503. (setq futurep nil)
  13504. ;; Weekday was given, but no day, so pick that day in the week
  13505. ;; on or after the derived date.
  13506. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  13507. (unless (equal wday wday1)
  13508. (setq day (+ day (% (- wday wday1 -7) 7))))))
  13509. (if (and (boundp 'org-time-was-given)
  13510. (nth 2 tl))
  13511. (setq org-time-was-given t))
  13512. (if (< year 100) (setq year (+ 2000 year)))
  13513. ;; Check of the date is representable
  13514. (if org-read-date-force-compatible-dates
  13515. (progn
  13516. (if (< year 1970)
  13517. (setq year 1970 org-read-date-analyze-forced-year t))
  13518. (if (> year 2037)
  13519. (setq year 2037 org-read-date-analyze-forced-year t)))
  13520. (condition-case nil
  13521. (encode-time second minute hour day month year)
  13522. (error
  13523. (setq year (nth 5 defdecode))
  13524. (setq org-read-date-analyze-forced-year t))))
  13525. (setq org-read-date-analyze-futurep futurep)
  13526. (list second minute hour day month year)))
  13527. (defvar parse-time-weekdays)
  13528. (defun org-read-date-get-relative (s today default)
  13529. "Check string S for special relative date string.
  13530. TODAY and DEFAULT are internal times, for today and for a default.
  13531. Return shift list (N what def-flag)
  13532. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  13533. N is the number of WHATs to shift.
  13534. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  13535. the DEFAULT date rather than TODAY."
  13536. (when (and
  13537. (string-match
  13538. (concat
  13539. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  13540. "\\([0-9]+\\)?"
  13541. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  13542. "\\([ \t]\\|$\\)") s)
  13543. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  13544. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  13545. (string-to-char (substring (match-string 1 s) -1))
  13546. ?+))
  13547. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  13548. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  13549. (what (if (match-end 3) (match-string 3 s) "d"))
  13550. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  13551. (date (if rel default today))
  13552. (wday (nth 6 (decode-time date)))
  13553. delta)
  13554. (if wday1
  13555. (progn
  13556. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  13557. (if (= dir ?-) (setq delta (- delta 7)))
  13558. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  13559. (list delta "d" rel))
  13560. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  13561. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  13562. "Turn a user-specified date into the internal representation.
  13563. The internal representation needed by the calendar is (month day year).
  13564. This is a wrapper to handle the brain-dead convention in calendar that
  13565. user function argument order change dependent on argument order."
  13566. (if (boundp 'calendar-date-style)
  13567. (cond
  13568. ((eq calendar-date-style 'american)
  13569. (list arg1 arg2 arg3))
  13570. ((eq calendar-date-style 'european)
  13571. (list arg2 arg1 arg3))
  13572. ((eq calendar-date-style 'iso)
  13573. (list arg2 arg3 arg1)))
  13574. (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  13575. (if (org-bound-and-true-p european-calendar-style)
  13576. (list arg2 arg1 arg3)
  13577. (list arg1 arg2 arg3)))))
  13578. (defun org-eval-in-calendar (form &optional keepdate)
  13579. "Eval FORM in the calendar window and return to current window.
  13580. Also, store the cursor date in variable org-ans2."
  13581. (let ((sf (selected-frame))
  13582. (sw (selected-window)))
  13583. (select-window (get-buffer-window "*Calendar*" t))
  13584. (eval form)
  13585. (when (and (not keepdate) (calendar-cursor-to-date))
  13586. (let* ((date (calendar-cursor-to-date))
  13587. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13588. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  13589. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  13590. (select-window sw)
  13591. (org-select-frame-set-input-focus sf)))
  13592. (defun org-calendar-select ()
  13593. "Return to `org-read-date' with the date currently selected.
  13594. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13595. (interactive)
  13596. (when (calendar-cursor-to-date)
  13597. (let* ((date (calendar-cursor-to-date))
  13598. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13599. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13600. (if (active-minibuffer-window) (exit-minibuffer))))
  13601. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  13602. "Insert a date stamp for the date given by the internal TIME.
  13603. WITH-HM means use the stamp format that includes the time of the day.
  13604. INACTIVE means use square brackets instead of angular ones, so that the
  13605. stamp will not contribute to the agenda.
  13606. PRE and POST are optional strings to be inserted before and after the
  13607. stamp.
  13608. The command returns the inserted time stamp."
  13609. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  13610. stamp)
  13611. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  13612. (insert-before-markers (or pre ""))
  13613. (when (listp extra)
  13614. (setq extra (car extra))
  13615. (if (and (stringp extra)
  13616. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  13617. (setq extra (format "-%02d:%02d"
  13618. (string-to-number (match-string 1 extra))
  13619. (string-to-number (match-string 2 extra))))
  13620. (setq extra nil)))
  13621. (when extra
  13622. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  13623. (insert-before-markers (setq stamp (format-time-string fmt time)))
  13624. (insert-before-markers (or post ""))
  13625. (setq org-last-inserted-timestamp stamp)))
  13626. (defun org-toggle-time-stamp-overlays ()
  13627. "Toggle the use of custom time stamp formats."
  13628. (interactive)
  13629. (setq org-display-custom-times (not org-display-custom-times))
  13630. (unless org-display-custom-times
  13631. (let ((p (point-min)) (bmp (buffer-modified-p)))
  13632. (while (setq p (next-single-property-change p 'display))
  13633. (if (and (get-text-property p 'display)
  13634. (eq (get-text-property p 'face) 'org-date))
  13635. (remove-text-properties
  13636. p (setq p (next-single-property-change p 'display))
  13637. '(display t))))
  13638. (set-buffer-modified-p bmp)))
  13639. (if (featurep 'xemacs)
  13640. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  13641. (org-restart-font-lock)
  13642. (setq org-table-may-need-update t)
  13643. (if org-display-custom-times
  13644. (message "Time stamps are overlayed with custom format")
  13645. (message "Time stamp overlays removed")))
  13646. (defun org-display-custom-time (beg end)
  13647. "Overlay modified time stamp format over timestamp between BEG and END."
  13648. (let* ((ts (buffer-substring beg end))
  13649. t1 w1 with-hm tf time str w2 (off 0))
  13650. (save-match-data
  13651. (setq t1 (org-parse-time-string ts t))
  13652. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)?\\'" ts)
  13653. (setq off (- (match-end 0) (match-beginning 0)))))
  13654. (setq end (- end off))
  13655. (setq w1 (- end beg)
  13656. with-hm (and (nth 1 t1) (nth 2 t1))
  13657. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  13658. time (org-fix-decoded-time t1)
  13659. str (org-add-props
  13660. (format-time-string
  13661. (substring tf 1 -1) (apply 'encode-time time))
  13662. nil 'mouse-face 'highlight)
  13663. w2 (length str))
  13664. (if (not (= w2 w1))
  13665. (add-text-properties (1+ beg) (+ 2 beg)
  13666. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  13667. (if (featurep 'xemacs)
  13668. (progn
  13669. (put-text-property beg end 'invisible t)
  13670. (put-text-property beg end 'end-glyph (make-glyph str)))
  13671. (put-text-property beg end 'display str))))
  13672. (defun org-translate-time (string)
  13673. "Translate all timestamps in STRING to custom format.
  13674. But do this only if the variable `org-display-custom-times' is set."
  13675. (when org-display-custom-times
  13676. (save-match-data
  13677. (let* ((start 0)
  13678. (re org-ts-regexp-both)
  13679. t1 with-hm inactive tf time str beg end)
  13680. (while (setq start (string-match re string start))
  13681. (setq beg (match-beginning 0)
  13682. end (match-end 0)
  13683. t1 (save-match-data
  13684. (org-parse-time-string (substring string beg end) t))
  13685. with-hm (and (nth 1 t1) (nth 2 t1))
  13686. inactive (equal (substring string beg (1+ beg)) "[")
  13687. tf (funcall (if with-hm 'cdr 'car)
  13688. org-time-stamp-custom-formats)
  13689. time (org-fix-decoded-time t1)
  13690. str (format-time-string
  13691. (concat
  13692. (if inactive "[" "<") (substring tf 1 -1)
  13693. (if inactive "]" ">"))
  13694. (apply 'encode-time time))
  13695. string (replace-match str t t string)
  13696. start (+ start (length str)))))))
  13697. string)
  13698. (defun org-fix-decoded-time (time)
  13699. "Set 0 instead of nil for the first 6 elements of time.
  13700. Don't touch the rest."
  13701. (let ((n 0))
  13702. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  13703. (defun org-days-to-time (timestamp-string)
  13704. "Difference between TIMESTAMP-STRING and now in days."
  13705. (- (time-to-days (org-time-string-to-time timestamp-string))
  13706. (time-to-days (current-time))))
  13707. (defun org-deadline-close (timestamp-string &optional ndays)
  13708. "Is the time in TIMESTAMP-STRING close to the current date?"
  13709. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  13710. (and (< (org-days-to-time timestamp-string) ndays)
  13711. (not (org-entry-is-done-p))))
  13712. (defun org-get-wdays (ts)
  13713. "Get the deadline lead time appropriate for timestring TS."
  13714. (cond
  13715. ((<= org-deadline-warning-days 0)
  13716. ;; 0 or negative, enforce this value no matter what
  13717. (- org-deadline-warning-days))
  13718. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  13719. ;; lead time is specified.
  13720. (floor (* (string-to-number (match-string 1 ts))
  13721. (cdr (assoc (match-string 2 ts)
  13722. '(("d" . 1) ("w" . 7)
  13723. ("m" . 30.4) ("y" . 365.25)))))))
  13724. ;; go for the default.
  13725. (t org-deadline-warning-days)))
  13726. (defun org-calendar-select-mouse (ev)
  13727. "Return to `org-read-date' with the date currently selected.
  13728. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  13729. (interactive "e")
  13730. (mouse-set-point ev)
  13731. (when (calendar-cursor-to-date)
  13732. (let* ((date (calendar-cursor-to-date))
  13733. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13734. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  13735. (if (active-minibuffer-window) (exit-minibuffer))))
  13736. (defun org-check-deadlines (ndays)
  13737. "Check if there are any deadlines due or past due.
  13738. A deadline is considered due if it happens within `org-deadline-warning-days'
  13739. days from today's date. If the deadline appears in an entry marked DONE,
  13740. it is not shown. The prefix arg NDAYS can be used to test that many
  13741. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  13742. (interactive "P")
  13743. (let* ((org-warn-days
  13744. (cond
  13745. ((equal ndays '(4)) 100000)
  13746. (ndays (prefix-numeric-value ndays))
  13747. (t (abs org-deadline-warning-days))))
  13748. (case-fold-search nil)
  13749. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  13750. (callback
  13751. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  13752. (message "%d deadlines past-due or due within %d days"
  13753. (org-occur regexp nil callback)
  13754. org-warn-days)))
  13755. (defun org-check-before-date (date)
  13756. "Check if there are deadlines or scheduled entries before DATE."
  13757. (interactive (list (org-read-date)))
  13758. (let ((case-fold-search nil)
  13759. (regexp (concat "\\<\\(" org-deadline-string
  13760. "\\|" org-scheduled-string
  13761. "\\) *<\\([^>]+\\)>"))
  13762. (callback
  13763. (lambda () (time-less-p
  13764. (org-time-string-to-time (match-string 2))
  13765. (org-time-string-to-time date)))))
  13766. (message "%d entries before %s"
  13767. (org-occur regexp nil callback) date)))
  13768. (defun org-check-after-date (date)
  13769. "Check if there are deadlines or scheduled entries after DATE."
  13770. (interactive (list (org-read-date)))
  13771. (let ((case-fold-search nil)
  13772. (regexp (concat "\\<\\(" org-deadline-string
  13773. "\\|" org-scheduled-string
  13774. "\\) *<\\([^>]+\\)>"))
  13775. (callback
  13776. (lambda () (not
  13777. (time-less-p
  13778. (org-time-string-to-time (match-string 2))
  13779. (org-time-string-to-time date))))))
  13780. (message "%d entries after %s"
  13781. (org-occur regexp nil callback) date)))
  13782. (defun org-evaluate-time-range (&optional to-buffer)
  13783. "Evaluate a time range by computing the difference between start and end.
  13784. Normally the result is just printed in the echo area, but with prefix arg
  13785. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  13786. If the time range is actually in a table, the result is inserted into the
  13787. next column.
  13788. For time difference computation, a year is assumed to be exactly 365
  13789. days in order to avoid rounding problems."
  13790. (interactive "P")
  13791. (or
  13792. (org-clock-update-time-maybe)
  13793. (save-excursion
  13794. (unless (org-at-date-range-p t)
  13795. (goto-char (point-at-bol))
  13796. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  13797. (if (not (org-at-date-range-p t))
  13798. (error "Not at a time-stamp range, and none found in current line")))
  13799. (let* ((ts1 (match-string 1))
  13800. (ts2 (match-string 2))
  13801. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  13802. (match-end (match-end 0))
  13803. (time1 (org-time-string-to-time ts1))
  13804. (time2 (org-time-string-to-time ts2))
  13805. (t1 (org-float-time time1))
  13806. (t2 (org-float-time time2))
  13807. (diff (abs (- t2 t1)))
  13808. (negative (< (- t2 t1) 0))
  13809. ;; (ys (floor (* 365 24 60 60)))
  13810. (ds (* 24 60 60))
  13811. (hs (* 60 60))
  13812. (fy "%dy %dd %02d:%02d")
  13813. (fy1 "%dy %dd")
  13814. (fd "%dd %02d:%02d")
  13815. (fd1 "%dd")
  13816. (fh "%02d:%02d")
  13817. y d h m align)
  13818. (if havetime
  13819. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13820. y 0
  13821. d (floor (/ diff ds)) diff (mod diff ds)
  13822. h (floor (/ diff hs)) diff (mod diff hs)
  13823. m (floor (/ diff 60)))
  13824. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  13825. y 0
  13826. d (floor (+ (/ diff ds) 0.5))
  13827. h 0 m 0))
  13828. (if (not to-buffer)
  13829. (message "%s" (org-make-tdiff-string y d h m))
  13830. (if (org-at-table-p)
  13831. (progn
  13832. (goto-char match-end)
  13833. (setq align t)
  13834. (and (looking-at " *|") (goto-char (match-end 0))))
  13835. (goto-char match-end))
  13836. (if (looking-at
  13837. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  13838. (replace-match ""))
  13839. (if negative (insert " -"))
  13840. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  13841. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  13842. (insert " " (format fh h m))))
  13843. (if align (org-table-align))
  13844. (message "Time difference inserted")))))
  13845. (defun org-make-tdiff-string (y d h m)
  13846. (let ((fmt "")
  13847. (l nil))
  13848. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  13849. l (push y l)))
  13850. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  13851. l (push d l)))
  13852. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  13853. l (push h l)))
  13854. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  13855. l (push m l)))
  13856. (apply 'format fmt (nreverse l))))
  13857. (defun org-time-string-to-time (s)
  13858. (apply 'encode-time (org-parse-time-string s)))
  13859. (defun org-time-string-to-seconds (s)
  13860. (org-float-time (org-time-string-to-time s)))
  13861. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  13862. "Convert a time stamp to an absolute day number.
  13863. If there is a specifier for a cyclic time stamp, get the closest date to
  13864. DAYNR.
  13865. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  13866. The variable date is bound by the calendar when this is called."
  13867. (cond
  13868. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  13869. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  13870. daynr
  13871. (+ daynr 1000)))
  13872. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  13873. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  13874. (time-to-days (current-time))) (match-string 0 s)
  13875. prefer show-all))
  13876. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  13877. (defun org-days-to-iso-week (days)
  13878. "Return the iso week number."
  13879. (require 'cal-iso)
  13880. (car (calendar-iso-from-absolute days)))
  13881. (defun org-small-year-to-year (year)
  13882. "Convert 2-digit years into 4-digit years.
  13883. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  13884. The year 2000 cannot be abbreviated. Any year larger than 99
  13885. is returned unchanged."
  13886. (if (< year 38)
  13887. (setq year (+ 2000 year))
  13888. (if (< year 100)
  13889. (setq year (+ 1900 year))))
  13890. year)
  13891. (defun org-time-from-absolute (d)
  13892. "Return the time corresponding to date D.
  13893. D may be an absolute day number, or a calendar-type list (month day year)."
  13894. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  13895. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  13896. (defun org-calendar-holiday ()
  13897. "List of holidays, for Diary display in Org-mode."
  13898. (require 'holidays)
  13899. (let ((hl (funcall
  13900. (if (fboundp 'calendar-check-holidays)
  13901. 'calendar-check-holidays 'check-calendar-holidays) date)))
  13902. (if hl (mapconcat 'identity hl "; "))))
  13903. (defun org-diary-sexp-entry (sexp entry date)
  13904. "Process a SEXP diary ENTRY for DATE."
  13905. (require 'diary-lib)
  13906. (let ((result (if calendar-debug-sexp
  13907. (let ((stack-trace-on-error t))
  13908. (eval (car (read-from-string sexp))))
  13909. (condition-case nil
  13910. (eval (car (read-from-string sexp)))
  13911. (error
  13912. (beep)
  13913. (message "Bad sexp at line %d in %s: %s"
  13914. (org-current-line)
  13915. (buffer-file-name) sexp)
  13916. (sleep-for 2))))))
  13917. (cond ((stringp result) (split-string result "; "))
  13918. ((and (consp result)
  13919. (not (consp (cdr result)))
  13920. (stringp (cdr result))) (cdr result))
  13921. ((and (consp result)
  13922. (stringp (car result))) result)
  13923. (result entry)
  13924. (t nil))))
  13925. (defun org-diary-to-ical-string (frombuf)
  13926. "Get iCalendar entries from diary entries in buffer FROMBUF.
  13927. This uses the icalendar.el library."
  13928. (let* ((tmpdir (if (featurep 'xemacs)
  13929. (temp-directory)
  13930. temporary-file-directory))
  13931. (tmpfile (make-temp-name
  13932. (expand-file-name "orgics" tmpdir)))
  13933. buf rtn b e)
  13934. (with-current-buffer frombuf
  13935. (icalendar-export-region (point-min) (point-max) tmpfile)
  13936. (setq buf (find-buffer-visiting tmpfile))
  13937. (set-buffer buf)
  13938. (goto-char (point-min))
  13939. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  13940. (setq b (match-beginning 0)))
  13941. (goto-char (point-max))
  13942. (if (re-search-backward "^END:VEVENT" nil t)
  13943. (setq e (match-end 0)))
  13944. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  13945. (kill-buffer buf)
  13946. (delete-file tmpfile)
  13947. rtn))
  13948. (defun org-closest-date (start current change prefer show-all)
  13949. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  13950. When PREFER is `past', return a date that is either CURRENT or past.
  13951. When PREFER is `future', return a date that is either CURRENT or future.
  13952. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  13953. ;; Make the proper lists from the dates
  13954. (catch 'exit
  13955. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  13956. dn dw sday cday n1 n2 n0
  13957. d m y y1 y2 date1 date2 nmonths nm ny m2)
  13958. (setq start (org-date-to-gregorian start)
  13959. current (org-date-to-gregorian
  13960. (if show-all
  13961. current
  13962. (time-to-days (current-time))))
  13963. sday (calendar-absolute-from-gregorian start)
  13964. cday (calendar-absolute-from-gregorian current))
  13965. (if (<= cday sday) (throw 'exit sday))
  13966. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  13967. (setq dn (string-to-number (match-string 1 change))
  13968. dw (cdr (assoc (match-string 2 change) a1)))
  13969. (error "Invalid change specifier: %s" change))
  13970. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  13971. (cond
  13972. ((eq dw 'day)
  13973. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  13974. n2 (+ n1 dn)))
  13975. ((eq dw 'year)
  13976. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  13977. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  13978. (setq date1 (list m d y1)
  13979. n1 (calendar-absolute-from-gregorian date1)
  13980. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  13981. n2 (calendar-absolute-from-gregorian date2)))
  13982. ((eq dw 'month)
  13983. ;; approx number of month between the two dates
  13984. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  13985. ;; How often does dn fit in there?
  13986. (setq d (nth 1 start) m (car start) y (nth 2 start)
  13987. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  13988. m (+ m nm)
  13989. ny (floor (/ m 12))
  13990. y (+ y ny)
  13991. m (- m (* ny 12)))
  13992. (while (> m 12) (setq m (- m 12) y (1+ y)))
  13993. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  13994. (setq m2 (+ m dn) y2 y)
  13995. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  13996. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  13997. (while (<= n2 cday)
  13998. (setq n1 n2 m m2 y y2)
  13999. (setq m2 (+ m dn) y2 y)
  14000. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  14001. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  14002. ;; Make sure n1 is the earlier date
  14003. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  14004. (if show-all
  14005. (cond
  14006. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14007. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14008. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  14009. (cond
  14010. ((eq prefer 'past) (if (= cday n2) n2 n1))
  14011. ((eq prefer 'future) (if (= cday n1) n1 n2))
  14012. (t (if (= cday n1) n1 n2)))))))
  14013. (defun org-date-to-gregorian (date)
  14014. "Turn any specification of DATE into a Gregorian date for the calendar."
  14015. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  14016. ((and (listp date) (= (length date) 3)) date)
  14017. ((stringp date)
  14018. (setq date (org-parse-time-string date))
  14019. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  14020. ((listp date)
  14021. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  14022. (defun org-parse-time-string (s &optional nodefault)
  14023. "Parse the standard Org-mode time string.
  14024. This should be a lot faster than the normal `parse-time-string'.
  14025. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  14026. hour and minute fields will be nil if not given."
  14027. (if (string-match org-ts-regexp0 s)
  14028. (list 0
  14029. (if (or (match-beginning 8) (not nodefault))
  14030. (string-to-number (or (match-string 8 s) "0")))
  14031. (if (or (match-beginning 7) (not nodefault))
  14032. (string-to-number (or (match-string 7 s) "0")))
  14033. (string-to-number (match-string 4 s))
  14034. (string-to-number (match-string 3 s))
  14035. (string-to-number (match-string 2 s))
  14036. nil nil nil)
  14037. (error "Not a standard Org-mode time string: %s" s)))
  14038. (defun org-timestamp-up (&optional arg)
  14039. "Increase the date item at the cursor by one.
  14040. If the cursor is on the year, change the year. If it is on the month or
  14041. the day, change that.
  14042. With prefix ARG, change by that many units."
  14043. (interactive "p")
  14044. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  14045. (defun org-timestamp-down (&optional arg)
  14046. "Decrease the date item at the cursor by one.
  14047. If the cursor is on the year, change the year. If it is on the month or
  14048. the day, change that.
  14049. With prefix ARG, change by that many units."
  14050. (interactive "p")
  14051. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  14052. (defun org-timestamp-up-day (&optional arg)
  14053. "Increase the date in the time stamp by one day.
  14054. With prefix ARG, change that many days."
  14055. (interactive "p")
  14056. (if (and (not (org-at-timestamp-p t))
  14057. (org-on-heading-p))
  14058. (org-todo 'up)
  14059. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  14060. (defun org-timestamp-down-day (&optional arg)
  14061. "Decrease the date in the time stamp by one day.
  14062. With prefix ARG, change that many days."
  14063. (interactive "p")
  14064. (if (and (not (org-at-timestamp-p t))
  14065. (org-on-heading-p))
  14066. (org-todo 'down)
  14067. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  14068. (defun org-at-timestamp-p (&optional inactive-ok)
  14069. "Determine if the cursor is in or at a timestamp."
  14070. (interactive)
  14071. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  14072. (pos (point))
  14073. (ans (or (looking-at tsr)
  14074. (save-excursion
  14075. (skip-chars-backward "^[<\n\r\t")
  14076. (if (> (point) (point-min)) (backward-char 1))
  14077. (and (looking-at tsr)
  14078. (> (- (match-end 0) pos) -1))))))
  14079. (and ans
  14080. (boundp 'org-ts-what)
  14081. (setq org-ts-what
  14082. (cond
  14083. ((= pos (match-beginning 0)) 'bracket)
  14084. ((= pos (1- (match-end 0))) 'bracket)
  14085. ((org-pos-in-match-range pos 2) 'year)
  14086. ((org-pos-in-match-range pos 3) 'month)
  14087. ((org-pos-in-match-range pos 7) 'hour)
  14088. ((org-pos-in-match-range pos 8) 'minute)
  14089. ((or (org-pos-in-match-range pos 4)
  14090. (org-pos-in-match-range pos 5)) 'day)
  14091. ((and (> pos (or (match-end 8) (match-end 5)))
  14092. (< pos (match-end 0)))
  14093. (- pos (or (match-end 8) (match-end 5))))
  14094. (t 'day))))
  14095. ans))
  14096. (defun org-toggle-timestamp-type ()
  14097. "Toggle the type (<active> or [inactive]) of a time stamp."
  14098. (interactive)
  14099. (when (org-at-timestamp-p t)
  14100. (let ((beg (match-beginning 0)) (end (match-end 0))
  14101. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  14102. (save-excursion
  14103. (goto-char beg)
  14104. (while (re-search-forward "[][<>]" end t)
  14105. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  14106. t t)))
  14107. (message "Timestamp is now %sactive"
  14108. (if (equal (char-after beg) ?<) "" "in")))))
  14109. (defun org-timestamp-change (n &optional what updown)
  14110. "Change the date in the time stamp at point.
  14111. The date will be changed by N times WHAT. WHAT can be `day', `month',
  14112. `year', `minute', `second'. If WHAT is not given, the cursor position
  14113. in the timestamp determines what will be changed."
  14114. (let ((pos (point))
  14115. with-hm inactive
  14116. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  14117. org-ts-what
  14118. extra rem
  14119. ts time time0)
  14120. (if (not (org-at-timestamp-p t))
  14121. (error "Not at a timestamp"))
  14122. (if (and (not what) (eq org-ts-what 'bracket))
  14123. (org-toggle-timestamp-type)
  14124. (if (and (not what) (not (eq org-ts-what 'day))
  14125. org-display-custom-times
  14126. (get-text-property (point) 'display)
  14127. (not (get-text-property (1- (point)) 'display)))
  14128. (setq org-ts-what 'day))
  14129. (setq org-ts-what (or what org-ts-what)
  14130. inactive (= (char-after (match-beginning 0)) ?\[)
  14131. ts (match-string 0))
  14132. (replace-match "")
  14133. (if (string-match
  14134. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\(/[0-9]+[dwmy]\\)?\\)*\\)[]>]"
  14135. ts)
  14136. (setq extra (match-string 1 ts)))
  14137. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  14138. (setq with-hm t))
  14139. (setq time0 (org-parse-time-string ts))
  14140. (when (and updown
  14141. (eq org-ts-what 'minute)
  14142. (not current-prefix-arg))
  14143. ;; This looks like s-up and s-down. Change by one rounding step.
  14144. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  14145. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  14146. (setcar (cdr time0) (+ (nth 1 time0)
  14147. (if (> n 0) (- rem) (- dm rem))))))
  14148. (setq time
  14149. (encode-time (or (car time0) 0)
  14150. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  14151. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  14152. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  14153. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  14154. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  14155. (nthcdr 6 time0)))
  14156. (when (and (member org-ts-what '(hour minute))
  14157. extra
  14158. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  14159. (setq extra (org-modify-ts-extra
  14160. extra
  14161. (if (eq org-ts-what 'hour) 2 5)
  14162. n dm)))
  14163. (when (integerp org-ts-what)
  14164. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  14165. (if (eq what 'calendar)
  14166. (let ((cal-date (org-get-date-from-calendar)))
  14167. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  14168. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  14169. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  14170. (setcar time0 (or (car time0) 0))
  14171. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  14172. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  14173. (setq time (apply 'encode-time time0))))
  14174. (setq org-last-changed-timestamp
  14175. (org-insert-time-stamp time with-hm inactive nil nil extra))
  14176. (org-clock-update-time-maybe)
  14177. (goto-char pos)
  14178. ;; Try to recenter the calendar window, if any
  14179. (if (and org-calendar-follow-timestamp-change
  14180. (get-buffer-window "*Calendar*" t)
  14181. (memq org-ts-what '(day month year)))
  14182. (org-recenter-calendar (time-to-days time))))))
  14183. (defun org-modify-ts-extra (s pos n dm)
  14184. "Change the different parts of the lead-time and repeat fields in timestamp."
  14185. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  14186. ng h m new rem)
  14187. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  14188. (cond
  14189. ((or (org-pos-in-match-range pos 2)
  14190. (org-pos-in-match-range pos 3))
  14191. (setq m (string-to-number (match-string 3 s))
  14192. h (string-to-number (match-string 2 s)))
  14193. (if (org-pos-in-match-range pos 2)
  14194. (setq h (+ h n))
  14195. (setq n (* dm (org-no-warnings (signum n))))
  14196. (when (not (= 0 (setq rem (% m dm))))
  14197. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  14198. (setq m (+ m n)))
  14199. (if (< m 0) (setq m (+ m 60) h (1- h)))
  14200. (if (> m 59) (setq m (- m 60) h (1+ h)))
  14201. (setq h (min 24 (max 0 h)))
  14202. (setq ng 1 new (format "-%02d:%02d" h m)))
  14203. ((org-pos-in-match-range pos 6)
  14204. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  14205. ((org-pos-in-match-range pos 5)
  14206. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  14207. ((org-pos-in-match-range pos 9)
  14208. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  14209. ((org-pos-in-match-range pos 8)
  14210. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  14211. (when ng
  14212. (setq s (concat
  14213. (substring s 0 (match-beginning ng))
  14214. new
  14215. (substring s (match-end ng))))))
  14216. s))
  14217. (defun org-recenter-calendar (date)
  14218. "If the calendar is visible, recenter it to DATE."
  14219. (let* ((win (selected-window))
  14220. (cwin (get-buffer-window "*Calendar*" t))
  14221. (calendar-move-hook nil))
  14222. (when cwin
  14223. (select-window cwin)
  14224. (calendar-goto-date (if (listp date) date
  14225. (calendar-gregorian-from-absolute date)))
  14226. (select-window win))))
  14227. (defun org-goto-calendar (&optional arg)
  14228. "Go to the Emacs calendar at the current date.
  14229. If there is a time stamp in the current line, go to that date.
  14230. A prefix ARG can be used to force the current date."
  14231. (interactive "P")
  14232. (let ((tsr org-ts-regexp) diff
  14233. (calendar-move-hook nil)
  14234. (calendar-view-holidays-initially-flag nil)
  14235. (calendar-view-diary-initially-flag nil))
  14236. (if (or (org-at-timestamp-p)
  14237. (save-excursion
  14238. (beginning-of-line 1)
  14239. (looking-at (concat ".*" tsr))))
  14240. (let ((d1 (time-to-days (current-time)))
  14241. (d2 (time-to-days
  14242. (org-time-string-to-time (match-string 1)))))
  14243. (setq diff (- d2 d1))))
  14244. (calendar)
  14245. (calendar-goto-today)
  14246. (if (and diff (not arg)) (calendar-forward-day diff))))
  14247. (defun org-get-date-from-calendar ()
  14248. "Return a list (month day year) of date at point in calendar."
  14249. (with-current-buffer "*Calendar*"
  14250. (save-match-data
  14251. (calendar-cursor-to-date))))
  14252. (defun org-date-from-calendar ()
  14253. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  14254. If there is already a time stamp at the cursor position, update it."
  14255. (interactive)
  14256. (if (org-at-timestamp-p t)
  14257. (org-timestamp-change 0 'calendar)
  14258. (let ((cal-date (org-get-date-from-calendar)))
  14259. (org-insert-time-stamp
  14260. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  14261. (defun org-minutes-to-hh:mm-string (m)
  14262. "Compute H:MM from a number of minutes."
  14263. (let ((h (/ m 60)))
  14264. (setq m (- m (* 60 h)))
  14265. (format org-time-clocksum-format h m)))
  14266. (defun org-hh:mm-string-to-minutes (s)
  14267. "Convert a string H:MM to a number of minutes.
  14268. If the string is just a number, interpret it as minutes.
  14269. In fact, the first hh:mm or number in the string will be taken,
  14270. there can be extra stuff in the string.
  14271. If no number is found, the return value is 0."
  14272. (cond
  14273. ((integerp s) s)
  14274. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  14275. (+ (* (string-to-number (match-string 1 s)) 60)
  14276. (string-to-number (match-string 2 s))))
  14277. ((string-match "\\([0-9]+\\)" s)
  14278. (string-to-number (match-string 1 s)))
  14279. (t 0)))
  14280. (defcustom org-effort-durations
  14281. `(("h" . 60)
  14282. ("d" . ,(* 60 8))
  14283. ("w" . ,(* 60 8 5))
  14284. ("m" . ,(* 60 8 5 4))
  14285. ("y" . ,(* 60 8 5 40)))
  14286. "Conversion factor to minutes for an effort modifier.
  14287. Each entry has the form (MODIFIER . MINUTES).
  14288. In an effort string, a number followed by MODIFIER is multiplied
  14289. by the specified number of MINUTES to obtain an effort in
  14290. minutes.
  14291. For example, if the value of this variable is ((\"hours\" . 60)), then an
  14292. effort string \"2hours\" is equivalent to 120 minutes."
  14293. :group 'org-agenda
  14294. :type '(alist :key-type (string :tag "Modifier")
  14295. :value-type (number :tag "Minutes")))
  14296. (defun org-duration-string-to-minutes (s)
  14297. "Convert a duration string S to minutes.
  14298. A bare number is interpreted as minutes, modifiers can be set by
  14299. customizing `org-effort-durations' (which see).
  14300. Entries containing a colon are interpreted as H:MM by
  14301. `org-hh:mm-string-to-minutes'."
  14302. (let ((result 0)
  14303. (re (concat "\\([0-9]+\\) *\\("
  14304. (regexp-opt (mapcar 'car org-effort-durations))
  14305. "\\)")))
  14306. (while (string-match re s)
  14307. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  14308. (string-to-number (match-string 1 s))))
  14309. (setq s (replace-match "" nil t s)))
  14310. (incf result (org-hh:mm-string-to-minutes s))
  14311. result))
  14312. ;;;; Files
  14313. (defun org-save-all-org-buffers ()
  14314. "Save all Org-mode buffers without user confirmation."
  14315. (interactive)
  14316. (message "Saving all Org-mode buffers...")
  14317. (save-some-buffers t 'org-mode-p)
  14318. (when (featurep 'org-id) (org-id-locations-save))
  14319. (message "Saving all Org-mode buffers... done"))
  14320. (defun org-revert-all-org-buffers ()
  14321. "Revert all Org-mode buffers.
  14322. Prompt for confirmation when there are unsaved changes.
  14323. Be sure you know what you are doing before letting this function
  14324. overwrite your changes.
  14325. This function is useful in a setup where one tracks org files
  14326. with a version control system, to revert on one machine after pulling
  14327. changes from another. I believe the procedure must be like this:
  14328. 1. M-x org-save-all-org-buffers
  14329. 2. Pull changes from the other machine, resolve conflicts
  14330. 3. M-x org-revert-all-org-buffers"
  14331. (interactive)
  14332. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  14333. (error "Abort"))
  14334. (save-excursion
  14335. (save-window-excursion
  14336. (mapc
  14337. (lambda (b)
  14338. (when (and (with-current-buffer b (org-mode-p))
  14339. (with-current-buffer b buffer-file-name))
  14340. (switch-to-buffer b)
  14341. (revert-buffer t 'no-confirm)))
  14342. (buffer-list))
  14343. (when (and (featurep 'org-id) org-id-track-globally)
  14344. (org-id-locations-load)))))
  14345. ;;;; Agenda files
  14346. ;;;###autoload
  14347. (defun org-switchb (&optional arg)
  14348. "Switch between Org buffers.
  14349. With a prefix argument, restrict available to files.
  14350. With two prefix arguments, restrict available buffers to agenda files.
  14351. Defaults to `iswitchb' for buffer name completion.
  14352. Set `org-completion-use-ido' to make it use ido instead."
  14353. (interactive "P")
  14354. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  14355. ((equal arg '(16)) (org-buffer-list 'agenda))
  14356. (t (org-buffer-list))))
  14357. (org-completion-use-iswitchb org-completion-use-iswitchb)
  14358. (org-completion-use-ido org-completion-use-ido))
  14359. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  14360. (setq org-completion-use-iswitchb t))
  14361. (switch-to-buffer
  14362. (org-icompleting-read "Org buffer: "
  14363. (mapcar 'list (mapcar 'buffer-name blist))
  14364. nil t))))
  14365. ;;; Define some older names previously used for this functionality
  14366. ;;;###autoload
  14367. (defalias 'org-ido-switchb 'org-switchb)
  14368. ;;;###autoload
  14369. (defalias 'org-iswitchb 'org-switchb)
  14370. (defun org-buffer-list (&optional predicate exclude-tmp)
  14371. "Return a list of Org buffers.
  14372. PREDICATE can be `export', `files' or `agenda'.
  14373. export restrict the list to Export buffers.
  14374. files restrict the list to buffers visiting Org files.
  14375. agenda restrict the list to buffers visiting agenda files.
  14376. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  14377. (let* ((bfn nil)
  14378. (agenda-files (and (eq predicate 'agenda)
  14379. (mapcar 'file-truename (org-agenda-files t))))
  14380. (filter
  14381. (cond
  14382. ((eq predicate 'files)
  14383. (lambda (b) (with-current-buffer b (org-mode-p))))
  14384. ((eq predicate 'export)
  14385. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  14386. ((eq predicate 'agenda)
  14387. (lambda (b)
  14388. (with-current-buffer b
  14389. (and (org-mode-p)
  14390. (setq bfn (buffer-file-name b))
  14391. (member (file-truename bfn) agenda-files)))))
  14392. (t (lambda (b) (with-current-buffer b
  14393. (or (org-mode-p)
  14394. (string-match "\*Org .*Export"
  14395. (buffer-name b)))))))))
  14396. (delq nil
  14397. (mapcar
  14398. (lambda(b)
  14399. (if (and (funcall filter b)
  14400. (or (not exclude-tmp)
  14401. (not (string-match "tmp" (buffer-name b)))))
  14402. b
  14403. nil))
  14404. (buffer-list)))))
  14405. (defun org-agenda-files (&optional unrestricted archives)
  14406. "Get the list of agenda files.
  14407. Optional UNRESTRICTED means return the full list even if a restriction
  14408. is currently in place.
  14409. When ARCHIVES is t, include all archive files that are really being
  14410. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  14411. `org-agenda-archives-mode' is t."
  14412. (let ((files
  14413. (cond
  14414. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  14415. ((stringp org-agenda-files) (org-read-agenda-file-list))
  14416. ((listp org-agenda-files) org-agenda-files)
  14417. (t (error "Invalid value of `org-agenda-files'")))))
  14418. (setq files (apply 'append
  14419. (mapcar (lambda (f)
  14420. (if (file-directory-p f)
  14421. (directory-files
  14422. f t org-agenda-file-regexp)
  14423. (list f)))
  14424. files)))
  14425. (when org-agenda-skip-unavailable-files
  14426. (setq files (delq nil
  14427. (mapcar (function
  14428. (lambda (file)
  14429. (and (file-readable-p file) file)))
  14430. files))))
  14431. (when (or (eq archives t)
  14432. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  14433. (setq files (org-add-archive-files files)))
  14434. files))
  14435. (defun org-agenda-file-p (&optional file)
  14436. "Return non-nil, if FILE is an agenda file.
  14437. If FILE is omitted, use the file associated with the current
  14438. buffer."
  14439. (member (or file (buffer-file-name))
  14440. (org-agenda-files t)))
  14441. (defun org-edit-agenda-file-list ()
  14442. "Edit the list of agenda files.
  14443. Depending on setup, this either uses customize to edit the variable
  14444. `org-agenda-files', or it visits the file that is holding the list. In the
  14445. latter case, the buffer is set up in a way that saving it automatically kills
  14446. the buffer and restores the previous window configuration."
  14447. (interactive)
  14448. (if (stringp org-agenda-files)
  14449. (let ((cw (current-window-configuration)))
  14450. (find-file org-agenda-files)
  14451. (org-set-local 'org-window-configuration cw)
  14452. (org-add-hook 'after-save-hook
  14453. (lambda ()
  14454. (set-window-configuration
  14455. (prog1 org-window-configuration
  14456. (kill-buffer (current-buffer))))
  14457. (org-install-agenda-files-menu)
  14458. (message "New agenda file list installed"))
  14459. nil 'local)
  14460. (message "%s" (substitute-command-keys
  14461. "Edit list and finish with \\[save-buffer]")))
  14462. (customize-variable 'org-agenda-files)))
  14463. (defun org-store-new-agenda-file-list (list)
  14464. "Set new value for the agenda file list and save it correctly."
  14465. (if (stringp org-agenda-files)
  14466. (let ((fe (org-read-agenda-file-list t)) b u)
  14467. (while (setq b (find-buffer-visiting org-agenda-files))
  14468. (kill-buffer b))
  14469. (with-temp-file org-agenda-files
  14470. (insert
  14471. (mapconcat
  14472. (lambda (f) ;; Keep un-expanded entries.
  14473. (if (setq u (assoc f fe))
  14474. (cdr u)
  14475. f))
  14476. list "\n")
  14477. "\n")))
  14478. (let ((org-mode-hook nil) (org-inhibit-startup t)
  14479. (org-insert-mode-line-in-empty-file nil))
  14480. (setq org-agenda-files list)
  14481. (customize-save-variable 'org-agenda-files org-agenda-files))))
  14482. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  14483. "Read the list of agenda files from a file.
  14484. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  14485. filenames, used by `org-store-new-agenda-file-list' to write back
  14486. un-expanded file names."
  14487. (when (file-directory-p org-agenda-files)
  14488. (error "`org-agenda-files' cannot be a single directory"))
  14489. (when (stringp org-agenda-files)
  14490. (with-temp-buffer
  14491. (insert-file-contents org-agenda-files)
  14492. (mapcar
  14493. (lambda (f)
  14494. (let ((e (expand-file-name (substitute-in-file-name f)
  14495. org-directory)))
  14496. (if pair-with-expansion
  14497. (cons e f)
  14498. e)))
  14499. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  14500. ;;;###autoload
  14501. (defun org-cycle-agenda-files ()
  14502. "Cycle through the files in `org-agenda-files'.
  14503. If the current buffer visits an agenda file, find the next one in the list.
  14504. If the current buffer does not, find the first agenda file."
  14505. (interactive)
  14506. (let* ((fs (org-agenda-files t))
  14507. (files (append fs (list (car fs))))
  14508. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  14509. file)
  14510. (unless files (error "No agenda files"))
  14511. (catch 'exit
  14512. (while (setq file (pop files))
  14513. (if (equal (file-truename file) tcf)
  14514. (when (car files)
  14515. (find-file (car files))
  14516. (throw 'exit t))))
  14517. (find-file (car fs)))
  14518. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  14519. (defun org-agenda-file-to-front (&optional to-end)
  14520. "Move/add the current file to the top of the agenda file list.
  14521. If the file is not present in the list, it is added to the front. If it is
  14522. present, it is moved there. With optional argument TO-END, add/move to the
  14523. end of the list."
  14524. (interactive "P")
  14525. (let ((org-agenda-skip-unavailable-files nil)
  14526. (file-alist (mapcar (lambda (x)
  14527. (cons (file-truename x) x))
  14528. (org-agenda-files t)))
  14529. (ctf (file-truename buffer-file-name))
  14530. x had)
  14531. (setq x (assoc ctf file-alist) had x)
  14532. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  14533. (if to-end
  14534. (setq file-alist (append (delq x file-alist) (list x)))
  14535. (setq file-alist (cons x (delq x file-alist))))
  14536. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  14537. (org-install-agenda-files-menu)
  14538. (message "File %s to %s of agenda file list"
  14539. (if had "moved" "added") (if to-end "end" "front"))))
  14540. (defun org-remove-file (&optional file)
  14541. "Remove current file from the list of files in variable `org-agenda-files'.
  14542. These are the files which are being checked for agenda entries.
  14543. Optional argument FILE means use this file instead of the current."
  14544. (interactive)
  14545. (let* ((org-agenda-skip-unavailable-files nil)
  14546. (file (or file buffer-file-name))
  14547. (true-file (file-truename file))
  14548. (afile (abbreviate-file-name file))
  14549. (files (delq nil (mapcar
  14550. (lambda (x)
  14551. (if (equal true-file
  14552. (file-truename x))
  14553. nil x))
  14554. (org-agenda-files t)))))
  14555. (if (not (= (length files) (length (org-agenda-files t))))
  14556. (progn
  14557. (org-store-new-agenda-file-list files)
  14558. (org-install-agenda-files-menu)
  14559. (message "Removed file: %s" afile))
  14560. (message "File was not in list: %s (not removed)" afile))))
  14561. (defun org-file-menu-entry (file)
  14562. (vector file (list 'find-file file) t))
  14563. (defun org-check-agenda-file (file)
  14564. "Make sure FILE exists. If not, ask user what to do."
  14565. (when (not (file-exists-p file))
  14566. (message "non-existent agenda file %s. [R]emove from list or [A]bort?"
  14567. (abbreviate-file-name file))
  14568. (let ((r (downcase (read-char-exclusive))))
  14569. (cond
  14570. ((equal r ?r)
  14571. (org-remove-file file)
  14572. (throw 'nextfile t))
  14573. (t (error "Abort"))))))
  14574. (defun org-get-agenda-file-buffer (file)
  14575. "Get a buffer visiting FILE. If the buffer needs to be created, add
  14576. it to the list of buffers which might be released later."
  14577. (let ((buf (org-find-base-buffer-visiting file)))
  14578. (if buf
  14579. buf ; just return it
  14580. ;; Make a new buffer and remember it
  14581. (setq buf (find-file-noselect file))
  14582. (if buf (push buf org-agenda-new-buffers))
  14583. buf)))
  14584. (defun org-release-buffers (blist)
  14585. "Release all buffers in list, asking the user for confirmation when needed.
  14586. When a buffer is unmodified, it is just killed. When modified, it is saved
  14587. \(if the user agrees) and then killed."
  14588. (let (buf file)
  14589. (while (setq buf (pop blist))
  14590. (setq file (buffer-file-name buf))
  14591. (when (and (buffer-modified-p buf)
  14592. file
  14593. (y-or-n-p (format "Save file %s? " file)))
  14594. (with-current-buffer buf (save-buffer)))
  14595. (kill-buffer buf))))
  14596. (defun org-prepare-agenda-buffers (files)
  14597. "Create buffers for all agenda files, protect archived trees and comments."
  14598. (interactive)
  14599. (let ((pa '(:org-archived t))
  14600. (pc '(:org-comment t))
  14601. (pall '(:org-archived t :org-comment t))
  14602. (inhibit-read-only t)
  14603. (rea (concat ":" org-archive-tag ":"))
  14604. bmp file re)
  14605. (save-excursion
  14606. (save-restriction
  14607. (while (setq file (pop files))
  14608. (catch 'nextfile
  14609. (if (bufferp file)
  14610. (set-buffer file)
  14611. (org-check-agenda-file file)
  14612. (set-buffer (org-get-agenda-file-buffer file)))
  14613. (widen)
  14614. (setq bmp (buffer-modified-p))
  14615. (org-refresh-category-properties)
  14616. (setq org-todo-keywords-for-agenda
  14617. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  14618. (setq org-done-keywords-for-agenda
  14619. (append org-done-keywords-for-agenda org-done-keywords))
  14620. (setq org-todo-keyword-alist-for-agenda
  14621. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  14622. (setq org-drawers-for-agenda
  14623. (append org-drawers-for-agenda org-drawers))
  14624. (setq org-tag-alist-for-agenda
  14625. (append org-tag-alist-for-agenda org-tag-alist))
  14626. (save-excursion
  14627. (remove-text-properties (point-min) (point-max) pall)
  14628. (when org-agenda-skip-archived-trees
  14629. (goto-char (point-min))
  14630. (while (re-search-forward rea nil t)
  14631. (if (org-on-heading-p t)
  14632. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  14633. (goto-char (point-min))
  14634. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  14635. (while (re-search-forward re nil t)
  14636. (add-text-properties
  14637. (match-beginning 0) (org-end-of-subtree t) pc)))
  14638. (set-buffer-modified-p bmp)))))
  14639. (setq org-todo-keywords-for-agenda
  14640. (org-uniquify org-todo-keywords-for-agenda))
  14641. (setq org-todo-keyword-alist-for-agenda
  14642. (org-uniquify org-todo-keyword-alist-for-agenda)
  14643. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  14644. ;;;; Embedded LaTeX
  14645. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  14646. "Keymap for the minor `org-cdlatex-mode'.")
  14647. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  14648. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  14649. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  14650. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  14651. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  14652. (defvar org-cdlatex-texmathp-advice-is-done nil
  14653. "Flag remembering if we have applied the advice to texmathp already.")
  14654. (define-minor-mode org-cdlatex-mode
  14655. "Toggle the minor `org-cdlatex-mode'.
  14656. This mode supports entering LaTeX environment and math in LaTeX fragments
  14657. in Org-mode.
  14658. \\{org-cdlatex-mode-map}"
  14659. nil " OCDL" nil
  14660. (when org-cdlatex-mode (require 'cdlatex))
  14661. (unless org-cdlatex-texmathp-advice-is-done
  14662. (setq org-cdlatex-texmathp-advice-is-done t)
  14663. (defadvice texmathp (around org-math-always-on activate)
  14664. "Always return t in org-mode buffers.
  14665. This is because we want to insert math symbols without dollars even outside
  14666. the LaTeX math segments. If Orgmode thinks that point is actually inside
  14667. an embedded LaTeX fragment, let texmathp do its job.
  14668. \\[org-cdlatex-mode-map]"
  14669. (interactive)
  14670. (let (p)
  14671. (cond
  14672. ((not (org-mode-p)) ad-do-it)
  14673. ((eq this-command 'cdlatex-math-symbol)
  14674. (setq ad-return-value t
  14675. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  14676. (t
  14677. (let ((p (org-inside-LaTeX-fragment-p)))
  14678. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  14679. (setq ad-return-value t
  14680. texmathp-why '("Org-mode embedded math" . 0))
  14681. (if p ad-do-it)))))))))
  14682. (defun turn-on-org-cdlatex ()
  14683. "Unconditionally turn on `org-cdlatex-mode'."
  14684. (org-cdlatex-mode 1))
  14685. (defun org-inside-LaTeX-fragment-p ()
  14686. "Test if point is inside a LaTeX fragment.
  14687. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  14688. sequence appearing also before point.
  14689. Even though the matchers for math are configurable, this function assumes
  14690. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  14691. delimiters are skipped when they have been removed by customization.
  14692. The return value is nil, or a cons cell with the delimiter and
  14693. and the position of this delimiter.
  14694. This function does a reasonably good job, but can locally be fooled by
  14695. for example currency specifications. For example it will assume being in
  14696. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  14697. fragments that are properly closed, but during editing, we have to live
  14698. with the uncertainty caused by missing closing delimiters. This function
  14699. looks only before point, not after."
  14700. (catch 'exit
  14701. (let ((pos (point))
  14702. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  14703. (lim (progn
  14704. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  14705. (point)))
  14706. dd-on str (start 0) m re)
  14707. (goto-char pos)
  14708. (when dodollar
  14709. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  14710. re (nth 1 (assoc "$" org-latex-regexps)))
  14711. (while (string-match re str start)
  14712. (cond
  14713. ((= (match-end 0) (length str))
  14714. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  14715. ((= (match-end 0) (- (length str) 5))
  14716. (throw 'exit nil))
  14717. (t (setq start (match-end 0))))))
  14718. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  14719. (goto-char pos)
  14720. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  14721. (and (match-beginning 2) (throw 'exit nil))
  14722. ;; count $$
  14723. (while (re-search-backward "\\$\\$" lim t)
  14724. (setq dd-on (not dd-on)))
  14725. (goto-char pos)
  14726. (if dd-on (cons "$$" m))))))
  14727. (defun org-inside-latex-macro-p ()
  14728. "Is point inside a LaTeX macro or its arguments?"
  14729. (save-match-data
  14730. (org-in-regexp
  14731. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  14732. (defun org-try-cdlatex-tab ()
  14733. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  14734. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  14735. - inside a LaTeX fragment, or
  14736. - after the first word in a line, where an abbreviation expansion could
  14737. insert a LaTeX environment."
  14738. (when org-cdlatex-mode
  14739. (cond
  14740. ((save-excursion
  14741. (skip-chars-backward "a-zA-Z0-9*")
  14742. (skip-chars-backward " \t")
  14743. (bolp))
  14744. (cdlatex-tab) t)
  14745. ((org-inside-LaTeX-fragment-p)
  14746. (cdlatex-tab) t)
  14747. (t nil))))
  14748. (defun org-cdlatex-underscore-caret (&optional arg)
  14749. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  14750. Revert to the normal definition outside of these fragments."
  14751. (interactive "P")
  14752. (if (org-inside-LaTeX-fragment-p)
  14753. (call-interactively 'cdlatex-sub-superscript)
  14754. (let (org-cdlatex-mode)
  14755. (call-interactively (key-binding (vector last-input-event))))))
  14756. (defun org-cdlatex-math-modify (&optional arg)
  14757. "Execute `cdlatex-math-modify' in LaTeX fragments.
  14758. Revert to the normal definition outside of these fragments."
  14759. (interactive "P")
  14760. (if (org-inside-LaTeX-fragment-p)
  14761. (call-interactively 'cdlatex-math-modify)
  14762. (let (org-cdlatex-mode)
  14763. (call-interactively (key-binding (vector last-input-event))))))
  14764. (defvar org-latex-fragment-image-overlays nil
  14765. "List of overlays carrying the images of latex fragments.")
  14766. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  14767. (defun org-remove-latex-fragment-image-overlays ()
  14768. "Remove all overlays with LaTeX fragment images in current buffer."
  14769. (mapc 'delete-overlay org-latex-fragment-image-overlays)
  14770. (setq org-latex-fragment-image-overlays nil))
  14771. (defun org-preview-latex-fragment (&optional subtree)
  14772. "Preview the LaTeX fragment at point, or all locally or globally.
  14773. If the cursor is in a LaTeX fragment, create the image and overlay
  14774. it over the source code. If there is no fragment at point, display
  14775. all fragments in the current text, from one headline to the next. With
  14776. prefix SUBTREE, display all fragments in the current subtree. With a
  14777. double prefix arg \\[universal-argument] \\[universal-argument], or when \
  14778. the cursor is before the first headline,
  14779. display all fragments in the buffer.
  14780. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  14781. (interactive "P")
  14782. (org-remove-latex-fragment-image-overlays)
  14783. (save-excursion
  14784. (save-restriction
  14785. (let (beg end at msg)
  14786. (cond
  14787. ((or (equal subtree '(16))
  14788. (not (save-excursion
  14789. (re-search-backward (concat "^" outline-regexp) nil t))))
  14790. (setq beg (point-min) end (point-max)
  14791. msg "Creating images for buffer...%s"))
  14792. ((equal subtree '(4))
  14793. (org-back-to-heading)
  14794. (setq beg (point) end (org-end-of-subtree t)
  14795. msg "Creating images for subtree...%s"))
  14796. (t
  14797. (if (setq at (org-inside-LaTeX-fragment-p))
  14798. (goto-char (max (point-min) (- (cdr at) 2)))
  14799. (org-back-to-heading))
  14800. (setq beg (point) end (progn (outline-next-heading) (point))
  14801. msg (if at "Creating image...%s"
  14802. "Creating images for entry...%s"))))
  14803. (message msg "")
  14804. (narrow-to-region beg end)
  14805. (goto-char beg)
  14806. (org-format-latex
  14807. (concat "ltxpng/" (file-name-sans-extension
  14808. (file-name-nondirectory
  14809. buffer-file-name)))
  14810. default-directory 'overlays msg at 'forbuffer 'dvipng)
  14811. (message msg "done. Use `C-c C-c' to remove images.")))))
  14812. (defvar org-latex-regexps
  14813. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  14814. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  14815. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  14816. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14817. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  14818. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  14819. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  14820. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  14821. "Regular expressions for matching embedded LaTeX.")
  14822. (defvar org-export-have-math nil) ;; dynamic scoping
  14823. (defun org-format-latex (prefix &optional dir overlays msg at
  14824. forbuffer processing-type)
  14825. "Replace LaTeX fragments with links to an image, and produce images.
  14826. Some of the options can be changed using the variable
  14827. `org-format-latex-options'."
  14828. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  14829. (let* ((prefixnodir (file-name-nondirectory prefix))
  14830. (absprefix (expand-file-name prefix dir))
  14831. (todir (file-name-directory absprefix))
  14832. (opt org-format-latex-options)
  14833. (matchers (plist-get opt :matchers))
  14834. (re-list org-latex-regexps)
  14835. (org-format-latex-header-extra
  14836. (plist-get (org-infile-export-plist) :latex-header-extra))
  14837. (cnt 0) txt hash link beg end re e checkdir
  14838. executables-checked string
  14839. m n block linkfile movefile ov)
  14840. ;; Check the different regular expressions
  14841. (while (setq e (pop re-list))
  14842. (setq m (car e) re (nth 1 e) n (nth 2 e)
  14843. block (if (nth 3 e) "\n\n" ""))
  14844. (when (member m matchers)
  14845. (goto-char (point-min))
  14846. (while (re-search-forward re nil t)
  14847. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  14848. (not (get-text-property (match-beginning n)
  14849. 'org-protected))
  14850. (or (not overlays)
  14851. (not (eq (get-char-property (match-beginning n)
  14852. 'org-overlay-type)
  14853. 'org-latex-overlay))))
  14854. (setq org-export-have-math t)
  14855. (cond
  14856. ((eq processing-type 'verbatim)
  14857. ;; Leave the text verbatim, just protect it
  14858. (add-text-properties (match-beginning n) (match-end n)
  14859. '(org-protected t)))
  14860. ((eq processing-type 'mathjax)
  14861. ;; Prepare for MathJax processing
  14862. (setq string (match-string n))
  14863. (if (member m '("$" "$1"))
  14864. (save-excursion
  14865. (delete-region (match-beginning n) (match-end n))
  14866. (goto-char (match-beginning n))
  14867. (insert (org-add-props (concat "\\(" (substring string 1 -1)
  14868. "\\)")
  14869. '(org-protected t))))
  14870. (add-text-properties (match-beginning n) (match-end n)
  14871. '(org-protected t))))
  14872. ((or (eq processing-type 'dvipng) t)
  14873. ;; Process to an image
  14874. (setq txt (match-string n)
  14875. beg (match-beginning n) end (match-end n)
  14876. cnt (1+ cnt))
  14877. (let (print-length print-level) ; make sure full list is printed
  14878. (setq hash (sha1 (prin1-to-string
  14879. (list org-format-latex-header
  14880. org-format-latex-header-extra
  14881. org-export-latex-default-packages-alist
  14882. org-export-latex-packages-alist
  14883. org-format-latex-options
  14884. forbuffer txt)))
  14885. linkfile (format "%s_%s.png" prefix hash)
  14886. movefile (format "%s_%s.png" absprefix hash)))
  14887. (setq link (concat block "[[file:" linkfile "]]" block))
  14888. (if msg (message msg cnt))
  14889. (goto-char beg)
  14890. (unless checkdir ; make sure the directory exists
  14891. (setq checkdir t)
  14892. (or (file-directory-p todir) (make-directory todir t)))
  14893. (unless executables-checked
  14894. (org-check-external-command
  14895. "latex" "needed to convert LaTeX fragments to images")
  14896. (org-check-external-command
  14897. "dvipng" "needed to convert LaTeX fragments to images")
  14898. (setq executables-checked t))
  14899. (unless (file-exists-p movefile)
  14900. (org-create-formula-image
  14901. txt movefile opt forbuffer))
  14902. (if overlays
  14903. (progn
  14904. (mapc (lambda (o)
  14905. (if (eq (overlay-get o 'org-overlay-type)
  14906. 'org-latex-overlay)
  14907. (delete-overlay o)))
  14908. (overlays-in beg end))
  14909. (setq ov (make-overlay beg end))
  14910. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  14911. (if (featurep 'xemacs)
  14912. (progn
  14913. (overlay-put ov 'invisible t)
  14914. (overlay-put
  14915. ov 'end-glyph
  14916. (make-glyph (vector 'png :file movefile))))
  14917. (overlay-put
  14918. ov 'display
  14919. (list 'image :type 'png :file movefile :ascent 'center)))
  14920. (push ov org-latex-fragment-image-overlays)
  14921. (goto-char end))
  14922. (delete-region beg end)
  14923. (insert (org-add-props link
  14924. (list 'org-latex-src
  14925. (replace-regexp-in-string
  14926. "\"" "" txt)))))))))))))
  14927. ;; This function borrows from Ganesh Swami's latex2png.el
  14928. (defun org-create-formula-image (string tofile options buffer)
  14929. "This calls dvipng."
  14930. (require 'org-latex)
  14931. (let* ((tmpdir (if (featurep 'xemacs)
  14932. (temp-directory)
  14933. temporary-file-directory))
  14934. (texfilebase (make-temp-name
  14935. (expand-file-name "orgtex" tmpdir)))
  14936. (texfile (concat texfilebase ".tex"))
  14937. (dvifile (concat texfilebase ".dvi"))
  14938. (pngfile (concat texfilebase ".png"))
  14939. (fnh (if (featurep 'xemacs)
  14940. (font-height (get-face-font 'default))
  14941. (face-attribute 'default :height nil)))
  14942. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  14943. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  14944. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  14945. "Black"))
  14946. (bg (or (plist-get options (if buffer :background :html-background))
  14947. "Transparent")))
  14948. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  14949. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  14950. (with-temp-file texfile
  14951. (insert (org-splice-latex-header
  14952. org-format-latex-header
  14953. org-export-latex-default-packages-alist
  14954. org-export-latex-packages-alist t
  14955. org-format-latex-header-extra))
  14956. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")
  14957. (require 'org-latex)
  14958. (org-export-latex-fix-inputenc))
  14959. (let ((dir default-directory))
  14960. (condition-case nil
  14961. (progn
  14962. (cd tmpdir)
  14963. (call-process "latex" nil nil nil texfile))
  14964. (error nil))
  14965. (cd dir))
  14966. (if (not (file-exists-p dvifile))
  14967. (progn (message "Failed to create dvi file from %s" texfile) nil)
  14968. (condition-case nil
  14969. (call-process "dvipng" nil nil nil
  14970. "-fg" fg "-bg" bg
  14971. "-D" dpi
  14972. ;;"-x" scale "-y" scale
  14973. "-T" "tight"
  14974. "-o" pngfile
  14975. dvifile)
  14976. (error nil))
  14977. (if (not (file-exists-p pngfile))
  14978. (if org-format-latex-signal-error
  14979. (error "Failed to create png file from %s" texfile)
  14980. (message "Failed to create png file from %s" texfile)
  14981. nil)
  14982. ;; Use the requested file name and clean up
  14983. (copy-file pngfile tofile 'replace)
  14984. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  14985. (delete-file (concat texfilebase e)))
  14986. pngfile))))
  14987. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  14988. "Fill a LaTeX header template TPL.
  14989. In the template, the following place holders will be recognized:
  14990. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  14991. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  14992. [PACKAGES] \\usepackage statements for PKG
  14993. [NO-PACKAGES] do not include PKG
  14994. [EXTRA] the string EXTRA
  14995. [NO-EXTRA] do not include EXTRA
  14996. For backward compatibility, if both the positive and the negative place
  14997. holder is missing, the positive one (without the \"NO-\") will be
  14998. assumed to be present at the end of the template.
  14999. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  15000. EXTRA is a string.
  15001. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  15002. (let (rpl (end ""))
  15003. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  15004. (setq rpl (if (or (match-end 1) (not def-pkg))
  15005. "" (org-latex-packages-to-string def-pkg snippets-p t))
  15006. tpl (replace-match rpl t t tpl))
  15007. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  15008. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  15009. (setq rpl (if (or (match-end 1) (not pkg))
  15010. "" (org-latex-packages-to-string pkg snippets-p t))
  15011. tpl (replace-match rpl t t tpl))
  15012. (if pkg (setq end
  15013. (concat end "\n"
  15014. (org-latex-packages-to-string pkg snippets-p)))))
  15015. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  15016. (setq rpl (if (or (match-end 1) (not extra))
  15017. "" (concat extra "\n"))
  15018. tpl (replace-match rpl t t tpl))
  15019. (if (and extra (string-match "\\S-" extra))
  15020. (setq end (concat end "\n" extra))))
  15021. (if (string-match "\\S-" end)
  15022. (concat tpl "\n" end)
  15023. tpl)))
  15024. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  15025. "Turn an alist of packages into a string with the \\usepackage macros."
  15026. (setq pkg (mapconcat (lambda(p)
  15027. (cond
  15028. ((stringp p) p)
  15029. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  15030. (format "%% Package %s omitted" (cadr p)))
  15031. ((equal "" (car p))
  15032. (format "\\usepackage{%s}" (cadr p)))
  15033. (t
  15034. (format "\\usepackage[%s]{%s}"
  15035. (car p) (cadr p)))))
  15036. pkg
  15037. "\n"))
  15038. (if newline (concat pkg "\n") pkg))
  15039. (defun org-dvipng-color (attr)
  15040. "Return an rgb color specification for dvipng."
  15041. (apply 'format "rgb %s %s %s"
  15042. (mapcar 'org-normalize-color
  15043. (color-values (face-attribute 'default attr nil)))))
  15044. (defun org-normalize-color (value)
  15045. "Return string to be used as color value for an RGB component."
  15046. (format "%g" (/ value 65535.0)))
  15047. ;; Image display
  15048. (defvar org-inline-image-overlays nil)
  15049. (make-variable-buffer-local 'org-inline-image-overlays)
  15050. (defun org-toggle-inline-images (&optional include-linked)
  15051. "Toggle the display of inline images.
  15052. INCLUDE-LINKED is passed to `org-display-inline-images'."
  15053. (interactive "P")
  15054. (if org-inline-image-overlays
  15055. (progn
  15056. (org-remove-inline-images)
  15057. (message "Inline image display turned off"))
  15058. (org-display-inline-images include-linked)
  15059. (if org-inline-image-overlays
  15060. (message "%d images displayed inline"
  15061. (length org-inline-image-overlays))
  15062. (message "No images to display inline"))))
  15063. (defun org-display-inline-images (&optional include-linked refresh beg end)
  15064. "Display inline images.
  15065. Normally only links without a description part are inlined, because this
  15066. is how it will work for export. When INCLUDE-LINKED is set, also links
  15067. with a description part will be inlined. This can be nice for a quick
  15068. look at those images, but it does not reflect what exported files will look
  15069. like.
  15070. When REFRESH is set, refresh existing images between BEG and END.
  15071. This will create new image displays only if necessary.
  15072. BEG and END default to the buffer boundaries."
  15073. (interactive "P")
  15074. (unless refresh
  15075. (org-remove-inline-images)
  15076. (if (fboundp 'clear-image-cache) (clear-image-cache)))
  15077. (save-excursion
  15078. (save-restriction
  15079. (widen)
  15080. (setq beg (or beg (point-min)) end (or end (point-max)))
  15081. (goto-char (point-min))
  15082. (let ((re (concat "\\[\\[\\(\\(file:\\)\\|\\([./~]\\)\\)\\([^]\n]+?"
  15083. (substring (org-image-file-name-regexp) 0 -2)
  15084. "\\)\\]" (if include-linked "" "\\]")))
  15085. old file ov img)
  15086. (while (re-search-forward re end t)
  15087. (setq old (get-char-property-and-overlay (match-beginning 1)
  15088. 'org-image-overlay))
  15089. (setq file (expand-file-name
  15090. (concat (or (match-string 3) "") (match-string 4))))
  15091. (when (file-exists-p file)
  15092. (if (and (car-safe old) refresh)
  15093. (image-refresh (overlay-get (cdr old) 'display))
  15094. (setq img (save-match-data (create-image file)))
  15095. (when img
  15096. (setq ov (make-overlay (match-beginning 0) (match-end 0)))
  15097. (overlay-put ov 'display img)
  15098. (overlay-put ov 'face 'default)
  15099. (overlay-put ov 'org-image-overlay t)
  15100. (overlay-put ov 'modification-hooks
  15101. (list 'org-display-inline-modification-hook))
  15102. (push ov org-inline-image-overlays)))))))))
  15103. (defun org-display-inline-modification-hook (ov after beg end &optional len)
  15104. "Remove inline-display overlay if a corresponding region is modified."
  15105. (let ((inhibit-modification-hooks t))
  15106. (when (and ov after)
  15107. (delete ov org-inline-image-overlays)
  15108. (delete-overlay ov))))
  15109. (defun org-remove-inline-images ()
  15110. "Remove inline display of images."
  15111. (interactive)
  15112. (mapc 'delete-overlay org-inline-image-overlays)
  15113. (setq org-inline-image-overlays nil))
  15114. ;;;; Key bindings
  15115. ;; Make `C-c C-x' a prefix key
  15116. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  15117. ;; TAB key with modifiers
  15118. (org-defkey org-mode-map "\C-i" 'org-cycle)
  15119. (org-defkey org-mode-map [(tab)] 'org-cycle)
  15120. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  15121. (org-defkey org-mode-map [(meta tab)] 'pcomplete)
  15122. (org-defkey org-mode-map "\M-\t" 'pcomplete)
  15123. (org-defkey org-mode-map "\M-\C-i" 'pcomplete)
  15124. ;; The following line is necessary under Suse GNU/Linux
  15125. (unless (featurep 'xemacs)
  15126. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  15127. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  15128. (define-key org-mode-map [backtab] 'org-shifttab)
  15129. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  15130. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  15131. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  15132. ;; Cursor keys with modifiers
  15133. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  15134. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  15135. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  15136. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  15137. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  15138. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  15139. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  15140. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  15141. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  15142. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  15143. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  15144. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  15145. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  15146. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  15147. ;; Babel keys
  15148. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  15149. (mapc (lambda (pair)
  15150. (define-key org-babel-map (car pair) (cdr pair)))
  15151. org-babel-key-bindings)
  15152. ;;; Extra keys for tty access.
  15153. ;; We only set them when really needed because otherwise the
  15154. ;; menus don't show the simple keys
  15155. (when (or org-use-extra-keys
  15156. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  15157. (not window-system))
  15158. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  15159. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  15160. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  15161. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  15162. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  15163. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  15164. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  15165. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  15166. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  15167. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  15168. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  15169. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  15170. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  15171. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  15172. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  15173. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  15174. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  15175. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  15176. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  15177. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  15178. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  15179. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  15180. (org-defkey org-mode-map [?\e (tab)] 'pcomplete)
  15181. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  15182. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  15183. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  15184. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  15185. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  15186. ;; All the other keys
  15187. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  15188. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  15189. (if (boundp 'narrow-map)
  15190. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  15191. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  15192. (if (boundp 'narrow-map)
  15193. (org-defkey narrow-map "b" 'org-narrow-to-block)
  15194. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  15195. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-same-level)
  15196. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-same-level)
  15197. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  15198. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  15199. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  15200. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  15201. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  15202. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  15203. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  15204. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  15205. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  15206. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  15207. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  15208. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  15209. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  15210. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  15211. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  15212. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  15213. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  15214. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  15215. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  15216. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  15217. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  15218. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  15219. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  15220. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  15221. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  15222. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  15223. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  15224. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  15225. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  15226. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  15227. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  15228. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  15229. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  15230. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  15231. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  15232. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  15233. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  15234. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  15235. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  15236. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  15237. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  15238. (org-defkey org-mode-map "\C-c^" 'org-sort)
  15239. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  15240. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  15241. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  15242. (org-defkey org-mode-map "\C-m" 'org-return)
  15243. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  15244. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  15245. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  15246. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  15247. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  15248. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  15249. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  15250. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  15251. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  15252. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  15253. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  15254. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  15255. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  15256. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  15257. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  15258. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  15259. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  15260. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  15261. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  15262. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  15263. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  15264. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  15265. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  15266. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  15267. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  15268. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  15269. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  15270. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  15271. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  15272. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  15273. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  15274. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  15275. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  15276. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  15277. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  15278. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  15279. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  15280. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  15281. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  15282. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  15283. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  15284. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  15285. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  15286. (org-defkey org-mode-map [(control ?c) (control ?x) ?\:] 'org-timer-cancel-timer)
  15287. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  15288. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  15289. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  15290. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  15291. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  15292. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  15293. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  15294. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  15295. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  15296. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  15297. (when (featurep 'xemacs)
  15298. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  15299. (defconst org-speed-commands-default
  15300. '(
  15301. ("Outline Navigation")
  15302. ("n" . (org-speed-move-safe 'outline-next-visible-heading))
  15303. ("p" . (org-speed-move-safe 'outline-previous-visible-heading))
  15304. ("f" . (org-speed-move-safe 'org-forward-same-level))
  15305. ("b" . (org-speed-move-safe 'org-backward-same-level))
  15306. ("u" . (org-speed-move-safe 'outline-up-heading))
  15307. ("j" . org-goto)
  15308. ("g" . (org-refile t))
  15309. ("Outline Visibility")
  15310. ("c" . org-cycle)
  15311. ("C" . org-shifttab)
  15312. (" " . org-display-outline-path)
  15313. ("Outline Structure Editing")
  15314. ("U" . org-shiftmetaup)
  15315. ("D" . org-shiftmetadown)
  15316. ("r" . org-metaright)
  15317. ("l" . org-metaleft)
  15318. ("R" . org-shiftmetaright)
  15319. ("L" . org-shiftmetaleft)
  15320. ("i" . (progn (forward-char 1) (call-interactively
  15321. 'org-insert-heading-respect-content)))
  15322. ("^" . org-sort)
  15323. ("w" . org-refile)
  15324. ("a" . org-archive-subtree-default-with-confirmation)
  15325. ("." . org-mark-subtree)
  15326. ("Clock Commands")
  15327. ("I" . org-clock-in)
  15328. ("O" . org-clock-out)
  15329. ("Meta Data Editing")
  15330. ("t" . org-todo)
  15331. ("0" . (org-priority ?\ ))
  15332. ("1" . (org-priority ?A))
  15333. ("2" . (org-priority ?B))
  15334. ("3" . (org-priority ?C))
  15335. (";" . org-set-tags-command)
  15336. ("e" . org-set-effort)
  15337. ("Agenda Views etc")
  15338. ("v" . org-agenda)
  15339. ("/" . org-sparse-tree)
  15340. ("Misc")
  15341. ("o" . org-open-at-point)
  15342. ("?" . org-speed-command-help)
  15343. ("<" . (org-agenda-set-restriction-lock 'subtree))
  15344. (">" . (org-agenda-remove-restriction-lock))
  15345. )
  15346. "The default speed commands.")
  15347. (defun org-print-speed-command (e)
  15348. (if (> (length (car e)) 1)
  15349. (progn
  15350. (princ "\n")
  15351. (princ (car e))
  15352. (princ "\n")
  15353. (princ (make-string (length (car e)) ?-))
  15354. (princ "\n"))
  15355. (princ (car e))
  15356. (princ " ")
  15357. (if (symbolp (cdr e))
  15358. (princ (symbol-name (cdr e)))
  15359. (prin1 (cdr e)))
  15360. (princ "\n")))
  15361. (defun org-speed-command-help ()
  15362. "Show the available speed commands."
  15363. (interactive)
  15364. (if (not org-use-speed-commands)
  15365. (error "Speed commands are not activated, customize `org-use-speed-commands'")
  15366. (with-output-to-temp-buffer "*Help*"
  15367. (princ "User-defined Speed commands\n===========================\n")
  15368. (mapc 'org-print-speed-command org-speed-commands-user)
  15369. (princ "\n")
  15370. (princ "Built-in Speed commands\n=======================\n")
  15371. (mapc 'org-print-speed-command org-speed-commands-default))
  15372. (with-current-buffer "*Help*"
  15373. (setq truncate-lines t))))
  15374. (defun org-speed-move-safe (cmd)
  15375. "Execute CMD, but make sure that the cursor always ends up in a headline.
  15376. If not, return to the original position and throw an error."
  15377. (interactive)
  15378. (let ((pos (point)))
  15379. (call-interactively cmd)
  15380. (unless (and (bolp) (org-on-heading-p))
  15381. (goto-char pos)
  15382. (error "Boundary reached while executing %s" cmd))))
  15383. (defvar org-self-insert-command-undo-counter 0)
  15384. (defvar org-table-auto-blank-field) ; defined in org-table.el
  15385. (defvar org-speed-command nil)
  15386. (defun org-speed-command-default-hook (keys)
  15387. "Hook for activating single-letter speed commands.
  15388. `org-speed-commands-default' specifies a minimal command set. Use
  15389. `org-speed-commands-user' for further customization."
  15390. (when (or (and (bolp) (looking-at outline-regexp))
  15391. (and (functionp org-use-speed-commands)
  15392. (funcall org-use-speed-commands)))
  15393. (cdr (assoc keys (append org-speed-commands-user
  15394. org-speed-commands-default)))))
  15395. (defun org-babel-speed-command-hook (keys)
  15396. "Hook for activating single-letter code block commands."
  15397. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  15398. (cdr (assoc keys org-babel-key-bindings))))
  15399. (defcustom org-speed-command-hook
  15400. '(org-speed-command-default-hook org-babel-speed-command-hook)
  15401. "Hook for activating speed commands at strategic locations.
  15402. Hook functions are called in sequence until a valid handler is
  15403. found.
  15404. Each hook takes a single argument, a user-pressed command key
  15405. which is also a `self-insert-command' from the global map.
  15406. Within the hook, examine the cursor position and the command key
  15407. and return nil or a valid handler as appropriate. Handler could
  15408. be one of an interactive command, a function, or a form.
  15409. Set `org-use-speed-commands' to non-nil value to enable this
  15410. hook. The default setting is `org-speed-command-default-hook'."
  15411. :group 'org-structure
  15412. :type 'hook)
  15413. (defun org-self-insert-command (N)
  15414. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  15415. If the cursor is in a table looking at whitespace, the whitespace is
  15416. overwritten, and the table is not marked as requiring realignment."
  15417. (interactive "p")
  15418. (cond
  15419. ((and org-use-speed-commands
  15420. (setq org-speed-command
  15421. (run-hook-with-args-until-success
  15422. 'org-speed-command-hook (this-command-keys))))
  15423. (cond
  15424. ((commandp org-speed-command)
  15425. (setq this-command org-speed-command)
  15426. (call-interactively org-speed-command))
  15427. ((functionp org-speed-command)
  15428. (funcall org-speed-command))
  15429. ((and org-speed-command (listp org-speed-command))
  15430. (eval org-speed-command))
  15431. (t (let (org-use-speed-commands)
  15432. (call-interactively 'org-self-insert-command)))))
  15433. ((and
  15434. (org-table-p)
  15435. (progn
  15436. ;; check if we blank the field, and if that triggers align
  15437. (and (featurep 'org-table) org-table-auto-blank-field
  15438. (member last-command
  15439. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c yas/expand))
  15440. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  15441. ;; got extra space, this field does not determine column width
  15442. (let (org-table-may-need-update) (org-table-blank-field))
  15443. ;; no extra space, this field may determine column width
  15444. (org-table-blank-field)))
  15445. t)
  15446. (eq N 1)
  15447. (looking-at "[^|\n]* |"))
  15448. (let (org-table-may-need-update)
  15449. (goto-char (1- (match-end 0)))
  15450. (delete-backward-char 1)
  15451. (goto-char (match-beginning 0))
  15452. (self-insert-command N)))
  15453. (t
  15454. (setq org-table-may-need-update t)
  15455. (self-insert-command N)
  15456. (org-fix-tags-on-the-fly)
  15457. (if org-self-insert-cluster-for-undo
  15458. (if (not (eq last-command 'org-self-insert-command))
  15459. (setq org-self-insert-command-undo-counter 1)
  15460. (if (>= org-self-insert-command-undo-counter 20)
  15461. (setq org-self-insert-command-undo-counter 1)
  15462. (and (> org-self-insert-command-undo-counter 0)
  15463. buffer-undo-list
  15464. (not (cadr buffer-undo-list)) ; remove nil entry
  15465. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  15466. (setq org-self-insert-command-undo-counter
  15467. (1+ org-self-insert-command-undo-counter))))))))
  15468. (defun org-fix-tags-on-the-fly ()
  15469. (when (and (equal (char-after (point-at-bol)) ?*)
  15470. (org-on-heading-p))
  15471. (org-align-tags-here org-tags-column)))
  15472. (defun org-delete-backward-char (N)
  15473. "Like `delete-backward-char', insert whitespace at field end in tables.
  15474. When deleting backwards, in tables this function will insert whitespace in
  15475. front of the next \"|\" separator, to keep the table aligned. The table will
  15476. still be marked for re-alignment if the field did fill the entire column,
  15477. because, in this case the deletion might narrow the column."
  15478. (interactive "p")
  15479. (if (and (org-table-p)
  15480. (eq N 1)
  15481. (string-match "|" (buffer-substring (point-at-bol) (point)))
  15482. (looking-at ".*?|"))
  15483. (let ((pos (point))
  15484. (noalign (looking-at "[^|\n\r]* |"))
  15485. (c org-table-may-need-update))
  15486. (backward-delete-char N)
  15487. (if (not overwrite-mode)
  15488. (progn
  15489. (skip-chars-forward "^|")
  15490. (insert " ")
  15491. (goto-char (1- pos))))
  15492. ;; noalign: if there were two spaces at the end, this field
  15493. ;; does not determine the width of the column.
  15494. (if noalign (setq org-table-may-need-update c)))
  15495. (backward-delete-char N)
  15496. (org-fix-tags-on-the-fly)))
  15497. (defun org-delete-char (N)
  15498. "Like `delete-char', but insert whitespace at field end in tables.
  15499. When deleting characters, in tables this function will insert whitespace in
  15500. front of the next \"|\" separator, to keep the table aligned. The table will
  15501. still be marked for re-alignment if the field did fill the entire column,
  15502. because, in this case the deletion might narrow the column."
  15503. (interactive "p")
  15504. (if (and (org-table-p)
  15505. (not (bolp))
  15506. (not (= (char-after) ?|))
  15507. (eq N 1))
  15508. (if (looking-at ".*?|")
  15509. (let ((pos (point))
  15510. (noalign (looking-at "[^|\n\r]* |"))
  15511. (c org-table-may-need-update))
  15512. (replace-match (concat
  15513. (substring (match-string 0) 1 -1)
  15514. " |"))
  15515. (goto-char pos)
  15516. ;; noalign: if there were two spaces at the end, this field
  15517. ;; does not determine the width of the column.
  15518. (if noalign (setq org-table-may-need-update c)))
  15519. (delete-char N))
  15520. (delete-char N)
  15521. (org-fix-tags-on-the-fly)))
  15522. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  15523. (put 'org-self-insert-command 'delete-selection t)
  15524. (put 'orgtbl-self-insert-command 'delete-selection t)
  15525. (put 'org-delete-char 'delete-selection 'supersede)
  15526. (put 'org-delete-backward-char 'delete-selection 'supersede)
  15527. (put 'org-yank 'delete-selection 'yank)
  15528. ;; Make `flyspell-mode' delay after some commands
  15529. (put 'org-self-insert-command 'flyspell-delayed t)
  15530. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  15531. (put 'org-delete-char 'flyspell-delayed t)
  15532. (put 'org-delete-backward-char 'flyspell-delayed t)
  15533. ;; Make pabbrev-mode expand after org-mode commands
  15534. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  15535. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  15536. ;; How to do this: Measure non-white length of current string
  15537. ;; If equal to column width, we should realign.
  15538. (defun org-remap (map &rest commands)
  15539. "In MAP, remap the functions given in COMMANDS.
  15540. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  15541. (let (new old)
  15542. (while commands
  15543. (setq old (pop commands) new (pop commands))
  15544. (if (fboundp 'command-remapping)
  15545. (org-defkey map (vector 'remap old) new)
  15546. (substitute-key-definition old new map global-map)))))
  15547. (when (eq org-enable-table-editor 'optimized)
  15548. ;; If the user wants maximum table support, we need to hijack
  15549. ;; some standard editing functions
  15550. (org-remap org-mode-map
  15551. 'self-insert-command 'org-self-insert-command
  15552. 'delete-char 'org-delete-char
  15553. 'delete-backward-char 'org-delete-backward-char)
  15554. (org-defkey org-mode-map "|" 'org-force-self-insert))
  15555. (defvar org-ctrl-c-ctrl-c-hook nil
  15556. "Hook for functions attaching themselves to `C-c C-c'.
  15557. This can be used to add additional functionality to the C-c C-c key which
  15558. executes context-dependent commands.
  15559. Each function will be called with no arguments. The function must check
  15560. if the context is appropriate for it to act. If yes, it should do its
  15561. thing and then return a non-nil value. If the context is wrong,
  15562. just do nothing and return nil.")
  15563. (defvar org-tab-first-hook nil
  15564. "Hook for functions to attach themselves to TAB.
  15565. See `org-ctrl-c-ctrl-c-hook' for more information.
  15566. This hook runs as the first action when TAB is pressed, even before
  15567. `org-cycle' messes around with the `outline-regexp' to cater for
  15568. inline tasks and plain list item folding.
  15569. If any function in this hook returns t, any other actions that
  15570. would have been caused by TAB (such as table field motion or visibility
  15571. cycling) will not occur.")
  15572. (defvar org-tab-after-check-for-table-hook nil
  15573. "Hook for functions to attach themselves to TAB.
  15574. See `org-ctrl-c-ctrl-c-hook' for more information.
  15575. This hook runs after it has been established that the cursor is not in a
  15576. table, but before checking if the cursor is in a headline or if global cycling
  15577. should be done.
  15578. If any function in this hook returns t, not other actions like visibility
  15579. cycling will be done.")
  15580. (defvar org-tab-after-check-for-cycling-hook nil
  15581. "Hook for functions to attach themselves to TAB.
  15582. See `org-ctrl-c-ctrl-c-hook' for more information.
  15583. This hook runs after it has been established that not table field motion and
  15584. not visibility should be done because of current context. This is probably
  15585. the place where a package like yasnippets can hook in.")
  15586. (defvar org-tab-before-tab-emulation-hook nil
  15587. "Hook for functions to attach themselves to TAB.
  15588. See `org-ctrl-c-ctrl-c-hook' for more information.
  15589. This hook runs after every other options for TAB have been exhausted, but
  15590. before indentation and \t insertion takes place.")
  15591. (defvar org-metaleft-hook nil
  15592. "Hook for functions attaching themselves to `M-left'.
  15593. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15594. (defvar org-metaright-hook nil
  15595. "Hook for functions attaching themselves to `M-right'.
  15596. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15597. (defvar org-metaup-hook nil
  15598. "Hook for functions attaching themselves to `M-up'.
  15599. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15600. (defvar org-metadown-hook nil
  15601. "Hook for functions attaching themselves to `M-down'.
  15602. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15603. (defvar org-shiftmetaleft-hook nil
  15604. "Hook for functions attaching themselves to `M-S-left'.
  15605. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15606. (defvar org-shiftmetaright-hook nil
  15607. "Hook for functions attaching themselves to `M-S-right'.
  15608. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15609. (defvar org-shiftmetaup-hook nil
  15610. "Hook for functions attaching themselves to `M-S-up'.
  15611. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15612. (defvar org-shiftmetadown-hook nil
  15613. "Hook for functions attaching themselves to `M-S-down'.
  15614. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15615. (defvar org-metareturn-hook nil
  15616. "Hook for functions attaching themselves to `M-RET'.
  15617. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15618. (defvar org-shiftup-hook nil
  15619. "Hook for functions attaching themselves to `S-up'.
  15620. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15621. (defvar org-shiftup-final-hook nil
  15622. "Hook for functions attaching themselves to `S-up'.
  15623. This one runs after all other options except shift-select have been excluded.
  15624. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15625. (defvar org-shiftdown-hook nil
  15626. "Hook for functions attaching themselves to `S-down'.
  15627. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15628. (defvar org-shiftdown-final-hook nil
  15629. "Hook for functions attaching themselves to `S-down'.
  15630. This one runs after all other options except shift-select have been excluded.
  15631. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15632. (defvar org-shiftleft-hook nil
  15633. "Hook for functions attaching themselves to `S-left'.
  15634. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15635. (defvar org-shiftleft-final-hook nil
  15636. "Hook for functions attaching themselves to `S-left'.
  15637. This one runs after all other options except shift-select have been excluded.
  15638. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15639. (defvar org-shiftright-hook nil
  15640. "Hook for functions attaching themselves to `S-right'.
  15641. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15642. (defvar org-shiftright-final-hook nil
  15643. "Hook for functions attaching themselves to `S-right'.
  15644. This one runs after all other options except shift-select have been excluded.
  15645. See `org-ctrl-c-ctrl-c-hook' for more information.")
  15646. (defun org-modifier-cursor-error ()
  15647. "Throw an error, a modified cursor command was applied in wrong context."
  15648. (error "This command is active in special context like tables, headlines or items"))
  15649. (defun org-shiftselect-error ()
  15650. "Throw an error because Shift-Cursor command was applied in wrong context."
  15651. (if (and (boundp 'shift-select-mode) shift-select-mode)
  15652. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  15653. (error "This command works only in special context like headlines or timestamps")))
  15654. (defun org-call-for-shift-select (cmd)
  15655. (let ((this-command-keys-shift-translated t))
  15656. (call-interactively cmd)))
  15657. (defun org-shifttab (&optional arg)
  15658. "Global visibility cycling or move to previous table field.
  15659. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  15660. on context.
  15661. See the individual commands for more information."
  15662. (interactive "P")
  15663. (cond
  15664. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  15665. ((integerp arg)
  15666. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  15667. (message "Content view to level: %d" arg)
  15668. (org-content (prefix-numeric-value arg2))
  15669. (setq org-cycle-global-status 'overview)))
  15670. (t (call-interactively 'org-global-cycle))))
  15671. (defun org-shiftmetaleft ()
  15672. "Promote subtree or delete table column.
  15673. Calls `org-promote-subtree', `org-outdent-item',
  15674. or `org-table-delete-column', depending on context.
  15675. See the individual commands for more information."
  15676. (interactive)
  15677. (cond
  15678. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  15679. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  15680. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  15681. ((org-at-item-p) (call-interactively 'org-outdent-item-tree))
  15682. (t (org-modifier-cursor-error))))
  15683. (defun org-shiftmetaright ()
  15684. "Demote subtree or insert table column.
  15685. Calls `org-demote-subtree', `org-indent-item',
  15686. or `org-table-insert-column', depending on context.
  15687. See the individual commands for more information."
  15688. (interactive)
  15689. (cond
  15690. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  15691. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  15692. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  15693. ((org-at-item-p) (call-interactively 'org-indent-item-tree))
  15694. (t (org-modifier-cursor-error))))
  15695. (defun org-shiftmetaup (&optional arg)
  15696. "Move subtree up or kill table row.
  15697. Calls `org-move-subtree-up' or `org-table-kill-row' or
  15698. `org-move-item-up' depending on context. See the individual commands
  15699. for more information."
  15700. (interactive "P")
  15701. (cond
  15702. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  15703. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  15704. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15705. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15706. (t (org-modifier-cursor-error))))
  15707. (defun org-shiftmetadown (&optional arg)
  15708. "Move subtree down or insert table row.
  15709. Calls `org-move-subtree-down' or `org-table-insert-row' or
  15710. `org-move-item-down', depending on context. See the individual
  15711. commands for more information."
  15712. (interactive "P")
  15713. (cond
  15714. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  15715. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  15716. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15717. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15718. (t (org-modifier-cursor-error))))
  15719. (defsubst org-hidden-tree-error ()
  15720. (error
  15721. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  15722. (defun org-metaleft (&optional arg)
  15723. "Promote heading or move table column to left.
  15724. Calls `org-do-promote' or `org-table-move-column', depending on context.
  15725. With no specific context, calls the Emacs default `backward-word'.
  15726. See the individual commands for more information."
  15727. (interactive "P")
  15728. (cond
  15729. ((run-hook-with-args-until-success 'org-metaleft-hook))
  15730. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  15731. ((org-with-limited-levels
  15732. (or (org-on-heading-p)
  15733. (and (org-region-active-p)
  15734. (save-excursion
  15735. (goto-char (region-beginning))
  15736. (org-on-heading-p)))))
  15737. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15738. (call-interactively 'org-do-promote))
  15739. ;; At an inline task.
  15740. ((org-on-heading-p)
  15741. (call-interactively 'org-inlinetask-promote))
  15742. ((or (org-at-item-p)
  15743. (and (org-region-active-p)
  15744. (save-excursion
  15745. (goto-char (region-beginning))
  15746. (org-at-item-p))))
  15747. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15748. (call-interactively 'org-outdent-item))
  15749. (t (call-interactively 'backward-word))))
  15750. (defun org-metaright (&optional arg)
  15751. "Demote subtree or move table column to right.
  15752. Calls `org-do-demote' or `org-table-move-column', depending on context.
  15753. With no specific context, calls the Emacs default `forward-word'.
  15754. See the individual commands for more information."
  15755. (interactive "P")
  15756. (cond
  15757. ((run-hook-with-args-until-success 'org-metaright-hook))
  15758. ((org-at-table-p) (call-interactively 'org-table-move-column))
  15759. ((org-with-limited-levels
  15760. (or (org-on-heading-p)
  15761. (and (org-region-active-p)
  15762. (save-excursion
  15763. (goto-char (region-beginning))
  15764. (org-on-heading-p)))))
  15765. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  15766. (call-interactively 'org-do-demote))
  15767. ;; At an inline task.
  15768. ((org-on-heading-p)
  15769. (call-interactively 'org-inlinetask-demote))
  15770. ((or (org-at-item-p)
  15771. (and (org-region-active-p)
  15772. (save-excursion
  15773. (goto-char (region-beginning))
  15774. (org-at-item-p))))
  15775. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  15776. (call-interactively 'org-indent-item))
  15777. (t (call-interactively 'forward-word))))
  15778. (defun org-check-for-hidden (what)
  15779. "Check if there are hidden headlines/items in the current visual line.
  15780. WHAT can be either `headlines' or `items'. If the current line is
  15781. an outline or item heading and it has a folded subtree below it,
  15782. this function returns t, nil otherwise."
  15783. (let ((re (cond
  15784. ((eq what 'headlines) (concat "^" org-outline-regexp))
  15785. ((eq what 'items) (org-item-beginning-re))
  15786. (t (error "This should not happen"))))
  15787. beg end)
  15788. (save-excursion
  15789. (catch 'exit
  15790. (unless (org-region-active-p)
  15791. (setq beg (point-at-bol))
  15792. (beginning-of-line 2)
  15793. (while (and (not (eobp)) ;; this is like `next-line'
  15794. (get-char-property (1- (point)) 'invisible))
  15795. (beginning-of-line 2))
  15796. (setq end (point))
  15797. (goto-char beg)
  15798. (goto-char (point-at-eol))
  15799. (setq end (max end (point)))
  15800. (while (re-search-forward re end t)
  15801. (if (get-char-property (match-beginning 0) 'invisible)
  15802. (throw 'exit t))))
  15803. nil))))
  15804. (defun org-metaup (&optional arg)
  15805. "Move subtree up or move table row up.
  15806. Calls `org-move-subtree-up' or `org-table-move-row' or
  15807. `org-move-item-up', depending on context. See the individual commands
  15808. for more information."
  15809. (interactive "P")
  15810. (cond
  15811. ((run-hook-with-args-until-success 'org-metaup-hook))
  15812. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  15813. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  15814. ((org-at-item-p) (call-interactively 'org-move-item-up))
  15815. (t (transpose-lines 1) (beginning-of-line -1))))
  15816. (defun org-metadown (&optional arg)
  15817. "Move subtree down or move table row down.
  15818. Calls `org-move-subtree-down' or `org-table-move-row' or
  15819. `org-move-item-down', depending on context. See the individual
  15820. commands for more information."
  15821. (interactive "P")
  15822. (cond
  15823. ((run-hook-with-args-until-success 'org-metadown-hook))
  15824. ((org-at-table-p) (call-interactively 'org-table-move-row))
  15825. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  15826. ((org-at-item-p) (call-interactively 'org-move-item-down))
  15827. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  15828. (defun org-shiftup (&optional arg)
  15829. "Increase item in timestamp or increase priority of current headline.
  15830. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  15831. depending on context. See the individual commands for more information."
  15832. (interactive "P")
  15833. (cond
  15834. ((run-hook-with-args-until-success 'org-shiftup-hook))
  15835. ((and org-support-shift-select (org-region-active-p))
  15836. (org-call-for-shift-select 'previous-line))
  15837. ((org-at-timestamp-p t)
  15838. (call-interactively (if org-edit-timestamp-down-means-later
  15839. 'org-timestamp-down 'org-timestamp-up)))
  15840. ((and (not (eq org-support-shift-select 'always))
  15841. org-enable-priority-commands
  15842. (org-on-heading-p))
  15843. (call-interactively 'org-priority-up))
  15844. ((and (not org-support-shift-select) (org-at-item-p))
  15845. (call-interactively 'org-previous-item))
  15846. ((org-clocktable-try-shift 'up arg))
  15847. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  15848. (org-support-shift-select
  15849. (org-call-for-shift-select 'previous-line))
  15850. (t (org-shiftselect-error))))
  15851. (defun org-shiftdown (&optional arg)
  15852. "Decrease item in timestamp or decrease priority of current headline.
  15853. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  15854. depending on context. See the individual commands for more information."
  15855. (interactive "P")
  15856. (cond
  15857. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  15858. ((and org-support-shift-select (org-region-active-p))
  15859. (org-call-for-shift-select 'next-line))
  15860. ((org-at-timestamp-p t)
  15861. (call-interactively (if org-edit-timestamp-down-means-later
  15862. 'org-timestamp-up 'org-timestamp-down)))
  15863. ((and (not (eq org-support-shift-select 'always))
  15864. org-enable-priority-commands
  15865. (org-on-heading-p))
  15866. (call-interactively 'org-priority-down))
  15867. ((and (not org-support-shift-select) (org-at-item-p))
  15868. (call-interactively 'org-next-item))
  15869. ((org-clocktable-try-shift 'down arg))
  15870. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  15871. (org-support-shift-select
  15872. (org-call-for-shift-select 'next-line))
  15873. (t (org-shiftselect-error))))
  15874. (defun org-shiftright (&optional arg)
  15875. "Cycle the thing at point or in the current line, depending on context.
  15876. Depending on context, this does one of the following:
  15877. - switch a timestamp at point one day into the future
  15878. - on a headline, switch to the next TODO keyword.
  15879. - on an item, switch entire list to the next bullet type
  15880. - on a property line, switch to the next allowed value
  15881. - on a clocktable definition line, move time block into the future"
  15882. (interactive "P")
  15883. (cond
  15884. ((run-hook-with-args-until-success 'org-shiftright-hook))
  15885. ((and org-support-shift-select (org-region-active-p))
  15886. (org-call-for-shift-select 'forward-char))
  15887. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  15888. ((and (not (eq org-support-shift-select 'always))
  15889. (org-on-heading-p))
  15890. (let ((org-inhibit-logging
  15891. (not org-treat-S-cursor-todo-selection-as-state-change))
  15892. (org-inhibit-blocking
  15893. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15894. (org-call-with-arg 'org-todo 'right)))
  15895. ((or (and org-support-shift-select
  15896. (not (eq org-support-shift-select 'always))
  15897. (org-at-item-bullet-p))
  15898. (and (not org-support-shift-select) (org-at-item-p)))
  15899. (org-call-with-arg 'org-cycle-list-bullet nil))
  15900. ((and (not (eq org-support-shift-select 'always))
  15901. (org-at-property-p))
  15902. (call-interactively 'org-property-next-allowed-value))
  15903. ((org-clocktable-try-shift 'right arg))
  15904. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  15905. (org-support-shift-select
  15906. (org-call-for-shift-select 'forward-char))
  15907. (t (org-shiftselect-error))))
  15908. (defun org-shiftleft (&optional arg)
  15909. "Cycle the thing at point or in the current line, depending on context.
  15910. Depending on context, this does one of the following:
  15911. - switch a timestamp at point one day into the past
  15912. - on a headline, switch to the previous TODO keyword.
  15913. - on an item, switch entire list to the previous bullet type
  15914. - on a property line, switch to the previous allowed value
  15915. - on a clocktable definition line, move time block into the past"
  15916. (interactive "P")
  15917. (cond
  15918. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  15919. ((and org-support-shift-select (org-region-active-p))
  15920. (org-call-for-shift-select 'backward-char))
  15921. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  15922. ((and (not (eq org-support-shift-select 'always))
  15923. (org-on-heading-p))
  15924. (let ((org-inhibit-logging
  15925. (not org-treat-S-cursor-todo-selection-as-state-change))
  15926. (org-inhibit-blocking
  15927. (not org-treat-S-cursor-todo-selection-as-state-change)))
  15928. (org-call-with-arg 'org-todo 'left)))
  15929. ((or (and org-support-shift-select
  15930. (not (eq org-support-shift-select 'always))
  15931. (org-at-item-bullet-p))
  15932. (and (not org-support-shift-select) (org-at-item-p)))
  15933. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  15934. ((and (not (eq org-support-shift-select 'always))
  15935. (org-at-property-p))
  15936. (call-interactively 'org-property-previous-allowed-value))
  15937. ((org-clocktable-try-shift 'left arg))
  15938. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  15939. (org-support-shift-select
  15940. (org-call-for-shift-select 'backward-char))
  15941. (t (org-shiftselect-error))))
  15942. (defun org-shiftcontrolright ()
  15943. "Switch to next TODO set."
  15944. (interactive)
  15945. (cond
  15946. ((and org-support-shift-select (org-region-active-p))
  15947. (org-call-for-shift-select 'forward-word))
  15948. ((and (not (eq org-support-shift-select 'always))
  15949. (org-on-heading-p))
  15950. (org-call-with-arg 'org-todo 'nextset))
  15951. (org-support-shift-select
  15952. (org-call-for-shift-select 'forward-word))
  15953. (t (org-shiftselect-error))))
  15954. (defun org-shiftcontrolleft ()
  15955. "Switch to previous TODO set."
  15956. (interactive)
  15957. (cond
  15958. ((and org-support-shift-select (org-region-active-p))
  15959. (org-call-for-shift-select 'backward-word))
  15960. ((and (not (eq org-support-shift-select 'always))
  15961. (org-on-heading-p))
  15962. (org-call-with-arg 'org-todo 'previousset))
  15963. (org-support-shift-select
  15964. (org-call-for-shift-select 'backward-word))
  15965. (t (org-shiftselect-error))))
  15966. (defun org-ctrl-c-ret ()
  15967. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  15968. (interactive)
  15969. (cond
  15970. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  15971. (t (call-interactively 'org-insert-heading))))
  15972. (defun org-copy-special ()
  15973. "Copy region in table or copy current subtree.
  15974. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  15975. See the individual commands for more information."
  15976. (interactive)
  15977. (call-interactively
  15978. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  15979. (defun org-cut-special ()
  15980. "Cut region in table or cut current subtree.
  15981. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  15982. See the individual commands for more information."
  15983. (interactive)
  15984. (call-interactively
  15985. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  15986. (defun org-paste-special (arg)
  15987. "Paste rectangular region into table, or past subtree relative to level.
  15988. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  15989. See the individual commands for more information."
  15990. (interactive "P")
  15991. (if (org-at-table-p)
  15992. (org-table-paste-rectangle)
  15993. (org-paste-subtree arg)))
  15994. (defun org-edit-special (&optional arg)
  15995. "Call a special editor for the stuff at point.
  15996. When at a table, call the formula editor with `org-table-edit-formulas'.
  15997. When at the first line of an src example, call `org-edit-src-code'.
  15998. When in an #+include line, visit the include file. Otherwise call
  15999. `ffap' to visit the file at point."
  16000. (interactive)
  16001. ;; possibly prep session before editing source
  16002. (when arg
  16003. (let* ((info (org-babel-get-src-block-info))
  16004. (lang (nth 0 info))
  16005. (params (nth 2 info))
  16006. (session (cdr (assoc :session params))))
  16007. (when (and info session) ;; we are in a source-code block with a session
  16008. (funcall
  16009. (intern (concat "org-babel-prep-session:" lang)) session params))))
  16010. (cond ;; proceed with `org-edit-special'
  16011. ((save-excursion
  16012. (beginning-of-line 1)
  16013. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  16014. (find-file (org-trim (match-string 1))))
  16015. ((org-edit-src-code))
  16016. ((org-edit-fixed-width-region))
  16017. ((org-at-table.el-p)
  16018. (org-edit-src-code))
  16019. ((or (org-at-table-p)
  16020. (save-excursion
  16021. (beginning-of-line 1)
  16022. (looking-at "[ \t]*#\\+TBLFM:")))
  16023. (call-interactively 'org-table-edit-formulas))
  16024. (t (call-interactively 'ffap))))
  16025. (defun org-ctrl-c-ctrl-c (&optional arg)
  16026. "Set tags in headline, or update according to changed information at point.
  16027. This command does many different things, depending on context:
  16028. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  16029. this is what we do.
  16030. - If the cursor is on a statistics cookie, update it.
  16031. - If the cursor is in a headline, prompt for tags and insert them
  16032. into the current line, aligned to `org-tags-column'. When called
  16033. with prefix arg, realign all tags in the current buffer.
  16034. - If the cursor is in one of the special #+KEYWORD lines, this
  16035. triggers scanning the buffer for these lines and updating the
  16036. information.
  16037. - If the cursor is inside a table, realign the table. This command
  16038. works even if the automatic table editor has been turned off.
  16039. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  16040. the entire table.
  16041. - If the cursor is at a footnote reference or definition, jump to
  16042. the corresponding definition or references, respectively.
  16043. - If the cursor is a the beginning of a dynamic block, update it.
  16044. - If the current buffer is a capture buffer, close note and file it.
  16045. - If the cursor is on a <<<target>>>, update radio targets and
  16046. corresponding links in this buffer.
  16047. - If the cursor is on a numbered item in a plain list, renumber the
  16048. ordered list.
  16049. - If the cursor is on a checkbox, toggle it.
  16050. - If the cursor is on a code block, evaluate it. The variable
  16051. `org-confirm-babel-evaluate' can be used to control prompting
  16052. before code block evaluation, by default every code block
  16053. evaluation requires confirmation. Code block evaluation can be
  16054. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  16055. (interactive "P")
  16056. (let ((org-enable-table-editor t))
  16057. (cond
  16058. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  16059. org-occur-highlights
  16060. org-latex-fragment-image-overlays)
  16061. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  16062. (org-remove-occur-highlights)
  16063. (org-remove-latex-fragment-image-overlays)
  16064. (message "Temporary highlights/overlays removed from current buffer"))
  16065. ((and (local-variable-p 'org-finish-function (current-buffer))
  16066. (fboundp org-finish-function))
  16067. (funcall org-finish-function))
  16068. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  16069. ((or (looking-at org-property-start-re)
  16070. (org-at-property-p))
  16071. (call-interactively 'org-property-action))
  16072. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  16073. ((and (org-in-regexp "\\[\\([0-9]*%\\|[0-9]*/[0-9]*\\)\\]")
  16074. (or (org-on-heading-p) (org-at-item-p)))
  16075. (call-interactively 'org-update-statistics-cookies))
  16076. ((org-on-heading-p) (call-interactively 'org-set-tags))
  16077. ((org-at-table.el-p)
  16078. (message "Use C-c ' to edit table.el tables"))
  16079. ((org-at-table-p)
  16080. (org-table-maybe-eval-formula)
  16081. (if arg
  16082. (call-interactively 'org-table-recalculate)
  16083. (org-table-maybe-recalculate-line))
  16084. (call-interactively 'org-table-align)
  16085. (orgtbl-send-table 'maybe))
  16086. ((or (org-footnote-at-reference-p)
  16087. (org-footnote-at-definition-p))
  16088. (call-interactively 'org-footnote-action))
  16089. ((org-at-item-checkbox-p)
  16090. ;; Cursor at a checkbox: repair list and update checkboxes. Send
  16091. ;; list only if at top item.
  16092. (let* ((cbox (match-string 1))
  16093. (struct (org-list-struct))
  16094. (old-struct (copy-tree struct))
  16095. (parents (org-list-parents-alist struct))
  16096. (prevs (org-list-prevs-alist struct))
  16097. (orderedp (org-entry-get nil "ORDERED"))
  16098. (firstp (= (org-list-get-top-point struct) (point-at-bol)))
  16099. block-item)
  16100. ;; Use a light version of `org-toggle-checkbox' to avoid
  16101. ;; computing list structure twice.
  16102. (org-list-set-checkbox (point-at-bol) struct
  16103. (cond
  16104. ((equal arg '(16)) "[-]")
  16105. ((equal arg '(4)) nil)
  16106. ((equal "[X]" cbox) "[ ]")
  16107. (t "[X]")))
  16108. (org-list-struct-fix-ind struct parents)
  16109. (org-list-struct-fix-bul struct prevs)
  16110. (setq block-item
  16111. (org-list-struct-fix-box struct parents prevs orderedp))
  16112. (when block-item
  16113. (message
  16114. "Checkboxes were removed due to unchecked box at line %d"
  16115. (org-current-line block-item)))
  16116. (org-list-struct-apply-struct struct old-struct)
  16117. (org-update-checkbox-count-maybe)
  16118. (when firstp (org-list-send-list 'maybe))))
  16119. ((org-at-item-p)
  16120. ;; Cursor at an item: repair list. Do checkbox related actions
  16121. ;; only if function was called with an argument. Send list only
  16122. ;; if at top item.
  16123. (let* ((struct (org-list-struct))
  16124. (old-struct (copy-tree struct))
  16125. (parents (org-list-parents-alist struct))
  16126. (prevs (org-list-prevs-alist struct))
  16127. (firstp (= (org-list-get-top-point struct) (point-at-bol))))
  16128. (org-list-struct-fix-ind struct parents)
  16129. (org-list-struct-fix-bul struct prevs)
  16130. (when arg
  16131. (org-list-set-checkbox (point-at-bol) struct "[ ]")
  16132. (org-list-struct-fix-box struct parents prevs))
  16133. (org-list-struct-apply-struct struct old-struct)
  16134. (when arg (org-update-checkbox-count-maybe))
  16135. (when firstp (org-list-send-list 'maybe))))
  16136. ((save-excursion (beginning-of-line 1) (looking-at org-dblock-start-re))
  16137. ;; Dynamic block
  16138. (beginning-of-line 1)
  16139. (save-excursion (org-update-dblock)))
  16140. ((save-excursion
  16141. (beginning-of-line 1)
  16142. (looking-at "[ \t]*#\\+\\([A-Z]+\\)"))
  16143. (cond
  16144. ((equal (match-string 1) "TBLFM")
  16145. ;; Recalculate the table before this line
  16146. (save-excursion
  16147. (beginning-of-line 1)
  16148. (skip-chars-backward " \r\n\t")
  16149. (if (org-at-table-p)
  16150. (org-call-with-arg 'org-table-recalculate (or arg t)))))
  16151. (t
  16152. (let ((org-inhibit-startup-visibility-stuff t)
  16153. (org-startup-align-all-tables nil))
  16154. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  16155. (message "Local setup has been refreshed"))))
  16156. ((org-clock-update-time-maybe))
  16157. (t (error "C-c C-c can do nothing useful at this location")))))
  16158. (defun org-mode-restart ()
  16159. "Restart Org-mode, to scan again for special lines.
  16160. Also updates the keyword regular expressions."
  16161. (interactive)
  16162. (org-mode)
  16163. (message "Org-mode restarted"))
  16164. (defun org-kill-note-or-show-branches ()
  16165. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  16166. (interactive)
  16167. (if (not org-finish-function)
  16168. (progn
  16169. (hide-subtree)
  16170. (call-interactively 'show-branches))
  16171. (let ((org-note-abort t))
  16172. (funcall org-finish-function))))
  16173. (defun org-return (&optional indent)
  16174. "Goto next table row or insert a newline.
  16175. Calls `org-table-next-row' or `newline', depending on context.
  16176. See the individual commands for more information."
  16177. (interactive)
  16178. (cond
  16179. ((bobp) (if indent (newline-and-indent) (newline)))
  16180. ((org-at-table-p)
  16181. (org-table-justify-field-maybe)
  16182. (call-interactively 'org-table-next-row))
  16183. ;; when `newline-and-indent' is called within a list, make sure
  16184. ;; text moved stays inside the item.
  16185. ((and (org-in-item-p) indent)
  16186. (if (and (org-at-item-p) (>= (point) (match-end 0)))
  16187. (progn
  16188. (newline)
  16189. (org-indent-line-to (length (match-string 0))))
  16190. (let ((ind (org-get-indentation)))
  16191. (newline)
  16192. (if (org-looking-back org-list-end-re)
  16193. (org-indent-line-function)
  16194. (org-indent-line-to ind)))))
  16195. ((and org-return-follows-link
  16196. (eq (get-text-property (point) 'face) 'org-link))
  16197. (call-interactively 'org-open-at-point))
  16198. ((and (org-at-heading-p)
  16199. (looking-at
  16200. (org-re "\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")))
  16201. (org-show-entry)
  16202. (end-of-line 1)
  16203. (newline))
  16204. (t (if indent (newline-and-indent) (newline)))))
  16205. (defun org-return-indent ()
  16206. "Goto next table row or insert a newline and indent.
  16207. Calls `org-table-next-row' or `newline-and-indent', depending on
  16208. context. See the individual commands for more information."
  16209. (interactive)
  16210. (org-return t))
  16211. (defun org-ctrl-c-star ()
  16212. "Compute table, or change heading status of lines.
  16213. Calls `org-table-recalculate' or `org-toggle-heading',
  16214. depending on context."
  16215. (interactive)
  16216. (cond
  16217. ((org-at-table-p)
  16218. (call-interactively 'org-table-recalculate))
  16219. (t
  16220. ;; Convert all lines in region to list items
  16221. (call-interactively 'org-toggle-heading))))
  16222. (defun org-ctrl-c-minus ()
  16223. "Insert separator line in table or modify bullet status of line.
  16224. Also turns a plain line or a region of lines into list items.
  16225. Calls `org-table-insert-hline', `org-toggle-item', or
  16226. `org-cycle-list-bullet', depending on context."
  16227. (interactive)
  16228. (cond
  16229. ((org-at-table-p)
  16230. (call-interactively 'org-table-insert-hline))
  16231. ((org-region-active-p)
  16232. (call-interactively 'org-toggle-item))
  16233. ((org-in-item-p)
  16234. (call-interactively 'org-cycle-list-bullet))
  16235. (t
  16236. (call-interactively 'org-toggle-item))))
  16237. (defun org-toggle-item (arg)
  16238. "Convert headings or normal lines to items, items to normal lines.
  16239. If there is no active region, only the current line is considered.
  16240. If the first non blank line in the region is an headline, convert
  16241. all headlines to items.
  16242. If it is an item, convert all items to normal lines.
  16243. If it is normal text, change region into an item. With a prefix
  16244. argument ARG, change each line in region into an item."
  16245. (interactive "P")
  16246. (let (l2 l beg end)
  16247. (if (org-region-active-p)
  16248. (setq beg (region-beginning) end (region-end))
  16249. (setq beg (point-at-bol)
  16250. end (min (1+ (point-at-eol)) (point-max))))
  16251. (org-with-limited-levels
  16252. (save-excursion
  16253. (goto-char end)
  16254. (setq l2 (org-current-line))
  16255. (goto-char beg)
  16256. (beginning-of-line 1)
  16257. ;; Ignore blank lines at beginning of region
  16258. (skip-chars-forward " \t\r\n")
  16259. (beginning-of-line 1)
  16260. (setq l (1- (org-current-line)))
  16261. (cond
  16262. ;; Case 1. Start at an item: de-itemize.
  16263. ((org-at-item-p)
  16264. (while (< (setq l (1+ l)) l2)
  16265. (when (org-at-item-p)
  16266. (skip-chars-forward " \t")
  16267. (delete-region (point) (match-end 0)))
  16268. (beginning-of-line 2)))
  16269. ;; Case 2. Start an an heading: convert to items.
  16270. ((org-on-heading-p)
  16271. (let* ((bul (org-list-bullet-string "-"))
  16272. (len (length bul))
  16273. (ind 0) (level 0))
  16274. (while (< (setq l (1+ l)) l2)
  16275. (cond
  16276. ((looking-at outline-regexp)
  16277. (let* ((lvl (org-reduced-level
  16278. (- (length (match-string 0)) 2)))
  16279. (s (concat (make-string (* len lvl) ? ) bul)))
  16280. (replace-match s t t)
  16281. (setq ind (length s) level lvl)))
  16282. ;; Ignore blank lines and inline tasks.
  16283. ((looking-at "^[ \t]*$"))
  16284. ((looking-at "^\\*+ "))
  16285. ;; Ensure normal text belongs to the new item.
  16286. (t (org-indent-line-to (+ (max (- (org-get-indentation) level 2) 0)
  16287. ind))))
  16288. (beginning-of-line 2))))
  16289. ;; Case 3. Normal line with ARG: turn each of them into items
  16290. ;; unless they are already one.
  16291. (arg
  16292. (while (< (setq l (1+ l)) l2)
  16293. (unless (or (org-on-heading-p) (org-at-item-p))
  16294. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16295. (replace-match
  16296. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  16297. (beginning-of-line 2)))
  16298. ;; Case 4. Normal line without ARG: make the first line of
  16299. ;; region an item, and shift indentation of others
  16300. ;; lines to set them as item's body.
  16301. (t (let* ((bul (org-list-bullet-string "-"))
  16302. (bul-len (length bul))
  16303. (ref-ind (org-get-indentation)))
  16304. (skip-chars-forward " \t")
  16305. (insert bul)
  16306. (beginning-of-line 2)
  16307. (while (and (< (setq l (1+ l)) l2) (< (point) end))
  16308. ;; Ensure that lines less indented than first one
  16309. ;; still get included in item body.
  16310. (org-indent-line-to (+ (max ref-ind (org-get-indentation))
  16311. bul-len))
  16312. (beginning-of-line 2)))))))))
  16313. (defun org-toggle-heading (&optional nstars)
  16314. "Convert headings to normal text, or items or text to headings.
  16315. If there is no active region, only the current line is considered.
  16316. If the first non blank line is an headline, remove the stars from
  16317. all headlines in the region.
  16318. If it is a plain list item, turn all plain list items into headings.
  16319. If it is a normal line, turn each and every normal line (i.e. not
  16320. an heading or an item) in the region into a heading.
  16321. When converting a line into a heading, the number of stars is chosen
  16322. such that the lines become children of the current entry. However,
  16323. when a prefix argument is given, its value determines the number of
  16324. stars to add."
  16325. (interactive "P")
  16326. (let (l2 l itemp beg end)
  16327. (if (org-region-active-p)
  16328. (setq beg (region-beginning) end (copy-marker (region-end)))
  16329. (setq beg (point-at-bol)
  16330. end (min (1+ (point-at-eol)) (point-max))))
  16331. ;; Ensure inline tasks don't count as headings.
  16332. (org-with-limited-levels
  16333. (save-excursion
  16334. (goto-char end)
  16335. (setq l2 (org-current-line))
  16336. (goto-char beg)
  16337. (beginning-of-line 1)
  16338. ;; Ignore blank lines at beginning of region
  16339. (skip-chars-forward " \t\r\n")
  16340. (beginning-of-line 1)
  16341. (setq l (1- (org-current-line)))
  16342. (cond
  16343. ;; Case 1. Started at an heading: de-star headings.
  16344. ((org-on-heading-p)
  16345. (while (< (setq l (1+ l)) l2)
  16346. (when (org-on-heading-p t)
  16347. (looking-at outline-regexp) (replace-match ""))
  16348. (beginning-of-line 2)))
  16349. ;; Case 2. Started at an item: change items into headlines.
  16350. ((org-at-item-p)
  16351. (let ((stars (make-string
  16352. (if nstars
  16353. (prefix-numeric-value current-prefix-arg)
  16354. (or (org-current-level) 0))
  16355. ?*)))
  16356. (while (< (point) end)
  16357. (when (org-at-item-p)
  16358. ;; Pay attention to cases when region ends before list.
  16359. (let* ((struct (org-list-struct))
  16360. (list-end (min (org-list-get-bottom-point struct) end)))
  16361. (save-restriction
  16362. (narrow-to-region (point) list-end)
  16363. (insert
  16364. (org-list-to-subtree
  16365. (org-list-parse-list t)
  16366. '(:istart (concat stars (funcall get-stars depth))
  16367. :icount (concat stars
  16368. (funcall get-stars depth))))))))
  16369. (beginning-of-line 2))))
  16370. ;; Case 3. Started at normal text: make every line an heading,
  16371. ;; skipping headlines and items.
  16372. (t (let* ((stars (make-string
  16373. (if nstars
  16374. (prefix-numeric-value current-prefix-arg)
  16375. (or (org-current-level) 0))
  16376. ?*))
  16377. (add-stars (cond (nstars "")
  16378. ((equal stars "") "*")
  16379. (org-odd-levels-only "**")
  16380. (t "*")))
  16381. (rpl (concat stars add-stars " ")))
  16382. (while (< (setq l (1+ l)) l2)
  16383. (unless (or (org-on-heading-p) (org-at-item-p))
  16384. (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  16385. (replace-match (concat rpl (match-string 2)))))
  16386. (beginning-of-line 2)))))))))
  16387. (defun org-meta-return (&optional arg)
  16388. "Insert a new heading or wrap a region in a table.
  16389. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  16390. See the individual commands for more information."
  16391. (interactive "P")
  16392. (cond
  16393. ((run-hook-with-args-until-success 'org-metareturn-hook))
  16394. ((org-at-table-p)
  16395. (call-interactively 'org-table-wrap-region))
  16396. (t (call-interactively 'org-insert-heading))))
  16397. ;;; Menu entries
  16398. ;; Define the Org-mode menus
  16399. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  16400. '("Tbl"
  16401. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  16402. ["Next Field" org-cycle (org-at-table-p)]
  16403. ["Previous Field" org-shifttab (org-at-table-p)]
  16404. ["Next Row" org-return (org-at-table-p)]
  16405. "--"
  16406. ["Blank Field" org-table-blank-field (org-at-table-p)]
  16407. ["Edit Field" org-table-edit-field (org-at-table-p)]
  16408. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  16409. "--"
  16410. ("Column"
  16411. ["Move Column Left" org-metaleft (org-at-table-p)]
  16412. ["Move Column Right" org-metaright (org-at-table-p)]
  16413. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  16414. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  16415. ("Row"
  16416. ["Move Row Up" org-metaup (org-at-table-p)]
  16417. ["Move Row Down" org-metadown (org-at-table-p)]
  16418. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  16419. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  16420. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  16421. "--"
  16422. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  16423. ("Rectangle"
  16424. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  16425. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  16426. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  16427. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  16428. "--"
  16429. ("Calculate"
  16430. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  16431. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  16432. ["Edit Formulas" org-edit-special (org-at-table-p)]
  16433. "--"
  16434. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  16435. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  16436. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  16437. "--"
  16438. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  16439. "--"
  16440. ["Sum Column/Rectangle" org-table-sum
  16441. (or (org-at-table-p) (org-region-active-p))]
  16442. ["Which Column?" org-table-current-column (org-at-table-p)])
  16443. ["Debug Formulas"
  16444. org-table-toggle-formula-debugger
  16445. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  16446. ["Show Col/Row Numbers"
  16447. org-table-toggle-coordinate-overlays
  16448. :style toggle
  16449. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  16450. "--"
  16451. ["Create" org-table-create (and (not (org-at-table-p))
  16452. org-enable-table-editor)]
  16453. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  16454. ["Import from File" org-table-import (not (org-at-table-p))]
  16455. ["Export to File" org-table-export (org-at-table-p)]
  16456. "--"
  16457. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  16458. (easy-menu-define org-org-menu org-mode-map "Org menu"
  16459. '("Org"
  16460. ("Show/Hide"
  16461. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  16462. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  16463. ["Sparse Tree..." org-sparse-tree t]
  16464. ["Reveal Context" org-reveal t]
  16465. ["Show All" show-all t]
  16466. "--"
  16467. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  16468. "--"
  16469. ["New Heading" org-insert-heading t]
  16470. ("Navigate Headings"
  16471. ["Up" outline-up-heading t]
  16472. ["Next" outline-next-visible-heading t]
  16473. ["Previous" outline-previous-visible-heading t]
  16474. ["Next Same Level" outline-forward-same-level t]
  16475. ["Previous Same Level" outline-backward-same-level t]
  16476. "--"
  16477. ["Jump" org-goto t])
  16478. ("Edit Structure"
  16479. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  16480. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  16481. "--"
  16482. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  16483. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  16484. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  16485. "--"
  16486. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  16487. "--"
  16488. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  16489. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  16490. ["Demote Heading" org-metaright (not (org-at-table-p))]
  16491. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  16492. "--"
  16493. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  16494. "--"
  16495. ["Convert to odd levels" org-convert-to-odd-levels t]
  16496. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  16497. ("Editing"
  16498. ["Emphasis..." org-emphasize t]
  16499. ["Edit Source Example" org-edit-special t]
  16500. "--"
  16501. ["Footnote new/jump" org-footnote-action t]
  16502. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  16503. ("Archive"
  16504. ["Archive (default method)" org-archive-subtree-default t]
  16505. "--"
  16506. ["Move Subtree to Archive file" org-advertized-archive-subtree t]
  16507. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  16508. ["Move subtree to Archive sibling" org-archive-to-archive-sibling t]
  16509. )
  16510. "--"
  16511. ("Hyperlinks"
  16512. ["Store Link (Global)" org-store-link t]
  16513. ["Find existing link to here" org-occur-link-in-agenda-files t]
  16514. ["Insert Link" org-insert-link t]
  16515. ["Follow Link" org-open-at-point t]
  16516. "--"
  16517. ["Next link" org-next-link t]
  16518. ["Previous link" org-previous-link t]
  16519. "--"
  16520. ["Descriptive Links"
  16521. (progn (add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  16522. :style radio
  16523. :selected (member '(org-link) buffer-invisibility-spec)]
  16524. ["Literal Links"
  16525. (progn
  16526. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  16527. :style radio
  16528. :selected (not (member '(org-link) buffer-invisibility-spec))])
  16529. "--"
  16530. ("TODO Lists"
  16531. ["TODO/DONE/-" org-todo t]
  16532. ("Select keyword"
  16533. ["Next keyword" org-shiftright (org-on-heading-p)]
  16534. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  16535. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  16536. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  16537. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  16538. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  16539. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  16540. "--"
  16541. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  16542. :selected org-enforce-todo-dependencies :style toggle :active t]
  16543. "Settings for tree at point"
  16544. ["Do Children sequentially" org-toggle-ordered-property :style radio
  16545. :selected (org-entry-get nil "ORDERED")
  16546. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16547. ["Do Children parallel" org-toggle-ordered-property :style radio
  16548. :selected (not (org-entry-get nil "ORDERED"))
  16549. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  16550. "--"
  16551. ["Set Priority" org-priority t]
  16552. ["Priority Up" org-shiftup t]
  16553. ["Priority Down" org-shiftdown t]
  16554. "--"
  16555. ["Get news from all feeds" org-feed-update-all t]
  16556. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  16557. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  16558. ("TAGS and Properties"
  16559. ["Set Tags" org-set-tags-command t]
  16560. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  16561. "--"
  16562. ["Set property" org-set-property t]
  16563. ["Column view of properties" org-columns t]
  16564. ["Insert Column View DBlock" org-insert-columns-dblock t])
  16565. ("Dates and Scheduling"
  16566. ["Timestamp" org-time-stamp t]
  16567. ["Timestamp (inactive)" org-time-stamp-inactive t]
  16568. ("Change Date"
  16569. ["1 Day Later" org-shiftright t]
  16570. ["1 Day Earlier" org-shiftleft t]
  16571. ["1 ... Later" org-shiftup t]
  16572. ["1 ... Earlier" org-shiftdown t])
  16573. ["Compute Time Range" org-evaluate-time-range t]
  16574. ["Schedule Item" org-schedule t]
  16575. ["Deadline" org-deadline t]
  16576. "--"
  16577. ["Custom time format" org-toggle-time-stamp-overlays
  16578. :style radio :selected org-display-custom-times]
  16579. "--"
  16580. ["Goto Calendar" org-goto-calendar t]
  16581. ["Date from Calendar" org-date-from-calendar t]
  16582. "--"
  16583. ["Start/Restart Timer" org-timer-start t]
  16584. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  16585. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  16586. ["Insert Timer String" org-timer t]
  16587. ["Insert Timer Item" org-timer-item t])
  16588. ("Logging work"
  16589. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  16590. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  16591. ["Clock out" org-clock-out t]
  16592. ["Clock cancel" org-clock-cancel t]
  16593. "--"
  16594. ["Mark as default task" org-clock-mark-default-task t]
  16595. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  16596. ["Goto running clock" org-clock-goto t]
  16597. "--"
  16598. ["Display times" org-clock-display t]
  16599. ["Create clock table" org-clock-report t]
  16600. "--"
  16601. ["Record DONE time"
  16602. (progn (setq org-log-done (not org-log-done))
  16603. (message "Switching to %s will %s record a timestamp"
  16604. (car org-done-keywords)
  16605. (if org-log-done "automatically" "not")))
  16606. :style toggle :selected org-log-done])
  16607. "--"
  16608. ["Agenda Command..." org-agenda t]
  16609. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  16610. ("File List for Agenda")
  16611. ("Special views current file"
  16612. ["TODO Tree" org-show-todo-tree t]
  16613. ["Check Deadlines" org-check-deadlines t]
  16614. ["Timeline" org-timeline t]
  16615. ["Tags/Property tree" org-match-sparse-tree t])
  16616. "--"
  16617. ["Export/Publish..." org-export t]
  16618. ("LaTeX"
  16619. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  16620. :selected org-cdlatex-mode]
  16621. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  16622. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  16623. ["Modify math symbol" org-cdlatex-math-modify
  16624. (org-inside-LaTeX-fragment-p)]
  16625. ["Insert citation" org-reftex-citation t]
  16626. "--"
  16627. ["Template for BEAMER" org-insert-beamer-options-template t])
  16628. "--"
  16629. ("MobileOrg"
  16630. ["Push Files and Views" org-mobile-push t]
  16631. ["Get Captured and Flagged" org-mobile-pull t]
  16632. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  16633. "--"
  16634. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  16635. "--"
  16636. ("Documentation"
  16637. ["Show Version" org-version t]
  16638. ["Info Documentation" org-info t])
  16639. ("Customize"
  16640. ["Browse Org Group" org-customize t]
  16641. "--"
  16642. ["Expand This Menu" org-create-customize-menu
  16643. (fboundp 'customize-menu-create)])
  16644. ["Send bug report" org-submit-bug-report t]
  16645. "--"
  16646. ("Refresh/Reload"
  16647. ["Refresh setup current buffer" org-mode-restart t]
  16648. ["Reload Org (after update)" org-reload t]
  16649. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x r"])
  16650. ))
  16651. (defun org-info (&optional node)
  16652. "Read documentation for Org-mode in the info system.
  16653. With optional NODE, go directly to that node."
  16654. (interactive)
  16655. (info (format "(org)%s" (or node ""))))
  16656. ;;;###autoload
  16657. (defun org-submit-bug-report ()
  16658. "Submit a bug report on Org-mode via mail.
  16659. Don't hesitate to report any problems or inaccurate documentation.
  16660. If you don't have setup sending mail from (X)Emacs, please copy the
  16661. output buffer into your mail program, as it gives us important
  16662. information about your Org-mode version and configuration."
  16663. (interactive)
  16664. (require 'reporter)
  16665. (org-load-modules-maybe)
  16666. (org-require-autoloaded-modules)
  16667. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  16668. (reporter-submit-bug-report
  16669. "emacs-orgmode@gnu.org"
  16670. (org-version)
  16671. (let (list)
  16672. (save-window-excursion
  16673. (switch-to-buffer (get-buffer-create "*Warn about privacy*"))
  16674. (delete-other-windows)
  16675. (erase-buffer)
  16676. (insert "You are about to submit a bug report to the Org-mode mailing list.
  16677. We would like to add your full Org-mode and Outline configuration to the
  16678. bug report. This greatly simplifies the work of the maintainer and
  16679. other experts on the mailing list.
  16680. HOWEVER, some variables you have customized may contain private
  16681. information. The names of customers, colleagues, or friends, might
  16682. appear in the form of file names, tags, todo states, or search strings.
  16683. If you answer yes to the prompt, you might want to check and remove
  16684. such private information before sending the email.")
  16685. (add-text-properties (point-min) (point-max) '(face org-warning))
  16686. (when (yes-or-no-p "Include your Org-mode configuration ")
  16687. (mapatoms
  16688. (lambda (v)
  16689. (and (boundp v)
  16690. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  16691. (or (and (symbol-value v)
  16692. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  16693. (and
  16694. (get v 'custom-type) (get v 'standard-value)
  16695. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  16696. (push v list)))))
  16697. (kill-buffer (get-buffer "*Warn about privacy*"))
  16698. list))
  16699. nil nil
  16700. "Remember to cover the basics, that is, what you expected to happen and
  16701. what in fact did happen. You don't know how to make a good report? See
  16702. http://orgmode.org/manual/Feedback.html#Feedback
  16703. Your bug report will be posted to the Org-mode mailing list.
  16704. ------------------------------------------------------------------------")
  16705. (save-excursion
  16706. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  16707. (replace-match "\\1Bug: \\3 [\\2]")))))
  16708. (defun org-install-agenda-files-menu ()
  16709. (let ((bl (buffer-list)))
  16710. (save-excursion
  16711. (while bl
  16712. (set-buffer (pop bl))
  16713. (if (org-mode-p) (setq bl nil)))
  16714. (when (org-mode-p)
  16715. (easy-menu-change
  16716. '("Org") "File List for Agenda"
  16717. (append
  16718. (list
  16719. ["Edit File List" (org-edit-agenda-file-list) t]
  16720. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  16721. ["Remove Current File from List" org-remove-file t]
  16722. ["Cycle through agenda files" org-cycle-agenda-files t]
  16723. ["Occur in all agenda files" org-occur-in-agenda-files t]
  16724. "--")
  16725. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  16726. ;;;; Documentation
  16727. ;;;###autoload
  16728. (defun org-require-autoloaded-modules ()
  16729. (interactive)
  16730. (mapc 'require
  16731. '(org-agenda org-archive org-ascii org-attach org-clock org-colview
  16732. org-docbook org-exp org-html org-icalendar
  16733. org-id org-latex
  16734. org-publish org-remember org-table
  16735. org-timer org-xoxo)))
  16736. ;;;###autoload
  16737. (defun org-reload (&optional uncompiled)
  16738. "Reload all org lisp files.
  16739. With prefix arg UNCOMPILED, load the uncompiled versions."
  16740. (interactive "P")
  16741. (require 'find-func)
  16742. (let* ((file-re "^\\(org\\|orgtbl\\)\\(\\.el\\|-.*\\.el\\)")
  16743. (dir-org (file-name-directory (org-find-library-name "org")))
  16744. (dir-org-contrib (ignore-errors
  16745. (file-name-directory
  16746. (org-find-library-name "org-contribdir"))))
  16747. (babel-files
  16748. (mapcar (lambda (el) (concat "ob" (when el (format "-%s" el)) ".el"))
  16749. (append (list nil "comint" "eval" "exp" "keys"
  16750. "lob" "ref" "table" "tangle")
  16751. (delq nil
  16752. (mapcar
  16753. (lambda (lang)
  16754. (when (cdr lang) (symbol-name (car lang))))
  16755. org-babel-load-languages)))))
  16756. (files
  16757. (append (directory-files dir-org t file-re)
  16758. babel-files
  16759. (and dir-org-contrib
  16760. (directory-files dir-org-contrib t file-re))))
  16761. (remove-re (concat (if (featurep 'xemacs)
  16762. "org-colview" "org-colview-xemacs")
  16763. "\\'")))
  16764. (setq files (mapcar 'file-name-sans-extension files))
  16765. (setq files (mapcar
  16766. (lambda (x) (if (string-match remove-re x) nil x))
  16767. files))
  16768. (setq files (delq nil files))
  16769. (mapc
  16770. (lambda (f)
  16771. (when (featurep (intern (file-name-nondirectory f)))
  16772. (if (and (not uncompiled)
  16773. (file-exists-p (concat f ".elc")))
  16774. (load (concat f ".elc") nil nil t)
  16775. (load (concat f ".el") nil nil t))))
  16776. files))
  16777. (org-version))
  16778. ;;;###autoload
  16779. (defun org-customize ()
  16780. "Call the customize function with org as argument."
  16781. (interactive)
  16782. (org-load-modules-maybe)
  16783. (org-require-autoloaded-modules)
  16784. (customize-browse 'org))
  16785. (defun org-create-customize-menu ()
  16786. "Create a full customization menu for Org-mode, insert it into the menu."
  16787. (interactive)
  16788. (org-load-modules-maybe)
  16789. (org-require-autoloaded-modules)
  16790. (if (fboundp 'customize-menu-create)
  16791. (progn
  16792. (easy-menu-change
  16793. '("Org") "Customize"
  16794. `(["Browse Org group" org-customize t]
  16795. "--"
  16796. ,(customize-menu-create 'org)
  16797. ["Set" Custom-set t]
  16798. ["Save" Custom-save t]
  16799. ["Reset to Current" Custom-reset-current t]
  16800. ["Reset to Saved" Custom-reset-saved t]
  16801. ["Reset to Standard Settings" Custom-reset-standard t]))
  16802. (message "\"Org\"-menu now contains full customization menu"))
  16803. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  16804. ;;;; Miscellaneous stuff
  16805. ;;; Generally useful functions
  16806. (defun org-get-at-bol (property)
  16807. "Get text property PROPERTY at beginning of line."
  16808. (get-text-property (point-at-bol) property))
  16809. (defun org-find-text-property-in-string (prop s)
  16810. "Return the first non-nil value of property PROP in string S."
  16811. (or (get-text-property 0 prop s)
  16812. (get-text-property (or (next-single-property-change 0 prop s) 0)
  16813. prop s)))
  16814. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  16815. "Display the given MESSAGE as a warning."
  16816. (if (fboundp 'display-warning)
  16817. (display-warning 'org message
  16818. (if (featurep 'xemacs) 'warning :warning))
  16819. (let ((buf (get-buffer-create "*Org warnings*")))
  16820. (with-current-buffer buf
  16821. (goto-char (point-max))
  16822. (insert "Warning (Org): " message)
  16823. (unless (bolp)
  16824. (newline)))
  16825. (display-buffer buf)
  16826. (sit-for 0))))
  16827. (defun org-eval (form)
  16828. "Eval FORM and return result."
  16829. (condition-case error
  16830. (eval form)
  16831. (error (format "%%![Error: %s]" error))))
  16832. (defun org-in-commented-line ()
  16833. "Is point in a line starting with `#'?"
  16834. (equal (char-after (point-at-bol)) ?#))
  16835. (defun org-in-indented-comment-line ()
  16836. "Is point in a line starting with `#' after some white space?"
  16837. (save-excursion
  16838. (save-match-data
  16839. (goto-char (point-at-bol))
  16840. (looking-at "[ \t]*#"))))
  16841. (defun org-in-verbatim-emphasis ()
  16842. (save-match-data
  16843. (and (org-in-regexp org-emph-re 2) (member (match-string 3) '("=" "~")))))
  16844. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  16845. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  16846. (if (and marker (marker-buffer marker)
  16847. (buffer-live-p (marker-buffer marker)))
  16848. (progn
  16849. (switch-to-buffer (marker-buffer marker))
  16850. (if (or (> marker (point-max)) (< marker (point-min)))
  16851. (widen))
  16852. (goto-char marker)
  16853. (org-show-context 'org-goto))
  16854. (if bookmark
  16855. (bookmark-jump bookmark)
  16856. (error "Cannot find location"))))
  16857. (defun org-quote-csv-field (s)
  16858. "Quote field for inclusion in CSV material."
  16859. (if (string-match "[\",]" s)
  16860. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  16861. s))
  16862. (defun org-force-self-insert (N)
  16863. "Needed to enforce self-insert under remapping."
  16864. (interactive "p")
  16865. (self-insert-command N))
  16866. (defun org-string-width (s)
  16867. "Compute width of string, ignoring invisible characters.
  16868. This ignores character with invisibility property `org-link', and also
  16869. characters with property `org-cwidth', because these will become invisible
  16870. upon the next fontification round."
  16871. (let (b l)
  16872. (when (or (eq t buffer-invisibility-spec)
  16873. (assq 'org-link buffer-invisibility-spec))
  16874. (while (setq b (text-property-any 0 (length s)
  16875. 'invisible 'org-link s))
  16876. (setq s (concat (substring s 0 b)
  16877. (substring s (or (next-single-property-change
  16878. b 'invisible s) (length s)))))))
  16879. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  16880. (setq s (concat (substring s 0 b)
  16881. (substring s (or (next-single-property-change
  16882. b 'org-cwidth s) (length s))))))
  16883. (setq l (string-width s) b -1)
  16884. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  16885. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  16886. l))
  16887. (defun org-shorten-string (s maxlength)
  16888. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  16889. If the string is shorter or has length MAXLENGTH, just return the
  16890. original string. If it is longer, the functions finds a space in the
  16891. string, breaks this string off at that locations and adds three dots
  16892. as ellipsis. Including the ellipsis, the string will not be longer
  16893. than MAXLENGTH. If finding a good breaking point in the string does
  16894. not work, the string is just chopped off in the middle of a word
  16895. if necessary."
  16896. (if (<= (length s) maxlength)
  16897. s
  16898. (let* ((n (max (- maxlength 4) 1))
  16899. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  16900. (if (string-match re s)
  16901. (concat (match-string 1 s) "...")
  16902. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  16903. (defun org-get-indentation (&optional line)
  16904. "Get the indentation of the current line, interpreting tabs.
  16905. When LINE is given, assume it represents a line and compute its indentation."
  16906. (if line
  16907. (if (string-match "^ *" (org-remove-tabs line))
  16908. (match-end 0))
  16909. (save-excursion
  16910. (beginning-of-line 1)
  16911. (skip-chars-forward " \t")
  16912. (current-column))))
  16913. (defun org-get-string-indentation (s)
  16914. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  16915. (let ((n -1) (i 0) (w tab-width) c)
  16916. (catch 'exit
  16917. (while (< (setq n (1+ n)) (length s))
  16918. (setq c (aref s n))
  16919. (cond ((= c ?\ ) (setq i (1+ i)))
  16920. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  16921. (t (throw 'exit t)))))
  16922. i))
  16923. (defun org-remove-tabs (s &optional width)
  16924. "Replace tabulators in S with spaces.
  16925. Assumes that s is a single line, starting in column 0."
  16926. (setq width (or width tab-width))
  16927. (while (string-match "\t" s)
  16928. (setq s (replace-match
  16929. (make-string
  16930. (- (* width (/ (+ (match-beginning 0) width) width))
  16931. (match-beginning 0)) ?\ )
  16932. t t s)))
  16933. s)
  16934. (defun org-fix-indentation (line ind)
  16935. "Fix indentation in LINE.
  16936. IND is a cons cell with target and minimum indentation.
  16937. If the current indentation in LINE is smaller than the minimum,
  16938. leave it alone. If it is larger than ind, set it to the target."
  16939. (let* ((l (org-remove-tabs line))
  16940. (i (org-get-indentation l))
  16941. (i1 (car ind)) (i2 (cdr ind)))
  16942. (if (>= i i2) (setq l (substring line i2)))
  16943. (if (> i1 0)
  16944. (concat (make-string i1 ?\ ) l)
  16945. l)))
  16946. (defun org-remove-indentation (code &optional n)
  16947. "Remove the maximum common indentation from the lines in CODE.
  16948. N may optionally be the number of spaces to remove."
  16949. (with-temp-buffer
  16950. (insert code)
  16951. (org-do-remove-indentation n)
  16952. (buffer-string)))
  16953. (defun org-do-remove-indentation (&optional n)
  16954. "Remove the maximum common indentation from the buffer."
  16955. (untabify (point-min) (point-max))
  16956. (let ((min 10000) re)
  16957. (if n
  16958. (setq min n)
  16959. (goto-char (point-min))
  16960. (while (re-search-forward "^ *[^ \n]" nil t)
  16961. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  16962. (unless (or (= min 0) (= min 10000))
  16963. (setq re (format "^ \\{%d\\}" min))
  16964. (goto-char (point-min))
  16965. (while (re-search-forward re nil t)
  16966. (replace-match "")
  16967. (end-of-line 1))
  16968. min)))
  16969. (defun org-fill-template (template alist)
  16970. "Find each %key of ALIST in TEMPLATE and replace it."
  16971. (let ((case-fold-search nil)
  16972. entry key value)
  16973. (setq alist (sort (copy-sequence alist)
  16974. (lambda (a b) (< (length (car a)) (length (car b))))))
  16975. (while (setq entry (pop alist))
  16976. (setq template
  16977. (replace-regexp-in-string
  16978. (concat "%" (regexp-quote (car entry)))
  16979. (cdr entry) template t t)))
  16980. template))
  16981. (defun org-base-buffer (buffer)
  16982. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  16983. (if (not buffer)
  16984. buffer
  16985. (or (buffer-base-buffer buffer)
  16986. buffer)))
  16987. (defun org-trim (s)
  16988. "Remove whitespace at beginning and end of string."
  16989. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  16990. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  16991. s)
  16992. (defun org-wrap (string &optional width lines)
  16993. "Wrap string to either a number of lines, or a width in characters.
  16994. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  16995. that costs. If there is a word longer than WIDTH, the text is actually
  16996. wrapped to the length of that word.
  16997. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  16998. many lines, whatever width that takes.
  16999. The return value is a list of lines, without newlines at the end."
  17000. (let* ((words (org-split-string string "[ \t\n]+"))
  17001. (maxword (apply 'max (mapcar 'org-string-width words)))
  17002. w ll)
  17003. (cond (width
  17004. (org-do-wrap words (max maxword width)))
  17005. (lines
  17006. (setq w maxword)
  17007. (setq ll (org-do-wrap words maxword))
  17008. (if (<= (length ll) lines)
  17009. ll
  17010. (setq ll words)
  17011. (while (> (length ll) lines)
  17012. (setq w (1+ w))
  17013. (setq ll (org-do-wrap words w)))
  17014. ll))
  17015. (t (error "Cannot wrap this")))))
  17016. (defun org-do-wrap (words width)
  17017. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  17018. (let (lines line)
  17019. (while words
  17020. (setq line (pop words))
  17021. (while (and words (< (+ (length line) (length (car words))) width))
  17022. (setq line (concat line " " (pop words))))
  17023. (setq lines (push line lines)))
  17024. (nreverse lines)))
  17025. (defun org-split-string (string &optional separators)
  17026. "Splits STRING into substrings at SEPARATORS.
  17027. No empty strings are returned if there are matches at the beginning
  17028. and end of string."
  17029. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  17030. (start 0)
  17031. notfirst
  17032. (list nil))
  17033. (while (and (string-match rexp string
  17034. (if (and notfirst
  17035. (= start (match-beginning 0))
  17036. (< start (length string)))
  17037. (1+ start) start))
  17038. (< (match-beginning 0) (length string)))
  17039. (setq notfirst t)
  17040. (or (eq (match-beginning 0) 0)
  17041. (and (eq (match-beginning 0) (match-end 0))
  17042. (eq (match-beginning 0) start))
  17043. (setq list
  17044. (cons (substring string start (match-beginning 0))
  17045. list)))
  17046. (setq start (match-end 0)))
  17047. (or (eq start (length string))
  17048. (setq list
  17049. (cons (substring string start)
  17050. list)))
  17051. (nreverse list)))
  17052. (defun org-quote-vert (s)
  17053. "Replace \"|\" with \"\\vert\"."
  17054. (while (string-match "|" s)
  17055. (setq s (replace-match "\\vert" t t s)))
  17056. s)
  17057. (defun org-uuidgen-p (s)
  17058. "Is S an ID created by UUIDGEN?"
  17059. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  17060. (defun org-context ()
  17061. "Return a list of contexts of the current cursor position.
  17062. If several contexts apply, all are returned.
  17063. Each context entry is a list with a symbol naming the context, and
  17064. two positions indicating start and end of the context. Possible
  17065. contexts are:
  17066. :headline anywhere in a headline
  17067. :headline-stars on the leading stars in a headline
  17068. :todo-keyword on a TODO keyword (including DONE) in a headline
  17069. :tags on the TAGS in a headline
  17070. :priority on the priority cookie in a headline
  17071. :item on the first line of a plain list item
  17072. :item-bullet on the bullet/number of a plain list item
  17073. :checkbox on the checkbox in a plain list item
  17074. :table in an org-mode table
  17075. :table-special on a special filed in a table
  17076. :table-table in a table.el table
  17077. :link on a hyperlink
  17078. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  17079. :target on a <<target>>
  17080. :radio-target on a <<<radio-target>>>
  17081. :latex-fragment on a LaTeX fragment
  17082. :latex-preview on a LaTeX fragment with overlayed preview image
  17083. This function expects the position to be visible because it uses font-lock
  17084. faces as a help to recognize the following contexts: :table-special, :link,
  17085. and :keyword."
  17086. (let* ((f (get-text-property (point) 'face))
  17087. (faces (if (listp f) f (list f)))
  17088. (p (point)) clist o)
  17089. ;; First the large context
  17090. (cond
  17091. ((org-on-heading-p t)
  17092. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  17093. (when (progn
  17094. (beginning-of-line 1)
  17095. (looking-at org-todo-line-tags-regexp))
  17096. (push (org-point-in-group p 1 :headline-stars) clist)
  17097. (push (org-point-in-group p 2 :todo-keyword) clist)
  17098. (push (org-point-in-group p 4 :tags) clist))
  17099. (goto-char p)
  17100. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  17101. (if (looking-at "\\[#[A-Z0-9]\\]")
  17102. (push (org-point-in-group p 0 :priority) clist)))
  17103. ((org-at-item-p)
  17104. (push (org-point-in-group p 2 :item-bullet) clist)
  17105. (push (list :item (point-at-bol)
  17106. (save-excursion (org-end-of-item) (point)))
  17107. clist)
  17108. (and (org-at-item-checkbox-p)
  17109. (push (org-point-in-group p 0 :checkbox) clist)))
  17110. ((org-at-table-p)
  17111. (push (list :table (org-table-begin) (org-table-end)) clist)
  17112. (if (memq 'org-formula faces)
  17113. (push (list :table-special
  17114. (previous-single-property-change p 'face)
  17115. (next-single-property-change p 'face)) clist)))
  17116. ((org-at-table-p 'any)
  17117. (push (list :table-table) clist)))
  17118. (goto-char p)
  17119. ;; Now the small context
  17120. (cond
  17121. ((org-at-timestamp-p)
  17122. (push (org-point-in-group p 0 :timestamp) clist))
  17123. ((memq 'org-link faces)
  17124. (push (list :link
  17125. (previous-single-property-change p 'face)
  17126. (next-single-property-change p 'face)) clist))
  17127. ((memq 'org-special-keyword faces)
  17128. (push (list :keyword
  17129. (previous-single-property-change p 'face)
  17130. (next-single-property-change p 'face)) clist))
  17131. ((org-on-target-p)
  17132. (push (org-point-in-group p 0 :target) clist)
  17133. (goto-char (1- (match-beginning 0)))
  17134. (if (looking-at org-radio-target-regexp)
  17135. (push (org-point-in-group p 0 :radio-target) clist))
  17136. (goto-char p))
  17137. ((setq o (car (delq nil
  17138. (mapcar
  17139. (lambda (x)
  17140. (if (memq x org-latex-fragment-image-overlays) x))
  17141. (overlays-at (point))))))
  17142. (push (list :latex-fragment
  17143. (overlay-start o) (overlay-end o)) clist)
  17144. (push (list :latex-preview
  17145. (overlay-start o) (overlay-end o)) clist))
  17146. ((org-inside-LaTeX-fragment-p)
  17147. ;; FIXME: positions wrong.
  17148. (push (list :latex-fragment (point) (point)) clist)))
  17149. (setq clist (nreverse (delq nil clist)))
  17150. clist))
  17151. ;; FIXME: Compare with at-regexp-p Do we need both?
  17152. (defun org-in-regexp (re &optional nlines visually)
  17153. "Check if point is inside a match of regexp.
  17154. Normally only the current line is checked, but you can include NLINES extra
  17155. lines both before and after point into the search.
  17156. If VISUALLY is set, require that the cursor is not after the match but
  17157. really on, so that the block visually is on the match."
  17158. (catch 'exit
  17159. (let ((pos (point))
  17160. (eol (point-at-eol (+ 1 (or nlines 0))))
  17161. (inc (if visually 1 0)))
  17162. (save-excursion
  17163. (beginning-of-line (- 1 (or nlines 0)))
  17164. (while (re-search-forward re eol t)
  17165. (if (and (<= (match-beginning 0) pos)
  17166. (>= (+ inc (match-end 0)) pos))
  17167. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  17168. (defun org-at-regexp-p (regexp)
  17169. "Is point inside a match of REGEXP in the current line?"
  17170. (catch 'exit
  17171. (save-excursion
  17172. (let ((pos (point)) (end (point-at-eol)))
  17173. (beginning-of-line 1)
  17174. (while (re-search-forward regexp end t)
  17175. (if (and (<= (match-beginning 0) pos)
  17176. (>= (match-end 0) pos))
  17177. (throw 'exit t)))
  17178. nil))))
  17179. (defun org-in-regexps-block-p (start-re end-re &optional bound)
  17180. "Return t if the current point is between matches of START-RE and END-RE.
  17181. This will also return t if point is on one of the two matches or
  17182. in an unfinished block. END-RE can be a string or a form
  17183. returning a string.
  17184. An optional third argument bounds the search for START-RE. It
  17185. defaults to previous heading or `point-min'."
  17186. (let ((pos (point))
  17187. (limit (or bound (save-excursion (outline-previous-heading)))))
  17188. (save-excursion
  17189. ;; we're on a block when point is on start-re...
  17190. (or (org-at-regexp-p start-re)
  17191. ;; ... or start-re can be found above...
  17192. (and (re-search-backward start-re limit t)
  17193. ;; ... but no end-re between start-re and point.
  17194. (not (re-search-forward (eval end-re) pos t)))))))
  17195. (defun org-occur-in-agenda-files (regexp &optional nlines)
  17196. "Call `multi-occur' with buffers for all agenda files."
  17197. (interactive "sOrg-files matching: \np")
  17198. (let* ((files (org-agenda-files))
  17199. (tnames (mapcar 'file-truename files))
  17200. (extra org-agenda-text-search-extra-files)
  17201. f)
  17202. (when (eq (car extra) 'agenda-archives)
  17203. (setq extra (cdr extra))
  17204. (setq files (org-add-archive-files files)))
  17205. (while (setq f (pop extra))
  17206. (unless (member (file-truename f) tnames)
  17207. (add-to-list 'files f 'append)
  17208. (add-to-list 'tnames (file-truename f) 'append)))
  17209. (multi-occur
  17210. (mapcar (lambda (x)
  17211. (with-current-buffer
  17212. (or (get-file-buffer x) (find-file-noselect x))
  17213. (widen)
  17214. (current-buffer)))
  17215. files)
  17216. regexp)))
  17217. (if (boundp 'occur-mode-find-occurrence-hook)
  17218. ;; Emacs 23
  17219. (add-hook 'occur-mode-find-occurrence-hook
  17220. (lambda ()
  17221. (when (org-mode-p)
  17222. (org-reveal))))
  17223. ;; Emacs 22
  17224. (defadvice occur-mode-goto-occurrence
  17225. (after org-occur-reveal activate)
  17226. (and (org-mode-p) (org-reveal)))
  17227. (defadvice occur-mode-goto-occurrence-other-window
  17228. (after org-occur-reveal activate)
  17229. (and (org-mode-p) (org-reveal)))
  17230. (defadvice occur-mode-display-occurrence
  17231. (after org-occur-reveal activate)
  17232. (when (org-mode-p)
  17233. (let ((pos (occur-mode-find-occurrence)))
  17234. (with-current-buffer (marker-buffer pos)
  17235. (save-excursion
  17236. (goto-char pos)
  17237. (org-reveal)))))))
  17238. (defun org-occur-link-in-agenda-files ()
  17239. "Create a link and search for it in the agendas.
  17240. The link is not stored in `org-stored-links', it is just created
  17241. for the search purpose."
  17242. (interactive)
  17243. (let ((link (condition-case nil
  17244. (org-store-link nil)
  17245. (error "Unable to create a link to here"))))
  17246. (org-occur-in-agenda-files (regexp-quote link))))
  17247. (defun org-uniquify (list)
  17248. "Remove duplicate elements from LIST."
  17249. (let (res)
  17250. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  17251. res))
  17252. (defun org-delete-all (elts list)
  17253. "Remove all elements in ELTS from LIST."
  17254. (while elts
  17255. (setq list (delete (pop elts) list)))
  17256. list)
  17257. (defun org-count (cl-item cl-seq)
  17258. "Count the number of occurrences of ITEM in SEQ.
  17259. Taken from `count' in cl-seq.el with all keyword arguments removed."
  17260. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  17261. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  17262. (while (< cl-start cl-end)
  17263. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  17264. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  17265. (setq cl-start (1+ cl-start)))
  17266. cl-count))
  17267. (defun org-remove-if (predicate seq)
  17268. "Remove everything from SEQ that fulfills PREDICATE."
  17269. (let (res e)
  17270. (while seq
  17271. (setq e (pop seq))
  17272. (if (not (funcall predicate e)) (push e res)))
  17273. (nreverse res)))
  17274. (defun org-remove-if-not (predicate seq)
  17275. "Remove everything from SEQ that does not fulfill PREDICATE."
  17276. (let (res e)
  17277. (while seq
  17278. (setq e (pop seq))
  17279. (if (funcall predicate e) (push e res)))
  17280. (nreverse res)))
  17281. (defun org-back-over-empty-lines ()
  17282. "Move backwards over whitespace, to the beginning of the first empty line.
  17283. Returns the number of empty lines passed."
  17284. (let ((pos (point)))
  17285. (if (cdr (assoc 'heading org-blank-before-new-entry))
  17286. (skip-chars-backward " \t\n\r")
  17287. (forward-line -1))
  17288. (beginning-of-line 2)
  17289. (goto-char (min (point) pos))
  17290. (count-lines (point) pos)))
  17291. (defun org-skip-whitespace ()
  17292. (skip-chars-forward " \t\n\r"))
  17293. (defun org-point-in-group (point group &optional context)
  17294. "Check if POINT is in match-group GROUP.
  17295. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  17296. match. If the match group does not exist or point is not inside it,
  17297. return nil."
  17298. (and (match-beginning group)
  17299. (>= point (match-beginning group))
  17300. (<= point (match-end group))
  17301. (if context
  17302. (list context (match-beginning group) (match-end group))
  17303. t)))
  17304. (defun org-switch-to-buffer-other-window (&rest args)
  17305. "Switch to buffer in a second window on the current frame.
  17306. In particular, do not allow pop-up frames.
  17307. Returns the newly created buffer."
  17308. (let (pop-up-frames special-display-buffer-names special-display-regexps
  17309. special-display-function)
  17310. (apply 'switch-to-buffer-other-window args)))
  17311. (defun org-combine-plists (&rest plists)
  17312. "Create a single property list from all plists in PLISTS.
  17313. The process starts by copying the first list, and then setting properties
  17314. from the other lists. Settings in the last list are the most significant
  17315. ones and overrule settings in the other lists."
  17316. (let ((rtn (copy-sequence (pop plists)))
  17317. p v ls)
  17318. (while plists
  17319. (setq ls (pop plists))
  17320. (while ls
  17321. (setq p (pop ls) v (pop ls))
  17322. (setq rtn (plist-put rtn p v))))
  17323. rtn))
  17324. (defun org-move-line-down (arg)
  17325. "Move the current line down. With prefix argument, move it past ARG lines."
  17326. (interactive "p")
  17327. (let ((col (current-column))
  17328. beg end pos)
  17329. (beginning-of-line 1) (setq beg (point))
  17330. (beginning-of-line 2) (setq end (point))
  17331. (beginning-of-line (+ 1 arg))
  17332. (setq pos (move-marker (make-marker) (point)))
  17333. (insert (delete-and-extract-region beg end))
  17334. (goto-char pos)
  17335. (org-move-to-column col)))
  17336. (defun org-move-line-up (arg)
  17337. "Move the current line up. With prefix argument, move it past ARG lines."
  17338. (interactive "p")
  17339. (let ((col (current-column))
  17340. beg end pos)
  17341. (beginning-of-line 1) (setq beg (point))
  17342. (beginning-of-line 2) (setq end (point))
  17343. (beginning-of-line (- arg))
  17344. (setq pos (move-marker (make-marker) (point)))
  17345. (insert (delete-and-extract-region beg end))
  17346. (goto-char pos)
  17347. (org-move-to-column col)))
  17348. (defun org-replace-escapes (string table)
  17349. "Replace %-escapes in STRING with values in TABLE.
  17350. TABLE is an association list with keys like \"%a\" and string values.
  17351. The sequences in STRING may contain normal field width and padding information,
  17352. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  17353. so values can contain further %-escapes if they are define later in TABLE."
  17354. (let ((tbl (copy-alist table))
  17355. (case-fold-search nil)
  17356. (pchg 0)
  17357. e re rpl)
  17358. (while (setq e (pop tbl))
  17359. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  17360. (when (and (cdr e) (string-match re (cdr e)))
  17361. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  17362. (safe "SREF"))
  17363. (add-text-properties 0 3 (list 'sref sref) safe)
  17364. (setcdr e (replace-match safe t t (cdr e)))))
  17365. (while (string-match re string)
  17366. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  17367. (cdr e)))
  17368. (setq string (replace-match rpl t t string))))
  17369. (while (setq pchg (next-property-change pchg string))
  17370. (let ((sref (get-text-property pchg 'sref string)))
  17371. (when (and sref (string-match "SREF" string pchg))
  17372. (setq string (replace-match sref t t string)))))
  17373. string))
  17374. (defun org-sublist (list start end)
  17375. "Return a section of LIST, from START to END.
  17376. Counting starts at 1."
  17377. (let (rtn (c start))
  17378. (setq list (nthcdr (1- start) list))
  17379. (while (and list (<= c end))
  17380. (push (pop list) rtn)
  17381. (setq c (1+ c)))
  17382. (nreverse rtn)))
  17383. (defun org-find-base-buffer-visiting (file)
  17384. "Like `find-buffer-visiting' but always return the base buffer and
  17385. not an indirect buffer."
  17386. (let ((buf (or (get-file-buffer file)
  17387. (find-buffer-visiting file))))
  17388. (if buf
  17389. (or (buffer-base-buffer buf) buf)
  17390. nil)))
  17391. (defun org-image-file-name-regexp (&optional extensions)
  17392. "Return regexp matching the file names of images.
  17393. If EXTENSIONS is given, only match these."
  17394. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  17395. (image-file-name-regexp)
  17396. (let ((image-file-name-extensions
  17397. (or extensions
  17398. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  17399. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  17400. (concat "\\."
  17401. (regexp-opt (nconc (mapcar 'upcase
  17402. image-file-name-extensions)
  17403. image-file-name-extensions)
  17404. t)
  17405. "\\'"))))
  17406. (defun org-file-image-p (file &optional extensions)
  17407. "Return non-nil if FILE is an image."
  17408. (save-match-data
  17409. (string-match (org-image-file-name-regexp extensions) file)))
  17410. (defun org-get-cursor-date ()
  17411. "Return the date at cursor in as a time.
  17412. This works in the calendar and in the agenda, anywhere else it just
  17413. returns the current time."
  17414. (let (date day defd)
  17415. (cond
  17416. ((eq major-mode 'calendar-mode)
  17417. (setq date (calendar-cursor-to-date)
  17418. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  17419. ((eq major-mode 'org-agenda-mode)
  17420. (setq day (get-text-property (point) 'day))
  17421. (if day
  17422. (setq date (calendar-gregorian-from-absolute day)
  17423. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  17424. (nth 2 date))))))
  17425. (or defd (current-time))))
  17426. (defvar org-agenda-action-marker (make-marker)
  17427. "Marker pointing to the entry for the next agenda action.")
  17428. (defun org-mark-entry-for-agenda-action ()
  17429. "Mark the current entry as target of an agenda action.
  17430. Agenda actions are actions executed from the agenda with the key `k',
  17431. which make use of the date at the cursor."
  17432. (interactive)
  17433. (move-marker org-agenda-action-marker
  17434. (save-excursion (org-back-to-heading t) (point))
  17435. (current-buffer))
  17436. (message
  17437. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  17438. (defun org-mark-subtree ()
  17439. "Mark the current subtree.
  17440. This puts point at the start of the current subtree, and mark at the end.
  17441. If point is in an inline task, mark that task instead."
  17442. (interactive)
  17443. (let ((inline-task-p
  17444. (and (featurep 'org-inlinetask)
  17445. (org-inlinetask-in-task-p)))
  17446. (beg))
  17447. ;; Get beginning of subtree
  17448. (cond
  17449. (inline-task-p (org-inlinetask-goto-beginning))
  17450. ((org-at-heading-p) (beginning-of-line))
  17451. (t (org-with-limited-levels (outline-previous-visible-heading 1))))
  17452. (setq beg (point))
  17453. ;; Get end of it
  17454. (if inline-task-p
  17455. (org-inlinetask-goto-end)
  17456. (org-end-of-subtree))
  17457. ;; Mark zone
  17458. (push-mark (point) nil t)
  17459. (goto-char beg)))
  17460. ;;; Paragraph filling stuff.
  17461. ;; We want this to be just right, so use the full arsenal.
  17462. (defun org-indent-line-function ()
  17463. "Indent line depending on context."
  17464. (interactive)
  17465. (let* ((pos (point))
  17466. (itemp (org-at-item-p))
  17467. (case-fold-search t)
  17468. (org-drawer-regexp (or org-drawer-regexp "\000"))
  17469. (inline-task-p (and (featurep 'org-inlinetask)
  17470. (org-inlinetask-in-task-p)))
  17471. (inline-re (and inline-task-p
  17472. (org-inlinetask-outline-regexp)))
  17473. column)
  17474. (beginning-of-line 1)
  17475. (cond
  17476. ;; Comments
  17477. ((looking-at "# ") (setq column 0))
  17478. ;; Headings
  17479. ((looking-at "\\*+ ") (setq column 0))
  17480. ;; Footnote definition
  17481. ((looking-at org-footnote-definition-re) (setq column 0))
  17482. ;; Literal examples
  17483. ((looking-at "[ \t]*:[ \t]")
  17484. (setq column (org-get-indentation))) ; do nothing
  17485. ;; Lists
  17486. ((ignore-errors (goto-char (org-in-item-p)))
  17487. (setq column (if itemp
  17488. (org-get-indentation)
  17489. (org-list-item-body-column (point))))
  17490. (goto-char pos))
  17491. ;; Drawers
  17492. ((and (looking-at "[ \t]*:END:")
  17493. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  17494. (save-excursion
  17495. (goto-char (1- (match-beginning 1)))
  17496. (setq column (current-column))))
  17497. ;; Special blocks
  17498. ((and (looking-at "[ \t]*#\\+end_\\([a-z]+\\)")
  17499. (save-excursion
  17500. (re-search-backward
  17501. (concat "^[ \t]*#\\+begin_" (downcase (match-string 1))) nil t)))
  17502. (setq column (org-get-indentation (match-string 0))))
  17503. ((and (not (looking-at "[ \t]*#\\+begin_"))
  17504. (org-in-regexps-block-p "^[ \t]*#\\+begin_" "[ \t]*#\\+end_"))
  17505. (save-excursion
  17506. (re-search-backward "^[ \t]*#\\+begin_\\([a-z]+\\)" nil t))
  17507. (setq column
  17508. (if (equal (downcase (match-string 1)) "src")
  17509. ;; src blocks: let `org-edit-src-exit' handle them
  17510. (org-get-indentation)
  17511. (org-get-indentation (match-string 0)))))
  17512. ;; This line has nothing special, look at the previous relevant
  17513. ;; line to compute indentation
  17514. (t
  17515. (beginning-of-line 0)
  17516. (while (and (not (bobp))
  17517. (not (looking-at org-drawer-regexp))
  17518. ;; When point started in an inline task, do not move
  17519. ;; above task starting line.
  17520. (not (and inline-task-p (looking-at inline-re)))
  17521. ;; Skip drawers, blocks, empty lines, verbatim,
  17522. ;; comments, tables, footnotes definitions, lists,
  17523. ;; inline tasks.
  17524. (or (and (looking-at "[ \t]*:END:")
  17525. (re-search-backward org-drawer-regexp nil t))
  17526. (and (looking-at "[ \t]*#\\+end_")
  17527. (re-search-backward "[ \t]*#\\+begin_"nil t))
  17528. (looking-at "[ \t]*[\n:#|]")
  17529. (looking-at org-footnote-definition-re)
  17530. (and (ignore-errors (goto-char (org-in-item-p)))
  17531. (goto-char
  17532. (org-list-get-top-point (org-list-struct))))
  17533. (and (not inline-task-p)
  17534. (featurep 'org-inlinetask)
  17535. (org-inlinetask-in-task-p)
  17536. (or (org-inlinetask-goto-beginning) t))))
  17537. (beginning-of-line 0))
  17538. (cond
  17539. ;; There was an heading above.
  17540. ((looking-at "\\*+[ \t]+")
  17541. (if (not org-adapt-indentation)
  17542. (setq column 0)
  17543. (goto-char (match-end 0))
  17544. (setq column (current-column))))
  17545. ;; A drawer had started and is unfinished
  17546. ((looking-at org-drawer-regexp)
  17547. (goto-char (1- (match-beginning 1)))
  17548. (setq column (current-column)))
  17549. ;; Else, nothing noticeable found: get indentation and go on.
  17550. (t (setq column (org-get-indentation))))))
  17551. ;; Now apply indentation and move cursor accordingly
  17552. (goto-char pos)
  17553. (if (<= (current-column) (current-indentation))
  17554. (org-indent-line-to column)
  17555. (save-excursion (org-indent-line-to column)))
  17556. ;; Special polishing for properties, see `org-property-format'
  17557. (setq column (current-column))
  17558. (beginning-of-line 1)
  17559. (if (looking-at
  17560. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  17561. (replace-match (concat (match-string 1)
  17562. (format org-property-format
  17563. (match-string 2) (match-string 3)))
  17564. t t))
  17565. (org-move-to-column column)))
  17566. (defvar org-adaptive-fill-regexp-backup adaptive-fill-regexp
  17567. "Variable to store copy of `adaptive-fill-regexp'.
  17568. Since `adaptive-fill-regexp' is set to never match, we need to
  17569. store a backup of its value before entering `org-mode' so that
  17570. the functionality can be provided as a fall-back.")
  17571. (defun org-set-autofill-regexps ()
  17572. (interactive)
  17573. ;; In the paragraph separator we include headlines, because filling
  17574. ;; text in a line directly attached to a headline would otherwise
  17575. ;; fill the headline as well.
  17576. (org-set-local 'comment-start-skip "^#+[ \t]*")
  17577. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|#]")
  17578. ;; The paragraph starter includes hand-formatted lists.
  17579. (org-set-local
  17580. 'paragraph-start
  17581. (concat
  17582. "\f" "\\|"
  17583. "[ ]*$" "\\|"
  17584. "\\*+ " "\\|"
  17585. "[ \t]*#" "\\|"
  17586. (org-item-re) "\\|"
  17587. "[ \t]*[:|]" "\\|"
  17588. "\\$\\$" "\\|"
  17589. "\\\\\\(begin\\|end\\|[][]\\)"))
  17590. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  17591. ;; But only if the user has not turned off tables or fixed-width regions
  17592. (org-set-local
  17593. 'auto-fill-inhibit-regexp
  17594. (concat "\\*+ \\|#\\+"
  17595. "\\|[ \t]*" org-keyword-time-regexp
  17596. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  17597. (concat
  17598. "\\|[ \t]*["
  17599. (if org-enable-table-editor "|" "")
  17600. (if org-enable-fixed-width-editor ":" "")
  17601. "]"))))
  17602. ;; We use our own fill-paragraph function, to make sure that tables
  17603. ;; and fixed-width regions are not wrapped. That function will pass
  17604. ;; through to `fill-paragraph' when appropriate.
  17605. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  17606. ;; Prevent auto-fill from inserting unwanted new items.
  17607. (org-set-local 'fill-nobreak-predicate
  17608. (if (memq 'org-fill-item-nobreak-p fill-nobreak-predicate)
  17609. fill-nobreak-predicate
  17610. (cons 'org-fill-item-nobreak-p fill-nobreak-predicate)))
  17611. ;; Adaptive filling: To get full control, first make sure that
  17612. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  17613. (unless (local-variable-p 'adaptive-fill-regexp (current-buffer))
  17614. (org-set-local 'org-adaptive-fill-regexp-backup
  17615. adaptive-fill-regexp))
  17616. (org-set-local 'adaptive-fill-regexp "\000")
  17617. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  17618. (org-set-local 'adaptive-fill-function
  17619. 'org-adaptive-fill-function)
  17620. (org-set-local
  17621. 'align-mode-rules-list
  17622. '((org-in-buffer-settings
  17623. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  17624. (modes . '(org-mode))))))
  17625. (defun org-fill-item-nobreak-p ()
  17626. "Non-nil when a line break at point would insert a new item."
  17627. (and (looking-at (org-item-re)) (org-list-in-valid-context-p)))
  17628. (defun org-fill-paragraph (&optional justify)
  17629. "Re-align a table, pass through to fill-paragraph if no table."
  17630. (let ((table-p (org-at-table-p))
  17631. (table.el-p (org-at-table.el-p))
  17632. (itemp (org-in-item-p)))
  17633. (cond ((and (equal (char-after (point-at-bol)) ?*)
  17634. (save-excursion (goto-char (point-at-bol))
  17635. (looking-at outline-regexp)))
  17636. t) ; skip headlines
  17637. (table.el-p t) ; skip table.el tables
  17638. (table-p (org-table-align) t) ; align Org tables
  17639. (itemp ; align text in items
  17640. (let* ((struct (save-excursion (goto-char itemp)
  17641. (org-list-struct)))
  17642. (parents (org-list-parents-alist struct))
  17643. (children (org-list-get-children itemp struct parents))
  17644. beg end prev next prefix)
  17645. ;; Determine in which part of item point is: before
  17646. ;; first child, after last child, between two
  17647. ;; sub-lists, or simply in item if there's no child.
  17648. (cond
  17649. ((not children)
  17650. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17651. beg itemp
  17652. end (org-list-get-item-end itemp struct)))
  17653. ((< (point) (setq next (car children)))
  17654. (setq prefix (make-string (org-list-item-body-column itemp) ?\ )
  17655. beg itemp
  17656. end next))
  17657. ((> (point) (setq prev (car (last children))))
  17658. (setq beg (org-list-get-item-end prev struct)
  17659. end (org-list-get-item-end itemp struct)
  17660. prefix (save-excursion
  17661. (goto-char beg)
  17662. (skip-chars-forward " \t")
  17663. (make-string (current-column) ?\ ))))
  17664. (t (catch 'exit
  17665. (while (setq next (pop children))
  17666. (if (> (point) next)
  17667. (setq prev next)
  17668. (setq beg (org-list-get-item-end prev struct)
  17669. end next
  17670. prefix (save-excursion
  17671. (goto-char beg)
  17672. (skip-chars-forward " \t")
  17673. (make-string (current-column) ?\ )))
  17674. (throw 'exit nil))))))
  17675. ;; Use `fill-paragraph' with buffer narrowed to item
  17676. ;; without any child, and with our computed PREFIX.
  17677. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17678. (save-restriction
  17679. (narrow-to-region beg end)
  17680. (save-excursion (fill-paragraph justify)))) t))
  17681. ;; Special case where point is not in a list but is on
  17682. ;; a paragraph adjacent to a list: make sure this paragraph
  17683. ;; doesn't get merged with the end of the list by narrowing
  17684. ;; buffer first.
  17685. ((save-excursion (forward-paragraph -1)
  17686. (setq itemp (org-in-item-p)))
  17687. (let ((struct (save-excursion (goto-char itemp)
  17688. (org-list-struct))))
  17689. (save-restriction
  17690. (narrow-to-region (org-list-get-bottom-point struct)
  17691. (save-excursion (forward-paragraph 1)
  17692. (point)))
  17693. (fill-paragraph justify) t)))
  17694. ;; Else simply call `fill-paragraph'.
  17695. (t nil))))
  17696. ;; For reference, this is the default value of adaptive-fill-regexp
  17697. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  17698. (defun org-adaptive-fill-function ()
  17699. "Return a fill prefix for org-mode files."
  17700. (let (itemp)
  17701. (save-excursion
  17702. (cond
  17703. ;; Comment line
  17704. ((looking-at "#[ \t]+")
  17705. (match-string-no-properties 0))
  17706. ;; Plain list item
  17707. ((org-at-item-p)
  17708. (make-string (org-list-item-body-column (point-at-bol)) ?\ ))
  17709. ;; Point is in a list after `backward-paragraph': original
  17710. ;; point wasn't in the list, or filling would have been taken
  17711. ;; care of by `org-auto-fill-function', but the list and the
  17712. ;; real paragraph are not separated by a blank line. Thus, move
  17713. ;; point after the list to go back to real paragraph and
  17714. ;; determine fill-prefix.
  17715. ((setq itemp (org-in-item-p))
  17716. (goto-char itemp)
  17717. (let* ((struct (org-list-struct))
  17718. (bottom (org-list-get-bottom-point struct)))
  17719. (goto-char bottom)
  17720. (make-string (org-get-indentation) ?\ )))
  17721. ;; Other text
  17722. ((looking-at org-adaptive-fill-regexp-backup)
  17723. (match-string-no-properties 0))))))
  17724. (defun org-auto-fill-function ()
  17725. "Auto-fill function."
  17726. (let (itemp prefix)
  17727. ;; When in a list, compute an appropriate fill-prefix and make
  17728. ;; sure it will be used by `do-auto-fill'.
  17729. (if (setq itemp (org-in-item-p))
  17730. (progn
  17731. (setq prefix (make-string (org-list-item-body-column itemp) ?\ ))
  17732. (flet ((fill-context-prefix (from to &optional flr) prefix))
  17733. (do-auto-fill)))
  17734. ;; Else just use `do-auto-fill'.
  17735. (do-auto-fill))))
  17736. ;;; Other stuff.
  17737. (defun org-toggle-fixed-width-section (arg)
  17738. "Toggle the fixed-width export.
  17739. If there is no active region, the QUOTE keyword at the current headline is
  17740. inserted or removed. When present, it causes the text between this headline
  17741. and the next to be exported as fixed-width text, and unmodified.
  17742. If there is an active region, this command adds or removes a colon as the
  17743. first character of this line. If the first character of a line is a colon,
  17744. this line is also exported in fixed-width font."
  17745. (interactive "P")
  17746. (let* ((cc 0)
  17747. (regionp (org-region-active-p))
  17748. (beg (if regionp (region-beginning) (point)))
  17749. (end (if regionp (region-end)))
  17750. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  17751. (case-fold-search nil)
  17752. (re "[ \t]*\\(: \\)")
  17753. off)
  17754. (if regionp
  17755. (save-excursion
  17756. (goto-char beg)
  17757. (setq cc (current-column))
  17758. (beginning-of-line 1)
  17759. (setq off (looking-at re))
  17760. (while (> nlines 0)
  17761. (setq nlines (1- nlines))
  17762. (beginning-of-line 1)
  17763. (cond
  17764. (arg
  17765. (org-move-to-column cc t)
  17766. (insert ": \n")
  17767. (forward-line -1))
  17768. ((and off (looking-at re))
  17769. (replace-match "" t t nil 1))
  17770. ((not off) (org-move-to-column cc t) (insert ": ")))
  17771. (forward-line 1)))
  17772. (save-excursion
  17773. (org-back-to-heading)
  17774. (if (looking-at (concat outline-regexp
  17775. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  17776. (replace-match "" t t nil 1)
  17777. (if (looking-at outline-regexp)
  17778. (progn
  17779. (goto-char (match-end 0))
  17780. (insert org-quote-string " "))))))))
  17781. (defun org-reftex-citation ()
  17782. "Use reftex-citation to insert a citation into the buffer.
  17783. This looks for a line like
  17784. #+BIBLIOGRAPHY: foo plain option:-d
  17785. and derives from it that foo.bib is the bibliography file relevant
  17786. for this document. It then installs the necessary environment for RefTeX
  17787. to work in this buffer and calls `reftex-citation' to insert a citation
  17788. into the buffer.
  17789. Export of such citations to both LaTeX and HTML is handled by the contributed
  17790. package org-exp-bibtex by Taru Karttunen."
  17791. (interactive)
  17792. (let ((reftex-docstruct-symbol 'rds)
  17793. (reftex-cite-format "\\cite{%l}")
  17794. rds bib)
  17795. (save-excursion
  17796. (save-restriction
  17797. (widen)
  17798. (let ((case-fold-search t)
  17799. (re "^#\\+bibliography:[ \t]+\\([^ \t\n]+\\)"))
  17800. (if (not (save-excursion
  17801. (or (re-search-forward re nil t)
  17802. (re-search-backward re nil t))))
  17803. (error "No bibliography defined in file")
  17804. (setq bib (concat (match-string 1) ".bib")
  17805. rds (list (list 'bib bib)))))))
  17806. (call-interactively 'reftex-citation)))
  17807. ;;;; Functions extending outline functionality
  17808. (defun org-beginning-of-line (&optional arg)
  17809. "Go to the beginning of the current line. If that is invisible, continue
  17810. to a visible line beginning. This makes the function of C-a more intuitive.
  17811. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17812. first attempt, and only move to after the tags when the cursor is already
  17813. beyond the end of the headline."
  17814. (interactive "P")
  17815. (let ((pos (point))
  17816. (special (if (consp org-special-ctrl-a/e)
  17817. (car org-special-ctrl-a/e)
  17818. org-special-ctrl-a/e))
  17819. refpos)
  17820. (if (org-bound-and-true-p line-move-visual)
  17821. (beginning-of-visual-line 1)
  17822. (beginning-of-line 1))
  17823. (if (and arg (fboundp 'move-beginning-of-line))
  17824. (call-interactively 'move-beginning-of-line)
  17825. (if (bobp)
  17826. nil
  17827. (backward-char 1)
  17828. (if (org-truely-invisible-p)
  17829. (while (and (not (bobp)) (org-truely-invisible-p))
  17830. (backward-char 1)
  17831. (beginning-of-line 1))
  17832. (forward-char 1))))
  17833. (when special
  17834. (cond
  17835. ((and (looking-at org-complex-heading-regexp)
  17836. (= (char-after (match-end 1)) ?\ ))
  17837. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  17838. (point-at-eol)))
  17839. (goto-char
  17840. (if (eq special t)
  17841. (cond ((> pos refpos) refpos)
  17842. ((= pos (point)) refpos)
  17843. (t (point)))
  17844. (cond ((> pos (point)) (point))
  17845. ((not (eq last-command this-command)) (point))
  17846. (t refpos)))))
  17847. ((org-at-item-p)
  17848. (goto-char
  17849. (if (eq special t)
  17850. (cond ((> pos (match-end 0)) (match-end 0))
  17851. ((= pos (point)) (match-end 0))
  17852. (t (point)))
  17853. (cond ((> pos (point)) (point))
  17854. ((not (eq last-command this-command)) (point))
  17855. (t (match-end 0))))))))
  17856. (org-no-warnings
  17857. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17858. (defun org-end-of-line (&optional arg)
  17859. "Go to the end of the line.
  17860. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  17861. first attempt, and only move to after the tags when the cursor is already
  17862. beyond the end of the headline."
  17863. (interactive "P")
  17864. (let ((special (if (consp org-special-ctrl-a/e)
  17865. (cdr org-special-ctrl-a/e)
  17866. org-special-ctrl-a/e)))
  17867. (if (or (not special)
  17868. (not (org-on-heading-p))
  17869. arg)
  17870. (call-interactively
  17871. (cond ((org-bound-and-true-p line-move-visual) 'end-of-visual-line)
  17872. ((fboundp 'move-end-of-line) 'move-end-of-line)
  17873. (t 'end-of-line)))
  17874. (let ((pos (point)))
  17875. (beginning-of-line 1)
  17876. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  17877. (if (eq special t)
  17878. (if (or (< pos (match-beginning 1))
  17879. (= pos (match-end 0)))
  17880. (goto-char (match-beginning 1))
  17881. (goto-char (match-end 0)))
  17882. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  17883. (goto-char (match-end 0))
  17884. (goto-char (match-beginning 1))))
  17885. (call-interactively (if (fboundp 'move-end-of-line)
  17886. 'move-end-of-line
  17887. 'end-of-line)))))
  17888. (org-no-warnings
  17889. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  17890. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  17891. (define-key org-mode-map "\C-e" 'org-end-of-line)
  17892. (defun org-backward-sentence (&optional arg)
  17893. "Go to beginning of sentence, or beginning of table field.
  17894. This will call `backward-sentence' or `org-table-beginning-of-field',
  17895. depending on context."
  17896. (interactive "P")
  17897. (cond
  17898. ((org-at-table-p) (call-interactively 'org-table-beginning-of-field))
  17899. (t (call-interactively 'backward-sentence))))
  17900. (defun org-forward-sentence (&optional arg)
  17901. "Go to end of sentence, or end of table field.
  17902. This will call `forward-sentence' or `org-table-end-of-field',
  17903. depending on context."
  17904. (interactive "P")
  17905. (cond
  17906. ((org-at-table-p) (call-interactively 'org-table-end-of-field))
  17907. (t (call-interactively 'forward-sentence))))
  17908. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  17909. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  17910. (defun org-kill-line (&optional arg)
  17911. "Kill line, to tags or end of line."
  17912. (interactive "P")
  17913. (cond
  17914. ((or (not org-special-ctrl-k)
  17915. (bolp)
  17916. (not (org-on-heading-p)))
  17917. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  17918. org-ctrl-k-protect-subtree)
  17919. (if (or (eq org-ctrl-k-protect-subtree 'error)
  17920. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  17921. (error "C-k aborted - would kill hidden subtree")))
  17922. (call-interactively 'kill-line))
  17923. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  17924. (kill-region (point) (match-beginning 1))
  17925. (org-set-tags nil t))
  17926. (t (kill-region (point) (point-at-eol)))))
  17927. (define-key org-mode-map "\C-k" 'org-kill-line)
  17928. (defun org-yank (&optional arg)
  17929. "Yank. If the kill is a subtree, treat it specially.
  17930. This command will look at the current kill and check if is a single
  17931. subtree, or a series of subtrees[1]. If it passes the test, and if the
  17932. cursor is at the beginning of a line or after the stars of a currently
  17933. empty headline, then the yank is handled specially. How exactly depends
  17934. on the value of the following variables, both set by default.
  17935. org-yank-folded-subtrees
  17936. When set, the subtree(s) will be folded after insertion, but only
  17937. if doing so would now swallow text after the yanked text.
  17938. org-yank-adjusted-subtrees
  17939. When set, the subtree will be promoted or demoted in order to
  17940. fit into the local outline tree structure, which means that the level
  17941. will be adjusted so that it becomes the smaller one of the two
  17942. *visible* surrounding headings.
  17943. Any prefix to this command will cause `yank' to be called directly with
  17944. no special treatment. In particular, a simple \\[universal-argument] prefix \
  17945. will just
  17946. plainly yank the text as it is.
  17947. \[1] The test checks if the first non-white line is a heading
  17948. and if there are no other headings with fewer stars."
  17949. (interactive "P")
  17950. (org-yank-generic 'yank arg))
  17951. (defun org-yank-generic (command arg)
  17952. "Perform some yank-like command.
  17953. This function implements the behavior described in the `org-yank'
  17954. documentation. However, it has been generalized to work for any
  17955. interactive command with similar behavior."
  17956. ;; pretend to be command COMMAND
  17957. (setq this-command command)
  17958. (if arg
  17959. (call-interactively command)
  17960. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  17961. (and (org-kill-is-subtree-p)
  17962. (or (bolp)
  17963. (and (looking-at "[ \t]*$")
  17964. (string-match
  17965. "\\`\\*+\\'"
  17966. (buffer-substring (point-at-bol) (point)))))))
  17967. swallowp)
  17968. (cond
  17969. ((and subtreep org-yank-folded-subtrees)
  17970. (let ((beg (point))
  17971. end)
  17972. (if (and subtreep org-yank-adjusted-subtrees)
  17973. (org-paste-subtree nil nil 'for-yank)
  17974. (call-interactively command))
  17975. (setq end (point))
  17976. (goto-char beg)
  17977. (when (and (bolp) subtreep
  17978. (not (setq swallowp
  17979. (org-yank-folding-would-swallow-text beg end))))
  17980. (or (looking-at outline-regexp)
  17981. (re-search-forward (concat "^" outline-regexp) end t))
  17982. (while (and (< (point) end) (looking-at outline-regexp))
  17983. (hide-subtree)
  17984. (org-cycle-show-empty-lines 'folded)
  17985. (condition-case nil
  17986. (outline-forward-same-level 1)
  17987. (error (goto-char end)))))
  17988. (when swallowp
  17989. (message
  17990. "Inserted text not folded because that would swallow text"))
  17991. (goto-char end)
  17992. (skip-chars-forward " \t\n\r")
  17993. (beginning-of-line 1)
  17994. (push-mark beg 'nomsg)))
  17995. ((and subtreep org-yank-adjusted-subtrees)
  17996. (let ((beg (point-at-bol)))
  17997. (org-paste-subtree nil nil 'for-yank)
  17998. (push-mark beg 'nomsg)))
  17999. (t
  18000. (call-interactively command))))))
  18001. (defun org-yank-folding-would-swallow-text (beg end)
  18002. "Would hide-subtree at BEG swallow any text after END?"
  18003. (let (level)
  18004. (save-excursion
  18005. (goto-char beg)
  18006. (when (or (looking-at outline-regexp)
  18007. (re-search-forward (concat "^" outline-regexp) end t))
  18008. (setq level (org-outline-level)))
  18009. (goto-char end)
  18010. (skip-chars-forward " \t\r\n\v\f")
  18011. (if (or (eobp)
  18012. (and (bolp) (looking-at org-outline-regexp)
  18013. (<= (org-outline-level) level)))
  18014. nil ; Nothing would be swallowed
  18015. t)))) ; something would swallow
  18016. (define-key org-mode-map "\C-y" 'org-yank)
  18017. (defun org-truely-invisible-p ()
  18018. "Check if point is at a character currently not visible.
  18019. This version does not only check the character property, but also
  18020. `visible-mode'."
  18021. ;; Early versions of noutline don't have `outline-invisible-p'.
  18022. (if (org-bound-and-true-p visible-mode)
  18023. nil
  18024. (outline-invisible-p)))
  18025. (defun org-invisible-p2 ()
  18026. "Check if point is at a character currently not visible."
  18027. (save-excursion
  18028. (if (and (eolp) (not (bobp))) (backward-char 1))
  18029. ;; Early versions of noutline don't have `outline-invisible-p'.
  18030. (outline-invisible-p)))
  18031. (defun org-back-to-heading (&optional invisible-ok)
  18032. "Call `outline-back-to-heading', but provide a better error message."
  18033. (condition-case nil
  18034. (outline-back-to-heading invisible-ok)
  18035. (error (error "Before first headline at position %d in buffer %s"
  18036. (point) (current-buffer)))))
  18037. (defun org-beginning-of-defun ()
  18038. "Go to the beginning of the subtree, i.e. back to the heading."
  18039. (org-back-to-heading))
  18040. (defun org-end-of-defun ()
  18041. "Go to the end of the subtree."
  18042. (org-end-of-subtree nil t))
  18043. (defun org-before-first-heading-p ()
  18044. "Before first heading?"
  18045. (save-excursion
  18046. (end-of-line)
  18047. (null (re-search-backward "^\\*+ " nil t))))
  18048. (defun org-on-heading-p (&optional ignored)
  18049. (outline-on-heading-p t))
  18050. (defun org-at-heading-p (&optional ignored)
  18051. (outline-on-heading-p t))
  18052. (defun org-point-at-end-of-empty-headline ()
  18053. "If point is at the end of an empty headline, return t, else nil.
  18054. If the heading only contains a TODO keyword, it is still still considered
  18055. empty."
  18056. (and (looking-at "[ \t]*$")
  18057. (save-excursion
  18058. (beginning-of-line 1)
  18059. (let ((case-fold-search nil))
  18060. (looking-at (concat "^\\(\\*+\\)[ \t]+\\(" org-todo-regexp
  18061. "\\)?[ \t]*$"))))))
  18062. (defun org-at-heading-or-item-p ()
  18063. (or (org-on-heading-p) (org-at-item-p)))
  18064. (defun org-on-target-p ()
  18065. (or (org-in-regexp org-radio-target-regexp)
  18066. (org-in-regexp org-target-regexp)))
  18067. (defun org-up-heading-all (arg)
  18068. "Move to the heading line of which the present line is a subheading.
  18069. This function considers both visible and invisible heading lines.
  18070. With argument, move up ARG levels."
  18071. (if (fboundp 'outline-up-heading-all)
  18072. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  18073. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  18074. (defun org-up-heading-safe ()
  18075. "Move to the heading line of which the present line is a subheading.
  18076. This version will not throw an error. It will return the level of the
  18077. headline found, or nil if no higher level is found.
  18078. Also, this function will be a lot faster than `outline-up-heading',
  18079. because it relies on stars being the outline starters. This can really
  18080. make a significant difference in outlines with very many siblings."
  18081. (let (start-level re)
  18082. (org-back-to-heading t)
  18083. (setq start-level (funcall outline-level))
  18084. (if (equal start-level 1)
  18085. nil
  18086. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  18087. (if (re-search-backward re nil t)
  18088. (funcall outline-level)))))
  18089. (defun org-first-sibling-p ()
  18090. "Is this heading the first child of its parents?"
  18091. (interactive)
  18092. (let ((re (concat "^" outline-regexp))
  18093. level l)
  18094. (unless (org-at-heading-p t)
  18095. (error "Not at a heading"))
  18096. (setq level (funcall outline-level))
  18097. (save-excursion
  18098. (if (not (re-search-backward re nil t))
  18099. t
  18100. (setq l (funcall outline-level))
  18101. (< l level)))))
  18102. (defun org-goto-sibling (&optional previous)
  18103. "Goto the next sibling, even if it is invisible.
  18104. When PREVIOUS is set, go to the previous sibling instead. Returns t
  18105. when a sibling was found. When none is found, return nil and don't
  18106. move point."
  18107. (let ((fun (if previous 're-search-backward 're-search-forward))
  18108. (pos (point))
  18109. (re (concat "^" outline-regexp))
  18110. level l)
  18111. (when (condition-case nil (org-back-to-heading t) (error nil))
  18112. (setq level (funcall outline-level))
  18113. (catch 'exit
  18114. (or previous (forward-char 1))
  18115. (while (funcall fun re nil t)
  18116. (setq l (funcall outline-level))
  18117. (when (< l level) (goto-char pos) (throw 'exit nil))
  18118. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  18119. (goto-char pos)
  18120. nil))))
  18121. (defun org-show-siblings ()
  18122. "Show all siblings of the current headline."
  18123. (save-excursion
  18124. (while (org-goto-sibling) (org-flag-heading nil)))
  18125. (save-excursion
  18126. (while (org-goto-sibling 'previous)
  18127. (org-flag-heading nil))))
  18128. (defun org-goto-first-child ()
  18129. "Goto the first child, even if it is invisible.
  18130. Return t when a child was found. Otherwise don't move point and
  18131. return nil."
  18132. (let (level (pos (point)) (re (concat "^" outline-regexp)))
  18133. (when (condition-case nil (org-back-to-heading t) (error nil))
  18134. (setq level (outline-level))
  18135. (forward-char 1)
  18136. (if (and (re-search-forward re nil t) (> (outline-level) level))
  18137. (progn (goto-char (match-beginning 0)) t)
  18138. (goto-char pos) nil))))
  18139. (defun org-show-hidden-entry ()
  18140. "Show an entry where even the heading is hidden."
  18141. (save-excursion
  18142. (org-show-entry)))
  18143. (defun org-flag-heading (flag &optional entry)
  18144. "Flag the current heading. FLAG non-nil means make invisible.
  18145. When ENTRY is non-nil, show the entire entry."
  18146. (save-excursion
  18147. (org-back-to-heading t)
  18148. ;; Check if we should show the entire entry
  18149. (if entry
  18150. (progn
  18151. (org-show-entry)
  18152. (save-excursion
  18153. (and (outline-next-heading)
  18154. (org-flag-heading nil))))
  18155. (outline-flag-region (max (point-min) (1- (point)))
  18156. (save-excursion (outline-end-of-heading) (point))
  18157. flag))))
  18158. (defun org-get-next-sibling ()
  18159. "Move to next heading of the same level, and return point.
  18160. If there is no such heading, return nil.
  18161. This is like outline-next-sibling, but invisible headings are ok."
  18162. (let ((level (funcall outline-level)))
  18163. (outline-next-heading)
  18164. (while (and (not (eobp)) (> (funcall outline-level) level))
  18165. (outline-next-heading))
  18166. (if (or (eobp) (< (funcall outline-level) level))
  18167. nil
  18168. (point))))
  18169. (defun org-get-last-sibling ()
  18170. "Move to previous heading of the same level, and return point.
  18171. If there is no such heading, return nil."
  18172. (let ((opoint (point))
  18173. (level (funcall outline-level)))
  18174. (outline-previous-heading)
  18175. (when (and (/= (point) opoint) (outline-on-heading-p t))
  18176. (while (and (> (funcall outline-level) level)
  18177. (not (bobp)))
  18178. (outline-previous-heading))
  18179. (if (< (funcall outline-level) level)
  18180. nil
  18181. (point)))))
  18182. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  18183. ;; This contains an exact copy of the original function, but it uses
  18184. ;; `org-back-to-heading', to make it work also in invisible
  18185. ;; trees. And is uses an invisible-OK argument.
  18186. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  18187. ;; Furthermore, when used inside Org, finding the end of a large subtree
  18188. ;; with many children and grandchildren etc, this can be much faster
  18189. ;; than the outline version.
  18190. (org-back-to-heading invisible-OK)
  18191. (let ((first t)
  18192. (level (funcall outline-level)))
  18193. (if (and (org-mode-p) (< level 1000))
  18194. ;; A true heading (not a plain list item), in Org-mode
  18195. ;; This means we can easily find the end by looking
  18196. ;; only for the right number of stars. Using a regexp to do
  18197. ;; this is so much faster than using a Lisp loop.
  18198. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  18199. (forward-char 1)
  18200. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  18201. ;; something else, do it the slow way
  18202. (while (and (not (eobp))
  18203. (or first (> (funcall outline-level) level)))
  18204. (setq first nil)
  18205. (outline-next-heading)))
  18206. (unless to-heading
  18207. (if (memq (preceding-char) '(?\n ?\^M))
  18208. (progn
  18209. ;; Go to end of line before heading
  18210. (forward-char -1)
  18211. (if (memq (preceding-char) '(?\n ?\^M))
  18212. ;; leave blank line before heading
  18213. (forward-char -1))))))
  18214. (point))
  18215. (defadvice outline-end-of-subtree (around prefer-org-version activate compile)
  18216. "Use Org version in org-mode, for dramatic speed-up."
  18217. (if (org-mode-p)
  18218. (progn
  18219. (org-end-of-subtree nil t)
  18220. (unless (eobp) (backward-char 1)))
  18221. ad-do-it))
  18222. (defun org-end-of-meta-data-and-drawers ()
  18223. "Jump to the first text after meta data and drawers in the current entry.
  18224. This will move over empty lines, lines with planning time stamps,
  18225. clocking lines, and drawers."
  18226. (org-back-to-heading t)
  18227. (let ((end (save-excursion (outline-next-heading) (point)))
  18228. (re (concat "[ \t]*$"
  18229. "\\|"
  18230. "\\(" org-drawer-regexp "\\)" ; group 1 are drawers
  18231. "\\|"
  18232. "\\([ \t]*\\(" org-keyword-time-regexp "\\)\\)")))
  18233. (forward-line 1)
  18234. (while (looking-at (concat "[ \t]*\\(" org-keyword-time-regexp "\\)"))
  18235. (if (not (match-end 1))
  18236. ;; empty or planning line
  18237. (forward-line 1)
  18238. ;; a drawer, find the end
  18239. (re-search-forward "^[ \t]*:END:" end 'move)
  18240. (forward-line 1)))
  18241. (point)))
  18242. (defun org-forward-same-level (arg &optional invisible-ok)
  18243. "Move forward to the arg'th subheading at same level as this one.
  18244. Stop at the first and last subheadings of a superior heading.
  18245. Normally this only looks at visible headings, but when INVISIBLE-OK is non-nil
  18246. it wil also look at invisible ones."
  18247. (interactive "p")
  18248. (org-back-to-heading invisible-ok)
  18249. (org-on-heading-p)
  18250. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18251. (re (format "^\\*\\{1,%d\\} " level))
  18252. l)
  18253. (forward-char 1)
  18254. (while (> arg 0)
  18255. (while (and (re-search-forward re nil 'move)
  18256. (setq l (- (match-end 0) (match-beginning 0) 1))
  18257. (= l level)
  18258. (not invisible-ok)
  18259. (progn (backward-char 1) (outline-invisible-p)))
  18260. (if (< l level) (setq arg 1)))
  18261. (setq arg (1- arg)))
  18262. (beginning-of-line 1)))
  18263. (defun org-backward-same-level (arg &optional invisible-ok)
  18264. "Move backward to the arg'th subheading at same level as this one.
  18265. Stop at the first and last subheadings of a superior heading."
  18266. (interactive "p")
  18267. (org-back-to-heading)
  18268. (org-on-heading-p)
  18269. (let* ((level (- (match-end 0) (match-beginning 0) 1))
  18270. (re (format "^\\*\\{1,%d\\} " level))
  18271. l)
  18272. (while (> arg 0)
  18273. (while (and (re-search-backward re nil 'move)
  18274. (setq l (- (match-end 0) (match-beginning 0) 1))
  18275. (= l level)
  18276. (not invisible-ok)
  18277. (outline-invisible-p))
  18278. (if (< l level) (setq arg 1)))
  18279. (setq arg (1- arg)))))
  18280. (defun org-show-subtree ()
  18281. "Show everything after this heading at deeper levels."
  18282. (outline-flag-region
  18283. (point)
  18284. (save-excursion
  18285. (org-end-of-subtree t t))
  18286. nil))
  18287. (defun org-show-entry ()
  18288. "Show the body directly following this heading.
  18289. Show the heading too, if it is currently invisible."
  18290. (interactive)
  18291. (save-excursion
  18292. (condition-case nil
  18293. (progn
  18294. (org-back-to-heading t)
  18295. (outline-flag-region
  18296. (max (point-min) (1- (point)))
  18297. (save-excursion
  18298. (if (re-search-forward
  18299. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  18300. (match-beginning 1)
  18301. (point-max)))
  18302. nil)
  18303. (org-cycle-hide-drawers 'children))
  18304. (error nil))))
  18305. (defun org-make-options-regexp (kwds &optional extra)
  18306. "Make a regular expression for keyword lines."
  18307. (concat
  18308. "^"
  18309. "#?[ \t]*\\+\\("
  18310. (mapconcat 'regexp-quote kwds "\\|")
  18311. (if extra (concat "\\|" extra))
  18312. "\\):[ \t]*"
  18313. "\\(.*\\)"))
  18314. ;; Make isearch reveal the necessary context
  18315. (defun org-isearch-end ()
  18316. "Reveal context after isearch exits."
  18317. (when isearch-success ; only if search was successful
  18318. (if (featurep 'xemacs)
  18319. ;; Under XEmacs, the hook is run in the correct place,
  18320. ;; we directly show the context.
  18321. (org-show-context 'isearch)
  18322. ;; In Emacs the hook runs *before* restoring the overlays.
  18323. ;; So we have to use a one-time post-command-hook to do this.
  18324. ;; (Emacs 22 has a special variable, see function `org-mode')
  18325. (unless (and (boundp 'isearch-mode-end-hook-quit)
  18326. isearch-mode-end-hook-quit)
  18327. ;; Only when the isearch was not quitted.
  18328. (org-add-hook 'post-command-hook 'org-isearch-post-command
  18329. 'append 'local)))))
  18330. (defun org-isearch-post-command ()
  18331. "Remove self from hook, and show context."
  18332. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  18333. (org-show-context 'isearch))
  18334. ;;;; Integration with and fixes for other packages
  18335. ;;; Imenu support
  18336. (defvar org-imenu-markers nil
  18337. "All markers currently used by Imenu.")
  18338. (make-variable-buffer-local 'org-imenu-markers)
  18339. (defun org-imenu-new-marker (&optional pos)
  18340. "Return a new marker for use by Imenu, and remember the marker."
  18341. (let ((m (make-marker)))
  18342. (move-marker m (or pos (point)))
  18343. (push m org-imenu-markers)
  18344. m))
  18345. (defun org-imenu-get-tree ()
  18346. "Produce the index for Imenu."
  18347. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  18348. (setq org-imenu-markers nil)
  18349. (let* ((n org-imenu-depth)
  18350. (re (concat "^" outline-regexp))
  18351. (subs (make-vector (1+ n) nil))
  18352. (last-level 0)
  18353. m level head)
  18354. (save-excursion
  18355. (save-restriction
  18356. (widen)
  18357. (goto-char (point-max))
  18358. (while (re-search-backward re nil t)
  18359. (setq level (org-reduced-level (funcall outline-level)))
  18360. (when (<= level n)
  18361. (looking-at org-complex-heading-regexp)
  18362. (setq head (org-link-display-format
  18363. (org-match-string-no-properties 4))
  18364. m (org-imenu-new-marker))
  18365. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  18366. (if (>= level last-level)
  18367. (push (cons head m) (aref subs level))
  18368. (push (cons head (aref subs (1+ level))) (aref subs level))
  18369. (loop for i from (1+ level) to n do (aset subs i nil)))
  18370. (setq last-level level)))))
  18371. (aref subs 1)))
  18372. (eval-after-load "imenu"
  18373. '(progn
  18374. (add-hook 'imenu-after-jump-hook
  18375. (lambda ()
  18376. (if (org-mode-p)
  18377. (org-show-context 'org-goto))))))
  18378. (defun org-link-display-format (link)
  18379. "Replace a link with either the description, or the link target
  18380. if no description is present"
  18381. (save-match-data
  18382. (if (string-match org-bracket-link-analytic-regexp link)
  18383. (replace-match (if (match-end 5)
  18384. (match-string 5 link)
  18385. (concat (match-string 1 link)
  18386. (match-string 3 link)))
  18387. nil t link)
  18388. link)))
  18389. ;; Speedbar support
  18390. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  18391. "Overlay marking the agenda restriction line in speedbar.")
  18392. (overlay-put org-speedbar-restriction-lock-overlay
  18393. 'face 'org-agenda-restriction-lock)
  18394. (overlay-put org-speedbar-restriction-lock-overlay
  18395. 'help-echo "Agendas are currently limited to this item.")
  18396. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  18397. (defun org-speedbar-set-agenda-restriction ()
  18398. "Restrict future agenda commands to the location at point in speedbar.
  18399. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  18400. (interactive)
  18401. (require 'org-agenda)
  18402. (let (p m tp np dir txt)
  18403. (cond
  18404. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18405. 'org-imenu t))
  18406. (setq m (get-text-property p 'org-imenu-marker))
  18407. (with-current-buffer (marker-buffer m)
  18408. (goto-char m)
  18409. (org-agenda-set-restriction-lock 'subtree)))
  18410. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  18411. 'speedbar-function 'speedbar-find-file))
  18412. (setq tp (previous-single-property-change
  18413. (1+ p) 'speedbar-function)
  18414. np (next-single-property-change
  18415. tp 'speedbar-function)
  18416. dir (speedbar-line-directory)
  18417. txt (buffer-substring-no-properties (or tp (point-min))
  18418. (or np (point-max))))
  18419. (with-current-buffer (find-file-noselect
  18420. (let ((default-directory dir))
  18421. (expand-file-name txt)))
  18422. (unless (org-mode-p)
  18423. (error "Cannot restrict to non-Org-mode file"))
  18424. (org-agenda-set-restriction-lock 'file)))
  18425. (t (error "Don't know how to restrict Org-mode's agenda")))
  18426. (move-overlay org-speedbar-restriction-lock-overlay
  18427. (point-at-bol) (point-at-eol))
  18428. (setq current-prefix-arg nil)
  18429. (org-agenda-maybe-redo)))
  18430. (eval-after-load "speedbar"
  18431. '(progn
  18432. (speedbar-add-supported-extension ".org")
  18433. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  18434. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  18435. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  18436. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18437. (add-hook 'speedbar-visiting-tag-hook
  18438. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  18439. ;;; Fixes and Hacks for problems with other packages
  18440. ;; Make flyspell not check words in links, to not mess up our keymap
  18441. (defun org-mode-flyspell-verify ()
  18442. "Don't let flyspell put overlays at active buttons."
  18443. (and (not (get-text-property (max (1- (point)) (point-min)) 'keymap))
  18444. (not (get-text-property (max (1- (point)) (point-min)) 'org-no-flyspell))))
  18445. (defun org-remove-flyspell-overlays-in (beg end)
  18446. "Remove flyspell overlays in region."
  18447. (and (org-bound-and-true-p flyspell-mode)
  18448. (fboundp 'flyspell-delete-region-overlays)
  18449. (flyspell-delete-region-overlays beg end))
  18450. (add-text-properties beg end '(org-no-flyspell t)))
  18451. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  18452. (eval-after-load "bookmark"
  18453. '(if (boundp 'bookmark-after-jump-hook)
  18454. ;; We can use the hook
  18455. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  18456. ;; Hook not available, use advice
  18457. (defadvice bookmark-jump (after org-make-visible activate)
  18458. "Make the position visible."
  18459. (org-bookmark-jump-unhide))))
  18460. ;; Make sure saveplace shows the location if it was hidden
  18461. (eval-after-load "saveplace"
  18462. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  18463. "Make the position visible."
  18464. (org-bookmark-jump-unhide)))
  18465. ;; Make sure ecb shows the location if it was hidden
  18466. (eval-after-load "ecb"
  18467. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  18468. "Make hierarchy visible when jumping into location from ECB tree buffer."
  18469. (if (org-mode-p)
  18470. (org-show-context))))
  18471. (defun org-bookmark-jump-unhide ()
  18472. "Unhide the current position, to show the bookmark location."
  18473. (and (org-mode-p)
  18474. (or (outline-invisible-p)
  18475. (save-excursion (goto-char (max (point-min) (1- (point))))
  18476. (outline-invisible-p)))
  18477. (org-show-context 'bookmark-jump)))
  18478. ;; Make session.el ignore our circular variable
  18479. (eval-after-load "session"
  18480. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  18481. ;;;; Experimental code
  18482. (defun org-closed-in-range ()
  18483. "Sparse tree of items closed in a certain time range.
  18484. Still experimental, may disappear in the future."
  18485. (interactive)
  18486. ;; Get the time interval from the user.
  18487. (let* ((time1 (org-float-time
  18488. (org-read-date nil 'to-time nil "Starting date: ")))
  18489. (time2 (org-float-time
  18490. (org-read-date nil 'to-time nil "End date:")))
  18491. ;; callback function
  18492. (callback (lambda ()
  18493. (let ((time
  18494. (org-float-time
  18495. (apply 'encode-time
  18496. (org-parse-time-string
  18497. (match-string 1))))))
  18498. ;; check if time in interval
  18499. (and (>= time time1) (<= time time2))))))
  18500. ;; make tree, check each match with the callback
  18501. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  18502. ;;;; Finish up
  18503. (provide 'org)
  18504. (run-hooks 'org-load-hook)
  18505. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  18506. ;;; org.el ends here