org.el 583 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689
  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
  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: 6.23b
  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. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  73. ;; the file noutline.el being loaded.
  74. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  75. ;; We require noutline, which might be provided in outline.el
  76. (require 'outline) (require 'noutline)
  77. ;; Other stuff we need.
  78. (require 'time-date)
  79. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  80. (require 'easymenu)
  81. (require 'org-macs)
  82. (require 'org-compat)
  83. (require 'org-faces)
  84. (require 'org-list)
  85. (require 'org-footnote)
  86. ;;;; Customization variables
  87. ;;; Version
  88. (defconst org-version "6.23b"
  89. "The version number of the file org.el.")
  90. (defun org-version (&optional here)
  91. "Show the org-mode version in the echo area.
  92. With prefix arg HERE, insert it at point."
  93. (interactive "P")
  94. (let ((version (format "Org-mode version %s" org-version)))
  95. (message version)
  96. (if here
  97. (insert version))))
  98. ;;; Compatibility constants
  99. ;;; The custom variables
  100. (defgroup org nil
  101. "Outline-based notes management and organizer."
  102. :tag "Org"
  103. :group 'outlines
  104. :group 'hypermedia
  105. :group 'calendar)
  106. (defcustom org-load-hook nil
  107. "Hook that is run after org.el has been loaded."
  108. :group 'org
  109. :type 'hook)
  110. (defvar org-modules) ; defined below
  111. (defvar org-modules-loaded nil
  112. "Have the modules been loaded already?")
  113. (defun org-load-modules-maybe (&optional force)
  114. "Load all extensions listed in `org-modules'."
  115. (when (or force (not org-modules-loaded))
  116. (mapc (lambda (ext)
  117. (condition-case nil (require ext)
  118. (error (message "Problems while trying to load feature `%s'" ext))))
  119. org-modules)
  120. (setq org-modules-loaded t)))
  121. (defun org-set-modules (var value)
  122. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  123. (set var value)
  124. (when (featurep 'org)
  125. (org-load-modules-maybe 'force)))
  126. (when (org-bound-and-true-p org-modules)
  127. (let ((a (member 'org-infojs org-modules)))
  128. (and a (setcar a 'org-jsinfo))))
  129. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  130. "Modules that should always be loaded together with org.el.
  131. If a description starts with <C>, the file is not part of Emacs
  132. and loading it will require that you have downloaded and properly installed
  133. the org-mode distribution.
  134. You can also use this system to load external packages (i.e. neither Org
  135. core modules, not modules from the CONTRIB directory). Just add symbols
  136. to the end of the list. If the package is called org-xyz.el, then you need
  137. to add the symbol `xyz', and the package must have a call to
  138. (provide 'org-xyz)"
  139. :group 'org
  140. :set 'org-set-modules
  141. :type
  142. '(set :greedy t
  143. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  144. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  145. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  146. (const :tag " id: Global IDs for identifying entries" org-id)
  147. (const :tag " info: Links to Info nodes" org-info)
  148. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  149. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  150. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  151. (const :tag " mew Links to Mew folders/messages" org-mew)
  152. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  153. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  154. (const :tag " vm: Links to VM folders/messages" org-vm)
  155. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  156. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  157. (const :tag " mouse: Additional mouse support" org-mouse)
  158. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  159. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  160. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  161. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  162. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  163. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  164. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  165. (const :tag "C eval: Include command output as text" org-eval)
  166. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  167. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  168. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  169. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  170. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  171. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  172. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  173. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  174. (const :tag "C R: Computation using the R language" org-R)
  175. (const :tag "C registry: A registry for Org links" org-registry)
  176. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  177. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  178. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  179. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  180. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  181. (defcustom org-support-shift-select nil
  182. "Non-nil means, make shift-cursor commands select text when possible.
  183. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
  184. selecting a region, or enlarge thusly regions started in this way.
  185. In Org-mode, in special contexts, these same keys are used for other
  186. purposes, important enough to compete with shift selection. Org tries
  187. to balance these needs by supporting `shift-select-mode' outside these
  188. special contexts, under control of this variable.
  189. The default of this variable is nil, to avoid confusing behavior. Shifted
  190. cursor keys will then execute Org commands in the following contexts:
  191. - on a headline, changing TODO state (left/right) and priority (up/down)
  192. - on a time stamp, changing the time
  193. - in a plain list item, changing the bullet type
  194. - in a property definition line, switching between allowed values
  195. - in the BEGIN line of a clock table (changing the time block).
  196. Outside these contexts, the commands will throw an error.
  197. When this variable is t and the cursor is not in a special context,
  198. Org-mode will support shift-selection for making and enlarging regions.
  199. To make this more effective, the bullet cycling will no longer happen
  200. anywhere in an item line, but only if the cursor is exactly on the bullet.
  201. If you set this variable to the symbol `always', then the keys
  202. will not be special in headlines, property lines, and item lines, to make
  203. shift selection work there as well. If this is what you want, you can
  204. use the following alternative commands: `C-c C-t' and `C-c ,' to
  205. change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
  206. TODO sets, `C-c -' to cycle item bullet types, and properties can be
  207. edited by hand or in column view.
  208. However, when the cursor is on a timestamp, shift-cursor commands
  209. will still edit the time stamp - this is just too good to give up.
  210. XEmacs user should have this variable set to nil, because shift-select-mode
  211. is Emacs 23 only."
  212. :group 'org
  213. :type '(choice
  214. (const :tag "Never" nil)
  215. (const :tag "When outside special context" t)
  216. (const :tag "Everywhere except timestamps" always)))
  217. (defgroup org-startup nil
  218. "Options concerning startup of Org-mode."
  219. :tag "Org Startup"
  220. :group 'org)
  221. (defcustom org-startup-folded t
  222. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  223. This can also be configured on a per-file basis by adding one of
  224. the following lines anywhere in the buffer:
  225. #+STARTUP: fold
  226. #+STARTUP: nofold
  227. #+STARTUP: content"
  228. :group 'org-startup
  229. :type '(choice
  230. (const :tag "nofold: show all" nil)
  231. (const :tag "fold: overview" t)
  232. (const :tag "content: all headlines" content)))
  233. (defcustom org-startup-truncated t
  234. "Non-nil means, entering Org-mode will set `truncate-lines'.
  235. This is useful since some lines containing links can be very long and
  236. uninteresting. Also tables look terrible when wrapped."
  237. :group 'org-startup
  238. :type 'boolean)
  239. (defcustom org-startup-align-all-tables nil
  240. "Non-nil means, align all tables when visiting a file.
  241. This is useful when the column width in tables is forced with <N> cookies
  242. in table fields. Such tables will look correct only after the first re-align.
  243. This can also be configured on a per-file basis by adding one of
  244. the following lines anywhere in the buffer:
  245. #+STARTUP: align
  246. #+STARTUP: noalign"
  247. :group 'org-startup
  248. :type 'boolean)
  249. (defcustom org-insert-mode-line-in-empty-file nil
  250. "Non-nil means insert the first line setting Org-mode in empty files.
  251. When the function `org-mode' is called interactively in an empty file, this
  252. normally means that the file name does not automatically trigger Org-mode.
  253. To ensure that the file will always be in Org-mode in the future, a
  254. line enforcing Org-mode will be inserted into the buffer, if this option
  255. has been set."
  256. :group 'org-startup
  257. :type 'boolean)
  258. (defcustom org-replace-disputed-keys nil
  259. "Non-nil means use alternative key bindings for some keys.
  260. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  261. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  262. If you want to use Org-mode together with one of these other modes,
  263. or more generally if you would like to move some Org-mode commands to
  264. other keys, set this variable and configure the keys with the variable
  265. `org-disputed-keys'.
  266. This option is only relevant at load-time of Org-mode, and must be set
  267. *before* org.el is loaded. Changing it requires a restart of Emacs to
  268. become effective."
  269. :group 'org-startup
  270. :type 'boolean)
  271. (defcustom org-use-extra-keys nil
  272. "Non-nil means use extra key sequence definitions for certain
  273. commands. This happens automatically if you run XEmacs or if
  274. window-system is nil. This variable lets you do the same
  275. manually. You must set it before loading org.
  276. Example: on Carbon Emacs 22 running graphically, with an external
  277. keyboard on a Powerbook, the default way of setting M-left might
  278. not work for either Alt or ESC. Setting this variable will make
  279. it work for ESC."
  280. :group 'org-startup
  281. :type 'boolean)
  282. (if (fboundp 'defvaralias)
  283. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  284. (defcustom org-disputed-keys
  285. '(([(shift up)] . [(meta p)])
  286. ([(shift down)] . [(meta n)])
  287. ([(shift left)] . [(meta -)])
  288. ([(shift right)] . [(meta +)])
  289. ([(control shift right)] . [(meta shift +)])
  290. ([(control shift left)] . [(meta shift -)]))
  291. "Keys for which Org-mode and other modes compete.
  292. This is an alist, cars are the default keys, second element specifies
  293. the alternative to use when `org-replace-disputed-keys' is t.
  294. Keys can be specified in any syntax supported by `define-key'.
  295. The value of this option takes effect only at Org-mode's startup,
  296. therefore you'll have to restart Emacs to apply it after changing."
  297. :group 'org-startup
  298. :type 'alist)
  299. (defun org-key (key)
  300. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  301. Or return the original if not disputed."
  302. (if org-replace-disputed-keys
  303. (let* ((nkey (key-description key))
  304. (x (org-find-if (lambda (x)
  305. (equal (key-description (car x)) nkey))
  306. org-disputed-keys)))
  307. (if x (cdr x) key))
  308. key))
  309. (defun org-find-if (predicate seq)
  310. (catch 'exit
  311. (while seq
  312. (if (funcall predicate (car seq))
  313. (throw 'exit (car seq))
  314. (pop seq)))))
  315. (defun org-defkey (keymap key def)
  316. "Define a key, possibly translated, as returned by `org-key'."
  317. (define-key keymap (org-key key) def))
  318. (defcustom org-ellipsis nil
  319. "The ellipsis to use in the Org-mode outline.
  320. When nil, just use the standard three dots. When a string, use that instead,
  321. When a face, use the standard 3 dots, but with the specified face.
  322. The change affects only Org-mode (which will then use its own display table).
  323. Changing this requires executing `M-x org-mode' in a buffer to become
  324. effective."
  325. :group 'org-startup
  326. :type '(choice (const :tag "Default" nil)
  327. (face :tag "Face" :value org-warning)
  328. (string :tag "String" :value "...#")))
  329. (defvar org-display-table nil
  330. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  331. (defgroup org-keywords nil
  332. "Keywords in Org-mode."
  333. :tag "Org Keywords"
  334. :group 'org)
  335. (defcustom org-deadline-string "DEADLINE:"
  336. "String to mark deadline entries.
  337. A deadline is this string, followed by a time stamp. Should be a word,
  338. terminated by a colon. You can insert a schedule keyword and
  339. a timestamp with \\[org-deadline].
  340. Changes become only effective after restarting Emacs."
  341. :group 'org-keywords
  342. :type 'string)
  343. (defcustom org-scheduled-string "SCHEDULED:"
  344. "String to mark scheduled TODO entries.
  345. A schedule is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-schedule].
  348. Changes become only effective after restarting Emacs."
  349. :group 'org-keywords
  350. :type 'string)
  351. (defcustom org-closed-string "CLOSED:"
  352. "String used as the prefix for timestamps logging closing a TODO entry."
  353. :group 'org-keywords
  354. :type 'string)
  355. (defcustom org-clock-string "CLOCK:"
  356. "String used as prefix for timestamps clocking work hours on an item."
  357. :group 'org-keywords
  358. :type 'string)
  359. (defcustom org-comment-string "COMMENT"
  360. "Entries starting with this keyword will never be exported.
  361. An entry can be toggled between COMMENT and normal with
  362. \\[org-toggle-comment].
  363. Changes become only effective after restarting Emacs."
  364. :group 'org-keywords
  365. :type 'string)
  366. (defcustom org-quote-string "QUOTE"
  367. "Entries starting with this keyword will be exported in fixed-width font.
  368. Quoting applies only to the text in the entry following the headline, and does
  369. not extend beyond the next headline, even if that is lower level.
  370. An entry can be toggled between QUOTE and normal with
  371. \\[org-toggle-fixed-width-section]."
  372. :group 'org-keywords
  373. :type 'string)
  374. (defconst org-repeat-re
  375. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  376. "Regular expression for specifying repeated events.
  377. After a match, group 1 contains the repeat expression.")
  378. (defgroup org-structure nil
  379. "Options concerning the general structure of Org-mode files."
  380. :tag "Org Structure"
  381. :group 'org)
  382. (defgroup org-reveal-location nil
  383. "Options about how to make context of a location visible."
  384. :tag "Org Reveal Location"
  385. :group 'org-structure)
  386. (defconst org-context-choice
  387. '(choice
  388. (const :tag "Always" t)
  389. (const :tag "Never" nil)
  390. (repeat :greedy t :tag "Individual contexts"
  391. (cons
  392. (choice :tag "Context"
  393. (const agenda)
  394. (const org-goto)
  395. (const occur-tree)
  396. (const tags-tree)
  397. (const link-search)
  398. (const mark-goto)
  399. (const bookmark-jump)
  400. (const isearch)
  401. (const default))
  402. (boolean))))
  403. "Contexts for the reveal options.")
  404. (defcustom org-show-hierarchy-above '((default . t))
  405. "Non-nil means, show full hierarchy when revealing a location.
  406. Org-mode often shows locations in an org-mode file which might have
  407. been invisible before. When this is set, the hierarchy of headings
  408. above the exposed location is shown.
  409. Turning this off for example for sparse trees makes them very compact.
  410. Instead of t, this can also be an alist specifying this option for different
  411. contexts. Valid contexts are
  412. agenda when exposing an entry from the agenda
  413. org-goto when using the command `org-goto' on key C-c C-j
  414. occur-tree when using the command `org-occur' on key C-c /
  415. tags-tree when constructing a sparse tree based on tags matches
  416. link-search when exposing search matches associated with a link
  417. mark-goto when exposing the jump goal of a mark
  418. bookmark-jump when exposing a bookmark location
  419. isearch when exiting from an incremental search
  420. default default for all contexts not set explicitly"
  421. :group 'org-reveal-location
  422. :type org-context-choice)
  423. (defcustom org-show-following-heading '((default . nil))
  424. "Non-nil means, show following heading when revealing a location.
  425. Org-mode often shows locations in an org-mode file which might have
  426. been invisible before. When this is set, the heading following the
  427. match is shown.
  428. Turning this off for example for sparse trees makes them very compact,
  429. but makes it harder to edit the location of the match. In such a case,
  430. use the command \\[org-reveal] to show more context.
  431. Instead of t, this can also be an alist specifying this option for different
  432. contexts. See `org-show-hierarchy-above' for valid contexts."
  433. :group 'org-reveal-location
  434. :type org-context-choice)
  435. (defcustom org-show-siblings '((default . nil) (isearch t))
  436. "Non-nil means, show all sibling heading when revealing a location.
  437. Org-mode often shows locations in an org-mode file which might have
  438. been invisible before. When this is set, the sibling of the current entry
  439. heading are all made visible. If `org-show-hierarchy-above' is t,
  440. the same happens on each level of the hierarchy above the current entry.
  441. By default this is on for the isearch context, off for all other contexts.
  442. Turning this off for example for sparse trees makes them very compact,
  443. but makes it harder to edit the location of the match. In such a case,
  444. use the command \\[org-reveal] to show more context.
  445. Instead of t, this can also be an alist specifying this option for different
  446. contexts. See `org-show-hierarchy-above' for valid contexts."
  447. :group 'org-reveal-location
  448. :type org-context-choice)
  449. (defcustom org-show-entry-below '((default . nil))
  450. "Non-nil means, show the entry below a headline when revealing a location.
  451. Org-mode often shows locations in an org-mode file which might have
  452. been invisible before. When this is set, the text below the headline that is
  453. exposed is also shown.
  454. By default this is off for all contexts.
  455. Instead of t, this can also be an alist specifying this option for different
  456. contexts. See `org-show-hierarchy-above' for valid contexts."
  457. :group 'org-reveal-location
  458. :type org-context-choice)
  459. (defcustom org-indirect-buffer-display 'other-window
  460. "How should indirect tree buffers be displayed?
  461. This applies to indirect buffers created with the commands
  462. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  463. Valid values are:
  464. current-window Display in the current window
  465. other-window Just display in another window.
  466. dedicated-frame Create one new frame, and re-use it each time.
  467. new-frame Make a new frame each time. Note that in this case
  468. previously-made indirect buffers are kept, and you need to
  469. kill these buffers yourself."
  470. :group 'org-structure
  471. :group 'org-agenda-windows
  472. :type '(choice
  473. (const :tag "In current window" current-window)
  474. (const :tag "In current frame, other window" other-window)
  475. (const :tag "Each time a new frame" new-frame)
  476. (const :tag "One dedicated frame" dedicated-frame)))
  477. (defgroup org-cycle nil
  478. "Options concerning visibility cycling in Org-mode."
  479. :tag "Org Cycle"
  480. :group 'org-structure)
  481. (defcustom org-drawers '("PROPERTIES" "CLOCK" "LOGBOOK")
  482. "Names of drawers. Drawers are not opened by cycling on the headline above.
  483. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  484. this:
  485. :DRAWERNAME:
  486. .....
  487. :END:
  488. The drawer \"PROPERTIES\" is special for capturing properties through
  489. the property API.
  490. Drawers can be defined on the per-file basis with a line like:
  491. #+DRAWERS: HIDDEN STATE PROPERTIES"
  492. :group 'org-structure
  493. :type '(repeat (string :tag "Drawer Name")))
  494. (defcustom org-cycle-global-at-bob nil
  495. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  496. This makes it possible to do global cycling without having to use S-TAB or
  497. C-u TAB. For this special case to work, the first line of the buffer
  498. must not be a headline - it may be empty or some other text. When used in
  499. this way, `org-cycle-hook' is disables temporarily, to make sure the
  500. cursor stays at the beginning of the buffer.
  501. When this option is nil, don't do anything special at the beginning
  502. of the buffer."
  503. :group 'org-cycle
  504. :type 'boolean)
  505. (defcustom org-cycle-emulate-tab t
  506. "Where should `org-cycle' emulate TAB.
  507. nil Never
  508. white Only in completely white lines
  509. whitestart Only at the beginning of lines, before the first non-white char
  510. t Everywhere except in headlines
  511. exc-hl-bol Everywhere except at the start of a headline
  512. If TAB is used in a place where it does not emulate TAB, the current subtree
  513. visibility is cycled."
  514. :group 'org-cycle
  515. :type '(choice (const :tag "Never" nil)
  516. (const :tag "Only in completely white lines" white)
  517. (const :tag "Before first char in a line" whitestart)
  518. (const :tag "Everywhere except in headlines" t)
  519. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  520. ))
  521. (defcustom org-cycle-separator-lines 2
  522. "Number of empty lines needed to keep an empty line between collapsed trees.
  523. If you leave an empty line between the end of a subtree and the following
  524. headline, this empty line is hidden when the subtree is folded.
  525. Org-mode will leave (exactly) one empty line visible if the number of
  526. empty lines is equal or larger to the number given in this variable.
  527. So the default 2 means, at least 2 empty lines after the end of a subtree
  528. are needed to produce free space between a collapsed subtree and the
  529. following headline.
  530. Special case: when 0, never leave empty lines in collapsed view."
  531. :group 'org-cycle
  532. :type 'integer)
  533. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  534. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  535. org-cycle-hide-drawers
  536. org-cycle-show-empty-lines
  537. org-optimize-window-after-visibility-change)
  538. "Hook that is run after `org-cycle' has changed the buffer visibility.
  539. The function(s) in this hook must accept a single argument which indicates
  540. the new state that was set by the most recent `org-cycle' command. The
  541. argument is a symbol. After a global state change, it can have the values
  542. `overview', `content', or `all'. After a local state change, it can have
  543. the values `folded', `children', or `subtree'."
  544. :group 'org-cycle
  545. :type 'hook)
  546. (defgroup org-edit-structure nil
  547. "Options concerning structure editing in Org-mode."
  548. :tag "Org Edit Structure"
  549. :group 'org-structure)
  550. (defcustom org-odd-levels-only nil
  551. "Non-nil means, skip even levels and only use odd levels for the outline.
  552. This has the effect that two stars are being added/taken away in
  553. promotion/demotion commands. It also influences how levels are
  554. handled by the exporters.
  555. Changing it requires restart of `font-lock-mode' to become effective
  556. for fontification also in regions already fontified.
  557. You may also set this on a per-file basis by adding one of the following
  558. lines to the buffer:
  559. #+STARTUP: odd
  560. #+STARTUP: oddeven"
  561. :group 'org-edit-structure
  562. :group 'org-font-lock
  563. :type 'boolean)
  564. (defcustom org-adapt-indentation t
  565. "Non-nil means, adapt indentation when promoting and demoting.
  566. When this is set and the *entire* text in an entry is indented, the
  567. indentation is increased by one space in a demotion command, and
  568. decreased by one in a promotion command. If any line in the entry
  569. body starts at column 0, indentation is not changed at all."
  570. :group 'org-edit-structure
  571. :type 'boolean)
  572. (defcustom org-special-ctrl-a/e nil
  573. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  574. When t, `C-a' will bring back the cursor to the beginning of the
  575. headline text, i.e. after the stars and after a possible TODO keyword.
  576. In an item, this will be the position after the bullet.
  577. When the cursor is already at that position, another `C-a' will bring
  578. it to the beginning of the line.
  579. `C-e' will jump to the end of the headline, ignoring the presence of tags
  580. in the headline. A second `C-e' will then jump to the true end of the
  581. line, after any tags.
  582. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  583. and only a directly following, identical keypress will bring the cursor
  584. to the special positions.
  585. This may also be a cons cell where the behavior for `C-a' and `C-e' is
  586. set separately."
  587. :group 'org-edit-structure
  588. :type '(choice
  589. (const :tag "off" nil)
  590. (const :tag "after stars/bullet and before tags first" t)
  591. (const :tag "true line boundary first" reversed)
  592. (cons :tag "Set C-a and C-e separately"
  593. (choice :tag "Special C-a"
  594. (const :tag "off" nil)
  595. (const :tag "after stars/bullet first" t)
  596. (const :tag "before stars/bullet first" reversed))
  597. (choice :tag "Special C-e"
  598. (const :tag "off" nil)
  599. (const :tag "before tags first" t)
  600. (const :tag "after tags first" reversed)))))
  601. (if (fboundp 'defvaralias)
  602. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  603. (defcustom org-special-ctrl-k nil
  604. "Non-nil means `C-k' will behave specially in headlines.
  605. When nil, `C-k' will call the default `kill-line' command.
  606. When t, the following will happen while the cursor is in the headline:
  607. - When the cursor is at the beginning of a headline, kill the entire
  608. line and possible the folded subtree below the line.
  609. - When in the middle of the headline text, kill the headline up to the tags.
  610. - When after the headline text, kill the tags."
  611. :group 'org-edit-structure
  612. :type 'boolean)
  613. (defcustom org-yank-folded-subtrees t
  614. "Non-nil means, when yanking subtrees, fold them.
  615. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  616. it starts with a heading and all other headings in it are either children
  617. or siblings, then fold all the subtrees. However, do this only if no
  618. text after the yank would be swallowed into a folded tree by this action."
  619. :group 'org-edit-structure
  620. :type 'boolean)
  621. (defcustom org-yank-adjusted-subtrees nil
  622. "Non-nil means, when yanking subtrees, adjust the level.
  623. With this setting, `org-paste-subtree' is used to insert the subtree, see
  624. this function for details."
  625. :group 'org-edit-structure
  626. :type 'boolean)
  627. (defcustom org-M-RET-may-split-line '((default . t))
  628. "Non-nil means, M-RET will split the line at the cursor position.
  629. When nil, it will go to the end of the line before making a
  630. new line.
  631. You may also set this option in a different way for different
  632. contexts. Valid contexts are:
  633. headline when creating a new headline
  634. item when creating a new item
  635. table in a table field
  636. default the value to be used for all contexts not explicitly
  637. customized"
  638. :group 'org-structure
  639. :group 'org-table
  640. :type '(choice
  641. (const :tag "Always" t)
  642. (const :tag "Never" nil)
  643. (repeat :greedy t :tag "Individual contexts"
  644. (cons
  645. (choice :tag "Context"
  646. (const headline)
  647. (const item)
  648. (const table)
  649. (const default))
  650. (boolean)))))
  651. (defcustom org-insert-heading-respect-content nil
  652. "Non-nil means, insert new headings after the current subtree.
  653. When nil, the new heading is created directly after the current line.
  654. The commands \\[org-insert-heading-respect-content] and
  655. \\[org-insert-todo-heading-respect-content] turn this variable on
  656. for the duration of the command."
  657. :group 'org-structure
  658. :type 'boolean)
  659. (defcustom org-blank-before-new-entry '((heading . auto)
  660. (plain-list-item . auto))
  661. "Should `org-insert-heading' leave a blank line before new heading/item?
  662. The value is an alist, with `heading' and `plain-list-item' as car,
  663. and a boolean flag as cdr."
  664. :group 'org-edit-structure
  665. :type '(list
  666. (cons (const heading)
  667. (choice (const :tag "Never" nil)
  668. (const :tag "Always" t)
  669. (const :tag "Auto" auto)))
  670. (cons (const plain-list-item)
  671. (choice (const :tag "Never" nil)
  672. (const :tag "Always" t)
  673. (const :tag "Auto" auto)))))
  674. (defcustom org-insert-heading-hook nil
  675. "Hook being run after inserting a new heading."
  676. :group 'org-edit-structure
  677. :type 'hook)
  678. (defcustom org-enable-fixed-width-editor t
  679. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  680. This currently only means, they are never auto-wrapped.
  681. When nil, such lines will be treated like ordinary lines.
  682. See also the QUOTE keyword."
  683. :group 'org-edit-structure
  684. :type 'boolean)
  685. (defcustom org-edit-src-region-extra nil
  686. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  687. For examples see the function `org-edit-src-find-region-and-lang'.
  688. The regular expression identifying the begin marker should end with a newline,
  689. and the regexp marking the end line should start with a newline, to make sure
  690. there are kept outside the narrowed region."
  691. :group 'org-edit-structure
  692. :type '(repeat
  693. (list
  694. (regexp :tag "begin regexp")
  695. (regexp :tag "end regexp")
  696. (choice :tag "language"
  697. (string :tag "specify")
  698. (integer :tag "from match group")
  699. (const :tag "from `lang' element")
  700. (const :tag "from `style' element")))))
  701. (defcustom org-coderef-label-format "(ref:%s)"
  702. "The default coderef format.
  703. This format string will be used to search for coderef labels in literal
  704. examples (EXAMPLE and SRC blocks). The format can be overwritten
  705. an individual literal example with the -f option, like
  706. #+BEGIN_SRC pascal +n -r -l \"((%s))\"
  707. ...
  708. #+END_SRC
  709. If you want to use this for HTML export, make sure that the format does
  710. not introduce special font-locking, and avoid the HTML special
  711. characters `<', `>', and `&'. The reason for this restriction is that
  712. the labels are searched for only after htmlize has done its job."
  713. :group 'org-edit-structure ; FIXME this is not in the right group
  714. :type 'string)
  715. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  716. "The mode that should be used to edit fixed-width regions.
  717. These are the regions where each line starts with a colon."
  718. :group 'org-edit-structure
  719. :type '(choice
  720. (const artist-mode)
  721. (const picture-mode)
  722. (const fundamental-mode)
  723. (function :tag "Other (specify)")))
  724. (defcustom org-goto-auto-isearch t
  725. "Non-nil means, typing characters in org-goto starts incremental search."
  726. :group 'org-edit-structure
  727. :type 'boolean)
  728. (defgroup org-sparse-trees nil
  729. "Options concerning sparse trees in Org-mode."
  730. :tag "Org Sparse Trees"
  731. :group 'org-structure)
  732. (defcustom org-highlight-sparse-tree-matches t
  733. "Non-nil means, highlight all matches that define a sparse tree.
  734. The highlights will automatically disappear the next time the buffer is
  735. changed by an edit command."
  736. :group 'org-sparse-trees
  737. :type 'boolean)
  738. (defcustom org-remove-highlights-with-change t
  739. "Non-nil means, any change to the buffer will remove temporary highlights.
  740. Such highlights are created by `org-occur' and `org-clock-display'.
  741. When nil, `C-c C-c needs to be used to get rid of the highlights.
  742. The highlights created by `org-preview-latex-fragment' always need
  743. `C-c C-c' to be removed."
  744. :group 'org-sparse-trees
  745. :group 'org-time
  746. :type 'boolean)
  747. (defcustom org-occur-hook '(org-first-headline-recenter)
  748. "Hook that is run after `org-occur' has constructed a sparse tree.
  749. This can be used to recenter the window to show as much of the structure
  750. as possible."
  751. :group 'org-sparse-trees
  752. :type 'hook)
  753. (defgroup org-imenu-and-speedbar nil
  754. "Options concerning imenu and speedbar in Org-mode."
  755. :tag "Org Imenu and Speedbar"
  756. :group 'org-structure)
  757. (defcustom org-imenu-depth 2
  758. "The maximum level for Imenu access to Org-mode headlines.
  759. This also applied for speedbar access."
  760. :group 'org-imenu-and-speedbar
  761. :type 'number)
  762. (defgroup org-table nil
  763. "Options concerning tables in Org-mode."
  764. :tag "Org Table"
  765. :group 'org)
  766. (defcustom org-enable-table-editor 'optimized
  767. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  768. When nil, such lines will be treated like ordinary lines.
  769. When equal to the symbol `optimized', the table editor will be optimized to
  770. do the following:
  771. - Automatic overwrite mode in front of whitespace in table fields.
  772. This makes the structure of the table stay in tact as long as the edited
  773. field does not exceed the column width.
  774. - Minimize the number of realigns. Normally, the table is aligned each time
  775. TAB or RET are pressed to move to another field. With optimization this
  776. happens only if changes to a field might have changed the column width.
  777. Optimization requires replacing the functions `self-insert-command',
  778. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  779. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  780. very good at guessing when a re-align will be necessary, but you can always
  781. force one with \\[org-ctrl-c-ctrl-c].
  782. If you would like to use the optimized version in Org-mode, but the
  783. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  784. This variable can be used to turn on and off the table editor during a session,
  785. but in order to toggle optimization, a restart is required.
  786. See also the variable `org-table-auto-blank-field'."
  787. :group 'org-table
  788. :type '(choice
  789. (const :tag "off" nil)
  790. (const :tag "on" t)
  791. (const :tag "on, optimized" optimized)))
  792. (defcustom org-table-tab-recognizes-table.el t
  793. "Non-nil means, TAB will automatically notice a table.el table.
  794. When it sees such a table, it moves point into it and - if necessary -
  795. calls `table-recognize-table'."
  796. :group 'org-table-editing
  797. :type 'boolean)
  798. (defgroup org-link nil
  799. "Options concerning links in Org-mode."
  800. :tag "Org Link"
  801. :group 'org)
  802. (defvar org-link-abbrev-alist-local nil
  803. "Buffer-local version of `org-link-abbrev-alist', which see.
  804. The value of this is taken from the #+LINK lines.")
  805. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  806. (defcustom org-link-abbrev-alist nil
  807. "Alist of link abbreviations.
  808. The car of each element is a string, to be replaced at the start of a link.
  809. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  810. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  811. [[linkkey:tag][description]]
  812. The 'linkkey' must be a word word, starting with a letter, followed
  813. by letters, numbers, '-' or '_'.
  814. If REPLACE is a string, the tag will simply be appended to create the link.
  815. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  816. the placeholder \"%h\" will cause a url-encoded version of the tag to
  817. be inserted at that point (see the function `url-hexify-string').
  818. REPLACE may also be a function that will be called with the tag as the
  819. only argument to create the link, which should be returned as a string.
  820. See the manual for examples."
  821. :group 'org-link
  822. :type '(repeat
  823. (cons
  824. (string :tag "Protocol")
  825. (choice
  826. (string :tag "Format")
  827. (function)))))
  828. (defcustom org-descriptive-links t
  829. "Non-nil means, hide link part and only show description of bracket links.
  830. Bracket links are like [[link][description]]. This variable sets the initial
  831. state in new org-mode buffers. The setting can then be toggled on a
  832. per-buffer basis from the Org->Hyperlinks menu."
  833. :group 'org-link
  834. :type 'boolean)
  835. (defcustom org-link-file-path-type 'adaptive
  836. "How the path name in file links should be stored.
  837. Valid values are:
  838. relative Relative to the current directory, i.e. the directory of the file
  839. into which the link is being inserted.
  840. absolute Absolute path, if possible with ~ for home directory.
  841. noabbrev Absolute path, no abbreviation of home directory.
  842. adaptive Use relative path for files in the current directory and sub-
  843. directories of it. For other files, use an absolute path."
  844. :group 'org-link
  845. :type '(choice
  846. (const relative)
  847. (const absolute)
  848. (const noabbrev)
  849. (const adaptive)))
  850. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  851. "Types of links that should be activated in Org-mode files.
  852. This is a list of symbols, each leading to the activation of a certain link
  853. type. In principle, it does not hurt to turn on most link types - there may
  854. be a small gain when turning off unused link types. The types are:
  855. bracket The recommended [[link][description]] or [[link]] links with hiding.
  856. angular Links in angular brackets that may contain whitespace like
  857. <bbdb:Carsten Dominik>.
  858. plain Plain links in normal text, no whitespace, like http://google.com.
  859. radio Text that is matched by a radio target, see manual for details.
  860. tag Tag settings in a headline (link to tag search).
  861. date Time stamps (link to calendar).
  862. footnote Footnote labels.
  863. Changing this variable requires a restart of Emacs to become effective."
  864. :group 'org-link
  865. :type '(set :greedy t
  866. (const :tag "Double bracket links (new style)" bracket)
  867. (const :tag "Angular bracket links (old style)" angular)
  868. (const :tag "Plain text links" plain)
  869. (const :tag "Radio target matches" radio)
  870. (const :tag "Tags" tag)
  871. (const :tag "Timestamps" date)
  872. (const :tag "Footnotes" footnote)))
  873. (defcustom org-make-link-description-function nil
  874. "Function to use to generate link descriptions from links. If
  875. nil the link location will be used. This function must take two
  876. parameters; the first is the link and the second the description
  877. org-insert-link has generated, and should return the description
  878. to use."
  879. :group 'org-link
  880. :type 'function)
  881. (defgroup org-link-store nil
  882. "Options concerning storing links in Org-mode."
  883. :tag "Org Store Link"
  884. :group 'org-link)
  885. (defcustom org-email-link-description-format "Email %c: %.30s"
  886. "Format of the description part of a link to an email or usenet message.
  887. The following %-escapes will be replaced by corresponding information:
  888. %F full \"From\" field
  889. %f name, taken from \"From\" field, address if no name
  890. %T full \"To\" field
  891. %t first name in \"To\" field, address if no name
  892. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  893. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  894. %s subject
  895. %m message-id.
  896. You may use normal field width specification between the % and the letter.
  897. This is for example useful to limit the length of the subject.
  898. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  899. :group 'org-link-store
  900. :type 'string)
  901. (defcustom org-from-is-user-regexp
  902. (let (r1 r2)
  903. (when (and user-mail-address (not (string= user-mail-address "")))
  904. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  905. (when (and user-full-name (not (string= user-full-name "")))
  906. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  907. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  908. "Regexp matched against the \"From:\" header of an email or usenet message.
  909. It should match if the message is from the user him/herself."
  910. :group 'org-link-store
  911. :type 'regexp)
  912. (defcustom org-link-to-org-use-id 'create-if-interactive
  913. "Non-nil means, storing a link to an Org file will use entry IDs.
  914. Note that before this variable is even considered, org-id must be loaded,
  915. to please customize `org-modules' and turn it on.
  916. The variable can have the following values:
  917. t Create an ID if needed to make a link to the current entry.
  918. create-if-interactive
  919. If `org-store-link' is called directly (interactively, as a user
  920. command), do create an ID to support the link. But when doing the
  921. job for remember, only use the ID if it already exists. The
  922. purpose of this setting is to avoid proliferation of unwanted
  923. IDs, just because you happen to be in an Org file when you
  924. call `org-remember' that automatically and preemptively
  925. creates a link. If you do want to get an ID link in a remember
  926. template to an entry not having an ID, create it first by
  927. explicitly creating a link to it, using `C-c C-l' first.
  928. use-existing
  929. Use existing ID, do not create one.
  930. nil Never use an ID to make a link, instead link using a text search for
  931. the headline text."
  932. :group 'org-link-store
  933. :type '(choice
  934. (const :tag "Create ID to make link" t)
  935. (const :tag "Create if string link interactively"
  936. 'create-if-interactive)
  937. (const :tag "Only use existing" 'use-existing)
  938. (const :tag "Do not use ID to create link" nil)))
  939. (defcustom org-context-in-file-links t
  940. "Non-nil means, file links from `org-store-link' contain context.
  941. A search string will be added to the file name with :: as separator and
  942. used to find the context when the link is activated by the command
  943. `org-open-at-point'.
  944. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  945. negates this setting for the duration of the command."
  946. :group 'org-link-store
  947. :type 'boolean)
  948. (defcustom org-keep-stored-link-after-insertion nil
  949. "Non-nil means, keep link in list for entire session.
  950. The command `org-store-link' adds a link pointing to the current
  951. location to an internal list. These links accumulate during a session.
  952. The command `org-insert-link' can be used to insert links into any
  953. Org-mode file (offering completion for all stored links). When this
  954. option is nil, every link which has been inserted once using \\[org-insert-link]
  955. will be removed from the list, to make completing the unused links
  956. more efficient."
  957. :group 'org-link-store
  958. :type 'boolean)
  959. (defgroup org-link-follow nil
  960. "Options concerning following links in Org-mode."
  961. :tag "Org Follow Link"
  962. :group 'org-link)
  963. (defcustom org-link-translation-function nil
  964. "Function to translate links with different syntax to Org syntax.
  965. This can be used to translate links created for example by the Planner
  966. or emacs-wiki packages to Org syntax.
  967. The function must accept two parameters, a TYPE containing the link
  968. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  969. which is everything after the link protocol. It should return a cons
  970. with possibly modified values of type and path.
  971. Org contains a function for this, so if you set this variable to
  972. `org-translate-link-from-planner', you should be able follow many
  973. links created by planner."
  974. :group 'org-link-follow
  975. :type 'function)
  976. (defcustom org-follow-link-hook nil
  977. "Hook that is run after a link has been followed."
  978. :group 'org-link-follow
  979. :type 'hook)
  980. (defcustom org-tab-follows-link nil
  981. "Non-nil means, on links TAB will follow the link.
  982. Needs to be set before org.el is loaded.
  983. This really should not be used, it does not make sense, and the
  984. implementation is bad."
  985. :group 'org-link-follow
  986. :type 'boolean)
  987. (defcustom org-return-follows-link nil
  988. "Non-nil means, on links RET will follow the link.
  989. Needs to be set before org.el is loaded."
  990. :group 'org-link-follow
  991. :type 'boolean)
  992. (defcustom org-mouse-1-follows-link
  993. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  994. "Non-nil means, mouse-1 on a link will follow the link.
  995. A longer mouse click will still set point. Does not work on XEmacs.
  996. Needs to be set before org.el is loaded."
  997. :group 'org-link-follow
  998. :type 'boolean)
  999. (defcustom org-mark-ring-length 4
  1000. "Number of different positions to be recorded in the ring
  1001. Changing this requires a restart of Emacs to work correctly."
  1002. :group 'org-link-follow
  1003. :type 'integer)
  1004. (defcustom org-link-frame-setup
  1005. '((vm . vm-visit-folder-other-frame)
  1006. (gnus . gnus-other-frame)
  1007. (file . find-file-other-window))
  1008. "Setup the frame configuration for following links.
  1009. When following a link with Emacs, it may often be useful to display
  1010. this link in another window or frame. This variable can be used to
  1011. set this up for the different types of links.
  1012. For VM, use any of
  1013. `vm-visit-folder'
  1014. `vm-visit-folder-other-frame'
  1015. For Gnus, use any of
  1016. `gnus'
  1017. `gnus-other-frame'
  1018. `org-gnus-no-new-news'
  1019. For FILE, use any of
  1020. `find-file'
  1021. `find-file-other-window'
  1022. `find-file-other-frame'
  1023. For the calendar, use the variable `calendar-setup'.
  1024. For BBDB, it is currently only possible to display the matches in
  1025. another window."
  1026. :group 'org-link-follow
  1027. :type '(list
  1028. (cons (const vm)
  1029. (choice
  1030. (const vm-visit-folder)
  1031. (const vm-visit-folder-other-window)
  1032. (const vm-visit-folder-other-frame)))
  1033. (cons (const gnus)
  1034. (choice
  1035. (const gnus)
  1036. (const gnus-other-frame)
  1037. (const org-gnus-no-new-news)))
  1038. (cons (const file)
  1039. (choice
  1040. (const find-file)
  1041. (const find-file-other-window)
  1042. (const find-file-other-frame)))))
  1043. (defcustom org-display-internal-link-with-indirect-buffer nil
  1044. "Non-nil means, use indirect buffer to display infile links.
  1045. Activating internal links (from one location in a file to another location
  1046. in the same file) normally just jumps to the location. When the link is
  1047. activated with a C-u prefix (or with mouse-3), the link is displayed in
  1048. another window. When this option is set, the other window actually displays
  1049. an indirect buffer clone of the current buffer, to avoid any visibility
  1050. changes to the current buffer."
  1051. :group 'org-link-follow
  1052. :type 'boolean)
  1053. (defcustom org-open-non-existing-files nil
  1054. "Non-nil means, `org-open-file' will open non-existing files.
  1055. When nil, an error will be generated."
  1056. :group 'org-link-follow
  1057. :type 'boolean)
  1058. (defcustom org-open-directory-means-index-dot-org nil
  1059. "Non-nil means, a link to a directory really means to index.org.
  1060. When nil, following a directory link will run dired or open a finder/explorer
  1061. window on that directory."
  1062. :group 'org-link-follow
  1063. :type 'boolean)
  1064. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  1065. "Function and arguments to call for following mailto links.
  1066. This is a list with the first element being a lisp function, and the
  1067. remaining elements being arguments to the function. In string arguments,
  1068. %a will be replaced by the address, and %s will be replaced by the subject
  1069. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  1070. :group 'org-link-follow
  1071. :type '(choice
  1072. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  1073. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1074. (const :tag "message-mail" (message-mail "%a" "%s"))
  1075. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1076. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1077. "Non-nil means, ask for confirmation before executing shell links.
  1078. Shell links can be dangerous: just think about a link
  1079. [[shell:rm -rf ~/*][Google Search]]
  1080. This link would show up in your Org-mode document as \"Google Search\",
  1081. but really it would remove your entire home directory.
  1082. Therefore we advise against setting this variable to nil.
  1083. Just change it to `y-or-n-p' of you want to confirm with a
  1084. single keystroke rather than having to type \"yes\"."
  1085. :group 'org-link-follow
  1086. :type '(choice
  1087. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1088. (const :tag "with y-or-n (faster)" y-or-n-p)
  1089. (const :tag "no confirmation (dangerous)" nil)))
  1090. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1091. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1092. Elisp links can be dangerous: just think about a link
  1093. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1094. This link would show up in your Org-mode document as \"Google Search\",
  1095. but really it would remove your entire home directory.
  1096. Therefore we advise against setting this variable to nil.
  1097. Just change it to `y-or-n-p' of you want to confirm with a
  1098. single keystroke rather than having to type \"yes\"."
  1099. :group 'org-link-follow
  1100. :type '(choice
  1101. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1102. (const :tag "with y-or-n (faster)" y-or-n-p)
  1103. (const :tag "no confirmation (dangerous)" nil)))
  1104. (defconst org-file-apps-defaults-gnu
  1105. '((remote . emacs)
  1106. (system . mailcap)
  1107. (t . mailcap))
  1108. "Default file applications on a UNIX or GNU/Linux system.
  1109. See `org-file-apps'.")
  1110. (defconst org-file-apps-defaults-macosx
  1111. '((remote . emacs)
  1112. (t . "open %s")
  1113. (system . "open %s")
  1114. ("ps.gz" . "gv %s")
  1115. ("eps.gz" . "gv %s")
  1116. ("dvi" . "xdvi %s")
  1117. ("fig" . "xfig %s"))
  1118. "Default file applications on a MacOS X system.
  1119. The system \"open\" is known as a default, but we use X11 applications
  1120. for some files for which the OS does not have a good default.
  1121. See `org-file-apps'.")
  1122. (defconst org-file-apps-defaults-windowsnt
  1123. (list
  1124. '(remote . emacs)
  1125. (cons t
  1126. (list (if (featurep 'xemacs)
  1127. 'mswindows-shell-execute
  1128. 'w32-shell-execute)
  1129. "open" 'file))
  1130. (cons 'system
  1131. (list (if (featurep 'xemacs)
  1132. 'mswindows-shell-execute
  1133. 'w32-shell-execute)
  1134. "open" 'file)))
  1135. "Default file applications on a Windows NT system.
  1136. The system \"open\" is used for most files.
  1137. See `org-file-apps'.")
  1138. (defcustom org-file-apps
  1139. '(
  1140. (auto-mode . emacs)
  1141. ("\\.x?html?\\'" . default)
  1142. ("\\.pdf\\'" . default)
  1143. )
  1144. "External applications for opening `file:path' items in a document.
  1145. Org-mode uses system defaults for different file types, but
  1146. you can use this variable to set the application for a given file
  1147. extension. The entries in this list are cons cells where the car identifies
  1148. files and the cdr the corresponding command. Possible values for the
  1149. file identifier are
  1150. \"regex\" Regular expression matched against the file name. For backward
  1151. compatibility, this can also be a string with only alphanumeric
  1152. characters, which is then interpreted as an extension.
  1153. `directory' Matches a directory
  1154. `remote' Matches a remote file, accessible through tramp or efs.
  1155. Remote files most likely should be visited through Emacs
  1156. because external applications cannot handle such paths.
  1157. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1158. so all files Emacs knows how to handle. Using this with
  1159. command `emacs' will open most files in Emacs. Beware that this
  1160. will also open html files inside Emacs, unless you add
  1161. (\"html\" . default) to the list as well.
  1162. t Default for files not matched by any of the other options.
  1163. `system' The system command to open files, like `open' on Windows
  1164. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1165. that will be selected if you call `C-c C-o' with a double
  1166. `C-u C-u' prefix.
  1167. Possible values for the command are:
  1168. `emacs' The file will be visited by the current Emacs process.
  1169. `default' Use the default application for this file type, which is the
  1170. association for t in the list, most likely in the system-specific
  1171. part.
  1172. This can be used to overrule an unwanted setting in the
  1173. system-specific variable.
  1174. `system' Use the system command for opening files, like \"open\".
  1175. This command is specified by the entry whose car is `system'.
  1176. Most likely, the system-specific version of this variable
  1177. does define this command, but you can overrule/replace it
  1178. here.
  1179. string A command to be executed by a shell; %s will be replaced
  1180. by the path to the file.
  1181. sexp A Lisp form which will be evaluated. The file path will
  1182. be available in the Lisp variable `file'.
  1183. For more examples, see the system specific constants
  1184. `org-file-apps-defaults-macosx'
  1185. `org-file-apps-defaults-windowsnt'
  1186. `org-file-apps-defaults-gnu'."
  1187. :group 'org-link-follow
  1188. :type '(repeat
  1189. (cons (choice :value ""
  1190. (string :tag "Extension")
  1191. (const :tag "System command to open files" system)
  1192. (const :tag "Default for unrecognized files" t)
  1193. (const :tag "Remote file" remote)
  1194. (const :tag "Links to a directory" directory)
  1195. (const :tag "Any files that have Emacs modes"
  1196. auto-mode))
  1197. (choice :value ""
  1198. (const :tag "Visit with Emacs" emacs)
  1199. (const :tag "Use default" default)
  1200. (const :tag "Use the system command" system)
  1201. (string :tag "Command")
  1202. (sexp :tag "Lisp form")))))
  1203. (defgroup org-refile nil
  1204. "Options concerning refiling entries in Org-mode."
  1205. :tag "Org Refile"
  1206. :group 'org)
  1207. (defcustom org-directory "~/org"
  1208. "Directory with org files.
  1209. This is just a default location to look for Org files. There is no need
  1210. at all to put your files into this directory. It is only used in the
  1211. following situations:
  1212. 1. When a remember template specifies a target file that is not an
  1213. absolute path. The path will then be interpreted relative to
  1214. `org-directory'
  1215. 2. When a remember note is filed away in an interactive way (when exiting the
  1216. note buffer with `C-1 C-c C-c'. The the user is prompted for an org file,
  1217. with `org-directory' as the default path."
  1218. :group 'org-refile
  1219. :group 'org-remember
  1220. :type 'directory)
  1221. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1222. "Default target for storing notes.
  1223. Used by the hooks for remember.el. This can be a string, or nil to mean
  1224. the value of `remember-data-file'.
  1225. You can set this on a per-template basis with the variable
  1226. `org-remember-templates'."
  1227. :group 'org-refile
  1228. :group 'org-remember
  1229. :type '(choice
  1230. (const :tag "Default from remember-data-file" nil)
  1231. file))
  1232. (defcustom org-goto-interface 'outline
  1233. "The default interface to be used for `org-goto'.
  1234. Allowed values are:
  1235. outline The interface shows an outline of the relevant file
  1236. and the correct heading is found by moving through
  1237. the outline or by searching with incremental search.
  1238. outline-path-completion Headlines in the current buffer are offered via
  1239. completion. This is the interface also used by
  1240. the refile command."
  1241. :group 'org-refile
  1242. :type '(choice
  1243. (const :tag "Outline" outline)
  1244. (const :tag "Outline-path-completion" outline-path-completion)))
  1245. (defcustom org-goto-max-level 5
  1246. "Maximum level to be considered when running org-goto with refile interface."
  1247. :group 'org-refile
  1248. :type 'number)
  1249. (defcustom org-reverse-note-order nil
  1250. "Non-nil means, store new notes at the beginning of a file or entry.
  1251. When nil, new notes will be filed to the end of a file or entry.
  1252. This can also be a list with cons cells of regular expressions that
  1253. are matched against file names, and values."
  1254. :group 'org-remember
  1255. :group 'org-refile
  1256. :type '(choice
  1257. (const :tag "Reverse always" t)
  1258. (const :tag "Reverse never" nil)
  1259. (repeat :tag "By file name regexp"
  1260. (cons regexp boolean))))
  1261. (defcustom org-refile-targets nil
  1262. "Targets for refiling entries with \\[org-refile].
  1263. This is list of cons cells. Each cell contains:
  1264. - a specification of the files to be considered, either a list of files,
  1265. or a symbol whose function or variable value will be used to retrieve
  1266. a file name or a list of file names. If you use `org-agenda-files' for
  1267. that, all agenda files will be scanned for targets. Nil means, consider
  1268. headings in the current buffer.
  1269. - A specification of how to find candidate refile targets. This may be
  1270. any of:
  1271. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1272. This tag has to be present in all target headlines, inheritance will
  1273. not be considered.
  1274. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1275. todo keyword.
  1276. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1277. headlines that are refiling targets.
  1278. - a cons cell (:level . N). Any headline of level N is considered a target.
  1279. Note that, when `org-odd-levels-only' is set, level corresponds to
  1280. order in hierarchy, not to the number of stars.
  1281. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1282. Note that, when `org-odd-levels-only' is set, level corresponds to
  1283. order in hierarchy, not to the number of stars.
  1284. When this variable is nil, all top-level headlines in the current buffer
  1285. are used, equivalent to the value `((nil . (:level . 1))'."
  1286. :group 'org-refile
  1287. :type '(repeat
  1288. (cons
  1289. (choice :value org-agenda-files
  1290. (const :tag "All agenda files" org-agenda-files)
  1291. (const :tag "Current buffer" nil)
  1292. (function) (variable) (file))
  1293. (choice :tag "Identify target headline by"
  1294. (cons :tag "Specific tag" (const :value :tag) (string))
  1295. (cons :tag "TODO keyword" (const :value :todo) (string))
  1296. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1297. (cons :tag "Level number" (const :value :level) (integer))
  1298. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1299. (defcustom org-refile-use-outline-path nil
  1300. "Non-nil means, provide refile targets as paths.
  1301. So a level 3 headline will be available as level1/level2/level3.
  1302. When the value is `file', also include the file name (without directory)
  1303. into the path. When `full-file-path', include the full file path."
  1304. :group 'org-refile
  1305. :type '(choice
  1306. (const :tag "Not" nil)
  1307. (const :tag "Yes" t)
  1308. (const :tag "Start with file name" file)
  1309. (const :tag "Start with full file path" full-file-path)))
  1310. (defcustom org-outline-path-complete-in-steps t
  1311. "Non-nil means, complete the outline path in hierarchical steps.
  1312. When Org-mode uses the refile interface to select an outline path
  1313. \(see variable `org-refile-use-outline-path'), the completion of
  1314. the path can be done is a single go, or if can be done in steps down
  1315. the headline hierarchy. Going in steps is probably the best if you
  1316. do not use a special completion package like `ido' or `icicles'.
  1317. However, when using these packages, going in one step can be very
  1318. fast, while still showing the whole path to the entry."
  1319. :group 'org-refile
  1320. :type 'boolean)
  1321. (defgroup org-todo nil
  1322. "Options concerning TODO items in Org-mode."
  1323. :tag "Org TODO"
  1324. :group 'org)
  1325. (defgroup org-progress nil
  1326. "Options concerning Progress logging in Org-mode."
  1327. :tag "Org Progress"
  1328. :group 'org-time)
  1329. (defvar org-todo-interpretation-widgets
  1330. '(
  1331. (:tag "Sequence (cycling hits every state)" sequence)
  1332. (:tag "Type (cycling directly to DONE)" type))
  1333. "The available interpretation symbols for customizing
  1334. `org-todo-keywords'.
  1335. Interested libraries should add to this list.")
  1336. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1337. "List of TODO entry keyword sequences and their interpretation.
  1338. \\<org-mode-map>This is a list of sequences.
  1339. Each sequence starts with a symbol, either `sequence' or `type',
  1340. indicating if the keywords should be interpreted as a sequence of
  1341. action steps, or as different types of TODO items. The first
  1342. keywords are states requiring action - these states will select a headline
  1343. for inclusion into the global TODO list Org-mode produces. If one of
  1344. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1345. signify that no further action is necessary. If \"|\" is not found,
  1346. the last keyword is treated as the only DONE state of the sequence.
  1347. The command \\[org-todo] cycles an entry through these states, and one
  1348. additional state where no keyword is present. For details about this
  1349. cycling, see the manual.
  1350. TODO keywords and interpretation can also be set on a per-file basis with
  1351. the special #+SEQ_TODO and #+TYP_TODO lines.
  1352. Each keyword can optionally specify a character for fast state selection
  1353. \(in combination with the variable `org-use-fast-todo-selection')
  1354. and specifiers for state change logging, using the same syntax
  1355. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1356. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1357. indicates to record a time stamp each time this state is selected.
  1358. Each keyword may also specify if a timestamp or a note should be
  1359. recorded when entering or leaving the state, by adding additional
  1360. characters in the parenthesis after the keyword. This looks like this:
  1361. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1362. record only the time of the state change. With X and Y being either
  1363. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1364. Y when leaving the state if and only if the *target* state does not
  1365. define X. You may omit any of the fast-selection key or X or /Y,
  1366. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1367. For backward compatibility, this variable may also be just a list
  1368. of keywords - in this case the interpretation (sequence or type) will be
  1369. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1370. :group 'org-todo
  1371. :group 'org-keywords
  1372. :type '(choice
  1373. (repeat :tag "Old syntax, just keywords"
  1374. (string :tag "Keyword"))
  1375. (repeat :tag "New syntax"
  1376. (cons
  1377. (choice
  1378. :tag "Interpretation"
  1379. ;;Quick and dirty way to see
  1380. ;;`org-todo-interpretations'. This takes the
  1381. ;;place of item arguments
  1382. :convert-widget
  1383. (lambda (widget)
  1384. (widget-put widget
  1385. :args (mapcar
  1386. #'(lambda (x)
  1387. (widget-convert
  1388. (cons 'const x)))
  1389. org-todo-interpretation-widgets))
  1390. widget))
  1391. (repeat
  1392. (string :tag "Keyword"))))))
  1393. (defvar org-todo-keywords-1 nil
  1394. "All TODO and DONE keywords active in a buffer.")
  1395. (make-variable-buffer-local 'org-todo-keywords-1)
  1396. (defvar org-todo-keywords-for-agenda nil)
  1397. (defvar org-done-keywords-for-agenda nil)
  1398. (defvar org-todo-keyword-alist-for-agenda nil)
  1399. (defvar org-tag-alist-for-agenda nil)
  1400. (defvar org-agenda-contributing-files nil)
  1401. (defvar org-not-done-keywords nil)
  1402. (make-variable-buffer-local 'org-not-done-keywords)
  1403. (defvar org-done-keywords nil)
  1404. (make-variable-buffer-local 'org-done-keywords)
  1405. (defvar org-todo-heads nil)
  1406. (make-variable-buffer-local 'org-todo-heads)
  1407. (defvar org-todo-sets nil)
  1408. (make-variable-buffer-local 'org-todo-sets)
  1409. (defvar org-todo-log-states nil)
  1410. (make-variable-buffer-local 'org-todo-log-states)
  1411. (defvar org-todo-kwd-alist nil)
  1412. (make-variable-buffer-local 'org-todo-kwd-alist)
  1413. (defvar org-todo-key-alist nil)
  1414. (make-variable-buffer-local 'org-todo-key-alist)
  1415. (defvar org-todo-key-trigger nil)
  1416. (make-variable-buffer-local 'org-todo-key-trigger)
  1417. (defcustom org-todo-interpretation 'sequence
  1418. "Controls how TODO keywords are interpreted.
  1419. This variable is in principle obsolete and is only used for
  1420. backward compatibility, if the interpretation of todo keywords is
  1421. not given already in `org-todo-keywords'. See that variable for
  1422. more information."
  1423. :group 'org-todo
  1424. :group 'org-keywords
  1425. :type '(choice (const sequence)
  1426. (const type)))
  1427. (defcustom org-use-fast-todo-selection t
  1428. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1429. This variable describes if and under what circumstances the cycling
  1430. mechanism for TODO keywords will be replaced by a single-key, direct
  1431. selection scheme.
  1432. When nil, fast selection is never used.
  1433. When the symbol `prefix', it will be used when `org-todo' is called with
  1434. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1435. in an agenda buffer.
  1436. When t, fast selection is used by default. In this case, the prefix
  1437. argument forces cycling instead.
  1438. In all cases, the special interface is only used if access keys have actually
  1439. been assigned by the user, i.e. if keywords in the configuration are followed
  1440. by a letter in parenthesis, like TODO(t)."
  1441. :group 'org-todo
  1442. :type '(choice
  1443. (const :tag "Never" nil)
  1444. (const :tag "By default" t)
  1445. (const :tag "Only with C-u C-c C-t" prefix)))
  1446. (defcustom org-provide-todo-statistics t
  1447. "Non-nil means, update todo statistics after insert and toggle.
  1448. When this is set, todo statistics is updated in the parent of the current
  1449. entry each time a todo state is changed."
  1450. :group 'org-todo
  1451. :type 'boolean)
  1452. (defcustom org-after-todo-state-change-hook nil
  1453. "Hook which is run after the state of a TODO item was changed.
  1454. The new state (a string with a TODO keyword, or nil) is available in the
  1455. Lisp variable `state'."
  1456. :group 'org-todo
  1457. :type 'hook)
  1458. (defvar org-blocker-hook nil
  1459. "Hook for functions that are allowed to block a state change.
  1460. Each function gets as its single argument a property list, see
  1461. `org-trigger-hook' for more information about this list.
  1462. If any of the functions in this hook returns nil, the state change
  1463. is blocked.")
  1464. (defvar org-trigger-hook nil
  1465. "Hook for functions that are triggered by a state change.
  1466. Each function gets as its single argument a property list with at least
  1467. the following elements:
  1468. (:type type-of-change :position pos-at-entry-start
  1469. :from old-state :to new-state)
  1470. Depending on the type, more properties may be present.
  1471. This mechanism is currently implemented for:
  1472. TODO state changes
  1473. ------------------
  1474. :type todo-state-change
  1475. :from previous state (keyword as a string), or nil, or a symbol
  1476. 'todo' or 'done', to indicate the general type of state.
  1477. :to new state, like in :from")
  1478. (defcustom org-enforce-todo-dependencies nil
  1479. "Non-nil means, undone TODO entries will block switching the parent to DONE.
  1480. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  1481. be blocked if any prior sibling is not yet done.
  1482. This variable needs to be set before org.el is loaded, and you need to
  1483. restart Emacs after a change to make the change effective. The only way
  1484. to change is while Emacs is running is through the customize interface."
  1485. :set (lambda (var val)
  1486. (set var val)
  1487. (if val
  1488. (add-hook 'org-blocker-hook
  1489. 'org-block-todo-from-children-or-siblings)
  1490. (remove-hook 'org-blocker-hook
  1491. 'org-block-todo-from-children-or-siblings)))
  1492. :group 'org-todo
  1493. :type 'boolean)
  1494. (defcustom org-enforce-todo-checkbox-dependencies nil
  1495. "Non-nil means, unchecked boxes will block switching the parent to DONE.
  1496. When this is nil, checkboxes have no influence on switching TODO states.
  1497. When non-nil, you first need to check off all check boxes before the TODO
  1498. entry can be switched to DONE.
  1499. This variable needs to be set before org.el is loaded, and you need to
  1500. restart Emacs after a change to make the change effective. The only way
  1501. to change is while Emacs is running is through the customize interface."
  1502. :set (lambda (var val)
  1503. (set var val)
  1504. (if val
  1505. (add-hook 'org-blocker-hook
  1506. 'org-block-todo-from-checkboxes)
  1507. (remove-hook 'org-blocker-hook
  1508. 'org-block-todo-from-checkboxes)))
  1509. :group 'org-todo
  1510. :type 'boolean)
  1511. (defcustom org-todo-state-tags-triggers nil
  1512. "Tag changes that should be triggered by TODO state changes.
  1513. This is a list. Each entry is
  1514. (state-change (tag . flag) .......)
  1515. State-change can be a string with a state, and empty string to indicate the
  1516. state that has no TODO keyword, or it can be one of the symbols `todo'
  1517. or `done', meaning any not-done or done state, respectively."
  1518. :group 'org-todo
  1519. :group 'org-tags
  1520. :type '(repeat
  1521. (cons (choice :tag "When changing to"
  1522. (const :tag "Not-done state" todo)
  1523. (const :tag "Done state" done)
  1524. (string :tag "State"))
  1525. (repeat
  1526. (cons :tag "Tag action"
  1527. (string :tag "Tag")
  1528. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1529. (defcustom org-log-done nil
  1530. "Information to record when a task moves to the DONE state.
  1531. Possible values are:
  1532. nil Don't add anything, just change the keyword
  1533. time Add a time stamp to the task
  1534. note Prompt a closing note and add it with template `org-log-note-headings'
  1535. This option can also be set with on a per-file-basis with
  1536. #+STARTUP: nologdone
  1537. #+STARTUP: logdone
  1538. #+STARTUP: lognotedone
  1539. You can have local logging settings for a subtree by setting the LOGGING
  1540. property to one or more of these keywords."
  1541. :group 'org-todo
  1542. :group 'org-progress
  1543. :type '(choice
  1544. (const :tag "No logging" nil)
  1545. (const :tag "Record CLOSED timestamp" time)
  1546. (const :tag "Record CLOSED timestamp with closing note." note)))
  1547. ;; Normalize old uses of org-log-done.
  1548. (cond
  1549. ((eq org-log-done t) (setq org-log-done 'time))
  1550. ((and (listp org-log-done) (memq 'done org-log-done))
  1551. (setq org-log-done 'note)))
  1552. (defcustom org-log-note-clock-out nil
  1553. "Non-nil means, record a note when clocking out of an item.
  1554. This can also be configured on a per-file basis by adding one of
  1555. the following lines anywhere in the buffer:
  1556. #+STARTUP: lognoteclock-out
  1557. #+STARTUP: nolognoteclock-out"
  1558. :group 'org-todo
  1559. :group 'org-progress
  1560. :type 'boolean)
  1561. (defcustom org-log-done-with-time t
  1562. "Non-nil means, the CLOSED time stamp will contain date and time.
  1563. When nil, only the date will be recorded."
  1564. :group 'org-progress
  1565. :type 'boolean)
  1566. (defcustom org-log-note-headings
  1567. '((done . "CLOSING NOTE %t")
  1568. (state . "State %-12s from %-12S %t")
  1569. (note . "Note taken on %t")
  1570. (clock-out . ""))
  1571. "Headings for notes added to entries.
  1572. The value is an alist, with the car being a symbol indicating the note
  1573. context, and the cdr is the heading to be used. The heading may also be the
  1574. empty string.
  1575. %t in the heading will be replaced by a time stamp.
  1576. %s will be replaced by the new TODO state, in double quotes.
  1577. %S will be replaced by the old TODO state, in double quotes.
  1578. %u will be replaced by the user name.
  1579. %U will be replaced by the full user name."
  1580. :group 'org-todo
  1581. :group 'org-progress
  1582. :type '(list :greedy t
  1583. (cons (const :tag "Heading when closing an item" done) string)
  1584. (cons (const :tag
  1585. "Heading when changing todo state (todo sequence only)"
  1586. state) string)
  1587. (cons (const :tag "Heading when just taking a note" note) string)
  1588. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1589. (unless (assq 'note org-log-note-headings)
  1590. (push '(note . "%t") org-log-note-headings))
  1591. (defcustom org-log-into-drawer nil
  1592. "Non-nil means, insert state change notes and time stamps into a drawer.
  1593. When nil, state changes notes will be inserted after the headline and
  1594. any scheduling and clock lines, but not inside a drawer.
  1595. The value of this variable should be the name of the drawer to use.
  1596. LOGBOOK is proposed at the default drawer for this purpose, you can
  1597. also set this to a string to define the drawer of your choice.
  1598. A value of t is also allowed, representing \"LOGBOOK\".
  1599. If this variable is set, `org-log-state-notes-insert-after-drawers'
  1600. will be ognored."
  1601. :group 'org-todo
  1602. :group 'org-progress
  1603. :type '(choice
  1604. (const :tag "Not into a drawer" nil)
  1605. (const :tag "LOGBOOK" t)
  1606. (string :tag "Other")))
  1607. (if (fboundp 'defvaralias)
  1608. (defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer))
  1609. (defcustom org-log-state-notes-insert-after-drawers nil
  1610. "Non-nil means, insert state change notes after any drawers in entry.
  1611. Only the drawers that *immediately* follow the headline and the
  1612. deadline/scheduled line are skipped.
  1613. When nil, insert notes right after the heading and perhaps the line
  1614. with deadline/scheduling if present.
  1615. This variable will have no effect if `org-log-into-drawer' is
  1616. set."
  1617. :group 'org-todo
  1618. :group 'org-progress
  1619. :type 'boolean)
  1620. (defcustom org-log-states-order-reversed t
  1621. "Non-nil means, the latest state change note will be directly after heading.
  1622. When nil, the notes will be orderer according to time."
  1623. :group 'org-todo
  1624. :group 'org-progress
  1625. :type 'boolean)
  1626. (defcustom org-log-repeat 'time
  1627. "Non-nil means, record moving through the DONE state when triggering repeat.
  1628. An auto-repeating tasks is immediately switched back to TODO when marked
  1629. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1630. the TODO keyword definition, or recording a closing note by setting
  1631. `org-log-done', there will be no record of the task moving through DONE.
  1632. This variable forces taking a note anyway. Possible values are:
  1633. nil Don't force a record
  1634. time Record a time stamp
  1635. note Record a note
  1636. This option can also be set with on a per-file-basis with
  1637. #+STARTUP: logrepeat
  1638. #+STARTUP: lognoterepeat
  1639. #+STARTUP: nologrepeat
  1640. You can have local logging settings for a subtree by setting the LOGGING
  1641. property to one or more of these keywords."
  1642. :group 'org-todo
  1643. :group 'org-progress
  1644. :type '(choice
  1645. (const :tag "Don't force a record" nil)
  1646. (const :tag "Force recording the DONE state" time)
  1647. (const :tag "Force recording a note with the DONE state" note)))
  1648. (defgroup org-priorities nil
  1649. "Priorities in Org-mode."
  1650. :tag "Org Priorities"
  1651. :group 'org-todo)
  1652. (defcustom org-highest-priority ?A
  1653. "The highest priority of TODO items. A character like ?A, ?B etc.
  1654. Must have a smaller ASCII number than `org-lowest-priority'."
  1655. :group 'org-priorities
  1656. :type 'character)
  1657. (defcustom org-lowest-priority ?C
  1658. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1659. Must have a larger ASCII number than `org-highest-priority'."
  1660. :group 'org-priorities
  1661. :type 'character)
  1662. (defcustom org-default-priority ?B
  1663. "The default priority of TODO items.
  1664. This is the priority an item get if no explicit priority is given."
  1665. :group 'org-priorities
  1666. :type 'character)
  1667. (defcustom org-priority-start-cycle-with-default t
  1668. "Non-nil means, start with default priority when starting to cycle.
  1669. When this is nil, the first step in the cycle will be (depending on the
  1670. command used) one higher or lower that the default priority."
  1671. :group 'org-priorities
  1672. :type 'boolean)
  1673. (defgroup org-time nil
  1674. "Options concerning time stamps and deadlines in Org-mode."
  1675. :tag "Org Time"
  1676. :group 'org)
  1677. (defcustom org-insert-labeled-timestamps-at-point nil
  1678. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1679. When nil, these labeled time stamps are forces into the second line of an
  1680. entry, just after the headline. When scheduling from the global TODO list,
  1681. the time stamp will always be forced into the second line."
  1682. :group 'org-time
  1683. :type 'boolean)
  1684. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1685. "Formats for `format-time-string' which are used for time stamps.
  1686. It is not recommended to change this constant.")
  1687. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1688. "Number of minutes to round time stamps to.
  1689. These are two values, the first applies when first creating a time stamp.
  1690. The second applies when changing it with the commands `S-up' and `S-down'.
  1691. When changing the time stamp, this means that it will change in steps
  1692. of N minutes, as given by the second value.
  1693. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1694. numbers should be factors of 60, so for example 5, 10, 15.
  1695. When this is larger than 1, you can still force an exact time-stamp by using
  1696. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1697. and by using a prefix arg to `S-up/down' to specify the exact number
  1698. of minutes to shift."
  1699. :group 'org-time
  1700. :get '(lambda (var) ; Make sure all entries have 5 elements
  1701. (if (integerp (default-value var))
  1702. (list (default-value var) 5)
  1703. (default-value var)))
  1704. :type '(list
  1705. (integer :tag "when inserting times")
  1706. (integer :tag "when modifying times")))
  1707. ;; Normalize old customizations of this variable.
  1708. (when (integerp org-time-stamp-rounding-minutes)
  1709. (setq org-time-stamp-rounding-minutes
  1710. (list org-time-stamp-rounding-minutes
  1711. org-time-stamp-rounding-minutes)))
  1712. (defcustom org-display-custom-times nil
  1713. "Non-nil means, overlay custom formats over all time stamps.
  1714. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1715. To turn this on on a per-file basis, insert anywhere in the file:
  1716. #+STARTUP: customtime"
  1717. :group 'org-time
  1718. :set 'set-default
  1719. :type 'sexp)
  1720. (make-variable-buffer-local 'org-display-custom-times)
  1721. (defcustom org-time-stamp-custom-formats
  1722. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1723. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1724. These are overlayed over the default ISO format if the variable
  1725. `org-display-custom-times' is set. Time like %H:%M should be at the
  1726. end of the second format."
  1727. :group 'org-time
  1728. :type 'sexp)
  1729. (defun org-time-stamp-format (&optional long inactive)
  1730. "Get the right format for a time string."
  1731. (let ((f (if long (cdr org-time-stamp-formats)
  1732. (car org-time-stamp-formats))))
  1733. (if inactive
  1734. (concat "[" (substring f 1 -1) "]")
  1735. f)))
  1736. (defcustom org-time-clocksum-format "%d:%02d"
  1737. "The format string used when creating CLOCKSUM lines, or when
  1738. org-mode generates a time duration."
  1739. :group 'org-time
  1740. :type 'string)
  1741. (defcustom org-deadline-warning-days 14
  1742. "No. of days before expiration during which a deadline becomes active.
  1743. This variable governs the display in sparse trees and in the agenda.
  1744. When 0 or negative, it means use this number (the absolute value of it)
  1745. even if a deadline has a different individual lead time specified."
  1746. :group 'org-time
  1747. :group 'org-agenda-daily/weekly
  1748. :type 'number)
  1749. (defcustom org-read-date-prefer-future t
  1750. "Non-nil means, assume future for incomplete date input from user.
  1751. This affects the following situations:
  1752. 1. The user gives a day, but no month.
  1753. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1754. read this as the third of *next* month. However, if you enter \"17\",
  1755. it will be considered as *this* month.
  1756. 2. The user gives a month but not a year.
  1757. For example, if it is april and you enter \"feb 2\", this will be read
  1758. as feb 2, *next* year. \"May 5\", however, will be this year.
  1759. Currently this does not work for ISO week specifications.
  1760. When this option is nil, the current month and year will always be used
  1761. as defaults."
  1762. :group 'org-time
  1763. :type 'boolean)
  1764. (defcustom org-read-date-display-live t
  1765. "Non-nil means, display current interpretation of date prompt live.
  1766. This display will be in an overlay, in the minibuffer."
  1767. :group 'org-time
  1768. :type 'boolean)
  1769. (defcustom org-read-date-popup-calendar t
  1770. "Non-nil means, pop up a calendar when prompting for a date.
  1771. In the calendar, the date can be selected with mouse-1. However, the
  1772. minibuffer will also be active, and you can simply enter the date as well.
  1773. When nil, only the minibuffer will be available."
  1774. :group 'org-time
  1775. :type 'boolean)
  1776. (if (fboundp 'defvaralias)
  1777. (defvaralias 'org-popup-calendar-for-date-prompt
  1778. 'org-read-date-popup-calendar))
  1779. (defcustom org-extend-today-until 0
  1780. "The hour when your day really ends. Must be an integer.
  1781. This has influence for the following applications:
  1782. - When switching the agenda to \"today\". It it is still earlier than
  1783. the time given here, the day recognized as TODAY is actually yesterday.
  1784. - When a date is read from the user and it is still before the time given
  1785. here, the current date and time will be assumed to be yesterday, 23:59.
  1786. Also, timestamps inserted in remember templates follow this rule.
  1787. IMPORTANT: This is a feature whose implementation is and likely will
  1788. remain incomplete. Really, it is only here because past midnight seems to
  1789. be the favorite working time of John Wiegley :-)"
  1790. :group 'org-time
  1791. :type 'number)
  1792. (defcustom org-edit-timestamp-down-means-later nil
  1793. "Non-nil means, S-down will increase the time in a time stamp.
  1794. When nil, S-up will increase."
  1795. :group 'org-time
  1796. :type 'boolean)
  1797. (defcustom org-calendar-follow-timestamp-change t
  1798. "Non-nil means, make the calendar window follow timestamp changes.
  1799. When a timestamp is modified and the calendar window is visible, it will be
  1800. moved to the new date."
  1801. :group 'org-time
  1802. :type 'boolean)
  1803. (defgroup org-tags nil
  1804. "Options concerning tags in Org-mode."
  1805. :tag "Org Tags"
  1806. :group 'org)
  1807. (defcustom org-tag-alist nil
  1808. "List of tags allowed in Org-mode files.
  1809. When this list is nil, Org-mode will base TAG input on what is already in the
  1810. buffer.
  1811. The value of this variable is an alist, the car of each entry must be a
  1812. keyword as a string, the cdr may be a character that is used to select
  1813. that tag through the fast-tag-selection interface.
  1814. See the manual for details."
  1815. :group 'org-tags
  1816. :type '(repeat
  1817. (choice
  1818. (cons (string :tag "Tag name")
  1819. (character :tag "Access char"))
  1820. (const :tag "Start radio group" (:startgroup))
  1821. (const :tag "End radio group" (:endgroup))
  1822. (const :tag "New line" (:newline)))))
  1823. (defvar org-file-tags nil
  1824. "List of tags that can be inherited by all entries in the file.
  1825. The tags will be inherited if the variable `org-use-tag-inheritance'
  1826. says they should be.
  1827. This variable is populated from #+TAG lines.")
  1828. (defcustom org-use-fast-tag-selection 'auto
  1829. "Non-nil means, use fast tag selection scheme.
  1830. This is a special interface to select and deselect tags with single keys.
  1831. When nil, fast selection is never used.
  1832. When the symbol `auto', fast selection is used if and only if selection
  1833. characters for tags have been configured, either through the variable
  1834. `org-tag-alist' or through a #+TAGS line in the buffer.
  1835. When t, fast selection is always used and selection keys are assigned
  1836. automatically if necessary."
  1837. :group 'org-tags
  1838. :type '(choice
  1839. (const :tag "Always" t)
  1840. (const :tag "Never" nil)
  1841. (const :tag "When selection characters are configured" 'auto)))
  1842. (defcustom org-fast-tag-selection-single-key nil
  1843. "Non-nil means, fast tag selection exits after first change.
  1844. When nil, you have to press RET to exit it.
  1845. During fast tag selection, you can toggle this flag with `C-c'.
  1846. This variable can also have the value `expert'. In this case, the window
  1847. displaying the tags menu is not even shown, until you press C-c again."
  1848. :group 'org-tags
  1849. :type '(choice
  1850. (const :tag "No" nil)
  1851. (const :tag "Yes" t)
  1852. (const :tag "Expert" expert)))
  1853. (defvar org-fast-tag-selection-include-todo nil
  1854. "Non-nil means, fast tags selection interface will also offer TODO states.
  1855. This is an undocumented feature, you should not rely on it.")
  1856. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  1857. "The column to which tags should be indented in a headline.
  1858. If this number is positive, it specifies the column. If it is negative,
  1859. it means that the tags should be flushright to that column. For example,
  1860. -80 works well for a normal 80 character screen."
  1861. :group 'org-tags
  1862. :type 'integer)
  1863. (defcustom org-auto-align-tags t
  1864. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1865. These operations change the length of a headline and therefore shift
  1866. the tags around. With this options turned on, after each such operation
  1867. the tags are again aligned to `org-tags-column'."
  1868. :group 'org-tags
  1869. :type 'boolean)
  1870. (defcustom org-use-tag-inheritance t
  1871. "Non-nil means, tags in levels apply also for sublevels.
  1872. When nil, only the tags directly given in a specific line apply there.
  1873. This may also be a list of tags that should be inherited, or a regexp that
  1874. matches tags that should be inherited. Additional control is possible
  1875. with the variable `org-tags-exclude-from-inheritance' which gives an
  1876. explicit list of tags to be excluded from inheritance., even if the value of
  1877. `org-use-tag-inheritance' would select it for inheritance.
  1878. If this option is t, a match early-on in a tree can lead to a large
  1879. number of matches in the subtree when constructing the agenda or creating
  1880. a sparse tree. If you only want to see the first match in a tree during
  1881. a search, check out the variable `org-tags-match-list-sublevels'."
  1882. :group 'org-tags
  1883. :type '(choice
  1884. (const :tag "Not" nil)
  1885. (const :tag "Always" t)
  1886. (repeat :tag "Specific tags" (string :tag "Tag"))
  1887. (regexp :tag "Tags matched by regexp")))
  1888. (defcustom org-tags-exclude-from-inheritance nil
  1889. "List of tags that should never be inherited.
  1890. This is a way to exclude a few tags from inheritance. For way to do
  1891. the opposite, to actively allow inheritance for selected tags,
  1892. see the variable `org-use-tag-inheritance'."
  1893. :group 'org-tags
  1894. :type '(repeat (string :tag "Tag")))
  1895. (defun org-tag-inherit-p (tag)
  1896. "Check if TAG is one that should be inherited."
  1897. (cond
  1898. ((member tag org-tags-exclude-from-inheritance) nil)
  1899. ((eq org-use-tag-inheritance t) t)
  1900. ((not org-use-tag-inheritance) nil)
  1901. ((stringp org-use-tag-inheritance)
  1902. (string-match org-use-tag-inheritance tag))
  1903. ((listp org-use-tag-inheritance)
  1904. (member tag org-use-tag-inheritance))
  1905. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1906. (defcustom org-tags-match-list-sublevels t
  1907. "Non-nil means list also sublevels of headlines matching tag search.
  1908. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1909. the sublevels of a headline matching a tag search often also match
  1910. the same search. Listing all of them can create very long lists.
  1911. Setting this variable to nil causes subtrees of a match to be skipped.
  1912. This option is off by default, because inheritance in on. If you turn
  1913. inheritance off, you very likely want to turn this option on.
  1914. As a special case, if the tag search is restricted to TODO items, the
  1915. value of this variable is ignored and sublevels are always checked, to
  1916. make sure all corresponding TODO items find their way into the list.
  1917. This variable is semi-obsolete and probably should always be true. It
  1918. is better to limit inheritance to certain tags using the variables
  1919. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1920. :group 'org-tags
  1921. :type 'boolean)
  1922. (defvar org-tags-history nil
  1923. "History of minibuffer reads for tags.")
  1924. (defvar org-last-tags-completion-table nil
  1925. "The last used completion table for tags.")
  1926. (defvar org-after-tags-change-hook nil
  1927. "Hook that is run after the tags in a line have changed.")
  1928. (defgroup org-properties nil
  1929. "Options concerning properties in Org-mode."
  1930. :tag "Org Properties"
  1931. :group 'org)
  1932. (defcustom org-property-format "%-10s %s"
  1933. "How property key/value pairs should be formatted by `indent-line'.
  1934. When `indent-line' hits a property definition, it will format the line
  1935. according to this format, mainly to make sure that the values are
  1936. lined-up with respect to each other."
  1937. :group 'org-properties
  1938. :type 'string)
  1939. (defcustom org-use-property-inheritance nil
  1940. "Non-nil means, properties apply also for sublevels.
  1941. This setting is chiefly used during property searches. Turning it on can
  1942. cause significant overhead when doing a search, which is why it is not
  1943. on by default.
  1944. When nil, only the properties directly given in the current entry count.
  1945. When t, every property is inherited. The value may also be a list of
  1946. properties that should have inheritance, or a regular expression matching
  1947. properties that should be inherited.
  1948. However, note that some special properties use inheritance under special
  1949. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1950. and the properties ending in \"_ALL\" when they are used as descriptor
  1951. for valid values of a property.
  1952. Note for programmers:
  1953. When querying an entry with `org-entry-get', you can control if inheritance
  1954. should be used. By default, `org-entry-get' looks only at the local
  1955. properties. You can request inheritance by setting the inherit argument
  1956. to t (to force inheritance) or to `selective' (to respect the setting
  1957. in this variable)."
  1958. :group 'org-properties
  1959. :type '(choice
  1960. (const :tag "Not" nil)
  1961. (const :tag "Always" t)
  1962. (repeat :tag "Specific properties" (string :tag "Property"))
  1963. (regexp :tag "Properties matched by regexp")))
  1964. (defun org-property-inherit-p (property)
  1965. "Check if PROPERTY is one that should be inherited."
  1966. (cond
  1967. ((eq org-use-property-inheritance t) t)
  1968. ((not org-use-property-inheritance) nil)
  1969. ((stringp org-use-property-inheritance)
  1970. (string-match org-use-property-inheritance property))
  1971. ((listp org-use-property-inheritance)
  1972. (member property org-use-property-inheritance))
  1973. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1974. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1975. "The default column format, if no other format has been defined.
  1976. This variable can be set on the per-file basis by inserting a line
  1977. #+COLUMNS: %25ITEM ....."
  1978. :group 'org-properties
  1979. :type 'string)
  1980. (defcustom org-columns-ellipses ".."
  1981. "The ellipses to be used when a field in column view is truncated.
  1982. When this is the empty string, as many characters as possible are shown,
  1983. but then there will be no visual indication that the field has been truncated.
  1984. When this is a string of length N, the last N characters of a truncated
  1985. field are replaced by this string. If the column is narrower than the
  1986. ellipses string, only part of the ellipses string will be shown."
  1987. :group 'org-properties
  1988. :type 'string)
  1989. (defcustom org-columns-modify-value-for-display-function nil
  1990. "Function that modifies values for display in column view.
  1991. For example, it can be used to cut out a certain part from a time stamp.
  1992. The function must take 2 arguments:
  1993. column-title The title of the column (*not* the property name)
  1994. value The value that should be modified.
  1995. The function should return the value that should be displayed,
  1996. or nil if the normal value should be used."
  1997. :group 'org-properties
  1998. :type 'function)
  1999. (defcustom org-effort-property "Effort"
  2000. "The property that is being used to keep track of effort estimates.
  2001. Effort estimates given in this property need to have the format H:MM."
  2002. :group 'org-properties
  2003. :group 'org-progress
  2004. :type '(string :tag "Property"))
  2005. (defconst org-global-properties-fixed
  2006. '(("VISIBILITY_ALL" . "folded children content all"))
  2007. "List of property/value pairs that can be inherited by any entry.
  2008. These are fixed values, for the preset properties.")
  2009. (defcustom org-global-properties nil
  2010. "List of property/value pairs that can be inherited by any entry.
  2011. You can set buffer-local values for the same purpose in the variable
  2012. `org-file-properties' this by adding lines like
  2013. #+PROPERTY: NAME VALUE"
  2014. :group 'org-properties
  2015. :type '(repeat
  2016. (cons (string :tag "Property")
  2017. (string :tag "Value"))))
  2018. (defvar org-file-properties nil
  2019. "List of property/value pairs that can be inherited by any entry.
  2020. Valid for the current buffer.
  2021. This variable is populated from #+PROPERTY lines.")
  2022. (make-variable-buffer-local 'org-file-properties)
  2023. (defgroup org-agenda nil
  2024. "Options concerning agenda views in Org-mode."
  2025. :tag "Org Agenda"
  2026. :group 'org)
  2027. (defvar org-category nil
  2028. "Variable used by org files to set a category for agenda display.
  2029. Such files should use a file variable to set it, for example
  2030. # -*- mode: org; org-category: \"ELisp\"
  2031. or contain a special line
  2032. #+CATEGORY: ELisp
  2033. If the file does not specify a category, then file's base name
  2034. is used instead.")
  2035. (make-variable-buffer-local 'org-category)
  2036. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  2037. (defcustom org-agenda-files nil
  2038. "The files to be used for agenda display.
  2039. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  2040. \\[org-remove-file]. You can also use customize to edit the list.
  2041. If an entry is a directory, all files in that directory that are matched by
  2042. `org-agenda-file-regexp' will be part of the file list.
  2043. If the value of the variable is not a list but a single file name, then
  2044. the list of agenda files is actually stored and maintained in that file, one
  2045. agenda file per line."
  2046. :group 'org-agenda
  2047. :type '(choice
  2048. (repeat :tag "List of files and directories" file)
  2049. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  2050. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  2051. "Regular expression to match files for `org-agenda-files'.
  2052. If any element in the list in that variable contains a directory instead
  2053. of a normal file, all files in that directory that are matched by this
  2054. regular expression will be included."
  2055. :group 'org-agenda
  2056. :type 'regexp)
  2057. (defcustom org-agenda-text-search-extra-files nil
  2058. "List of extra files to be searched by text search commands.
  2059. These files will be search in addition to the agenda files by the
  2060. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  2061. Note that these files will only be searched for text search commands,
  2062. not for the other agenda views like todo lists, tag searches or the weekly
  2063. agenda. This variable is intended to list notes and possibly archive files
  2064. that should also be searched by these two commands.
  2065. In fact, if the first element in the list is the symbol `agenda-archives',
  2066. than all archive files of all agenda files will be added to the search
  2067. scope."
  2068. :group 'org-agenda
  2069. :type '(set :greedy t
  2070. (const :tag "Agenda Archives" agenda-archives)
  2071. (repeat :inline t (file))))
  2072. (if (fboundp 'defvaralias)
  2073. (defvaralias 'org-agenda-multi-occur-extra-files
  2074. 'org-agenda-text-search-extra-files))
  2075. (defcustom org-agenda-skip-unavailable-files nil
  2076. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  2077. A nil value means to remove them, after a query, from the list."
  2078. :group 'org-agenda
  2079. :type 'boolean)
  2080. (defcustom org-calendar-to-agenda-key [?c]
  2081. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  2082. The command `org-calendar-goto-agenda' will be bound to this key. The
  2083. default is the character `c' because then `c' can be used to switch back and
  2084. forth between agenda and calendar."
  2085. :group 'org-agenda
  2086. :type 'sexp)
  2087. (defcustom org-calendar-agenda-action-key [?k]
  2088. "The key to be installed in `calendar-mode-map' for agenda-action.
  2089. The command `org-agenda-action' will be bound to this key. The
  2090. default is the character `k' because we use the same key in the agenda."
  2091. :group 'org-agenda
  2092. :type 'sexp)
  2093. (eval-after-load "calendar"
  2094. '(progn
  2095. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  2096. 'org-calendar-goto-agenda)
  2097. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  2098. 'org-agenda-action)))
  2099. (defgroup org-latex nil
  2100. "Options for embedding LaTeX code into Org-mode."
  2101. :tag "Org LaTeX"
  2102. :group 'org)
  2103. (defcustom org-format-latex-options
  2104. '(:foreground default :background default :scale 1.0
  2105. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  2106. :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  2107. "Options for creating images from LaTeX fragments.
  2108. This is a property list with the following properties:
  2109. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  2110. `default' means use the foreground of the default face.
  2111. :background the background color, or \"Transparent\".
  2112. `default' means use the background of the default face.
  2113. :scale a scaling factor for the size of the images.
  2114. :html-foreground, :html-background, :html-scale
  2115. the same numbers for HTML export.
  2116. :matchers a list indicating which matchers should be used to
  2117. find LaTeX fragments. Valid members of this list are:
  2118. \"begin\" find environments
  2119. \"$1\" find single characters surrounded by $.$
  2120. \"$\" find math expressions surrounded by $...$
  2121. \"$$\" find math expressions surrounded by $$....$$
  2122. \"\\(\" find math expressions surrounded by \\(...\\)
  2123. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  2124. :group 'org-latex
  2125. :type 'plist)
  2126. (defcustom org-format-latex-header "\\documentclass{article}
  2127. \\usepackage{fullpage} % do not remove
  2128. \\usepackage{amssymb}
  2129. \\usepackage[usenames]{color}
  2130. \\usepackage{amsmath}
  2131. \\usepackage{latexsym}
  2132. \\usepackage[mathscr]{eucal}
  2133. \\pagestyle{empty} % do not remove"
  2134. "The document header used for processing LaTeX fragments."
  2135. :group 'org-latex
  2136. :type 'string)
  2137. (defgroup org-font-lock nil
  2138. "Font-lock settings for highlighting in Org-mode."
  2139. :tag "Org Font Lock"
  2140. :group 'org)
  2141. (defcustom org-level-color-stars-only nil
  2142. "Non-nil means fontify only the stars in each headline.
  2143. When nil, the entire headline is fontified.
  2144. Changing it requires restart of `font-lock-mode' to become effective
  2145. also in regions already fontified."
  2146. :group 'org-font-lock
  2147. :type 'boolean)
  2148. (defcustom org-hide-leading-stars nil
  2149. "Non-nil means, hide the first N-1 stars in a headline.
  2150. This works by using the face `org-hide' for these stars. This
  2151. face is white for a light background, and black for a dark
  2152. background. You may have to customize the face `org-hide' to
  2153. make this work.
  2154. Changing it requires restart of `font-lock-mode' to become effective
  2155. also in regions already fontified.
  2156. You may also set this on a per-file basis by adding one of the following
  2157. lines to the buffer:
  2158. #+STARTUP: hidestars
  2159. #+STARTUP: showstars"
  2160. :group 'org-font-lock
  2161. :type 'boolean)
  2162. (defcustom org-fontify-done-headline nil
  2163. "Non-nil means, change the face of a headline if it is marked DONE.
  2164. Normally, only the TODO/DONE keyword indicates the state of a headline.
  2165. When this is non-nil, the headline after the keyword is set to the
  2166. `org-headline-done' as an additional indication."
  2167. :group 'org-font-lock
  2168. :type 'boolean)
  2169. (defcustom org-fontify-emphasized-text t
  2170. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  2171. Changing this variable requires a restart of Emacs to take effect."
  2172. :group 'org-font-lock
  2173. :type 'boolean)
  2174. (defcustom org-highlight-latex-fragments-and-specials nil
  2175. "Non-nil means, fontify what is treated specially by the exporters."
  2176. :group 'org-font-lock
  2177. :type 'boolean)
  2178. (defcustom org-hide-emphasis-markers nil
  2179. "Non-nil mean font-lock should hide the emphasis marker characters."
  2180. :group 'org-font-lock
  2181. :type 'boolean)
  2182. (defvar org-emph-re nil
  2183. "Regular expression for matching emphasis.")
  2184. (defvar org-verbatim-re nil
  2185. "Regular expression for matching verbatim text.")
  2186. (defvar org-emphasis-regexp-components) ; defined just below
  2187. (defvar org-emphasis-alist) ; defined just below
  2188. (defun org-set-emph-re (var val)
  2189. "Set variable and compute the emphasis regular expression."
  2190. (set var val)
  2191. (when (and (boundp 'org-emphasis-alist)
  2192. (boundp 'org-emphasis-regexp-components)
  2193. org-emphasis-alist org-emphasis-regexp-components)
  2194. (let* ((e org-emphasis-regexp-components)
  2195. (pre (car e))
  2196. (post (nth 1 e))
  2197. (border (nth 2 e))
  2198. (body (nth 3 e))
  2199. (nl (nth 4 e))
  2200. (body1 (concat body "*?"))
  2201. (markers (mapconcat 'car org-emphasis-alist ""))
  2202. (vmarkers (mapconcat
  2203. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2204. org-emphasis-alist "")))
  2205. ;; make sure special characters appear at the right position in the class
  2206. (if (string-match "\\^" markers)
  2207. (setq markers (concat (replace-match "" t t markers) "^")))
  2208. (if (string-match "-" markers)
  2209. (setq markers (concat (replace-match "" t t markers) "-")))
  2210. (if (string-match "\\^" vmarkers)
  2211. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2212. (if (string-match "-" vmarkers)
  2213. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2214. (if (> nl 0)
  2215. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2216. (int-to-string nl) "\\}")))
  2217. ;; Make the regexp
  2218. (setq org-emph-re
  2219. (concat "\\([" pre "]\\|^\\)"
  2220. "\\("
  2221. "\\([" markers "]\\)"
  2222. "\\("
  2223. "[^" border "]\\|"
  2224. "[^" border "]"
  2225. body1
  2226. "[^" border "]"
  2227. "\\)"
  2228. "\\3\\)"
  2229. "\\([" post "]\\|$\\)"))
  2230. (setq org-verbatim-re
  2231. (concat "\\([" pre "]\\|^\\)"
  2232. "\\("
  2233. "\\([" vmarkers "]\\)"
  2234. "\\("
  2235. "[^" border "]\\|"
  2236. "[^" border "]"
  2237. body1
  2238. "[^" border "]"
  2239. "\\)"
  2240. "\\3\\)"
  2241. "\\([" post "]\\|$\\)")))))
  2242. (defcustom org-emphasis-regexp-components
  2243. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2244. "Components used to build the regular expression for emphasis.
  2245. This is a list with 6 entries. Terminology: In an emphasis string
  2246. like \" *strong word* \", we call the initial space PREMATCH, the final
  2247. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2248. and \"trong wor\" is the body. The different components in this variable
  2249. specify what is allowed/forbidden in each part:
  2250. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2251. post Chars allowed as postmatch. End of line will be allowed too.
  2252. border The chars *forbidden* as border characters.
  2253. body-regexp A regexp like \".\" to match a body character. Don't use
  2254. non-shy groups here, and don't allow newline here.
  2255. newline The maximum number of newlines allowed in an emphasis exp.
  2256. Use customize to modify this, or restart Emacs after changing it."
  2257. :group 'org-font-lock
  2258. :set 'org-set-emph-re
  2259. :type '(list
  2260. (sexp :tag "Allowed chars in pre ")
  2261. (sexp :tag "Allowed chars in post ")
  2262. (sexp :tag "Forbidden chars in border ")
  2263. (sexp :tag "Regexp for body ")
  2264. (integer :tag "number of newlines allowed")
  2265. (option (boolean :tag "Please ignore this button"))))
  2266. (defcustom org-emphasis-alist
  2267. `(("*" bold "<b>" "</b>")
  2268. ("/" italic "<i>" "</i>")
  2269. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2270. ("=" org-code "<code>" "</code>" verbatim)
  2271. ("~" org-verbatim "<code>" "</code>" verbatim)
  2272. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2273. "<del>" "</del>")
  2274. )
  2275. "Special syntax for emphasized text.
  2276. Text starting and ending with a special character will be emphasized, for
  2277. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2278. characters, the face to be used by font-lock for highlighting in Org-mode
  2279. Emacs buffers, and the HTML tags to be used for this.
  2280. Use customize to modify this, or restart Emacs after changing it."
  2281. :group 'org-font-lock
  2282. :set 'org-set-emph-re
  2283. :type '(repeat
  2284. (list
  2285. (string :tag "Marker character")
  2286. (choice
  2287. (face :tag "Font-lock-face")
  2288. (plist :tag "Face property list"))
  2289. (string :tag "HTML start tag")
  2290. (string :tag "HTML end tag")
  2291. (option (const verbatim)))))
  2292. ;;; Miscellaneous options
  2293. (defgroup org-completion nil
  2294. "Completion in Org-mode."
  2295. :tag "Org Completion"
  2296. :group 'org)
  2297. (defcustom org-completion-use-ido nil
  2298. "Non-nil means, use ido completion wherever possible.
  2299. Note that `ido-mode' must be active for this variable to be relevant.
  2300. If you decide to turn this variable on, you might well want to turn off
  2301. `org-outline-path-complete-in-steps'."
  2302. :group 'org-completion
  2303. :type 'boolean)
  2304. (defcustom org-completion-fallback-command 'hippie-expand
  2305. "The expansion command called by \\[org-complete] in normal context.
  2306. Normal means, no org-mode-specific context."
  2307. :group 'org-completion
  2308. :type 'function)
  2309. ;;; Functions and variables from ther packages
  2310. ;; Declared here to avoid compiler warnings
  2311. ;; XEmacs only
  2312. (defvar outline-mode-menu-heading)
  2313. (defvar outline-mode-menu-show)
  2314. (defvar outline-mode-menu-hide)
  2315. (defvar zmacs-regions) ; XEmacs regions
  2316. ;; Emacs only
  2317. (defvar mark-active)
  2318. ;; Various packages
  2319. (declare-function find-library-name "find-func" (library))
  2320. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2321. (declare-function calendar-forward-day "cal-move" (arg))
  2322. (declare-function calendar-goto-date "cal-move" (date))
  2323. (declare-function calendar-goto-today "cal-move" ())
  2324. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2325. (defvar calc-embedded-close-formula)
  2326. (defvar calc-embedded-open-formula)
  2327. (declare-function cdlatex-tab "ext:cdlatex" ())
  2328. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2329. (defvar font-lock-unfontify-region-function)
  2330. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2331. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2332. (defvar iswitchb-temp-buflist)
  2333. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2334. (defvar org-agenda-tags-todo-honor-ignore-options)
  2335. (declare-function org-agenda-skip "org-agenda" ())
  2336. (declare-function org-format-agenda-item "org-agenda"
  2337. (extra txt &optional category tags dotime noprefix remove-re))
  2338. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2339. (declare-function org-agenda-change-all-lines "org-agenda"
  2340. (newhead hdmarker &optional fixface just-this))
  2341. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2342. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2343. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2344. (beg end))
  2345. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2346. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  2347. "org-agenda" (&optional end))
  2348. (declare-function parse-time-string "parse-time" (string))
  2349. (declare-function remember "remember" (&optional initial))
  2350. (declare-function remember-buffer-desc "remember" ())
  2351. (declare-function remember-finalize "remember" ())
  2352. (defvar remember-save-after-remembering)
  2353. (defvar remember-data-file)
  2354. (defvar remember-register)
  2355. (defvar remember-buffer)
  2356. (defvar remember-handler-functions)
  2357. (defvar remember-annotation-functions)
  2358. (defvar texmathp-why)
  2359. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2360. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2361. (defvar w3m-current-url)
  2362. (defvar w3m-current-title)
  2363. (defvar org-latex-regexps)
  2364. ;;; Autoload and prepare some org modules
  2365. ;; Some table stuff that needs to be defined here, because it is used
  2366. ;; by the functions setting up org-mode or checking for table context.
  2367. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2368. "Detects an org-type or table-type table.")
  2369. (defconst org-table-line-regexp "^[ \t]*|"
  2370. "Detects an org-type table line.")
  2371. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2372. "Detects an org-type table line.")
  2373. (defconst org-table-hline-regexp "^[ \t]*|-"
  2374. "Detects an org-type table hline.")
  2375. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2376. "Detects a table-type table hline.")
  2377. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2378. "Searching from within a table (any type) this finds the first line
  2379. outside the table.")
  2380. ;; Autoload the functions in org-table.el that are needed by functions here.
  2381. (eval-and-compile
  2382. (org-autoload "org-table"
  2383. '(org-table-align org-table-begin org-table-blank-field
  2384. org-table-convert org-table-convert-region org-table-copy-down
  2385. org-table-copy-region org-table-create
  2386. org-table-create-or-convert-from-region
  2387. org-table-create-with-table.el org-table-current-dline
  2388. org-table-cut-region org-table-delete-column org-table-edit-field
  2389. org-table-edit-formulas org-table-end org-table-eval-formula
  2390. org-table-export org-table-field-info
  2391. org-table-get-stored-formulas org-table-goto-column
  2392. org-table-hline-and-move org-table-import org-table-insert-column
  2393. org-table-insert-hline org-table-insert-row org-table-iterate
  2394. org-table-justify-field-maybe org-table-kill-row
  2395. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2396. org-table-move-column org-table-move-column-left
  2397. org-table-move-column-right org-table-move-row
  2398. org-table-move-row-down org-table-move-row-up
  2399. org-table-next-field org-table-next-row org-table-paste-rectangle
  2400. org-table-previous-field org-table-recalculate
  2401. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2402. org-table-toggle-coordinate-overlays
  2403. org-table-toggle-formula-debugger org-table-wrap-region
  2404. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2405. (defun org-at-table-p (&optional table-type)
  2406. "Return t if the cursor is inside an org-type table.
  2407. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2408. (if org-enable-table-editor
  2409. (save-excursion
  2410. (beginning-of-line 1)
  2411. (looking-at (if table-type org-table-any-line-regexp
  2412. org-table-line-regexp)))
  2413. nil))
  2414. (defsubst org-table-p () (org-at-table-p))
  2415. (defun org-at-table.el-p ()
  2416. "Return t if and only if we are at a table.el table."
  2417. (and (org-at-table-p 'any)
  2418. (save-excursion
  2419. (goto-char (org-table-begin 'any))
  2420. (looking-at org-table1-hline-regexp))))
  2421. (defun org-table-recognize-table.el ()
  2422. "If there is a table.el table nearby, recognize it and move into it."
  2423. (if org-table-tab-recognizes-table.el
  2424. (if (org-at-table.el-p)
  2425. (progn
  2426. (beginning-of-line 1)
  2427. (if (looking-at org-table-dataline-regexp)
  2428. nil
  2429. (if (looking-at org-table1-hline-regexp)
  2430. (progn
  2431. (beginning-of-line 2)
  2432. (if (looking-at org-table-any-border-regexp)
  2433. (beginning-of-line -1)))))
  2434. (if (re-search-forward "|" (org-table-end t) t)
  2435. (progn
  2436. (require 'table)
  2437. (if (table--at-cell-p (point))
  2438. t
  2439. (message "recognizing table.el table...")
  2440. (table-recognize-table)
  2441. (message "recognizing table.el table...done")))
  2442. (error "This should not happen..."))
  2443. t)
  2444. nil)
  2445. nil))
  2446. (defun org-at-table-hline-p ()
  2447. "Return t if the cursor is inside a hline in a table."
  2448. (if org-enable-table-editor
  2449. (save-excursion
  2450. (beginning-of-line 1)
  2451. (looking-at org-table-hline-regexp))
  2452. nil))
  2453. (defvar org-table-clean-did-remove-column nil)
  2454. (defun org-table-map-tables (function)
  2455. "Apply FUNCTION to the start of all tables in the buffer."
  2456. (save-excursion
  2457. (save-restriction
  2458. (widen)
  2459. (goto-char (point-min))
  2460. (while (re-search-forward org-table-any-line-regexp nil t)
  2461. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2462. (beginning-of-line 1)
  2463. (if (looking-at org-table-line-regexp)
  2464. (save-excursion (funcall function)))
  2465. (re-search-forward org-table-any-border-regexp nil 1))))
  2466. (message "Mapping tables: done"))
  2467. ;; Declare and autoload functions from org-exp.el
  2468. (declare-function org-default-export-plist "org-exp")
  2469. (declare-function org-infile-export-plist "org-exp")
  2470. (declare-function org-get-current-options "org-exp")
  2471. (eval-and-compile
  2472. (org-autoload "org-exp"
  2473. '(org-export org-export-as-ascii org-export-visible
  2474. org-insert-export-options-template org-export-as-html-and-open
  2475. org-export-as-html-batch org-export-as-html-to-buffer
  2476. org-replace-region-by-html org-export-region-as-html
  2477. org-export-as-html org-export-icalendar-this-file
  2478. org-export-icalendar-all-agenda-files
  2479. org-table-clean-before-export
  2480. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2481. ;; Declare and autoload functions from org-agenda.el
  2482. (eval-and-compile
  2483. (org-autoload "org-agenda"
  2484. '(org-agenda org-agenda-list org-search-view
  2485. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2486. org-diary org-agenda-to-appt
  2487. org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
  2488. ;; Autoload org-remember
  2489. (eval-and-compile
  2490. (org-autoload "org-remember"
  2491. '(org-remember-insinuate org-remember-annotation
  2492. org-remember-apply-template org-remember org-remember-handler)))
  2493. ;; Autoload org-clock.el
  2494. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2495. (beg end))
  2496. (declare-function org-clock-update-mode-line "org-clock" ())
  2497. (defvar org-clock-start-time)
  2498. (defvar org-clock-marker (make-marker)
  2499. "Marker recording the last clock-in.")
  2500. (eval-and-compile
  2501. (org-autoload
  2502. "org-clock"
  2503. '(org-clock-in org-clock-out org-clock-cancel
  2504. org-clock-goto org-clock-sum org-clock-display
  2505. org-clock-remove-overlays org-clock-report
  2506. org-clocktable-shift org-dblock-write:clocktable
  2507. org-get-clocktable)))
  2508. (defun org-clock-update-time-maybe ()
  2509. "If this is a CLOCK line, update it and return t.
  2510. Otherwise, return nil."
  2511. (interactive)
  2512. (save-excursion
  2513. (beginning-of-line 1)
  2514. (skip-chars-forward " \t")
  2515. (when (looking-at org-clock-string)
  2516. (let ((re (concat "[ \t]*" org-clock-string
  2517. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2518. "\\([ \t]*=>.*\\)?\\)?"))
  2519. ts te h m s neg)
  2520. (cond
  2521. ((not (looking-at re))
  2522. nil)
  2523. ((not (match-end 2))
  2524. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2525. (> org-clock-marker (point))
  2526. (<= org-clock-marker (point-at-eol)))
  2527. ;; The clock is running here
  2528. (setq org-clock-start-time
  2529. (apply 'encode-time
  2530. (org-parse-time-string (match-string 1))))
  2531. (org-clock-update-mode-line)))
  2532. (t
  2533. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2534. (end-of-line 1)
  2535. (setq ts (match-string 1)
  2536. te (match-string 3))
  2537. (setq s (- (time-to-seconds
  2538. (apply 'encode-time (org-parse-time-string te)))
  2539. (time-to-seconds
  2540. (apply 'encode-time (org-parse-time-string ts))))
  2541. neg (< s 0)
  2542. s (abs s)
  2543. h (floor (/ s 3600))
  2544. s (- s (* 3600 h))
  2545. m (floor (/ s 60))
  2546. s (- s (* 60 s)))
  2547. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2548. t))))))
  2549. (defun org-check-running-clock ()
  2550. "Check if the current buffer contains the running clock.
  2551. If yes, offer to stop it and to save the buffer with the changes."
  2552. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2553. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2554. (buffer-name))))
  2555. (org-clock-out)
  2556. (when (y-or-n-p "Save changed buffer?")
  2557. (save-buffer))))
  2558. (defun org-clocktable-try-shift (dir n)
  2559. "Check if this line starts a clock table, if yes, shift the time block."
  2560. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2561. (org-clocktable-shift dir n)))
  2562. ;; Autoload org-timer.el
  2563. ;(declare-function org-timer "org-timer")
  2564. (eval-and-compile
  2565. (org-autoload
  2566. "org-timer"
  2567. '(org-timer-start org-timer org-timer-item
  2568. org-timer-change-times-in-region)))
  2569. ;; Autoload archiving code
  2570. ;; The stuff that is needed for cycling and tags has to be defined here.
  2571. (defgroup org-archive nil
  2572. "Options concerning archiving in Org-mode."
  2573. :tag "Org Archive"
  2574. :group 'org-structure)
  2575. (defcustom org-archive-location "%s_archive::"
  2576. "The location where subtrees should be archived.
  2577. The value of this variable is a string, consisting of two parts,
  2578. separated by a double-colon. The first part is a filename and
  2579. the second part is a headline.
  2580. When the filename is omitted, archiving happens in the same file.
  2581. %s in the filename will be replaced by the current file
  2582. name (without the directory part). Archiving to a different file
  2583. is useful to keep archived entries from contributing to the
  2584. Org-mode Agenda.
  2585. The archived entries will be filed as subtrees of the specified
  2586. headline. When the headline is omitted, the subtrees are simply
  2587. filed away at the end of the file, as top-level entries. Also in
  2588. the heading you can use %s to represent the file name, this can be
  2589. useful when using the same archive for a number of different files.
  2590. Here are a few examples:
  2591. \"%s_archive::\"
  2592. If the current file is Projects.org, archive in file
  2593. Projects.org_archive, as top-level trees. This is the default.
  2594. \"::* Archived Tasks\"
  2595. Archive in the current file, under the top-level headline
  2596. \"* Archived Tasks\".
  2597. \"~/org/archive.org::\"
  2598. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2599. \"~/org/archive.org::From %s\"
  2600. Archive in file ~/org/archive.org (absolute path), und headlines
  2601. \"From FILENAME\" where file name is the current file name.
  2602. \"basement::** Finished Tasks\"
  2603. Archive in file ./basement (relative path), as level 3 trees
  2604. below the level 2 heading \"** Finished Tasks\".
  2605. You may set this option on a per-file basis by adding to the buffer a
  2606. line like
  2607. #+ARCHIVE: basement::** Finished Tasks
  2608. You may also define it locally for a subtree by setting an ARCHIVE property
  2609. in the entry. If such a property is found in an entry, or anywhere up
  2610. the hierarchy, it will be used."
  2611. :group 'org-archive
  2612. :type 'string)
  2613. (defcustom org-archive-tag "ARCHIVE"
  2614. "The tag that marks a subtree as archived.
  2615. An archived subtree does not open during visibility cycling, and does
  2616. not contribute to the agenda listings.
  2617. After changing this, font-lock must be restarted in the relevant buffers to
  2618. get the proper fontification."
  2619. :group 'org-archive
  2620. :group 'org-keywords
  2621. :type 'string)
  2622. (defcustom org-agenda-skip-archived-trees t
  2623. "Non-nil means, the agenda will skip any items located in archived trees.
  2624. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2625. variable is no longer recommended, you should leave it at the value t.
  2626. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2627. :group 'org-archive
  2628. :group 'org-agenda-skip
  2629. :type 'boolean)
  2630. (defcustom org-cycle-open-archived-trees nil
  2631. "Non-nil means, `org-cycle' will open archived trees.
  2632. An archived tree is a tree marked with the tag ARCHIVE.
  2633. When nil, archived trees will stay folded. You can still open them with
  2634. normal outline commands like `show-all', but not with the cycling commands."
  2635. :group 'org-archive
  2636. :group 'org-cycle
  2637. :type 'boolean)
  2638. (defcustom org-sparse-tree-open-archived-trees nil
  2639. "Non-nil means sparse tree construction shows matches in archived trees.
  2640. When nil, matches in these trees are highlighted, but the trees are kept in
  2641. collapsed state."
  2642. :group 'org-archive
  2643. :group 'org-sparse-trees
  2644. :type 'boolean)
  2645. (defun org-cycle-hide-archived-subtrees (state)
  2646. "Re-hide all archived subtrees after a visibility state change."
  2647. (when (and (not org-cycle-open-archived-trees)
  2648. (not (memq state '(overview folded))))
  2649. (save-excursion
  2650. (let* ((globalp (memq state '(contents all)))
  2651. (beg (if globalp (point-min) (point)))
  2652. (end (if globalp (point-max) (org-end-of-subtree t))))
  2653. (org-hide-archived-subtrees beg end)
  2654. (goto-char beg)
  2655. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2656. (message "%s" (substitute-command-keys
  2657. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2658. (defun org-force-cycle-archived ()
  2659. "Cycle subtree even if it is archived."
  2660. (interactive)
  2661. (setq this-command 'org-cycle)
  2662. (let ((org-cycle-open-archived-trees t))
  2663. (call-interactively 'org-cycle)))
  2664. (defun org-hide-archived-subtrees (beg end)
  2665. "Re-hide all archived subtrees after a visibility state change."
  2666. (save-excursion
  2667. (let* ((re (concat ":" org-archive-tag ":")))
  2668. (goto-char beg)
  2669. (while (re-search-forward re end t)
  2670. (and (org-on-heading-p) (hide-subtree))
  2671. (org-end-of-subtree t)))))
  2672. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2673. (eval-and-compile
  2674. (org-autoload "org-archive"
  2675. '(org-add-archive-files org-archive-subtree
  2676. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2677. ;; Autoload Column View Code
  2678. (declare-function org-columns-number-to-string "org-colview")
  2679. (declare-function org-columns-get-format-and-top-level "org-colview")
  2680. (declare-function org-columns-compute "org-colview")
  2681. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2682. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2683. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2684. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2685. ;; Autoload ID code
  2686. (declare-function org-id-store-link "org-id")
  2687. (declare-function org-id-locations-load "org-id")
  2688. (declare-function org-id-locations-save "org-id")
  2689. (defvar org-id-track-globally)
  2690. (org-autoload "org-id"
  2691. '(org-id-get-create org-id-new org-id-copy org-id-get
  2692. org-id-get-with-outline-path-completion
  2693. org-id-get-with-outline-drilling
  2694. org-id-goto org-id-find org-id-store-link))
  2695. ;; Autoload Plotting Code
  2696. (org-autoload "org-plot"
  2697. '(org-plot/gnuplot))
  2698. ;;; Variables for pre-computed regular expressions, all buffer local
  2699. (defvar org-drawer-regexp nil
  2700. "Matches first line of a hidden block.")
  2701. (make-variable-buffer-local 'org-drawer-regexp)
  2702. (defvar org-todo-regexp nil
  2703. "Matches any of the TODO state keywords.")
  2704. (make-variable-buffer-local 'org-todo-regexp)
  2705. (defvar org-not-done-regexp nil
  2706. "Matches any of the TODO state keywords except the last one.")
  2707. (make-variable-buffer-local 'org-not-done-regexp)
  2708. (defvar org-todo-line-regexp nil
  2709. "Matches a headline and puts TODO state into group 2 if present.")
  2710. (make-variable-buffer-local 'org-todo-line-regexp)
  2711. (defvar org-complex-heading-regexp nil
  2712. "Matches a headline and puts everything into groups:
  2713. group 1: the stars
  2714. group 2: The todo keyword, maybe
  2715. group 3: Priority cookie
  2716. group 4: True headline
  2717. group 5: Tags")
  2718. (make-variable-buffer-local 'org-complex-heading-regexp)
  2719. (defvar org-todo-line-tags-regexp nil
  2720. "Matches a headline and puts TODO state into group 2 if present.
  2721. Also put tags into group 4 if tags are present.")
  2722. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2723. (defvar org-nl-done-regexp nil
  2724. "Matches newline followed by a headline with the DONE keyword.")
  2725. (make-variable-buffer-local 'org-nl-done-regexp)
  2726. (defvar org-looking-at-done-regexp nil
  2727. "Matches the DONE keyword a point.")
  2728. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2729. (defvar org-ds-keyword-length 12
  2730. "Maximum length of the Deadline and SCHEDULED keywords.")
  2731. (make-variable-buffer-local 'org-ds-keyword-length)
  2732. (defvar org-deadline-regexp nil
  2733. "Matches the DEADLINE keyword.")
  2734. (make-variable-buffer-local 'org-deadline-regexp)
  2735. (defvar org-deadline-time-regexp nil
  2736. "Matches the DEADLINE keyword together with a time stamp.")
  2737. (make-variable-buffer-local 'org-deadline-time-regexp)
  2738. (defvar org-deadline-line-regexp nil
  2739. "Matches the DEADLINE keyword and the rest of the line.")
  2740. (make-variable-buffer-local 'org-deadline-line-regexp)
  2741. (defvar org-scheduled-regexp nil
  2742. "Matches the SCHEDULED keyword.")
  2743. (make-variable-buffer-local 'org-scheduled-regexp)
  2744. (defvar org-scheduled-time-regexp nil
  2745. "Matches the SCHEDULED keyword together with a time stamp.")
  2746. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2747. (defvar org-closed-time-regexp nil
  2748. "Matches the CLOSED keyword together with a time stamp.")
  2749. (make-variable-buffer-local 'org-closed-time-regexp)
  2750. (defvar org-keyword-time-regexp nil
  2751. "Matches any of the 4 keywords, together with the time stamp.")
  2752. (make-variable-buffer-local 'org-keyword-time-regexp)
  2753. (defvar org-keyword-time-not-clock-regexp nil
  2754. "Matches any of the 3 keywords, together with the time stamp.")
  2755. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2756. (defvar org-maybe-keyword-time-regexp nil
  2757. "Matches a timestamp, possibly preceeded by a keyword.")
  2758. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2759. (defvar org-planning-or-clock-line-re nil
  2760. "Matches a line with planning or clock info.")
  2761. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2762. (defconst org-plain-time-of-day-regexp
  2763. (concat
  2764. "\\(\\<[012]?[0-9]"
  2765. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2766. "\\(--?"
  2767. "\\(\\<[012]?[0-9]"
  2768. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2769. "\\)?")
  2770. "Regular expression to match a plain time or time range.
  2771. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2772. groups carry important information:
  2773. 0 the full match
  2774. 1 the first time, range or not
  2775. 8 the second time, if it is a range.")
  2776. (defconst org-plain-time-extension-regexp
  2777. (concat
  2778. "\\(\\<[012]?[0-9]"
  2779. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2780. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2781. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2782. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2783. groups carry important information:
  2784. 0 the full match
  2785. 7 hours of duration
  2786. 9 minutes of duration")
  2787. (defconst org-stamp-time-of-day-regexp
  2788. (concat
  2789. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2790. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2791. "\\(--?"
  2792. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2793. "Regular expression to match a timestamp time or time range.
  2794. After a match, the following groups carry important information:
  2795. 0 the full match
  2796. 1 date plus weekday, for backreferencing to make sure both times on same day
  2797. 2 the first time, range or not
  2798. 4 the second time, if it is a range.")
  2799. (defconst org-startup-options
  2800. '(("fold" org-startup-folded t)
  2801. ("overview" org-startup-folded t)
  2802. ("nofold" org-startup-folded nil)
  2803. ("showall" org-startup-folded nil)
  2804. ("content" org-startup-folded content)
  2805. ("hidestars" org-hide-leading-stars t)
  2806. ("showstars" org-hide-leading-stars nil)
  2807. ("odd" org-odd-levels-only t)
  2808. ("oddeven" org-odd-levels-only nil)
  2809. ("align" org-startup-align-all-tables t)
  2810. ("noalign" org-startup-align-all-tables nil)
  2811. ("customtime" org-display-custom-times t)
  2812. ("logdone" org-log-done time)
  2813. ("lognotedone" org-log-done note)
  2814. ("nologdone" org-log-done nil)
  2815. ("lognoteclock-out" org-log-note-clock-out t)
  2816. ("nolognoteclock-out" org-log-note-clock-out nil)
  2817. ("logrepeat" org-log-repeat state)
  2818. ("lognoterepeat" org-log-repeat note)
  2819. ("nologrepeat" org-log-repeat nil)
  2820. ("fninline" org-footnote-define-inline t)
  2821. ("nofninline" org-footnote-define-inline nil)
  2822. ("fnlocal" org-footnote-section nil)
  2823. ("fnauto" org-footnote-auto-label t)
  2824. ("fnprompt" org-footnote-auto-label nil)
  2825. ("fnconfirm" org-footnote-auto-label confirm)
  2826. ("fnplain" org-footnote-auto-label plain)
  2827. ("constcgs" constants-unit-system cgs)
  2828. ("constSI" constants-unit-system SI))
  2829. "Variable associated with STARTUP options for org-mode.
  2830. Each element is a list of three items: The startup options as written
  2831. in the #+STARTUP line, the corresponding variable, and the value to
  2832. set this variable to if the option is found. An optional forth element PUSH
  2833. means to push this value onto the list in the variable.")
  2834. (defun org-set-regexps-and-options ()
  2835. "Precompute regular expressions for current buffer."
  2836. (when (org-mode-p)
  2837. (org-set-local 'org-todo-kwd-alist nil)
  2838. (org-set-local 'org-todo-key-alist nil)
  2839. (org-set-local 'org-todo-key-trigger nil)
  2840. (org-set-local 'org-todo-keywords-1 nil)
  2841. (org-set-local 'org-done-keywords nil)
  2842. (org-set-local 'org-todo-heads nil)
  2843. (org-set-local 'org-todo-sets nil)
  2844. (org-set-local 'org-todo-log-states nil)
  2845. (org-set-local 'org-file-properties nil)
  2846. (org-set-local 'org-file-tags nil)
  2847. (let ((re (org-make-options-regexp
  2848. '("CATEGORY" "TODO" "COLUMNS"
  2849. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2850. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")
  2851. "\\(?:[a-zA-Z][0-9a-zA-Z_]*_TODO\\)"))
  2852. (splitre "[ \t]+")
  2853. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2854. tail sep kws1 prio props ftags drawers
  2855. ext-setup-or-nil setup-contents (start 0))
  2856. (save-excursion
  2857. (save-restriction
  2858. (widen)
  2859. (goto-char (point-min))
  2860. (while (or (and ext-setup-or-nil
  2861. (string-match re ext-setup-or-nil start)
  2862. (setq start (match-end 0)))
  2863. (and (setq ext-setup-or-nil nil start 0)
  2864. (re-search-forward re nil t)))
  2865. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2866. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2867. (cond
  2868. ((equal key "CATEGORY")
  2869. (if (string-match "[ \t]+$" value)
  2870. (setq value (replace-match "" t t value)))
  2871. (setq cat value))
  2872. ((member key '("SEQ_TODO" "TODO"))
  2873. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2874. ((equal key "TYP_TODO")
  2875. (push (cons 'type (org-split-string value splitre)) kwds))
  2876. ((string-match "\\`\\([a-zA-Z][0-9a-zA-Z_]*\\)_TODO\\'" key)
  2877. ;; general TODO-like setup
  2878. (push (cons (intern (downcase (match-string 1 key)))
  2879. (org-split-string value splitre)) kwds))
  2880. ((equal key "TAGS")
  2881. (setq tags (append tags (if tags '("\\n") nil)
  2882. (org-split-string value splitre))))
  2883. ((equal key "COLUMNS")
  2884. (org-set-local 'org-columns-default-format value))
  2885. ((equal key "LINK")
  2886. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2887. (push (cons (match-string 1 value)
  2888. (org-trim (match-string 2 value)))
  2889. links)))
  2890. ((equal key "PRIORITIES")
  2891. (setq prio (org-split-string value " +")))
  2892. ((equal key "PROPERTY")
  2893. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2894. (push (cons (match-string 1 value) (match-string 2 value))
  2895. props)))
  2896. ((equal key "FILETAGS")
  2897. (when (string-match "\\S-" value)
  2898. (setq ftags
  2899. (append
  2900. ftags
  2901. (apply 'append
  2902. (mapcar (lambda (x) (org-split-string x ":"))
  2903. (org-split-string value)))))))
  2904. ((equal key "DRAWERS")
  2905. (setq drawers (org-split-string value splitre)))
  2906. ((equal key "CONSTANTS")
  2907. (setq const (append const (org-split-string value splitre))))
  2908. ((equal key "STARTUP")
  2909. (let ((opts (org-split-string value splitre))
  2910. l var val)
  2911. (while (setq l (pop opts))
  2912. (when (setq l (assoc l org-startup-options))
  2913. (setq var (nth 1 l) val (nth 2 l))
  2914. (if (not (nth 3 l))
  2915. (set (make-local-variable var) val)
  2916. (if (not (listp (symbol-value var)))
  2917. (set (make-local-variable var) nil))
  2918. (set (make-local-variable var) (symbol-value var))
  2919. (add-to-list var val))))))
  2920. ((equal key "ARCHIVE")
  2921. (string-match " *$" value)
  2922. (setq arch (replace-match "" t t value))
  2923. (remove-text-properties 0 (length arch)
  2924. '(face t fontified t) arch))
  2925. ((equal key "SETUPFILE")
  2926. (setq setup-contents (org-file-contents
  2927. (expand-file-name
  2928. (org-remove-double-quotes value))
  2929. 'noerror))
  2930. (if (not ext-setup-or-nil)
  2931. (setq ext-setup-or-nil setup-contents start 0)
  2932. (setq ext-setup-or-nil
  2933. (concat (substring ext-setup-or-nil 0 start)
  2934. "\n" setup-contents "\n"
  2935. (substring ext-setup-or-nil start)))))
  2936. ))))
  2937. (when cat
  2938. (org-set-local 'org-category (intern cat))
  2939. (push (cons "CATEGORY" cat) props))
  2940. (when prio
  2941. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2942. (setq prio (mapcar 'string-to-char prio))
  2943. (org-set-local 'org-highest-priority (nth 0 prio))
  2944. (org-set-local 'org-lowest-priority (nth 1 prio))
  2945. (org-set-local 'org-default-priority (nth 2 prio)))
  2946. (and props (org-set-local 'org-file-properties (nreverse props)))
  2947. (and ftags (org-set-local 'org-file-tags ftags))
  2948. (and drawers (org-set-local 'org-drawers drawers))
  2949. (and arch (org-set-local 'org-archive-location arch))
  2950. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2951. ;; Process the TODO keywords
  2952. (unless kwds
  2953. ;; Use the global values as if they had been given locally.
  2954. (setq kwds (default-value 'org-todo-keywords))
  2955. (if (stringp (car kwds))
  2956. (setq kwds (list (cons org-todo-interpretation
  2957. (default-value 'org-todo-keywords)))))
  2958. (setq kwds (reverse kwds)))
  2959. (setq kwds (nreverse kwds))
  2960. (let (inter kws kw)
  2961. (while (setq kws (pop kwds))
  2962. (let ((kws (or
  2963. (run-hook-with-args-until-success
  2964. 'org-todo-setup-filter-hook kws)
  2965. kws)))
  2966. (setq inter (pop kws) sep (member "|" kws)
  2967. kws0 (delete "|" (copy-sequence kws))
  2968. kwsa nil
  2969. kws1 (mapcar
  2970. (lambda (x)
  2971. ;; 1 2
  2972. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2973. (progn
  2974. (setq kw (match-string 1 x)
  2975. key (and (match-end 2) (match-string 2 x))
  2976. log (org-extract-log-state-settings x))
  2977. (push (cons kw (and key (string-to-char key))) kwsa)
  2978. (and log (push log org-todo-log-states))
  2979. kw)
  2980. (error "Invalid TODO keyword %s" x)))
  2981. kws0)
  2982. kwsa (if kwsa (append '((:startgroup))
  2983. (nreverse kwsa)
  2984. '((:endgroup))))
  2985. hw (car kws1)
  2986. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2987. tail (list inter hw (car dws) (org-last dws))))
  2988. (add-to-list 'org-todo-heads hw 'append)
  2989. (push kws1 org-todo-sets)
  2990. (setq org-done-keywords (append org-done-keywords dws nil))
  2991. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2992. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2993. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2994. (setq org-todo-sets (nreverse org-todo-sets)
  2995. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2996. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2997. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2998. ;; Process the constants
  2999. (when const
  3000. (let (e cst)
  3001. (while (setq e (pop const))
  3002. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  3003. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  3004. (setq org-table-formula-constants-local cst)))
  3005. ;; Process the tags.
  3006. (when tags
  3007. (let (e tgs)
  3008. (while (setq e (pop tags))
  3009. (cond
  3010. ((equal e "{") (push '(:startgroup) tgs))
  3011. ((equal e "}") (push '(:endgroup) tgs))
  3012. ((equal e "\\n") (push '(:newline) tgs))
  3013. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  3014. (push (cons (match-string 1 e)
  3015. (string-to-char (match-string 2 e)))
  3016. tgs))
  3017. (t (push (list e) tgs))))
  3018. (org-set-local 'org-tag-alist nil)
  3019. (while (setq e (pop tgs))
  3020. (or (and (stringp (car e))
  3021. (assoc (car e) org-tag-alist))
  3022. (push e org-tag-alist)))))
  3023. ;; Compute the regular expressions and other local variables
  3024. (if (not org-done-keywords)
  3025. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  3026. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  3027. (length org-scheduled-string)
  3028. (length org-clock-string)
  3029. (length org-closed-string)))
  3030. org-drawer-regexp
  3031. (concat "^[ \t]*:\\("
  3032. (mapconcat 'regexp-quote org-drawers "\\|")
  3033. "\\):[ \t]*$")
  3034. org-not-done-keywords
  3035. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  3036. org-todo-regexp
  3037. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  3038. "\\|") "\\)\\>")
  3039. org-not-done-regexp
  3040. (concat "\\<\\("
  3041. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  3042. "\\)\\>")
  3043. org-todo-line-regexp
  3044. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3045. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3046. "\\)\\>\\)?[ \t]*\\(.*\\)")
  3047. org-complex-heading-regexp
  3048. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3049. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3050. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  3051. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3052. org-nl-done-regexp
  3053. (concat "\n\\*+[ \t]+"
  3054. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  3055. "\\)" "\\>")
  3056. org-todo-line-tags-regexp
  3057. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  3058. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  3059. (org-re
  3060. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  3061. org-looking-at-done-regexp
  3062. (concat "^" "\\(?:"
  3063. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  3064. "\\>")
  3065. org-deadline-regexp (concat "\\<" org-deadline-string)
  3066. org-deadline-time-regexp
  3067. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  3068. org-deadline-line-regexp
  3069. (concat "\\<\\(" org-deadline-string "\\).*")
  3070. org-scheduled-regexp
  3071. (concat "\\<" org-scheduled-string)
  3072. org-scheduled-time-regexp
  3073. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  3074. org-closed-time-regexp
  3075. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  3076. org-keyword-time-regexp
  3077. (concat "\\<\\(" org-scheduled-string
  3078. "\\|" org-deadline-string
  3079. "\\|" org-closed-string
  3080. "\\|" org-clock-string "\\)"
  3081. " *[[<]\\([^]>]+\\)[]>]")
  3082. org-keyword-time-not-clock-regexp
  3083. (concat "\\<\\(" org-scheduled-string
  3084. "\\|" org-deadline-string
  3085. "\\|" org-closed-string
  3086. "\\)"
  3087. " *[[<]\\([^]>]+\\)[]>]")
  3088. org-maybe-keyword-time-regexp
  3089. (concat "\\(\\<\\(" org-scheduled-string
  3090. "\\|" org-deadline-string
  3091. "\\|" org-closed-string
  3092. "\\|" org-clock-string "\\)\\)?"
  3093. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  3094. org-planning-or-clock-line-re
  3095. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  3096. "\\|" org-deadline-string
  3097. "\\|" org-closed-string "\\|" org-clock-string
  3098. "\\)\\>\\)")
  3099. )
  3100. (org-compute-latex-and-specials-regexp)
  3101. (org-set-font-lock-defaults))))
  3102. (defun org-file-contents (file &optional noerror)
  3103. "Return the contents of FILE, as a string."
  3104. (if (or (not file)
  3105. (not (file-readable-p file)))
  3106. (if noerror
  3107. (progn
  3108. (message "Cannot read file %s" file)
  3109. (ding) (sit-for 2)
  3110. "")
  3111. (error "Cannot read file %s" file))
  3112. (with-temp-buffer
  3113. (insert-file-contents file)
  3114. (buffer-string))))
  3115. (defun org-extract-log-state-settings (x)
  3116. "Extract the log state setting from a TODO keyword string.
  3117. This will extract info from a string like \"WAIT(w@/!)\"."
  3118. (let (kw key log1 log2)
  3119. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  3120. (setq kw (match-string 1 x)
  3121. key (and (match-end 2) (match-string 2 x))
  3122. log1 (and (match-end 3) (match-string 3 x))
  3123. log2 (and (match-end 4) (match-string 4 x)))
  3124. (and (or log1 log2)
  3125. (list kw
  3126. (and log1 (if (equal log1 "!") 'time 'note))
  3127. (and log2 (if (equal log2 "!") 'time 'note)))))))
  3128. (defun org-remove-keyword-keys (list)
  3129. "Remove a pair of parenthesis at the end of each string in LIST."
  3130. (mapcar (lambda (x)
  3131. (if (string-match "(.*)$" x)
  3132. (substring x 0 (match-beginning 0))
  3133. x))
  3134. list))
  3135. ;; FIXME: this could be done much better, using second characters etc.
  3136. (defun org-assign-fast-keys (alist)
  3137. "Assign fast keys to a keyword-key alist.
  3138. Respect keys that are already there."
  3139. (let (new e k c c1 c2 (char ?a))
  3140. (while (setq e (pop alist))
  3141. (cond
  3142. ((equal e '(:startgroup)) (push e new))
  3143. ((equal e '(:endgroup)) (push e new))
  3144. ((equal e '(:newline)) (push e new))
  3145. (t
  3146. (setq k (car e) c2 nil)
  3147. (if (cdr e)
  3148. (setq c (cdr e))
  3149. ;; automatically assign a character.
  3150. (setq c1 (string-to-char
  3151. (downcase (substring
  3152. k (if (= (string-to-char k) ?@) 1 0)))))
  3153. (if (or (rassoc c1 new) (rassoc c1 alist))
  3154. (while (or (rassoc char new) (rassoc char alist))
  3155. (setq char (1+ char)))
  3156. (setq c2 c1))
  3157. (setq c (or c2 char)))
  3158. (push (cons k c) new))))
  3159. (nreverse new)))
  3160. ;;; Some variables used in various places
  3161. (defvar org-window-configuration nil
  3162. "Used in various places to store a window configuration.")
  3163. (defvar org-finish-function nil
  3164. "Function to be called when `C-c C-c' is used.
  3165. This is for getting out of special buffers like remember.")
  3166. ;; FIXME: Occasionally check by commenting these, to make sure
  3167. ;; no other functions uses these, forgetting to let-bind them.
  3168. (defvar entry)
  3169. (defvar state)
  3170. (defvar last-state)
  3171. (defvar date)
  3172. (defvar description)
  3173. ;; Defined somewhere in this file, but used before definition.
  3174. (defvar org-html-entities)
  3175. (defvar org-struct-menu)
  3176. (defvar org-org-menu)
  3177. (defvar org-tbl-menu)
  3178. (defvar org-agenda-keymap)
  3179. ;;;; Define the Org-mode
  3180. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  3181. (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."))
  3182. ;; We use a before-change function to check if a table might need
  3183. ;; an update.
  3184. (defvar org-table-may-need-update t
  3185. "Indicates that a table might need an update.
  3186. This variable is set by `org-before-change-function'.
  3187. `org-table-align' sets it back to nil.")
  3188. (defun org-before-change-function (beg end)
  3189. "Every change indicates that a table might need an update."
  3190. (setq org-table-may-need-update t))
  3191. (defvar org-mode-map)
  3192. (defvar org-mode-hook nil
  3193. "Mode hook for Org-mode, run after the mode was turned on.")
  3194. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  3195. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  3196. (defvar org-table-buffer-is-an nil)
  3197. (defconst org-outline-regexp "\\*+ ")
  3198. ;;;###autoload
  3199. (define-derived-mode org-mode outline-mode "Org"
  3200. "Outline-based notes management and organizer, alias
  3201. \"Carsten's outline-mode for keeping track of everything.\"
  3202. Org-mode develops organizational tasks around a NOTES file which
  3203. contains information about projects as plain text. Org-mode is
  3204. implemented on top of outline-mode, which is ideal to keep the content
  3205. of large files well structured. It supports ToDo items, deadlines and
  3206. time stamps, which magically appear in the diary listing of the Emacs
  3207. calendar. Tables are easily created with a built-in table editor.
  3208. Plain text URL-like links connect to websites, emails (VM), Usenet
  3209. messages (Gnus), BBDB entries, and any files related to the project.
  3210. For printing and sharing of notes, an Org-mode file (or a part of it)
  3211. can be exported as a structured ASCII or HTML file.
  3212. The following commands are available:
  3213. \\{org-mode-map}"
  3214. ;; Get rid of Outline menus, they are not needed
  3215. ;; Need to do this here because define-derived-mode sets up
  3216. ;; the keymap so late. Still, it is a waste to call this each time
  3217. ;; we switch another buffer into org-mode.
  3218. (if (featurep 'xemacs)
  3219. (when (boundp 'outline-mode-menu-heading)
  3220. ;; Assume this is Greg's port, it used easymenu
  3221. (easy-menu-remove outline-mode-menu-heading)
  3222. (easy-menu-remove outline-mode-menu-show)
  3223. (easy-menu-remove outline-mode-menu-hide))
  3224. (define-key org-mode-map [menu-bar headings] 'undefined)
  3225. (define-key org-mode-map [menu-bar hide] 'undefined)
  3226. (define-key org-mode-map [menu-bar show] 'undefined))
  3227. (org-load-modules-maybe)
  3228. (easy-menu-add org-org-menu)
  3229. (easy-menu-add org-tbl-menu)
  3230. (org-install-agenda-files-menu)
  3231. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3232. (org-add-to-invisibility-spec '(org-cwidth))
  3233. (when (featurep 'xemacs)
  3234. (org-set-local 'line-move-ignore-invisible t))
  3235. (org-set-local 'outline-regexp org-outline-regexp)
  3236. (org-set-local 'outline-level 'org-outline-level)
  3237. (when (and org-ellipsis
  3238. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3239. (fboundp 'make-glyph-code))
  3240. (unless org-display-table
  3241. (setq org-display-table (make-display-table)))
  3242. (set-display-table-slot
  3243. org-display-table 4
  3244. (vconcat (mapcar
  3245. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3246. org-ellipsis)))
  3247. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3248. (setq buffer-display-table org-display-table))
  3249. (org-set-regexps-and-options)
  3250. (when (and org-tag-faces (not org-tags-special-faces-re))
  3251. ;; tag faces set outside customize.... force initialization.
  3252. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3253. ;; Calc embedded
  3254. (org-set-local 'calc-embedded-open-mode "# ")
  3255. (modify-syntax-entry ?# "<")
  3256. (modify-syntax-entry ?@ "w")
  3257. (if org-startup-truncated (setq truncate-lines t))
  3258. (org-set-local 'font-lock-unfontify-region-function
  3259. 'org-unfontify-region)
  3260. ;; Activate before-change-function
  3261. (org-set-local 'org-table-may-need-update t)
  3262. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3263. 'local)
  3264. ;; Check for running clock before killing a buffer
  3265. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3266. ;; Paragraphs and auto-filling
  3267. (org-set-autofill-regexps)
  3268. (setq indent-line-function 'org-indent-line-function)
  3269. (org-update-radio-target-regexp)
  3270. ;; Make sure dependence stuff works reliably, even for users who set it
  3271. ;; too late :-(
  3272. (if org-enforce-todo-dependencies
  3273. (add-hook 'org-blocker-hook
  3274. 'org-block-todo-from-children-or-siblings)
  3275. (remove-hook 'org-blocker-hook
  3276. 'org-block-todo-from-children-or-siblings))
  3277. (if org-enforce-todo-checkbox-dependencies
  3278. (add-hook 'org-blocker-hook
  3279. 'org-block-todo-from-checkboxes)
  3280. (remove-hook 'org-blocker-hook
  3281. 'org-block-todo-from-checkboxes))
  3282. ;; Comment characters
  3283. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3284. (org-set-local 'comment-padding " ")
  3285. ;; Align options lines
  3286. (org-set-local
  3287. 'align-mode-rules-list
  3288. '((org-in-buffer-settings
  3289. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3290. (modes . '(org-mode)))))
  3291. ;; Imenu
  3292. (org-set-local 'imenu-create-index-function
  3293. 'org-imenu-get-tree)
  3294. ;; Make isearch reveal context
  3295. (if (or (featurep 'xemacs)
  3296. (not (boundp 'outline-isearch-open-invisible-function)))
  3297. ;; Emacs 21 and XEmacs make use of the hook
  3298. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3299. ;; Emacs 22 deals with this through a special variable
  3300. (org-set-local 'outline-isearch-open-invisible-function
  3301. (lambda (&rest ignore) (org-show-context 'isearch))))
  3302. ;; If empty file that did not turn on org-mode automatically, make it to.
  3303. (if (and org-insert-mode-line-in-empty-file
  3304. (interactive-p)
  3305. (= (point-min) (point-max)))
  3306. (insert "# -*- mode: org -*-\n\n"))
  3307. (unless org-inhibit-startup
  3308. (when org-startup-align-all-tables
  3309. (let ((bmp (buffer-modified-p)))
  3310. (org-table-map-tables 'org-table-align)
  3311. (set-buffer-modified-p bmp)))
  3312. (org-set-startup-visibility)))
  3313. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3314. (defun org-current-time ()
  3315. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3316. (if (> (car org-time-stamp-rounding-minutes) 1)
  3317. (let ((r (car org-time-stamp-rounding-minutes))
  3318. (time (decode-time)))
  3319. (apply 'encode-time
  3320. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3321. (nthcdr 2 time))))
  3322. (current-time)))
  3323. ;;;; Font-Lock stuff, including the activators
  3324. (defvar org-mouse-map (make-sparse-keymap))
  3325. (org-defkey org-mouse-map
  3326. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3327. (org-defkey org-mouse-map
  3328. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3329. (when org-mouse-1-follows-link
  3330. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3331. (when org-tab-follows-link
  3332. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3333. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3334. (require 'font-lock)
  3335. (defconst org-non-link-chars "]\t\n\r<>")
  3336. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3337. "shell" "elisp"))
  3338. (defvar org-link-types-re nil
  3339. "Matches a link that has a url-like prefix like \"http:\"")
  3340. (defvar org-link-re-with-space nil
  3341. "Matches a link with spaces, optional angular brackets around it.")
  3342. (defvar org-link-re-with-space2 nil
  3343. "Matches a link with spaces, optional angular brackets around it.")
  3344. (defvar org-link-re-with-space3 nil
  3345. "Matches a link with spaces, only for internal part in bracket links.")
  3346. (defvar org-angle-link-re nil
  3347. "Matches link with angular brackets, spaces are allowed.")
  3348. (defvar org-plain-link-re nil
  3349. "Matches plain link, without spaces.")
  3350. (defvar org-bracket-link-regexp nil
  3351. "Matches a link in double brackets.")
  3352. (defvar org-bracket-link-analytic-regexp nil
  3353. "Regular expression used to analyze links.
  3354. Here is what the match groups contain after a match:
  3355. 1: http:
  3356. 2: http
  3357. 3: path
  3358. 4: [desc]
  3359. 5: desc")
  3360. (defvar org-bracket-link-analytic-regexp++ nil
  3361. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3362. (defvar org-any-link-re nil
  3363. "Regular expression matching any link.")
  3364. (defun org-make-link-regexps ()
  3365. "Update the link regular expressions.
  3366. This should be called after the variable `org-link-types' has changed."
  3367. (setq org-link-types-re
  3368. (concat
  3369. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3370. org-link-re-with-space
  3371. (concat
  3372. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3373. "\\([^" org-non-link-chars " ]"
  3374. "[^" org-non-link-chars "]*"
  3375. "[^" org-non-link-chars " ]\\)>?")
  3376. org-link-re-with-space2
  3377. (concat
  3378. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3379. "\\([^" org-non-link-chars " ]"
  3380. "[^\t\n\r]*"
  3381. "[^" org-non-link-chars " ]\\)>?")
  3382. org-link-re-with-space3
  3383. (concat
  3384. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3385. "\\([^" org-non-link-chars " ]"
  3386. "[^\t\n\r]*\\)")
  3387. org-angle-link-re
  3388. (concat
  3389. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3390. "\\([^" org-non-link-chars " ]"
  3391. "[^" org-non-link-chars "]*"
  3392. "\\)>")
  3393. org-plain-link-re
  3394. (concat
  3395. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3396. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3397. org-bracket-link-regexp
  3398. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3399. org-bracket-link-analytic-regexp
  3400. (concat
  3401. "\\[\\["
  3402. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3403. "\\([^]]+\\)"
  3404. "\\]"
  3405. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3406. "\\]")
  3407. org-bracket-link-analytic-regexp++
  3408. (concat
  3409. "\\[\\["
  3410. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3411. "\\([^]]+\\)"
  3412. "\\]"
  3413. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3414. "\\]")
  3415. org-any-link-re
  3416. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3417. org-angle-link-re "\\)\\|\\("
  3418. org-plain-link-re "\\)")))
  3419. (org-make-link-regexps)
  3420. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3421. "Regular expression for fast time stamp matching.")
  3422. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3423. "Regular expression for fast time stamp matching.")
  3424. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3425. "Regular expression matching time strings for analysis.
  3426. This one does not require the space after the date, so it can be used
  3427. on a string that terminates immediately after the date.")
  3428. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3429. "Regular expression matching time strings for analysis.")
  3430. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3431. "Regular expression matching time stamps, with groups.")
  3432. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3433. "Regular expression matching time stamps (also [..]), with groups.")
  3434. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3435. "Regular expression matching a time stamp range.")
  3436. (defconst org-tr-regexp-both
  3437. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3438. "Regular expression matching a time stamp range.")
  3439. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3440. org-ts-regexp "\\)?")
  3441. "Regular expression matching a time stamp or time stamp range.")
  3442. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3443. org-ts-regexp-both "\\)?")
  3444. "Regular expression matching a time stamp or time stamp range.
  3445. The time stamps may be either active or inactive.")
  3446. (defvar org-emph-face nil)
  3447. (defun org-do-emphasis-faces (limit)
  3448. "Run through the buffer and add overlays to links."
  3449. (let (rtn)
  3450. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3451. (if (not (= (char-after (match-beginning 3))
  3452. (char-after (match-beginning 4))))
  3453. (progn
  3454. (setq rtn t)
  3455. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3456. 'face
  3457. (nth 1 (assoc (match-string 3)
  3458. org-emphasis-alist)))
  3459. (add-text-properties (match-beginning 2) (match-end 2)
  3460. '(font-lock-multiline t))
  3461. (when org-hide-emphasis-markers
  3462. (add-text-properties (match-end 4) (match-beginning 5)
  3463. '(invisible org-link))
  3464. (add-text-properties (match-beginning 3) (match-end 3)
  3465. '(invisible org-link)))))
  3466. (backward-char 1))
  3467. rtn))
  3468. (defun org-emphasize (&optional char)
  3469. "Insert or change an emphasis, i.e. a font like bold or italic.
  3470. If there is an active region, change that region to a new emphasis.
  3471. If there is no region, just insert the marker characters and position
  3472. the cursor between them.
  3473. CHAR should be either the marker character, or the first character of the
  3474. HTML tag associated with that emphasis. If CHAR is a space, the means
  3475. to remove the emphasis of the selected region.
  3476. If char is not given (for example in an interactive call) it
  3477. will be prompted for."
  3478. (interactive)
  3479. (let ((eal org-emphasis-alist) e det
  3480. (erc org-emphasis-regexp-components)
  3481. (prompt "")
  3482. (string "") beg end move tag c s)
  3483. (if (org-region-active-p)
  3484. (setq beg (region-beginning) end (region-end)
  3485. string (buffer-substring beg end))
  3486. (setq move t))
  3487. (while (setq e (pop eal))
  3488. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3489. c (aref tag 0))
  3490. (push (cons c (string-to-char (car e))) det)
  3491. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3492. (substring tag 1)))))
  3493. (setq det (nreverse det))
  3494. (unless char
  3495. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3496. (setq char (read-char-exclusive)))
  3497. (setq char (or (cdr (assoc char det)) char))
  3498. (if (equal char ?\ )
  3499. (setq s "" move nil)
  3500. (unless (assoc (char-to-string char) org-emphasis-alist)
  3501. (error "No such emphasis marker: \"%c\"" char))
  3502. (setq s (char-to-string char)))
  3503. (while (and (> (length string) 1)
  3504. (equal (substring string 0 1) (substring string -1))
  3505. (assoc (substring string 0 1) org-emphasis-alist))
  3506. (setq string (substring string 1 -1)))
  3507. (setq string (concat s string s))
  3508. (if beg (delete-region beg end))
  3509. (unless (or (bolp)
  3510. (string-match (concat "[" (nth 0 erc) "\n]")
  3511. (char-to-string (char-before (point)))))
  3512. (insert " "))
  3513. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3514. (char-to-string (char-after (point))))
  3515. (insert " ") (backward-char 1))
  3516. (insert string)
  3517. (and move (backward-char 1))))
  3518. (defconst org-nonsticky-props
  3519. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3520. (defun org-activate-plain-links (limit)
  3521. "Run through the buffer and add overlays to links."
  3522. (catch 'exit
  3523. (let (f)
  3524. (while (re-search-forward org-plain-link-re limit t)
  3525. (setq f (get-text-property (match-beginning 0) 'face))
  3526. (if (or (eq f 'org-tag)
  3527. (and (listp f) (memq 'org-tag f)))
  3528. nil
  3529. (add-text-properties (match-beginning 0) (match-end 0)
  3530. (list 'mouse-face 'highlight
  3531. 'rear-nonsticky org-nonsticky-props
  3532. 'keymap org-mouse-map
  3533. ))
  3534. (throw 'exit t))))))
  3535. (defun org-activate-code (limit)
  3536. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3537. (progn
  3538. (remove-text-properties (match-beginning 0) (match-end 0)
  3539. '(display t invisible t intangible t))
  3540. t)))
  3541. (defun org-activate-angle-links (limit)
  3542. "Run through the buffer and add overlays to links."
  3543. (if (re-search-forward org-angle-link-re limit t)
  3544. (progn
  3545. (add-text-properties (match-beginning 0) (match-end 0)
  3546. (list 'mouse-face 'highlight
  3547. 'rear-nonsticky org-nonsticky-props
  3548. 'keymap org-mouse-map
  3549. ))
  3550. t)))
  3551. (defun org-activate-footnote-links (limit)
  3552. "Run through the buffer and add overlays to links."
  3553. (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)"
  3554. limit t)
  3555. (progn
  3556. (add-text-properties (match-beginning 2) (match-end 2)
  3557. (list 'mouse-face 'highlight
  3558. 'rear-nonsticky org-nonsticky-props
  3559. 'keymap org-mouse-map
  3560. 'help-echo
  3561. (if (= (point-at-bol) (match-beginning 2))
  3562. "Footnote definition"
  3563. "Footnote reference")
  3564. ))
  3565. t)))
  3566. (defun org-activate-bracket-links (limit)
  3567. "Run through the buffer and add overlays to bracketed links."
  3568. (if (re-search-forward org-bracket-link-regexp limit t)
  3569. (let* ((help (concat "LINK: "
  3570. (org-match-string-no-properties 1)))
  3571. ;; FIXME: above we should remove the escapes.
  3572. ;; but that requires another match, protecting match data,
  3573. ;; a lot of overhead for font-lock.
  3574. (ip (org-maybe-intangible
  3575. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3576. 'keymap org-mouse-map 'mouse-face 'highlight
  3577. 'font-lock-multiline t 'help-echo help)))
  3578. (vp (list 'rear-nonsticky org-nonsticky-props
  3579. 'keymap org-mouse-map 'mouse-face 'highlight
  3580. ' font-lock-multiline t 'help-echo help)))
  3581. ;; We need to remove the invisible property here. Table narrowing
  3582. ;; may have made some of this invisible.
  3583. (remove-text-properties (match-beginning 0) (match-end 0)
  3584. '(invisible nil))
  3585. (if (match-end 3)
  3586. (progn
  3587. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3588. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3589. (add-text-properties (match-end 3) (match-end 0) ip))
  3590. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3591. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3592. (add-text-properties (match-end 1) (match-end 0) ip))
  3593. t)))
  3594. (defun org-activate-dates (limit)
  3595. "Run through the buffer and add overlays to dates."
  3596. (if (re-search-forward org-tsr-regexp-both limit t)
  3597. (progn
  3598. (add-text-properties (match-beginning 0) (match-end 0)
  3599. (list 'mouse-face 'highlight
  3600. 'rear-nonsticky org-nonsticky-props
  3601. 'keymap org-mouse-map))
  3602. (when org-display-custom-times
  3603. (if (match-end 3)
  3604. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3605. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3606. t)))
  3607. (defvar org-target-link-regexp nil
  3608. "Regular expression matching radio targets in plain text.")
  3609. (make-variable-buffer-local 'org-target-link-regexp)
  3610. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3611. "Regular expression matching a link target.")
  3612. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3613. "Regular expression matching a radio target.")
  3614. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3615. "Regular expression matching any target.")
  3616. (defun org-activate-target-links (limit)
  3617. "Run through the buffer and add overlays to target matches."
  3618. (when org-target-link-regexp
  3619. (let ((case-fold-search t))
  3620. (if (re-search-forward org-target-link-regexp limit t)
  3621. (progn
  3622. (add-text-properties (match-beginning 0) (match-end 0)
  3623. (list 'mouse-face 'highlight
  3624. 'rear-nonsticky org-nonsticky-props
  3625. 'keymap org-mouse-map
  3626. 'help-echo "Radio target link"
  3627. 'org-linked-text t))
  3628. t)))))
  3629. (defun org-update-radio-target-regexp ()
  3630. "Find all radio targets in this file and update the regular expression."
  3631. (interactive)
  3632. (when (memq 'radio org-activate-links)
  3633. (setq org-target-link-regexp
  3634. (org-make-target-link-regexp (org-all-targets 'radio)))
  3635. (org-restart-font-lock)))
  3636. (defun org-hide-wide-columns (limit)
  3637. (let (s e)
  3638. (setq s (text-property-any (point) (or limit (point-max))
  3639. 'org-cwidth t))
  3640. (when s
  3641. (setq e (next-single-property-change s 'org-cwidth))
  3642. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3643. (goto-char e)
  3644. t)))
  3645. (defvar org-latex-and-specials-regexp nil
  3646. "Regular expression for highlighting export special stuff.")
  3647. (defvar org-match-substring-regexp)
  3648. (defvar org-match-substring-with-braces-regexp)
  3649. (defvar org-export-html-special-string-regexps)
  3650. (defun org-compute-latex-and-specials-regexp ()
  3651. "Compute regular expression for stuff treated specially by exporters."
  3652. (if (not org-highlight-latex-fragments-and-specials)
  3653. (org-set-local 'org-latex-and-specials-regexp nil)
  3654. (require 'org-exp)
  3655. (let*
  3656. ((matchers (plist-get org-format-latex-options :matchers))
  3657. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3658. org-latex-regexps)))
  3659. (options (org-combine-plists (org-default-export-plist)
  3660. (org-infile-export-plist)))
  3661. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3662. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3663. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3664. (org-export-html-expand (plist-get options :expand-quoted-html))
  3665. (org-export-with-special-strings (plist-get options :special-strings))
  3666. (re-sub
  3667. (cond
  3668. ((equal org-export-with-sub-superscripts '{})
  3669. (list org-match-substring-with-braces-regexp))
  3670. (org-export-with-sub-superscripts
  3671. (list org-match-substring-regexp))
  3672. (t nil)))
  3673. (re-latex
  3674. (if org-export-with-LaTeX-fragments
  3675. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3676. (re-macros
  3677. (if org-export-with-TeX-macros
  3678. (list (concat "\\\\"
  3679. (regexp-opt
  3680. (append (mapcar 'car org-html-entities)
  3681. (if (boundp 'org-latex-entities)
  3682. org-latex-entities nil))
  3683. 'words))) ; FIXME
  3684. ))
  3685. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3686. (re-special (if org-export-with-special-strings
  3687. (mapcar (lambda (x) (car x))
  3688. org-export-html-special-string-regexps)))
  3689. (re-rest
  3690. (delq nil
  3691. (list
  3692. (if org-export-html-expand "@<[^>\n]+>")
  3693. ))))
  3694. (org-set-local
  3695. 'org-latex-and-specials-regexp
  3696. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3697. re-rest) "\\|")))))
  3698. (defun org-do-latex-and-special-faces (limit)
  3699. "Run through the buffer and add overlays to links."
  3700. (when org-latex-and-specials-regexp
  3701. (let (rtn d)
  3702. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3703. limit t))
  3704. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3705. 'face))
  3706. '(org-code org-verbatim underline)))
  3707. (progn
  3708. (setq rtn t
  3709. d (cond ((member (char-after (1+ (match-beginning 0)))
  3710. '(?_ ?^)) 1)
  3711. (t 0)))
  3712. (font-lock-prepend-text-property
  3713. (+ d (match-beginning 0)) (match-end 0)
  3714. 'face 'org-latex-and-export-specials)
  3715. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3716. '(font-lock-multiline t)))))
  3717. rtn)))
  3718. (defun org-restart-font-lock ()
  3719. "Restart font-lock-mode, to force refontification."
  3720. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3721. (font-lock-mode -1)
  3722. (font-lock-mode 1)))
  3723. (defun org-all-targets (&optional radio)
  3724. "Return a list of all targets in this file.
  3725. With optional argument RADIO, only find radio targets."
  3726. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3727. rtn)
  3728. (save-excursion
  3729. (goto-char (point-min))
  3730. (while (re-search-forward re nil t)
  3731. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3732. rtn)))
  3733. (defun org-make-target-link-regexp (targets)
  3734. "Make regular expression matching all strings in TARGETS.
  3735. The regular expression finds the targets also if there is a line break
  3736. between words."
  3737. (and targets
  3738. (concat
  3739. "\\<\\("
  3740. (mapconcat
  3741. (lambda (x)
  3742. (while (string-match " +" x)
  3743. (setq x (replace-match "\\s-+" t t x)))
  3744. x)
  3745. targets
  3746. "\\|")
  3747. "\\)\\>")))
  3748. (defun org-activate-tags (limit)
  3749. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3750. (progn
  3751. (add-text-properties (match-beginning 1) (match-end 1)
  3752. (list 'mouse-face 'highlight
  3753. 'rear-nonsticky org-nonsticky-props
  3754. 'keymap org-mouse-map))
  3755. t)))
  3756. (defun org-outline-level ()
  3757. (save-excursion
  3758. (looking-at outline-regexp)
  3759. (if (match-beginning 1)
  3760. (+ (org-get-string-indentation (match-string 1)) 1000)
  3761. (1- (- (match-end 0) (match-beginning 0))))))
  3762. (defvar org-font-lock-keywords nil)
  3763. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3764. "Regular expression matching a property line.")
  3765. (defvar org-font-lock-hook nil
  3766. "Functions to be called for special font lock stuff.")
  3767. (defun org-font-lock-hook (limit)
  3768. (run-hook-with-args 'org-font-lock-hook limit))
  3769. (defun org-set-font-lock-defaults ()
  3770. (let* ((em org-fontify-emphasized-text)
  3771. (lk org-activate-links)
  3772. (org-font-lock-extra-keywords
  3773. (list
  3774. ;; Call the hook
  3775. '(org-font-lock-hook)
  3776. ;; Headlines
  3777. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3778. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3779. ;; Table lines
  3780. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3781. (1 'org-table t))
  3782. ;; Table internals
  3783. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3784. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3785. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3786. ;; Drawers
  3787. (list org-drawer-regexp '(0 'org-special-keyword t))
  3788. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3789. ;; Properties
  3790. (list org-property-re
  3791. '(1 'org-special-keyword t)
  3792. '(3 'org-property-value t))
  3793. (if org-format-transports-properties-p
  3794. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3795. ;; Links
  3796. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3797. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3798. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3799. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3800. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3801. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3802. (if (memq 'footnote lk) '(org-activate-footnote-links
  3803. (2 'org-footnote t)))
  3804. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3805. '(org-hide-wide-columns (0 nil append))
  3806. ;; TODO lines
  3807. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3808. '(1 (org-get-todo-face 1) t))
  3809. ;; DONE
  3810. (if org-fontify-done-headline
  3811. (list (concat "^[*]+ +\\<\\("
  3812. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3813. "\\)\\(.*\\)")
  3814. '(2 'org-headline-done t))
  3815. nil)
  3816. ;; Priorities
  3817. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3818. ;; Tags
  3819. '(org-font-lock-add-tag-faces)
  3820. ;; Special keywords
  3821. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3822. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3823. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3824. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3825. ;; Emphasis
  3826. (if em
  3827. (if (featurep 'xemacs)
  3828. '(org-do-emphasis-faces (0 nil append))
  3829. '(org-do-emphasis-faces)))
  3830. ;; Checkboxes
  3831. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3832. 2 'bold prepend)
  3833. (if org-provide-checkbox-statistics
  3834. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3835. (0 (org-get-checkbox-statistics-face) t)))
  3836. ;; Description list items
  3837. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3838. 2 'bold prepend)
  3839. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3840. '(1 'org-archived prepend))
  3841. ;; Specials
  3842. '(org-do-latex-and-special-faces)
  3843. ;; Code
  3844. '(org-activate-code (1 'org-code t))
  3845. ;; COMMENT
  3846. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3847. "\\|" org-quote-string "\\)\\>")
  3848. '(1 'org-special-keyword t))
  3849. '("^#.*" (0 'font-lock-comment-face t))
  3850. )))
  3851. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3852. ;; Now set the full font-lock-keywords
  3853. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3854. (org-set-local 'font-lock-defaults
  3855. '(org-font-lock-keywords t nil nil backward-paragraph))
  3856. (kill-local-variable 'font-lock-keywords) nil))
  3857. (defvar org-m nil)
  3858. (defvar org-l nil)
  3859. (defvar org-f nil)
  3860. (defun org-get-level-face (n)
  3861. "Get the right face for match N in font-lock matching of headlines."
  3862. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3863. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3864. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3865. (cond
  3866. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3867. ((eq n 2) org-f)
  3868. (t (if org-level-color-stars-only nil org-f))))
  3869. (defun org-get-todo-face (kwd)
  3870. "Get the right face for a TODO keyword KWD.
  3871. If KWD is a number, get the corresponding match group."
  3872. (if (numberp kwd) (setq kwd (match-string kwd)))
  3873. (or (cdr (assoc kwd org-todo-keyword-faces))
  3874. (and (member kwd org-done-keywords) 'org-done)
  3875. 'org-todo))
  3876. (defun org-font-lock-add-tag-faces (limit)
  3877. "Add the special tag faces."
  3878. (when (and org-tag-faces org-tags-special-faces-re)
  3879. (while (re-search-forward org-tags-special-faces-re limit t)
  3880. (add-text-properties (match-beginning 1) (match-end 1)
  3881. (list 'face (org-get-tag-face 1)
  3882. 'font-lock-fontified t))
  3883. (backward-char 1))))
  3884. (defun org-get-tag-face (kwd)
  3885. "Get the right face for a TODO keyword KWD.
  3886. If KWD is a number, get the corresponding match group."
  3887. (if (numberp kwd) (setq kwd (match-string kwd)))
  3888. (or (cdr (assoc kwd org-tag-faces))
  3889. 'org-tag))
  3890. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3891. "Remove fontification and activation overlays from links."
  3892. (font-lock-default-unfontify-region beg end)
  3893. (let* ((buffer-undo-list t)
  3894. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3895. (inhibit-modification-hooks t)
  3896. deactivate-mark buffer-file-name buffer-file-truename)
  3897. (remove-text-properties beg end
  3898. '(mouse-face t keymap t org-linked-text t
  3899. invisible t intangible t))))
  3900. ;;;; Visibility cycling, including org-goto and indirect buffer
  3901. ;;; Cycling
  3902. (defvar org-cycle-global-status nil)
  3903. (make-variable-buffer-local 'org-cycle-global-status)
  3904. (defvar org-cycle-subtree-status nil)
  3905. (make-variable-buffer-local 'org-cycle-subtree-status)
  3906. ;;;###autoload
  3907. (defun org-cycle (&optional arg)
  3908. "Visibility cycling for Org-mode.
  3909. - When this function is called with a prefix argument, rotate the entire
  3910. buffer through 3 states (global cycling)
  3911. 1. OVERVIEW: Show only top-level headlines.
  3912. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3913. 3. SHOW ALL: Show everything.
  3914. When called with two C-u C-u prefixes, switch to the startup visibility,
  3915. determined by the variable `org-startup-folded', and by any VISIBILITY
  3916. properties in the buffer.
  3917. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3918. including drawers.
  3919. - When point is at the beginning of a headline, rotate the subtree started
  3920. by this line through 3 different states (local cycling)
  3921. 1. FOLDED: Only the main headline is shown.
  3922. 2. CHILDREN: The main headline and the direct children are shown.
  3923. From this state, you can move to one of the children
  3924. and zoom in further.
  3925. 3. SUBTREE: Show the entire subtree, including body text.
  3926. - When there is a numeric prefix, go up to a heading with level ARG, do
  3927. a `show-subtree' and return to the previous cursor position. If ARG
  3928. is negative, go up that many levels.
  3929. - When point is not at the beginning of a headline, execute the global
  3930. binding for TAB, which is re-indenting the line. See the option
  3931. `org-cycle-emulate-tab' for details.
  3932. - Special case: if point is at the beginning of the buffer and there is
  3933. no headline in line 1, this function will act as if called with prefix arg.
  3934. But only if also the variable `org-cycle-global-at-bob' is t."
  3935. (interactive "P")
  3936. (org-load-modules-maybe)
  3937. (let* ((outline-regexp
  3938. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3939. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3940. outline-regexp))
  3941. (bob-special (and org-cycle-global-at-bob (bobp)
  3942. (not (looking-at outline-regexp))))
  3943. (org-cycle-hook
  3944. (if bob-special
  3945. (delq 'org-optimize-window-after-visibility-change
  3946. (copy-sequence org-cycle-hook))
  3947. org-cycle-hook))
  3948. (pos (point)))
  3949. (if (or bob-special (equal arg '(4)))
  3950. ;; special case: use global cycling
  3951. (setq arg t))
  3952. (cond
  3953. ((equal arg '(16))
  3954. (org-set-startup-visibility)
  3955. (message "Startup visibility, plus VISIBILITY properties"))
  3956. ((equal arg '(64))
  3957. (show-all)
  3958. (message "Entire buffer visible, including drawers"))
  3959. ((org-at-table-p 'any)
  3960. ;; Enter the table or move to the next field in the table
  3961. (or (org-table-recognize-table.el)
  3962. (progn
  3963. (if arg (org-table-edit-field t)
  3964. (org-table-justify-field-maybe)
  3965. (call-interactively 'org-table-next-field)))))
  3966. ((eq arg t) ;; Global cycling
  3967. (cond
  3968. ((and (eq last-command this-command)
  3969. (eq org-cycle-global-status 'overview))
  3970. ;; We just created the overview - now do table of contents
  3971. ;; This can be slow in very large buffers, so indicate action
  3972. (message "CONTENTS...")
  3973. (org-content)
  3974. (message "CONTENTS...done")
  3975. (setq org-cycle-global-status 'contents)
  3976. (run-hook-with-args 'org-cycle-hook 'contents))
  3977. ((and (eq last-command this-command)
  3978. (eq org-cycle-global-status 'contents))
  3979. ;; We just showed the table of contents - now show everything
  3980. (show-all)
  3981. (message "SHOW ALL")
  3982. (setq org-cycle-global-status 'all)
  3983. (run-hook-with-args 'org-cycle-hook 'all))
  3984. (t
  3985. ;; Default action: go to overview
  3986. (org-overview)
  3987. (message "OVERVIEW")
  3988. (setq org-cycle-global-status 'overview)
  3989. (run-hook-with-args 'org-cycle-hook 'overview))))
  3990. ((and org-drawers org-drawer-regexp
  3991. (save-excursion
  3992. (beginning-of-line 1)
  3993. (looking-at org-drawer-regexp)))
  3994. ;; Toggle block visibility
  3995. (org-flag-drawer
  3996. (not (get-char-property (match-end 0) 'invisible))))
  3997. ((integerp arg)
  3998. ;; Show-subtree, ARG levels up from here.
  3999. (save-excursion
  4000. (org-back-to-heading)
  4001. (outline-up-heading (if (< arg 0) (- arg)
  4002. (- (funcall outline-level) arg)))
  4003. (org-show-subtree)))
  4004. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  4005. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  4006. ;; At a heading: rotate between three different views
  4007. (org-back-to-heading)
  4008. (let ((goal-column 0) eoh eol eos)
  4009. ;; First, some boundaries
  4010. (save-excursion
  4011. (org-back-to-heading)
  4012. (save-excursion
  4013. (beginning-of-line 2)
  4014. (while (and (not (eobp)) ;; this is like `next-line'
  4015. (get-char-property (1- (point)) 'invisible))
  4016. (beginning-of-line 2)) (setq eol (point)))
  4017. (outline-end-of-heading) (setq eoh (point))
  4018. (org-end-of-subtree t)
  4019. (unless (eobp)
  4020. (skip-chars-forward " \t\n")
  4021. (beginning-of-line 1) ; in case this is an item
  4022. )
  4023. (setq eos (1- (point))))
  4024. ;; Find out what to do next and set `this-command'
  4025. (cond
  4026. ((= eos eoh)
  4027. ;; Nothing is hidden behind this heading
  4028. (message "EMPTY ENTRY")
  4029. (setq org-cycle-subtree-status nil)
  4030. (save-excursion
  4031. (goto-char eos)
  4032. (outline-next-heading)
  4033. (if (org-invisible-p) (org-flag-heading nil))))
  4034. ((or (>= eol eos)
  4035. (not (string-match "\\S-" (buffer-substring eol eos))))
  4036. ;; Entire subtree is hidden in one line: open it
  4037. (org-show-entry)
  4038. (show-children)
  4039. (message "CHILDREN")
  4040. (save-excursion
  4041. (goto-char eos)
  4042. (outline-next-heading)
  4043. (if (org-invisible-p) (org-flag-heading nil)))
  4044. (setq org-cycle-subtree-status 'children)
  4045. (run-hook-with-args 'org-cycle-hook 'children))
  4046. ((and (eq last-command this-command)
  4047. (eq org-cycle-subtree-status 'children))
  4048. ;; We just showed the children, now show everything.
  4049. (org-show-subtree)
  4050. (message "SUBTREE")
  4051. (setq org-cycle-subtree-status 'subtree)
  4052. (run-hook-with-args 'org-cycle-hook 'subtree))
  4053. (t
  4054. ;; Default action: hide the subtree.
  4055. (hide-subtree)
  4056. (message "FOLDED")
  4057. (setq org-cycle-subtree-status 'folded)
  4058. (run-hook-with-args 'org-cycle-hook 'folded)))))
  4059. ;; TAB emulation and template completion
  4060. (buffer-read-only (org-back-to-heading))
  4061. ((org-try-structure-completion))
  4062. ((org-try-cdlatex-tab))
  4063. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  4064. (or (not (bolp))
  4065. (not (looking-at outline-regexp))))
  4066. (call-interactively (global-key-binding "\t")))
  4067. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  4068. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  4069. (or (and (eq org-cycle-emulate-tab 'white)
  4070. (= (match-end 0) (point-at-eol)))
  4071. (and (eq org-cycle-emulate-tab 'whitestart)
  4072. (>= (match-end 0) pos))))
  4073. t
  4074. (eq org-cycle-emulate-tab t))
  4075. (call-interactively (global-key-binding "\t")))
  4076. (t (save-excursion
  4077. (org-back-to-heading)
  4078. (org-cycle))))))
  4079. ;;;###autoload
  4080. (defun org-global-cycle (&optional arg)
  4081. "Cycle the global visibility. For details see `org-cycle'.
  4082. With C-u prefix arg, switch to startup visibility.
  4083. With a numeric prefix, show all headlines up to that level."
  4084. (interactive "P")
  4085. (let ((org-cycle-include-plain-lists
  4086. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  4087. (cond
  4088. ((integerp arg)
  4089. (show-all)
  4090. (hide-sublevels arg)
  4091. (setq org-cycle-global-status 'contents))
  4092. ((equal arg '(4))
  4093. (org-set-startup-visibility)
  4094. (message "Startup visibility, plus VISIBILITY properties."))
  4095. (t
  4096. (org-cycle '(4))))))
  4097. (defun org-set-startup-visibility ()
  4098. "Set the visibility required by startup options and properties."
  4099. (cond
  4100. ((eq org-startup-folded t)
  4101. (org-cycle '(4)))
  4102. ((eq org-startup-folded 'content)
  4103. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  4104. (org-cycle '(4)) (org-cycle '(4)))))
  4105. (org-set-visibility-according-to-property 'no-cleanup)
  4106. (org-cycle-hide-archived-subtrees 'all)
  4107. (org-cycle-hide-drawers 'all)
  4108. (org-cycle-show-empty-lines 'all))
  4109. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  4110. "Switch subtree visibilities according to :VISIBILITY: property."
  4111. (interactive)
  4112. (let (org-show-entry-below state)
  4113. (save-excursion
  4114. (goto-char (point-min))
  4115. (while (re-search-forward
  4116. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  4117. nil t)
  4118. (setq state (match-string 1))
  4119. (save-excursion
  4120. (org-back-to-heading t)
  4121. (hide-subtree)
  4122. (org-reveal)
  4123. (cond
  4124. ((equal state '("fold" "folded"))
  4125. (hide-subtree))
  4126. ((equal state "children")
  4127. (org-show-hidden-entry)
  4128. (show-children))
  4129. ((equal state "content")
  4130. (save-excursion
  4131. (save-restriction
  4132. (org-narrow-to-subtree)
  4133. (org-content))))
  4134. ((member state '("all" "showall"))
  4135. (show-subtree)))))
  4136. (unless no-cleanup
  4137. (org-cycle-hide-archived-subtrees 'all)
  4138. (org-cycle-hide-drawers 'all)
  4139. (org-cycle-show-empty-lines 'all)))))
  4140. (defun org-overview ()
  4141. "Switch to overview mode, showing only top-level headlines.
  4142. Really, this shows all headlines with level equal or greater than the level
  4143. of the first headline in the buffer. This is important, because if the
  4144. first headline is not level one, then (hide-sublevels 1) gives confusing
  4145. results."
  4146. (interactive)
  4147. (let ((level (save-excursion
  4148. (goto-char (point-min))
  4149. (if (re-search-forward (concat "^" outline-regexp) nil t)
  4150. (progn
  4151. (goto-char (match-beginning 0))
  4152. (funcall outline-level))))))
  4153. (and level (hide-sublevels level))))
  4154. (defun org-content (&optional arg)
  4155. "Show all headlines in the buffer, like a table of contents.
  4156. With numerical argument N, show content up to level N."
  4157. (interactive "P")
  4158. (save-excursion
  4159. ;; Visit all headings and show their offspring
  4160. (and (integerp arg) (org-overview))
  4161. (goto-char (point-max))
  4162. (catch 'exit
  4163. (while (and (progn (condition-case nil
  4164. (outline-previous-visible-heading 1)
  4165. (error (goto-char (point-min))))
  4166. t)
  4167. (looking-at outline-regexp))
  4168. (if (integerp arg)
  4169. (show-children (1- arg))
  4170. (show-branches))
  4171. (if (bobp) (throw 'exit nil))))))
  4172. (defun org-optimize-window-after-visibility-change (state)
  4173. "Adjust the window after a change in outline visibility.
  4174. This function is the default value of the hook `org-cycle-hook'."
  4175. (when (get-buffer-window (current-buffer))
  4176. (cond
  4177. ((eq state 'content) nil)
  4178. ((eq state 'all) nil)
  4179. ((eq state 'folded) nil)
  4180. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  4181. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  4182. (defun org-compact-display-after-subtree-move ()
  4183. "Show a compacter version of the tree of the entry's parent."
  4184. (save-excursion
  4185. (if (org-up-heading-safe)
  4186. (progn
  4187. (hide-subtree)
  4188. (show-entry)
  4189. (show-children)
  4190. (org-cycle-show-empty-lines 'children)
  4191. (org-cycle-hide-drawers 'children))
  4192. (org-overview))))
  4193. (defun org-cycle-show-empty-lines (state)
  4194. "Show empty lines above all visible headlines.
  4195. The region to be covered depends on STATE when called through
  4196. `org-cycle-hook'. Lisp program can use t for STATE to get the
  4197. entire buffer covered. Note that an empty line is only shown if there
  4198. are at least `org-cycle-separator-lines' empty lines before the headline."
  4199. (when (> org-cycle-separator-lines 0)
  4200. (save-excursion
  4201. (let* ((n org-cycle-separator-lines)
  4202. (re (cond
  4203. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  4204. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  4205. (t (let ((ns (number-to-string (- n 2))))
  4206. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  4207. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  4208. beg end)
  4209. (cond
  4210. ((memq state '(overview contents t))
  4211. (setq beg (point-min) end (point-max)))
  4212. ((memq state '(children folded))
  4213. (setq beg (point) end (progn (org-end-of-subtree t t)
  4214. (beginning-of-line 2)
  4215. (point)))))
  4216. (when beg
  4217. (goto-char beg)
  4218. (while (re-search-forward re end t)
  4219. (if (not (get-char-property (match-end 1) 'invisible))
  4220. (outline-flag-region
  4221. (match-beginning 1) (match-end 1) nil)))))))
  4222. ;; Never hide empty lines at the end of the file.
  4223. (save-excursion
  4224. (goto-char (point-max))
  4225. (outline-previous-heading)
  4226. (outline-end-of-heading)
  4227. (if (and (looking-at "[ \t\n]+")
  4228. (= (match-end 0) (point-max)))
  4229. (outline-flag-region (point) (match-end 0) nil))))
  4230. (defun org-show-empty-lines-in-parent ()
  4231. "Move to the parent and re-show empty lines before visible headlines."
  4232. (save-excursion
  4233. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4234. (org-cycle-show-empty-lines context))))
  4235. (defun org-cycle-hide-drawers (state)
  4236. "Re-hide all drawers after a visibility state change."
  4237. (when (and (org-mode-p)
  4238. (not (memq state '(overview folded))))
  4239. (save-excursion
  4240. (let* ((globalp (memq state '(contents all)))
  4241. (beg (if globalp (point-min) (point)))
  4242. (end (if globalp (point-max) (org-end-of-subtree t))))
  4243. (goto-char beg)
  4244. (while (re-search-forward org-drawer-regexp end t)
  4245. (org-flag-drawer t))))))
  4246. (defun org-flag-drawer (flag)
  4247. (save-excursion
  4248. (beginning-of-line 1)
  4249. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4250. (let ((b (match-end 0))
  4251. (outline-regexp org-outline-regexp))
  4252. (if (re-search-forward
  4253. "^[ \t]*:END:"
  4254. (save-excursion (outline-next-heading) (point)) t)
  4255. (outline-flag-region b (point-at-eol) flag)
  4256. (error ":END: line missing"))))))
  4257. (defun org-subtree-end-visible-p ()
  4258. "Is the end of the current subtree visible?"
  4259. (pos-visible-in-window-p
  4260. (save-excursion (org-end-of-subtree t) (point))))
  4261. (defun org-first-headline-recenter (&optional N)
  4262. "Move cursor to the first headline and recenter the headline.
  4263. Optional argument N means, put the headline into the Nth line of the window."
  4264. (goto-char (point-min))
  4265. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4266. (beginning-of-line)
  4267. (recenter (prefix-numeric-value N))))
  4268. ;;; Org-goto
  4269. (defvar org-goto-window-configuration nil)
  4270. (defvar org-goto-marker nil)
  4271. (defvar org-goto-map
  4272. (let ((map (make-sparse-keymap)))
  4273. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4274. (while (setq cmd (pop cmds))
  4275. (substitute-key-definition cmd cmd map global-map)))
  4276. (suppress-keymap map)
  4277. (org-defkey map "\C-m" 'org-goto-ret)
  4278. (org-defkey map [(return)] 'org-goto-ret)
  4279. (org-defkey map [(left)] 'org-goto-left)
  4280. (org-defkey map [(right)] 'org-goto-right)
  4281. (org-defkey map [(control ?g)] 'org-goto-quit)
  4282. (org-defkey map "\C-i" 'org-cycle)
  4283. (org-defkey map [(tab)] 'org-cycle)
  4284. (org-defkey map [(down)] 'outline-next-visible-heading)
  4285. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4286. (if org-goto-auto-isearch
  4287. (if (fboundp 'define-key-after)
  4288. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4289. nil)
  4290. (org-defkey map "q" 'org-goto-quit)
  4291. (org-defkey map "n" 'outline-next-visible-heading)
  4292. (org-defkey map "p" 'outline-previous-visible-heading)
  4293. (org-defkey map "f" 'outline-forward-same-level)
  4294. (org-defkey map "b" 'outline-backward-same-level)
  4295. (org-defkey map "u" 'outline-up-heading))
  4296. (org-defkey map "/" 'org-occur)
  4297. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4298. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4299. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4300. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4301. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4302. map))
  4303. (defconst org-goto-help
  4304. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4305. RET=jump to location [Q]uit and return to previous location
  4306. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4307. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4308. ;; FIXME: Docstring doe not mention both interfaces
  4309. (defun org-goto (&optional alternative-interface)
  4310. "Look up a different location in the current file, keeping current visibility.
  4311. When you want look-up or go to a different location in a document, the
  4312. fastest way is often to fold the entire buffer and then dive into the tree.
  4313. This method has the disadvantage, that the previous location will be folded,
  4314. which may not be what you want.
  4315. This command works around this by showing a copy of the current buffer
  4316. in an indirect buffer, in overview mode. You can dive into the tree in
  4317. that copy, use org-occur and incremental search to find a location.
  4318. When pressing RET or `Q', the command returns to the original buffer in
  4319. which the visibility is still unchanged. After RET is will also jump to
  4320. the location selected in the indirect buffer and expose the
  4321. the headline hierarchy above."
  4322. (interactive "P")
  4323. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4324. (org-refile-use-outline-path t)
  4325. (interface
  4326. (if (not alternative-interface)
  4327. org-goto-interface
  4328. (if (eq org-goto-interface 'outline)
  4329. 'outline-path-completion
  4330. 'outline)))
  4331. (org-goto-start-pos (point))
  4332. (selected-point
  4333. (if (eq interface 'outline)
  4334. (car (org-get-location (current-buffer) org-goto-help))
  4335. (nth 3 (org-refile-get-location "Goto: ")))))
  4336. (if selected-point
  4337. (progn
  4338. (org-mark-ring-push org-goto-start-pos)
  4339. (goto-char selected-point)
  4340. (if (or (org-invisible-p) (org-invisible-p2))
  4341. (org-show-context 'org-goto)))
  4342. (message "Quit"))))
  4343. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4344. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4345. (defvar org-goto-local-auto-isearch-map) ; defined below
  4346. (defun org-get-location (buf help)
  4347. "Let the user select a location in the Org-mode buffer BUF.
  4348. This function uses a recursive edit. It returns the selected position
  4349. or nil."
  4350. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4351. (isearch-hide-immediately nil)
  4352. (isearch-search-fun-function
  4353. (lambda () 'org-goto-local-search-headings))
  4354. (org-goto-selected-point org-goto-exit-command))
  4355. (save-excursion
  4356. (save-window-excursion
  4357. (delete-other-windows)
  4358. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4359. (switch-to-buffer
  4360. (condition-case nil
  4361. (make-indirect-buffer (current-buffer) "*org-goto*")
  4362. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4363. (with-output-to-temp-buffer "*Help*"
  4364. (princ help))
  4365. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4366. (setq buffer-read-only nil)
  4367. (let ((org-startup-truncated t)
  4368. (org-startup-folded nil)
  4369. (org-startup-align-all-tables nil))
  4370. (org-mode)
  4371. (org-overview))
  4372. (setq buffer-read-only t)
  4373. (if (and (boundp 'org-goto-start-pos)
  4374. (integer-or-marker-p org-goto-start-pos))
  4375. (let ((org-show-hierarchy-above t)
  4376. (org-show-siblings t)
  4377. (org-show-following-heading t))
  4378. (goto-char org-goto-start-pos)
  4379. (and (org-invisible-p) (org-show-context)))
  4380. (goto-char (point-min)))
  4381. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  4382. (message "Select location and press RET")
  4383. (use-local-map org-goto-map)
  4384. (recursive-edit)
  4385. ))
  4386. (kill-buffer "*org-goto*")
  4387. (cons org-goto-selected-point org-goto-exit-command)))
  4388. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4389. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4390. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4391. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4392. (defun org-goto-local-search-headings (string bound noerror)
  4393. "Search and make sure that any matches are in headlines."
  4394. (catch 'return
  4395. (while (if isearch-forward
  4396. (search-forward string bound noerror)
  4397. (search-backward string bound noerror))
  4398. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4399. (and (member :headline context)
  4400. (not (member :tags context))))
  4401. (throw 'return (point))))))
  4402. (defun org-goto-local-auto-isearch ()
  4403. "Start isearch."
  4404. (interactive)
  4405. (goto-char (point-min))
  4406. (let ((keys (this-command-keys)))
  4407. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4408. (isearch-mode t)
  4409. (isearch-process-search-char (string-to-char keys)))))
  4410. (defun org-goto-ret (&optional arg)
  4411. "Finish `org-goto' by going to the new location."
  4412. (interactive "P")
  4413. (setq org-goto-selected-point (point)
  4414. org-goto-exit-command 'return)
  4415. (throw 'exit nil))
  4416. (defun org-goto-left ()
  4417. "Finish `org-goto' by going to the new location."
  4418. (interactive)
  4419. (if (org-on-heading-p)
  4420. (progn
  4421. (beginning-of-line 1)
  4422. (setq org-goto-selected-point (point)
  4423. org-goto-exit-command 'left)
  4424. (throw 'exit nil))
  4425. (error "Not on a heading")))
  4426. (defun org-goto-right ()
  4427. "Finish `org-goto' by going to the new location."
  4428. (interactive)
  4429. (if (org-on-heading-p)
  4430. (progn
  4431. (setq org-goto-selected-point (point)
  4432. org-goto-exit-command 'right)
  4433. (throw 'exit nil))
  4434. (error "Not on a heading")))
  4435. (defun org-goto-quit ()
  4436. "Finish `org-goto' without cursor motion."
  4437. (interactive)
  4438. (setq org-goto-selected-point nil)
  4439. (setq org-goto-exit-command 'quit)
  4440. (throw 'exit nil))
  4441. ;;; Indirect buffer display of subtrees
  4442. (defvar org-indirect-dedicated-frame nil
  4443. "This is the frame being used for indirect tree display.")
  4444. (defvar org-last-indirect-buffer nil)
  4445. (defun org-tree-to-indirect-buffer (&optional arg)
  4446. "Create indirect buffer and narrow it to current subtree.
  4447. With numerical prefix ARG, go up to this level and then take that tree.
  4448. If ARG is negative, go up that many levels.
  4449. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4450. indirect buffer previously made with this command, to avoid proliferation of
  4451. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4452. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4453. is kept so that you can work with several indirect buffers at the same time.
  4454. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4455. requests that a new frame be made for the new buffer, so that the dedicated
  4456. frame is not changed."
  4457. (interactive "P")
  4458. (let ((cbuf (current-buffer))
  4459. (cwin (selected-window))
  4460. (pos (point))
  4461. beg end level heading ibuf)
  4462. (save-excursion
  4463. (org-back-to-heading t)
  4464. (when (numberp arg)
  4465. (setq level (org-outline-level))
  4466. (if (< arg 0) (setq arg (+ level arg)))
  4467. (while (> (setq level (org-outline-level)) arg)
  4468. (outline-up-heading 1 t)))
  4469. (setq beg (point)
  4470. heading (org-get-heading))
  4471. (org-end-of-subtree t) (setq end (point)))
  4472. (if (and (buffer-live-p org-last-indirect-buffer)
  4473. (not (eq org-indirect-buffer-display 'new-frame))
  4474. (not arg))
  4475. (kill-buffer org-last-indirect-buffer))
  4476. (setq ibuf (org-get-indirect-buffer cbuf)
  4477. org-last-indirect-buffer ibuf)
  4478. (cond
  4479. ((or (eq org-indirect-buffer-display 'new-frame)
  4480. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4481. (select-frame (make-frame))
  4482. (delete-other-windows)
  4483. (switch-to-buffer ibuf)
  4484. (org-set-frame-title heading))
  4485. ((eq org-indirect-buffer-display 'dedicated-frame)
  4486. (raise-frame
  4487. (select-frame (or (and org-indirect-dedicated-frame
  4488. (frame-live-p org-indirect-dedicated-frame)
  4489. org-indirect-dedicated-frame)
  4490. (setq org-indirect-dedicated-frame (make-frame)))))
  4491. (delete-other-windows)
  4492. (switch-to-buffer ibuf)
  4493. (org-set-frame-title (concat "Indirect: " heading)))
  4494. ((eq org-indirect-buffer-display 'current-window)
  4495. (switch-to-buffer ibuf))
  4496. ((eq org-indirect-buffer-display 'other-window)
  4497. (pop-to-buffer ibuf))
  4498. (t (error "Invalid value.")))
  4499. (if (featurep 'xemacs)
  4500. (save-excursion (org-mode) (turn-on-font-lock)))
  4501. (narrow-to-region beg end)
  4502. (show-all)
  4503. (goto-char pos)
  4504. (and (window-live-p cwin) (select-window cwin))))
  4505. (defun org-get-indirect-buffer (&optional buffer)
  4506. (setq buffer (or buffer (current-buffer)))
  4507. (let ((n 1) (base (buffer-name buffer)) bname)
  4508. (while (buffer-live-p
  4509. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4510. (setq n (1+ n)))
  4511. (condition-case nil
  4512. (make-indirect-buffer buffer bname 'clone)
  4513. (error (make-indirect-buffer buffer bname)))))
  4514. (defun org-set-frame-title (title)
  4515. "Set the title of the current frame to the string TITLE."
  4516. ;; FIXME: how to name a single frame in XEmacs???
  4517. (unless (featurep 'xemacs)
  4518. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4519. ;;;; Structure editing
  4520. ;;; Inserting headlines
  4521. (defun org-previous-line-empty-p ()
  4522. (save-excursion
  4523. (and (not (bobp))
  4524. (or (beginning-of-line 0) t)
  4525. (save-match-data
  4526. (looking-at "[ \t]*$")))))
  4527. (defun org-insert-heading (&optional force-heading)
  4528. "Insert a new heading or item with same depth at point.
  4529. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4530. If point is at the beginning of a headline, insert a sibling before the
  4531. current headline. If point is not at the beginning, do not split the line,
  4532. but create the new headline after the current line."
  4533. (interactive "P")
  4534. (if (= (buffer-size) 0)
  4535. (insert "\n* ")
  4536. (when (or force-heading (not (org-insert-item)))
  4537. (let* ((empty-line-p nil)
  4538. (head (save-excursion
  4539. (condition-case nil
  4540. (progn
  4541. (org-back-to-heading)
  4542. (setq empty-line-p (org-previous-line-empty-p))
  4543. (match-string 0))
  4544. (error "*"))))
  4545. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  4546. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  4547. pos hide-previous previous-pos)
  4548. (cond
  4549. ((and (org-on-heading-p) (bolp)
  4550. (or (bobp)
  4551. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4552. ;; insert before the current line
  4553. (open-line (if blank 2 1)))
  4554. ((and (bolp)
  4555. (or (bobp)
  4556. (save-excursion
  4557. (backward-char 1) (not (org-invisible-p)))))
  4558. ;; insert right here
  4559. nil)
  4560. (t
  4561. ;; somewhere in the line
  4562. (save-excursion
  4563. (setq previous-pos (point-at-bol))
  4564. (end-of-line)
  4565. (setq hide-previous (org-invisible-p)))
  4566. (and org-insert-heading-respect-content (org-show-subtree))
  4567. (let ((split
  4568. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4569. (save-excursion
  4570. (let ((p (point)))
  4571. (goto-char (point-at-bol))
  4572. (and (looking-at org-complex-heading-regexp)
  4573. (> p (match-beginning 4)))))))
  4574. tags pos)
  4575. (cond
  4576. (org-insert-heading-respect-content
  4577. (org-end-of-subtree nil t)
  4578. (or (bolp) (newline))
  4579. (or (org-previous-line-empty-p)
  4580. (and blank (newline)))
  4581. (open-line 1))
  4582. ((org-on-heading-p)
  4583. (when hide-previous
  4584. (show-children)
  4585. (org-show-entry))
  4586. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4587. (setq tags (and (match-end 2) (match-string 2)))
  4588. (and (match-end 1)
  4589. (delete-region (match-beginning 1) (match-end 1)))
  4590. (setq pos (point-at-bol))
  4591. (or split (end-of-line 1))
  4592. (delete-horizontal-space)
  4593. (newline (if blank 2 1))
  4594. (when tags
  4595. (save-excursion
  4596. (goto-char pos)
  4597. (end-of-line 1)
  4598. (insert " " tags)
  4599. (org-set-tags nil 'align))))
  4600. (t
  4601. (or split (end-of-line 1))
  4602. (newline (if blank 2 1)))))))
  4603. (insert head) (just-one-space)
  4604. (setq pos (point))
  4605. (end-of-line 1)
  4606. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4607. (when (and org-insert-heading-respect-content hide-previous)
  4608. (save-excursion
  4609. (goto-char previous-pos)
  4610. (hide-subtree)))
  4611. (run-hooks 'org-insert-heading-hook)))))
  4612. (defun org-get-heading (&optional no-tags)
  4613. "Return the heading of the current entry, without the stars."
  4614. (save-excursion
  4615. (org-back-to-heading t)
  4616. (if (looking-at
  4617. (if no-tags
  4618. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4619. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4620. (match-string 1) "")))
  4621. (defun org-heading-components ()
  4622. "Return the components of the current heading.
  4623. This is a list with the following elements:
  4624. - the level as an integer
  4625. - the reduced level, different if `org-odd-levels-only' is set.
  4626. - the TODO keyword, or nil
  4627. - the priority character, like ?A, or nil if no priority is given
  4628. - the headline text itself, or the tags string if no headline text
  4629. - the tags string, or nil."
  4630. (save-excursion
  4631. (org-back-to-heading t)
  4632. (if (looking-at org-complex-heading-regexp)
  4633. (list (length (match-string 1))
  4634. (org-reduced-level (length (match-string 1)))
  4635. (org-match-string-no-properties 2)
  4636. (and (match-end 3) (aref (match-string 3) 2))
  4637. (org-match-string-no-properties 4)
  4638. (org-match-string-no-properties 5)))))
  4639. (defun org-insert-heading-after-current ()
  4640. "Insert a new heading with same level as current, after current subtree."
  4641. (interactive)
  4642. (org-back-to-heading)
  4643. (org-insert-heading)
  4644. (org-move-subtree-down)
  4645. (end-of-line 1))
  4646. (defun org-insert-heading-respect-content ()
  4647. (interactive)
  4648. (let ((org-insert-heading-respect-content t))
  4649. (org-insert-heading t)))
  4650. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4651. (interactive "P")
  4652. (let ((org-insert-heading-respect-content t))
  4653. (org-insert-todo-heading force-state t)))
  4654. (defun org-insert-todo-heading (arg &optional force-heading)
  4655. "Insert a new heading with the same level and TODO state as current heading.
  4656. If the heading has no TODO state, or if the state is DONE, use the first
  4657. state (TODO by default). Also with prefix arg, force first state."
  4658. (interactive "P")
  4659. (when (or force-heading (not (org-insert-item 'checkbox)))
  4660. (org-insert-heading force-heading)
  4661. (save-excursion
  4662. (org-back-to-heading)
  4663. (outline-previous-heading)
  4664. (looking-at org-todo-line-regexp))
  4665. (let*
  4666. ((new-mark-x
  4667. (if (or arg
  4668. (not (match-beginning 2))
  4669. (member (match-string 2) org-done-keywords))
  4670. (car org-todo-keywords-1)
  4671. (match-string 2)))
  4672. (new-mark
  4673. (or
  4674. (run-hook-with-args-until-success
  4675. 'org-todo-get-default-hook new-mark-x nil)
  4676. new-mark-x)))
  4677. (insert new-mark " "))
  4678. (when org-provide-todo-statistics
  4679. (org-update-parent-todo-statistics))))
  4680. (defun org-insert-subheading (arg)
  4681. "Insert a new subheading and demote it.
  4682. Works for outline headings and for plain lists alike."
  4683. (interactive "P")
  4684. (org-insert-heading arg)
  4685. (cond
  4686. ((org-on-heading-p) (org-do-demote))
  4687. ((org-at-item-p) (org-indent-item 1))))
  4688. (defun org-insert-todo-subheading (arg)
  4689. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4690. Works for outline headings and for plain lists alike."
  4691. (interactive "P")
  4692. (org-insert-todo-heading arg)
  4693. (cond
  4694. ((org-on-heading-p) (org-do-demote))
  4695. ((org-at-item-p) (org-indent-item 1))))
  4696. ;;; Promotion and Demotion
  4697. (defun org-promote-subtree ()
  4698. "Promote the entire subtree.
  4699. See also `org-promote'."
  4700. (interactive)
  4701. (save-excursion
  4702. (org-map-tree 'org-promote))
  4703. (org-fix-position-after-promote))
  4704. (defun org-demote-subtree ()
  4705. "Demote the entire subtree. See `org-demote'.
  4706. See also `org-promote'."
  4707. (interactive)
  4708. (save-excursion
  4709. (org-map-tree 'org-demote))
  4710. (org-fix-position-after-promote))
  4711. (defun org-do-promote ()
  4712. "Promote the current heading higher up the tree.
  4713. If the region is active in `transient-mark-mode', promote all headings
  4714. in the region."
  4715. (interactive)
  4716. (save-excursion
  4717. (if (org-region-active-p)
  4718. (org-map-region 'org-promote (region-beginning) (region-end))
  4719. (org-promote)))
  4720. (org-fix-position-after-promote))
  4721. (defun org-do-demote ()
  4722. "Demote the current heading lower down the tree.
  4723. If the region is active in `transient-mark-mode', demote all headings
  4724. in the region."
  4725. (interactive)
  4726. (save-excursion
  4727. (if (org-region-active-p)
  4728. (org-map-region 'org-demote (region-beginning) (region-end))
  4729. (org-demote)))
  4730. (org-fix-position-after-promote))
  4731. (defun org-fix-position-after-promote ()
  4732. "Make sure that after pro/demotion cursor position is right."
  4733. (let ((pos (point)))
  4734. (when (save-excursion
  4735. (beginning-of-line 1)
  4736. (looking-at org-todo-line-regexp)
  4737. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4738. (cond ((eobp) (insert " "))
  4739. ((eolp) (insert " "))
  4740. ((equal (char-after) ?\ ) (forward-char 1))))))
  4741. (defun org-reduced-level (l)
  4742. "Compute the effective level of a heading.
  4743. This takes into account the setting of `org-odd-levels-only'."
  4744. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4745. (defun org-get-valid-level (level &optional change)
  4746. "Rectify a level change under the influence of `org-odd-levels-only'
  4747. LEVEL is a current level, CHANGE is by how much the level should be
  4748. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4749. even level numbers will become the next higher odd number."
  4750. (if org-odd-levels-only
  4751. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4752. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4753. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4754. (max 1 (+ level change))))
  4755. (if (boundp 'define-obsolete-function-alias)
  4756. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4757. (define-obsolete-function-alias 'org-get-legal-level
  4758. 'org-get-valid-level)
  4759. (define-obsolete-function-alias 'org-get-legal-level
  4760. 'org-get-valid-level "23.1")))
  4761. (defun org-promote ()
  4762. "Promote the current heading higher up the tree.
  4763. If the region is active in `transient-mark-mode', promote all headings
  4764. in the region."
  4765. (org-back-to-heading t)
  4766. (let* ((level (save-match-data (funcall outline-level)))
  4767. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4768. (diff (abs (- level (length up-head) -1))))
  4769. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4770. (replace-match up-head nil t)
  4771. ;; Fixup tag positioning
  4772. (and org-auto-align-tags (org-set-tags nil t))
  4773. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4774. (defun org-demote ()
  4775. "Demote the current heading lower down the tree.
  4776. If the region is active in `transient-mark-mode', demote all headings
  4777. in the region."
  4778. (org-back-to-heading t)
  4779. (let* ((level (save-match-data (funcall outline-level)))
  4780. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4781. (diff (abs (- level (length down-head) -1))))
  4782. (replace-match down-head nil t)
  4783. ;; Fixup tag positioning
  4784. (and org-auto-align-tags (org-set-tags nil t))
  4785. (if org-adapt-indentation (org-fixup-indentation diff))))
  4786. (defun org-map-tree (fun)
  4787. "Call FUN for every heading underneath the current one."
  4788. (org-back-to-heading)
  4789. (let ((level (funcall outline-level)))
  4790. (save-excursion
  4791. (funcall fun)
  4792. (while (and (progn
  4793. (outline-next-heading)
  4794. (> (funcall outline-level) level))
  4795. (not (eobp)))
  4796. (funcall fun)))))
  4797. (defun org-map-region (fun beg end)
  4798. "Call FUN for every heading between BEG and END."
  4799. (let ((org-ignore-region t))
  4800. (save-excursion
  4801. (setq end (copy-marker end))
  4802. (goto-char beg)
  4803. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4804. (< (point) end))
  4805. (funcall fun))
  4806. (while (and (progn
  4807. (outline-next-heading)
  4808. (< (point) end))
  4809. (not (eobp)))
  4810. (funcall fun)))))
  4811. (defun org-fixup-indentation (diff)
  4812. "Change the indentation in the current entry by DIFF
  4813. However, if any line in the current entry has no indentation, or if it
  4814. would end up with no indentation after the change, nothing at all is done."
  4815. (save-excursion
  4816. (let ((end (save-excursion (outline-next-heading)
  4817. (point-marker)))
  4818. (prohibit (if (> diff 0)
  4819. "^\\S-"
  4820. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4821. col)
  4822. (unless (save-excursion (end-of-line 1)
  4823. (re-search-forward prohibit end t))
  4824. (while (and (< (point) end)
  4825. (re-search-forward "^[ \t]+" end t))
  4826. (goto-char (match-end 0))
  4827. (setq col (current-column))
  4828. (if (< diff 0) (replace-match ""))
  4829. (org-indent-to-column (+ diff col))))
  4830. (move-marker end nil))))
  4831. (defun org-convert-to-odd-levels ()
  4832. "Convert an org-mode file with all levels allowed to one with odd levels.
  4833. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4834. level 5 etc."
  4835. (interactive)
  4836. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4837. (let ((org-odd-levels-only nil) n)
  4838. (save-excursion
  4839. (goto-char (point-min))
  4840. (while (re-search-forward "^\\*\\*+ " nil t)
  4841. (setq n (- (length (match-string 0)) 2))
  4842. (while (>= (setq n (1- n)) 0)
  4843. (org-demote))
  4844. (end-of-line 1))))))
  4845. (defun org-convert-to-oddeven-levels ()
  4846. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4847. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4848. section with an even level, conversion would destroy the structure of the file. An error
  4849. is signaled in this case."
  4850. (interactive)
  4851. (goto-char (point-min))
  4852. ;; First check if there are no even levels
  4853. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4854. (org-show-context t)
  4855. (error "Not all levels are odd in this file. Conversion not possible."))
  4856. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4857. (let ((org-odd-levels-only nil) n)
  4858. (save-excursion
  4859. (goto-char (point-min))
  4860. (while (re-search-forward "^\\*\\*+ " nil t)
  4861. (setq n (/ (1- (length (match-string 0))) 2))
  4862. (while (>= (setq n (1- n)) 0)
  4863. (org-promote))
  4864. (end-of-line 1))))))
  4865. (defun org-tr-level (n)
  4866. "Make N odd if required."
  4867. (if org-odd-levels-only (1+ (/ n 2)) n))
  4868. ;;; Vertical tree motion, cutting and pasting of subtrees
  4869. (defun org-move-subtree-up (&optional arg)
  4870. "Move the current subtree up past ARG headlines of the same level."
  4871. (interactive "p")
  4872. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4873. (defun org-move-subtree-down (&optional arg)
  4874. "Move the current subtree down past ARG headlines of the same level."
  4875. (interactive "p")
  4876. (setq arg (prefix-numeric-value arg))
  4877. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4878. 'outline-get-last-sibling))
  4879. (ins-point (make-marker))
  4880. (cnt (abs arg))
  4881. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4882. ;; Select the tree
  4883. (org-back-to-heading)
  4884. (setq beg0 (point))
  4885. (save-excursion
  4886. (setq ne-beg (org-back-over-empty-lines))
  4887. (setq beg (point)))
  4888. (save-match-data
  4889. (save-excursion (outline-end-of-heading)
  4890. (setq folded (org-invisible-p)))
  4891. (outline-end-of-subtree))
  4892. (outline-next-heading)
  4893. (setq ne-end (org-back-over-empty-lines))
  4894. (setq end (point))
  4895. (goto-char beg0)
  4896. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4897. ;; include less whitespace
  4898. (save-excursion
  4899. (goto-char beg)
  4900. (forward-line (- ne-beg ne-end))
  4901. (setq beg (point))))
  4902. ;; Find insertion point, with error handling
  4903. (while (> cnt 0)
  4904. (or (and (funcall movfunc) (looking-at outline-regexp))
  4905. (progn (goto-char beg0)
  4906. (error "Cannot move past superior level or buffer limit")))
  4907. (setq cnt (1- cnt)))
  4908. (if (> arg 0)
  4909. ;; Moving forward - still need to move over subtree
  4910. (progn (org-end-of-subtree t t)
  4911. (save-excursion
  4912. (org-back-over-empty-lines)
  4913. (or (bolp) (newline)))))
  4914. (setq ne-ins (org-back-over-empty-lines))
  4915. (move-marker ins-point (point))
  4916. (setq txt (buffer-substring beg end))
  4917. (org-save-markers-in-region beg end)
  4918. (delete-region beg end)
  4919. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4920. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4921. (let ((bbb (point)))
  4922. (insert-before-markers txt)
  4923. (org-reinstall-markers-in-region bbb)
  4924. (move-marker ins-point bbb))
  4925. (or (bolp) (insert "\n"))
  4926. (setq ins-end (point))
  4927. (goto-char ins-point)
  4928. (org-skip-whitespace)
  4929. (when (and (< arg 0)
  4930. (org-first-sibling-p)
  4931. (> ne-ins ne-beg))
  4932. ;; Move whitespace back to beginning
  4933. (save-excursion
  4934. (goto-char ins-end)
  4935. (let ((kill-whole-line t))
  4936. (kill-line (- ne-ins ne-beg)) (point)))
  4937. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4938. (move-marker ins-point nil)
  4939. (org-compact-display-after-subtree-move)
  4940. (org-show-empty-lines-in-parent)
  4941. (unless folded
  4942. (org-show-entry)
  4943. (show-children)
  4944. (org-cycle-hide-drawers 'children))))
  4945. (defvar org-subtree-clip ""
  4946. "Clipboard for cut and paste of subtrees.
  4947. This is actually only a copy of the kill, because we use the normal kill
  4948. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4949. (defvar org-subtree-clip-folded nil
  4950. "Was the last copied subtree folded?
  4951. This is used to fold the tree back after pasting.")
  4952. (defun org-cut-subtree (&optional n)
  4953. "Cut the current subtree into the clipboard.
  4954. With prefix arg N, cut this many sequential subtrees.
  4955. This is a short-hand for marking the subtree and then cutting it."
  4956. (interactive "p")
  4957. (org-copy-subtree n 'cut))
  4958. (defun org-copy-subtree (&optional n cut force-store-markers)
  4959. "Cut the current subtree into the clipboard.
  4960. With prefix arg N, cut this many sequential subtrees.
  4961. This is a short-hand for marking the subtree and then copying it.
  4962. If CUT is non-nil, actually cut the subtree.
  4963. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4964. of some markers in the region, even if CUT is non-nil. This is
  4965. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4966. (interactive "p")
  4967. (let (beg end folded (beg0 (point)))
  4968. (if (interactive-p)
  4969. (org-back-to-heading nil) ; take what looks like a subtree
  4970. (org-back-to-heading t)) ; take what is really there
  4971. (org-back-over-empty-lines)
  4972. (setq beg (point))
  4973. (skip-chars-forward " \t\r\n")
  4974. (save-match-data
  4975. (save-excursion (outline-end-of-heading)
  4976. (setq folded (org-invisible-p)))
  4977. (condition-case nil
  4978. (outline-forward-same-level (1- n))
  4979. (error nil))
  4980. (org-end-of-subtree t t))
  4981. (org-back-over-empty-lines)
  4982. (setq end (point))
  4983. (goto-char beg0)
  4984. (when (> end beg)
  4985. (setq org-subtree-clip-folded folded)
  4986. (when (or cut force-store-markers)
  4987. (org-save-markers-in-region beg end))
  4988. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4989. (setq org-subtree-clip (current-kill 0))
  4990. (message "%s: Subtree(s) with %d characters"
  4991. (if cut "Cut" "Copied")
  4992. (length org-subtree-clip)))))
  4993. (defun org-paste-subtree (&optional level tree for-yank)
  4994. "Paste the clipboard as a subtree, with modification of headline level.
  4995. The entire subtree is promoted or demoted in order to match a new headline
  4996. level.
  4997. If the cursor is at the beginning of a headline, the same level as
  4998. that headline is used to paste the tree
  4999. If not, the new level is derived from the *visible* headings
  5000. before and after the insertion point, and taken to be the inferior headline
  5001. level of the two. So if the previous visible heading is level 3 and the
  5002. next is level 4 (or vice versa), level 4 will be used for insertion.
  5003. This makes sure that the subtree remains an independent subtree and does
  5004. not swallow low level entries.
  5005. You can also force a different level, either by using a numeric prefix
  5006. argument, or by inserting the heading marker by hand. For example, if the
  5007. cursor is after \"*****\", then the tree will be shifted to level 5.
  5008. If optional TREE is given, use this text instead of the kill ring.
  5009. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  5010. move back over whitespace before inserting, and move point to the end of
  5011. the inserted text when done."
  5012. (interactive "P")
  5013. (unless (org-kill-is-subtree-p tree)
  5014. (error "%s"
  5015. (substitute-command-keys
  5016. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  5017. (let* ((visp (not (org-invisible-p)))
  5018. (txt (or tree (and kill-ring (current-kill 0))))
  5019. (^re (concat "^\\(" outline-regexp "\\)"))
  5020. (re (concat "\\(" outline-regexp "\\)"))
  5021. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  5022. (old-level (if (string-match ^re txt)
  5023. (- (match-end 0) (match-beginning 0) 1)
  5024. -1))
  5025. (force-level (cond (level (prefix-numeric-value level))
  5026. ((and (looking-at "[ \t]*$")
  5027. (string-match
  5028. ^re_ (buffer-substring
  5029. (point-at-bol) (point))))
  5030. (- (match-end 1) (match-beginning 1)))
  5031. ((and (bolp)
  5032. (looking-at org-outline-regexp))
  5033. (- (match-end 0) (point) 1))
  5034. (t nil)))
  5035. (previous-level (save-excursion
  5036. (condition-case nil
  5037. (progn
  5038. (outline-previous-visible-heading 1)
  5039. (if (looking-at re)
  5040. (- (match-end 0) (match-beginning 0) 1)
  5041. 1))
  5042. (error 1))))
  5043. (next-level (save-excursion
  5044. (condition-case nil
  5045. (progn
  5046. (or (looking-at outline-regexp)
  5047. (outline-next-visible-heading 1))
  5048. (if (looking-at re)
  5049. (- (match-end 0) (match-beginning 0) 1)
  5050. 1))
  5051. (error 1))))
  5052. (new-level (or force-level (max previous-level next-level)))
  5053. (shift (if (or (= old-level -1)
  5054. (= new-level -1)
  5055. (= old-level new-level))
  5056. 0
  5057. (- new-level old-level)))
  5058. (delta (if (> shift 0) -1 1))
  5059. (func (if (> shift 0) 'org-demote 'org-promote))
  5060. (org-odd-levels-only nil)
  5061. beg end newend)
  5062. ;; Remove the forced level indicator
  5063. (if force-level
  5064. (delete-region (point-at-bol) (point)))
  5065. ;; Paste
  5066. (beginning-of-line 1)
  5067. (unless for-yank (org-back-over-empty-lines))
  5068. (setq beg (point))
  5069. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  5070. (insert-before-markers txt)
  5071. (unless (string-match "\n\\'" txt) (insert "\n"))
  5072. (setq newend (point))
  5073. (org-reinstall-markers-in-region beg)
  5074. (setq end (point))
  5075. (goto-char beg)
  5076. (skip-chars-forward " \t\n\r")
  5077. (setq beg (point))
  5078. (if (and (org-invisible-p) visp)
  5079. (save-excursion (outline-show-heading)))
  5080. ;; Shift if necessary
  5081. (unless (= shift 0)
  5082. (save-restriction
  5083. (narrow-to-region beg end)
  5084. (while (not (= shift 0))
  5085. (org-map-region func (point-min) (point-max))
  5086. (setq shift (+ delta shift)))
  5087. (goto-char (point-min))
  5088. (setq newend (point-max))))
  5089. (when (or (interactive-p) for-yank)
  5090. (message "Clipboard pasted as level %d subtree" new-level))
  5091. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  5092. kill-ring
  5093. (eq org-subtree-clip (current-kill 0))
  5094. org-subtree-clip-folded)
  5095. ;; The tree was folded before it was killed/copied
  5096. (hide-subtree))
  5097. (and for-yank (goto-char newend))))
  5098. (defun org-kill-is-subtree-p (&optional txt)
  5099. "Check if the current kill is an outline subtree, or a set of trees.
  5100. Returns nil if kill does not start with a headline, or if the first
  5101. headline level is not the largest headline level in the tree.
  5102. So this will actually accept several entries of equal levels as well,
  5103. which is OK for `org-paste-subtree'.
  5104. If optional TXT is given, check this string instead of the current kill."
  5105. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  5106. (start-level (and kill
  5107. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  5108. org-outline-regexp "\\)")
  5109. kill)
  5110. (- (match-end 2) (match-beginning 2) 1)))
  5111. (re (concat "^" org-outline-regexp))
  5112. (start (1+ (or (match-beginning 2) -1))))
  5113. (if (not start-level)
  5114. (progn
  5115. nil) ;; does not even start with a heading
  5116. (catch 'exit
  5117. (while (setq start (string-match re kill (1+ start)))
  5118. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  5119. (throw 'exit nil)))
  5120. t))))
  5121. (defvar org-markers-to-move nil
  5122. "Markers that should be moved with a cut-and-paste operation.
  5123. Those markers are stored together with their positions relative to
  5124. the start of the region.")
  5125. (defun org-save-markers-in-region (beg end)
  5126. "Check markers in region.
  5127. If these markers are between BEG and END, record their position relative
  5128. to BEG, so that after moving the block of text, we can put the markers back
  5129. into place.
  5130. This function gets called just before an entry or tree gets cut from the
  5131. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  5132. called immediately, to move the markers with the entries."
  5133. (setq org-markers-to-move nil)
  5134. (when (featurep 'org-clock)
  5135. (org-clock-save-markers-for-cut-and-paste beg end))
  5136. (when (featurep 'org-agenda)
  5137. (org-agenda-save-markers-for-cut-and-paste beg end)))
  5138. (defun org-check-and-save-marker (marker beg end)
  5139. "Check if MARKER is between BEG and END.
  5140. If yes, remember the marker and the distance to BEG."
  5141. (when (and (marker-buffer marker)
  5142. (equal (marker-buffer marker) (current-buffer)))
  5143. (if (and (>= marker beg) (< marker end))
  5144. (push (cons marker (- marker beg)) org-markers-to-move))))
  5145. (defun org-reinstall-markers-in-region (beg)
  5146. "Move all remembered markers to their position relative to BEG."
  5147. (mapc (lambda (x)
  5148. (move-marker (car x) (+ beg (cdr x))))
  5149. org-markers-to-move)
  5150. (setq org-markers-to-move nil))
  5151. (defun org-narrow-to-subtree ()
  5152. "Narrow buffer to the current subtree."
  5153. (interactive)
  5154. (save-excursion
  5155. (save-match-data
  5156. (narrow-to-region
  5157. (progn (org-back-to-heading) (point))
  5158. (progn (org-end-of-subtree t) (point))))))
  5159. ;;; Outline Sorting
  5160. (defun org-sort (with-case)
  5161. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  5162. Optional argument WITH-CASE means sort case-sensitively.
  5163. With a double prefix argument, also remove duplicate entries."
  5164. (interactive "P")
  5165. (if (org-at-table-p)
  5166. (org-call-with-arg 'org-table-sort-lines with-case)
  5167. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  5168. (defun org-sort-remove-invisible (s)
  5169. (remove-text-properties 0 (length s) org-rm-props s)
  5170. (while (string-match org-bracket-link-regexp s)
  5171. (setq s (replace-match (if (match-end 2)
  5172. (match-string 3 s)
  5173. (match-string 1 s)) t t s)))
  5174. s)
  5175. (defvar org-priority-regexp) ; defined later in the file
  5176. (defun org-sort-entries-or-items
  5177. (&optional with-case sorting-type getkey-func compare-func property)
  5178. "Sort entries on a certain level of an outline tree.
  5179. If there is an active region, the entries in the region are sorted.
  5180. Else, if the cursor is before the first entry, sort the top-level items.
  5181. Else, the children of the entry at point are sorted.
  5182. Sorting can be alphabetically, numerically, and by date/time as given by
  5183. the first time stamp in the entry. The command prompts for the sorting
  5184. type unless it has been given to the function through the SORTING-TYPE
  5185. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  5186. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  5187. called with point at the beginning of the record. It must return either
  5188. a string or a number that should serve as the sorting key for that record.
  5189. Comparing entries ignores case by default. However, with an optional argument
  5190. WITH-CASE, the sorting considers case as well."
  5191. (interactive "P")
  5192. (let ((case-func (if with-case 'identity 'downcase))
  5193. start beg end stars re re2
  5194. txt what tmp plain-list-p)
  5195. ;; Find beginning and end of region to sort
  5196. (cond
  5197. ((org-region-active-p)
  5198. ;; we will sort the region
  5199. (setq end (region-end)
  5200. what "region")
  5201. (goto-char (region-beginning))
  5202. (if (not (org-on-heading-p)) (outline-next-heading))
  5203. (setq start (point)))
  5204. ((org-at-item-p)
  5205. ;; we will sort this plain list
  5206. (org-beginning-of-item-list) (setq start (point))
  5207. (org-end-of-item-list) (setq end (point))
  5208. (goto-char start)
  5209. (setq plain-list-p t
  5210. what "plain list"))
  5211. ((or (org-on-heading-p)
  5212. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  5213. ;; we will sort the children of the current headline
  5214. (org-back-to-heading)
  5215. (setq start (point)
  5216. end (progn (org-end-of-subtree t t)
  5217. (org-back-over-empty-lines)
  5218. (point))
  5219. what "children")
  5220. (goto-char start)
  5221. (show-subtree)
  5222. (outline-next-heading))
  5223. (t
  5224. ;; we will sort the top-level entries in this file
  5225. (goto-char (point-min))
  5226. (or (org-on-heading-p) (outline-next-heading))
  5227. (setq start (point) end (point-max) what "top-level")
  5228. (goto-char start)
  5229. (show-all)))
  5230. (setq beg (point))
  5231. (if (>= beg end) (error "Nothing to sort"))
  5232. (unless plain-list-p
  5233. (looking-at "\\(\\*+\\)")
  5234. (setq stars (match-string 1)
  5235. re (concat "^" (regexp-quote stars) " +")
  5236. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  5237. txt (buffer-substring beg end))
  5238. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  5239. (if (and (not (equal stars "*")) (string-match re2 txt))
  5240. (error "Region to sort contains a level above the first entry")))
  5241. (unless sorting-type
  5242. (message
  5243. (if plain-list-p
  5244. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  5245. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  5246. what)
  5247. (setq sorting-type (read-char-exclusive))
  5248. (and (= (downcase sorting-type) ?f)
  5249. (setq getkey-func
  5250. (org-ido-completing-read "Sort using function: "
  5251. obarray 'fboundp t nil nil))
  5252. (setq getkey-func (intern getkey-func)))
  5253. (and (= (downcase sorting-type) ?r)
  5254. (setq property
  5255. (org-ido-completing-read "Property: "
  5256. (mapcar 'list (org-buffer-property-keys t))
  5257. nil t))))
  5258. (message "Sorting entries...")
  5259. (save-restriction
  5260. (narrow-to-region start end)
  5261. (let ((dcst (downcase sorting-type))
  5262. (now (current-time)))
  5263. (sort-subr
  5264. (/= dcst sorting-type)
  5265. ;; This function moves to the beginning character of the "record" to
  5266. ;; be sorted.
  5267. (if plain-list-p
  5268. (lambda nil
  5269. (if (org-at-item-p) t (goto-char (point-max))))
  5270. (lambda nil
  5271. (if (re-search-forward re nil t)
  5272. (goto-char (match-beginning 0))
  5273. (goto-char (point-max)))))
  5274. ;; This function moves to the last character of the "record" being
  5275. ;; sorted.
  5276. (if plain-list-p
  5277. 'org-end-of-item
  5278. (lambda nil
  5279. (save-match-data
  5280. (condition-case nil
  5281. (outline-forward-same-level 1)
  5282. (error
  5283. (goto-char (point-max)))))))
  5284. ;; This function returns the value that gets sorted against.
  5285. (if plain-list-p
  5286. (lambda nil
  5287. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5288. (cond
  5289. ((= dcst ?n)
  5290. (string-to-number (buffer-substring (match-end 0)
  5291. (point-at-eol))))
  5292. ((= dcst ?a)
  5293. (buffer-substring (match-end 0) (point-at-eol)))
  5294. ((= dcst ?t)
  5295. (if (re-search-forward org-ts-regexp
  5296. (point-at-eol) t)
  5297. (org-time-string-to-time (match-string 0))
  5298. now))
  5299. ((= dcst ?f)
  5300. (if getkey-func
  5301. (progn
  5302. (setq tmp (funcall getkey-func))
  5303. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5304. tmp)
  5305. (error "Invalid key function `%s'" getkey-func)))
  5306. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5307. (lambda nil
  5308. (cond
  5309. ((= dcst ?n)
  5310. (if (looking-at org-complex-heading-regexp)
  5311. (string-to-number (match-string 4))
  5312. nil))
  5313. ((= dcst ?a)
  5314. (if (looking-at org-complex-heading-regexp)
  5315. (funcall case-func (match-string 4))
  5316. nil))
  5317. ((= dcst ?t)
  5318. (if (re-search-forward org-ts-regexp
  5319. (save-excursion
  5320. (forward-line 2)
  5321. (point)) t)
  5322. (org-time-string-to-time (match-string 0))
  5323. now))
  5324. ((= dcst ?p)
  5325. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5326. (string-to-char (match-string 2))
  5327. org-default-priority))
  5328. ((= dcst ?r)
  5329. (or (org-entry-get nil property) ""))
  5330. ((= dcst ?o)
  5331. (if (looking-at org-complex-heading-regexp)
  5332. (- 9999 (length (member (match-string 2)
  5333. org-todo-keywords-1)))))
  5334. ((= dcst ?f)
  5335. (if getkey-func
  5336. (progn
  5337. (setq tmp (funcall getkey-func))
  5338. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5339. tmp)
  5340. (error "Invalid key function `%s'" getkey-func)))
  5341. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5342. nil
  5343. (cond
  5344. ((= dcst ?a) 'string<)
  5345. ((= dcst ?t) 'time-less-p)
  5346. ((= dcst ?f) compare-func)
  5347. (t nil)))))
  5348. (message "Sorting entries...done")))
  5349. (defun org-do-sort (table what &optional with-case sorting-type)
  5350. "Sort TABLE of WHAT according to SORTING-TYPE.
  5351. The user will be prompted for the SORTING-TYPE if the call to this
  5352. function does not specify it. WHAT is only for the prompt, to indicate
  5353. what is being sorted. The sorting key will be extracted from
  5354. the car of the elements of the table.
  5355. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5356. (unless sorting-type
  5357. (message
  5358. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5359. what)
  5360. (setq sorting-type (read-char-exclusive)))
  5361. (let ((dcst (downcase sorting-type))
  5362. extractfun comparefun)
  5363. ;; Define the appropriate functions
  5364. (cond
  5365. ((= dcst ?n)
  5366. (setq extractfun 'string-to-number
  5367. comparefun (if (= dcst sorting-type) '< '>)))
  5368. ((= dcst ?a)
  5369. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5370. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5371. comparefun (if (= dcst sorting-type)
  5372. 'string<
  5373. (lambda (a b) (and (not (string< a b))
  5374. (not (string= a b)))))))
  5375. ((= dcst ?t)
  5376. (setq extractfun
  5377. (lambda (x)
  5378. (if (or (string-match org-ts-regexp x)
  5379. (string-match org-ts-regexp-both x))
  5380. (time-to-seconds
  5381. (org-time-string-to-time (match-string 0 x)))
  5382. 0))
  5383. comparefun (if (= dcst sorting-type) '< '>)))
  5384. (t (error "Invalid sorting type `%c'" sorting-type)))
  5385. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5386. table)
  5387. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5388. ;;; Editing source examples
  5389. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5390. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5391. (defvar org-edit-src-force-single-line nil)
  5392. (defvar org-edit-src-from-org-mode nil)
  5393. (defvar org-edit-src-picture nil)
  5394. (define-minor-mode org-exit-edit-mode
  5395. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5396. (defun org-edit-src-code ()
  5397. "Edit the source code example at point.
  5398. An indirect buffer is created, and that buffer is then narrowed to the
  5399. example at point and switched to the correct language mode. When done,
  5400. exit by killing the buffer with \\[org-edit-src-exit]."
  5401. (interactive)
  5402. (let ((line (org-current-line))
  5403. (case-fold-search t)
  5404. (msg (substitute-command-keys
  5405. "Edit, then exit with C-c ' (C-c and single quote)"))
  5406. (info (org-edit-src-find-region-and-lang))
  5407. (org-mode-p (eq major-mode 'org-mode))
  5408. beg end lang lang-f single lfmt)
  5409. (if (not info)
  5410. nil
  5411. (setq beg (nth 0 info)
  5412. end (nth 1 info)
  5413. lang (nth 2 info)
  5414. single (nth 3 info)
  5415. lfmt (nth 4 info)
  5416. lang-f (intern (concat lang "-mode")))
  5417. (unless (functionp lang-f)
  5418. (error "No such language mode: %s" lang-f))
  5419. (goto-line line)
  5420. (if (get-buffer "*Org Edit Src Example*")
  5421. (kill-buffer "*Org Edit Src Example*"))
  5422. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5423. "*Org Edit Src Example*"))
  5424. (narrow-to-region beg end)
  5425. (remove-text-properties beg end '(display nil invisible nil
  5426. intangible nil))
  5427. (let ((org-inhibit-startup t))
  5428. (funcall lang-f))
  5429. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5430. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5431. (when lfmt
  5432. (set (make-local-variable 'org-coderef-label-format) lfmt))
  5433. (when org-mode-p
  5434. (goto-char (point-min))
  5435. (while (re-search-forward "^," nil t)
  5436. (replace-match "")))
  5437. (goto-line line)
  5438. (org-exit-edit-mode)
  5439. (org-set-local 'header-line-format msg)
  5440. (message "%s" msg)
  5441. t)))
  5442. (defun org-edit-fixed-width-region ()
  5443. "Edit the fixed-width ascii drawing at point.
  5444. This must be a region where each line starts with a colon followed by
  5445. a space character.
  5446. An indirect buffer is created, and that buffer is then narrowed to the
  5447. example at point and switched to artist-mode. When done,
  5448. exit by killing the buffer with \\[org-edit-src-exit]."
  5449. (interactive)
  5450. (let ((line (org-current-line))
  5451. (case-fold-search t)
  5452. (msg (substitute-command-keys
  5453. "Edit, then exit with C-c ' (C-c and single quote)"))
  5454. (org-mode-p (eq major-mode 'org-mode))
  5455. beg end)
  5456. (beginning-of-line 1)
  5457. (if (looking-at "[ \t]*[^:\n \t]")
  5458. nil
  5459. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5460. (setq beg (point) end beg)
  5461. (save-excursion
  5462. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5463. (setq beg (point-at-bol 2))
  5464. (setq beg (point))))
  5465. (save-excursion
  5466. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5467. (setq end (1- (match-beginning 0)))
  5468. (setq end (point))))
  5469. (goto-line line))
  5470. (if (get-buffer "*Org Edit Picture*")
  5471. (kill-buffer "*Org Edit Picture*"))
  5472. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5473. "*Org Edit Picture*"))
  5474. (narrow-to-region beg end)
  5475. (remove-text-properties beg end '(display nil invisible nil
  5476. intangible nil))
  5477. (when (fboundp 'font-lock-unfontify-region)
  5478. (font-lock-unfontify-region (point-min) (point-max)))
  5479. (cond
  5480. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5481. (fundamental-mode)
  5482. (artist-mode 1))
  5483. (t (funcall org-edit-fixed-width-region-mode)))
  5484. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5485. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5486. (set (make-local-variable 'org-edit-src-picture) t)
  5487. (goto-char (point-min))
  5488. (while (re-search-forward "^[ \t]*: ?" nil t)
  5489. (replace-match ""))
  5490. (goto-line line)
  5491. (org-exit-edit-mode)
  5492. (org-set-local 'header-line-format msg)
  5493. (message "%s" msg)
  5494. t)))
  5495. (defun org-edit-src-find-region-and-lang ()
  5496. "Find the region and language for a local edit.
  5497. Return a list with beginning and end of the region, a string representing
  5498. the language, a switch telling of the content should be in a single line."
  5499. (let ((re-list
  5500. (append
  5501. org-edit-src-region-extra
  5502. '(
  5503. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5504. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5505. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5506. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5507. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5508. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5509. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5510. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5511. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5512. ("^#\\+html:" "\n" "html" single-line)
  5513. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5514. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5515. ("^#\\+latex:" "\n" "latex" single-line)
  5516. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5517. ("^#\\+ascii:" "\n" "ascii" single-line)
  5518. )))
  5519. (pos (point))
  5520. re1 re2 single beg end lang lfmt match-re1)
  5521. (catch 'exit
  5522. (while (setq entry (pop re-list))
  5523. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5524. single (nth 3 entry))
  5525. (save-excursion
  5526. (if (or (looking-at re1)
  5527. (re-search-backward re1 nil t))
  5528. (progn
  5529. (setq match-re1 (match-string 0))
  5530. (setq beg (match-end 0)
  5531. lang (org-edit-src-get-lang lang)
  5532. lfmt (org-edit-src-get-label-format match-re1))
  5533. (if (and (re-search-forward re2 nil t)
  5534. (>= (match-end 0) pos))
  5535. (throw 'exit (list beg (match-beginning 0)
  5536. lang single lfmt))))
  5537. (if (or (looking-at re2)
  5538. (re-search-forward re2 nil t))
  5539. (progn
  5540. (setq end (match-beginning 0))
  5541. (if (and (re-search-backward re1 nil t)
  5542. (<= (match-beginning 0) pos))
  5543. (progn
  5544. (setq lfmt (org-edit-src-get-label-format
  5545. (match-string 0)))
  5546. (throw 'exit
  5547. (list (match-end 0) end
  5548. (org-edit-src-get-lang lang)
  5549. single lfmt))))))))))))
  5550. (defun org-edit-src-get-lang (lang)
  5551. "Extract the src language."
  5552. (let ((m (match-string 0)))
  5553. (cond
  5554. ((stringp lang) lang)
  5555. ((integerp lang) (match-string lang))
  5556. ((and (eq lang 'lang)
  5557. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5558. (match-string 1 m))
  5559. ((and (eq lang 'style)
  5560. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5561. (match-string 1 m))
  5562. (t "fundamental"))))
  5563. (defun org-edit-src-get-label-format (s)
  5564. "Extract the label format."
  5565. (save-match-data
  5566. (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
  5567. (match-string 1 s))))
  5568. (defun org-edit-src-exit ()
  5569. "Exit special edit and protect problematic lines."
  5570. (interactive)
  5571. (unless (buffer-base-buffer (current-buffer))
  5572. (error "This is not an indirect buffer, something is wrong..."))
  5573. (unless (> (point-min) 1)
  5574. (error "This buffer is not narrowed, something is wrong..."))
  5575. (goto-char (point-min))
  5576. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5577. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5578. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5579. (goto-char (point-min))
  5580. (while (re-search-forward "\n" nil t)
  5581. (replace-match " "))
  5582. (goto-char (point-min))
  5583. (if (looking-at "\\s-*") (replace-match " "))
  5584. (if (re-search-forward "\\s-+\\'" nil t)
  5585. (replace-match "")))
  5586. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5587. (goto-char (point-min))
  5588. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5589. (replace-match ",\\1"))
  5590. (when font-lock-mode
  5591. (font-lock-unfontify-region (point-min) (point-max)))
  5592. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5593. (when (org-bound-and-true-p org-edit-src-picture)
  5594. (untabify (point-min) (point-max))
  5595. (goto-char (point-min))
  5596. (while (re-search-forward "^" nil t)
  5597. (replace-match ": "))
  5598. (when font-lock-mode
  5599. (font-lock-unfontify-region (point-min) (point-max)))
  5600. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5601. (kill-buffer (current-buffer))
  5602. (and (org-mode-p) (org-restart-font-lock)))
  5603. ;;; The orgstruct minor mode
  5604. ;; Define a minor mode which can be used in other modes in order to
  5605. ;; integrate the org-mode structure editing commands.
  5606. ;; This is really a hack, because the org-mode structure commands use
  5607. ;; keys which normally belong to the major mode. Here is how it
  5608. ;; works: The minor mode defines all the keys necessary to operate the
  5609. ;; structure commands, but wraps the commands into a function which
  5610. ;; tests if the cursor is currently at a headline or a plain list
  5611. ;; item. If that is the case, the structure command is used,
  5612. ;; temporarily setting many Org-mode variables like regular
  5613. ;; expressions for filling etc. However, when any of those keys is
  5614. ;; used at a different location, function uses `key-binding' to look
  5615. ;; up if the key has an associated command in another currently active
  5616. ;; keymap (minor modes, major mode, global), and executes that
  5617. ;; command. There might be problems if any of the keys is otherwise
  5618. ;; used as a prefix key.
  5619. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5620. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5621. ;; addresses this by checking explicitly for both bindings.
  5622. (defvar orgstruct-mode-map (make-sparse-keymap)
  5623. "Keymap for the minor `orgstruct-mode'.")
  5624. (defvar org-local-vars nil
  5625. "List of local variables, for use by `orgstruct-mode'")
  5626. ;;;###autoload
  5627. (define-minor-mode orgstruct-mode
  5628. "Toggle the minor more `orgstruct-mode'.
  5629. This mode is for using Org-mode structure commands in other modes.
  5630. The following key behave as if Org-mode was active, if the cursor
  5631. is on a headline, or on a plain list item (both in the definition
  5632. of Org-mode).
  5633. M-up Move entry/item up
  5634. M-down Move entry/item down
  5635. M-left Promote
  5636. M-right Demote
  5637. M-S-up Move entry/item up
  5638. M-S-down Move entry/item down
  5639. M-S-left Promote subtree
  5640. M-S-right Demote subtree
  5641. M-q Fill paragraph and items like in Org-mode
  5642. C-c ^ Sort entries
  5643. C-c - Cycle list bullet
  5644. TAB Cycle item visibility
  5645. M-RET Insert new heading/item
  5646. S-M-RET Insert new TODO heading / Checkbox item
  5647. C-c C-c Set tags / toggle checkbox"
  5648. nil " OrgStruct" nil
  5649. (org-load-modules-maybe)
  5650. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5651. ;;;###autoload
  5652. (defun turn-on-orgstruct ()
  5653. "Unconditionally turn on `orgstruct-mode'."
  5654. (orgstruct-mode 1))
  5655. (defun orgstruct++-mode (&optional arg)
  5656. "Toggle `orgstruct-mode', the enhanced version of it.
  5657. In addition to setting orgstruct-mode, this also exports all indentation and
  5658. autofilling variables from org-mode into the buffer. It will also
  5659. recognize item context in multiline items.
  5660. Note that turning off orgstruct-mode will *not* remove the
  5661. indentation/paragraph settings. This can only be done by refreshing the
  5662. major mode, for example with \[normal-mode]."
  5663. (interactive "P")
  5664. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  5665. (if (< arg 1)
  5666. (orgstruct-mode -1)
  5667. (orgstruct-mode 1)
  5668. (let (var val)
  5669. (mapc
  5670. (lambda (x)
  5671. (when (string-match
  5672. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5673. (symbol-name (car x)))
  5674. (setq var (car x) val (nth 1 x))
  5675. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5676. org-local-vars)
  5677. (org-set-local 'orgstruct-is-++ t))))
  5678. (defvar orgstruct-is-++ nil
  5679. "Is orgstruct-mode in ++ version in the current-buffer?")
  5680. (make-variable-buffer-local 'orgstruct-is-++)
  5681. ;;;###autoload
  5682. (defun turn-on-orgstruct++ ()
  5683. "Unconditionally turn on `orgstruct++-mode'."
  5684. (orgstruct++-mode 1))
  5685. (defun orgstruct-error ()
  5686. "Error when there is no default binding for a structure key."
  5687. (interactive)
  5688. (error "This key has no function outside structure elements"))
  5689. (defun orgstruct-setup ()
  5690. "Setup orgstruct keymaps."
  5691. (let ((nfunc 0)
  5692. (bindings
  5693. (list
  5694. '([(meta up)] org-metaup)
  5695. '([(meta down)] org-metadown)
  5696. '([(meta left)] org-metaleft)
  5697. '([(meta right)] org-metaright)
  5698. '([(meta shift up)] org-shiftmetaup)
  5699. '([(meta shift down)] org-shiftmetadown)
  5700. '([(meta shift left)] org-shiftmetaleft)
  5701. '([(meta shift right)] org-shiftmetaright)
  5702. '([(shift up)] org-shiftup)
  5703. '([(shift down)] org-shiftdown)
  5704. '([(shift left)] org-shiftleft)
  5705. '([(shift right)] org-shiftright)
  5706. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5707. '("\M-q" fill-paragraph)
  5708. '("\C-c^" org-sort)
  5709. '("\C-c-" org-cycle-list-bullet)))
  5710. elt key fun cmd)
  5711. (while (setq elt (pop bindings))
  5712. (setq nfunc (1+ nfunc))
  5713. (setq key (org-key (car elt))
  5714. fun (nth 1 elt)
  5715. cmd (orgstruct-make-binding fun nfunc key))
  5716. (org-defkey orgstruct-mode-map key cmd))
  5717. ;; Special treatment needed for TAB and RET
  5718. (org-defkey orgstruct-mode-map [(tab)]
  5719. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5720. (org-defkey orgstruct-mode-map "\C-i"
  5721. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5722. (org-defkey orgstruct-mode-map "\M-\C-m"
  5723. (orgstruct-make-binding 'org-insert-heading 105
  5724. "\M-\C-m" [(meta return)]))
  5725. (org-defkey orgstruct-mode-map [(meta return)]
  5726. (orgstruct-make-binding 'org-insert-heading 106
  5727. [(meta return)] "\M-\C-m"))
  5728. (org-defkey orgstruct-mode-map [(shift meta return)]
  5729. (orgstruct-make-binding 'org-insert-todo-heading 107
  5730. [(meta return)] "\M-\C-m"))
  5731. (unless org-local-vars
  5732. (setq org-local-vars (org-get-local-variables)))
  5733. t))
  5734. (defun orgstruct-make-binding (fun n &rest keys)
  5735. "Create a function for binding in the structure minor mode.
  5736. FUN is the command to call inside a table. N is used to create a unique
  5737. command name. KEYS are keys that should be checked in for a command
  5738. to execute outside of tables."
  5739. (eval
  5740. (list 'defun
  5741. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5742. '(arg)
  5743. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5744. "Outside of structure, run the binding of `"
  5745. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5746. "'.")
  5747. '(interactive "p")
  5748. (list 'if
  5749. `(org-context-p 'headline 'item
  5750. (and orgstruct-is-++
  5751. ,(and (memq fun '(org-insert-heading org-insert-todo-heading)) t)
  5752. 'item-body))
  5753. (list 'org-run-like-in-org-mode (list 'quote fun))
  5754. (list 'let '(orgstruct-mode)
  5755. (list 'call-interactively
  5756. (append '(or)
  5757. (mapcar (lambda (k)
  5758. (list 'key-binding k))
  5759. keys)
  5760. '('orgstruct-error))))))))
  5761. (defun org-context-p (&rest contexts)
  5762. "Check if local context is any of CONTEXTS.
  5763. Possible values in the list of contexts are `table', `headline', and `item'."
  5764. (let ((pos (point)))
  5765. (goto-char (point-at-bol))
  5766. (prog1 (or (and (memq 'table contexts)
  5767. (looking-at "[ \t]*|"))
  5768. (and (memq 'headline contexts)
  5769. ;;????????? (looking-at "\\*+"))
  5770. (looking-at outline-regexp))
  5771. (and (memq 'item contexts)
  5772. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  5773. (and (memq 'item-body contexts)
  5774. (org-in-item-p)))
  5775. (goto-char pos))))
  5776. (defun org-get-local-variables ()
  5777. "Return a list of all local variables in an org-mode buffer."
  5778. (let (varlist)
  5779. (with-current-buffer (get-buffer-create "*Org tmp*")
  5780. (erase-buffer)
  5781. (org-mode)
  5782. (setq varlist (buffer-local-variables)))
  5783. (kill-buffer "*Org tmp*")
  5784. (delq nil
  5785. (mapcar
  5786. (lambda (x)
  5787. (setq x
  5788. (if (symbolp x)
  5789. (list x)
  5790. (list (car x) (list 'quote (cdr x)))))
  5791. (if (string-match
  5792. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5793. (symbol-name (car x)))
  5794. x nil))
  5795. varlist))))
  5796. ;;;###autoload
  5797. (defun org-run-like-in-org-mode (cmd)
  5798. (org-load-modules-maybe)
  5799. (unless org-local-vars
  5800. (setq org-local-vars (org-get-local-variables)))
  5801. (eval (list 'let org-local-vars
  5802. (list 'call-interactively (list 'quote cmd)))))
  5803. ;;;; Archiving
  5804. (defun org-get-category (&optional pos)
  5805. "Get the category applying to position POS."
  5806. (get-text-property (or pos (point)) 'org-category))
  5807. (defun org-refresh-category-properties ()
  5808. "Refresh category text properties in the buffer."
  5809. (let ((def-cat (cond
  5810. ((null org-category)
  5811. (if buffer-file-name
  5812. (file-name-sans-extension
  5813. (file-name-nondirectory buffer-file-name))
  5814. "???"))
  5815. ((symbolp org-category) (symbol-name org-category))
  5816. (t org-category)))
  5817. beg end cat pos optionp)
  5818. (org-unmodified
  5819. (save-excursion
  5820. (save-restriction
  5821. (widen)
  5822. (goto-char (point-min))
  5823. (put-text-property (point) (point-max) 'org-category def-cat)
  5824. (while (re-search-forward
  5825. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5826. (setq pos (match-end 0)
  5827. optionp (equal (char-after (match-beginning 0)) ?#)
  5828. cat (org-trim (match-string 2)))
  5829. (if optionp
  5830. (setq beg (point-at-bol) end (point-max))
  5831. (org-back-to-heading t)
  5832. (setq beg (point) end (org-end-of-subtree t t)))
  5833. (put-text-property beg end 'org-category cat)
  5834. (goto-char pos)))))))
  5835. ;;;; Link Stuff
  5836. ;;; Link abbreviations
  5837. (defun org-link-expand-abbrev (link)
  5838. "Apply replacements as defined in `org-link-abbrev-alist."
  5839. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5840. (let* ((key (match-string 1 link))
  5841. (as (or (assoc key org-link-abbrev-alist-local)
  5842. (assoc key org-link-abbrev-alist)))
  5843. (tag (and (match-end 2) (match-string 3 link)))
  5844. rpl)
  5845. (if (not as)
  5846. link
  5847. (setq rpl (cdr as))
  5848. (cond
  5849. ((symbolp rpl) (funcall rpl tag))
  5850. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5851. ((string-match "%h" rpl)
  5852. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5853. (t (concat rpl tag)))))
  5854. link))
  5855. ;;; Storing and inserting links
  5856. (defvar org-insert-link-history nil
  5857. "Minibuffer history for links inserted with `org-insert-link'.")
  5858. (defvar org-stored-links nil
  5859. "Contains the links stored with `org-store-link'.")
  5860. (defvar org-store-link-plist nil
  5861. "Plist with info about the most recently link created with `org-store-link'.")
  5862. (defvar org-link-protocols nil
  5863. "Link protocols added to Org-mode using `org-add-link-type'.")
  5864. (defvar org-store-link-functions nil
  5865. "List of functions that are called to create and store a link.
  5866. Each function will be called in turn until one returns a non-nil
  5867. value. Each function should check if it is responsible for creating
  5868. this link (for example by looking at the major mode).
  5869. If not, it must exit and return nil.
  5870. If yes, it should return a non-nil value after a calling
  5871. `org-store-link-props' with a list of properties and values.
  5872. Special properties are:
  5873. :type The link prefix. like \"http\". This must be given.
  5874. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5875. This is obligatory as well.
  5876. :description Optional default description for the second pair
  5877. of brackets in an Org-mode link. The user can still change
  5878. this when inserting this link into an Org-mode buffer.
  5879. In addition to these, any additional properties can be specified
  5880. and then used in remember templates.")
  5881. (defun org-add-link-type (type &optional follow export)
  5882. "Add TYPE to the list of `org-link-types'.
  5883. Re-compute all regular expressions depending on `org-link-types'
  5884. FOLLOW and EXPORT are two functions.
  5885. FOLLOW should take the link path as the single argument and do whatever
  5886. is necessary to follow the link, for example find a file or display
  5887. a mail message.
  5888. EXPORT should format the link path for export to one of the export formats.
  5889. It should be a function accepting three arguments:
  5890. path the path of the link, the text after the prefix (like \"http:\")
  5891. desc the description of the link, if any, nil if there was no description
  5892. format the export format, a symbol like `html' or `latex'.
  5893. The function may use the FORMAT information to return different values
  5894. depending on the format. The return value will be put literally into
  5895. the exported file.
  5896. Org-mode has a built-in default for exporting links. If you are happy with
  5897. this default, there is no need to define an export function for the link
  5898. type. For a simple example of an export function, see `org-bbdb.el'."
  5899. (add-to-list 'org-link-types type t)
  5900. (org-make-link-regexps)
  5901. (if (assoc type org-link-protocols)
  5902. (setcdr (assoc type org-link-protocols) (list follow export))
  5903. (push (list type follow export) org-link-protocols)))
  5904. ;;;###autoload
  5905. (defun org-store-link (arg)
  5906. "\\<org-mode-map>Store an org-link to the current location.
  5907. This link is added to `org-stored-links' and can later be inserted
  5908. into an org-buffer with \\[org-insert-link].
  5909. For some link types, a prefix arg is interpreted:
  5910. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5911. For file links, arg negates `org-context-in-file-links'."
  5912. (interactive "P")
  5913. (org-load-modules-maybe)
  5914. (setq org-store-link-plist nil) ; reset
  5915. (let (link cpltxt desc description search txt)
  5916. (cond
  5917. ((run-hook-with-args-until-success 'org-store-link-functions)
  5918. (setq link (plist-get org-store-link-plist :link)
  5919. desc (or (plist-get org-store-link-plist :description) link)))
  5920. ((equal (buffer-name) "*Org Edit Src Example*")
  5921. (let (label gc)
  5922. (while (or (not label)
  5923. (save-excursion
  5924. (save-restriction
  5925. (widen)
  5926. (goto-char (point-min))
  5927. (re-search-forward
  5928. (regexp-quote (format org-coderef-label-format label))
  5929. nil t))))
  5930. (when label (message "Label exists already") (sit-for 2))
  5931. (setq label (read-string "Code line label: " label)))
  5932. (end-of-line 1)
  5933. (setq link (format org-coderef-label-format label))
  5934. (setq gc (- 79 (length link)))
  5935. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5936. (insert link)
  5937. (setq link (concat "(" label ")") desc nil)))
  5938. ((eq major-mode 'calendar-mode)
  5939. (let ((cd (calendar-cursor-to-date)))
  5940. (setq link
  5941. (format-time-string
  5942. (car org-time-stamp-formats)
  5943. (apply 'encode-time
  5944. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5945. nil nil nil))))
  5946. (org-store-link-props :type "calendar" :date cd)))
  5947. ((eq major-mode 'w3-mode)
  5948. (setq cpltxt (url-view-url t)
  5949. link (org-make-link cpltxt))
  5950. (org-store-link-props :type "w3" :url (url-view-url t)))
  5951. ((eq major-mode 'w3m-mode)
  5952. (setq cpltxt (or w3m-current-title w3m-current-url)
  5953. link (org-make-link w3m-current-url))
  5954. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5955. ((setq search (run-hook-with-args-until-success
  5956. 'org-create-file-search-functions))
  5957. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5958. "::" search))
  5959. (setq cpltxt (or description link)))
  5960. ((eq major-mode 'image-mode)
  5961. (setq cpltxt (concat "file:"
  5962. (abbreviate-file-name buffer-file-name))
  5963. link (org-make-link cpltxt))
  5964. (org-store-link-props :type "image" :file buffer-file-name))
  5965. ((eq major-mode 'dired-mode)
  5966. ;; link to the file in the current line
  5967. (setq cpltxt (concat "file:"
  5968. (abbreviate-file-name
  5969. (expand-file-name
  5970. (dired-get-filename nil t))))
  5971. link (org-make-link cpltxt)))
  5972. ((and buffer-file-name (org-mode-p))
  5973. (cond
  5974. ((org-in-regexp "<<\\(.*?\\)>>")
  5975. (setq cpltxt
  5976. (concat "file:"
  5977. (abbreviate-file-name buffer-file-name)
  5978. "::" (match-string 1))
  5979. link (org-make-link cpltxt)))
  5980. ((and (featurep 'org-id)
  5981. (or (eq org-link-to-org-use-id t)
  5982. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5983. (interactive-p))
  5984. (and org-link-to-org-use-id
  5985. (condition-case nil
  5986. (org-entry-get nil "ID")
  5987. (error nil)))))
  5988. ;; We can make a link using the ID.
  5989. (setq link (condition-case nil
  5990. (prog1 (org-id-store-link)
  5991. (setq desc (plist-get org-store-link-plist
  5992. :description)))
  5993. (error
  5994. ;; probably before first headline, link to file only
  5995. (concat "file:"
  5996. (abbreviate-file-name buffer-file-name))))))
  5997. (t
  5998. ;; Just link to current headline
  5999. (setq cpltxt (concat "file:"
  6000. (abbreviate-file-name buffer-file-name)))
  6001. ;; Add a context search string
  6002. (when (org-xor org-context-in-file-links arg)
  6003. (setq txt (cond
  6004. ((org-on-heading-p) nil)
  6005. ((org-region-active-p)
  6006. (buffer-substring (region-beginning) (region-end)))
  6007. (t nil)))
  6008. (when (or (null txt) (string-match "\\S-" txt))
  6009. (setq cpltxt
  6010. (concat cpltxt "::"
  6011. (condition-case nil
  6012. (org-make-org-heading-search-string txt)
  6013. (error "")))
  6014. desc "NONE")))
  6015. (if (string-match "::\\'" cpltxt)
  6016. (setq cpltxt (substring cpltxt 0 -2)))
  6017. (setq link (org-make-link cpltxt)))))
  6018. ((buffer-file-name (buffer-base-buffer))
  6019. ;; Just link to this file here.
  6020. (setq cpltxt (concat "file:"
  6021. (abbreviate-file-name
  6022. (buffer-file-name (buffer-base-buffer)))))
  6023. ;; Add a context string
  6024. (when (org-xor org-context-in-file-links arg)
  6025. (setq txt (if (org-region-active-p)
  6026. (buffer-substring (region-beginning) (region-end))
  6027. (buffer-substring (point-at-bol) (point-at-eol))))
  6028. ;; Only use search option if there is some text.
  6029. (when (string-match "\\S-" txt)
  6030. (setq cpltxt
  6031. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  6032. desc "NONE")))
  6033. (setq link (org-make-link cpltxt)))
  6034. ((interactive-p)
  6035. (error "Cannot link to a buffer which is not visiting a file"))
  6036. (t (setq link nil)))
  6037. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6038. (setq link (or link cpltxt)
  6039. desc (or desc cpltxt))
  6040. (if (equal desc "NONE") (setq desc nil))
  6041. (if (and (interactive-p) link)
  6042. (progn
  6043. (setq org-stored-links
  6044. (cons (list link desc) org-stored-links))
  6045. (message "Stored: %s" (or desc link)))
  6046. (and link (org-make-link-string link desc)))))
  6047. (defun org-store-link-props (&rest plist)
  6048. "Store link properties, extract names and addresses."
  6049. (let (x adr)
  6050. (when (setq x (plist-get plist :from))
  6051. (setq adr (mail-extract-address-components x))
  6052. (setq plist (plist-put plist :fromname (car adr)))
  6053. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  6054. (when (setq x (plist-get plist :to))
  6055. (setq adr (mail-extract-address-components x))
  6056. (setq plist (plist-put plist :toname (car adr)))
  6057. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  6058. (let ((from (plist-get plist :from))
  6059. (to (plist-get plist :to)))
  6060. (when (and from to org-from-is-user-regexp)
  6061. (setq plist
  6062. (plist-put plist :fromto
  6063. (if (string-match org-from-is-user-regexp from)
  6064. (concat "to %t")
  6065. (concat "from %f"))))))
  6066. (setq org-store-link-plist plist))
  6067. (defun org-add-link-props (&rest plist)
  6068. "Add these properties to the link property list."
  6069. (let (key value)
  6070. (while plist
  6071. (setq key (pop plist) value (pop plist))
  6072. (setq org-store-link-plist
  6073. (plist-put org-store-link-plist key value)))))
  6074. (defun org-email-link-description (&optional fmt)
  6075. "Return the description part of an email link.
  6076. This takes information from `org-store-link-plist' and formats it
  6077. according to FMT (default from `org-email-link-description-format')."
  6078. (setq fmt (or fmt org-email-link-description-format))
  6079. (let* ((p org-store-link-plist)
  6080. (to (plist-get p :toaddress))
  6081. (from (plist-get p :fromaddress))
  6082. (table
  6083. (list
  6084. (cons "%c" (plist-get p :fromto))
  6085. (cons "%F" (plist-get p :from))
  6086. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6087. (cons "%T" (plist-get p :to))
  6088. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6089. (cons "%s" (plist-get p :subject))
  6090. (cons "%m" (plist-get p :message-id)))))
  6091. (when (string-match "%c" fmt)
  6092. ;; Check if the user wrote this message
  6093. (if (and org-from-is-user-regexp from to
  6094. (save-match-data (string-match org-from-is-user-regexp from)))
  6095. (setq fmt (replace-match "to %t" t t fmt))
  6096. (setq fmt (replace-match "from %f" t t fmt))))
  6097. (org-replace-escapes fmt table)))
  6098. (defun org-make-org-heading-search-string (&optional string heading)
  6099. "Make search string for STRING or current headline."
  6100. (interactive)
  6101. (let ((s (or string (org-get-heading))))
  6102. (unless (and string (not heading))
  6103. ;; We are using a headline, clean up garbage in there.
  6104. (if (string-match org-todo-regexp s)
  6105. (setq s (replace-match "" t t s)))
  6106. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6107. (setq s (replace-match "" t t s)))
  6108. (setq s (org-trim s))
  6109. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6110. org-comment-string "\\)") s)
  6111. (setq s (replace-match "" t t s)))
  6112. (while (string-match org-ts-regexp s)
  6113. (setq s (replace-match "" t t s))))
  6114. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6115. (setq s (replace-match " " t t s)))
  6116. (or string (setq s (concat "*" s))) ; Add * for headlines
  6117. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6118. (defun org-make-link (&rest strings)
  6119. "Concatenate STRINGS."
  6120. (apply 'concat strings))
  6121. (defun org-make-link-string (link &optional description)
  6122. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6123. (unless (string-match "\\S-" link)
  6124. (error "Empty link"))
  6125. (when (stringp description)
  6126. ;; Remove brackets from the description, they are fatal.
  6127. (while (string-match "\\[" description)
  6128. (setq description (replace-match "{" t t description)))
  6129. (while (string-match "\\]" description)
  6130. (setq description (replace-match "}" t t description))))
  6131. (when (equal (org-link-escape link) description)
  6132. ;; No description needed, it is identical
  6133. (setq description nil))
  6134. (when (and (not description)
  6135. (not (equal link (org-link-escape link))))
  6136. (setq description (org-extract-attributes link)))
  6137. (concat "[[" (org-link-escape link) "]"
  6138. (if description (concat "[" description "]") "")
  6139. "]"))
  6140. (defconst org-link-escape-chars
  6141. '((?\ . "%20")
  6142. (?\[ . "%5B")
  6143. (?\] . "%5D")
  6144. (?\340 . "%E0") ; `a
  6145. (?\342 . "%E2") ; ^a
  6146. (?\347 . "%E7") ; ,c
  6147. (?\350 . "%E8") ; `e
  6148. (?\351 . "%E9") ; 'e
  6149. (?\352 . "%EA") ; ^e
  6150. (?\356 . "%EE") ; ^i
  6151. (?\364 . "%F4") ; ^o
  6152. (?\371 . "%F9") ; `u
  6153. (?\373 . "%FB") ; ^u
  6154. (?\; . "%3B")
  6155. (?? . "%3F")
  6156. (?= . "%3D")
  6157. (?+ . "%2B")
  6158. )
  6159. "Association list of escapes for some characters problematic in links.
  6160. This is the list that is used for internal purposes.")
  6161. (defconst org-link-escape-chars-browser
  6162. '((?\ . "%20")) ; 32 for the SPC char
  6163. "Association list of escapes for some characters problematic in links.
  6164. This is the list that is used before handing over to the browser.")
  6165. (defun org-link-escape (text &optional table)
  6166. "Escape characters in TEXT that are problematic for links."
  6167. (setq table (or table org-link-escape-chars))
  6168. (when text
  6169. (let ((re (mapconcat (lambda (x) (regexp-quote
  6170. (char-to-string (car x))))
  6171. table "\\|")))
  6172. (while (string-match re text)
  6173. (setq text
  6174. (replace-match
  6175. (cdr (assoc (string-to-char (match-string 0 text))
  6176. table))
  6177. t t text)))
  6178. text)))
  6179. (defun org-link-unescape (text &optional table)
  6180. "Reverse the action of `org-link-escape'."
  6181. (setq table (or table org-link-escape-chars))
  6182. (when text
  6183. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6184. table "\\|")))
  6185. (while (string-match re text)
  6186. (setq text
  6187. (replace-match
  6188. (char-to-string (car (rassoc (match-string 0 text) table)))
  6189. t t text)))
  6190. text)))
  6191. (defun org-xor (a b)
  6192. "Exclusive or."
  6193. (if a (not b) b))
  6194. (defun org-fixup-message-id-for-http (s)
  6195. "Replace special characters in a message id, so it can be used in an http query."
  6196. (while (string-match "<" s)
  6197. (setq s (replace-match "%3C" t t s)))
  6198. (while (string-match ">" s)
  6199. (setq s (replace-match "%3E" t t s)))
  6200. (while (string-match "@" s)
  6201. (setq s (replace-match "%40" t t s)))
  6202. s)
  6203. ;;;###autoload
  6204. (defun org-insert-link-global ()
  6205. "Insert a link like Org-mode does.
  6206. This command can be called in any mode to insert a link in Org-mode syntax."
  6207. (interactive)
  6208. (org-load-modules-maybe)
  6209. (org-run-like-in-org-mode 'org-insert-link))
  6210. (defun org-insert-link (&optional complete-file link-location)
  6211. "Insert a link. At the prompt, enter the link.
  6212. Completion can be used to insert any of the link protocol prefixes like
  6213. http or ftp in use.
  6214. The history can be used to select a link previously stored with
  6215. `org-store-link'. When the empty string is entered (i.e. if you just
  6216. press RET at the prompt), the link defaults to the most recently
  6217. stored link. As SPC triggers completion in the minibuffer, you need to
  6218. use M-SPC or C-q SPC to force the insertion of a space character.
  6219. You will also be prompted for a description, and if one is given, it will
  6220. be displayed in the buffer instead of the link.
  6221. If there is already a link at point, this command will allow you to edit link
  6222. and description parts.
  6223. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6224. be selected using completion. The path to the file will be relative to the
  6225. current directory if the file is in the current directory or a subdirectory.
  6226. Otherwise, the link will be the absolute path as completed in the minibuffer
  6227. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  6228. option `org-link-file-path-type'.
  6229. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6230. the current directory or below.
  6231. With three \\[universal-argument] prefixes, negate the meaning of
  6232. `org-keep-stored-link-after-insertion'.
  6233. If `org-make-link-description-function' is non-nil, this function will be
  6234. called with the link target, and the result will be the default
  6235. link description.
  6236. If the LINK-LOCATION parameter is non-nil, this value will be
  6237. used as the link location instead of reading one interactively."
  6238. (interactive "P")
  6239. (let* ((wcf (current-window-configuration))
  6240. (region (if (org-region-active-p)
  6241. (buffer-substring (region-beginning) (region-end))))
  6242. (remove (and region (list (region-beginning) (region-end))))
  6243. (desc region)
  6244. tmphist ; byte-compile incorrectly complains about this
  6245. (link link-location)
  6246. entry file)
  6247. (cond
  6248. (link-location) ; specified by arg, just use it.
  6249. ((org-in-regexp org-bracket-link-regexp 1)
  6250. ;; We do have a link at point, and we are going to edit it.
  6251. (setq remove (list (match-beginning 0) (match-end 0)))
  6252. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6253. (setq link (read-string "Link: "
  6254. (org-link-unescape
  6255. (org-match-string-no-properties 1)))))
  6256. ((or (org-in-regexp org-angle-link-re)
  6257. (org-in-regexp org-plain-link-re))
  6258. ;; Convert to bracket link
  6259. (setq remove (list (match-beginning 0) (match-end 0))
  6260. link (read-string "Link: "
  6261. (org-remove-angle-brackets (match-string 0)))))
  6262. ((member complete-file '((4) (16)))
  6263. ;; Completing read for file names.
  6264. (setq file (read-file-name "File: "))
  6265. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6266. (pwd1 (file-name-as-directory (abbreviate-file-name
  6267. (expand-file-name ".")))))
  6268. (cond
  6269. ((equal complete-file '(16))
  6270. (setq link (org-make-link
  6271. "file:"
  6272. (abbreviate-file-name (expand-file-name file)))))
  6273. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6274. (setq link (org-make-link "file:" (match-string 1 file))))
  6275. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6276. (expand-file-name file))
  6277. (setq link (org-make-link
  6278. "file:" (match-string 1 (expand-file-name file)))))
  6279. (t (setq link (org-make-link "file:" file))))))
  6280. (t
  6281. ;; Read link, with completion for stored links.
  6282. (with-output-to-temp-buffer "*Org Links*"
  6283. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6284. (when org-stored-links
  6285. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6286. (princ (mapconcat
  6287. (lambda (x)
  6288. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6289. (reverse org-stored-links) "\n"))))
  6290. (let ((cw (selected-window)))
  6291. (select-window (get-buffer-window "*Org Links*"))
  6292. (setq truncate-lines t)
  6293. (org-fit-window-to-buffer)
  6294. (select-window cw))
  6295. ;; Fake a link history, containing the stored links.
  6296. (setq tmphist (append (mapcar 'car org-stored-links)
  6297. org-insert-link-history))
  6298. (unwind-protect
  6299. (setq link
  6300. (let ((org-completion-use-ido nil))
  6301. (org-completing-read
  6302. "Link: "
  6303. (append
  6304. (mapcar (lambda (x) (list (concat (car x) ":")))
  6305. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6306. (mapcar (lambda (x) (list (concat x ":")))
  6307. org-link-types))
  6308. nil nil nil
  6309. 'tmphist
  6310. (or (car (car org-stored-links))))))
  6311. (set-window-configuration wcf)
  6312. (kill-buffer "*Org Links*"))
  6313. (setq entry (assoc link org-stored-links))
  6314. (or entry (push link org-insert-link-history))
  6315. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6316. (not org-keep-stored-link-after-insertion))
  6317. (setq org-stored-links (delq (assoc link org-stored-links)
  6318. org-stored-links)))
  6319. (setq desc (or desc (nth 1 entry)))))
  6320. (if (string-match org-plain-link-re link)
  6321. ;; URL-like link, normalize the use of angular brackets.
  6322. (setq link (org-make-link (org-remove-angle-brackets link))))
  6323. ;; Check if we are linking to the current file with a search option
  6324. ;; If yes, simplify the link by using only the search option.
  6325. (when (and buffer-file-name
  6326. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6327. (let* ((path (match-string 1 link))
  6328. (case-fold-search nil)
  6329. (search (match-string 2 link)))
  6330. (save-match-data
  6331. (if (equal (file-truename buffer-file-name) (file-truename path))
  6332. ;; We are linking to this same file, with a search option
  6333. (setq link search)))))
  6334. ;; Check if we can/should use a relative path. If yes, simplify the link
  6335. (when (string-match "^file:\\(.*\\)" link)
  6336. (let* ((path (match-string 1 link))
  6337. (origpath path)
  6338. (case-fold-search nil))
  6339. (cond
  6340. ((or (eq org-link-file-path-type 'absolute)
  6341. (equal complete-file '(16)))
  6342. (setq path (abbreviate-file-name (expand-file-name path))))
  6343. ((eq org-link-file-path-type 'noabbrev)
  6344. (setq path (expand-file-name path)))
  6345. ((eq org-link-file-path-type 'relative)
  6346. (setq path (file-relative-name path)))
  6347. (t
  6348. (save-match-data
  6349. (if (string-match (concat "^" (regexp-quote
  6350. (file-name-as-directory
  6351. (expand-file-name "."))))
  6352. (expand-file-name path))
  6353. ;; We are linking a file with relative path name.
  6354. (setq path (substring (expand-file-name path)
  6355. (match-end 0)))
  6356. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6357. (setq link (concat "file:" path))
  6358. (if (equal desc origpath)
  6359. (setq desc path))))
  6360. (if org-make-link-description-function
  6361. (setq desc (funcall org-make-link-description-function link desc)))
  6362. (setq desc (read-string "Description: " desc))
  6363. (unless (string-match "\\S-" desc) (setq desc nil))
  6364. (if remove (apply 'delete-region remove))
  6365. (insert (org-make-link-string link desc))))
  6366. (defun org-completing-read (&rest args)
  6367. "Completing-read with SPACE being a normal character."
  6368. (let ((minibuffer-local-completion-map
  6369. (copy-keymap minibuffer-local-completion-map)))
  6370. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6371. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  6372. (apply 'org-ido-completing-read args)))
  6373. (defun org-completing-read-no-ido (&rest args)
  6374. (let (org-completion-use-ido)
  6375. (apply 'org-completing-read args)))
  6376. (defun org-ido-completing-read (&rest args)
  6377. "Completing-read using `ido-mode' speedups if available"
  6378. (if (and org-completion-use-ido
  6379. (fboundp 'ido-completing-read)
  6380. (boundp 'ido-mode) ido-mode
  6381. (listp (second args)))
  6382. (let ((ido-enter-matching-directory nil))
  6383. (apply 'ido-completing-read (concat (car args))
  6384. (mapcar (lambda (x) (car x)) (nth 1 args))
  6385. (cddr args)))
  6386. (apply 'completing-read args)))
  6387. (defun org-extract-attributes (s)
  6388. "Extract the attributes cookie from a string and set as text property."
  6389. (let (a attr (start 0) key value)
  6390. (save-match-data
  6391. (when (string-match "{{\\([^}]+\\)}}$" s)
  6392. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6393. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6394. (setq key (match-string 1 a) value (match-string 2 a)
  6395. start (match-end 0)
  6396. attr (plist-put attr (intern key) value))))
  6397. (org-add-props s nil 'org-attr attr))
  6398. s))
  6399. (defun org-attributes-to-string (plist)
  6400. "Format a property list into an HTML attribute list."
  6401. (let ((s "") key value)
  6402. (while plist
  6403. (setq key (pop plist) value (pop plist))
  6404. (and value
  6405. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6406. s))
  6407. ;;; Opening/following a link
  6408. (defvar org-link-search-failed nil)
  6409. (defun org-next-link ()
  6410. "Move forward to the next link.
  6411. If the link is in hidden text, expose it."
  6412. (interactive)
  6413. (when (and org-link-search-failed (eq this-command last-command))
  6414. (goto-char (point-min))
  6415. (message "Link search wrapped back to beginning of buffer"))
  6416. (setq org-link-search-failed nil)
  6417. (let* ((pos (point))
  6418. (ct (org-context))
  6419. (a (assoc :link ct)))
  6420. (if a (goto-char (nth 2 a)))
  6421. (if (re-search-forward org-any-link-re nil t)
  6422. (progn
  6423. (goto-char (match-beginning 0))
  6424. (if (org-invisible-p) (org-show-context)))
  6425. (goto-char pos)
  6426. (setq org-link-search-failed t)
  6427. (error "No further link found"))))
  6428. (defun org-previous-link ()
  6429. "Move backward to the previous link.
  6430. If the link is in hidden text, expose it."
  6431. (interactive)
  6432. (when (and org-link-search-failed (eq this-command last-command))
  6433. (goto-char (point-max))
  6434. (message "Link search wrapped back to end of buffer"))
  6435. (setq org-link-search-failed nil)
  6436. (let* ((pos (point))
  6437. (ct (org-context))
  6438. (a (assoc :link ct)))
  6439. (if a (goto-char (nth 1 a)))
  6440. (if (re-search-backward org-any-link-re nil t)
  6441. (progn
  6442. (goto-char (match-beginning 0))
  6443. (if (org-invisible-p) (org-show-context)))
  6444. (goto-char pos)
  6445. (setq org-link-search-failed t)
  6446. (error "No further link found"))))
  6447. (defun org-translate-link (s)
  6448. "Translate a link string if a translation function has been defined."
  6449. (if (and org-link-translation-function
  6450. (fboundp org-link-translation-function)
  6451. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6452. (progn
  6453. (setq s (funcall org-link-translation-function
  6454. (match-string 1) (match-string 2)))
  6455. (concat (car s) ":" (cdr s)))
  6456. s))
  6457. (defun org-translate-link-from-planner (type path)
  6458. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6459. This is still an experimental function, your mileage may vary."
  6460. (cond
  6461. ((member type '("http" "https" "news" "ftp"))
  6462. ;; standard Internet links are the same.
  6463. nil)
  6464. ((and (equal type "irc") (string-match "^//" path))
  6465. ;; Planner has two / at the beginning of an irc link, we have 1.
  6466. ;; We should have zero, actually....
  6467. (setq path (substring path 1)))
  6468. ((and (equal type "lisp") (string-match "^/" path))
  6469. ;; Planner has a slash, we do not.
  6470. (setq type "elisp" path (substring path 1)))
  6471. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6472. ;; A typical message link. Planner has the id after the fina slash,
  6473. ;; we separate it with a hash mark
  6474. (setq path (concat (match-string 1 path) "#"
  6475. (org-remove-angle-brackets (match-string 2 path)))))
  6476. )
  6477. (cons type path))
  6478. (defun org-find-file-at-mouse (ev)
  6479. "Open file link or URL at mouse."
  6480. (interactive "e")
  6481. (mouse-set-point ev)
  6482. (org-open-at-point 'in-emacs))
  6483. (defun org-open-at-mouse (ev)
  6484. "Open file link or URL at mouse."
  6485. (interactive "e")
  6486. (mouse-set-point ev)
  6487. (if (eq major-mode 'org-agenda-mode)
  6488. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6489. (org-open-at-point))
  6490. (defvar org-window-config-before-follow-link nil
  6491. "The window configuration before following a link.
  6492. This is saved in case the need arises to restore it.")
  6493. (defvar org-open-link-marker (make-marker)
  6494. "Marker pointing to the location where `org-open-at-point; was called.")
  6495. ;;;###autoload
  6496. (defun org-open-at-point-global ()
  6497. "Follow a link like Org-mode does.
  6498. This command can be called in any mode to follow a link that has
  6499. Org-mode syntax."
  6500. (interactive)
  6501. (org-run-like-in-org-mode 'org-open-at-point))
  6502. ;;;###autoload
  6503. (defun org-open-link-from-string (s &optional arg)
  6504. "Open a link in the string S, as if it was in Org-mode."
  6505. (interactive "sLink: \nP")
  6506. (with-temp-buffer
  6507. (let ((org-inhibit-startup t))
  6508. (org-mode)
  6509. (insert s)
  6510. (goto-char (point-min))
  6511. (org-open-at-point arg))))
  6512. (defun org-open-at-point (&optional in-emacs)
  6513. "Open link at or after point.
  6514. If there is no link at point, this function will search forward up to
  6515. the end of the current subtree.
  6516. Normally, files will be opened by an appropriate application. If the
  6517. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6518. With a double prefix argument, try to open outside of Emacs, in the
  6519. application the system uses for this file type."
  6520. (interactive "P")
  6521. (org-load-modules-maybe)
  6522. (move-marker org-open-link-marker (point))
  6523. (setq org-window-config-before-follow-link (current-window-configuration))
  6524. (org-remove-occur-highlights nil nil t)
  6525. (cond
  6526. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6527. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6528. (org-footnote-action))
  6529. (t
  6530. (let (type path link line search (pos (point)))
  6531. (catch 'match
  6532. (save-excursion
  6533. (skip-chars-forward "^]\n\r")
  6534. (when (org-in-regexp org-bracket-link-regexp)
  6535. (setq link (org-extract-attributes
  6536. (org-link-unescape (org-match-string-no-properties 1))))
  6537. (while (string-match " *\n *" link)
  6538. (setq link (replace-match " " t t link)))
  6539. (setq link (org-link-expand-abbrev link))
  6540. (cond
  6541. ((or (file-name-absolute-p link)
  6542. (string-match "^\\.\\.?/" link))
  6543. (setq type "file" path link))
  6544. ((string-match org-link-re-with-space3 link)
  6545. (setq type (match-string 1 link) path (match-string 2 link)))
  6546. (t (setq type "thisfile" path link)))
  6547. (throw 'match t)))
  6548. (when (get-text-property (point) 'org-linked-text)
  6549. (setq type "thisfile"
  6550. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6551. (1+ (point)) (point))
  6552. path (buffer-substring
  6553. (previous-single-property-change pos 'org-linked-text)
  6554. (next-single-property-change pos 'org-linked-text)))
  6555. (throw 'match t))
  6556. (save-excursion
  6557. (when (or (org-in-regexp org-angle-link-re)
  6558. (org-in-regexp org-plain-link-re))
  6559. (setq type (match-string 1) path (match-string 2))
  6560. (throw 'match t)))
  6561. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6562. (setq type "tree-match"
  6563. path (match-string 1))
  6564. (throw 'match t))
  6565. (save-excursion
  6566. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6567. (setq type "tags"
  6568. path (match-string 1))
  6569. (while (string-match ":" path)
  6570. (setq path (replace-match "+" t t path)))
  6571. (throw 'match t))))
  6572. (unless path
  6573. (error "No link found"))
  6574. ;; Remove any trailing spaces in path
  6575. (if (string-match " +\\'" path)
  6576. (setq path (replace-match "" t t path)))
  6577. (if (and org-link-translation-function
  6578. (fboundp org-link-translation-function))
  6579. ;; Check if we need to translate the link
  6580. (let ((tmp (funcall org-link-translation-function type path)))
  6581. (setq type (car tmp) path (cdr tmp))))
  6582. (cond
  6583. ((assoc type org-link-protocols)
  6584. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6585. ((equal type "mailto")
  6586. (let ((cmd (car org-link-mailto-program))
  6587. (args (cdr org-link-mailto-program)) args1
  6588. (address path) (subject "") a)
  6589. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6590. (setq address (match-string 1 path)
  6591. subject (org-link-escape (match-string 2 path))))
  6592. (while args
  6593. (cond
  6594. ((not (stringp (car args))) (push (pop args) args1))
  6595. (t (setq a (pop args))
  6596. (if (string-match "%a" a)
  6597. (setq a (replace-match address t t a)))
  6598. (if (string-match "%s" a)
  6599. (setq a (replace-match subject t t a)))
  6600. (push a args1))))
  6601. (apply cmd (nreverse args1))))
  6602. ((member type '("http" "https" "ftp" "news"))
  6603. (browse-url (concat type ":" (org-link-escape
  6604. path org-link-escape-chars-browser))))
  6605. ((member type '("message"))
  6606. (browse-url (concat type ":" path)))
  6607. ((string= type "tags")
  6608. (org-tags-view in-emacs path))
  6609. ((string= type "thisfile")
  6610. (if in-emacs
  6611. (switch-to-buffer-other-window
  6612. (org-get-buffer-for-internal-link (current-buffer)))
  6613. (org-mark-ring-push))
  6614. (let ((cmd `(org-link-search
  6615. ,path
  6616. ,(cond ((equal in-emacs '(4)) 'occur)
  6617. ((equal in-emacs '(16)) 'org-occur)
  6618. (t nil))
  6619. ,pos)))
  6620. (condition-case nil (eval cmd)
  6621. (error (progn (widen) (eval cmd))))))
  6622. ((string= type "tree-match")
  6623. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6624. ((string= type "file")
  6625. (if (string-match "::\\([0-9]+\\)\\'" path)
  6626. (setq line (string-to-number (match-string 1 path))
  6627. path (substring path 0 (match-beginning 0)))
  6628. (if (string-match "::\\(.+\\)\\'" path)
  6629. (setq search (match-string 1 path)
  6630. path (substring path 0 (match-beginning 0)))))
  6631. (if (string-match "[*?{]" (file-name-nondirectory path))
  6632. (dired path)
  6633. (org-open-file path in-emacs line search)))
  6634. ((string= type "news")
  6635. (require 'org-gnus)
  6636. (org-gnus-follow-link path))
  6637. ((string= type "shell")
  6638. (let ((cmd path))
  6639. (if (or (not org-confirm-shell-link-function)
  6640. (funcall org-confirm-shell-link-function
  6641. (format "Execute \"%s\" in shell? "
  6642. (org-add-props cmd nil
  6643. 'face 'org-warning))))
  6644. (progn
  6645. (message "Executing %s" cmd)
  6646. (shell-command cmd))
  6647. (error "Abort"))))
  6648. ((string= type "elisp")
  6649. (let ((cmd path))
  6650. (if (or (not org-confirm-elisp-link-function)
  6651. (funcall org-confirm-elisp-link-function
  6652. (format "Execute \"%s\" as elisp? "
  6653. (org-add-props cmd nil
  6654. 'face 'org-warning))))
  6655. (message "%s => %s" cmd
  6656. (if (equal (string-to-char cmd) ?\()
  6657. (eval (read cmd))
  6658. (call-interactively (read cmd))))
  6659. (error "Abort"))))
  6660. (t
  6661. (browse-url-at-point))))))
  6662. (move-marker org-open-link-marker nil)
  6663. (run-hook-with-args 'org-follow-link-hook))
  6664. ;;;; Time estimates
  6665. (defun org-get-effort (&optional pom)
  6666. "Get the effort estimate for the current entry."
  6667. (org-entry-get pom org-effort-property))
  6668. ;;; File search
  6669. (defvar org-create-file-search-functions nil
  6670. "List of functions to construct the right search string for a file link.
  6671. These functions are called in turn with point at the location to
  6672. which the link should point.
  6673. A function in the hook should first test if it would like to
  6674. handle this file type, for example by checking the major-mode or
  6675. the file extension. If it decides not to handle this file, it
  6676. should just return nil to give other functions a chance. If it
  6677. does handle the file, it must return the search string to be used
  6678. when following the link. The search string will be part of the
  6679. file link, given after a double colon, and `org-open-at-point'
  6680. will automatically search for it. If special measures must be
  6681. taken to make the search successful, another function should be
  6682. added to the companion hook `org-execute-file-search-functions',
  6683. which see.
  6684. A function in this hook may also use `setq' to set the variable
  6685. `description' to provide a suggestion for the descriptive text to
  6686. be used for this link when it gets inserted into an Org-mode
  6687. buffer with \\[org-insert-link].")
  6688. (defvar org-execute-file-search-functions nil
  6689. "List of functions to execute a file search triggered by a link.
  6690. Functions added to this hook must accept a single argument, the
  6691. search string that was part of the file link, the part after the
  6692. double colon. The function must first check if it would like to
  6693. handle this search, for example by checking the major-mode or the
  6694. file extension. If it decides not to handle this search, it
  6695. should just return nil to give other functions a chance. If it
  6696. does handle the search, it must return a non-nil value to keep
  6697. other functions from trying.
  6698. Each function can access the current prefix argument through the
  6699. variable `current-prefix-argument'. Note that a single prefix is
  6700. used to force opening a link in Emacs, so it may be good to only
  6701. use a numeric or double prefix to guide the search function.
  6702. In case this is needed, a function in this hook can also restore
  6703. the window configuration before `org-open-at-point' was called using:
  6704. (set-window-configuration org-window-config-before-follow-link)")
  6705. (defun org-link-search (s &optional type avoid-pos)
  6706. "Search for a link search option.
  6707. If S is surrounded by forward slashes, it is interpreted as a
  6708. regular expression. In org-mode files, this will create an `org-occur'
  6709. sparse tree. In ordinary files, `occur' will be used to list matches.
  6710. If the current buffer is in `dired-mode', grep will be used to search
  6711. in all files. If AVOID-POS is given, ignore matches near that position."
  6712. (let ((case-fold-search t)
  6713. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6714. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6715. (append '(("") (" ") ("\t") ("\n"))
  6716. org-emphasis-alist)
  6717. "\\|") "\\)"))
  6718. (pos (point))
  6719. (pre nil) (post nil)
  6720. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6721. (cond
  6722. ;; First check if there are any special
  6723. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6724. ;; Now try the builtin stuff
  6725. ((save-excursion
  6726. (goto-char (point-min))
  6727. (and
  6728. (re-search-forward
  6729. (concat "<<" (regexp-quote s0) ">>") nil t)
  6730. (setq type 'dedicated
  6731. pos (match-beginning 0))))
  6732. ;; There is an exact target for this
  6733. (goto-char pos))
  6734. ((and (string-match "^(\\(.*\\))$" s0)
  6735. (save-excursion
  6736. (goto-char (point-min))
  6737. (and
  6738. (re-search-forward
  6739. (concat "[^[]" (regexp-quote
  6740. (format org-coderef-label-format
  6741. (match-string 1 s0))))
  6742. nil t)
  6743. (setq type 'dedicated
  6744. pos (1+ (match-beginning 0))))))
  6745. ;; There is a coderef target for this
  6746. (goto-char pos))
  6747. ((string-match "^/\\(.*\\)/$" s)
  6748. ;; A regular expression
  6749. (cond
  6750. ((org-mode-p)
  6751. (org-occur (match-string 1 s)))
  6752. ;;((eq major-mode 'dired-mode)
  6753. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6754. (t (org-do-occur (match-string 1 s)))))
  6755. (t
  6756. ;; A normal search strings
  6757. (when (equal (string-to-char s) ?*)
  6758. ;; Anchor on headlines, post may include tags.
  6759. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6760. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6761. s (substring s 1)))
  6762. (remove-text-properties
  6763. 0 (length s)
  6764. '(face nil mouse-face nil keymap nil fontified nil) s)
  6765. ;; Make a series of regular expressions to find a match
  6766. (setq words (org-split-string s "[ \n\r\t]+")
  6767. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6768. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6769. "\\)" markers)
  6770. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6771. re2a (concat "[ \t\r\n]" re2a_)
  6772. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6773. re4 (concat "[^a-zA-Z_]" re4_)
  6774. re1 (concat pre re2 post)
  6775. re3 (concat pre (if pre re4_ re4) post)
  6776. re5 (concat pre ".*" re4)
  6777. re2 (concat pre re2)
  6778. re2a (concat pre (if pre re2a_ re2a))
  6779. re4 (concat pre (if pre re4_ re4))
  6780. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6781. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6782. re5 "\\)"
  6783. ))
  6784. (cond
  6785. ((eq type 'org-occur) (org-occur reall))
  6786. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6787. (t (goto-char (point-min))
  6788. (setq type 'fuzzy)
  6789. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6790. (org-search-not-self 1 re1 nil t)
  6791. (org-search-not-self 1 re2 nil t)
  6792. (org-search-not-self 1 re2a nil t)
  6793. (org-search-not-self 1 re3 nil t)
  6794. (org-search-not-self 1 re4 nil t)
  6795. (org-search-not-self 1 re5 nil t)
  6796. )
  6797. (goto-char (match-beginning 1))
  6798. (goto-char pos)
  6799. (error "No match")))))
  6800. (t
  6801. ;; Normal string-search
  6802. (goto-char (point-min))
  6803. (if (search-forward s nil t)
  6804. (goto-char (match-beginning 0))
  6805. (error "No match"))))
  6806. (and (org-mode-p) (org-show-context 'link-search))
  6807. type))
  6808. (defun org-search-not-self (group &rest args)
  6809. "Execute `re-search-forward', but only accept matches that do not
  6810. enclose the position of `org-open-link-marker'."
  6811. (let ((m org-open-link-marker))
  6812. (catch 'exit
  6813. (while (apply 're-search-forward args)
  6814. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6815. (goto-char (match-end group))
  6816. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6817. (> (match-beginning 0) (marker-position m))
  6818. (< (match-end 0) (marker-position m)))
  6819. (save-match-data
  6820. (or (not (org-in-regexp
  6821. org-bracket-link-analytic-regexp 1))
  6822. (not (match-end 4)) ; no description
  6823. (and (<= (match-beginning 4) (point))
  6824. (>= (match-end 4) (point))))))
  6825. (throw 'exit (point))))))))
  6826. (defun org-get-buffer-for-internal-link (buffer)
  6827. "Return a buffer to be used for displaying the link target of internal links."
  6828. (cond
  6829. ((not org-display-internal-link-with-indirect-buffer)
  6830. buffer)
  6831. ((string-match "(Clone)$" (buffer-name buffer))
  6832. (message "Buffer is already a clone, not making another one")
  6833. ;; we also do not modify visibility in this case
  6834. buffer)
  6835. (t ; make a new indirect buffer for displaying the link
  6836. (let* ((bn (buffer-name buffer))
  6837. (ibn (concat bn "(Clone)"))
  6838. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6839. (with-current-buffer ib (org-overview))
  6840. ib))))
  6841. (defun org-do-occur (regexp &optional cleanup)
  6842. "Call the Emacs command `occur'.
  6843. If CLEANUP is non-nil, remove the printout of the regular expression
  6844. in the *Occur* buffer. This is useful if the regex is long and not useful
  6845. to read."
  6846. (occur regexp)
  6847. (when cleanup
  6848. (let ((cwin (selected-window)) win beg end)
  6849. (when (setq win (get-buffer-window "*Occur*"))
  6850. (select-window win))
  6851. (goto-char (point-min))
  6852. (when (re-search-forward "match[a-z]+" nil t)
  6853. (setq beg (match-end 0))
  6854. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6855. (setq end (1- (match-beginning 0)))))
  6856. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6857. (goto-char (point-min))
  6858. (select-window cwin))))
  6859. ;;; The mark ring for links jumps
  6860. (defvar org-mark-ring nil
  6861. "Mark ring for positions before jumps in Org-mode.")
  6862. (defvar org-mark-ring-last-goto nil
  6863. "Last position in the mark ring used to go back.")
  6864. ;; Fill and close the ring
  6865. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6866. (loop for i from 1 to org-mark-ring-length do
  6867. (push (make-marker) org-mark-ring))
  6868. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6869. org-mark-ring)
  6870. (defun org-mark-ring-push (&optional pos buffer)
  6871. "Put the current position or POS into the mark ring and rotate it."
  6872. (interactive)
  6873. (setq pos (or pos (point)))
  6874. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6875. (move-marker (car org-mark-ring)
  6876. (or pos (point))
  6877. (or buffer (current-buffer)))
  6878. (message "%s"
  6879. (substitute-command-keys
  6880. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6881. (defun org-mark-ring-goto (&optional n)
  6882. "Jump to the previous position in the mark ring.
  6883. With prefix arg N, jump back that many stored positions. When
  6884. called several times in succession, walk through the entire ring.
  6885. Org-mode commands jumping to a different position in the current file,
  6886. or to another Org-mode file, automatically push the old position
  6887. onto the ring."
  6888. (interactive "p")
  6889. (let (p m)
  6890. (if (eq last-command this-command)
  6891. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6892. (setq p org-mark-ring))
  6893. (setq org-mark-ring-last-goto p)
  6894. (setq m (car p))
  6895. (switch-to-buffer (marker-buffer m))
  6896. (goto-char m)
  6897. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6898. (defun org-remove-angle-brackets (s)
  6899. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6900. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6901. s)
  6902. (defun org-add-angle-brackets (s)
  6903. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6904. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6905. s)
  6906. (defun org-remove-double-quotes (s)
  6907. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6908. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6909. s)
  6910. ;;; Following specific links
  6911. (defun org-follow-timestamp-link ()
  6912. (cond
  6913. ((org-at-date-range-p t)
  6914. (let ((org-agenda-start-on-weekday)
  6915. (t1 (match-string 1))
  6916. (t2 (match-string 2)))
  6917. (setq t1 (time-to-days (org-time-string-to-time t1))
  6918. t2 (time-to-days (org-time-string-to-time t2)))
  6919. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6920. ((org-at-timestamp-p t)
  6921. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6922. (substring (match-string 1) 0 10)))
  6923. 1))
  6924. (t (error "This should not happen"))))
  6925. ;;; Following file links
  6926. (defvar org-wait nil)
  6927. (defun org-open-file (path &optional in-emacs line search)
  6928. "Open the file at PATH.
  6929. First, this expands any special file name abbreviations. Then the
  6930. configuration variable `org-file-apps' is checked if it contains an
  6931. entry for this file type, and if yes, the corresponding command is launched.
  6932. If no application is found, Emacs simply visits the file.
  6933. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6934. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6935. and o use an external application to visit the file.
  6936. Optional LINE specifies a line to go to, optional SEARCH a string to
  6937. search for. If LINE or SEARCH is given, the file will always be
  6938. opened in Emacs.
  6939. If the file does not exist, an error is thrown."
  6940. (setq in-emacs (or in-emacs line search))
  6941. (let* ((file (if (equal path "")
  6942. buffer-file-name
  6943. (substitute-in-file-name (expand-file-name path))))
  6944. (apps (append org-file-apps (org-default-apps)))
  6945. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6946. (dirp (if remp nil (file-directory-p file)))
  6947. (file (if (and dirp org-open-directory-means-index-dot-org)
  6948. (concat (file-name-as-directory file) "index.org")
  6949. file))
  6950. (a-m-a-p (assq 'auto-mode apps))
  6951. (dfile (downcase file))
  6952. (old-buffer (current-buffer))
  6953. (old-pos (point))
  6954. (old-mode major-mode)
  6955. ext cmd)
  6956. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6957. (setq ext (match-string 1 dfile))
  6958. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6959. (setq ext (match-string 1 dfile))))
  6960. (cond
  6961. ((equal in-emacs '(16))
  6962. (setq cmd (cdr (assoc 'system apps))))
  6963. (in-emacs (setq cmd 'emacs))
  6964. (t
  6965. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6966. (and dirp (cdr (assoc 'directory apps)))
  6967. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6968. 'string-match)
  6969. (cdr (assoc ext apps))
  6970. (cdr (assoc t apps))))))
  6971. (when (eq cmd 'system)
  6972. (setq cmd (cdr (assoc 'system apps))))
  6973. (when (eq cmd 'default)
  6974. (setq cmd (cdr (assoc t apps))))
  6975. (when (eq cmd 'mailcap)
  6976. (require 'mailcap)
  6977. (mailcap-parse-mailcaps)
  6978. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6979. (command (mailcap-mime-info mime-type)))
  6980. (if (stringp command)
  6981. (setq cmd command)
  6982. (setq cmd 'emacs))))
  6983. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6984. (not (file-exists-p file))
  6985. (not org-open-non-existing-files))
  6986. (error "No such file: %s" file))
  6987. (cond
  6988. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6989. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6990. (while (string-match "['\"]%s['\"]" cmd)
  6991. (setq cmd (replace-match "%s" t t cmd)))
  6992. (while (string-match "%s" cmd)
  6993. (setq cmd (replace-match
  6994. (save-match-data
  6995. (shell-quote-argument
  6996. (convert-standard-filename file)))
  6997. t t cmd)))
  6998. (save-window-excursion
  6999. (start-process-shell-command cmd nil cmd)
  7000. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  7001. ))
  7002. ((or (stringp cmd)
  7003. (eq cmd 'emacs))
  7004. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  7005. (widen)
  7006. (if line (goto-line line)
  7007. (if search (org-link-search search))))
  7008. ((consp cmd)
  7009. (let ((file (convert-standard-filename file)))
  7010. (eval cmd)))
  7011. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  7012. (and (org-mode-p) (eq old-mode 'org-mode)
  7013. (or (not (equal old-buffer (current-buffer)))
  7014. (not (equal old-pos (point))))
  7015. (org-mark-ring-push old-pos old-buffer))))
  7016. (defun org-default-apps ()
  7017. "Return the default applications for this operating system."
  7018. (cond
  7019. ((eq system-type 'darwin)
  7020. org-file-apps-defaults-macosx)
  7021. ((eq system-type 'windows-nt)
  7022. org-file-apps-defaults-windowsnt)
  7023. (t org-file-apps-defaults-gnu)))
  7024. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  7025. "Convert extensions to regular expressions in the cars of LIST.
  7026. Also, weed out any non-string entries, because the return value is used
  7027. only for regexp matching.
  7028. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  7029. point to the symbol `emacs', indicating that the file should
  7030. be opened in Emacs."
  7031. (append
  7032. (delq nil
  7033. (mapcar (lambda (x)
  7034. (if (not (stringp (car x)))
  7035. nil
  7036. (if (string-match "\\W" (car x))
  7037. x
  7038. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  7039. list))
  7040. (if add-auto-mode
  7041. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  7042. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  7043. (defun org-file-remote-p (file)
  7044. "Test whether FILE specifies a location on a remote system.
  7045. Return non-nil if the location is indeed remote.
  7046. For example, the filename \"/user@host:/foo\" specifies a location
  7047. on the system \"/user@host:\"."
  7048. (cond ((fboundp 'file-remote-p)
  7049. (file-remote-p file))
  7050. ((fboundp 'tramp-handle-file-remote-p)
  7051. (tramp-handle-file-remote-p file))
  7052. ((and (boundp 'ange-ftp-name-format)
  7053. (string-match (car ange-ftp-name-format) file))
  7054. t)
  7055. (t nil)))
  7056. ;;;; Refiling
  7057. (defun org-get-org-file ()
  7058. "Read a filename, with default directory `org-directory'."
  7059. (let ((default (or org-default-notes-file remember-data-file)))
  7060. (read-file-name (format "File name [%s]: " default)
  7061. (file-name-as-directory org-directory)
  7062. default)))
  7063. (defun org-notes-order-reversed-p ()
  7064. "Check if the current file should receive notes in reversed order."
  7065. (cond
  7066. ((not org-reverse-note-order) nil)
  7067. ((eq t org-reverse-note-order) t)
  7068. ((not (listp org-reverse-note-order)) nil)
  7069. (t (catch 'exit
  7070. (let ((all org-reverse-note-order)
  7071. entry)
  7072. (while (setq entry (pop all))
  7073. (if (string-match (car entry) buffer-file-name)
  7074. (throw 'exit (cdr entry))))
  7075. nil)))))
  7076. (defvar org-refile-target-table nil
  7077. "The list of refile targets, created by `org-refile'.")
  7078. (defvar org-agenda-new-buffers nil
  7079. "Buffers created to visit agenda files.")
  7080. (defun org-get-refile-targets (&optional default-buffer)
  7081. "Produce a table with refile targets."
  7082. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  7083. targets txt re files f desc descre fast-path-p level)
  7084. (message "Getting targets...")
  7085. (with-current-buffer (or default-buffer (current-buffer))
  7086. (while (setq entry (pop entries))
  7087. (setq files (car entry) desc (cdr entry))
  7088. (setq fast-path-p nil)
  7089. (cond
  7090. ((null files) (setq files (list (current-buffer))))
  7091. ((eq files 'org-agenda-files)
  7092. (setq files (org-agenda-files 'unrestricted)))
  7093. ((and (symbolp files) (fboundp files))
  7094. (setq files (funcall files)))
  7095. ((and (symbolp files) (boundp files))
  7096. (setq files (symbol-value files))))
  7097. (if (stringp files) (setq files (list files)))
  7098. (cond
  7099. ((eq (car desc) :tag)
  7100. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  7101. ((eq (car desc) :todo)
  7102. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  7103. ((eq (car desc) :regexp)
  7104. (setq descre (cdr desc)))
  7105. ((eq (car desc) :level)
  7106. (setq descre (concat "^\\*\\{" (number-to-string
  7107. (if org-odd-levels-only
  7108. (1- (* 2 (cdr desc)))
  7109. (cdr desc)))
  7110. "\\}[ \t]")))
  7111. ((eq (car desc) :maxlevel)
  7112. (setq fast-path-p t)
  7113. (setq descre (concat "^\\*\\{1," (number-to-string
  7114. (if org-odd-levels-only
  7115. (1- (* 2 (cdr desc)))
  7116. (cdr desc)))
  7117. "\\}[ \t]")))
  7118. (t (error "Bad refiling target description %s" desc)))
  7119. (while (setq f (pop files))
  7120. (save-excursion
  7121. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  7122. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  7123. (setq f (expand-file-name f))
  7124. (save-excursion
  7125. (save-restriction
  7126. (widen)
  7127. (goto-char (point-min))
  7128. (while (re-search-forward descre nil t)
  7129. (goto-char (point-at-bol))
  7130. (when (looking-at org-complex-heading-regexp)
  7131. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  7132. txt (org-link-display-format (match-string 4))
  7133. re (concat "^" (regexp-quote
  7134. (buffer-substring (match-beginning 1)
  7135. (match-end 4)))))
  7136. (if (match-end 5) (setq re (concat re "[ \t]+"
  7137. (regexp-quote
  7138. (match-string 5)))))
  7139. (setq re (concat re "[ \t]*$"))
  7140. (when org-refile-use-outline-path
  7141. (setq txt (mapconcat 'org-protect-slash
  7142. (append
  7143. (if (eq org-refile-use-outline-path 'file)
  7144. (list (file-name-nondirectory
  7145. (buffer-file-name (buffer-base-buffer))))
  7146. (if (eq org-refile-use-outline-path 'full-file-path)
  7147. (list (buffer-file-name (buffer-base-buffer)))))
  7148. (org-get-outline-path fast-path-p level txt)
  7149. (list txt))
  7150. "/")))
  7151. (push (list txt f re (point)) targets))
  7152. (goto-char (point-at-eol))))))))
  7153. (message "Getting targets...done")
  7154. (nreverse targets))))
  7155. (defun org-protect-slash (s)
  7156. (while (string-match "/" s)
  7157. (setq s (replace-match "\\" t t s)))
  7158. s)
  7159. (defvar org-olpa (make-vector 20 nil))
  7160. (defun org-get-outline-path (&optional fastp level heading)
  7161. "Return the outline path to the current entry, as a list."
  7162. (if fastp
  7163. (progn
  7164. (if (> level 19)
  7165. (error "Outline path failure, more than 19 levels."))
  7166. (loop for i from level upto 19 do
  7167. (aset org-olpa i nil))
  7168. (prog1
  7169. (delq nil (append org-olpa nil))
  7170. (aset org-olpa level heading)))
  7171. (let (rtn)
  7172. (save-excursion
  7173. (while (org-up-heading-safe)
  7174. (when (looking-at org-complex-heading-regexp)
  7175. (push (org-match-string-no-properties 4) rtn)))
  7176. rtn))))
  7177. (defvar org-refile-history nil
  7178. "History for refiling operations.")
  7179. (defun org-refile (&optional goto default-buffer)
  7180. "Move the entry at point to another heading.
  7181. The list of target headings is compiled using the information in
  7182. `org-refile-targets', which see. This list is created before each use
  7183. and will therefore always be up-to-date.
  7184. At the target location, the entry is filed as a subitem of the target heading.
  7185. Depending on `org-reverse-note-order', the new subitem will either be the
  7186. first or the last subitem.
  7187. If there is an active region, all entries in that region will be moved.
  7188. However, the region must fulfil the requirement that the first heading
  7189. is the first one sets the top-level of the moved text - at most siblings
  7190. below it are allowed.
  7191. With prefix arg GOTO, the command will only visit the target location,
  7192. not actually move anything.
  7193. With a double prefix `C-u C-u', go to the location where the last refiling
  7194. operation has put the subtree."
  7195. (interactive "P")
  7196. (let* ((cbuf (current-buffer))
  7197. (regionp (org-region-active-p))
  7198. (region-start (and regionp (region-beginning)))
  7199. (region-end (and regionp (region-end)))
  7200. (region-length (and regionp (- region-end region-start)))
  7201. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7202. pos it nbuf file re level reversed)
  7203. (when regionp (goto-char region-start)
  7204. (unless (org-kill-is-subtree-p
  7205. (buffer-substring region-start region-end))
  7206. (error "The region is not a (sequence of) subtree(s)")))
  7207. (if (equal goto '(16))
  7208. (org-refile-goto-last-stored)
  7209. (when (setq it (org-refile-get-location
  7210. (if goto "Goto: " "Refile to: ") default-buffer))
  7211. (setq file (nth 1 it)
  7212. re (nth 2 it)
  7213. pos (nth 3 it))
  7214. (if (and (equal (buffer-file-name) file)
  7215. (if regionp
  7216. (and (>= pos region-start)
  7217. (<= pos region-end))
  7218. (and (>= pos (point))
  7219. (< pos (save-excursion
  7220. (org-end-of-subtree t t))))))
  7221. (error "Cannot refile to position inside the tree or region"))
  7222. (setq nbuf (or (find-buffer-visiting file)
  7223. (find-file-noselect file)))
  7224. (if goto
  7225. (progn
  7226. (switch-to-buffer nbuf)
  7227. (goto-char pos)
  7228. (org-show-context 'org-goto))
  7229. (if regionp
  7230. (progn
  7231. (kill-new (buffer-substring region-start region-end))
  7232. (org-save-markers-in-region region-start region-end))
  7233. (org-copy-subtree 1 nil t))
  7234. (save-excursion
  7235. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7236. (find-file-noselect file))))
  7237. (setq reversed (org-notes-order-reversed-p))
  7238. (save-excursion
  7239. (save-restriction
  7240. (widen)
  7241. (goto-char pos)
  7242. (looking-at outline-regexp)
  7243. (setq level (org-get-valid-level (funcall outline-level) 1))
  7244. (goto-char
  7245. (if reversed
  7246. (or (outline-next-heading) (point-max))
  7247. (or (save-excursion (outline-get-next-sibling))
  7248. (org-end-of-subtree t t)
  7249. (point-max))))
  7250. (if (not (bolp)) (newline))
  7251. (bookmark-set "org-refile-last-stored")
  7252. (org-paste-subtree level))))
  7253. (if regionp
  7254. (delete-region (point) (+ (point) region-length))
  7255. (org-cut-subtree))
  7256. (setq org-markers-to-move nil)
  7257. (message "Refiled to \"%s\"" (car it)))))))
  7258. (defun org-refile-goto-last-stored ()
  7259. "Go to the location where the last refile was stored."
  7260. (interactive)
  7261. (bookmark-jump "org-refile-last-stored")
  7262. (message "This is the location of the last refile"))
  7263. (defun org-refile-get-location (&optional prompt default-buffer)
  7264. "Prompt the user for a refile location, using PROMPT."
  7265. (let ((org-refile-targets org-refile-targets)
  7266. (org-refile-use-outline-path org-refile-use-outline-path))
  7267. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7268. (unless org-refile-target-table
  7269. (error "No refile targets"))
  7270. (let* ((cbuf (current-buffer))
  7271. (partial-completion-mode nil)
  7272. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  7273. (cfunc (if (and org-refile-use-outline-path
  7274. org-outline-path-complete-in-steps)
  7275. 'org-olpath-completing-read
  7276. 'org-ido-completing-read))
  7277. (extra (if org-refile-use-outline-path "/" ""))
  7278. (filename (and cfn (expand-file-name cfn)))
  7279. (tbl (mapcar
  7280. (lambda (x)
  7281. (if (not (equal filename (nth 1 x)))
  7282. (cons (concat (car x) extra " ("
  7283. (file-name-nondirectory (nth 1 x)) ")")
  7284. (cdr x))
  7285. (cons (concat (car x) extra) (cdr x))))
  7286. org-refile-target-table))
  7287. (completion-ignore-case t))
  7288. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7289. tbl)))
  7290. (defun org-olpath-completing-read (prompt collection &rest args)
  7291. "Read an outline path like a file name."
  7292. (let ((thetable collection)
  7293. (org-completion-use-ido nil)) ; does not work with ido.
  7294. (apply
  7295. 'org-ido-completing-read prompt
  7296. (lambda (string predicate &optional flag)
  7297. (let (rtn r f (l (length string)))
  7298. (cond
  7299. ((eq flag nil)
  7300. ;; try completion
  7301. (try-completion string thetable))
  7302. ((eq flag t)
  7303. ;; all-completions
  7304. (setq rtn (all-completions string thetable predicate))
  7305. (mapcar
  7306. (lambda (x)
  7307. (setq r (substring x l))
  7308. (if (string-match " ([^)]*)$" x)
  7309. (setq f (match-string 0 x))
  7310. (setq f ""))
  7311. (if (string-match "/" r)
  7312. (concat string (substring r 0 (match-end 0)) f)
  7313. x))
  7314. rtn))
  7315. ((eq flag 'lambda)
  7316. ;; exact match?
  7317. (assoc string thetable)))
  7318. ))
  7319. args)))
  7320. ;;;; Dynamic blocks
  7321. (defun org-find-dblock (name)
  7322. "Find the first dynamic block with name NAME in the buffer.
  7323. If not found, stay at current position and return nil."
  7324. (let (pos)
  7325. (save-excursion
  7326. (goto-char (point-min))
  7327. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7328. nil t)
  7329. (match-beginning 0))))
  7330. (if pos (goto-char pos))
  7331. pos))
  7332. (defconst org-dblock-start-re
  7333. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7334. "Matches the startline of a dynamic block, with parameters.")
  7335. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7336. "Matches the end of a dynamic block.")
  7337. (defun org-create-dblock (plist)
  7338. "Create a dynamic block section, with parameters taken from PLIST.
  7339. PLIST must contain a :name entry which is used as name of the block."
  7340. (unless (bolp) (newline))
  7341. (let ((name (plist-get plist :name)))
  7342. (insert "#+BEGIN: " name)
  7343. (while plist
  7344. (if (eq (car plist) :name)
  7345. (setq plist (cddr plist))
  7346. (insert " " (prin1-to-string (pop plist)))))
  7347. (insert "\n\n#+END:\n")
  7348. (beginning-of-line -2)))
  7349. (defun org-prepare-dblock ()
  7350. "Prepare dynamic block for refresh.
  7351. This empties the block, puts the cursor at the insert position and returns
  7352. the property list including an extra property :name with the block name."
  7353. (unless (looking-at org-dblock-start-re)
  7354. (error "Not at a dynamic block"))
  7355. (let* ((begdel (1+ (match-end 0)))
  7356. (name (org-no-properties (match-string 1)))
  7357. (params (append (list :name name)
  7358. (read (concat "(" (match-string 3) ")")))))
  7359. (unless (re-search-forward org-dblock-end-re nil t)
  7360. (error "Dynamic block not terminated"))
  7361. (setq params
  7362. (append params
  7363. (list :content (buffer-substring
  7364. begdel (match-beginning 0)))))
  7365. (delete-region begdel (match-beginning 0))
  7366. (goto-char begdel)
  7367. (open-line 1)
  7368. params))
  7369. (defun org-map-dblocks (&optional command)
  7370. "Apply COMMAND to all dynamic blocks in the current buffer.
  7371. If COMMAND is not given, use `org-update-dblock'."
  7372. (let ((cmd (or command 'org-update-dblock))
  7373. pos)
  7374. (save-excursion
  7375. (goto-char (point-min))
  7376. (while (re-search-forward org-dblock-start-re nil t)
  7377. (goto-char (setq pos (match-beginning 0)))
  7378. (condition-case nil
  7379. (funcall cmd)
  7380. (error (message "Error during update of dynamic block")))
  7381. (goto-char pos)
  7382. (unless (re-search-forward org-dblock-end-re nil t)
  7383. (error "Dynamic block not terminated"))))))
  7384. (defun org-dblock-update (&optional arg)
  7385. "User command for updating dynamic blocks.
  7386. Update the dynamic block at point. With prefix ARG, update all dynamic
  7387. blocks in the buffer."
  7388. (interactive "P")
  7389. (if arg
  7390. (org-update-all-dblocks)
  7391. (or (looking-at org-dblock-start-re)
  7392. (org-beginning-of-dblock))
  7393. (org-update-dblock)))
  7394. (defun org-update-dblock ()
  7395. "Update the dynamic block at point
  7396. This means to empty the block, parse for parameters and then call
  7397. the correct writing function."
  7398. (save-window-excursion
  7399. (let* ((pos (point))
  7400. (line (org-current-line))
  7401. (params (org-prepare-dblock))
  7402. (name (plist-get params :name))
  7403. (cmd (intern (concat "org-dblock-write:" name))))
  7404. (message "Updating dynamic block `%s' at line %d..." name line)
  7405. (funcall cmd params)
  7406. (message "Updating dynamic block `%s' at line %d...done" name line)
  7407. (goto-char pos))))
  7408. (defun org-beginning-of-dblock ()
  7409. "Find the beginning of the dynamic block at point.
  7410. Error if there is no such block at point."
  7411. (let ((pos (point))
  7412. beg)
  7413. (end-of-line 1)
  7414. (if (and (re-search-backward org-dblock-start-re nil t)
  7415. (setq beg (match-beginning 0))
  7416. (re-search-forward org-dblock-end-re nil t)
  7417. (> (match-end 0) pos))
  7418. (goto-char beg)
  7419. (goto-char pos)
  7420. (error "Not in a dynamic block"))))
  7421. (defun org-update-all-dblocks ()
  7422. "Update all dynamic blocks in the buffer.
  7423. This function can be used in a hook."
  7424. (when (org-mode-p)
  7425. (org-map-dblocks 'org-update-dblock)))
  7426. ;;;; Completion
  7427. (defconst org-additional-option-like-keywords
  7428. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7429. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7430. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7431. "BEGIN_QUOTE" "END_QUOTE"
  7432. "BEGIN_VERSE" "END_VERSE"
  7433. "BEGIN_SRC" "END_SRC"
  7434. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7435. (defcustom org-structure-template-alist
  7436. '(
  7437. ("s" "#+begin_src ?\n\n#+end_src"
  7438. "<src lang=\"?\">\n\n</src>")
  7439. ("e" "#+begin_example\n?\n#+end_example"
  7440. "<example>\n?\n</example>")
  7441. ("q" "#+begin_quote\n?\n#+end_quote"
  7442. "<quote>\n?\n</quote>")
  7443. ("v" "#+begin_verse\n?\n#+end_verse"
  7444. "<verse>\n?\n/verse>")
  7445. ("l" "#+begin_latex\n?\n#+end_latex"
  7446. "<literal style=\"latex\">\n?\n</literal>")
  7447. ("L" "#+latex: "
  7448. "<literal style=\"latex\">?</literal>")
  7449. ("h" "#+begin_html\n?\n#+end_html"
  7450. "<literal style=\"html\">\n?\n</literal>")
  7451. ("H" "#+html: "
  7452. "<literal style=\"html\">?</literal>")
  7453. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7454. ("A" "#+ascii: ")
  7455. ("i" "#+include %file ?"
  7456. "<include file=%file markup=\"?\">")
  7457. )
  7458. "Structure completion elements.
  7459. This is a list of abbreviation keys and values. The value gets inserted
  7460. it you type @samp{.} followed by the key and then the completion key,
  7461. usually `M-TAB'. %file will be replaced by a file name after prompting
  7462. for the file using completion.
  7463. There are two templates for each key, the first uses the original Org syntax,
  7464. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7465. the default when the /org-mtags.el/ module has been loaded. See also the
  7466. variable `org-mtags-prefer-muse-templates'.
  7467. This is an experimental feature, it is undecided if it is going to stay in."
  7468. :group 'org-completion
  7469. :type '(repeat
  7470. (string :tag "Key")
  7471. (string :tag "Template")
  7472. (string :tag "Muse Template")))
  7473. (defun org-try-structure-completion ()
  7474. "Try to complete a structure template before point.
  7475. This looks for strings like \"<e\" on an otherwise empty line and
  7476. expands them."
  7477. (let ((l (buffer-substring (point-at-bol) (point)))
  7478. a)
  7479. (when (and (looking-at "[ \t]*$")
  7480. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7481. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7482. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7483. (match-beginning 1)) a)
  7484. t)))
  7485. (defun org-complete-expand-structure-template (start cell)
  7486. "Expand a structure template."
  7487. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7488. (rpl (nth (if musep 2 1) cell)))
  7489. (delete-region start (point))
  7490. (when (string-match "\\`#\\+" rpl)
  7491. (cond
  7492. ((bolp))
  7493. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7494. (delete-region (point-at-bol) (point)))
  7495. (t (newline))))
  7496. (setq start (point))
  7497. (if (string-match "%file" rpl)
  7498. (setq rpl (replace-match
  7499. (concat
  7500. "\""
  7501. (save-match-data
  7502. (abbreviate-file-name (read-file-name "Include file: ")))
  7503. "\"")
  7504. t t rpl)))
  7505. (insert rpl)
  7506. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7507. (defun org-complete (&optional arg)
  7508. "Perform completion on word at point.
  7509. At the beginning of a headline, this completes TODO keywords as given in
  7510. `org-todo-keywords'.
  7511. If the current word is preceded by a backslash, completes the TeX symbols
  7512. that are supported for HTML support.
  7513. If the current word is preceded by \"#+\", completes special words for
  7514. setting file options.
  7515. In the line after \"#+STARTUP:, complete valid keywords.\"
  7516. At all other locations, this simply calls the value of
  7517. `org-completion-fallback-command'."
  7518. (interactive "P")
  7519. (org-without-partial-completion
  7520. (catch 'exit
  7521. (let* ((a nil)
  7522. (end (point))
  7523. (beg1 (save-excursion
  7524. (skip-chars-backward (org-re "[:alnum:]_@"))
  7525. (point)))
  7526. (beg (save-excursion
  7527. (skip-chars-backward "a-zA-Z0-9_:$")
  7528. (point)))
  7529. (confirm (lambda (x) (stringp (car x))))
  7530. (searchhead (equal (char-before beg) ?*))
  7531. (struct
  7532. (when (and (member (char-before beg1) '(?. ?<))
  7533. (setq a (assoc (buffer-substring beg1 (point))
  7534. org-structure-template-alist)))
  7535. (org-complete-expand-structure-template (1- beg1) a)
  7536. (throw 'exit t)))
  7537. (tag (and (equal (char-before beg1) ?:)
  7538. (equal (char-after (point-at-bol)) ?*)))
  7539. (prop (and (equal (char-before beg1) ?:)
  7540. (not (equal (char-after (point-at-bol)) ?*))))
  7541. (texp (equal (char-before beg) ?\\))
  7542. (link (equal (char-before beg) ?\[))
  7543. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7544. beg)
  7545. "#+"))
  7546. (startup (string-match "^#\\+STARTUP:.*"
  7547. (buffer-substring (point-at-bol) (point))))
  7548. (completion-ignore-case opt)
  7549. (type nil)
  7550. (tbl nil)
  7551. (table (cond
  7552. (opt
  7553. (setq type :opt)
  7554. (require 'org-exp)
  7555. (append
  7556. (mapcar
  7557. (lambda (x)
  7558. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7559. (cons (match-string 2 x) (match-string 1 x)))
  7560. (org-split-string (org-get-current-options) "\n"))
  7561. (mapcar 'list org-additional-option-like-keywords)))
  7562. (startup
  7563. (setq type :startup)
  7564. org-startup-options)
  7565. (link (append org-link-abbrev-alist-local
  7566. org-link-abbrev-alist))
  7567. (texp
  7568. (setq type :tex)
  7569. org-html-entities)
  7570. ((string-match "\\`\\*+[ \t]+\\'"
  7571. (buffer-substring (point-at-bol) beg))
  7572. (setq type :todo)
  7573. (mapcar 'list org-todo-keywords-1))
  7574. (searchhead
  7575. (setq type :searchhead)
  7576. (save-excursion
  7577. (goto-char (point-min))
  7578. (while (re-search-forward org-todo-line-regexp nil t)
  7579. (push (list
  7580. (org-make-org-heading-search-string
  7581. (match-string 3) t))
  7582. tbl)))
  7583. tbl)
  7584. (tag (setq type :tag beg beg1)
  7585. (or org-tag-alist (org-get-buffer-tags)))
  7586. (prop (setq type :prop beg beg1)
  7587. (mapcar 'list (org-buffer-property-keys nil t t)))
  7588. (t (progn
  7589. (call-interactively org-completion-fallback-command)
  7590. (throw 'exit nil)))))
  7591. (pattern (buffer-substring-no-properties beg end))
  7592. (completion (try-completion pattern table confirm)))
  7593. (cond ((eq completion t)
  7594. (if (not (assoc (upcase pattern) table))
  7595. (message "Already complete")
  7596. (if (and (equal type :opt)
  7597. (not (member (car (assoc (upcase pattern) table))
  7598. org-additional-option-like-keywords)))
  7599. (insert (substring (cdr (assoc (upcase pattern) table))
  7600. (length pattern)))
  7601. (if (memq type '(:tag :prop)) (insert ":")))))
  7602. ((null completion)
  7603. (message "Can't find completion for \"%s\"" pattern)
  7604. (ding))
  7605. ((not (string= pattern completion))
  7606. (delete-region beg end)
  7607. (if (string-match " +$" completion)
  7608. (setq completion (replace-match "" t t completion)))
  7609. (insert completion)
  7610. (if (get-buffer-window "*Completions*")
  7611. (delete-window (get-buffer-window "*Completions*")))
  7612. (if (assoc completion table)
  7613. (if (eq type :todo) (insert " ")
  7614. (if (memq type '(:tag :prop)) (insert ":"))))
  7615. (if (and (equal type :opt) (assoc completion table))
  7616. (message "%s" (substitute-command-keys
  7617. "Press \\[org-complete] again to insert example settings"))))
  7618. (t
  7619. (message "Making completion list...")
  7620. (let ((list (sort (all-completions pattern table confirm)
  7621. 'string<)))
  7622. (with-output-to-temp-buffer "*Completions*"
  7623. (condition-case nil
  7624. ;; Protection needed for XEmacs and emacs 21
  7625. (display-completion-list list pattern)
  7626. (error (display-completion-list list)))))
  7627. (message "Making completion list...%s" "done")))))))
  7628. ;;;; TODO, DEADLINE, Comments
  7629. (defun org-toggle-comment ()
  7630. "Change the COMMENT state of an entry."
  7631. (interactive)
  7632. (save-excursion
  7633. (org-back-to-heading)
  7634. (let (case-fold-search)
  7635. (if (looking-at (concat outline-regexp
  7636. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7637. (replace-match "" t t nil 1)
  7638. (if (looking-at outline-regexp)
  7639. (progn
  7640. (goto-char (match-end 0))
  7641. (insert org-comment-string " ")))))))
  7642. (defvar org-last-todo-state-is-todo nil
  7643. "This is non-nil when the last TODO state change led to a TODO state.
  7644. If the last change removed the TODO tag or switched to DONE, then
  7645. this is nil.")
  7646. (defvar org-setting-tags nil) ; dynamically skipped
  7647. (defun org-parse-local-options (string var)
  7648. "Parse STRING for startup setting relevant for variable VAR."
  7649. (let ((rtn (symbol-value var))
  7650. e opts)
  7651. (save-match-data
  7652. (if (or (not string) (not (string-match "\\S-" string)))
  7653. rtn
  7654. (setq opts (delq nil (mapcar (lambda (x)
  7655. (setq e (assoc x org-startup-options))
  7656. (if (eq (nth 1 e) var) e nil))
  7657. (org-split-string string "[ \t]+"))))
  7658. (if (not opts)
  7659. rtn
  7660. (setq rtn nil)
  7661. (while (setq e (pop opts))
  7662. (if (not (nth 3 e))
  7663. (setq rtn (nth 2 e))
  7664. (if (not (listp rtn)) (setq rtn nil))
  7665. (push (nth 2 e) rtn)))
  7666. rtn)))))
  7667. (defvar org-todo-setup-filter-hook nil
  7668. "Hook for functions that pre-filter todo specs.
  7669. Each function takes a todo spec and returns either `nil' or the spec
  7670. transformed into canonical form." )
  7671. (defvar org-todo-get-default-hook nil
  7672. "Hook for functions that get a default item for todo.
  7673. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  7674. `nil' or a string to be used for the todo mark." )
  7675. (defvar org-agenda-headline-snapshot-before-repeat)
  7676. (defun org-todo (&optional arg)
  7677. "Change the TODO state of an item.
  7678. The state of an item is given by a keyword at the start of the heading,
  7679. like
  7680. *** TODO Write paper
  7681. *** DONE Call mom
  7682. The different keywords are specified in the variable `org-todo-keywords'.
  7683. By default the available states are \"TODO\" and \"DONE\".
  7684. So for this example: when the item starts with TODO, it is changed to DONE.
  7685. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7686. DONE are present, add TODO at the beginning of the heading.
  7687. With C-u prefix arg, use completion to determine the new state.
  7688. With numeric prefix arg, switch to that state.
  7689. With a double C-u prefix, switch to the next set of TODO keywords (nextset).
  7690. With a tripple C-u prefix, circumvent any state blocking.
  7691. For calling through lisp, arg is also interpreted in the following way:
  7692. 'none -> empty state
  7693. \"\"(empty string) -> switch to empty state
  7694. 'done -> switch to DONE
  7695. 'nextset -> switch to the next set of keywords
  7696. 'previousset -> switch to the previous set of keywords
  7697. \"WAITING\" -> switch to the specified keyword, but only if it
  7698. really is a member of `org-todo-keywords'."
  7699. (interactive "P")
  7700. (if (equal arg '(16)) (setq arg 'nextset))
  7701. (let ((org-blocker-hook org-blocker-hook))
  7702. (when (equal arg '(64))
  7703. (setq arg nil org-blocker-hook nil))
  7704. (save-excursion
  7705. (catch 'exit
  7706. (org-back-to-heading)
  7707. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7708. (or (looking-at (concat " +" org-todo-regexp " *"))
  7709. (looking-at " *"))
  7710. (let* ((match-data (match-data))
  7711. (startpos (point-at-bol))
  7712. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7713. (org-log-done org-log-done)
  7714. (org-log-repeat org-log-repeat)
  7715. (org-todo-log-states org-todo-log-states)
  7716. (this (match-string 1))
  7717. (hl-pos (match-beginning 0))
  7718. (head (org-get-todo-sequence-head this))
  7719. (ass (assoc head org-todo-kwd-alist))
  7720. (interpret (nth 1 ass))
  7721. (done-word (nth 3 ass))
  7722. (final-done-word (nth 4 ass))
  7723. (last-state (or this ""))
  7724. (completion-ignore-case t)
  7725. (member (member this org-todo-keywords-1))
  7726. (tail (cdr member))
  7727. (state (cond
  7728. ((and org-todo-key-trigger
  7729. (or (and (equal arg '(4))
  7730. (eq org-use-fast-todo-selection 'prefix))
  7731. (and (not arg) org-use-fast-todo-selection
  7732. (not (eq org-use-fast-todo-selection
  7733. 'prefix)))))
  7734. ;; Use fast selection
  7735. (org-fast-todo-selection))
  7736. ((and (equal arg '(4))
  7737. (or (not org-use-fast-todo-selection)
  7738. (not org-todo-key-trigger)))
  7739. ;; Read a state with completion
  7740. (org-ido-completing-read
  7741. "State: " (mapcar (lambda(x) (list x))
  7742. org-todo-keywords-1)
  7743. nil t))
  7744. ((eq arg 'right)
  7745. (if this
  7746. (if tail (car tail) nil)
  7747. (car org-todo-keywords-1)))
  7748. ((eq arg 'left)
  7749. (if (equal member org-todo-keywords-1)
  7750. nil
  7751. (if this
  7752. (nth (- (length org-todo-keywords-1)
  7753. (length tail) 2)
  7754. org-todo-keywords-1)
  7755. (org-last org-todo-keywords-1))))
  7756. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7757. (setq arg nil))) ; hack to fall back to cycling
  7758. (arg
  7759. ;; user or caller requests a specific state
  7760. (cond
  7761. ((equal arg "") nil)
  7762. ((eq arg 'none) nil)
  7763. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7764. ((eq arg 'nextset)
  7765. (or (car (cdr (member head org-todo-heads)))
  7766. (car org-todo-heads)))
  7767. ((eq arg 'previousset)
  7768. (let ((org-todo-heads (reverse org-todo-heads)))
  7769. (or (car (cdr (member head org-todo-heads)))
  7770. (car org-todo-heads))))
  7771. ((car (member arg org-todo-keywords-1)))
  7772. ((nth (1- (prefix-numeric-value arg))
  7773. org-todo-keywords-1))))
  7774. ((null member) (or head (car org-todo-keywords-1)))
  7775. ((equal this final-done-word) nil) ;; -> make empty
  7776. ((null tail) nil) ;; -> first entry
  7777. ((memq interpret '(type priority))
  7778. (if (eq this-command last-command)
  7779. (car tail)
  7780. (if (> (length tail) 0)
  7781. (or done-word (car org-done-keywords))
  7782. nil)))
  7783. (t
  7784. (car tail))))
  7785. (state (or
  7786. (run-hook-with-args-until-success
  7787. 'org-todo-get-default-hook state last-state)
  7788. state))
  7789. (next (if state (concat " " state " ") " "))
  7790. (change-plist (list :type 'todo-state-change :from this :to state
  7791. :position startpos))
  7792. dolog now-done-p)
  7793. (when org-blocker-hook
  7794. (setq org-last-todo-state-is-todo
  7795. (not (member this org-done-keywords)))
  7796. (unless (save-excursion
  7797. (save-match-data
  7798. (run-hook-with-args-until-failure
  7799. 'org-blocker-hook change-plist)))
  7800. (if (interactive-p)
  7801. (error "TODO state change from %s to %s blocked" this state)
  7802. ;; fail silently
  7803. (message "TODO state change from %s to %s blocked" this state)
  7804. (throw 'exit nil))))
  7805. (store-match-data match-data)
  7806. (replace-match next t t)
  7807. (unless (pos-visible-in-window-p hl-pos)
  7808. (message "TODO state changed to %s" (org-trim next)))
  7809. (unless head
  7810. (setq head (org-get-todo-sequence-head state)
  7811. ass (assoc head org-todo-kwd-alist)
  7812. interpret (nth 1 ass)
  7813. done-word (nth 3 ass)
  7814. final-done-word (nth 4 ass)))
  7815. (when (memq arg '(nextset previousset))
  7816. (message "Keyword-Set %d/%d: %s"
  7817. (- (length org-todo-sets) -1
  7818. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7819. (length org-todo-sets)
  7820. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7821. (setq org-last-todo-state-is-todo
  7822. (not (member state org-done-keywords)))
  7823. (setq now-done-p (and (member state org-done-keywords)
  7824. (not (member this org-done-keywords))))
  7825. (and logging (org-local-logging logging))
  7826. (when (and (or org-todo-log-states org-log-done)
  7827. (not (memq arg '(nextset previousset))))
  7828. ;; we need to look at recording a time and note
  7829. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7830. (nth 2 (assoc this org-todo-log-states))))
  7831. (when (and state
  7832. (member state org-not-done-keywords)
  7833. (not (member this org-not-done-keywords)))
  7834. ;; This is now a todo state and was not one before
  7835. ;; If there was a CLOSED time stamp, get rid of it.
  7836. (org-add-planning-info nil nil 'closed))
  7837. (when (and now-done-p org-log-done)
  7838. ;; It is now done, and it was not done before
  7839. (org-add-planning-info 'closed (org-current-time))
  7840. (if (and (not dolog) (eq 'note org-log-done))
  7841. (org-add-log-setup 'done state this 'findpos 'note)))
  7842. (when (and state dolog)
  7843. ;; This is a non-nil state, and we need to log it
  7844. (org-add-log-setup 'state state this 'findpos dolog)))
  7845. ;; Fixup tag positioning
  7846. (org-todo-trigger-tag-changes state)
  7847. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7848. (when org-provide-todo-statistics
  7849. (org-update-parent-todo-statistics))
  7850. (run-hooks 'org-after-todo-state-change-hook)
  7851. (if (and arg (not (member state org-done-keywords)))
  7852. (setq head (org-get-todo-sequence-head state)))
  7853. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7854. ;; Do we need to trigger a repeat?
  7855. (when now-done-p
  7856. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7857. ;; This is for the agenda, take a snapshot of the headline.
  7858. (save-match-data
  7859. (setq org-agenda-headline-snapshot-before-repeat
  7860. (org-get-heading))))
  7861. (org-auto-repeat-maybe state))
  7862. ;; Fixup cursor location if close to the keyword
  7863. (if (and (outline-on-heading-p)
  7864. (not (bolp))
  7865. (save-excursion (beginning-of-line 1)
  7866. (looking-at org-todo-line-regexp))
  7867. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7868. (progn
  7869. (goto-char (or (match-end 2) (match-end 1)))
  7870. (just-one-space)))
  7871. (when org-trigger-hook
  7872. (save-excursion
  7873. (run-hook-with-args 'org-trigger-hook change-plist))))))))
  7874. (defun org-block-todo-from-children-or-siblings (change-plist)
  7875. "Block turning an entry into a TODO, using the hierarchy.
  7876. This checks whether the current task should be blocked from state
  7877. changes. Such blocking occurs when:
  7878. 1. The task has children which are not all in a completed state.
  7879. 2. A task has a parent with the property :ORDERED:, and there
  7880. are siblings prior to the current task with incomplete
  7881. status."
  7882. (catch 'dont-block
  7883. ;; If this is not a todo state change, or if this entry is already DONE,
  7884. ;; do not block
  7885. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7886. (member (plist-get change-plist :from)
  7887. (cons 'done org-done-keywords))
  7888. (member (plist-get change-plist :to)
  7889. (cons 'todo org-not-done-keywords)))
  7890. (throw 'dont-block t))
  7891. ;; If this task has children, and any are undone, it's blocked
  7892. (save-excursion
  7893. (org-back-to-heading t)
  7894. (let ((this-level (funcall outline-level)))
  7895. (outline-next-heading)
  7896. (let ((child-level (funcall outline-level)))
  7897. (while (and (not (eobp))
  7898. (> child-level this-level))
  7899. ;; this todo has children, check whether they are all
  7900. ;; completed
  7901. (if (and (not (org-entry-is-done-p))
  7902. (org-entry-is-todo-p))
  7903. (throw 'dont-block nil))
  7904. (outline-next-heading)
  7905. (setq child-level (funcall outline-level))))))
  7906. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  7907. ;; any previous siblings are undone, it's blocked
  7908. (save-excursion
  7909. (org-back-to-heading t)
  7910. (when (save-excursion
  7911. (ignore-errors
  7912. (org-up-heading-all 1)
  7913. (org-entry-get (point) "ORDERED")))
  7914. (let* ((this-level (funcall outline-level))
  7915. (current-level this-level))
  7916. (while (and (not (bobp))
  7917. (= current-level this-level))
  7918. (outline-previous-heading)
  7919. (setq current-level (funcall outline-level))
  7920. (if (= current-level this-level)
  7921. ;; this todo has children, check whether they are all
  7922. ;; completed
  7923. (if (and (not (org-entry-is-done-p))
  7924. (org-entry-is-todo-p))
  7925. (throw 'dont-block nil)))))))
  7926. t)) ; don't block
  7927. (defcustom org-track-ordered-property-with-tag nil
  7928. "Should the ORDERED property also be shown as a tag?
  7929. The ORDERED property decides if an entry should require subtasks to be
  7930. completed in sequence. Since a property is not very visible, setting
  7931. this option means that toggling the ORDERED property with the command
  7932. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  7933. not relevant for the behavior, but it makes things more visible.
  7934. Note that toggling the tag with tags commands will not change the property
  7935. and therefore not influence behavior!
  7936. This can be t, meaning the tag ORDERED should be used, It can also be a
  7937. string to select a different tag for this task."
  7938. :group 'org-todo
  7939. :type '(choice
  7940. (const :tag "No tracking" nil)
  7941. (const :tag "Track with ORDERED tag" t)
  7942. (string :tag "Use other tag")))
  7943. (defun org-toggle-ordered-property ()
  7944. "Toggle the ORDERED property of the current entry.
  7945. For better visibility, you can track the value of this property with a tag.
  7946. See variable `org-track-ordered-property-with-tag'."
  7947. (interactive)
  7948. (let* ((t1 org-track-ordered-property-with-tag)
  7949. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  7950. (save-excursion
  7951. (org-back-to-heading)
  7952. (if (org-entry-get nil "ORDERED")
  7953. (progn
  7954. (org-delete-property "ORDERED")
  7955. (and tag (org-toggle-tag tag 'off))
  7956. (message "Subtasks can be completed in arbitrary order"))
  7957. (org-entry-put nil "ORDERED" "t")
  7958. (and tag (org-toggle-tag tag 'on))
  7959. (message "Subtasks must be completed in sequence")))))
  7960. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  7961. (defun org-block-todo-from-checkboxes (change-plist)
  7962. "Block turning an entry into a TODO, using checkboxes.
  7963. This checks whether the current task should be blocked from state
  7964. changes because there are uncheckd boxes in this entry."
  7965. (catch 'dont-block
  7966. ;; If this is not a todo state change, or if this entry is already DONE,
  7967. ;; do not block
  7968. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  7969. (member (plist-get change-plist :from)
  7970. (cons 'done org-done-keywords))
  7971. (member (plist-get change-plist :to)
  7972. (cons 'todo org-not-done-keywords)))
  7973. (throw 'dont-block t))
  7974. ;; If this task has checkboxes that are not checked, it's blocked
  7975. (save-excursion
  7976. (org-back-to-heading t)
  7977. (let ((beg (point)) end)
  7978. (outline-next-heading)
  7979. (setq end (point))
  7980. (goto-char beg)
  7981. (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
  7982. end t)
  7983. (progn
  7984. (if (boundp 'org-blocked-by-checkboxes)
  7985. (setq org-blocked-by-checkboxes t))
  7986. (throw 'dont-block nil)))))
  7987. t)) ; do not block
  7988. (defun org-update-parent-todo-statistics ()
  7989. "Update any statistics cookie in the parent of the current headline."
  7990. (interactive)
  7991. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7992. level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
  7993. (catch 'exit
  7994. (save-excursion
  7995. (setq level (org-up-heading-safe))
  7996. (unless level
  7997. (throw 'exit nil))
  7998. (while (re-search-forward box-re (point-at-eol) t)
  7999. (setq cnt-all 0 cnt-done 0 cookie-present t)
  8000. (setq is-percent (match-end 2))
  8001. (save-match-data
  8002. (unless (outline-next-heading) (throw 'exit nil))
  8003. (while (looking-at org-todo-line-regexp)
  8004. (setq kwd (match-string 2))
  8005. (and kwd (setq cnt-all (1+ cnt-all)))
  8006. (and (member kwd org-done-keywords)
  8007. (setq cnt-done (1+ cnt-done)))
  8008. (condition-case nil
  8009. (org-forward-same-level 1)
  8010. (error (end-of-line 1)))))
  8011. (replace-match
  8012. (if is-percent
  8013. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  8014. (format "[%d/%d]" cnt-done cnt-all))))
  8015. (when cookie-present
  8016. (run-hook-with-args 'org-after-todo-statistics-hook
  8017. cnt-done (- cnt-all cnt-done)))))))
  8018. (defvar org-after-todo-statistics-hook nil
  8019. "Hook that is called after a TODO statistics cookie has been updated.
  8020. Each function is called with two arguments: the number of not-done entries
  8021. and the number of done entries.
  8022. For example, the following function, when added to this hook, will switch
  8023. an entry to DONE when all children are done, and back to TODO when new
  8024. entries are set to a TODO status. Note that this hook is only called
  8025. when there is a statistics cookie in the headline!
  8026. (defun org-summary-todo (n-done n-not-done)
  8027. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  8028. (let (org-log-done org-log-states) ; turn off logging
  8029. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  8030. ")
  8031. (defun org-todo-trigger-tag-changes (state)
  8032. "Apply the changes defined in `org-todo-state-tags-triggers'."
  8033. (let ((l org-todo-state-tags-triggers)
  8034. changes)
  8035. (when (or (not state) (equal state ""))
  8036. (setq changes (append changes (cdr (assoc "" l)))))
  8037. (when (and (stringp state) (> (length state) 0))
  8038. (setq changes (append changes (cdr (assoc state l)))))
  8039. (when (member state org-not-done-keywords)
  8040. (setq changes (append changes (cdr (assoc 'todo l)))))
  8041. (when (member state org-done-keywords)
  8042. (setq changes (append changes (cdr (assoc 'done l)))))
  8043. (dolist (c changes)
  8044. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  8045. (defun org-local-logging (value)
  8046. "Get logging settings from a property VALUE."
  8047. (let* (words w a)
  8048. ;; directly set the variables, they are already local.
  8049. (setq org-log-done nil
  8050. org-log-repeat nil
  8051. org-todo-log-states nil)
  8052. (setq words (org-split-string value))
  8053. (while (setq w (pop words))
  8054. (cond
  8055. ((setq a (assoc w org-startup-options))
  8056. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  8057. (set (nth 1 a) (nth 2 a))))
  8058. ((setq a (org-extract-log-state-settings w))
  8059. (and (member (car a) org-todo-keywords-1)
  8060. (push a org-todo-log-states)))))))
  8061. (defun org-get-todo-sequence-head (kwd)
  8062. "Return the head of the TODO sequence to which KWD belongs.
  8063. If KWD is not set, check if there is a text property remembering the
  8064. right sequence."
  8065. (let (p)
  8066. (cond
  8067. ((not kwd)
  8068. (or (get-text-property (point-at-bol) 'org-todo-head)
  8069. (progn
  8070. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  8071. nil (point-at-eol)))
  8072. (get-text-property p 'org-todo-head))))
  8073. ((not (member kwd org-todo-keywords-1))
  8074. (car org-todo-keywords-1))
  8075. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  8076. (defun org-fast-todo-selection ()
  8077. "Fast TODO keyword selection with single keys.
  8078. Returns the new TODO keyword, or nil if no state change should occur."
  8079. (let* ((fulltable org-todo-key-alist)
  8080. (done-keywords org-done-keywords) ;; needed for the faces.
  8081. (maxlen (apply 'max (mapcar
  8082. (lambda (x)
  8083. (if (stringp (car x)) (string-width (car x)) 0))
  8084. fulltable)))
  8085. (expert nil)
  8086. (fwidth (+ maxlen 3 1 3))
  8087. (ncol (/ (- (window-width) 4) fwidth))
  8088. tg cnt e c tbl
  8089. groups ingroup)
  8090. (save-excursion
  8091. (save-window-excursion
  8092. (if expert
  8093. (set-buffer (get-buffer-create " *Org todo*"))
  8094. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  8095. (erase-buffer)
  8096. (org-set-local 'org-done-keywords done-keywords)
  8097. (setq tbl fulltable cnt 0)
  8098. (while (setq e (pop tbl))
  8099. (cond
  8100. ((equal e '(:startgroup))
  8101. (push '() groups) (setq ingroup t)
  8102. (when (not (= cnt 0))
  8103. (setq cnt 0)
  8104. (insert "\n"))
  8105. (insert "{ "))
  8106. ((equal e '(:endgroup))
  8107. (setq ingroup nil cnt 0)
  8108. (insert "}\n"))
  8109. ((equal e '(:newline))
  8110. (insert "\n "))
  8111. (t
  8112. (setq tg (car e) c (cdr e))
  8113. (if ingroup (push tg (car groups)))
  8114. (setq tg (org-add-props tg nil 'face
  8115. (org-get-todo-face tg)))
  8116. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8117. (insert "[" c "] " tg (make-string
  8118. (- fwidth 4 (length tg)) ?\ ))
  8119. (when (= (setq cnt (1+ cnt)) ncol)
  8120. (insert "\n")
  8121. (if ingroup (insert " "))
  8122. (setq cnt 0)))))
  8123. (insert "\n")
  8124. (goto-char (point-min))
  8125. (if (not expert) (org-fit-window-to-buffer))
  8126. (message "[a-z..]:Set [SPC]:clear")
  8127. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8128. (cond
  8129. ((or (= c ?\C-g)
  8130. (and (= c ?q) (not (rassoc c fulltable))))
  8131. (setq quit-flag t))
  8132. ((= c ?\ ) nil)
  8133. ((setq e (rassoc c fulltable) tg (car e))
  8134. tg)
  8135. (t (setq quit-flag t)))))))
  8136. (defun org-entry-is-todo-p ()
  8137. (member (org-get-todo-state) org-not-done-keywords))
  8138. (defun org-entry-is-done-p ()
  8139. (member (org-get-todo-state) org-done-keywords))
  8140. (defun org-get-todo-state ()
  8141. (save-excursion
  8142. (org-back-to-heading t)
  8143. (and (looking-at org-todo-line-regexp)
  8144. (match-end 2)
  8145. (match-string 2))))
  8146. (defun org-at-date-range-p (&optional inactive-ok)
  8147. "Is the cursor inside a date range?"
  8148. (interactive)
  8149. (save-excursion
  8150. (catch 'exit
  8151. (let ((pos (point)))
  8152. (skip-chars-backward "^[<\r\n")
  8153. (skip-chars-backward "<[")
  8154. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8155. (>= (match-end 0) pos)
  8156. (throw 'exit t))
  8157. (skip-chars-backward "^<[\r\n")
  8158. (skip-chars-backward "<[")
  8159. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  8160. (>= (match-end 0) pos)
  8161. (throw 'exit t)))
  8162. nil)))
  8163. (defun org-get-repeat ()
  8164. "Check if there is a deadline/schedule with repeater in this entry."
  8165. (save-match-data
  8166. (save-excursion
  8167. (org-back-to-heading t)
  8168. (if (re-search-forward
  8169. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  8170. (match-string 1)))))
  8171. (defvar org-last-changed-timestamp)
  8172. (defvar org-last-inserted-timestamp)
  8173. (defvar org-log-post-message)
  8174. (defvar org-log-note-purpose)
  8175. (defvar org-log-note-how)
  8176. (defvar org-log-note-extra)
  8177. (defun org-auto-repeat-maybe (done-word)
  8178. "Check if the current headline contains a repeated deadline/schedule.
  8179. If yes, set TODO state back to what it was and change the base date
  8180. of repeating deadline/scheduled time stamps to new date.
  8181. This function is run automatically after each state change to a DONE state."
  8182. ;; last-state is dynamically scoped into this function
  8183. (let* ((repeat (org-get-repeat))
  8184. (aa (assoc last-state org-todo-kwd-alist))
  8185. (interpret (nth 1 aa))
  8186. (head (nth 2 aa))
  8187. (whata '(("d" . day) ("m" . month) ("y" . year)))
  8188. (msg "Entry repeats: ")
  8189. (org-log-done nil)
  8190. (org-todo-log-states nil)
  8191. (nshiftmax 10) (nshift 0)
  8192. re type n what ts time)
  8193. (when repeat
  8194. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  8195. (org-todo (if (eq interpret 'type) last-state head))
  8196. (when org-log-repeat
  8197. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  8198. (memq 'org-add-log-note post-command-hook))
  8199. ;; OK, we are already setup for some record
  8200. (if (eq org-log-repeat 'note)
  8201. ;; make sure we take a note, not only a time stamp
  8202. (setq org-log-note-how 'note))
  8203. ;; Set up for taking a record
  8204. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  8205. last-state
  8206. 'findpos org-log-repeat)))
  8207. (org-back-to-heading t)
  8208. (org-add-planning-info nil nil 'closed)
  8209. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  8210. org-deadline-time-regexp "\\)\\|\\("
  8211. org-ts-regexp "\\)"))
  8212. (while (re-search-forward
  8213. re (save-excursion (outline-next-heading) (point)) t)
  8214. (setq type (if (match-end 1) org-scheduled-string
  8215. (if (match-end 3) org-deadline-string "Plain:"))
  8216. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  8217. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  8218. (setq n (string-to-number (match-string 2 ts))
  8219. what (match-string 3 ts))
  8220. (if (equal what "w") (setq n (* n 7) what "d"))
  8221. ;; Preparation, see if we need to modify the start date for the change
  8222. (when (match-end 1)
  8223. (setq time (save-match-data (org-time-string-to-time ts)))
  8224. (cond
  8225. ((equal (match-string 1 ts) ".")
  8226. ;; Shift starting date to today
  8227. (org-timestamp-change
  8228. (- (time-to-days (current-time)) (time-to-days time))
  8229. 'day))
  8230. ((equal (match-string 1 ts) "+")
  8231. (while (or (= nshift 0)
  8232. (<= (time-to-days time) (time-to-days (current-time))))
  8233. (when (= (incf nshift) nshiftmax)
  8234. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  8235. (error "Abort")))
  8236. (org-timestamp-change n (cdr (assoc what whata)))
  8237. (org-at-timestamp-p t)
  8238. (setq ts (match-string 1))
  8239. (setq time (save-match-data (org-time-string-to-time ts))))
  8240. (org-timestamp-change (- n) (cdr (assoc what whata)))
  8241. ;; rematch, so that we have everything in place for the real shift
  8242. (org-at-timestamp-p t)
  8243. (setq ts (match-string 1))
  8244. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  8245. (org-timestamp-change n (cdr (assoc what whata)))
  8246. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  8247. (setq org-log-post-message msg)
  8248. (message "%s" msg))))
  8249. (defun org-show-todo-tree (arg)
  8250. "Make a compact tree which shows all headlines marked with TODO.
  8251. The tree will show the lines where the regexp matches, and all higher
  8252. headlines above the match.
  8253. With a \\[universal-argument] prefix, also show the DONE entries.
  8254. With a numeric prefix N, construct a sparse tree for the Nth element
  8255. of `org-todo-keywords-1'."
  8256. (interactive "P")
  8257. (let ((case-fold-search nil)
  8258. (kwd-re
  8259. (cond ((null arg) org-not-done-regexp)
  8260. ((equal arg '(4))
  8261. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  8262. (mapcar 'list org-todo-keywords-1))))
  8263. (concat "\\("
  8264. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  8265. "\\)\\>")))
  8266. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  8267. (regexp-quote (nth (1- (prefix-numeric-value arg))
  8268. org-todo-keywords-1)))
  8269. (t (error "Invalid prefix argument: %s" arg)))))
  8270. (message "%d TODO entries found"
  8271. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  8272. (defun org-deadline (&optional remove time)
  8273. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  8274. With argument REMOVE, remove any deadline from the item.
  8275. When TIME is set, it should be an internal time specification, and the
  8276. scheduling will use the corresponding date."
  8277. (interactive "P")
  8278. (if remove
  8279. (progn
  8280. (org-remove-timestamp-with-keyword org-deadline-string)
  8281. (message "Item no longer has a deadline."))
  8282. (if (org-get-repeat)
  8283. (error "Cannot change deadline on task with repeater, please do that by hand")
  8284. (org-add-planning-info 'deadline time 'closed)
  8285. (message "Deadline on %s" org-last-inserted-timestamp))))
  8286. (defun org-schedule (&optional remove time)
  8287. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  8288. With argument REMOVE, remove any scheduling date from the item.
  8289. When TIME is set, it should be an internal time specification, and the
  8290. scheduling will use the corresponding date."
  8291. (interactive "P")
  8292. (if remove
  8293. (progn
  8294. (org-remove-timestamp-with-keyword org-scheduled-string)
  8295. (message "Item is no longer scheduled."))
  8296. (if (org-get-repeat)
  8297. (error "Cannot reschedule task with repeater, please do that by hand")
  8298. (org-add-planning-info 'scheduled time 'closed)
  8299. (message "Scheduled to %s" org-last-inserted-timestamp))))
  8300. (defun org-get-scheduled-time (pom &optional inherit)
  8301. "Get the scheduled time as a time tuple, of a format suitable
  8302. for calling org-schedule with, or if there is no scheduling,
  8303. returns nil."
  8304. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  8305. (when time
  8306. (apply 'encode-time (org-parse-time-string time)))))
  8307. (defun org-get-deadline-time (pom &optional inherit)
  8308. "Get the deadine as a time tuple, of a format suitable for
  8309. calling org-deadlin with, or if there is no scheduling, returns
  8310. nil."
  8311. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  8312. (when time
  8313. (apply 'encode-time (org-parse-time-string time)))))
  8314. (defun org-remove-timestamp-with-keyword (keyword)
  8315. "Remove all time stamps with KEYWORD in the current entry."
  8316. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  8317. beg)
  8318. (save-excursion
  8319. (org-back-to-heading t)
  8320. (setq beg (point))
  8321. (org-end-of-subtree t t)
  8322. (while (re-search-backward re beg t)
  8323. (replace-match "")
  8324. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  8325. (equal (char-before) ?\ ))
  8326. (backward-delete-char 1)
  8327. (if (string-match "^[ \t]*$" (buffer-substring
  8328. (point-at-bol) (point-at-eol)))
  8329. (delete-region (point-at-bol)
  8330. (min (point-max) (1+ (point-at-eol))))))))))
  8331. (defun org-add-planning-info (what &optional time &rest remove)
  8332. "Insert new timestamp with keyword in the line directly after the headline.
  8333. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  8334. If non is given, the user is prompted for a date.
  8335. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  8336. be removed."
  8337. (interactive)
  8338. (let (org-time-was-given org-end-time-was-given ts
  8339. end default-time default-input)
  8340. (catch 'exit
  8341. (when (and (not time) (memq what '(scheduled deadline)))
  8342. ;; Try to get a default date/time from existing timestamp
  8343. (save-excursion
  8344. (org-back-to-heading t)
  8345. (setq end (save-excursion (outline-next-heading) (point)))
  8346. (when (re-search-forward (if (eq what 'scheduled)
  8347. org-scheduled-time-regexp
  8348. org-deadline-time-regexp)
  8349. end t)
  8350. (setq ts (match-string 1)
  8351. default-time
  8352. (apply 'encode-time (org-parse-time-string ts))
  8353. default-input (and ts (org-get-compact-tod ts))))))
  8354. (when what
  8355. ;; If necessary, get the time from the user
  8356. (setq time (or time (org-read-date nil 'to-time nil nil
  8357. default-time default-input))))
  8358. (when (and org-insert-labeled-timestamps-at-point
  8359. (member what '(scheduled deadline)))
  8360. (insert
  8361. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  8362. (org-insert-time-stamp time org-time-was-given
  8363. nil nil nil (list org-end-time-was-given))
  8364. (setq what nil))
  8365. (save-excursion
  8366. (save-restriction
  8367. (let (col list elt ts buffer-invisibility-spec)
  8368. (org-back-to-heading t)
  8369. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8370. (goto-char (match-end 1))
  8371. (setq col (current-column))
  8372. (goto-char (match-end 0))
  8373. (if (eobp) (insert "\n") (forward-char 1))
  8374. (when (and (not what)
  8375. (not (looking-at org-keyword-time-not-clock-regexp)))
  8376. ;; Nothing to add, nothing to remove...... :-)
  8377. (throw 'exit nil))
  8378. (if (and (not (looking-at outline-regexp))
  8379. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8380. "[^\r\n]*"))
  8381. (not (equal (match-string 1) org-clock-string)))
  8382. (narrow-to-region (match-beginning 0) (match-end 0))
  8383. (insert-before-markers "\n")
  8384. (backward-char 1)
  8385. (narrow-to-region (point) (point))
  8386. (and org-adapt-indentation (org-indent-to-column col)))
  8387. ;; Check if we have to remove something.
  8388. (setq list (cons what remove))
  8389. (while list
  8390. (setq elt (pop list))
  8391. (goto-char (point-min))
  8392. (when (or (and (eq elt 'scheduled)
  8393. (re-search-forward org-scheduled-time-regexp nil t))
  8394. (and (eq elt 'deadline)
  8395. (re-search-forward org-deadline-time-regexp nil t))
  8396. (and (eq elt 'closed)
  8397. (re-search-forward org-closed-time-regexp nil t)))
  8398. (replace-match "")
  8399. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8400. (if (looking-at " +") (replace-match ""))))
  8401. (goto-char (point-max))
  8402. (when what
  8403. (insert
  8404. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8405. (cond ((eq what 'scheduled) org-scheduled-string)
  8406. ((eq what 'deadline) org-deadline-string)
  8407. ((eq what 'closed) org-closed-string))
  8408. " ")
  8409. (setq ts (org-insert-time-stamp
  8410. time
  8411. (or org-time-was-given
  8412. (and (eq what 'closed) org-log-done-with-time))
  8413. (eq what 'closed)
  8414. nil nil (list org-end-time-was-given)))
  8415. (end-of-line 1))
  8416. (goto-char (point-min))
  8417. (widen)
  8418. (if (and (looking-at "[ \t]+\n")
  8419. (equal (char-before) ?\n))
  8420. (delete-region (1- (point)) (point-at-eol)))
  8421. ts))))))
  8422. (defvar org-log-note-marker (make-marker))
  8423. (defvar org-log-note-purpose nil)
  8424. (defvar org-log-note-state nil)
  8425. (defvar org-log-note-previous-state nil)
  8426. (defvar org-log-note-how nil)
  8427. (defvar org-log-note-extra nil)
  8428. (defvar org-log-note-window-configuration nil)
  8429. (defvar org-log-note-return-to (make-marker))
  8430. (defvar org-log-post-message nil
  8431. "Message to be displayed after a log note has been stored.
  8432. The auto-repeater uses this.")
  8433. (defun org-add-note ()
  8434. "Add a note to the current entry.
  8435. This is done in the same way as adding a state change note."
  8436. (interactive)
  8437. (org-add-log-setup 'note nil nil 'findpos nil))
  8438. (defvar org-property-end-re)
  8439. (defun org-add-log-setup (&optional purpose state prev-state
  8440. findpos how &optional extra)
  8441. "Set up the post command hook to take a note.
  8442. If this is about to TODO state change, the new state is expected in STATE.
  8443. When FINDPOS is non-nil, find the correct position for the note in
  8444. the current entry. If not, assume that it can be inserted at point.
  8445. HOW is an indicator what kind of note should be created.
  8446. EXTRA is additional text that will be inserted into the notes buffer."
  8447. (let ((drawer (cond ((stringp org-log-into-drawer)
  8448. org-log-into-drawer)
  8449. (org-log-into-drawer "LOGBOOK")
  8450. (t nil))))
  8451. (save-restriction
  8452. (save-excursion
  8453. (when findpos
  8454. (org-back-to-heading t)
  8455. (narrow-to-region (point) (save-excursion
  8456. (outline-next-heading) (point)))
  8457. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8458. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8459. "[^\r\n]*\\)?"))
  8460. (goto-char (match-end 0))
  8461. (cond
  8462. (drawer
  8463. (if (re-search-forward (concat "^[ \t]*:" drawer ":[ \t]*$")
  8464. nil t)
  8465. (progn
  8466. (goto-char (match-end 0))
  8467. (or org-log-states-order-reversed
  8468. (and (re-search-forward org-property-end-re nil t)
  8469. (goto-char (1- (match-beginning 0))))))
  8470. (insert "\n:" drawer ":\n:END:")
  8471. (beginning-of-line 0)
  8472. (org-indent-line-function)
  8473. (beginning-of-line 2)
  8474. (org-indent-line-function)
  8475. (end-of-line 0)))
  8476. ((and org-log-state-notes-insert-after-drawers
  8477. (save-excursion
  8478. (forward-line) (looking-at org-drawer-regexp)))
  8479. (forward-line)
  8480. (while (looking-at org-drawer-regexp)
  8481. (goto-char (match-end 0))
  8482. (re-search-forward org-property-end-re (point-max) t)
  8483. (forward-line))
  8484. (forward-line -1)))
  8485. (unless org-log-states-order-reversed
  8486. (and (= (char-after) ?\n) (forward-char 1))
  8487. (org-skip-over-state-notes)
  8488. (skip-chars-backward " \t\n\r")))
  8489. (move-marker org-log-note-marker (point))
  8490. (setq org-log-note-purpose purpose
  8491. org-log-note-state state
  8492. org-log-note-previous-state prev-state
  8493. org-log-note-how how
  8494. org-log-note-extra extra)
  8495. (add-hook 'post-command-hook 'org-add-log-note 'append)))))
  8496. (defun org-skip-over-state-notes ()
  8497. "Skip past the list of State notes in an entry."
  8498. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8499. (while (looking-at "[ \t]*- State")
  8500. (condition-case nil
  8501. (org-next-item)
  8502. (error (org-end-of-item)))))
  8503. (defun org-add-log-note (&optional purpose)
  8504. "Pop up a window for taking a note, and add this note later at point."
  8505. (remove-hook 'post-command-hook 'org-add-log-note)
  8506. (setq org-log-note-window-configuration (current-window-configuration))
  8507. (delete-other-windows)
  8508. (move-marker org-log-note-return-to (point))
  8509. (switch-to-buffer (marker-buffer org-log-note-marker))
  8510. (goto-char org-log-note-marker)
  8511. (org-switch-to-buffer-other-window "*Org Note*")
  8512. (erase-buffer)
  8513. (if (memq org-log-note-how '(time state))
  8514. (let (current-prefix-arg) (org-store-log-note))
  8515. (let ((org-inhibit-startup t)) (org-mode))
  8516. (insert (format "# Insert note for %s.
  8517. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8518. (cond
  8519. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8520. ((eq org-log-note-purpose 'done) "closed todo item")
  8521. ((eq org-log-note-purpose 'state)
  8522. (format "state change from \"%s\" to \"%s\""
  8523. (or org-log-note-previous-state "")
  8524. (or org-log-note-state "")))
  8525. ((eq org-log-note-purpose 'note)
  8526. "this entry")
  8527. (t (error "This should not happen")))))
  8528. (if org-log-note-extra (insert org-log-note-extra))
  8529. (org-set-local 'org-finish-function 'org-store-log-note)))
  8530. (defvar org-note-abort nil) ; dynamically scoped
  8531. (defun org-store-log-note ()
  8532. "Finish taking a log note, and insert it to where it belongs."
  8533. (let ((txt (buffer-string))
  8534. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8535. lines ind)
  8536. (kill-buffer (current-buffer))
  8537. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8538. (setq txt (replace-match "" t t txt)))
  8539. (if (string-match "\\s-+\\'" txt)
  8540. (setq txt (replace-match "" t t txt)))
  8541. (setq lines (org-split-string txt "\n"))
  8542. (when (and note (string-match "\\S-" note))
  8543. (setq note
  8544. (org-replace-escapes
  8545. note
  8546. (list (cons "%u" (user-login-name))
  8547. (cons "%U" user-full-name)
  8548. (cons "%t" (format-time-string
  8549. (org-time-stamp-format 'long 'inactive)
  8550. (current-time)))
  8551. (cons "%s" (if org-log-note-state
  8552. (concat "\"" org-log-note-state "\"")
  8553. ""))
  8554. (cons "%S" (if org-log-note-previous-state
  8555. (concat "\"" org-log-note-previous-state "\"")
  8556. "\"\"")))))
  8557. (if lines (setq note (concat note " \\\\")))
  8558. (push note lines))
  8559. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8560. (when lines
  8561. (save-excursion
  8562. (set-buffer (marker-buffer org-log-note-marker))
  8563. (save-excursion
  8564. (goto-char org-log-note-marker)
  8565. (move-marker org-log-note-marker nil)
  8566. (end-of-line 1)
  8567. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8568. (insert "- " (pop lines))
  8569. (org-indent-line-function)
  8570. (beginning-of-line 1)
  8571. (looking-at "[ \t]*")
  8572. (setq ind (concat (match-string 0) " "))
  8573. (end-of-line 1)
  8574. (while lines (insert "\n" ind (pop lines)))
  8575. (message "Note stored")
  8576. (org-back-to-heading t)
  8577. (org-cycle-hide-drawers 'children)))))
  8578. (set-window-configuration org-log-note-window-configuration)
  8579. (with-current-buffer (marker-buffer org-log-note-return-to)
  8580. (goto-char org-log-note-return-to))
  8581. (move-marker org-log-note-return-to nil)
  8582. (and org-log-post-message (message "%s" org-log-post-message)))
  8583. (defun org-sparse-tree (&optional arg)
  8584. "Create a sparse tree, prompt for the details.
  8585. This command can create sparse trees. You first need to select the type
  8586. of match used to create the tree:
  8587. t Show entries with a specific TODO keyword.
  8588. T Show entries selected by a tags match.
  8589. p Enter a property name and its value (both with completion on existing
  8590. names/values) and show entries with that property.
  8591. r Show entries matching a regular expression
  8592. d Show deadlines due within `org-deadline-warning-days'."
  8593. (interactive "P")
  8594. (let (ans kwd value)
  8595. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8596. (setq ans (read-char-exclusive))
  8597. (cond
  8598. ((equal ans ?d)
  8599. (call-interactively 'org-check-deadlines))
  8600. ((equal ans ?b)
  8601. (call-interactively 'org-check-before-date))
  8602. ((equal ans ?t)
  8603. (org-show-todo-tree '(4)))
  8604. ((equal ans ?T)
  8605. (call-interactively 'org-tags-sparse-tree))
  8606. ((member ans '(?p ?P))
  8607. (setq kwd (org-ido-completing-read "Property: "
  8608. (mapcar 'list (org-buffer-property-keys))))
  8609. (setq value (org-ido-completing-read "Value: "
  8610. (mapcar 'list (org-property-values kwd))))
  8611. (unless (string-match "\\`{.*}\\'" value)
  8612. (setq value (concat "\"" value "\"")))
  8613. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8614. ((member ans '(?r ?R ?/))
  8615. (call-interactively 'org-occur))
  8616. (t (error "No such sparse tree command \"%c\"" ans)))))
  8617. (defvar org-occur-highlights nil
  8618. "List of overlays used for occur matches.")
  8619. (make-variable-buffer-local 'org-occur-highlights)
  8620. (defvar org-occur-parameters nil
  8621. "Parameters of the active org-occur calls.
  8622. This is a list, each call to org-occur pushes as cons cell,
  8623. containing the regular expression and the callback, onto the list.
  8624. The list can contain several entries if `org-occur' has been called
  8625. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8626. will only contain one set of parameters. When the highlights are
  8627. removed (for example with `C-c C-c', or with the next edit (depending
  8628. on `org-remove-highlights-with-change'), this variable is emptied
  8629. as well.")
  8630. (make-variable-buffer-local 'org-occur-parameters)
  8631. (defun org-occur (regexp &optional keep-previous callback)
  8632. "Make a compact tree which shows all matches of REGEXP.
  8633. The tree will show the lines where the regexp matches, and all higher
  8634. headlines above the match. It will also show the heading after the match,
  8635. to make sure editing the matching entry is easy.
  8636. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8637. call to `org-occur' will be kept, to allow stacking of calls to this
  8638. command.
  8639. If CALLBACK is non-nil, it is a function which is called to confirm
  8640. that the match should indeed be shown."
  8641. (interactive "sRegexp: \nP")
  8642. (unless keep-previous
  8643. (org-remove-occur-highlights nil nil t))
  8644. (push (cons regexp callback) org-occur-parameters)
  8645. (let ((cnt 0))
  8646. (save-excursion
  8647. (goto-char (point-min))
  8648. (if (or (not keep-previous) ; do not want to keep
  8649. (not org-occur-highlights)) ; no previous matches
  8650. ;; hide everything
  8651. (org-overview))
  8652. (while (re-search-forward regexp nil t)
  8653. (when (or (not callback)
  8654. (save-match-data (funcall callback)))
  8655. (setq cnt (1+ cnt))
  8656. (when org-highlight-sparse-tree-matches
  8657. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8658. (org-show-context 'occur-tree))))
  8659. (when org-remove-highlights-with-change
  8660. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8661. nil 'local))
  8662. (unless org-sparse-tree-open-archived-trees
  8663. (org-hide-archived-subtrees (point-min) (point-max)))
  8664. (run-hooks 'org-occur-hook)
  8665. (if (interactive-p)
  8666. (message "%d match(es) for regexp %s" cnt regexp))
  8667. cnt))
  8668. (defun org-show-context (&optional key)
  8669. "Make sure point and context and visible.
  8670. How much context is shown depends upon the variables
  8671. `org-show-hierarchy-above', `org-show-following-heading'. and
  8672. `org-show-siblings'."
  8673. (let ((heading-p (org-on-heading-p t))
  8674. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8675. (following-p (org-get-alist-option org-show-following-heading key))
  8676. (entry-p (org-get-alist-option org-show-entry-below key))
  8677. (siblings-p (org-get-alist-option org-show-siblings key)))
  8678. (catch 'exit
  8679. ;; Show heading or entry text
  8680. (if (and heading-p (not entry-p))
  8681. (org-flag-heading nil) ; only show the heading
  8682. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8683. (org-show-hidden-entry))) ; show entire entry
  8684. (when following-p
  8685. ;; Show next sibling, or heading below text
  8686. (save-excursion
  8687. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8688. (org-flag-heading nil))))
  8689. (when siblings-p (org-show-siblings))
  8690. (when hierarchy-p
  8691. ;; show all higher headings, possibly with siblings
  8692. (save-excursion
  8693. (while (and (condition-case nil
  8694. (progn (org-up-heading-all 1) t)
  8695. (error nil))
  8696. (not (bobp)))
  8697. (org-flag-heading nil)
  8698. (when siblings-p (org-show-siblings))))))))
  8699. (defun org-reveal (&optional siblings)
  8700. "Show current entry, hierarchy above it, and the following headline.
  8701. This can be used to show a consistent set of context around locations
  8702. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8703. not t for the search context.
  8704. With optional argument SIBLINGS, on each level of the hierarchy all
  8705. siblings are shown. This repairs the tree structure to what it would
  8706. look like when opened with hierarchical calls to `org-cycle'."
  8707. (interactive "P")
  8708. (let ((org-show-hierarchy-above t)
  8709. (org-show-following-heading t)
  8710. (org-show-siblings (if siblings t org-show-siblings)))
  8711. (org-show-context nil)))
  8712. (defun org-highlight-new-match (beg end)
  8713. "Highlight from BEG to END and mark the highlight is an occur headline."
  8714. (let ((ov (org-make-overlay beg end)))
  8715. (org-overlay-put ov 'face 'secondary-selection)
  8716. (push ov org-occur-highlights)))
  8717. (defun org-remove-occur-highlights (&optional beg end noremove)
  8718. "Remove the occur highlights from the buffer.
  8719. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8720. from the `before-change-functions' in the current buffer."
  8721. (interactive)
  8722. (unless org-inhibit-highlight-removal
  8723. (mapc 'org-delete-overlay org-occur-highlights)
  8724. (setq org-occur-highlights nil)
  8725. (setq org-occur-parameters nil)
  8726. (unless noremove
  8727. (remove-hook 'before-change-functions
  8728. 'org-remove-occur-highlights 'local))))
  8729. ;;;; Priorities
  8730. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8731. "Regular expression matching the priority indicator.")
  8732. (defvar org-remove-priority-next-time nil)
  8733. (defun org-priority-up ()
  8734. "Increase the priority of the current item."
  8735. (interactive)
  8736. (org-priority 'up))
  8737. (defun org-priority-down ()
  8738. "Decrease the priority of the current item."
  8739. (interactive)
  8740. (org-priority 'down))
  8741. (defun org-priority (&optional action)
  8742. "Change the priority of an item by ARG.
  8743. ACTION can be `set', `up', `down', or a character."
  8744. (interactive)
  8745. (setq action (or action 'set))
  8746. (let (current new news have remove)
  8747. (save-excursion
  8748. (org-back-to-heading t)
  8749. (if (looking-at org-priority-regexp)
  8750. (setq current (string-to-char (match-string 2))
  8751. have t)
  8752. (setq current org-default-priority))
  8753. (cond
  8754. ((or (eq action 'set)
  8755. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8756. (if (not (eq action 'set))
  8757. (setq new action)
  8758. (message "Priority %c-%c, SPC to remove: "
  8759. org-highest-priority org-lowest-priority)
  8760. (setq new (read-char-exclusive)))
  8761. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8762. (= (upcase org-lowest-priority) org-lowest-priority))
  8763. (setq new (upcase new)))
  8764. (cond ((equal new ?\ ) (setq remove t))
  8765. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8766. (error "Priority must be between `%c' and `%c'"
  8767. org-highest-priority org-lowest-priority))))
  8768. ((eq action 'up)
  8769. (if (and (not have) (eq last-command this-command))
  8770. (setq new org-lowest-priority)
  8771. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8772. org-default-priority (1- current)))))
  8773. ((eq action 'down)
  8774. (if (and (not have) (eq last-command this-command))
  8775. (setq new org-highest-priority)
  8776. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8777. org-default-priority (1+ current)))))
  8778. (t (error "Invalid action")))
  8779. (if (or (< (upcase new) org-highest-priority)
  8780. (> (upcase new) org-lowest-priority))
  8781. (setq remove t))
  8782. (setq news (format "%c" new))
  8783. (if have
  8784. (if remove
  8785. (replace-match "" t t nil 1)
  8786. (replace-match news t t nil 2))
  8787. (if remove
  8788. (error "No priority cookie found in line")
  8789. (looking-at org-todo-line-regexp)
  8790. (if (match-end 2)
  8791. (progn
  8792. (goto-char (match-end 2))
  8793. (insert " [#" news "]"))
  8794. (goto-char (match-beginning 3))
  8795. (insert "[#" news "] ")))))
  8796. (org-preserve-lc (org-set-tags nil 'align))
  8797. (if remove
  8798. (message "Priority removed")
  8799. (message "Priority of current item set to %s" news))))
  8800. (defun org-get-priority (s)
  8801. "Find priority cookie and return priority."
  8802. (save-match-data
  8803. (if (not (string-match org-priority-regexp s))
  8804. (* 1000 (- org-lowest-priority org-default-priority))
  8805. (* 1000 (- org-lowest-priority
  8806. (string-to-char (match-string 2 s)))))))
  8807. ;;;; Tags
  8808. (defvar org-agenda-archives-mode)
  8809. (defun org-scan-tags (action matcher &optional todo-only)
  8810. "Scan headline tags with inheritance and produce output ACTION.
  8811. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8812. or `agenda' to produce an entry list for an agenda view. It can also be
  8813. a Lisp form or a function that should be called at each matched headline, in
  8814. this case the return value is a list of all return values from these calls.
  8815. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8816. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8817. only lines with a TODO keyword are included in the output."
  8818. (require 'org-agenda)
  8819. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8820. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8821. (org-re
  8822. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8823. (props (list 'face 'default
  8824. 'done-face 'org-done
  8825. 'undone-face 'default
  8826. 'mouse-face 'highlight
  8827. 'org-not-done-regexp org-not-done-regexp
  8828. 'org-todo-regexp org-todo-regexp
  8829. 'keymap org-agenda-keymap
  8830. 'help-echo
  8831. (format "mouse-2 or RET jump to org file %s"
  8832. (abbreviate-file-name
  8833. (or (buffer-file-name (buffer-base-buffer))
  8834. (buffer-name (buffer-base-buffer)))))))
  8835. (case-fold-search nil)
  8836. lspos tags tags-list
  8837. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8838. (llast 0) rtn rtn1 level category i txt
  8839. todo marker entry priority)
  8840. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8841. (setq action (list 'lambda nil action)))
  8842. (save-excursion
  8843. (goto-char (point-min))
  8844. (when (eq action 'sparse-tree)
  8845. (org-overview)
  8846. (org-remove-occur-highlights))
  8847. (while (re-search-forward re nil t)
  8848. (catch :skip
  8849. (setq todo (if (match-end 1) (match-string 2))
  8850. tags (if (match-end 4) (match-string 4)))
  8851. (goto-char (setq lspos (1+ (match-beginning 0))))
  8852. (setq level (org-reduced-level (funcall outline-level))
  8853. category (org-get-category))
  8854. (setq i llast llast level)
  8855. ;; remove tag lists from same and sublevels
  8856. (while (>= i level)
  8857. (when (setq entry (assoc i tags-alist))
  8858. (setq tags-alist (delete entry tags-alist)))
  8859. (setq i (1- i)))
  8860. ;; add the next tags
  8861. (when tags
  8862. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8863. tags-alist
  8864. (cons (cons level tags) tags-alist)))
  8865. ;; compile tags for current headline
  8866. (setq tags-list
  8867. (if org-use-tag-inheritance
  8868. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8869. tags))
  8870. (when org-use-tag-inheritance
  8871. (setcdr (car tags-alist)
  8872. (mapcar (lambda (x)
  8873. (setq x (copy-sequence x))
  8874. (org-add-prop-inherited x))
  8875. (cdar tags-alist))))
  8876. (when (and tags org-use-tag-inheritance
  8877. (not (eq t org-use-tag-inheritance)))
  8878. ;; selective inheritance, remove uninherited ones
  8879. (setcdr (car tags-alist)
  8880. (org-remove-uniherited-tags (cdar tags-alist))))
  8881. (when (and (or (not todo-only)
  8882. (and (member todo org-not-done-keywords)
  8883. (or (not org-agenda-tags-todo-honor-ignore-options)
  8884. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
  8885. (let ((case-fold-search t)) (eval matcher))
  8886. (or
  8887. (not (member org-archive-tag tags-list))
  8888. ;; we have an archive tag, should we use this anyway?
  8889. (or (not org-agenda-skip-archived-trees)
  8890. (and (eq action 'agenda) org-agenda-archives-mode))))
  8891. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8892. ;; select this headline
  8893. (cond
  8894. ((eq action 'sparse-tree)
  8895. (and org-highlight-sparse-tree-matches
  8896. (org-get-heading) (match-end 0)
  8897. (org-highlight-new-match
  8898. (match-beginning 0) (match-beginning 1)))
  8899. (org-show-context 'tags-tree))
  8900. ((eq action 'agenda)
  8901. (setq txt (org-format-agenda-item
  8902. ""
  8903. (concat
  8904. (if org-tags-match-list-sublevels
  8905. (make-string (1- level) ?.) "")
  8906. (org-get-heading))
  8907. category (org-get-tags-at))
  8908. priority (org-get-priority txt))
  8909. (goto-char lspos)
  8910. (setq marker (org-agenda-new-marker))
  8911. (org-add-props txt props
  8912. 'org-marker marker 'org-hd-marker marker 'org-category category
  8913. 'todo-state todo
  8914. 'priority priority 'type "tagsmatch")
  8915. (push txt rtn))
  8916. ((functionp action)
  8917. (save-excursion
  8918. (setq rtn1 (funcall action))
  8919. (push rtn1 rtn))
  8920. (goto-char (point-at-eol)))
  8921. (t (error "Invalid action")))
  8922. ;; if we are to skip sublevels, jump to end of subtree
  8923. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8924. (when (and (eq action 'sparse-tree)
  8925. (not org-sparse-tree-open-archived-trees))
  8926. (org-hide-archived-subtrees (point-min) (point-max)))
  8927. (nreverse rtn)))
  8928. (defun org-remove-uniherited-tags (tags)
  8929. "Remove all tags that are not inherited from the list TAGS."
  8930. (cond
  8931. ((eq org-use-tag-inheritance t)
  8932. (if org-tags-exclude-from-inheritance
  8933. (org-delete-all org-tags-exclude-from-inheritance tags)
  8934. tags))
  8935. ((not org-use-tag-inheritance) nil)
  8936. ((stringp org-use-tag-inheritance)
  8937. (delq nil (mapcar
  8938. (lambda (x)
  8939. (if (and (string-match org-use-tag-inheritance x)
  8940. (not (member x org-tags-exclude-from-inheritance)))
  8941. x nil))
  8942. tags)))
  8943. ((listp org-use-tag-inheritance)
  8944. (delq nil (mapcar
  8945. (lambda (x)
  8946. (if (member x org-use-tag-inheritance) x nil))
  8947. tags)))))
  8948. (defvar todo-only) ;; dynamically scoped
  8949. (defun org-tags-sparse-tree (&optional todo-only match)
  8950. "Create a sparse tree according to tags string MATCH.
  8951. MATCH can contain positive and negative selection of tags, like
  8952. \"+WORK+URGENT-WITHBOSS\".
  8953. If optional argument TODO-ONLY is non-nil, only select lines that are
  8954. also TODO lines."
  8955. (interactive "P")
  8956. (org-prepare-agenda-buffers (list (current-buffer)))
  8957. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8958. (defvar org-cached-props nil)
  8959. (defun org-cached-entry-get (pom property)
  8960. (if (or (eq t org-use-property-inheritance)
  8961. (and (stringp org-use-property-inheritance)
  8962. (string-match org-use-property-inheritance property))
  8963. (and (listp org-use-property-inheritance)
  8964. (member property org-use-property-inheritance)))
  8965. ;; Caching is not possible, check it directly
  8966. (org-entry-get pom property 'inherit)
  8967. ;; Get all properties, so that we can do complicated checks easily
  8968. (cdr (assoc property (or org-cached-props
  8969. (setq org-cached-props
  8970. (org-entry-properties pom)))))))
  8971. (defun org-global-tags-completion-table (&optional files)
  8972. "Return the list of all tags in all agenda buffer/files."
  8973. (save-excursion
  8974. (org-uniquify
  8975. (delq nil
  8976. (apply 'append
  8977. (mapcar
  8978. (lambda (file)
  8979. (set-buffer (find-file-noselect file))
  8980. (append (org-get-buffer-tags)
  8981. (mapcar (lambda (x) (if (stringp (car-safe x))
  8982. (list (car-safe x)) nil))
  8983. org-tag-alist)))
  8984. (if (and files (car files))
  8985. files
  8986. (org-agenda-files))))))))
  8987. (defun org-make-tags-matcher (match)
  8988. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8989. ;; todo-only is scoped dynamically into this function, and the function
  8990. ;; may change it if the matcher asks for it.
  8991. (unless match
  8992. ;; Get a new match request, with completion
  8993. (let ((org-last-tags-completion-table
  8994. (org-global-tags-completion-table)))
  8995. (setq match (org-completing-read-no-ido
  8996. "Match: " 'org-tags-completion-function nil nil nil
  8997. 'org-tags-history))))
  8998. ;; Parse the string and create a lisp form
  8999. (let ((match0 match)
  9000. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  9001. minus tag mm
  9002. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  9003. orterms term orlist re-p str-p level-p level-op time-p
  9004. prop-p pn pv po cat-p gv rest)
  9005. (if (string-match "/+" match)
  9006. ;; match contains also a todo-matching request
  9007. (progn
  9008. (setq tagsmatch (substring match 0 (match-beginning 0))
  9009. todomatch (substring match (match-end 0)))
  9010. (if (string-match "^!" todomatch)
  9011. (setq todo-only t todomatch (substring todomatch 1)))
  9012. (if (string-match "^\\s-*$" todomatch)
  9013. (setq todomatch nil)))
  9014. ;; only matching tags
  9015. (setq tagsmatch match todomatch nil))
  9016. ;; Make the tags matcher
  9017. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  9018. (setq tagsmatcher t)
  9019. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  9020. (while (setq term (pop orterms))
  9021. (while (and (equal (substring term -1) "\\") orterms)
  9022. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  9023. (while (string-match re term)
  9024. (setq rest (substring term (match-end 0))
  9025. minus (and (match-end 1)
  9026. (equal (match-string 1 term) "-"))
  9027. tag (match-string 2 term)
  9028. re-p (equal (string-to-char tag) ?{)
  9029. level-p (match-end 4)
  9030. prop-p (match-end 5)
  9031. mm (cond
  9032. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  9033. (level-p
  9034. (setq level-op (org-op-to-function (match-string 3 term)))
  9035. `(,level-op level ,(string-to-number
  9036. (match-string 4 term))))
  9037. (prop-p
  9038. (setq pn (match-string 5 term)
  9039. po (match-string 6 term)
  9040. pv (match-string 7 term)
  9041. cat-p (equal pn "CATEGORY")
  9042. re-p (equal (string-to-char pv) ?{)
  9043. str-p (equal (string-to-char pv) ?\")
  9044. time-p (save-match-data
  9045. (string-match "^\"[[<].*[]>]\"$" pv))
  9046. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  9047. (if time-p (setq pv (org-matcher-time pv)))
  9048. (setq po (org-op-to-function po (if time-p 'time str-p)))
  9049. (cond
  9050. ((equal pn "CATEGORY")
  9051. (setq gv '(get-text-property (point) 'org-category)))
  9052. ((equal pn "TODO")
  9053. (setq gv 'todo))
  9054. (t
  9055. (setq gv `(org-cached-entry-get nil ,pn))))
  9056. (if re-p
  9057. (if (eq po 'org<>)
  9058. `(not (string-match ,pv (or ,gv "")))
  9059. `(string-match ,pv (or ,gv "")))
  9060. (if str-p
  9061. `(,po (or ,gv "") ,pv)
  9062. `(,po (string-to-number (or ,gv ""))
  9063. ,(string-to-number pv) ))))
  9064. (t `(member ,(downcase tag) tags-list)))
  9065. mm (if minus (list 'not mm) mm)
  9066. term rest)
  9067. (push mm tagsmatcher))
  9068. (push (if (> (length tagsmatcher) 1)
  9069. (cons 'and tagsmatcher)
  9070. (car tagsmatcher))
  9071. orlist)
  9072. (setq tagsmatcher nil))
  9073. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  9074. (setq tagsmatcher
  9075. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  9076. ;; Make the todo matcher
  9077. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  9078. (setq todomatcher t)
  9079. (setq orterms (org-split-string todomatch "|") orlist nil)
  9080. (while (setq term (pop orterms))
  9081. (while (string-match re term)
  9082. (setq minus (and (match-end 1)
  9083. (equal (match-string 1 term) "-"))
  9084. kwd (match-string 2 term)
  9085. re-p (equal (string-to-char kwd) ?{)
  9086. term (substring term (match-end 0))
  9087. mm (if re-p
  9088. `(string-match ,(substring kwd 1 -1) todo)
  9089. (list 'equal 'todo kwd))
  9090. mm (if minus (list 'not mm) mm))
  9091. (push mm todomatcher))
  9092. (push (if (> (length todomatcher) 1)
  9093. (cons 'and todomatcher)
  9094. (car todomatcher))
  9095. orlist)
  9096. (setq todomatcher nil))
  9097. (setq todomatcher (if (> (length orlist) 1)
  9098. (cons 'or orlist) (car orlist))))
  9099. ;; Return the string and lisp forms of the matcher
  9100. (setq matcher (if todomatcher
  9101. (list 'and tagsmatcher todomatcher)
  9102. tagsmatcher))
  9103. (cons match0 matcher)))
  9104. (defun org-op-to-function (op &optional stringp)
  9105. "Turn an operator into the appropriate function."
  9106. (setq op
  9107. (cond
  9108. ((equal op "<" ) '(< string< org-time<))
  9109. ((equal op ">" ) '(> org-string> org-time>))
  9110. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  9111. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  9112. ((member op '("=" "==")) '(= string= org-time=))
  9113. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  9114. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  9115. (defun org<> (a b) (not (= a b)))
  9116. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  9117. (defun org-string>= (a b) (not (string< a b)))
  9118. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  9119. (defun org-string<> (a b) (not (string= a b)))
  9120. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  9121. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  9122. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  9123. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  9124. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  9125. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  9126. (defun org-2ft (s)
  9127. "Convert S to a floating point time.
  9128. If S is already a number, just return it. If it is a string, parse
  9129. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  9130. (cond
  9131. ((numberp s) s)
  9132. ((stringp s)
  9133. (condition-case nil
  9134. (float-time (apply 'encode-time (org-parse-time-string s)))
  9135. (error 0.)))
  9136. (t 0.)))
  9137. (defun org-time-today ()
  9138. "Time in seconds today at 0:00.
  9139. Returns the float number of seconds since the beginning of the
  9140. epoch to the beginning of today (00:00)."
  9141. (float-time (apply 'encode-time
  9142. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  9143. (defun org-matcher-time (s)
  9144. "Interpret a time comparison value."
  9145. (save-match-data
  9146. (cond
  9147. ((string= s "<now>") (float-time))
  9148. ((string= s "<today>") (org-time-today))
  9149. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  9150. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  9151. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  9152. (+ (org-time-today)
  9153. (* (string-to-number (match-string 1 s))
  9154. (cdr (assoc (match-string 2 s)
  9155. '(("d" . 86400.0) ("w" . 604800.0)
  9156. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  9157. (t (org-2ft s)))))
  9158. (defun org-match-any-p (re list)
  9159. "Does re match any element of list?"
  9160. (setq list (mapcar (lambda (x) (string-match re x)) list))
  9161. (delq nil list))
  9162. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  9163. (defvar org-tags-overlay (org-make-overlay 1 1))
  9164. (org-detach-overlay org-tags-overlay)
  9165. (defun org-get-local-tags-at (&optional pos)
  9166. "Get a list of tags defined in the current headline."
  9167. (org-get-tags-at pos 'local))
  9168. (defun org-get-local-tags ()
  9169. "Get a list of tags defined in the current headline."
  9170. (org-get-tags-at nil 'local))
  9171. (defun org-get-tags-at (&optional pos local)
  9172. "Get a list of all headline tags applicable at POS.
  9173. POS defaults to point. If tags are inherited, the list contains
  9174. the targets in the same sequence as the headlines appear, i.e.
  9175. the tags of the current headline come last.
  9176. When LOCAL is non-nil, only return tags from the current headline,
  9177. ignore inherited ones."
  9178. (interactive)
  9179. (let (tags ltags lastpos parent)
  9180. (save-excursion
  9181. (save-restriction
  9182. (widen)
  9183. (goto-char (or pos (point)))
  9184. (save-match-data
  9185. (catch 'done
  9186. (condition-case nil
  9187. (progn
  9188. (org-back-to-heading t)
  9189. (while (not (equal lastpos (point)))
  9190. (setq lastpos (point))
  9191. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  9192. (setq ltags (org-split-string
  9193. (org-match-string-no-properties 1) ":"))
  9194. (when parent
  9195. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  9196. (setq tags (append
  9197. (if parent
  9198. (org-remove-uniherited-tags ltags)
  9199. ltags)
  9200. tags)))
  9201. (or org-use-tag-inheritance (throw 'done t))
  9202. (if local (throw 'done t))
  9203. (org-up-heading-all 1)
  9204. (setq parent t)))
  9205. (error nil)))))
  9206. (append (org-remove-uniherited-tags org-file-tags) tags))))
  9207. (defun org-add-prop-inherited (s)
  9208. (add-text-properties 0 (length s) '(inherited t) s)
  9209. s)
  9210. (defun org-toggle-tag (tag &optional onoff)
  9211. "Toggle the tag TAG for the current line.
  9212. If ONOFF is `on' or `off', don't toggle but set to this state."
  9213. (let (res current)
  9214. (save-excursion
  9215. (org-back-to-heading t)
  9216. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  9217. (point-at-eol) t)
  9218. (progn
  9219. (setq current (match-string 1))
  9220. (replace-match ""))
  9221. (setq current ""))
  9222. (setq current (nreverse (org-split-string current ":")))
  9223. (cond
  9224. ((eq onoff 'on)
  9225. (setq res t)
  9226. (or (member tag current) (push tag current)))
  9227. ((eq onoff 'off)
  9228. (or (not (member tag current)) (setq current (delete tag current))))
  9229. (t (if (member tag current)
  9230. (setq current (delete tag current))
  9231. (setq res t)
  9232. (push tag current))))
  9233. (end-of-line 1)
  9234. (if current
  9235. (progn
  9236. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  9237. (org-set-tags nil t))
  9238. (delete-horizontal-space))
  9239. (run-hooks 'org-after-tags-change-hook))
  9240. res))
  9241. (defun org-align-tags-here (to-col)
  9242. ;; Assumes that this is a headline
  9243. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  9244. (beginning-of-line 1)
  9245. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9246. (< pos (match-beginning 2)))
  9247. (progn
  9248. (setq tags-l (- (match-end 2) (match-beginning 2)))
  9249. (goto-char (match-beginning 1))
  9250. (insert " ")
  9251. (delete-region (point) (1+ (match-beginning 2)))
  9252. (setq ncol (max (1+ (current-column))
  9253. (1+ col)
  9254. (if (> to-col 0)
  9255. to-col
  9256. (- (abs to-col) tags-l))))
  9257. (setq p (point))
  9258. (insert (make-string (- ncol (current-column)) ?\ ))
  9259. (setq ncol (current-column))
  9260. (when indent-tabs-mode (tabify p (point-at-eol)))
  9261. (org-move-to-column (min ncol col) t))
  9262. (goto-char pos))))
  9263. (defun org-set-tags-command (&optional arg just-align)
  9264. "Call the set-tags command for the current entry."
  9265. (interactive "P")
  9266. (if (org-on-heading-p)
  9267. (org-set-tags arg just-align)
  9268. (save-excursion
  9269. (org-back-to-heading t)
  9270. (org-set-tags arg just-align))))
  9271. (defun org-set-tags (&optional arg just-align)
  9272. "Set the tags for the current headline.
  9273. With prefix ARG, realign all tags in headings in the current buffer."
  9274. (interactive "P")
  9275. (let* ((re (concat "^" outline-regexp))
  9276. (current (org-get-tags-string))
  9277. (col (current-column))
  9278. (org-setting-tags t)
  9279. table current-tags inherited-tags ; computed below when needed
  9280. tags p0 c0 c1 rpl)
  9281. (if arg
  9282. (save-excursion
  9283. (goto-char (point-min))
  9284. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  9285. (while (re-search-forward re nil t)
  9286. (org-set-tags nil t)
  9287. (end-of-line 1)))
  9288. (message "All tags realigned to column %d" org-tags-column))
  9289. (if just-align
  9290. (setq tags current)
  9291. ;; Get a new set of tags from the user
  9292. (save-excursion
  9293. (setq table (or org-tag-alist (org-get-buffer-tags))
  9294. org-last-tags-completion-table table
  9295. current-tags (org-split-string current ":")
  9296. inherited-tags (nreverse
  9297. (nthcdr (length current-tags)
  9298. (nreverse (org-get-tags-at))))
  9299. tags
  9300. (if (or (eq t org-use-fast-tag-selection)
  9301. (and org-use-fast-tag-selection
  9302. (delq nil (mapcar 'cdr table))))
  9303. (org-fast-tag-selection
  9304. current-tags inherited-tags table
  9305. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  9306. (let ((org-add-colon-after-tag-completion t))
  9307. (org-trim
  9308. (org-without-partial-completion
  9309. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  9310. nil nil current 'org-tags-history)))))))
  9311. (while (string-match "[-+&]+" tags)
  9312. ;; No boolean logic, just a list
  9313. (setq tags (replace-match ":" t t tags))))
  9314. (if (string-match "\\`[\t ]*\\'" tags)
  9315. (setq tags "")
  9316. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  9317. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  9318. ;; Insert new tags at the correct column
  9319. (beginning-of-line 1)
  9320. (cond
  9321. ((and (equal current "") (equal tags "")))
  9322. ((re-search-forward
  9323. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  9324. (point-at-eol) t)
  9325. (if (equal tags "")
  9326. (setq rpl "")
  9327. (goto-char (match-beginning 0))
  9328. (setq c0 (current-column) p0 (point)
  9329. c1 (max (1+ c0) (if (> org-tags-column 0)
  9330. org-tags-column
  9331. (- (- org-tags-column) (length tags))))
  9332. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  9333. (replace-match rpl t t)
  9334. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  9335. tags)
  9336. (t (error "Tags alignment failed")))
  9337. (org-move-to-column col)
  9338. (unless just-align
  9339. (run-hooks 'org-after-tags-change-hook)))))
  9340. (defun org-change-tag-in-region (beg end tag off)
  9341. "Add or remove TAG for each entry in the region.
  9342. This works in the agenda, and also in an org-mode buffer."
  9343. (interactive
  9344. (list (region-beginning) (region-end)
  9345. (let ((org-last-tags-completion-table
  9346. (if (org-mode-p)
  9347. (org-get-buffer-tags)
  9348. (org-global-tags-completion-table))))
  9349. (org-ido-completing-read
  9350. "Tag: " 'org-tags-completion-function nil nil nil
  9351. 'org-tags-history))
  9352. (progn
  9353. (message "[s]et or [r]emove? ")
  9354. (equal (read-char-exclusive) ?r))))
  9355. (if (fboundp 'deactivate-mark) (deactivate-mark))
  9356. (let ((agendap (equal major-mode 'org-agenda-mode))
  9357. l1 l2 m buf pos newhead (cnt 0))
  9358. (goto-char end)
  9359. (setq l2 (1- (org-current-line)))
  9360. (goto-char beg)
  9361. (setq l1 (org-current-line))
  9362. (loop for l from l1 to l2 do
  9363. (goto-line l)
  9364. (setq m (get-text-property (point) 'org-hd-marker))
  9365. (when (or (and (org-mode-p) (org-on-heading-p))
  9366. (and agendap m))
  9367. (setq buf (if agendap (marker-buffer m) (current-buffer))
  9368. pos (if agendap m (point)))
  9369. (with-current-buffer buf
  9370. (save-excursion
  9371. (save-restriction
  9372. (goto-char pos)
  9373. (setq cnt (1+ cnt))
  9374. (org-toggle-tag tag (if off 'off 'on))
  9375. (setq newhead (org-get-heading)))))
  9376. (and agendap (org-agenda-change-all-lines newhead m))))
  9377. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  9378. (defun org-tags-completion-function (string predicate &optional flag)
  9379. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  9380. (confirm (lambda (x) (stringp (car x)))))
  9381. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  9382. (setq s1 (match-string 1 string)
  9383. s2 (match-string 2 string))
  9384. (setq s1 "" s2 string))
  9385. (cond
  9386. ((eq flag nil)
  9387. ;; try completion
  9388. (setq rtn (try-completion s2 ctable confirm))
  9389. (if (stringp rtn)
  9390. (setq rtn
  9391. (concat s1 s2 (substring rtn (length s2))
  9392. (if (and org-add-colon-after-tag-completion
  9393. (assoc rtn ctable))
  9394. ":" ""))))
  9395. rtn)
  9396. ((eq flag t)
  9397. ;; all-completions
  9398. (all-completions s2 ctable confirm)
  9399. )
  9400. ((eq flag 'lambda)
  9401. ;; exact match?
  9402. (assoc s2 ctable)))
  9403. ))
  9404. (defun org-fast-tag-insert (kwd tags face &optional end)
  9405. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  9406. (insert (format "%-12s" (concat kwd ":"))
  9407. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  9408. (or end "")))
  9409. (defun org-fast-tag-show-exit (flag)
  9410. (save-excursion
  9411. (goto-line 3)
  9412. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  9413. (replace-match ""))
  9414. (when flag
  9415. (end-of-line 1)
  9416. (org-move-to-column (- (window-width) 19) t)
  9417. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  9418. (defun org-set-current-tags-overlay (current prefix)
  9419. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  9420. (if (featurep 'xemacs)
  9421. (org-overlay-display org-tags-overlay (concat prefix s)
  9422. 'secondary-selection)
  9423. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  9424. (org-overlay-display org-tags-overlay (concat prefix s)))))
  9425. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  9426. "Fast tag selection with single keys.
  9427. CURRENT is the current list of tags in the headline, INHERITED is the
  9428. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  9429. possibly with grouping information. TODO-TABLE is a similar table with
  9430. TODO keywords, should these have keys assigned to them.
  9431. If the keys are nil, a-z are automatically assigned.
  9432. Returns the new tags string, or nil to not change the current settings."
  9433. (let* ((fulltable (append table todo-table))
  9434. (maxlen (apply 'max (mapcar
  9435. (lambda (x)
  9436. (if (stringp (car x)) (string-width (car x)) 0))
  9437. fulltable)))
  9438. (buf (current-buffer))
  9439. (expert (eq org-fast-tag-selection-single-key 'expert))
  9440. (buffer-tags nil)
  9441. (fwidth (+ maxlen 3 1 3))
  9442. (ncol (/ (- (window-width) 4) fwidth))
  9443. (i-face 'org-done)
  9444. (c-face 'org-todo)
  9445. tg cnt e c char c1 c2 ntable tbl rtn
  9446. ov-start ov-end ov-prefix
  9447. (exit-after-next org-fast-tag-selection-single-key)
  9448. (done-keywords org-done-keywords)
  9449. groups ingroup)
  9450. (save-excursion
  9451. (beginning-of-line 1)
  9452. (if (looking-at
  9453. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9454. (setq ov-start (match-beginning 1)
  9455. ov-end (match-end 1)
  9456. ov-prefix "")
  9457. (setq ov-start (1- (point-at-eol))
  9458. ov-end (1+ ov-start))
  9459. (skip-chars-forward "^\n\r")
  9460. (setq ov-prefix
  9461. (concat
  9462. (buffer-substring (1- (point)) (point))
  9463. (if (> (current-column) org-tags-column)
  9464. " "
  9465. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9466. (org-move-overlay org-tags-overlay ov-start ov-end)
  9467. (save-window-excursion
  9468. (if expert
  9469. (set-buffer (get-buffer-create " *Org tags*"))
  9470. (delete-other-windows)
  9471. (split-window-vertically)
  9472. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9473. (erase-buffer)
  9474. (org-set-local 'org-done-keywords done-keywords)
  9475. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9476. (org-fast-tag-insert "Current" current c-face "\n\n")
  9477. (org-fast-tag-show-exit exit-after-next)
  9478. (org-set-current-tags-overlay current ov-prefix)
  9479. (setq tbl fulltable char ?a cnt 0)
  9480. (while (setq e (pop tbl))
  9481. (cond
  9482. ((equal e '(:startgroup))
  9483. (push '() groups) (setq ingroup t)
  9484. (when (not (= cnt 0))
  9485. (setq cnt 0)
  9486. (insert "\n"))
  9487. (insert "{ "))
  9488. ((equal e '(:endgroup))
  9489. (setq ingroup nil cnt 0)
  9490. (insert "}\n"))
  9491. ((equal e '(:newline))
  9492. (insert "\n "))
  9493. (t
  9494. (setq tg (car e) c2 nil)
  9495. (if (cdr e)
  9496. (setq c (cdr e))
  9497. ;; automatically assign a character.
  9498. (setq c1 (string-to-char
  9499. (downcase (substring
  9500. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9501. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9502. (while (or (rassoc char ntable) (rassoc char table))
  9503. (setq char (1+ char)))
  9504. (setq c2 c1))
  9505. (setq c (or c2 char)))
  9506. (if ingroup (push tg (car groups)))
  9507. (setq tg (org-add-props tg nil 'face
  9508. (cond
  9509. ((not (assoc tg table))
  9510. (org-get-todo-face tg))
  9511. ((member tg current) c-face)
  9512. ((member tg inherited) i-face)
  9513. (t nil))))
  9514. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9515. (insert "[" c "] " tg (make-string
  9516. (- fwidth 4 (length tg)) ?\ ))
  9517. (push (cons tg c) ntable)
  9518. (when (= (setq cnt (1+ cnt)) ncol)
  9519. (insert "\n")
  9520. (if ingroup (insert " "))
  9521. (setq cnt 0)))))
  9522. (setq ntable (nreverse ntable))
  9523. (insert "\n")
  9524. (goto-char (point-min))
  9525. (if (not expert) (org-fit-window-to-buffer))
  9526. (setq rtn
  9527. (catch 'exit
  9528. (while t
  9529. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9530. (if groups " [!] no groups" " [!]groups")
  9531. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9532. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9533. (cond
  9534. ((= c ?\r) (throw 'exit t))
  9535. ((= c ?!)
  9536. (setq groups (not groups))
  9537. (goto-char (point-min))
  9538. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9539. ((= c ?\C-c)
  9540. (if (not expert)
  9541. (org-fast-tag-show-exit
  9542. (setq exit-after-next (not exit-after-next)))
  9543. (setq expert nil)
  9544. (delete-other-windows)
  9545. (split-window-vertically)
  9546. (org-switch-to-buffer-other-window " *Org tags*")
  9547. (org-fit-window-to-buffer)))
  9548. ((or (= c ?\C-g)
  9549. (and (= c ?q) (not (rassoc c ntable))))
  9550. (org-detach-overlay org-tags-overlay)
  9551. (setq quit-flag t))
  9552. ((= c ?\ )
  9553. (setq current nil)
  9554. (if exit-after-next (setq exit-after-next 'now)))
  9555. ((= c ?\t)
  9556. (condition-case nil
  9557. (setq tg (org-ido-completing-read
  9558. "Tag: "
  9559. (or buffer-tags
  9560. (with-current-buffer buf
  9561. (org-get-buffer-tags)))))
  9562. (quit (setq tg "")))
  9563. (when (string-match "\\S-" tg)
  9564. (add-to-list 'buffer-tags (list tg))
  9565. (if (member tg current)
  9566. (setq current (delete tg current))
  9567. (push tg current)))
  9568. (if exit-after-next (setq exit-after-next 'now)))
  9569. ((setq e (rassoc c todo-table) tg (car e))
  9570. (with-current-buffer buf
  9571. (save-excursion (org-todo tg)))
  9572. (if exit-after-next (setq exit-after-next 'now)))
  9573. ((setq e (rassoc c ntable) tg (car e))
  9574. (if (member tg current)
  9575. (setq current (delete tg current))
  9576. (loop for g in groups do
  9577. (if (member tg g)
  9578. (mapc (lambda (x)
  9579. (setq current (delete x current)))
  9580. g)))
  9581. (push tg current))
  9582. (if exit-after-next (setq exit-after-next 'now))))
  9583. ;; Create a sorted list
  9584. (setq current
  9585. (sort current
  9586. (lambda (a b)
  9587. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9588. (if (eq exit-after-next 'now) (throw 'exit t))
  9589. (goto-char (point-min))
  9590. (beginning-of-line 2)
  9591. (delete-region (point) (point-at-eol))
  9592. (org-fast-tag-insert "Current" current c-face)
  9593. (org-set-current-tags-overlay current ov-prefix)
  9594. (while (re-search-forward
  9595. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9596. (setq tg (match-string 1))
  9597. (add-text-properties
  9598. (match-beginning 1) (match-end 1)
  9599. (list 'face
  9600. (cond
  9601. ((member tg current) c-face)
  9602. ((member tg inherited) i-face)
  9603. (t (get-text-property (match-beginning 1) 'face))))))
  9604. (goto-char (point-min)))))
  9605. (org-detach-overlay org-tags-overlay)
  9606. (if rtn
  9607. (mapconcat 'identity current ":")
  9608. nil))))
  9609. (defun org-get-tags-string ()
  9610. "Get the TAGS string in the current headline."
  9611. (unless (org-on-heading-p t)
  9612. (error "Not on a heading"))
  9613. (save-excursion
  9614. (beginning-of-line 1)
  9615. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9616. (org-match-string-no-properties 1)
  9617. "")))
  9618. (defun org-get-tags ()
  9619. "Get the list of tags specified in the current headline."
  9620. (org-split-string (org-get-tags-string) ":"))
  9621. (defun org-get-buffer-tags ()
  9622. "Get a table of all tags used in the buffer, for completion."
  9623. (let (tags)
  9624. (save-excursion
  9625. (goto-char (point-min))
  9626. (while (re-search-forward
  9627. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9628. (when (equal (char-after (point-at-bol 0)) ?*)
  9629. (mapc (lambda (x) (add-to-list 'tags x))
  9630. (org-split-string (org-match-string-no-properties 1) ":")))))
  9631. (mapcar 'list tags)))
  9632. ;;;; The mapping API
  9633. ;;;###autoload
  9634. (defun org-map-entries (func &optional match scope &rest skip)
  9635. "Call FUNC at each headline selected by MATCH in SCOPE.
  9636. FUNC is a function or a lisp form. The function will be called without
  9637. arguments, with the cursor positioned at the beginning of the headline.
  9638. The return values of all calls to the function will be collected and
  9639. returned as a list.
  9640. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9641. Only headlines that are matched by this query will be considered during
  9642. the iteration. When MATCH is nil or t, all headlines will be
  9643. visited by the iteration.
  9644. SCOPE determines the scope of this command. It can be any of:
  9645. nil The current buffer, respecting the restriction if any
  9646. tree The subtree started with the entry at point
  9647. file The current buffer, without restriction
  9648. file-with-archives
  9649. The current buffer, and any archives associated with it
  9650. agenda All agenda files
  9651. agenda-with-archives
  9652. All agenda files with any archive files associated with them
  9653. \(file1 file2 ...)
  9654. If this is a list, all files in the list will be scanned
  9655. The remaining args are treated as settings for the skipping facilities of
  9656. the scanner. The following items can be given here:
  9657. archive skip trees with the archive tag.
  9658. comment skip trees with the COMMENT keyword
  9659. function or Emacs Lisp form:
  9660. will be used as value for `org-agenda-skip-function', so whenever
  9661. the the function returns t, FUNC will not be called for that
  9662. entry and search will continue from the point where the
  9663. function leaves it."
  9664. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9665. (org-agenda-skip-archived-trees (memq 'archive skip))
  9666. (org-agenda-skip-comment-trees (memq 'comment skip))
  9667. (org-agenda-skip-function
  9668. (car (org-delete-all '(comment archive) skip)))
  9669. (org-tags-match-list-sublevels t)
  9670. matcher file res
  9671. org-todo-keywords-for-agenda
  9672. org-done-keywords-for-agenda
  9673. org-todo-keyword-alist-for-agenda
  9674. org-tag-alist-for-agenda)
  9675. (cond
  9676. ((eq match t) (setq matcher t))
  9677. ((eq match nil) (setq matcher t))
  9678. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9679. (save-excursion
  9680. (save-restriction
  9681. (when (eq scope 'tree)
  9682. (org-back-to-heading t)
  9683. (org-narrow-to-subtree)
  9684. (setq scope nil))
  9685. (if (not scope)
  9686. (progn
  9687. (org-prepare-agenda-buffers
  9688. (list (buffer-file-name (current-buffer))))
  9689. (setq res (org-scan-tags func matcher)))
  9690. ;; Get the right scope
  9691. (cond
  9692. ((and scope (listp scope) (symbolp (car scope)))
  9693. (setq scope (eval scope)))
  9694. ((eq scope 'agenda)
  9695. (setq scope (org-agenda-files t)))
  9696. ((eq scope 'agenda-with-archives)
  9697. (setq scope (org-agenda-files t))
  9698. (setq scope (org-add-archive-files scope)))
  9699. ((eq scope 'file)
  9700. (setq scope (list (buffer-file-name))))
  9701. ((eq scope 'file-with-archives)
  9702. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9703. (org-prepare-agenda-buffers scope)
  9704. (while (setq file (pop scope))
  9705. (with-current-buffer (org-find-base-buffer-visiting file)
  9706. (save-excursion
  9707. (save-restriction
  9708. (widen)
  9709. (goto-char (point-min))
  9710. (setq res (append res (org-scan-tags func matcher))))))))))
  9711. res))
  9712. ;;;; Properties
  9713. ;;; Setting and retrieving properties
  9714. (defconst org-special-properties
  9715. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9716. "TIMESTAMP" "TIMESTAMP_IA")
  9717. "The special properties valid in Org-mode.
  9718. These are properties that are not defined in the property drawer,
  9719. but in some other way.")
  9720. (defconst org-default-properties
  9721. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9722. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9723. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9724. "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
  9725. "Some properties that are used by Org-mode for various purposes.
  9726. Being in this list makes sure that they are offered for completion.")
  9727. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9728. "Regular expression matching the first line of a property drawer.")
  9729. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9730. "Regular expression matching the first line of a property drawer.")
  9731. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9732. "Regular expression matching the first line of a property drawer.")
  9733. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9734. "Regular expression matching the first line of a property drawer.")
  9735. (defconst org-property-drawer-re
  9736. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9737. org-property-end-re "\\)\n?")
  9738. "Matches an entire property drawer.")
  9739. (defconst org-clock-drawer-re
  9740. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9741. org-property-end-re "\\)\n?")
  9742. "Matches an entire clock drawer.")
  9743. (defun org-property-action ()
  9744. "Do an action on properties."
  9745. (interactive)
  9746. (let (c)
  9747. (org-at-property-p)
  9748. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9749. (setq c (read-char-exclusive))
  9750. (cond
  9751. ((equal c ?s)
  9752. (call-interactively 'org-set-property))
  9753. ((equal c ?d)
  9754. (call-interactively 'org-delete-property))
  9755. ((equal c ?D)
  9756. (call-interactively 'org-delete-property-globally))
  9757. ((equal c ?c)
  9758. (call-interactively 'org-compute-property-at-point))
  9759. (t (error "No such property action %c" c)))))
  9760. (defun org-at-property-p ()
  9761. "Is the cursor in a property line?"
  9762. ;; FIXME: Does not check if we are actually in the drawer.
  9763. ;; FIXME: also returns true on any drawers.....
  9764. ;; This is used by C-c C-c for property action.
  9765. (save-excursion
  9766. (beginning-of-line 1)
  9767. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9768. (defun org-get-property-block (&optional beg end force)
  9769. "Return the (beg . end) range of the body of the property drawer.
  9770. BEG and END can be beginning and end of subtree, if not given
  9771. they will be found.
  9772. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9773. (catch 'exit
  9774. (save-excursion
  9775. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9776. (end (or end (progn (outline-next-heading) (point)))))
  9777. (goto-char beg)
  9778. (if (re-search-forward org-property-start-re end t)
  9779. (setq beg (1+ (match-end 0)))
  9780. (if force
  9781. (save-excursion
  9782. (org-insert-property-drawer)
  9783. (setq end (progn (outline-next-heading) (point))))
  9784. (throw 'exit nil))
  9785. (goto-char beg)
  9786. (if (re-search-forward org-property-start-re end t)
  9787. (setq beg (1+ (match-end 0)))))
  9788. (if (re-search-forward org-property-end-re end t)
  9789. (setq end (match-beginning 0))
  9790. (or force (throw 'exit nil))
  9791. (goto-char beg)
  9792. (setq end beg)
  9793. (org-indent-line-function)
  9794. (insert ":END:\n"))
  9795. (cons beg end)))))
  9796. (defun org-entry-properties (&optional pom which)
  9797. "Get all properties of the entry at point-or-marker POM.
  9798. This includes the TODO keyword, the tags, time strings for deadline,
  9799. scheduled, and clocking, and any additional properties defined in the
  9800. entry. The return value is an alist, keys may occur multiple times
  9801. if the property key was used several times.
  9802. POM may also be nil, in which case the current entry is used.
  9803. If WHICH is nil or `all', get all properties. If WHICH is
  9804. `special' or `standard', only get that subclass."
  9805. (setq which (or which 'all))
  9806. (org-with-point-at pom
  9807. (let ((clockstr (substring org-clock-string 0 -1))
  9808. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9809. beg end range props sum-props key value string clocksum)
  9810. (save-excursion
  9811. (when (condition-case nil
  9812. (and (org-mode-p) (org-back-to-heading t))
  9813. (error nil))
  9814. (setq beg (point))
  9815. (setq sum-props (get-text-property (point) 'org-summaries))
  9816. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9817. (outline-next-heading)
  9818. (setq end (point))
  9819. (when (memq which '(all special))
  9820. ;; Get the special properties, like TODO and tags
  9821. (goto-char beg)
  9822. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9823. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9824. (when (looking-at org-priority-regexp)
  9825. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9826. (when (and (setq value (org-get-tags-string))
  9827. (string-match "\\S-" value))
  9828. (push (cons "TAGS" value) props))
  9829. (when (setq value (org-get-tags-at))
  9830. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9831. props))
  9832. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9833. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9834. string (if (equal key clockstr)
  9835. (org-no-properties
  9836. (org-trim
  9837. (buffer-substring
  9838. (match-beginning 3) (goto-char (point-at-eol)))))
  9839. (substring (org-match-string-no-properties 3) 1 -1)))
  9840. (unless key
  9841. (if (= (char-after (match-beginning 3)) ?\[)
  9842. (setq key "TIMESTAMP_IA")
  9843. (setq key "TIMESTAMP")))
  9844. (when (or (equal key clockstr) (not (assoc key props)))
  9845. (push (cons key string) props)))
  9846. )
  9847. (when (memq which '(all standard))
  9848. ;; Get the standard properties, like :PORP: ...
  9849. (setq range (org-get-property-block beg end))
  9850. (when range
  9851. (goto-char (car range))
  9852. (while (re-search-forward
  9853. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9854. (cdr range) t)
  9855. (setq key (org-match-string-no-properties 1)
  9856. value (org-trim (or (org-match-string-no-properties 2) "")))
  9857. (unless (member key excluded)
  9858. (push (cons key (or value "")) props)))))
  9859. (if clocksum
  9860. (push (cons "CLOCKSUM"
  9861. (org-columns-number-to-string (/ (float clocksum) 60.)
  9862. 'add_times))
  9863. props))
  9864. (unless (assoc "CATEGORY" props)
  9865. (setq value (or (org-get-category)
  9866. (progn (org-refresh-category-properties)
  9867. (org-get-category))))
  9868. (push (cons "CATEGORY" value) props))
  9869. (append sum-props (nreverse props)))))))
  9870. (defun org-entry-get (pom property &optional inherit)
  9871. "Get value of PROPERTY for entry at point-or-marker POM.
  9872. If INHERIT is non-nil and the entry does not have the property,
  9873. then also check higher levels of the hierarchy.
  9874. If INHERIT is the symbol `selective', use inheritance only if the setting
  9875. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9876. If the property is present but empty, the return value is the empty string.
  9877. If the property is not present at all, nil is returned."
  9878. (org-with-point-at pom
  9879. (if (and inherit (if (eq inherit 'selective)
  9880. (org-property-inherit-p property)
  9881. t))
  9882. (org-entry-get-with-inheritance property)
  9883. (if (member property org-special-properties)
  9884. ;; We need a special property. Use brute force, get all properties.
  9885. (cdr (assoc property (org-entry-properties nil 'special)))
  9886. (let ((range (org-get-property-block)))
  9887. (if (and range
  9888. (goto-char (car range))
  9889. (re-search-forward
  9890. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9891. (cdr range) t))
  9892. ;; Found the property, return it.
  9893. (if (match-end 1)
  9894. (org-match-string-no-properties 1)
  9895. "")))))))
  9896. (defun org-property-or-variable-value (var &optional inherit)
  9897. "Check if there is a property fixing the value of VAR.
  9898. If yes, return this value. If not, return the current value of the variable."
  9899. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9900. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9901. (read prop)
  9902. (symbol-value var))))
  9903. (defun org-entry-delete (pom property)
  9904. "Delete the property PROPERTY from entry at point-or-marker POM."
  9905. (org-with-point-at pom
  9906. (if (member property org-special-properties)
  9907. nil ; cannot delete these properties.
  9908. (let ((range (org-get-property-block)))
  9909. (if (and range
  9910. (goto-char (car range))
  9911. (re-search-forward
  9912. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9913. (cdr range) t))
  9914. (progn
  9915. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9916. t)
  9917. nil)))))
  9918. ;; Multi-values properties are properties that contain multiple values
  9919. ;; These values are assumed to be single words, separated by whitespace.
  9920. (defun org-entry-add-to-multivalued-property (pom property value)
  9921. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9922. (let* ((old (org-entry-get pom property))
  9923. (values (and old (org-split-string old "[ \t]"))))
  9924. (setq value (org-entry-protect-space value))
  9925. (unless (member value values)
  9926. (setq values (cons value values))
  9927. (org-entry-put pom property
  9928. (mapconcat 'identity values " ")))))
  9929. (defun org-entry-remove-from-multivalued-property (pom property value)
  9930. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9931. (let* ((old (org-entry-get pom property))
  9932. (values (and old (org-split-string old "[ \t]"))))
  9933. (setq value (org-entry-protect-space value))
  9934. (when (member value values)
  9935. (setq values (delete value values))
  9936. (org-entry-put pom property
  9937. (mapconcat 'identity values " ")))))
  9938. (defun org-entry-member-in-multivalued-property (pom property value)
  9939. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9940. (let* ((old (org-entry-get pom property))
  9941. (values (and old (org-split-string old "[ \t]"))))
  9942. (setq value (org-entry-protect-space value))
  9943. (member value values)))
  9944. (defun org-entry-get-multivalued-property (pom property)
  9945. "Return a list of values in a multivalued property."
  9946. (let* ((value (org-entry-get pom property))
  9947. (values (and value (org-split-string value "[ \t]"))))
  9948. (mapcar 'org-entry-restore-space values)))
  9949. (defun org-entry-put-multivalued-property (pom property &rest values)
  9950. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9951. VALUES should be a list of strings. Spaces will be protected."
  9952. (org-entry-put pom property
  9953. (mapconcat 'org-entry-protect-space values " "))
  9954. (let* ((value (org-entry-get pom property))
  9955. (values (and value (org-split-string value "[ \t]"))))
  9956. (mapcar 'org-entry-restore-space values)))
  9957. (defun org-entry-protect-space (s)
  9958. "Protect spaces and newline in string S."
  9959. (while (string-match " " s)
  9960. (setq s (replace-match "%20" t t s)))
  9961. (while (string-match "\n" s)
  9962. (setq s (replace-match "%0A" t t s)))
  9963. s)
  9964. (defun org-entry-restore-space (s)
  9965. "Restore spaces and newline in string S."
  9966. (while (string-match "%20" s)
  9967. (setq s (replace-match " " t t s)))
  9968. (while (string-match "%0A" s)
  9969. (setq s (replace-match "\n" t t s)))
  9970. s)
  9971. (defvar org-entry-property-inherited-from (make-marker)
  9972. "Marker pointing to the entry from where a property was inherited.
  9973. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9974. location of the entry where the inheritance search matched. If there was
  9975. no match, the marker will point nowhere.
  9976. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9977. is set.")
  9978. (defun org-entry-get-with-inheritance (property)
  9979. "Get entry property, and search higher levels if not present."
  9980. (move-marker org-entry-property-inherited-from nil)
  9981. (let (tmp)
  9982. (save-excursion
  9983. (save-restriction
  9984. (widen)
  9985. (catch 'ex
  9986. (while t
  9987. (when (setq tmp (org-entry-get nil property))
  9988. (org-back-to-heading t)
  9989. (move-marker org-entry-property-inherited-from (point))
  9990. (throw 'ex tmp))
  9991. (or (org-up-heading-safe) (throw 'ex nil)))))
  9992. (or tmp
  9993. (cdr (assoc property org-file-properties))
  9994. (cdr (assoc property org-global-properties))
  9995. (cdr (assoc property org-global-properties-fixed))))))
  9996. (defun org-entry-put (pom property value)
  9997. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9998. (org-with-point-at pom
  9999. (org-back-to-heading t)
  10000. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  10001. range)
  10002. (cond
  10003. ((equal property "TODO")
  10004. (when (and (stringp value) (string-match "\\S-" value)
  10005. (not (member value org-todo-keywords-1)))
  10006. (error "\"%s\" is not a valid TODO state" value))
  10007. (if (or (not value)
  10008. (not (string-match "\\S-" value)))
  10009. (setq value 'none))
  10010. (org-todo value)
  10011. (org-set-tags nil 'align))
  10012. ((equal property "PRIORITY")
  10013. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  10014. (string-to-char value) ?\ ))
  10015. (org-set-tags nil 'align))
  10016. ((equal property "SCHEDULED")
  10017. (if (re-search-forward org-scheduled-time-regexp end t)
  10018. (cond
  10019. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10020. ((eq value 'later) (org-timestamp-change 1 'day))
  10021. (t (call-interactively 'org-schedule)))
  10022. (call-interactively 'org-schedule)))
  10023. ((equal property "DEADLINE")
  10024. (if (re-search-forward org-deadline-time-regexp end t)
  10025. (cond
  10026. ((eq value 'earlier) (org-timestamp-change -1 'day))
  10027. ((eq value 'later) (org-timestamp-change 1 'day))
  10028. (t (call-interactively 'org-deadline)))
  10029. (call-interactively 'org-deadline)))
  10030. ((member property org-special-properties)
  10031. (error "The %s property can not yet be set with `org-entry-put'"
  10032. property))
  10033. (t ; a non-special property
  10034. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  10035. (setq range (org-get-property-block beg end 'force))
  10036. (goto-char (car range))
  10037. (if (re-search-forward
  10038. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  10039. (progn
  10040. (delete-region (match-beginning 1) (match-end 1))
  10041. (goto-char (match-beginning 1)))
  10042. (goto-char (cdr range))
  10043. (insert "\n")
  10044. (backward-char 1)
  10045. (org-indent-line-function)
  10046. (insert ":" property ":"))
  10047. (and value (insert " " value))
  10048. (org-indent-line-function)))))))
  10049. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  10050. "Get all property keys in the current buffer.
  10051. With INCLUDE-SPECIALS, also list the special properties that reflect things
  10052. like tags and TODO state.
  10053. With INCLUDE-DEFAULTS, also include properties that has special meaning
  10054. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  10055. With INCLUDE-COLUMNS, also include property names given in COLUMN
  10056. formats in the current buffer."
  10057. (let (rtn range cfmt s p)
  10058. (save-excursion
  10059. (save-restriction
  10060. (widen)
  10061. (goto-char (point-min))
  10062. (while (re-search-forward org-property-start-re nil t)
  10063. (setq range (org-get-property-block))
  10064. (goto-char (car range))
  10065. (while (re-search-forward
  10066. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  10067. (cdr range) t)
  10068. (add-to-list 'rtn (org-match-string-no-properties 1)))
  10069. (outline-next-heading))))
  10070. (when include-specials
  10071. (setq rtn (append org-special-properties rtn)))
  10072. (when include-defaults
  10073. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  10074. (when include-columns
  10075. (save-excursion
  10076. (save-restriction
  10077. (widen)
  10078. (goto-char (point-min))
  10079. (while (re-search-forward
  10080. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  10081. nil t)
  10082. (setq cfmt (match-string 2) s 0)
  10083. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  10084. cfmt s)
  10085. (setq s (match-end 0)
  10086. p (match-string 1 cfmt))
  10087. (unless (or (equal p "ITEM")
  10088. (member p org-special-properties))
  10089. (add-to-list 'rtn (match-string 1 cfmt))))))))
  10090. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  10091. (defun org-property-values (key)
  10092. "Return a list of all values of property KEY."
  10093. (save-excursion
  10094. (save-restriction
  10095. (widen)
  10096. (goto-char (point-min))
  10097. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  10098. values)
  10099. (while (re-search-forward re nil t)
  10100. (add-to-list 'values (org-trim (match-string 1))))
  10101. (delete "" values)))))
  10102. (defun org-insert-property-drawer ()
  10103. "Insert a property drawer into the current entry."
  10104. (interactive)
  10105. (org-back-to-heading t)
  10106. (looking-at outline-regexp)
  10107. (let ((indent (- (match-end 0)(match-beginning 0)))
  10108. (beg (point))
  10109. (re (concat "^[ \t]*" org-keyword-time-regexp))
  10110. end hiddenp)
  10111. (outline-next-heading)
  10112. (setq end (point))
  10113. (goto-char beg)
  10114. (while (re-search-forward re end t))
  10115. (setq hiddenp (org-invisible-p))
  10116. (end-of-line 1)
  10117. (and (equal (char-after) ?\n) (forward-char 1))
  10118. (while (looking-at "^[ \t]*\\(:CLOCK:\\|:LOGBOOK:\\|CLOCK:\\|:END:\\)")
  10119. (if (member (match-string 1) '("CLOCK:" ":END:"))
  10120. ;; just skip this line
  10121. (beginning-of-line 2)
  10122. ;; Drawer start, find the end
  10123. (re-search-forward "^\\*+ \\|^[ \t]*:END:" nil t)
  10124. (beginning-of-line 1)))
  10125. (org-skip-over-state-notes)
  10126. (skip-chars-backward " \t\n\r")
  10127. (if (eq (char-before) ?*) (forward-char 1))
  10128. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  10129. (beginning-of-line 0)
  10130. (org-indent-to-column indent)
  10131. (beginning-of-line 2)
  10132. (org-indent-to-column indent)
  10133. (beginning-of-line 0)
  10134. (if hiddenp
  10135. (save-excursion
  10136. (org-back-to-heading t)
  10137. (hide-entry))
  10138. (org-flag-drawer t))))
  10139. (defun org-set-property (property value)
  10140. "In the current entry, set PROPERTY to VALUE.
  10141. When called interactively, this will prompt for a property name, offering
  10142. completion on existing and default properties. And then it will prompt
  10143. for a value, offering completion either on allowed values (via an inherited
  10144. xxx_ALL property) or on existing values in other instances of this property
  10145. in the current file."
  10146. (interactive
  10147. (let* ((completion-ignore-case t)
  10148. (keys (org-buffer-property-keys nil t t))
  10149. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  10150. (prop (if (member prop0 keys)
  10151. prop0
  10152. (or (cdr (assoc (downcase prop0)
  10153. (mapcar (lambda (x) (cons (downcase x) x))
  10154. keys)))
  10155. prop0)))
  10156. (cur (org-entry-get nil prop))
  10157. (allowed (org-property-get-allowed-values nil prop 'table))
  10158. (existing (mapcar 'list (org-property-values prop)))
  10159. (val (if allowed
  10160. (org-completing-read "Value: " allowed nil 'req-match)
  10161. (let (org-completion-use-ido)
  10162. (org-completing-read
  10163. (concat "Value" (if (and cur (string-match "\\S-" cur))
  10164. (concat "[" cur "]") "")
  10165. ": ")
  10166. existing nil nil "" nil cur)))))
  10167. (list prop (if (equal val "") cur val))))
  10168. (unless (equal (org-entry-get nil property) value)
  10169. (org-entry-put nil property value)))
  10170. (defun org-delete-property (property)
  10171. "In the current entry, delete PROPERTY."
  10172. (interactive
  10173. (let* ((completion-ignore-case t)
  10174. (prop (org-ido-completing-read
  10175. "Property: " (org-entry-properties nil 'standard))))
  10176. (list prop)))
  10177. (message "Property %s %s" property
  10178. (if (org-entry-delete nil property)
  10179. "deleted"
  10180. "was not present in the entry")))
  10181. (defun org-delete-property-globally (property)
  10182. "Remove PROPERTY globally, from all entries."
  10183. (interactive
  10184. (let* ((completion-ignore-case t)
  10185. (prop (org-ido-completing-read
  10186. "Globally remove property: "
  10187. (mapcar 'list (org-buffer-property-keys)))))
  10188. (list prop)))
  10189. (save-excursion
  10190. (save-restriction
  10191. (widen)
  10192. (goto-char (point-min))
  10193. (let ((cnt 0))
  10194. (while (re-search-forward
  10195. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  10196. nil t)
  10197. (setq cnt (1+ cnt))
  10198. (replace-match ""))
  10199. (message "Property \"%s\" removed from %d entries" property cnt)))))
  10200. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  10201. (defun org-compute-property-at-point ()
  10202. "Compute the property at point.
  10203. This looks for an enclosing column format, extracts the operator and
  10204. then applies it to the property in the column format's scope."
  10205. (interactive)
  10206. (unless (org-at-property-p)
  10207. (error "Not at a property"))
  10208. (let ((prop (org-match-string-no-properties 2)))
  10209. (org-columns-get-format-and-top-level)
  10210. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  10211. (error "No operator defined for property %s" prop))
  10212. (org-columns-compute prop)))
  10213. (defun org-property-get-allowed-values (pom property &optional table)
  10214. "Get allowed values for the property PROPERTY.
  10215. When TABLE is non-nil, return an alist that can directly be used for
  10216. completion."
  10217. (let (vals)
  10218. (cond
  10219. ((equal property "TODO")
  10220. (setq vals (org-with-point-at pom
  10221. (append org-todo-keywords-1 '("")))))
  10222. ((equal property "PRIORITY")
  10223. (let ((n org-lowest-priority))
  10224. (while (>= n org-highest-priority)
  10225. (push (char-to-string n) vals)
  10226. (setq n (1- n)))))
  10227. ((member property org-special-properties))
  10228. (t
  10229. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  10230. (when (and vals (string-match "\\S-" vals))
  10231. (setq vals (car (read-from-string (concat "(" vals ")"))))
  10232. (setq vals (mapcar (lambda (x)
  10233. (cond ((stringp x) x)
  10234. ((numberp x) (number-to-string x))
  10235. ((symbolp x) (symbol-name x))
  10236. (t "???")))
  10237. vals)))))
  10238. (if table (mapcar 'list vals) vals)))
  10239. (defun org-property-previous-allowed-value (&optional previous)
  10240. "Switch to the next allowed value for this property."
  10241. (interactive)
  10242. (org-property-next-allowed-value t))
  10243. (defun org-property-next-allowed-value (&optional previous)
  10244. "Switch to the next allowed value for this property."
  10245. (interactive)
  10246. (unless (org-at-property-p)
  10247. (error "Not at a property"))
  10248. (let* ((key (match-string 2))
  10249. (value (match-string 3))
  10250. (allowed (or (org-property-get-allowed-values (point) key)
  10251. (and (member value '("[ ]" "[-]" "[X]"))
  10252. '("[ ]" "[X]"))))
  10253. nval)
  10254. (unless allowed
  10255. (error "Allowed values for this property have not been defined"))
  10256. (if previous (setq allowed (reverse allowed)))
  10257. (if (member value allowed)
  10258. (setq nval (car (cdr (member value allowed)))))
  10259. (setq nval (or nval (car allowed)))
  10260. (if (equal nval value)
  10261. (error "Only one allowed value for this property"))
  10262. (org-at-property-p)
  10263. (replace-match (concat " :" key ": " nval) t t)
  10264. (org-indent-line-function)
  10265. (beginning-of-line 1)
  10266. (skip-chars-forward " \t")))
  10267. (defun org-find-entry-with-id (ident)
  10268. "Locate the entry that contains the ID property with exact value IDENT.
  10269. IDENT can be a string, a symbol or a number, this function will search for
  10270. the string representation of it.
  10271. Return the position where this entry starts, or nil if there is no such entry."
  10272. (interactive "sID: ")
  10273. (let ((id (cond
  10274. ((stringp ident) ident)
  10275. ((symbol-name ident) (symbol-name ident))
  10276. ((numberp ident) (number-to-string ident))
  10277. (t (error "IDENT %s must be a string, symbol or number" ident))))
  10278. (case-fold-search nil))
  10279. (save-excursion
  10280. (save-restriction
  10281. (widen)
  10282. (goto-char (point-min))
  10283. (when (re-search-forward
  10284. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  10285. nil t)
  10286. (org-back-to-heading)
  10287. (point))))))
  10288. ;;;; Timestamps
  10289. (defvar org-last-changed-timestamp nil)
  10290. (defvar org-last-inserted-timestamp nil
  10291. "The last time stamp inserted with `org-insert-time-stamp'.")
  10292. (defvar org-time-was-given) ; dynamically scoped parameter
  10293. (defvar org-end-time-was-given) ; dynamically scoped parameter
  10294. (defvar org-ts-what) ; dynamically scoped parameter
  10295. (defun org-time-stamp (arg &optional inactive)
  10296. "Prompt for a date/time and insert a time stamp.
  10297. If the user specifies a time like HH:MM, or if this command is called
  10298. with a prefix argument, the time stamp will contain date and time.
  10299. Otherwise, only the date will be included. All parts of a date not
  10300. specified by the user will be filled in from the current date/time.
  10301. So if you press just return without typing anything, the time stamp
  10302. will represent the current date/time. If there is already a timestamp
  10303. at the cursor, it will be modified."
  10304. (interactive "P")
  10305. (let* ((ts nil)
  10306. (default-time
  10307. ;; Default time is either today, or, when entering a range,
  10308. ;; the range start.
  10309. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  10310. (save-excursion
  10311. (re-search-backward
  10312. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  10313. (- (point) 20) t)))
  10314. (apply 'encode-time (org-parse-time-string (match-string 1)))
  10315. (current-time)))
  10316. (default-input (and ts (org-get-compact-tod ts)))
  10317. org-time-was-given org-end-time-was-given time)
  10318. (cond
  10319. ((and (org-at-timestamp-p t)
  10320. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  10321. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  10322. (insert "--")
  10323. (setq time (let ((this-command this-command))
  10324. (org-read-date arg 'totime nil nil
  10325. default-time default-input)))
  10326. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  10327. ((org-at-timestamp-p t)
  10328. (setq time (let ((this-command this-command))
  10329. (org-read-date arg 'totime nil nil default-time default-input)))
  10330. (when (org-at-timestamp-p t) ; just to get the match data
  10331. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  10332. (replace-match "")
  10333. (setq org-last-changed-timestamp
  10334. (org-insert-time-stamp
  10335. time (or org-time-was-given arg)
  10336. inactive nil nil (list org-end-time-was-given))))
  10337. (message "Timestamp updated"))
  10338. (t
  10339. (setq time (let ((this-command this-command))
  10340. (org-read-date arg 'totime nil nil default-time default-input)))
  10341. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  10342. nil nil (list org-end-time-was-given))))))
  10343. ;; FIXME: can we use this for something else, like computing time differences?
  10344. (defun org-get-compact-tod (s)
  10345. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  10346. (let* ((t1 (match-string 1 s))
  10347. (h1 (string-to-number (match-string 2 s)))
  10348. (m1 (string-to-number (match-string 3 s)))
  10349. (t2 (and (match-end 4) (match-string 5 s)))
  10350. (h2 (and t2 (string-to-number (match-string 6 s))))
  10351. (m2 (and t2 (string-to-number (match-string 7 s))))
  10352. dh dm)
  10353. (if (not t2)
  10354. t1
  10355. (setq dh (- h2 h1) dm (- m2 m1))
  10356. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  10357. (concat t1 "+" (number-to-string dh)
  10358. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  10359. (defun org-time-stamp-inactive (&optional arg)
  10360. "Insert an inactive time stamp.
  10361. An inactive time stamp is enclosed in square brackets instead of angle
  10362. brackets. It is inactive in the sense that it does not trigger agenda entries,
  10363. does not link to the calendar and cannot be changed with the S-cursor keys.
  10364. So these are more for recording a certain time/date."
  10365. (interactive "P")
  10366. (org-time-stamp arg 'inactive))
  10367. (defvar org-date-ovl (org-make-overlay 1 1))
  10368. (org-overlay-put org-date-ovl 'face 'org-warning)
  10369. (org-detach-overlay org-date-ovl)
  10370. (defvar org-ans1) ; dynamically scoped parameter
  10371. (defvar org-ans2) ; dynamically scoped parameter
  10372. (defvar org-plain-time-of-day-regexp) ; defined below
  10373. (defvar org-overriding-default-time nil) ; dynamically scoped
  10374. (defvar org-read-date-overlay nil)
  10375. (defvar org-dcst nil) ; dynamically scoped
  10376. (defun org-read-date (&optional with-time to-time from-string prompt
  10377. default-time default-input)
  10378. "Read a date, possibly a time, and make things smooth for the user.
  10379. The prompt will suggest to enter an ISO date, but you can also enter anything
  10380. which will at least partially be understood by `parse-time-string'.
  10381. Unrecognized parts of the date will default to the current day, month, year,
  10382. hour and minute. If this command is called to replace a timestamp at point,
  10383. of to enter the second timestamp of a range, the default time is taken from the
  10384. existing stamp. For example,
  10385. 3-2-5 --> 2003-02-05
  10386. feb 15 --> currentyear-02-15
  10387. sep 12 9 --> 2009-09-12
  10388. 12:45 --> today 12:45
  10389. 22 sept 0:34 --> currentyear-09-22 0:34
  10390. 12 --> currentyear-currentmonth-12
  10391. Fri --> nearest Friday (today or later)
  10392. etc.
  10393. Furthermore you can specify a relative date by giving, as the *first* thing
  10394. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  10395. change in days weeks, months, years.
  10396. With a single plus or minus, the date is relative to today. With a double
  10397. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  10398. +4d --> four days from today
  10399. +4 --> same as above
  10400. +2w --> two weeks from today
  10401. ++5 --> five days from default date
  10402. The function understands only English month and weekday abbreviations,
  10403. but this can be configured with the variables `parse-time-months' and
  10404. `parse-time-weekdays'.
  10405. While prompting, a calendar is popped up - you can also select the
  10406. date with the mouse (button 1). The calendar shows a period of three
  10407. months. To scroll it to other months, use the keys `>' and `<'.
  10408. If you don't like the calendar, turn it off with
  10409. \(setq org-read-date-popup-calendar nil)
  10410. With optional argument TO-TIME, the date will immediately be converted
  10411. to an internal time.
  10412. With an optional argument WITH-TIME, the prompt will suggest to also
  10413. insert a time. Note that when WITH-TIME is not set, you can still
  10414. enter a time, and this function will inform the calling routine about
  10415. this change. The calling routine may then choose to change the format
  10416. used to insert the time stamp into the buffer to include the time.
  10417. With optional argument FROM-STRING, read from this string instead from
  10418. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  10419. the time/date that is used for everything that is not specified by the
  10420. user."
  10421. (require 'parse-time)
  10422. (let* ((org-time-stamp-rounding-minutes
  10423. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  10424. (org-dcst org-display-custom-times)
  10425. (ct (org-current-time))
  10426. (def (or org-overriding-default-time default-time ct))
  10427. (defdecode (decode-time def))
  10428. (dummy (progn
  10429. (when (< (nth 2 defdecode) org-extend-today-until)
  10430. (setcar (nthcdr 2 defdecode) -1)
  10431. (setcar (nthcdr 1 defdecode) 59)
  10432. (setq def (apply 'encode-time defdecode)
  10433. defdecode (decode-time def)))))
  10434. (calendar-move-hook nil)
  10435. (calendar-view-diary-initially-flag nil)
  10436. (view-diary-entries-initially nil)
  10437. (calendar-view-holidays-initially-flag nil)
  10438. (view-calendar-holidays-initially nil)
  10439. (timestr (format-time-string
  10440. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  10441. (prompt (concat (if prompt (concat prompt " ") "")
  10442. (format "Date+time [%s]: " timestr)))
  10443. ans (org-ans0 "") org-ans1 org-ans2 final)
  10444. (cond
  10445. (from-string (setq ans from-string))
  10446. (org-read-date-popup-calendar
  10447. (save-excursion
  10448. (save-window-excursion
  10449. (calendar)
  10450. (calendar-forward-day (- (time-to-days def)
  10451. (calendar-absolute-from-gregorian
  10452. (calendar-current-date))))
  10453. (org-eval-in-calendar nil t)
  10454. (let* ((old-map (current-local-map))
  10455. (map (copy-keymap calendar-mode-map))
  10456. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  10457. (org-defkey map (kbd "RET") 'org-calendar-select)
  10458. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  10459. 'org-calendar-select-mouse)
  10460. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  10461. 'org-calendar-select-mouse)
  10462. (org-defkey minibuffer-local-map [(meta shift left)]
  10463. (lambda () (interactive)
  10464. (org-eval-in-calendar '(calendar-backward-month 1))))
  10465. (org-defkey minibuffer-local-map [(meta shift right)]
  10466. (lambda () (interactive)
  10467. (org-eval-in-calendar '(calendar-forward-month 1))))
  10468. (org-defkey minibuffer-local-map [(meta shift up)]
  10469. (lambda () (interactive)
  10470. (org-eval-in-calendar '(calendar-backward-year 1))))
  10471. (org-defkey minibuffer-local-map [(meta shift down)]
  10472. (lambda () (interactive)
  10473. (org-eval-in-calendar '(calendar-forward-year 1))))
  10474. (org-defkey minibuffer-local-map [(shift up)]
  10475. (lambda () (interactive)
  10476. (org-eval-in-calendar '(calendar-backward-week 1))))
  10477. (org-defkey minibuffer-local-map [(shift down)]
  10478. (lambda () (interactive)
  10479. (org-eval-in-calendar '(calendar-forward-week 1))))
  10480. (org-defkey minibuffer-local-map [(shift left)]
  10481. (lambda () (interactive)
  10482. (org-eval-in-calendar '(calendar-backward-day 1))))
  10483. (org-defkey minibuffer-local-map [(shift right)]
  10484. (lambda () (interactive)
  10485. (org-eval-in-calendar '(calendar-forward-day 1))))
  10486. (org-defkey minibuffer-local-map ">"
  10487. (lambda () (interactive)
  10488. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10489. (org-defkey minibuffer-local-map "<"
  10490. (lambda () (interactive)
  10491. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10492. (unwind-protect
  10493. (progn
  10494. (use-local-map map)
  10495. (add-hook 'post-command-hook 'org-read-date-display)
  10496. (setq org-ans0 (read-string prompt default-input nil nil))
  10497. ;; org-ans0: from prompt
  10498. ;; org-ans1: from mouse click
  10499. ;; org-ans2: from calendar motion
  10500. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10501. (remove-hook 'post-command-hook 'org-read-date-display)
  10502. (use-local-map old-map)
  10503. (when org-read-date-overlay
  10504. (org-delete-overlay org-read-date-overlay)
  10505. (setq org-read-date-overlay nil)))))))
  10506. (t ; Naked prompt only
  10507. (unwind-protect
  10508. (setq ans (read-string prompt default-input nil timestr))
  10509. (when org-read-date-overlay
  10510. (org-delete-overlay org-read-date-overlay)
  10511. (setq org-read-date-overlay nil)))))
  10512. (setq final (org-read-date-analyze ans def defdecode))
  10513. (if to-time
  10514. (apply 'encode-time final)
  10515. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10516. (format "%04d-%02d-%02d %02d:%02d"
  10517. (nth 5 final) (nth 4 final) (nth 3 final)
  10518. (nth 2 final) (nth 1 final))
  10519. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10520. (defvar def)
  10521. (defvar defdecode)
  10522. (defvar with-time)
  10523. (defun org-read-date-display ()
  10524. "Display the current date prompt interpretation in the minibuffer."
  10525. (when org-read-date-display-live
  10526. (when org-read-date-overlay
  10527. (org-delete-overlay org-read-date-overlay))
  10528. (let ((p (point)))
  10529. (end-of-line 1)
  10530. (while (not (equal (buffer-substring
  10531. (max (point-min) (- (point) 4)) (point))
  10532. " "))
  10533. (insert " "))
  10534. (goto-char p))
  10535. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10536. " " (or org-ans1 org-ans2)))
  10537. (org-end-time-was-given nil)
  10538. (f (org-read-date-analyze ans def defdecode))
  10539. (fmts (if org-dcst
  10540. org-time-stamp-custom-formats
  10541. org-time-stamp-formats))
  10542. (fmt (if (or with-time
  10543. (and (boundp 'org-time-was-given) org-time-was-given))
  10544. (cdr fmts)
  10545. (car fmts)))
  10546. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10547. (when (and org-end-time-was-given
  10548. (string-match org-plain-time-of-day-regexp txt))
  10549. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10550. org-end-time-was-given
  10551. (substring txt (match-end 0)))))
  10552. (setq org-read-date-overlay
  10553. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10554. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10555. (defun org-read-date-analyze (ans def defdecode)
  10556. "Analyse the combined answer of the date prompt."
  10557. ;; FIXME: cleanup and comment
  10558. (let (delta deltan deltaw deltadef year month day
  10559. hour minute second wday pm h2 m2 tl wday1
  10560. iso-year iso-weekday iso-week iso-year iso-date)
  10561. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10562. (setq ans "+0"))
  10563. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10564. (setq ans (replace-match "" t t ans)
  10565. deltan (car delta)
  10566. deltaw (nth 1 delta)
  10567. deltadef (nth 2 delta)))
  10568. ;; Check if there is an iso week date in there
  10569. ;; If yes, sore the info and postpone interpreting it until the rest
  10570. ;; of the parsing is done
  10571. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10572. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10573. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10574. iso-week (string-to-number (match-string 2 ans)))
  10575. (setq ans (replace-match "" t t ans)))
  10576. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10577. (when (string-match
  10578. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10579. (setq year (if (match-end 2)
  10580. (string-to-number (match-string 2 ans))
  10581. (string-to-number (format-time-string "%Y")))
  10582. month (string-to-number (match-string 3 ans))
  10583. day (string-to-number (match-string 4 ans)))
  10584. (if (< year 100) (setq year (+ 2000 year)))
  10585. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10586. t nil ans)))
  10587. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10588. ;; If there is a time with am/pm, and *no* time without it, we convert
  10589. ;; so that matching will be successful.
  10590. (loop for i from 1 to 2 do ; twice, for end time as well
  10591. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10592. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10593. (setq hour (string-to-number (match-string 1 ans))
  10594. minute (if (match-end 3)
  10595. (string-to-number (match-string 3 ans))
  10596. 0)
  10597. pm (equal ?p
  10598. (string-to-char (downcase (match-string 4 ans)))))
  10599. (if (and (= hour 12) (not pm))
  10600. (setq hour 0)
  10601. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10602. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10603. t t ans))))
  10604. ;; Check if a time range is given as a duration
  10605. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10606. (setq hour (string-to-number (match-string 1 ans))
  10607. h2 (+ hour (string-to-number (match-string 3 ans)))
  10608. minute (string-to-number (match-string 2 ans))
  10609. m2 (+ minute (if (match-end 5) (string-to-number
  10610. (match-string 5 ans))0)))
  10611. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10612. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10613. t t ans)))
  10614. ;; Check if there is a time range
  10615. (when (boundp 'org-end-time-was-given)
  10616. (setq org-time-was-given nil)
  10617. (when (and (string-match org-plain-time-of-day-regexp ans)
  10618. (match-end 8))
  10619. (setq org-end-time-was-given (match-string 8 ans))
  10620. (setq ans (concat (substring ans 0 (match-beginning 7))
  10621. (substring ans (match-end 7))))))
  10622. (setq tl (parse-time-string ans)
  10623. day (or (nth 3 tl) (nth 3 defdecode))
  10624. month (or (nth 4 tl)
  10625. (if (and org-read-date-prefer-future
  10626. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10627. (1+ (nth 4 defdecode))
  10628. (nth 4 defdecode)))
  10629. year (or (nth 5 tl)
  10630. (if (and org-read-date-prefer-future
  10631. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10632. (1+ (nth 5 defdecode))
  10633. (nth 5 defdecode)))
  10634. hour (or (nth 2 tl) (nth 2 defdecode))
  10635. minute (or (nth 1 tl) (nth 1 defdecode))
  10636. second (or (nth 0 tl) 0)
  10637. wday (nth 6 tl))
  10638. ;; Special date definitions below
  10639. (cond
  10640. (iso-week
  10641. ;; There was an iso week
  10642. (setq year (or iso-year year)
  10643. day (or iso-weekday wday 1)
  10644. wday nil ; to make sure that the trigger below does not match
  10645. iso-date (calendar-gregorian-from-absolute
  10646. (calendar-absolute-from-iso
  10647. (list iso-week day year))))
  10648. ; FIXME: Should we also push ISO weeks into the future?
  10649. ; (when (and org-read-date-prefer-future
  10650. ; (not iso-year)
  10651. ; (< (calendar-absolute-from-gregorian iso-date)
  10652. ; (time-to-days (current-time))))
  10653. ; (setq year (1+ year)
  10654. ; iso-date (calendar-gregorian-from-absolute
  10655. ; (calendar-absolute-from-iso
  10656. ; (list iso-week day year)))))
  10657. (setq month (car iso-date)
  10658. year (nth 2 iso-date)
  10659. day (nth 1 iso-date)))
  10660. (deltan
  10661. (unless deltadef
  10662. (let ((now (decode-time (current-time))))
  10663. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10664. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10665. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10666. ((equal deltaw "m") (setq month (+ month deltan)))
  10667. ((equal deltaw "y") (setq year (+ year deltan)))))
  10668. ((and wday (not (nth 3 tl)))
  10669. ;; Weekday was given, but no day, so pick that day in the week
  10670. ;; on or after the derived date.
  10671. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10672. (unless (equal wday wday1)
  10673. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10674. (if (and (boundp 'org-time-was-given)
  10675. (nth 2 tl))
  10676. (setq org-time-was-given t))
  10677. (if (< year 100) (setq year (+ 2000 year)))
  10678. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10679. (list second minute hour day month year)))
  10680. (defvar parse-time-weekdays)
  10681. (defun org-read-date-get-relative (s today default)
  10682. "Check string S for special relative date string.
  10683. TODAY and DEFAULT are internal times, for today and for a default.
  10684. Return shift list (N what def-flag)
  10685. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10686. N is the number of WHATs to shift.
  10687. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10688. the DEFAULT date rather than TODAY."
  10689. (when (and
  10690. (string-match
  10691. (concat
  10692. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10693. "\\([0-9]+\\)?"
  10694. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10695. "\\([ \t]\\|$\\)") s)
  10696. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10697. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10698. (string-to-char (substring (match-string 1 s) -1))
  10699. ?+))
  10700. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10701. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10702. (what (if (match-end 3) (match-string 3 s) "d"))
  10703. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10704. (date (if rel default today))
  10705. (wday (nth 6 (decode-time date)))
  10706. delta)
  10707. (if wday1
  10708. (progn
  10709. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10710. (if (= dir ?-) (setq delta (- delta 7)))
  10711. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10712. (list delta "d" rel))
  10713. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10714. (defun org-eval-in-calendar (form &optional keepdate)
  10715. "Eval FORM in the calendar window and return to current window.
  10716. Also, store the cursor date in variable org-ans2."
  10717. (let ((sw (selected-window)))
  10718. (select-window (get-buffer-window "*Calendar*"))
  10719. (eval form)
  10720. (when (and (not keepdate) (calendar-cursor-to-date))
  10721. (let* ((date (calendar-cursor-to-date))
  10722. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10723. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10724. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10725. (select-window sw)))
  10726. (defun org-calendar-select ()
  10727. "Return to `org-read-date' with the date currently selected.
  10728. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10729. (interactive)
  10730. (when (calendar-cursor-to-date)
  10731. (let* ((date (calendar-cursor-to-date))
  10732. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10733. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10734. (if (active-minibuffer-window) (exit-minibuffer))))
  10735. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10736. "Insert a date stamp for the date given by the internal TIME.
  10737. WITH-HM means, use the stamp format that includes the time of the day.
  10738. INACTIVE means use square brackets instead of angular ones, so that the
  10739. stamp will not contribute to the agenda.
  10740. PRE and POST are optional strings to be inserted before and after the
  10741. stamp.
  10742. The command returns the inserted time stamp."
  10743. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10744. stamp)
  10745. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10746. (insert-before-markers (or pre ""))
  10747. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10748. (when (listp extra)
  10749. (setq extra (car extra))
  10750. (if (and (stringp extra)
  10751. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10752. (setq extra (format "-%02d:%02d"
  10753. (string-to-number (match-string 1 extra))
  10754. (string-to-number (match-string 2 extra))))
  10755. (setq extra nil)))
  10756. (when extra
  10757. (backward-char 1)
  10758. (insert-before-markers extra)
  10759. (forward-char 1))
  10760. (insert-before-markers (or post ""))
  10761. (setq org-last-inserted-timestamp stamp)))
  10762. (defun org-toggle-time-stamp-overlays ()
  10763. "Toggle the use of custom time stamp formats."
  10764. (interactive)
  10765. (setq org-display-custom-times (not org-display-custom-times))
  10766. (unless org-display-custom-times
  10767. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10768. (while (setq p (next-single-property-change p 'display))
  10769. (if (and (get-text-property p 'display)
  10770. (eq (get-text-property p 'face) 'org-date))
  10771. (remove-text-properties
  10772. p (setq p (next-single-property-change p 'display))
  10773. '(display t))))
  10774. (set-buffer-modified-p bmp)))
  10775. (if (featurep 'xemacs)
  10776. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10777. (org-restart-font-lock)
  10778. (setq org-table-may-need-update t)
  10779. (if org-display-custom-times
  10780. (message "Time stamps are overlayed with custom format")
  10781. (message "Time stamp overlays removed")))
  10782. (defun org-display-custom-time (beg end)
  10783. "Overlay modified time stamp format over timestamp between BEG and END."
  10784. (let* ((ts (buffer-substring beg end))
  10785. t1 w1 with-hm tf time str w2 (off 0))
  10786. (save-match-data
  10787. (setq t1 (org-parse-time-string ts t))
  10788. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10789. (setq off (- (match-end 0) (match-beginning 0)))))
  10790. (setq end (- end off))
  10791. (setq w1 (- end beg)
  10792. with-hm (and (nth 1 t1) (nth 2 t1))
  10793. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10794. time (org-fix-decoded-time t1)
  10795. str (org-add-props
  10796. (format-time-string
  10797. (substring tf 1 -1) (apply 'encode-time time))
  10798. nil 'mouse-face 'highlight)
  10799. w2 (length str))
  10800. (if (not (= w2 w1))
  10801. (add-text-properties (1+ beg) (+ 2 beg)
  10802. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10803. (if (featurep 'xemacs)
  10804. (progn
  10805. (put-text-property beg end 'invisible t)
  10806. (put-text-property beg end 'end-glyph (make-glyph str)))
  10807. (put-text-property beg end 'display str))))
  10808. (defun org-translate-time (string)
  10809. "Translate all timestamps in STRING to custom format.
  10810. But do this only if the variable `org-display-custom-times' is set."
  10811. (when org-display-custom-times
  10812. (save-match-data
  10813. (let* ((start 0)
  10814. (re org-ts-regexp-both)
  10815. t1 with-hm inactive tf time str beg end)
  10816. (while (setq start (string-match re string start))
  10817. (setq beg (match-beginning 0)
  10818. end (match-end 0)
  10819. t1 (save-match-data
  10820. (org-parse-time-string (substring string beg end) t))
  10821. with-hm (and (nth 1 t1) (nth 2 t1))
  10822. inactive (equal (substring string beg (1+ beg)) "[")
  10823. tf (funcall (if with-hm 'cdr 'car)
  10824. org-time-stamp-custom-formats)
  10825. time (org-fix-decoded-time t1)
  10826. str (format-time-string
  10827. (concat
  10828. (if inactive "[" "<") (substring tf 1 -1)
  10829. (if inactive "]" ">"))
  10830. (apply 'encode-time time))
  10831. string (replace-match str t t string)
  10832. start (+ start (length str)))))))
  10833. string)
  10834. (defun org-fix-decoded-time (time)
  10835. "Set 0 instead of nil for the first 6 elements of time.
  10836. Don't touch the rest."
  10837. (let ((n 0))
  10838. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10839. (defun org-days-to-time (timestamp-string)
  10840. "Difference between TIMESTAMP-STRING and now in days."
  10841. (- (time-to-days (org-time-string-to-time timestamp-string))
  10842. (time-to-days (current-time))))
  10843. (defun org-deadline-close (timestamp-string &optional ndays)
  10844. "Is the time in TIMESTAMP-STRING close to the current date?"
  10845. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10846. (and (< (org-days-to-time timestamp-string) ndays)
  10847. (not (org-entry-is-done-p))))
  10848. (defun org-get-wdays (ts)
  10849. "Get the deadline lead time appropriate for timestring TS."
  10850. (cond
  10851. ((<= org-deadline-warning-days 0)
  10852. ;; 0 or negative, enforce this value no matter what
  10853. (- org-deadline-warning-days))
  10854. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\| \\)" ts)
  10855. ;; lead time is specified.
  10856. (floor (* (string-to-number (match-string 1 ts))
  10857. (cdr (assoc (match-string 2 ts)
  10858. '(("d" . 1) ("w" . 7)
  10859. ("m" . 30.4) ("y" . 365.25)))))))
  10860. ;; go for the default.
  10861. (t org-deadline-warning-days)))
  10862. (defun org-calendar-select-mouse (ev)
  10863. "Return to `org-read-date' with the date currently selected.
  10864. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10865. (interactive "e")
  10866. (mouse-set-point ev)
  10867. (when (calendar-cursor-to-date)
  10868. (let* ((date (calendar-cursor-to-date))
  10869. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10870. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10871. (if (active-minibuffer-window) (exit-minibuffer))))
  10872. (defun org-check-deadlines (ndays)
  10873. "Check if there are any deadlines due or past due.
  10874. A deadline is considered due if it happens within `org-deadline-warning-days'
  10875. days from today's date. If the deadline appears in an entry marked DONE,
  10876. it is not shown. The prefix arg NDAYS can be used to test that many
  10877. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10878. (interactive "P")
  10879. (let* ((org-warn-days
  10880. (cond
  10881. ((equal ndays '(4)) 100000)
  10882. (ndays (prefix-numeric-value ndays))
  10883. (t (abs org-deadline-warning-days))))
  10884. (case-fold-search nil)
  10885. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10886. (callback
  10887. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10888. (message "%d deadlines past-due or due within %d days"
  10889. (org-occur regexp nil callback)
  10890. org-warn-days)))
  10891. (defun org-check-before-date (date)
  10892. "Check if there are deadlines or scheduled entries before DATE."
  10893. (interactive (list (org-read-date)))
  10894. (let ((case-fold-search nil)
  10895. (regexp (concat "\\<\\(" org-deadline-string
  10896. "\\|" org-scheduled-string
  10897. "\\) *<\\([^>]+\\)>"))
  10898. (callback
  10899. (lambda () (time-less-p
  10900. (org-time-string-to-time (match-string 2))
  10901. (org-time-string-to-time date)))))
  10902. (message "%d entries before %s"
  10903. (org-occur regexp nil callback) date)))
  10904. (defun org-evaluate-time-range (&optional to-buffer)
  10905. "Evaluate a time range by computing the difference between start and end.
  10906. Normally the result is just printed in the echo area, but with prefix arg
  10907. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10908. If the time range is actually in a table, the result is inserted into the
  10909. next column.
  10910. For time difference computation, a year is assumed to be exactly 365
  10911. days in order to avoid rounding problems."
  10912. (interactive "P")
  10913. (or
  10914. (org-clock-update-time-maybe)
  10915. (save-excursion
  10916. (unless (org-at-date-range-p t)
  10917. (goto-char (point-at-bol))
  10918. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10919. (if (not (org-at-date-range-p t))
  10920. (error "Not at a time-stamp range, and none found in current line")))
  10921. (let* ((ts1 (match-string 1))
  10922. (ts2 (match-string 2))
  10923. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10924. (match-end (match-end 0))
  10925. (time1 (org-time-string-to-time ts1))
  10926. (time2 (org-time-string-to-time ts2))
  10927. (t1 (time-to-seconds time1))
  10928. (t2 (time-to-seconds time2))
  10929. (diff (abs (- t2 t1)))
  10930. (negative (< (- t2 t1) 0))
  10931. ;; (ys (floor (* 365 24 60 60)))
  10932. (ds (* 24 60 60))
  10933. (hs (* 60 60))
  10934. (fy "%dy %dd %02d:%02d")
  10935. (fy1 "%dy %dd")
  10936. (fd "%dd %02d:%02d")
  10937. (fd1 "%dd")
  10938. (fh "%02d:%02d")
  10939. y d h m align)
  10940. (if havetime
  10941. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10942. y 0
  10943. d (floor (/ diff ds)) diff (mod diff ds)
  10944. h (floor (/ diff hs)) diff (mod diff hs)
  10945. m (floor (/ diff 60)))
  10946. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10947. y 0
  10948. d (floor (+ (/ diff ds) 0.5))
  10949. h 0 m 0))
  10950. (if (not to-buffer)
  10951. (message "%s" (org-make-tdiff-string y d h m))
  10952. (if (org-at-table-p)
  10953. (progn
  10954. (goto-char match-end)
  10955. (setq align t)
  10956. (and (looking-at " *|") (goto-char (match-end 0))))
  10957. (goto-char match-end))
  10958. (if (looking-at
  10959. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10960. (replace-match ""))
  10961. (if negative (insert " -"))
  10962. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10963. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10964. (insert " " (format fh h m))))
  10965. (if align (org-table-align))
  10966. (message "Time difference inserted")))))
  10967. (defun org-make-tdiff-string (y d h m)
  10968. (let ((fmt "")
  10969. (l nil))
  10970. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10971. l (push y l)))
  10972. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10973. l (push d l)))
  10974. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10975. l (push h l)))
  10976. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10977. l (push m l)))
  10978. (apply 'format fmt (nreverse l))))
  10979. (defun org-time-string-to-time (s)
  10980. (apply 'encode-time (org-parse-time-string s)))
  10981. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10982. "Convert a time stamp to an absolute day number.
  10983. If there is a specifyer for a cyclic time stamp, get the closest date to
  10984. DAYNR.
  10985. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10986. (cond
  10987. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10988. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10989. daynr
  10990. (+ daynr 1000)))
  10991. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10992. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10993. (time-to-days (current-time))) (match-string 0 s)
  10994. prefer show-all))
  10995. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10996. (defun org-days-to-iso-week (days)
  10997. "Return the iso week number."
  10998. (require 'cal-iso)
  10999. (car (calendar-iso-from-absolute days)))
  11000. (defun org-small-year-to-year (year)
  11001. "Convert 2-digit years into 4-digit years.
  11002. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  11003. The year 2000 cannot be abbreviated. Any year larger than 99
  11004. is returned unchanged."
  11005. (if (< year 38)
  11006. (setq year (+ 2000 year))
  11007. (if (< year 100)
  11008. (setq year (+ 1900 year))))
  11009. year)
  11010. (defun org-time-from-absolute (d)
  11011. "Return the time corresponding to date D.
  11012. D may be an absolute day number, or a calendar-type list (month day year)."
  11013. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  11014. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  11015. (defun org-calendar-holiday ()
  11016. "List of holidays, for Diary display in Org-mode."
  11017. (require 'holidays)
  11018. (let ((hl (funcall
  11019. (if (fboundp 'calendar-check-holidays)
  11020. 'calendar-check-holidays 'check-calendar-holidays) date)))
  11021. (if hl (mapconcat 'identity hl "; "))))
  11022. (defun org-diary-sexp-entry (sexp entry date)
  11023. "Process a SEXP diary ENTRY for DATE."
  11024. (require 'diary-lib)
  11025. (let ((result (if calendar-debug-sexp
  11026. (let ((stack-trace-on-error t))
  11027. (eval (car (read-from-string sexp))))
  11028. (condition-case nil
  11029. (eval (car (read-from-string sexp)))
  11030. (error
  11031. (beep)
  11032. (message "Bad sexp at line %d in %s: %s"
  11033. (org-current-line)
  11034. (buffer-file-name) sexp)
  11035. (sleep-for 2))))))
  11036. (cond ((stringp result) result)
  11037. ((and (consp result)
  11038. (stringp (cdr result))) (cdr result))
  11039. (result entry)
  11040. (t nil))))
  11041. (defun org-diary-to-ical-string (frombuf)
  11042. "Get iCalendar entries from diary entries in buffer FROMBUF.
  11043. This uses the icalendar.el library."
  11044. (let* ((tmpdir (if (featurep 'xemacs)
  11045. (temp-directory)
  11046. temporary-file-directory))
  11047. (tmpfile (make-temp-name
  11048. (expand-file-name "orgics" tmpdir)))
  11049. buf rtn b e)
  11050. (save-excursion
  11051. (set-buffer frombuf)
  11052. (icalendar-export-region (point-min) (point-max) tmpfile)
  11053. (setq buf (find-buffer-visiting tmpfile))
  11054. (set-buffer buf)
  11055. (goto-char (point-min))
  11056. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  11057. (setq b (match-beginning 0)))
  11058. (goto-char (point-max))
  11059. (if (re-search-backward "^END:VEVENT" nil t)
  11060. (setq e (match-end 0)))
  11061. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  11062. (kill-buffer buf)
  11063. (delete-file tmpfile)
  11064. rtn))
  11065. (defun org-closest-date (start current change prefer show-all)
  11066. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  11067. When PREFER is `past' return a date that is either CURRENT or past.
  11068. When PREFER is `future', return a date that is either CURRENT or future.
  11069. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  11070. ;; Make the proper lists from the dates
  11071. (catch 'exit
  11072. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  11073. dn dw sday cday n1 n2 n0
  11074. d m y y1 y2 date1 date2 nmonths nm ny m2)
  11075. (setq start (org-date-to-gregorian start)
  11076. current (org-date-to-gregorian
  11077. (if show-all
  11078. current
  11079. (time-to-days (current-time))))
  11080. sday (calendar-absolute-from-gregorian start)
  11081. cday (calendar-absolute-from-gregorian current))
  11082. (if (<= cday sday) (throw 'exit sday))
  11083. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  11084. (setq dn (string-to-number (match-string 1 change))
  11085. dw (cdr (assoc (match-string 2 change) a1)))
  11086. (error "Invalid change specifyer: %s" change))
  11087. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  11088. (cond
  11089. ((eq dw 'day)
  11090. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  11091. n2 (+ n1 dn)))
  11092. ((eq dw 'year)
  11093. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  11094. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  11095. (setq date1 (list m d y1)
  11096. n1 (calendar-absolute-from-gregorian date1)
  11097. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  11098. n2 (calendar-absolute-from-gregorian date2)))
  11099. ((eq dw 'month)
  11100. ;; approx number of month between the two dates
  11101. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  11102. ;; How often does dn fit in there?
  11103. (setq d (nth 1 start) m (car start) y (nth 2 start)
  11104. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  11105. m (+ m nm)
  11106. ny (floor (/ m 12))
  11107. y (+ y ny)
  11108. m (- m (* ny 12)))
  11109. (while (> m 12) (setq m (- m 12) y (1+ y)))
  11110. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  11111. (setq m2 (+ m dn) y2 y)
  11112. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11113. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  11114. (while (<= n2 cday)
  11115. (setq n1 n2 m m2 y y2)
  11116. (setq m2 (+ m dn) y2 y)
  11117. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  11118. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  11119. ;; Make sure n1 is the earlier date
  11120. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  11121. (if show-all
  11122. (cond
  11123. ((eq prefer 'past) n1)
  11124. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11125. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  11126. (cond
  11127. ((eq prefer 'past) n1)
  11128. ((eq prefer 'future) (if (= cday n1) n1 n2))
  11129. (t (if (= cday n1) n1 n2)))))))
  11130. (defun org-date-to-gregorian (date)
  11131. "Turn any specification of DATE into a gregorian date for the calendar."
  11132. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  11133. ((and (listp date) (= (length date) 3)) date)
  11134. ((stringp date)
  11135. (setq date (org-parse-time-string date))
  11136. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  11137. ((listp date)
  11138. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  11139. (defun org-parse-time-string (s &optional nodefault)
  11140. "Parse the standard Org-mode time string.
  11141. This should be a lot faster than the normal `parse-time-string'.
  11142. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  11143. hour and minute fields will be nil if not given."
  11144. (if (string-match org-ts-regexp0 s)
  11145. (list 0
  11146. (if (or (match-beginning 8) (not nodefault))
  11147. (string-to-number (or (match-string 8 s) "0")))
  11148. (if (or (match-beginning 7) (not nodefault))
  11149. (string-to-number (or (match-string 7 s) "0")))
  11150. (string-to-number (match-string 4 s))
  11151. (string-to-number (match-string 3 s))
  11152. (string-to-number (match-string 2 s))
  11153. nil nil nil)
  11154. (make-list 9 0)))
  11155. (defun org-timestamp-up (&optional arg)
  11156. "Increase the date item at the cursor by one.
  11157. If the cursor is on the year, change the year. If it is on the month or
  11158. the day, change that.
  11159. With prefix ARG, change by that many units."
  11160. (interactive "p")
  11161. (org-timestamp-change (prefix-numeric-value arg)))
  11162. (defun org-timestamp-down (&optional arg)
  11163. "Decrease the date item at the cursor by one.
  11164. If the cursor is on the year, change the year. If it is on the month or
  11165. the day, change that.
  11166. With prefix ARG, change by that many units."
  11167. (interactive "p")
  11168. (org-timestamp-change (- (prefix-numeric-value arg))))
  11169. (defun org-timestamp-up-day (&optional arg)
  11170. "Increase the date in the time stamp by one day.
  11171. With prefix ARG, change that many days."
  11172. (interactive "p")
  11173. (if (and (not (org-at-timestamp-p t))
  11174. (org-on-heading-p))
  11175. (org-todo 'up)
  11176. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  11177. (defun org-timestamp-down-day (&optional arg)
  11178. "Decrease the date in the time stamp by one day.
  11179. With prefix ARG, change that many days."
  11180. (interactive "p")
  11181. (if (and (not (org-at-timestamp-p t))
  11182. (org-on-heading-p))
  11183. (org-todo 'down)
  11184. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  11185. (defun org-at-timestamp-p (&optional inactive-ok)
  11186. "Determine if the cursor is in or at a timestamp."
  11187. (interactive)
  11188. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  11189. (pos (point))
  11190. (ans (or (looking-at tsr)
  11191. (save-excursion
  11192. (skip-chars-backward "^[<\n\r\t")
  11193. (if (> (point) (point-min)) (backward-char 1))
  11194. (and (looking-at tsr)
  11195. (> (- (match-end 0) pos) -1))))))
  11196. (and ans
  11197. (boundp 'org-ts-what)
  11198. (setq org-ts-what
  11199. (cond
  11200. ((= pos (match-beginning 0)) 'bracket)
  11201. ((= pos (1- (match-end 0))) 'bracket)
  11202. ((org-pos-in-match-range pos 2) 'year)
  11203. ((org-pos-in-match-range pos 3) 'month)
  11204. ((org-pos-in-match-range pos 7) 'hour)
  11205. ((org-pos-in-match-range pos 8) 'minute)
  11206. ((or (org-pos-in-match-range pos 4)
  11207. (org-pos-in-match-range pos 5)) 'day)
  11208. ((and (> pos (or (match-end 8) (match-end 5)))
  11209. (< pos (match-end 0)))
  11210. (- pos (or (match-end 8) (match-end 5))))
  11211. (t 'day))))
  11212. ans))
  11213. (defun org-toggle-timestamp-type ()
  11214. "Toggle the type (<active> or [inactive]) of a time stamp."
  11215. (interactive)
  11216. (when (org-at-timestamp-p t)
  11217. (let ((beg (match-beginning 0)) (end (match-end 0))
  11218. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  11219. (save-excursion
  11220. (goto-char beg)
  11221. (while (re-search-forward "[][<>]" end t)
  11222. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  11223. t t)))
  11224. (message "Timestamp is now %sactive"
  11225. (if (equal (char-after beg) ?<) "" "in")))))
  11226. (defun org-timestamp-change (n &optional what)
  11227. "Change the date in the time stamp at point.
  11228. The date will be changed by N times WHAT. WHAT can be `day', `month',
  11229. `year', `minute', `second'. If WHAT is not given, the cursor position
  11230. in the timestamp determines what will be changed."
  11231. (let ((pos (point))
  11232. with-hm inactive
  11233. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  11234. org-ts-what
  11235. extra rem
  11236. ts time time0)
  11237. (if (not (org-at-timestamp-p t))
  11238. (error "Not at a timestamp"))
  11239. (if (and (not what) (eq org-ts-what 'bracket))
  11240. (org-toggle-timestamp-type)
  11241. (if (and (not what) (not (eq org-ts-what 'day))
  11242. org-display-custom-times
  11243. (get-text-property (point) 'display)
  11244. (not (get-text-property (1- (point)) 'display)))
  11245. (setq org-ts-what 'day))
  11246. (setq org-ts-what (or what org-ts-what)
  11247. inactive (= (char-after (match-beginning 0)) ?\[)
  11248. ts (match-string 0))
  11249. (replace-match "")
  11250. (if (string-match
  11251. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  11252. ts)
  11253. (setq extra (match-string 1 ts)))
  11254. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  11255. (setq with-hm t))
  11256. (setq time0 (org-parse-time-string ts))
  11257. (when (and (eq org-ts-what 'minute)
  11258. (eq current-prefix-arg nil))
  11259. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  11260. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  11261. (setcar (cdr time0) (+ (nth 1 time0)
  11262. (if (> n 0) (- rem) (- dm rem))))))
  11263. (setq time
  11264. (encode-time (or (car time0) 0)
  11265. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  11266. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  11267. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  11268. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  11269. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  11270. (nthcdr 6 time0)))
  11271. (when (integerp org-ts-what)
  11272. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  11273. (if (eq what 'calendar)
  11274. (let ((cal-date (org-get-date-from-calendar)))
  11275. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  11276. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  11277. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  11278. (setcar time0 (or (car time0) 0))
  11279. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  11280. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  11281. (setq time (apply 'encode-time time0))))
  11282. (setq org-last-changed-timestamp
  11283. (org-insert-time-stamp time with-hm inactive nil nil extra))
  11284. (org-clock-update-time-maybe)
  11285. (goto-char pos)
  11286. ;; Try to recenter the calendar window, if any
  11287. (if (and org-calendar-follow-timestamp-change
  11288. (get-buffer-window "*Calendar*" t)
  11289. (memq org-ts-what '(day month year)))
  11290. (org-recenter-calendar (time-to-days time))))))
  11291. (defun org-modify-ts-extra (s pos n dm)
  11292. "Change the different parts of the lead-time and repeat fields in timestamp."
  11293. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  11294. ng h m new rem)
  11295. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  11296. (cond
  11297. ((or (org-pos-in-match-range pos 2)
  11298. (org-pos-in-match-range pos 3))
  11299. (setq m (string-to-number (match-string 3 s))
  11300. h (string-to-number (match-string 2 s)))
  11301. (if (org-pos-in-match-range pos 2)
  11302. (setq h (+ h n))
  11303. (setq n (* dm (org-no-warnings (signum n))))
  11304. (when (not (= 0 (setq rem (% m dm))))
  11305. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  11306. (setq m (+ m n)))
  11307. (if (< m 0) (setq m (+ m 60) h (1- h)))
  11308. (if (> m 59) (setq m (- m 60) h (1+ h)))
  11309. (setq h (min 24 (max 0 h)))
  11310. (setq ng 1 new (format "-%02d:%02d" h m)))
  11311. ((org-pos-in-match-range pos 6)
  11312. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  11313. ((org-pos-in-match-range pos 5)
  11314. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  11315. ((org-pos-in-match-range pos 9)
  11316. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  11317. ((org-pos-in-match-range pos 8)
  11318. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  11319. (when ng
  11320. (setq s (concat
  11321. (substring s 0 (match-beginning ng))
  11322. new
  11323. (substring s (match-end ng))))))
  11324. s))
  11325. (defun org-recenter-calendar (date)
  11326. "If the calendar is visible, recenter it to DATE."
  11327. (let* ((win (selected-window))
  11328. (cwin (get-buffer-window "*Calendar*" t))
  11329. (calendar-move-hook nil))
  11330. (when cwin
  11331. (select-window cwin)
  11332. (calendar-goto-date (if (listp date) date
  11333. (calendar-gregorian-from-absolute date)))
  11334. (select-window win))))
  11335. (defun org-goto-calendar (&optional arg)
  11336. "Go to the Emacs calendar at the current date.
  11337. If there is a time stamp in the current line, go to that date.
  11338. A prefix ARG can be used to force the current date."
  11339. (interactive "P")
  11340. (let ((tsr org-ts-regexp) diff
  11341. (calendar-move-hook nil)
  11342. (calendar-view-holidays-initially-flag nil)
  11343. (view-calendar-holidays-initially nil)
  11344. (calendar-view-diary-initially-flag nil)
  11345. (view-diary-entries-initially nil))
  11346. (if (or (org-at-timestamp-p)
  11347. (save-excursion
  11348. (beginning-of-line 1)
  11349. (looking-at (concat ".*" tsr))))
  11350. (let ((d1 (time-to-days (current-time)))
  11351. (d2 (time-to-days
  11352. (org-time-string-to-time (match-string 1)))))
  11353. (setq diff (- d2 d1))))
  11354. (calendar)
  11355. (calendar-goto-today)
  11356. (if (and diff (not arg)) (calendar-forward-day diff))))
  11357. (defun org-get-date-from-calendar ()
  11358. "Return a list (month day year) of date at point in calendar."
  11359. (with-current-buffer "*Calendar*"
  11360. (save-match-data
  11361. (calendar-cursor-to-date))))
  11362. (defun org-date-from-calendar ()
  11363. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  11364. If there is already a time stamp at the cursor position, update it."
  11365. (interactive)
  11366. (if (org-at-timestamp-p t)
  11367. (org-timestamp-change 0 'calendar)
  11368. (let ((cal-date (org-get-date-from-calendar)))
  11369. (org-insert-time-stamp
  11370. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  11371. (defun org-minutes-to-hh:mm-string (m)
  11372. "Compute H:MM from a number of minutes."
  11373. (let ((h (/ m 60)))
  11374. (setq m (- m (* 60 h)))
  11375. (format org-time-clocksum-format h m)))
  11376. (defun org-hh:mm-string-to-minutes (s)
  11377. "Convert a string H:MM to a number of minutes."
  11378. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  11379. (+ (* (string-to-number (match-string 1 s)) 60)
  11380. (string-to-number (match-string 2 s)))
  11381. 0))
  11382. ;;;; Files
  11383. (defun org-save-all-org-buffers ()
  11384. "Save all Org-mode buffers without user confirmation."
  11385. (interactive)
  11386. (message "Saving all Org-mode buffers...")
  11387. (save-some-buffers t 'org-mode-p)
  11388. (when (featurep 'org-id) (org-id-locations-save))
  11389. (message "Saving all Org-mode buffers... done"))
  11390. (defun org-revert-all-org-buffers ()
  11391. "Revert all Org-mode buffers.
  11392. Prompt for confirmation when there are unsaved changes.
  11393. Be sure you know what you are doing before letting this function
  11394. overwrite your changes.
  11395. This function is useful in a setup where one tracks org files
  11396. with a version control system, to revert on one machine after pulling
  11397. changes from another. I believe the procedure must be like this:
  11398. 1. M-x org-save-all-org-buffers
  11399. 2. Pull changes from the other machine, resolve conflicts
  11400. 3. M-x org-revert-all-org-buffers"
  11401. (interactive)
  11402. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  11403. (error "Abort"))
  11404. (save-excursion
  11405. (save-window-excursion
  11406. (mapc
  11407. (lambda (b)
  11408. (when (and (with-current-buffer b (org-mode-p))
  11409. (with-current-buffer b buffer-file-name))
  11410. (switch-to-buffer b)
  11411. (revert-buffer t 'no-confirm)))
  11412. (buffer-list))
  11413. (when (and (featurep 'org-id) org-id-track-globally)
  11414. (org-id-locations-load)))))
  11415. ;;;; Agenda files
  11416. ;;;###autoload
  11417. (defun org-iswitchb (&optional arg)
  11418. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  11419. With a prefix argument, restrict available to files.
  11420. With two prefix arguments, restrict available buffers to agenda files.
  11421. Due to some yet unresolved reason, the global function
  11422. `iswitchb-mode' needs to be active for this function to work."
  11423. (interactive "P")
  11424. (require 'iswitchb)
  11425. (let ((enabled iswitchb-mode) blist)
  11426. (or enabled (iswitchb-mode 1))
  11427. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11428. ((equal arg '(16)) (org-buffer-list 'agenda))
  11429. (t (org-buffer-list))))
  11430. (unwind-protect
  11431. (let ((iswitchb-make-buflist-hook
  11432. (lambda ()
  11433. (setq iswitchb-temp-buflist
  11434. (mapcar 'buffer-name blist)))))
  11435. (switch-to-buffer
  11436. (iswitchb-read-buffer
  11437. "Switch-to: " nil t))
  11438. (or enabled (iswitchb-mode -1))))))
  11439. ;;;###autoload
  11440. (defun org-ido-switchb (&optional arg)
  11441. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  11442. With a prefix argument, restrict available to files.
  11443. With two prefix arguments, restrict available buffers to agenda files."
  11444. (interactive "P")
  11445. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  11446. ((equal arg '(16)) (org-buffer-list 'agenda))
  11447. (t (org-buffer-list)))))
  11448. (switch-to-buffer
  11449. (org-ido-completing-read "Org buffer: "
  11450. (mapcar 'buffer-name blist)
  11451. nil t))))
  11452. (defun org-buffer-list (&optional predicate exclude-tmp)
  11453. "Return a list of Org buffers.
  11454. PREDICATE can be `export', `files' or `agenda'.
  11455. export restrict the list to Export buffers.
  11456. files restrict the list to buffers visiting Org files.
  11457. agenda restrict the list to buffers visiting agenda files.
  11458. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  11459. (let* ((bfn nil)
  11460. (agenda-files (and (eq predicate 'agenda)
  11461. (mapcar 'file-truename (org-agenda-files t))))
  11462. (filter
  11463. (cond
  11464. ((eq predicate 'files)
  11465. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  11466. ((eq predicate 'export)
  11467. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  11468. ((eq predicate 'agenda)
  11469. (lambda (b)
  11470. (with-current-buffer b
  11471. (and (eq major-mode 'org-mode)
  11472. (setq bfn (buffer-file-name b))
  11473. (member (file-truename bfn) agenda-files)))))
  11474. (t (lambda (b) (with-current-buffer b
  11475. (or (eq major-mode 'org-mode)
  11476. (string-match "\*Org .*Export"
  11477. (buffer-name b)))))))))
  11478. (delq nil
  11479. (mapcar
  11480. (lambda(b)
  11481. (if (and (funcall filter b)
  11482. (or (not exclude-tmp)
  11483. (not (string-match "tmp" (buffer-name b)))))
  11484. b
  11485. nil))
  11486. (buffer-list)))))
  11487. (defun org-agenda-files (&optional unrestricted archives)
  11488. "Get the list of agenda files.
  11489. Optional UNRESTRICTED means return the full list even if a restriction
  11490. is currently in place.
  11491. When ARCHIVES is t, include all archive files hat are really being
  11492. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  11493. `org-agenda-archives-mode' is t."
  11494. (let ((files
  11495. (cond
  11496. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  11497. ((stringp org-agenda-files) (org-read-agenda-file-list))
  11498. ((listp org-agenda-files) org-agenda-files)
  11499. (t (error "Invalid value of `org-agenda-files'")))))
  11500. (setq files (apply 'append
  11501. (mapcar (lambda (f)
  11502. (if (file-directory-p f)
  11503. (directory-files
  11504. f t org-agenda-file-regexp)
  11505. (list f)))
  11506. files)))
  11507. (when org-agenda-skip-unavailable-files
  11508. (setq files (delq nil
  11509. (mapcar (function
  11510. (lambda (file)
  11511. (and (file-readable-p file) file)))
  11512. files))))
  11513. (when (or (eq archives t)
  11514. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11515. (setq files (org-add-archive-files files)))
  11516. files))
  11517. (defun org-edit-agenda-file-list ()
  11518. "Edit the list of agenda files.
  11519. Depending on setup, this either uses customize to edit the variable
  11520. `org-agenda-files', or it visits the file that is holding the list. In the
  11521. latter case, the buffer is set up in a way that saving it automatically kills
  11522. the buffer and restores the previous window configuration."
  11523. (interactive)
  11524. (if (stringp org-agenda-files)
  11525. (let ((cw (current-window-configuration)))
  11526. (find-file org-agenda-files)
  11527. (org-set-local 'org-window-configuration cw)
  11528. (org-add-hook 'after-save-hook
  11529. (lambda ()
  11530. (set-window-configuration
  11531. (prog1 org-window-configuration
  11532. (kill-buffer (current-buffer))))
  11533. (org-install-agenda-files-menu)
  11534. (message "New agenda file list installed"))
  11535. nil 'local)
  11536. (message "%s" (substitute-command-keys
  11537. "Edit list and finish with \\[save-buffer]")))
  11538. (customize-variable 'org-agenda-files)))
  11539. (defun org-store-new-agenda-file-list (list)
  11540. "Set new value for the agenda file list and save it correctly."
  11541. (if (stringp org-agenda-files)
  11542. (let ((f org-agenda-files) b)
  11543. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11544. (with-temp-file f
  11545. (insert (mapconcat 'identity list "\n") "\n")))
  11546. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11547. (setq org-agenda-files list)
  11548. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11549. (defun org-read-agenda-file-list ()
  11550. "Read the list of agenda files from a file."
  11551. (when (file-directory-p org-agenda-files)
  11552. (error "`org-agenda-files' cannot be a single directory"))
  11553. (when (stringp org-agenda-files)
  11554. (with-temp-buffer
  11555. (insert-file-contents org-agenda-files)
  11556. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11557. ;;;###autoload
  11558. (defun org-cycle-agenda-files ()
  11559. "Cycle through the files in `org-agenda-files'.
  11560. If the current buffer visits an agenda file, find the next one in the list.
  11561. If the current buffer does not, find the first agenda file."
  11562. (interactive)
  11563. (let* ((fs (org-agenda-files t))
  11564. (files (append fs (list (car fs))))
  11565. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11566. file)
  11567. (unless files (error "No agenda files"))
  11568. (catch 'exit
  11569. (while (setq file (pop files))
  11570. (if (equal (file-truename file) tcf)
  11571. (when (car files)
  11572. (find-file (car files))
  11573. (throw 'exit t))))
  11574. (find-file (car fs)))
  11575. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11576. (defun org-agenda-file-to-front (&optional to-end)
  11577. "Move/add the current file to the top of the agenda file list.
  11578. If the file is not present in the list, it is added to the front. If it is
  11579. present, it is moved there. With optional argument TO-END, add/move to the
  11580. end of the list."
  11581. (interactive "P")
  11582. (let ((org-agenda-skip-unavailable-files nil)
  11583. (file-alist (mapcar (lambda (x)
  11584. (cons (file-truename x) x))
  11585. (org-agenda-files t)))
  11586. (ctf (file-truename buffer-file-name))
  11587. x had)
  11588. (setq x (assoc ctf file-alist) had x)
  11589. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11590. (if to-end
  11591. (setq file-alist (append (delq x file-alist) (list x)))
  11592. (setq file-alist (cons x (delq x file-alist))))
  11593. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11594. (org-install-agenda-files-menu)
  11595. (message "File %s to %s of agenda file list"
  11596. (if had "moved" "added") (if to-end "end" "front"))))
  11597. (defun org-remove-file (&optional file)
  11598. "Remove current file from the list of files in variable `org-agenda-files'.
  11599. These are the files which are being checked for agenda entries.
  11600. Optional argument FILE means, use this file instead of the current."
  11601. (interactive)
  11602. (let* ((org-agenda-skip-unavailable-files nil)
  11603. (file (or file buffer-file-name))
  11604. (true-file (file-truename file))
  11605. (afile (abbreviate-file-name file))
  11606. (files (delq nil (mapcar
  11607. (lambda (x)
  11608. (if (equal true-file
  11609. (file-truename x))
  11610. nil x))
  11611. (org-agenda-files t)))))
  11612. (if (not (= (length files) (length (org-agenda-files t))))
  11613. (progn
  11614. (org-store-new-agenda-file-list files)
  11615. (org-install-agenda-files-menu)
  11616. (message "Removed file: %s" afile))
  11617. (message "File was not in list: %s (not removed)" afile))))
  11618. (defun org-file-menu-entry (file)
  11619. (vector file (list 'find-file file) t))
  11620. (defun org-check-agenda-file (file)
  11621. "Make sure FILE exists. If not, ask user what to do."
  11622. (when (not (file-exists-p file))
  11623. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11624. (abbreviate-file-name file))
  11625. (let ((r (downcase (read-char-exclusive))))
  11626. (cond
  11627. ((equal r ?r)
  11628. (org-remove-file file)
  11629. (throw 'nextfile t))
  11630. (t (error "Abort"))))))
  11631. (defun org-get-agenda-file-buffer (file)
  11632. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11633. it to the list of buffers which might be released later."
  11634. (let ((buf (org-find-base-buffer-visiting file)))
  11635. (if buf
  11636. buf ; just return it
  11637. ;; Make a new buffer and remember it
  11638. (setq buf (find-file-noselect file))
  11639. (if buf (push buf org-agenda-new-buffers))
  11640. buf)))
  11641. (defun org-release-buffers (blist)
  11642. "Release all buffers in list, asking the user for confirmation when needed.
  11643. When a buffer is unmodified, it is just killed. When modified, it is saved
  11644. \(if the user agrees) and then killed."
  11645. (let (buf file)
  11646. (while (setq buf (pop blist))
  11647. (setq file (buffer-file-name buf))
  11648. (when (and (buffer-modified-p buf)
  11649. file
  11650. (y-or-n-p (format "Save file %s? " file)))
  11651. (with-current-buffer buf (save-buffer)))
  11652. (kill-buffer buf))))
  11653. (defun org-prepare-agenda-buffers (files)
  11654. "Create buffers for all agenda files, protect archived trees and comments."
  11655. (interactive)
  11656. (let ((pa '(:org-archived t))
  11657. (pc '(:org-comment t))
  11658. (pall '(:org-archived t :org-comment t))
  11659. (inhibit-read-only t)
  11660. (rea (concat ":" org-archive-tag ":"))
  11661. bmp file re)
  11662. (save-excursion
  11663. (save-restriction
  11664. (while (setq file (pop files))
  11665. (if (bufferp file)
  11666. (set-buffer file)
  11667. (org-check-agenda-file file)
  11668. (set-buffer (org-get-agenda-file-buffer file)))
  11669. (widen)
  11670. (setq bmp (buffer-modified-p))
  11671. (org-refresh-category-properties)
  11672. (setq org-todo-keywords-for-agenda
  11673. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11674. (setq org-done-keywords-for-agenda
  11675. (append org-done-keywords-for-agenda org-done-keywords))
  11676. (setq org-todo-keyword-alist-for-agenda
  11677. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11678. (setq org-tag-alist-for-agenda
  11679. (append org-tag-alist-for-agenda org-tag-alist))
  11680. (save-excursion
  11681. (remove-text-properties (point-min) (point-max) pall)
  11682. (when org-agenda-skip-archived-trees
  11683. (goto-char (point-min))
  11684. (while (re-search-forward rea nil t)
  11685. (if (org-on-heading-p t)
  11686. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11687. (goto-char (point-min))
  11688. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11689. (while (re-search-forward re nil t)
  11690. (add-text-properties
  11691. (match-beginning 0) (org-end-of-subtree t) pc)))
  11692. (set-buffer-modified-p bmp))))
  11693. (setq org-todo-keyword-alist-for-agenda
  11694. (org-uniquify org-todo-keyword-alist-for-agenda)
  11695. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11696. ;;;; Embedded LaTeX
  11697. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11698. "Keymap for the minor `org-cdlatex-mode'.")
  11699. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11700. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11701. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11702. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11703. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11704. (defvar org-cdlatex-texmathp-advice-is-done nil
  11705. "Flag remembering if we have applied the advice to texmathp already.")
  11706. (define-minor-mode org-cdlatex-mode
  11707. "Toggle the minor `org-cdlatex-mode'.
  11708. This mode supports entering LaTeX environment and math in LaTeX fragments
  11709. in Org-mode.
  11710. \\{org-cdlatex-mode-map}"
  11711. nil " OCDL" nil
  11712. (when org-cdlatex-mode (require 'cdlatex))
  11713. (unless org-cdlatex-texmathp-advice-is-done
  11714. (setq org-cdlatex-texmathp-advice-is-done t)
  11715. (defadvice texmathp (around org-math-always-on activate)
  11716. "Always return t in org-mode buffers.
  11717. This is because we want to insert math symbols without dollars even outside
  11718. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11719. an embedded LaTeX fragment, let texmathp do its job.
  11720. \\[org-cdlatex-mode-map]"
  11721. (interactive)
  11722. (let (p)
  11723. (cond
  11724. ((not (org-mode-p)) ad-do-it)
  11725. ((eq this-command 'cdlatex-math-symbol)
  11726. (setq ad-return-value t
  11727. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11728. (t
  11729. (let ((p (org-inside-LaTeX-fragment-p)))
  11730. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11731. (setq ad-return-value t
  11732. texmathp-why '("Org-mode embedded math" . 0))
  11733. (if p ad-do-it)))))))))
  11734. (defun turn-on-org-cdlatex ()
  11735. "Unconditionally turn on `org-cdlatex-mode'."
  11736. (org-cdlatex-mode 1))
  11737. (defun org-inside-LaTeX-fragment-p ()
  11738. "Test if point is inside a LaTeX fragment.
  11739. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11740. sequence appearing also before point.
  11741. Even though the matchers for math are configurable, this function assumes
  11742. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11743. delimiters are skipped when they have been removed by customization.
  11744. The return value is nil, or a cons cell with the delimiter and
  11745. and the position of this delimiter.
  11746. This function does a reasonably good job, but can locally be fooled by
  11747. for example currency specifications. For example it will assume being in
  11748. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11749. fragments that are properly closed, but during editing, we have to live
  11750. with the uncertainty caused by missing closing delimiters. This function
  11751. looks only before point, not after."
  11752. (catch 'exit
  11753. (let ((pos (point))
  11754. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11755. (lim (progn
  11756. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11757. (point)))
  11758. dd-on str (start 0) m re)
  11759. (goto-char pos)
  11760. (when dodollar
  11761. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11762. re (nth 1 (assoc "$" org-latex-regexps)))
  11763. (while (string-match re str start)
  11764. (cond
  11765. ((= (match-end 0) (length str))
  11766. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11767. ((= (match-end 0) (- (length str) 5))
  11768. (throw 'exit nil))
  11769. (t (setq start (match-end 0))))))
  11770. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11771. (goto-char pos)
  11772. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11773. (and (match-beginning 2) (throw 'exit nil))
  11774. ;; count $$
  11775. (while (re-search-backward "\\$\\$" lim t)
  11776. (setq dd-on (not dd-on)))
  11777. (goto-char pos)
  11778. (if dd-on (cons "$$" m))))))
  11779. (defun org-try-cdlatex-tab ()
  11780. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11781. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11782. - inside a LaTeX fragment, or
  11783. - after the first word in a line, where an abbreviation expansion could
  11784. insert a LaTeX environment."
  11785. (when org-cdlatex-mode
  11786. (cond
  11787. ((save-excursion
  11788. (skip-chars-backward "a-zA-Z0-9*")
  11789. (skip-chars-backward " \t")
  11790. (bolp))
  11791. (cdlatex-tab) t)
  11792. ((org-inside-LaTeX-fragment-p)
  11793. (cdlatex-tab) t)
  11794. (t nil))))
  11795. (defun org-cdlatex-underscore-caret (&optional arg)
  11796. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11797. Revert to the normal definition outside of these fragments."
  11798. (interactive "P")
  11799. (if (org-inside-LaTeX-fragment-p)
  11800. (call-interactively 'cdlatex-sub-superscript)
  11801. (let (org-cdlatex-mode)
  11802. (call-interactively (key-binding (vector last-input-event))))))
  11803. (defun org-cdlatex-math-modify (&optional arg)
  11804. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11805. Revert to the normal definition outside of these fragments."
  11806. (interactive "P")
  11807. (if (org-inside-LaTeX-fragment-p)
  11808. (call-interactively 'cdlatex-math-modify)
  11809. (let (org-cdlatex-mode)
  11810. (call-interactively (key-binding (vector last-input-event))))))
  11811. (defvar org-latex-fragment-image-overlays nil
  11812. "List of overlays carrying the images of latex fragments.")
  11813. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11814. (defun org-remove-latex-fragment-image-overlays ()
  11815. "Remove all overlays with LaTeX fragment images in current buffer."
  11816. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11817. (setq org-latex-fragment-image-overlays nil))
  11818. (defun org-preview-latex-fragment (&optional subtree)
  11819. "Preview the LaTeX fragment at point, or all locally or globally.
  11820. If the cursor is in a LaTeX fragment, create the image and overlay
  11821. it over the source code. If there is no fragment at point, display
  11822. all fragments in the current text, from one headline to the next. With
  11823. prefix SUBTREE, display all fragments in the current subtree. With a
  11824. double prefix `C-u C-u', or when the cursor is before the first headline,
  11825. display all fragments in the buffer.
  11826. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11827. (interactive "P")
  11828. (org-remove-latex-fragment-image-overlays)
  11829. (save-excursion
  11830. (save-restriction
  11831. (let (beg end at msg)
  11832. (cond
  11833. ((or (equal subtree '(16))
  11834. (not (save-excursion
  11835. (re-search-backward (concat "^" outline-regexp) nil t))))
  11836. (setq beg (point-min) end (point-max)
  11837. msg "Creating images for buffer...%s"))
  11838. ((equal subtree '(4))
  11839. (org-back-to-heading)
  11840. (setq beg (point) end (org-end-of-subtree t)
  11841. msg "Creating images for subtree...%s"))
  11842. (t
  11843. (if (setq at (org-inside-LaTeX-fragment-p))
  11844. (goto-char (max (point-min) (- (cdr at) 2)))
  11845. (org-back-to-heading))
  11846. (setq beg (point) end (progn (outline-next-heading) (point))
  11847. msg (if at "Creating image...%s"
  11848. "Creating images for entry...%s"))))
  11849. (message msg "")
  11850. (narrow-to-region beg end)
  11851. (goto-char beg)
  11852. (org-format-latex
  11853. (concat "ltxpng/" (file-name-sans-extension
  11854. (file-name-nondirectory
  11855. buffer-file-name)))
  11856. default-directory 'overlays msg at 'forbuffer)
  11857. (message msg "done. Use `C-c C-c' to remove images.")))))
  11858. (defvar org-latex-regexps
  11859. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11860. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11861. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11862. ("$1" "\\([^$]\\)\\(\\$[^ \r\n,;.$]\\$\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11863. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([- .,?;:'\")\000]\\|$\\)" 2 nil)
  11864. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11865. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11866. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11867. "Regular expressions for matching embedded LaTeX.")
  11868. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11869. "Replace LaTeX fragments with links to an image, and produce images."
  11870. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11871. (let* ((prefixnodir (file-name-nondirectory prefix))
  11872. (absprefix (expand-file-name prefix dir))
  11873. (todir (file-name-directory absprefix))
  11874. (opt org-format-latex-options)
  11875. (matchers (plist-get opt :matchers))
  11876. (re-list org-latex-regexps)
  11877. (cnt 0) txt link beg end re e checkdir
  11878. m n block linkfile movefile ov)
  11879. ;; Check if there are old images files with this prefix, and remove them
  11880. (when (file-directory-p todir)
  11881. (mapc 'delete-file
  11882. (directory-files
  11883. todir 'full
  11884. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11885. ;; Check the different regular expressions
  11886. (while (setq e (pop re-list))
  11887. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11888. block (if (nth 3 e) "\n\n" ""))
  11889. (when (member m matchers)
  11890. (goto-char (point-min))
  11891. (while (re-search-forward re nil t)
  11892. (when (and (or (not at) (equal (cdr at) (match-beginning n)))
  11893. (not (get-text-property (match-beginning n)
  11894. 'org-protected)))
  11895. (setq txt (match-string n)
  11896. beg (match-beginning n) end (match-end n)
  11897. cnt (1+ cnt)
  11898. linkfile (format "%s_%04d.png" prefix cnt)
  11899. movefile (format "%s_%04d.png" absprefix cnt)
  11900. link (concat block "[[file:" linkfile "]]" block))
  11901. (if msg (message msg cnt))
  11902. (goto-char beg)
  11903. (unless checkdir ; make sure the directory exists
  11904. (setq checkdir t)
  11905. (or (file-directory-p todir) (make-directory todir)))
  11906. (org-create-formula-image
  11907. txt movefile opt forbuffer)
  11908. (if overlays
  11909. (progn
  11910. (setq ov (org-make-overlay beg end))
  11911. (if (featurep 'xemacs)
  11912. (progn
  11913. (org-overlay-put ov 'invisible t)
  11914. (org-overlay-put
  11915. ov 'end-glyph
  11916. (make-glyph (vector 'png :file movefile))))
  11917. (org-overlay-put
  11918. ov 'display
  11919. (list 'image :type 'png :file movefile :ascent 'center)))
  11920. (push ov org-latex-fragment-image-overlays)
  11921. (goto-char end))
  11922. (delete-region beg end)
  11923. (insert link))))))))
  11924. ;; This function borrows from Ganesh Swami's latex2png.el
  11925. (defun org-create-formula-image (string tofile options buffer)
  11926. (let* ((tmpdir (if (featurep 'xemacs)
  11927. (temp-directory)
  11928. temporary-file-directory))
  11929. (texfilebase (make-temp-name
  11930. (expand-file-name "orgtex" tmpdir)))
  11931. (texfile (concat texfilebase ".tex"))
  11932. (dvifile (concat texfilebase ".dvi"))
  11933. (pngfile (concat texfilebase ".png"))
  11934. (fnh (if (featurep 'xemacs)
  11935. (font-height (get-face-font 'default))
  11936. (face-attribute 'default :height nil)))
  11937. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11938. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11939. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11940. "Black"))
  11941. (bg (or (plist-get options (if buffer :background :html-background))
  11942. "Transparent")))
  11943. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11944. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11945. (with-temp-file texfile
  11946. (insert org-format-latex-header
  11947. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11948. (let ((dir default-directory))
  11949. (condition-case nil
  11950. (progn
  11951. (cd tmpdir)
  11952. (call-process "latex" nil nil nil texfile))
  11953. (error nil))
  11954. (cd dir))
  11955. (if (not (file-exists-p dvifile))
  11956. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11957. (condition-case nil
  11958. (call-process "dvipng" nil nil nil
  11959. "-E" "-fg" fg "-bg" bg
  11960. "-D" dpi
  11961. ;;"-x" scale "-y" scale
  11962. "-T" "tight"
  11963. "-o" pngfile
  11964. dvifile)
  11965. (error nil))
  11966. (if (not (file-exists-p pngfile))
  11967. (progn (message "Failed to create png file from %s" texfile) nil)
  11968. ;; Use the requested file name and clean up
  11969. (copy-file pngfile tofile 'replace)
  11970. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11971. (delete-file (concat texfilebase e)))
  11972. pngfile))))
  11973. (defun org-dvipng-color (attr)
  11974. "Return an rgb color specification for dvipng."
  11975. (apply 'format "rgb %s %s %s"
  11976. (mapcar 'org-normalize-color
  11977. (color-values (face-attribute 'default attr nil)))))
  11978. (defun org-normalize-color (value)
  11979. "Return string to be used as color value for an RGB component."
  11980. (format "%g" (/ value 65535.0)))
  11981. ;;;; Key bindings
  11982. ;; Make `C-c C-x' a prefix key
  11983. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11984. ;; TAB key with modifiers
  11985. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11986. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11987. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11988. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11989. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11990. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11991. ;; The following line is necessary under Suse GNU/Linux
  11992. (unless (featurep 'xemacs)
  11993. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11994. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11995. (define-key org-mode-map [backtab] 'org-shifttab)
  11996. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11997. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11998. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11999. ;; Cursor keys with modifiers
  12000. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  12001. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  12002. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  12003. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  12004. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12005. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12006. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12007. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12008. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  12009. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  12010. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  12011. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  12012. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  12013. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  12014. ;;; Extra keys for tty access.
  12015. ;; We only set them when really needed because otherwise the
  12016. ;; menus don't show the simple keys
  12017. (when (or org-use-extra-keys
  12018. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  12019. (not window-system))
  12020. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  12021. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  12022. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  12023. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  12024. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  12025. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  12026. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  12027. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  12028. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  12029. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  12030. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  12031. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  12032. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  12033. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  12034. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  12035. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  12036. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  12037. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  12038. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  12039. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  12040. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  12041. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  12042. ;; All the other keys
  12043. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12044. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  12045. (if (boundp 'narrow-map)
  12046. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  12047. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  12048. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  12049. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  12050. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  12051. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  12052. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  12053. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  12054. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  12055. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  12056. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  12057. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  12058. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  12059. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  12060. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12061. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  12062. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  12063. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12064. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  12065. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  12066. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  12067. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  12068. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  12069. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  12070. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  12071. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  12072. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  12073. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  12074. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  12075. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12076. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12077. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12078. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12079. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  12080. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  12081. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12082. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  12083. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12084. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  12085. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  12086. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12087. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  12088. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  12089. (org-defkey org-mode-map "\C-c^" 'org-sort)
  12090. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12091. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  12092. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  12093. (org-defkey org-mode-map "\C-m" 'org-return)
  12094. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  12095. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  12096. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  12097. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  12098. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  12099. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  12100. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  12101. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12102. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12103. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12104. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  12105. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  12106. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  12107. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  12108. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12109. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  12110. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  12111. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  12112. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12113. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12114. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12115. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  12116. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  12117. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  12118. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  12119. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  12120. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  12121. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  12122. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  12123. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  12124. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  12125. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  12126. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  12127. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  12128. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  12129. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  12130. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  12131. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  12132. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  12133. (when (featurep 'xemacs)
  12134. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  12135. (defvar org-table-auto-blank-field) ; defined in org-table.el
  12136. (defun org-self-insert-command (N)
  12137. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12138. If the cursor is in a table looking at whitespace, the whitespace is
  12139. overwritten, and the table is not marked as requiring realignment."
  12140. (interactive "p")
  12141. (if (and (org-table-p)
  12142. (progn
  12143. ;; check if we blank the field, and if that triggers align
  12144. (and (featurep 'org-table) org-table-auto-blank-field
  12145. (member last-command
  12146. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12147. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12148. ;; got extra space, this field does not determine column width
  12149. (let (org-table-may-need-update) (org-table-blank-field))
  12150. ;; no extra space, this field may determine column width
  12151. (org-table-blank-field)))
  12152. t)
  12153. (eq N 1)
  12154. (looking-at "[^|\n]* |"))
  12155. (let (org-table-may-need-update)
  12156. (goto-char (1- (match-end 0)))
  12157. (delete-backward-char 1)
  12158. (goto-char (match-beginning 0))
  12159. (self-insert-command N))
  12160. (setq org-table-may-need-update t)
  12161. (self-insert-command N)
  12162. (org-fix-tags-on-the-fly)))
  12163. (defun org-fix-tags-on-the-fly ()
  12164. (when (and (equal (char-after (point-at-bol)) ?*)
  12165. (org-on-heading-p))
  12166. (org-align-tags-here org-tags-column)))
  12167. (defun org-delete-backward-char (N)
  12168. "Like `delete-backward-char', insert whitespace at field end in tables.
  12169. When deleting backwards, in tables this function will insert whitespace in
  12170. front of the next \"|\" separator, to keep the table aligned. The table will
  12171. still be marked for re-alignment if the field did fill the entire column,
  12172. because, in this case the deletion might narrow the column."
  12173. (interactive "p")
  12174. (if (and (org-table-p)
  12175. (eq N 1)
  12176. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12177. (looking-at ".*?|"))
  12178. (let ((pos (point))
  12179. (noalign (looking-at "[^|\n\r]* |"))
  12180. (c org-table-may-need-update))
  12181. (backward-delete-char N)
  12182. (skip-chars-forward "^|")
  12183. (insert " ")
  12184. (goto-char (1- pos))
  12185. ;; noalign: if there were two spaces at the end, this field
  12186. ;; does not determine the width of the column.
  12187. (if noalign (setq org-table-may-need-update c)))
  12188. (backward-delete-char N)
  12189. (org-fix-tags-on-the-fly)))
  12190. (defun org-delete-char (N)
  12191. "Like `delete-char', but insert whitespace at field end in tables.
  12192. When deleting characters, in tables this function will insert whitespace in
  12193. front of the next \"|\" separator, to keep the table aligned. The table will
  12194. still be marked for re-alignment if the field did fill the entire column,
  12195. because, in this case the deletion might narrow the column."
  12196. (interactive "p")
  12197. (if (and (org-table-p)
  12198. (not (bolp))
  12199. (not (= (char-after) ?|))
  12200. (eq N 1))
  12201. (if (looking-at ".*?|")
  12202. (let ((pos (point))
  12203. (noalign (looking-at "[^|\n\r]* |"))
  12204. (c org-table-may-need-update))
  12205. (replace-match (concat
  12206. (substring (match-string 0) 1 -1)
  12207. " |"))
  12208. (goto-char pos)
  12209. ;; noalign: if there were two spaces at the end, this field
  12210. ;; does not determine the width of the column.
  12211. (if noalign (setq org-table-may-need-update c)))
  12212. (delete-char N))
  12213. (delete-char N)
  12214. (org-fix-tags-on-the-fly)))
  12215. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  12216. (put 'org-self-insert-command 'delete-selection t)
  12217. (put 'orgtbl-self-insert-command 'delete-selection t)
  12218. (put 'org-delete-char 'delete-selection 'supersede)
  12219. (put 'org-delete-backward-char 'delete-selection 'supersede)
  12220. (put 'org-yank 'delete-selection 'yank)
  12221. ;; Make `flyspell-mode' delay after some commands
  12222. (put 'org-self-insert-command 'flyspell-delayed t)
  12223. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  12224. (put 'org-delete-char 'flyspell-delayed t)
  12225. (put 'org-delete-backward-char 'flyspell-delayed t)
  12226. ;; Make pabbrev-mode expand after org-mode commands
  12227. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  12228. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  12229. ;; How to do this: Measure non-white length of current string
  12230. ;; If equal to column width, we should realign.
  12231. (defun org-remap (map &rest commands)
  12232. "In MAP, remap the functions given in COMMANDS.
  12233. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12234. (let (new old)
  12235. (while commands
  12236. (setq old (pop commands) new (pop commands))
  12237. (if (fboundp 'command-remapping)
  12238. (org-defkey map (vector 'remap old) new)
  12239. (substitute-key-definition old new map global-map)))))
  12240. (when (eq org-enable-table-editor 'optimized)
  12241. ;; If the user wants maximum table support, we need to hijack
  12242. ;; some standard editing functions
  12243. (org-remap org-mode-map
  12244. 'self-insert-command 'org-self-insert-command
  12245. 'delete-char 'org-delete-char
  12246. 'delete-backward-char 'org-delete-backward-char)
  12247. (org-defkey org-mode-map "|" 'org-force-self-insert))
  12248. (defvar org-ctrl-c-ctrl-c-hook nil
  12249. "Hook for functions attaching themselves to `C-c C-c'.
  12250. This can be used to add additional functionality to the C-c C-c key which
  12251. executes context-dependent commands.
  12252. Each function will be called with no arguments. The function must check
  12253. if the context is appropriate for it to act. If yes, it should do its
  12254. thing and then return a non-nil value. If the context is wrong,
  12255. just do nothing.")
  12256. (defvar org-metaleft-hook nil
  12257. "Hook for functions attaching themselves to `M-left'.
  12258. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12259. (defvar org-metaright-hook nil
  12260. "Hook for functions attaching themselves to `M-right'.
  12261. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12262. (defvar org-metaup-hook nil
  12263. "Hook for functions attaching themselves to `M-up'.
  12264. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12265. (defvar org-metadown-hook nil
  12266. "Hook for functions attaching themselves to `M-down'.
  12267. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12268. (defvar org-shiftmetaleft-hook nil
  12269. "Hook for functions attaching themselves to `M-S-left'.
  12270. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12271. (defvar org-shiftmetaright-hook nil
  12272. "Hook for functions attaching themselves to `M-S-right'.
  12273. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12274. (defvar org-shiftmetaup-hook nil
  12275. "Hook for functions attaching themselves to `M-S-up'.
  12276. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12277. (defvar org-shiftmetadown-hook nil
  12278. "Hook for functions attaching themselves to `M-S-down'.
  12279. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12280. (defvar org-metareturn-hook nil
  12281. "Hook for functions attaching themselves to `M-RET'.
  12282. See `org-ctrl-c-ctrl-c-hook' for more information.")
  12283. (defun org-modifier-cursor-error ()
  12284. "Throw an error, a modified cursor command was applied in wrong context."
  12285. (error "This command is active in special context like tables, headlines or items"))
  12286. (defun org-shiftselect-error ()
  12287. "Throw an error because Shift-Cursor command was applied in wrong context."
  12288. (if (and (boundp 'shift-select-mode) shift-select-mode)
  12289. (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
  12290. (error "This command works only in special context like headlines or timestamps.")))
  12291. (defun org-call-for-shift-select (cmd)
  12292. (let ((this-command-keys-shift-translated t))
  12293. (call-interactively cmd)))
  12294. (defun org-shifttab (&optional arg)
  12295. "Global visibility cycling or move to previous table field.
  12296. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  12297. on context.
  12298. See the individual commands for more information."
  12299. (interactive "P")
  12300. (cond
  12301. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  12302. ((integerp arg)
  12303. (message "Content view to level: %d" arg)
  12304. (org-content (prefix-numeric-value arg))
  12305. (setq org-cycle-global-status 'overview))
  12306. (t (call-interactively 'org-global-cycle))))
  12307. (defun org-shiftmetaleft ()
  12308. "Promote subtree or delete table column.
  12309. Calls `org-promote-subtree', `org-outdent-item',
  12310. or `org-table-delete-column', depending on context.
  12311. See the individual commands for more information."
  12312. (interactive)
  12313. (cond
  12314. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  12315. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  12316. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  12317. ((org-at-item-p) (call-interactively 'org-outdent-item))
  12318. (t (org-modifier-cursor-error))))
  12319. (defun org-shiftmetaright ()
  12320. "Demote subtree or insert table column.
  12321. Calls `org-demote-subtree', `org-indent-item',
  12322. or `org-table-insert-column', depending on context.
  12323. See the individual commands for more information."
  12324. (interactive)
  12325. (cond
  12326. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  12327. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  12328. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  12329. ((org-at-item-p) (call-interactively 'org-indent-item))
  12330. (t (org-modifier-cursor-error))))
  12331. (defun org-shiftmetaup (&optional arg)
  12332. "Move subtree up or kill table row.
  12333. Calls `org-move-subtree-up' or `org-table-kill-row' or
  12334. `org-move-item-up' depending on context. See the individual commands
  12335. for more information."
  12336. (interactive "P")
  12337. (cond
  12338. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  12339. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  12340. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12341. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12342. (t (org-modifier-cursor-error))))
  12343. (defun org-shiftmetadown (&optional arg)
  12344. "Move subtree down or insert table row.
  12345. Calls `org-move-subtree-down' or `org-table-insert-row' or
  12346. `org-move-item-down', depending on context. See the individual
  12347. commands for more information."
  12348. (interactive "P")
  12349. (cond
  12350. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  12351. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  12352. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12353. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12354. (t (org-modifier-cursor-error))))
  12355. (defun org-metaleft (&optional arg)
  12356. "Promote heading or move table column to left.
  12357. Calls `org-do-promote' or `org-table-move-column', depending on context.
  12358. With no specific context, calls the Emacs default `backward-word'.
  12359. See the individual commands for more information."
  12360. (interactive "P")
  12361. (cond
  12362. ((run-hook-with-args-until-success 'org-metaleft-hook))
  12363. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  12364. ((or (org-on-heading-p)
  12365. (and (org-region-active-p)
  12366. (save-excursion
  12367. (goto-char (region-beginning))
  12368. (org-on-heading-p))))
  12369. (call-interactively 'org-do-promote))
  12370. ((or (org-at-item-p)
  12371. (and (org-region-active-p)
  12372. (save-excursion
  12373. (goto-char (region-beginning))
  12374. (org-at-item-p))))
  12375. (call-interactively 'org-outdent-item))
  12376. (t (call-interactively 'backward-word))))
  12377. (defun org-metaright (&optional arg)
  12378. "Demote subtree or move table column to right.
  12379. Calls `org-do-demote' or `org-table-move-column', depending on context.
  12380. With no specific context, calls the Emacs default `forward-word'.
  12381. See the individual commands for more information."
  12382. (interactive "P")
  12383. (cond
  12384. ((run-hook-with-args-until-success 'org-metaright-hook))
  12385. ((org-at-table-p) (call-interactively 'org-table-move-column))
  12386. ((or (org-on-heading-p)
  12387. (and (org-region-active-p)
  12388. (save-excursion
  12389. (goto-char (region-beginning))
  12390. (org-on-heading-p))))
  12391. (call-interactively 'org-do-demote))
  12392. ((or (org-at-item-p)
  12393. (and (org-region-active-p)
  12394. (save-excursion
  12395. (goto-char (region-beginning))
  12396. (org-at-item-p))))
  12397. (call-interactively 'org-indent-item))
  12398. (t (call-interactively 'forward-word))))
  12399. (defun org-metaup (&optional arg)
  12400. "Move subtree up or move table row up.
  12401. Calls `org-move-subtree-up' or `org-table-move-row' or
  12402. `org-move-item-up', depending on context. See the individual commands
  12403. for more information."
  12404. (interactive "P")
  12405. (cond
  12406. ((run-hook-with-args-until-success 'org-metaup-hook))
  12407. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  12408. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  12409. ((org-at-item-p) (call-interactively 'org-move-item-up))
  12410. (t (transpose-lines 1) (beginning-of-line -1))))
  12411. (defun org-metadown (&optional arg)
  12412. "Move subtree down or move table row down.
  12413. Calls `org-move-subtree-down' or `org-table-move-row' or
  12414. `org-move-item-down', depending on context. See the individual
  12415. commands for more information."
  12416. (interactive "P")
  12417. (cond
  12418. ((run-hook-with-args-until-success 'org-metadown-hook))
  12419. ((org-at-table-p) (call-interactively 'org-table-move-row))
  12420. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  12421. ((org-at-item-p) (call-interactively 'org-move-item-down))
  12422. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  12423. (defun org-shiftup (&optional arg)
  12424. "Increase item in timestamp or increase priority of current headline.
  12425. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  12426. depending on context. See the individual commands for more information."
  12427. (interactive "P")
  12428. (cond
  12429. ((and org-support-shift-select (org-region-active-p))
  12430. (org-call-for-shift-select 'previous-line))
  12431. ((org-at-timestamp-p t)
  12432. (call-interactively (if org-edit-timestamp-down-means-later
  12433. 'org-timestamp-down 'org-timestamp-up)))
  12434. ((and (not (eq org-support-shift-select 'always))
  12435. (org-on-heading-p))
  12436. (call-interactively 'org-priority-up))
  12437. ((and (not org-support-shift-select) (org-at-item-p))
  12438. (call-interactively 'org-previous-item))
  12439. ((org-clocktable-try-shift 'up arg))
  12440. (org-support-shift-select
  12441. (org-call-for-shift-select 'previous-line))
  12442. (t (org-shiftselect-error))))
  12443. (defun org-shiftdown (&optional arg)
  12444. "Decrease item in timestamp or decrease priority of current headline.
  12445. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  12446. depending on context. See the individual commands for more information."
  12447. (interactive "P")
  12448. (cond
  12449. ((and org-support-shift-select (org-region-active-p))
  12450. (org-call-for-shift-select 'next-line))
  12451. ((org-at-timestamp-p t)
  12452. (call-interactively (if org-edit-timestamp-down-means-later
  12453. 'org-timestamp-up 'org-timestamp-down)))
  12454. ((and (not (eq org-support-shift-select 'always))
  12455. (org-on-heading-p))
  12456. (call-interactively 'org-priority-down))
  12457. ((and (not org-support-shift-select) (org-at-item-p))
  12458. (call-interactively 'org-next-item))
  12459. ((org-clocktable-try-shift 'down arg))
  12460. (org-support-shift-select
  12461. (org-call-for-shift-select 'next-line))
  12462. (t (org-shiftselect-error))))
  12463. (defun org-shiftright (&optional arg)
  12464. "Cycle the thing at point or in the current line, depending on context.
  12465. Depending on context, this does one of the following:
  12466. - switch a timestamp at point one day into the future
  12467. - on a headline, switch to the next TODO keyword.
  12468. - on an item, switch entire list to the next bullet type
  12469. - on a property line, switch to the next allowed value
  12470. - on a clocktable definition line, move time block into the future"
  12471. (interactive "P")
  12472. (cond
  12473. ((and org-support-shift-select (org-region-active-p))
  12474. (org-call-for-shift-select 'forward-char))
  12475. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  12476. ((and (not (eq org-support-shift-select 'always))
  12477. (org-on-heading-p))
  12478. (org-call-with-arg 'org-todo 'right))
  12479. ((or (and org-support-shift-select
  12480. (not (eq org-support-shift-select 'always))
  12481. (org-at-item-bullet-p))
  12482. (and (not org-support-shift-select) (org-at-item-p)))
  12483. (org-call-with-arg 'org-cycle-list-bullet nil))
  12484. ((and (not (eq org-support-shift-select 'always))
  12485. (org-at-property-p))
  12486. (call-interactively 'org-property-next-allowed-value))
  12487. ((org-clocktable-try-shift 'right arg))
  12488. (org-support-shift-select
  12489. (org-call-for-shift-select 'forward-char))
  12490. (t (org-shiftselect-error))))
  12491. (defun org-shiftleft (&optional arg)
  12492. "Cycle the thing at point or in the current line, depending on context.
  12493. Depending on context, this does one of the following:
  12494. - switch a timestamp at point one day into the past
  12495. - on a headline, switch to the previous TODO keyword.
  12496. - on an item, switch entire list to the previous bullet type
  12497. - on a property line, switch to the previous allowed value
  12498. - on a clocktable definition line, move time block into the past"
  12499. (interactive "P")
  12500. (cond
  12501. ((and org-support-shift-select (org-region-active-p))
  12502. (org-call-for-shift-select 'backward-char))
  12503. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  12504. ((and (not (eq org-support-shift-select 'always))
  12505. (org-on-heading-p))
  12506. (org-call-with-arg 'org-todo 'left))
  12507. ((or (and org-support-shift-select
  12508. (not (eq org-support-shift-select 'always))
  12509. (org-at-item-bullet-p))
  12510. (and (not org-support-shift-select) (org-at-item-p)))
  12511. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  12512. ((and (not (eq org-support-shift-select 'always))
  12513. (org-at-property-p))
  12514. (call-interactively 'org-property-previous-allowed-value))
  12515. ((org-clocktable-try-shift 'left arg))
  12516. (org-support-shift-select
  12517. (org-call-for-shift-select 'backward-char))
  12518. (t (org-shiftselect-error))))
  12519. (defun org-shiftcontrolright ()
  12520. "Switch to next TODO set."
  12521. (interactive)
  12522. (cond
  12523. ((and org-support-shift-select (org-region-active-p))
  12524. (org-call-for-shift-select 'forward-word))
  12525. ((and (not (eq org-support-shift-select 'always))
  12526. (org-on-heading-p))
  12527. (org-call-with-arg 'org-todo 'nextset))
  12528. (org-support-shift-select
  12529. (org-call-for-shift-select 'forward-word))
  12530. (t (org-shiftselect-error))))
  12531. (defun org-shiftcontrolleft ()
  12532. "Switch to previous TODO set."
  12533. (interactive)
  12534. (cond
  12535. ((and org-support-shift-select (org-region-active-p))
  12536. (org-call-for-shift-select 'backward-word))
  12537. ((and (not (eq org-support-shift-select 'always))
  12538. (org-on-heading-p))
  12539. (org-call-with-arg 'org-todo 'previousset))
  12540. (org-support-shift-select
  12541. (org-call-for-shift-select 'backward-word))
  12542. (t (org-shiftselect-error))))
  12543. (defun org-ctrl-c-ret ()
  12544. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  12545. (interactive)
  12546. (cond
  12547. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  12548. (t (call-interactively 'org-insert-heading))))
  12549. (defun org-copy-special ()
  12550. "Copy region in table or copy current subtree.
  12551. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  12552. See the individual commands for more information."
  12553. (interactive)
  12554. (call-interactively
  12555. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  12556. (defun org-cut-special ()
  12557. "Cut region in table or cut current subtree.
  12558. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  12559. See the individual commands for more information."
  12560. (interactive)
  12561. (call-interactively
  12562. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  12563. (defun org-paste-special (arg)
  12564. "Paste rectangular region into table, or past subtree relative to level.
  12565. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  12566. See the individual commands for more information."
  12567. (interactive "P")
  12568. (if (org-at-table-p)
  12569. (org-table-paste-rectangle)
  12570. (org-paste-subtree arg)))
  12571. (defun org-edit-special ()
  12572. "Call a special editor for the stuff at point.
  12573. When at a table, call the formula editor with `org-table-edit-formulas'.
  12574. When at the first line of an src example, call `org-edit-src-code'.
  12575. When in an #+include line, visit the include file. Otherwise call
  12576. `ffap' to visit the file at point."
  12577. (interactive)
  12578. (cond
  12579. ((org-at-table-p)
  12580. (call-interactively 'org-table-edit-formulas))
  12581. ((save-excursion
  12582. (beginning-of-line 1)
  12583. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  12584. (find-file (org-trim (match-string 1))))
  12585. ((org-edit-src-code))
  12586. ((org-edit-fixed-width-region))
  12587. (t (call-interactively 'ffap))))
  12588. (defun org-ctrl-c-ctrl-c (&optional arg)
  12589. "Set tags in headline, or update according to changed information at point.
  12590. This command does many different things, depending on context:
  12591. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  12592. this is what we do.
  12593. - If the cursor is in a headline, prompt for tags and insert them
  12594. into the current line, aligned to `org-tags-column'. When called
  12595. with prefix arg, realign all tags in the current buffer.
  12596. - If the cursor is in one of the special #+KEYWORD lines, this
  12597. triggers scanning the buffer for these lines and updating the
  12598. information.
  12599. - If the cursor is inside a table, realign the table. This command
  12600. works even if the automatic table editor has been turned off.
  12601. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  12602. the entire table.
  12603. - If the cursor is at a footnote reference or definition, jump to
  12604. the corresponding definition or references, respectively.
  12605. - If the cursor is a the beginning of a dynamic block, update it.
  12606. - If the cursor is inside a table created by the table.el package,
  12607. activate that table.
  12608. - If the current buffer is a remember buffer, close note and file
  12609. it. A prefix argument of 1 files to the default location
  12610. without further interaction. A prefix argument of 2 files to
  12611. the currently clocking task.
  12612. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  12613. links in this buffer.
  12614. - If the cursor is on a numbered item in a plain list, renumber the
  12615. ordered list.
  12616. - If the cursor is on a checkbox, toggle it."
  12617. (interactive "P")
  12618. (let ((org-enable-table-editor t))
  12619. (cond
  12620. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  12621. org-occur-highlights
  12622. org-latex-fragment-image-overlays)
  12623. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  12624. (org-remove-occur-highlights)
  12625. (org-remove-latex-fragment-image-overlays)
  12626. (message "Temporary highlights/overlays removed from current buffer"))
  12627. ((and (local-variable-p 'org-finish-function (current-buffer))
  12628. (fboundp org-finish-function))
  12629. (funcall org-finish-function))
  12630. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  12631. ((org-at-property-p)
  12632. (call-interactively 'org-property-action))
  12633. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12634. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12635. ((org-at-table.el-p)
  12636. (require 'table)
  12637. (beginning-of-line 1)
  12638. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12639. (call-interactively 'table-recognize-table))
  12640. ((org-at-table-p)
  12641. (org-table-maybe-eval-formula)
  12642. (if arg
  12643. (call-interactively 'org-table-recalculate)
  12644. (org-table-maybe-recalculate-line))
  12645. (call-interactively 'org-table-align))
  12646. ((or (org-footnote-at-reference-p)
  12647. (org-footnote-at-definition-p))
  12648. (call-interactively 'org-footnote-action))
  12649. ((org-at-item-checkbox-p)
  12650. (call-interactively 'org-toggle-checkbox))
  12651. ((org-at-item-p)
  12652. (if arg
  12653. (call-interactively 'org-toggle-checkbox)
  12654. (call-interactively 'org-maybe-renumber-ordered-list)))
  12655. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12656. ;; Dynamic block
  12657. (beginning-of-line 1)
  12658. (save-excursion (org-update-dblock)))
  12659. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12660. (cond
  12661. ((equal (match-string 1) "TBLFM")
  12662. ;; Recalculate the table before this line
  12663. (save-excursion
  12664. (beginning-of-line 1)
  12665. (skip-chars-backward " \r\n\t")
  12666. (if (org-at-table-p)
  12667. (org-call-with-arg 'org-table-recalculate t))))
  12668. (t
  12669. ; (org-set-regexps-and-options)
  12670. ; (org-restart-font-lock)
  12671. (let ((org-inhibit-startup t)) (org-mode-restart))
  12672. (message "Local setup has been refreshed"))))
  12673. (t (error "C-c C-c can do nothing useful at this location.")))))
  12674. (defun org-mode-restart ()
  12675. "Restart Org-mode, to scan again for special lines.
  12676. Also updates the keyword regular expressions."
  12677. (interactive)
  12678. (org-mode)
  12679. (message "Org-mode restarted"))
  12680. (defun org-kill-note-or-show-branches ()
  12681. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12682. (interactive)
  12683. (if (not org-finish-function)
  12684. (call-interactively 'show-branches)
  12685. (let ((org-note-abort t))
  12686. (funcall org-finish-function))))
  12687. (defun org-return (&optional indent)
  12688. "Goto next table row or insert a newline.
  12689. Calls `org-table-next-row' or `newline', depending on context.
  12690. See the individual commands for more information."
  12691. (interactive)
  12692. (cond
  12693. ((bobp) (if indent (newline-and-indent) (newline)))
  12694. ((org-at-table-p)
  12695. (org-table-justify-field-maybe)
  12696. (call-interactively 'org-table-next-row))
  12697. ((and org-return-follows-link
  12698. (eq (get-text-property (point) 'face) 'org-link))
  12699. (call-interactively 'org-open-at-point))
  12700. ((and (org-at-heading-p)
  12701. (looking-at
  12702. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12703. (org-show-entry)
  12704. (end-of-line 1)
  12705. (newline))
  12706. (t (if indent (newline-and-indent) (newline)))))
  12707. (defun org-return-indent ()
  12708. "Goto next table row or insert a newline and indent.
  12709. Calls `org-table-next-row' or `newline-and-indent', depending on
  12710. context. See the individual commands for more information."
  12711. (interactive)
  12712. (org-return t))
  12713. (defun org-ctrl-c-star ()
  12714. "Compute table, or change heading status of lines.
  12715. Calls `org-table-recalculate' or `org-toggle-heading',
  12716. depending on context."
  12717. (interactive)
  12718. (cond
  12719. ((org-at-table-p)
  12720. (call-interactively 'org-table-recalculate))
  12721. (t
  12722. ;; Convert all lines in region to list items
  12723. (call-interactively 'org-toggle-heading))))
  12724. (defun org-ctrl-c-minus ()
  12725. "Insert separator line in table or modify bullet status of line.
  12726. Also turns a plain line or a region of lines into list items.
  12727. Calls `org-table-insert-hline', `org-toggle-item', or
  12728. `org-cycle-list-bullet', depending on context."
  12729. (interactive)
  12730. (cond
  12731. ((org-at-table-p)
  12732. (call-interactively 'org-table-insert-hline))
  12733. ((org-region-active-p)
  12734. (call-interactively 'org-toggle-item))
  12735. ((org-in-item-p)
  12736. (call-interactively 'org-cycle-list-bullet))
  12737. (t
  12738. (call-interactively 'org-toggle-item))))
  12739. (defun org-toggle-item ()
  12740. "Convert headings or normal lines to items, items to normal lines.
  12741. If there is no active region, only the current line is considered.
  12742. If the first line in the region is a headline, convert all headlines to items.
  12743. If the first line in the region is an item, convert all items to normal lines.
  12744. If the first line is normal text, add an item bullet to each line."
  12745. (interactive)
  12746. (let (l2 l beg end)
  12747. (if (org-region-active-p)
  12748. (setq beg (region-beginning) end (region-end))
  12749. (setq beg (point-at-bol)
  12750. end (min (1+ (point-at-eol)) (point-max))))
  12751. (save-excursion
  12752. (goto-char end)
  12753. (setq l2 (org-current-line))
  12754. (goto-char beg)
  12755. (beginning-of-line 1)
  12756. (setq l (1- (org-current-line)))
  12757. (if (org-at-item-p)
  12758. ;; We already have items, de-itemize
  12759. (while (< (setq l (1+ l)) l2)
  12760. (when (org-at-item-p)
  12761. (goto-char (match-beginning 2))
  12762. (delete-region (match-beginning 2) (match-end 2))
  12763. (and (looking-at "[ \t]+") (replace-match "")))
  12764. (beginning-of-line 2))
  12765. (if (org-on-heading-p)
  12766. ;; Headings, convert to items
  12767. (while (< (setq l (1+ l)) l2)
  12768. (if (looking-at org-outline-regexp)
  12769. (replace-match "- " t t))
  12770. (beginning-of-line 2))
  12771. ;; normal lines, turn them into items
  12772. (while (< (setq l (1+ l)) l2)
  12773. (unless (org-at-item-p)
  12774. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12775. (replace-match "\\1- \\2")))
  12776. (beginning-of-line 2)))))))
  12777. (defun org-toggle-heading (&optional nstars)
  12778. "Convert headings to normal text, or items or text to headings.
  12779. If there is no active region, only the current line is considered.
  12780. If the first line is a heading, remove the stars from all headlines
  12781. in the region.
  12782. If the first line is a plain list item, turn all plain list items into
  12783. headings.
  12784. If the first line is a normal line, turn each and every line in the region
  12785. into a heading.
  12786. When converting a line into a heading, the number of stars is chosen
  12787. such that the lines become children of the current entry. However, when
  12788. a prefix argument is given, its value determines the number of stars to add."
  12789. (interactive "P")
  12790. (let (l2 l itemp beg end)
  12791. (if (org-region-active-p)
  12792. (setq beg (region-beginning) end (region-end))
  12793. (setq beg (point-at-bol)
  12794. end (min (1+ (point-at-eol)) (point-max))))
  12795. (save-excursion
  12796. (goto-char end)
  12797. (setq l2 (org-current-line))
  12798. (goto-char beg)
  12799. (beginning-of-line 1)
  12800. (setq l (1- (org-current-line)))
  12801. (if (org-on-heading-p)
  12802. ;; We already have headlines, de-star them
  12803. (while (< (setq l (1+ l)) l2)
  12804. (when (org-on-heading-p t)
  12805. (and (looking-at outline-regexp) (replace-match "")))
  12806. (beginning-of-line 2))
  12807. (setq itemp (org-at-item-p))
  12808. (let* ((stars
  12809. (if nstars
  12810. (make-string (prefix-numeric-value current-prefix-arg)
  12811. ?*)
  12812. (save-excursion
  12813. (re-search-backward org-complex-heading-regexp nil t)
  12814. (or (match-string 1) "*"))))
  12815. (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
  12816. (rpl (concat stars add-stars " ")))
  12817. (while (< (setq l (1+ l)) l2)
  12818. (if itemp
  12819. (and (org-at-item-p) (replace-match rpl t t))
  12820. (unless (org-on-heading-p)
  12821. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12822. (replace-match (concat rpl (match-string 2))))))
  12823. (beginning-of-line 2)))))))
  12824. (defun org-meta-return (&optional arg)
  12825. "Insert a new heading or wrap a region in a table.
  12826. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12827. See the individual commands for more information."
  12828. (interactive "P")
  12829. (cond
  12830. ((run-hook-with-args-until-success 'org-metareturn-hook))
  12831. ((org-at-table-p)
  12832. (call-interactively 'org-table-wrap-region))
  12833. (t (call-interactively 'org-insert-heading))))
  12834. ;;; Menu entries
  12835. ;; Define the Org-mode menus
  12836. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12837. '("Tbl"
  12838. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12839. ["Next Field" org-cycle (org-at-table-p)]
  12840. ["Previous Field" org-shifttab (org-at-table-p)]
  12841. ["Next Row" org-return (org-at-table-p)]
  12842. "--"
  12843. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12844. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12845. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12846. "--"
  12847. ("Column"
  12848. ["Move Column Left" org-metaleft (org-at-table-p)]
  12849. ["Move Column Right" org-metaright (org-at-table-p)]
  12850. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12851. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12852. ("Row"
  12853. ["Move Row Up" org-metaup (org-at-table-p)]
  12854. ["Move Row Down" org-metadown (org-at-table-p)]
  12855. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12856. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12857. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12858. "--"
  12859. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12860. ("Rectangle"
  12861. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12862. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12863. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12864. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12865. "--"
  12866. ("Calculate"
  12867. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12868. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12869. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12870. "--"
  12871. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12872. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12873. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12874. "--"
  12875. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12876. "--"
  12877. ["Sum Column/Rectangle" org-table-sum
  12878. (or (org-at-table-p) (org-region-active-p))]
  12879. ["Which Column?" org-table-current-column (org-at-table-p)])
  12880. ["Debug Formulas"
  12881. org-table-toggle-formula-debugger
  12882. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12883. ["Show Col/Row Numbers"
  12884. org-table-toggle-coordinate-overlays
  12885. :style toggle
  12886. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12887. "--"
  12888. ["Create" org-table-create (and (not (org-at-table-p))
  12889. org-enable-table-editor)]
  12890. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12891. ["Import from File" org-table-import (not (org-at-table-p))]
  12892. ["Export to File" org-table-export (org-at-table-p)]
  12893. "--"
  12894. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12895. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12896. '("Org"
  12897. ("Show/Hide"
  12898. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12899. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12900. ["Sparse Tree..." org-sparse-tree t]
  12901. ["Reveal Context" org-reveal t]
  12902. ["Show All" show-all t]
  12903. "--"
  12904. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12905. "--"
  12906. ["New Heading" org-insert-heading t]
  12907. ("Navigate Headings"
  12908. ["Up" outline-up-heading t]
  12909. ["Next" outline-next-visible-heading t]
  12910. ["Previous" outline-previous-visible-heading t]
  12911. ["Next Same Level" outline-forward-same-level t]
  12912. ["Previous Same Level" outline-backward-same-level t]
  12913. "--"
  12914. ["Jump" org-goto t])
  12915. ("Edit Structure"
  12916. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12917. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12918. "--"
  12919. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12920. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12921. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12922. "--"
  12923. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12924. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12925. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12926. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12927. "--"
  12928. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12929. "--"
  12930. ["Convert to odd levels" org-convert-to-odd-levels t]
  12931. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12932. ("Editing"
  12933. ["Emphasis..." org-emphasize t]
  12934. ["Edit Source Example" org-edit-special t]
  12935. "--"
  12936. ["Footnote new/jump" org-footnote-action t]
  12937. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12938. ("Archive"
  12939. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12940. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12941. ; :active t :keys "C-u C-c C-x C-a"]
  12942. ["Sparse trees open ARCHIVE trees"
  12943. (setq org-sparse-tree-open-archived-trees
  12944. (not org-sparse-tree-open-archived-trees))
  12945. :style toggle :selected org-sparse-tree-open-archived-trees]
  12946. ["Cycling opens ARCHIVE trees"
  12947. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12948. :style toggle :selected org-cycle-open-archived-trees]
  12949. "--"
  12950. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12951. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12952. ; ["Check and Move Children" (org-archive-subtree '(4))
  12953. ; :active t :keys "C-u C-c C-x C-s"]
  12954. )
  12955. "--"
  12956. ("TODO Lists"
  12957. ["TODO/DONE/-" org-todo t]
  12958. ("Select keyword"
  12959. ["Next keyword" org-shiftright (org-on-heading-p)]
  12960. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12961. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12962. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12963. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12964. ["Show TODO Tree" org-show-todo-tree t]
  12965. ["Global TODO list" org-todo-list t]
  12966. "--"
  12967. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  12968. :selected org-enforce-todo-dependencies :style toggle :active t]
  12969. "Settings for tree at point"
  12970. ["Do Children sequentially" org-toggle-ordered-property :style radio
  12971. :selected (ignore-errors (org-entry-get nil "ORDERED"))
  12972. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12973. ["Do Children parallel" org-toggle-ordered-property :style radio
  12974. :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
  12975. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  12976. "--"
  12977. ["Set Priority" org-priority t]
  12978. ["Priority Up" org-shiftup t]
  12979. ["Priority Down" org-shiftdown t])
  12980. ("TAGS and Properties"
  12981. ["Set Tags" org-set-tags-command t]
  12982. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12983. "--"
  12984. ["Set property" org-set-property t]
  12985. ["Column view of properties" org-columns t]
  12986. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12987. ("Dates and Scheduling"
  12988. ["Timestamp" org-time-stamp t]
  12989. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12990. ("Change Date"
  12991. ["1 Day Later" org-shiftright t]
  12992. ["1 Day Earlier" org-shiftleft t]
  12993. ["1 ... Later" org-shiftup t]
  12994. ["1 ... Earlier" org-shiftdown t])
  12995. ["Compute Time Range" org-evaluate-time-range t]
  12996. ["Schedule Item" org-schedule t]
  12997. ["Deadline" org-deadline t]
  12998. "--"
  12999. ["Custom time format" org-toggle-time-stamp-overlays
  13000. :style radio :selected org-display-custom-times]
  13001. "--"
  13002. ["Goto Calendar" org-goto-calendar t]
  13003. ["Date from Calendar" org-date-from-calendar t]
  13004. "--"
  13005. ["Start/Restart Timer" org-timer-start t]
  13006. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  13007. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  13008. ["Insert Timer String" org-timer t]
  13009. ["Insert Timer Item" org-timer-item t])
  13010. ("Logging work"
  13011. ["Clock in" org-clock-in t]
  13012. ["Clock out" org-clock-out t]
  13013. ["Clock cancel" org-clock-cancel t]
  13014. ["Goto running clock" org-clock-goto t]
  13015. ["Display times" org-clock-display t]
  13016. ["Create clock table" org-clock-report t]
  13017. "--"
  13018. ["Record DONE time"
  13019. (progn (setq org-log-done (not org-log-done))
  13020. (message "Switching to %s will %s record a timestamp"
  13021. (car org-done-keywords)
  13022. (if org-log-done "automatically" "not")))
  13023. :style toggle :selected org-log-done])
  13024. "--"
  13025. ["Agenda Command..." org-agenda t]
  13026. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  13027. ("File List for Agenda")
  13028. ("Special views current file"
  13029. ["TODO Tree" org-show-todo-tree t]
  13030. ["Check Deadlines" org-check-deadlines t]
  13031. ["Timeline" org-timeline t]
  13032. ["Tags Tree" org-tags-sparse-tree t])
  13033. "--"
  13034. ("Hyperlinks"
  13035. ["Store Link (Global)" org-store-link t]
  13036. ["Insert Link" org-insert-link t]
  13037. ["Follow Link" org-open-at-point t]
  13038. "--"
  13039. ["Next link" org-next-link t]
  13040. ["Previous link" org-previous-link t]
  13041. "--"
  13042. ["Descriptive Links"
  13043. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13044. :style radio
  13045. :selected (member '(org-link) buffer-invisibility-spec)]
  13046. ["Literal Links"
  13047. (progn
  13048. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13049. :style radio
  13050. :selected (not (member '(org-link) buffer-invisibility-spec))])
  13051. "--"
  13052. ["Export/Publish..." org-export t]
  13053. ("LaTeX"
  13054. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  13055. :selected org-cdlatex-mode]
  13056. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  13057. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  13058. ["Modify math symbol" org-cdlatex-math-modify
  13059. (org-inside-LaTeX-fragment-p)]
  13060. ["Export LaTeX fragments as images"
  13061. (if (featurep 'org-exp)
  13062. (setq org-export-with-LaTeX-fragments
  13063. (not org-export-with-LaTeX-fragments))
  13064. (require 'org-exp))
  13065. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  13066. org-export-with-LaTeX-fragments)])
  13067. "--"
  13068. ("Documentation"
  13069. ["Show Version" org-version t]
  13070. ["Info Documentation" org-info t])
  13071. ("Customize"
  13072. ["Browse Org Group" org-customize t]
  13073. "--"
  13074. ["Expand This Menu" org-create-customize-menu
  13075. (fboundp 'customize-menu-create)])
  13076. "--"
  13077. ["Refresh setup" org-mode-restart t]
  13078. ))
  13079. (defun org-info (&optional node)
  13080. "Read documentation for Org-mode in the info system.
  13081. With optional NODE, go directly to that node."
  13082. (interactive)
  13083. (info (format "(org)%s" (or node ""))))
  13084. (defun org-install-agenda-files-menu ()
  13085. (let ((bl (buffer-list)))
  13086. (save-excursion
  13087. (while bl
  13088. (set-buffer (pop bl))
  13089. (if (org-mode-p) (setq bl nil)))
  13090. (when (org-mode-p)
  13091. (easy-menu-change
  13092. '("Org") "File List for Agenda"
  13093. (append
  13094. (list
  13095. ["Edit File List" (org-edit-agenda-file-list) t]
  13096. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13097. ["Remove Current File from List" org-remove-file t]
  13098. ["Cycle through agenda files" org-cycle-agenda-files t]
  13099. ["Occur in all agenda files" org-occur-in-agenda-files t]
  13100. "--")
  13101. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13102. ;;;; Documentation
  13103. ;;;###autoload
  13104. (defun org-require-autoloaded-modules ()
  13105. (interactive)
  13106. (mapc 'require
  13107. '(org-agenda org-archive org-attach org-clock org-colview
  13108. org-exp org-id org-export-latex org-publish
  13109. org-remember org-table org-timer)))
  13110. ;;;###autoload
  13111. (defun org-reload (&optional uncompiled)
  13112. "Reload all org lisp files.
  13113. With prefix arg UNCOMPILED, load the uncompiled versions."
  13114. (interactive "P")
  13115. (require 'find-func)
  13116. (let* ((dir (file-name-directory (find-library-name "org")))
  13117. (files (directory-files dir t "\\.el\\'"))
  13118. (remove-re (concat (if (featurep 'xemacs)
  13119. "org-colview" "org-colview-xemacs")
  13120. "\\'")))
  13121. (setq files (mapcar 'file-name-sans-extension files))
  13122. (setq files (mapcar
  13123. (lambda (x) (if (string-match remove-re x) nil x))
  13124. files))
  13125. (setq files (delq nil files))
  13126. (mapc
  13127. (lambda (f)
  13128. (if (and (not uncompiled)
  13129. (file-exists-p (concat f ".elc")))
  13130. (load (concat f ".elc") nil nil t)
  13131. (load (concat f ".el") nil nil t)))
  13132. files)))
  13133. ;;;###autoload
  13134. (defun org-customize ()
  13135. "Call the customize function with org as argument."
  13136. (interactive)
  13137. (org-load-modules-maybe)
  13138. (org-require-autoloaded-modules)
  13139. (customize-browse 'org))
  13140. (defun org-create-customize-menu ()
  13141. "Create a full customization menu for Org-mode, insert it into the menu."
  13142. (interactive)
  13143. (org-load-modules-maybe)
  13144. (org-require-autoloaded-modules)
  13145. (if (fboundp 'customize-menu-create)
  13146. (progn
  13147. (easy-menu-change
  13148. '("Org") "Customize"
  13149. `(["Browse Org group" org-customize t]
  13150. "--"
  13151. ,(customize-menu-create 'org)
  13152. ["Set" Custom-set t]
  13153. ["Save" Custom-save t]
  13154. ["Reset to Current" Custom-reset-current t]
  13155. ["Reset to Saved" Custom-reset-saved t]
  13156. ["Reset to Standard Settings" Custom-reset-standard t]))
  13157. (message "\"Org\"-menu now contains full customization menu"))
  13158. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13159. ;;;; Miscellaneous stuff
  13160. ;;; Generally useful functions
  13161. (defun org-find-text-property-in-string (prop s)
  13162. "Return the first non-nil value of property PROP in string S."
  13163. (or (get-text-property 0 prop s)
  13164. (get-text-property (or (next-single-property-change 0 prop s) 0)
  13165. prop s)))
  13166. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  13167. "Display the given MESSAGE as a warning."
  13168. (if (fboundp 'display-warning)
  13169. (display-warning 'org message
  13170. (if (featurep 'xemacs)
  13171. 'warning
  13172. :warning))
  13173. (let ((buf (get-buffer-create "*Org warnings*")))
  13174. (with-current-buffer buf
  13175. (goto-char (point-max))
  13176. (insert "Warning (Org): " message)
  13177. (unless (bolp)
  13178. (newline)))
  13179. (display-buffer buf)
  13180. (sit-for 0))))
  13181. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  13182. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  13183. (if (and marker (marker-buffer marker)
  13184. (buffer-live-p (marker-buffer marker)))
  13185. (progn
  13186. (switch-to-buffer (marker-buffer marker))
  13187. (if (or (> marker (point-max)) (< marker (point-min)))
  13188. (widen))
  13189. (goto-char marker)
  13190. (org-show-context 'org-goto))
  13191. (if bookmark
  13192. (bookmark-jump bookmark)
  13193. (error "Cannot find location"))))
  13194. (defun org-quote-csv-field (s)
  13195. "Quote field for inclusion in CSV material."
  13196. (if (string-match "[\",]" s)
  13197. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  13198. s))
  13199. (defun org-plist-delete (plist property)
  13200. "Delete PROPERTY from PLIST.
  13201. This is in contrast to merely setting it to 0."
  13202. (let (p)
  13203. (while plist
  13204. (if (not (eq property (car plist)))
  13205. (setq p (plist-put p (car plist) (nth 1 plist))))
  13206. (setq plist (cddr plist)))
  13207. p))
  13208. (defun org-force-self-insert (N)
  13209. "Needed to enforce self-insert under remapping."
  13210. (interactive "p")
  13211. (self-insert-command N))
  13212. (defun org-string-width (s)
  13213. "Compute width of string, ignoring invisible characters.
  13214. This ignores character with invisibility property `org-link', and also
  13215. characters with property `org-cwidth', because these will become invisible
  13216. upon the next fontification round."
  13217. (let (b l)
  13218. (when (or (eq t buffer-invisibility-spec)
  13219. (assq 'org-link buffer-invisibility-spec))
  13220. (while (setq b (text-property-any 0 (length s)
  13221. 'invisible 'org-link s))
  13222. (setq s (concat (substring s 0 b)
  13223. (substring s (or (next-single-property-change
  13224. b 'invisible s) (length s)))))))
  13225. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  13226. (setq s (concat (substring s 0 b)
  13227. (substring s (or (next-single-property-change
  13228. b 'org-cwidth s) (length s))))))
  13229. (setq l (string-width s) b -1)
  13230. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  13231. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  13232. l))
  13233. (defun org-get-indentation (&optional line)
  13234. "Get the indentation of the current line, interpreting tabs.
  13235. When LINE is given, assume it represents a line and compute its indentation."
  13236. (if line
  13237. (if (string-match "^ *" (org-remove-tabs line))
  13238. (match-end 0))
  13239. (save-excursion
  13240. (beginning-of-line 1)
  13241. (skip-chars-forward " \t")
  13242. (current-column))))
  13243. (defun org-remove-tabs (s &optional width)
  13244. "Replace tabulators in S with spaces.
  13245. Assumes that s is a single line, starting in column 0."
  13246. (setq width (or width tab-width))
  13247. (while (string-match "\t" s)
  13248. (setq s (replace-match
  13249. (make-string
  13250. (- (* width (/ (+ (match-beginning 0) width) width))
  13251. (match-beginning 0)) ?\ )
  13252. t t s)))
  13253. s)
  13254. (defun org-fix-indentation (line ind)
  13255. "Fix indentation in LINE.
  13256. IND is a cons cell with target and minimum indentation.
  13257. If the current indentation in LINE is smaller than the minimum,
  13258. leave it alone. If it is larger than ind, set it to the target."
  13259. (let* ((l (org-remove-tabs line))
  13260. (i (org-get-indentation l))
  13261. (i1 (car ind)) (i2 (cdr ind)))
  13262. (if (>= i i2) (setq l (substring line i2)))
  13263. (if (> i1 0)
  13264. (concat (make-string i1 ?\ ) l)
  13265. l)))
  13266. (defun org-base-buffer (buffer)
  13267. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  13268. (if (not buffer)
  13269. buffer
  13270. (or (buffer-base-buffer buffer)
  13271. buffer)))
  13272. (defun org-trim (s)
  13273. "Remove whitespace at beginning and end of string."
  13274. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  13275. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  13276. s)
  13277. (defun org-wrap (string &optional width lines)
  13278. "Wrap string to either a number of lines, or a width in characters.
  13279. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  13280. that costs. If there is a word longer than WIDTH, the text is actually
  13281. wrapped to the length of that word.
  13282. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  13283. many lines, whatever width that takes.
  13284. The return value is a list of lines, without newlines at the end."
  13285. (let* ((words (org-split-string string "[ \t\n]+"))
  13286. (maxword (apply 'max (mapcar 'org-string-width words)))
  13287. w ll)
  13288. (cond (width
  13289. (org-do-wrap words (max maxword width)))
  13290. (lines
  13291. (setq w maxword)
  13292. (setq ll (org-do-wrap words maxword))
  13293. (if (<= (length ll) lines)
  13294. ll
  13295. (setq ll words)
  13296. (while (> (length ll) lines)
  13297. (setq w (1+ w))
  13298. (setq ll (org-do-wrap words w)))
  13299. ll))
  13300. (t (error "Cannot wrap this")))))
  13301. (defun org-do-wrap (words width)
  13302. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  13303. (let (lines line)
  13304. (while words
  13305. (setq line (pop words))
  13306. (while (and words (< (+ (length line) (length (car words))) width))
  13307. (setq line (concat line " " (pop words))))
  13308. (setq lines (push line lines)))
  13309. (nreverse lines)))
  13310. (defun org-split-string (string &optional separators)
  13311. "Splits STRING into substrings at SEPARATORS.
  13312. No empty strings are returned if there are matches at the beginning
  13313. and end of string."
  13314. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  13315. (start 0)
  13316. notfirst
  13317. (list nil))
  13318. (while (and (string-match rexp string
  13319. (if (and notfirst
  13320. (= start (match-beginning 0))
  13321. (< start (length string)))
  13322. (1+ start) start))
  13323. (< (match-beginning 0) (length string)))
  13324. (setq notfirst t)
  13325. (or (eq (match-beginning 0) 0)
  13326. (and (eq (match-beginning 0) (match-end 0))
  13327. (eq (match-beginning 0) start))
  13328. (setq list
  13329. (cons (substring string start (match-beginning 0))
  13330. list)))
  13331. (setq start (match-end 0)))
  13332. (or (eq start (length string))
  13333. (setq list
  13334. (cons (substring string start)
  13335. list)))
  13336. (nreverse list)))
  13337. (defun org-context ()
  13338. "Return a list of contexts of the current cursor position.
  13339. If several contexts apply, all are returned.
  13340. Each context entry is a list with a symbol naming the context, and
  13341. two positions indicating start and end of the context. Possible
  13342. contexts are:
  13343. :headline anywhere in a headline
  13344. :headline-stars on the leading stars in a headline
  13345. :todo-keyword on a TODO keyword (including DONE) in a headline
  13346. :tags on the TAGS in a headline
  13347. :priority on the priority cookie in a headline
  13348. :item on the first line of a plain list item
  13349. :item-bullet on the bullet/number of a plain list item
  13350. :checkbox on the checkbox in a plain list item
  13351. :table in an org-mode table
  13352. :table-special on a special filed in a table
  13353. :table-table in a table.el table
  13354. :link on a hyperlink
  13355. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13356. :target on a <<target>>
  13357. :radio-target on a <<<radio-target>>>
  13358. :latex-fragment on a LaTeX fragment
  13359. :latex-preview on a LaTeX fragment with overlayed preview image
  13360. This function expects the position to be visible because it uses font-lock
  13361. faces as a help to recognize the following contexts: :table-special, :link,
  13362. and :keyword."
  13363. (let* ((f (get-text-property (point) 'face))
  13364. (faces (if (listp f) f (list f)))
  13365. (p (point)) clist o)
  13366. ;; First the large context
  13367. (cond
  13368. ((org-on-heading-p t)
  13369. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13370. (when (progn
  13371. (beginning-of-line 1)
  13372. (looking-at org-todo-line-tags-regexp))
  13373. (push (org-point-in-group p 1 :headline-stars) clist)
  13374. (push (org-point-in-group p 2 :todo-keyword) clist)
  13375. (push (org-point-in-group p 4 :tags) clist))
  13376. (goto-char p)
  13377. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13378. (if (looking-at "\\[#[A-Z0-9]\\]")
  13379. (push (org-point-in-group p 0 :priority) clist)))
  13380. ((org-at-item-p)
  13381. (push (org-point-in-group p 2 :item-bullet) clist)
  13382. (push (list :item (point-at-bol)
  13383. (save-excursion (org-end-of-item) (point)))
  13384. clist)
  13385. (and (org-at-item-checkbox-p)
  13386. (push (org-point-in-group p 0 :checkbox) clist)))
  13387. ((org-at-table-p)
  13388. (push (list :table (org-table-begin) (org-table-end)) clist)
  13389. (if (memq 'org-formula faces)
  13390. (push (list :table-special
  13391. (previous-single-property-change p 'face)
  13392. (next-single-property-change p 'face)) clist)))
  13393. ((org-at-table-p 'any)
  13394. (push (list :table-table) clist)))
  13395. (goto-char p)
  13396. ;; Now the small context
  13397. (cond
  13398. ((org-at-timestamp-p)
  13399. (push (org-point-in-group p 0 :timestamp) clist))
  13400. ((memq 'org-link faces)
  13401. (push (list :link
  13402. (previous-single-property-change p 'face)
  13403. (next-single-property-change p 'face)) clist))
  13404. ((memq 'org-special-keyword faces)
  13405. (push (list :keyword
  13406. (previous-single-property-change p 'face)
  13407. (next-single-property-change p 'face)) clist))
  13408. ((org-on-target-p)
  13409. (push (org-point-in-group p 0 :target) clist)
  13410. (goto-char (1- (match-beginning 0)))
  13411. (if (looking-at org-radio-target-regexp)
  13412. (push (org-point-in-group p 0 :radio-target) clist))
  13413. (goto-char p))
  13414. ((setq o (car (delq nil
  13415. (mapcar
  13416. (lambda (x)
  13417. (if (memq x org-latex-fragment-image-overlays) x))
  13418. (org-overlays-at (point))))))
  13419. (push (list :latex-fragment
  13420. (org-overlay-start o) (org-overlay-end o)) clist)
  13421. (push (list :latex-preview
  13422. (org-overlay-start o) (org-overlay-end o)) clist))
  13423. ((org-inside-LaTeX-fragment-p)
  13424. ;; FIXME: positions wrong.
  13425. (push (list :latex-fragment (point) (point)) clist)))
  13426. (setq clist (nreverse (delq nil clist)))
  13427. clist))
  13428. ;; FIXME: Compare with at-regexp-p Do we need both?
  13429. (defun org-in-regexp (re &optional nlines visually)
  13430. "Check if point is inside a match of regexp.
  13431. Normally only the current line is checked, but you can include NLINES extra
  13432. lines both before and after point into the search.
  13433. If VISUALLY is set, require that the cursor is not after the match but
  13434. really on, so that the block visually is on the match."
  13435. (catch 'exit
  13436. (let ((pos (point))
  13437. (eol (point-at-eol (+ 1 (or nlines 0))))
  13438. (inc (if visually 1 0)))
  13439. (save-excursion
  13440. (beginning-of-line (- 1 (or nlines 0)))
  13441. (while (re-search-forward re eol t)
  13442. (if (and (<= (match-beginning 0) pos)
  13443. (>= (+ inc (match-end 0)) pos))
  13444. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  13445. (defun org-at-regexp-p (regexp)
  13446. "Is point inside a match of REGEXP in the current line?"
  13447. (catch 'exit
  13448. (save-excursion
  13449. (let ((pos (point)) (end (point-at-eol)))
  13450. (beginning-of-line 1)
  13451. (while (re-search-forward regexp end t)
  13452. (if (and (<= (match-beginning 0) pos)
  13453. (>= (match-end 0) pos))
  13454. (throw 'exit t)))
  13455. nil))))
  13456. (defun org-occur-in-agenda-files (regexp &optional nlines)
  13457. "Call `multi-occur' with buffers for all agenda files."
  13458. (interactive "sOrg-files matching: \np")
  13459. (let* ((files (org-agenda-files))
  13460. (tnames (mapcar 'file-truename files))
  13461. (extra org-agenda-text-search-extra-files)
  13462. f)
  13463. (when (eq (car extra) 'agenda-archives)
  13464. (setq extra (cdr extra))
  13465. (setq files (org-add-archive-files files)))
  13466. (while (setq f (pop extra))
  13467. (unless (member (file-truename f) tnames)
  13468. (add-to-list 'files f 'append)
  13469. (add-to-list 'tnames (file-truename f) 'append)))
  13470. (multi-occur
  13471. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  13472. regexp)))
  13473. (if (boundp 'occur-mode-find-occurrence-hook)
  13474. ;; Emacs 23
  13475. (add-hook 'occur-mode-find-occurrence-hook
  13476. (lambda ()
  13477. (when (org-mode-p)
  13478. (org-reveal))))
  13479. ;; Emacs 22
  13480. (defadvice occur-mode-goto-occurrence
  13481. (after org-occur-reveal activate)
  13482. (and (org-mode-p) (org-reveal)))
  13483. (defadvice occur-mode-goto-occurrence-other-window
  13484. (after org-occur-reveal activate)
  13485. (and (org-mode-p) (org-reveal)))
  13486. (defadvice occur-mode-display-occurrence
  13487. (after org-occur-reveal activate)
  13488. (when (org-mode-p)
  13489. (let ((pos (occur-mode-find-occurrence)))
  13490. (with-current-buffer (marker-buffer pos)
  13491. (save-excursion
  13492. (goto-char pos)
  13493. (org-reveal)))))))
  13494. (defun org-uniquify (list)
  13495. "Remove duplicate elements from LIST."
  13496. (let (res)
  13497. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  13498. res))
  13499. (defun org-delete-all (elts list)
  13500. "Remove all elements in ELTS from LIST."
  13501. (while elts
  13502. (setq list (delete (pop elts) list)))
  13503. list)
  13504. (defun org-back-over-empty-lines ()
  13505. "Move backwards over whitespace, to the beginning of the first empty line.
  13506. Returns the number of empty lines passed."
  13507. (let ((pos (point)))
  13508. (skip-chars-backward " \t\n\r")
  13509. (beginning-of-line 2)
  13510. (goto-char (min (point) pos))
  13511. (count-lines (point) pos)))
  13512. (defun org-skip-whitespace ()
  13513. (skip-chars-forward " \t\n\r"))
  13514. (defun org-point-in-group (point group &optional context)
  13515. "Check if POINT is in match-group GROUP.
  13516. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13517. match. If the match group does ot exist or point is not inside it,
  13518. return nil."
  13519. (and (match-beginning group)
  13520. (>= point (match-beginning group))
  13521. (<= point (match-end group))
  13522. (if context
  13523. (list context (match-beginning group) (match-end group))
  13524. t)))
  13525. (defun org-switch-to-buffer-other-window (&rest args)
  13526. "Switch to buffer in a second window on the current frame.
  13527. In particular, do not allow pop-up frames."
  13528. (let (pop-up-frames special-display-buffer-names special-display-regexps
  13529. special-display-function)
  13530. (apply 'switch-to-buffer-other-window args)))
  13531. (defun org-combine-plists (&rest plists)
  13532. "Create a single property list from all plists in PLISTS.
  13533. The process starts by copying the first list, and then setting properties
  13534. from the other lists. Settings in the last list are the most significant
  13535. ones and overrule settings in the other lists."
  13536. (let ((rtn (copy-sequence (pop plists)))
  13537. p v ls)
  13538. (while plists
  13539. (setq ls (pop plists))
  13540. (while ls
  13541. (setq p (pop ls) v (pop ls))
  13542. (setq rtn (plist-put rtn p v))))
  13543. rtn))
  13544. (defun org-move-line-down (arg)
  13545. "Move the current line down. With prefix argument, move it past ARG lines."
  13546. (interactive "p")
  13547. (let ((col (current-column))
  13548. beg end pos)
  13549. (beginning-of-line 1) (setq beg (point))
  13550. (beginning-of-line 2) (setq end (point))
  13551. (beginning-of-line (+ 1 arg))
  13552. (setq pos (move-marker (make-marker) (point)))
  13553. (insert (delete-and-extract-region beg end))
  13554. (goto-char pos)
  13555. (org-move-to-column col)))
  13556. (defun org-move-line-up (arg)
  13557. "Move the current line up. With prefix argument, move it past ARG lines."
  13558. (interactive "p")
  13559. (let ((col (current-column))
  13560. beg end pos)
  13561. (beginning-of-line 1) (setq beg (point))
  13562. (beginning-of-line 2) (setq end (point))
  13563. (beginning-of-line (- arg))
  13564. (setq pos (move-marker (make-marker) (point)))
  13565. (insert (delete-and-extract-region beg end))
  13566. (goto-char pos)
  13567. (org-move-to-column col)))
  13568. (defun org-replace-escapes (string table)
  13569. "Replace %-escapes in STRING with values in TABLE.
  13570. TABLE is an association list with keys like \"%a\" and string values.
  13571. The sequences in STRING may contain normal field width and padding information,
  13572. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  13573. so values can contain further %-escapes if they are define later in TABLE."
  13574. (let ((case-fold-search nil)
  13575. e re rpl)
  13576. (while (setq e (pop table))
  13577. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  13578. (while (string-match re string)
  13579. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  13580. (cdr e)))
  13581. (setq string (replace-match rpl t t string))))
  13582. string))
  13583. (defun org-sublist (list start end)
  13584. "Return a section of LIST, from START to END.
  13585. Counting starts at 1."
  13586. (let (rtn (c start))
  13587. (setq list (nthcdr (1- start) list))
  13588. (while (and list (<= c end))
  13589. (push (pop list) rtn)
  13590. (setq c (1+ c)))
  13591. (nreverse rtn)))
  13592. (defun org-find-base-buffer-visiting (file)
  13593. "Like `find-buffer-visiting' but alway return the base buffer and
  13594. not an indirect buffer."
  13595. (let ((buf (find-buffer-visiting file)))
  13596. (if buf
  13597. (or (buffer-base-buffer buf) buf)
  13598. nil)))
  13599. (defun org-image-file-name-regexp (&optional extensions)
  13600. "Return regexp matching the file names of images.
  13601. If EXTENSIONS is given, only match these."
  13602. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  13603. (image-file-name-regexp)
  13604. (let ((image-file-name-extensions
  13605. (or extensions
  13606. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13607. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  13608. (concat "\\."
  13609. (regexp-opt (nconc (mapcar 'upcase
  13610. image-file-name-extensions)
  13611. image-file-name-extensions)
  13612. t)
  13613. "\\'"))))
  13614. (defun org-file-image-p (file &optional extensions)
  13615. "Return non-nil if FILE is an image."
  13616. (save-match-data
  13617. (string-match (org-image-file-name-regexp extensions) file)))
  13618. (defun org-get-cursor-date ()
  13619. "Return the date at cursor in as a time.
  13620. This works in the calendar and in the agenda, anywhere else it just
  13621. returns the current time."
  13622. (let (date day defd)
  13623. (cond
  13624. ((eq major-mode 'calendar-mode)
  13625. (setq date (calendar-cursor-to-date)
  13626. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  13627. ((eq major-mode 'org-agenda-mode)
  13628. (setq day (get-text-property (point) 'day))
  13629. (if day
  13630. (setq date (calendar-gregorian-from-absolute day)
  13631. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  13632. (nth 2 date))))))
  13633. (or defd (current-time))))
  13634. (defvar org-agenda-action-marker (make-marker)
  13635. "Marker pointing to the entry for the next agenda action.")
  13636. (defun org-mark-entry-for-agenda-action ()
  13637. "Mark the current entry as target of an agenda action.
  13638. Agenda actions are actions executed from the agenda with the key `k',
  13639. which make use of the date at the cursor."
  13640. (interactive)
  13641. (move-marker org-agenda-action-marker
  13642. (save-excursion (org-back-to-heading t) (point))
  13643. (current-buffer))
  13644. (message
  13645. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  13646. ;;; Paragraph filling stuff.
  13647. ;; We want this to be just right, so use the full arsenal.
  13648. (defun org-indent-line-function ()
  13649. "Indent line like previous, but further if previous was headline or item."
  13650. (interactive)
  13651. (let* ((pos (point))
  13652. (itemp (org-at-item-p))
  13653. (org-drawer-regexp (or org-drawer-regexp "\000"))
  13654. column bpos bcol tpos tcol bullet btype bullet-type)
  13655. ;; Find the previous relevant line
  13656. (beginning-of-line 1)
  13657. (cond
  13658. ((looking-at "#") (setq column 0))
  13659. ((looking-at "\\*+ ") (setq column 0))
  13660. ((and (looking-at "[ \t]*:END:")
  13661. (save-excursion (re-search-backward org-drawer-regexp nil t)))
  13662. (save-excursion
  13663. (goto-char (1- (match-beginning 1)))
  13664. (setq column (current-column))))
  13665. (t
  13666. (beginning-of-line 0)
  13667. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]")
  13668. (not (looking-at "[ \t]*:END:"))
  13669. (not (looking-at org-drawer-regexp)))
  13670. (beginning-of-line 0))
  13671. (cond
  13672. ((looking-at "\\*+[ \t]+")
  13673. (if (not org-adapt-indentation)
  13674. (setq column 0)
  13675. (goto-char (match-end 0))
  13676. (setq column (current-column))))
  13677. ((looking-at org-drawer-regexp)
  13678. (goto-char (1- (match-beginning 1)))
  13679. (setq column (current-column)))
  13680. ((looking-at "\\([ \t]*\\):END:")
  13681. (goto-char (match-end 1))
  13682. (setq column (current-column)))
  13683. ((org-in-item-p)
  13684. (org-beginning-of-item)
  13685. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  13686. (setq bpos (match-beginning 1) tpos (match-end 0)
  13687. bcol (progn (goto-char bpos) (current-column))
  13688. tcol (progn (goto-char tpos) (current-column))
  13689. bullet (match-string 1)
  13690. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  13691. (if (> tcol (+ bcol org-description-max-indent))
  13692. (setq tcol (+ bcol 5)))
  13693. (if (not itemp)
  13694. (setq column tcol)
  13695. (goto-char pos)
  13696. (beginning-of-line 1)
  13697. (if (looking-at "\\S-")
  13698. (progn
  13699. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13700. (setq bullet (match-string 1)
  13701. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13702. (setq column (if (equal btype bullet-type) bcol tcol)))
  13703. (setq column (org-get-indentation)))))
  13704. (t (setq column (org-get-indentation))))))
  13705. (goto-char pos)
  13706. (if (<= (current-column) (current-indentation))
  13707. (org-indent-line-to column)
  13708. (save-excursion (org-indent-line-to column)))
  13709. (setq column (current-column))
  13710. (beginning-of-line 1)
  13711. (if (looking-at
  13712. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13713. (replace-match (concat "\\1" (format org-property-format
  13714. (match-string 2) (match-string 3)))
  13715. t nil))
  13716. (org-move-to-column column)))
  13717. (defun org-set-autofill-regexps ()
  13718. (interactive)
  13719. ;; In the paragraph separator we include headlines, because filling
  13720. ;; text in a line directly attached to a headline would otherwise
  13721. ;; fill the headline as well.
  13722. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13723. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13724. ;; The paragraph starter includes hand-formatted lists.
  13725. (org-set-local 'paragraph-start
  13726. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13727. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13728. ;; But only if the user has not turned off tables or fixed-width regions
  13729. (org-set-local
  13730. 'auto-fill-inhibit-regexp
  13731. (concat "\\*+ \\|#\\+"
  13732. "\\|[ \t]*" org-keyword-time-regexp
  13733. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13734. (concat
  13735. "\\|[ \t]*["
  13736. (if org-enable-table-editor "|" "")
  13737. (if org-enable-fixed-width-editor ":" "")
  13738. "]"))))
  13739. ;; We use our own fill-paragraph function, to make sure that tables
  13740. ;; and fixed-width regions are not wrapped. That function will pass
  13741. ;; through to `fill-paragraph' when appropriate.
  13742. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13743. ; Adaptive filling: To get full control, first make sure that
  13744. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13745. (org-set-local 'adaptive-fill-regexp "\000")
  13746. (org-set-local 'adaptive-fill-function
  13747. 'org-adaptive-fill-function)
  13748. (org-set-local
  13749. 'align-mode-rules-list
  13750. '((org-in-buffer-settings
  13751. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13752. (modes . '(org-mode))))))
  13753. (defun org-fill-paragraph (&optional justify)
  13754. "Re-align a table, pass through to fill-paragraph if no table."
  13755. (let ((table-p (org-at-table-p))
  13756. (table.el-p (org-at-table.el-p)))
  13757. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13758. (save-excursion (goto-char (point-at-bol))
  13759. (looking-at outline-regexp)))
  13760. t) ; skip headlines
  13761. (table.el-p t) ; skip table.el tables
  13762. (table-p (org-table-align) t) ; align org-mode tables
  13763. (t nil)))) ; call paragraph-fill
  13764. ;; For reference, this is the default value of adaptive-fill-regexp
  13765. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13766. (defun org-adaptive-fill-function ()
  13767. "Return a fill prefix for org-mode files.
  13768. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13769. work correctly."
  13770. (cond ((looking-at "#[ \t]+")
  13771. (match-string 0))
  13772. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13773. (save-excursion
  13774. (if (> (match-end 1) (+ (match-beginning 1)
  13775. org-description-max-indent))
  13776. (goto-char (+ (match-beginning 1) 5))
  13777. (goto-char (match-end 0)))
  13778. (make-string (current-column) ?\ )))
  13779. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13780. (save-excursion
  13781. (goto-char (match-end 0))
  13782. (make-string (current-column) ?\ )))
  13783. (t nil)))
  13784. ;;; Other stuff.
  13785. (defun org-toggle-fixed-width-section (arg)
  13786. "Toggle the fixed-width export.
  13787. If there is no active region, the QUOTE keyword at the current headline is
  13788. inserted or removed. When present, it causes the text between this headline
  13789. and the next to be exported as fixed-width text, and unmodified.
  13790. If there is an active region, this command adds or removes a colon as the
  13791. first character of this line. If the first character of a line is a colon,
  13792. this line is also exported in fixed-width font."
  13793. (interactive "P")
  13794. (let* ((cc 0)
  13795. (regionp (org-region-active-p))
  13796. (beg (if regionp (region-beginning) (point)))
  13797. (end (if regionp (region-end)))
  13798. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13799. (case-fold-search nil)
  13800. (re "[ \t]*\\(:\\)")
  13801. off)
  13802. (if regionp
  13803. (save-excursion
  13804. (goto-char beg)
  13805. (setq cc (current-column))
  13806. (beginning-of-line 1)
  13807. (setq off (looking-at re))
  13808. (while (> nlines 0)
  13809. (setq nlines (1- nlines))
  13810. (beginning-of-line 1)
  13811. (cond
  13812. (arg
  13813. (org-move-to-column cc t)
  13814. (insert ":\n")
  13815. (forward-line -1))
  13816. ((and off (looking-at re))
  13817. (replace-match "" t t nil 1))
  13818. ((not off) (org-move-to-column cc t) (insert ":")))
  13819. (forward-line 1)))
  13820. (save-excursion
  13821. (org-back-to-heading)
  13822. (if (looking-at (concat outline-regexp
  13823. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13824. (replace-match "" t t nil 1)
  13825. (if (looking-at outline-regexp)
  13826. (progn
  13827. (goto-char (match-end 0))
  13828. (insert org-quote-string " "))))))))
  13829. ;;;; Functions extending outline functionality
  13830. (defun org-beginning-of-line (&optional arg)
  13831. "Go to the beginning of the current line. If that is invisible, continue
  13832. to a visible line beginning. This makes the function of C-a more intuitive.
  13833. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13834. first attempt, and only move to after the tags when the cursor is already
  13835. beyond the end of the headline."
  13836. (interactive "P")
  13837. (let ((pos (point))
  13838. (special (if (consp org-special-ctrl-a/e)
  13839. (car org-special-ctrl-a/e)
  13840. org-special-ctrl-a/e))
  13841. refpos)
  13842. (beginning-of-line 1)
  13843. (if (and arg (fboundp 'move-beginning-of-line))
  13844. (call-interactively 'move-beginning-of-line)
  13845. (if (bobp)
  13846. nil
  13847. (backward-char 1)
  13848. (if (org-invisible-p)
  13849. (while (and (not (bobp)) (org-invisible-p))
  13850. (backward-char 1)
  13851. (beginning-of-line 1))
  13852. (forward-char 1))))
  13853. (when special
  13854. (cond
  13855. ((and (looking-at org-complex-heading-regexp)
  13856. (= (char-after (match-end 1)) ?\ ))
  13857. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13858. (point-at-eol)))
  13859. (goto-char
  13860. (if (eq special t)
  13861. (cond ((> pos refpos) refpos)
  13862. ((= pos (point)) refpos)
  13863. (t (point)))
  13864. (cond ((> pos (point)) (point))
  13865. ((not (eq last-command this-command)) (point))
  13866. (t refpos)))))
  13867. ((org-at-item-p)
  13868. (goto-char
  13869. (if (eq special t)
  13870. (cond ((> pos (match-end 4)) (match-end 4))
  13871. ((= pos (point)) (match-end 4))
  13872. (t (point)))
  13873. (cond ((> pos (point)) (point))
  13874. ((not (eq last-command this-command)) (point))
  13875. (t (match-end 4))))))))
  13876. (org-no-warnings
  13877. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13878. (defun org-end-of-line (&optional arg)
  13879. "Go to the end of the line.
  13880. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13881. first attempt, and only move to after the tags when the cursor is already
  13882. beyond the end of the headline."
  13883. (interactive "P")
  13884. (let ((special (if (consp org-special-ctrl-a/e)
  13885. (cdr org-special-ctrl-a/e)
  13886. org-special-ctrl-a/e)))
  13887. (if (or (not special)
  13888. (not (org-on-heading-p))
  13889. arg)
  13890. (call-interactively (if (fboundp 'move-end-of-line)
  13891. 'move-end-of-line
  13892. 'end-of-line))
  13893. (let ((pos (point)))
  13894. (beginning-of-line 1)
  13895. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13896. (if (eq special t)
  13897. (if (or (< pos (match-beginning 1))
  13898. (= pos (match-end 0)))
  13899. (goto-char (match-beginning 1))
  13900. (goto-char (match-end 0)))
  13901. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13902. (goto-char (match-end 0))
  13903. (goto-char (match-beginning 1))))
  13904. (call-interactively (if (fboundp 'move-end-of-line)
  13905. 'move-end-of-line
  13906. 'end-of-line)))))
  13907. (org-no-warnings
  13908. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13909. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13910. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13911. (defun org-kill-line (&optional arg)
  13912. "Kill line, to tags or end of line."
  13913. (interactive "P")
  13914. (cond
  13915. ((or (not org-special-ctrl-k)
  13916. (bolp)
  13917. (not (org-on-heading-p)))
  13918. (call-interactively 'kill-line))
  13919. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13920. (kill-region (point) (match-beginning 1))
  13921. (org-set-tags nil t))
  13922. (t (kill-region (point) (point-at-eol)))))
  13923. (define-key org-mode-map "\C-k" 'org-kill-line)
  13924. (defun org-yank (&optional arg)
  13925. "Yank. If the kill is a subtree, treat it specially.
  13926. This command will look at the current kill and check if is a single
  13927. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13928. cursor is at the beginning of a line or after the stars of a currently
  13929. empty headline, then the yank is handled specially. How exactly depends
  13930. on the value of the following variables, both set by default.
  13931. org-yank-folded-subtrees
  13932. When set, the subtree(s) will be folded after insertion, but only
  13933. if doing so would now swallow text after the yanked text.
  13934. org-yank-adjusted-subtrees
  13935. When set, the subtree will be promoted or demoted in order to
  13936. fit into the local outline tree structure, which means that the level
  13937. will be adjusted so that it becomes the smaller one of the two
  13938. *visible* surrounding headings.
  13939. Any prefix to this command will cause `yank' to be called directly with
  13940. no special treatment. In particular, a simple `C-u' prefix will just
  13941. plainly yank the text as it is.
  13942. \[1] Basically, the test checks if the first non-white line is a heading
  13943. and if there are no other headings with fewer stars."
  13944. (interactive "P")
  13945. (setq this-command 'yank)
  13946. (if arg
  13947. (call-interactively 'yank)
  13948. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13949. (and (org-kill-is-subtree-p)
  13950. (or (bolp)
  13951. (and (looking-at "[ \t]*$")
  13952. (string-match
  13953. "\\`\\*+\\'"
  13954. (buffer-substring (point-at-bol) (point)))))))
  13955. swallowp)
  13956. (cond
  13957. ((and subtreep org-yank-folded-subtrees)
  13958. (let ((beg (point))
  13959. end)
  13960. (if (and subtreep org-yank-adjusted-subtrees)
  13961. (org-paste-subtree nil nil 'for-yank)
  13962. (call-interactively 'yank))
  13963. (setq end (point))
  13964. (goto-char beg)
  13965. (when (and (bolp) subtreep
  13966. (not (setq swallowp
  13967. (org-yank-folding-would-swallow-text beg end))))
  13968. (or (looking-at outline-regexp)
  13969. (re-search-forward (concat "^" outline-regexp) end t))
  13970. (while (and (< (point) end) (looking-at outline-regexp))
  13971. (hide-subtree)
  13972. (org-cycle-show-empty-lines 'folded)
  13973. (condition-case nil
  13974. (outline-forward-same-level 1)
  13975. (error (goto-char end)))))
  13976. (when swallowp
  13977. (message
  13978. "Yanked text not folded because that would swallow text"))
  13979. (goto-char end)
  13980. (skip-chars-forward " \t\n\r")
  13981. (beginning-of-line 1)
  13982. (push-mark beg 'nomsg)))
  13983. ((and subtreep org-yank-adjusted-subtrees)
  13984. (let ((beg (point-at-bol)))
  13985. (org-paste-subtree nil nil 'for-yank)
  13986. (push-mark beg 'nomsg)))
  13987. (t
  13988. (call-interactively 'yank))))))
  13989. (defun org-yank-folding-would-swallow-text (beg end)
  13990. "Would hide-subtree at BEG swallow any text after END?"
  13991. (let (level)
  13992. (save-excursion
  13993. (goto-char beg)
  13994. (when (or (looking-at outline-regexp)
  13995. (re-search-forward (concat "^" outline-regexp) end t))
  13996. (setq level (org-outline-level)))
  13997. (goto-char end)
  13998. (skip-chars-forward " \t\r\n\v\f")
  13999. (if (or (eobp)
  14000. (and (bolp) (looking-at org-outline-regexp)
  14001. (<= (org-outline-level) level)))
  14002. nil ; Nothing would be swallowed
  14003. t)))) ; something would swallow
  14004. (define-key org-mode-map "\C-y" 'org-yank)
  14005. (defun org-invisible-p ()
  14006. "Check if point is at a character currently not visible."
  14007. ;; Early versions of noutline don't have `outline-invisible-p'.
  14008. (if (fboundp 'outline-invisible-p)
  14009. (outline-invisible-p)
  14010. (get-char-property (point) 'invisible)))
  14011. (defun org-invisible-p2 ()
  14012. "Check if point is at a character currently not visible."
  14013. (save-excursion
  14014. (if (and (eolp) (not (bobp))) (backward-char 1))
  14015. ;; Early versions of noutline don't have `outline-invisible-p'.
  14016. (if (fboundp 'outline-invisible-p)
  14017. (outline-invisible-p)
  14018. (get-char-property (point) 'invisible))))
  14019. (defun org-back-to-heading (&optional invisible-ok)
  14020. "Call `outline-back-to-heading', but provide a better error message."
  14021. (condition-case nil
  14022. (outline-back-to-heading invisible-ok)
  14023. (error (error "Before first headline at position %d in buffer %s"
  14024. (point) (current-buffer)))))
  14025. (defun org-before-first-heading-p ()
  14026. "Before first heading?"
  14027. (save-excursion
  14028. (null (re-search-backward "^\\*+ " nil t))))
  14029. (defalias 'org-on-heading-p 'outline-on-heading-p)
  14030. (defalias 'org-at-heading-p 'outline-on-heading-p)
  14031. (defun org-at-heading-or-item-p ()
  14032. (or (org-on-heading-p) (org-at-item-p)))
  14033. (defun org-on-target-p ()
  14034. (or (org-in-regexp org-radio-target-regexp)
  14035. (org-in-regexp org-target-regexp)))
  14036. (defun org-up-heading-all (arg)
  14037. "Move to the heading line of which the present line is a subheading.
  14038. This function considers both visible and invisible heading lines.
  14039. With argument, move up ARG levels."
  14040. (if (fboundp 'outline-up-heading-all)
  14041. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  14042. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  14043. (defun org-up-heading-safe ()
  14044. "Move to the heading line of which the present line is a subheading.
  14045. This version will not throw an error. It will return the level of the
  14046. headline found, or nil if no higher level is found."
  14047. (let (start-level re)
  14048. (org-back-to-heading t)
  14049. (setq start-level (funcall outline-level))
  14050. (if (equal start-level 1)
  14051. nil
  14052. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  14053. (if (re-search-backward re nil t)
  14054. (funcall outline-level)))))
  14055. (defun org-first-sibling-p ()
  14056. "Is this heading the first child of its parents?"
  14057. (interactive)
  14058. (let ((re (concat "^" outline-regexp))
  14059. level l)
  14060. (unless (org-at-heading-p t)
  14061. (error "Not at a heading"))
  14062. (setq level (funcall outline-level))
  14063. (save-excursion
  14064. (if (not (re-search-backward re nil t))
  14065. t
  14066. (setq l (funcall outline-level))
  14067. (< l level)))))
  14068. (defun org-goto-sibling (&optional previous)
  14069. "Goto the next sibling, even if it is invisible.
  14070. When PREVIOUS is set, go to the previous sibling instead. Returns t
  14071. when a sibling was found. When none is found, return nil and don't
  14072. move point."
  14073. (let ((fun (if previous 're-search-backward 're-search-forward))
  14074. (pos (point))
  14075. (re (concat "^" outline-regexp))
  14076. level l)
  14077. (when (condition-case nil (org-back-to-heading t) (error nil))
  14078. (setq level (funcall outline-level))
  14079. (catch 'exit
  14080. (or previous (forward-char 1))
  14081. (while (funcall fun re nil t)
  14082. (setq l (funcall outline-level))
  14083. (when (< l level) (goto-char pos) (throw 'exit nil))
  14084. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  14085. (goto-char pos)
  14086. nil))))
  14087. (defun org-show-siblings ()
  14088. "Show all siblings of the current headline."
  14089. (save-excursion
  14090. (while (org-goto-sibling) (org-flag-heading nil)))
  14091. (save-excursion
  14092. (while (org-goto-sibling 'previous)
  14093. (org-flag-heading nil))))
  14094. (defun org-show-hidden-entry ()
  14095. "Show an entry where even the heading is hidden."
  14096. (save-excursion
  14097. (org-show-entry)))
  14098. (defun org-flag-heading (flag &optional entry)
  14099. "Flag the current heading. FLAG non-nil means make invisible.
  14100. When ENTRY is non-nil, show the entire entry."
  14101. (save-excursion
  14102. (org-back-to-heading t)
  14103. ;; Check if we should show the entire entry
  14104. (if entry
  14105. (progn
  14106. (org-show-entry)
  14107. (save-excursion
  14108. (and (outline-next-heading)
  14109. (org-flag-heading nil))))
  14110. (outline-flag-region (max (point-min) (1- (point)))
  14111. (save-excursion (outline-end-of-heading) (point))
  14112. flag))))
  14113. (defun org-forward-same-level (arg)
  14114. "Move forward to the ARG'th subheading at same level as this one.
  14115. Stop at the first and last subheadings of a superior heading.
  14116. This is like outline-forward-same-level, but invisible headings are ok."
  14117. (interactive "p")
  14118. (org-back-to-heading t)
  14119. (while (> arg 0)
  14120. (let ((point-to-move-to (save-excursion
  14121. (org-get-next-sibling))))
  14122. (if point-to-move-to
  14123. (progn
  14124. (goto-char point-to-move-to)
  14125. (setq arg (1- arg)))
  14126. (progn
  14127. (setq arg 0)
  14128. (error "No following same-level heading"))))))
  14129. (defun org-get-next-sibling ()
  14130. "Move to next heading of the same level, and return point.
  14131. If there is no such heading, return nil.
  14132. This is like outline-next-sibling, but invisible headings are ok."
  14133. (let ((level (funcall outline-level)))
  14134. (outline-next-heading)
  14135. (while (and (not (eobp)) (> (funcall outline-level) level))
  14136. (outline-next-heading))
  14137. (if (or (eobp) (< (funcall outline-level) level))
  14138. nil
  14139. (point))))
  14140. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  14141. ;; This is an exact copy of the original function, but it uses
  14142. ;; `org-back-to-heading', to make it work also in invisible
  14143. ;; trees. And is uses an invisible-OK argument.
  14144. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  14145. (org-back-to-heading invisible-OK)
  14146. (let ((first t)
  14147. (level (funcall outline-level)))
  14148. (while (and (not (eobp))
  14149. (or first (> (funcall outline-level) level)))
  14150. (setq first nil)
  14151. (outline-next-heading))
  14152. (unless to-heading
  14153. (if (memq (preceding-char) '(?\n ?\^M))
  14154. (progn
  14155. ;; Go to end of line before heading
  14156. (forward-char -1)
  14157. (if (memq (preceding-char) '(?\n ?\^M))
  14158. ;; leave blank line before heading
  14159. (forward-char -1))))))
  14160. (point))
  14161. (defun org-show-subtree ()
  14162. "Show everything after this heading at deeper levels."
  14163. (outline-flag-region
  14164. (point)
  14165. (save-excursion
  14166. (outline-end-of-subtree) (outline-next-heading) (point))
  14167. nil))
  14168. (defun org-show-entry ()
  14169. "Show the body directly following this heading.
  14170. Show the heading too, if it is currently invisible."
  14171. (interactive)
  14172. (save-excursion
  14173. (condition-case nil
  14174. (progn
  14175. (org-back-to-heading t)
  14176. (outline-flag-region
  14177. (max (point-min) (1- (point)))
  14178. (save-excursion
  14179. (if (re-search-forward
  14180. (concat "[\r\n]\\(" outline-regexp "\\)") nil t)
  14181. (match-beginning 1)
  14182. (point-max)))
  14183. nil))
  14184. (error nil))))
  14185. (defun org-make-options-regexp (kwds &optional extra)
  14186. "Make a regular expression for keyword lines."
  14187. (concat
  14188. "^"
  14189. "#?[ \t]*\\+\\("
  14190. (mapconcat 'regexp-quote kwds "\\|")
  14191. (if extra (concat "\\|" extra))
  14192. "\\):[ \t]*"
  14193. "\\(.+\\)"))
  14194. ;; Make isearch reveal the necessary context
  14195. (defun org-isearch-end ()
  14196. "Reveal context after isearch exits."
  14197. (when isearch-success ; only if search was successful
  14198. (if (featurep 'xemacs)
  14199. ;; Under XEmacs, the hook is run in the correct place,
  14200. ;; we directly show the context.
  14201. (org-show-context 'isearch)
  14202. ;; In Emacs the hook runs *before* restoring the overlays.
  14203. ;; So we have to use a one-time post-command-hook to do this.
  14204. ;; (Emacs 22 has a special variable, see function `org-mode')
  14205. (unless (and (boundp 'isearch-mode-end-hook-quit)
  14206. isearch-mode-end-hook-quit)
  14207. ;; Only when the isearch was not quitted.
  14208. (org-add-hook 'post-command-hook 'org-isearch-post-command
  14209. 'append 'local)))))
  14210. (defun org-isearch-post-command ()
  14211. "Remove self from hook, and show context."
  14212. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  14213. (org-show-context 'isearch))
  14214. ;;;; Integration with and fixes for other packages
  14215. ;;; Imenu support
  14216. (defvar org-imenu-markers nil
  14217. "All markers currently used by Imenu.")
  14218. (make-variable-buffer-local 'org-imenu-markers)
  14219. (defun org-imenu-new-marker (&optional pos)
  14220. "Return a new marker for use by Imenu, and remember the marker."
  14221. (let ((m (make-marker)))
  14222. (move-marker m (or pos (point)))
  14223. (push m org-imenu-markers)
  14224. m))
  14225. (defun org-imenu-get-tree ()
  14226. "Produce the index for Imenu."
  14227. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  14228. (setq org-imenu-markers nil)
  14229. (let* ((n org-imenu-depth)
  14230. (re (concat "^" outline-regexp))
  14231. (subs (make-vector (1+ n) nil))
  14232. (last-level 0)
  14233. m level head)
  14234. (save-excursion
  14235. (save-restriction
  14236. (widen)
  14237. (goto-char (point-max))
  14238. (while (re-search-backward re nil t)
  14239. (setq level (org-reduced-level (funcall outline-level)))
  14240. (when (<= level n)
  14241. (looking-at org-complex-heading-regexp)
  14242. (setq head (org-link-display-format
  14243. (org-match-string-no-properties 4))
  14244. m (org-imenu-new-marker))
  14245. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  14246. (if (>= level last-level)
  14247. (push (cons head m) (aref subs level))
  14248. (push (cons head (aref subs (1+ level))) (aref subs level))
  14249. (loop for i from (1+ level) to n do (aset subs i nil)))
  14250. (setq last-level level)))))
  14251. (aref subs 1)))
  14252. (eval-after-load "imenu"
  14253. '(progn
  14254. (add-hook 'imenu-after-jump-hook
  14255. (lambda ()
  14256. (if (eq major-mode 'org-mode)
  14257. (org-show-context 'org-goto))))))
  14258. (defun org-link-display-format (link)
  14259. "Replace a link with either the description, or the link target
  14260. if no description is present"
  14261. (save-match-data
  14262. (if (string-match org-bracket-link-analytic-regexp link)
  14263. (replace-match (or (match-string 5 link)
  14264. (concat (match-string 1 link)
  14265. (match-string 3 link)))
  14266. nil nil link)
  14267. link)))
  14268. ;; Speedbar support
  14269. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  14270. "Overlay marking the agenda restriction line in speedbar.")
  14271. (org-overlay-put org-speedbar-restriction-lock-overlay
  14272. 'face 'org-agenda-restriction-lock)
  14273. (org-overlay-put org-speedbar-restriction-lock-overlay
  14274. 'help-echo "Agendas are currently limited to this item.")
  14275. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  14276. (defun org-speedbar-set-agenda-restriction ()
  14277. "Restrict future agenda commands to the location at point in speedbar.
  14278. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  14279. (interactive)
  14280. (require 'org-agenda)
  14281. (let (p m tp np dir txt)
  14282. (cond
  14283. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14284. 'org-imenu t))
  14285. (setq m (get-text-property p 'org-imenu-marker))
  14286. (save-excursion
  14287. (save-restriction
  14288. (set-buffer (marker-buffer m))
  14289. (goto-char m)
  14290. (org-agenda-set-restriction-lock 'subtree))))
  14291. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  14292. 'speedbar-function 'speedbar-find-file))
  14293. (setq tp (previous-single-property-change
  14294. (1+ p) 'speedbar-function)
  14295. np (next-single-property-change
  14296. tp 'speedbar-function)
  14297. dir (speedbar-line-directory)
  14298. txt (buffer-substring-no-properties (or tp (point-min))
  14299. (or np (point-max))))
  14300. (save-excursion
  14301. (save-restriction
  14302. (set-buffer (find-file-noselect
  14303. (let ((default-directory dir))
  14304. (expand-file-name txt))))
  14305. (unless (org-mode-p)
  14306. (error "Cannot restrict to non-Org-mode file"))
  14307. (org-agenda-set-restriction-lock 'file))))
  14308. (t (error "Don't know how to restrict Org-mode's agenda")))
  14309. (org-move-overlay org-speedbar-restriction-lock-overlay
  14310. (point-at-bol) (point-at-eol))
  14311. (setq current-prefix-arg nil)
  14312. (org-agenda-maybe-redo)))
  14313. (eval-after-load "speedbar"
  14314. '(progn
  14315. (speedbar-add-supported-extension ".org")
  14316. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  14317. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  14318. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  14319. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  14320. (add-hook 'speedbar-visiting-tag-hook
  14321. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  14322. ;;; Fixes and Hacks for problems with other packages
  14323. ;; Make flyspell not check words in links, to not mess up our keymap
  14324. (defun org-mode-flyspell-verify ()
  14325. "Don't let flyspell put overlays at active buttons."
  14326. (not (get-text-property (point) 'keymap)))
  14327. ;; Make `bookmark-jump' show the jump location if it was hidden.
  14328. (eval-after-load "bookmark"
  14329. '(if (boundp 'bookmark-after-jump-hook)
  14330. ;; We can use the hook
  14331. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  14332. ;; Hook not available, use advice
  14333. (defadvice bookmark-jump (after org-make-visible activate)
  14334. "Make the position visible."
  14335. (org-bookmark-jump-unhide))))
  14336. ;; Make sure saveplace show the location if it was hidden
  14337. (eval-after-load "saveplace"
  14338. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  14339. "Make the position visible."
  14340. (org-bookmark-jump-unhide)))
  14341. (defun org-bookmark-jump-unhide ()
  14342. "Unhide the current position, to show the bookmark location."
  14343. (and (org-mode-p)
  14344. (or (org-invisible-p)
  14345. (save-excursion (goto-char (max (point-min) (1- (point))))
  14346. (org-invisible-p)))
  14347. (org-show-context 'bookmark-jump)))
  14348. ;; Make session.el ignore our circular variable
  14349. (eval-after-load "session"
  14350. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  14351. ;;;; Experimental code
  14352. (defun org-closed-in-range ()
  14353. "Sparse tree of items closed in a certain time range.
  14354. Still experimental, may disappear in the future."
  14355. (interactive)
  14356. ;; Get the time interval from the user.
  14357. (let* ((time1 (time-to-seconds
  14358. (org-read-date nil 'to-time nil "Starting date: ")))
  14359. (time2 (time-to-seconds
  14360. (org-read-date nil 'to-time nil "End date:")))
  14361. ;; callback function
  14362. (callback (lambda ()
  14363. (let ((time
  14364. (time-to-seconds
  14365. (apply 'encode-time
  14366. (org-parse-time-string
  14367. (match-string 1))))))
  14368. ;; check if time in interval
  14369. (and (>= time time1) (<= time time2))))))
  14370. ;; make tree, check each match with the callback
  14371. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  14372. ;;;; Finish up
  14373. (provide 'org)
  14374. (run-hooks 'org-load-hook)
  14375. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  14376. ;;; org.el ends here